OSDN Git Service

Merge "Enhancements to workload scripts" into mnc-dev
[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 <memory>
22 #include <string>
23 #include <vector>
24
25 #include <base/macros.h>
26
27 #include "perf_event.h"
28 #include "record.h"
29 #include "record_file_format.h"
30
31 class EventFd;
32
33 // RecordFileWriter writes to a perf record file, like perf.data.
34 class RecordFileWriter {
35  public:
36   static std::unique_ptr<RecordFileWriter> CreateInstance(
37       const std::string& filename, const perf_event_attr& event_attr,
38       const std::vector<std::unique_ptr<EventFd>>& event_fds);
39
40   ~RecordFileWriter();
41
42   bool WriteData(const void* buf, size_t len);
43
44   bool WriteData(const std::vector<char>& data) {
45     return WriteData(data.data(), data.size());
46   }
47
48   // Use MmapRecords and SampleRecords in record file to conclude which modules/files were executing
49   // at sample times.
50   bool GetHitModules(std::vector<std::string>* hit_kernel_modules,
51                      std::vector<std::string>* hit_user_files);
52
53   bool WriteFeatureHeader(size_t feature_count);
54   bool WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records);
55
56   // Normally, Close() should be called after writing. But if something
57   // wrong happens and we need to finish in advance, the destructor
58   // will take care of calling Close().
59   bool Close();
60
61  private:
62   RecordFileWriter(const std::string& filename, FILE* fp);
63   bool WriteAttrSection(const perf_event_attr& event_attr,
64                         const std::vector<std::unique_ptr<EventFd>>& event_fds);
65   void GetHitModulesInBuffer(const char* p, const char* end,
66                              std::vector<std::string>* hit_kernel_modules,
67                              std::vector<std::string>* hit_user_files);
68   bool WriteFileHeader();
69   bool Write(const void* buf, size_t len);
70
71   const std::string filename_;
72   FILE* record_fp_;
73
74   perf_event_attr event_attr_;
75   uint64_t attr_section_offset_;
76   uint64_t attr_section_size_;
77   uint64_t data_section_offset_;
78   uint64_t data_section_size_;
79
80   std::vector<int> features_;
81   int feature_count_;
82   int current_feature_index_;
83
84   DISALLOW_COPY_AND_ASSIGN(RecordFileWriter);
85 };
86
87 // RecordFileReader read contents from a perf record file, like perf.data.
88 class RecordFileReader {
89  public:
90   static std::unique_ptr<RecordFileReader> CreateInstance(const std::string& filename);
91
92   ~RecordFileReader();
93
94   const PerfFileFormat::FileHeader* FileHeader();
95   std::vector<const PerfFileFormat::FileAttr*> AttrSection();
96   std::vector<uint64_t> IdsForAttr(const PerfFileFormat::FileAttr* attr);
97   std::vector<std::unique_ptr<const Record>> DataSection();
98   std::vector<PerfFileFormat::SectionDesc> FeatureSectionDescriptors();
99   const char* DataAtOffset(uint64_t offset) {
100     return mmap_addr_ + offset;
101   }
102   bool Close();
103
104  private:
105   RecordFileReader(const std::string& filename, int fd);
106   bool MmapFile();
107
108   const std::string filename_;
109   int record_fd_;
110
111   const char* mmap_addr_;
112   size_t mmap_len_;
113
114   DISALLOW_COPY_AND_ASSIGN(RecordFileReader);
115 };
116
117 #endif  // SIMPLE_PERF_RECORD_FILE_H_