x86/xen: resume timer irqs early
[linux/fpc-iii.git] / tools / perf / tests / perf-time-to-tsc.c
blob4ca1b938f6a620380f8cae76435f8ad6b142eecd
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <inttypes.h>
5 #include <sys/prctl.h>
7 #include "parse-events.h"
8 #include "evlist.h"
9 #include "evsel.h"
10 #include "thread_map.h"
11 #include "cpumap.h"
12 #include "tests.h"
14 #include "../arch/x86/util/tsc.h"
16 #define CHECK__(x) { \
17 while ((x) < 0) { \
18 pr_debug(#x " failed!\n"); \
19 goto out_err; \
20 } \
23 #define CHECK_NOT_NULL__(x) { \
24 while ((x) == NULL) { \
25 pr_debug(#x " failed!\n"); \
26 goto out_err; \
27 } \
30 static u64 rdtsc(void)
32 unsigned int low, high;
34 asm volatile("rdtsc" : "=a" (low), "=d" (high));
36 return low | ((u64)high) << 32;
39 /**
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,
53 .freq = 4000,
54 .target = {
55 .uses_mmap = true,
57 .sample_time = true,
59 struct thread_map *threads = NULL;
60 struct cpu_map *cpus = NULL;
61 struct perf_evlist *evlist = NULL;
62 struct perf_evsel *evsel = NULL;
63 int err = -1, ret, i;
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);
88 evsel->attr.comm = 1;
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);
98 if (ret) {
99 if (ret == -EOPNOTSUPP) {
100 fprintf(stderr, " (not supported)");
101 return 0;
103 goto out_err;
106 perf_evlist__enable(evlist);
108 comm1 = "Test COMM 1";
109 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0));
111 test_tsc = rdtsc();
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())
125 goto next_event;
127 if (strcmp(event->comm.comm, comm1) == 0) {
128 CHECK__(perf_evsel__parse_sample(evsel, event,
129 &sample));
130 comm1_time = sample.time;
132 if (strcmp(event->comm.comm, comm2) == 0) {
133 CHECK__(perf_evsel__parse_sample(evsel, event,
134 &sample));
135 comm2_time = sample.time;
137 next_event:
138 perf_evlist__mmap_consume(evlist, i);
142 if (!comm1_time || !comm2_time)
143 goto out_err;
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)
158 goto out_err;
160 if (test_tsc <= comm1_tsc ||
161 test_tsc >= comm2_tsc)
162 goto out_err;
164 err = 0;
166 out_err:
167 if (evlist) {
168 perf_evlist__disable(evlist);
169 perf_evlist__munmap(evlist);
170 perf_evlist__close(evlist);
171 perf_evlist__delete(evlist);
173 if (cpus)
174 cpu_map__delete(cpus);
175 if (threads)
176 thread_map__delete(threads);
178 return err;