DESCRIPTION
The sampling interface provides API to measure and get count for specific perf events.
The following test tries to explain count on sampling.c
example.
It is by no means complete guide to sampling, but shows libperf basic API for sampling.
The sampling.c
comes with libperf package and can be compiled and run like:
It requires root access, because it uses hardware cycles event.
The sampling.c
example profiles/samples all CPUs with hardware cycles, in a
nutshell it:
-
creates events
-
adds them to the event list
-
opens and enables events through the event list
-
sleeps for 3 seconds
-
disables events
-
reads and displays recorded samples
-
destroys the event list
The first thing you need to do before using libperf is to call init function:
It will setup the library and sets function for debug output from library.
The libperf_print
callback will receive any message with its debug level,
defined as:
Once the setup is complete we start by defining cycles event using the struct perf_event_attr
:
Next step is to prepare CPUs map.
In this case we will monitor all the available CPUs:
Now we create libperf’s event list, which will serve as holder for the cycles event:
We create libperf’s event for the cycles attribute we defined earlier and add it to the list:
Configure event list with the cpus map and open event:
Once the events list is open, we can create memory maps AKA perf ring buffers:
The event is created as disabled (note the disabled = 1
assignment above),
so we need to enable the events list explicitly.
From this moment the cycles event is sampling.
We will sleep for 3 seconds while the ring buffers get data from all CPUs, then we disable the events list.
Following code walks through the ring buffers and reads stored events/samples:
Each sample needs to get parsed:
And finally cleanup.
We close the whole events list (both events) and remove it together with the threads map:
REPORTING BUGS
Report bugs to <linux-perf-users@vger.kernel.org>.
LICENSE
libperf is Free Software licensed under the GNU LGPL 2.1
SEE ALSO
libperf(3), libperf-counting(7)