xref: /openbmc/linux/tools/perf/util/event.h (revision 62eab49f)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_RECORD_H
3 #define __PERF_RECORD_H
4 /*
5  * The linux/stddef.h isn't need here, but is needed for __always_inline used
6  * in files included from uapi/linux/perf_event.h such as
7  * /usr/include/linux/swab.h and /usr/include/linux/byteorder/little_endian.h,
8  * detected in at least musl libc, used in Alpine Linux. -acme
9  */
10 #include <stdio.h>
11 #include <linux/stddef.h>
12 #include <perf/event.h>
13 #include <linux/types.h>
14 
15 #include "perf_regs.h"
16 
17 struct dso;
18 struct machine;
19 struct perf_event_attr;
20 
21 #ifdef __LP64__
22 /*
23  * /usr/include/inttypes.h uses just 'lu' for PRIu64, but we end up defining
24  * __u64 as long long unsigned int, and then -Werror=format= kicks in and
25  * complains of the mismatched types, so use these two special extra PRI
26  * macros to overcome that.
27  */
28 #define PRI_lu64 "l" PRIu64
29 #define PRI_lx64 "l" PRIx64
30 #define PRI_ld64 "l" PRId64
31 #else
32 #define PRI_lu64 PRIu64
33 #define PRI_lx64 PRIx64
34 #define PRI_ld64 PRId64
35 #endif
36 
37 #define PERF_SAMPLE_MASK				\
38 	(PERF_SAMPLE_IP | PERF_SAMPLE_TID |		\
39 	 PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR |		\
40 	PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID |	\
41 	 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD |		\
42 	 PERF_SAMPLE_IDENTIFIER)
43 
44 /* perf sample has 16 bits size limit */
45 #define PERF_SAMPLE_MAX_SIZE (1 << 16)
46 
47 struct regs_dump {
48 	u64 abi;
49 	u64 mask;
50 	u64 *regs;
51 
52 	/* Cached values/mask filled by first register access. */
53 	u64 cache_regs[PERF_REGS_MAX];
54 	u64 cache_mask;
55 };
56 
57 struct stack_dump {
58 	u16 offset;
59 	u64 size;
60 	char *data;
61 };
62 
63 struct sample_read_value {
64 	u64 value;
65 	u64 id;
66 };
67 
68 struct sample_read {
69 	u64 time_enabled;
70 	u64 time_running;
71 	union {
72 		struct {
73 			u64 nr;
74 			struct sample_read_value *values;
75 		} group;
76 		struct sample_read_value one;
77 	};
78 };
79 
80 struct ip_callchain {
81 	u64 nr;
82 	u64 ips[];
83 };
84 
85 struct branch_stack;
86 
87 enum {
88 	PERF_IP_FLAG_BRANCH		= 1ULL << 0,
89 	PERF_IP_FLAG_CALL		= 1ULL << 1,
90 	PERF_IP_FLAG_RETURN		= 1ULL << 2,
91 	PERF_IP_FLAG_CONDITIONAL	= 1ULL << 3,
92 	PERF_IP_FLAG_SYSCALLRET		= 1ULL << 4,
93 	PERF_IP_FLAG_ASYNC		= 1ULL << 5,
94 	PERF_IP_FLAG_INTERRUPT		= 1ULL << 6,
95 	PERF_IP_FLAG_TX_ABORT		= 1ULL << 7,
96 	PERF_IP_FLAG_TRACE_BEGIN	= 1ULL << 8,
97 	PERF_IP_FLAG_TRACE_END		= 1ULL << 9,
98 	PERF_IP_FLAG_IN_TX		= 1ULL << 10,
99 	PERF_IP_FLAG_VMENTRY		= 1ULL << 11,
100 	PERF_IP_FLAG_VMEXIT		= 1ULL << 12,
101 };
102 
103 #define PERF_IP_FLAG_CHARS "bcrosyiABEx"
104 
105 #define PERF_BRANCH_MASK		(\
106 	PERF_IP_FLAG_BRANCH		|\
107 	PERF_IP_FLAG_CALL		|\
108 	PERF_IP_FLAG_RETURN		|\
109 	PERF_IP_FLAG_CONDITIONAL	|\
110 	PERF_IP_FLAG_SYSCALLRET		|\
111 	PERF_IP_FLAG_ASYNC		|\
112 	PERF_IP_FLAG_INTERRUPT		|\
113 	PERF_IP_FLAG_TX_ABORT		|\
114 	PERF_IP_FLAG_TRACE_BEGIN	|\
115 	PERF_IP_FLAG_TRACE_END		|\
116 	PERF_IP_FLAG_VMENTRY		|\
117 	PERF_IP_FLAG_VMEXIT)
118 
119 #define MAX_INSN 16
120 
121 struct aux_sample {
122 	u64 size;
123 	void *data;
124 };
125 
126 struct perf_sample {
127 	u64 ip;
128 	u32 pid, tid;
129 	u64 time;
130 	u64 addr;
131 	u64 id;
132 	u64 stream_id;
133 	u64 period;
134 	u64 weight;
135 	u64 transaction;
136 	u64 insn_cnt;
137 	u64 cyc_cnt;
138 	u32 cpu;
139 	u32 raw_size;
140 	u64 data_src;
141 	u64 phys_addr;
142 	u64 data_page_size;
143 	u64 code_page_size;
144 	u64 cgroup;
145 	u32 flags;
146 	u16 insn_len;
147 	u8  cpumode;
148 	u16 misc;
149 	u16 ins_lat;
150 	bool no_hw_idx;		/* No hw_idx collected in branch_stack */
151 	char insn[MAX_INSN];
152 	void *raw_data;
153 	struct ip_callchain *callchain;
154 	struct branch_stack *branch_stack;
155 	struct regs_dump  user_regs;
156 	struct regs_dump  intr_regs;
157 	struct stack_dump user_stack;
158 	struct sample_read read;
159 	struct aux_sample aux_sample;
160 };
161 
162 #define PERF_MEM_DATA_SRC_NONE \
163 	(PERF_MEM_S(OP, NA) |\
164 	 PERF_MEM_S(LVL, NA) |\
165 	 PERF_MEM_S(SNOOP, NA) |\
166 	 PERF_MEM_S(LOCK, NA) |\
167 	 PERF_MEM_S(TLB, NA))
168 
169 /* Attribute type for custom synthesized events */
170 #define PERF_TYPE_SYNTH		(INT_MAX + 1U)
171 
172 /* Attribute config for custom synthesized events */
173 enum perf_synth_id {
174 	PERF_SYNTH_INTEL_PTWRITE,
175 	PERF_SYNTH_INTEL_MWAIT,
176 	PERF_SYNTH_INTEL_PWRE,
177 	PERF_SYNTH_INTEL_EXSTOP,
178 	PERF_SYNTH_INTEL_PWRX,
179 	PERF_SYNTH_INTEL_CBR,
180 	PERF_SYNTH_INTEL_PSB,
181 };
182 
183 /*
184  * Raw data formats for synthesized events. Note that 4 bytes of padding are
185  * present to match the 'size' member of PERF_SAMPLE_RAW data which is always
186  * 8-byte aligned. That means we must dereference raw_data with an offset of 4.
187  * Refer perf_sample__synth_ptr() and perf_synth__raw_data().  It also means the
188  * structure sizes are 4 bytes bigger than the raw_size, refer
189  * perf_synth__raw_size().
190  */
191 
192 struct perf_synth_intel_ptwrite {
193 	u32 padding;
194 	union {
195 		struct {
196 			u32	ip		:  1,
197 				reserved	: 31;
198 		};
199 		u32	flags;
200 	};
201 	u64	payload;
202 };
203 
204 struct perf_synth_intel_mwait {
205 	u32 padding;
206 	u32 reserved;
207 	union {
208 		struct {
209 			u64	hints		:  8,
210 				reserved1	: 24,
211 				extensions	:  2,
212 				reserved2	: 30;
213 		};
214 		u64	payload;
215 	};
216 };
217 
218 struct perf_synth_intel_pwre {
219 	u32 padding;
220 	u32 reserved;
221 	union {
222 		struct {
223 			u64	reserved1	:  7,
224 				hw		:  1,
225 				subcstate	:  4,
226 				cstate		:  4,
227 				reserved2	: 48;
228 		};
229 		u64	payload;
230 	};
231 };
232 
233 struct perf_synth_intel_exstop {
234 	u32 padding;
235 	union {
236 		struct {
237 			u32	ip		:  1,
238 				reserved	: 31;
239 		};
240 		u32	flags;
241 	};
242 };
243 
244 struct perf_synth_intel_pwrx {
245 	u32 padding;
246 	u32 reserved;
247 	union {
248 		struct {
249 			u64	deepest_cstate	:  4,
250 				last_cstate	:  4,
251 				wake_reason	:  4,
252 				reserved1	: 52;
253 		};
254 		u64	payload;
255 	};
256 };
257 
258 struct perf_synth_intel_cbr {
259 	u32 padding;
260 	union {
261 		struct {
262 			u32	cbr		:  8,
263 				reserved1	:  8,
264 				max_nonturbo	:  8,
265 				reserved2	:  8;
266 		};
267 		u32	flags;
268 	};
269 	u32 freq;
270 	u32 reserved3;
271 };
272 
273 struct perf_synth_intel_psb {
274 	u32 padding;
275 	u32 reserved;
276 	u64 offset;
277 };
278 
279 /*
280  * raw_data is always 4 bytes from an 8-byte boundary, so subtract 4 to get
281  * 8-byte alignment.
282  */
283 static inline void *perf_sample__synth_ptr(struct perf_sample *sample)
284 {
285 	return sample->raw_data - 4;
286 }
287 
288 static inline void *perf_synth__raw_data(void *p)
289 {
290 	return p + 4;
291 }
292 
293 #define perf_synth__raw_size(d) (sizeof(d) - 4)
294 
295 #define perf_sample__bad_synth_size(s, d) ((s)->raw_size < sizeof(d) - 4)
296 
297 enum {
298 	PERF_STAT_ROUND_TYPE__INTERVAL	= 0,
299 	PERF_STAT_ROUND_TYPE__FINAL	= 1,
300 };
301 
302 void perf_event__print_totals(void);
303 
304 struct perf_cpu_map;
305 struct perf_record_stat_config;
306 struct perf_stat_config;
307 struct perf_tool;
308 
309 void perf_event__read_stat_config(struct perf_stat_config *config,
310 				  struct perf_record_stat_config *event);
311 
312 int perf_event__process_comm(struct perf_tool *tool,
313 			     union perf_event *event,
314 			     struct perf_sample *sample,
315 			     struct machine *machine);
316 int perf_event__process_lost(struct perf_tool *tool,
317 			     union perf_event *event,
318 			     struct perf_sample *sample,
319 			     struct machine *machine);
320 int perf_event__process_lost_samples(struct perf_tool *tool,
321 				     union perf_event *event,
322 				     struct perf_sample *sample,
323 				     struct machine *machine);
324 int perf_event__process_aux(struct perf_tool *tool,
325 			    union perf_event *event,
326 			    struct perf_sample *sample,
327 			    struct machine *machine);
328 int perf_event__process_itrace_start(struct perf_tool *tool,
329 				     union perf_event *event,
330 				     struct perf_sample *sample,
331 				     struct machine *machine);
332 int perf_event__process_switch(struct perf_tool *tool,
333 			       union perf_event *event,
334 			       struct perf_sample *sample,
335 			       struct machine *machine);
336 int perf_event__process_namespaces(struct perf_tool *tool,
337 				   union perf_event *event,
338 				   struct perf_sample *sample,
339 				   struct machine *machine);
340 int perf_event__process_cgroup(struct perf_tool *tool,
341 			       union perf_event *event,
342 			       struct perf_sample *sample,
343 			       struct machine *machine);
344 int perf_event__process_mmap(struct perf_tool *tool,
345 			     union perf_event *event,
346 			     struct perf_sample *sample,
347 			     struct machine *machine);
348 int perf_event__process_mmap2(struct perf_tool *tool,
349 			     union perf_event *event,
350 			     struct perf_sample *sample,
351 			     struct machine *machine);
352 int perf_event__process_fork(struct perf_tool *tool,
353 			     union perf_event *event,
354 			     struct perf_sample *sample,
355 			     struct machine *machine);
356 int perf_event__process_exit(struct perf_tool *tool,
357 			     union perf_event *event,
358 			     struct perf_sample *sample,
359 			     struct machine *machine);
360 int perf_event__process_ksymbol(struct perf_tool *tool,
361 				union perf_event *event,
362 				struct perf_sample *sample,
363 				struct machine *machine);
364 int perf_event__process_bpf(struct perf_tool *tool,
365 			    union perf_event *event,
366 			    struct perf_sample *sample,
367 			    struct machine *machine);
368 int perf_event__process_text_poke(struct perf_tool *tool,
369 				  union perf_event *event,
370 				  struct perf_sample *sample,
371 				  struct machine *machine);
372 int perf_event__process(struct perf_tool *tool,
373 			union perf_event *event,
374 			struct perf_sample *sample,
375 			struct machine *machine);
376 
377 struct addr_location;
378 
379 int machine__resolve(struct machine *machine, struct addr_location *al,
380 		     struct perf_sample *sample);
381 
382 void addr_location__put(struct addr_location *al);
383 
384 struct thread;
385 
386 bool is_bts_event(struct perf_event_attr *attr);
387 bool sample_addr_correlates_sym(struct perf_event_attr *attr);
388 void thread__resolve(struct thread *thread, struct addr_location *al,
389 		     struct perf_sample *sample);
390 
391 const char *perf_event__name(unsigned int id);
392 
393 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp);
394 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp);
395 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp);
396 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp);
397 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp);
398 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp);
399 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp);
400 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp);
401 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp);
402 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp);
403 size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp);
404 size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp);
405 size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp);
406 size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *machine,FILE *fp);
407 size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILE *fp);
408 
409 int kallsyms__get_function_start(const char *kallsyms_filename,
410 				 const char *symbol_name, u64 *addr);
411 
412 void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max);
413 void  cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
414 			       u16 type, int max);
415 
416 void event_attr_init(struct perf_event_attr *attr);
417 
418 int perf_event_paranoid(void);
419 bool perf_event_paranoid_check(int max_level);
420 
421 extern int sysctl_perf_event_max_stack;
422 extern int sysctl_perf_event_max_contexts_per_stack;
423 extern unsigned int proc_map_timeout;
424 
425 #define PAGE_SIZE_NAME_LEN	32
426 char *get_page_size_name(u64 size, char *str);
427 
428 void arch_perf_parse_sample_weight(struct perf_sample *data, const __u64 *array, u64 type);
429 void arch_perf_synthesize_sample_weight(const struct perf_sample *data, __u64 *array, u64 type);
430 
431 #endif /* __PERF_RECORD_H */
432