DESCRIPTION

The counting interface provides API to measure and get count for specific perf events.

The following test tries to explain count on counting.c example.

It is by no means complete guide to counting, but shows libperf basic API for counting.

The counting.c comes with libperf package and can be compiled and run like:

It requires root access, because of the PERF_COUNT_SW_CPU_CLOCK event, which is available only for root.

The counting.c example monitors two events on the current process and displays their count, in a nutshell it:

  • creates events

  • adds them to the event list

  • opens and enables events through the event list

  • does some workload

  • disables events

  • reads and displays event counts

  • 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 specific events using the struct perf_event_attr.

We create software events for cpu and task:

The read_format setup tells perf to include timing details together with each count.

Next step is to prepare threads map.

In this case we will monitor current process, so we create threads map with single pid (0):

Now we create libperf’s event list, which will serve as holder for the events we want:

We create libperf’s events for the attributes we defined earlier and add them to the list:

Configure event list with the thread map and open events:

Both events are created as disabled (note the disabled = 1 assignment above), so we need to enable the whole list explicitly (both events).

From this moment events are counting and we can do our workload.

When we are done we disable the events list.

Now we need to get the counts from events, following code iterates through the events list and read counts:

And finally cleanup.

We close the whole events list (both events) and remove it together with the threads map:

REPORTING BUGS

LICENSE

libperf is Free Software licensed under the GNU LGPL 2.1

RESOURCES

SEE ALSO

libperf(3), libperf-sampling(7)