3 #include <linux/types.h>
6 #include "parse-events.h"
9 #include "thread_map.h"
13 #include "../arch/x86/util/tsc.h"
15 #define CHECK__(x) { \
17 pr_debug(#x " failed!\n"); \
22 #define CHECK_NOT_NULL__(x) { \
23 while ((x) == NULL) { \
24 pr_debug(#x " failed!\n"); \
29 static u64
rdtsc(void)
31 unsigned int low
, high
;
33 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
35 return low
| ((u64
)high
) << 32;
39 * test__perf_time_to_tsc - test converting perf time to TSC.
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.
46 int test__perf_time_to_tsc(void)
48 struct record_opts opts
= {
49 .mmap_pages
= UINT_MAX
,
50 .user_freq
= UINT_MAX
,
51 .user_interval
= ULLONG_MAX
,
58 struct thread_map
*threads
= NULL
;
59 struct cpu_map
*cpus
= NULL
;
60 struct perf_evlist
*evlist
= NULL
;
61 struct perf_evsel
*evsel
= NULL
;
63 const char *comm1
, *comm2
;
64 struct perf_tsc_conversion tc
;
65 struct perf_event_mmap_page
*pc
;
66 union perf_event
*event
;
67 u64 test_tsc
, comm1_tsc
, comm2_tsc
;
68 u64 test_time
, comm1_time
= 0, comm2_time
= 0;
70 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
71 CHECK_NOT_NULL__(threads
);
73 cpus
= cpu_map__new(NULL
);
74 CHECK_NOT_NULL__(cpus
);
76 evlist
= perf_evlist__new();
77 CHECK_NOT_NULL__(evlist
);
79 perf_evlist__set_maps(evlist
, cpus
, threads
);
81 CHECK__(parse_events(evlist
, "cycles:u"));
83 perf_evlist__config(evlist
, &opts
);
85 evsel
= perf_evlist__first(evlist
);
88 evsel
->attr
.disabled
= 1;
89 evsel
->attr
.enable_on_exec
= 0;
91 CHECK__(perf_evlist__open(evlist
));
93 CHECK__(perf_evlist__mmap(evlist
, UINT_MAX
, false));
95 pc
= evlist
->mmap
[0].base
;
96 ret
= perf_read_tsc_conversion(pc
, &tc
);
98 if (ret
== -EOPNOTSUPP
) {
99 fprintf(stderr
, " (not supported)");
105 perf_evlist__enable(evlist
);
107 comm1
= "Test COMM 1";
108 CHECK__(prctl(PR_SET_NAME
, (unsigned long)comm1
, 0, 0, 0));
112 comm2
= "Test COMM 2";
113 CHECK__(prctl(PR_SET_NAME
, (unsigned long)comm2
, 0, 0, 0));
115 perf_evlist__disable(evlist
);
117 for (i
= 0; i
< evlist
->nr_mmaps
; i
++) {
118 while ((event
= perf_evlist__mmap_read(evlist
, i
)) != NULL
) {
119 struct perf_sample sample
;
121 if (event
->header
.type
!= PERF_RECORD_COMM
||
122 (pid_t
)event
->comm
.pid
!= getpid() ||
123 (pid_t
)event
->comm
.tid
!= getpid())
126 if (strcmp(event
->comm
.comm
, comm1
) == 0) {
127 CHECK__(perf_evsel__parse_sample(evsel
, event
,
129 comm1_time
= sample
.time
;
131 if (strcmp(event
->comm
.comm
, comm2
) == 0) {
132 CHECK__(perf_evsel__parse_sample(evsel
, event
,
134 comm2_time
= sample
.time
;
137 perf_evlist__mmap_consume(evlist
, i
);
141 if (!comm1_time
|| !comm2_time
)
144 test_time
= tsc_to_perf_time(test_tsc
, &tc
);
145 comm1_tsc
= perf_time_to_tsc(comm1_time
, &tc
);
146 comm2_tsc
= perf_time_to_tsc(comm2_time
, &tc
);
148 pr_debug("1st event perf time %"PRIu64
" tsc %"PRIu64
"\n",
149 comm1_time
, comm1_tsc
);
150 pr_debug("rdtsc time %"PRIu64
" tsc %"PRIu64
"\n",
151 test_time
, test_tsc
);
152 pr_debug("2nd event perf time %"PRIu64
" tsc %"PRIu64
"\n",
153 comm2_time
, comm2_tsc
);
155 if (test_time
<= comm1_time
||
156 test_time
>= comm2_time
)
159 if (test_tsc
<= comm1_tsc
||
160 test_tsc
>= comm2_tsc
)
167 perf_evlist__disable(evlist
);
168 perf_evlist__delete(evlist
);