7 #include "parse-events.h"
10 #include "thread_map.h"
14 #include "../arch/x86/util/tsc.h"
16 #define CHECK__(x) { \
18 pr_debug(#x " failed!\n"); \
23 #define CHECK_NOT_NULL__(x) { \
24 while ((x) == NULL) { \
25 pr_debug(#x " failed!\n"); \
30 static u64
rdtsc(void)
32 unsigned int low
, high
;
34 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
36 return low
| ((u64
)high
) << 32;
40 * test__perf_time_to_tsc - test converting perf time to TSC.
42 * This function implements a test that checks that the conversion of perf time
43 * to and from TSC is consistent with the order of events. If the test passes
44 * %0 is returned, otherwise %-1 is returned. If TSC conversion is not
45 * supported then then the test passes but " (not supported)" is printed.
47 int test__perf_time_to_tsc(void)
49 struct perf_record_opts opts
= {
50 .mmap_pages
= UINT_MAX
,
51 .user_freq
= UINT_MAX
,
52 .user_interval
= ULLONG_MAX
,
59 struct thread_map
*threads
= NULL
;
60 struct cpu_map
*cpus
= NULL
;
61 struct perf_evlist
*evlist
= NULL
;
62 struct perf_evsel
*evsel
= NULL
;
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;
71 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
72 CHECK_NOT_NULL__(threads
);
74 cpus
= cpu_map__new(NULL
);
75 CHECK_NOT_NULL__(cpus
);
77 evlist
= perf_evlist__new();
78 CHECK_NOT_NULL__(evlist
);
80 perf_evlist__set_maps(evlist
, cpus
, threads
);
82 CHECK__(parse_events(evlist
, "cycles:u"));
84 perf_evlist__config(evlist
, &opts
);
86 evsel
= perf_evlist__first(evlist
);
89 evsel
->attr
.disabled
= 1;
90 evsel
->attr
.enable_on_exec
= 0;
92 CHECK__(perf_evlist__open(evlist
));
94 CHECK__(perf_evlist__mmap(evlist
, UINT_MAX
, false));
96 pc
= evlist
->mmap
[0].base
;
97 ret
= perf_read_tsc_conversion(pc
, &tc
);
99 if (ret
== -EOPNOTSUPP
) {
100 fprintf(stderr
, " (not supported)");
106 perf_evlist__enable(evlist
);
108 comm1
= "Test COMM 1";
109 CHECK__(prctl(PR_SET_NAME
, (unsigned long)comm1
, 0, 0, 0));
113 comm2
= "Test COMM 2";
114 CHECK__(prctl(PR_SET_NAME
, (unsigned long)comm2
, 0, 0, 0));
116 perf_evlist__disable(evlist
);
118 for (i
= 0; i
< evlist
->nr_mmaps
; i
++) {
119 while ((event
= perf_evlist__mmap_read(evlist
, i
)) != NULL
) {
120 struct perf_sample sample
;
122 if (event
->header
.type
!= PERF_RECORD_COMM
||
123 (pid_t
)event
->comm
.pid
!= getpid() ||
124 (pid_t
)event
->comm
.tid
!= getpid())
127 if (strcmp(event
->comm
.comm
, comm1
) == 0) {
128 CHECK__(perf_evsel__parse_sample(evsel
, event
,
130 comm1_time
= sample
.time
;
132 if (strcmp(event
->comm
.comm
, comm2
) == 0) {
133 CHECK__(perf_evsel__parse_sample(evsel
, event
,
135 comm2_time
= sample
.time
;
138 perf_evlist__mmap_consume(evlist
, i
);
142 if (!comm1_time
|| !comm2_time
)
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
);
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
);
156 if (test_time
<= comm1_time
||
157 test_time
>= comm2_time
)
160 if (test_tsc
<= comm1_tsc
||
161 test_tsc
>= comm2_tsc
)
168 perf_evlist__disable(evlist
);
169 perf_evlist__munmap(evlist
);
170 perf_evlist__close(evlist
);
171 perf_evlist__delete(evlist
);
174 cpu_map__delete(cpus
);
176 thread_map__delete(threads
);