€•&Œsphinx.addnodes”Œdocument”“”)�”}”(Œ rawsource”Œ”Œchildren”]”(Œ translations”Œ LanguagesNode”“”)�”}”(hhh]”(hŒ pending_xref”“”)�”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…”�”}”Œparent”hsbaŒ attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ2/translations/zh_CN/userspace-api/perf_ring_buffer”Œmodname”NŒ classname”NŒ refexplicit”ˆuŒtagname”hhh ubh)�”}”(hhh]”hŒChinese (Traditional)”…”�”}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ2/translations/zh_TW/userspace-api/perf_ring_buffer”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒItalian”…”�”}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ2/translations/it_IT/userspace-api/perf_ring_buffer”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒJapanese”…”�”}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ2/translations/ja_JP/userspace-api/perf_ring_buffer”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒKorean”…”�”}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ2/translations/ko_KR/userspace-api/perf_ring_buffer”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒPortuguese (Brazilian)”…”�”}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ2/translations/pt_BR/userspace-api/perf_ring_buffer”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒSpanish”…”�”}”hh–sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ2/translations/sp_SP/userspace-api/perf_ring_buffer”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h hhŒ _document”hŒsource”NŒline”NubhŒcomment”“”)�”}”(hŒ SPDX-License-Identifier: GPL-2.0”h]”hŒ SPDX-License-Identifier: GPL-2.0”…”�”}”hh·sbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1hµhhh²hh³ŒL/var/lib/git/docbuild/linux/Documentation/userspace-api/perf_ring_buffer.rst”h´KubhŒsection”“”)�”}”(hhh]”(hŒtitle”“”)�”}”(hŒPerf ring buffer”h]”hŒPerf ring buffer”…”�”}”(hhÏh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhhÊh²hh³hÇh´Kubh¶)�”}”(hX@CONTENTS 1. Introduction 2. Ring buffer implementation 2.1 Basic algorithm 2.2 Ring buffer for different tracing modes 2.2.1 Default mode 2.2.2 Per-thread mode 2.2.3 Per-CPU mode 2.2.4 System wide mode 2.3 Accessing buffer 2.3.1 Producer-consumer model 2.3.2 Properties of the ring buffers 2.3.3 Writing samples into buffer 2.3.4 Reading samples from buffer 2.3.5 Memory synchronization 3. The mechanism of AUX ring buffer 3.1 The relationship between AUX and regular ring buffers 3.2 AUX events 3.3 Snapshot mode”h]”hX@CONTENTS 1. Introduction 2. Ring buffer implementation 2.1 Basic algorithm 2.2 Ring buffer for different tracing modes 2.2.1 Default mode 2.2.2 Per-thread mode 2.2.3 Per-CPU mode 2.2.4 System wide mode 2.3 Accessing buffer 2.3.1 Producer-consumer model 2.3.2 Properties of the ring buffers 2.3.3 Writing samples into buffer 2.3.4 Reading samples from buffer 2.3.5 Memory synchronization 3. The mechanism of AUX ring buffer 3.1 The relationship between AUX and regular ring buffers 3.2 AUX events 3.3 Snapshot mode”…”�”}”hhÝsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1hµhhÊh²hh³hÇh´KubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ1. Introduction”h]”hŒ1. Introduction”…”�”}”(hhîh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhhëh²hh³hÇh´K ubhŒ paragraph”“”)�”}”(hX'The ring buffer is a fundamental mechanism for data transfer. perf uses ring buffers to transfer event data from kernel to user space, another kind of ring buffer which is so called auxiliary (AUX) ring buffer also plays an important role for hardware tracing with Intel PT, Arm CoreSight, etc.”h]”hX'The ring buffer is a fundamental mechanism for data transfer. perf uses ring buffers to transfer event data from kernel to user space, another kind of ring buffer which is so called auxiliary (AUX) ring buffer also plays an important role for hardware tracing with Intel PT, Arm CoreSight, etc.”…”�”}”(hhþh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´K"hhëh²hubhý)�”}”(hX„The ring buffer implementation is critical but it's also a very challenging work. On the one hand, the kernel and perf tool in the user space use the ring buffer to exchange data and stores data into data file, thus the ring buffer needs to transfer data with high throughput; on the other hand, the ring buffer management should avoid significant overload to distract profiling results.”h]”hX†The ring buffer implementation is critical but it’s also a very challenging work. On the one hand, the kernel and perf tool in the user space use the ring buffer to exchange data and stores data into data file, thus the ring buffer needs to transfer data with high throughput; on the other hand, the ring buffer management should avoid significant overload to distract profiling results.”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´K(hhëh²hubhý)�”}”(hŒÅThis documentation dives into the details for perf ring buffer with two parts: firstly it explains the perf ring buffer implementation, then the second part discusses the AUX ring buffer mechanism.”h]”hŒÅThis documentation dives into the details for perf ring buffer with two parts: firstly it explains the perf ring buffer implementation, then the second part discusses the AUX ring buffer mechanism.”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´K/hhëh²hubeh}”(h]”Œ introduction”ah ]”h"]”Œ1. introduction”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´K ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ2. Ring buffer implementation”h]”hŒ2. Ring buffer implementation”…”�”}”(hj3h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj0h²hh³hÇh´K4ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ2.1 Basic algorithm”h]”hŒ2.1 Basic algorithm”…”�”}”(hjDh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjAh²hh³hÇh´K7ubhý)�”}”(hŒ·That said, a typical ring buffer is managed by a head pointer and a tail pointer; the head pointer is manipulated by a writer and the tail pointer is updated by a reader respectively.”h]”hŒ·That said, a typical ring buffer is managed by a head pointer and a tail pointer; the head pointer is manipulated by a writer and the tail pointer is updated by a reader respectively.”…”�”}”(hjRh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´K9hjAh²hubhŒ literal_block”“”)�”}”(hŒ¼+---------------------------+ | | |***|***|***| | | +---------------------------+ `-> Tail `-> Head * : the data is filled by the writer. Figure 1. Ring buffer”h]”hŒ¼+---------------------------+ | | |***|***|***| | | +---------------------------+ `-> Tail `-> Head * : the data is filled by the writer. Figure 1. Ring buffer”…”�”}”hjbsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´K?hjAh²hubhý)�”}”(hX¢Perf uses the same way to manage its ring buffer. In the implementation there are two key data structures held together in a set of consecutive pages, the control structure and then the ring buffer itself. The page with the control structure in is known as the "user page". Being held in continuous virtual addresses simplifies locating the ring buffer address, it is in the pages after the page with the user page.”h]”hX¦Perf uses the same way to manage its ring buffer. In the implementation there are two key data structures held together in a set of consecutive pages, the control structure and then the ring buffer itself. The page with the control structure in is known as the “user pageâ€�. Being held in continuous virtual addresses simplifies locating the ring buffer address, it is in the pages after the page with the user page.”…”�”}”(hjph²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´KHhjAh²hubhý)�”}”(hXXThe control structure is named as ``perf_event_mmap_page``, it contains a head pointer ``data_head`` and a tail pointer ``data_tail``. When the kernel starts to fill records into the ring buffer, it updates the head pointer to reserve the memory so later it can safely store events into the buffer. On the other side, when the user page is a writable mapping, the perf tool has the permission to update the tail pointer after consuming data from the ring buffer. Yet another case is for the user page's read-only mapping, which is to be addressed in the section :ref:`writing_samples_into_buffer`.”h]”(hŒ"The control structure is named as ”…”�”}”(hj~h²hh³Nh´NubhŒliteral”“”)�”}”(hŒ``perf_event_mmap_page``”h]”hŒperf_event_mmap_page”…”�”}”(hjˆh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj~ubhŒ, it contains a head pointer ”…”�”}”(hj~h²hh³Nh´Nubj‡)�”}”(hŒ ``data_head``”h]”hŒ data_head”…”�”}”(hjšh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj~ubhŒ and a tail pointer ”…”�”}”(hj~h²hh³Nh´Nubj‡)�”}”(hŒ ``data_tail``”h]”hŒ data_tail”…”�”}”(hj¬h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj~ubhX². When the kernel starts to fill records into the ring buffer, it updates the head pointer to reserve the memory so later it can safely store events into the buffer. On the other side, when the user page is a writable mapping, the perf tool has the permission to update the tail pointer after consuming data from the ring buffer. Yet another case is for the user page’s read-only mapping, which is to be addressed in the section ”…”�”}”(hj~h²hh³Nh´Nubh)�”}”(hŒ":ref:`writing_samples_into_buffer`”h]”hŒinline”“”)�”}”(hjÀh]”hŒwriting_samples_into_buffer”…”�”}”(hjÄh²hh³Nh´Nubah}”(h]”h ]”(Œxref”Œstd”Œstd-ref”eh"]”h$]”h&]”uh1jÂhj¾ubah}”(h]”h ]”h"]”h$]”h&]”Œrefdoc”Œuserspace-api/perf_ring_buffer”Œ refdomain”jÏŒreftype”Œref”Œ refexplicit”‰Œrefwarn”ˆŒ reftarget”Œwriting_samples_into_buffer”uh1hh³hÇh´KOhj~ubhŒ.”…”�”}”(hj~h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´KOhjAh²hubja)�”}”(hX¿ user page ring buffer +---------+---------+ +---------------------------------------+ |data_head|data_tail|...| | |***|***|***|***|***| | | | +---------+---------+ +---------------------------------------+ ` `----------------^ ^ `----------------------------------------------| * : the data is filled by the writer. Figure 2. Perf ring buffer”h]”hX¿ user page ring buffer +---------+---------+ +---------------------------------------+ |data_head|data_tail|...| | |***|***|***|***|***| | | | +---------+---------+ +---------------------------------------+ ` `----------------^ ^ `----------------------------------------------| * : the data is filled by the writer. Figure 2. Perf ring buffer”…”�”}”hjísbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´K[hjAh²hubhý)�”}”(hX°When using the ``perf record`` tool, we can specify the ring buffer size with option ``-m`` or ``--mmap-pages=``, the given size will be rounded up to a power of two that is a multiple of a page size. Though the kernel allocates at once for all memory pages, it's deferred to map the pages to VMA area until the perf tool accesses the buffer from the user space. In other words, at the first time accesses the buffer's page from user space in the perf tool, a data abort exception for page fault is taken and the kernel uses this occasion to map the page into process VMA (see ``perf_mmap_fault()``), thus the perf tool can continue to access the page after returning from the exception.”h]”(hŒWhen using the ”…”�”}”(hjûh²hh³Nh´Nubj‡)�”}”(hŒ``perf record``”h]”hŒ perf record”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjûubhŒ7 tool, we can specify the ring buffer size with option ”…”�”}”(hjûh²hh³Nh´Nubj‡)�”}”(hŒ``-m``”h]”hŒ-m”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjûubhŒ or ”…”�”}”(hjûh²hh³Nh´Nubj‡)�”}”(hŒ``--mmap-pages=``”h]”hŒ --mmap-pages=”…”�”}”(hj'h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjûubhXÖ, the given size will be rounded up to a power of two that is a multiple of a page size. Though the kernel allocates at once for all memory pages, it’s deferred to map the pages to VMA area until the perf tool accesses the buffer from the user space. In other words, at the first time accesses the buffer’s page from user space in the perf tool, a data abort exception for page fault is taken and the kernel uses this occasion to map the page into process VMA (see ”…”�”}”(hjûh²hh³Nh´Nubj‡)�”}”(hŒ``perf_mmap_fault()``”h]”hŒperf_mmap_fault()”…”�”}”(hj9h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjûubhŒY), thus the perf tool can continue to access the page after returning from the exception.”…”�”}”(hjûh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´KfhjAh²hubeh}”(h]”Œbasic-algorithm”ah ]”h"]”Œ2.1 basic algorithm”ah$]”h&]”uh1hÈhj0h²hh³hÇh´K7ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ+2.2 Ring buffer for different tracing modes”h]”hŒ+2.2 Ring buffer for different tracing modes”…”�”}”(hj\h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjYh²hh³hÇh´Krubhý)�”}”(hXThe perf profiles programs with different modes: default mode, per thread mode, per cpu mode, and system wide mode. This section describes these modes and how the ring buffer meets requirements for them. At last we will review the race conditions caused by these modes.”h]”hXThe perf profiles programs with different modes: default mode, per thread mode, per cpu mode, and system wide mode. This section describes these modes and how the ring buffer meets requirements for them. At last we will review the race conditions caused by these modes.”…”�”}”(hjjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´KthjYh²hubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ2.2.1 Default mode”h]”hŒ2.2.1 Default mode”…”�”}”(hj{h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjxh²hh³hÇh´Kzubhý)�”}”(hŒeUsually we execute ``perf record`` command followed by a profiling program name, like below command::”h]”(hŒUsually we execute ”…”�”}”(hj‰h²hh³Nh´Nubj‡)�”}”(hŒ``perf record``”h]”hŒ perf record”…”�”}”(hj‘h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj‰ubhŒB command followed by a profiling program name, like below command:”…”�”}”(hj‰h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´K|hjxh²hubja)�”}”(hŒperf record test_program”h]”hŒperf record test_program”…”�”}”hj©sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´Khjxh²hubhý)�”}”(hXXThis command doesn't specify any options for CPU and thread modes, the perf tool applies the default mode on the perf event. It maps all the CPUs in the system and the profiled program's PID on the perf event, and it enables inheritance mode on the event so that child tasks inherits the events. As a result, the perf event is attributed as::”h]”hX[This command doesn’t specify any options for CPU and thread modes, the perf tool applies the default mode on the perf event. It maps all the CPUs in the system and the profiled program’s PID on the perf event, and it enables inheritance mode on the event so that child tasks inherits the events. As a result, the perf event is attributed as:”…”�”}”(hj·h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´K�hjxh²hubja)�”}”(hŒqevsel::cpus::map[] = { 0 .. _SC_NPROCESSORS_ONLN-1 } evsel::threads::map[] = { pid } evsel::attr::inherit = 1”h]”hŒqevsel::cpus::map[] = { 0 .. _SC_NPROCESSORS_ONLN-1 } evsel::threads::map[] = { pid } evsel::attr::inherit = 1”…”�”}”hjÅsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´K‡hjxh²hubhý)�”}”(hXThese attributions finally will be reflected on the deployment of ring buffers. As shown below, the perf tool allocates individual ring buffer for each CPU, but it only enables events for the profiled program rather than for all threads in the system. The *T1* thread represents the thread context of the 'test_program', whereas *T2* and *T3* are irrelevant threads in the system. The perf samples are exclusively collected for the *T1* thread and stored in the ring buffer associated with the CPU on which the *T1* thread is running.”h]”(hXThese attributions finally will be reflected on the deployment of ring buffers. As shown below, the perf tool allocates individual ring buffer for each CPU, but it only enables events for the profiled program rather than for all threads in the system. The ”…”�”}”(hjÓh²hh³Nh´NubhŒemphasis”“”)�”}”(hŒ*T1*”h]”hŒT1”…”�”}”(hjÝh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jÛhjÓubhŒI thread represents the thread context of the ‘test_program’, whereas ”…”�”}”(hjÓh²hh³Nh´NubjÜ)�”}”(hŒ*T2*”h]”hŒT2”…”�”}”(hjïh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jÛhjÓubhŒ and ”…”�”}”(hjÓh²hh³Nh´NubjÜ)�”}”(hŒ*T3*”h]”hŒT3”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jÛhjÓubhŒ\ are irrelevant threads in the system. The perf samples are exclusively collected for the ”…”�”}”(hjÓh²hh³Nh´NubjÜ)�”}”(hŒ*T1*”h]”hŒT1”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jÛhjÓubhŒK thread and stored in the ring buffer associated with the CPU on which the ”…”�”}”(hjÓh²hh³Nh´NubjÜ)�”}”(hŒ*T1*”h]”hŒT1”…”�”}”(hj%h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jÛhjÓubhŒ thread is running.”…”�”}”(hjÓh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´K‹hjxh²hubja)�”}”(hX¨ T1 T2 T1 +----+ +-----------+ +----+ CPU0 |xxxx| |xxxxxxxxxxx| |xxxx| +----+--------------+-----------+----------+----+--------> | | v v +-----------------------------------------------------+ | Ring buffer 0 | +-----------------------------------------------------+ T1 +-----+ CPU1 |xxxxx| -----+-----+---------------------------------------------> | v +-----------------------------------------------------+ | Ring buffer 1 | +-----------------------------------------------------+ T1 T3 +----+ +-------+ CPU2 |xxxx| |xxxxxxx| --------------------------+----+--------+-------+--------> | v +-----------------------------------------------------+ | Ring buffer 2 | +-----------------------------------------------------+ T1 +--------------+ CPU3 |xxxxxxxxxxxxxx| -----------+--------------+------------------------------> | v +-----------------------------------------------------+ | Ring buffer 3 | +-----------------------------------------------------+ T1: Thread 1; T2: Thread 2; T3: Thread 3 x: Thread is in running state Figure 3. Ring buffer for default mode”h]”hX¨ T1 T2 T1 +----+ +-----------+ +----+ CPU0 |xxxx| |xxxxxxxxxxx| |xxxx| +----+--------------+-----------+----------+----+--------> | | v v +-----------------------------------------------------+ | Ring buffer 0 | +-----------------------------------------------------+ T1 +-----+ CPU1 |xxxxx| -----+-----+---------------------------------------------> | v +-----------------------------------------------------+ | Ring buffer 1 | +-----------------------------------------------------+ T1 T3 +----+ +-------+ CPU2 |xxxx| |xxxxxxx| --------------------------+----+--------+-------+--------> | v +-----------------------------------------------------+ | Ring buffer 2 | +-----------------------------------------------------+ T1 +--------------+ CPU3 |xxxxxxxxxxxxxx| -----------+--------------+------------------------------> | v +-----------------------------------------------------+ | Ring buffer 3 | +-----------------------------------------------------+ T1: Thread 1; T2: Thread 2; T3: Thread 3 x: Thread is in running state Figure 3. Ring buffer for default mode”…”�”}”hj=sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´K–hjxh²hubeh}”(h]”Œ default-mode”ah ]”h"]”Œ2.2.1 default mode”ah$]”h&]”uh1hÈhjYh²hh³hÇh´KzubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ2.2.2 Per-thread mode”h]”hŒ2.2.2 Per-thread mode”…”�”}”(hjVh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjSh²hh³hÇh´KÄubhý)�”}”(hŒ;By specifying option ``--per-thread`` in perf command, e.g.”h]”(hŒBy specifying option ”…”�”}”(hjdh²hh³Nh´Nubj‡)�”}”(hŒ``--per-thread``”h]”hŒ --per-thread”…”�”}”(hjlh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjdubhŒ in perf command, e.g.”…”�”}”(hjdh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´KÆhjSh²hubja)�”}”(hŒ%perf record --per-thread test_program”h]”hŒ%perf record --per-thread test_program”…”�”}”hj„sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´KÊhjSh²hubhý)�”}”(hŒ{The perf event doesn't map to any CPUs and is only bound to the profiled process, thus, the perf event's attributions are::”h]”hŒ~The perf event doesn’t map to any CPUs and is only bound to the profiled process, thus, the perf event’s attributions are:”…”�”}”(hj’h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´KÌhjSh²hubja)�”}”(hŒXevsel::cpus::map[0] = { -1 } evsel::threads::map[] = { pid } evsel::attr::inherit = 0”h]”hŒXevsel::cpus::map[0] = { -1 } evsel::threads::map[] = { pid } evsel::attr::inherit = 0”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´KÏhjSh²hubhý)�”}”(hXƒIn this mode, a single ring buffer is allocated for the profiled thread; if the thread is scheduled on a CPU, the events on that CPU will be enabled; and if the thread is scheduled out from the CPU, the events on the CPU will be disabled. When the thread is migrated from one CPU to another, the events are to be disabled on the previous CPU and enabled on the next CPU correspondingly.”h]”hXƒIn this mode, a single ring buffer is allocated for the profiled thread; if the thread is scheduled on a CPU, the events on that CPU will be enabled; and if the thread is scheduled out from the CPU, the events on the CPU will be disabled. When the thread is migrated from one CPU to another, the events are to be disabled on the previous CPU and enabled on the next CPU correspondingly.”…”�”}”(hj®h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´KÓhjSh²hubja)�”}”(hXú T1 T2 T1 +----+ +-----------+ +----+ CPU0 |xxxx| |xxxxxxxxxxx| |xxxx| +----+--------------+-----------+----------+----+--------> | | | T1 | | +-----+ | CPU1 | |xxxxx| | --|--+-----+----------------------------------|----------> | | | | | T1 T3 | | | +----+ +---+ | CPU2 | | |xxxx| |xxx| | --|-----|-----------------+----+--------+---+-|----------> | | | | | | T1 | | | | +--------------+ | | CPU3 | | |xxxxxxxxxxxxxx| | | --|-----|--+--------------+-|-----------------|----------> | | | | | v v v v v +-----------------------------------------------------+ | Ring buffer | +-----------------------------------------------------+ T1: Thread 1 x: Thread is in running state Figure 4. Ring buffer for per-thread mode”h]”hXú T1 T2 T1 +----+ +-----------+ +----+ CPU0 |xxxx| |xxxxxxxxxxx| |xxxx| +----+--------------+-----------+----------+----+--------> | | | T1 | | +-----+ | CPU1 | |xxxxx| | --|--+-----+----------------------------------|----------> | | | | | T1 T3 | | | +----+ +---+ | CPU2 | | |xxxx| |xxx| | --|-----|-----------------+----+--------+---+-|----------> | | | | | | T1 | | | | +--------------+ | | CPU3 | | |xxxxxxxxxxxxxx| | | --|-----|--+--------------+-|-----------------|----------> | | | | | v v v v v +-----------------------------------------------------+ | Ring buffer | +-----------------------------------------------------+ T1: Thread 1 x: Thread is in running state Figure 4. Ring buffer for per-thread mode”…”�”}”hj¼sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´KÜhjSh²hubhý)�”}”(hX_When perf runs in per-thread mode, a ring buffer is allocated for the profiled thread *T1*. The ring buffer is dedicated for thread *T1*, if the thread *T1* is running, the perf events will be recorded into the ring buffer; when the thread is sleeping, all associated events will be disabled, thus no trace data will be recorded into the ring buffer.”h]”(hŒVWhen perf runs in per-thread mode, a ring buffer is allocated for the profiled thread ”…”�”}”(hjÊh²hh³Nh´NubjÜ)�”}”(hŒ*T1*”h]”hŒT1”…”�”}”(hjÒh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jÛhjÊubhŒ+. The ring buffer is dedicated for thread ”…”�”}”(hjÊh²hh³Nh´NubjÜ)�”}”(hŒ*T1*”h]”hŒT1”…”�”}”(hjäh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jÛhjÊubhŒ, if the thread ”…”�”}”(hjÊh²hh³Nh´NubjÜ)�”}”(hŒ*T1*”h]”hŒT1”…”�”}”(hjöh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jÛhjÊubhŒÂ is running, the perf events will be recorded into the ring buffer; when the thread is sleeping, all associated events will be disabled, thus no trace data will be recorded into the ring buffer.”…”�”}”(hjÊh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´KúhjSh²hubeh}”(h]”Œper-thread-mode”ah ]”h"]”Œ2.2.2 per-thread mode”ah$]”h&]”uh1hÈhjYh²hh³hÇh´KÄubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ2.2.3 Per-CPU mode”h]”hŒ2.2.3 Per-CPU mode”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjh²hh³hÇh´Mubhý)�”}”(hŒ�The option ``-C`` is used to collect samples on the list of CPUs, for example the below perf command receives option ``-C 0,2``::”h]”(hŒ The option ”…”�”}”(hj'h²hh³Nh´Nubj‡)�”}”(hŒ``-C``”h]”hŒ-C”…”�”}”(hj/h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj'ubhŒd is used to collect samples on the list of CPUs, for example the below perf command receives option ”…”�”}”(hj'h²hh³Nh´Nubj‡)�”}”(hŒ ``-C 0,2``”h]”hŒ-C 0,2”…”�”}”(hjAh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj'ubhŒ:”…”�”}”(hj'h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mhjh²hubja)�”}”(hŒperf record -C 0,2 test_program”h]”hŒperf record -C 0,2 test_program”…”�”}”hjYsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´Mhjh²hubhý)�”}”(hŒ‚It maps the perf event to CPUs 0 and 2, and the event is not associated to any PID. Thus the perf event attributions are set as::”h]”hŒ�It maps the perf event to CPUs 0 and 2, and the event is not associated to any PID. Thus the perf event attributions are set as:”…”�”}”(hjgh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mhjh²hubja)�”}”(hŒYevsel::cpus::map[0] = { 0, 2 } evsel::threads::map[] = { -1 } evsel::attr::inherit = 0”h]”hŒYevsel::cpus::map[0] = { 0, 2 } evsel::threads::map[] = { -1 } evsel::attr::inherit = 0”…”�”}”hjusbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´M hjh²hubhý)�”}”(hXThis results in the session of ``perf record`` will sample all threads on CPU0 and CPU2, and be terminated until test_program exits. Even there have tasks running on CPU1 and CPU3, since the ring buffer is absent for them, any activities on these two CPUs will be ignored. A usage case is to combine the options for per-thread mode and per-CPU mode, e.g. the options ``–C 0,2`` and ``––per–thread`` are specified together, the samples are recorded only when the profiled thread is scheduled on any of the listed CPUs.”h]”(hŒThis results in the session of ”…”�”}”(hjƒh²hh³Nh´Nubj‡)�”}”(hŒ``perf record``”h]”hŒ perf record”…”�”}”(hj‹h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjƒubhXC will sample all threads on CPU0 and CPU2, and be terminated until test_program exits. Even there have tasks running on CPU1 and CPU3, since the ring buffer is absent for them, any activities on these two CPUs will be ignored. A usage case is to combine the options for per-thread mode and per-CPU mode, e.g. the options ”…”�”}”(hjƒh²hh³Nh´Nubj‡)�”}”(hŒ ``–C 0,2``”h]”hŒ–C 0,2”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjƒubhŒ and ”…”�”}”(hjƒh²hh³Nh´Nubj‡)�”}”(hŒ``––per–thread``”h]”hŒ––per–thread”…”�”}”(hj¯h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjƒubhŒw are specified together, the samples are recorded only when the profiled thread is scheduled on any of the listed CPUs.”…”�”}”(hjƒh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mhjh²hubja)�”}”(hXì T1 T2 T1 +----+ +-----------+ +----+ CPU0 |xxxx| |xxxxxxxxxxx| |xxxx| +----+--------------+-----------+----------+----+--------> | | | v v v +-----------------------------------------------------+ | Ring buffer 0 | +-----------------------------------------------------+ T1 +-----+ CPU1 |xxxxx| -----+-----+---------------------------------------------> T1 T3 +----+ +-------+ CPU2 |xxxx| |xxxxxxx| --------------------------+----+--------+-------+--------> | | v v +-----------------------------------------------------+ | Ring buffer 1 | +-----------------------------------------------------+ T1 +--------------+ CPU3 |xxxxxxxxxxxxxx| -----------+--------------+------------------------------> T1: Thread 1; T2: Thread 2; T3: Thread 3 x: Thread is in running state Figure 5. Ring buffer for per-CPU mode”h]”hXì T1 T2 T1 +----+ +-----------+ +----+ CPU0 |xxxx| |xxxxxxxxxxx| |xxxx| +----+--------------+-----------+----------+----+--------> | | | v v v +-----------------------------------------------------+ | Ring buffer 0 | +-----------------------------------------------------+ T1 +-----+ CPU1 |xxxxx| -----+-----+---------------------------------------------> T1 T3 +----+ +-------+ CPU2 |xxxx| |xxxxxxx| --------------------------+----+--------+-------+--------> | | v v +-----------------------------------------------------+ | Ring buffer 1 | +-----------------------------------------------------+ T1 +--------------+ CPU3 |xxxxxxxxxxxxxx| -----------+--------------+------------------------------> T1: Thread 1; T2: Thread 2; T3: Thread 3 x: Thread is in running state Figure 5. Ring buffer for per-CPU mode”…”�”}”hjÇsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´Mhjh²hubeh}”(h]”Œ per-cpu-mode”ah ]”h"]”Œ2.2.3 per-cpu mode”ah$]”h&]”uh1hÈhjYh²hh³hÇh´MubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ2.2.4 System wide mode”h]”hŒ2.2.4 System wide mode”…”�”}”(hjàh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjÝh²hh³hÇh´M=ubhý)�”}”(hŒ—By using option ``–a`` or ``––all–cpus``, perf collects samples on all CPUs for all tasks, we call it as the system wide mode, the command is::”h]”(hŒBy using option ”…”�”}”(hjîh²hh³Nh´Nubj‡)�”}”(hŒ``–a``”h]”hŒ–a”…”�”}”(hjöh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjîubhŒ or ”…”�”}”(hjîh²hh³Nh´Nubj‡)�”}”(hŒ``––all–cpus``”h]”hŒ––all–cpus”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjîubhŒf, perf collects samples on all CPUs for all tasks, we call it as the system wide mode, the command is:”…”�”}”(hjîh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M?hjÝh²hubja)�”}”(hŒperf record -a test_program”h]”hŒperf record -a test_program”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´MBhjÝh²hubhý)�”}”(hŒlSimilar to the per-CPU mode, the perf event doesn't bind to any PID, and it maps to all CPUs in the system::”h]”hŒmSimilar to the per-CPU mode, the perf event doesn’t bind to any PID, and it maps to all CPUs in the system:”…”�”}”(hj.h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MDhjÝh²hubja)�”}”(hŒpevsel::cpus::map[] = { 0 .. _SC_NPROCESSORS_ONLN-1 } evsel::threads::map[] = { -1 } evsel::attr::inherit = 0”h]”hŒpevsel::cpus::map[] = { 0 .. _SC_NPROCESSORS_ONLN-1 } evsel::threads::map[] = { -1 } evsel::attr::inherit = 0”…”�”}”hj<sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´MGhjÝh²hubhý)�”}”(hŒÓIn the system wide mode, every CPU has its own ring buffer, all threads are monitored during the running state and the samples are recorded into the ring buffer belonging to the CPU which the events occurred on.”h]”hŒÓIn the system wide mode, every CPU has its own ring buffer, all threads are monitored during the running state and the samples are recorded into the ring buffer belonging to the CPU which the events occurred on.”…”�”}”(hjJh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MKhjÝh²hubja)�”}”(hXÌ T1 T2 T1 +----+ +-----------+ +----+ CPU0 |xxxx| |xxxxxxxxxxx| |xxxx| +----+--------------+-----------+----------+----+--------> | | | v v v +-----------------------------------------------------+ | Ring buffer 0 | +-----------------------------------------------------+ T1 +-----+ CPU1 |xxxxx| -----+-----+---------------------------------------------> | v +-----------------------------------------------------+ | Ring buffer 1 | +-----------------------------------------------------+ T1 T3 +----+ +-------+ CPU2 |xxxx| |xxxxxxx| --------------------------+----+--------+-------+--------> | | v v +-----------------------------------------------------+ | Ring buffer 2 | +-----------------------------------------------------+ T1 +--------------+ CPU3 |xxxxxxxxxxxxxx| -----------+--------------+------------------------------> | v +-----------------------------------------------------+ | Ring buffer 3 | +-----------------------------------------------------+ T1: Thread 1; T2: Thread 2; T3: Thread 3 x: Thread is in running state Figure 6. Ring buffer for system wide mode”h]”hXÌ T1 T2 T1 +----+ +-----------+ +----+ CPU0 |xxxx| |xxxxxxxxxxx| |xxxx| +----+--------------+-----------+----------+----+--------> | | | v v v +-----------------------------------------------------+ | Ring buffer 0 | +-----------------------------------------------------+ T1 +-----+ CPU1 |xxxxx| -----+-----+---------------------------------------------> | v +-----------------------------------------------------+ | Ring buffer 1 | +-----------------------------------------------------+ T1 T3 +----+ +-------+ CPU2 |xxxx| |xxxxxxx| --------------------------+----+--------+-------+--------> | | v v +-----------------------------------------------------+ | Ring buffer 2 | +-----------------------------------------------------+ T1 +--------------+ CPU3 |xxxxxxxxxxxxxx| -----------+--------------+------------------------------> | v +-----------------------------------------------------+ | Ring buffer 3 | +-----------------------------------------------------+ T1: Thread 1; T2: Thread 2; T3: Thread 3 x: Thread is in running state Figure 6. Ring buffer for system wide mode”…”�”}”hjXsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´MQhjÝh²hubeh}”(h]”Œsystem-wide-mode”ah ]”h"]”Œ2.2.4 system wide mode”ah$]”h&]”uh1hÈhjYh²hh³hÇh´M=ubeh}”(h]”Œ'ring-buffer-for-different-tracing-modes”ah ]”h"]”Œ+2.2 ring buffer for different tracing modes”ah$]”h&]”uh1hÈhj0h²hh³hÇh´KrubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ2.3 Accessing buffer”h]”hŒ2.3 Accessing buffer”…”�”}”(hjyh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjvh²hh³hÇh´Mubhý)�”}”(hŒ~Based on the understanding of how the ring buffer is allocated in various modes, this section explains access the ring buffer.”h]”hŒ~Based on the understanding of how the ring buffer is allocated in various modes, this section explains access the ring buffer.”…”�”}”(hj‡h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M�hjvh²hubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ2.3.1 Producer-consumer model”h]”hŒ2.3.1 Producer-consumer model”…”�”}”(hj˜h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj•h²hh³hÇh´M…ubhý)�”}”(hX<In the Linux kernel, the PMU events can produce samples which are stored into the ring buffer; the perf command in user space consumes the samples by reading out data from the ring buffer and finally saves the data into the file for post analysis. It’s a typical producer-consumer model for using the ring buffer.”h]”hX<In the Linux kernel, the PMU events can produce samples which are stored into the ring buffer; the perf command in user space consumes the samples by reading out data from the ring buffer and finally saves the data into the file for post analysis. It’s a typical producer-consumer model for using the ring buffer.”…”�”}”(hj¦h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M‡hj•h²hubhý)�”}”(hX£The perf process polls on the PMU events and sleeps when no events are incoming. To prevent frequent exchanges between the kernel and user space, the kernel event core layer introduces a watermark, which is stored in the ``perf_buffer::watermark``. When a sample is recorded into the ring buffer, and if the used buffer exceeds the watermark, the kernel wakes up the perf process to read samples from the ring buffer.”h]”(hŒÞThe perf process polls on the PMU events and sleeps when no events are incoming. To prevent frequent exchanges between the kernel and user space, the kernel event core layer introduces a watermark, which is stored in the ”…”�”}”(hj´h²hh³Nh´Nubj‡)�”}”(hŒ``perf_buffer::watermark``”h]”hŒperf_buffer::watermark”…”�”}”(hj¼h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj´ubhŒ«. When a sample is recorded into the ring buffer, and if the used buffer exceeds the watermark, the kernel wakes up the perf process to read samples from the ring buffer.”…”�”}”(hj´h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M�hj•h²hubja)�”}”(hXÎ Perf / | Read samples Polling / `--------------| Ring buffer v v ;---------------------v +----------------+ +---------+---------+ +-------------------+ |Event wait queue| |data_head|data_tail| |***|***| | |***| +----------------+ +---------+---------+ +-------------------+ ^ ^ `------------------------^ | Wake up tasks | Store samples +-----------------------------+ | Kernel event core layer | +-----------------------------+ * : the data is filled by the writer. Figure 7. Writing and reading the ring buffer”h]”hXÎ Perf / | Read samples Polling / `--------------| Ring buffer v v ;---------------------v +----------------+ +---------+---------+ +-------------------+ |Event wait queue| |data_head|data_tail| |***|***| | |***| +----------------+ +---------+---------+ +-------------------+ ^ ^ `------------------------^ | Wake up tasks | Store samples +-----------------------------+ | Kernel event core layer | +-----------------------------+ * : the data is filled by the writer. Figure 7. Writing and reading the ring buffer”…”�”}”hjÔsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´M–hj•h²hubhý)�”}”(hX:When the kernel event core layer notifies the user space, because multiple events might share the same ring buffer for recording samples, the core layer iterates every event associated with the ring buffer and wakes up tasks waiting on the event. This is fulfilled by the kernel function ``ring_buffer_wakeup()``.”h]”(hX!When the kernel event core layer notifies the user space, because multiple events might share the same ring buffer for recording samples, the core layer iterates every event associated with the ring buffer and wakes up tasks waiting on the event. This is fulfilled by the kernel function ”…”�”}”(hjâh²hh³Nh´Nubj‡)�”}”(hŒ``ring_buffer_wakeup()``”h]”hŒring_buffer_wakeup()”…”�”}”(hjêh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjâubhŒ.”…”�”}”(hjâh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M§hj•h²hubhý)�”}”(hXÑAfter the perf process is woken up, it starts to check the ring buffers one by one, if it finds any ring buffer containing samples it will read out the samples for statistics or saving into the data file. Given the perf process is able to run on any CPU, this leads to the ring buffer potentially being accessed from multiple CPUs simultaneously, which causes race conditions. The race condition handling is described in the section :ref:`memory_synchronization`.”h]”(hX³After the perf process is woken up, it starts to check the ring buffers one by one, if it finds any ring buffer containing samples it will read out the samples for statistics or saving into the data file. Given the perf process is able to run on any CPU, this leads to the ring buffer potentially being accessed from multiple CPUs simultaneously, which causes race conditions. The race condition handling is described in the section ”…”�”}”(hjh²hh³Nh´Nubh)�”}”(hŒ:ref:`memory_synchronization`”h]”jÃ)�”}”(hj h]”hŒmemory_synchronization”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”(jÎŒstd”Œstd-ref”eh"]”h$]”h&]”uh1jÂhj ubah}”(h]”h ]”h"]”h$]”h&]”Œrefdoc”jÛŒ refdomain”jŒreftype”Œref”Œ refexplicit”‰Œrefwarn”ˆjáŒmemory_synchronization”uh1hh³hÇh´M­hjubhŒ.”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M­hj•h²hubeh}”(h]”Œproducer-consumer-model”ah ]”h"]”Œ2.3.1 producer-consumer model”ah$]”h&]”uh1hÈhjvh²hh³hÇh´M…ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ$2.3.2 Properties of the ring buffers”h]”hŒ$2.3.2 Properties of the ring buffers”…”�”}”(hj?h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj<h²hh³hÇh´M¶ubhý)�”}”(hX.Linux kernel supports two write directions for the ring buffer: forward and backward. The forward writing saves samples from the beginning of the ring buffer, the backward writing stores data from the end of the ring buffer with the reversed direction. The perf tool determines the writing direction.”h]”hX.Linux kernel supports two write directions for the ring buffer: forward and backward. The forward writing saves samples from the beginning of the ring buffer, the backward writing stores data from the end of the ring buffer with the reversed direction. The perf tool determines the writing direction.”…”�”}”(hjMh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M¸hj<h²hubhý)�”}”(hŒeAdditionally, the tool can map buffers in either read-write mode or read-only mode to the user space.”h]”hŒeAdditionally, the tool can map buffers in either read-write mode or read-only mode to the user space.”…”�”}”(hj[h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M½hj<h²hubhý)�”}”(hXhThe ring buffer in the read-write mode is mapped with the property ``PROT_READ | PROT_WRITE``. With the write permission, the perf tool updates the ``data_tail`` to indicate the data start position. Combining with the head pointer ``data_head``, which works as the end position of the current data, the perf tool can easily know where read out the data from.”h]”(hŒCThe ring buffer in the read-write mode is mapped with the property ”…”�”}”(hjih²hh³Nh´Nubj‡)�”}”(hŒ``PROT_READ | PROT_WRITE``”h]”hŒPROT_READ | PROT_WRITE”…”�”}”(hjqh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjiubhŒ8. With the write permission, the perf tool updates the ”…”�”}”(hjih²hh³Nh´Nubj‡)�”}”(hŒ ``data_tail``”h]”hŒ data_tail”…”�”}”(hjƒh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjiubhŒG to indicate the data start position. Combining with the head pointer ”…”�”}”(hjih²hh³Nh´Nubj‡)�”}”(hŒ ``data_head``”h]”hŒ data_head”…”�”}”(hj•h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjiubhŒr, which works as the end position of the current data, the perf tool can easily know where read out the data from.”…”�”}”(hjih²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MÀhj<h²hubhý)�”}”(hŒ·Alternatively, in the read-only mode, only the kernel keeps to update the ``data_head`` while the user space cannot access the ``data_tail`` due to the mapping property ``PROT_READ``.”h]”(hŒJAlternatively, in the read-only mode, only the kernel keeps to update the ”…”�”}”(hj­h²hh³Nh´Nubj‡)�”}”(hŒ ``data_head``”h]”hŒ data_head”…”�”}”(hjµh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj­ubhŒ( while the user space cannot access the ”…”�”}”(hj­h²hh³Nh´Nubj‡)�”}”(hŒ ``data_tail``”h]”hŒ data_tail”…”�”}”(hjÇh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj­ubhŒ due to the mapping property ”…”�”}”(hj­h²hh³Nh´Nubj‡)�”}”(hŒ ``PROT_READ``”h]”hŒ PROT_READ”…”�”}”(hjÙh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj­ubhŒ.”…”�”}”(hj­h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MÇhj<h²hubhý)�”}”(hŒìAs a result, the matrix below illustrates the various combinations of direction and mapping characteristics. The perf tool employs two of these combinations to support buffer types: the non-overwrite buffer and the overwritable buffer.”h]”hŒìAs a result, the matrix below illustrates the various combinations of direction and mapping characteristics. The perf tool employs two of these combinations to support buffer types: the non-overwrite buffer and the overwritable buffer.”…”�”}”(hjñh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MËhj<h²hubhŒtable”“”)�”}”(hhh]”hŒtgroup”“”)�”}”(hhh]”(hŒcolspec”“”)�”}”(hhh]”h}”(h]”h ]”h"]”h$]”h&]”Œcolwidth”Kuh1j hjubj )�”}”(hhh]”h}”(h]”h ]”h"]”h$]”h&]”jKuh1j hjubj )�”}”(hhh]”h}”(h]”h ]”h"]”h$]”h&]”jKuh1j hjubhŒthead”“”)�”}”(hhh]”hŒrow”“”)�”}”(hhh]”(hŒentry”“”)�”}”(hhh]”hý)�”}”(hŒ Mapping mode”h]”hŒ Mapping mode”…”�”}”(hj6h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MÔhj3ubah}”(h]”h ]”h"]”h$]”h&]”uh1j1hj.ubj2)�”}”(hhh]”hý)�”}”(hŒForward”h]”hŒForward”…”�”}”(hjMh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MÕhjJubah}”(h]”h ]”h"]”h$]”h&]”uh1j1hj.ubj2)�”}”(hhh]”hý)�”}”(hŒBackward”h]”hŒBackward”…”�”}”(hjdh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MÖhjaubah}”(h]”h ]”h"]”h$]”h&]”uh1j1hj.ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j,hj)ubah}”(h]”h ]”h"]”h$]”h&]”uh1j'hjubhŒtbody”“”)�”}”(hhh]”(j-)�”}”(hhh]”(j2)�”}”(hhh]”hý)�”}”(hŒ read-write”h]”hŒ read-write”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M×hjŒubah}”(h]”h ]”h"]”h$]”h&]”uh1j1hj‰ubj2)�”}”(hhh]”hý)�”}”(hŒNon-overwrite ring buffer”h]”hŒNon-overwrite ring buffer”…”�”}”(hj¦h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MØhj£ubah}”(h]”h ]”h"]”h$]”h&]”uh1j1hj‰ubj2)�”}”(hhh]”hý)�”}”(hŒNot used”h]”hŒNot used”…”�”}”(hj½h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MÙhjºubah}”(h]”h ]”h"]”h$]”h&]”uh1j1hj‰ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j,hj†ubj-)�”}”(hhh]”(j2)�”}”(hhh]”hý)�”}”(hŒ read-only”h]”hŒ read-only”…”�”}”(hjÝh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MÚhjÚubah}”(h]”h ]”h"]”h$]”h&]”uh1j1hj×ubj2)�”}”(hhh]”hý)�”}”(hŒNot used”h]”hŒNot used”…”�”}”(hjôh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MÛhjñubah}”(h]”h ]”h"]”h$]”h&]”uh1j1hj×ubj2)�”}”(hhh]”hý)�”}”(hŒOverwritable ring buffer”h]”hŒOverwritable ring buffer”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MÜhjubah}”(h]”h ]”h"]”h$]”h&]”uh1j1hj×ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j,hj†ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j„hjubeh}”(h]”h ]”h"]”h$]”h&]”Œcols”Kuh1jhjubah}”(h]”h ]”Œcolwidths-given”ah"]”h$]”h&]”uh1jÿhj<h²hh³Nh´Nubhý)�”}”(hXÑThe non-overwrite ring buffer uses the read-write mapping with forward writing. It starts to save data from the beginning of the ring buffer and wrap around when overflow, which is used with the read-write mode in the normal ring buffer. When the consumer doesn't keep up with the producer, it would lose some data, the kernel keeps how many records it lost and generates the ``PERF_RECORD_LOST`` records in the next time when it finds a space in the ring buffer.”h]”(hX|The non-overwrite ring buffer uses the read-write mapping with forward writing. It starts to save data from the beginning of the ring buffer and wrap around when overflow, which is used with the read-write mode in the normal ring buffer. When the consumer doesn’t keep up with the producer, it would lose some data, the kernel keeps how many records it lost and generates the ”…”�”}”(hj9h²hh³Nh´Nubj‡)�”}”(hŒ``PERF_RECORD_LOST``”h]”hŒPERF_RECORD_LOST”…”�”}”(hjAh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj9ubhŒC records in the next time when it finds a space in the ring buffer.”…”�”}”(hj9h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MÞhj<h²hubhý)�”}”(hX|The overwritable ring buffer uses the backward writing with the read-only mode. It saves the data from the end of the ring buffer and the ``data_head`` keeps the position of current data, the perf always knows where it starts to read and until the end of the ring buffer, thus it don't need the ``data_tail``. In this mode, it will not generate the ``PERF_RECORD_LOST`` records.”h]”(hŒ‹The overwritable ring buffer uses the backward writing with the read-only mode. It saves the data from the end of the ring buffer and the ”…”�”}”(hjYh²hh³Nh´Nubj‡)�”}”(hŒ ``data_head``”h]”hŒ data_head”…”�”}”(hjah²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjYubhŒ’ keeps the position of current data, the perf always knows where it starts to read and until the end of the ring buffer, thus it don’t need the ”…”�”}”(hjYh²hh³Nh´Nubj‡)�”}”(hŒ ``data_tail``”h]”hŒ data_tail”…”�”}”(hjsh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjYubhŒ*. In this mode, it will not generate the ”…”�”}”(hjYh²hh³Nh´Nubj‡)�”}”(hŒ``PERF_RECORD_LOST``”h]”hŒPERF_RECORD_LOST”…”�”}”(hj…h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjYubhŒ records.”…”�”}”(hjYh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mæhj<h²hubhŒtarget”“”)�”}”(hŒ .. _writing_samples_into_buffer:”h]”h}”(h]”h ]”h"]”h$]”h&]”Œrefid”Œwriting-samples-into-buffer”uh1j�h´Míhj<h²hh³hÇubeh}”(h]”Œproperties-of-the-ring-buffers”ah ]”h"]”Œ$2.3.2 properties of the ring buffers”ah$]”h&]”uh1hÈhjvh²hh³hÇh´M¶ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ!2.3.3 Writing samples into buffer”h]”hŒ!2.3.3 Writing samples into buffer”…”�”}”(hj¶h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj³h²hh³hÇh´Mðubhý)�”}”(hXvWhen a sample is taken and saved into the ring buffer, the kernel prepares sample fields based on the sample type; then it prepares the info for writing ring buffer which is stored in the structure ``perf_output_handle``. In the end, the kernel outputs the sample into the ring buffer and updates the head pointer in the user page so the perf tool can see the latest value.”h]”(hŒÆWhen a sample is taken and saved into the ring buffer, the kernel prepares sample fields based on the sample type; then it prepares the info for writing ring buffer which is stored in the structure ”…”�”}”(hjÄh²hh³Nh´Nubj‡)�”}”(hŒ``perf_output_handle``”h]”hŒperf_output_handle”…”�”}”(hjÌh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjÄubhŒš. In the end, the kernel outputs the sample into the ring buffer and updates the head pointer in the user page so the perf tool can see the latest value.”…”�”}”(hjÄh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mòhj³h²hubhý)�”}”(hXThe structure ``perf_output_handle`` serves as a temporary context for tracking the information related to the buffer. The advantages of it is that it enables concurrent writing to the buffer by different events. For example, a software event and a hardware PMU event both are enabled for profiling, two instances of ``perf_output_handle`` serve as separate contexts for the software event and the hardware event respectively. This allows each event to reserve its own memory space for populating the record data.”h]”(hŒThe structure ”…”�”}”(hjäh²hh³Nh´Nubj‡)�”}”(hŒ``perf_output_handle``”h]”hŒperf_output_handle”…”�”}”(hjìh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjäubhX serves as a temporary context for tracking the information related to the buffer. The advantages of it is that it enables concurrent writing to the buffer by different events. For example, a software event and a hardware PMU event both are enabled for profiling, two instances of ”…”�”}”(hjäh²hh³Nh´Nubj‡)�”}”(hŒ``perf_output_handle``”h]”hŒperf_output_handle”…”�”}”(hjþh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjäubhŒ® serve as separate contexts for the software event and the hardware event respectively. This allows each event to reserve its own memory space for populating the record data.”…”�”}”(hjäh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mùhj³h²hubeh}”(h]”(jªŒid1”eh ]”h"]”(Œ!2.3.3 writing samples into buffer”Œwriting_samples_into_buffer”eh$]”h&]”uh1hÈhjvh²hh³hÇh´MðŒexpect_referenced_by_name”}”j jŸsŒexpect_referenced_by_id”}”jªjŸsubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ!2.3.4 Reading samples from buffer”h]”hŒ!2.3.4 Reading samples from buffer”…”�”}”(hj& h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj# h²hh³hÇh´Mubhý)�”}”(hX�In the user space, the perf tool utilizes the ``perf_event_mmap_page`` structure to handle the head and tail of the buffer. It also uses ``perf_mmap`` structure to keep track of a context for the ring buffer, this context includes information about the buffer's starting and ending addresses. Additionally, the mask value can be utilized to compute the circular buffer pointer even for an overflow.”h]”(hŒ.In the user space, the perf tool utilizes the ”…”�”}”(hj4 h²hh³Nh´Nubj‡)�”}”(hŒ``perf_event_mmap_page``”h]”hŒperf_event_mmap_page”…”�”}”(hj< h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj4 ubhŒD structure to handle the head and tail of the buffer. It also uses ”…”�”}”(hj4 h²hh³Nh´Nubj‡)�”}”(hŒ ``perf_mmap``”h]”hŒ perf_mmap”…”�”}”(hjN h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj4 ubhŒû structure to keep track of a context for the ring buffer, this context includes information about the buffer’s starting and ending addresses. Additionally, the mask value can be utilized to compute the circular buffer pointer even for an overflow.”…”�”}”(hj4 h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mhj# h²hubhý)�”}”(hŒ¾Similar to the kernel, the perf tool in the user space first reads out the recorded data from the ring buffer, and then updates the buffer's tail pointer ``perf_event_mmap_page::data_tail``.”h]”(hŒœSimilar to the kernel, the perf tool in the user space first reads out the recorded data from the ring buffer, and then updates the buffer’s tail pointer ”…”�”}”(hjf h²hh³Nh´Nubj‡)�”}”(hŒ#``perf_event_mmap_page::data_tail``”h]”hŒperf_event_mmap_page::data_tail”…”�”}”(hjn h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjf ubhŒ.”…”�”}”(hjf h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M hj# h²hubjž)�”}”(hŒ.. _memory_synchronization:”h]”h}”(h]”h ]”h"]”h$]”h&]”j©Œmemory-synchronization”uh1j�h´Mhj# h²hh³hÇubeh}”(h]”Œreading-samples-from-buffer”ah ]”h"]”Œ!2.3.4 reading samples from buffer”ah$]”h&]”uh1hÈhjvh²hh³hÇh´MubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ2.3.5 Memory synchronization”h]”hŒ2.3.5 Memory synchronization”…”�”}”(hjœ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj™ h²hh³hÇh´Mubhý)�”}”(hXwThe modern CPUs with relaxed memory model cannot promise the memory ordering, this means it’s possible to access the ring buffer and the ``perf_event_mmap_page`` structure out of order. To assure the specific sequence for memory accessing perf ring buffer, memory barriers are used to assure the data dependency. The rationale for the memory synchronization is as below::”h]”(hŒ‹The modern CPUs with relaxed memory model cannot promise the memory ordering, this means it’s possible to access the ring buffer and the ”…”�”}”(hjª h²hh³Nh´Nubj‡)�”}”(hŒ``perf_event_mmap_page``”h]”hŒperf_event_mmap_page”…”�”}”(hj² h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjª ubhŒÓ structure out of order. To assure the specific sequence for memory accessing perf ring buffer, memory barriers are used to assure the data dependency. The rationale for the memory synchronization is as below:”…”�”}”(hjª h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mhj™ h²hubja)�”}”(hX%Kernel User space if (LOAD ->data_tail) { LOAD ->data_head (A) smp_rmb() (C) STORE $data LOAD $data smp_wmb() (B) smp_mb() (D) STORE ->data_head STORE ->data_tail }”h]”hX%Kernel User space if (LOAD ->data_tail) { LOAD ->data_head (A) smp_rmb() (C) STORE $data LOAD $data smp_wmb() (B) smp_mb() (D) STORE ->data_head STORE ->data_tail }”…”�”}”hjÊ sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´Mhj™ h²hubhý)�”}”(hŒ¦The comments in tools/include/linux/ring_buffer.h gives nice description for why and how to use memory barriers, here we will just provide an alternative explanation:”h]”hŒ¦The comments in tools/include/linux/ring_buffer.h gives nice description for why and how to use memory barriers, here we will just provide an alternative explanation:”…”�”}”(hjØ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M%hj™ h²hubhý)�”}”(hŒ—(A) is a control dependency so that CPU assures order between checking pointer ``perf_event_mmap_page::data_tail`` and filling sample into ring buffer;”h]”(hŒO(A) is a control dependency so that CPU assures order between checking pointer ”…”�”}”(hjæ h²hh³Nh´Nubj‡)�”}”(hŒ#``perf_event_mmap_page::data_tail``”h]”hŒperf_event_mmap_page::data_tail”…”�”}”(hjî h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjæ ubhŒ% and filling sample into ring buffer;”…”�”}”(hjæ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M)hj™ h²hubhý)�”}”(hX+(D) pairs with (A). (D) separates the ring buffer data reading from writing the pointer ``data_tail``, perf tool first consumes samples and then tells the kernel that the data chunk has been released. Since a reading operation is followed by a writing operation, thus (D) is a full memory barrier.”h]”(hŒY(D) pairs with (A). (D) separates the ring buffer data reading from writing the pointer ”…”�”}”(hj h²hh³Nh´Nubj‡)�”}”(hŒ ``data_tail``”h]”hŒ data_tail”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj ubhŒÅ, perf tool first consumes samples and then tells the kernel that the data chunk has been released. Since a reading operation is followed by a writing operation, thus (D) is a full memory barrier.”…”�”•¢}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M-hj™ h²hubhý)�”}”(hŒ–(B) is a writing barrier in the middle of two writing operations, which makes sure that recording a sample must be prior to updating the head pointer.”h]”hŒ–(B) is a writing barrier in the middle of two writing operations, which makes sure that recording a sample must be prior to updating the head pointer.”…”�”}”(hj& h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M3hj™ h²hubhý)�”}”(hŒo(C) pairs with (B). (C) is a read memory barrier to ensure the head pointer is fetched before reading samples.”h]”hŒo(C) pairs with (B). (C) is a read memory barrier to ensure the head pointer is fetched before reading samples.”…”�”}”(hj4 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M7hj™ h²hubhý)�”}”(hXTo implement the above algorithm, the ``perf_output_put_handle()`` function in the kernel and two helpers ``ring_buffer_read_head()`` and ``ring_buffer_write_tail()`` in the user space are introduced, they rely on memory barriers as described above to ensure the data dependency.”h]”(hŒ&To implement the above algorithm, the ”…”�”}”(hjB h²hh³Nh´Nubj‡)�”}”(hŒ``perf_output_put_handle()``”h]”hŒperf_output_put_handle()”…”�”}”(hjJ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjB ubhŒ( function in the kernel and two helpers ”…”�”}”(hjB h²hh³Nh´Nubj‡)�”}”(hŒ``ring_buffer_read_head()``”h]”hŒring_buffer_read_head()”…”�”}”(hj\ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjB ubhŒ and ”…”�”}”(hjB h²hh³Nh´Nubj‡)�”}”(hŒ``ring_buffer_write_tail()``”h]”hŒring_buffer_write_tail()”…”�”}”(hjn h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjB ubhŒq in the user space are introduced, they rely on memory barriers as described above to ensure the data dependency.”…”�”}”(hjB h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M:hj™ h²hubhý)�”}”(hXSome architectures support one-way permeable barrier with load-acquire and store-release operations, these barriers are more relaxed with less performance penalty, so (C) and (D) can be optimized to use barriers ``smp_load_acquire()`` and ``smp_store_release()`` respectively.”h]”(hŒÔSome architectures support one-way permeable barrier with load-acquire and store-release operations, these barriers are more relaxed with less performance penalty, so (C) and (D) can be optimized to use barriers ”…”�”}”(hj† h²hh³Nh´Nubj‡)�”}”(hŒ``smp_load_acquire()``”h]”hŒsmp_load_acquire()”…”�”}”(hjŽ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj† ubhŒ and ”…”�”}”(hj† h²hh³Nh´Nubj‡)�”}”(hŒ``smp_store_release()``”h]”hŒsmp_store_release()”…”�”}”(hj  h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj† ubhŒ respectively.”…”�”}”(hj† h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M?hj™ h²hubhý)�”}”(hX�If an architecture doesn’t support load-acquire and store-release in its memory model, it will roll back to the old fashion of memory barrier operations. In this case, ``smp_load_acquire()`` encapsulates ``READ_ONCE()`` + ``smp_mb()``, since ``smp_mb()`` is costly, ``ring_buffer_read_head()`` doesn't invoke ``smp_load_acquire()`` and it uses the barriers ``READ_ONCE()`` + ``smp_rmb()`` instead.”h]”(hŒ«If an architecture doesn’t support load-acquire and store-release in its memory model, it will roll back to the old fashion of memory barrier operations. In this case, ”…”�”}”(hj¸ h²hh³Nh´Nubj‡)�”}”(hŒ``smp_load_acquire()``”h]”hŒsmp_load_acquire()”…”�”}”(hjÀ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj¸ ubhŒ encapsulates ”…”�”}”(hj¸ h²hh³Nh´Nubj‡)�”}”(hŒ``READ_ONCE()``”h]”hŒ READ_ONCE()”…”�”}”(hjÒ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj¸ ubhŒ + ”…”�”}”(hj¸ h²hh³Nh´Nubj‡)�”}”(hŒ ``smp_mb()``”h]”hŒsmp_mb()”…”�”}”(hjä h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj¸ ubhŒ, since ”…”�”}”(hj¸ h²hh³Nh´Nubj‡)�”}”(hŒ ``smp_mb()``”h]”hŒsmp_mb()”…”�”}”(hjö h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj¸ ubhŒ is costly, ”…”�”}”(hj¸ h²hh³Nh´Nubj‡)�”}”(hŒ``ring_buffer_read_head()``”h]”hŒring_buffer_read_head()”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj¸ ubhŒ doesn’t invoke ”…”�”}”(hj¸ h²hh³Nh´Nubj‡)�”}”(hŒ``smp_load_acquire()``”h]”hŒsmp_load_acquire()”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj¸ ubhŒ and it uses the barriers ”…”�”}”(hj¸ h²hh³Nh´Nubj‡)�”}”(hŒ``READ_ONCE()``”h]”hŒ READ_ONCE()”…”�”}”(hj, h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj¸ ubhŒ + ”…”�”}”hj¸ sbj‡)�”}”(hŒ ``smp_rmb()``”h]”hŒ smp_rmb()”…”�”}”(hj> h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj¸ ubhŒ instead.”…”�”}”(hj¸ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MDhj™ h²hubeh}”(h]”(j� Œid2”eh ]”h"]”(Œ2.3.5 memory synchronization”Œmemory_synchronization”eh$]”h&]”uh1hÈhjvh²hh³hÇh´Mj }”j\ j† sj! }”j� j† subeh}”(h]”Œaccessing-buffer”ah ]”h"]”Œ2.3 accessing buffer”ah$]”h&]”uh1hÈhj0h²hh³hÇh´Mubeh}”(h]”Œring-buffer-implementation”ah ]”h"]”Œ2. ring buffer implementation”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´K4ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ#3. The mechanism of AUX ring buffer”h]”hŒ#3. The mechanism of AUX ring buffer”…”�”}”(hjt h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjq h²hh³hÇh´MLubhý)�”}”(hXxIn this chapter, we will explain the implementation of the AUX ring buffer. In the first part it will discuss the connection between the AUX ring buffer and the regular ring buffer, then the second part will examine how the AUX ring buffer co-works with the regular ring buffer, as well as the additional features introduced by the AUX ring buffer for the sampling mechanism.”h]”hXxIn this chapter, we will explain the implementation of the AUX ring buffer. In the first part it will discuss the connection between the AUX ring buffer and the regular ring buffer, then the second part will examine how the AUX ring buffer co-works with the regular ring buffer, as well as the additional features introduced by the AUX ring buffer for the sampling mechanism.”…”�”}”(hj‚ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MNhjq h²hubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ93.1 The relationship between AUX and regular ring buffers”h]”hŒ93.1 The relationship between AUX and regular ring buffers”…”�”}”(hj“ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj� h²hh³hÇh´MVubhý)�”}”(hXSGenerally, the AUX ring buffer is an auxiliary for the regular ring buffer. The regular ring buffer is primarily used to store the event samples and every event format complies with the definition in the union ``perf_event``; the AUX ring buffer is for recording the hardware trace data and the trace data format is hardware IP dependent.”h]”(hŒÓGenerally, the AUX ring buffer is an auxiliary for the regular ring buffer. The regular ring buffer is primarily used to store the event samples and every event format complies with the definition in the union ”…”�”}”(hj¡ h²hh³Nh´Nubj‡)�”}”(hŒ``perf_event``”h]”hŒ perf_event”…”�”}”(hj© h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj¡ ubhŒr; the AUX ring buffer is for recording the hardware trace data and the trace data format is hardware IP dependent.”…”�”}”(hj¡ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MXhj� h²hubhý)�”}”(hXáThe general use and advantage of the AUX ring buffer is that it is written directly by hardware rather than by the kernel. For example, regular profile samples that write to the regular ring buffer cause an interrupt. Tracing execution requires a high number of samples and using interrupts would be overwhelming for the regular ring buffer mechanism. Having an AUX buffer allows for a region of memory more decoupled from the kernel and written to directly by hardware tracing.”h]”hXáThe general use and advantage of the AUX ring buffer is that it is written directly by hardware rather than by the kernel. For example, regular profile samples that write to the regular ring buffer cause an interrupt. Tracing execution requires a high number of samples and using interrupts would be overwhelming for the regular ring buffer mechanism. Having an AUX buffer allows for a region of memory more decoupled from the kernel and written to directly by hardware tracing.”…”�”}”(hjÁ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M^hj� h²hubhý)�”}”(hXThe AUX ring buffer reuses the same algorithm with the regular ring buffer for the buffer management. The control structure ``perf_event_mmap_page`` extends the new fields ``aux_head`` and ``aux_tail`` for the head and tail pointers of the AUX ring buffer.”h]”(hŒ}The AUX ring buffer reuses the same algorithm with the regular ring buffer for the buffer management. The control structure ”…”�”}”(hjÏ h²hh³Nh´Nubj‡)�”}”(hŒ``perf_event_mmap_page``”h]”hŒperf_event_mmap_page”…”�”}”(hj× h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjÏ ubhŒ extends the new fields ”…”�”}”(hjÏ h²hh³Nh´Nubj‡)�”}”(hŒ ``aux_head``”h]”hŒaux_head”…”�”}”(hjé h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjÏ ubhŒ and ”…”�”}”(hjÏ h²hh³Nh´Nubj‡)�”}”(hŒ ``aux_tail``”h]”hŒaux_tail”…”�”}”(hjû h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjÏ ubhŒ7 for the head and tail pointers of the AUX ring buffer.”…”�”}”(hjÏ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mfhj� h²hubhý)�”}”(hX¢During the initialisation phase, besides the mmap()-ed regular ring buffer, the perf tool invokes a second syscall in the ``auxtrace_mmap__mmap()`` function for the mmap of the AUX buffer with non-zero file offset; ``rb_alloc_aux()`` in the kernel allocates pages correspondingly, these pages will be deferred to map into VMA when handling the page fault, which is the same lazy mechanism with the regular ring buffer.”h]”(hŒzDuring the initialisation phase, besides the mmap()-ed regular ring buffer, the perf tool invokes a second syscall in the ”…”�”}”(hj h²hh³Nh´Nubj‡)�”}”(hŒ``auxtrace_mmap__mmap()``”h]”hŒauxtrace_mmap__mmap()”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj ubhŒD function for the mmap of the AUX buffer with non-zero file offset; ”…”�”}”(hj h²hh³Nh´Nubj‡)�”}”(hŒ``rb_alloc_aux()``”h]”hŒrb_alloc_aux()”…”�”}”(hj- h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj ubhŒ¹ in the kernel allocates pages correspondingly, these pages will be deferred to map into VMA when handling the page fault, which is the same lazy mechanism with the regular ring buffer.”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mkhj� h²hubhý)�”}”(hŒOAUX events and AUX trace data are two different things. Let's see an example::”h]”hŒPAUX events and AUX trace data are two different things. Let’s see an example:”…”�”}”(hjE h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mshj� h²hubja)�”}”(hŒ/perf record -a -e cycles -e cs_etm// -- sleep 2”h]”hŒ/perf record -a -e cycles -e cs_etm// -- sleep 2”…”�”}”hjS sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´Mvhj� h²hubhý)�”}”(hŒõThe above command enables two events: one is the event *cycles* from PMU and another is the AUX event *cs_etm* from Arm CoreSight, both are saved into the regular ring buffer while the CoreSight's AUX trace data is stored in the AUX ring buffer.”h]”(hŒ7The above command enables two events: one is the event ”…”�”}”(hja h²hh³Nh´NubjÜ)�”}”(hŒ*cycles*”h]”hŒcycles”…”�”}”(hji h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jÛhja ubhŒ' from PMU and another is the AUX event ”…”�”}”(hja h²hh³Nh´NubjÜ)�”}”(hŒ*cs_etm*”h]”hŒcs_etm”…”�”}”(hj{ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jÛhja ubhŒ‰ from Arm CoreSight, both are saved into the regular ring buffer while the CoreSight’s AUX trace data is stored in the AUX ring buffer.”…”�”}”(hja h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mxhj� h²hubhý)�”}”(hXfAs a result, we can see the regular ring buffer and the AUX ring buffer are allocated in pairs. The perf in default mode allocates the regular ring buffer and the AUX ring buffer per CPU-wise, which is the same as the system wide mode, however, the default mode records samples only for the profiled program, whereas the latter mode profiles for all programs in the system. For per-thread mode, the perf tool allocates only one regular ring buffer and one AUX ring buffer for the whole session. For the per-CPU mode, the perf allocates two kinds of ring buffers for selected CPUs specified by the option ``-C``.”h]”(hX_As a result, we can see the regular ring buffer and the AUX ring buffer are allocated in pairs. The perf in default mode allocates the regular ring buffer and the AUX ring buffer per CPU-wise, which is the same as the system wide mode, however, the default mode records samples only for the profiled program, whereas the latter mode profiles for all programs in the system. For per-thread mode, the perf tool allocates only one regular ring buffer and one AUX ring buffer for the whole session. For the per-CPU mode, the perf allocates two kinds of ring buffers for selected CPUs specified by the option ”…”�”}”(hj“ h²hh³Nh´Nubj‡)�”}”(hŒ``-C``”h]”hŒ-C”…”�”}”(hj› h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj“ ubhŒ.”…”�”}”(hj“ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M}hj� h²hubhý)�”}”(hŒÝThe below figure demonstrates the buffers' layout in the system wide mode; if there are any activities on one CPU, the AUX event samples and the hardware trace data will be recorded into the dedicated buffers for the CPU.”h]”hŒßThe below figure demonstrates the buffers’ layout in the system wide mode; if there are any activities on one CPU, the AUX event samples and the hardware trace data will be recorded into the dedicated buffers for the CPU.”…”�”}”(hj³ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M‡hj� h²hubja)�”}”(hX T1 T2 T1 +----+ +-----------+ +----+ CPU0 |xxxx| |xxxxxxxxxxx| |xxxx| +----+--------------+-----------+----------+----+--------> | | | v v v +-----------------------------------------------------+ | Ring buffer 0 | +-----------------------------------------------------+ | | | v v v +-----------------------------------------------------+ | AUX Ring buffer 0 | +-----------------------------------------------------+ T1 +-----+ CPU1 |xxxxx| -----+-----+---------------------------------------------> | v +-----------------------------------------------------+ | Ring buffer 1 | +-----------------------------------------------------+ | v +-----------------------------------------------------+ | AUX Ring buffer 1 | +-----------------------------------------------------+ T1 T3 +----+ +-------+ CPU2 |xxxx| |xxxxxxx| --------------------------+----+--------+-------+--------> | | v v +-----------------------------------------------------+ | Ring buffer 2 | +-----------------------------------------------------+ | | v v +-----------------------------------------------------+ | AUX Ring buffer 2 | +-----------------------------------------------------+ T1 +--------------+ CPU3 |xxxxxxxxxxxxxx| -----------+--------------+------------------------------> | v +-----------------------------------------------------+ | Ring buffer 3 | +-----------------------------------------------------+ | v +-----------------------------------------------------+ | AUX Ring buffer 3 | +-----------------------------------------------------+ T1: Thread 1; T2: Thread 2; T3: Thread 3 x: Thread is in running state Figure 8. AUX ring buffer for system wide mode”h]”hX T1 T2 T1 +----+ +-----------+ +----+ CPU0 |xxxx| |xxxxxxxxxxx| |xxxx| +----+--------------+-----------+----------+----+--------> | | | v v v +-----------------------------------------------------+ | Ring buffer 0 | +-----------------------------------------------------+ | | | v v v +-----------------------------------------------------+ | AUX Ring buffer 0 | +-----------------------------------------------------+ T1 +-----+ CPU1 |xxxxx| -----+-----+---------------------------------------------> | v +-----------------------------------------------------+ | Ring buffer 1 | +-----------------------------------------------------+ | v +-----------------------------------------------------+ | AUX Ring buffer 1 | +-----------------------------------------------------+ T1 T3 +----+ +-------+ CPU2 |xxxx| |xxxxxxx| --------------------------+----+--------+-------+--------> | | v v +-----------------------------------------------------+ | Ring buffer 2 | +-----------------------------------------------------+ | | v v +-----------------------------------------------------+ | AUX Ring buffer 2 | +-----------------------------------------------------+ T1 +--------------+ CPU3 |xxxxxxxxxxxxxx| -----------+--------------+------------------------------> | v +-----------------------------------------------------+ | Ring buffer 3 | +-----------------------------------------------------+ | v +-----------------------------------------------------+ | AUX Ring buffer 3 | +-----------------------------------------------------+ T1: Thread 1; T2: Thread 2; T3: Thread 3 x: Thread is in running state Figure 8. AUX ring buffer for system wide mode”…”�”}”hjÁ sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´MŽhj� h²hubeh}”(h]”Œ5the-relationship-between-aux-and-regular-ring-buffers”ah ]”h"]”Œ93.1 the relationship between aux and regular ring buffers”ah$]”h&]”uh1hÈhjq h²hh³hÇh´MVubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ3.2 AUX events”h]”hŒ3.2 AUX events”…”�”}”(hjÚ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj× h²hh³hÇh´MÐubhý)�”}”(hŒãSimilar to ``perf_output_begin()`` and ``perf_output_end()``'s working for the regular ring buffer, ``perf_aux_output_begin()`` and ``perf_aux_output_end()`` serve for the AUX ring buffer for processing the hardware trace data.”h]”(hŒ Similar to ”…”�”}”(hjè h²hh³Nh´Nubj‡)�”}”(hŒ``perf_output_begin()``”h]”hŒperf_output_begin()”…”�”}”(hjð h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjè ubhŒ and ”…”�”}”(hjè h²hh³Nh´Nubj‡)�”}”(hŒ``perf_output_end()``”h]”hŒperf_output_end()”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjè ubhŒ*’s working for the regular ring buffer, ”…”�”}”(hjè h²hh³Nh´Nubj‡)�”}”(hŒ``perf_aux_output_begin()``”h]”hŒperf_aux_output_begin()”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjè ubhŒ and ”…”�”}”hjè sbj‡)�”}”(hŒ``perf_aux_output_end()``”h]”hŒperf_aux_output_end()”…”�”}”(hj& h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjè ubhŒF serve for the AUX ring buffer for processing the hardware trace data.”…”�”}”(hjè h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MÒhj× h²hubhý)�”}”(hX Once the hardware trace data is stored into the AUX ring buffer, the PMU driver will stop hardware tracing by calling the ``pmu::stop()`` callback. Similar to the regular ring buffer, the AUX ring buffer needs to apply the memory synchronization mechanism as discussed in the section :ref:`memory_synchronization`. Since the AUX ring buffer is managed by the PMU driver, the barrier (B), which is a writing barrier to ensure the trace data is externally visible prior to updating the head pointer, is asked to be implemented in the PMU driver.”h]”(hŒzOnce the hardware trace data is stored into the AUX ring buffer, the PMU driver will stop hardware tracing by calling the ”…”�”}”(hj> h²hh³Nh´Nubj‡)�”}”(hŒ``pmu::stop()``”h]”hŒ pmu::stop()”…”�”}”(hjF h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj> ubhŒ“ callback. Similar to the regular ring buffer, the AUX ring buffer needs to apply the memory synchronization mechanism as discussed in the section ”…”�”}”(hj> h²hh³Nh´Nubh)�”}”(hŒ:ref:`memory_synchronization`”h]”jÃ)�”}”(hjZ h]”hŒmemory_synchronization”…”�”}”(hj\ h²hh³Nh´Nubah}”(h]”h ]”(jÎŒstd”Œstd-ref”eh"]”h$]”h&]”uh1jÂhjX ubah}”(h]”h ]”h"]”h$]”h&]”Œrefdoc”jÛŒ refdomain”jf Œreftype”Œref”Œ refexplicit”‰Œrefwarn”ˆjáŒmemory_synchronization”uh1hh³hÇh´MÖhj> ubhŒç. Since the AUX ring buffer is managed by the PMU driver, the barrier (B), which is a writing barrier to ensure the trace data is externally visible prior to updating the head pointer, is asked to be implemented in the PMU driver.”…”�”}”(hj> h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MÖhj× h²hubhý)�”}”(hŒaThen ``pmu::stop()`` can safely call the ``perf_aux_output_end()`` function to finish two things:”h]”(hŒThen ”…”�”}”(hj‚ h²hh³Nh´Nubj‡)�”}”(hŒ``pmu::stop()``”h]”hŒ pmu::stop()”…”�”}”(hjŠ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj‚ ubhŒ can safely call the ”…”�”}”(hj‚ h²hh³Nh´Nubj‡)�”}”(hŒ``perf_aux_output_end()``”h]”hŒperf_aux_output_end()”…”�”}”(hjœ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj‚ ubhŒ function to finish two things:”…”�”}”(hj‚ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mßhj× h²hubhŒ bullet_list”“”)�”}”(hhh]”(hŒ list_item”“”)�”}”(hŒØIt fills an event ``PERF_RECORD_AUX`` into the regular ring buffer, this event delivers the information of the start address and data size for a chunk of hardware trace data has been stored into the AUX ring buffer; ”h]”hý)�”}”(hŒ×It fills an event ``PERF_RECORD_AUX`` into the regular ring buffer, this event delivers the information of the start address and data size for a chunk of hardware trace data has been stored into the AUX ring buffer;”h]”(hŒIt fills an event ”…”�”}”(hj¿ h²hh³Nh´Nubj‡)�”}”(hŒ``PERF_RECORD_AUX``”h]”hŒPERF_RECORD_AUX”…”�”}”(hjÇ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj¿ ubhŒ² into the regular ring buffer, this event delivers the information of the start address and data size for a chunk of hardware trace data has been stored into the AUX ring buffer;”…”�”}”(hj¿ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mâhj» ubah}”(h]”h ]”h"]”h$]”h&]”uh1j¹ hj¶ h²hh³hÇh´Nubjº )�”}”(hX%Since the hardware trace driver has stored new trace data into the AUX ring buffer, the argument *size* indicates how many bytes have been consumed by the hardware tracing, thus ``perf_aux_output_end()`` updates the header pointer ``perf_buffer::aux_head`` to reflect the latest buffer usage. ”h]”hý)�”}”(hX$Since the hardware trace driver has stored new trace data into the AUX ring buffer, the argument *size* indicates how many bytes have been consumed by the hardware tracing, thus ``perf_aux_output_end()`` updates the header pointer ``perf_buffer::aux_head`` to reflect the latest buffer usage.”h]”(hŒaSince the hardware trace driver has stored new trace data into the AUX ring buffer, the argument ”…”�”}”(hjé h²hh³Nh´NubjÜ)�”}”(hŒ*size*”h]”hŒsize”…”�”}”(hjñ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jÛhjé ubhŒK indicates how many bytes have been consumed by the hardware tracing, thus ”…”�”}”(hjé h²hh³Nh´Nubj‡)�”}”(hŒ``perf_aux_output_end()``”h]”hŒperf_aux_output_end()”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjé ubhŒ updates the header pointer ”…”�”}”(hjé h²hh³Nh´Nubj‡)�”}”(hŒ``perf_buffer::aux_head``”h]”hŒperf_buffer::aux_head”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjé ubhŒ$ to reflect the latest buffer usage.”…”�”}”(hjé h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mæhjå ubah}”(h]”h ]”h"]”h$]”h&]”uh1j¹ hj¶ h²hh³hÇh´Nubeh}”(h]”h ]”h"]”h$]”h&]”Œbullet”Œ-”uh1j´ h³hÇh´Mâhj× h²hubhý)�”}”(hŒÁAt the end, the PMU driver will restart hardware tracing. During this temporary suspending period, it will lose hardware trace data, which will introduce a discontinuity during decoding phase.”h]”hŒÁAt the end, the PMU driver will restart hardware tracing. During this temporary suspending period, it will lose hardware trace data, which will introduce a discontinuity during decoding phase.”…”�”}”(hj;h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mëhj× h²hubhý)�”}”(hXThe event ``PERF_RECORD_AUX`` presents an AUX event which is handled in the kernel, but it lacks the information for saving the AUX trace data in the perf file. When the perf tool copies the trace data from AUX ring buffer to the perf data file, it synthesizes a ``PERF_RECORD_AUXTRACE`` event which is not a kernel ABI, it's defined by the perf tool to describe which portion of data in the AUX ring buffer is saved. Afterwards, the perf tool reads out the AUX trace data from the perf file based on the ``PERF_RECORD_AUXTRACE`` events, and the ``PERF_RECORD_AUX`` event is used to decode a chunk of data by correlating with time order.”h]”(hŒ The event ”…”�”}”(hjIh²hh³Nh´Nubj‡)�”}”(hŒ``PERF_RECORD_AUX``”h]”hŒPERF_RECORD_AUX”…”�”}”(hjQh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjIubhŒë presents an AUX event which is handled in the kernel, but it lacks the information for saving the AUX trace data in the perf file. When the perf tool copies the trace data from AUX ring buffer to the perf data file, it synthesizes a ”…”�”}”(hjIh²hh³Nh´Nubj‡)�”}”(hŒ``PERF_RECORD_AUXTRACE``”h]”hŒPERF_RECORD_AUXTRACE”…”�”}”(hjch²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjIubhŒÝ event which is not a kernel ABI, it’s defined by the perf tool to describe which portion of data in the AUX ring buffer is saved. Afterwards, the perf tool reads out the AUX trace data from the perf file based on the ”…”�”}”(hjIh²hh³Nh´Nubj‡)�”}”(hŒ``PERF_RECORD_AUXTRACE``”h]”hŒPERF_RECORD_AUXTRACE”…”�”}”(hjuh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjIubhŒ events, and the ”…”�”}”(hjIh²hh³Nh´Nubj‡)�”}”(hŒ``PERF_RECORD_AUX``”h]”hŒPERF_RECORD_AUX”…”�”}”(hj‡h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjIubhŒH event is used to decode a chunk of data by correlating with time order.”…”�”}”(hjIh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mïhj× h²hubeh}”(h]”Œ aux-events”ah ]”h"]”Œ3.2 aux events”ah$]”h&]”uh1hÈhjq h²hh³hÇh´MÐubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ3.3 Snapshot mode”h]”hŒ3.3 Snapshot mode”…”�”}”(hjªh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj§h²hh³hÇh´Múubhý)�”}”(hŒùPerf supports snapshot mode for AUX ring buffer, in this mode, users only record AUX trace data at a specific time point which users are interested in. E.g. below gives an example of how to take snapshots with 1 second interval with Arm CoreSight::”h]”hŒøPerf supports snapshot mode for AUX ring buffer, in this mode, users only record AUX trace data at a specific time point which users are interested in. E.g. below gives an example of how to take snapshots with 1 second interval with Arm CoreSight:”…”�”}”(hj¸h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mühj§h²hubja)�”}”(hŒkperf record -e cs_etm//u -S -a program & PERFPID=$! while true; do kill -USR2 $PERFPID sleep 1 done”h]”hŒkperf record -e cs_etm//u -S -a program & PERFPID=$! while true; do kill -USR2 $PERFPID sleep 1 done”…”�”}”hjÆsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´Mhj§h²hubhý)�”}”(hŒ#The main flow for snapshot mode is:”h]”hŒ#The main flow for snapshot mode is:”…”�”}”(hjÔh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mhj§h²hubjµ )�”}”(hhh]”(jº )�”}”(hŒšBefore a snapshot is taken, the AUX ring buffer acts in free run mode. During free run mode the perf doesn't record any of the AUX events and trace data; ”h]”hý)�”}”(hŒ™Before a snapshot is taken, the AUX ring buffer acts in free run mode. During free run mode the perf doesn't record any of the AUX events and trace data;”h]”hŒ›Before a snapshot is taken, the AUX ring buffer acts in free run mode. During free run mode the perf doesn’t record any of the AUX events and trace data;”…”�”}”(hjéh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M hjåubah}”(h]”h ]”h"]”h$]”h&]”uh1j¹ hjâh²hh³hÇh´Nubjº )�”}”(hX3Once the perf tool receives the *USR2* signal, it triggers the callback function ``auxtrace_record::snapshot_start()`` to deactivate hardware tracing. The kernel driver then populates the AUX ring buffer with the hardware trace data, and the event ``PERF_RECORD_AUX`` is stored in the regular ring buffer; ”h]”hý)�”}”(hX2Once the perf tool receives the *USR2* signal, it triggers the callback function ``auxtrace_record::snapshot_start()`` to deactivate hardware tracing. The kernel driver then populates the AUX ring buffer with the hardware trace data, and the event ``PERF_RECORD_AUX`` is stored in the regular ring buffer;”h]”(hŒ Once the perf tool receives the ”…”�”}”(hjh²hh³Nh´NubjÜ)�”}”(hŒ*USR2*”h]”hŒUSR2”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jÛhjubhŒ+ signal, it triggers the callback function ”…”�”}”(hjh²hh³Nh´Nubj‡)�”}”(hŒ%``auxtrace_record::snapshot_start()``”h]”hŒ!auxtrace_record::snapshot_start()”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjubhŒƒ to deactivate hardware tracing. The kernel driver then populates the AUX ring buffer with the hardware trace data, and the event ”…”�”}”(hjh²hh³Nh´Nubj‡)�”}”(hŒ``PERF_RECORD_AUX``”h]”hŒPERF_RECORD_AUX”…”�”}”(hj-h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjubhŒ& is stored in the regular ring buffer;”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mhjýubah}”(h]”h ]”h"]”h$]”h&]”uh1j¹ hjâh²hh³hÇh´Nubjº )�”}”(hŒ£Then perf tool takes a snapshot, ``record__read_auxtrace_snapshot()`` reads out the hardware trace data from the AUX ring buffer and saves it into perf data file; ”h]”hý)�”}”(hŒ¢Then perf tool takes a snapshot, ``record__read_auxtrace_snapshot()`` reads out the hardware trace data from the AUX ring buffer and saves it into perf data file;”h]”(hŒ!Then perf tool takes a snapshot, ”…”�”}”(hjOh²hh³Nh´Nubj‡)�”}”(hŒ$``record__read_auxtrace_snapshot()``”h]”hŒ record__read_auxtrace_snapshot()”…”�”}”(hjWh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjOubhŒ] reads out the hardware trace data from the AUX ring buffer and saves it into perf data file;”…”�”}”(hjOh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´MhjKubah}”(h]”h ]”h"]”h$]”h&]”uh1j¹ hjâh²hh³hÇh´Nubjº )�”}”(hŒoAfter the snapshot is finished, ``auxtrace_record::snapshot_finish()`` restarts the PMU event for AUX tracing. ”h]”hý)�”}”(hŒnAfter the snapshot is finished, ``auxtrace_record::snapshot_finish()`` restarts the PMU event for AUX tracing.”h]”(hŒ After the snapshot is finished, ”…”�”}”(hjyh²hh³Nh´Nubj‡)�”}”(hŒ&``auxtrace_record::snapshot_finish()``”h]”hŒ"auxtrace_record::snapshot_finish()”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hjyubhŒ( restarts the PMU event for AUX tracing.”…”�”}”(hjyh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mhjuubah}”(h]”h ]”h"]”h$]”h&]”uh1j¹ hjâh²hh³hÇh´Nubeh}”(h]”h ]”h"]”h$]”h&]”j9j:uh1j´ h³hÇh´M hj§h²hubhý)�”}”(hXüThe perf only accesses the head pointer ``perf_event_mmap_page::aux_head`` in snapshot mode and doesn’t touch tail pointer ``aux_tail``, this is because the AUX ring buffer can overflow in free run mode, the tail pointer is useless in this case. Alternatively, the callback ``auxtrace_record::find_snapshot()`` is introduced for making the decision of whether the AUX ring buffer has been wrapped around or not, at the end it fixes up the AUX buffer's head which are used to calculate the trace data size.”h]”(hŒ(The perf only accesses the head pointer ”…”�”}”(hj¥h²hh³Nh´Nubj‡)�”}”(hŒ"``perf_event_mmap_page::aux_head``”h]”hŒperf_event_mmap_page::aux_head”…”�”}”(hj­h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj¥ubhŒ3 in snapshot mode and doesn’t touch tail pointer ”…”�”}”(hj¥h²hh³Nh´Nubj‡)�”}”(hŒ ``aux_tail``”h]”hŒaux_tail”…”�”}”(hj¿h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj¥ubhŒŒ, this is because the AUX ring buffer can overflow in free run mode, the tail pointer is useless in this case. Alternatively, the callback ”…”�”}”(hj¥h²hh³Nh´Nubj‡)�”}”(hŒ$``auxtrace_record::find_snapshot()``”h]”hŒ auxtrace_record::find_snapshot()”…”�”}”(hjÑh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j†hj¥ubhŒÅ is introduced for making the decision of whether the AUX ring buffer has been wrapped around or not, at the end it fixes up the AUX buffer’s head which are used to calculate the trace data size.”…”�”}”(hj¥h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´Mhj§h²hubhý)�”}”(hŒÔAs we know, the buffers' deployment can be per-thread mode, per-CPU mode, or system wide mode, and the snapshot can be applied to any of these modes. Below is an example of taking snapshot with system wide mode.”h]”hŒÖAs we know, the buffers’ deployment can be per-thread mode, per-CPU mode, or system wide mode, and the snapshot can be applied to any of these modes. Below is an example of taking snapshot with system wide mode.”…”�”}”(hjéh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hüh³hÇh´M$hj§h²hubja)�”}”(hX» Snapshot is taken | v +------------------------+ | AUX Ring buffer 0 | <- aux_head +------------------------+ v +--------------------------------+ | AUX Ring buffer 1 | <- aux_head +--------------------------------+ v +--------------------------------------------+ | AUX Ring buffer 2 | <- aux_head +--------------------------------------------+ v +---------------------------------------+ | AUX Ring buffer 3 | <- aux_head +---------------------------------------+ Figure 9. Snapshot with system wide mode”h]”hX» Snapshot is taken | v +------------------------+ | AUX Ring buffer 0 | <- aux_head +------------------------+ v +--------------------------------+ | AUX Ring buffer 1 | <- aux_head +--------------------------------+ v +--------------------------------------------+ | AUX Ring buffer 2 | <- aux_head +--------------------------------------------+ v +---------------------------------------+ | AUX Ring buffer 3 | <- aux_head +---------------------------------------+ Figure 9. Snapshot with system wide mode”…”�”}”hj÷sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1j`h³hÇh´M+hj§h²hubeh}”(h]”Œ snapshot-mode”ah ]”h"]”Œ3.3 snapshot mode”ah$]”h&]”uh1hÈhjq h²hh³hÇh´Múubeh}”(h]”Œ the-mechanism-of-aux-ring-buffer”ah ]”h"]”Œ#3. the mechanism of aux ring buffer”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´MLubeh}”(h]”Œperf-ring-buffer”ah ]”h"]”Œperf ring buffer”ah$]”h&]”uh1hÈhhh²hh³hÇh´Kubeh}”(h]”h ]”h"]”h$]”h&]”Œsource”hÇuh1hŒcurrent_source”NŒ current_line”NŒsettings”Œdocutils.frontend”ŒValues”“”)�”}”(hÍNŒ generator”NŒ datestamp”NŒ source_link”NŒ source_url”NŒ toc_backlinks”j1Œfootnote_backlinks”KŒ sectnum_xform”KŒstrip_comments”NŒstrip_elements_with_classes”NŒ strip_classes”NŒ report_level”KŒ halt_level”KŒexit_status_level”KŒdebug”NŒwarning_stream”NŒ traceback”ˆŒinput_encoding”Œ utf-8-sig”Œinput_encoding_error_handler”Œstrict”Œoutput_encoding”Œutf-8”Œoutput_encoding_error_handler”j?Œerror_encoding”Œutf-8”Œerror_encoding_error_handler”Œbackslashreplace”Œ language_code”Œen”Œrecord_dependencies”NŒconfig”NŒ id_prefix”hŒauto_id_prefix”Œid”Œ dump_settings”NŒdump_internals”NŒdump_transforms”NŒdump_pseudo_xml”NŒexpose_internals”NŒstrict_visitor”NŒ_disable_config”NŒ_source”hÇŒ _destination”NŒ _config_files”]”Œ7/var/lib/git/docbuild/linux/Documentation/docutils.conf”aŒfile_insertion_enabled”ˆŒ raw_enabled”KŒline_length_limit”M'Œpep_references”NŒ pep_base_url”Œhttps://peps.python.org/”Œpep_file_url_template”Œpep-%04d”Œrfc_references”NŒ rfc_base_url”Œ&https://datatracker.ietf.org/doc/html/”Œ tab_width”KŒtrim_footnote_reference_space”‰Œsyntax_highlight”Œlong”Œ smart_quotes”ˆŒsmartquotes_locales”]”Œcharacter_level_inline_markup”‰Œdoctitle_xform”‰Œ docinfo_xform”KŒsectsubtitle_xform”‰Œ image_loading”Œlink”Œembed_stylesheet”‰Œcloak_email_addresses”ˆŒsection_self_link”‰Œenv”NubŒreporter”NŒindirect_targets”]”Œsubstitution_defs”}”Œsubstitution_names”}”Œrefnames”}”Œrefids”}”(jª]”jŸaj� ]”j† auŒnameids”}”(jjj-j*jn jk jVjSjsjpjPjMjjjÚj×jkjhjf jc j9j6j°j­j jªj j j– j“ j\ j� j[ jX jjjÔ jÑ j¤j¡j juŒ nametypes”}”(j‰j-‰jn ‰jV‰js‰jP‰j‰jÚ‰jk‰jf ‰j9‰j°‰j ˆj ‰j– ‰j\ ˆj[ ‰j‰jÔ ‰j¤‰j ‰uh}”(jhÊj*hëjk j0jSjAjpjYjMjxjjSj×jjhjÝjc jvj6j•j­j<jªj³j j³j“ j# j� j™ jX j™ jjq jÑ j� j¡j× jj§uŒ footnote_refs”}”Œ citation_refs”}”Œ autofootnotes”]”Œautofootnote_refs”]”Œsymbol_footnotes”]”Œsymbol_footnote_refs”]”Œ footnotes”]”Œ citations”]”Œautofootnote_start”KŒsymbol_footnote_start”KŒ id_counter”Œ collections”ŒCounter”“”}”jMKs…”R”Œparse_messages”]”Œtransform_messages”]”(hŒsystem_message”“”)�”}”(hhh]”hý)�”}”(hhh]”hŒAHyperlink target "writing-samples-into-buffer" is not referenced.”…”�”}”hjªsbah}”(h]”h ]”h"]”h$]”h&]”uh1hühj§ubah}”(h]”h ]”h"]”h$]”h&]”Œlevel”KŒtype”ŒINFO”Œsource”hÇŒline”Míuh1j¥ubj¦)�”}”(hhh]”hý)�”}”(hhh]”hŒ