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 #ifndef SIMPLE_PERF_RECORD_FILE_H_
18 #define SIMPLE_PERF_RECORD_FILE_H_
25 #include <base/macros.h>
27 #include "perf_event.h"
29 #include "record_file_format.h"
33 // RecordFileWriter writes to a perf record file, like perf.data.
34 class RecordFileWriter {
36 static std::unique_ptr<RecordFileWriter> CreateInstance(
37 const std::string& filename, const perf_event_attr& event_attr,
38 const std::vector<std::unique_ptr<EventFd>>& event_fds);
42 bool WriteData(const void* buf, size_t len);
44 bool WriteData(const std::vector<char>& data) {
45 return WriteData(data.data(), data.size());
48 // Use MmapRecords and SampleRecords in record file to conclude which modules/files were executing
50 bool GetHitModules(std::vector<std::string>* hit_kernel_modules,
51 std::vector<std::string>* hit_user_files);
53 bool WriteFeatureHeader(size_t feature_count);
54 bool WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records);
56 // Normally, Close() should be called after writing. But if something
57 // wrong happens and we need to finish in advance, the destructor
58 // will take care of calling Close().
62 RecordFileWriter(const std::string& filename, FILE* fp);
63 bool WriteAttrSection(const perf_event_attr& event_attr,
64 const std::vector<std::unique_ptr<EventFd>>& event_fds);
65 void GetHitModulesInBuffer(const char* p, const char* end,
66 std::vector<std::string>* hit_kernel_modules,
67 std::vector<std::string>* hit_user_files);
68 bool WriteFileHeader();
69 bool Write(const void* buf, size_t len);
71 const std::string filename_;
74 perf_event_attr event_attr_;
75 uint64_t attr_section_offset_;
76 uint64_t attr_section_size_;
77 uint64_t data_section_offset_;
78 uint64_t data_section_size_;
80 std::vector<int> features_;
82 int current_feature_index_;
84 DISALLOW_COPY_AND_ASSIGN(RecordFileWriter);
87 // RecordFileReader read contents from a perf record file, like perf.data.
88 class RecordFileReader {
90 static std::unique_ptr<RecordFileReader> CreateInstance(const std::string& filename);
94 const PerfFileFormat::FileHeader* FileHeader();
95 std::vector<const PerfFileFormat::FileAttr*> AttrSection();
96 std::vector<uint64_t> IdsForAttr(const PerfFileFormat::FileAttr* attr);
97 std::vector<std::unique_ptr<const Record>> DataSection();
98 std::vector<PerfFileFormat::SectionDesc> FeatureSectionDescriptors();
99 const char* DataAtOffset(uint64_t offset) {
100 return mmap_addr_ + offset;
105 RecordFileReader(const std::string& filename, int fd);
108 const std::string filename_;
111 const char* mmap_addr_;
114 DISALLOW_COPY_AND_ASSIGN(RecordFileReader);
117 #endif // SIMPLE_PERF_RECORD_FILE_H_