]>
git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - tools/perf/tests/sw-clock.c
8 #include "util/evsel.h"
9 #include "util/evlist.h"
10 #include "util/cpumap.h"
11 #include "util/thread_map.h"
13 #define NR_LOOPS 10000000
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.
20 static int __test__sw_clock_freq(enum perf_sw_ids clock_id
)
24 u64 total_periods
= 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
,
33 .sample_type
= PERF_SAMPLE_PERIOD
,
39 struct thread_map
*threads
;
41 attr
.sample_freq
= 500;
43 evlist
= perf_evlist__new();
45 pr_debug("perf_evlist__new\n");
49 evsel
= perf_evsel__new(&attr
);
51 pr_debug("perf_evsel__new\n");
52 goto out_delete_evlist
;
54 perf_evlist__add(evlist
, evsel
);
56 cpus
= cpu_map__dummy_new();
57 threads
= thread_map__new_by_tid(getpid());
58 if (!cpus
|| !threads
) {
60 pr_debug("Not enough memory to create thread/cpu maps\n");
64 perf_evlist__set_maps(evlist
, cpus
, threads
);
69 if (perf_evlist__open(evlist
)) {
70 const char *knob
= "/proc/sys/kernel/perf_event_max_sample_rate";
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
;
79 err
= perf_evlist__mmap(evlist
, 128, true);
81 pr_debug("failed to mmap event: %d (%s)\n", errno
,
82 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
83 goto out_delete_evlist
;
86 perf_evlist__enable(evlist
);
89 for (i
= 0; i
< NR_LOOPS
; i
++)
92 perf_evlist__disable(evlist
);
94 while ((event
= perf_evlist__mmap_read(evlist
, 0)) != NULL
) {
95 struct perf_sample sample
;
97 if (event
->header
.type
!= PERF_RECORD_SAMPLE
)
100 err
= perf_evlist__parse_sample(evlist
, event
, &sample
);
102 pr_debug("Error during parse sample\n");
103 goto out_delete_evlist
;
106 total_periods
+= sample
.period
;
109 perf_evlist__mmap_consume(evlist
, 0);
112 if ((u64
) nr_samples
== total_periods
) {
113 pr_debug("All (%d) samples have period value of 1!\n",
120 thread_map__put(threads
);
122 perf_evlist__delete(evlist
);
126 int test__sw_clock_freq(int subtest __maybe_unused
)
130 ret
= __test__sw_clock_freq(PERF_COUNT_SW_CPU_CLOCK
);
132 ret
= __test__sw_clock_freq(PERF_COUNT_SW_TASK_CLOCK
);