perf test: Move each test suite struct to its test
[linux-2.6-block.git] / tools / perf / tests / sw-clock.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
a43783ae 2#include <errno.h>
fd20e811 3#include <inttypes.h>
bc96b361
NK
4#include <unistd.h>
5#include <stdlib.h>
6#include <signal.h>
7#include <sys/mman.h>
b4209025 8#include <linux/string.h>
bc96b361
NK
9
10#include "tests.h"
b4209025 11#include "util/debug.h"
bc96b361
NK
12#include "util/evsel.h"
13#include "util/evlist.h"
14#include "util/cpumap.h"
e0fcfb08 15#include "util/mmap.h"
bc96b361 16#include "util/thread_map.h"
453fa030 17#include <perf/evlist.h>
7728fa0c 18#include <perf/mmap.h>
bc96b361 19
3fe21305 20#define NR_LOOPS 10000000
bc96b361
NK
21
22/*
23 * This test will open software clock events (cpu-clock, task-clock)
24 * then check their frequency -> period conversion has no artifact of
25 * setting period to 1 forcefully.
26 */
27static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
28{
29 int i, err = -1;
30 volatile int tmp = 0;
31 u64 total_periods = 0;
32 int nr_samples = 0;
ba3dfff8 33 char sbuf[STRERR_BUFSIZE];
bc96b361 34 union perf_event *event;
32dcd021 35 struct evsel *evsel;
63503dba 36 struct evlist *evlist;
bc96b361
NK
37 struct perf_event_attr attr = {
38 .type = PERF_TYPE_SOFTWARE,
39 .config = clock_id,
40 .sample_type = PERF_SAMPLE_PERIOD,
41 .exclude_kernel = 1,
42 .disabled = 1,
43 .freq = 1,
44 };
97ab7c52
NK
45 struct perf_cpu_map *cpus = NULL;
46 struct perf_thread_map *threads = NULL;
a5830532 47 struct mmap *md;
bc96b361 48
67c1e4a5 49 attr.sample_freq = 500;
bc96b361 50
0f98b11c 51 evlist = evlist__new();
bc96b361 52 if (evlist == NULL) {
0f98b11c 53 pr_debug("evlist__new\n");
bc96b361
NK
54 return -1;
55 }
56
365c3ae7 57 evsel = evsel__new(&attr);
bc96b361 58 if (evsel == NULL) {
8f6725a2 59 pr_debug("evsel__new\n");
03ad9747 60 goto out_delete_evlist;
bc96b361 61 }
a1cf3a75 62 evlist__add(evlist, evsel);
bc96b361 63
397721e0 64 cpus = perf_cpu_map__dummy_new();
c5e6bd2e
AH
65 threads = thread_map__new_by_tid(getpid());
66 if (!cpus || !threads) {
bc96b361
NK
67 err = -ENOMEM;
68 pr_debug("Not enough memory to create thread/cpu maps\n");
97ab7c52 69 goto out_delete_evlist;
bc96b361
NK
70 }
71
453fa030 72 perf_evlist__set_maps(&evlist->core, cpus, threads);
c5e6bd2e 73
474ddc4c 74 if (evlist__open(evlist)) {
67c1e4a5
ACM
75 const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate";
76
d0b849e9 77 err = -errno;
67c1e4a5 78 pr_debug("Couldn't open evlist: %s\nHint: check %s, using %" PRIu64 " in this test.\n",
c8b5f2c9 79 str_error_r(errno, sbuf, sizeof(sbuf)),
ba3dfff8 80 knob, (u64)attr.sample_freq);
03ad9747 81 goto out_delete_evlist;
d0b849e9 82 }
bc96b361 83
9521b5f2 84 err = evlist__mmap(evlist, 128);
bc96b361
NK
85 if (err < 0) {
86 pr_debug("failed to mmap event: %d (%s)\n", errno,
c8b5f2c9 87 str_error_r(errno, sbuf, sizeof(sbuf)));
f26e1c7c 88 goto out_delete_evlist;
bc96b361
NK
89 }
90
1c87f165 91 evlist__enable(evlist);
bc96b361
NK
92
93 /* collect samples */
94 for (i = 0; i < NR_LOOPS; i++)
95 tmp++;
96
e74676de 97 evlist__disable(evlist);
bc96b361 98
5d0007cd 99 md = &evlist->mmap[0];
7c4d4182 100 if (perf_mmap__read_init(&md->core) < 0)
5d0007cd
KL
101 goto out_init;
102
151ed5d7 103 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
bc96b361
NK
104 struct perf_sample sample;
105
106 if (event->header.type != PERF_RECORD_SAMPLE)
8e50d384 107 goto next_event;
bc96b361 108
2a6599cd 109 err = evlist__parse_sample(evlist, event, &sample);
bc96b361
NK
110 if (err < 0) {
111 pr_debug("Error during parse sample\n");
983874d1 112 goto out_delete_evlist;
bc96b361
NK
113 }
114
115 total_periods += sample.period;
116 nr_samples++;
8e50d384 117next_event:
7728fa0c 118 perf_mmap__consume(&md->core);
bc96b361 119 }
32fdc2ca 120 perf_mmap__read_done(&md->core);
bc96b361 121
5d0007cd 122out_init:
bc96b361
NK
123 if ((u64) nr_samples == total_periods) {
124 pr_debug("All (%d) samples have period value of 1!\n",
125 nr_samples);
126 err = -1;
127 }
128
97ab7c52 129out_delete_evlist:
38f01d8d 130 perf_cpu_map__put(cpus);
7836e52e 131 perf_thread_map__put(threads);
c12995a5 132 evlist__delete(evlist);
bc96b361
NK
133 return err;
134}
135
d68f0365 136static int test__sw_clock_freq(struct test *test __maybe_unused, int subtest __maybe_unused)
bc96b361
NK
137{
138 int ret;
139
140 ret = __test__sw_clock_freq(PERF_COUNT_SW_CPU_CLOCK);
141 if (!ret)
142 ret = __test__sw_clock_freq(PERF_COUNT_SW_TASK_CLOCK);
143
144 return ret;
145}
d68f0365
IR
146
147DEFINE_SUITE("Software clock events period values", sw_clock_freq);