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_
28 #include <base/macros.h>
30 #include "perf_event.h"
32 #include "record_file_format.h"
35 const perf_event_attr* attr;
36 std::vector<uint64_t> ids;
39 // RecordFileWriter writes to a perf record file, like perf.data.
40 class RecordFileWriter {
42 static std::unique_ptr<RecordFileWriter> CreateInstance(const std::string& filename);
46 bool WriteAttrSection(const std::vector<AttrWithId>& attr_ids);
47 bool WriteData(const void* buf, size_t len);
49 bool WriteData(const std::vector<char>& data) {
50 return WriteData(data.data(), data.size());
53 bool WriteFeatureHeader(size_t feature_count);
54 bool WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records);
55 bool WriteFeatureString(int feature, const std::string& s);
56 bool WriteCmdlineFeature(const std::vector<std::string>& cmdline);
57 bool WriteBranchStackFeature();
59 // Normally, Close() should be called after writing. But if something
60 // wrong happens and we need to finish in advance, the destructor
61 // will take care of calling Close().
65 RecordFileWriter(const std::string& filename, FILE* fp);
66 void GetHitModulesInBuffer(const char* p, const char* end,
67 std::vector<std::string>* hit_kernel_modules,
68 std::vector<std::string>* hit_user_files);
69 bool WriteFileHeader();
70 bool Write(const void* buf, size_t len);
71 bool SeekFileEnd(uint64_t* file_end);
72 bool WriteFeatureBegin(uint64_t* start_offset);
73 bool WriteFeatureEnd(int feature, uint64_t start_offset);
75 const std::string filename_;
78 perf_event_attr event_attr_;
79 uint64_t attr_section_offset_;
80 uint64_t attr_section_size_;
81 uint64_t data_section_offset_;
82 uint64_t data_section_size_;
84 std::vector<int> features_;
86 int current_feature_index_;
88 DISALLOW_COPY_AND_ASSIGN(RecordFileWriter);
91 // RecordFileReader read contents from a perf record file, like perf.data.
92 class RecordFileReader {
94 static std::unique_ptr<RecordFileReader> CreateInstance(const std::string& filename);
98 const PerfFileFormat::FileHeader& FileHeader() const {
102 const std::vector<PerfFileFormat::FileAttr>& AttrSection() const {
106 const std::map<int, PerfFileFormat::SectionDesc>& FeatureSectionDescriptors() const {
107 return feature_section_descriptors_;
110 bool ReadIdsForAttr(const PerfFileFormat::FileAttr& attr, std::vector<uint64_t>* ids);
111 // If sorted is true, sort records before passing them to callback function.
112 bool ReadDataSection(std::function<bool(std::unique_ptr<Record>)> callback, bool sorted = true);
113 std::vector<std::string> ReadCmdlineFeature();
114 std::vector<BuildIdRecord> ReadBuildIdFeature();
115 std::string ReadFeatureString(int feature);
119 std::vector<std::unique_ptr<Record>> DataSection();
122 RecordFileReader(const std::string& filename, FILE* fp);
124 bool ReadAttrSection();
125 bool ReadFeatureSectionDescriptors();
126 bool ReadFeatureSection(int feature, std::vector<char>* data);
128 const std::string filename_;
131 PerfFileFormat::FileHeader header_;
132 std::vector<PerfFileFormat::FileAttr> file_attrs_;
133 std::map<int, PerfFileFormat::SectionDesc> feature_section_descriptors_;
135 DISALLOW_COPY_AND_ASSIGN(RecordFileReader);
138 #endif // SIMPLE_PERF_RECORD_FILE_H_