blob: 725a196991a88447cf0f80adf5b05bb47ad1f39e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Namhyung Kimbc96b362013-03-18 11:41:47 +09004#include <unistd.h>
5#include <stdlib.h>
6#include <signal.h>
7#include <sys/mman.h>
8
9#include "tests.h"
10#include "util/evsel.h"
11#include "util/evlist.h"
12#include "util/cpumap.h"
13#include "util/thread_map.h"
14
Adrian Hunter3fe21302013-11-12 11:45:21 -030015#define NR_LOOPS 10000000
Namhyung Kimbc96b362013-03-18 11:41:47 +090016
17/*
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.
21 */
22static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
23{
24 int i, err = -1;
25 volatile int tmp = 0;
26 u64 total_periods = 0;
27 int nr_samples = 0;
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000028 char sbuf[STRERR_BUFSIZE];
Namhyung Kimbc96b362013-03-18 11:41:47 +090029 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,
34 .config = clock_id,
35 .sample_type = PERF_SAMPLE_PERIOD,
36 .exclude_kernel = 1,
37 .disabled = 1,
38 .freq = 1,
39 };
Adrian Hunterc5e6bd22015-09-08 10:59:02 +030040 struct cpu_map *cpus;
41 struct thread_map *threads;
Namhyung Kimbc96b362013-03-18 11:41:47 +090042
Arnaldo Carvalho de Melo67c1e4a2013-11-11 16:33:18 -030043 attr.sample_freq = 500;
Namhyung Kimbc96b362013-03-18 11:41:47 +090044
45 evlist = perf_evlist__new();
46 if (evlist == NULL) {
47 pr_debug("perf_evlist__new\n");
48 return -1;
49 }
50
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -030051 evsel = perf_evsel__new(&attr);
Namhyung Kimbc96b362013-03-18 11:41:47 +090052 if (evsel == NULL) {
53 pr_debug("perf_evsel__new\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -030054 goto out_delete_evlist;
Namhyung Kimbc96b362013-03-18 11:41:47 +090055 }
56 perf_evlist__add(evlist, evsel);
57
Adrian Hunterc5e6bd22015-09-08 10:59:02 +030058 cpus = cpu_map__dummy_new();
59 threads = thread_map__new_by_tid(getpid());
60 if (!cpus || !threads) {
Namhyung Kimbc96b362013-03-18 11:41:47 +090061 err = -ENOMEM;
62 pr_debug("Not enough memory to create thread/cpu maps\n");
Adrian Hunterc5e6bd22015-09-08 10:59:02 +030063 goto out_free_maps;
Namhyung Kimbc96b362013-03-18 11:41:47 +090064 }
65
Adrian Hunterc5e6bd22015-09-08 10:59:02 +030066 perf_evlist__set_maps(evlist, cpus, threads);
67
68 cpus = NULL;
69 threads = NULL;
70
Arnaldo Carvalho de Melod0b849e2013-11-11 16:28:42 -030071 if (perf_evlist__open(evlist)) {
Arnaldo Carvalho de Melo67c1e4a2013-11-11 16:33:18 -030072 const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate";
73
Arnaldo Carvalho de Melod0b849e2013-11-11 16:28:42 -030074 err = -errno;
Arnaldo Carvalho de Melo67c1e4a2013-11-11 16:33:18 -030075 pr_debug("Couldn't open evlist: %s\nHint: check %s, using %" PRIu64 " in this test.\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030076 str_error_r(errno, sbuf, sizeof(sbuf)),
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000077 knob, (u64)attr.sample_freq);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -030078 goto out_delete_evlist;
Arnaldo Carvalho de Melod0b849e2013-11-11 16:28:42 -030079 }
Namhyung Kimbc96b362013-03-18 11:41:47 +090080
81 err = perf_evlist__mmap(evlist, 128, true);
82 if (err < 0) {
83 pr_debug("failed to mmap event: %d (%s)\n", errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030084 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -030085 goto out_delete_evlist;
Namhyung Kimbc96b362013-03-18 11:41:47 +090086 }
87
88 perf_evlist__enable(evlist);
89
90 /* collect samples */
91 for (i = 0; i < NR_LOOPS; i++)
92 tmp++;
93
94 perf_evlist__disable(evlist);
95
96 while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
97 struct perf_sample sample;
98
99 if (event->header.type != PERF_RECORD_SAMPLE)
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800100 goto next_event;
Namhyung Kimbc96b362013-03-18 11:41:47 +0900101
102 err = perf_evlist__parse_sample(evlist, event, &sample);
103 if (err < 0) {
104 pr_debug("Error during parse sample\n");
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300105 goto out_delete_evlist;
Namhyung Kimbc96b362013-03-18 11:41:47 +0900106 }
107
108 total_periods += sample.period;
109 nr_samples++;
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800110next_event:
111 perf_evlist__mmap_consume(evlist, 0);
Namhyung Kimbc96b362013-03-18 11:41:47 +0900112 }
113
114 if ((u64) nr_samples == total_periods) {
115 pr_debug("All (%d) samples have period value of 1!\n",
116 nr_samples);
117 err = -1;
118 }
119
Adrian Hunterc5e6bd22015-09-08 10:59:02 +0300120out_free_maps:
121 cpu_map__put(cpus);
122 thread_map__put(threads);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300123out_delete_evlist:
Namhyung Kimbc96b362013-03-18 11:41:47 +0900124 perf_evlist__delete(evlist);
125 return err;
126}
127
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300128int test__sw_clock_freq(struct test *test __maybe_unused, int subtest __maybe_unused)
Namhyung Kimbc96b362013-03-18 11:41:47 +0900129{
130 int ret;
131
132 ret = __test__sw_clock_freq(PERF_COUNT_SW_CPU_CLOCK);
133 if (!ret)
134 ret = __test__sw_clock_freq(PERF_COUNT_SW_TASK_CLOCK);
135
136 return ret;
137}