tools/lib/perf/Documentation/libperf-counting.txt

Source file repositories/reference/linux-study-clean/tools/lib/perf/Documentation/libperf-counting.txt

File Facts

System
Linux kernel
Corpus path
tools/lib/perf/Documentation/libperf-counting.txt
Extension
.txt
Size
5607 bytes
Lines
214
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: documentation
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

38         if (!threads) {
 39                 fprintf(stderr, "failed to create threads\n");
 40                 return -1;
 41         }
 42
 43         perf_thread_map__set_pid(threads, 0, 0);
--

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

[source,c]
--
 45         evlist = perf_evlist__new();
 46         if (!evlist) {
 47                 fprintf(stderr, "failed to create evlist\n");
 48                 goto out_threads;
 49         }
--

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

[source,c]
--
 51         evsel = perf_evsel__new(&attr1);
 52         if (!evsel) {
 53                 fprintf(stderr, "failed to create evsel1\n");
 54                 goto out_evlist;
 55         }
 56
 57         perf_evlist__add(evlist, evsel);
 58
 59         evsel = perf_evsel__new(&attr2);
 60         if (!evsel) {
 61                 fprintf(stderr, "failed to create evsel2\n");
 62                 goto out_evlist;
 63         }
 64
 65         perf_evlist__add(evlist, evsel);
--

Configure event list with the thread map and open events:

[source,c]
--
 67         perf_evlist__set_maps(evlist, NULL, threads);
 68
 69         err = perf_evlist__open(evlist);
 70         if (err) {
 71                 fprintf(stderr, "failed to open evsel\n");
 72                 goto out_evlist;
 73         }
--

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.

[source,c]
--
 75         perf_evlist__enable(evlist);
 76
 77         while (count--);
 78
 79         perf_evlist__disable(evlist);
--

Now we need to get the counts from events, following code iterates through the

Annotation

Implementation Notes