1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/types.h>
10 #include <perf/cpumap.h>
11 #include <perf/evlist.h>
12 #include <perf/mmap.h>
15 #include "parse-events.h"
18 #include "thread_map.h"
25 * Except x86_64/i386 and Arm64, other archs don't support TSC in perf. Just
26 * enable the test for x86_64/i386 and Arm64 archs.
28 #if defined(__x86_64__) || defined(__i386__) || defined(__aarch64__)
29 #define TSC_IS_SUPPORTED 1
31 #define TSC_IS_SUPPORTED 0
34 #define CHECK__(x) { \
36 pr_debug(#x " failed!\n"); \
41 #define CHECK_NOT_NULL__(x) { \
42 while ((x) == NULL) { \
43 pr_debug(#x " failed!\n"); \
48 static int test__tsc_is_supported(struct test_suite *test __maybe_unused,
49 int subtest __maybe_unused)
51 if (!TSC_IS_SUPPORTED) {
52 pr_debug("Test not supported on this architecture\n");
60 * test__perf_time_to_tsc - test converting perf time to TSC.
62 * This function implements a test that checks that the conversion of perf time
63 * to and from TSC is consistent with the order of events. If the test passes
64 * %0 is returned, otherwise %-1 is returned. If TSC conversion is not
65 * supported then the test passes but " (not supported)" is printed.
67 static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
69 struct record_opts opts = {
70 .mmap_pages = UINT_MAX,
71 .user_freq = UINT_MAX,
72 .user_interval = ULLONG_MAX,
78 struct perf_thread_map *threads = NULL;
79 struct perf_cpu_map *cpus = NULL;
80 struct evlist *evlist = NULL;
81 struct evsel *evsel = NULL;
82 int err = TEST_FAIL, ret, i;
83 const char *comm1, *comm2;
84 struct perf_tsc_conversion tc;
85 struct perf_event_mmap_page *pc;
86 union perf_event *event;
87 u64 test_tsc, comm1_tsc, comm2_tsc;
88 u64 test_time, comm1_time = 0, comm2_time = 0;
92 threads = thread_map__new(-1, getpid(), UINT_MAX);
93 CHECK_NOT_NULL__(threads);
95 cpus = perf_cpu_map__new(NULL);
96 CHECK_NOT_NULL__(cpus);
98 evlist = evlist__new();
99 CHECK_NOT_NULL__(evlist);
101 perf_evlist__set_maps(&evlist->core, cpus, threads);
103 CHECK__(parse_event(evlist, "cycles:u"));
105 evlist__config(evlist, &opts, NULL);
107 /* For hybrid "cycles:u", it creates two events */
108 evlist__for_each_entry(evlist, evsel) {
109 evsel->core.attr.comm = 1;
110 evsel->core.attr.disabled = 1;
111 evsel->core.attr.enable_on_exec = 0;
114 ret = evlist__open(evlist);
119 pr_debug("evlist__open() failed\n");
123 CHECK__(evlist__mmap(evlist, UINT_MAX));
125 pc = evlist->mmap[0].core.base;
126 ret = perf_read_tsc_conversion(pc, &tc);
128 if (ret == -EOPNOTSUPP) {
129 pr_debug("perf_read_tsc_conversion is not supported in current kernel\n");
135 evlist__enable(evlist);
137 comm1 = "Test COMM 1";
138 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0));
142 comm2 = "Test COMM 2";
143 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm2, 0, 0, 0));
145 evlist__disable(evlist);
147 for (i = 0; i < evlist->core.nr_mmaps; i++) {
148 md = &evlist->mmap[i];
149 if (perf_mmap__read_init(&md->core) < 0)
152 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
153 struct perf_sample sample;
155 if (event->header.type != PERF_RECORD_COMM ||
156 (pid_t)event->comm.pid != getpid() ||
157 (pid_t)event->comm.tid != getpid())
160 if (strcmp(event->comm.comm, comm1) == 0) {
161 CHECK_NOT_NULL__(evsel = evlist__event2evsel(evlist, event));
162 CHECK__(evsel__parse_sample(evsel, event, &sample));
163 comm1_time = sample.time;
165 if (strcmp(event->comm.comm, comm2) == 0) {
166 CHECK_NOT_NULL__(evsel = evlist__event2evsel(evlist, event));
167 CHECK__(evsel__parse_sample(evsel, event, &sample));
168 comm2_time = sample.time;
171 perf_mmap__consume(&md->core);
173 perf_mmap__read_done(&md->core);
176 if (!comm1_time || !comm2_time)
179 test_time = tsc_to_perf_time(test_tsc, &tc);
180 comm1_tsc = perf_time_to_tsc(comm1_time, &tc);
181 comm2_tsc = perf_time_to_tsc(comm2_time, &tc);
183 pr_debug("1st event perf time %"PRIu64" tsc %"PRIu64"\n",
184 comm1_time, comm1_tsc);
185 pr_debug("rdtsc time %"PRIu64" tsc %"PRIu64"\n",
186 test_time, test_tsc);
187 pr_debug("2nd event perf time %"PRIu64" tsc %"PRIu64"\n",
188 comm2_time, comm2_tsc);
190 if (test_time <= comm1_time ||
191 test_time >= comm2_time)
194 if (test_tsc <= comm1_tsc ||
195 test_tsc >= comm2_tsc)
201 evlist__delete(evlist);
202 perf_cpu_map__put(cpus);
203 perf_thread_map__put(threads);
207 static struct test_case time_to_tsc_tests[] = {
208 TEST_CASE_REASON("TSC support", tsc_is_supported,
209 "This architecture does not support"),
210 TEST_CASE_REASON("Perf time to TSC", perf_time_to_tsc,
211 "perf_read_tsc_conversion is not supported"),
215 struct test_suite suite__perf_time_to_tsc = {
216 .desc = "Convert perf time to TSC",
217 .test_cases = time_to_tsc_tests,