Timestamp offsets calculation per host CPU
v2 cover Timestamp offsets calculation per host CPU
The clock, used for trace time stamps, can vary between CPUs. When synchronizing host and guest trace time stamps, these variations could introduce inaccuracy.
The algorithm for host - guest trace time stamps synchronization is modified to calculate the offset per each host CPU, depending on the CPU affinity of host tasks, running the guest's VCPUs. The calculated array per CPU is stored in the trace.dat file, using TRACECMD_OPTION_TIME_SHIFT option.
The "trace-cmd dump --options" command and tracecmd_init_data() API are adjusted to read the new format of this option.
In order this per CPU synchronization data to be used when host and guest files are merged, information about guest VCPUs migration between host CPUs is needed. This information is extracted from host trace data, thus the host trace must be recored with "-e sched" option.
A new API tracecmd_open_merge() is introduced, which triggers this per CPU calculations when opening tracing files. This API must be used by KernelShark when opening host and guest files. There is a separate patch for this, not part of the series.
Tzvetomir Stoyanov (VMware) (5):
trace-cmd: Time stamp offset per host CPU
trace-cmd: Fix reading of the traceid option from trace.dat file
trace-cmd: Add new API to merge two trace files
trace-cmd: Add a define to enable per CPU timestamps synchronization
trace-cmd: Use per CPU synchronization data when calculating
timestamps offsets
include/trace-cmd/trace-cmd.h | 7 +- lib/trace-cmd/include/trace-tsync-local.h | 16 +- lib/trace-cmd/trace-input.c | 516 +++++++++++++++++++--- lib/trace-cmd/trace-timesync.c | 269 +++++++++-- tracecmd/include/trace-local.h | 4 + tracecmd/trace-dump.c | 60 ++- tracecmd/trace-record.c | 18 + tracecmd/trace-tsync.c | 144 +++--- 8 files changed, 832 insertions(+), 202 deletions(-)
Signature · 1 line
2.25.1
PATCH v2 1/5 trace-cmd: Time stamp offset per host CPU
The clock, used for trace time stamps, can vary between CPUs. When synchronizing host and guest trace time stamps, these variations could introduce inaccuracy.
The algorithm for host - guest trace time stamps synchronization is modified to calculate the offset per each host CPU, depending on the CPU affinity of host tasks, running the guest's VCPUs. The calculated array per CPU is stored in the trace.dat file, using TRACECMD_OPTION_TIME_SHIFT option.
The "trace-cmd dump --options" command and tracecmd_init_data() API are adjusted to read the new format of this option.
--- include/trace-cmd/trace-cmd.h | 6 +- lib/trace-cmd/include/trace-tsync-local.h | 16 +- lib/trace-cmd/trace-input.c | 184 +++++++++++---- lib/trace-cmd/trace-timesync.c | 269 +++++++++++++++++++--- tracecmd/include/trace-local.h | 4 + tracecmd/trace-dump.c | 60 +++-- tracecmd/trace-record.c | 18 ++ tracecmd/trace-tsync.c | 141 ++++++------ 8 files changed, 516 insertions(+), 182 deletions(-)
include/trace-cmd/trace-cmd.h +5 -1
@@ -431,6 +431,8 @@ struct tracecmd_time_sync {char *clock_str;struct tracecmd_msg_handle *msg_handle;void *context; +int cpu_max; +int *cpu_pid; };void tracecmd_tsync_init (void );
@@ -440,8 +442,10 @@ bool tsync_proto_is_supported(unsigned int proto_id);void tracecmd_tsync_with_host (struct tracecmd_time_sync *tsync);void tracecmd_tsync_with_guest (struct tracecmd_time_sync *tsync);int tracecmd_tsync_get_offsets (struct tracecmd_time_sync *tsync, -int *count, +int cpu_index,int *cpu,int *count,long long **ts,long long **offsets); +int tracecmd_tsync_get_cpu_count (struct tracecmd_time_sync *tsync, +int *cpu_count,int *max_cpu);void tracecmd_tsync_free (struct tracecmd_time_sync *tsync);/* --- Plugin handling --- */
lib/trace-cmd/include/trace-tsync-local.h +11 -5
@@ -8,16 +8,22 @@#include <stdbool.h> +struct clock_sync_cpu { +int cpu; +int sync_size;/* Allocated size of sync_ts and sync_offsets */ +int sync_count;/* Number of elements in sync_ts and sync_offsets */ +long long *sync_ts; +long long *sync_offsets; +}; +struct clock_sync_context {void *proto_data;/* time sync protocol specific data */ bool is_server;/* server side time sync role */ struct tracefs_instance *instance;/* ftrace buffer, used for time sync events */ -/* Arrays with calculated time offsets at given time */ -int sync_size;/* Allocated size of sync_ts and sync_offsets */ -int sync_count;/* Number of elements in sync_ts and sync_offsets */ -long long *sync_ts; -long long *sync_offsets; +/* Arrays with calculated time offsets at given time, per each host CPU */ +int cpu_sync_size; +struct clock_sync_cpu *cpu_sync;/* Identifiers of local and remote time sync peers: cid and port */ unsigned int local_cid;
lib/trace-cmd/trace-input.c +135 -49
270 lines not on this page · raw message
lib/trace-cmd/trace-timesync.c +235 -34
371 lines not on this page · raw message
tracecmd/include/trace-local.h +4 -0
@@ -280,6 +280,10 @@ int trace_open_vsock(unsigned int cid, unsigned int port);char *trace_get_guest_file (const char *file,const char *guest); +int trace_get_guest_cpu_mapping (unsigned int guest_cid, +int *cpu_max,int **cpu_pid); + +/* No longer in event-utils.h */ void __noreturn die (const char *fmt, ...);/* Can be overriden */ void *malloc_or_die (unsigned int size);/* Can be overridden */
tracecmd/trace-dump.c +40 -20
78 lines not on this page · raw message
tracecmd/trace-record.c +18 -0
@@ -3088,6 +3088,24 @@ static struct guest *get_guest_info(unsigned int guest_cid)return NULL ; } +int trace_get_guest_cpu_mapping (unsigned int guest_cid, +int *cpu_max,int **cpu_pid) +{ +struct guest *guest; + + guest =get_guest_info (guest_cid); +if (!guest) +return -1 ; + +if (cpu_pid) + *cpu_pid = guest->cpu_pid; +if (cpu_max) + *cpu_max = guest->cpu_max; + +return 0 ; +} + +static char *get_qemu_guest_name (char *arg) {char *tok, *end = arg;
tracecmd/trace-tsync.c +68 -73
203 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v2 2/5 trace-cmd: Fix reading of the traceid option from trace.dat file
In reply to Tzvetomir Stoyanov (VMware)Due to a copy / paste error, the reading of traceid option from trace.dat file is broken. There is no impact to the user, as this option is not used yet.
--- lib/trace-cmd/trace-input.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
lib/trace-cmd/trace-input.c +3 -1
@@ -2640,8 +2640,10 @@ static int handle_options(struct tracecmd_input *handle)trace_pid_map_load (handle, buf);break ;case TRACECMD_OPTION_TRACEID : +if (size !=8 ) +break ; handle->trace_id =tep_read_number (handle->pevent, - &cpus,8 ); + buf,8 );break ;case TRACECMD_OPTION_GUEST :trace_guest_load (handle, buf, size);
Signature · 1 line
2.25.1
PATCH v2 3/5 trace-cmd: Add new API to merge two trace files
In reply to Tzvetomir Stoyanov (VMware)The new API
struct tracecmd_input *tracecmd_open_merge(const char *file, const char *primary_file);
can be used to open a trace file and adjust its timestamps to the primary_file, if both were recorded in one tracing session.
A tracecmd_input handler to the file is allocated and returned.
The timestamps of all events are adjusted according to the time synchronization data, recorded in the file.
The API checks if the tracing session from the both files match.
In case they do not match, the timestamps are not corrected.
--- include/trace-cmd/trace-cmd.h | 1 + lib/trace-cmd/trace-input.c | 89 +++++++++++++++++++++++++++++------ 2 files changed, 76 insertions(+), 14 deletions(-)
include/trace-cmd/trace-cmd.h +1 -0
@@ -142,6 +142,7 @@ typedef void (*tracecmd_handle_init_func)(struct tracecmd_input *handle,struct tracecmd_input *tracecmd_alloc (const char *file);struct tracecmd_input *tracecmd_alloc_fd (int fd);struct tracecmd_input *tracecmd_open (const char *file); +struct tracecmd_input *tracecmd_open_merge (const char *file,const char *primary_file);struct tracecmd_input *tracecmd_open_fd (int fd);void tracecmd_ref (struct tracecmd_input *handle);void tracecmd_close (struct tracecmd_input *handle);
lib/trace-cmd/trace-input.c +75 -14
159 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v2 4/5 trace-cmd: Add a define to enable per CPU timestamps synchronization
In reply to Tzvetomir Stoyanov (VMware)The new define TSYNC_PER_CPU, enabled by default, can be used to enable the per CPU timestamps synchronization. It is mostly for development purposes, to compare this new logic to the legacy one, where a single CPU is used.
--- tracecmd/trace-tsync.c | 3 +++ 1 file changed, 3 insertions(+)
tracecmd/trace-tsync.c +3 -0
@@ -31,6 +31,7 @@ static void *tsync_host_thread(void *data)pthread_exit (0 ); } +#define TSYNC_PER_CPU int tracecmd_host_tsync (struct buffer_instance *instance,unsigned int tsync_port) {
@@ -54,9 +55,11 @@ int tracecmd_host_tsync(struct buffer_instance *instance, goto out; } +#ifdef TSYNC_PER_CPU ret =trace_get_guest_cpu_mapping (instance->cid, &instance->tsync.cpu_max, &instance->tsync.cpu_pid); +#endif instance->tsync.msg_handle = msg_handle;if (top_instance.clock)
Signature · 1 line
2.25.1
PATCH v2 5/5 trace-cmd: Use per CPU synchronization data when calculating timestamps offsets
In reply to Tzvetomir Stoyanov (VMware)When per CPU synchronization data is available in trace.dat file, use them when synchronizing event timestamps between host and guest machines.
This change is for testing purposes only, the code is not optimized and relies on few assumptions. To use the per CPU data, the host sched events of tasks, running each guest VCPU, must be available in the host tracing file. The host tracing must be recorded with at least the "-e sched" option. The migration of VCPUs between host CPUs is extracted using these events from the host trace file. This information is mandatory for the algorithm.
--- lib/trace-cmd/trace-input.c | 269 +++++++++++++++++++++++++++++++++--- 1 file changed, 249 insertions(+), 20 deletions(-)
lib/trace-cmd/trace-input.c +249 -20
@@ -77,6 +77,16 @@ struct input_buffer_instance {size_t offset; }; +struct ts_sched_cpu { +unsigned long long ts; +int host_cpu; +}; + +struct vcpu_sched_data { +int cpu_sched_count; +struct ts_sched_cpu *cpu_sched; +}; +struct ts_offset_sample {long long time;long long offset;
@@ -99,6 +109,8 @@ struct host_trace_info {unsigned long long peer_trace_id;bool sync_enable;struct tracecmd_input *peer_data; +int vcpu_count; +struct vcpu_sched_data *vcpu_sched;int cpu_count;struct ts_offset_cpu *cpu_time_offsets; };
@@ -1119,27 +1131,12 @@ timestamp_correction_calc(unsigned long long ts, struct ts_offset_sample *min,return ts + tscor; } -static unsigned long long timestamp_correct (unsigned long long ts, -struct tracecmd_input *handle) +static unsigned long long timestamp_correct_cpu (unsigned long long ts, +struct ts_offset_cpu *cpu_offset) { -struct host_trace_info *host = &handle->host; -struct ts_offset_cpu *cpu_offset =NULL ;int min, mid, max; -int i; -if (handle->ts_offset) -return ts + handle->ts_offset; -if (!host->sync_enable) -return ts; - -/* ToDo - find actual host CPU of this guest ts */ -for (i =0 ; i < host->cpu_count; i++) { -if (host->cpu_time_offsets[i].ts_samples_count) { - cpu_offset = &host->cpu_time_offsets[i]; -break ; - } - }if (!cpu_offset)return ts;
@@ -1178,6 +1175,90 @@ static unsigned long long timestamp_correct(unsigned long long ts, &cpu_offset->ts_samples[mid+1]); } +static struct ts_offset_cpu *timestamp_host_cpu_offset (struct host_trace_info *host, +int cpu) +{ +int i; + +if (cpu >=0 && cpu < host->cpu_count) +return host->cpu_time_offsets + cpu; + +if (cpu <0 ) { +for (i =0 ; i < host->cpu_count; i++) { +if (host->cpu_time_offsets[i].ts_samples_count) +return host->cpu_time_offsets + i; + } + } + +return NULL ; +} + +static int timestamp_vcpu_to_cpu_ts (struct host_trace_info *host, +int vcpu,unsigned long long ts) +{ +struct vcpu_sched_data *sched; +int min, mid, max; + +if (vcpu <0 || vcpu >= host->vcpu_count || !host->vcpu_sched) +return -1 ; + + sched = host->vcpu_sched + vcpu; +if (!sched) +return -1 ; + + min =0 ; + max = sched->cpu_sched_count -1 ; + mid = (min + max)/2 ; +while (min <= max) { +if (ts < sched->cpu_sched[mid].ts) + max = mid -1 ; +else if (ts > sched->cpu_sched[mid].ts) + min = mid +1 ; +else +break ; + mid = (min + max)/2 ; + } + +return sched->cpu_sched[mid].host_cpu; + +} + +static int timestamp_vcpu_to_cpu (struct host_trace_info *host, +int vcpu, +unsigned long long ts) +{ +struct ts_offset_cpu *cpu_offset =NULL ; +unsigned long long ts_guess; +int cpu; + + cpu_offset =timestamp_host_cpu_offset (host,-1 ); + ts_guess =timestamp_correct_cpu (ts, cpu_offset); + cpu =timestamp_vcpu_to_cpu_ts (host, vcpu, ts_guess); + +return cpu; +} + +static unsigned long long timestamp_correct (unsigned long long ts,int cpu, +struct tracecmd_input *handle) +{ +struct ts_offset_cpu *cpu_offset =NULL ; +int host_cpu; + +if (handle->ts_offset) +return ts + handle->ts_offset; + +if (!handle->host.sync_enable) +return ts; +if (handle->host.cpu_count ==1 ) + host_cpu =0 ; +else + host_cpu =timestamp_vcpu_to_cpu (&handle->host, cpu, ts); + + cpu_offset =timestamp_host_cpu_offset (&handle->host, host_cpu); + +return timestamp_correct_cpu (ts, cpu_offset);; +} +/* * Page is mapped, now read in the page header info. */
@@ -1199,7 +1280,8 @@ static int update_page_info(struct tracecmd_input *handle, int cpu) kbuffer_subbuffer_size(kbuf));return -1 ; } - handle->cpu_data[cpu].timestamp =timestamp_correct (kbuffer_timestamp (kbuf), handle); + handle->cpu_data[cpu].timestamp =timestamp_correct (kbuffer_timestamp (kbuf), + cpu, handle);if (handle->ts2secs) handle->cpu_data[cpu].timestamp *= handle->ts2secs;
@@ -1832,7 +1914,7 @@ read_again: goto read_again; } - handle->cpu_data[cpu].timestamp =timestamp_correct (ts, handle); + handle->cpu_data[cpu].timestamp =timestamp_correct (ts, cpu, handle);if (handle->ts2secs) { handle->cpu_data[cpu].timestamp *= handle->ts2secs;
@@ -2226,10 +2308,146 @@ error:return -1 ; } +static int tsync_store_cpu_sched_data (unsigned long long ts,int cpu, +int *sched_cpu_count, +struct ts_sched_cpu **sched_array) +{ +struct ts_sched_cpu *sched; + + sched =realloc (*sched_array, + (*sched_cpu_count +1 ) *sizeof (struct ts_sched_cpu )); +if (!sched) +return -1 ; + + sched[*sched_cpu_count].ts = ts; + sched[*sched_cpu_count].host_cpu = cpu; + *sched_array = sched; + (*sched_cpu_count)++; +return 0 ; +} + +static int tsync_compact_cpu_sched_data (int *sched_cpu_count, +struct ts_sched_cpu **sched_array) +{ +struct ts_sched_cpu *sched; +int cpu =-1 ; +int count; +int i, j; + +for (j =0 , i =0 ; i < *sched_cpu_count; i++) { +if (cpu == (*sched_array)[i].host_cpu) +continue ; + cpu = (*sched_array)[i].host_cpu; + j++; + } + sched =calloc (j,sizeof (struct ts_sched_cpu )); +if (!sched) +return -1 ; + count = j; +for (i =0 , j =0 ; i < *sched_cpu_count && j < count; i++) { +if (cpu == (*sched_array)[i].host_cpu) +continue ; +memcpy (sched + j, (*sched_array) + i,sizeof (struct ts_sched_cpu )); + cpu = (*sched_array)[i].host_cpu; + j++; + } + *sched_cpu_count = j; +free (*sched_array); + *sched_array = sched; + +return 0 ; +} + +static int trace_pid_sched_cmp (const void *a,const void *b) +{ +struct ts_sched_cpu *m_a = (struct ts_sched_cpu *)a; +struct ts_sched_cpu *m_b = (struct ts_sched_cpu *)b; + +if (m_a->ts > m_b->ts) +return 1 ; +if (m_a->ts < m_b->ts) +return -1 ; +return 0 ; +} + +static int tsync_load_cpu_sched_data (struct host_trace_info *host, +struct guest_trace_info *guest) +{ +struct tracecmd_input *peer = host->peer_data; +struct tep_format_field *next_pid =NULL ; +struct tep_event *sched_event; +unsigned long long pid; +struct tep_record *rec; +int i, j; +int ret =-1 ; +int *sched_cpu_count =NULL ; +struct ts_sched_cpu **sched =NULL ; + +if (!peer) +return -1 ; + + sched_event =tep_find_event_by_name (peer->pevent, +"sched" ,"sched_switch" ); +if (sched_event) + next_pid =tep_find_any_field (sched_event,"next_pid" ); +if (!next_pid) +return -1 ; + sched =calloc (guest->vcpu_count,sizeof (struct ts_sched_cpu *)); + sched_cpu_count =calloc (guest->vcpu_count,sizeof (int )); +if (!sched || !sched_cpu_count) + goto error; +for (i =0 ; i < peer->cpus; i++) { +for (rec =tracecmd_read_cpu_first (peer, i); + rec; rec =tracecmd_read_data (peer, i)) { +if (tep_data_type (peer->pevent, rec) != sched_event->id) +continue ; +if (tep_read_number_field (next_pid, rec->data, &pid)) +continue ; +for (j =0 ; j < guest->vcpu_count; j++) { +if (guest->cpu_pid[j] <0 ) +continue ; +if (pid != guest->cpu_pid[j]) +continue ; + ret =tsync_store_cpu_sched_data (rec->ts, + rec->cpu, + &sched_cpu_count[j], + &sched[j]); +if (ret) + goto error; + } + } + } + host->vcpu_sched =calloc (host->vcpu_count,sizeof (host->vcpu_count)); +if (!host->vcpu_sched) + goto error; + host->vcpu_count = guest->vcpu_count; +for (i =0 ; i < guest->vcpu_count; i++) { +if (!sched[i]) +continue ; +qsort (sched[i], sched_cpu_count[i], +sizeof (struct ts_sched_cpu ), trace_pid_sched_cmp); +tsync_compact_cpu_sched_data (&sched_cpu_count[i], + &sched[i]); + host->vcpu_sched[i].cpu_sched = sched[i]; + host->vcpu_sched[i].cpu_sched_count = sched_cpu_count[i]; + } + ret =0 ; + +error: +if (ret && sched) { +for (i =0 ; i < guest->vcpu_count; i++) +free (sched[i]); + } +free (sched); +free (sched_cpu_count); +return ret; +} +static void tsync_check_enable (struct tracecmd_input *handle) {struct host_trace_info *host = &handle->host;struct guest_trace_info *guest; +int ret; host->sync_enable = false;
@@ -2247,7 +2465,12 @@ static void tsync_check_enable(struct tracecmd_input *handle)if (!guest)return ; - host->sync_enable = true; + ret =0 ; +if (host->cpu_count >1 ) + ret =tsync_load_cpu_sched_data (host, guest); + +if (!ret) + host->sync_enable = true; } static int tsync_offset_load(struct tracecmd_input *handle,
@@ -2290,11 +2513,17 @@ static void trace_tsync_offset_free(struct host_trace_info *host)for (i =0 ; i < host->cpu_count; i++)free (host->cpu_time_offsets[i].ts_samples); +for (i =0 ; i < host->vcpu_count; i++) +free (host->vcpu_sched[i].cpu_sched);free (host->cpu_time_offsets); host->cpu_time_offsets =NULL ; host->cpu_count =0 ; +free (host->vcpu_sched); + host->vcpu_sched =NULL ; + host->vcpu_count =0 ; +if (host->peer_data) {tracecmd_close (host->peer_data); host->peer_data =NULL ;
Signature · 1 line
2.25.1