diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/perf/Makefile | 3 | ||||
-rw-r--r-- | tools/perf/arch/x86/Makefile | 2 | ||||
-rw-r--r-- | tools/perf/arch/x86/util/tsc.c | 59 | ||||
-rw-r--r-- | tools/perf/arch/x86/util/tsc.h | 20 | ||||
-rw-r--r-- | tools/perf/tests/builtin-test.c | 6 | ||||
-rw-r--r-- | tools/perf/tests/perf-time-to-tsc.c | 177 | ||||
-rw-r--r-- | tools/perf/tests/tests.h | 1 |
7 files changed, 268 insertions, 0 deletions
diff --git a/tools/perf/Makefile b/tools/perf/Makefile index 024680b23ddc..bfd12d02a304 100644 --- a/tools/perf/Makefile +++ b/tools/perf/Makefile | |||
@@ -389,6 +389,9 @@ LIB_OBJS += $(OUTPUT)tests/bp_signal.o | |||
389 | LIB_OBJS += $(OUTPUT)tests/bp_signal_overflow.o | 389 | LIB_OBJS += $(OUTPUT)tests/bp_signal_overflow.o |
390 | LIB_OBJS += $(OUTPUT)tests/task-exit.o | 390 | LIB_OBJS += $(OUTPUT)tests/task-exit.o |
391 | LIB_OBJS += $(OUTPUT)tests/sw-clock.o | 391 | LIB_OBJS += $(OUTPUT)tests/sw-clock.o |
392 | ifeq ($(ARCH),x86) | ||
393 | LIB_OBJS += $(OUTPUT)tests/perf-time-to-tsc.o | ||
394 | endif | ||
392 | 395 | ||
393 | BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o | 396 | BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o |
394 | BUILTIN_OBJS += $(OUTPUT)builtin-bench.o | 397 | BUILTIN_OBJS += $(OUTPUT)builtin-bench.o |
diff --git a/tools/perf/arch/x86/Makefile b/tools/perf/arch/x86/Makefile index 815841c04eb2..8801fe02f206 100644 --- a/tools/perf/arch/x86/Makefile +++ b/tools/perf/arch/x86/Makefile | |||
@@ -6,3 +6,5 @@ ifndef NO_LIBUNWIND | |||
6 | LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/unwind.o | 6 | LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/unwind.o |
7 | endif | 7 | endif |
8 | LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/header.o | 8 | LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/header.o |
9 | LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/tsc.o | ||
10 | LIB_H += arch/$(ARCH)/util/tsc.h | ||
diff --git a/tools/perf/arch/x86/util/tsc.c b/tools/perf/arch/x86/util/tsc.c new file mode 100644 index 000000000000..f1117441cdcd --- /dev/null +++ b/tools/perf/arch/x86/util/tsc.c | |||
@@ -0,0 +1,59 @@ | |||
1 | #include <stdbool.h> | ||
2 | #include <errno.h> | ||
3 | |||
4 | #include <linux/perf_event.h> | ||
5 | |||
6 | #include "../../perf.h" | ||
7 | #include "../../util/types.h" | ||
8 | #include "../../util/debug.h" | ||
9 | #include "tsc.h" | ||
10 | |||
11 | u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc) | ||
12 | { | ||
13 | u64 time, quot, rem; | ||
14 | |||
15 | time = ns - tc->time_zero; | ||
16 | quot = time / tc->time_mult; | ||
17 | rem = time % tc->time_mult; | ||
18 | return (quot << tc->time_shift) + | ||
19 | (rem << tc->time_shift) / tc->time_mult; | ||
20 | } | ||
21 | |||
22 | u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc) | ||
23 | { | ||
24 | u64 quot, rem; | ||
25 | |||
26 | quot = cyc >> tc->time_shift; | ||
27 | rem = cyc & ((1 << tc->time_shift) - 1); | ||
28 | return tc->time_zero + quot * tc->time_mult + | ||
29 | ((rem * tc->time_mult) >> tc->time_shift); | ||
30 | } | ||
31 | |||
32 | int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc, | ||
33 | struct perf_tsc_conversion *tc) | ||
34 | { | ||
35 | bool cap_usr_time_zero; | ||
36 | u32 seq; | ||
37 | int i = 0; | ||
38 | |||
39 | while (1) { | ||
40 | seq = pc->lock; | ||
41 | rmb(); | ||
42 | tc->time_mult = pc->time_mult; | ||
43 | tc->time_shift = pc->time_shift; | ||
44 | tc->time_zero = pc->time_zero; | ||
45 | cap_usr_time_zero = pc->cap_usr_time_zero; | ||
46 | rmb(); | ||
47 | if (pc->lock == seq && !(seq & 1)) | ||
48 | break; | ||
49 | if (++i > 10000) { | ||
50 | pr_debug("failed to get perf_event_mmap_page lock\n"); | ||
51 | return -EINVAL; | ||
52 | } | ||
53 | } | ||
54 | |||
55 | if (!cap_usr_time_zero) | ||
56 | return -EOPNOTSUPP; | ||
57 | |||
58 | return 0; | ||
59 | } | ||
diff --git a/tools/perf/arch/x86/util/tsc.h b/tools/perf/arch/x86/util/tsc.h new file mode 100644 index 000000000000..a24dec81c795 --- /dev/null +++ b/tools/perf/arch/x86/util/tsc.h | |||
@@ -0,0 +1,20 @@ | |||
1 | #ifndef TOOLS_PERF_ARCH_X86_UTIL_TSC_H__ | ||
2 | #define TOOLS_PERF_ARCH_X86_UTIL_TSC_H__ | ||
3 | |||
4 | #include "../../util/types.h" | ||
5 | |||
6 | struct perf_tsc_conversion { | ||
7 | u16 time_shift; | ||
8 | u32 time_mult; | ||
9 | u64 time_zero; | ||
10 | }; | ||
11 | |||
12 | struct perf_event_mmap_page; | ||
13 | |||
14 | int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc, | ||
15 | struct perf_tsc_conversion *tc); | ||
16 | |||
17 | u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc); | ||
18 | u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc); | ||
19 | |||
20 | #endif /* TOOLS_PERF_ARCH_X86_UTIL_TSC_H__ */ | ||
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index 35b45f1466b5..b7b4049fabbb 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c | |||
@@ -93,6 +93,12 @@ static struct test { | |||
93 | .desc = "Test software clock events have valid period values", | 93 | .desc = "Test software clock events have valid period values", |
94 | .func = test__sw_clock_freq, | 94 | .func = test__sw_clock_freq, |
95 | }, | 95 | }, |
96 | #if defined(__x86_64__) || defined(__i386__) | ||
97 | { | ||
98 | .desc = "Test converting perf time to TSC", | ||
99 | .func = test__perf_time_to_tsc, | ||
100 | }, | ||
101 | #endif | ||
96 | { | 102 | { |
97 | .func = NULL, | 103 | .func = NULL, |
98 | }, | 104 | }, |
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 | } | ||
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index 07a92f9c6712..d22202aa16e9 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h | |||
@@ -35,5 +35,6 @@ int test__bp_signal(void); | |||
35 | int test__bp_signal_overflow(void); | 35 | int test__bp_signal_overflow(void); |
36 | int test__task_exit(void); | 36 | int test__task_exit(void); |
37 | int test__sw_clock_freq(void); | 37 | int test__sw_clock_freq(void); |
38 | int test__perf_time_to_tsc(void); | ||
38 | 39 | ||
39 | #endif /* TESTS_H */ | 40 | #endif /* TESTS_H */ |