OSDN Git Service

Implement simpleperf record/dumprecord subcommands.
[android-x86/system-extras.git] / simpleperf / record.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_H_
18 #define SIMPLE_PERF_RECORD_H_
19
20 #include <string>
21 #include <vector>
22
23 #include "perf_event.h"
24
25 struct KernelMmap;
26 struct ModuleMmap;
27 struct ThreadComm;
28 struct ThreadMmap;
29
30 enum user_record_type {
31   PERF_RECORD_ATTR = 64,
32   PERF_RECORD_EVENT_TYPE,
33   PERF_RECORD_TRACING_DATA,
34   PERF_RECORD_BUILD_ID,
35   PERF_RECORD_FINISHED_ROUND,
36 };
37
38 struct PerfSampleIpType {
39   uint64_t ip;
40 };
41
42 struct PerfSampleTidType {
43   uint32_t pid, tid;
44 };
45
46 struct PerfSampleTimeType {
47   uint64_t time;
48 };
49
50 struct PerfSampleAddrType {
51   uint64_t addr;
52 };
53
54 struct PerfSampleIdType {
55   uint64_t id;
56 };
57
58 struct PerfSampleStreamIdType {
59   uint64_t stream_id;
60 };
61
62 struct PerfSampleCpuType {
63   uint32_t cpu, res;
64 };
65
66 struct PerfSamplePeriodType {
67   uint64_t period;
68 };
69
70 // SampleId is optional at the end of a record in binary format. Its content is determined by
71 // sample_id_all and sample_type in perf_event_attr. To avoid the complexity of referring to
72 // perf_event_attr each time, we copy sample_id_all and sample_type inside the SampleId structure.
73 struct SampleId {
74   bool sample_id_all;
75   uint64_t sample_type;
76
77   PerfSampleTidType tid_data;             // Valid if sample_id_all && PERF_SAMPLE_TID.
78   PerfSampleTimeType time_data;           // Valid if sample_id_all && PERF_SAMPLE_TIME.
79   PerfSampleIdType id_data;               // Valid if sample_id_all && PERF_SAMPLE_ID.
80   PerfSampleStreamIdType stream_id_data;  // Valid if sample_id_all && PERF_SAMPLE_STREAM_ID.
81   PerfSampleCpuType cpu_data;             // Valid if sample_id_all && PERF_SAMPLE_CPU.
82
83   SampleId();
84
85   // Create the content of sample_id. It depends on the attr we use.
86   size_t CreateContent(const perf_event_attr& attr);
87
88   // Parse sample_id from binary format in the buffer pointed by p.
89   void ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end);
90
91   // Write the binary format of sample_id to the buffer pointed by p.
92   void WriteToBinaryFormat(char*& p) const;
93   void Dump(size_t indent) const;
94 };
95
96 // Usually one record contains the following three parts in order in binary format:
97 //   perf_event_header (at the head of a record, containing type and size information)
98 //   data depends on the record type
99 //   sample_id (optional part at the end of a record)
100 // We hold the common parts (perf_event_header and sample_id) in the base class Record, and
101 // hold the type specific data part in classes derived from Record.
102 struct Record {
103   perf_event_header header;
104   SampleId sample_id;
105
106   Record();
107   Record(const perf_event_header* pheader);
108
109   virtual ~Record() {
110   }
111
112   void Dump(size_t indent = 0) const;
113
114  protected:
115   virtual void DumpData(size_t) const {
116   }
117 };
118
119 struct MmapRecord : public Record {
120   struct MmapRecordDataType {
121     uint32_t pid, tid;
122     uint64_t addr;
123     uint64_t len;
124     uint64_t pgoff;
125   } data;
126   std::string filename;
127
128   MmapRecord() {  // For storage in std::vector.
129   }
130
131   MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader);
132   void DumpData(size_t indent) const override;
133   std::vector<char> BinaryFormat() const;
134 };
135
136 struct CommRecord : public Record {
137   struct CommRecordDataType {
138     uint32_t pid, tid;
139   } data;
140   std::string comm;
141
142   CommRecord() {
143   }
144
145   CommRecord(const perf_event_attr& attr, const perf_event_header* pheader);
146   void DumpData(size_t indent) const override;
147   std::vector<char> BinaryFormat() const;
148 };
149
150 struct ExitRecord : public Record {
151   struct ExitRecordDataType {
152     uint32_t pid, ppid;
153     uint32_t tid, ptid;
154     uint64_t time;
155   } data;
156
157   ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader);
158   void DumpData(size_t indent) const override;
159 };
160
161 struct SampleRecord : public Record {
162   uint64_t sample_type;  // sample_type is a bit mask determining which fields below are valid.
163
164   PerfSampleIpType ip_data;               // Valid if PERF_SAMPLE_IP.
165   PerfSampleTidType tid_data;             // Valid if PERF_SAMPLE_TID.
166   PerfSampleTimeType time_data;           // Valid if PERF_SAMPLE_TIME.
167   PerfSampleAddrType addr_data;           // Valid if PERF_SAMPLE_ADDR.
168   PerfSampleIdType id_data;               // Valid if PERF_SAMPLE_ID.
169   PerfSampleStreamIdType stream_id_data;  // Valid if PERF_SAMPLE_STREAM_ID.
170   PerfSampleCpuType cpu_data;             // Valid if PERF_SAMPLE_CPU.
171   PerfSamplePeriodType period_data;       // Valid if PERF_SAMPLE_PERIOD.
172
173   SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader);
174   void DumpData(size_t indent) const override;
175 };
176
177 std::unique_ptr<const Record> ReadRecordFromBuffer(const perf_event_attr& attr,
178                                                    const perf_event_header* pheader);
179
180 #endif  // SIMPLE_PERF_RECORD_H_