If there is information for TSC to nanoseconds conversion in the trace file metadata, use it to recalculate the timestamps of all events. Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@xxxxxxxxx> --- lib/trace-cmd/trace-input.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index dfbe4af6..7ac81809 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -1196,6 +1196,23 @@ static void free_next(struct tracecmd_input *handle, int cpu) tracecmd_free_record(record); } +/* This functions was taken from the Linux kernel */ +static unsigned long long mul_u64_u32_shr(unsigned long long a, + unsigned long long mul, unsigned int shift) +{ + unsigned int ah, al; + unsigned long long ret; + + al = a; + ah = a >> 32; + + ret = (al * mul) >> shift; + if (ah) + ret += (ah * mul) << (32 - shift); + + return ret; +} + static inline unsigned long long timestamp_correction_calc(unsigned long long ts, unsigned int flags, struct ts_offset_sample *min, @@ -1279,14 +1296,18 @@ static unsigned long long timestamp_correct(unsigned long long ts, static unsigned long long timestamp_calc(unsigned long long ts, struct tracecmd_input *handle) { - unsigned long long tstamp; + unsigned long long t; - tstamp = timestamp_correct(ts, handle); + t = timestamp_correct(ts, handle); if (handle->ts2secs) - tstamp *= handle->ts2secs; + t *= handle->ts2secs; + else if (handle->tsc_calc.mult) { + t -= handle->tsc_calc.offset; + t = mul_u64_u32_shr(t, handle->tsc_calc.mult, handle->tsc_calc.shift); + } - return tstamp; + return t; } /* -- 2.30.2