1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/stddef.h>
6 #include <linux/perf_event.h>
8 #include "../../perf.h"
9 #include <linux/types.h>
10 #include "../../util/debug.h"
11 #include "../../util/tsc.h"
13 int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc,
14 struct perf_tsc_conversion *tc)
16 bool cap_user_time_zero;
23 tc->time_mult = pc->time_mult;
24 tc->time_shift = pc->time_shift;
25 tc->time_zero = pc->time_zero;
26 cap_user_time_zero = pc->cap_user_time_zero;
28 if (pc->lock == seq && !(seq & 1))
31 pr_debug("failed to get perf_event_mmap_page lock\n");
36 if (!cap_user_time_zero)
44 unsigned int low, high;
46 asm volatile("rdtsc" : "=a" (low), "=d" (high));
48 return low | ((u64)high) << 32;
51 int perf_event__synth_time_conv(const struct perf_event_mmap_page *pc,
52 struct perf_tool *tool,
53 perf_event__handler_t process,
54 struct machine *machine)
56 union perf_event event = {
59 .type = PERF_RECORD_TIME_CONV,
60 .size = sizeof(struct time_conv_event),
64 struct perf_tsc_conversion tc;
69 err = perf_read_tsc_conversion(pc, &tc);
70 if (err == -EOPNOTSUPP)
75 pr_debug2("Synthesizing TSC conversion information\n");
77 event.time_conv.time_mult = tc.time_mult;
78 event.time_conv.time_shift = tc.time_shift;
79 event.time_conv.time_zero = tc.time_zero;
81 return process(tool, &event, NULL, machine);