1 // SPDX-License-Identifier: GPL-2.0
10 #include "util/evsel.h"
11 #include "util/evlist.h"
12 #include "util/cpumap.h"
13 #include "util/thread_map.h"
15 #define NR_LOOPS 10000000
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.
22 static int __test__sw_clock_freq(enum perf_sw_ids clock_id
)
26 u64 total_periods
= 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
,
35 .sample_type
= PERF_SAMPLE_PERIOD
,
41 struct thread_map
*threads
;
43 attr
.sample_freq
= 500;
45 evlist
= perf_evlist__new();
47 pr_debug("perf_evlist__new\n");
51 evsel
= perf_evsel__new(&attr
);
53 pr_debug("perf_evsel__new\n");
54 goto out_delete_evlist
;
56 perf_evlist__add(evlist
, evsel
);
58 cpus
= cpu_map__dummy_new();
59 threads
= thread_map__new_by_tid(getpid());
60 if (!cpus
|| !threads
) {
62 pr_debug("Not enough memory to create thread/cpu maps\n");
66 perf_evlist__set_maps(evlist
, cpus
, threads
);
71 if (perf_evlist__open(evlist
)) {
72 const char *knob
= "/proc/sys/kernel/perf_event_max_sample_rate";
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
;
81 err
= perf_evlist__mmap(evlist
, 128);
83 pr_debug("failed to mmap event: %d (%s)\n", errno
,
84 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
85 goto out_delete_evlist
;
88 perf_evlist__enable(evlist
);
91 for (i
= 0; i
< NR_LOOPS
; i
++)
94 perf_evlist__disable(evlist
);
96 while ((event
= perf_evlist__mmap_read(evlist
, 0)) != NULL
) {
97 struct perf_sample sample
;
99 if (event
->header
.type
!= PERF_RECORD_SAMPLE
)
102 err
= perf_evlist__parse_sample(evlist
, event
, &sample
);
104 pr_debug("Error during parse sample\n");
105 goto out_delete_evlist
;
108 total_periods
+= sample
.period
;
111 perf_evlist__mmap_consume(evlist
, 0);
114 if ((u64
) nr_samples
== total_periods
) {
115 pr_debug("All (%d) samples have period value of 1!\n",
122 thread_map__put(threads
);
124 perf_evlist__delete(evlist
);
128 int test__sw_clock_freq(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
132 ret
= __test__sw_clock_freq(PERF_COUNT_SW_CPU_CLOCK
);
134 ret
= __test__sw_clock_freq(PERF_COUNT_SW_TASK_CLOCK
);