Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / tools / perf / tests / perf-time-to-tsc.c
blob3b7cd4d32dcb5d1bc2a1d98e71685f80e381ffea
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <linux/types.h>
4 #include <sys/prctl.h>
6 #include "parse-events.h"
7 #include "evlist.h"
8 #include "evsel.h"
9 #include "thread_map.h"
10 #include "cpumap.h"
11 #include "tests.h"
13 #include "../arch/x86/util/tsc.h"
15 #define CHECK__(x) { \
16 while ((x) < 0) { \
17 pr_debug(#x " failed!\n"); \
18 goto out_err; \
19 } \
22 #define CHECK_NOT_NULL__(x) { \
23 while ((x) == NULL) { \
24 pr_debug(#x " failed!\n"); \
25 goto out_err; \
26 } \
29 static u64 rdtsc(void)
31 unsigned int low, high;
33 asm volatile("rdtsc" : "=a" (low), "=d" (high));
35 return low | ((u64)high) << 32;
38 /**
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,
52 .freq = 4000,
53 .target = {
54 .uses_mmap = true,
56 .sample_time = true,
58 struct thread_map *threads = NULL;
59 struct cpu_map *cpus = NULL;
60 struct perf_evlist *evlist = NULL;
61 struct perf_evsel *evsel = NULL;
62 int err = -1, ret, i;
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);
87 evsel->attr.comm = 1;
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);
97 if (ret) {
98 if (ret == -EOPNOTSUPP) {
99 fprintf(stderr, " (not supported)");
100 return 0;
102 goto out_err;
105 perf_evlist__enable(evlist);
107 comm1 = "Test COMM 1";
108 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0));
110 test_tsc = rdtsc();
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())
124 goto next_event;
126 if (strcmp(event->comm.comm, comm1) == 0) {
127 CHECK__(perf_evsel__parse_sample(evsel, event,
128 &sample));
129 comm1_time = sample.time;
131 if (strcmp(event->comm.comm, comm2) == 0) {
132 CHECK__(perf_evsel__parse_sample(evsel, event,
133 &sample));
134 comm2_time = sample.time;
136 next_event:
137 perf_evlist__mmap_consume(evlist, i);
141 if (!comm1_time || !comm2_time)
142 goto out_err;
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)
157 goto out_err;
159 if (test_tsc <= comm1_tsc ||
160 test_tsc >= comm2_tsc)
161 goto out_err;
163 err = 0;
165 out_err:
166 if (evlist) {
167 perf_evlist__disable(evlist);
168 perf_evlist__delete(evlist);
171 return err;