OSDN Git Service

am 6999c941: (-s ours) am b89e81dc: fs_config: align with new explicit fs_config...
[android-x86/system-extras.git] / simpleperf / record_file.h
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 #include <map>
22 #include <memory>
23 #include <string>
24 #include <vector>
25
26 #include <base/macros.h>
27
28 #include "perf_event.h"
29 #include "record.h"
30 #include "record_file_format.h"
31
32 struct AttrWithId {
33   const perf_event_attr* attr;
34   std::vector<uint64_t> ids;
35 };
36
37 // RecordFileWriter writes to a perf record file, like perf.data.
38 class RecordFileWriter {
39  public:
40   static std::unique_ptr<RecordFileWriter> CreateInstance(const std::string& filename);
41
42   ~RecordFileWriter();
43
44   bool WriteAttrSection(const std::vector<AttrWithId>& attr_ids);
45   bool WriteData(const void* buf, size_t len);
46
47   bool WriteData(const std::vector<char>& data) {
48     return WriteData(data.data(), data.size());
49   }
50
51   // Read data section that has been written, for further processing.
52   bool ReadDataSection(std::vector<std::unique_ptr<Record>>* records);
53
54   bool WriteFeatureHeader(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
60   // Normally, Close() should be called after writing. But if something
61   // wrong happens and we need to finish in advance, the destructor
62   // will take care of calling Close().
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 Write(const void* buf, size_t len);
72   bool SeekFileEnd(uint64_t* file_end);
73   bool WriteFeatureBegin(uint64_t* start_offset);
74   bool WriteFeatureEnd(int feature, uint64_t start_offset);
75
76   const std::string filename_;
77   FILE* record_fp_;
78
79   perf_event_attr event_attr_;
80   uint64_t attr_section_offset_;
81   uint64_t attr_section_size_;
82   uint64_t data_section_offset_;
83   uint64_t data_section_size_;
84
85   std::vector<int> features_;
86   int feature_count_;
87   int current_feature_index_;
88
89   DISALLOW_COPY_AND_ASSIGN(RecordFileWriter);
90 };
91
92 // RecordFileReader read contents from a perf record file, like perf.data.
93 class RecordFileReader {
94  public:
95   static std::unique_ptr<RecordFileReader> CreateInstance(const std::string& filename);
96
97   ~RecordFileReader();
98
99   const PerfFileFormat::FileHeader* FileHeader();
100   std::vector<const PerfFileFormat::FileAttr*> AttrSection();
101   std::vector<uint64_t> IdsForAttr(const PerfFileFormat::FileAttr* attr);
102   std::vector<std::unique_ptr<Record>> DataSection();
103   const std::map<int, PerfFileFormat::SectionDesc>& FeatureSectionDescriptors();
104   const char* DataAtOffset(uint64_t offset) {
105     return mmap_addr_ + offset;
106   }
107   std::vector<std::string> ReadCmdlineFeature();
108   std::vector<BuildIdRecord> ReadBuildIdFeature();
109   std::string ReadFeatureString(int feature);
110   bool Close();
111
112  private:
113   RecordFileReader(const std::string& filename, int fd);
114   bool MmapFile();
115   bool GetFeatureSection(int feature, const char** pstart, const char** pend);
116
117   const std::string filename_;
118   int record_fd_;
119
120   const char* mmap_addr_;
121   size_t mmap_len_;
122
123   std::map<int, PerfFileFormat::SectionDesc> feature_sections_;
124
125   DISALLOW_COPY_AND_ASSIGN(RecordFileReader);
126 };
127
128 #endif  // SIMPLE_PERF_RECORD_FILE_H_