perf_ring_buffer.rst 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ================
  3. Perf ring buffer
  4. ================
  5. .. CONTENTS
  6. 1. Introduction
  7. 2. Ring buffer implementation
  8. 2.1 Basic algorithm
  9. 2.2 Ring buffer for different tracing modes
  10. 2.2.1 Default mode
  11. 2.2.2 Per-thread mode
  12. 2.2.3 Per-CPU mode
  13. 2.2.4 System wide mode
  14. 2.3 Accessing buffer
  15. 2.3.1 Producer-consumer model
  16. 2.3.2 Properties of the ring buffers
  17. 2.3.3 Writing samples into buffer
  18. 2.3.4 Reading samples from buffer
  19. 2.3.5 Memory synchronization
  20. 3. The mechanism of AUX ring buffer
  21. 3.1 The relationship between AUX and regular ring buffers
  22. 3.2 AUX events
  23. 3.3 Snapshot mode
  24. 1. Introduction
  25. ===============
  26. The ring buffer is a fundamental mechanism for data transfer. perf uses
  27. ring buffers to transfer event data from kernel to user space, another
  28. kind of ring buffer which is so called auxiliary (AUX) ring buffer also
  29. plays an important role for hardware tracing with Intel PT, Arm
  30. CoreSight, etc.
  31. The ring buffer implementation is critical but it's also a very
  32. challenging work. On the one hand, the kernel and perf tool in the user
  33. space use the ring buffer to exchange data and stores data into data
  34. file, thus the ring buffer needs to transfer data with high throughput;
  35. on the other hand, the ring buffer management should avoid significant
  36. overload to distract profiling results.
  37. This documentation dives into the details for perf ring buffer with two
  38. parts: firstly it explains the perf ring buffer implementation, then the
  39. second part discusses the AUX ring buffer mechanism.
  40. 2. Ring buffer implementation
  41. =============================
  42. 2.1 Basic algorithm
  43. -------------------
  44. That said, a typical ring buffer is managed by a head pointer and a tail
  45. pointer; the head pointer is manipulated by a writer and the tail
  46. pointer is updated by a reader respectively.
  47. ::
  48. +---------------------------+
  49. | | |***|***|***| | |
  50. +---------------------------+
  51. `-> Tail `-> Head
  52. * : the data is filled by the writer.
  53. Figure 1. Ring buffer
  54. Perf uses the same way to manage its ring buffer. In the implementation
  55. there are two key data structures held together in a set of consecutive
  56. pages, the control structure and then the ring buffer itself. The page
  57. with the control structure in is known as the "user page". Being held
  58. in continuous virtual addresses simplifies locating the ring buffer
  59. address, it is in the pages after the page with the user page.
  60. The control structure is named as ``perf_event_mmap_page``, it contains a
  61. head pointer ``data_head`` and a tail pointer ``data_tail``. When the
  62. kernel starts to fill records into the ring buffer, it updates the head
  63. pointer to reserve the memory so later it can safely store events into
  64. the buffer. On the other side, when the user page is a writable mapping,
  65. the perf tool has the permission to update the tail pointer after consuming
  66. data from the ring buffer. Yet another case is for the user page's
  67. read-only mapping, which is to be addressed in the section
  68. :ref:`writing_samples_into_buffer`.
  69. ::
  70. user page ring buffer
  71. +---------+---------+ +---------------------------------------+
  72. |data_head|data_tail|...| | |***|***|***|***|***| | | |
  73. +---------+---------+ +---------------------------------------+
  74. ` `----------------^ ^
  75. `----------------------------------------------|
  76. * : the data is filled by the writer.
  77. Figure 2. Perf ring buffer
  78. When using the ``perf record`` tool, we can specify the ring buffer size
  79. with option ``-m`` or ``--mmap-pages=``, the given size will be rounded up
  80. to a power of two that is a multiple of a page size. Though the kernel
  81. allocates at once for all memory pages, it's deferred to map the pages
  82. to VMA area until the perf tool accesses the buffer from the user space.
  83. In other words, at the first time accesses the buffer's page from user
  84. space in the perf tool, a data abort exception for page fault is taken
  85. and the kernel uses this occasion to map the page into process VMA
  86. (see ``perf_mmap_fault()``), thus the perf tool can continue to access
  87. the page after returning from the exception.
  88. 2.2 Ring buffer for different tracing modes
  89. -------------------------------------------
  90. The perf profiles programs with different modes: default mode, per thread
  91. mode, per cpu mode, and system wide mode. This section describes these
  92. modes and how the ring buffer meets requirements for them. At last we
  93. will review the race conditions caused by these modes.
  94. 2.2.1 Default mode
  95. ^^^^^^^^^^^^^^^^^^
  96. Usually we execute ``perf record`` command followed by a profiling program
  97. name, like below command::
  98. perf record test_program
  99. This command doesn't specify any options for CPU and thread modes, the
  100. perf tool applies the default mode on the perf event. It maps all the
  101. CPUs in the system and the profiled program's PID on the perf event, and
  102. it enables inheritance mode on the event so that child tasks inherits
  103. the events. As a result, the perf event is attributed as::
  104. evsel::cpus::map[] = { 0 .. _SC_NPROCESSORS_ONLN-1 }
  105. evsel::threads::map[] = { pid }
  106. evsel::attr::inherit = 1
  107. These attributions finally will be reflected on the deployment of ring
  108. buffers. As shown below, the perf tool allocates individual ring buffer
  109. for each CPU, but it only enables events for the profiled program rather
  110. than for all threads in the system. The *T1* thread represents the
  111. thread context of the 'test_program', whereas *T2* and *T3* are irrelevant
  112. threads in the system. The perf samples are exclusively collected for
  113. the *T1* thread and stored in the ring buffer associated with the CPU on
  114. which the *T1* thread is running.
  115. ::
  116. T1 T2 T1
  117. +----+ +-----------+ +----+
  118. CPU0 |xxxx| |xxxxxxxxxxx| |xxxx|
  119. +----+--------------+-----------+----------+----+-------->
  120. | |
  121. v v
  122. +-----------------------------------------------------+
  123. | Ring buffer 0 |
  124. +-----------------------------------------------------+
  125. T1
  126. +-----+
  127. CPU1 |xxxxx|
  128. -----+-----+--------------------------------------------->
  129. |
  130. v
  131. +-----------------------------------------------------+
  132. | Ring buffer 1 |
  133. +-----------------------------------------------------+
  134. T1 T3
  135. +----+ +-------+
  136. CPU2 |xxxx| |xxxxxxx|
  137. --------------------------+----+--------+-------+-------->
  138. |
  139. v
  140. +-----------------------------------------------------+
  141. | Ring buffer 2 |
  142. +-----------------------------------------------------+
  143. T1
  144. +--------------+
  145. CPU3 |xxxxxxxxxxxxxx|
  146. -----------+--------------+------------------------------>
  147. |
  148. v
  149. +-----------------------------------------------------+
  150. | Ring buffer 3 |
  151. +-----------------------------------------------------+
  152. T1: Thread 1; T2: Thread 2; T3: Thread 3
  153. x: Thread is in running state
  154. Figure 3. Ring buffer for default mode
  155. 2.2.2 Per-thread mode
  156. ^^^^^^^^^^^^^^^^^^^^^
  157. By specifying option ``--per-thread`` in perf command, e.g.
  158. ::
  159. perf record --per-thread test_program
  160. The perf event doesn't map to any CPUs and is only bound to the
  161. profiled process, thus, the perf event's attributions are::
  162. evsel::cpus::map[0] = { -1 }
  163. evsel::threads::map[] = { pid }
  164. evsel::attr::inherit = 0
  165. In this mode, a single ring buffer is allocated for the profiled thread;
  166. if the thread is scheduled on a CPU, the events on that CPU will be
  167. enabled; and if the thread is scheduled out from the CPU, the events on
  168. the CPU will be disabled. When the thread is migrated from one CPU to
  169. another, the events are to be disabled on the previous CPU and enabled
  170. on the next CPU correspondingly.
  171. ::
  172. T1 T2 T1
  173. +----+ +-----------+ +----+
  174. CPU0 |xxxx| |xxxxxxxxxxx| |xxxx|
  175. +----+--------------+-----------+----------+----+-------->
  176. | |
  177. | T1 |
  178. | +-----+ |
  179. CPU1 | |xxxxx| |
  180. --|--+-----+----------------------------------|---------->
  181. | | |
  182. | | T1 T3 |
  183. | | +----+ +---+ |
  184. CPU2 | | |xxxx| |xxx| |
  185. --|-----|-----------------+----+--------+---+-|---------->
  186. | | | |
  187. | | T1 | |
  188. | | +--------------+ | |
  189. CPU3 | | |xxxxxxxxxxxxxx| | |
  190. --|-----|--+--------------+-|-----------------|---------->
  191. | | | | |
  192. v v v v v
  193. +-----------------------------------------------------+
  194. | Ring buffer |
  195. +-----------------------------------------------------+
  196. T1: Thread 1
  197. x: Thread is in running state
  198. Figure 4. Ring buffer for per-thread mode
  199. When perf runs in per-thread mode, a ring buffer is allocated for the
  200. profiled thread *T1*. The ring buffer is dedicated for thread *T1*, if the
  201. thread *T1* is running, the perf events will be recorded into the ring
  202. buffer; when the thread is sleeping, all associated events will be
  203. disabled, thus no trace data will be recorded into the ring buffer.
  204. 2.2.3 Per-CPU mode
  205. ^^^^^^^^^^^^^^^^^^
  206. The option ``-C`` is used to collect samples on the list of CPUs, for
  207. example the below perf command receives option ``-C 0,2``::
  208. perf record -C 0,2 test_program
  209. It maps the perf event to CPUs 0 and 2, and the event is not associated to any
  210. PID. Thus the perf event attributions are set as::
  211. evsel::cpus::map[0] = { 0, 2 }
  212. evsel::threads::map[] = { -1 }
  213. evsel::attr::inherit = 0
  214. This results in the session of ``perf record`` will sample all threads on CPU0
  215. and CPU2, and be terminated until test_program exits. Even there have tasks
  216. running on CPU1 and CPU3, since the ring buffer is absent for them, any
  217. activities on these two CPUs will be ignored. A usage case is to combine the
  218. options for per-thread mode and per-CPU mode, e.g. the options ``–C 0,2`` and
  219. ``––per–thread`` are specified together, the samples are recorded only when
  220. the profiled thread is scheduled on any of the listed CPUs.
  221. ::
  222. T1 T2 T1
  223. +----+ +-----------+ +----+
  224. CPU0 |xxxx| |xxxxxxxxxxx| |xxxx|
  225. +----+--------------+-----------+----------+----+-------->
  226. | | |
  227. v v v
  228. +-----------------------------------------------------+
  229. | Ring buffer 0 |
  230. +-----------------------------------------------------+
  231. T1
  232. +-----+
  233. CPU1 |xxxxx|
  234. -----+-----+--------------------------------------------->
  235. T1 T3
  236. +----+ +-------+
  237. CPU2 |xxxx| |xxxxxxx|
  238. --------------------------+----+--------+-------+-------->
  239. | |
  240. v v
  241. +-----------------------------------------------------+
  242. | Ring buffer 1 |
  243. +-----------------------------------------------------+
  244. T1
  245. +--------------+
  246. CPU3 |xxxxxxxxxxxxxx|
  247. -----------+--------------+------------------------------>
  248. T1: Thread 1; T2: Thread 2; T3: Thread 3
  249. x: Thread is in running state
  250. Figure 5. Ring buffer for per-CPU mode
  251. 2.2.4 System wide mode
  252. ^^^^^^^^^^^^^^^^^^^^^^
  253. By using option ``–a`` or ``––all–cpus``, perf collects samples on all CPUs
  254. for all tasks, we call it as the system wide mode, the command is::
  255. perf record -a test_program
  256. Similar to the per-CPU mode, the perf event doesn't bind to any PID, and
  257. it maps to all CPUs in the system::
  258. evsel::cpus::map[] = { 0 .. _SC_NPROCESSORS_ONLN-1 }
  259. evsel::threads::map[] = { -1 }
  260. evsel::attr::inherit = 0
  261. In the system wide mode, every CPU has its own ring buffer, all threads
  262. are monitored during the running state and the samples are recorded into
  263. the ring buffer belonging to the CPU which the events occurred on.
  264. ::
  265. T1 T2 T1
  266. +----+ +-----------+ +----+
  267. CPU0 |xxxx| |xxxxxxxxxxx| |xxxx|
  268. +----+--------------+-----------+----------+----+-------->
  269. | | |
  270. v v v
  271. +-----------------------------------------------------+
  272. | Ring buffer 0 |
  273. +-----------------------------------------------------+
  274. T1
  275. +-----+
  276. CPU1 |xxxxx|
  277. -----+-----+--------------------------------------------->
  278. |
  279. v
  280. +-----------------------------------------------------+
  281. | Ring buffer 1 |
  282. +-----------------------------------------------------+
  283. T1 T3
  284. +----+ +-------+
  285. CPU2 |xxxx| |xxxxxxx|
  286. --------------------------+----+--------+-------+-------->
  287. | |
  288. v v
  289. +-----------------------------------------------------+
  290. | Ring buffer 2 |
  291. +-----------------------------------------------------+
  292. T1
  293. +--------------+
  294. CPU3 |xxxxxxxxxxxxxx|
  295. -----------+--------------+------------------------------>
  296. |
  297. v
  298. +-----------------------------------------------------+
  299. | Ring buffer 3 |
  300. +-----------------------------------------------------+
  301. T1: Thread 1; T2: Thread 2; T3: Thread 3
  302. x: Thread is in running state
  303. Figure 6. Ring buffer for system wide mode
  304. 2.3 Accessing buffer
  305. --------------------
  306. Based on the understanding of how the ring buffer is allocated in
  307. various modes, this section explains access the ring buffer.
  308. 2.3.1 Producer-consumer model
  309. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  310. In the Linux kernel, the PMU events can produce samples which are stored
  311. into the ring buffer; the perf command in user space consumes the
  312. samples by reading out data from the ring buffer and finally saves the
  313. data into the file for post analysis. It’s a typical producer-consumer
  314. model for using the ring buffer.
  315. The perf process polls on the PMU events and sleeps when no events are
  316. incoming. To prevent frequent exchanges between the kernel and user
  317. space, the kernel event core layer introduces a watermark, which is
  318. stored in the ``perf_buffer::watermark``. When a sample is recorded into
  319. the ring buffer, and if the used buffer exceeds the watermark, the
  320. kernel wakes up the perf process to read samples from the ring buffer.
  321. ::
  322. Perf
  323. / | Read samples
  324. Polling / `--------------| Ring buffer
  325. v v ;---------------------v
  326. +----------------+ +---------+---------+ +-------------------+
  327. |Event wait queue| |data_head|data_tail| |***|***| | |***|
  328. +----------------+ +---------+---------+ +-------------------+
  329. ^ ^ `------------------------^
  330. | Wake up tasks | Store samples
  331. +-----------------------------+
  332. | Kernel event core layer |
  333. +-----------------------------+
  334. * : the data is filled by the writer.
  335. Figure 7. Writing and reading the ring buffer
  336. When the kernel event core layer notifies the user space, because
  337. multiple events might share the same ring buffer for recording samples,
  338. the core layer iterates every event associated with the ring buffer and
  339. wakes up tasks waiting on the event. This is fulfilled by the kernel
  340. function ``ring_buffer_wakeup()``.
  341. After the perf process is woken up, it starts to check the ring buffers
  342. one by one, if it finds any ring buffer containing samples it will read
  343. out the samples for statistics or saving into the data file. Given the
  344. perf process is able to run on any CPU, this leads to the ring buffer
  345. potentially being accessed from multiple CPUs simultaneously, which
  346. causes race conditions. The race condition handling is described in the
  347. section :ref:`memory_synchronization`.
  348. 2.3.2 Properties of the ring buffers
  349. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  350. Linux kernel supports two write directions for the ring buffer: forward and
  351. backward. The forward writing saves samples from the beginning of the ring
  352. buffer, the backward writing stores data from the end of the ring buffer with
  353. the reversed direction. The perf tool determines the writing direction.
  354. Additionally, the tool can map buffers in either read-write mode or read-only
  355. mode to the user space.
  356. The ring buffer in the read-write mode is mapped with the property
  357. ``PROT_READ | PROT_WRITE``. With the write permission, the perf tool
  358. updates the ``data_tail`` to indicate the data start position. Combining
  359. with the head pointer ``data_head``, which works as the end position of
  360. the current data, the perf tool can easily know where read out the data
  361. from.
  362. Alternatively, in the read-only mode, only the kernel keeps to update
  363. the ``data_head`` while the user space cannot access the ``data_tail`` due
  364. to the mapping property ``PROT_READ``.
  365. As a result, the matrix below illustrates the various combinations of
  366. direction and mapping characteristics. The perf tool employs two of these
  367. combinations to support buffer types: the non-overwrite buffer and the
  368. overwritable buffer.
  369. .. list-table::
  370. :widths: 1 1 1
  371. :header-rows: 1
  372. * - Mapping mode
  373. - Forward
  374. - Backward
  375. * - read-write
  376. - Non-overwrite ring buffer
  377. - Not used
  378. * - read-only
  379. - Not used
  380. - Overwritable ring buffer
  381. The non-overwrite ring buffer uses the read-write mapping with forward
  382. writing. It starts to save data from the beginning of the ring buffer
  383. and wrap around when overflow, which is used with the read-write mode in
  384. the normal ring buffer. When the consumer doesn't keep up with the
  385. producer, it would lose some data, the kernel keeps how many records it
  386. lost and generates the ``PERF_RECORD_LOST`` records in the next time
  387. when it finds a space in the ring buffer.
  388. The overwritable ring buffer uses the backward writing with the
  389. read-only mode. It saves the data from the end of the ring buffer and
  390. the ``data_head`` keeps the position of current data, the perf always
  391. knows where it starts to read and until the end of the ring buffer, thus
  392. it don't need the ``data_tail``. In this mode, it will not generate the
  393. ``PERF_RECORD_LOST`` records.
  394. .. _writing_samples_into_buffer:
  395. 2.3.3 Writing samples into buffer
  396. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  397. When a sample is taken and saved into the ring buffer, the kernel
  398. prepares sample fields based on the sample type; then it prepares the
  399. info for writing ring buffer which is stored in the structure
  400. ``perf_output_handle``. In the end, the kernel outputs the sample into
  401. the ring buffer and updates the head pointer in the user page so the
  402. perf tool can see the latest value.
  403. The structure ``perf_output_handle`` serves as a temporary context for
  404. tracking the information related to the buffer. The advantages of it is
  405. that it enables concurrent writing to the buffer by different events.
  406. For example, a software event and a hardware PMU event both are enabled
  407. for profiling, two instances of ``perf_output_handle`` serve as separate
  408. contexts for the software event and the hardware event respectively.
  409. This allows each event to reserve its own memory space for populating
  410. the record data.
  411. 2.3.4 Reading samples from buffer
  412. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  413. In the user space, the perf tool utilizes the ``perf_event_mmap_page``
  414. structure to handle the head and tail of the buffer. It also uses
  415. ``perf_mmap`` structure to keep track of a context for the ring buffer, this
  416. context includes information about the buffer's starting and ending
  417. addresses. Additionally, the mask value can be utilized to compute the
  418. circular buffer pointer even for an overflow.
  419. Similar to the kernel, the perf tool in the user space first reads out
  420. the recorded data from the ring buffer, and then updates the buffer's
  421. tail pointer ``perf_event_mmap_page::data_tail``.
  422. .. _memory_synchronization:
  423. 2.3.5 Memory synchronization
  424. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  425. The modern CPUs with relaxed memory model cannot promise the memory
  426. ordering, this means it’s possible to access the ring buffer and the
  427. ``perf_event_mmap_page`` structure out of order. To assure the specific
  428. sequence for memory accessing perf ring buffer, memory barriers are
  429. used to assure the data dependency. The rationale for the memory
  430. synchronization is as below::
  431. Kernel User space
  432. if (LOAD ->data_tail) { LOAD ->data_head
  433. (A) smp_rmb() (C)
  434. STORE $data LOAD $data
  435. smp_wmb() (B) smp_mb() (D)
  436. STORE ->data_head STORE ->data_tail
  437. }
  438. The comments in tools/include/linux/ring_buffer.h gives nice description
  439. for why and how to use memory barriers, here we will just provide an
  440. alternative explanation:
  441. (A) is a control dependency so that CPU assures order between checking
  442. pointer ``perf_event_mmap_page::data_tail`` and filling sample into ring
  443. buffer;
  444. (D) pairs with (A). (D) separates the ring buffer data reading from
  445. writing the pointer ``data_tail``, perf tool first consumes samples and then
  446. tells the kernel that the data chunk has been released. Since a reading
  447. operation is followed by a writing operation, thus (D) is a full memory
  448. barrier.
  449. (B) is a writing barrier in the middle of two writing operations, which
  450. makes sure that recording a sample must be prior to updating the head
  451. pointer.
  452. (C) pairs with (B). (C) is a read memory barrier to ensure the head
  453. pointer is fetched before reading samples.
  454. To implement the above algorithm, the ``perf_output_put_handle()`` function
  455. in the kernel and two helpers ``ring_buffer_read_head()`` and
  456. ``ring_buffer_write_tail()`` in the user space are introduced, they rely
  457. on memory barriers as described above to ensure the data dependency.
  458. Some architectures support one-way permeable barrier with load-acquire
  459. and store-release operations, these barriers are more relaxed with less
  460. performance penalty, so (C) and (D) can be optimized to use barriers
  461. ``smp_load_acquire()`` and ``smp_store_release()`` respectively.
  462. If an architecture doesn’t support load-acquire and store-release in its
  463. memory model, it will roll back to the old fashion of memory barrier
  464. operations. In this case, ``smp_load_acquire()`` encapsulates
  465. ``READ_ONCE()`` + ``smp_mb()``, since ``smp_mb()`` is costly,
  466. ``ring_buffer_read_head()`` doesn't invoke ``smp_load_acquire()`` and it uses
  467. the barriers ``READ_ONCE()`` + ``smp_rmb()`` instead.
  468. 3. The mechanism of AUX ring buffer
  469. ===================================
  470. In this chapter, we will explain the implementation of the AUX ring
  471. buffer. In the first part it will discuss the connection between the
  472. AUX ring buffer and the regular ring buffer, then the second part will
  473. examine how the AUX ring buffer co-works with the regular ring buffer,
  474. as well as the additional features introduced by the AUX ring buffer for
  475. the sampling mechanism.
  476. 3.1 The relationship between AUX and regular ring buffers
  477. ---------------------------------------------------------
  478. Generally, the AUX ring buffer is an auxiliary for the regular ring
  479. buffer. The regular ring buffer is primarily used to store the event
  480. samples and every event format complies with the definition in the
  481. union ``perf_event``; the AUX ring buffer is for recording the hardware
  482. trace data and the trace data format is hardware IP dependent.
  483. The general use and advantage of the AUX ring buffer is that it is
  484. written directly by hardware rather than by the kernel. For example,
  485. regular profile samples that write to the regular ring buffer cause an
  486. interrupt. Tracing execution requires a high number of samples and
  487. using interrupts would be overwhelming for the regular ring buffer
  488. mechanism. Having an AUX buffer allows for a region of memory more
  489. decoupled from the kernel and written to directly by hardware tracing.
  490. The AUX ring buffer reuses the same algorithm with the regular ring
  491. buffer for the buffer management. The control structure
  492. ``perf_event_mmap_page`` extends the new fields ``aux_head`` and ``aux_tail``
  493. for the head and tail pointers of the AUX ring buffer.
  494. During the initialisation phase, besides the mmap()-ed regular ring
  495. buffer, the perf tool invokes a second syscall in the
  496. ``auxtrace_mmap__mmap()`` function for the mmap of the AUX buffer with
  497. non-zero file offset; ``rb_alloc_aux()`` in the kernel allocates pages
  498. correspondingly, these pages will be deferred to map into VMA when
  499. handling the page fault, which is the same lazy mechanism with the
  500. regular ring buffer.
  501. AUX events and AUX trace data are two different things. Let's see an
  502. example::
  503. perf record -a -e cycles -e cs_etm/@tmc_etr0/ -- sleep 2
  504. The above command enables two events: one is the event *cycles* from PMU
  505. and another is the AUX event *cs_etm* from Arm CoreSight, both are saved
  506. into the regular ring buffer while the CoreSight's AUX trace data is
  507. stored in the AUX ring buffer.
  508. As a result, we can see the regular ring buffer and the AUX ring buffer
  509. are allocated in pairs. The perf in default mode allocates the regular
  510. ring buffer and the AUX ring buffer per CPU-wise, which is the same as
  511. the system wide mode, however, the default mode records samples only for
  512. the profiled program, whereas the latter mode profiles for all programs
  513. in the system. For per-thread mode, the perf tool allocates only one
  514. regular ring buffer and one AUX ring buffer for the whole session. For
  515. the per-CPU mode, the perf allocates two kinds of ring buffers for
  516. selected CPUs specified by the option ``-C``.
  517. The below figure demonstrates the buffers' layout in the system wide
  518. mode; if there are any activities on one CPU, the AUX event samples and
  519. the hardware trace data will be recorded into the dedicated buffers for
  520. the CPU.
  521. ::
  522. T1 T2 T1
  523. +----+ +-----------+ +----+
  524. CPU0 |xxxx| |xxxxxxxxxxx| |xxxx|
  525. +----+--------------+-----------+----------+----+-------->
  526. | | |
  527. v v v
  528. +-----------------------------------------------------+
  529. | Ring buffer 0 |
  530. +-----------------------------------------------------+
  531. | | |
  532. v v v
  533. +-----------------------------------------------------+
  534. | AUX Ring buffer 0 |
  535. +-----------------------------------------------------+
  536. T1
  537. +-----+
  538. CPU1 |xxxxx|
  539. -----+-----+--------------------------------------------->
  540. |
  541. v
  542. +-----------------------------------------------------+
  543. | Ring buffer 1 |
  544. +-----------------------------------------------------+
  545. |
  546. v
  547. +-----------------------------------------------------+
  548. | AUX Ring buffer 1 |
  549. +-----------------------------------------------------+
  550. T1 T3
  551. +----+ +-------+
  552. CPU2 |xxxx| |xxxxxxx|
  553. --------------------------+----+--------+-------+-------->
  554. | |
  555. v v
  556. +-----------------------------------------------------+
  557. | Ring buffer 2 |
  558. +-----------------------------------------------------+
  559. | |
  560. v v
  561. +-----------------------------------------------------+
  562. | AUX Ring buffer 2 |
  563. +-----------------------------------------------------+
  564. T1
  565. +--------------+
  566. CPU3 |xxxxxxxxxxxxxx|
  567. -----------+--------------+------------------------------>
  568. |
  569. v
  570. +-----------------------------------------------------+
  571. | Ring buffer 3 |
  572. +-----------------------------------------------------+
  573. |
  574. v
  575. +-----------------------------------------------------+
  576. | AUX Ring buffer 3 |
  577. +-----------------------------------------------------+
  578. T1: Thread 1; T2: Thread 2; T3: Thread 3
  579. x: Thread is in running state
  580. Figure 8. AUX ring buffer for system wide mode
  581. 3.2 AUX events
  582. --------------
  583. Similar to ``perf_output_begin()`` and ``perf_output_end()``'s working for the
  584. regular ring buffer, ``perf_aux_output_begin()`` and ``perf_aux_output_end()``
  585. serve for the AUX ring buffer for processing the hardware trace data.
  586. Once the hardware trace data is stored into the AUX ring buffer, the PMU
  587. driver will stop hardware tracing by calling the ``pmu::stop()`` callback.
  588. Similar to the regular ring buffer, the AUX ring buffer needs to apply
  589. the memory synchronization mechanism as discussed in the section
  590. :ref:`memory_synchronization`. Since the AUX ring buffer is managed by the
  591. PMU driver, the barrier (B), which is a writing barrier to ensure the trace
  592. data is externally visible prior to updating the head pointer, is asked
  593. to be implemented in the PMU driver.
  594. Then ``pmu::stop()`` can safely call the ``perf_aux_output_end()`` function to
  595. finish two things:
  596. - It fills an event ``PERF_RECORD_AUX`` into the regular ring buffer, this
  597. event delivers the information of the start address and data size for a
  598. chunk of hardware trace data has been stored into the AUX ring buffer;
  599. - Since the hardware trace driver has stored new trace data into the AUX
  600. ring buffer, the argument *size* indicates how many bytes have been
  601. consumed by the hardware tracing, thus ``perf_aux_output_end()`` updates the
  602. header pointer ``perf_buffer::aux_head`` to reflect the latest buffer usage.
  603. At the end, the PMU driver will restart hardware tracing. During this
  604. temporary suspending period, it will lose hardware trace data, which
  605. will introduce a discontinuity during decoding phase.
  606. The event ``PERF_RECORD_AUX`` presents an AUX event which is handled in the
  607. kernel, but it lacks the information for saving the AUX trace data in
  608. the perf file. When the perf tool copies the trace data from AUX ring
  609. buffer to the perf data file, it synthesizes a ``PERF_RECORD_AUXTRACE``
  610. event which is not a kernel ABI, it's defined by the perf tool to describe
  611. which portion of data in the AUX ring buffer is saved. Afterwards, the perf
  612. tool reads out the AUX trace data from the perf file based on the
  613. ``PERF_RECORD_AUXTRACE`` events, and the ``PERF_RECORD_AUX`` event is used to
  614. decode a chunk of data by correlating with time order.
  615. 3.3 Snapshot mode
  616. -----------------
  617. Perf supports snapshot mode for AUX ring buffer, in this mode, users
  618. only record AUX trace data at a specific time point which users are
  619. interested in. E.g. below gives an example of how to take snapshots
  620. with 1 second interval with Arm CoreSight::
  621. perf record -e cs_etm/@tmc_etr0/u -S -a program &
  622. PERFPID=$!
  623. while true; do
  624. kill -USR2 $PERFPID
  625. sleep 1
  626. done
  627. The main flow for snapshot mode is:
  628. - Before a snapshot is taken, the AUX ring buffer acts in free run mode.
  629. During free run mode the perf doesn't record any of the AUX events and
  630. trace data;
  631. - Once the perf tool receives the *USR2* signal, it triggers the callback
  632. function ``auxtrace_record::snapshot_start()`` to deactivate hardware
  633. tracing. The kernel driver then populates the AUX ring buffer with the
  634. hardware trace data, and the event ``PERF_RECORD_AUX`` is stored in the
  635. regular ring buffer;
  636. - Then perf tool takes a snapshot, ``record__read_auxtrace_snapshot()``
  637. reads out the hardware trace data from the AUX ring buffer and saves it
  638. into perf data file;
  639. - After the snapshot is finished, ``auxtrace_record::snapshot_finish()``
  640. restarts the PMU event for AUX tracing.
  641. The perf only accesses the head pointer ``perf_event_mmap_page::aux_head``
  642. in snapshot mode and doesn’t touch tail pointer ``aux_tail``, this is
  643. because the AUX ring buffer can overflow in free run mode, the tail
  644. pointer is useless in this case. Alternatively, the callback
  645. ``auxtrace_record::find_snapshot()`` is introduced for making the decision
  646. of whether the AUX ring buffer has been wrapped around or not, at the
  647. end it fixes up the AUX buffer's head which are used to calculate the
  648. trace data size.
  649. As we know, the buffers' deployment can be per-thread mode, per-CPU
  650. mode, or system wide mode, and the snapshot can be applied to any of
  651. these modes. Below is an example of taking snapshot with system wide
  652. mode.
  653. ::
  654. Snapshot is taken
  655. |
  656. v
  657. +------------------------+
  658. | AUX Ring buffer 0 | <- aux_head
  659. +------------------------+
  660. v
  661. +--------------------------------+
  662. | AUX Ring buffer 1 | <- aux_head
  663. +--------------------------------+
  664. v
  665. +--------------------------------------------+
  666. | AUX Ring buffer 2 | <- aux_head
  667. +--------------------------------------------+
  668. v
  669. +---------------------------------------+
  670. | AUX Ring buffer 3 | <- aux_head
  671. +---------------------------------------+
  672. Figure 9. Snapshot with system wide mode