Refactoring and improvements of time sync logic
v2 cover Refactoring and improvements of time sync logic
The logic for host-guest time synchronization is moved from trace-cmd application context to the trace-cmd library.
Additional logic is added, to ensure the first pass of the synchronization is completed before starting the trace.
v2 changes:
- Coding style and gramar fixes.
Tzvetomir Stoyanov (VMware) (2):
trace-cmd: Move time sync logic in the trace-cmd library
trace-cmd: Wait for first time sync before the trace
lib/trace-cmd/Makefile | 2 - .../include/private/trace-cmd-private.h | 33 +- lib/trace-cmd/include/trace-tsync-local.h | 16 + lib/trace-cmd/trace-timesync.c | 515 ++++++++++++++++-- tracecmd/Makefile | 3 - tracecmd/include/trace-local.h | 5 +- tracecmd/trace-agent.c | 19 +- tracecmd/trace-record.c | 70 ++- tracecmd/trace-tsync.c | 294 ---------- 9 files changed, 576 insertions(+), 381 deletions(-) delete mode 100644 tracecmd/trace-tsync.c
Signature · 1 line
2.29.2
PATCH v2 1/2 trace-cmd: Move time sync logic in the trace-cmd library
The whole timestamp synchronization logic is moved in the trace-cmd library. It is compiled regardless of if vsock is supported or not, although it will not work if thers is no vsock support. The logic is abstracted, the application receives a time sync context which represents the synchronization session. These library APIs can be used to control the synchronization session:
tracecmd_tsync_init()
tracecmd_tsync_proto_getall()
tsync_proto_is_supported()
tracecmd_tsync_with_host() tracecmd_tsync_with_host_stop() tracecmd_tsync_with_guest() tracecmd_tsync_with_guest_stop()
tracecmd_tsync_free()
tracecmd_tsync_get_offsets() tracecmd_tsync_get_session_params() tracecmd_write_guest_time_shift()
--- lib/trace-cmd/Makefile | 2 - .../include/private/trace-cmd-private.h | 33 +- lib/trace-cmd/include/trace-tsync-local.h | 16 + lib/trace-cmd/trace-timesync.c | 503 ++++++++++++++++-- tracecmd/Makefile | 3 - tracecmd/include/trace-local.h | 5 +- tracecmd/trace-agent.c | 19 +- tracecmd/trace-record.c | 70 ++- tracecmd/trace-tsync.c | 294 ---------- 9 files changed, 565 insertions(+), 380 deletions(-) delete mode 100644 tracecmd/trace-tsync.c
lib/trace-cmd/Makefile +0 -2
@@ -17,9 +17,7 @@ OBJS += trace-util.o OBJS += trace-filter-hash.o OBJS += trace-msg.o OBJS += trace-plugin.o -ifeq ($(VSOCK_DEFINED), 1) OBJS += trace-timesync.o -endif # Additional util objects OBJS += trace-blk-hack.o
lib/trace-cmd/include/private/trace-cmd-private.h +15 -18
@@ -421,6 +421,7 @@ const char *tracecmd_clock_id2str(enum tracecmd_clocks clock);/* --- Timestamp synchronization --- */ +struct tracecmd_time_sync ;#define TRACECMD_TSYNC_PNAME_LENGTH 16#define TRACECMD_TSYNC_PROTO_NONE "none"
@@ -434,34 +435,30 @@ enum tracecmd_time_sync_role { TRACECMD_TIME_SYNC_ROLE_GUEST = (1 << 1), }; -struct tracecmd_time_sync { -const char *proto_name; -int loop_interval; -pthread_mutex_t lock; -pthread_cond_t cond; -char *clock_str; -struct tracecmd_msg_handle *msg_handle; -void *context; -int guest_pid; -int vcpu_count; -}; -/* Timestamp synchronization flags */ #define TRACECMD_TSYNC_FLAG_INTERPOLATE 0x1void tracecmd_tsync_init (void );int tracecmd_tsync_proto_getall (struct tracecmd_tsync_protos **protos,const char *clock,int role); -const char *tracecmd_tsync_proto_select (struct tracecmd_tsync_protos *protos,char *clock, -enum tracecmd_time_sync_role role);bool tsync_proto_is_supported (const char *proto_name); -void tracecmd_tsync_with_host (struct tracecmd_time_sync *tsync); -void tracecmd_tsync_with_guest (struct tracecmd_time_sync *tsync); +struct tracecmd_time_sync * +tracecmd_tsync_with_host (const struct tracecmd_tsync_protos *tsync_protos, +const char *clock); +int tracecmd_tsync_with_host_stop (struct tracecmd_time_sync *tsync); +struct tracecmd_time_sync * +tracecmd_tsync_with_guest (unsigned long long trace_id,int loop_interval, +unsigned int cid,unsigned int port,int guest_pid, +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 *count,long long **ts,long long **offsets,long long **scalings); -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);void tracecmd_tsync_free (struct tracecmd_time_sync *tsync); +int tracecmd_write_guest_time_shift (struct tracecmd_output *handle, +struct tracecmd_time_sync *tsync);/* --- Plugin handling --- */ extern struct tep_plugin_option trace_ftrace_options[];
lib/trace-cmd/include/trace-tsync-local.h +16 -0
@@ -8,6 +8,22 @@#include <stdbool.h> +struct tracecmd_time_sync { +pthread_t thread; +bool thread_running; +unsigned long long trace_id; +char *proto_name; +int loop_interval; +pthread_mutex_t lock; +pthread_cond_t cond; +pthread_barrier_t first_sync; +char *clock_str; +struct tracecmd_msg_handle *msg_handle; +void *context; +int guest_pid; +int vcpu_count; +}; +struct clock_sync_context {void *proto_data;/* time sync protocol specific data */ bool is_server;/* server side time sync role */
lib/trace-cmd/trace-timesync.c +471 -32
588 lines not on this page · raw message
tracecmd/Makefile +0 -3
@@ -36,9 +36,6 @@ TRACE_CMD_OBJS += trace-usage.o TRACE_CMD_OBJS += trace-dump.o TRACE_CMD_OBJS += trace-clear.o TRACE_CMD_OBJS += trace-vm.o -ifeq ($(VSOCK_DEFINED), 1) -TRACE_CMD_OBJS += trace-tsync.o -endif ifeq ($(VSOCK_DEFINED), 1) TRACE_CMD_OBJS += trace-agent.o
tracecmd/include/trace-local.h +2 -3
@@ -262,9 +262,8 @@ struct buffer_instance {int *fds;bool use_fifos; -pthread_t tsync_thread; -bool tsync_thread_running; -struct tracecmd_time_sync tsync; +int tsync_loop_interval; +struct tracecmd_time_sync *tsync; };void init_top_instance (void );
tracecmd/trace-agent.c +11 -8
@@ -143,12 +143,12 @@ static char *get_clock(int argc, char **argv)static void agent_handle (int sd,int nr_cpus,int page_size) {struct tracecmd_tsync_protos *tsync_protos =NULL ; +struct tracecmd_time_sync *tsync =NULL ;struct tracecmd_msg_handle *msg_handle; -const char *tsync_proto =NULL ; +char *tsync_proto =NULL ;unsigned long long trace_id;unsigned int tsync_port =0 ;unsigned int *ports; -pthread_t sync_thr;char **argv =NULL ;int argc =0 ;bool use_fifos;
@@ -176,10 +176,11 @@ static void agent_handle(int sd, int nr_cpus, int page_size)if (!use_fifos)make_vsocks (nr_cpus, fds, ports);if (tsync_protos && tsync_protos->names) { - tsync_proto =tracecmd_guest_tsync (tsync_protos, -get_clock (argc, argv), - &tsync_port, &sync_thr); -if (!tsync_proto) + tsync =tracecmd_tsync_with_host (tsync_protos, +get_clock (argc, argv)); +if (tsync) +tracecmd_tsync_get_session_params (tsync, &tsync_proto, &tsync_port); +else warning ("Failed to negotiate timestamps synchronization with the host" ); } trace_id =tracecmd_generate_traceid ();
@@ -192,8 +193,10 @@ static void agent_handle(int sd, int nr_cpus, int page_size)trace_record_agent (msg_handle, nr_cpus, fds, argc, argv, use_fifos, trace_id); -if (tsync_proto) -pthread_join (sync_thr,NULL ); +if (tsync) { +tracecmd_tsync_with_host_stop (tsync); +tracecmd_tsync_free (tsync); + }if (tsync_protos) {free (tsync_protos->names);
tracecmd/trace-record.c +50 -20
126 lines not on this page · raw message
tracecmd/trace-tsync.c deleted +0 -294
294 lines not on this page · raw message
Signature · 1 line
2.29.2
PATCH v2 2/2 trace-cmd: Wait for first time sync before the trace
In reply to Tzvetomir Stoyanov (VMware)Added a barrier in time synchronization threads to ensure the first time synchronization passed before to start the trace.
--- lib/trace-cmd/trace-timesync.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
lib/trace-cmd/trace-timesync.c +11 -1
@@ -534,6 +534,7 @@ void tracecmd_tsync_free(struct tracecmd_time_sync *tsync) tsync_context->sync_size =0 ;pthread_mutex_destroy (&tsync->lock);pthread_cond_destroy (&tsync->cond); +pthread_barrier_destroy (&tsync->first_sync);free (tsync->clock_str);free (tsync->proto_name);free (tsync);
@@ -645,12 +646,15 @@ static int tsync_with_guest(struct tracecmd_time_sync *tsync)int ts_array_size =CLOCK_TS_ARRAY ;struct tsync_proto *proto;struct timespec timeout; +bool first = true;bool end = false;int ret;clock_context_init (tsync, &proto, false); -if (!tsync->context) +if (!tsync->context) { +pthread_barrier_wait (&tsync->first_sync);return -1 ; + }if (tsync->loop_interval >0 && tsync->loop_interval < (CLOCK_TS_ARRAY *1000 ))
@@ -663,6 +667,10 @@ static int tsync_with_guest(struct tracecmd_time_sync *tsync)TRACECMD_TIME_SYNC_CMD_PROBE , 0, NULL); ret =tsync_get_sample (tsync, proto, ts_array_size); +if (first) { + first = false; +pthread_barrier_wait (&tsync->first_sync); + }if (ret || end)break ;if (tsync->loop_interval >0 ) {
@@ -754,6 +762,7 @@ tracecmd_tsync_with_guest(unsigned long long trace_id, int loop_interval, tsync->clock_str =strdup (clock);pthread_mutex_init (&tsync->lock,NULL );pthread_cond_init (&tsync->cond,NULL ); +pthread_barrier_init (&tsync->first_sync,NULL ,2 );pthread_attr_init (&attrib);pthread_attr_setdetachstate (&attrib,PTHREAD_CREATE_JOINABLE );
@@ -764,6 +773,7 @@ tracecmd_tsync_with_guest(unsigned long long trace_id, int loop_interval,if (!get_first_cpu (&pin_mask, &mask_size))pthread_setaffinity_np (tsync->thread, mask_size, pin_mask); +pthread_barrier_wait (&tsync->first_sync);if (pin_mask)CPU_FREE (pin_mask);
Signature · 1 line
2.29.2