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
;
44 attr
.sample_freq
= 500;
46 evlist
= perf_evlist__new();
48 pr_debug("perf_evlist__new\n");
52 evsel
= perf_evsel__new(&attr
);
54 pr_debug("perf_evsel__new\n");
55 goto out_delete_evlist
;
57 perf_evlist__add(evlist
, evsel
);
59 cpus
= cpu_map__dummy_new();
60 threads
= thread_map__new_by_tid(getpid());
61 if (!cpus
|| !threads
) {
63 pr_debug("Not enough memory to create thread/cpu maps\n");
67 perf_evlist__set_maps(evlist
, cpus
, threads
);
72 if (perf_evlist__open(evlist
)) {
73 const char *knob
= "/proc/sys/kernel/perf_event_max_sample_rate";
76 pr_debug("Couldn't open evlist: %s\nHint: check %s, using %" PRIu64
" in this test.\n",
77 str_error_r(errno
, sbuf
, sizeof(sbuf
)),
78 knob
, (u64
)attr
.sample_freq
);
79 goto out_delete_evlist
;
82 err
= perf_evlist__mmap(evlist
, 128);
84 pr_debug("failed to mmap event: %d (%s)\n", errno
,
85 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
86 goto out_delete_evlist
;
89 perf_evlist__enable(evlist
);
92 for (i
= 0; i
< NR_LOOPS
; i
++)
95 perf_evlist__disable(evlist
);
97 md
= &evlist
->mmap
[0];
98 if (perf_mmap__read_init(md
) < 0)
101 while ((event
= perf_mmap__read_event(md
)) != NULL
) {
102 struct perf_sample sample
;
104 if (event
->header
.type
!= PERF_RECORD_SAMPLE
)
107 err
= perf_evlist__parse_sample(evlist
, event
, &sample
);
109 pr_debug("Error during parse sample\n");
110 goto out_delete_evlist
;
113 total_periods
+= sample
.period
;
116 perf_mmap__consume(md
);
118 perf_mmap__read_done(md
);
121 if ((u64
) nr_samples
== total_periods
) {
122 pr_debug("All (%d) samples have period value of 1!\n",
129 thread_map__put(threads
);
131 perf_evlist__delete(evlist
);
135 int test__sw_clock_freq(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
139 ret
= __test__sw_clock_freq(PERF_COUNT_SW_CPU_CLOCK
);
141 ret
= __test__sw_clock_freq(PERF_COUNT_SW_TASK_CLOCK
);