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