blob: 2d1f4713b7282bee432d61836c80f932304a7a74 [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>
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -03004#include <stdbool.h>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +03005#include <stdio.h>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +03006#include <unistd.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +02007#include <linux/types.h>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +03008#include <sys/prctl.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +02009#include <perf/cpumap.h>
Jiri Olsa453fa032019-07-21 13:24:43 +020010#include <perf/evlist.h>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030011
Arnaldo Carvalho de Melob4209022019-08-29 15:56:40 -030012#include "debug.h"
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030013#include "parse-events.h"
14#include "evlist.h"
15#include "evsel.h"
16#include "thread_map.h"
17#include "cpumap.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030018#include "record.h"
Adrian Hunter0b437862014-07-14 13:03:03 +030019#include "tsc.h"
Matt Flemingd8b167f2015-10-05 15:40:20 +010020#include "tests/tests.h"
21
22#include "arch-tests.h"
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030023
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030024#define CHECK__(x) { \
25 while ((x) < 0) { \
26 pr_debug(#x " failed!\n"); \
27 goto out_err; \
28 } \
29}
30
31#define CHECK_NOT_NULL__(x) { \
32 while ((x) == NULL) { \
33 pr_debug(#x " failed!\n"); \
34 goto out_err; \
35 } \
36}
37
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030038/**
39 * test__perf_time_to_tsc - test converting perf time to TSC.
40 *
41 * This function implements a test that checks that the conversion of perf time
42 * to and from TSC is consistent with the order of events. If the test passes
43 * %0 is returned, otherwise %-1 is returned. If TSC conversion is not
44 * supported then then the test passes but " (not supported)" is printed.
45 */
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030046int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest __maybe_unused)
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030047{
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -030048 struct record_opts opts = {
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030049 .mmap_pages = UINT_MAX,
50 .user_freq = UINT_MAX,
51 .user_interval = ULLONG_MAX,
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030052 .target = {
53 .uses_mmap = true,
54 },
55 .sample_time = true,
56 };
Jiri Olsa9749b902019-07-21 13:23:50 +020057 struct perf_thread_map *threads = NULL;
Jiri Olsaf8548392019-07-21 13:23:49 +020058 struct perf_cpu_map *cpus = NULL;
Jiri Olsa63503db2019-07-21 13:23:52 +020059 struct evlist *evlist = NULL;
Jiri Olsa32dcd022019-07-21 13:23:51 +020060 struct evsel *evsel = NULL;
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030061 int err = -1, ret, i;
62 const char *comm1, *comm2;
63 struct perf_tsc_conversion tc;
64 struct perf_event_mmap_page *pc;
65 union perf_event *event;
66 u64 test_tsc, comm1_tsc, comm2_tsc;
67 u64 test_time, comm1_time = 0, comm2_time = 0;
Kan Liang9dfb85d2018-03-01 18:09:07 -050068 struct perf_mmap *md;
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030069
70 threads = thread_map__new(-1, getpid(), UINT_MAX);
71 CHECK_NOT_NULL__(threads);
72
Jiri Olsa9c3516d2019-07-21 13:24:30 +020073 cpus = perf_cpu_map__new(NULL);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030074 CHECK_NOT_NULL__(cpus);
75
Jiri Olsa0f98b112019-07-21 13:23:55 +020076 evlist = evlist__new();
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030077 CHECK_NOT_NULL__(evlist);
78
Jiri Olsa453fa032019-07-21 13:24:43 +020079 perf_evlist__set_maps(&evlist->core, cpus, threads);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030080
Jiri Olsab39b8392015-04-22 21:10:16 +020081 CHECK__(parse_events(evlist, "cycles:u", NULL));
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030082
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -030083 perf_evlist__config(evlist, &opts, NULL);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030084
85 evsel = perf_evlist__first(evlist);
86
Jiri Olsa1fc632c2019-07-21 13:24:29 +020087 evsel->core.attr.comm = 1;
88 evsel->core.attr.disabled = 1;
89 evsel->core.attr.enable_on_exec = 0;
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030090
Jiri Olsa474ddc42019-07-21 13:24:06 +020091 CHECK__(evlist__open(evlist));
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030092
Wang Nanf74b9d3a2017-12-03 02:00:37 +000093 CHECK__(perf_evlist__mmap(evlist, UINT_MAX));
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030094
95 pc = evlist->mmap[0].base;
96 ret = perf_read_tsc_conversion(pc, &tc);
97 if (ret) {
98 if (ret == -EOPNOTSUPP) {
99 fprintf(stderr, " (not supported)");
100 return 0;
101 }
102 goto out_err;
103 }
104
Jiri Olsa1c87f162019-07-21 13:24:08 +0200105 evlist__enable(evlist);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300106
107 comm1 = "Test COMM 1";
108 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0));
109
110 test_tsc = rdtsc();
111
112 comm2 = "Test COMM 2";
113 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm2, 0, 0, 0));
114
Jiri Olsae74676d2019-07-21 13:24:09 +0200115 evlist__disable(evlist);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300116
117 for (i = 0; i < evlist->nr_mmaps; i++) {
Kan Liang9dfb85d2018-03-01 18:09:07 -0500118 md = &evlist->mmap[i];
Kan Liangb9bae2c2018-03-06 10:36:07 -0500119 if (perf_mmap__read_init(md) < 0)
Kan Liang9dfb85d2018-03-01 18:09:07 -0500120 continue;
121
Kan Liang0019dc872018-03-06 10:36:06 -0500122 while ((event = perf_mmap__read_event(md)) != NULL) {
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300123 struct perf_sample sample;
124
125 if (event->header.type != PERF_RECORD_COMM ||
126 (pid_t)event->comm.pid != getpid() ||
127 (pid_t)event->comm.tid != getpid())
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800128 goto next_event;
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300129
130 if (strcmp(event->comm.comm, comm1) == 0) {
131 CHECK__(perf_evsel__parse_sample(evsel, event,
132 &sample));
133 comm1_time = sample.time;
134 }
135 if (strcmp(event->comm.comm, comm2) == 0) {
136 CHECK__(perf_evsel__parse_sample(evsel, event,
137 &sample));
138 comm2_time = sample.time;
139 }
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800140next_event:
Kan Liangd6ace3d2018-03-06 10:36:05 -0500141 perf_mmap__consume(md);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300142 }
Kan Liang9dfb85d2018-03-01 18:09:07 -0500143 perf_mmap__read_done(md);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300144 }
145
146 if (!comm1_time || !comm2_time)
147 goto out_err;
148
149 test_time = tsc_to_perf_time(test_tsc, &tc);
150 comm1_tsc = perf_time_to_tsc(comm1_time, &tc);
151 comm2_tsc = perf_time_to_tsc(comm2_time, &tc);
152
153 pr_debug("1st event perf time %"PRIu64" tsc %"PRIu64"\n",
154 comm1_time, comm1_tsc);
155 pr_debug("rdtsc time %"PRIu64" tsc %"PRIu64"\n",
156 test_time, test_tsc);
157 pr_debug("2nd event perf time %"PRIu64" tsc %"PRIu64"\n",
158 comm2_time, comm2_tsc);
159
160 if (test_time <= comm1_time ||
161 test_time >= comm2_time)
162 goto out_err;
163
164 if (test_tsc <= comm1_tsc ||
165 test_tsc >= comm2_tsc)
166 goto out_err;
167
168 err = 0;
169
170out_err:
Jiri Olsac12995a2019-07-21 13:23:56 +0200171 evlist__delete(evlist);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300172 return err;
173}