]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - 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
CommitLineData
fd20e811 1#include <inttypes.h>
bc96b361
NK
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
3fe21305 13#define NR_LOOPS 10000000
bc96b361
NK
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 */
20static 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;
ba3dfff8 26 char sbuf[STRERR_BUFSIZE];
bc96b361
NK
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 };
c5e6bd2e
AH
38 struct cpu_map *cpus;
39 struct thread_map *threads;
bc96b361 40
67c1e4a5 41 attr.sample_freq = 500;
bc96b361
NK
42
43 evlist = perf_evlist__new();
44 if (evlist == NULL) {
45 pr_debug("perf_evlist__new\n");
46 return -1;
47 }
48
ef503831 49 evsel = perf_evsel__new(&attr);
bc96b361
NK
50 if (evsel == NULL) {
51 pr_debug("perf_evsel__new\n");
03ad9747 52 goto out_delete_evlist;
bc96b361
NK
53 }
54 perf_evlist__add(evlist, evsel);
55
c5e6bd2e
AH
56 cpus = cpu_map__dummy_new();
57 threads = thread_map__new_by_tid(getpid());
58 if (!cpus || !threads) {
bc96b361
NK
59 err = -ENOMEM;
60 pr_debug("Not enough memory to create thread/cpu maps\n");
c5e6bd2e 61 goto out_free_maps;
bc96b361
NK
62 }
63
c5e6bd2e
AH
64 perf_evlist__set_maps(evlist, cpus, threads);
65
66 cpus = NULL;
67 threads = NULL;
68
d0b849e9 69 if (perf_evlist__open(evlist)) {
67c1e4a5
ACM
70 const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate";
71
d0b849e9 72 err = -errno;
67c1e4a5 73 pr_debug("Couldn't open evlist: %s\nHint: check %s, using %" PRIu64 " in this test.\n",
c8b5f2c9 74 str_error_r(errno, sbuf, sizeof(sbuf)),
ba3dfff8 75 knob, (u64)attr.sample_freq);
03ad9747 76 goto out_delete_evlist;
d0b849e9 77 }
bc96b361
NK
78
79 err = perf_evlist__mmap(evlist, 128, true);
80 if (err < 0) {
81 pr_debug("failed to mmap event: %d (%s)\n", errno,
c8b5f2c9 82 str_error_r(errno, sbuf, sizeof(sbuf)));
f26e1c7c 83 goto out_delete_evlist;
bc96b361
NK
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)
8e50d384 98 goto next_event;
bc96b361
NK
99
100 err = perf_evlist__parse_sample(evlist, event, &sample);
101 if (err < 0) {
102 pr_debug("Error during parse sample\n");
983874d1 103 goto out_delete_evlist;
bc96b361
NK
104 }
105
106 total_periods += sample.period;
107 nr_samples++;
8e50d384
ZZ
108next_event:
109 perf_evlist__mmap_consume(evlist, 0);
bc96b361
NK
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
c5e6bd2e
AH
118out_free_maps:
119 cpu_map__put(cpus);
120 thread_map__put(threads);
03ad9747 121out_delete_evlist:
bc96b361
NK
122 perf_evlist__delete(evlist);
123 return err;
124}
125
721a1f53 126int test__sw_clock_freq(int subtest __maybe_unused)
bc96b361
NK
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}