blob: 026d32ed078e6e436c743ba008831ed9958ae6a1 [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 Melof2a39fe2019-08-30 14:45:20 -03004#include <limits.h>
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -03005#include <stdbool.h>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +03006#include <stdio.h>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +03007#include <unistd.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +02008#include <linux/types.h>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +03009#include <sys/prctl.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +020010#include <perf/cpumap.h>
Jiri Olsa453fa032019-07-21 13:24:43 +020011#include <perf/evlist.h>
Jiri Olsa7728fa02019-10-07 14:53:17 +020012#include <perf/mmap.h>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030013
Arnaldo Carvalho de Melob4209022019-08-29 15:56:40 -030014#include "debug.h"
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030015#include "parse-events.h"
16#include "evlist.h"
17#include "evsel.h"
18#include "thread_map.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030019#include "record.h"
Adrian Hunter0b437862014-07-14 13:03:03 +030020#include "tsc.h"
Arnaldo Carvalho de Meloe0fcfb02019-09-23 12:20:38 -030021#include "util/mmap.h"
Matt Flemingd8b167f2015-10-05 15:40:20 +010022#include "tests/tests.h"
23
24#include "arch-tests.h"
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030025
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030026#define CHECK__(x) { \
27 while ((x) < 0) { \
28 pr_debug(#x " failed!\n"); \
29 goto out_err; \
30 } \
31}
32
33#define CHECK_NOT_NULL__(x) { \
34 while ((x) == NULL) { \
35 pr_debug(#x " failed!\n"); \
36 goto out_err; \
37 } \
38}
39
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030040/**
41 * test__perf_time_to_tsc - test converting perf time to TSC.
42 *
43 * This function implements a test that checks that the conversion of perf time
44 * to and from TSC is consistent with the order of events. If the test passes
45 * %0 is returned, otherwise %-1 is returned. If TSC conversion is not
46 * supported then then the test passes but " (not supported)" is printed.
47 */
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030048int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest __maybe_unused)
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030049{
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -030050 struct record_opts opts = {
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030051 .mmap_pages = UINT_MAX,
52 .user_freq = UINT_MAX,
53 .user_interval = ULLONG_MAX,
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030054 .target = {
55 .uses_mmap = true,
56 },
57 .sample_time = true,
58 };
Jiri Olsa9749b902019-07-21 13:23:50 +020059 struct perf_thread_map *threads = NULL;
Jiri Olsaf8548392019-07-21 13:23:49 +020060 struct perf_cpu_map *cpus = NULL;
Jiri Olsa63503db2019-07-21 13:23:52 +020061 struct evlist *evlist = NULL;
Jiri Olsa32dcd022019-07-21 13:23:51 +020062 struct evsel *evsel = NULL;
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030063 int err = -1, ret, i;
64 const char *comm1, *comm2;
65 struct perf_tsc_conversion tc;
66 struct perf_event_mmap_page *pc;
67 union perf_event *event;
68 u64 test_tsc, comm1_tsc, comm2_tsc;
69 u64 test_time, comm1_time = 0, comm2_time = 0;
Jiri Olsaa5830532019-07-27 20:30:53 +020070 struct mmap *md;
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030071
72 threads = thread_map__new(-1, getpid(), UINT_MAX);
73 CHECK_NOT_NULL__(threads);
74
Jiri Olsa9c3516d2019-07-21 13:24:30 +020075 cpus = perf_cpu_map__new(NULL);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030076 CHECK_NOT_NULL__(cpus);
77
Jiri Olsa0f98b112019-07-21 13:23:55 +020078 evlist = evlist__new();
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030079 CHECK_NOT_NULL__(evlist);
80
Jiri Olsa453fa032019-07-21 13:24:43 +020081 perf_evlist__set_maps(&evlist->core, cpus, threads);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030082
Jiri Olsab39b8392015-04-22 21:10:16 +020083 CHECK__(parse_events(evlist, "cycles:u", NULL));
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030084
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -030085 perf_evlist__config(evlist, &opts, NULL);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030086
Jiri Olsa515dbe42019-09-03 10:39:52 +020087 evsel = evlist__first(evlist);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030088
Jiri Olsa1fc632c2019-07-21 13:24:29 +020089 evsel->core.attr.comm = 1;
90 evsel->core.attr.disabled = 1;
91 evsel->core.attr.enable_on_exec = 0;
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030092
Jiri Olsa474ddc42019-07-21 13:24:06 +020093 CHECK__(evlist__open(evlist));
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030094
Jiri Olsa9521b5f2019-07-28 12:45:35 +020095 CHECK__(evlist__mmap(evlist, UINT_MAX));
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030096
Jiri Olsa547740f2019-07-27 22:07:44 +020097 pc = evlist->mmap[0].core.base;
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030098 ret = perf_read_tsc_conversion(pc, &tc);
99 if (ret) {
100 if (ret == -EOPNOTSUPP) {
101 fprintf(stderr, " (not supported)");
102 return 0;
103 }
104 goto out_err;
105 }
106
Jiri Olsa1c87f162019-07-21 13:24:08 +0200107 evlist__enable(evlist);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300108
109 comm1 = "Test COMM 1";
110 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0));
111
112 test_tsc = rdtsc();
113
114 comm2 = "Test COMM 2";
115 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm2, 0, 0, 0));
116
Jiri Olsae74676d2019-07-21 13:24:09 +0200117 evlist__disable(evlist);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300118
Jiri Olsac976ee12019-07-30 13:04:59 +0200119 for (i = 0; i < evlist->core.nr_mmaps; i++) {
Kan Liang9dfb85d2018-03-01 18:09:07 -0500120 md = &evlist->mmap[i];
Jiri Olsa7c4d4182019-10-07 14:53:18 +0200121 if (perf_mmap__read_init(&md->core) < 0)
Kan Liang9dfb85d2018-03-01 18:09:07 -0500122 continue;
123
Jiri Olsa151ed5d2019-10-07 14:53:20 +0200124 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300125 struct perf_sample sample;
126
127 if (event->header.type != PERF_RECORD_COMM ||
128 (pid_t)event->comm.pid != getpid() ||
129 (pid_t)event->comm.tid != getpid())
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800130 goto next_event;
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300131
132 if (strcmp(event->comm.comm, comm1) == 0) {
Arnaldo Carvalho de Melo6b6017a2020-04-30 11:03:49 -0300133 CHECK__(evsel__parse_sample(evsel, event, &sample));
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300134 comm1_time = sample.time;
135 }
136 if (strcmp(event->comm.comm, comm2) == 0) {
Arnaldo Carvalho de Melo6b6017a2020-04-30 11:03:49 -0300137 CHECK__(evsel__parse_sample(evsel, event, &sample));
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300138 comm2_time = sample.time;
139 }
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800140next_event:
Jiri Olsa7728fa02019-10-07 14:53:17 +0200141 perf_mmap__consume(&md->core);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300142 }
Jiri Olsa32fdc2c2019-10-07 14:53:19 +0200143 perf_mmap__read_done(&md->core);
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}