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_
26 #include <base/macros.h>
28 #include "perf_event.h"
30 #include "record_file_format.h"
34 // RecordFileWriter writes to a perf record file, like perf.data.
35 class RecordFileWriter {
37 static std::unique_ptr<RecordFileWriter> CreateInstance(
38 const std::string& filename, const perf_event_attr& event_attr,
39 const std::vector<std::unique_ptr<EventFd>>& event_fds);
43 bool WriteData(const void* buf, size_t len);
45 bool WriteData(const std::vector<char>& data) {
46 return WriteData(data.data(), data.size());
49 // Read data section that has been written, for further processing.
50 bool ReadDataSection(std::vector<std::unique_ptr<Record>>* records);
52 bool WriteFeatureHeader(size_t feature_count);
53 bool WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records);
54 bool WriteFeatureString(int feature, const std::string& s);
55 bool WriteCmdlineFeature(const std::vector<std::string>& cmdline);
56 bool WriteBranchStackFeature();
58 // Normally, Close() should be called after writing. But if something
59 // wrong happens and we need to finish in advance, the destructor
60 // will take care of calling Close().
64 RecordFileWriter(const std::string& filename, FILE* fp);
65 bool WriteAttrSection(const perf_event_attr& event_attr,
66 const std::vector<std::unique_ptr<EventFd>>& event_fds);
67 void GetHitModulesInBuffer(const char* p, const char* end,
68 std::vector<std::string>* hit_kernel_modules,
69 std::vector<std::string>* hit_user_files);
70 bool WriteFileHeader();
71 bool Write(const void* buf, size_t len);
72 bool SeekFileEnd(uint64_t* file_end);
73 bool WriteFeatureBegin(uint64_t* start_offset);
74 bool WriteFeatureEnd(int feature, uint64_t start_offset);
76 const std::string filename_;
79 perf_event_attr event_attr_;
80 uint64_t attr_section_offset_;
81 uint64_t attr_section_size_;
82 uint64_t data_section_offset_;
83 uint64_t data_section_size_;
85 std::vector<int> features_;
87 int current_feature_index_;
89 DISALLOW_COPY_AND_ASSIGN(RecordFileWriter);
92 // RecordFileReader read contents from a perf record file, like perf.data.
93 class RecordFileReader {
95 static std::unique_ptr<RecordFileReader> CreateInstance(const std::string& filename);
99 const PerfFileFormat::FileHeader* FileHeader();
100 std::vector<const PerfFileFormat::FileAttr*> AttrSection();
101 std::vector<uint64_t> IdsForAttr(const PerfFileFormat::FileAttr* attr);
102 std::vector<std::unique_ptr<Record>> DataSection();
103 const std::map<int, PerfFileFormat::SectionDesc>& FeatureSectionDescriptors();
104 const char* DataAtOffset(uint64_t offset) {
105 return mmap_addr_ + offset;
107 std::vector<std::string> ReadCmdlineFeature();
108 std::vector<BuildIdRecord> ReadBuildIdFeature();
109 std::string ReadFeatureString(int feature);
113 RecordFileReader(const std::string& filename, int fd);
115 bool GetFeatureSection(int feature, const char** pstart, const char** pend);
117 const std::string filename_;
120 const char* mmap_addr_;
123 std::map<int, PerfFileFormat::SectionDesc> feature_sections_;
125 DISALLOW_COPY_AND_ASSIGN(RecordFileReader);
128 #endif // SIMPLE_PERF_RECORD_FILE_H_