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<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::SeekFileEnd(uint64_t* file_end) {
133 if (fseek(record_fp_, 0, SEEK_END) == -1) {
134 PLOG(ERROR) << "fseek() failed";
137 long offset = ftell(record_fp_);
139 PLOG(ERROR) << "ftell() failed";
142 *file_end = static_cast<uint64_t>(offset);
146 bool RecordFileWriter::WriteFeatureHeader(size_t feature_count) {
147 feature_count_ = feature_count;
148 current_feature_index_ = 0;
149 uint64_t feature_header_size = feature_count * sizeof(SectionDesc);
151 // Reserve enough space in the record file for the feature header.
152 std::vector<unsigned char> zero_data(feature_header_size);
153 if (fseek(record_fp_, data_section_offset_ + data_section_size_, SEEK_SET) == -1) {
154 PLOG(ERROR) << "fseek() failed";
157 return Write(zero_data.data(), zero_data.size());
160 bool RecordFileWriter::WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records) {
161 uint64_t start_offset;
162 if (!WriteFeatureBegin(&start_offset)) {
165 for (auto& record : build_id_records) {
166 std::vector<char> data = record.BinaryFormat();
167 if (!Write(data.data(), data.size())) {
171 return WriteFeatureEnd(FEAT_BUILD_ID, start_offset);
174 bool RecordFileWriter::WriteFeatureString(int feature, const std::string& s) {
175 uint64_t start_offset;
176 if (!WriteFeatureBegin(&start_offset)) {
179 uint32_t len = static_cast<uint32_t>(ALIGN(s.size() + 1, 64));
180 if (!Write(&len, sizeof(len))) {
183 std::vector<char> v(len, '\0');
184 std::copy(s.begin(), s.end(), v.begin());
185 if (!Write(v.data(), v.size())) {
188 return WriteFeatureEnd(feature, start_offset);
191 bool RecordFileWriter::WriteCmdlineFeature(const std::vector<std::string>& cmdline) {
192 uint64_t start_offset;
193 if (!WriteFeatureBegin(&start_offset)) {
196 uint32_t arg_count = cmdline.size();
197 if (!Write(&arg_count, sizeof(arg_count))) {
200 for (auto& arg : cmdline) {
201 uint32_t len = static_cast<uint32_t>(ALIGN(arg.size() + 1, 64));
202 if (!Write(&len, sizeof(len))) {
205 std::vector<char> array(len, '\0');
206 std::copy(arg.begin(), arg.end(), array.begin());
207 if (!Write(array.data(), array.size())) {
211 return WriteFeatureEnd(FEAT_CMDLINE, start_offset);
214 bool RecordFileWriter::WriteBranchStackFeature() {
215 uint64_t start_offset;
216 if (!WriteFeatureBegin(&start_offset)) {
219 return WriteFeatureEnd(FEAT_BRANCH_STACK, start_offset);
222 bool RecordFileWriter::WriteFeatureBegin(uint64_t* start_offset) {
223 CHECK_LT(current_feature_index_, feature_count_);
224 if (!SeekFileEnd(start_offset)) {
230 bool RecordFileWriter::WriteFeatureEnd(int feature, uint64_t start_offset) {
232 if (!SeekFileEnd(&end_offset)) {
236 desc.offset = start_offset;
237 desc.size = end_offset - start_offset;
238 uint64_t feature_offset = data_section_offset_ + data_section_size_;
239 if (fseek(record_fp_, feature_offset + current_feature_index_ * sizeof(SectionDesc), SEEK_SET) ==
241 PLOG(ERROR) << "fseek() failed";
244 if (!Write(&desc, sizeof(SectionDesc))) {
247 ++current_feature_index_;
248 features_.push_back(feature);
252 bool RecordFileWriter::WriteFileHeader() {
254 memset(&header, 0, sizeof(header));
255 memcpy(header.magic, PERF_MAGIC, sizeof(header.magic));
256 header.header_size = sizeof(header);
257 header.attr_size = sizeof(FileAttr);
258 header.attrs.offset = attr_section_offset_;
259 header.attrs.size = attr_section_size_;
260 header.data.offset = data_section_offset_;
261 header.data.size = data_section_size_;
262 for (auto& feature : features_) {
265 header.features[i] |= (1 << j);
268 if (fseek(record_fp_, 0, SEEK_SET) == -1) {
271 if (!Write(&header, sizeof(header))) {
277 bool RecordFileWriter::Close() {
278 CHECK(record_fp_ != nullptr);
281 // Write file header. We gather enough information to write file header only after
282 // writing data section and feature section.
283 if (!WriteFileHeader()) {
287 if (fclose(record_fp_) != 0) {
288 PLOG(ERROR) << "failed to close record file '" << filename_ << "'";
291 record_fp_ = nullptr;