diff options
author | Adrian Hunter <adrian.hunter@intel.com> | 2013-06-28 09:22:19 -0400 |
---|---|---|
committer | Ingo Molnar <mingo@kernel.org> | 2013-07-23 06:17:59 -0400 |
commit | 3bd5a5fc8c6b9fe769777abf74b0ab5fbd7930b4 (patch) | |
tree | ac509b983fe147d6706449c3bc14031591807585 /tools/perf/tests/perf-time-to-tsc.c | |
parent | c73deb6aecda2955716f31572516f09d930ef450 (diff) |
perf tools: Add test for converting perf time to/from TSC
The test uses the newly added cap_usr_time_zero and time_zero of
perf_event_mmap_page. TSC from rdtsc is compared with the time
from 2 perf events. The test passes if the calculated times are
all in the correct order.
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Link: http://lkml.kernel.org/r/1372425741-1676-4-git-send-email-adrian.hunter@intel.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'tools/perf/tests/perf-time-to-tsc.c')
-rw-r--r-- | tools/perf/tests/perf-time-to-tsc.c | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/tools/perf/tests/perf-time-to-tsc.c b/tools/perf/tests/perf-time-to-tsc.c new file mode 100644 index 000000000000..0ab61b1f408e --- /dev/null +++ b/tools/perf/tests/perf-time-to-tsc.c | |||
@@ -0,0 +1,177 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <sys/types.h> | ||
3 | #include <unistd.h> | ||
4 | #include <inttypes.h> | ||
5 | #include <sys/prctl.h> | ||
6 | |||
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" | ||
13 | |||
14 | #include "../arch/x86/util/tsc.h" | ||
15 | |||
16 | #define CHECK__(x) { \ | ||
17 | while ((x) < 0) { \ | ||
18 | pr_debug(#x " failed!\n"); \ | ||
19 | goto out_err; \ | ||
20 | } \ | ||
21 | } | ||
22 | |||
23 | #define CHECK_NOT_NULL__(x) { \ | ||
24 | while ((x) == NULL) { \ | ||
25 | pr_debug(#x " failed!\n"); \ | ||
26 | goto out_err; \ | ||
27 | } \ | ||
28 | } | ||
29 | |||
30 | static u64 rdtsc(void) | ||
31 | { | ||
32 | unsigned int low, high; | ||
33 | |||
34 | asm volatile("rdtsc" : "=a" (low), "=d" (high)); | ||
35 | |||
36 | return low | ((u64)high) << 32; | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * test__perf_time_to_tsc - test converting perf time to TSC. | ||
41 | * | ||
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. | ||
46 | */ | ||
47 | int test__perf_time_to_tsc(void) | ||
48 | { | ||
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, | ||
56 | }, | ||
57 | .sample_time = true, | ||
58 | }; | ||
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; | ||
70 | |||
71 | threads = thread_map__new(-1, getpid(), UINT_MAX); | ||
72 | CHECK_NOT_NULL__(threads); | ||
73 | |||
74 | cpus = cpu_map__new(NULL); | ||
75 | CHECK_NOT_NULL__(cpus); | ||
76 | |||
77 | evlist = perf_evlist__new(); | ||
78 | CHECK_NOT_NULL__(evlist); | ||
79 | |||
80 | perf_evlist__set_maps(evlist, cpus, threads); | ||
81 | |||
82 | CHECK__(parse_events(evlist, "cycles:u")); | ||
83 | |||
84 | perf_evlist__config(evlist, &opts); | ||
85 | |||
86 | evsel = perf_evlist__first(evlist); | ||
87 | |||
88 | evsel->attr.comm = 1; | ||
89 | evsel->attr.disabled = 1; | ||
90 | evsel->attr.enable_on_exec = 0; | ||
91 | |||
92 | CHECK__(perf_evlist__open(evlist)); | ||
93 | |||
94 | CHECK__(perf_evlist__mmap(evlist, UINT_MAX, false)); | ||
95 | |||
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; | ||
102 | } | ||
103 | goto out_err; | ||
104 | } | ||
105 | |||
106 | perf_evlist__enable(evlist); | ||
107 | |||
108 | comm1 = "Test COMM 1"; | ||
109 | CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0)); | ||
110 | |||
111 | test_tsc = rdtsc(); | ||
112 | |||
113 | comm2 = "Test COMM 2"; | ||
114 | CHECK__(prctl(PR_SET_NAME, (unsigned long)comm2, 0, 0, 0)); | ||
115 | |||
116 | perf_evlist__disable(evlist); | ||
117 | |||
118 | for (i = 0; i < evlist->nr_mmaps; i++) { | ||
119 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { | ||
120 | struct perf_sample sample; | ||
121 | |||
122 | if (event->header.type != PERF_RECORD_COMM || | ||
123 | (pid_t)event->comm.pid != getpid() || | ||
124 | (pid_t)event->comm.tid != getpid()) | ||
125 | continue; | ||
126 | |||
127 | if (strcmp(event->comm.comm, comm1) == 0) { | ||
128 | CHECK__(perf_evsel__parse_sample(evsel, event, | ||
129 | &sample)); | ||
130 | comm1_time = sample.time; | ||
131 | } | ||
132 | if (strcmp(event->comm.comm, comm2) == 0) { | ||
133 | CHECK__(perf_evsel__parse_sample(evsel, event, | ||
134 | &sample)); | ||
135 | comm2_time = sample.time; | ||
136 | } | ||
137 | } | ||
138 | } | ||
139 | |||
140 | if (!comm1_time || !comm2_time) | ||
141 | goto out_err; | ||
142 | |||
143 | test_time = tsc_to_perf_time(test_tsc, &tc); | ||
144 | comm1_tsc = perf_time_to_tsc(comm1_time, &tc); | ||
145 | comm2_tsc = perf_time_to_tsc(comm2_time, &tc); | ||
146 | |||
147 | pr_debug("1st event perf time %"PRIu64" tsc %"PRIu64"\n", | ||
148 | comm1_time, comm1_tsc); | ||
149 | pr_debug("rdtsc time %"PRIu64" tsc %"PRIu64"\n", | ||
150 | test_time, test_tsc); | ||
151 | pr_debug("2nd event perf time %"PRIu64" tsc %"PRIu64"\n", | ||
152 | comm2_time, comm2_tsc); | ||
153 | |||
154 | if (test_time <= comm1_time || | ||
155 | test_time >= comm2_time) | ||
156 | goto out_err; | ||
157 | |||
158 | if (test_tsc <= comm1_tsc || | ||
159 | test_tsc >= comm2_tsc) | ||
160 | goto out_err; | ||
161 | |||
162 | err = 0; | ||
163 | |||
164 | out_err: | ||
165 | if (evlist) { | ||
166 | perf_evlist__disable(evlist); | ||
167 | perf_evlist__munmap(evlist); | ||
168 | perf_evlist__close(evlist); | ||
169 | perf_evlist__delete(evlist); | ||
170 | } | ||
171 | if (cpus) | ||
172 | cpu_map__delete(cpus); | ||
173 | if (threads) | ||
174 | thread_map__delete(threads); | ||
175 | |||
176 | return err; | ||
177 | } | ||