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

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

File Facts

System
Linux kernel
Corpus path
tools/lib/perf/Documentation/libperf-sampling.txt
Extension
.txt
Size
6673 bytes
Lines
245
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

43         if (!cpus) {
 44                 fprintf(stderr, "failed to create cpus\n");
 45                 return -1;
 46         }
--

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

[source,c]
--
 48         evlist = perf_evlist__new();
 49         if (!evlist) {
 50                 fprintf(stderr, "failed to create evlist\n");
 51                 goto out_cpus;
 52         }
--

We create libperf's event for the cycles attribute we defined earlier and add it to the list:

[source,c]
--
 54         evsel = perf_evsel__new(&attr);
 55         if (!evsel) {
 56                 fprintf(stderr, "failed to create cycles\n");
 57                 goto out_cpus;
 58         }
 59
 60         perf_evlist__add(evlist, evsel);
--

Configure event list with the cpus map and open event:

[source,c]
--
 62         perf_evlist__set_maps(evlist, cpus, NULL);
 63
 64         err = perf_evlist__open(evlist);
 65         if (err) {
 66                 fprintf(stderr, "failed to open evlist\n");
 67                 goto out_evlist;
 68         }
--

Once the events list is open, we can create memory maps AKA perf ring buffers:

[source,c]
--
 70         err = perf_evlist__mmap(evlist, 4);
 71         if (err) {
 72                 fprintf(stderr, "failed to mmap evlist\n");
 73                 goto out_evlist;
 74         }
--

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.

[source,c]
--
 76         perf_evlist__enable(evlist);
 77         sleep(3);
 78         perf_evlist__disable(evlist);
--

Following code walks through the ring buffers and reads stored events/samples:

Annotation

Implementation Notes