diff mbox series

[v2,12/18] trace-cmd: Convert tsc timestamps to nanosecods when reading trace data from a file

Message ID 20210322095945.259300-13-tz.stoyanov@gmail.com (mailing list archive)
State Superseded
Headers show
Series TSC trace clock to nanosecond conversion | expand

Commit Message

Tzvetomir Stoyanov (VMware) March 22, 2021, 9:59 a.m. UTC
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@gmail.com>
---
 lib/trace-cmd/trace-input.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

Comments

Steven Rostedt March 24, 2021, 1:41 a.m. UTC | #1
On Mon, 22 Mar 2021 11:59:39 +0200
"Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> wrote:

> @@ -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;
>  }
>  

Like I said, I like to save on extra variables ;-)

static unsigned long long timestamp_calc(unsigned long long ts,
					 struct tracecmd_input *handle)
{
	ts = timestamp_correct(ts, handle);

	if (handle->ts2secs)
		ts *= handle->ts2secs;
	else if (handle->tsc_calc.mult) {
		ts -= handle->tsc_calc.offset;
		ts = mul_u64_u32_shr(t, handle->tsc_calc.mult, handle->tsc_calc.shift);
	}

	return ts;
}

-- Steve
diff mbox series

Patch

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;
 }
 
 /*