blob: 828494db4a190945dd3da7aa0d3ae3a987a52d36 [file] [log] [blame]
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03001#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03002#include <inttypes.h>
Namhyung Kimbc96b362013-03-18 11:41:47 +09003#include <unistd.h>
4#include <stdlib.h>
5#include <signal.h>
6#include <sys/mman.h>
7
8#include "tests.h"
9#include "util/evsel.h"
10#include "util/evlist.h"
11#include "util/cpumap.h"
12#include "util/thread_map.h"
13
Adrian Hunter3fe21302013-11-12 11:45:21 -030014#define NR_LOOPS 10000000
Namhyung Kimbc96b362013-03-18 11:41:47 +090015
16/*
17 * This test will open software clock events (cpu-clock, task-clock)
18 * then check their frequency -> period conversion has no artifact of
19 * setting period to 1 forcefully.
20 */
21static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
22{
23 int i, err = -1;
24 volatile int tmp = 0;
25 u64 total_periods = 0;
26 int nr_samples = 0;
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000027 char sbuf[STRERR_BUFSIZE];
Namhyung Kimbc96b362013-03-18 11:41:47 +090028 union perf_event *event;
29 struct perf_evsel *evsel;
30 struct perf_evlist *evlist;
31 struct perf_event_attr attr = {
32 .type = PERF_TYPE_SOFTWARE,
33 .config = clock_id,
34 .sample_type = PERF_SAMPLE_PERIOD,
35 .exclude_kernel = 1,
36 .disabled = 1,
37 .freq = 1,
38 };
Adrian Hunterc5e6bd22015-09-08 10:59:02 +030039 struct cpu_map *cpus;
40 struct thread_map *threads;
Namhyung Kimbc96b362013-03-18 11:41:47 +090041
Arnaldo Carvalho de Melo67c1e4a2013-11-11 16:33:18 -030042 attr.sample_freq = 500;
Namhyung Kimbc96b362013-03-18 11:41:47 +090043
44 evlist = perf_evlist__new();
45 if (evlist == NULL) {
46 pr_debug("perf_evlist__new\n");
47 return -1;
48 }
49
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -030050 evsel = perf_evsel__new(&attr);
Namhyung Kimbc96b362013-03-18 11:41:47 +090051 if (evsel == NULL) {
52 pr_debug("perf_evsel__new\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -030053 goto out_delete_evlist;
Namhyung Kimbc96b362013-03-18 11:41:47 +090054 }
55 perf_evlist__add(evlist, evsel);
56
Adrian Hunterc5e6bd22015-09-08 10:59:02 +030057 cpus = cpu_map__dummy_new();
58 threads = thread_map__new_by_tid(getpid());
59 if (!cpus || !threads) {
Namhyung Kimbc96b362013-03-18 11:41:47 +090060 err = -ENOMEM;
61 pr_debug("Not enough memory to create thread/cpu maps\n");
Adrian Hunterc5e6bd22015-09-08 10:59:02 +030062 goto out_free_maps;
Namhyung Kimbc96b362013-03-18 11:41:47 +090063 }
64
Adrian Hunterc5e6bd22015-09-08 10:59:02 +030065 perf_evlist__set_maps(evlist, cpus, threads);
66
67 cpus = NULL;
68 threads = NULL;
69
Arnaldo Carvalho de Melod0b849e2013-11-11 16:28:42 -030070 if (perf_evlist__open(evlist)) {
Arnaldo Carvalho de Melo67c1e4a2013-11-11 16:33:18 -030071 const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate";
72
Arnaldo Carvalho de Melod0b849e2013-11-11 16:28:42 -030073 err = -errno;
Arnaldo Carvalho de Melo67c1e4a2013-11-11 16:33:18 -030074 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 -030075 str_error_r(errno, sbuf, sizeof(sbuf)),
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000076 knob, (u64)attr.sample_freq);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -030077 goto out_delete_evlist;
Arnaldo Carvalho de Melod0b849e2013-11-11 16:28:42 -030078 }
Namhyung Kimbc96b362013-03-18 11:41:47 +090079
80 err = perf_evlist__mmap(evlist, 128, true);
81 if (err < 0) {
82 pr_debug("failed to mmap event: %d (%s)\n", errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030083 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -030084 goto out_delete_evlist;
Namhyung Kimbc96b362013-03-18 11:41:47 +090085 }
86
87 perf_evlist__enable(evlist);
88
89 /* collect samples */
90 for (i = 0; i < NR_LOOPS; i++)
91 tmp++;
92
93 perf_evlist__disable(evlist);
94
95 while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
96 struct perf_sample sample;
97
98 if (event->header.type != PERF_RECORD_SAMPLE)
Zhouyi Zhou8e50d382013-10-24 15:43:33 +080099 goto next_event;
Namhyung Kimbc96b362013-03-18 11:41:47 +0900100
101 err = perf_evlist__parse_sample(evlist, event, &sample);
102 if (err < 0) {
103 pr_debug("Error during parse sample\n");
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300104 goto out_delete_evlist;
Namhyung Kimbc96b362013-03-18 11:41:47 +0900105 }
106
107 total_periods += sample.period;
108 nr_samples++;
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800109next_event:
110 perf_evlist__mmap_consume(evlist, 0);
Namhyung Kimbc96b362013-03-18 11:41:47 +0900111 }
112
113 if ((u64) nr_samples == total_periods) {
114 pr_debug("All (%d) samples have period value of 1!\n",
115 nr_samples);
116 err = -1;
117 }
118
Adrian Hunterc5e6bd22015-09-08 10:59:02 +0300119out_free_maps:
120 cpu_map__put(cpus);
121 thread_map__put(threads);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300122out_delete_evlist:
Namhyung Kimbc96b362013-03-18 11:41:47 +0900123 perf_evlist__delete(evlist);
124 return err;
125}
126
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -0300127int test__sw_clock_freq(int subtest __maybe_unused)
Namhyung Kimbc96b362013-03-18 11:41:47 +0900128{
129 int ret;
130
131 ret = __test__sw_clock_freq(PERF_COUNT_SW_CPU_CLOCK);
132 if (!ret)
133 ret = __test__sw_clock_freq(PERF_COUNT_SW_TASK_CLOCK);
134
135 return ret;
136}