Home | History | Annotate | Download | only in linux
      1 /*
      2  * Performance events:
      3  *
      4  *    Copyright (C) 2008-2009, Thomas Gleixner <tglx (at) linutronix.de>
      5  *    Copyright (C) 2008-2011, Red Hat, Inc., Ingo Molnar
      6  *    Copyright (C) 2008-2011, Red Hat, Inc., Peter Zijlstra
      7  *
      8  * Data type definitions, declarations, prototypes.
      9  *
     10  *    Started by: Thomas Gleixner and Ingo Molnar
     11  *
     12  * For licencing details see kernel-base/COPYING
     13  */
     14 #ifndef _LINUX_PERF_EVENT_H
     15 #define _LINUX_PERF_EVENT_H
     16 
     17 #include <linux/types.h>
     18 #include <linux/ioctl.h>
     19 #include <asm/byteorder.h>
     20 
     21 /*
     22  * User-space ABI bits:
     23  */
     24 
     25 /*
     26  * attr.type
     27  */
     28 enum perf_type_id {
     29 	PERF_TYPE_HARDWARE			= 0,
     30 	PERF_TYPE_SOFTWARE			= 1,
     31 	PERF_TYPE_TRACEPOINT			= 2,
     32 	PERF_TYPE_HW_CACHE			= 3,
     33 	PERF_TYPE_RAW				= 4,
     34 	PERF_TYPE_BREAKPOINT			= 5,
     35 
     36 	PERF_TYPE_MAX,				/* non-ABI */
     37 };
     38 
     39 /*
     40  * Generalized performance event event_id types, used by the
     41  * attr.event_id parameter of the sys_perf_event_open()
     42  * syscall:
     43  */
     44 enum perf_hw_id {
     45 	/*
     46 	 * Common hardware events, generalized by the kernel:
     47 	 */
     48 	PERF_COUNT_HW_CPU_CYCLES		= 0,
     49 	PERF_COUNT_HW_INSTRUCTIONS		= 1,
     50 	PERF_COUNT_HW_CACHE_REFERENCES		= 2,
     51 	PERF_COUNT_HW_CACHE_MISSES		= 3,
     52 	PERF_COUNT_HW_BRANCH_INSTRUCTIONS	= 4,
     53 	PERF_COUNT_HW_BRANCH_MISSES		= 5,
     54 	PERF_COUNT_HW_BUS_CYCLES		= 6,
     55 	PERF_COUNT_HW_STALLED_CYCLES_FRONTEND	= 7,
     56 	PERF_COUNT_HW_STALLED_CYCLES_BACKEND	= 8,
     57 
     58 	PERF_COUNT_HW_MAX,			/* non-ABI */
     59 };
     60 
     61 /*
     62  * Generalized hardware cache events:
     63  *
     64  *       { L1-D, L1-I, LLC, ITLB, DTLB, BPU, NODE } x
     65  *       { read, write, prefetch } x
     66  *       { accesses, misses }
     67  */
     68 enum perf_hw_cache_id {
     69 	PERF_COUNT_HW_CACHE_L1D			= 0,
     70 	PERF_COUNT_HW_CACHE_L1I			= 1,
     71 	PERF_COUNT_HW_CACHE_LL			= 2,
     72 	PERF_COUNT_HW_CACHE_DTLB		= 3,
     73 	PERF_COUNT_HW_CACHE_ITLB		= 4,
     74 	PERF_COUNT_HW_CACHE_BPU			= 5,
     75 	PERF_COUNT_HW_CACHE_NODE		= 6,
     76 
     77 	PERF_COUNT_HW_CACHE_MAX,		/* non-ABI */
     78 };
     79 
     80 enum perf_hw_cache_op_id {
     81 	PERF_COUNT_HW_CACHE_OP_READ		= 0,
     82 	PERF_COUNT_HW_CACHE_OP_WRITE		= 1,
     83 	PERF_COUNT_HW_CACHE_OP_PREFETCH		= 2,
     84 
     85 	PERF_COUNT_HW_CACHE_OP_MAX,		/* non-ABI */
     86 };
     87 
     88 enum perf_hw_cache_op_result_id {
     89 	PERF_COUNT_HW_CACHE_RESULT_ACCESS	= 0,
     90 	PERF_COUNT_HW_CACHE_RESULT_MISS		= 1,
     91 
     92 	PERF_COUNT_HW_CACHE_RESULT_MAX,		/* non-ABI */
     93 };
     94 
     95 /*
     96  * Special "software" events provided by the kernel, even if the hardware
     97  * does not support performance events. These events measure various
     98  * physical and sw events of the kernel (and allow the profiling of them as
     99  * well):
    100  */
    101 enum perf_sw_ids {
    102 	PERF_COUNT_SW_CPU_CLOCK			= 0,
    103 	PERF_COUNT_SW_TASK_CLOCK		= 1,
    104 	PERF_COUNT_SW_PAGE_FAULTS		= 2,
    105 	PERF_COUNT_SW_CONTEXT_SWITCHES		= 3,
    106 	PERF_COUNT_SW_CPU_MIGRATIONS		= 4,
    107 	PERF_COUNT_SW_PAGE_FAULTS_MIN		= 5,
    108 	PERF_COUNT_SW_PAGE_FAULTS_MAJ		= 6,
    109 	PERF_COUNT_SW_ALIGNMENT_FAULTS		= 7,
    110 	PERF_COUNT_SW_EMULATION_FAULTS		= 8,
    111 
    112 	PERF_COUNT_SW_MAX,			/* non-ABI */
    113 };
    114 
    115 /*
    116  * Bits that can be set in attr.sample_type to request information
    117  * in the overflow packets.
    118  */
    119 enum perf_event_sample_format {
    120 	PERF_SAMPLE_IP				= 1U << 0,
    121 	PERF_SAMPLE_TID				= 1U << 1,
    122 	PERF_SAMPLE_TIME			= 1U << 2,
    123 	PERF_SAMPLE_ADDR			= 1U << 3,
    124 	PERF_SAMPLE_READ			= 1U << 4,
    125 	PERF_SAMPLE_CALLCHAIN			= 1U << 5,
    126 	PERF_SAMPLE_ID				= 1U << 6,
    127 	PERF_SAMPLE_CPU				= 1U << 7,
    128 	PERF_SAMPLE_PERIOD			= 1U << 8,
    129 	PERF_SAMPLE_STREAM_ID			= 1U << 9,
    130 	PERF_SAMPLE_RAW				= 1U << 10,
    131 
    132 	PERF_SAMPLE_MAX = 1U << 11,		/* non-ABI */
    133 };
    134 
    135 /*
    136  * The format of the data returned by read() on a perf event fd,
    137  * as specified by attr.read_format:
    138  *
    139  * struct read_format {
    140  *	{ u64		value;
    141  *	  { u64		time_enabled; } && PERF_FORMAT_TOTAL_TIME_ENABLED
    142  *	  { u64		time_running; } && PERF_FORMAT_TOTAL_TIME_RUNNING
    143  *	  { u64		id;           } && PERF_FORMAT_ID
    144  *	} && !PERF_FORMAT_GROUP
    145  *
    146  *	{ u64		nr;
    147  *	  { u64		time_enabled; } && PERF_FORMAT_TOTAL_TIME_ENABLED
    148  *	  { u64		time_running; } && PERF_FORMAT_TOTAL_TIME_RUNNING
    149  *	  { u64		value;
    150  *	    { u64	id;           } && PERF_FORMAT_ID
    151  *	  }		cntr[nr];
    152  *	} && PERF_FORMAT_GROUP
    153  * };
    154  */
    155 enum perf_event_read_format {
    156 	PERF_FORMAT_TOTAL_TIME_ENABLED		= 1U << 0,
    157 	PERF_FORMAT_TOTAL_TIME_RUNNING		= 1U << 1,
    158 	PERF_FORMAT_ID				= 1U << 2,
    159 	PERF_FORMAT_GROUP			= 1U << 3,
    160 
    161 	PERF_FORMAT_MAX = 1U << 4,		/* non-ABI */
    162 };
    163 
    164 #define PERF_ATTR_SIZE_VER0	64	/* sizeof first published struct */
    165 
    166 /*
    167  * Hardware event_id to monitor via a performance monitoring event:
    168  */
    169 struct perf_event_attr {
    170 
    171 	/*
    172 	 * Major type: hardware/software/tracepoint/etc.
    173 	 */
    174 	__u32			type;
    175 
    176 	/*
    177 	 * Size of the attr structure, for fwd/bwd compat.
    178 	 */
    179 	__u32			size;
    180 
    181 	/*
    182 	 * Type specific configuration information.
    183 	 */
    184 	__u64			config;
    185 
    186 	union {
    187 		__u64		sample_period;
    188 		__u64		sample_freq;
    189 	};
    190 
    191 	__u64			sample_type;
    192 	__u64			read_format;
    193 
    194 	__u64			disabled       :  1, /* off by default        */
    195 				inherit	       :  1, /* children inherit it   */
    196 				pinned	       :  1, /* must always be on PMU */
    197 				exclusive      :  1, /* only group on PMU     */
    198 				exclude_user   :  1, /* don't count user      */
    199 				exclude_kernel :  1, /* ditto kernel          */
    200 				exclude_hv     :  1, /* ditto hypervisor      */
    201 				exclude_idle   :  1, /* don't count when idle */
    202 				mmap           :  1, /* include mmap data     */
    203 				comm	       :  1, /* include comm data     */
    204 				freq           :  1, /* use freq, not period  */
    205 				inherit_stat   :  1, /* per task counts       */
    206 				enable_on_exec :  1, /* next exec enables     */
    207 				task           :  1, /* trace fork/exit       */
    208 				watermark      :  1, /* wakeup_watermark      */
    209 				/*
    210 				 * precise_ip:
    211 				 *
    212 				 *  0 - SAMPLE_IP can have arbitrary skid
    213 				 *  1 - SAMPLE_IP must have constant skid
    214 				 *  2 - SAMPLE_IP requested to have 0 skid
    215 				 *  3 - SAMPLE_IP must have 0 skid
    216 				 *
    217 				 *  See also PERF_RECORD_MISC_EXACT_IP
    218 				 */
    219 				precise_ip     :  2, /* skid constraint       */
    220 				mmap_data      :  1, /* non-exec mmap data    */
    221 				sample_id_all  :  1, /* sample_type all events */
    222 
    223 				exclude_host   :  1, /* don't count in host   */
    224 				exclude_guest  :  1, /* don't count in guest  */
    225 
    226 				__reserved_1   : 43;
    227 
    228 	union {
    229 		__u32		wakeup_events;	  /* wakeup every n events */
    230 		__u32		wakeup_watermark; /* bytes before wakeup   */
    231 	};
    232 
    233 	__u32			bp_type;
    234 	union {
    235 		__u64		bp_addr;
    236 		__u64		config1; /* extension of config */
    237 	};
    238 	union {
    239 		__u64		bp_len;
    240 		__u64		config2; /* extension of config1 */
    241 	};
    242 };
    243 
    244 /*
    245  * Ioctls that can be done on a perf event fd:
    246  */
    247 #define PERF_EVENT_IOC_ENABLE		_IO ('$', 0)
    248 #define PERF_EVENT_IOC_DISABLE		_IO ('$', 1)
    249 #define PERF_EVENT_IOC_REFRESH		_IO ('$', 2)
    250 #define PERF_EVENT_IOC_RESET		_IO ('$', 3)
    251 #define PERF_EVENT_IOC_PERIOD		_IOW('$', 4, __u64)
    252 #define PERF_EVENT_IOC_SET_OUTPUT	_IO ('$', 5)
    253 #define PERF_EVENT_IOC_SET_FILTER	_IOW('$', 6, char *)
    254 
    255 enum perf_event_ioc_flags {
    256 	PERF_IOC_FLAG_GROUP		= 1U << 0,
    257 };
    258 
    259 /*
    260  * Structure of the page that can be mapped via mmap
    261  */
    262 struct perf_event_mmap_page {
    263 	__u32	version;		/* version number of this structure */
    264 	__u32	compat_version;		/* lowest version this is compat with */
    265 
    266 	/*
    267 	 * Bits needed to read the hw events in user-space.
    268 	 *
    269 	 *   u32 seq;
    270 	 *   s64 count;
    271 	 *
    272 	 *   do {
    273 	 *     seq = pc->lock;
    274 	 *
    275 	 *     barrier()
    276 	 *     if (pc->index) {
    277 	 *       count = pmc_read(pc->index - 1);
    278 	 *       count += pc->offset;
    279 	 *     } else
    280 	 *       goto regular_read;
    281 	 *
    282 	 *     barrier();
    283 	 *   } while (pc->lock != seq);
    284 	 *
    285 	 * NOTE: for obvious reason this only works on self-monitoring
    286 	 *       processes.
    287 	 */
    288 	__u32	lock;			/* seqlock for synchronization */
    289 	__u32	index;			/* hardware event identifier */
    290 	__s64	offset;			/* add to hardware event value */
    291 	__u64	time_enabled;		/* time event active */
    292 	__u64	time_running;		/* time event on cpu */
    293 
    294 		/*
    295 		 * Hole for extension of the self monitor capabilities
    296 		 */
    297 
    298 	__u64	__reserved[123];	/* align to 1k */
    299 
    300 	/*
    301 	 * Control data for the mmap() data buffer.
    302 	 *
    303 	 * User-space reading the @data_head value should issue an rmb(), on
    304 	 * SMP capable platforms, after reading this value -- see
    305 	 * perf_event_wakeup().
    306 	 *
    307 	 * When the mapping is PROT_WRITE the @data_tail value should be
    308 	 * written by userspace to reflect the last read data. In this case
    309 	 * the kernel will not over-write unread data.
    310 	 */
    311 	__u64   data_head;		/* head in the data section */
    312 	__u64	data_tail;		/* user-space written tail */
    313 };
    314 
    315 #define PERF_RECORD_MISC_CPUMODE_MASK		(7 << 0)
    316 #define PERF_RECORD_MISC_CPUMODE_UNKNOWN	(0 << 0)
    317 #define PERF_RECORD_MISC_KERNEL			(1 << 0)
    318 #define PERF_RECORD_MISC_USER			(2 << 0)
    319 #define PERF_RECORD_MISC_HYPERVISOR		(3 << 0)
    320 #define PERF_RECORD_MISC_GUEST_KERNEL		(4 << 0)
    321 #define PERF_RECORD_MISC_GUEST_USER		(5 << 0)
    322 
    323 /*
    324  * Indicates that the content of PERF_SAMPLE_IP points to
    325  * the actual instruction that triggered the event. See also
    326  * perf_event_attr::precise_ip.
    327  */
    328 #define PERF_RECORD_MISC_EXACT_IP		(1 << 14)
    329 /*
    330  * Reserve the last bit to indicate some extended misc field
    331  */
    332 #define PERF_RECORD_MISC_EXT_RESERVED		(1 << 15)
    333 
    334 struct perf_event_header {
    335 	__u32	type;
    336 	__u16	misc;
    337 	__u16	size;
    338 };
    339 
    340 enum perf_event_type {
    341 
    342 	/*
    343 	 * If perf_event_attr.sample_id_all is set then all event types will
    344 	 * have the sample_type selected fields related to where/when
    345 	 * (identity) an event took place (TID, TIME, ID, CPU, STREAM_ID)
    346 	 * described in PERF_RECORD_SAMPLE below, it will be stashed just after
    347 	 * the perf_event_header and the fields already present for the existing
    348 	 * fields, i.e. at the end of the payload. That way a newer perf.data
    349 	 * file will be supported by older perf tools, with these new optional
    350 	 * fields being ignored.
    351 	 *
    352 	 * The MMAP events record the PROT_EXEC mappings so that we can
    353 	 * correlate userspace IPs to code. They have the following structure:
    354 	 *
    355 	 * struct {
    356 	 *	struct perf_event_header	header;
    357 	 *
    358 	 *	u32				pid, tid;
    359 	 *	u64				addr;
    360 	 *	u64				len;
    361 	 *	u64				pgoff;
    362 	 *	char				filename[];
    363 	 * };
    364 	 */
    365 	PERF_RECORD_MMAP			= 1,
    366 
    367 	/*
    368 	 * struct {
    369 	 *	struct perf_event_header	header;
    370 	 *	u64				id;
    371 	 *	u64				lost;
    372 	 * };
    373 	 */
    374 	PERF_RECORD_LOST			= 2,
    375 
    376 	/*
    377 	 * struct {
    378 	 *	struct perf_event_header	header;
    379 	 *
    380 	 *	u32				pid, tid;
    381 	 *	char				comm[];
    382 	 * };
    383 	 */
    384 	PERF_RECORD_COMM			= 3,
    385 
    386 	/*
    387 	 * struct {
    388 	 *	struct perf_event_header	header;
    389 	 *	u32				pid, ppid;
    390 	 *	u32				tid, ptid;
    391 	 *	u64				time;
    392 	 * };
    393 	 */
    394 	PERF_RECORD_EXIT			= 4,
    395 
    396 	/*
    397 	 * struct {
    398 	 *	struct perf_event_header	header;
    399 	 *	u64				time;
    400 	 *	u64				id;
    401 	 *	u64				stream_id;
    402 	 * };
    403 	 */
    404 	PERF_RECORD_THROTTLE			= 5,
    405 	PERF_RECORD_UNTHROTTLE			= 6,
    406 
    407 	/*
    408 	 * struct {
    409 	 *	struct perf_event_header	header;
    410 	 *	u32				pid, ppid;
    411 	 *	u32				tid, ptid;
    412 	 *	u64				time;
    413 	 * };
    414 	 */
    415 	PERF_RECORD_FORK			= 7,
    416 
    417 	/*
    418 	 * struct {
    419 	 *	struct perf_event_header	header;
    420 	 *	u32				pid, tid;
    421 	 *
    422 	 *	struct read_format		values;
    423 	 * };
    424 	 */
    425 	PERF_RECORD_READ			= 8,
    426 
    427 	/*
    428 	 * struct {
    429 	 *	struct perf_event_header	header;
    430 	 *
    431 	 *	{ u64			ip;	  } && PERF_SAMPLE_IP
    432 	 *	{ u32			pid, tid; } && PERF_SAMPLE_TID
    433 	 *	{ u64			time;     } && PERF_SAMPLE_TIME
    434 	 *	{ u64			addr;     } && PERF_SAMPLE_ADDR
    435 	 *	{ u64			id;	  } && PERF_SAMPLE_ID
    436 	 *	{ u64			stream_id;} && PERF_SAMPLE_STREAM_ID
    437 	 *	{ u32			cpu, res; } && PERF_SAMPLE_CPU
    438 	 *	{ u64			period;   } && PERF_SAMPLE_PERIOD
    439 	 *
    440 	 *	{ struct read_format	values;	  } && PERF_SAMPLE_READ
    441 	 *
    442 	 *	{ u64			nr,
    443 	 *	  u64			ips[nr];  } && PERF_SAMPLE_CALLCHAIN
    444 	 *
    445 	 *	#
    446 	 *	# The RAW record below is opaque data wrt the ABI
    447 	 *	#
    448 	 *	# That is, the ABI doesn't make any promises wrt to
    449 	 *	# the stability of its content, it may vary depending
    450 	 *	# on event, hardware, kernel version and phase of
    451 	 *	# the moon.
    452 	 *	#
    453 	 *	# In other words, PERF_SAMPLE_RAW contents are not an ABI.
    454 	 *	#
    455 	 *
    456 	 *	{ u32			size;
    457 	 *	  char                  data[size];}&& PERF_SAMPLE_RAW
    458 	 * };
    459 	 */
    460 	PERF_RECORD_SAMPLE			= 9,
    461 
    462 	PERF_RECORD_MAX,			/* non-ABI */
    463 };
    464 
    465 enum perf_callchain_context {
    466 	PERF_CONTEXT_HV			= (__u64)-32,
    467 	PERF_CONTEXT_KERNEL		= (__u64)-128,
    468 	PERF_CONTEXT_USER		= (__u64)-512,
    469 
    470 	PERF_CONTEXT_GUEST		= (__u64)-2048,
    471 	PERF_CONTEXT_GUEST_KERNEL	= (__u64)-2176,
    472 	PERF_CONTEXT_GUEST_USER		= (__u64)-2560,
    473 
    474 	PERF_CONTEXT_MAX		= (__u64)-4095,
    475 };
    476 
    477 #define PERF_FLAG_FD_NO_GROUP		(1U << 0)
    478 #define PERF_FLAG_FD_OUTPUT		(1U << 1)
    479 #define PERF_FLAG_PID_CGROUP		(1U << 2) /* pid=cgroup id, per-cpu mode only */
    480 
    481 #endif /* _LINUX_PERF_EVENT_H */
    482