event.h 10.8 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2 3
#ifndef __PERF_RECORD_H
#define __PERF_RECORD_H
4 5 6 7 8 9
/*
 * The linux/stddef.h isn't need here, but is needed for __always_inline used
 * in files included from uapi/linux/perf_event.h such as
 * /usr/include/linux/swab.h and /usr/include/linux/byteorder/little_endian.h,
 * detected in at least musl libc, used in Alpine Linux. -acme
 */
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 18 19 20
struct dso;
struct machine;
struct perf_event_attr;

21 22 23 24 25 26 27 28 29
#ifdef __LP64__
/*
 * /usr/include/inttypes.h uses just 'lu' for PRIu64, but we end up defining
 * __u64 as long long unsigned int, and then -Werror=format= kicks in and
 * complains of the mismatched types, so use these two special extra PRI
 * macros to overcome that.
 */
#define PRI_lu64 "l" PRIu64
#define PRI_lx64 "l" PRIx64
30
#define PRI_ld64 "l" PRId64
31 32 33
#else
#define PRI_lu64 PRIu64
#define PRI_lx64 PRIx64
34
#define PRI_ld64 PRId64
35
#endif
36

37 38 39 40
#define PERF_SAMPLE_MASK				\
	(PERF_SAMPLE_IP | PERF_SAMPLE_TID |		\
	 PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR |		\
	PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID |	\
41 42
	 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD |		\
	 PERF_SAMPLE_IDENTIFIER)
43

44 45 46
/* perf sample has 16 bits size limit */
#define PERF_SAMPLE_MAX_SIZE (1 << 16)

47
struct regs_dump {
48
	u64 abi;
49
	u64 mask;
50
	u64 *regs;
51 52 53 54

	/* Cached values/mask filled by first register access. */
	u64 cache_regs[PERF_REGS_MAX];
	u64 cache_mask;
55 56 57 58 59 60 61 62
};

struct stack_dump {
	u16 offset;
	u64 size;
	char *data;
};

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
struct sample_read_value {
	u64 value;
	u64 id;
};

struct sample_read {
	u64 time_enabled;
	u64 time_running;
	union {
		struct {
			u64 nr;
			struct sample_read_value *values;
		} group;
		struct sample_read_value one;
	};
};

80 81
struct ip_callchain {
	u64 nr;
82
	u64 ips[];
83 84
};

85
struct branch_stack;
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100
enum {
	PERF_IP_FLAG_BRANCH		= 1ULL << 0,
	PERF_IP_FLAG_CALL		= 1ULL << 1,
	PERF_IP_FLAG_RETURN		= 1ULL << 2,
	PERF_IP_FLAG_CONDITIONAL	= 1ULL << 3,
	PERF_IP_FLAG_SYSCALLRET		= 1ULL << 4,
	PERF_IP_FLAG_ASYNC		= 1ULL << 5,
	PERF_IP_FLAG_INTERRUPT		= 1ULL << 6,
	PERF_IP_FLAG_TX_ABORT		= 1ULL << 7,
	PERF_IP_FLAG_TRACE_BEGIN	= 1ULL << 8,
	PERF_IP_FLAG_TRACE_END		= 1ULL << 9,
	PERF_IP_FLAG_IN_TX		= 1ULL << 10,
};

101 102
#define PERF_IP_FLAG_CHARS "bcrosyiABEx"

103 104 105 106 107 108 109 110 111 112 113 114
#define PERF_BRANCH_MASK		(\
	PERF_IP_FLAG_BRANCH		|\
	PERF_IP_FLAG_CALL		|\
	PERF_IP_FLAG_RETURN		|\
	PERF_IP_FLAG_CONDITIONAL	|\
	PERF_IP_FLAG_SYSCALLRET		|\
	PERF_IP_FLAG_ASYNC		|\
	PERF_IP_FLAG_INTERRUPT		|\
	PERF_IP_FLAG_TX_ABORT		|\
	PERF_IP_FLAG_TRACE_BEGIN	|\
	PERF_IP_FLAG_TRACE_END)

115 116
#define MAX_INSN 16

117 118 119 120 121
struct aux_sample {
	u64 size;
	void *data;
};

122
struct perf_sample {
123 124 125 126 127 128 129
	u64 ip;
	u32 pid, tid;
	u64 time;
	u64 addr;
	u64 id;
	u64 stream_id;
	u64 period;
130
	u64 weight;
131
	u64 transaction;
132 133
	u64 insn_cnt;
	u64 cyc_cnt;
134
	u32 cpu;
135
	u32 raw_size;
136
	u64 data_src;
137
	u64 phys_addr;
138
	u64 data_page_size;
139
	u64 code_page_size;
140
	u64 cgroup;
141 142
	u32 flags;
	u16 insn_len;
143
	u8  cpumode;
144
	u16 misc;
145
	u16 ins_lat;
146
	bool no_hw_idx;		/* No hw_idx collected in branch_stack */
147
	char insn[MAX_INSN];
148
	void *raw_data;
149
	struct ip_callchain *callchain;
150
	struct branch_stack *branch_stack;
151
	struct regs_dump  user_regs;
152
	struct regs_dump  intr_regs;
153
	struct stack_dump user_stack;
154
	struct sample_read read;
155
	struct aux_sample aux_sample;
156 157
};

158 159 160 161 162 163 164
#define PERF_MEM_DATA_SRC_NONE \
	(PERF_MEM_S(OP, NA) |\
	 PERF_MEM_S(LVL, NA) |\
	 PERF_MEM_S(SNOOP, NA) |\
	 PERF_MEM_S(LOCK, NA) |\
	 PERF_MEM_S(TLB, NA))

165 166 167
/* Attribute type for custom synthesized events */
#define PERF_TYPE_SYNTH		(INT_MAX + 1U)

168 169 170 171 172 173 174 175
/* Attribute config for custom synthesized events */
enum perf_synth_id {
	PERF_SYNTH_INTEL_PTWRITE,
	PERF_SYNTH_INTEL_MWAIT,
	PERF_SYNTH_INTEL_PWRE,
	PERF_SYNTH_INTEL_EXSTOP,
	PERF_SYNTH_INTEL_PWRX,
	PERF_SYNTH_INTEL_CBR,
A
Adrian Hunter 已提交
176
	PERF_SYNTH_INTEL_PSB,
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
};

/*
 * Raw data formats for synthesized events. Note that 4 bytes of padding are
 * present to match the 'size' member of PERF_SAMPLE_RAW data which is always
 * 8-byte aligned. That means we must dereference raw_data with an offset of 4.
 * Refer perf_sample__synth_ptr() and perf_synth__raw_data().  It also means the
 * structure sizes are 4 bytes bigger than the raw_size, refer
 * perf_synth__raw_size().
 */

struct perf_synth_intel_ptwrite {
	u32 padding;
	union {
		struct {
			u32	ip		:  1,
				reserved	: 31;
		};
		u32	flags;
	};
	u64	payload;
};

struct perf_synth_intel_mwait {
	u32 padding;
	u32 reserved;
	union {
		struct {
			u64	hints		:  8,
				reserved1	: 24,
				extensions	:  2,
				reserved2	: 30;
		};
		u64	payload;
	};
};

struct perf_synth_intel_pwre {
	u32 padding;
	u32 reserved;
	union {
		struct {
			u64	reserved1	:  7,
				hw		:  1,
				subcstate	:  4,
				cstate		:  4,
				reserved2	: 48;
		};
		u64	payload;
	};
};

struct perf_synth_intel_exstop {
	u32 padding;
	union {
		struct {
			u32	ip		:  1,
				reserved	: 31;
		};
		u32	flags;
	};
};

struct perf_synth_intel_pwrx {
	u32 padding;
	u32 reserved;
	union {
		struct {
			u64	deepest_cstate	:  4,
				last_cstate	:  4,
				wake_reason	:  4,
				reserved1	: 52;
		};
		u64	payload;
	};
};

struct perf_synth_intel_cbr {
	u32 padding;
	union {
		struct {
			u32	cbr		:  8,
				reserved1	:  8,
				max_nonturbo	:  8,
				reserved2	:  8;
		};
		u32	flags;
	};
	u32 freq;
	u32 reserved3;
};

A
Adrian Hunter 已提交
269 270 271 272 273 274
struct perf_synth_intel_psb {
	u32 padding;
	u32 reserved;
	u64 offset;
};

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
/*
 * raw_data is always 4 bytes from an 8-byte boundary, so subtract 4 to get
 * 8-byte alignment.
 */
static inline void *perf_sample__synth_ptr(struct perf_sample *sample)
{
	return sample->raw_data - 4;
}

static inline void *perf_synth__raw_data(void *p)
{
	return p + 4;
}

#define perf_synth__raw_size(d) (sizeof(d) - 4)

#define perf_sample__bad_synth_size(s, d) ((s)->raw_size < sizeof(d) - 4)

293 294 295 296 297
enum {
	PERF_STAT_ROUND_TYPE__INTERVAL	= 0,
	PERF_STAT_ROUND_TYPE__FINAL	= 1,
};

298
void perf_event__print_totals(void);
299

300
struct perf_cpu_map;
301
struct perf_record_stat_config;
302
struct perf_stat_config;
303
struct perf_tool;
304

305
void perf_event__read_stat_config(struct perf_stat_config *config,
306
				  struct perf_record_stat_config *event);
307

308
int perf_event__process_comm(struct perf_tool *tool,
309 310
			     union perf_event *event,
			     struct perf_sample *sample,
311
			     struct machine *machine);
312
int perf_event__process_lost(struct perf_tool *tool,
313 314
			     union perf_event *event,
			     struct perf_sample *sample,
315
			     struct machine *machine);
316 317 318 319
int perf_event__process_lost_samples(struct perf_tool *tool,
				     union perf_event *event,
				     struct perf_sample *sample,
				     struct machine *machine);
320 321 322 323
int perf_event__process_aux(struct perf_tool *tool,
			    union perf_event *event,
			    struct perf_sample *sample,
			    struct machine *machine);
324 325 326 327
int perf_event__process_itrace_start(struct perf_tool *tool,
				     union perf_event *event,
				     struct perf_sample *sample,
				     struct machine *machine);
328 329 330 331
int perf_event__process_switch(struct perf_tool *tool,
			       union perf_event *event,
			       struct perf_sample *sample,
			       struct machine *machine);
332 333 334 335
int perf_event__process_namespaces(struct perf_tool *tool,
				   union perf_event *event,
				   struct perf_sample *sample,
				   struct machine *machine);
336 337 338 339
int perf_event__process_cgroup(struct perf_tool *tool,
			       union perf_event *event,
			       struct perf_sample *sample,
			       struct machine *machine);
340
int perf_event__process_mmap(struct perf_tool *tool,
341 342
			     union perf_event *event,
			     struct perf_sample *sample,
343
			     struct machine *machine);
344 345 346 347
int perf_event__process_mmap2(struct perf_tool *tool,
			     union perf_event *event,
			     struct perf_sample *sample,
			     struct machine *machine);
348 349 350 351 352
int perf_event__process_fork(struct perf_tool *tool,
			     union perf_event *event,
			     struct perf_sample *sample,
			     struct machine *machine);
int perf_event__process_exit(struct perf_tool *tool,
353 354
			     union perf_event *event,
			     struct perf_sample *sample,
355
			     struct machine *machine);
356 357 358 359
int perf_event__process_ksymbol(struct perf_tool *tool,
				union perf_event *event,
				struct perf_sample *sample,
				struct machine *machine);
360 361 362 363
int perf_event__process_bpf(struct perf_tool *tool,
			    union perf_event *event,
			    struct perf_sample *sample,
			    struct machine *machine);
364 365 366 367
int perf_event__process_text_poke(struct perf_tool *tool,
				  union perf_event *event,
				  struct perf_sample *sample,
				  struct machine *machine);
368
int perf_event__process(struct perf_tool *tool,
369 370
			union perf_event *event,
			struct perf_sample *sample,
371
			struct machine *machine);
372

373
struct addr_location;
374

375 376
int machine__resolve(struct machine *machine, struct addr_location *al,
		     struct perf_sample *sample);
377

378 379
void addr_location__put(struct addr_location *al);

380 381 382 383
struct thread;

bool is_bts_event(struct perf_event_attr *attr);
bool sample_addr_correlates_sym(struct perf_event_attr *attr);
384 385
void thread__resolve(struct thread *thread, struct addr_location *al,
		     struct perf_sample *sample);
386

387
const char *perf_event__name(unsigned int id);
388

389 390
size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp);
size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp);
391
size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp);
392
size_t perf_event__fprintf_task(union perf_event *event, FILE *fp);
393
size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp);
394
size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp);
395
size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp);
396
size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp);
397
size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp);
398
size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp);
399
size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp);
400
size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp);
401
size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp);
402 403
size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *machine,FILE *fp);
size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILE *fp);
404

405 406
int kallsyms__get_function_start(const char *kallsyms_filename,
				 const char *symbol_name, u64 *addr);
407

408
void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max);
409
void  cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
410
			       u16 type, int max);
411 412 413 414

void event_attr_init(struct perf_event_attr *attr);

int perf_event_paranoid(void);
415
bool perf_event_paranoid_check(int max_level);
416 417 418

extern int sysctl_perf_event_max_stack;
extern int sysctl_perf_event_max_contexts_per_stack;
419
extern unsigned int proc_map_timeout;
420

K
Kan Liang 已提交
421 422 423
#define PAGE_SIZE_NAME_LEN	32
char *get_page_size_name(u64 size, char *str);

424 425 426
void arch_perf_parse_sample_weight(struct perf_sample *data, const __u64 *array, u64 type);
void arch_perf_synthesize_sample_weight(const struct perf_sample *data, __u64 *array, u64 type);

427
#endif /* __PERF_RECORD_H */