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"
23 #include "util/sample.h"
26 * Except x86_64/i386 and Arm64, other archs don't support TSC in perf. Just
27 * enable the test for x86_64/i386 and Arm64 archs.
29 #if defined(__x86_64__) || defined(__i386__) || defined(__aarch64__)
30 #define TSC_IS_SUPPORTED 1
32 #define TSC_IS_SUPPORTED 0
35 #define CHECK__(x) { \
37 pr_debug(#x " failed!\n"); \
42 #define CHECK_NOT_NULL__(x) { \
43 while ((x) == NULL) { \
44 pr_debug(#x " failed!\n"); \
49 static int test__tsc_is_supported(struct test_suite
*test __maybe_unused
,
50 int subtest __maybe_unused
)
52 if (!TSC_IS_SUPPORTED
) {
53 pr_debug("Test not supported on this architecture\n");
61 * test__perf_time_to_tsc - test converting perf time to TSC.
63 * This function implements a test that checks that the conversion of perf time
64 * to and from TSC is consistent with the order of events. If the test passes
65 * %0 is returned, otherwise %-1 is returned. If TSC conversion is not
66 * supported then the test passes but " (not supported)" is printed.
68 static int test__perf_time_to_tsc(struct test_suite
*test __maybe_unused
, int subtest __maybe_unused
)
70 struct record_opts opts
= {
71 .mmap_pages
= UINT_MAX
,
72 .user_freq
= UINT_MAX
,
73 .user_interval
= ULLONG_MAX
,
79 struct perf_thread_map
*threads
= NULL
;
80 struct perf_cpu_map
*cpus
= NULL
;
81 struct evlist
*evlist
= NULL
;
82 struct evsel
*evsel
= NULL
;
83 int err
= TEST_FAIL
, ret
, i
;
84 const char *comm1
, *comm2
;
85 struct perf_tsc_conversion tc
;
86 struct perf_event_mmap_page
*pc
;
87 union perf_event
*event
;
88 u64 test_tsc
, comm1_tsc
, comm2_tsc
;
89 u64 test_time
, comm1_time
= 0, comm2_time
= 0;
93 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
94 CHECK_NOT_NULL__(threads
);
96 cpus
= perf_cpu_map__new_online_cpus();
97 CHECK_NOT_NULL__(cpus
);
99 evlist
= evlist__new();
100 CHECK_NOT_NULL__(evlist
);
102 perf_evlist__set_maps(&evlist
->core
, cpus
, threads
);
104 CHECK__(parse_event(evlist
, "cycles:u"));
106 evlist__config(evlist
, &opts
, NULL
);
108 /* For hybrid "cycles:u", it creates two events */
109 evlist__for_each_entry(evlist
, evsel
) {
110 evsel
->core
.attr
.comm
= 1;
111 evsel
->core
.attr
.disabled
= 1;
112 evsel
->core
.attr
.enable_on_exec
= 0;
115 ret
= evlist__open(evlist
);
120 pr_debug("evlist__open() failed\n");
124 CHECK__(evlist__mmap(evlist
, UINT_MAX
));
126 pc
= evlist
->mmap
[0].core
.base
;
127 ret
= perf_read_tsc_conversion(pc
, &tc
);
129 if (ret
== -EOPNOTSUPP
) {
130 pr_debug("perf_read_tsc_conversion is not supported in current kernel\n");
136 evlist__enable(evlist
);
138 comm1
= "Test COMM 1";
139 CHECK__(prctl(PR_SET_NAME
, (unsigned long)comm1
, 0, 0, 0));
143 comm2
= "Test COMM 2";
144 CHECK__(prctl(PR_SET_NAME
, (unsigned long)comm2
, 0, 0, 0));
146 evlist__disable(evlist
);
148 for (i
= 0; i
< evlist
->core
.nr_mmaps
; i
++) {
149 md
= &evlist
->mmap
[i
];
150 if (perf_mmap__read_init(&md
->core
) < 0)
153 while ((event
= perf_mmap__read_event(&md
->core
)) != NULL
) {
154 struct perf_sample sample
;
156 if (event
->header
.type
!= PERF_RECORD_COMM
||
157 (pid_t
)event
->comm
.pid
!= getpid() ||
158 (pid_t
)event
->comm
.tid
!= getpid())
161 if (strcmp(event
->comm
.comm
, comm1
) == 0) {
162 CHECK_NOT_NULL__(evsel
= evlist__event2evsel(evlist
, event
));
163 CHECK__(evsel__parse_sample(evsel
, event
, &sample
));
164 comm1_time
= sample
.time
;
166 if (strcmp(event
->comm
.comm
, comm2
) == 0) {
167 CHECK_NOT_NULL__(evsel
= evlist__event2evsel(evlist
, event
));
168 CHECK__(evsel__parse_sample(evsel
, event
, &sample
));
169 comm2_time
= sample
.time
;
172 perf_mmap__consume(&md
->core
);
174 perf_mmap__read_done(&md
->core
);
177 if (!comm1_time
|| !comm2_time
)
180 test_time
= tsc_to_perf_time(test_tsc
, &tc
);
181 comm1_tsc
= perf_time_to_tsc(comm1_time
, &tc
);
182 comm2_tsc
= perf_time_to_tsc(comm2_time
, &tc
);
184 pr_debug("1st event perf time %"PRIu64
" tsc %"PRIu64
"\n",
185 comm1_time
, comm1_tsc
);
186 pr_debug("rdtsc time %"PRIu64
" tsc %"PRIu64
"\n",
187 test_time
, test_tsc
);
188 pr_debug("2nd event perf time %"PRIu64
" tsc %"PRIu64
"\n",
189 comm2_time
, comm2_tsc
);
191 if (test_time
<= comm1_time
||
192 test_time
>= comm2_time
)
195 if (test_tsc
<= comm1_tsc
||
196 test_tsc
>= comm2_tsc
)
202 evlist__delete(evlist
);
203 perf_cpu_map__put(cpus
);
204 perf_thread_map__put(threads
);
208 static struct test_case time_to_tsc_tests
[] = {
209 TEST_CASE_REASON("TSC support", tsc_is_supported
,
210 "This architecture does not support"),
211 TEST_CASE_REASON("Perf time to TSC", perf_time_to_tsc
,
212 "perf_read_tsc_conversion is not supported"),
216 struct test_suite suite__perf_time_to_tsc
= {
217 .desc
= "Convert perf time to TSC",
218 .test_cases
= time_to_tsc_tests
,