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