• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dso.h"
33 #include "event_attr.h"
34 #include "event_type.h"
35 #include "perf_event.h"
36 #include "record.h"
37 #include "record_file_format.h"
38 #include "thread_tree.h"
39 
40 namespace simpleperf {
41 
42 struct FileFeature {
43   std::string path;
44   DsoType type;
45   uint64_t min_vaddr;
46   uint64_t file_offset_of_min_vaddr;       // for DSO_ELF_FILE or DSO_KERNEL_MODULE
47   std::vector<Symbol> symbols;             // used for reading symbols
48   std::vector<const Symbol*> symbol_ptrs;  // used for writing symbols
49   std::vector<uint64_t> dex_file_offsets;
50 
FileFeatureFileFeature51   FileFeature() {}
52 
ClearFileFeature53   void Clear() {
54     path.clear();
55     type = DSO_UNKNOWN_FILE;
56     min_vaddr = 0;
57     file_offset_of_min_vaddr = 0;
58     symbols.clear();
59     symbol_ptrs.clear();
60     dex_file_offsets.clear();
61   }
62 
63   DISALLOW_COPY_AND_ASSIGN(FileFeature);
64 };
65 
66 struct DebugUnwindFile {
67   std::string path;
68   uint64_t size;
69 };
70 
71 using DebugUnwindFeature = std::vector<DebugUnwindFile>;
72 
73 // RecordFileWriter writes to a perf record file, like perf.data.
74 // User should call RecordFileWriter::Close() to finish writing the file, otherwise the file will
75 // be removed in RecordFileWriter::~RecordFileWriter().
76 class RecordFileWriter {
77  public:
78   static std::unique_ptr<RecordFileWriter> CreateInstance(const std::string& filename);
79 
80   // If own_fp = true, close fp when we finish writing.
81   RecordFileWriter(const std::string& filename, FILE* fp, bool own_fp);
82   ~RecordFileWriter();
83 
84   bool WriteAttrSection(const EventAttrIds& attr_ids);
85   bool WriteRecord(const Record& record);
86   bool WriteData(const void* buf, size_t len);
87 
GetDataSectionSize()88   uint64_t GetDataSectionSize() const { return data_section_size_; }
89   bool ReadDataSection(const std::function<void(const Record*)>& callback);
90 
91   bool BeginWriteFeatures(size_t feature_count);
92   bool WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records);
93   bool WriteFeatureString(int feature, const std::string& s);
94   bool WriteCmdlineFeature(const std::vector<std::string>& cmdline);
95   bool WriteBranchStackFeature();
96   bool WriteAuxTraceFeature(const std::vector<uint64_t>& auxtrace_offset);
97   bool WriteFileFeatures(const std::vector<Dso*>& dsos);
98   bool WriteFileFeature(const FileFeature& file);
99   bool WriteMetaInfoFeature(const std::unordered_map<std::string, std::string>& info_map);
100   bool WriteDebugUnwindFeature(const DebugUnwindFeature& debug_unwind);
101   bool WriteFeature(int feature, const char* data, size_t size);
102   bool EndWriteFeatures();
103 
104   bool Close();
105 
106  private:
107   void GetHitModulesInBuffer(const char* p, const char* end,
108                              std::vector<std::string>* hit_kernel_modules,
109                              std::vector<std::string>* hit_user_files);
110   bool WriteFileHeader();
111   bool Write(const void* buf, size_t len);
112   bool Read(void* buf, size_t len);
113   bool GetFilePos(uint64_t* file_pos);
114   bool WriteStringWithLength(const std::string& s);
115   bool WriteFeatureBegin(int feature);
116   bool WriteFeatureEnd(int feature);
117 
118   const std::string filename_;
119   FILE* record_fp_;
120   bool own_fp_;
121 
122   perf_event_attr event_attr_;
123   uint64_t attr_section_offset_;
124   uint64_t attr_section_size_;
125   uint64_t data_section_offset_;
126   uint64_t data_section_size_;
127   uint64_t feature_section_offset_;
128 
129   std::map<int, PerfFileFormat::SectionDesc> features_;
130   size_t feature_count_;
131 
132   DISALLOW_COPY_AND_ASSIGN(RecordFileWriter);
133 };
134 
135 // RecordFileReader read contents from a perf record file, like perf.data.
136 class RecordFileReader {
137  public:
138   static std::unique_ptr<RecordFileReader> CreateInstance(const std::string& filename);
139 
140   ~RecordFileReader();
141 
FileName()142   const std::string FileName() const { return filename_; }
FileHeader()143   const PerfFileFormat::FileHeader& FileHeader() const { return header_; }
144 
AttrSection()145   const EventAttrIds& AttrSection() const { return event_attrs_; }
146 
EventIdMap()147   const std::unordered_map<uint64_t, size_t>& EventIdMap() const { return event_id_to_attr_map_; }
148 
FeatureSectionDescriptors()149   const std::map<int, PerfFileFormat::SectionDesc>& FeatureSectionDescriptors() const {
150     return feature_section_descriptors_;
151   }
HasFeature(int feature)152   bool HasFeature(int feature) const {
153     return feature_section_descriptors_.find(feature) != feature_section_descriptors_.end();
154   }
155   bool ReadFeatureSection(int feature, std::vector<char>* data);
156   bool ReadFeatureSection(int feature, std::string* data);
157 
158   // There are two ways to read records in data section: one is by calling
159   // ReadDataSection(), and [callback] is called for each Record. the other
160   // is by calling ReadRecord() in a loop.
161 
162   // If sorted is true, sort records before passing them to callback function.
163   bool ReadDataSection(const std::function<bool(std::unique_ptr<Record>)>& callback);
164   bool ReadAtOffset(uint64_t offset, void* buf, size_t len);
165 
166   // Read next record. If read successfully, set [record] and return true.
167   // If there is no more records, set [record] to nullptr and return true.
168   // Otherwise return false.
169   bool ReadRecord(std::unique_ptr<Record>& record);
170 
171   size_t GetAttrIndexOfRecord(const Record* record);
172 
173   std::vector<std::string> ReadCmdlineFeature();
174   std::vector<BuildIdRecord> ReadBuildIdFeature();
175   std::string ReadFeatureString(int feature);
176   std::vector<uint64_t> ReadAuxTraceFeature();
177 
178   // File feature section contains many file information. This function reads
179   // one file information located at [read_pos]. [read_pos] is 0 at the first
180   // call, and is updated to point to the next file information.
181   // When read successfully, return true and set error to false.
182   // When no more data to read, return false and set error to false.
183   // When having error, return false and set error to true.
184   bool ReadFileFeature(uint64_t& read_pos, FileFeature& file, bool& error);
185 
GetMetaInfoFeature()186   const std::unordered_map<std::string, std::string>& GetMetaInfoFeature() { return meta_info_; }
187   std::string GetClockId();
188   std::optional<DebugUnwindFeature> ReadDebugUnwindFeature();
189 
190   bool LoadBuildIdAndFileFeatures(ThreadTree& thread_tree);
191 
192   // Read aux data into buf.
193   // When read successfully, return true and set error to false.
194   // When the data isn't available, return false and set error to false.
195   // When having error, return false and set error to true.
196   bool ReadAuxData(uint32_t cpu, uint64_t aux_offset, size_t size, std::vector<uint8_t>& buf,
197                    bool& error);
198 
199   bool Close();
200 
201   // For testing only.
202   std::vector<std::unique_ptr<Record>> DataSection();
203 
204  private:
205   RecordFileReader(const std::string& filename, FILE* fp);
206   bool ReadHeader();
207   bool CheckSectionDesc(const PerfFileFormat::SectionDesc& desc, uint64_t min_offset,
208                         uint64_t alignment = 1);
209   bool ReadAttrSection();
210   bool ReadIdSection(const PerfFileFormat::SectionDesc& section, std::vector<uint64_t>* ids);
211   bool ReadFeatureSectionDescriptors();
212   bool ReadFileV1Feature(uint64_t& read_pos, uint64_t max_size, FileFeature& file);
213   bool ReadFileV2Feature(uint64_t& read_pos, uint64_t max_size, FileFeature& file);
214   bool ReadMetaInfoFeature();
215   void UseRecordingEnvironment();
216   std::unique_ptr<Record> ReadRecord();
217   bool Read(void* buf, size_t len);
218   void ProcessEventIdRecord(const EventIdRecord& r);
219   bool BuildAuxDataLocation();
220 
221   const std::string filename_;
222   FILE* record_fp_;
223   uint64_t file_size_;
224 
225   PerfFileFormat::FileHeader header_;
226   EventAttrIds event_attrs_;
227   std::unordered_map<uint64_t, size_t> event_id_to_attr_map_;
228   std::map<int, PerfFileFormat::SectionDesc> feature_section_descriptors_;
229 
230   size_t event_id_pos_in_sample_records_;
231   size_t event_id_reverse_pos_in_non_sample_records_;
232 
233   uint64_t read_record_size_;
234 
235   std::unordered_map<std::string, std::string> meta_info_;
236   std::unique_ptr<ScopedCurrentArch> scoped_arch_;
237   std::unique_ptr<ScopedEventTypes> scoped_event_types_;
238 
239   struct AuxDataLocation {
240     uint64_t aux_offset;
241     uint64_t aux_size;
242     uint64_t file_offset;
243 
AuxDataLocationAuxDataLocation244     AuxDataLocation(uint64_t aux_offset, uint64_t aux_size, uint64_t file_offset)
245         : aux_offset(aux_offset), aux_size(aux_size), file_offset(file_offset) {}
246   };
247   // It maps from a cpu id to the locations (file offsets in perf.data) of aux data received from
248   // that cpu's aux buffer. It is used to locate aux data in perf.data.
249   std::unordered_map<uint32_t, std::vector<AuxDataLocation>> aux_data_location_;
250 
251   DISALLOW_COPY_AND_ASSIGN(RecordFileReader);
252 };
253 
254 bool IsPerfDataFile(const std::string& filename);
255 
256 }  // namespace simpleperf
257 
258 #endif  // SIMPLE_PERF_RECORD_FILE_H_
259