OSDN Git Service

Merge "Setup filesystem for automatic forced repair"
[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   bool WriteDataSection(const std::vector<std::unique_ptr<Record>>& records);
54
55   bool WriteFeatureHeader(size_t feature_count);
56   bool WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records);
57   bool WriteFeatureString(int feature, const std::string& s);
58   bool WriteCmdlineFeature(const std::vector<std::string>& cmdline);
59   bool WriteBranchStackFeature();
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 Write(const void* buf, size_t len);
73   bool SeekFileEnd(uint64_t* file_end);
74   bool WriteFeatureBegin(uint64_t* start_offset);
75   bool WriteFeatureEnd(int feature, uint64_t start_offset);
76
77   const std::string filename_;
78   FILE* record_fp_;
79
80   perf_event_attr event_attr_;
81   uint64_t attr_section_offset_;
82   uint64_t attr_section_size_;
83   uint64_t data_section_offset_;
84   uint64_t data_section_size_;
85
86   std::vector<int> features_;
87   int feature_count_;
88   int current_feature_index_;
89
90   DISALLOW_COPY_AND_ASSIGN(RecordFileWriter);
91 };
92
93 // RecordFileReader read contents from a perf record file, like perf.data.
94 class RecordFileReader {
95  public:
96   static std::unique_ptr<RecordFileReader> CreateInstance(const std::string& filename);
97
98   ~RecordFileReader();
99
100   const PerfFileFormat::FileHeader* FileHeader();
101   std::vector<const PerfFileFormat::FileAttr*> AttrSection();
102   std::vector<uint64_t> IdsForAttr(const PerfFileFormat::FileAttr* attr);
103   std::vector<std::unique_ptr<Record>> DataSection();
104   const std::map<int, PerfFileFormat::SectionDesc>& FeatureSectionDescriptors();
105   const char* DataAtOffset(uint64_t offset) {
106     return mmap_addr_ + offset;
107   }
108   std::vector<std::string> ReadCmdlineFeature();
109   std::vector<BuildIdRecord> ReadBuildIdFeature();
110   std::string ReadFeatureString(int feature);
111   bool Close();
112
113  private:
114   RecordFileReader(const std::string& filename, int fd);
115   bool MmapFile();
116   bool GetFeatureSection(int feature, const char** pstart, const char** pend);
117
118   const std::string filename_;
119   int record_fd_;
120
121   const char* mmap_addr_;
122   size_t mmap_len_;
123
124   std::map<int, PerfFileFormat::SectionDesc> feature_sections_;
125
126   DISALLOW_COPY_AND_ASSIGN(RecordFileReader);
127 };
128
129 #endif  // SIMPLE_PERF_RECORD_FILE_H_