• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copied from kernel sources. See COPYING for license details.
2 
3 #ifndef PERF_INTERNALS_H_
4 #define PERF_INTERNALS_H_
5 
6 #include <linux/limits.h>
7 #include <stddef.h>  // For NULL
8 #include <stdint.h>
9 #include <sys/types.h>  // For pid_t
10 
11 #include "perf_event.h"
12 
13 namespace quipper {
14 
15 // These typedefs are from tools/perf/util/types.h in the kernel.
16 typedef uint64_t u64;
17 typedef int64_t s64;
18 typedef unsigned int u32;
19 typedef signed int s32;
20 typedef unsigned short u16;
21 typedef signed short s16;
22 typedef unsigned char u8;
23 typedef signed char s8;
24 
25 // The first 64 bits of the perf header, used as a perf data file ID tag.
26 const uint64_t kPerfMagic = 0x32454c4946524550LL;  // "PERFILE2" little-endian
27 
28 // These data structures have been copied from the kernel. See files under
29 // tools/perf/util.
30 
31 //
32 // From tools/perf/util/header.h
33 //
34 
35 enum {
36   HEADER_RESERVED = 0, /* always cleared */
37   HEADER_FIRST_FEATURE = 1,
38   HEADER_TRACING_DATA = 1,
39   HEADER_BUILD_ID,
40 
41   HEADER_HOSTNAME,
42   HEADER_OSRELEASE,
43   HEADER_VERSION,
44   HEADER_ARCH,
45   HEADER_NRCPUS,
46   HEADER_CPUDESC,
47   HEADER_CPUID,
48   HEADER_TOTAL_MEM,
49   HEADER_CMDLINE,
50   HEADER_EVENT_DESC,
51   HEADER_CPU_TOPOLOGY,
52   HEADER_NUMA_TOPOLOGY,
53   HEADER_BRANCH_STACK,
54   HEADER_PMU_MAPPINGS,
55   HEADER_GROUP_DESC,
56   HEADER_LAST_FEATURE,
57   HEADER_FEAT_BITS = 256,
58 };
59 
60 struct perf_file_section {
61   u64 offset;
62   u64 size;
63 };
64 
65 struct perf_file_attr {
66   struct perf_event_attr attr;
67   struct perf_file_section ids;
68 };
69 
70 #define BITS_PER_BYTE 8
71 #define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
72 #define BITS_TO_LONGS(nr) \
73   DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
74 
75 #define DECLARE_BITMAP(name, bits) \
76   unsigned long name[BITS_TO_LONGS(bits)]
77 
78 struct perf_file_header {
79   u64 magic;
80   u64 size;
81   u64 attr_size;
82   struct perf_file_section attrs;
83   struct perf_file_section data;
84   struct perf_file_section event_types;
85   DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS);
86 };
87 
88 #undef BITS_PER_BYTE
89 #undef DIV_ROUND_UP
90 #undef BITS_TO_LONGS
91 #undef DECLARE_BITMAP
92 
93 struct perf_pipe_file_header {
94   u64 magic;
95   u64 size;
96 };
97 
98 //
99 // From tools/perf/util/event.h
100 //
101 
102 struct mmap_event {
103   struct perf_event_header header;
104   u32 pid, tid;
105   u64 start;
106   u64 len;
107   u64 pgoff;
108   char filename[PATH_MAX];
109 };
110 
111 struct mmap2_event {
112   struct perf_event_header header;
113   u32 pid, tid;
114   u64 start;
115   u64 len;
116   u64 pgoff;
117   u32 maj;
118   u32 min;
119   u64 ino;
120   u64 ino_generation;
121   u32 prot;
122   u32 flags;
123   char filename[PATH_MAX];
124 };
125 
126 struct comm_event {
127   struct perf_event_header header;
128   u32 pid, tid;
129   char comm[16];
130 };
131 
132 struct fork_event {
133   struct perf_event_header header;
134   u32 pid, ppid;
135   u32 tid, ptid;
136   u64 time;
137 };
138 
139 struct lost_event {
140   struct perf_event_header header;
141   u64 id;
142   u64 lost;
143 };
144 
145 /*
146  * PERF_FORMAT_ENABLED | PERF_FORMAT_RUNNING | PERF_FORMAT_ID
147  */
148 struct read_event {
149   struct perf_event_header header;
150   u32 pid, tid;
151   u64 value;
152   u64 time_enabled;
153   u64 time_running;
154   u64 id;
155 };
156 
157 struct throttle_event {
158   struct perf_event_header header;
159   u64 time;
160   u64 id;
161   u64 stream_id;
162 };
163 
164 #define PERF_SAMPLE_MASK                                                    \
165   (PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR | \
166    PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID | PERF_SAMPLE_CPU |               \
167    PERF_SAMPLE_PERIOD | PERF_SAMPLE_IDENTIFIER)
168 
169 /* perf sample has 16 bits size limit */
170 #define PERF_SAMPLE_MAX_SIZE (1 << 16)
171 
172 struct sample_event {
173   struct perf_event_header header;
174   u64 array[];
175 };
176 
177 #if 0
178 // PERF_REGS_MAX is arch-dependent, so this is not a useful struct as-is.
179 struct regs_dump {
180   u64 abi;
181   u64 mask;
182   u64 *regs;
183 
184   /* Cached values/mask filled by first register access. */
185   u64 cache_regs[PERF_REGS_MAX];
186   u64 cache_mask;
187 };
188 #endif
189 
190 struct stack_dump {
191   u16 offset;
192   u64 size;
193   char *data;
194 };
195 
196 struct sample_read_value {
197   u64 value;
198   u64 id;
199 };
200 
201 struct sample_read {
202   u64 time_enabled;
203   u64 time_running;
204   struct {
205     struct {
206       u64 nr;
207       struct sample_read_value *values;
208     } group;
209     struct sample_read_value one;
210   };
211 };
212 
213 struct ip_callchain {
214   u64 nr;
215   u64 ips[0];
216 };
217 
218 struct branch_flags {
219   u64 mispred : 1;
220   u64 predicted : 1;
221   u64 in_tx : 1;
222   u64 abort : 1;
223   u64 reserved : 60;
224 };
225 
226 struct branch_entry {
227   u64 from;
228   u64 to;
229   struct branch_flags flags;
230 };
231 
232 struct branch_stack {
233   u64 nr;
234   struct branch_entry entries[0];
235 };
236 
237 // All the possible fields of a perf sample.  This is not an actual data
238 // structure found in raw perf data, as each field may or may not be present in
239 // the data.
240 struct perf_sample {
241   u64 ip;
242   u32 pid, tid;
243   u64 time;
244   u64 addr;
245   u64 id;
246   u64 stream_id;
247   u64 period;
248   u64 weight;
249   u64 transaction;
250   u32 cpu;
251   u32 raw_size;
252   u64 data_src;
253   u32 flags;
254   u16 insn_len;
255   void *raw_data;
256   struct ip_callchain *callchain;
257   struct branch_stack *branch_stack;
258   // struct regs_dump  user_regs;  // See struct regs_dump above.
259   struct stack_dump user_stack;
260   struct sample_read read;
261 
perf_sampleperf_sample262   perf_sample() : raw_data(NULL), callchain(NULL), branch_stack(NULL) {
263     read.group.values = NULL;
264   }
~perf_sampleperf_sample265   ~perf_sample() {
266     delete[] callchain;
267     delete[] branch_stack;
268     delete[] reinterpret_cast<char *>(raw_data);
269     delete[] read.group.values;
270   }
271 };
272 
273 // Taken from tools/perf/util/include/linux/kernel.h
274 #define ALIGN(x, a) __ALIGN_MASK(x, (decltype(x))(a)-1)
275 #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
276 
277 // If this is changed, kBuildIDArraySize in perf_reader.h must also be changed.
278 #define BUILD_ID_SIZE 20
279 
280 struct build_id_event {
281   struct perf_event_header header;
282   pid_t pid;
283   u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))];
284   char filename[];
285 };
286 
287 #undef ALIGN
288 #undef __ALIGN_MASK
289 #undef BUILD_ID_SIZE
290 
291 enum perf_user_event_type {
292   /* above any possible kernel type */
293   PERF_RECORD_USER_TYPE_START = 64,
294   PERF_RECORD_HEADER_ATTR = 64,
295   PERF_RECORD_HEADER_EVENT_TYPE = 65, /* depreceated */
296   PERF_RECORD_HEADER_TRACING_DATA = 66,
297   PERF_RECORD_HEADER_BUILD_ID = 67,
298   PERF_RECORD_FINISHED_ROUND = 68,
299   PERF_RECORD_ID_INDEX = 69,
300   PERF_RECORD_AUXTRACE_INFO = 70,
301   PERF_RECORD_AUXTRACE = 71,
302   PERF_RECORD_AUXTRACE_ERROR = 72,
303   PERF_RECORD_THREAD_MAP = 73,
304   PERF_RECORD_CPU_MAP = 74,
305   PERF_RECORD_STAT_CONFIG = 75,
306   PERF_RECORD_STAT = 76,
307   PERF_RECORD_STAT_ROUND = 77,
308   PERF_RECORD_EVENT_UPDATE = 78,
309   PERF_RECORD_TIME_CONV = 79,
310   PERF_RECORD_HEADER_FEATURE = 80,
311   PERF_RECORD_HEADER_MAX = 81,
312 };
313 
314 struct attr_event {
315   struct perf_event_header header;
316   struct perf_event_attr attr;
317   u64 id[];
318 };
319 
320 #define MAX_EVENT_NAME 64
321 
322 struct perf_trace_event_type {
323   u64 event_id;
324   char name[MAX_EVENT_NAME];
325 };
326 
327 #undef MAX_EVENT_NAME
328 
329 struct event_type_event {
330   struct perf_event_header header;
331   struct perf_trace_event_type event_type;
332 };
333 
334 struct tracing_data_event {
335   struct perf_event_header header;
336   u32 size;
337 };
338 
339 struct auxtrace_event {
340   struct perf_event_header header;
341   u64 size;
342   u64 offset;
343   u64 reference;
344   u32 idx;
345   u32 tid;
346   u32 cpu;
347   u32 reserved__; /* For alignment */
348 };
349 
350 struct aux_event {
351   struct perf_event_header header;
352   u64 aux_offset;
353   u64 aux_size;
354   u64 flags;
355 };
356 
357 union perf_event {
358   struct perf_event_header header;
359   struct mmap_event mmap;
360   struct mmap2_event mmap2;
361   struct comm_event comm;
362   struct fork_event fork;
363   struct lost_event lost;
364   struct read_event read;
365   struct throttle_event throttle;
366   struct sample_event sample;
367   struct attr_event attr;
368   struct event_type_event event_type;
369   struct tracing_data_event tracing_data;
370   struct build_id_event build_id;
371   struct auxtrace_event auxtrace;
372   struct aux_event aux;
373 };
374 
375 typedef perf_event event_t;
376 
377 }  // namespace quipper
378 
379 #endif /*PERF_INTERNALS_H_*/
380