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 FileHeader()142 const PerfFileFormat::FileHeader& FileHeader() const { return header_; } 143 AttrSection()144 const EventAttrIds& AttrSection() const { return event_attrs_; } 145 EventIdMap()146 const std::unordered_map<uint64_t, size_t>& EventIdMap() const { return event_id_to_attr_map_; } 147 FeatureSectionDescriptors()148 const std::map<int, PerfFileFormat::SectionDesc>& FeatureSectionDescriptors() const { 149 return feature_section_descriptors_; 150 } HasFeature(int feature)151 bool HasFeature(int feature) const { 152 return feature_section_descriptors_.find(feature) != feature_section_descriptors_.end(); 153 } 154 bool ReadFeatureSection(int feature, std::vector<char>* data); 155 bool ReadFeatureSection(int feature, std::string* data); 156 157 // There are two ways to read records in data section: one is by calling 158 // ReadDataSection(), and [callback] is called for each Record. the other 159 // is by calling ReadRecord() in a loop. 160 161 // If sorted is true, sort records before passing them to callback function. 162 bool ReadDataSection(const std::function<bool(std::unique_ptr<Record>)>& callback); 163 bool ReadAtOffset(uint64_t offset, void* buf, size_t len); 164 165 // Read next record. If read successfully, set [record] and return true. 166 // If there is no more records, set [record] to nullptr and return true. 167 // Otherwise return false. 168 bool ReadRecord(std::unique_ptr<Record>& record); 169 170 size_t GetAttrIndexOfRecord(const Record* record); 171 172 std::vector<std::string> ReadCmdlineFeature(); 173 std::vector<BuildIdRecord> ReadBuildIdFeature(); 174 std::string ReadFeatureString(int feature); 175 std::vector<uint64_t> ReadAuxTraceFeature(); 176 177 // File feature section contains many file information. This function reads 178 // one file information located at [read_pos]. [read_pos] is 0 at the first 179 // call, and is updated to point to the next file information. 180 // When read successfully, return true and set error to false. 181 // When no more data to read, return false and set error to false. 182 // When having error, return false and set error to true. 183 bool ReadFileFeature(uint64_t& read_pos, FileFeature& file, bool& error); 184 GetMetaInfoFeature()185 const std::unordered_map<std::string, std::string>& GetMetaInfoFeature() { return meta_info_; } 186 std::string GetClockId(); 187 std::optional<DebugUnwindFeature> ReadDebugUnwindFeature(); 188 189 bool LoadBuildIdAndFileFeatures(ThreadTree& thread_tree); 190 191 // Read aux data into buf. 192 // When read successfully, return true and set error to false. 193 // When the data isn't available, return false and set error to false. 194 // When having error, return false and set error to true. 195 bool ReadAuxData(uint32_t cpu, uint64_t aux_offset, size_t size, std::vector<uint8_t>& buf, 196 bool& error); 197 198 bool Close(); 199 200 // For testing only. 201 std::vector<std::unique_ptr<Record>> DataSection(); 202 203 private: 204 RecordFileReader(const std::string& filename, FILE* fp); 205 bool ReadHeader(); 206 bool CheckSectionDesc(const PerfFileFormat::SectionDesc& desc, uint64_t min_offset, 207 uint64_t alignment = 1); 208 bool ReadAttrSection(); 209 bool ReadIdSection(const PerfFileFormat::SectionDesc& section, std::vector<uint64_t>* ids); 210 bool ReadFeatureSectionDescriptors(); 211 bool ReadFileV1Feature(uint64_t& read_pos, uint64_t max_size, FileFeature& file); 212 bool ReadFileV2Feature(uint64_t& read_pos, uint64_t max_size, FileFeature& file); 213 bool ReadMetaInfoFeature(); 214 void UseRecordingEnvironment(); 215 std::unique_ptr<Record> ReadRecord(); 216 bool Read(void* buf, size_t len); 217 void ProcessEventIdRecord(const EventIdRecord& r); 218 bool BuildAuxDataLocation(); 219 220 const std::string filename_; 221 FILE* record_fp_; 222 uint64_t file_size_; 223 224 PerfFileFormat::FileHeader header_; 225 EventAttrIds event_attrs_; 226 std::unordered_map<uint64_t, size_t> event_id_to_attr_map_; 227 std::map<int, PerfFileFormat::SectionDesc> feature_section_descriptors_; 228 229 size_t event_id_pos_in_sample_records_; 230 size_t event_id_reverse_pos_in_non_sample_records_; 231 232 uint64_t read_record_size_; 233 234 std::unordered_map<std::string, std::string> meta_info_; 235 std::unique_ptr<ScopedCurrentArch> scoped_arch_; 236 std::unique_ptr<ScopedEventTypes> scoped_event_types_; 237 238 struct AuxDataLocation { 239 uint64_t aux_offset; 240 uint64_t aux_size; 241 uint64_t file_offset; 242 AuxDataLocationAuxDataLocation243 AuxDataLocation(uint64_t aux_offset, uint64_t aux_size, uint64_t file_offset) 244 : aux_offset(aux_offset), aux_size(aux_size), file_offset(file_offset) {} 245 }; 246 // It maps from a cpu id to the locations (file offsets in perf.data) of aux data received from 247 // that cpu's aux buffer. It is used to locate aux data in perf.data. 248 std::unordered_map<uint32_t, std::vector<AuxDataLocation>> aux_data_location_; 249 250 DISALLOW_COPY_AND_ASSIGN(RecordFileReader); 251 }; 252 253 bool IsPerfDataFile(const std::string& filename); 254 255 } // namespace simpleperf 256 257 #endif // SIMPLE_PERF_RECORD_FILE_H_ 258