3 #include <linux/types.h>
6 #include "parse-events.h"
9 #include "thread_map.h"
14 #define CHECK__(x) { \
16 pr_debug(#x " failed!\n"); \
21 #define CHECK_NOT_NULL__(x) { \
22 while ((x) == NULL) { \
23 pr_debug(#x " failed!\n"); \
29 * test__perf_time_to_tsc - test converting perf time to TSC.
31 * This function implements a test that checks that the conversion of perf time
32 * to and from TSC is consistent with the order of events. If the test passes
33 * %0 is returned, otherwise %-1 is returned. If TSC conversion is not
34 * supported then then the test passes but " (not supported)" is printed.
36 int test__perf_time_to_tsc(void)
38 struct record_opts opts
= {
39 .mmap_pages
= UINT_MAX
,
40 .user_freq
= UINT_MAX
,
41 .user_interval
= ULLONG_MAX
,
48 struct thread_map
*threads
= NULL
;
49 struct cpu_map
*cpus
= NULL
;
50 struct perf_evlist
*evlist
= NULL
;
51 struct perf_evsel
*evsel
= NULL
;
53 const char *comm1
, *comm2
;
54 struct perf_tsc_conversion tc
;
55 struct perf_event_mmap_page
*pc
;
56 union perf_event
*event
;
57 u64 test_tsc
, comm1_tsc
, comm2_tsc
;
58 u64 test_time
, comm1_time
= 0, comm2_time
= 0;
60 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
61 CHECK_NOT_NULL__(threads
);
63 cpus
= cpu_map__new(NULL
);
64 CHECK_NOT_NULL__(cpus
);
66 evlist
= perf_evlist__new();
67 CHECK_NOT_NULL__(evlist
);
69 perf_evlist__set_maps(evlist
, cpus
, threads
);
71 CHECK__(parse_events(evlist
, "cycles:u"));
73 perf_evlist__config(evlist
, &opts
);
75 evsel
= perf_evlist__first(evlist
);
78 evsel
->attr
.disabled
= 1;
79 evsel
->attr
.enable_on_exec
= 0;
81 CHECK__(perf_evlist__open(evlist
));
83 CHECK__(perf_evlist__mmap(evlist
, UINT_MAX
, false));
85 pc
= evlist
->mmap
[0].base
;
86 ret
= perf_read_tsc_conversion(pc
, &tc
);
88 if (ret
== -EOPNOTSUPP
) {
89 fprintf(stderr
, " (not supported)");
95 perf_evlist__enable(evlist
);
97 comm1
= "Test COMM 1";
98 CHECK__(prctl(PR_SET_NAME
, (unsigned long)comm1
, 0, 0, 0));
102 comm2
= "Test COMM 2";
103 CHECK__(prctl(PR_SET_NAME
, (unsigned long)comm2
, 0, 0, 0));
105 perf_evlist__disable(evlist
);
107 for (i
= 0; i
< evlist
->nr_mmaps
; i
++) {
108 while ((event
= perf_evlist__mmap_read(evlist
, i
)) != NULL
) {
109 struct perf_sample sample
;
111 if (event
->header
.type
!= PERF_RECORD_COMM
||
112 (pid_t
)event
->comm
.pid
!= getpid() ||
113 (pid_t
)event
->comm
.tid
!= getpid())
116 if (strcmp(event
->comm
.comm
, comm1
) == 0) {
117 CHECK__(perf_evsel__parse_sample(evsel
, event
,
119 comm1_time
= sample
.time
;
121 if (strcmp(event
->comm
.comm
, comm2
) == 0) {
122 CHECK__(perf_evsel__parse_sample(evsel
, event
,
124 comm2_time
= sample
.time
;
127 perf_evlist__mmap_consume(evlist
, i
);
131 if (!comm1_time
|| !comm2_time
)
134 test_time
= tsc_to_perf_time(test_tsc
, &tc
);
135 comm1_tsc
= perf_time_to_tsc(comm1_time
, &tc
);
136 comm2_tsc
= perf_time_to_tsc(comm2_time
, &tc
);
138 pr_debug("1st event perf time %"PRIu64
" tsc %"PRIu64
"\n",
139 comm1_time
, comm1_tsc
);
140 pr_debug("rdtsc time %"PRIu64
" tsc %"PRIu64
"\n",
141 test_time
, test_tsc
);
142 pr_debug("2nd event perf time %"PRIu64
" tsc %"PRIu64
"\n",
143 comm2_time
, comm2_tsc
);
145 if (test_time
<= comm1_time
||
146 test_time
>= comm2_time
)
149 if (test_tsc
<= comm1_tsc
||
150 test_tsc
>= comm2_tsc
)
157 perf_evlist__disable(evlist
);
158 perf_evlist__delete(evlist
);