xref: /aosp_15_r20/system/extras/simpleperf/record_file.h (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #ifndef SIMPLE_PERF_RECORD_FILE_H_
18 #define SIMPLE_PERF_RECORD_FILE_H_
19 
20 #include <stdio.h>
21 
22 #include <functional>
23 #include <map>
24 #include <memory>
25 #include <optional>
26 #include <string>
27 #include <unordered_map>
28 #include <vector>
29 
30 #include <android-base/macros.h>
31 
32 #include "ZstdUtil.h"
33 #include "dso.h"
34 #include "event_attr.h"
35 #include "event_type.h"
36 #include "perf_event.h"
37 #include "record.h"
38 #include "record_file_format.h"
39 #include "thread_tree.h"
40 
41 namespace simpleperf {
42 
43 struct FileFeature {
44   std::string path;
45   DsoType type;
46   uint64_t min_vaddr;
47   uint64_t file_offset_of_min_vaddr;       // for DSO_ELF_FILE or DSO_KERNEL_MODULE
48   std::vector<Symbol> symbols;             // used for reading symbols
49   std::vector<const Symbol*> symbol_ptrs;  // used for writing symbols
50   std::vector<uint64_t> dex_file_offsets;
51 
FileFeatureFileFeature52   FileFeature() {}
53 
ClearFileFeature54   void Clear() {
55     path.clear();
56     type = DSO_UNKNOWN_FILE;
57     min_vaddr = 0;
58     file_offset_of_min_vaddr = 0;
59     symbols.clear();
60     symbol_ptrs.clear();
61     dex_file_offsets.clear();
62   }
63 
64   DISALLOW_COPY_AND_ASSIGN(FileFeature);
65 };
66 
67 struct DebugUnwindFile {
68   std::string path;
69   uint64_t size;
70 };
71 
72 using DebugUnwindFeature = std::vector<DebugUnwindFile>;
73 
74 // RecordFileWriter writes to a perf record file, like perf.data.
75 // User should call RecordFileWriter::Close() to finish writing the file, otherwise the file will
76 // be removed in RecordFileWriter::~RecordFileWriter().
77 class RecordFileWriter {
78  public:
79   static std::unique_ptr<RecordFileWriter> CreateInstance(const std::string& filename);
80 
81   // If own_fp = true, close fp when we finish writing.
82   RecordFileWriter(const std::string& filename, FILE* fp, bool own_fp);
83   ~RecordFileWriter();
84 
85   bool SetCompressionLevel(size_t compression_level);
86   bool WriteAttrSection(const EventAttrIds& attr_ids);
87   bool WriteRecord(const Record& record);
88   bool FinishWritingDataSection();
89 
GetDataSectionSize()90   uint64_t GetDataSectionSize() const { return data_section_size_; }
91   bool ReadDataSection(const std::function<void(const Record*)>& callback);
AuxTraceRecordOffsets()92   const std::vector<uint64_t>& AuxTraceRecordOffsets() const { return auxtrace_record_offsets_; }
93 
94   bool BeginWriteFeatures(size_t feature_count);
95   bool WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records);
96   bool WriteFeatureString(int feature, const std::string& s);
97   bool WriteCmdlineFeature(const std::vector<std::string>& cmdline);
98   bool WriteBranchStackFeature();
99   bool WriteAuxTraceFeature(const std::vector<uint64_t>& auxtrace_offset);
100   bool WriteFileFeatures(const std::vector<Dso*>& dsos);
101   bool WriteFileFeature(const FileFeature& file);
102   bool WriteMetaInfoFeature(const std::unordered_map<std::string, std::string>& info_map);
103   bool WriteDebugUnwindFeature(const DebugUnwindFeature& debug_unwind);
104   bool WriteInitMapFeature(const char* data, size_t size);
105   bool FinishWritingInitMapFeature();
106   bool WriteFeature(int feature, const char* data, size_t size);
107   bool EndWriteFeatures();
108 
109   bool Close();
GetCompressor()110   Compressor* GetCompressor() { return compressor_.get(); }
111 
112  private:
113   void GetHitModulesInBuffer(const char* p, const char* end,
114                              std::vector<std::string>* hit_kernel_modules,
115                              std::vector<std::string>* hit_user_files);
116   bool WriteFileHeader();
117   bool WriteAuxTraceRecord(const AuxTraceRecord& r);
118   // If flush=true, the previous data can be decompressed without any following data.
119   bool WriteCompressorOutput(bool flush, bool data_section);
120   bool WriteCompressRecord(const char* data, size_t size, bool data_section);
121   bool WriteData(const void* buf, size_t len);
122   bool Write(const void* buf, size_t len);
123   bool ReadFromDecompressor(Decompressor& decompressor,
124                             const std::function<void(const Record*)>& callback);
125   bool Read(void* buf, size_t len);
126   bool GetFilePos(uint64_t* file_pos);
127   bool WriteStringWithLength(const std::string& s);
128   bool WriteFeatureBegin(int feature);
129   bool WriteFeatureEnd(int feature);
130 
131   const std::string filename_;
132   FILE* record_fp_;
133   bool own_fp_;
134 
135   perf_event_attr event_attr_;
136   uint64_t attr_section_offset_;
137   uint64_t attr_section_size_;
138   uint64_t data_section_offset_;
139   uint64_t data_section_size_;
140   uint64_t feature_section_offset_;
141 
142   std::map<int, PerfFileFormat::SectionDesc> features_;
143   size_t feature_count_;
144 
145   std::unique_ptr<Compressor> compressor_;
146   std::vector<uint64_t> auxtrace_record_offsets_;
147 
148   DISALLOW_COPY_AND_ASSIGN(RecordFileWriter);
149 };
150 
151 // RecordFileReader read contents from a perf record file, like perf.data.
152 class RecordFileReader {
153  public:
154   static std::unique_ptr<RecordFileReader> CreateInstance(const std::string& filename);
155 
156   ~RecordFileReader();
157 
FileName()158   const std::string FileName() const { return filename_; }
FileHeader()159   const PerfFileFormat::FileHeader& FileHeader() const { return header_; }
160 
AttrSection()161   const EventAttrIds& AttrSection() const { return event_attrs_; }
162 
EventIdMap()163   const std::unordered_map<uint64_t, size_t>& EventIdMap() const { return event_id_to_attr_map_; }
164 
FeatureSectionDescriptors()165   const std::map<int, PerfFileFormat::SectionDesc>& FeatureSectionDescriptors() const {
166     return feature_section_descriptors_;
167   }
HasFeature(int feature)168   bool HasFeature(int feature) const {
169     return feature_section_descriptors_.find(feature) != feature_section_descriptors_.end();
170   }
171   bool ReadFeatureSection(int feature, std::vector<char>* data);
172   bool ReadFeatureSection(int feature, std::string* data);
173 
174   // There are two ways to read records in data section: one is by calling
175   // ReadDataSection(), and [callback] is called for each Record. the other
176   // is by calling ReadRecord() in a loop.
177 
178   // If sorted is true, sort records before passing them to callback function.
179   bool ReadDataSection(const std::function<bool(std::unique_ptr<Record>)>& callback);
180   bool ReadAtOffset(uint64_t offset, void* buf, size_t len);
181 
182   // Read next record. If read successfully, set [record] and return true.
183   // If there is no more records, set [record] to nullptr and return true.
184   // Otherwise return false.
185   bool ReadRecord(std::unique_ptr<Record>& record);
186 
187   size_t GetAttrIndexOfRecord(const Record* record);
188   std::optional<size_t> GetAttrIndexByEventId(uint64_t event_id);
189 
190   std::vector<std::string> ReadCmdlineFeature();
191   std::vector<BuildIdRecord> ReadBuildIdFeature();
192   std::string ReadFeatureString(int feature);
193   std::vector<uint64_t> ReadAuxTraceFeature();
194 
195   // File feature section contains many file information. This function reads
196   // one file information located at [read_pos]. [read_pos] is 0 at the first
197   // call, and is updated to point to the next file information.
198   // When read successfully, return true and set error to false.
199   // When no more data to read, return false and set error to false.
200   // When having error, return false and set error to true.
201   bool ReadFileFeature(uint64_t& read_pos, FileFeature& file, bool& error);
202 
GetMetaInfoFeature()203   const std::unordered_map<std::string, std::string>& GetMetaInfoFeature() { return meta_info_; }
204   std::string GetClockId();
205   std::optional<DebugUnwindFeature> ReadDebugUnwindFeature();
206   bool ReadInitMapFeature(const std::function<bool(std::unique_ptr<Record>)>& callback);
207 
208   bool LoadBuildIdAndFileFeatures(ThreadTree& thread_tree);
209 
210   // Read aux data into buf.
211   // When read successfully, return true and set error to false.
212   // When the data isn't available, return false and set error to false.
213   // When having error, return false and set error to true.
214   bool ReadAuxData(uint32_t cpu, uint64_t aux_offset, size_t size, std::vector<uint8_t>& buf,
215                    bool& error);
216 
217   bool Close();
218 
219   // For testing only.
220   std::vector<std::unique_ptr<Record>> DataSection();
221 
222  private:
223   struct ReadPos {
224     uint64_t pos = 0;
225     uint64_t end = 0;
226   };
227 
228   struct AuxDataLocation {
229     uint64_t aux_offset;
230     uint64_t aux_size;
231     uint64_t file_offset;
232 
233     AuxDataLocation(uint64_t aux_offset = 0, uint64_t aux_size = 0, uint64_t file_offset = 0)
aux_offsetAuxDataLocation234         : aux_offset(aux_offset), aux_size(aux_size), file_offset(file_offset) {}
235 
236     bool operator!=(const AuxDataLocation& other) const {
237       return aux_offset != other.aux_offset || aux_size != other.aux_size ||
238              file_offset != other.file_offset;
239     }
240   };
241 
242   RecordFileReader(const std::string& filename, FILE* fp);
243   bool ReadHeader();
244   bool CheckSectionDesc(const PerfFileFormat::SectionDesc& desc, uint64_t min_offset,
245                         uint64_t alignment = 1);
246   bool ReadAttrSection();
247   bool ReadIdSection(const PerfFileFormat::SectionDesc& section, std::vector<uint64_t>* ids);
248   bool ReadFeatureSectionDescriptors();
249   bool ReadFileV1Feature(uint64_t& read_pos, uint64_t max_size, FileFeature& file);
250   bool ReadFileV2Feature(uint64_t& read_pos, uint64_t max_size, FileFeature& file);
251   bool ReadMetaInfoFeature();
252   void UseRecordingEnvironment();
253   std::unique_ptr<Record> ReadRecord(ReadPos& pos);
254   std::unique_ptr<char[]> ReadRecordWithDecompression(ReadPos& pos);
255   bool Read(void* buf, size_t len);
256   void ProcessEventIdRecord(const EventIdRecord& r);
257   bool BuildAuxDataLocation();
258   bool ReadAuxDataFromDecompressor(uint32_t cpu, uint64_t aux_offset, size_t size,
259                                    std::vector<uint8_t>& buf, const AuxDataLocation& location,
260                                    bool& error);
261 
262   const std::string filename_;
263   FILE* record_fp_;
264   uint64_t file_size_;
265 
266   PerfFileFormat::FileHeader header_;
267   EventAttrIds event_attrs_;
268   std::unordered_map<uint64_t, size_t> event_id_to_attr_map_;
269   std::map<int, PerfFileFormat::SectionDesc> feature_section_descriptors_;
270 
271   size_t event_id_pos_in_sample_records_;
272   size_t event_id_reverse_pos_in_non_sample_records_;
273 
274   ReadPos read_record_pos_;
275 
276   std::unordered_map<std::string, std::string> meta_info_;
277   std::unique_ptr<ScopedCurrentArch> scoped_arch_;
278   std::unique_ptr<ScopedEventTypes> scoped_event_types_;
279 
280   // It maps from a cpu id to the locations (file offsets in perf.data) of aux data received from
281   // that cpu's aux buffer. It is used to locate aux data in perf.data.
282   std::unordered_map<uint32_t, std::vector<AuxDataLocation>> aux_data_location_;
283   std::unique_ptr<Decompressor> decompressor_;
284 
285   struct AuxDataDecompressor {
286     uint32_t cpu = 0;
287     AuxDataLocation location;
288     std::unique_ptr<Decompressor> decompressor;
289   };
290   std::unique_ptr<AuxDataDecompressor> auxdata_decompressor_;
291 
292   DISALLOW_COPY_AND_ASSIGN(RecordFileReader);
293 };
294 
295 bool IsPerfDataFile(const std::string& filename);
296 
297 }  // namespace simpleperf
298 
299 #endif  // SIMPLE_PERF_RECORD_FILE_H_
300