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"
25 #include <unordered_map>
28 #include <android-base/file.h>
29 #include <android-base/logging.h>
31 #include "event_attr.h"
32 #include "perf_event.h"
36 using namespace PerfFileFormat;
38 std::unique_ptr<RecordFileWriter> RecordFileWriter::CreateInstance(const std::string& filename) {
39 // Remove old perf.data to avoid file ownership problems.
41 if (!android::base::RemoveFileIfExists(filename, &err)) {
42 LOG(ERROR) << "failed to remove file " << filename << ": " << err;
45 FILE* fp = fopen(filename.c_str(), "web+");
47 PLOG(ERROR) << "failed to open record file '" << filename << "'";
51 return std::unique_ptr<RecordFileWriter>(new RecordFileWriter(filename, fp));
54 RecordFileWriter::RecordFileWriter(const std::string& filename, FILE* fp)
55 : filename_(filename),
57 attr_section_offset_(0),
58 attr_section_size_(0),
59 data_section_offset_(0),
60 data_section_size_(0),
61 feature_section_offset_(0),
65 RecordFileWriter::~RecordFileWriter() {
66 if (record_fp_ != nullptr) {
71 bool RecordFileWriter::WriteAttrSection(const std::vector<EventAttrWithId>& attr_ids) {
72 if (attr_ids.empty()) {
76 // Skip file header part.
77 if (fseek(record_fp_, sizeof(FileHeader), SEEK_SET) == -1) {
82 uint64_t id_section_offset;
83 if (!GetFilePos(&id_section_offset)) {
86 for (auto& attr_id : attr_ids) {
87 if (!Write(attr_id.ids.data(), attr_id.ids.size() * sizeof(uint64_t))) {
92 // Write attr section.
93 uint64_t attr_section_offset;
94 if (!GetFilePos(&attr_section_offset)) {
97 for (auto& attr_id : attr_ids) {
99 file_attr.attr = *attr_id.attr;
100 file_attr.ids.offset = id_section_offset;
101 file_attr.ids.size = attr_id.ids.size() * sizeof(uint64_t);
102 id_section_offset += file_attr.ids.size;
103 if (!Write(&file_attr, sizeof(file_attr))) {
108 uint64_t data_section_offset;
109 if (!GetFilePos(&data_section_offset)) {
113 attr_section_offset_ = attr_section_offset;
114 attr_section_size_ = data_section_offset - attr_section_offset;
115 data_section_offset_ = data_section_offset;
117 // Save event_attr for use when reading records.
118 event_attr_ = *attr_ids[0].attr;
122 bool RecordFileWriter::WriteRecord(const Record& record) {
123 // linux-tools-perf only accepts records with size <= 65535 bytes. To make
124 // perf.data generated by simpleperf be able to be parsed by linux-tools-perf,
125 // Split simpleperf custom records which are > 65535 into a bunch of
126 // RECORD_SPLIT records, followed by a RECORD_SPLIT_END record.
127 constexpr uint32_t RECORD_SIZE_LIMIT = 65535;
128 if (record.size() <= RECORD_SIZE_LIMIT) {
129 WriteData(record.Binary(), record.size());
132 CHECK_GT(record.type(), SIMPLE_PERF_RECORD_TYPE_START);
133 const char* p = record.Binary();
134 uint32_t left_bytes = static_cast<uint32_t>(record.size());
136 header.type = SIMPLE_PERF_RECORD_SPLIT;
137 char header_buf[Record::header_size()];
139 while (left_bytes > 0) {
140 uint32_t bytes_to_write = std::min(RECORD_SIZE_LIMIT - Record::header_size(), left_bytes);
141 header.size = bytes_to_write + Record::header_size();
142 header_p = header_buf;
143 header.MoveToBinaryFormat(header_p);
144 if (!WriteData(header_buf, Record::header_size())) {
147 if (!WriteData(p, bytes_to_write)) {
151 left_bytes -= bytes_to_write;
153 header.type = SIMPLE_PERF_RECORD_SPLIT_END;
154 header.size = Record::header_size();
155 header_p = header_buf;
156 header.MoveToBinaryFormat(header_p);
157 return WriteData(header_buf, Record::header_size());
160 bool RecordFileWriter::WriteData(const void* buf, size_t len) {
161 if (!Write(buf, len)) {
164 data_section_size_ += len;
168 bool RecordFileWriter::Write(const void* buf, size_t len) {
169 if (fwrite(buf, len, 1, record_fp_) != 1) {
170 PLOG(ERROR) << "failed to write to record file '" << filename_ << "'";
176 bool RecordFileWriter::Read(void* buf, size_t len) {
177 if (len != 0u && fread(buf, len, 1, record_fp_) != 1) {
178 PLOG(ERROR) << "failed to read record file '" << filename_ << "'";
184 bool RecordFileWriter::ReadDataSection(const std::function<void(const Record*)>& callback) {
185 if (fseek(record_fp_, data_section_offset_, SEEK_SET) == -1) {
186 PLOG(ERROR) << "fseek() failed";
189 std::vector<char> record_buf(512);
190 uint64_t read_pos = 0;
191 while (read_pos < data_section_size_) {
192 if (!Read(record_buf.data(), Record::header_size())) {
195 RecordHeader header(record_buf.data());
196 if (record_buf.size() < header.size) {
197 record_buf.resize(header.size);
199 if (!Read(record_buf.data() + Record::header_size(), header.size - Record::header_size())) {
202 read_pos += header.size;
203 std::unique_ptr<Record> r = ReadRecordFromBuffer(event_attr_, header.type, record_buf.data());
209 bool RecordFileWriter::GetFilePos(uint64_t* file_pos) {
210 off_t offset = ftello(record_fp_);
212 PLOG(ERROR) << "ftello() failed";
215 *file_pos = static_cast<uint64_t>(offset);
219 bool RecordFileWriter::BeginWriteFeatures(size_t feature_count) {
220 feature_section_offset_ = data_section_offset_ + data_section_size_;
221 feature_count_ = feature_count;
222 uint64_t feature_header_size = feature_count * sizeof(SectionDesc);
224 // Reserve enough space in the record file for the feature header.
225 std::vector<unsigned char> zero_data(feature_header_size);
226 if (fseek(record_fp_, feature_section_offset_, SEEK_SET) == -1) {
227 PLOG(ERROR) << "fseek() failed";
230 return Write(zero_data.data(), zero_data.size());
233 bool RecordFileWriter::WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records) {
234 if (!WriteFeatureBegin(FEAT_BUILD_ID)) {
237 for (auto& record : build_id_records) {
238 if (!Write(record.Binary(), record.size())) {
242 return WriteFeatureEnd(FEAT_BUILD_ID);
245 bool RecordFileWriter::WriteStringWithLength(const std::string& s) {
246 uint32_t len = static_cast<uint32_t>(Align(s.size() + 1, 64));
247 if (!Write(&len, sizeof(len))) {
250 if (!Write(&s[0], s.size() + 1)) {
253 size_t pad_size = Align(s.size() + 1, 64) - s.size() - 1;
255 char align_buf[pad_size];
256 memset(align_buf, '\0', pad_size);
257 if (!Write(align_buf, pad_size)) {
264 bool RecordFileWriter::WriteFeatureString(int feature, const std::string& s) {
265 if (!WriteFeatureBegin(feature)) {
268 if (!WriteStringWithLength(s)) {
271 return WriteFeatureEnd(feature);
274 bool RecordFileWriter::WriteCmdlineFeature(const std::vector<std::string>& cmdline) {
275 if (!WriteFeatureBegin(FEAT_CMDLINE)) {
278 uint32_t arg_count = cmdline.size();
279 if (!Write(&arg_count, sizeof(arg_count))) {
282 for (auto& arg : cmdline) {
283 if (!WriteStringWithLength(arg)) {
287 return WriteFeatureEnd(FEAT_CMDLINE);
290 bool RecordFileWriter::WriteBranchStackFeature() {
291 if (!WriteFeatureBegin(FEAT_BRANCH_STACK)) {
294 return WriteFeatureEnd(FEAT_BRANCH_STACK);
297 bool RecordFileWriter::WriteFileFeature(const std::string& file_path,
300 const std::vector<const Symbol*>& symbols) {
301 uint32_t size = file_path.size() + 1 + sizeof(uint32_t) * 2 +
302 sizeof(uint64_t) + symbols.size() * (sizeof(uint64_t) + sizeof(uint32_t));
303 for (const auto& symbol : symbols) {
304 size += strlen(symbol->Name()) + 1;
306 std::vector<char> buf(sizeof(uint32_t) + size);
307 char* p = buf.data();
308 MoveToBinaryFormat(size, p);
309 MoveToBinaryFormat(file_path.c_str(), file_path.size() + 1, p);
310 MoveToBinaryFormat(file_type, p);
311 MoveToBinaryFormat(min_vaddr, p);
312 uint32_t symbol_count = static_cast<uint32_t>(symbols.size());
313 MoveToBinaryFormat(symbol_count, p);
314 for (const auto& symbol : symbols) {
315 MoveToBinaryFormat(symbol->addr, p);
316 uint32_t len = symbol->len;
317 MoveToBinaryFormat(len, p);
318 MoveToBinaryFormat(symbol->Name(), strlen(symbol->Name()) + 1, p);
320 CHECK_EQ(buf.size(), static_cast<size_t>(p - buf.data()));
322 if (!WriteFeatureBegin(FEAT_FILE)) {
325 if (!Write(buf.data(), buf.size())) {
328 return WriteFeatureEnd(FEAT_FILE);
331 bool RecordFileWriter::WriteMetaInfoFeature(
332 const std::unordered_map<std::string, std::string>& info_map) {
334 for (auto& pair : info_map) {
335 size += pair.first.size() + 1;
336 size += pair.second.size() + 1;
338 std::vector<char> buf(size);
339 char* p = buf.data();
340 for (auto& pair : info_map) {
341 MoveToBinaryFormat(pair.first.c_str(), pair.first.size() + 1, p);
342 MoveToBinaryFormat(pair.second.c_str(), pair.second.size() + 1, p);
344 if (!WriteFeatureBegin(FEAT_META_INFO)) {
347 if (!Write(buf.data(), buf.size())) {
350 return WriteFeatureEnd(FEAT_META_INFO);
353 bool RecordFileWriter::WriteFeatureBegin(int feature) {
354 auto it = features_.find(feature);
355 if (it == features_.end()) {
356 CHECK_LT(features_.size(), feature_count_);
357 auto& sec = features_[feature];
358 if (!GetFilePos(&sec.offset)) {
366 bool RecordFileWriter::WriteFeatureEnd(int feature) {
367 auto it = features_.find(feature);
368 if (it == features_.end()) {
372 if (!GetFilePos(&offset)) {
375 it->second.size = offset - it->second.offset;
379 bool RecordFileWriter::EndWriteFeatures() {
380 // Used features (features_.size()) should be <= allocated feature space.
381 CHECK_LE(features_.size(), feature_count_);
382 if (fseek(record_fp_, feature_section_offset_, SEEK_SET) == -1) {
383 PLOG(ERROR) << "fseek() failed";
386 for (const auto& pair : features_) {
387 if (!Write(&pair.second, sizeof(SectionDesc))) {
394 bool RecordFileWriter::WriteFileHeader() {
396 memset(&header, 0, sizeof(header));
397 memcpy(header.magic, PERF_MAGIC, sizeof(header.magic));
398 header.header_size = sizeof(header);
399 header.attr_size = sizeof(FileAttr);
400 header.attrs.offset = attr_section_offset_;
401 header.attrs.size = attr_section_size_;
402 header.data.offset = data_section_offset_;
403 header.data.size = data_section_size_;
404 for (const auto& pair : features_) {
405 int i = pair.first / 8;
406 int j = pair.first % 8;
407 header.features[i] |= (1 << j);
410 if (fseek(record_fp_, 0, SEEK_SET) == -1) {
413 if (!Write(&header, sizeof(header))) {
419 bool RecordFileWriter::Close() {
420 CHECK(record_fp_ != nullptr);
423 // Write file header. We gather enough information to write file header only after
424 // writing data section and feature section.
425 if (!WriteFileHeader()) {
429 if (fclose(record_fp_) != 0) {
430 PLOG(ERROR) << "failed to close record file '" << filename_ << "'";
433 record_fp_ = nullptr;