Chapter 19. Monitoring processes for performance bottlenecks by using perf circular buffers
You can create circular buffers that take event-specific snapshots of data with the perf tool. Use them to monitor performance bottlenecks in specific processes or parts of applications running on your system.
In such cases, perf only writes data to a perf.data file for later analysis if a specified event is detected.
19.1. Circular buffers and event-specific snapshots with perf Copy linkLink copied to clipboard!
Recording hours of perf data before a specific event may cause excessive overhead or prove impractical for analysis. In such cases, use perf record to create custom circular buffers that take snapshots after specific events.
The --overwrite option makes perf record store all data in an overwritable circular buffer. When the buffer gets full, perf record automatically overwrites the oldest records which, therefore, never get written to a perf.data file.
Using the --overwrite and --switch-output-event options together configures a circular buffer. It records and dumps data continuously until it detects the --switch-output-event trigger event. The trigger signals perf record to write data from the circular buffer to the perf.data file after an event. This collects specific data while reducing overhead by only writing relevant information to the perf.data file.
19.2. Collecting specific data to monitor for performance bottlenecks by using perf circular buffers Copy linkLink copied to clipboard!
With the perf tool, you can create circular buffers. These are triggered by events you specify to only collect data you are interested in. To create circular buffers that collect event-specific data, use the --overwrite and --switch-output-event options for perf.
Prerequisites
-
You have the
perfuser space tool installed. For more information, see Installing perf. You have placed a
uprobein the process or application you are interested in monitoring at a location of interest within the process or application:# perf probe -x </path/to/executable> -a functionAdded new event: probe_executable:function (on function in /path/to/executable) You can now use it in all perf tools, such as: perf record -e probe_executable:function -aR sleep 1
Procedure
Create the circular buffer with the
uprobeas the trigger event:# perf record --overwrite -e cycles --switch-output-event probe_executable:function ./executable[ perf record: dump data: Woken up 1 times ] [ perf record: Dump perf.data.2021021012231959 ] [ perf record: dump data: Woken up 1 times ] [ perf record: Dump perf.data.2021021012232008 ] [ perf record: dump data: Woken up 1 times ] [ perf record: Dump perf.data.2021021012232082 ] [ perf record: Captured and wrote 5.621 MB perf.data.<timestamp> ]This command initiates the executable and collects cpu cycles, specified after the
-eoption, untilperfdetects theuprobe, the trigger event specified after the--switch-output-eventoption. At that point,perfsnapshots circular buffer data and stores it in a uniqueperf.datafile identified by timestamp. This example produced a total of 2 snapshots, the lastperf.datafile was forced by pressing Ctrl+C.