xref: /openbmc/linux/tools/lib/perf/Documentation/examples/sampling.c (revision 8dd06ef34b6e2f41b29fbf5fc1663780f2524285)
1*81de3bf3SJiri Olsa #include <linux/perf_event.h>
2*81de3bf3SJiri Olsa #include <perf/evlist.h>
3*81de3bf3SJiri Olsa #include <perf/evsel.h>
4*81de3bf3SJiri Olsa #include <perf/cpumap.h>
5*81de3bf3SJiri Olsa #include <perf/threadmap.h>
6*81de3bf3SJiri Olsa #include <perf/mmap.h>
7*81de3bf3SJiri Olsa #include <perf/core.h>
8*81de3bf3SJiri Olsa #include <perf/event.h>
9*81de3bf3SJiri Olsa #include <stdio.h>
10*81de3bf3SJiri Olsa #include <unistd.h>
11*81de3bf3SJiri Olsa 
libperf_print(enum libperf_print_level level,const char * fmt,va_list ap)12*81de3bf3SJiri Olsa static int libperf_print(enum libperf_print_level level,
13*81de3bf3SJiri Olsa                          const char *fmt, va_list ap)
14*81de3bf3SJiri Olsa {
15*81de3bf3SJiri Olsa 	return vfprintf(stderr, fmt, ap);
16*81de3bf3SJiri Olsa }
17*81de3bf3SJiri Olsa 
18*81de3bf3SJiri Olsa union u64_swap {
19*81de3bf3SJiri Olsa 	__u64 val64;
20*81de3bf3SJiri Olsa 	__u32 val32[2];
21*81de3bf3SJiri Olsa };
22*81de3bf3SJiri Olsa 
main(int argc,char ** argv)23*81de3bf3SJiri Olsa int main(int argc, char **argv)
24*81de3bf3SJiri Olsa {
25*81de3bf3SJiri Olsa 	struct perf_evlist *evlist;
26*81de3bf3SJiri Olsa 	struct perf_evsel *evsel;
27*81de3bf3SJiri Olsa 	struct perf_mmap *map;
28*81de3bf3SJiri Olsa 	struct perf_cpu_map *cpus;
29*81de3bf3SJiri Olsa 	struct perf_event_attr attr = {
30*81de3bf3SJiri Olsa 		.type        = PERF_TYPE_HARDWARE,
31*81de3bf3SJiri Olsa 		.config      = PERF_COUNT_HW_CPU_CYCLES,
32*81de3bf3SJiri Olsa 		.disabled    = 1,
33*81de3bf3SJiri Olsa 		.freq        = 1,
34*81de3bf3SJiri Olsa 		.sample_freq = 10,
35*81de3bf3SJiri Olsa 		.sample_type = PERF_SAMPLE_IP|PERF_SAMPLE_TID|PERF_SAMPLE_CPU|PERF_SAMPLE_PERIOD,
36*81de3bf3SJiri Olsa 	};
37*81de3bf3SJiri Olsa 	int err = -1;
38*81de3bf3SJiri Olsa 	union perf_event *event;
39*81de3bf3SJiri Olsa 
40*81de3bf3SJiri Olsa 	libperf_init(libperf_print);
41*81de3bf3SJiri Olsa 
42*81de3bf3SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
43*81de3bf3SJiri Olsa 	if (!cpus) {
44*81de3bf3SJiri Olsa 		fprintf(stderr, "failed to create cpus\n");
45*81de3bf3SJiri Olsa 		return -1;
46*81de3bf3SJiri Olsa 	}
47*81de3bf3SJiri Olsa 
48*81de3bf3SJiri Olsa 	evlist = perf_evlist__new();
49*81de3bf3SJiri Olsa 	if (!evlist) {
50*81de3bf3SJiri Olsa 		fprintf(stderr, "failed to create evlist\n");
51*81de3bf3SJiri Olsa 		goto out_cpus;
52*81de3bf3SJiri Olsa 	}
53*81de3bf3SJiri Olsa 
54*81de3bf3SJiri Olsa 	evsel = perf_evsel__new(&attr);
55*81de3bf3SJiri Olsa 	if (!evsel) {
56*81de3bf3SJiri Olsa 		fprintf(stderr, "failed to create cycles\n");
57*81de3bf3SJiri Olsa 		goto out_cpus;
58*81de3bf3SJiri Olsa 	}
59*81de3bf3SJiri Olsa 
60*81de3bf3SJiri Olsa 	perf_evlist__add(evlist, evsel);
61*81de3bf3SJiri Olsa 
62*81de3bf3SJiri Olsa 	perf_evlist__set_maps(evlist, cpus, NULL);
63*81de3bf3SJiri Olsa 
64*81de3bf3SJiri Olsa 	err = perf_evlist__open(evlist);
65*81de3bf3SJiri Olsa 	if (err) {
66*81de3bf3SJiri Olsa 		fprintf(stderr, "failed to open evlist\n");
67*81de3bf3SJiri Olsa 		goto out_evlist;
68*81de3bf3SJiri Olsa 	}
69*81de3bf3SJiri Olsa 
70*81de3bf3SJiri Olsa 	err = perf_evlist__mmap(evlist, 4);
71*81de3bf3SJiri Olsa 	if (err) {
72*81de3bf3SJiri Olsa 		fprintf(stderr, "failed to mmap evlist\n");
73*81de3bf3SJiri Olsa 		goto out_evlist;
74*81de3bf3SJiri Olsa 	}
75*81de3bf3SJiri Olsa 
76*81de3bf3SJiri Olsa 	perf_evlist__enable(evlist);
77*81de3bf3SJiri Olsa 	sleep(3);
78*81de3bf3SJiri Olsa 	perf_evlist__disable(evlist);
79*81de3bf3SJiri Olsa 
80*81de3bf3SJiri Olsa 	perf_evlist__for_each_mmap(evlist, map, false) {
81*81de3bf3SJiri Olsa 		if (perf_mmap__read_init(map) < 0)
82*81de3bf3SJiri Olsa 			continue;
83*81de3bf3SJiri Olsa 
84*81de3bf3SJiri Olsa 		while ((event = perf_mmap__read_event(map)) != NULL) {
85*81de3bf3SJiri Olsa 			int cpu, pid, tid;
86*81de3bf3SJiri Olsa 			__u64 ip, period, *array;
87*81de3bf3SJiri Olsa 			union u64_swap u;
88*81de3bf3SJiri Olsa 
89*81de3bf3SJiri Olsa 			array = event->sample.array;
90*81de3bf3SJiri Olsa 
91*81de3bf3SJiri Olsa 			ip = *array;
92*81de3bf3SJiri Olsa 			array++;
93*81de3bf3SJiri Olsa 
94*81de3bf3SJiri Olsa 			u.val64 = *array;
95*81de3bf3SJiri Olsa 			pid = u.val32[0];
96*81de3bf3SJiri Olsa 			tid = u.val32[1];
97*81de3bf3SJiri Olsa 			array++;
98*81de3bf3SJiri Olsa 
99*81de3bf3SJiri Olsa 			u.val64 = *array;
100*81de3bf3SJiri Olsa 			cpu = u.val32[0];
101*81de3bf3SJiri Olsa 			array++;
102*81de3bf3SJiri Olsa 
103*81de3bf3SJiri Olsa 			period = *array;
104*81de3bf3SJiri Olsa 
105*81de3bf3SJiri Olsa 			fprintf(stdout, "cpu %3d, pid %6d, tid %6d, ip %20llx, period %20llu\n",
106*81de3bf3SJiri Olsa 				cpu, pid, tid, ip, period);
107*81de3bf3SJiri Olsa 
108*81de3bf3SJiri Olsa 			perf_mmap__consume(map);
109*81de3bf3SJiri Olsa 		}
110*81de3bf3SJiri Olsa 
111*81de3bf3SJiri Olsa 		perf_mmap__read_done(map);
112*81de3bf3SJiri Olsa 	}
113*81de3bf3SJiri Olsa 
114*81de3bf3SJiri Olsa out_evlist:
115*81de3bf3SJiri Olsa 	perf_evlist__delete(evlist);
116*81de3bf3SJiri Olsa out_cpus:
117*81de3bf3SJiri Olsa 	perf_cpu_map__put(cpus);
118*81de3bf3SJiri Olsa 	return err;
119*81de3bf3SJiri Olsa }
120