• 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 <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 "perf_event.h"
34 #include "record.h"
35 #include "record_file_format.h"
36 #include "thread_tree.h"
37 
38 // RecordFileWriter writes to a perf record file, like perf.data.
39 // User should call RecordFileWriter::Close() to finish writing the file, otherwise the file will
40 // be removed in RecordFileWriter::~RecordFileWriter().
41 class RecordFileWriter {
42  public:
43   static std::unique_ptr<RecordFileWriter> CreateInstance(const std::string& filename);
44 
45   ~RecordFileWriter();
46 
47   bool WriteAttrSection(const std::vector<EventAttrWithId>& attr_ids);
48   bool WriteRecord(const Record& record);
49 
GetDataSectionSize()50   uint64_t GetDataSectionSize() const { return data_section_size_; }
51   bool ReadDataSection(const std::function<void(const Record*)>& callback);
52 
53   bool BeginWriteFeatures(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();
58   bool WriteFileFeatures(const std::vector<Dso*>& files);
59   bool WriteMetaInfoFeature(const std::unordered_map<std::string, std::string>& info_map);
60   bool WriteFeature(int feature, const std::vector<char>& data);
61   bool EndWriteFeatures();
62 
63   bool Close();
64 
65  private:
66   RecordFileWriter(const std::string& filename, FILE* fp);
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 WriteData(const void* buf, size_t len);
72   bool Write(const void* buf, size_t len);
73   bool Read(void* buf, size_t len);
74   bool GetFilePos(uint64_t* file_pos);
75   bool WriteStringWithLength(const std::string& s);
76   bool WriteFileFeature(const std::string& file_path,
77                         uint32_t file_type,
78                         uint64_t min_vaddr,
79                         uint64_t file_offset_of_min_vaddr,
80                         const std::vector<const Symbol*>& symbols,
81                         const std::vector<uint64_t>* dex_file_offsets);
82   bool WriteFeatureBegin(int feature);
83   bool WriteFeatureEnd(int feature);
84 
85   const std::string filename_;
86   FILE* record_fp_;
87 
88   perf_event_attr event_attr_;
89   uint64_t attr_section_offset_;
90   uint64_t attr_section_size_;
91   uint64_t data_section_offset_;
92   uint64_t data_section_size_;
93   uint64_t feature_section_offset_;
94 
95   std::map<int, PerfFileFormat::SectionDesc> features_;
96   size_t feature_count_;
97 
98   DISALLOW_COPY_AND_ASSIGN(RecordFileWriter);
99 };
100 
101 // RecordFileReader read contents from a perf record file, like perf.data.
102 class RecordFileReader {
103  public:
104   static std::unique_ptr<RecordFileReader> CreateInstance(const std::string& filename);
105 
106   ~RecordFileReader();
107 
FileHeader()108   const PerfFileFormat::FileHeader& FileHeader() const {
109     return header_;
110   }
111 
AttrSection()112   std::vector<EventAttrWithId> AttrSection() const {
113     std::vector<EventAttrWithId> result(file_attrs_.size());
114     for (size_t i = 0; i < file_attrs_.size(); ++i) {
115       result[i].attr = &file_attrs_[i].attr;
116       result[i].ids = event_ids_for_file_attrs_[i];
117     }
118     return result;
119   }
120 
FeatureSectionDescriptors()121   const std::map<int, PerfFileFormat::SectionDesc>& FeatureSectionDescriptors() const {
122     return feature_section_descriptors_;
123   }
HasFeature(int feature)124   bool HasFeature(int feature) const {
125     return feature_section_descriptors_.find(feature) != feature_section_descriptors_.end();
126   }
127   bool ReadFeatureSection(int feature, std::vector<char>* data);
128 
129   // There are two ways to read records in data section: one is by calling
130   // ReadDataSection(), and [callback] is called for each Record. the other
131   // is by calling ReadRecord() in a loop.
132 
133   // If sorted is true, sort records before passing them to callback function.
134   bool ReadDataSection(const std::function<bool(std::unique_ptr<Record>)>& callback);
135 
136   // Read next record. If read successfully, set [record] and return true.
137   // If there is no more records, set [record] to nullptr and return true.
138   // Otherwise return false.
139   bool ReadRecord(std::unique_ptr<Record>& record);
140 
141   size_t GetAttrIndexOfRecord(const Record* record);
142 
143   std::vector<std::string> ReadCmdlineFeature();
144   std::vector<BuildIdRecord> ReadBuildIdFeature();
145   std::string ReadFeatureString(int feature);
146 
147   // File feature section contains many file information. This function reads
148   // one file information located at [read_pos]. [read_pos] is 0 at the first
149   // call, and is updated to point to the next file information. Return true
150   // if read successfully, and return false if there is no more file
151   // information.
152   bool ReadFileFeature(size_t& read_pos, std::string* file_path, uint32_t* file_type,
153                        uint64_t* min_vaddr, uint64_t* file_offset_of_min_vaddr,
154                        std::vector<Symbol>* symbols, std::vector<uint64_t>* dex_file_offsets);
155   bool ReadMetaInfoFeature(std::unordered_map<std::string, std::string>* info_map);
156 
157   void LoadBuildIdAndFileFeatures(ThreadTree& thread_tree);
158 
159   bool Close();
160 
161   // For testing only.
162   std::vector<std::unique_ptr<Record>> DataSection();
163 
164  private:
165   RecordFileReader(const std::string& filename, FILE* fp);
166   bool ReadHeader();
167   bool ReadAttrSection();
168   bool ReadIdsForAttr(const PerfFileFormat::FileAttr& attr, std::vector<uint64_t>* ids);
169   bool ReadFeatureSectionDescriptors();
170   std::unique_ptr<Record> ReadRecord(uint64_t* nbytes_read);
171   bool Read(void* buf, size_t len);
172   void ProcessEventIdRecord(const EventIdRecord& r);
173 
174   const std::string filename_;
175   FILE* record_fp_;
176 
177   PerfFileFormat::FileHeader header_;
178   std::vector<PerfFileFormat::FileAttr> file_attrs_;
179   std::vector<std::vector<uint64_t>> event_ids_for_file_attrs_;
180   std::unordered_map<uint64_t, size_t> event_id_to_attr_map_;
181   std::map<int, PerfFileFormat::SectionDesc> feature_section_descriptors_;
182 
183   size_t event_id_pos_in_sample_records_;
184   size_t event_id_reverse_pos_in_non_sample_records_;
185 
186   uint64_t read_record_size_;
187 
188   DISALLOW_COPY_AND_ASSIGN(RecordFileReader);
189 };
190 
191 #endif  // SIMPLE_PERF_RECORD_FILE_H_
192