Fix overflow when applying tsc2nsec calculations
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.
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 | 146 ++++++++++++++---- 5 files changed, 240 insertions(+), 154 deletions(-)
Signature · 1 line
2.30.2
PATCH 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 1/1 trace-cmd: Suppress trace library warnings
In reply to Tzvetomir Stoyanov (VMware)Suppress all warnings from libtraceevent, libtracefs and libtracecmd if the trace-cmd application does not run in debug mode.
--- tracecmd/trace-cmd.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
tracecmd/trace-cmd.c +17 -0
@@ -35,6 +35,23 @@ void warning(const char *fmt, ...)fprintf (stderr,"\n" ); } +int tep_vwarning (const char *name,const char *fmt,va_list ap) +{ +int ret = errno; + +if (!tracecmd_get_debug ()) +return ret; + +if (errno) +perror (name); + +fprintf (stderr," " ); +vfprintf (stderr, fmt, ap); +fprintf (stderr,"\n" ); + +return ret; +} +void pr_stat (const char *fmt, ...) {va_list ap;
Signature · 1 line
2.30.2
PATCH 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
PATCH 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 | 146 ++++++++++++++++++++++++++------- 2 files changed, 116 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 +115 -31
190 lines not on this page · raw message
Signature · 1 line
2.30.2
Re: [PATCH 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 4159
Quoted 6 lines · On Thu, 15 Apr 2021 11:15:27 +0300 "Tzvetomir Stoyanov (VMware)"…
tracecmd/trace-record.c +1
-1@@ -4183,7 +4156,100 @@ static void add_options(struct tracecmd_output *handle, struct common_record_confree (clocks); } -static void write_guest_file (struct buffer_instance *instance) +int get_first_ts (char *file,unsigned long long *ts)
const char *file,
Quoted 13 lines
tracecmd/trace-record.c +13
-0+{ +enum kbuffer_long_size long_size; +enum kbuffer_endian endian; +struct kbuffer *kbuf =NULL ; +int psize, rsize; +char *page =NULL ; +int ret =-1 ; +char *data; +int fd; + + fd =open (file,O_RDONLY ); +if (fd <0 ) +return -1 ;
tracecmd/trace-record.c static void add_options(struct tracecmd_output *handle, struct common_record_con · line 4182
Quoted 10 lines
tracecmd/trace-record.c +10
-0+ + psize =getpagesize (); + page =calloc (1 , psize); +if (!page) + goto error; + + rsize =read (fd, page, psize); +if (rsize <=0 ) + goto error; +
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
tracecmd/trace-record.c static void add_options(struct tracecmd_output *handle, struct common_record_con · line 4194
Quoted 12 lines
tracecmd/trace-record.c +12
-0+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 error;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The above should be moved into the calling function. The kbuffer_load_subbuffer() will reset the kbuf. No need to constantly allocate it and set it up each time.
Quoted 12 lines
tracecmd/trace-record.c +12
-0+ +kbuffer_load_subbuffer (kbuf, page); +if (kbuffer_subbuffer_size (kbuf) > rsize) + goto error; + + data =kbuffer_read_event (kbuf, ts); +if (!data) + goto error; + + ret =0 ; +error: +if (kbuf)
tracecmd/trace-record.c static void add_options(struct tracecmd_output *handle, struct common_record_con · line 4216
Quoted 10 lines
tracecmd/trace-record.c +10
-0+kbuffer_free (kbuf); +free (page); +close (fd); +return ret; +} + +void get_first_ts_instance (struct buffer_instance *instance) +{ +struct tracecmd_ts_corrections corrections; +unsigned long long first_ts =0 ;
I wouldn't use first_ts as the check to start. What happens if the first_ts is actually zero?
use bool first = true; or something.
tracecmd/trace-record.c static void add_options(struct tracecmd_output *handle, struct common_record_con · line 4223
Quoted 7 lines
tracecmd/trace-record.c +7
-0+unsigned long long ts; +unsigned int flags; +int first_ts_cpu; +char *file; +int ret; +int i; +
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 return;
And shouldn't this be returning these errors? Do we really want to continue if this doesn't work?
tracecmd/trace-record.c static void add_options(struct tracecmd_output *handle, struct common_record_con · line 4228
Quoted 5 lines
tracecmd/trace-record.c +5
-0+for (i =0 ; i < instance->cpu_count; i++) { + file =get_temp_file (instance, i); +if (!file) +continue ; +if (!get_first_ts (file, &ts)) {
if (!get_first_ts(kbuf, file, &ts)) {tracecmd/trace-record.c static void add_options(struct tracecmd_output *handle, struct common_record_con · line 4229
Quoted 1 line
tracecmd/trace-record.c +1
-0+if (!first_ts || ts < first_ts) {
if (first || ts < first_ts) {
first = false;tracecmd/trace-record.c static void add_options(struct tracecmd_output *handle, struct common_record_con · line 4235
Quoted 6 lines
tracecmd/trace-record.c +6
-0+ first_ts = ts; + first_ts_cpu = i; + } + } +put_temp_file (file); + }
No need to check if kbuf is not NULL, kbuffer_free() handles that.
kbuffer_free(kbuf);
tracecmd/trace-record.c static void add_options(struct tracecmd_output *handle, struct common_record_con · line 4236
Quoted 1 line
tracecmd/trace-record.c +1
-0+if (first_ts) {
if (!first) {Quoted 43 lines
tracecmd/trace-record.c +25
-0+if (is_guest (instance)) { + ret =tracecmd_tsync_get_proto_flags (instance->tsync, &flags); +if (ret) +return ; + ret =tracecmd_tsync_get_offsets (instance->tsync, first_ts_cpu, &corrections); +if (ret) +return ; + instance->first_ts =tracecmd_guest_ts_calc (first_ts, &corrections, flags); +free (corrections.ts_samples); + }else { + instance->first_ts = first_ts; + } + } +} + +static void write_guest_file (struct common_record_context *ctx,struct buffer_instance *instance) {struct tracecmd_output *handle;int cpu_count = instance->cpu_count;@@ -4201,6 +4267,12 @@ static void write_guest_file(struct buffer_instance *instance)die ("error writing to %s" , file);if (instance->flags &BUFFER_FL_TSC2NSEC )tracecmd_set_out_clock (handle,TSCNSEC_CLOCK ); + +if (ctx->tsc2nsec.mult) +add_tsc2nsec (handle, &ctx->tsc2nsec); +tracecmd_write_guest_time_shift (handle, instance->tsync); +tracecmd_append_options (handle); + temp_files =malloc (sizeof (*temp_files) * cpu_count);if (!temp_files) die("failed to allocate temp_files for %d cpus",@@ -4219,6 +4291,9 @@ static void write_guest_file(struct buffer_instance *instance)for (i =0 ; i < cpu_count; i++)put_temp_file (temp_files[i]);free (temp_files); + +tracecmd_tsync_free (instance->tsync); + instance->tsync =NULL ; }static void record_data (struct common_record_context *ctx)
tracecmd/trace-record.c static void record_data(struct common_record_context *ctx) · line 4311
Quoted 6 lines
tracecmd/trace-record.c +3
-0@@ -4231,9 +4306,18 @@ static void record_data(struct common_record_context *ctx)char **temp_files;int i; +if (ctx->tsc2nsec.mult) { +for_all_instances (instance) { +get_first_ts_instance (instance);
Should check the return code, and at least warn if it fails. As it is done after the recording, we don't want to die. But at least let the user know something went wrong.
-- Steve
Quoted 13 lines
tracecmd/trace-record.c +7
-1+if (instance->first_ts && + (!ctx->tsc2nsec.offset || ctx->tsc2nsec.offset > instance->first_ts)) + ctx->tsc2nsec.offset = instance->first_ts; + } + } +for_all_instances (instance) {if (is_guest (instance)) -write_guest_file (instance); +write_guest_file (ctx, instance);else if (host && instance->msg_handle)finish_network (instance->msg_handle);else
Re: [PATCH 3/3] trace-cmd: Get the timestamp of the first recorded event as TSC offset
Quoted 2 lines · On Thu, 15 Apr 2021 17:18:45 -0400 Steven Rostedt <[email protected]> wrote:
if (!kbuf) goto return;
And that was suppose to be "return;" not "goto return;" :-p
-- Steve
Re: [PATCH 3/3] trace-cmd: Get the timestamp of the first recorded event as TSC offset
In reply to Steven RostedtQuoted 1 line · Steven Rostedt
[...]
Quoted 13 lines
tracecmd/trace-record.c static void record_data(struct common_record_context *ctx) · line 4311
Quoted 8 lines
static void
record_data(struct common_record_context *ctx)tracecmd/trace-record.c +3
-0@@ -4231,9 +4306,18 @@ static void record_data(struct common_record_context *ctx)char **temp_files;int i; +if (ctx->tsc2nsec.mult) { +for_all_instances (instance) { +get_first_ts_instance (instance);Should check the return code, and at least warn if it fails. As it is done after the recording, we don't want to die. But at least let the user know something went wrong.
Quoted 3 lines
-- Steve
Quoted 1 line
tracecmd/trace-record.c +1
-0+ if (instance->first_ts &&
I'll add return code and will check it instead of that "instance->first_ts". Failing to get the first ts could be a normal case, if there are no events recorded in that instance.
Quoted 13 lines
Quoted 12 lines
tracecmd/trace-record.c +6
-1+ (!ctx->tsc2nsec.offset || ctx->tsc2nsec.offset > instance->first_ts)) + ctx->tsc2nsec.offset = instance->first_ts; + } + } +for_all_instances (instance) {if (is_guest (instance)) -write_guest_file (instance); +write_guest_file (ctx, instance);else if (host && instance->msg_handle)finish_network (instance->msg_handle);else
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov VMware Open Source Technology Center