OSDN Git Service

Track rename from base/ to android-base/.
[android-x86/system-extras.git] / simpleperf / record_file_reader.cpp
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "record_file.h"
18
19 #include <fcntl.h>
20 #include <string.h>
21 #include <sys/mman.h>
22 #include <unistd.h>
23 #include <set>
24 #include <vector>
25
26 #include <android-base/logging.h>
27
28 #include "perf_event.h"
29 #include "record.h"
30 #include "utils.h"
31
32 using namespace PerfFileFormat;
33
34 std::unique_ptr<RecordFileReader> RecordFileReader::CreateInstance(const std::string& filename) {
35   FILE* fp = fopen(filename.c_str(), "reb");
36   if (fp == nullptr) {
37     PLOG(ERROR) << "failed to open record file '" << filename << "'";
38     return nullptr;
39   }
40   auto reader = std::unique_ptr<RecordFileReader>(new RecordFileReader(filename, fp));
41   if (!reader->ReadHeader() || !reader->ReadAttrSection() ||
42       !reader->ReadFeatureSectionDescriptors()) {
43     return nullptr;
44   }
45   return reader;
46 }
47
48 RecordFileReader::RecordFileReader(const std::string& filename, FILE* fp)
49     : filename_(filename), record_fp_(fp) {
50 }
51
52 RecordFileReader::~RecordFileReader() {
53   if (record_fp_ != nullptr) {
54     Close();
55   }
56 }
57
58 bool RecordFileReader::Close() {
59   bool result = true;
60   if (fclose(record_fp_) != 0) {
61     PLOG(ERROR) << "failed to close record file '" << filename_ << "'";
62     result = false;
63   }
64   record_fp_ = nullptr;
65   return result;
66 }
67
68 bool RecordFileReader::ReadHeader() {
69   if (fread(&header_, sizeof(header_), 1, record_fp_) != 1) {
70     PLOG(ERROR) << "failed to read file " << filename_;
71     return false;
72   }
73   return true;
74 }
75
76 bool RecordFileReader::ReadAttrSection() {
77   size_t attr_count = header_.attrs.size / header_.attr_size;
78   if (header_.attr_size != sizeof(FileAttr)) {
79     LOG(WARNING) << "attr size (" << header_.attr_size << ") in " << filename_
80                  << " doesn't match expected size (" << sizeof(FileAttr) << ")";
81     return false;
82   }
83   if (attr_count == 0) {
84     LOG(ERROR) << "no attr in file " << filename_;
85     return false;
86   }
87   if (fseek(record_fp_, header_.attrs.offset, SEEK_SET) != 0) {
88     PLOG(ERROR) << "failed to fseek()";
89     return false;
90   }
91   for (size_t i = 0; i < attr_count; ++i) {
92     std::vector<char> buf(header_.attr_size);
93     if (fread(&buf[0], buf.size(), 1, record_fp_) != 1) {
94       PLOG(ERROR) << "failed to read " << filename_;
95       return false;
96     }
97     // The size of perf_event_attr is changing between different linux kernel versions.
98     // Make sure we copy correct data to memory.
99     FileAttr attr;
100     memset(&attr, 0, sizeof(attr));
101     size_t section_desc_size = sizeof(attr.ids);
102     size_t perf_event_attr_size = header_.attr_size - section_desc_size;
103     memcpy(&attr.attr, &buf[0], std::min(sizeof(attr.attr), perf_event_attr_size));
104     memcpy(&attr.ids, &buf[perf_event_attr_size], section_desc_size);
105     file_attrs_.push_back(attr);
106   }
107   return true;
108 }
109
110 bool RecordFileReader::ReadFeatureSectionDescriptors() {
111   std::vector<int> features;
112   for (size_t i = 0; i < sizeof(header_.features); ++i) {
113     for (size_t j = 0; j < 8; ++j) {
114       if (header_.features[i] & (1 << j)) {
115         features.push_back(i * 8 + j);
116       }
117     }
118   }
119   uint64_t feature_section_offset = header_.data.offset + header_.data.size;
120   if (fseek(record_fp_, feature_section_offset, SEEK_SET) != 0) {
121     PLOG(ERROR) << "failed to fseek()";
122     return false;
123   }
124   for (const auto& id : features) {
125     SectionDesc desc;
126     if (fread(&desc, sizeof(desc), 1, record_fp_) != 1) {
127       PLOG(ERROR) << "failed to read " << filename_;
128       return false;
129     }
130     feature_section_descriptors_.emplace(id, desc);
131   }
132   return true;
133 }
134
135 bool RecordFileReader::ReadIdsForAttr(const FileAttr& attr, std::vector<uint64_t>* ids) {
136   size_t id_count = attr.ids.size / sizeof(uint64_t);
137   if (fseek(record_fp_, attr.ids.offset, SEEK_SET) != 0) {
138     PLOG(ERROR) << "failed to fseek()";
139     return false;
140   }
141   ids->resize(id_count);
142   if (fread(ids->data(), attr.ids.size, 1, record_fp_) != 1) {
143     PLOG(ERROR) << "failed to read file " << filename_;
144     return false;
145   }
146   return true;
147 }
148
149 bool RecordFileReader::ReadDataSection(std::function<bool(std::unique_ptr<Record>)> callback,
150                                        bool sorted) {
151   if (fseek(record_fp_, header_.data.offset, SEEK_SET) != 0) {
152     PLOG(ERROR) << "failed to fseek()";
153     return false;
154   }
155   RecordCache cache(file_attrs_[0].attr);
156   for (size_t nbytes_read = 0; nbytes_read < header_.data.size;) {
157     std::unique_ptr<Record> record = ReadRecordFromFile(file_attrs_[0].attr, record_fp_);
158     if (record == nullptr) {
159       return false;
160     }
161     nbytes_read += record->size();
162     if (sorted) {
163       cache.Push(std::move(record));
164       record = cache.Pop();
165       if (record != nullptr) {
166         if (!callback(std::move(record))) {
167           return false;
168         }
169       }
170     } else {
171       if (!callback(std::move(record))) {
172         return false;
173       }
174     }
175   }
176   std::vector<std::unique_ptr<Record>> records = cache.PopAll();
177   for (auto& record : records) {
178     if (!callback(std::move(record))) {
179       return false;
180     }
181   }
182   return true;
183 }
184
185 bool RecordFileReader::ReadFeatureSection(int feature, std::vector<char>* data) {
186   const std::map<int, SectionDesc>& section_map = FeatureSectionDescriptors();
187   auto it = section_map.find(feature);
188   if (it == section_map.end()) {
189     return false;
190   }
191   SectionDesc section = it->second;
192   data->resize(section.size);
193   if (fseek(record_fp_, section.offset, SEEK_SET) != 0) {
194     PLOG(ERROR) << "failed to fseek()";
195     return false;
196   }
197   if (fread(data->data(), data->size(), 1, record_fp_) != 1) {
198     PLOG(ERROR) << "failed to read " << filename_;
199     return false;
200   }
201   return true;
202 }
203
204 std::vector<std::string> RecordFileReader::ReadCmdlineFeature() {
205   std::vector<char> buf;
206   if (!ReadFeatureSection(FEAT_CMDLINE, &buf)) {
207     return std::vector<std::string>();
208   }
209   const char* p = buf.data();
210   const char* end = buf.data() + buf.size();
211   std::vector<std::string> cmdline;
212   uint32_t arg_count;
213   MoveFromBinaryFormat(arg_count, p);
214   CHECK_LE(p, end);
215   for (size_t i = 0; i < arg_count; ++i) {
216     uint32_t len;
217     MoveFromBinaryFormat(len, p);
218     CHECK_LE(p + len, end);
219     cmdline.push_back(p);
220     p += len;
221   }
222   return cmdline;
223 }
224
225 std::vector<BuildIdRecord> RecordFileReader::ReadBuildIdFeature() {
226   std::vector<char> buf;
227   if (!ReadFeatureSection(FEAT_BUILD_ID, &buf)) {
228     return std::vector<BuildIdRecord>();
229   }
230   const char* p = buf.data();
231   const char* end = buf.data() + buf.size();
232   std::vector<BuildIdRecord> result;
233   while (p < end) {
234     const perf_event_header* header = reinterpret_cast<const perf_event_header*>(p);
235     CHECK_LE(p + header->size, end);
236     BuildIdRecord record(header);
237     // Set type explicitly as the perf.data produced by perf doesn't set it.
238     record.header.type = PERF_RECORD_BUILD_ID;
239     result.push_back(record);
240     p += header->size;
241   }
242   return result;
243 }
244
245 std::string RecordFileReader::ReadFeatureString(int feature) {
246   std::vector<char> buf;
247   if (!ReadFeatureSection(feature, &buf)) {
248     return std::string();
249   }
250   const char* p = buf.data();
251   const char* end = buf.data() + buf.size();
252   uint32_t len;
253   MoveFromBinaryFormat(len, p);
254   CHECK_LE(p + len, end);
255   return p;
256 }
257
258 std::vector<std::unique_ptr<Record>> RecordFileReader::DataSection() {
259   std::vector<std::unique_ptr<Record>> records;
260   ReadDataSection([&](std::unique_ptr<Record> record) {
261     records.push_back(std::move(record));
262     return true;
263   });
264   return records;
265 }