Fix overflow when applying tsc2nsec calculations
v2 cover Fix overflow when applying tsc2nsec calculations
Verify that applying tsc2nsec corrections on the recorded timestamps will not cause an overflow. When recording the trace, in case of tsc2nsec clock, get the first TSC timestamp as offset to be used in tsc2nsec conversion.
v2 changes:
- Optimize the logic for getting the timestamp of the first recorded event.
Tzvetomir Stoyanov (VMware) (3):
trace-cmd library: Add new trace-cmd library APIs for guest ts
corrections
trace-cmd library: Add check before applying tsc2nsec offset
trace-cmd: Get the timestamp of the first recorded event as TSC offset
.../include/private/trace-cmd-private.h | 18 ++- lib/trace-cmd/trace-input.c | 116 ++----------- lib/trace-cmd/trace-timesync.c | 113 ++++++++++--- tracecmd/include/trace-local.h | 1 + tracecmd/trace-record.c | 152 ++++++++++++++---- 5 files changed, 246 insertions(+), 154 deletions(-)
Signature · 1 line
2.30.2
PATCH v2 1/3 trace-cmd library: Add new trace-cmd library APIs for guest ts corrections
The logic for converting guest to host timestamps is local to the trace-cmd library and is used only when a trace file is opened. In order to reuse that logic, a new APIs are added:
tracecmd_tsync_get_proto_flags()
tracecmd_tsync_get_offsets()
tracecmd_guest_ts_calc()
struct ts_offset_sample
struct tracecmd_ts_corrections
--- .../include/private/trace-cmd-private.h | 18 ++- lib/trace-cmd/trace-input.c | 106 ++-------------- lib/trace-cmd/trace-timesync.c | 113 ++++++++++++++---- 3 files changed, 116 insertions(+), 121 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +16 -2
@@ -443,6 +443,17 @@ enum tracecmd_time_sync_role {/* Timestamp synchronization flags */ #define TRACECMD_TSYNC_FLAG_INTERPOLATE 0x1 +struct ts_offset_sample { +long long time; +long long offset; +long long scaling; +}; + +struct tracecmd_ts_corrections { +int ts_samples_count; +struct ts_offset_sample *ts_samples; +}; +void tracecmd_tsync_init (void );int tracecmd_tsync_proto_getall (struct tracecmd_tsync_protos **protos,const char *clock,int role);bool tsync_proto_is_supported (const char *proto_name);
@@ -456,8 +467,8 @@ tracecmd_tsync_with_guest(unsigned long long trace_id, int loop_interval, int guest_cpus, const char *proto_name, const char *clock);int tracecmd_tsync_with_guest_stop (struct tracecmd_time_sync *tsync);int tracecmd_tsync_get_offsets (struct tracecmd_time_sync *tsync,int cpu, -int *count,long long **ts, -long long **offsets,long long **scalings); +struct tracecmd_ts_corrections *offsets); +int tracecmd_tsync_get_proto_flags (struct tracecmd_time_sync *tsync,unsigned int *flags);int tracecmd_tsync_get_session_params (struct tracecmd_time_sync *tsync,char **selected_proto,unsigned int *tsync_port);
@@ -465,6 +476,9 @@ void tracecmd_tsync_free(struct tracecmd_time_sync *tsync);int tracecmd_write_guest_time_shift (struct tracecmd_output *handle,struct tracecmd_time_sync *tsync); +unsigned long long tracecmd_guest_ts_calc (unsigned long long ts, +struct tracecmd_ts_corrections *tsync,int flags); +/* --- Plugin handling --- */ extern struct tep_plugin_option trace_ftrace_options[];
lib/trace-cmd/trace-input.c +9 -97
143 lines not on this page · raw message
lib/trace-cmd/trace-timesync.c +91 -22
165 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v2 2/3 trace-cmd library: Add check before applying tsc2nsec offset
In reply to Tzvetomir Stoyanov (VMware)When converting TSC timestamps to nanoseconds, an offset is used. That offset is subtracted from each TSC timestamp, before the conversion. However, if the timestamp is lower that the offset this causes an overflow, as both timestamp and offset are unsigned long integers. A check is added to verify the subtraction will not cause an overflow. In case of an overflow, the timestamp is set to 0 and a warning message is printed.
--- lib/trace-cmd/trace-input.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
lib/trace-cmd/trace-input.c +8 -2
@@ -1230,8 +1230,14 @@ static unsigned long long timestamp_calc(unsigned long long ts, int cpu, ts *= handle->ts2secs; }else if (handle->tsc_calc.mult) {/* auto calculated TSC clock frequency */ - ts -= handle->tsc_calc.offset; - ts =mul_u64_u32_shr (ts, handle->tsc_calc.mult, handle->tsc_calc.shift); +if (ts > handle->tsc_calc.offset) { + ts -= handle->tsc_calc.offset; + ts =mul_u64_u32_shr (ts, handle->tsc_calc.mult, handle->tsc_calc.shift); + }else { +tracecmd_warning ("Timestamp $llu is before the initial offset %llu, set it to 0" , + ts, handle->tsc_calc.offset); + ts =0 ; + } }/* User specified time offset with --ts-offset or --date options */
Signature · 1 line
2.30.2
Re: [PATCH v2 2/3] trace-cmd library: Add check before applying tsc2nsec offset
Quoted 18 lines · On Fri, 16 Apr 2021 13:34:08 +0300 "Tzvetomir Stoyanov (VMware)"…
When converting TSC timestamps to nanoseconds, an offset is used. That offset is subtracted from each TSC timestamp, before the conversion. However, if the timestamp is lower that the offset this causes an overflow, as both timestamp and offset are unsigned long integers. A check is added to verify the subtraction will not cause an overflow. In case of an overflow, the timestamp is set to 0 and a warning message is printed.
Signed-off-by Tzvetomir Stoyanov (VMware) <[email protected]>--- lib/trace-cmd/trace-input.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)lib/trace-cmd/trace-input.c +0
-0
lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1237
Quoted 10 lines
lib/trace-cmd/trace-input.c +5
-2@@ -1230,8 +1230,14 @@ static unsigned long long timestamp_calc(unsigned long long ts, int cpu, ts *= handle->ts2secs; }else if (handle->tsc_calc.mult) {/* auto calculated TSC clock frequency */ - ts -= handle->tsc_calc.offset; - ts =mul_u64_u32_shr (ts, handle->tsc_calc.mult, handle->tsc_calc.shift); +if (ts > handle->tsc_calc.offset) { + ts -= handle->tsc_calc.offset; + ts =mul_u64_u32_shr (ts, handle->tsc_calc.mult, handle->tsc_calc.shift); + }else { + tracecmd_warning("Timestamp $llu is before the initial offset %llu, set it to 0",
$llu ??
Are you testing these? The bad trace files that I supplied triggers this.
lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1239
Quoted 2 lines
lib/trace-cmd/trace-input.c +2
-0+ ts, handle->tsc_calc.offset); + ts =0 ;
We should turn off the offset and not just zero out the timestamps, otherwise we just have useless data:
Timestamp $llu is before the initial offset 20094822433105, set it to 0 Timestamp $llu is before the initial offset 20094822433105, set it to 0 Timestamp $llu is before the initial offset 20094822460994, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094822961507, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094822967292, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094823302351, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094823306954, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094824963751, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094824965723, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094824972124, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094824972697, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094828868305, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094828871264, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094828881104, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094828882014, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094831874377, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094831876734, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094831881252, set it to 0
[..]
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094832060068, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094835262757, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094835265223, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094835270122, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094835273058, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094835273614, set it to 0
cpus=2
trace-cmd-3196 [000] 0.000000: sched_stat_runtime: comm=trace-cmd pid=3196 runtime=85576 [ns] vruntime=12853032342 [ns]
trace-cmd-3196 [000] 0.000000: sched_switch: trace-cmd:3196 [120] S ==> trace-cmd:3199 [120]
trace-cmd-3199 [000] 0.000000: sched_stat_runtime: comm=trace-cmd pid=3199 runtime=156103 [ns] vruntime=12857102867 [ns]
trace-cmd-3199 [000] 0.000000: sched_switch: trace-cmd:3199 [120] S ==> trace-cmd:3198 [120]
trace-cmd-3198 [000] 0.000000: sched_stat_runtime: comm=trace-cmd pid=3198 runtime=100540 [ns] vruntime=12858890364 [ns]
trace-cmd-3198 [000] 0.000000: sched_switch: trace-cmd:3198 [120] S ==> swapper/0:0 [120]
<idle>-0 [000] 0.000000: hrtimer_cancel: hrtimer=0xffff8cc5bd41f780
<idle>-0 [000] 0.000000: hrtimer_expire_entry: hrtimer=0xffff8cc5bd41f780 now=1444054745103 function=tick_sched_timer/0x0
<idle>-0 [000] 0.000000: hrtimer_expire_exit: hrtimer=0xffff8cc5bd41f780
<idle>-0 [000] 0.000000: hrtimer_start: hrtimer=0xffff8cc5bd41f780 function=tick_sched_timer/0x0 expires=1444055726117 softexpires=1444055726117
<idle>-0 [000] 0.000000: hrtimer_cancel: hrtimer=0xffff8cc5bd41f780
<idle>-0 [000] 0.000000: hrtimer_expire_entry: hrtimer=0xffff8cc5bd41f780 now=1444055895819 function=tick_sched_timer/0x0
<idle>-0 [000] 0.000000: hrtimer_expire_exit: hrtimer=0xffff8cc5bd41f780
<idle>-0 [000] 0.000000: hrtimer_start: hrtimer=0xffff8cc5bd41f780 function=tick_sched_timer/0x0 expires=1444056726117 softexpires=1444056726117
<idle>-0 [000] 0.000000: hrtimer_cancel: hrtimer=0xffff8cc5bd41f780
<idle>-0 [000] 0.000000: hrtimer_expire_entry: hrtimer=0xffff8cc5bd41f780 now=1444056782207 function=tick_sched_timer/0x0
<idle>-0 [000] 0.000000: softirq_raise: vec=1 [action=TIMER]
<idle>-0 [000] 0.000000: hrtimer_expire_exit: hrtimer=0xffff8cc5bd41f780
<idle>-0 [000] 0.000000: hrtimer_start: hrtimer=0xffff8cc5bd41f780 function=tick_sched_timer/0x0 expires=1444057726117 softexpires=1444057726117
<idle>-0 [000] 0.000000: softirq_entry: vec=1 [action=TIMER]
<idle>-0 [000] 0.000000: timer_cancel: timer=0xffffa61a0006be40
<idle>-0 [000] 0.000000: timer_expire_entry: timer=0xffffa61a0006be40 function=process_timeout now=4296111312 baseclk=4296111312
<idle>-0 [000] 0.000000: sched_waking: comm=rcu_sched pid=12 prio=120 target_cpu=000We don't want all timestamps zero. We want to disable the starting event.
By having this instead:
} else {
tracecmd_warning("Timestamp %llu is before the initial offset %llu\n"
"\tSetting offset to 0",
ts, handle->tsc_calc.offset);
handle->tsc_calc.offset = 0;
ts = mul_u64_u32_shr(ts, handle->tsc_calc.mult, handle->tsc_calc.shift);
}We get this as output:
Timestamp 20094822433105 is before the initial offset 20159736262966
Setting offset to 0
cpus=2
trace-cmd-3196 [000] 5923.651995: sched_stat_runtime: comm=trace-cmd pid=3196 runtime=85576 [ns] vruntime=12853032342 [ns]
trace-cmd-3196 [000] 5923.652004: sched_switch: trace-cmd:3196 [120] S ==> trace-cmd:3199 [120]
trace-cmd-3199 [000] 5923.652151: sched_stat_runtime: comm=trace-cmd pid=3199 runtime=156103 [ns] vruntime=12857102867 [ns]
trace-cmd-3199 [000] 5923.652153: sched_switch: trace-cmd:3199 [120] S ==> trace-cmd:3198 [120]
trace-cmd-3198 [000] 5923.652252: sched_stat_runtime: comm=trace-cmd pid=3198 runtime=100540 [ns] vruntime=12858890364 [ns]
trace-cmd-3198 [000] 5923.652253: sched_switch: trace-cmd:3198 [120] S ==> swapper/0:0 [120]
<idle>-0 [000] 5923.652741: hrtimer_cancel: hrtimer=0xffff8cc5bd41f780
<idle>-0 [000] 5923.652742: hrtimer_expire_entry: hrtimer=0xffff8cc5bd41f780 now=1444054745103 function=tick_sched_timer/0x0
<idle>-0 [000] 5923.652744: hrtimer_expire_exit: hrtimer=0xffff8cc5bd41f780
<idle>-0 [000] 5923.652744: hrtimer_start: hrtimer=0xffff8cc5bd41f780 function=tick_sched_timer/0x0 expires=1444055726117 softexpires=1444055726117
<idle>-0 [000] 5923.653892: hrtimer_cancel: hrtimer=0xffff8cc5bd41f780Much more usable.
-- Steve
Quoted 4 lines
lib/trace-cmd/trace-input.c +1
-0+ } }/* User specified time offset with --ts-offset or --date options */
Re: [PATCH v2 2/3] trace-cmd library: Add check before applying tsc2nsec offset
Quoted 119 lines · Steven Rostedt
Quoted 18 lines · On Fri, 16 Apr 2021 13:34:08 +0300 "Tzvetomir Stoyanov (VMware)"…
When converting TSC timestamps to nanoseconds, an offset is used. That offset is subtracted from each TSC timestamp, before the conversion. However, if the timestamp is lower that the offset this causes an overflow, as both timestamp and offset are unsigned long integers. A check is added to verify the subtraction will not cause an overflow. In case of an overflow, the timestamp is set to 0 and a warning message is printed.
Signed-off-by Tzvetomir Stoyanov (VMware) <[email protected]>--- lib/trace-cmd/trace-input.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)lib/trace-cmd/trace-input.c +0
-0lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1237
Quoted 10 lines
lib/trace-cmd/trace-input.c +5
-2@@ -1230,8 +1230,14 @@ static unsigned long long timestamp_calc(unsigned long long ts, int cpu, ts *= handle->ts2secs; }else if (handle->tsc_calc.mult) {/* auto calculated TSC clock frequency */ - ts -= handle->tsc_calc.offset; - ts =mul_u64_u32_shr (ts, handle->tsc_calc.mult, handle->tsc_calc.shift); +if (ts > handle->tsc_calc.offset) { + ts -= handle->tsc_calc.offset; + ts =mul_u64_u32_shr (ts, handle->tsc_calc.mult, handle->tsc_calc.shift); + }else { + tracecmd_warning("Timestamp $llu is before the initial offset %llu, set it to 0",$llu ??Are you testing these? The bad trace files that I supplied triggers this.
lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1239
Quoted 2 lines
lib/trace-cmd/trace-input.c +2
-0+ ts, handle->tsc_calc.offset); + ts =0 ;We should turn off the offset and not just zero out the timestamps, otherwise we just have useless data:
Timestamp $llu is before the initial offset 20094822433105, set it to 0 Timestamp $llu is before the initial offset 20094822433105, set it to 0 Timestamp $llu is before the initial offset 20094822460994, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094822961507, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094822967292, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094823302351, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094823306954, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094824963751, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094824965723, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094824972124, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094824972697, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094828868305, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094828871264, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094828881104, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094828882014, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094831874377, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094831876734, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094831881252, set it to 0
[..]
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094832060068, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094835262757, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094835265223, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094835270122, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094835273058, set it to 0
libtracecmd: File exists
Timestamp $llu is before the initial offset 20094835273614, set it to 0
cpus=2trace-cmd-3196 [000] 0.000000: sched_stat_runtime: comm=trace-cmd pid=3196 runtime=85576 [ns] vruntime=12853032342 [ns] trace-cmd-3196 [000] 0.000000: sched_switch: trace-cmd:3196 [120] S ==> trace-cmd:3199 [120] trace-cmd-3199 [000] 0.000000: sched_stat_runtime: comm=trace-cmd pid=3199 runtime=156103 [ns] vruntime=12857102867 [ns] trace-cmd-3199 [000] 0.000000: sched_switch: trace-cmd:3199 [120] S ==> trace-cmd:3198 [120] trace-cmd-3198 [000] 0.000000: sched_stat_runtime: comm=trace-cmd pid=3198 runtime=100540 [ns] vruntime=12858890364 [ns] trace-cmd-3198 [000] 0.000000: sched_switch: trace-cmd:3198 [120] S ==> swapper/0:0 [120] <idle>-0 [000] 0.000000: hrtimer_cancel: hrtimer=0xffff8cc5bd41f780 <idle>-0 [000] 0.000000: hrtimer_expire_entry: hrtimer=0xffff8cc5bd41f780 now=1444054745103 function=tick_sched_timer/0x0 <idle>-0 [000] 0.000000: hrtimer_expire_exit: hrtimer=0xffff8cc5bd41f780 <idle>-0 [000] 0.000000: hrtimer_start: hrtimer=0xffff8cc5bd41f780 function=tick_sched_timer/0x0 expires=1444055726117 softexpires=1444055726117 <idle>-0 [000] 0.000000: hrtimer_cancel: hrtimer=0xffff8cc5bd41f780 <idle>-0 [000] 0.000000: hrtimer_expire_entry: hrtimer=0xffff8cc5bd41f780 now=1444055895819 function=tick_sched_timer/0x0 <idle>-0 [000] 0.000000: hrtimer_expire_exit: hrtimer=0xffff8cc5bd41f780 <idle>-0 [000] 0.000000: hrtimer_start: hrtimer=0xffff8cc5bd41f780 function=tick_sched_timer/0x0 expires=1444056726117 softexpires=1444056726117 <idle>-0 [000] 0.000000: hrtimer_cancel: hrtimer=0xffff8cc5bd41f780 <idle>-0 [000] 0.000000: hrtimer_expire_entry: hrtimer=0xffff8cc5bd41f780 now=1444056782207 function=tick_sched_timer/0x0 <idle>-0 [000] 0.000000: softirq_raise: vec=1 [action=TIMER] <idle>-0 [000] 0.000000: hrtimer_expire_exit: hrtimer=0xffff8cc5bd41f780 <idle>-0 [000] 0.000000: hrtimer_start: hrtimer=0xffff8cc5bd41f780 function=tick_sched_timer/0x0 expires=1444057726117 softexpires=1444057726117 <idle>-0 [000] 0.000000: softirq_entry: vec=1 [action=TIMER] <idle>-0 [000] 0.000000: timer_cancel: timer=0xffffa61a0006be40 <idle>-0 [000] 0.000000: timer_expire_entry: timer=0xffffa61a0006be40 function=process_timeout now=4296111312 baseclk=4296111312 <idle>-0 [000] 0.000000: sched_waking: comm=rcu_sched pid=12 prio=120 target_cpu=000We don't want all timestamps zero. We want to disable the starting event.
By having this instead:
Quoted 8 lines
} else {
tracecmd_warning("Timestamp %llu is before the initial offset %llu\n"
"\tSetting offset to 0",
ts, handle->tsc_calc.offset);
handle->tsc_calc.offset = 0;
ts = mul_u64_u32_shr(ts, handle->tsc_calc.mult, handle->tsc_calc.shift);
}This will set the guest offset to 0, will not change the offsets of the other files. The result is that the files will not be displayed in sync, because of these few broken events. As this is only a sanity check, and should not happen if the patch "trace-cmd: Get the timestamp of the first recorded event as TSC offset" is applied, I would suggest to zero the broken timestamps and to advise the user to use "--raw-ts" option instead:
"Timestamp %llu is before the initial offset %llu, set it to 0.
Display the files with --raw-ts option to see the original timestamps.\n"
Quoted 27 lines
We get this as output:
Timestamp 20094822433105 is before the initial offset 20159736262966
Setting offset to 0
cpus=2trace-cmd-3196 [000] 5923.651995: sched_stat_runtime: comm=trace-cmd pid=3196 runtime=85576 [ns] vruntime=12853032342 [ns] trace-cmd-3196 [000] 5923.652004: sched_switch: trace-cmd:3196 [120] S ==> trace-cmd:3199 [120] trace-cmd-3199 [000] 5923.652151: sched_stat_runtime: comm=trace-cmd pid=3199 runtime=156103 [ns] vruntime=12857102867 [ns] trace-cmd-3199 [000] 5923.652153: sched_switch: trace-cmd:3199 [120] S ==> trace-cmd:3198 [120] trace-cmd-3198 [000] 5923.652252: sched_stat_runtime: comm=trace-cmd pid=3198 runtime=100540 [ns] vruntime=12858890364 [ns] trace-cmd-3198 [000] 5923.652253: sched_switch: trace-cmd:3198 [120] S ==> swapper/0:0 [120] <idle>-0 [000] 5923.652741: hrtimer_cancel: hrtimer=0xffff8cc5bd41f780 <idle>-0 [000] 5923.652742: hrtimer_expire_entry: hrtimer=0xffff8cc5bd41f780 now=1444054745103 function=tick_sched_timer/0x0 <idle>-0 [000] 5923.652744: hrtimer_expire_exit: hrtimer=0xffff8cc5bd41f780 <idle>-0 [000] 5923.652744: hrtimer_start: hrtimer=0xffff8cc5bd41f780 function=tick_sched_timer/0x0 expires=1444055726117 softexpires=1444055726117 <idle>-0 [000] 5923.653892: hrtimer_cancel: hrtimer=0xffff8cc5bd41f780Much more usable.
-- Steve
Quoted 4 lines
lib/trace-cmd/trace-input.c +1
-0+ } }/* User specified time offset with --ts-offset or --date options */
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov VMware Open Source Technology Center
Re: [PATCH v2 2/3] trace-cmd library: Add check before applying tsc2nsec offset
Quoted 13 lines · On Mon, 19 Apr 2021 11:08:04 +0300 Tzvetomir Stoyanov <[email protected]>…
Quoted 4 lines
We don't want all timestamps zero. We want to disable the starting event.
By having this instead:
Quoted 8 lines
} else { tracecmd_warning("Timestamp %llu is before the initial offset %llu\n" "\tSetting offset to 0", ts, handle->tsc_calc.offset); handle->tsc_calc.offset = 0; ts = mul_u64_u32_shr(ts, handle->tsc_calc.mult, handle->tsc_calc.shift); }
Quoted 10 lines
This will set the guest offset to 0, will not change the offsets of the other files. The result is that the files will not be displayed in sync, because of these few broken events. As this is only a sanity check, and should not happen if the patch "trace-cmd: Get the timestamp of the first recorded event as TSC offset" is applied, I would suggest to zero the broken timestamps and to advise the user to use "--raw-ts" option instead:
"Timestamp %llu is before the initial offset %llu, set it to 0.
Display the files with --raw-ts option to see the original timestamps.\n"
I still don't like it, because a thousand events all with the same timestamp will still screw up everything (including the output of kernelshark, as you'll see a thousand events at time zero, and then the rest of the events at some way off in the future time).
And as this is in the library, having a warning is only applicable to trace-cmd, and not any other user case.
Actually, I think we are looking at the wrong place to do the check. All event streams (CPUs) need to be in monotonic order, or it's just corrupted to begin with.
The library should supply a check to see if each stream starts after the offset, and if not, give a report back to the user (trace-cmd in this case, or kernelsharks ftrace plugin). Then give the user a way to give another offset to tell all the other streams to convert to.
That is:
max = 0;
for each stream:
ret = read first event, compared to offset
if (!ret) {
/* ret will be how much off by offset */
if (ret > max)
max = ret;
}
if (max) {
for each stream:
Update offset by subtracting max
}Look at each stream, and have some callback give you how much ahead the first event is from its given offset. Take the biggest value from reading all the streams, then tell all the streams to subtract its offset by that max value. The end result is that all streams now start at a positive value.
-- Steve
Re: [PATCH v2 2/3] trace-cmd library: Add check before applying tsc2nsec offset
Quoted 15 lines · On Mon, 19 Apr 2021 09:45:43 -0400 Steven Rostedt <[email protected]> wrote:
That is:
max = 0; for each stream: ret = read first event, compared to offset if (!ret) { /* ret will be how much off by offset */ if (ret > max) max = ret; } if (max) { for each stream: Update offset by subtracting max }
Quoted 4 lines
Look at each stream, and have some callback give you how much ahead the first event is from its given offset. Take the biggest value from reading all the streams, then tell all the streams to subtract its offset by that max value. The end result is that all streams now start at a positive value.
From our call, here's the pseudo code that I was talking about:
lib/trace-cmd/trace-input.c static unsigned long long timestamp_host_sync(unsigned long long ts, int cpu, · line 1306
Quoted 11 lines
lib/trace-cmd/trace-input.c +1
-1@@ -1302,7 +1302,7 @@ static unsigned long long timestamp_host_sync(unsigned long long ts, int cpu, &tsync->ts_samples[mid+1]); } -static unsigned long long timestamp_calc (unsigned long long ts,int cpu, +static unsigned long long pre_timestamp_calc (unsigned long long ts,int cpu,struct tracecmd_input *handle)
I pulled out the timestamp_calc into a helper function.
lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1321
Quoted 7 lines
lib/trace-cmd/trace-input.c +0
-1{/* do not modify raw timestamps */ @@ -1318,17 +1318,44 @@ static unsigned long long timestamp_calc(unsigned long long ts, int cpu, ts *= handle->ts2secs; }else if (handle->tsc_calc.mult) {/* auto calculated TSC clock frequency */ - ts -= handle->tsc_calc.offset;
And removed the calc offset.
lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1325
Quoted 7 lines
lib/trace-cmd/trace-input.c +1
-2ts =mul_u64_u32_shr (ts, handle->tsc_calc.mult, handle->tsc_calc.shift); }/* User specified time offset with --ts-offset or --date options */ -if (handle->ts_offset) - ts += handle->ts_offset; + ts += handle->ts_offset;
As we mentioned (and this can be a separate patch), the if statement is useless.
Quoted 5 lines
lib/trace-cmd/trace-input.c +1
-0return ts; } +static unsigned long long timestamp_calc(unsigned long long ts, int cpu,
lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1339
Quoted 10 lines
lib/trace-cmd/trace-input.c +10
-0+ struct tracecmd_input *handle) +{ +static int once; + + ts =pre_timestamp_calc (ts, cpu, handle); +if (!once && ts > handle->start_ts_offset) { + once++; +tracecmd_warning (); + } + ts -= handle->start_ts_offset;
After looking at this more, I think we should just have the ts_offset and start_ts_offset be the same. And remove the ts += handle->ts_offset, from the pre_timestamp_calc() above, and have this check test just ts_offset.
So now the timestamp_calc() will get the timestamp and then apply the ts_offset separately (and warn if the offset is greater than the ts).
lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1350
Quoted 11 lines
lib/trace-cmd/trace-input.c +11
-0+} + + + +long long tracecmd_cpu_first_ts_offset(struct tracecmd_input *handle, int cpu) +} +struct tep_record rec; + + rec =first_event (handle, cpu); +return pre_timestamp_calc (rec->ts, cpu, handle) - handle->ts_offset; +}
Add an API that shows the difference between the first stream event timestamp against the user supplied (or file supplied) ts_offset.
lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1355
Quoted 5 lines
lib/trace-cmd/trace-input.c +5
-0+ +int tracecmd_modify_ts_offset (struct tracecmd_input *handle,long long offset) +{ + handle->ts_offset += offset; +}
Allow the user to tweak that offset. As we already have:
tracecmd_set_ts_offset(handle, offset) to set ts_offset, if the user found that the offset was before, it could tweak it.
lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1361
Quoted 6 lines
lib/trace-cmd/trace-input.c +2
-0+ +/* * Page is mapped, now read in the page header info. */
That is, in the options, we would need to have the calc offset from the file (doing the sync), do:
handle->ts_offset -= start_offset.
Or something like that. I'll let you look at this code and see what you come up with, and we can discuss this further.
-- Steve
Re: [PATCH v2 2/3] trace-cmd library: Add check before applying tsc2nsec offset
Quoted 129 lines · Steven Rostedt
Quoted 15 lines · On Mon, 19 Apr 2021 09:45:43 -0400 Steven Rostedt <[email protected]> wrote:
That is:
max = 0; for each stream: ret = read first event, compared to offset if (!ret) { /* ret will be how much off by offset */ if (ret > max) max = ret; } if (max) { for each stream: Update offset by subtracting max }Quoted 4 lines
Look at each stream, and have some callback give you how much ahead the first event is from its given offset. Take the biggest value from reading all the streams, then tell all the streams to subtract its offset by that max value. The end result is that all streams now start at a positive value.
From our call, here's the pseudo code that I was talking about:
lib/trace-cmd/trace-input.c static unsigned long long timestamp_host_sync(unsigned long long ts, int cpu, · line 1306
Quoted 11 lines
lib/trace-cmd/trace-input.c +1
-1@@ -1302,7 +1302,7 @@ static unsigned long long timestamp_host_sync(unsigned long long ts, int cpu, &tsync->ts_samples[mid+1]); } -static unsigned long long timestamp_calc (unsigned long long ts,int cpu, +static unsigned long long pre_timestamp_calc (unsigned long long ts,int cpu,struct tracecmd_input *handle)I pulled out the
timestamp_calcinto a helper function.lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1321
Quoted 7 lines
lib/trace-cmd/trace-input.c +0
-1{/* do not modify raw timestamps */ @@ -1318,17 +1318,44 @@ static unsigned long long timestamp_calc(unsigned long long ts, int cpu, ts *= handle->ts2secs; }else if (handle->tsc_calc.mult) {/* auto calculated TSC clock frequency */ - ts -= handle->tsc_calc.offset;And removed the calc offset.
lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1325
Quoted 7 lines
lib/trace-cmd/trace-input.c +1
-2ts =mul_u64_u32_shr (ts, handle->tsc_calc.mult, handle->tsc_calc.shift); }/* User specified time offset with --ts-offset or --date options */ -if (handle->ts_offset) - ts += handle->ts_offset; + ts += handle->ts_offset;As we mentioned (and this can be a separate patch), the if statement is useless.
Quoted 5 lines
lib/trace-cmd/trace-input.c +1
-0return ts; } +static unsigned long long timestamp_calc(unsigned long long ts, int cpu,lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1339
Quoted 10 lines
lib/trace-cmd/trace-input.c +10
-0+ struct tracecmd_input *handle) +{ +static int once; + + ts =pre_timestamp_calc (ts, cpu, handle); +if (!once && ts > handle->start_ts_offset) { + once++; +tracecmd_warning (); + } + ts -= handle->start_ts_offset;After looking at this more, I think we should just have the
ts_offsetandstart_ts_offsetbe the same. And remove the ts +=handle->ts_offset, from thepre_timestamp_calc()above, and have this check test justts_offset.So now the
timestamp_calc()will get the timestamp and then apply thets_offsetseparately (and warn if the offset is greater than the ts).lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1350
Quoted 11 lines
lib/trace-cmd/trace-input.c +11
-0+} + + + +long long tracecmd_cpu_first_ts_offset(struct tracecmd_input *handle, int cpu) +} +struct tep_record rec; + + rec =first_event (handle, cpu); +return pre_timestamp_calc (rec->ts, cpu, handle) - handle->ts_offset; +}Add an API that shows the difference between the first stream event timestamp against the user supplied (or file supplied)
ts_offset.lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1355
Quoted 5 lines
lib/trace-cmd/trace-input.c +5
-0+ +int tracecmd_modify_ts_offset (struct tracecmd_input *handle,long long offset) +{ + handle->ts_offset += offset; +}Allow the user to tweak that offset. As we already have:
tracecmd_set_ts_offset(handle, offset)to setts_offset, if the user found that the offset was before, it could tweak it.lib/trace-cmd/trace-input.c static unsigned long long timestamp_calc(unsigned long long ts, int cpu, · line 1361
Quoted 6 lines
lib/trace-cmd/trace-input.c +2
-0+ +/* * Page is mapped, now read in the page header info. */ That is, in the options, we would need to have the calc offset from the file (doing the sync), do:
handle->ts_offset -= start_offset.Or something like that. I'll let you look at this code and see what you come up with, and we can discuss this further.
-- Steve
Quoted 12 lines · Steven Rostedt
Quoted 8 lines · On Fri, 16 Apr 2021 10:01:18 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 5 lines · On Fri, 16 Apr 2021 09:59:08 -0400 Steven Rostedt <[email protected]> wrote:
As for
pr_stat(), I think we should rename it totep_info()andtep_vinfo()that acts just liketep_warning(), except it is for informational output (stdout instead of stderr). This is similar to what the kernel has.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Since
tep_vwarning()takes a name, so cantep_vinfo(), and I was thinking that we should expose this string to the application.Oh, and we could add a
tep_critical()andtep_vcritical()which means that the error is something really bad happened, (like failed to allocate).Any thoughts on this?
These changes are superseded by "RFC [PATCH 0/5] tsc2nsec fixes", where some of these suggestions are implemented.
https://lore.kernel.org/linux-trace-devel/[email protected]/
--
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
PATCH v2 3/3 trace-cmd: Get the timestamp of the first recorded event as TSC offset
In reply to Tzvetomir Stoyanov (VMware)When converting TSC timestamps to nanoseconds, an offset is used. That offset is subtracted from each TSC timestamp, before the conversion. At the end of the trace, the lowest recorded TSC timestamp is selected as TSC offset, that will be used for tsc2nsec conversion.
--- tracecmd/include/trace-local.h | 1 + tracecmd/trace-record.c | 152 ++++++++++++++++++++++++++------- 2 files changed, 122 insertions(+), 31 deletions(-)
tracecmd/include/trace-local.h +1 -0
@@ -213,6 +213,7 @@ struct buffer_instance {char *name;struct tracefs_instance *tracefs;unsigned long long trace_id; +unsigned long long first_ts;char *cpumask;char *output_file;struct event_list *events;
tracecmd/trace-record.c +121 -31
198 lines not on this page · raw message
Signature · 1 line
2.30.2
Re: [PATCH v2 3/3] trace-cmd: Get the timestamp of the first recorded event as TSC offset
tracecmd/trace-record.c static void add_options(struct tracecmd_output *handle, struct common_record_con · line 4194
Quoted 7 lines · On Fri, 16 Apr 2021 13:34:09 +0300 "Tzvetomir Stoyanov (VMware)"…
tracecmd/trace-record.c +7
-0+int get_first_ts_instance (struct buffer_instance *instance) +{ +struct tracecmd_ts_corrections corrections; +unsigned long long first_ts =0 ; +enum kbuffer_long_size long_size; +enum kbuffer_endian endian; +struct kbuffer *kbuf =NULL ;
Small nit, but I'll leave it as it doesn't really hurt anything. But assigning kbuf to NULL isn't needed. It gets assigned in the kbuffer_alloc() before it is ever referenced, and the only error path before that is a return. But if we ever add another error path, it's safe to have it NULL.
-- Steve
Quoted 28 lines
tracecmd/trace-record.c +28
-0+unsigned long long ts; +unsigned int flags; +int first_ts_cpu; +bool first = true; +char *page; +char *file; +int psize; +int ret; +int i; + + psize =getpagesize (); + page =calloc (1 , psize); +if (!page) +return -1 ; + +if (tracecmd_host_bigendian ()) + endian =KBUFFER_ENDIAN_BIG ; +else + endian =KBUFFER_ENDIAN_LITTLE ; +if (sizeof (long ) ==8 ) + long_size =KBUFFER_LSIZE_8 ; +else + long_size =KBUFFER_LSIZE_4 ; + + kbuf =kbuffer_alloc (long_size, endian); +if (!kbuf) + goto out; +