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 <string>
26 #include <unordered_map>
27 #include <vector>
28 
29 #include <android-base/macros.h>
30 
31 #include "dso.h"
32 #include "event_attr.h"
33 #include "event_type.h"
34 #include "perf_event.h"
35 #include "record.h"
36 #include "record_file_format.h"
37 #include "thread_tree.h"
38 
39 // RecordFileWriter writes to a perf record file, like perf.data.
40 // User should call RecordFileWriter::Close() to finish writing the file, otherwise the file will
41 // be removed in RecordFileWriter::~RecordFileWriter().
42 class RecordFileWriter {
43  public:
44   static std::unique_ptr<RecordFileWriter> CreateInstance(const std::string& filename);
45 
46   ~RecordFileWriter();
47 
48   bool WriteAttrSection(const std::vector<EventAttrWithId>& attr_ids);
49   bool WriteRecord(const Record& record);
50 
GetDataSectionSize()51   uint64_t GetDataSectionSize() const { return data_section_size_; }
52   bool ReadDataSection(const std::function<void(const Record*)>& callback);
53 
54   bool BeginWriteFeatures(size_t feature_count);
55   bool WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records);
56   bool WriteFeatureString(int feature, const std::string& s);
57   bool WriteCmdlineFeature(const std::vector<std::string>& cmdline);
58   bool WriteBranchStackFeature();
59   bool WriteAuxTraceFeature(const std::vector<uint64_t>& auxtrace_offset);
60   bool WriteFileFeatures(const std::vector<Dso*>& files);
61   bool WriteMetaInfoFeature(const std::unordered_map<std::string, std::string>& info_map);
62   bool WriteFeature(int feature, const std::vector<char>& data);
63   bool EndWriteFeatures();
64 
65   bool Close();
66 
67  private:
68   RecordFileWriter(const std::string& filename, FILE* fp);
69   void GetHitModulesInBuffer(const char* p, const char* end,
70                              std::vector<std::string>* hit_kernel_modules,
71                              std::vector<std::string>* hit_user_files);
72   bool WriteFileHeader();
73   bool WriteData(const void* buf, size_t len);
74   bool Write(const void* buf, size_t len);
75   bool Read(void* buf, size_t len);
76   bool GetFilePos(uint64_t* file_pos);
77   bool WriteStringWithLength(const std::string& s);
78   bool WriteFileFeature(const std::string& file_path,
79                         uint32_t file_type,
80                         uint64_t min_vaddr,
81                         uint64_t file_offset_of_min_vaddr,
82                         const std::vector<const Symbol*>& symbols,
83                         const std::vector<uint64_t>* dex_file_offsets);
84   bool WriteFeatureBegin(int feature);
85   bool WriteFeatureEnd(int feature);
86 
87   const std::string filename_;
88   FILE* record_fp_;
89 
90   perf_event_attr event_attr_;
91   uint64_t attr_section_offset_;
92   uint64_t attr_section_size_;
93   uint64_t data_section_offset_;
94   uint64_t data_section_size_;
95   uint64_t feature_section_offset_;
96 
97   std::map<int, PerfFileFormat::SectionDesc> features_;
98   size_t feature_count_;
99 
100   DISALLOW_COPY_AND_ASSIGN(RecordFileWriter);
101 };
102 
103 // RecordFileReader read contents from a perf record file, like perf.data.
104 class RecordFileReader {
105  public:
106   static std::unique_ptr<RecordFileReader> CreateInstance(const std::string& filename);
107 
108   ~RecordFileReader();
109 
FileHeader()110   const PerfFileFormat::FileHeader& FileHeader() const {
111     return header_;
112   }
113 
AttrSection()114   std::vector<EventAttrWithId> AttrSection() const {
115     std::vector<EventAttrWithId> result(file_attrs_.size());
116     for (size_t i = 0; i < file_attrs_.size(); ++i) {
117       result[i].attr = &file_attrs_[i].attr;
118       result[i].ids = event_ids_for_file_attrs_[i];
119     }
120     return result;
121   }
122 
FeatureSectionDescriptors()123   const std::map<int, PerfFileFormat::SectionDesc>& FeatureSectionDescriptors() const {
124     return feature_section_descriptors_;
125   }
HasFeature(int feature)126   bool HasFeature(int feature) const {
127     return feature_section_descriptors_.find(feature) != feature_section_descriptors_.end();
128   }
129   bool ReadFeatureSection(int feature, std::vector<char>* data);
130 
131   // There are two ways to read records in data section: one is by calling
132   // ReadDataSection(), and [callback] is called for each Record. the other
133   // is by calling ReadRecord() in a loop.
134 
135   // If sorted is true, sort records before passing them to callback function.
136   bool ReadDataSection(const std::function<bool(std::unique_ptr<Record>)>& callback);
137 
138   // Read next record. If read successfully, set [record] and return true.
139   // If there is no more records, set [record] to nullptr and return true.
140   // Otherwise return false.
141   bool ReadRecord(std::unique_ptr<Record>& record);
142 
143   size_t GetAttrIndexOfRecord(const Record* record);
144 
145   std::vector<std::string> ReadCmdlineFeature();
146   std::vector<BuildIdRecord> ReadBuildIdFeature();
147   std::string ReadFeatureString(int feature);
148   std::vector<uint64_t> ReadAuxTraceFeature();
149 
150   // File feature section contains many file information. This function reads
151   // one file information located at [read_pos]. [read_pos] is 0 at the first
152   // call, and is updated to point to the next file information. Return true
153   // if read successfully, and return false if there is no more file
154   // information.
155   bool ReadFileFeature(size_t& read_pos, std::string* file_path, uint32_t* file_type,
156                        uint64_t* min_vaddr, uint64_t* file_offset_of_min_vaddr,
157                        std::vector<Symbol>* symbols, std::vector<uint64_t>* dex_file_offsets);
158 
GetMetaInfoFeature()159   const std::unordered_map<std::string, std::string>& GetMetaInfoFeature() { return meta_info_; }
160 
161   void LoadBuildIdAndFileFeatures(ThreadTree& thread_tree);
162 
163   bool ReadAuxData(uint32_t cpu, uint64_t aux_offset, void* buf, size_t size);
164 
165   bool Close();
166 
167   // For testing only.
168   std::vector<std::unique_ptr<Record>> DataSection();
169 
170  private:
171   RecordFileReader(const std::string& filename, FILE* fp);
172   bool ReadHeader();
173   bool ReadAttrSection();
174   bool ReadIdsForAttr(const PerfFileFormat::FileAttr& attr, std::vector<uint64_t>* ids);
175   bool ReadFeatureSectionDescriptors();
176   bool ReadMetaInfoFeature();
177   void UseRecordingEnvironment();
178   std::unique_ptr<Record> ReadRecord();
179   bool Read(void* buf, size_t len);
180   bool ReadAtOffset(uint64_t offset, void* buf, size_t len);
181   void ProcessEventIdRecord(const EventIdRecord& r);
182   bool BuildAuxDataLocation();
183 
184   const std::string filename_;
185   FILE* record_fp_;
186 
187   PerfFileFormat::FileHeader header_;
188   std::vector<PerfFileFormat::FileAttr> file_attrs_;
189   std::vector<std::vector<uint64_t>> event_ids_for_file_attrs_;
190   std::unordered_map<uint64_t, size_t> event_id_to_attr_map_;
191   std::map<int, PerfFileFormat::SectionDesc> feature_section_descriptors_;
192 
193   size_t event_id_pos_in_sample_records_;
194   size_t event_id_reverse_pos_in_non_sample_records_;
195 
196   uint64_t read_record_size_;
197 
198   std::unordered_map<std::string, std::string> meta_info_;
199   std::unique_ptr<ScopedCurrentArch> scoped_arch_;
200   std::unique_ptr<ScopedEventTypes> scoped_event_types_;
201 
202   struct AuxDataLocation {
203     uint64_t aux_offset;
204     uint64_t aux_size;
205     uint64_t file_offset;
206 
AuxDataLocationAuxDataLocation207     AuxDataLocation(uint64_t aux_offset, uint64_t aux_size, uint64_t file_offset)
208         : aux_offset(aux_offset), aux_size(aux_size), file_offset(file_offset) {}
209   };
210   // It maps from a cpu id to the locations (file offsets in perf.data) of aux data received from
211   // that cpu's aux buffer. It is used to locate aux data in perf.data.
212   std::unordered_map<uint32_t, std::vector<AuxDataLocation>> aux_data_location_;
213 
214   DISALLOW_COPY_AND_ASSIGN(RecordFileReader);
215 };
216 
217 #endif  // SIMPLE_PERF_RECORD_FILE_H_
218