blob: 403f2d9cee9641adae0057068957ff1f8cd56918 [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;
Kan Liang5d0007c2018-03-01 18:09:08 -050042 struct perf_mmap *md;
43 u64 end, start;
Namhyung Kimbc96b362013-03-18 11:41:47 +090044
Arnaldo Carvalho de Melo67c1e4a2013-11-11 16:33:18 -030045 attr.sample_freq = 500;
Namhyung Kimbc96b362013-03-18 11:41:47 +090046
47 evlist = perf_evlist__new();
48 if (evlist == NULL) {
49 pr_debug("perf_evlist__new\n");
50 return -1;
51 }
52
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -030053 evsel = perf_evsel__new(&attr);
Namhyung Kimbc96b362013-03-18 11:41:47 +090054 if (evsel == NULL) {
55 pr_debug("perf_evsel__new\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -030056 goto out_delete_evlist;
Namhyung Kimbc96b362013-03-18 11:41:47 +090057 }
58 perf_evlist__add(evlist, evsel);
59
Adrian Hunterc5e6bd22015-09-08 10:59:02 +030060 cpus = cpu_map__dummy_new();
61 threads = thread_map__new_by_tid(getpid());
62 if (!cpus || !threads) {
Namhyung Kimbc96b362013-03-18 11:41:47 +090063 err = -ENOMEM;
64 pr_debug("Not enough memory to create thread/cpu maps\n");
Adrian Hunterc5e6bd22015-09-08 10:59:02 +030065 goto out_free_maps;
Namhyung Kimbc96b362013-03-18 11:41:47 +090066 }
67
Adrian Hunterc5e6bd22015-09-08 10:59:02 +030068 perf_evlist__set_maps(evlist, cpus, threads);
69
70 cpus = NULL;
71 threads = NULL;
72
Arnaldo Carvalho de Melod0b849e2013-11-11 16:28:42 -030073 if (perf_evlist__open(evlist)) {
Arnaldo Carvalho de Melo67c1e4a2013-11-11 16:33:18 -030074 const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate";
75
Arnaldo Carvalho de Melod0b849e2013-11-11 16:28:42 -030076 err = -errno;
Arnaldo Carvalho de Melo67c1e4a2013-11-11 16:33:18 -030077 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 -030078 str_error_r(errno, sbuf, sizeof(sbuf)),
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000079 knob, (u64)attr.sample_freq);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -030080 goto out_delete_evlist;
Arnaldo Carvalho de Melod0b849e2013-11-11 16:28:42 -030081 }
Namhyung Kimbc96b362013-03-18 11:41:47 +090082
Wang Nanf74b9d3a2017-12-03 02:00:37 +000083 err = perf_evlist__mmap(evlist, 128);
Namhyung Kimbc96b362013-03-18 11:41:47 +090084 if (err < 0) {
85 pr_debug("failed to mmap event: %d (%s)\n", errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030086 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -030087 goto out_delete_evlist;
Namhyung Kimbc96b362013-03-18 11:41:47 +090088 }
89
90 perf_evlist__enable(evlist);
91
92 /* collect samples */
93 for (i = 0; i < NR_LOOPS; i++)
94 tmp++;
95
96 perf_evlist__disable(evlist);
97
Kan Liang5d0007c2018-03-01 18:09:08 -050098 md = &evlist->mmap[0];
99 if (perf_mmap__read_init(md, false, &start, &end) < 0)
100 goto out_init;
101
Kan Liang0019dc872018-03-06 10:36:06 -0500102 while ((event = perf_mmap__read_event(md)) != NULL) {
Namhyung Kimbc96b362013-03-18 11:41:47 +0900103 struct perf_sample sample;
104
105 if (event->header.type != PERF_RECORD_SAMPLE)
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800106 goto next_event;
Namhyung Kimbc96b362013-03-18 11:41:47 +0900107
108 err = perf_evlist__parse_sample(evlist, event, &sample);
109 if (err < 0) {
110 pr_debug("Error during parse sample\n");
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300111 goto out_delete_evlist;
Namhyung Kimbc96b362013-03-18 11:41:47 +0900112 }
113
114 total_periods += sample.period;
115 nr_samples++;
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800116next_event:
Kan Liangd6ace3d2018-03-06 10:36:05 -0500117 perf_mmap__consume(md);
Namhyung Kimbc96b362013-03-18 11:41:47 +0900118 }
Kan Liang5d0007c2018-03-01 18:09:08 -0500119 perf_mmap__read_done(md);
Namhyung Kimbc96b362013-03-18 11:41:47 +0900120
Kan Liang5d0007c2018-03-01 18:09:08 -0500121out_init:
Namhyung Kimbc96b362013-03-18 11:41:47 +0900122 if ((u64) nr_samples == total_periods) {
123 pr_debug("All (%d) samples have period value of 1!\n",
124 nr_samples);
125 err = -1;
126 }
127
Adrian Hunterc5e6bd22015-09-08 10:59:02 +0300128out_free_maps:
129 cpu_map__put(cpus);
130 thread_map__put(threads);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300131out_delete_evlist:
Namhyung Kimbc96b362013-03-18 11:41:47 +0900132 perf_evlist__delete(evlist);
133 return err;
134}
135
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300136int test__sw_clock_freq(struct test *test __maybe_unused, int subtest __maybe_unused)
Namhyung Kimbc96b362013-03-18 11:41:47 +0900137{
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}