blob: a9bc77df6a652b3d9681522a4a9579bf905258e8 [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>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +03004#include <stdio.h>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +03005#include <unistd.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +02006#include <linux/types.h>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +03007#include <sys/prctl.h>
8
9#include "parse-events.h"
10#include "evlist.h"
11#include "evsel.h"
12#include "thread_map.h"
13#include "cpumap.h"
Adrian Hunter0b437862014-07-14 13:03:03 +030014#include "tsc.h"
Matt Flemingd8b167f2015-10-05 15:40:20 +010015#include "tests/tests.h"
16
17#include "arch-tests.h"
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030018
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030019#define CHECK__(x) { \
20 while ((x) < 0) { \
21 pr_debug(#x " failed!\n"); \
22 goto out_err; \
23 } \
24}
25
26#define CHECK_NOT_NULL__(x) { \
27 while ((x) == NULL) { \
28 pr_debug(#x " failed!\n"); \
29 goto out_err; \
30 } \
31}
32
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030033/**
34 * test__perf_time_to_tsc - test converting perf time to TSC.
35 *
36 * This function implements a test that checks that the conversion of perf time
37 * to and from TSC is consistent with the order of events. If the test passes
38 * %0 is returned, otherwise %-1 is returned. If TSC conversion is not
39 * supported then then the test passes but " (not supported)" is printed.
40 */
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030041int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest __maybe_unused)
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030042{
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -030043 struct record_opts opts = {
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030044 .mmap_pages = UINT_MAX,
45 .user_freq = UINT_MAX,
46 .user_interval = ULLONG_MAX,
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030047 .target = {
48 .uses_mmap = true,
49 },
50 .sample_time = true,
51 };
52 struct thread_map *threads = NULL;
53 struct cpu_map *cpus = NULL;
54 struct perf_evlist *evlist = NULL;
55 struct perf_evsel *evsel = NULL;
56 int err = -1, ret, i;
57 const char *comm1, *comm2;
58 struct perf_tsc_conversion tc;
59 struct perf_event_mmap_page *pc;
60 union perf_event *event;
61 u64 test_tsc, comm1_tsc, comm2_tsc;
62 u64 test_time, comm1_time = 0, comm2_time = 0;
Kan Liang9dfb85d2018-03-01 18:09:07 -050063 struct perf_mmap *md;
64 u64 end, start;
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030065
66 threads = thread_map__new(-1, getpid(), UINT_MAX);
67 CHECK_NOT_NULL__(threads);
68
69 cpus = cpu_map__new(NULL);
70 CHECK_NOT_NULL__(cpus);
71
72 evlist = perf_evlist__new();
73 CHECK_NOT_NULL__(evlist);
74
75 perf_evlist__set_maps(evlist, cpus, threads);
76
Jiri Olsab39b8392015-04-22 21:10:16 +020077 CHECK__(parse_events(evlist, "cycles:u", NULL));
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030078
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -030079 perf_evlist__config(evlist, &opts, NULL);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030080
81 evsel = perf_evlist__first(evlist);
82
83 evsel->attr.comm = 1;
84 evsel->attr.disabled = 1;
85 evsel->attr.enable_on_exec = 0;
86
87 CHECK__(perf_evlist__open(evlist));
88
Wang Nanf74b9d3a2017-12-03 02:00:37 +000089 CHECK__(perf_evlist__mmap(evlist, UINT_MAX));
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030090
91 pc = evlist->mmap[0].base;
92 ret = perf_read_tsc_conversion(pc, &tc);
93 if (ret) {
94 if (ret == -EOPNOTSUPP) {
95 fprintf(stderr, " (not supported)");
96 return 0;
97 }
98 goto out_err;
99 }
100
101 perf_evlist__enable(evlist);
102
103 comm1 = "Test COMM 1";
104 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0));
105
106 test_tsc = rdtsc();
107
108 comm2 = "Test COMM 2";
109 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm2, 0, 0, 0));
110
111 perf_evlist__disable(evlist);
112
113 for (i = 0; i < evlist->nr_mmaps; i++) {
Kan Liang9dfb85d2018-03-01 18:09:07 -0500114 md = &evlist->mmap[i];
115 if (perf_mmap__read_init(md, false, &start, &end) < 0)
116 continue;
117
118 while ((event = perf_mmap__read_event(md, false, &start, end)) != NULL) {
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300119 struct perf_sample sample;
120
121 if (event->header.type != PERF_RECORD_COMM ||
122 (pid_t)event->comm.pid != getpid() ||
123 (pid_t)event->comm.tid != getpid())
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800124 goto next_event;
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300125
126 if (strcmp(event->comm.comm, comm1) == 0) {
127 CHECK__(perf_evsel__parse_sample(evsel, event,
128 &sample));
129 comm1_time = sample.time;
130 }
131 if (strcmp(event->comm.comm, comm2) == 0) {
132 CHECK__(perf_evsel__parse_sample(evsel, event,
133 &sample));
134 comm2_time = sample.time;
135 }
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800136next_event:
Kan Liangd6ace3d2018-03-06 10:36:05 -0500137 perf_mmap__consume(md);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300138 }
Kan Liang9dfb85d2018-03-01 18:09:07 -0500139 perf_mmap__read_done(md);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300140 }
141
142 if (!comm1_time || !comm2_time)
143 goto out_err;
144
145 test_time = tsc_to_perf_time(test_tsc, &tc);
146 comm1_tsc = perf_time_to_tsc(comm1_time, &tc);
147 comm2_tsc = perf_time_to_tsc(comm2_time, &tc);
148
149 pr_debug("1st event perf time %"PRIu64" tsc %"PRIu64"\n",
150 comm1_time, comm1_tsc);
151 pr_debug("rdtsc time %"PRIu64" tsc %"PRIu64"\n",
152 test_time, test_tsc);
153 pr_debug("2nd event perf time %"PRIu64" tsc %"PRIu64"\n",
154 comm2_time, comm2_tsc);
155
156 if (test_time <= comm1_time ||
157 test_time >= comm2_time)
158 goto out_err;
159
160 if (test_tsc <= comm1_tsc ||
161 test_tsc >= comm2_tsc)
162 goto out_err;
163
164 err = 0;
165
166out_err:
Arnaldo Carvalho de Melo61b3f662016-06-22 10:10:52 -0300167 perf_evlist__delete(evlist);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300168 return err;
169}