0001
0002 #include <errno.h>
0003 #include <inttypes.h>
0004 #include <string.h>
0005
0006 #include <linux/compiler.h>
0007 #include <linux/perf_event.h>
0008 #include <linux/stddef.h>
0009 #include <linux/types.h>
0010
0011 #include <asm/barrier.h>
0012
0013 #include "event.h"
0014 #include "synthetic-events.h"
0015 #include "debug.h"
0016 #include "tsc.h"
0017
0018 u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc)
0019 {
0020 u64 t, quot, rem;
0021
0022 t = ns - tc->time_zero;
0023 quot = t / tc->time_mult;
0024 rem = t % tc->time_mult;
0025 return (quot << tc->time_shift) +
0026 (rem << tc->time_shift) / tc->time_mult;
0027 }
0028
0029 u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc)
0030 {
0031 u64 quot, rem;
0032
0033 if (tc->cap_user_time_short)
0034 cyc = tc->time_cycles +
0035 ((cyc - tc->time_cycles) & tc->time_mask);
0036
0037 quot = cyc >> tc->time_shift;
0038 rem = cyc & (((u64)1 << tc->time_shift) - 1);
0039 return tc->time_zero + quot * tc->time_mult +
0040 ((rem * tc->time_mult) >> tc->time_shift);
0041 }
0042
0043 int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc,
0044 struct perf_tsc_conversion *tc)
0045 {
0046 u32 seq;
0047 int i = 0;
0048
0049 while (1) {
0050 seq = pc->lock;
0051 rmb();
0052 tc->time_mult = pc->time_mult;
0053 tc->time_shift = pc->time_shift;
0054 tc->time_zero = pc->time_zero;
0055 tc->time_cycles = pc->time_cycles;
0056 tc->time_mask = pc->time_mask;
0057 tc->cap_user_time_zero = pc->cap_user_time_zero;
0058 tc->cap_user_time_short = pc->cap_user_time_short;
0059 rmb();
0060 if (pc->lock == seq && !(seq & 1))
0061 break;
0062 if (++i > 10000) {
0063 pr_debug("failed to get perf_event_mmap_page lock\n");
0064 return -EINVAL;
0065 }
0066 }
0067
0068 if (!tc->cap_user_time_zero)
0069 return -EOPNOTSUPP;
0070
0071 return 0;
0072 }
0073
0074 int perf_event__synth_time_conv(const struct perf_event_mmap_page *pc,
0075 struct perf_tool *tool,
0076 perf_event__handler_t process,
0077 struct machine *machine)
0078 {
0079 union perf_event event = {
0080 .time_conv = {
0081 .header = {
0082 .type = PERF_RECORD_TIME_CONV,
0083 .size = sizeof(struct perf_record_time_conv),
0084 },
0085 },
0086 };
0087 struct perf_tsc_conversion tc;
0088 int err;
0089
0090 if (!pc)
0091 return 0;
0092 err = perf_read_tsc_conversion(pc, &tc);
0093 if (err == -EOPNOTSUPP)
0094 return 0;
0095 if (err)
0096 return err;
0097
0098 pr_debug2("Synthesizing TSC conversion information\n");
0099
0100 event.time_conv.time_mult = tc.time_mult;
0101 event.time_conv.time_shift = tc.time_shift;
0102 event.time_conv.time_zero = tc.time_zero;
0103 event.time_conv.time_cycles = tc.time_cycles;
0104 event.time_conv.time_mask = tc.time_mask;
0105 event.time_conv.cap_user_time_zero = tc.cap_user_time_zero;
0106 event.time_conv.cap_user_time_short = tc.cap_user_time_short;
0107
0108 return process(tool, &event, NULL, machine);
0109 }
0110
0111 u64 __weak rdtsc(void)
0112 {
0113 return 0;
0114 }
0115
0116 size_t perf_event__fprintf_time_conv(union perf_event *event, FILE *fp)
0117 {
0118 struct perf_record_time_conv *tc = (struct perf_record_time_conv *)event;
0119 size_t ret;
0120
0121 ret = fprintf(fp, "\n... Time Shift %" PRI_lu64 "\n", tc->time_shift);
0122 ret += fprintf(fp, "... Time Muliplier %" PRI_lu64 "\n", tc->time_mult);
0123 ret += fprintf(fp, "... Time Zero %" PRI_lu64 "\n", tc->time_zero);
0124
0125
0126
0127
0128
0129
0130 if (event_contains(*tc, time_cycles)) {
0131 ret += fprintf(fp, "... Time Cycles %" PRI_lu64 "\n",
0132 tc->time_cycles);
0133 ret += fprintf(fp, "... Time Mask %#" PRI_lx64 "\n",
0134 tc->time_mask);
0135 ret += fprintf(fp, "... Cap Time Zero %" PRId32 "\n",
0136 tc->cap_user_time_zero);
0137 ret += fprintf(fp, "... Cap Time Short %" PRId32 "\n",
0138 tc->cap_user_time_short);
0139 }
0140
0141 return ret;
0142 }