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"
26 #include <android-base/logging.h>
28 #include "perf_event.h"
32 using namespace PerfFileFormat;
34 std::unique_ptr<RecordFileReader> RecordFileReader::CreateInstance(const std::string& filename) {
35 FILE* fp = fopen(filename.c_str(), "reb");
37 PLOG(ERROR) << "failed to open record file '" << filename << "'";
40 auto reader = std::unique_ptr<RecordFileReader>(new RecordFileReader(filename, fp));
41 if (!reader->ReadHeader() || !reader->ReadAttrSection() ||
42 !reader->ReadFeatureSectionDescriptors()) {
48 RecordFileReader::RecordFileReader(const std::string& filename, FILE* fp)
49 : filename_(filename), record_fp_(fp) {
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 if (fread(&header_, sizeof(header_), 1, record_fp_) != 1) {
70 PLOG(ERROR) << "failed to read file " << filename_;
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) << ")";
83 if (attr_count == 0) {
84 LOG(ERROR) << "no attr in file " << filename_;
87 if (fseek(record_fp_, header_.attrs.offset, SEEK_SET) != 0) {
88 PLOG(ERROR) << "failed to fseek()";
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_;
97 // The size of perf_event_attr is changing between different linux kernel versions.
98 // Make sure we copy correct data to memory.
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);
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);
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()";
124 for (const auto& id : features) {
126 if (fread(&desc, sizeof(desc), 1, record_fp_) != 1) {
127 PLOG(ERROR) << "failed to read " << filename_;
130 feature_section_descriptors_.emplace(id, desc);
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()";
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_;
149 bool RecordFileReader::ReadDataSection(std::function<bool(std::unique_ptr<Record>)> callback,
151 if (fseek(record_fp_, header_.data.offset, SEEK_SET) != 0) {
152 PLOG(ERROR) << "failed to fseek()";
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) {
161 nbytes_read += record->size();
163 cache.Push(std::move(record));
164 record = cache.Pop();
165 if (record != nullptr) {
166 if (!callback(std::move(record))) {
171 if (!callback(std::move(record))) {
176 std::vector<std::unique_ptr<Record>> records = cache.PopAll();
177 for (auto& record : records) {
178 if (!callback(std::move(record))) {
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()) {
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()";
197 if (fread(data->data(), data->size(), 1, record_fp_) != 1) {
198 PLOG(ERROR) << "failed to read " << filename_;
204 std::vector<std::string> RecordFileReader::ReadCmdlineFeature() {
205 std::vector<char> buf;
206 if (!ReadFeatureSection(FEAT_CMDLINE, &buf)) {
207 return std::vector<std::string>();
209 const char* p = buf.data();
210 const char* end = buf.data() + buf.size();
211 std::vector<std::string> cmdline;
213 MoveFromBinaryFormat(arg_count, p);
215 for (size_t i = 0; i < arg_count; ++i) {
217 MoveFromBinaryFormat(len, p);
218 CHECK_LE(p + len, end);
219 cmdline.push_back(p);
225 std::vector<BuildIdRecord> RecordFileReader::ReadBuildIdFeature() {
226 std::vector<char> buf;
227 if (!ReadFeatureSection(FEAT_BUILD_ID, &buf)) {
228 return std::vector<BuildIdRecord>();
230 const char* p = buf.data();
231 const char* end = buf.data() + buf.size();
232 std::vector<BuildIdRecord> result;
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);
245 std::string RecordFileReader::ReadFeatureString(int feature) {
246 std::vector<char> buf;
247 if (!ReadFeatureSection(feature, &buf)) {
248 return std::string();
250 const char* p = buf.data();
251 const char* end = buf.data() + buf.size();
253 MoveFromBinaryFormat(len, p);
254 CHECK_LE(p + len, end);
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));