2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "record_file.h"
24 #include <android-base/logging.h>
26 #include "event_attr.h"
30 using namespace PerfFileFormat;
32 std::unique_ptr<RecordFileReader> RecordFileReader::CreateInstance(const std::string& filename) {
33 std::string mode = std::string("rb") + CLOSE_ON_EXEC_MODE;
34 FILE* fp = fopen(filename.c_str(), mode.c_str());
36 PLOG(ERROR) << "failed to open record file '" << filename << "'";
39 auto reader = std::unique_ptr<RecordFileReader>(new RecordFileReader(filename, fp));
40 if (!reader->ReadHeader() || !reader->ReadAttrSection() ||
41 !reader->ReadFeatureSectionDescriptors()) {
47 RecordFileReader::RecordFileReader(const std::string& filename, FILE* fp)
48 : filename_(filename), record_fp_(fp), event_id_pos_in_sample_records_(0),
49 event_id_reverse_pos_in_non_sample_records_(0), read_record_size_(0) {
52 RecordFileReader::~RecordFileReader() {
53 if (record_fp_ != nullptr) {
58 bool RecordFileReader::Close() {
60 if (fclose(record_fp_) != 0) {
61 PLOG(ERROR) << "failed to close record file '" << filename_ << "'";
68 bool RecordFileReader::ReadHeader() {
69 return Read(&header_, sizeof(header_));
72 bool RecordFileReader::ReadAttrSection() {
73 size_t attr_count = header_.attrs.size / header_.attr_size;
74 if (header_.attr_size != sizeof(FileAttr)) {
75 LOG(DEBUG) << "attr size (" << header_.attr_size << ") in " << filename_
76 << " doesn't match expected size (" << sizeof(FileAttr) << ")";
78 if (attr_count == 0) {
79 LOG(ERROR) << "no attr in file " << filename_;
82 if (fseek(record_fp_, header_.attrs.offset, SEEK_SET) != 0) {
83 PLOG(ERROR) << "fseek() failed";
86 for (size_t i = 0; i < attr_count; ++i) {
87 std::vector<char> buf(header_.attr_size);
88 if (!Read(buf.data(), buf.size())) {
91 // The size of perf_event_attr is changing between different linux kernel versions.
92 // Make sure we copy correct data to memory.
94 memset(&attr, 0, sizeof(attr));
95 size_t section_desc_size = sizeof(attr.ids);
96 size_t perf_event_attr_size = header_.attr_size - section_desc_size;
97 memcpy(&attr.attr, &buf[0], std::min(sizeof(attr.attr), perf_event_attr_size));
98 memcpy(&attr.ids, &buf[perf_event_attr_size], section_desc_size);
99 file_attrs_.push_back(attr);
101 if (file_attrs_.size() > 1) {
102 std::vector<perf_event_attr> attrs;
103 for (const auto& file_attr : file_attrs_) {
104 attrs.push_back(file_attr.attr);
106 if (!GetCommonEventIdPositionsForAttrs(attrs, &event_id_pos_in_sample_records_,
107 &event_id_reverse_pos_in_non_sample_records_)) {
111 for (size_t i = 0; i < file_attrs_.size(); ++i) {
112 std::vector<uint64_t> ids;
113 if (!ReadIdsForAttr(file_attrs_[i], &ids)) {
116 event_ids_for_file_attrs_.push_back(ids);
117 for (auto id : ids) {
118 event_id_to_attr_map_[id] = i;
124 bool RecordFileReader::ReadFeatureSectionDescriptors() {
125 std::vector<int> features;
126 for (size_t i = 0; i < sizeof(header_.features); ++i) {
127 for (size_t j = 0; j < 8; ++j) {
128 if (header_.features[i] & (1 << j)) {
129 features.push_back(i * 8 + j);
133 uint64_t feature_section_offset = header_.data.offset + header_.data.size;
134 if (fseek(record_fp_, feature_section_offset, SEEK_SET) != 0) {
135 PLOG(ERROR) << "fseek() failed";
138 for (const auto& id : features) {
140 if (!Read(&desc, sizeof(desc))) {
143 feature_section_descriptors_.emplace(id, desc);
148 bool RecordFileReader::ReadIdsForAttr(const FileAttr& attr, std::vector<uint64_t>* ids) {
149 size_t id_count = attr.ids.size / sizeof(uint64_t);
150 if (fseek(record_fp_, attr.ids.offset, SEEK_SET) != 0) {
151 PLOG(ERROR) << "fseek() failed";
154 ids->resize(id_count);
155 if (!Read(ids->data(), attr.ids.size)) {
161 bool RecordFileReader::ReadDataSection(
162 const std::function<bool(std::unique_ptr<Record>)>& callback, bool sorted) {
163 std::unique_ptr<Record> record;
164 while (ReadRecord(record, sorted)) {
165 if (record == nullptr) {
168 if (!callback(std::move(record))) {
175 bool RecordFileReader::ReadRecord(std::unique_ptr<Record>& record,
177 if (read_record_size_ == 0) {
178 if (fseek(record_fp_, header_.data.offset, SEEK_SET) != 0) {
179 PLOG(ERROR) << "fseek() failed";
182 bool has_timestamp = true;
183 for (const auto& attr : file_attrs_) {
184 if (!IsTimestampSupported(attr.attr)) {
185 has_timestamp = false;
189 record_cache_.reset(new RecordCache(has_timestamp));
192 while (read_record_size_ < header_.data.size && record == nullptr) {
193 record = ReadRecord(&read_record_size_);
194 if (record == nullptr) {
197 if (record->type() == SIMPLE_PERF_RECORD_EVENT_ID) {
198 ProcessEventIdRecord(*static_cast<EventIdRecord*>(record.get()));
201 record_cache_->Push(std::move(record));
202 record = record_cache_->Pop();
205 if (record == nullptr) {
206 record = record_cache_->ForcedPop();
211 std::unique_ptr<Record> RecordFileReader::ReadRecord(uint64_t* nbytes_read) {
212 char header_buf[Record::header_size()];
213 if (!Read(header_buf, Record::header_size())) {
216 RecordHeader header(header_buf);
217 std::unique_ptr<char[]> p;
218 if (header.type == SIMPLE_PERF_RECORD_SPLIT) {
219 // Read until meeting a RECORD_SPLIT_END record.
220 std::vector<char> buf;
222 char header_buf[Record::header_size()];
223 while (header.type == SIMPLE_PERF_RECORD_SPLIT) {
224 size_t bytes_to_read = header.size - Record::header_size();
225 buf.resize(cur_size + bytes_to_read);
226 if (!Read(&buf[cur_size], bytes_to_read)) {
229 cur_size += bytes_to_read;
230 *nbytes_read += header.size;
231 if (!Read(header_buf, Record::header_size())) {
234 header = RecordHeader(header_buf);
236 if (header.type != SIMPLE_PERF_RECORD_SPLIT_END) {
237 LOG(ERROR) << "SPLIT records are not followed by a SPLIT_END record.";
240 *nbytes_read += header.size;
241 header = RecordHeader(buf.data());
242 p.reset(new char[header.size]);
243 memcpy(p.get(), buf.data(), buf.size());
245 p.reset(new char[header.size]);
246 memcpy(p.get(), header_buf, Record::header_size());
247 if (header.size > Record::header_size()) {
248 if (!Read(p.get() + Record::header_size(), header.size - Record::header_size())) {
252 *nbytes_read += header.size;
255 const perf_event_attr* attr = &file_attrs_[0].attr;
256 if (file_attrs_.size() > 1 && header.type < PERF_RECORD_USER_DEFINED_TYPE_START) {
257 bool has_event_id = false;
259 if (header.type == PERF_RECORD_SAMPLE) {
260 if (header.size > event_id_pos_in_sample_records_ + sizeof(uint64_t)) {
262 event_id = *reinterpret_cast<uint64_t*>(p.get() + event_id_pos_in_sample_records_);
265 if (header.size > event_id_reverse_pos_in_non_sample_records_) {
267 event_id = *reinterpret_cast<uint64_t*>(p.get() + header.size - event_id_reverse_pos_in_non_sample_records_);
271 auto it = event_id_to_attr_map_.find(event_id);
272 if (it != event_id_to_attr_map_.end()) {
273 attr = &file_attrs_[it->second].attr;
277 return ReadRecordFromOwnedBuffer(*attr, header.type, p.release());
280 bool RecordFileReader::Read(void* buf, size_t len) {
281 if (len != 0 && fread(buf, len, 1, record_fp_) != 1) {
282 PLOG(FATAL) << "failed to read file " << filename_;
288 void RecordFileReader::ProcessEventIdRecord(const EventIdRecord& r) {
289 for (size_t i = 0; i < r.count; ++i) {
290 event_ids_for_file_attrs_[r.data[i].attr_id].push_back(r.data[i].event_id);
291 event_id_to_attr_map_[r.data[i].event_id] = r.data[i].attr_id;
295 size_t RecordFileReader::GetAttrIndexOfRecord(const SampleRecord& record) {
296 auto it = event_id_to_attr_map_.find(record.id_data.id);
297 if (it != event_id_to_attr_map_.end()) {
303 bool RecordFileReader::ReadFeatureSection(int feature, std::vector<char>* data) {
304 const std::map<int, SectionDesc>& section_map = FeatureSectionDescriptors();
305 auto it = section_map.find(feature);
306 if (it == section_map.end()) {
309 SectionDesc section = it->second;
310 data->resize(section.size);
311 if (section.size == 0) {
314 if (fseek(record_fp_, section.offset, SEEK_SET) != 0) {
315 PLOG(ERROR) << "fseek() failed";
318 if (!Read(data->data(), data->size())) {
324 std::vector<std::string> RecordFileReader::ReadCmdlineFeature() {
325 std::vector<char> buf;
326 if (!ReadFeatureSection(FEAT_CMDLINE, &buf)) {
327 return std::vector<std::string>();
329 const char* p = buf.data();
330 const char* end = buf.data() + buf.size();
331 std::vector<std::string> cmdline;
333 MoveFromBinaryFormat(arg_count, p);
335 for (size_t i = 0; i < arg_count; ++i) {
337 MoveFromBinaryFormat(len, p);
338 CHECK_LE(p + len, end);
339 cmdline.push_back(p);
345 std::vector<BuildIdRecord> RecordFileReader::ReadBuildIdFeature() {
346 std::vector<char> buf;
347 if (!ReadFeatureSection(FEAT_BUILD_ID, &buf)) {
348 return std::vector<BuildIdRecord>();
350 const char* p = buf.data();
351 const char* end = buf.data() + buf.size();
352 std::vector<BuildIdRecord> result;
354 BuildIdRecord record(p);
355 // Set type explicitly as the perf.data produced by perf doesn't set it.
356 record.SetTypeAndMisc(PERF_RECORD_BUILD_ID, record.misc());
357 CHECK_LE(p + record.size(), end);
359 result.push_back(std::move(record));
364 std::string RecordFileReader::ReadFeatureString(int feature) {
365 std::vector<char> buf;
366 if (!ReadFeatureSection(feature, &buf)) {
367 return std::string();
369 const char* p = buf.data();
370 const char* end = buf.data() + buf.size();
372 MoveFromBinaryFormat(len, p);
373 CHECK_LE(p + len, end);
377 std::vector<std::unique_ptr<Record>> RecordFileReader::DataSection() {
378 std::vector<std::unique_ptr<Record>> records;
379 ReadDataSection([&](std::unique_ptr<Record> record) {
380 records.push_back(std::move(record));