OSDN Git Service

am 5c9978ce: Merge "Simpleperf: use ThreadTree when getting hit files in record command."
[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 class EventFd;
33
34 // RecordFileWriter writes to a perf record file, like perf.data.
35 class RecordFileWriter {
36  public:
37   static std::unique_ptr<RecordFileWriter> CreateInstance(
38       const std::string& filename, const perf_event_attr& event_attr,
39       const std::vector<std::unique_ptr<EventFd>>& event_fds);
40
41   ~RecordFileWriter();
42
43   bool WriteData(const void* buf, size_t len);
44
45   bool WriteData(const std::vector<char>& data) {
46     return WriteData(data.data(), data.size());
47   }
48
49   // Read data section that has been written, for further processing.
50   bool ReadDataSection(std::vector<std::unique_ptr<Record>>* records);
51
52   bool WriteFeatureHeader(size_t feature_count);
53   bool WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records);
54   bool WriteFeatureString(int feature, const std::string& s);
55   bool WriteCmdlineFeature(const std::vector<std::string>& cmdline);
56   bool WriteBranchStackFeature();
57
58   // Normally, Close() should be called after writing. But if something
59   // wrong happens and we need to finish in advance, the destructor
60   // will take care of calling Close().
61   bool Close();
62
63  private:
64   RecordFileWriter(const std::string& filename, FILE* fp);
65   bool WriteAttrSection(const perf_event_attr& event_attr,
66                         const std::vector<std::unique_ptr<EventFd>>& event_fds);
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_