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 <base/logging.h>
28 #include "perf_event.h"
32 using namespace PerfFileFormat;
34 std::unique_ptr<RecordFileWriter> RecordFileWriter::CreateInstance(const std::string& filename) {
35 // Remove old perf.data to avoid file ownership problems.
36 if (!RemovePossibleFile(filename)) {
39 FILE* fp = fopen(filename.c_str(), "web+");
41 PLOG(ERROR) << "failed to open record file '" << filename << "'";
45 return std::unique_ptr<RecordFileWriter>(new RecordFileWriter(filename, fp));
48 RecordFileWriter::RecordFileWriter(const std::string& filename, FILE* fp)
49 : filename_(filename),
51 attr_section_offset_(0),
52 attr_section_size_(0),
53 data_section_offset_(0),
54 data_section_size_(0),
56 current_feature_index_(0) {
59 RecordFileWriter::~RecordFileWriter() {
60 if (record_fp_ != nullptr) {
65 bool RecordFileWriter::WriteAttrSection(const std::vector<AttrWithId>& attr_ids) {
66 if (attr_ids.empty()) {
70 // Skip file header part.
71 if (fseek(record_fp_, sizeof(FileHeader), SEEK_SET) == -1) {
76 long id_section_offset = ftell(record_fp_);
77 if (id_section_offset == -1) {
80 for (auto& attr_id : attr_ids) {
81 if (!Write(attr_id.ids.data(), attr_id.ids.size() * sizeof(uint64_t))) {
86 // Write attr section.
87 long attr_section_offset = ftell(record_fp_);
88 if (attr_section_offset == -1) {
91 for (auto& attr_id : attr_ids) {
93 file_attr.attr = *attr_id.attr;
94 file_attr.ids.offset = id_section_offset;
95 file_attr.ids.size = attr_id.ids.size() * sizeof(uint64_t);
96 id_section_offset += file_attr.ids.size;
97 if (!Write(&file_attr, sizeof(file_attr))) {
102 long data_section_offset = ftell(record_fp_);
103 if (data_section_offset == -1) {
107 attr_section_offset_ = attr_section_offset;
108 attr_section_size_ = data_section_offset - attr_section_offset;
109 data_section_offset_ = data_section_offset;
111 // Save event_attr for use when reading records.
112 event_attr_ = *attr_ids[0].attr;
116 bool RecordFileWriter::WriteData(const void* buf, size_t len) {
117 if (!Write(buf, len)) {
120 data_section_size_ += len;
124 bool RecordFileWriter::Write(const void* buf, size_t len) {
125 if (fwrite(buf, len, 1, record_fp_) != 1) {
126 PLOG(ERROR) << "failed to write to record file '" << filename_ << "'";
132 bool RecordFileWriter::ReadDataSection(std::vector<std::unique_ptr<Record>>* records) {
133 if (fflush(record_fp_) != 0) {
134 PLOG(ERROR) << "fflush() failed";
138 if (!SeekFileEnd(&file_size)) {
141 size_t mmap_len = static_cast<size_t>(file_size);
142 void* mmap_addr = mmap(nullptr, mmap_len, PROT_READ, MAP_SHARED, fileno(record_fp_), 0);
143 if (mmap_addr == MAP_FAILED) {
144 PLOG(ERROR) << "mmap() failed";
147 const char* data_section = reinterpret_cast<char*>(mmap_addr) + data_section_offset_;
148 std::vector<std::unique_ptr<Record>> result =
149 ReadRecordsFromBuffer(event_attr_, data_section, data_section_size_);
150 if (munmap(mmap_addr, mmap_len) == -1) {
151 PLOG(ERROR) << "munmap() failed";
154 *records = std::move(result);
158 bool RecordFileWriter::SeekFileEnd(uint64_t* file_end) {
159 if (fseek(record_fp_, 0, SEEK_END) == -1) {
160 PLOG(ERROR) << "fseek() failed";
163 long offset = ftell(record_fp_);
165 PLOG(ERROR) << "ftell() failed";
168 *file_end = static_cast<uint64_t>(offset);
172 bool RecordFileWriter::WriteFeatureHeader(size_t feature_count) {
173 feature_count_ = feature_count;
174 current_feature_index_ = 0;
175 uint64_t feature_header_size = feature_count * sizeof(SectionDesc);
177 // Reserve enough space in the record file for the feature header.
178 std::vector<unsigned char> zero_data(feature_header_size);
179 if (fseek(record_fp_, data_section_offset_ + data_section_size_, SEEK_SET) == -1) {
180 PLOG(ERROR) << "fseek() failed";
183 return Write(zero_data.data(), zero_data.size());
186 bool RecordFileWriter::WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records) {
187 uint64_t start_offset;
188 if (!WriteFeatureBegin(&start_offset)) {
191 for (auto& record : build_id_records) {
192 std::vector<char> data = record.BinaryFormat();
193 if (!Write(data.data(), data.size())) {
197 return WriteFeatureEnd(FEAT_BUILD_ID, start_offset);
200 bool RecordFileWriter::WriteFeatureString(int feature, const std::string& s) {
201 uint64_t start_offset;
202 if (!WriteFeatureBegin(&start_offset)) {
205 uint32_t len = static_cast<uint32_t>(ALIGN(s.size() + 1, 64));
206 if (!Write(&len, sizeof(len))) {
209 std::vector<char> v(len, '\0');
210 std::copy(s.begin(), s.end(), v.begin());
211 if (!Write(v.data(), v.size())) {
214 return WriteFeatureEnd(feature, start_offset);
217 bool RecordFileWriter::WriteCmdlineFeature(const std::vector<std::string>& cmdline) {
218 uint64_t start_offset;
219 if (!WriteFeatureBegin(&start_offset)) {
222 uint32_t arg_count = cmdline.size();
223 if (!Write(&arg_count, sizeof(arg_count))) {
226 for (auto& arg : cmdline) {
227 uint32_t len = static_cast<uint32_t>(ALIGN(arg.size() + 1, 64));
228 if (!Write(&len, sizeof(len))) {
231 std::vector<char> array(len, '\0');
232 std::copy(arg.begin(), arg.end(), array.begin());
233 if (!Write(array.data(), array.size())) {
237 return WriteFeatureEnd(FEAT_CMDLINE, start_offset);
240 bool RecordFileWriter::WriteBranchStackFeature() {
241 uint64_t start_offset;
242 if (!WriteFeatureBegin(&start_offset)) {
245 return WriteFeatureEnd(FEAT_BRANCH_STACK, start_offset);
248 bool RecordFileWriter::WriteFeatureBegin(uint64_t* start_offset) {
249 CHECK_LT(current_feature_index_, feature_count_);
250 if (!SeekFileEnd(start_offset)) {
256 bool RecordFileWriter::WriteFeatureEnd(int feature, uint64_t start_offset) {
258 if (!SeekFileEnd(&end_offset)) {
262 desc.offset = start_offset;
263 desc.size = end_offset - start_offset;
264 uint64_t feature_offset = data_section_offset_ + data_section_size_;
265 if (fseek(record_fp_, feature_offset + current_feature_index_ * sizeof(SectionDesc), SEEK_SET) ==
267 PLOG(ERROR) << "fseek() failed";
270 if (!Write(&desc, sizeof(SectionDesc))) {
273 ++current_feature_index_;
274 features_.push_back(feature);
278 bool RecordFileWriter::WriteFileHeader() {
280 memset(&header, 0, sizeof(header));
281 memcpy(header.magic, PERF_MAGIC, sizeof(header.magic));
282 header.header_size = sizeof(header);
283 header.attr_size = sizeof(FileAttr);
284 header.attrs.offset = attr_section_offset_;
285 header.attrs.size = attr_section_size_;
286 header.data.offset = data_section_offset_;
287 header.data.size = data_section_size_;
288 for (auto& feature : features_) {
291 header.features[i] |= (1 << j);
294 if (fseek(record_fp_, 0, SEEK_SET) == -1) {
297 if (!Write(&header, sizeof(header))) {
303 bool RecordFileWriter::Close() {
304 CHECK(record_fp_ != nullptr);
307 // Write file header. We gather enough information to write file header only after
308 // writing data section and feature section.
309 if (!WriteFileHeader()) {
313 if (fclose(record_fp_) != 0) {
314 PLOG(ERROR) << "failed to close record file '" << filename_ << "'";
317 record_fp_ = nullptr;