trace-cmd: Align guest TSC calculation with the kernel
v2 cover trace-cmd: Align guest TSC calculation with the kernel
The formula for calculating guest TSC clock, used in the kernel is:
guest_tsc = tsc-offset + (host_tsc * tsc-scaling-ratio) >> tsc-scaling-ratio-frac-bits
Current trace-cmd implementation does not take into account fraction bits, when calculating guest clock offset in case of a clock scaling. Additional logic is added, to use the same formula in trace-cmd. The changes are backward compatible with trace-cmd 2.9.
The changes are not tested with a kernel that has guest clock scaling enabled. The goal of the patch set is to extend the time sync option, saved in the guest trace.dat file, with all needed data used in the kernel for guest TSC calculations.
v2 changes:
- Use strcmp() instead of strncmp() when searching for KVM debug files.
- Added more comments.
- Code clean ups.
Tzvetomir Stoyanov (VMware) (4):
trace-cmd library: Do not use strncmp() when searching for KVM debug
files
trace-cmd library: Extend host-guest time sync with fraction bits trace-cmd library: Read and use fraction bits from
TRACECMD_OPTION_TIME_SHIFT
trace-cmd: Dump fraction bits from TRACECMD_OPTION_TIME_SHIFT
.../include/private/trace-cmd-private.h | 4 +- lib/trace-cmd/include/trace-tsync-local.h | 7 +- lib/trace-cmd/trace-input.c | 79 +++++++------- lib/trace-cmd/trace-timesync-kvm.c | 62 +++++++---- lib/trace-cmd/trace-timesync-ptp.c | 4 +- lib/trace-cmd/trace-timesync.c | 55 ++++++++-- tracecmd/trace-dump.c | 102 +++++++++++++----- 7 files changed, 210 insertions(+), 103 deletions(-)
Signature · 1 line
2.31.1
PATCH v2 1/4 trace-cmd library: Do not use strncmp() when searching for KVM debug files
Using strncmp() when looking for KVM debug files with given names in the debugfs is useless and could be dangerous, strcmp() should be used instead. Limiting the string compare to the name of the searched file only could bring problems, in case of files with overlapping names.
--- lib/trace-cmd/trace-timesync-kvm.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
lib/trace-cmd/trace-timesync-kvm.c +2 -4
@@ -163,14 +163,12 @@ static int kvm_open_vcpu_dir(struct kvm_clock_sync *kvm, int cpu, char *dir_str) goto error;while ((entry =readdir (dir))) {if (entry->d_type !=DT_DIR ) { -if (!strncmp (entry->d_name,KVM_DEBUG_OFFSET_FILE , -strlen (KVM_DEBUG_OFFSET_FILE ))) { +if (!strcmp (entry->d_name,KVM_DEBUG_OFFSET_FILE )) {snprintf (path,sizeof (path),"%s/%s" , dir_str, entry->d_name); kvm->vcpu_offsets[cpu] =strdup (path); } -if (!strncmp (entry->d_name,KVM_DEBUG_SCALING_FILE , -strlen (KVM_DEBUG_SCALING_FILE ))) { +if (!strcmp (entry->d_name,KVM_DEBUG_SCALING_FILE )) {snprintf (path,sizeof (path),"%s/%s" , dir_str, entry->d_name); kvm->vcpu_scalings[cpu] =strdup (path);
Signature · 1 line
2.31.1
PATCH v2 2/4 trace-cmd library: Extend host-guest time sync with fraction bits
In reply to Tzvetomir Stoyanov (VMware)The formula, used in the Linux kernel for guest TSC clock is:
guest_tsc = tsc-offset + (host_tsc * tsc-scaling-ratio) >> tsc-scaling-ratio-frac-bits
The KVM hypervisor exports these parameters in the debugfs, if clock scaling is supported:
/sys/kernel/debug/kvm:
tsc-offset
tsc-scaling-ratio
tsc-scaling-ratio-frac-bits
Current trace-cmd implementation does not take into account fraction bits, when calculating guest clock offset in case of a clock scaling. The TRACECMD_OPTION_TIME_SHIFT option is extended with per-cpu array with fraction bits. KVM and PTP algorithms are extended to get fraction:
- in PTP, there are fixed scaling=1 and fraction=0;
- KVM gets these from KVM debugfs, if available.
The formula from the kernel is implemented in trace-cmd library, when calculating timestamps of the guest events.
This change is backward compatible with trace-cmd 2.9, where the TRACECMD_OPTION_TIME_SHIFT option is introduced.
--- .../include/private/trace-cmd-private.h | 4 +- lib/trace-cmd/include/trace-tsync-local.h | 7 ++- lib/trace-cmd/trace-timesync-kvm.c | 56 ++++++++++++++----- lib/trace-cmd/trace-timesync-ptp.c | 4 +- lib/trace-cmd/trace-timesync.c | 55 ++++++++++++++---- 5 files changed, 96 insertions(+), 30 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -2
@@ -454,8 +454,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); +int *count,long long **ts, +long long **offsets,long long **scalings,long long **frac);int tracecmd_tsync_get_session_params (struct tracecmd_time_sync *tsync,char **selected_proto,unsigned int *tsync_port);
lib/trace-cmd/include/trace-tsync-local.h +4 -3
@@ -27,14 +27,15 @@ struct tracecmd_time_sync {struct clock_sync_offsets {/* Arrays with calculated time offsets at given time */ int sync_size;/* Allocated size of sync_ts, -* sync_offsets and sync_scalings +* sync_offsets, sync_scalings and sync_frac */ int sync_count;/* Number of elements in sync_ts, -* sync_offsets and sync_scalings +* sync_offsets, sync_scalings and sync_frac */ long long *sync_ts;long long *sync_offsets;long long *sync_scalings; +long long *sync_frac; };struct clock_sync_context {
@@ -60,7 +61,7 @@ int tracecmd_tsync_proto_register(const char *proto_name, int accuracy, int roleint (*init)(struct tracecmd_time_sync *), int (*free)(struct tracecmd_time_sync *), int (*calc)(struct tracecmd_time_sync *, -long long *,long long *, +long long *,long long *,long long *, long long *, unsigned int));int tracecmd_tsync_proto_unregister (char *proto_name);
lib/trace-cmd/trace-timesync-kvm.c +42 -14
180 lines not on this page · raw message
lib/trace-cmd/trace-timesync-ptp.c +3 -1
@@ -663,7 +663,7 @@ static int ptp_clock_server(struct tracecmd_time_sync *tsync, }static int ptp_clock_sync_calc (struct tracecmd_time_sync *tsync, -long long *offset,long long *scaling, +long long *offset,long long *scaling,long long *frac,long long *timestamp,unsigned int cpu) {struct clock_sync_context *clock_context;
@@ -689,6 +689,8 @@ static int ptp_clock_sync_calc(struct tracecmd_time_sync *tsync,if (scaling) *scaling =1 ; +if (frac) + *frac =0 ;if (clock_context->is_server) ret =ptp_clock_server (tsync, offset, timestamp);else
lib/trace-cmd/trace-timesync.c +45 -10
170 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v2 3/4 trace-cmd library: Read and use fraction bits from TRACECMD_OPTION_TIME_SHIFT
In reply to Tzvetomir Stoyanov (VMware)If there is array with guest time scaling fraction bits in TRACECMD_OPTION_TIME_SHIFT option, read that information.
The formula for calculating guest timestamps is aligned with the formula used in the kernel:
guest_tsc = tsc-offset + (host_tsc * tsc-scaling-ratio) >> tsc-scaling-ratio-frac-bits
--- lib/trace-cmd/trace-input.c | 79 ++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 40 deletions(-)
lib/trace-cmd/trace-input.c +39 -40
@@ -83,6 +83,7 @@ struct ts_offset_sample {long long time;long long offset;long long scaling; +long long fraction; };struct guest_trace_info {
@@ -1245,7 +1246,6 @@ timestamp_correction_calc(unsigned long long ts, unsigned int flags,struct ts_offset_sample *min, struct ts_offset_sample *max) { - long long scaling; long long tscor;if (flags &TRACECMD_TSYNC_FLAG_INTERPOLATE ) {
@@ -1253,15 +1253,12 @@ timestamp_correction_calc(unsigned long long ts, unsigned int flags,long long offset = ((long long )ts - min->time) * (max->offset - min->offset); - scaling = (min->scaling + max->scaling) /2 ; tscor = min->offset + (offset + delta /2 ) / delta; - }else { - scaling = min->scaling; tscor = min->offset; } - ts *= scaling; + ts = (ts * min->scaling) >> min->fraction;if (tscor <0 )return ts -llabs (tscor);
@@ -2347,37 +2344,16 @@ static int tsync_offset_cmp(const void *a, const void *b)#define safe_read_loop (type) \ do { \ - int i; \ - for (i = 0; i < ts_offsets->ts_samples_count; i++) \ - safe_read(ts_offsets->ts_samples[i].type, 8); \ + int ii; \ + for (ii = 0; ii < ts_offsets->ts_samples_count; ii++) \ + safe_read(ts_offsets->ts_samples[ii].type, 8); \ } while (0) -static int tsync_offset_load (struct tep_handle *tep, -struct timesync_offsets *ts_offsets,char *buf,int size) -{ -int start_size = size; -int i, j; - -safe_read_loop (time); -safe_read_loop (offset); -safe_read_loop (scaling); -qsort (ts_offsets->ts_samples, ts_offsets->ts_samples_count, -sizeof (struct ts_offset_sample ), tsync_offset_cmp); -/* Filter possible samples with equal time */ -for (i =0 , j =0 ; i < ts_offsets->ts_samples_count; i++) { -if (i ==0 || ts_offsets->ts_samples[i].time != ts_offsets->ts_samples[i-1 ].time) - ts_offsets->ts_samples[j++] = ts_offsets->ts_samples[i]; - } - ts_offsets->ts_samples_count = j; - -return start_size - size; -} -static int tsync_cpu_offsets_load (struct tracecmd_input *handle,char *buf,int size) {struct tep_handle *tep = handle->pevent; -int ret; -int i; +struct timesync_offsets *ts_offsets; +int i, j, k;safe_read (handle->host.cpu_count,4 ); handle->host.ts_offsets = calloc(handle->host.cpu_count,
@@ -2385,17 +2361,36 @@ static int tsync_cpu_offsets_load(struct tracecmd_input *handle, char *buf, intif (!handle->host.ts_offsets)return -ENOMEM ;for (i =0 ; i < handle->host.cpu_count; i++) { -safe_read (handle->host.ts_offsets[i].ts_samples_count,4 ); - handle->host.ts_offsets[i].ts_samples =calloc (handle->host.ts_offsets[i].ts_samples_count, -sizeof (struct ts_offset_sample )); -if (!handle->host.ts_offsets[i].ts_samples) + ts_offsets = &handle->host.ts_offsets[i]; +safe_read (ts_offsets->ts_samples_count,4 ); + ts_offsets->ts_samples =calloc (ts_offsets->ts_samples_count, +sizeof (struct ts_offset_sample )); +if (!ts_offsets->ts_samples)return -ENOMEM ; - ret =tsync_offset_load (tep, &handle->host.ts_offsets[i], buf, size); -if (ret <=0 ) -return -EFAULT ; - size -= ret; - buf += ret; +safe_read_loop (time); +safe_read_loop (offset); +safe_read_loop (scaling); } + +if (size >0 ) { +for (i =0 ; i < handle->host.cpu_count; i++) { + ts_offsets = &handle->host.ts_offsets[i]; +safe_read_loop (fraction); + } + } + +for (i =0 ; i < handle->host.cpu_count; i++) { + ts_offsets = &handle->host.ts_offsets[i]; +qsort (ts_offsets->ts_samples, ts_offsets->ts_samples_count, +sizeof (struct ts_offset_sample ), tsync_offset_cmp); +/* Filter possible samples with equal time */ +for (k =0 , j =0 ; k < ts_offsets->ts_samples_count; k++) { +if (k ==0 || ts_offsets->ts_samples[k].time != ts_offsets->ts_samples[k-1 ].time) + ts_offsets->ts_samples[j++] = ts_offsets->ts_samples[k]; + } + ts_offsets->ts_samples_count = j; + } +return 0 ; }
@@ -2722,6 +2717,10 @@ static int handle_options(struct tracecmd_input *handle)* long long array of size [count] of timestamp offsets. * long long array of size [count] of timestamp scaling ratios.* * ] +* array of size [CPU count]: +* [ +* long long array of size [count] of timestamp scaling fraction bits.* +* ]* */ if (size <16 || (handle->flags &TRACECMD_FL_RAW_TS ))break ;
Signature · 1 line
2.31.1
PATCH v2 4/4 trace-cmd: Dump fraction bits from TRACECMD_OPTION_TIME_SHIFT
In reply to Tzvetomir Stoyanov (VMware)As the TRACECMD_OPTION_TIME_SHIFT option is extended with new data, fraction bits used to calculate the guest TSC clock in case of clock scaling, it will be useful to dump that new information.
--- tracecmd/trace-dump.c | 102 ++++++++++++++++++++++++++++++------------ 1 file changed, 73 insertions(+), 29 deletions(-)
tracecmd/trace-dump.c +73 -29
@@ -376,13 +376,18 @@ static void dump_option_xlong(int fd, int size, char *desc)do_print (OPTIONS ,"0x%llX\n" , val); } +struct time_shift_cpu { +unsigned int count; +long long *scalings; +long long *frac; +long long *offsets; +unsigned long long *times; +}; +static void dump_option_timeshift (int fd,int size) { -long long *scalings =NULL ; -long long *offsets =NULL ; -unsigned long long *times =NULL ; +struct time_shift_cpu *cpus_data;long long trace_id; -unsigned int count;unsigned int flags;unsigned int cpus;int i, j;
@@ -400,41 +405,80 @@ static void dump_option_timeshift(int fd, int size) }do_print (OPTIONS ,"\t\t[Option TimeShift, %d bytes]\n" , size);read_file_number (fd, &trace_id,8 ); + size -=8 ;do_print (OPTIONS ,"0x%llX [peer's trace id]\n" , trace_id);read_file_number (fd, &flags,4 ); + size -=4 ;do_print (OPTIONS ,"0x%llX [peer's protocol flags]\n" , flags);read_file_number (fd, &cpus,4 ); + size -=4 ;do_print (OPTIONS ,"0x%llX [peer's CPU count]\n" , cpus); + cpus_data =calloc (cpus,sizeof (struct time_shift_cpu )); +if (!cpus_data) +return ;for (j =0 ; j < cpus; j++) { -read_file_number (fd, &count,4 ); -do_print (OPTIONS ,"%lld [samples count for CPU %d]\n" , count, j); - times =calloc (count,sizeof (long long )); - offsets =calloc (count,sizeof (long long )); - scalings =calloc (count,sizeof (long long )); -if (!times || !offsets || !scalings) +if (size <4 ) goto out; -for (i =0 ; i < count; i++) -read_file_number (fd, times + i,8 ); -for (i =0 ; i < count; i++) -read_file_number (fd, offsets + i,8 ); -for (i =0 ; i < count; i++) -read_file_number (fd, scalings + i,8 ); - -for (i =0 ; i < count; i++) -do_print (OPTIONS ,"\t%lld %lld %llu [offset * scaling @ time]\n" , - offsets[i], scalings[1 ], times[i]); -free (times); -free (offsets); -free (scalings); - times =NULL ; - offsets =NULL ; - scalings =NULL ; +read_file_number (fd, &cpus_data[j].count,4 ); + size -=4 ; +do_print (OPTIONS ,"%lld [samples count for CPU %d]\n" , cpus_data[j].count, j); + cpus_data[j].times =calloc (cpus_data[j].count,sizeof (long long )); + cpus_data[j].offsets =calloc (cpus_data[j].count,sizeof (long long )); + cpus_data[j].scalings =calloc (cpus_data[j].count,sizeof (long long )); + cpus_data[j].frac =calloc (cpus_data[j].count,sizeof (long long )); +if (!cpus_data[j].times || !cpus_data[j].offsets || + !cpus_data[j].scalings || !cpus_data[j].frac) + goto out; +for (i =0 ; i < cpus_data[j].count; i++) { +if (size <8 ) + goto out; +read_file_number (fd, cpus_data[j].times + i,8 ); + size -=8 ; + } +for (i =0 ; i < cpus_data[j].count; i++) { +if (size <8 ) + goto out; +read_file_number (fd, cpus_data[j].offsets + i,8 ); + size -=8 ; + } +for (i =0 ; i < cpus_data[j].count; i++) { +if (size <8 ) + goto out; +read_file_number (fd, cpus_data[j].scalings + i,8 ); + size -=8 ; + } + } +if (size >0 ) { +for (j =0 ; j < cpus; j++) { +if (!cpus_data[j].frac) + goto out; +for (i =0 ; i < cpus_data[j].count; i++) { +if (size <8 ) + goto out; +read_file_number (fd, cpus_data[j].frac + i,8 ); + size -=8 ; + } + } } + +for (j =0 ; j < cpus; j++) { +for (i =0 ; i < cpus_data[j].count; i++) +do_print (OPTIONS ,"\t%lld %lld %llu %llu[offset * scaling >> fraction @ time]\n" , + cpus_data[j].offsets[i], cpus_data[j].scalings[i], + cpus_data[j].frac[i], cpus_data[j].times[i]); + + } + out: -free (times); -free (offsets); -free (scalings); +if (j < cpus) +do_print (OPTIONS ,"Broken time shift option\n" ); +for (j =0 ; j < cpus; j++) { +free (cpus_data[j].times); +free (cpus_data[j].offsets); +free (cpus_data[j].scalings); + } +free (cpus_data); }void dump_option_guest (int fd,int size)
Signature · 1 line
2.31.1
Re: [PATCH v2 4/4] trace-cmd: Dump fraction bits from TRACECMD_OPTION_TIME_SHIFT
Quoted 53 lines · On Thu, 14 Oct 2021 18:02:04 +0300 "Tzvetomir Stoyanov (VMware)"…
(quoted diff) +49
-0+ read_file_number(fd, &cpus_data[j].count, 4); + size -= 4; + do_print(OPTIONS, "%lld [samples count for CPU %d]\n", cpus_data[j].count, j); + cpus_data[j].times = calloc(cpus_data[j].count, sizeof(long long)); + cpus_data[j].offsets = calloc(cpus_data[j].count, sizeof(long long)); + cpus_data[j].scalings = calloc(cpus_data[j].count, sizeof(long long)); + cpus_data[j].frac = calloc(cpus_data[j].count, sizeof(long long)); + if (!cpus_data[j].times || !cpus_data[j].offsets || + !cpus_data[j].scalings || !cpus_data[j].frac) + goto out; + for (i = 0; i < cpus_data[j].count; i++) { + if (size < 8) + goto out; + read_file_number(fd, cpus_data[j].times + i, 8); + size -= 8; + } + for (i = 0; i < cpus_data[j].count; i++) { + if (size < 8) + goto out; + read_file_number(fd, cpus_data[j].offsets + i, 8); + size -= 8; + } + for (i = 0; i < cpus_data[j].count; i++) { + if (size < 8) + goto out; + read_file_number(fd, cpus_data[j].scalings + i, 8); + size -= 8; + } + } + if (size > 0) { + for (j = 0; j < cpus; j++) { + if (!cpus_data[j].frac) + goto out; + for (i = 0; i < cpus_data[j].count; i++) { + if (size < 8) + goto out; + read_file_number(fd, cpus_data[j].frac + i, 8); + size -= 8; + } + } } + + for (j = 0; j < cpus; j++) { + for (i = 0; i < cpus_data[j].count; i++) + do_print(OPTIONS, "\t%lld %lld %llu %llu[offset * scaling >> fraction @ time]\n", + cpus_data[j].offsets[i], cpus_data[j].scalings[i], + cpus_data[j].frac[i], cpus_data[j].times[i]); + + } +
Review
Quoted 10 lines
(quoted diff) +6
-3out: - free(times); - free(offsets); - free(scalings); + if (j < cpus) + do_print(OPTIONS, "Broken time shift option\n"); + for (j = 0; j < cpus; j++) { + free(cpus_data[j].times); + free(cpus_data[j].offsets); + free(cpus_data[j].scalings);
Looks like you forgot to add:
free(cpus_data[j].frac);
This is the only issue I found in this series. I'll just add a patch to fix it and move on.
Thanks!
-- Steve
Quoted 2 lines
(quoted diff) +2
-0+ } + free(cpus_data);