1 .. SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
6 Compile and install libperf from kernel sources
7 ===============================================
10 git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
11 cd linux/tools/perf/lib
13 sudo make install prefix=/usr
17 The libperf library provides several high level objects:
20 Provides a cpu list abstraction.
22 struct perf_thread_map
23 Provides a thread list abstraction.
26 Provides an abstraction for single a perf event.
29 Gathers several struct perf_evsel object and performs functions on all of them.
31 The exported API binds these objects together,
32 for full reference see the libperf.7 man page.
36 Examples aim to explain libperf functionality on simple use cases.
37 They are based in on a checked out linux kernel git tree:
41 $ cd tools/perf/lib/Documentation/tutorial/
43 ex-1-compile ex-2-evsel-stat ex-3-evlist-stat
47 This example shows the basic usage of *struct perf_cpu_map*,
48 how to create it and display its cpus:
54 gcc -o test test.c -lperf
59 The full code listing is here:
63 1 #include <perf/cpumap.h>
65 3 int main(int argc, char **Argv)
67 5 struct perf_cpu_map *cpus;
70 8 cpus = perf_cpu_map__new(NULL);
72 10 perf_cpu_map__for_each_cpu(cpu, tmp, cpus)
73 11 fprintf(stdout, "%d ", cpu);
75 13 fprintf(stdout, "\n");
77 15 perf_cpu_map__put(cpus);
82 First you need to include the proper header to have *struct perf_cpumap*
83 declaration and functions:
87 1 #include <perf/cpumap.h>
90 The *struct perf_cpumap* object is created by *perf_cpu_map__new* call.
91 The *NULL* argument asks it to populate the object with the current online CPUs list:
95 8 cpus = perf_cpu_map__new(NULL);
97 This is paired with a *perf_cpu_map__put*, that drops its reference at the end, possibly deleting it.
101 15 perf_cpu_map__put(cpus);
103 The iteration through the *struct perf_cpumap* CPUs is done using the *perf_cpu_map__for_each_cpu*
104 macro which requires 3 arguments:
106 - cpu - the cpu numer
107 - tmp - iteration helper variable
108 - cpus - the *struct perf_cpumap* object
112 10 perf_cpu_map__for_each_cpu(cpu, tmp, cpus)
113 11 fprintf(stdout, "%d ", cpu);
115 ex-2-evsel-stat example
116 =======================
120 ex-3-evlist-stat example
121 ========================