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 struct dso;
16 struct machine;
17 struct perf_event_attr;
18 struct perf_sample;
19 
20 #ifdef __LP64__
21 /*
22  * /usr/include/inttypes.h uses just 'lu' for PRIu64, but we end up defining
23  * __u64 as long long unsigned int, and then -Werror=format= kicks in and
24  * complains of the mismatched types, so use these two special extra PRI
25  * macros to overcome that.
26  */
27 #define PRI_lu64 "l" PRIu64
28 #define PRI_lx64 "l" PRIx64
29 #define PRI_ld64 "l" PRId64
30 #else
31 #define PRI_lu64 PRIu64
32 #define PRI_lx64 PRIx64
33 #define PRI_ld64 PRId64
34 #endif
35 
36 #define PERF_SAMPLE_MASK				\
37 	(PERF_SAMPLE_IP | PERF_SAMPLE_TID |		\
38 	 PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR |		\
39 	PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID |	\
40 	 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD |		\
41 	 PERF_SAMPLE_IDENTIFIER)
42 
43 /* perf sample has 16 bits size limit */
44 #define PERF_SAMPLE_MAX_SIZE (1 << 16)
45 
46 struct ip_callchain {
47 	u64 nr;
48 	u64 ips[];
49 };
50 
51 struct branch_stack;
52 
53 enum {
54 	PERF_IP_FLAG_BRANCH		= 1ULL << 0,
55 	PERF_IP_FLAG_CALL		= 1ULL << 1,
56 	PERF_IP_FLAG_RETURN		= 1ULL << 2,
57 	PERF_IP_FLAG_CONDITIONAL	= 1ULL << 3,
58 	PERF_IP_FLAG_SYSCALLRET		= 1ULL << 4,
59 	PERF_IP_FLAG_ASYNC		= 1ULL << 5,
60 	PERF_IP_FLAG_INTERRUPT		= 1ULL << 6,
61 	PERF_IP_FLAG_TX_ABORT		= 1ULL << 7,
62 	PERF_IP_FLAG_TRACE_BEGIN	= 1ULL << 8,
63 	PERF_IP_FLAG_TRACE_END		= 1ULL << 9,
64 	PERF_IP_FLAG_IN_TX		= 1ULL << 10,
65 	PERF_IP_FLAG_VMENTRY		= 1ULL << 11,
66 	PERF_IP_FLAG_VMEXIT		= 1ULL << 12,
67 	PERF_IP_FLAG_INTR_DISABLE	= 1ULL << 13,
68 	PERF_IP_FLAG_INTR_TOGGLE	= 1ULL << 14,
69 };
70 
71 #define PERF_IP_FLAG_CHARS "bcrosyiABExghDt"
72 
73 #define PERF_BRANCH_MASK		(\
74 	PERF_IP_FLAG_BRANCH		|\
75 	PERF_IP_FLAG_CALL		|\
76 	PERF_IP_FLAG_RETURN		|\
77 	PERF_IP_FLAG_CONDITIONAL	|\
78 	PERF_IP_FLAG_SYSCALLRET		|\
79 	PERF_IP_FLAG_ASYNC		|\
80 	PERF_IP_FLAG_INTERRUPT		|\
81 	PERF_IP_FLAG_TX_ABORT		|\
82 	PERF_IP_FLAG_TRACE_BEGIN	|\
83 	PERF_IP_FLAG_TRACE_END		|\
84 	PERF_IP_FLAG_VMENTRY		|\
85 	PERF_IP_FLAG_VMEXIT)
86 
87 #define PERF_MEM_DATA_SRC_NONE \
88 	(PERF_MEM_S(OP, NA) |\
89 	 PERF_MEM_S(LVL, NA) |\
90 	 PERF_MEM_S(SNOOP, NA) |\
91 	 PERF_MEM_S(LOCK, NA) |\
92 	 PERF_MEM_S(TLB, NA))
93 
94 /* Attribute type for custom synthesized events */
95 #define PERF_TYPE_SYNTH		(INT_MAX + 1U)
96 
97 /* Attribute config for custom synthesized events */
98 enum perf_synth_id {
99 	PERF_SYNTH_INTEL_PTWRITE,
100 	PERF_SYNTH_INTEL_MWAIT,
101 	PERF_SYNTH_INTEL_PWRE,
102 	PERF_SYNTH_INTEL_EXSTOP,
103 	PERF_SYNTH_INTEL_PWRX,
104 	PERF_SYNTH_INTEL_CBR,
105 	PERF_SYNTH_INTEL_PSB,
106 	PERF_SYNTH_INTEL_EVT,
107 	PERF_SYNTH_INTEL_IFLAG_CHG,
108 };
109 
110 /*
111  * Raw data formats for synthesized events. Note that 4 bytes of padding are
112  * present to match the 'size' member of PERF_SAMPLE_RAW data which is always
113  * 8-byte aligned. That means we must dereference raw_data with an offset of 4.
114  * Refer perf_sample__synth_ptr() and perf_synth__raw_data().  It also means the
115  * structure sizes are 4 bytes bigger than the raw_size, refer
116  * perf_synth__raw_size().
117  */
118 
119 struct perf_synth_intel_ptwrite {
120 	u32 padding;
121 	union {
122 		struct {
123 			u32	ip		:  1,
124 				reserved	: 31;
125 		};
126 		u32	flags;
127 	};
128 	u64	payload;
129 };
130 
131 struct perf_synth_intel_mwait {
132 	u32 padding;
133 	u32 reserved;
134 	union {
135 		struct {
136 			u64	hints		:  8,
137 				reserved1	: 24,
138 				extensions	:  2,
139 				reserved2	: 30;
140 		};
141 		u64	payload;
142 	};
143 };
144 
145 struct perf_synth_intel_pwre {
146 	u32 padding;
147 	u32 reserved;
148 	union {
149 		struct {
150 			u64	reserved1	:  7,
151 				hw		:  1,
152 				subcstate	:  4,
153 				cstate		:  4,
154 				reserved2	: 48;
155 		};
156 		u64	payload;
157 	};
158 };
159 
160 struct perf_synth_intel_exstop {
161 	u32 padding;
162 	union {
163 		struct {
164 			u32	ip		:  1,
165 				reserved	: 31;
166 		};
167 		u32	flags;
168 	};
169 };
170 
171 struct perf_synth_intel_pwrx {
172 	u32 padding;
173 	u32 reserved;
174 	union {
175 		struct {
176 			u64	deepest_cstate	:  4,
177 				last_cstate	:  4,
178 				wake_reason	:  4,
179 				reserved1	: 52;
180 		};
181 		u64	payload;
182 	};
183 };
184 
185 struct perf_synth_intel_cbr {
186 	u32 padding;
187 	union {
188 		struct {
189 			u32	cbr		:  8,
190 				reserved1	:  8,
191 				max_nonturbo	:  8,
192 				reserved2	:  8;
193 		};
194 		u32	flags;
195 	};
196 	u32 freq;
197 	u32 reserved3;
198 };
199 
200 struct perf_synth_intel_psb {
201 	u32 padding;
202 	u32 reserved;
203 	u64 offset;
204 };
205 
206 struct perf_synth_intel_evd {
207 	union {
208 		struct {
209 			u8	evd_type;
210 			u8	reserved[7];
211 		};
212 		u64	et;
213 	};
214 	u64	payload;
215 };
216 
217 /* Intel PT Event Trace */
218 struct perf_synth_intel_evt {
219 	u32 padding;
220 	union {
221 		struct {
222 			u32	type		:  5,
223 				reserved	:  2,
224 				ip		:  1,
225 				vector		:  8,
226 				evd_cnt		: 16;
227 		};
228 		u32	cfe;
229 	};
230 	struct perf_synth_intel_evd evd[0];
231 };
232 
233 struct perf_synth_intel_iflag_chg {
234 	u32 padding;
235 	union {
236 		struct {
237 			u32	iflag		:  1,
238 				via_branch	:  1;
239 		};
240 		u32	flags;
241 	};
242 	u64	branch_ip; /* If via_branch */
243 };
244 
perf_synth__raw_data(void * p)245 static inline void *perf_synth__raw_data(void *p)
246 {
247 	return p + 4;
248 }
249 
250 #define perf_synth__raw_size(d) (sizeof(d) - 4)
251 
252 #define perf_sample__bad_synth_size(s, d) ((s)->raw_size < sizeof(d) - 4)
253 
254 enum {
255 	PERF_STAT_ROUND_TYPE__INTERVAL	= 0,
256 	PERF_STAT_ROUND_TYPE__FINAL	= 1,
257 };
258 
259 void perf_event__print_totals(void);
260 
261 struct perf_cpu_map;
262 struct perf_record_stat_config;
263 struct perf_stat_config;
264 struct perf_tool;
265 
266 void perf_event__read_stat_config(struct perf_stat_config *config,
267 				  struct perf_record_stat_config *event);
268 
269 int perf_event__process_comm(struct perf_tool *tool,
270 			     union perf_event *event,
271 			     struct perf_sample *sample,
272 			     struct machine *machine);
273 int perf_event__process_lost(struct perf_tool *tool,
274 			     union perf_event *event,
275 			     struct perf_sample *sample,
276 			     struct machine *machine);
277 int perf_event__process_lost_samples(struct perf_tool *tool,
278 				     union perf_event *event,
279 				     struct perf_sample *sample,
280 				     struct machine *machine);
281 int perf_event__process_aux(struct perf_tool *tool,
282 			    union perf_event *event,
283 			    struct perf_sample *sample,
284 			    struct machine *machine);
285 int perf_event__process_itrace_start(struct perf_tool *tool,
286 				     union perf_event *event,
287 				     struct perf_sample *sample,
288 				     struct machine *machine);
289 int perf_event__process_aux_output_hw_id(struct perf_tool *tool,
290 					 union perf_event *event,
291 					 struct perf_sample *sample,
292 					 struct machine *machine);
293 int perf_event__process_switch(struct perf_tool *tool,
294 			       union perf_event *event,
295 			       struct perf_sample *sample,
296 			       struct machine *machine);
297 int perf_event__process_namespaces(struct perf_tool *tool,
298 				   union perf_event *event,
299 				   struct perf_sample *sample,
300 				   struct machine *machine);
301 int perf_event__process_cgroup(struct perf_tool *tool,
302 			       union perf_event *event,
303 			       struct perf_sample *sample,
304 			       struct machine *machine);
305 int perf_event__process_mmap(struct perf_tool *tool,
306 			     union perf_event *event,
307 			     struct perf_sample *sample,
308 			     struct machine *machine);
309 int perf_event__process_mmap2(struct perf_tool *tool,
310 			     union perf_event *event,
311 			     struct perf_sample *sample,
312 			     struct machine *machine);
313 int perf_event__process_fork(struct perf_tool *tool,
314 			     union perf_event *event,
315 			     struct perf_sample *sample,
316 			     struct machine *machine);
317 int perf_event__process_exit(struct perf_tool *tool,
318 			     union perf_event *event,
319 			     struct perf_sample *sample,
320 			     struct machine *machine);
321 int perf_event__process_ksymbol(struct perf_tool *tool,
322 				union perf_event *event,
323 				struct perf_sample *sample,
324 				struct machine *machine);
325 int perf_event__process_bpf(struct perf_tool *tool,
326 			    union perf_event *event,
327 			    struct perf_sample *sample,
328 			    struct machine *machine);
329 int perf_event__process_text_poke(struct perf_tool *tool,
330 				  union perf_event *event,
331 				  struct perf_sample *sample,
332 				  struct machine *machine);
333 int perf_event__process(struct perf_tool *tool,
334 			union perf_event *event,
335 			struct perf_sample *sample,
336 			struct machine *machine);
337 
338 bool is_bts_event(struct perf_event_attr *attr);
339 bool sample_addr_correlates_sym(struct perf_event_attr *attr);
340 
341 const char *perf_event__name(unsigned int id);
342 
343 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp);
344 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp);
345 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp);
346 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp);
347 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp);
348 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp);
349 size_t perf_event__fprintf_aux_output_hw_id(union perf_event *event, FILE *fp);
350 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp);
351 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp);
352 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp);
353 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp);
354 size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp);
355 size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp);
356 size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp);
357 size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *machine,FILE *fp);
358 size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILE *fp);
359 
360 int kallsyms__get_function_start(const char *kallsyms_filename,
361 				 const char *symbol_name, u64 *addr);
362 
363 void event_attr_init(struct perf_event_attr *attr);
364 
365 int perf_event_paranoid(void);
366 bool perf_event_paranoid_check(int max_level);
367 
368 extern int sysctl_perf_event_max_stack;
369 extern int sysctl_perf_event_max_contexts_per_stack;
370 extern unsigned int proc_map_timeout;
371 
372 #define PAGE_SIZE_NAME_LEN	32
373 char *get_page_size_name(u64 size, char *str);
374 
375 void arch_perf_parse_sample_weight(struct perf_sample *data, const __u64 *array, u64 type);
376 void arch_perf_synthesize_sample_weight(const struct perf_sample *data, __u64 *array, u64 type);
377 const char *arch_perf_header_entry(const char *se_header);
378 int arch_support_sort_key(const char *sort_key);
379 
perf_event_header__cpumode_is_guest(u8 cpumode)380 static inline bool perf_event_header__cpumode_is_guest(u8 cpumode)
381 {
382 	return cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
383 	       cpumode == PERF_RECORD_MISC_GUEST_USER;
384 }
385 
perf_event_header__misc_is_guest(u16 misc)386 static inline bool perf_event_header__misc_is_guest(u16 misc)
387 {
388 	return perf_event_header__cpumode_is_guest(misc & PERF_RECORD_MISC_CPUMODE_MASK);
389 }
390 
perf_event_header__is_guest(const struct perf_event_header * header)391 static inline bool perf_event_header__is_guest(const struct perf_event_header *header)
392 {
393 	return perf_event_header__misc_is_guest(header->misc);
394 }
395 
perf_event__is_guest(const union perf_event * event)396 static inline bool perf_event__is_guest(const union perf_event *event)
397 {
398 	return perf_event_header__is_guest(&event->header);
399 }
400 
401 #endif /* __PERF_RECORD_H */
402