design.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. Performance Counters for Linux
  2. ------------------------------
  3. Performance counters are special hardware registers available on most modern
  4. CPUs. These registers count the number of certain types of hw events: such
  5. as instructions executed, cachemisses suffered, or branches mis-predicted -
  6. without slowing down the kernel or applications. These registers can also
  7. trigger interrupts when a threshold number of events have passed - and can
  8. thus be used to profile the code that runs on that CPU.
  9. The Linux Performance Counter subsystem provides an abstraction of these
  10. hardware capabilities. It provides per task and per CPU counters, counter
  11. groups, and it provides event capabilities on top of those. It
  12. provides "virtual" 64-bit counters, regardless of the width of the
  13. underlying hardware counters.
  14. Performance counters are accessed via special file descriptors.
  15. There's one file descriptor per virtual counter used.
  16. The special file descriptor is opened via the perf_counter_open()
  17. system call:
  18. int sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr,
  19. pid_t pid, int cpu, int group_fd,
  20. unsigned long flags);
  21. The syscall returns the new fd. The fd can be used via the normal
  22. VFS system calls: read() can be used to read the counter, fcntl()
  23. can be used to set the blocking mode, etc.
  24. Multiple counters can be kept open at a time, and the counters
  25. can be poll()ed.
  26. When creating a new counter fd, 'perf_counter_hw_event' is:
  27. struct perf_counter_hw_event {
  28. /*
  29. * The MSB of the config word signifies if the rest contains cpu
  30. * specific (raw) counter configuration data, if unset, the next
  31. * 7 bits are an event type and the rest of the bits are the event
  32. * identifier.
  33. */
  34. __u64 config;
  35. __u64 irq_period;
  36. __u32 record_type;
  37. __u32 read_format;
  38. __u64 disabled : 1, /* off by default */
  39. nmi : 1, /* NMI sampling */
  40. inherit : 1, /* children inherit it */
  41. pinned : 1, /* must always be on PMU */
  42. exclusive : 1, /* only group on PMU */
  43. exclude_user : 1, /* don't count user */
  44. exclude_kernel : 1, /* ditto kernel */
  45. exclude_hv : 1, /* ditto hypervisor */
  46. exclude_idle : 1, /* don't count when idle */
  47. mmap : 1, /* include mmap data */
  48. munmap : 1, /* include munmap data */
  49. comm : 1, /* include comm data */
  50. __reserved_1 : 52;
  51. __u32 extra_config_len;
  52. __u32 wakeup_events; /* wakeup every n events */
  53. __u64 __reserved_2;
  54. __u64 __reserved_3;
  55. };
  56. The 'config' field specifies what the counter should count. It
  57. is divided into 3 bit-fields:
  58. raw_type: 1 bit (most significant bit) 0x8000_0000_0000_0000
  59. type: 7 bits (next most significant) 0x7f00_0000_0000_0000
  60. event_id: 56 bits (least significant) 0x00ff_ffff_ffff_ffff
  61. If 'raw_type' is 1, then the counter will count a hardware event
  62. specified by the remaining 63 bits of event_config. The encoding is
  63. machine-specific.
  64. If 'raw_type' is 0, then the 'type' field says what kind of counter
  65. this is, with the following encoding:
  66. enum perf_event_types {
  67. PERF_TYPE_HARDWARE = 0,
  68. PERF_TYPE_SOFTWARE = 1,
  69. PERF_TYPE_TRACEPOINT = 2,
  70. };
  71. A counter of PERF_TYPE_HARDWARE will count the hardware event
  72. specified by 'event_id':
  73. /*
  74. * Generalized performance counter event types, used by the hw_event.event_id
  75. * parameter of the sys_perf_counter_open() syscall:
  76. */
  77. enum hw_event_ids {
  78. /*
  79. * Common hardware events, generalized by the kernel:
  80. */
  81. PERF_COUNT_CPU_CYCLES = 0,
  82. PERF_COUNT_INSTRUCTIONS = 1,
  83. PERF_COUNT_CACHE_REFERENCES = 2,
  84. PERF_COUNT_CACHE_MISSES = 3,
  85. PERF_COUNT_BRANCH_INSTRUCTIONS = 4,
  86. PERF_COUNT_BRANCH_MISSES = 5,
  87. PERF_COUNT_BUS_CYCLES = 6,
  88. };
  89. These are standardized types of events that work relatively uniformly
  90. on all CPUs that implement Performance Counters support under Linux,
  91. although there may be variations (e.g., different CPUs might count
  92. cache references and misses at different levels of the cache hierarchy).
  93. If a CPU is not able to count the selected event, then the system call
  94. will return -EINVAL.
  95. More hw_event_types are supported as well, but they are CPU-specific
  96. and accessed as raw events. For example, to count "External bus
  97. cycles while bus lock signal asserted" events on Intel Core CPUs, pass
  98. in a 0x4064 event_id value and set hw_event.raw_type to 1.
  99. A counter of type PERF_TYPE_SOFTWARE will count one of the available
  100. software events, selected by 'event_id':
  101. /*
  102. * Special "software" counters provided by the kernel, even if the hardware
  103. * does not support performance counters. These counters measure various
  104. * physical and sw events of the kernel (and allow the profiling of them as
  105. * well):
  106. */
  107. enum sw_event_ids {
  108. PERF_COUNT_CPU_CLOCK = 0,
  109. PERF_COUNT_TASK_CLOCK = 1,
  110. PERF_COUNT_PAGE_FAULTS = 2,
  111. PERF_COUNT_CONTEXT_SWITCHES = 3,
  112. PERF_COUNT_CPU_MIGRATIONS = 4,
  113. PERF_COUNT_PAGE_FAULTS_MIN = 5,
  114. PERF_COUNT_PAGE_FAULTS_MAJ = 6,
  115. };
  116. Counters of the type PERF_TYPE_TRACEPOINT are available when the ftrace event
  117. tracer is available, and event_id values can be obtained from
  118. /debug/tracing/events/*/*/id
  119. Counters come in two flavours: counting counters and sampling
  120. counters. A "counting" counter is one that is used for counting the
  121. number of events that occur, and is characterised by having
  122. irq_period = 0.
  123. A read() on a counter returns the current value of the counter and possible
  124. additional values as specified by 'read_format', each value is a u64 (8 bytes)
  125. in size.
  126. /*
  127. * Bits that can be set in hw_event.read_format to request that
  128. * reads on the counter should return the indicated quantities,
  129. * in increasing order of bit value, after the counter value.
  130. */
  131. enum perf_counter_read_format {
  132. PERF_FORMAT_TOTAL_TIME_ENABLED = 1,
  133. PERF_FORMAT_TOTAL_TIME_RUNNING = 2,
  134. };
  135. Using these additional values one can establish the overcommit ratio for a
  136. particular counter allowing one to take the round-robin scheduling effect
  137. into account.
  138. A "sampling" counter is one that is set up to generate an interrupt
  139. every N events, where N is given by 'irq_period'. A sampling counter
  140. has irq_period > 0. The record_type controls what data is recorded on each
  141. interrupt:
  142. /*
  143. * Bits that can be set in hw_event.record_type to request information
  144. * in the overflow packets.
  145. */
  146. enum perf_counter_record_format {
  147. PERF_RECORD_IP = 1U << 0,
  148. PERF_RECORD_TID = 1U << 1,
  149. PERF_RECORD_TIME = 1U << 2,
  150. PERF_RECORD_ADDR = 1U << 3,
  151. PERF_RECORD_GROUP = 1U << 4,
  152. PERF_RECORD_CALLCHAIN = 1U << 5,
  153. };
  154. Such (and other) events will be recorded in a ring-buffer, which is
  155. available to user-space using mmap() (see below).
  156. The 'disabled' bit specifies whether the counter starts out disabled
  157. or enabled. If it is initially disabled, it can be enabled by ioctl
  158. or prctl (see below).
  159. The 'nmi' bit specifies, for hardware events, whether the counter
  160. should be set up to request non-maskable interrupts (NMIs) or normal
  161. interrupts. This bit is ignored if the user doesn't have
  162. CAP_SYS_ADMIN privilege (i.e. is not root) or if the CPU doesn't
  163. generate NMIs from hardware counters.
  164. The 'inherit' bit, if set, specifies that this counter should count
  165. events on descendant tasks as well as the task specified. This only
  166. applies to new descendents, not to any existing descendents at the
  167. time the counter is created (nor to any new descendents of existing
  168. descendents).
  169. The 'pinned' bit, if set, specifies that the counter should always be
  170. on the CPU if at all possible. It only applies to hardware counters
  171. and only to group leaders. If a pinned counter cannot be put onto the
  172. CPU (e.g. because there are not enough hardware counters or because of
  173. a conflict with some other event), then the counter goes into an
  174. 'error' state, where reads return end-of-file (i.e. read() returns 0)
  175. until the counter is subsequently enabled or disabled.
  176. The 'exclusive' bit, if set, specifies that when this counter's group
  177. is on the CPU, it should be the only group using the CPU's counters.
  178. In future, this will allow sophisticated monitoring programs to supply
  179. extra configuration information via 'extra_config_len' to exploit
  180. advanced features of the CPU's Performance Monitor Unit (PMU) that are
  181. not otherwise accessible and that might disrupt other hardware
  182. counters.
  183. The 'exclude_user', 'exclude_kernel' and 'exclude_hv' bits provide a
  184. way to request that counting of events be restricted to times when the
  185. CPU is in user, kernel and/or hypervisor mode.
  186. The 'mmap' and 'munmap' bits allow recording of PROT_EXEC mmap/munmap
  187. operations, these can be used to relate userspace IP addresses to actual
  188. code, even after the mapping (or even the whole process) is gone,
  189. these events are recorded in the ring-buffer (see below).
  190. The 'comm' bit allows tracking of process comm data on process creation.
  191. This too is recorded in the ring-buffer (see below).
  192. The 'pid' parameter to the perf_counter_open() system call allows the
  193. counter to be specific to a task:
  194. pid == 0: if the pid parameter is zero, the counter is attached to the
  195. current task.
  196. pid > 0: the counter is attached to a specific task (if the current task
  197. has sufficient privilege to do so)
  198. pid < 0: all tasks are counted (per cpu counters)
  199. The 'cpu' parameter allows a counter to be made specific to a CPU:
  200. cpu >= 0: the counter is restricted to a specific CPU
  201. cpu == -1: the counter counts on all CPUs
  202. (Note: the combination of 'pid == -1' and 'cpu == -1' is not valid.)
  203. A 'pid > 0' and 'cpu == -1' counter is a per task counter that counts
  204. events of that task and 'follows' that task to whatever CPU the task
  205. gets schedule to. Per task counters can be created by any user, for
  206. their own tasks.
  207. A 'pid == -1' and 'cpu == x' counter is a per CPU counter that counts
  208. all events on CPU-x. Per CPU counters need CAP_SYS_ADMIN privilege.
  209. The 'flags' parameter is currently unused and must be zero.
  210. The 'group_fd' parameter allows counter "groups" to be set up. A
  211. counter group has one counter which is the group "leader". The leader
  212. is created first, with group_fd = -1 in the perf_counter_open call
  213. that creates it. The rest of the group members are created
  214. subsequently, with group_fd giving the fd of the group leader.
  215. (A single counter on its own is created with group_fd = -1 and is
  216. considered to be a group with only 1 member.)
  217. A counter group is scheduled onto the CPU as a unit, that is, it will
  218. only be put onto the CPU if all of the counters in the group can be
  219. put onto the CPU. This means that the values of the member counters
  220. can be meaningfully compared, added, divided (to get ratios), etc.,
  221. with each other, since they have counted events for the same set of
  222. executed instructions.
  223. Like stated, asynchronous events, like counter overflow or PROT_EXEC mmap
  224. tracking are logged into a ring-buffer. This ring-buffer is created and
  225. accessed through mmap().
  226. The mmap size should be 1+2^n pages, where the first page is a meta-data page
  227. (struct perf_counter_mmap_page) that contains various bits of information such
  228. as where the ring-buffer head is.
  229. /*
  230. * Structure of the page that can be mapped via mmap
  231. */
  232. struct perf_counter_mmap_page {
  233. __u32 version; /* version number of this structure */
  234. __u32 compat_version; /* lowest version this is compat with */
  235. /*
  236. * Bits needed to read the hw counters in user-space.
  237. *
  238. * u32 seq;
  239. * s64 count;
  240. *
  241. * do {
  242. * seq = pc->lock;
  243. *
  244. * barrier()
  245. * if (pc->index) {
  246. * count = pmc_read(pc->index - 1);
  247. * count += pc->offset;
  248. * } else
  249. * goto regular_read;
  250. *
  251. * barrier();
  252. * } while (pc->lock != seq);
  253. *
  254. * NOTE: for obvious reason this only works on self-monitoring
  255. * processes.
  256. */
  257. __u32 lock; /* seqlock for synchronization */
  258. __u32 index; /* hardware counter identifier */
  259. __s64 offset; /* add to hardware counter value */
  260. /*
  261. * Control data for the mmap() data buffer.
  262. *
  263. * User-space reading this value should issue an rmb(), on SMP capable
  264. * platforms, after reading this value -- see perf_counter_wakeup().
  265. */
  266. __u32 data_head; /* head in the data section */
  267. };
  268. NOTE: the hw-counter userspace bits are arch specific and are currently only
  269. implemented on powerpc.
  270. The following 2^n pages are the ring-buffer which contains events of the form:
  271. #define PERF_EVENT_MISC_KERNEL (1 << 0)
  272. #define PERF_EVENT_MISC_USER (1 << 1)
  273. #define PERF_EVENT_MISC_OVERFLOW (1 << 2)
  274. struct perf_event_header {
  275. __u32 type;
  276. __u16 misc;
  277. __u16 size;
  278. };
  279. enum perf_event_type {
  280. /*
  281. * The MMAP events record the PROT_EXEC mappings so that we can
  282. * correlate userspace IPs to code. They have the following structure:
  283. *
  284. * struct {
  285. * struct perf_event_header header;
  286. *
  287. * u32 pid, tid;
  288. * u64 addr;
  289. * u64 len;
  290. * u64 pgoff;
  291. * char filename[];
  292. * };
  293. */
  294. PERF_EVENT_MMAP = 1,
  295. PERF_EVENT_MUNMAP = 2,
  296. /*
  297. * struct {
  298. * struct perf_event_header header;
  299. *
  300. * u32 pid, tid;
  301. * char comm[];
  302. * };
  303. */
  304. PERF_EVENT_COMM = 3,
  305. /*
  306. * When header.misc & PERF_EVENT_MISC_OVERFLOW the event_type field
  307. * will be PERF_RECORD_*
  308. *
  309. * struct {
  310. * struct perf_event_header header;
  311. *
  312. * { u64 ip; } && PERF_RECORD_IP
  313. * { u32 pid, tid; } && PERF_RECORD_TID
  314. * { u64 time; } && PERF_RECORD_TIME
  315. * { u64 addr; } && PERF_RECORD_ADDR
  316. *
  317. * { u64 nr;
  318. * { u64 event, val; } cnt[nr]; } && PERF_RECORD_GROUP
  319. *
  320. * { u16 nr,
  321. * hv,
  322. * kernel,
  323. * user;
  324. * u64 ips[nr]; } && PERF_RECORD_CALLCHAIN
  325. * };
  326. */
  327. };
  328. NOTE: PERF_RECORD_CALLCHAIN is arch specific and currently only implemented
  329. on x86.
  330. Notification of new events is possible through poll()/select()/epoll() and
  331. fcntl() managing signals.
  332. Normally a notification is generated for every page filled, however one can
  333. additionally set perf_counter_hw_event.wakeup_events to generate one every
  334. so many counter overflow events.
  335. Future work will include a splice() interface to the ring-buffer.
  336. Counters can be enabled and disabled in two ways: via ioctl and via
  337. prctl. When a counter is disabled, it doesn't count or generate
  338. events but does continue to exist and maintain its count value.
  339. An individual counter or counter group can be enabled with
  340. ioctl(fd, PERF_COUNTER_IOC_ENABLE);
  341. or disabled with
  342. ioctl(fd, PERF_COUNTER_IOC_DISABLE);
  343. Enabling or disabling the leader of a group enables or disables the
  344. whole group; that is, while the group leader is disabled, none of the
  345. counters in the group will count. Enabling or disabling a member of a
  346. group other than the leader only affects that counter - disabling an
  347. non-leader stops that counter from counting but doesn't affect any
  348. other counter.
  349. Additionally, non-inherited overflow counters can use
  350. ioctl(fd, PERF_COUNTER_IOC_REFRESH, nr);
  351. to enable a counter for 'nr' events, after which it gets disabled again.
  352. A process can enable or disable all the counter groups that are
  353. attached to it, using prctl:
  354. prctl(PR_TASK_PERF_COUNTERS_ENABLE);
  355. prctl(PR_TASK_PERF_COUNTERS_DISABLE);
  356. This applies to all counters on the current process, whether created
  357. by this process or by another, and doesn't affect any counters that
  358. this process has created on other processes. It only enables or
  359. disables the group leaders, not any other members in the groups.