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>
29 #include "perf_event.h"
33 using namespace PerfFileFormat;
35 std::unique_ptr<RecordFileWriter> RecordFileWriter::CreateInstance(
36 const std::string& filename, const perf_event_attr& event_attr,
37 const std::vector<std::unique_ptr<EventFd>>& event_fds) {
38 // Remove old perf.data to avoid file ownership problems.
39 if (!RemovePossibleFile(filename)) {
42 FILE* fp = fopen(filename.c_str(), "web+");
44 PLOG(ERROR) << "failed to open record file '" << filename << "'";
48 auto writer = std::unique_ptr<RecordFileWriter>(new RecordFileWriter(filename, fp));
49 if (!writer->WriteAttrSection(event_attr, event_fds)) {
55 RecordFileWriter::RecordFileWriter(const std::string& filename, FILE* fp)
56 : filename_(filename),
58 attr_section_offset_(0),
59 attr_section_size_(0),
60 data_section_offset_(0),
61 data_section_size_(0),
63 current_feature_index_(0) {
66 RecordFileWriter::~RecordFileWriter() {
67 if (record_fp_ != nullptr) {
72 bool RecordFileWriter::WriteAttrSection(const perf_event_attr& event_attr,
73 const std::vector<std::unique_ptr<EventFd>>& event_fds) {
74 // Skip file header part.
75 if (fseek(record_fp_, sizeof(FileHeader), SEEK_SET) == -1) {
80 std::vector<uint64_t> ids;
81 for (auto& event_fd : event_fds) {
82 ids.push_back(event_fd->Id());
84 long id_section_offset = ftell(record_fp_);
85 if (id_section_offset == -1) {
88 if (!Write(ids.data(), ids.size() * sizeof(uint64_t))) {
92 // Write attr section.
94 attr.attr = event_attr;
95 attr.ids.offset = id_section_offset;
96 attr.ids.size = ids.size() * sizeof(uint64_t);
98 long attr_section_offset = ftell(record_fp_);
99 if (attr_section_offset == -1) {
102 if (!Write(&attr, sizeof(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_ = sizeof(attr);
113 data_section_offset_ = data_section_offset;
115 // Save event_attr for use when reading records.
116 event_attr_ = event_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 void RecordFileWriter::GetHitModulesInBuffer(const char* p, const char* end,
137 std::vector<std::string>* hit_kernel_modules,
138 std::vector<std::string>* hit_user_files) {
139 std::vector<std::unique_ptr<const Record>> kernel_mmaps;
140 std::vector<std::unique_ptr<const Record>> user_mmaps;
141 std::set<std::string> hit_kernel_set;
142 std::set<std::string> hit_user_set;
145 auto header = reinterpret_cast<const perf_event_header*>(p);
146 CHECK_LE(p + header->size, end);
148 std::unique_ptr<const Record> record = ReadRecordFromBuffer(event_attr_, header);
149 CHECK(record != nullptr);
150 if (record->header.type == PERF_RECORD_MMAP) {
151 if (record->header.misc & PERF_RECORD_MISC_KERNEL) {
152 kernel_mmaps.push_back(std::move(record));
154 user_mmaps.push_back(std::move(record));
156 } else if (record->header.type == PERF_RECORD_SAMPLE) {
157 auto& r = *static_cast<const SampleRecord*>(record.get());
158 if (!(r.sample_type & PERF_SAMPLE_IP) || !(r.sample_type & PERF_SAMPLE_TID)) {
161 uint32_t pid = r.tid_data.pid;
162 uint64_t ip = r.ip_data.ip;
163 if (r.header.misc & PERF_RECORD_MISC_KERNEL) {
164 // Loop from back to front, because new MmapRecords are inserted at the end of the mmaps,
165 // and we want to match the newest one.
166 for (auto it = kernel_mmaps.rbegin(); it != kernel_mmaps.rend(); ++it) {
167 auto& m_record = *reinterpret_cast<const MmapRecord*>(it->get());
168 if (ip >= m_record.data.addr && ip < m_record.data.addr + m_record.data.len) {
169 hit_kernel_set.insert(m_record.filename);
174 for (auto it = user_mmaps.rbegin(); it != user_mmaps.rend(); ++it) {
175 auto& m_record = *reinterpret_cast<const MmapRecord*>(it->get());
176 if (pid == m_record.data.pid && ip >= m_record.data.addr &&
177 ip < m_record.data.addr + m_record.data.len) {
178 hit_user_set.insert(m_record.filename);
185 hit_kernel_modules->clear();
186 hit_kernel_modules->insert(hit_kernel_modules->begin(), hit_kernel_set.begin(),
187 hit_kernel_set.end());
188 hit_user_files->clear();
189 hit_user_files->insert(hit_user_files->begin(), hit_user_set.begin(), hit_user_set.end());
192 bool RecordFileWriter::GetHitModules(std::vector<std::string>* hit_kernel_modules,
193 std::vector<std::string>* hit_user_files) {
194 if (fflush(record_fp_) != 0) {
195 PLOG(ERROR) << "fflush() failed";
199 if (!SeekFileEnd(&file_size)) {
202 size_t mmap_len = static_cast<size_t>(file_size);
203 void* mmap_addr = mmap(nullptr, mmap_len, PROT_READ, MAP_SHARED, fileno(record_fp_), 0);
204 if (mmap_addr == MAP_FAILED) {
205 PLOG(ERROR) << "mmap() failed";
208 const char* data_section_p = reinterpret_cast<const char*>(mmap_addr) + data_section_offset_;
209 const char* data_section_end = data_section_p + data_section_size_;
210 GetHitModulesInBuffer(data_section_p, data_section_end, hit_kernel_modules, hit_user_files);
212 if (munmap(mmap_addr, mmap_len) == -1) {
213 PLOG(ERROR) << "munmap() failed";
219 bool RecordFileWriter::SeekFileEnd(uint64_t* file_end) {
220 if (fseek(record_fp_, 0, SEEK_END) == -1) {
221 PLOG(ERROR) << "fseek() failed";
224 long offset = ftell(record_fp_);
226 PLOG(ERROR) << "ftell() failed";
229 *file_end = static_cast<uint64_t>(offset);
233 bool RecordFileWriter::WriteFeatureHeader(size_t feature_count) {
234 feature_count_ = feature_count;
235 current_feature_index_ = 0;
236 uint64_t feature_header_size = feature_count * sizeof(SectionDesc);
238 // Reserve enough space in the record file for the feature header.
239 std::vector<unsigned char> zero_data(feature_header_size);
240 if (fseek(record_fp_, data_section_offset_ + data_section_size_, SEEK_SET) == -1) {
241 PLOG(ERROR) << "fseek() failed";
244 return Write(zero_data.data(), zero_data.size());
247 bool RecordFileWriter::WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records) {
248 uint64_t start_offset;
249 if (!WriteFeatureBegin(&start_offset)) {
252 for (auto& record : build_id_records) {
253 std::vector<char> data = record.BinaryFormat();
254 if (!Write(data.data(), data.size())) {
258 return WriteFeatureEnd(FEAT_BUILD_ID, start_offset);
261 bool RecordFileWriter::WriteFeatureString(int feature, const std::string& s) {
262 uint64_t start_offset;
263 if (!WriteFeatureBegin(&start_offset)) {
266 uint32_t len = static_cast<uint32_t>(ALIGN(s.size() + 1, 64));
267 if (!Write(&len, sizeof(len))) {
270 std::vector<char> v(len, '\0');
271 std::copy(s.begin(), s.end(), v.begin());
272 if (!Write(v.data(), v.size())) {
275 return WriteFeatureEnd(feature, start_offset);
278 bool RecordFileWriter::WriteCmdlineFeature(const std::vector<std::string>& cmdline) {
279 uint64_t start_offset;
280 if (!WriteFeatureBegin(&start_offset)) {
283 uint32_t arg_count = cmdline.size();
284 if (!Write(&arg_count, sizeof(arg_count))) {
287 for (auto& arg : cmdline) {
288 uint32_t len = static_cast<uint32_t>(ALIGN(arg.size() + 1, 64));
289 if (!Write(&len, sizeof(len))) {
292 std::vector<char> array(len, '\0');
293 std::copy(arg.begin(), arg.end(), array.begin());
294 if (!Write(array.data(), array.size())) {
298 return WriteFeatureEnd(FEAT_CMDLINE, start_offset);
301 bool RecordFileWriter::WriteBranchStackFeature() {
302 uint64_t start_offset;
303 if (!WriteFeatureBegin(&start_offset)) {
306 return WriteFeatureEnd(FEAT_BRANCH_STACK, start_offset);
309 bool RecordFileWriter::WriteFeatureBegin(uint64_t* start_offset) {
310 CHECK_LT(current_feature_index_, feature_count_);
311 if (!SeekFileEnd(start_offset)) {
317 bool RecordFileWriter::WriteFeatureEnd(int feature, uint64_t start_offset) {
319 if (!SeekFileEnd(&end_offset)) {
323 desc.offset = start_offset;
324 desc.size = end_offset - start_offset;
325 uint64_t feature_offset = data_section_offset_ + data_section_size_;
326 if (fseek(record_fp_, feature_offset + current_feature_index_ * sizeof(SectionDesc), SEEK_SET) ==
328 PLOG(ERROR) << "fseek() failed";
331 if (!Write(&desc, sizeof(SectionDesc))) {
334 ++current_feature_index_;
335 features_.push_back(feature);
339 bool RecordFileWriter::WriteFileHeader() {
341 memset(&header, 0, sizeof(header));
342 memcpy(header.magic, PERF_MAGIC, sizeof(header.magic));
343 header.header_size = sizeof(header);
344 header.attr_size = sizeof(FileAttr);
345 header.attrs.offset = attr_section_offset_;
346 header.attrs.size = attr_section_size_;
347 header.data.offset = data_section_offset_;
348 header.data.size = data_section_size_;
349 for (auto& feature : features_) {
352 header.features[i] |= (1 << j);
355 if (fseek(record_fp_, 0, SEEK_SET) == -1) {
358 if (!Write(&header, sizeof(header))) {
364 bool RecordFileWriter::Close() {
365 CHECK(record_fp_ != nullptr);
368 // Write file header. We gather enough information to write file header only after
369 // writing data section and feature section.
370 if (!WriteFileHeader()) {
374 if (fclose(record_fp_) != 0) {
375 PLOG(ERROR) << "failed to close record file '" << filename_ << "'";
378 record_fp_ = nullptr;