xref: /openbmc/linux/tools/perf/tests/sw-clock.c (revision f7d84fa7)
1 #include <errno.h>
2 #include <inttypes.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <signal.h>
6 #include <sys/mman.h>
7 
8 #include "tests.h"
9 #include "util/evsel.h"
10 #include "util/evlist.h"
11 #include "util/cpumap.h"
12 #include "util/thread_map.h"
13 
14 #define NR_LOOPS  10000000
15 
16 /*
17  * This test will open software clock events (cpu-clock, task-clock)
18  * then check their frequency -> period conversion has no artifact of
19  * setting period to 1 forcefully.
20  */
21 static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
22 {
23 	int i, err = -1;
24 	volatile int tmp = 0;
25 	u64 total_periods = 0;
26 	int nr_samples = 0;
27 	char sbuf[STRERR_BUFSIZE];
28 	union perf_event *event;
29 	struct perf_evsel *evsel;
30 	struct perf_evlist *evlist;
31 	struct perf_event_attr attr = {
32 		.type = PERF_TYPE_SOFTWARE,
33 		.config = clock_id,
34 		.sample_type = PERF_SAMPLE_PERIOD,
35 		.exclude_kernel = 1,
36 		.disabled = 1,
37 		.freq = 1,
38 	};
39 	struct cpu_map *cpus;
40 	struct thread_map *threads;
41 
42 	attr.sample_freq = 500;
43 
44 	evlist = perf_evlist__new();
45 	if (evlist == NULL) {
46 		pr_debug("perf_evlist__new\n");
47 		return -1;
48 	}
49 
50 	evsel = perf_evsel__new(&attr);
51 	if (evsel == NULL) {
52 		pr_debug("perf_evsel__new\n");
53 		goto out_delete_evlist;
54 	}
55 	perf_evlist__add(evlist, evsel);
56 
57 	cpus = cpu_map__dummy_new();
58 	threads = thread_map__new_by_tid(getpid());
59 	if (!cpus || !threads) {
60 		err = -ENOMEM;
61 		pr_debug("Not enough memory to create thread/cpu maps\n");
62 		goto out_free_maps;
63 	}
64 
65 	perf_evlist__set_maps(evlist, cpus, threads);
66 
67 	cpus	= NULL;
68 	threads = NULL;
69 
70 	if (perf_evlist__open(evlist)) {
71 		const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate";
72 
73 		err = -errno;
74 		pr_debug("Couldn't open evlist: %s\nHint: check %s, using %" PRIu64 " in this test.\n",
75 			 str_error_r(errno, sbuf, sizeof(sbuf)),
76 			 knob, (u64)attr.sample_freq);
77 		goto out_delete_evlist;
78 	}
79 
80 	err = perf_evlist__mmap(evlist, 128, true);
81 	if (err < 0) {
82 		pr_debug("failed to mmap event: %d (%s)\n", errno,
83 			 str_error_r(errno, sbuf, sizeof(sbuf)));
84 		goto out_delete_evlist;
85 	}
86 
87 	perf_evlist__enable(evlist);
88 
89 	/* collect samples */
90 	for (i = 0; i < NR_LOOPS; i++)
91 		tmp++;
92 
93 	perf_evlist__disable(evlist);
94 
95 	while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
96 		struct perf_sample sample;
97 
98 		if (event->header.type != PERF_RECORD_SAMPLE)
99 			goto next_event;
100 
101 		err = perf_evlist__parse_sample(evlist, event, &sample);
102 		if (err < 0) {
103 			pr_debug("Error during parse sample\n");
104 			goto out_delete_evlist;
105 		}
106 
107 		total_periods += sample.period;
108 		nr_samples++;
109 next_event:
110 		perf_evlist__mmap_consume(evlist, 0);
111 	}
112 
113 	if ((u64) nr_samples == total_periods) {
114 		pr_debug("All (%d) samples have period value of 1!\n",
115 			 nr_samples);
116 		err = -1;
117 	}
118 
119 out_free_maps:
120 	cpu_map__put(cpus);
121 	thread_map__put(threads);
122 out_delete_evlist:
123 	perf_evlist__delete(evlist);
124 	return err;
125 }
126 
127 int test__sw_clock_freq(int subtest __maybe_unused)
128 {
129 	int ret;
130 
131 	ret = __test__sw_clock_freq(PERF_COUNT_SW_CPU_CLOCK);
132 	if (!ret)
133 		ret = __test__sw_clock_freq(PERF_COUNT_SW_TASK_CLOCK);
134 
135 	return ret;
136 }
137