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