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"
27 #include <android-base/file.h>
28 #include <android-base/logging.h>
30 #include "perf_event.h"
34 using namespace PerfFileFormat;
36 std::unique_ptr<RecordFileWriter> RecordFileWriter::CreateInstance(const std::string& filename) {
37 // Remove old perf.data to avoid file ownership problems.
39 if (!android::base::RemoveFileIfExists(filename, &err)) {
40 LOG(ERROR) << "failed to remove file " << filename << ": " << err;
43 FILE* fp = fopen(filename.c_str(), "web+");
45 PLOG(ERROR) << "failed to open record file '" << filename << "'";
49 return std::unique_ptr<RecordFileWriter>(new RecordFileWriter(filename, fp));
52 RecordFileWriter::RecordFileWriter(const std::string& filename, FILE* fp)
53 : filename_(filename),
55 attr_section_offset_(0),
56 attr_section_size_(0),
57 data_section_offset_(0),
58 data_section_size_(0),
60 current_feature_index_(0) {
63 RecordFileWriter::~RecordFileWriter() {
64 if (record_fp_ != nullptr) {
69 bool RecordFileWriter::WriteAttrSection(const std::vector<AttrWithId>& attr_ids) {
70 if (attr_ids.empty()) {
74 // Skip file header part.
75 if (fseek(record_fp_, sizeof(FileHeader), SEEK_SET) == -1) {
80 long id_section_offset = ftell(record_fp_);
81 if (id_section_offset == -1) {
84 for (auto& attr_id : attr_ids) {
85 if (!Write(attr_id.ids.data(), attr_id.ids.size() * sizeof(uint64_t))) {
90 // Write attr section.
91 long attr_section_offset = ftell(record_fp_);
92 if (attr_section_offset == -1) {
95 for (auto& attr_id : attr_ids) {
97 file_attr.attr = *attr_id.attr;
98 file_attr.ids.offset = id_section_offset;
99 file_attr.ids.size = attr_id.ids.size() * sizeof(uint64_t);
100 id_section_offset += file_attr.ids.size;
101 if (!Write(&file_attr, sizeof(file_attr))) {
106 long data_section_offset = ftell(record_fp_);
107 if (data_section_offset == -1) {
111 attr_section_offset_ = attr_section_offset;
112 attr_section_size_ = data_section_offset - attr_section_offset;
113 data_section_offset_ = data_section_offset;
115 // Save event_attr for use when reading records.
116 event_attr_ = *attr_ids[0].attr;
120 bool RecordFileWriter::WriteData(const void* buf, size_t len) {
121 if (!Write(buf, len)) {
124 data_section_size_ += len;
128 bool RecordFileWriter::Write(const void* buf, size_t len) {
129 if (fwrite(buf, len, 1, record_fp_) != 1) {
130 PLOG(ERROR) << "failed to write to record file '" << filename_ << "'";
136 bool RecordFileWriter::SeekFileEnd(uint64_t* file_end) {
137 if (fseek(record_fp_, 0, SEEK_END) == -1) {
138 PLOG(ERROR) << "fseek() failed";
141 long offset = ftell(record_fp_);
143 PLOG(ERROR) << "ftell() failed";
146 *file_end = static_cast<uint64_t>(offset);
150 bool RecordFileWriter::WriteFeatureHeader(size_t feature_count) {
151 feature_count_ = feature_count;
152 current_feature_index_ = 0;
153 uint64_t feature_header_size = feature_count * sizeof(SectionDesc);
155 // Reserve enough space in the record file for the feature header.
156 std::vector<unsigned char> zero_data(feature_header_size);
157 if (fseek(record_fp_, data_section_offset_ + data_section_size_, SEEK_SET) == -1) {
158 PLOG(ERROR) << "fseek() failed";
161 return Write(zero_data.data(), zero_data.size());
164 bool RecordFileWriter::WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records) {
165 uint64_t start_offset;
166 if (!WriteFeatureBegin(&start_offset)) {
169 for (auto& record : build_id_records) {
170 std::vector<char> data = record.BinaryFormat();
171 if (!Write(data.data(), data.size())) {
175 return WriteFeatureEnd(FEAT_BUILD_ID, start_offset);
178 bool RecordFileWriter::WriteFeatureString(int feature, const std::string& s) {
179 uint64_t start_offset;
180 if (!WriteFeatureBegin(&start_offset)) {
183 uint32_t len = static_cast<uint32_t>(ALIGN(s.size() + 1, 64));
184 if (!Write(&len, sizeof(len))) {
187 std::vector<char> v(len, '\0');
188 std::copy(s.begin(), s.end(), v.begin());
189 if (!Write(v.data(), v.size())) {
192 return WriteFeatureEnd(feature, start_offset);
195 bool RecordFileWriter::WriteCmdlineFeature(const std::vector<std::string>& cmdline) {
196 uint64_t start_offset;
197 if (!WriteFeatureBegin(&start_offset)) {
200 uint32_t arg_count = cmdline.size();
201 if (!Write(&arg_count, sizeof(arg_count))) {
204 for (auto& arg : cmdline) {
205 uint32_t len = static_cast<uint32_t>(ALIGN(arg.size() + 1, 64));
206 if (!Write(&len, sizeof(len))) {
209 std::vector<char> array(len, '\0');
210 std::copy(arg.begin(), arg.end(), array.begin());
211 if (!Write(array.data(), array.size())) {
215 return WriteFeatureEnd(FEAT_CMDLINE, start_offset);
218 bool RecordFileWriter::WriteBranchStackFeature() {
219 uint64_t start_offset;
220 if (!WriteFeatureBegin(&start_offset)) {
223 return WriteFeatureEnd(FEAT_BRANCH_STACK, start_offset);
226 bool RecordFileWriter::WriteFeatureBegin(uint64_t* start_offset) {
227 CHECK_LT(current_feature_index_, feature_count_);
228 if (!SeekFileEnd(start_offset)) {
234 bool RecordFileWriter::WriteFeatureEnd(int feature, uint64_t start_offset) {
236 if (!SeekFileEnd(&end_offset)) {
240 desc.offset = start_offset;
241 desc.size = end_offset - start_offset;
242 uint64_t feature_offset = data_section_offset_ + data_section_size_;
243 if (fseek(record_fp_, feature_offset + current_feature_index_ * sizeof(SectionDesc), SEEK_SET) ==
245 PLOG(ERROR) << "fseek() failed";
248 if (!Write(&desc, sizeof(SectionDesc))) {
251 ++current_feature_index_;
252 features_.push_back(feature);
256 bool RecordFileWriter::WriteFileHeader() {
258 memset(&header, 0, sizeof(header));
259 memcpy(header.magic, PERF_MAGIC, sizeof(header.magic));
260 header.header_size = sizeof(header);
261 header.attr_size = sizeof(FileAttr);
262 header.attrs.offset = attr_section_offset_;
263 header.attrs.size = attr_section_size_;
264 header.data.offset = data_section_offset_;
265 header.data.size = data_section_size_;
266 for (auto& feature : features_) {
269 header.features[i] |= (1 << j);
272 if (fseek(record_fp_, 0, SEEK_SET) == -1) {
275 if (!Write(&header, sizeof(header))) {
281 bool RecordFileWriter::Close() {
282 CHECK(record_fp_ != nullptr);
285 // Write file header. We gather enough information to write file header only after
286 // writing data section and feature section.
287 if (!WriteFileHeader()) {
291 if (fclose(record_fp_) != 0) {
292 PLOG(ERROR) << "failed to close record file '" << filename_ << "'";
295 record_fp_ = nullptr;