Skip to contentSkip to conversation

trace-cmd: Timetamps sync between host and guest machines, relying on vsock events.

PATCH Superseded Tzvetomir Stoyanov 2 participants 13 messages Mar 26, 2019

v10 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v10 cover trace-cmd: Timetamps sync between host and guest machines, relying on vsock events.

[
v10 changes:
- Fixed broken compilation, call to timestamp_correction_calc() in timestamp_correct was smashed.
- Replaced deprecated tep_data_event_from_type() API with tep_find_event().
- Fixed a warning on assignment const to non const.

v9 changes:
- Fixed implementation of binary search algorithm in timestamp_correct()

v8 changes:
- Added rmdir() call in tracecmd_remove_instance(), to completely remove the instance. However, there is an issue with deleting the instances using rmdir(), which is investigated.
- Few changes in read_qemu_guests_pids(), timestamp_correct(), tsync_offset_load()
tracecmd_clock_context_new() and find_raw_events() suggested by Slavomir.

v7 changes:
- Added warning messages in case time synchronization cannot be negotiated or fails.
- Few optimizations and checks in read_qemu_guests_pids(), tsync_offset_load(), and find_raw_events(), suggested by Slavomir Kaslev.
- Reworked timestamp_correct() to not use static variables.
- Check TRACECMD_OPTION_TIME_SHIFT before reading time sync samples from the trace.dat file

v6 changes:
- Refactored tracecmd_msg_snd_time_sync() and tracecmd_msg_rcv_time_sync() functions:
removed any time sync calculations logic as separate functions in trace-timesync.c file
- Defined TSYNC_PROBE, TSYNC_REQ and TSYNC_RESP messages, in order to make the time sync protocol comprehensible.
- Addressed Steven Rostedt comments.
- Addressed Slavomir Kaslev commnets.

v5 changes:
- Rebased to Slavomir's v8 "Add VM kernel tracing over vsockets and FIFOs" patch series.
- Implemented an algorithm for time drift correction.
- Addressed Slavomir's commnets.
- Refactored the code: moved all time sync specific implementation in trace-timesync.c
- Isolated all hardcoded event specific stuff in a structure, so it could be easily moved to external plugins.
- Added a check for VSOCK support: do not perform vsock dependent time synchronisation in case there is no VSOCK support.

v4 changes:
- Removed the implementation of PTP-like algorithm. The current logic relies on matching time stamps of kvm_exit/virtio_transport_recv_pkt events on host to virtio_transport_alloc_pkt/vp_notify events on guest.
- Rebased to Slavomir's v7 "Add VM kernel tracing over vsockets and FIFOs" patch series.
- Decreased the time synch probes from 5000 to 300.
- Addressed Steven Rostedt comments.
- Code cleanup.

v3 changes:
- Removed any magic constants, used in the PTP-like algorithm, as Slavomir Kaslev suggested.
- Implemented new algorithm, based on mapping kvm_exit events in host context to vsock_send events in guest context, suggested by Steven Rostedt.

v2 changes:
- Addressed Steven Rostedt comments.
- Modified PTP-like timestamps sync algorithm to gain more accuracy, with the help of Yordan Karadzhov and Slavomir Kaslev.
]

This patch series is a POC for timestamps sync between guest and host machines. The algorithm relies on matching time stamps of guest and host vsock events.

The patch series depends on Slavomir's changes, introduced by the v8 patch series "Add VM kernel tracing over vsockets and FIFOs"

Tzvetomir Stoyanov (9):
trace-cmd: Implemented new lib API: tracecmd_local_events_system()
trace-cmd: Added support for negative time offsets in trace.dat file
trace-cmd: Fix tracecmd_read_page_record() to read more than one event
trace-cmd: Added implementation of htonll() and ntohll()
trace-cmd: Refactored few functions in trace-record.c
trace-cmd: Find and store pids of tasks, which run virtual CPUs of
given VM
trace-cmd: Implemented new API tracecmd_add_option_v()
trace-cmd: Implemented new option in trace.dat file:
TRACECMD_OPTION_TIME_SHIFT
trace-cmd [POC]: Implemented timestamps synch algorithm, using vsock
events.

 include/trace-cmd/trace-cmd.h    |  31 +-
 include/traceevent/event-parse.h |   1 +
 lib/trace-cmd/trace-input.c      | 144 +++++-
 lib/trace-cmd/trace-util.c       |  95 ++--
 tracecmd/Makefile                |   1 +
 tracecmd/include/trace-local.h   |  43 +-
 tracecmd/include/trace-msg.h     |  10 +
 tracecmd/trace-agent.c           |  13 +-
 tracecmd/trace-msg.c             | 208 +++++++-
 tracecmd/trace-output.c          | 117 ++++-
 tracecmd/trace-read.c            |   4 +-
 tracecmd/trace-record.c          | 230 +++++++--
 tracecmd/trace-timesync.c        | 805 +++++++++++++++++++++++++++++++
 13 files changed, 1569 insertions(+), 133 deletions(-)
 create mode 100644 tracecmd/trace-timesync.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH v10 1/9 trace-cmd: Implemented new lib API: tracecmd_local_events_system()

The new tracecmd lib API tracecmd_local_events_system() creates a tep handler and initializes it with the events of the specified subsystems.

---
 include/trace-cmd/trace-cmd.h |  2 +
 lib/trace-cmd/trace-util.c    | 95 ++++++++++++++++++++++++-----------
 2 files changed, 67 insertions(+), 30 deletions(-)
include/trace-cmd/trace-cmd.h +2 -0
index 27ba89d..21f83e2 100644
@@ -32,6 +32,8 @@ void tracecmd_unload_plugins(struct tep_plugin_list *list, struct tep_handle *pe
 char **tracecmd_event_systems(const char *tracing_dir);
 char **tracecmd_system_events(const char *tracing_dir, const char *system);
 struct tep_handle *tracecmd_local_events(const char *tracing_dir);
+struct tep_handle *tracecmd_local_events_system(const char *tracing_dir,
+						const char * const *sys_names);
 int tracecmd_fill_local_events(const char *tracing_dir, struct tep_handle *pevent);
 char **tracecmd_local_plugins(const char *tracing_dir);
 
lib/trace-cmd/trace-util.c +65 -30

121 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw

PATCH v10 2/9 trace-cmd: Added support for negative time offsets in trace.dat file

In reply to Tzvetomir Stoyanov

When synchronizing timestamps between different machines, there are cases when the time offset is negative. This patch changes the way time offset is written and read from trace.dat file - as signed decimal, instead of hex.

---
 include/trace-cmd/trace-cmd.h | 2 +-
 lib/trace-cmd/trace-input.c   | 6 +++---
 tracecmd/trace-read.c         | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
include/trace-cmd/trace-cmd.h +1 -1
index 21f83e2..428d792 100644
@@ -126,7 +126,7 @@ int tracecmd_is_buffer_instance(struct tracecmd_input *handle);
 void tracecmd_create_top_instance(char *name);
 void tracecmd_remove_instances(void);
 
-void tracecmd_set_ts_offset(struct tracecmd_input *handle, unsigned long long offset);
+void tracecmd_set_ts_offset(struct tracecmd_input *handle, long long offset);
 void tracecmd_set_ts2secs(struct tracecmd_input *handle, unsigned long long hz);
 
 void tracecmd_print_events(struct tracecmd_input *handle, const char *regex);
lib/trace-cmd/trace-input.c +3 -3
index 34a8301..be6720c 100644
@@ -91,7 +91,7 @@ struct tracecmd_input {
 	bool			read_page;
 	bool			use_pipe;
 	struct cpu_data 	*cpu_data;
-	unsigned long long	ts_offset;
+	long long		ts_offset;
 	double			ts2secs;
 	char *			cpustats;
 	char *			uname;
@@ -2082,7 +2082,7 @@ static int init_cpu(struct tracecmd_input *handle, int cpu)
 }
 
 void tracecmd_set_ts_offset(struct tracecmd_input *handle,
-			    unsigned long long offset)
+			    long long offset)
 {
 	handle->ts_offset = offset;
 }
@@ -2099,7 +2099,7 @@ void tracecmd_set_ts2secs(struct tracecmd_input *handle,
 
 static int handle_options(struct tracecmd_input *handle)
 {
-	unsigned long long offset;
+	long long offset;
 	unsigned short option;
 	unsigned int size;
 	char *cpustats = NULL;
tracecmd/trace-read.c +2 -2
index 804022a..e309672 100644
@@ -58,7 +58,7 @@ static struct list_head handle_list;
 struct input_files {
 	struct list_head	list;
 	const char		*file;
-	unsigned long long	tsoffset;
+	long long		tsoffset;
 	unsigned long long	ts2secs;
 };
 static struct list_head input_files;
@@ -1413,7 +1413,7 @@ void trace_report (int argc, char **argv)
 	struct input_files *inputs;
 	struct handle_list *handles;
 	enum output_type otype;
-	unsigned long long tsoffset = 0;
+	long long tsoffset = 0;
 	unsigned long long ts2secs = 0;
 	unsigned long long ts2sc;
 	int show_stat = 0;
Signature · 1 line
2.20.1
#3 lore raw

PATCH v10 3/9 trace-cmd: Fix tracecmd_read_page_record() to read more than one event

In reply to Tzvetomir Stoyanov

The kbuffer_next_event() will return the next event on the sub buffer. If we pass in the last_record to tracecmd_read_page_record(), it initializes the sub buffer, and by calling kbuffer_next_event() (instead of kbuffer_read_event()), the second event on the sub buffer is returned. This causes the match of the last_record not to match if the last_record happens to be the first event on the sub buffer.

---
 lib/trace-cmd/trace-input.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
lib/trace-cmd/trace-input.c +8 -4
index be6720c..0a6e820 100644
@@ -1679,18 +1679,22 @@ tracecmd_read_page_record(struct tep_handle *pevent, void *page, int size,
 			goto out_free;
 		}
 
-		do {
+		ptr = kbuffer_read_event(kbuf, &ts);
+		while (ptr < last_record->data) {
 			ptr = kbuffer_next_event(kbuf, NULL);
 			if (!ptr)
 				break;
-		} while (ptr < last_record->data);
+			if (ptr == last_record->data)
+				break;
+		}
 		if (ptr != last_record->data) {
 			warning("tracecmd_read_page_record: could not find last_record");
 			goto out_free;
 		}
-	}
+		ptr = kbuffer_next_event(kbuf, &ts);
+	} else
+		ptr = kbuffer_read_event(kbuf, &ts);
 
-	ptr = kbuffer_read_event(kbuf, &ts);
 	if (!ptr)
 		goto out_free;
 
Signature · 1 line
2.20.1
#4 lore raw

PATCH v10 4/9 trace-cmd: Added implementation of htonll() and ntohll()

In reply to Tzvetomir Stoyanov

Added implementation of htonll() and ntohll() as
macros, if they are not already defined.

---
 tracecmd/include/trace-msg.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
tracecmd/include/trace-msg.h +10 -0
index b7fe10b..445f799 100644
@@ -15,4 +15,14 @@ extern unsigned int page_size;
 void plog(const char *fmt, ...);
 void pdie(const char *fmt, ...);
 
+#ifndef htonll
+# if __BYTE_ORDER == __LITTLE_ENDIAN
+#define htonll(x) __bswap_64(x)
+#define ntohll(x) __bswap_64(x)
+#else
+#define htonll(x) (x)
+#define ntohll(x) (x)
+#endif
+#endif
+
 #endif /* _TRACE_MSG_H_ */
Signature · 1 line
2.20.1
#5 lore raw

PATCH v10 5/9 trace-cmd: Refactored few functions in trace-record.c

In reply to Tzvetomir Stoyanov

In order to reuse the code inside the trace-cmd application, few functions from trace-record.c are refactored:
- make_instances() and tracecmd_remove_instances() are splited.
New ones are created: tracecmd_make_instance() and tracecmd_remove_instance(), which are visible outside the trace-record.c
- Following functions are made non-static: tracecmd_init_instance()
get_instance_dir(), write_instance_file(), write_tracing_on(), tracecmd_set_clock()
- New function is implemented: tracecmd_local_cpu_count(), an internal
API to get local_cpu_count.

---
 tracecmd/include/trace-local.h |  9 ++++
 tracecmd/trace-record.c        | 88 +++++++++++++++++++---------------
 2 files changed, 59 insertions(+), 38 deletions(-)
tracecmd/include/trace-local.h +9 -0
index d7bdb1f..8413054 100644
@@ -235,6 +235,15 @@ void update_first_instance(struct buffer_instance *instance, int topt);
 void show_instance_file(struct buffer_instance *instance, const char *name);
 
 int count_cpus(void);
+void write_tracing_on(struct buffer_instance *instance, int on);
+char *get_instance_dir(struct buffer_instance *instance);
+int write_instance_file(struct buffer_instance *instance,
+			const char *file, const char *str, const char *type);
+void tracecmd_init_instance(struct buffer_instance *instance);
+void tracecmd_make_instance(struct buffer_instance *instance);
+int tracecmd_local_cpu_count(void);
+void tracecmd_set_clock(struct buffer_instance *instance);
+void tracecmd_remove_instance(struct buffer_instance *instance);
 
 /* No longer in event-utils.h */
 void __noreturn die(const char *fmt, ...); /* Can be overriden */
tracecmd/trace-record.c +50 -38

190 lines not on this page · raw message

Signature · 1 line
2.20.1
#6 lore raw

PATCH v10 6/9 trace-cmd: Find and store pids of tasks, which run virtual CPUs of given VM

In reply to Tzvetomir Stoyanov

In order to match host and guest events, a mapping between guest VCPU and the host task, running this VCPU is needed. Extended existing struct guest to hold such mapping and added logic in read_qemu_guests() function to initialize it. Implemented a new internal API, get_guest_vcpu_pid(), to retrieve VCPU-task mapping for given VM.

---
 tracecmd/include/trace-local.h |  1 +
 tracecmd/trace-record.c        | 57 ++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
tracecmd/include/trace-local.h +1 -0
index 8413054..62f5e47 100644
@@ -245,6 +245,7 @@ int tracecmd_local_cpu_count(void);
 void tracecmd_set_clock(struct buffer_instance *instance);
 void tracecmd_remove_instance(struct buffer_instance *instance);
 
+int get_guest_vcpu_pid(unsigned int guest_cid, unsigned int guest_vcpu);
 /* 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-record.c +57 -0

85 lines not on this page · raw message

Signature · 1 line
2.20.1
#7 lore raw

Re: [PATCH v10 6/9] trace-cmd: Find and store pids of tasks, which run virtual CPUs of given VM

Quoted 45 lines · Tzvetomir Stoyanov

In order to match host and guest events, a mapping between guest VCPU and the host task, running this VCPU is needed. Extended existing struct guest to hold such mapping and added logic in read_qemu_guests() function to initialize it. Implemented a new internal API, get_guest_vcpu_pid(), to retrieve VCPU-task mapping for given VM.

---
 tracecmd/include/trace-local.h |  1 +
 tracecmd/trace-record.c        | 57 ++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
tracecmd/include/trace-local.h +1 -0
index 8413054..62f5e47 100644
@@ -245,6 +245,7 @@ int tracecmd_local_cpu_count(void);
 void tracecmd_set_clock(struct buffer_instance *instance);
 void tracecmd_remove_instance(struct buffer_instance *instance);
 
+int get_guest_vcpu_pid(unsigned int guest_cid, unsigned int guest_vcpu);
 /* 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-record.c +2 -0
index eacf7d2..72a8ed3 100644
@@ -2746,10 +2746,12 @@ static bool is_digits(const char *s)
        return true;
 }
 
+#define VCPUS_MAX 256
 struct guest {
        char *name;
        int cid;
        int pid;
+       int cpu_pid[VCPUS_MAX];
 };
 
 static struct guest *guests;
@@ -2767,6 +2769,46 @@ static char *get_qemu_guest_name(char *arg)
        return arg;
 }

tracecmd/trace-record.c static char *get_qemu_guest_name(char *arg) · line 2780

Quoted 10 lines
tracecmd/trace-record.c +9 -0
 
+static void read_qemu_guests_pids(char *guest_task, struct guest *guest)
+{
+       struct dirent *entry_t;
+       char path[PATH_MAX];
+       char *buf = NULL;
+       size_t n = 0;
+       int vcpu;
+       DIR *dir;
+       FILE *ft;

Nit: I think the entry_t/ft are left overs from before when this function was inlined in read_qemu_guests(). Can you change them to entry/f now that there's no name collision?

Cheers,

-- Slavi

#8 lore raw

PATCH v10 7/9 trace-cmd: Implemented new API tracecmd_add_option_v()

In reply to Tzvetomir Stoyanov

This patch implements a new tracecmd API, tracecmd_add_option_v() It adds new option in trace.dat, similar to tracecmd_add_option(), but the option's data is passed as list of buffers. The standard struct iovec is used as input parameter, containing the option's data buffers.

---
 include/trace-cmd/trace-cmd.h    |   5 ++
 include/traceevent/event-parse.h |   1 +
 tracecmd/trace-output.c          | 117 ++++++++++++++++++++++++++-----
 3 files changed, 106 insertions(+), 17 deletions(-)
include/trace-cmd/trace-cmd.h +5 -0
index 428d792..f7c043a 100644
@@ -245,11 +245,16 @@ struct tracecmd_output *tracecmd_create_init_file_override(const char *output_fi
 struct tracecmd_option *tracecmd_add_option(struct tracecmd_output *handle,
 					    unsigned short id, int size,
 					    const void *data);
+struct tracecmd_option *
+tracecmd_add_option_v(struct tracecmd_output *handle,
+		    unsigned short id, const struct iovec *vector, int count);
+
 struct tracecmd_option *tracecmd_add_buffer_option(struct tracecmd_output *handle,
 						   const char *name, int cpus);
 
 int tracecmd_write_cpus(struct tracecmd_output *handle, int cpus);
 int tracecmd_write_options(struct tracecmd_output *handle);
+int tracecmd_append_options(struct tracecmd_output *handle);
 int tracecmd_update_option(struct tracecmd_output *handle,
 			   struct tracecmd_option *option, int size,
 			   const void *data);
include/traceevent/event-parse.h +1 -0
index 3f2ae80..9705b51 100644
@@ -11,6 +11,7 @@
 #include <stdio.h>
 #include <regex.h>
 #include <string.h>
+#include <sys/uio.h>
 
 #include "trace-seq.h"
 
tracecmd/trace-output.c +100 -17

159 lines not on this page · raw message

Signature · 1 line
2.20.1
#9 lore raw

PATCH v10 8/9 trace-cmd: Implemented new option in trace.dat file: TRACECMD_OPTION_TIME_SHIFT

In reply to Tzvetomir Stoyanov

The TRACECMD_OPTION_TIME_SHIFT is used when synchronizing trace time stamps between two trace.dat files. It contains multiple long long (time, offset) pairs, describing time stamps _offset_, measured in the given local _time_. The content of the option buffer is:
first 4 bytes - integer, count of timestamp offsets
long long array of size _count_, local time in which the offset is measured long long array of size _count_, offset of the time stamps

---
 include/trace-cmd/trace-cmd.h |   1 +
 lib/trace-cmd/trace-input.c   | 126 +++++++++++++++++++++++++++++++++-
 2 files changed, 125 insertions(+), 2 deletions(-)
include/trace-cmd/trace-cmd.h +1 -0
index f7c043a..5552396 100644
@@ -82,6 +82,7 @@ enum {
 	TRACECMD_OPTION_HOOK,
 	TRACECMD_OPTION_OFFSET,
 	TRACECMD_OPTION_CPUCOUNT,
+	TRACECMD_OPTION_TIME_SHIFT,
 };
 
 enum {
lib/trace-cmd/trace-input.c +124 -2

174 lines not on this page · raw message

Signature · 1 line
2.20.1
#10 lore raw

Re: [PATCH v10 8/9] trace-cmd: Implemented new option in trace.dat file: TRACECMD_OPTION_TIME_SHIFT

On Tue, Mar 26, 2019 at 5:07 PM Tzvetomir Stoyanov <[email protected]> wrote:

<snip>

Quoted 23 lines
lib/trace-cmd/trace-input.c +23 -0
+static int tsync_offset_cmp(const void *a, const void *b)
+{
+       struct ts_offset_sample *ts_a = (struct ts_offset_sample *)a;
+       struct ts_offset_sample *ts_b = (struct ts_offset_sample *)b;
+
+       if (ts_a->time > ts_b->time)
+               return 1;
+       if (ts_a->time < ts_b->time)
+               return -1;
+       return 0;
+}
+
+static void tsync_offset_load(struct tracecmd_input *handle, char *buf)
+{
+       int i, j;
+       long long *buf8 = (long long *)buf;
+
+       for (i = 0; i < handle->ts_samples_count; i++) {
+               handle->ts_samples[i].time = tep_read_number(handle->pevent,
+                                                         buf8+i, 8);
+               handle->ts_samples[i].offset = tep_read_number(handle->pevent,
+                                               buf8+handle->ts_samples_count+i, 8);
+       }

lib/trace-cmd/trace-input.c void tracecmd_set_ts2secs(struct tracecmd_input *handle, · line 2203

Quoted 10 lines
lib/trace-cmd/trace-input.c +10 -0
+       qsort(handle->ts_samples,
+             handle->ts_samples_count, sizeof(struct ts_offset_sample),
+             tsync_offset_cmp);
+       /* Filter possible samples with equal time */
+       for (i = 0, j = 0; i < handle->ts_samples_count; i++) {
+               if (i == 0 ||
+                   handle->ts_samples[i].time != handle->ts_samples[i-1].time) {
+                       handle->ts_samples[j++] = handle->ts_samples[i];
+               }
+       }

handle->ts_samples_count is never corrected here in case there are samples with duplicate timestamps.

    handle->ts_samples_count = j;

lib/trace-cmd/trace-input.c static int handle_options(struct tracecmd_input *handle) · line 2216

Quoted 10 lines
lib/trace-cmd/trace-input.c +3 -0
+}
+
 static int handle_options(struct tracecmd_input *handle)
 {
        long long offset;
@@ -2111,6 +2213,7 @@ static int handle_options(struct tracecmd_input *handle)
        struct input_buffer_instance *buffer;
        struct hook_list *hook;
        char *buf;
+       int tsync;

Nit: s/tsync/sampes_size?

Quoted 32 lines
lib/trace-cmd/trace-input.c +19 -1
        int cpus;
 
        for (;;) {
@@ -2155,6 +2258,25 @@ static int handle_options(struct tracecmd_input *handle)
                        offset = strtoll(buf, NULL, 0);
                        handle->ts_offset += offset;
                        break;
+               case TRACECMD_OPTION_TIME_SHIFT:
+                       /*
+                        * int (4 bytes) count of timestamp offsets.
+                        * long long array of size [count] of times,
+                        *      when the offsets were calculated.
+                        * long long array of size [count] of timestamp offsets.
+                        */
+                       if (handle->flags & TRACECMD_FL_IGNORE_DATE)
+                               break;
+                       handle->ts_samples_count = tep_read_number(handle->pevent,
+                                                               buf, 4);
+                       tsync = (sizeof(long long)*handle->ts_samples_count);
+                       if (size != (4+(2*tsync)))
+                               break;
+                       handle->ts_samples = malloc(2*tsync);
+                       if (!handle->ts_samples)
+                               return -ENOMEM;
+                       tsync_offset_load(handle, buf+4);
+                       break;
                case TRACECMD_OPTION_CPUSTAT:
                        buf[size-1] = '\n';
                        cpustats = realloc(cpustats, cpustats_size + size + 1);
--

2.20.1

#11 lore raw

PATCH v10 9/9 trace-cmd [POC]: Implemented timestamps synch algorithm, using vsock events.

In reply to Tzvetomir Stoyanov

This is a POC patch, implementing an algorithm for syncing timestamps between host and guest machines, using vsock trace events to catch the host / guest time.

---
 include/trace-cmd/trace-cmd.h  |  21 +-
 tracecmd/Makefile              |   1 +
 tracecmd/include/trace-local.h |  33 +-
 tracecmd/trace-agent.c         |  13 +-
 tracecmd/trace-msg.c           | 208 ++++++++-
 tracecmd/trace-output.c        |   2 +-
 tracecmd/trace-record.c        |  85 +++-
 tracecmd/trace-timesync.c      | 805 +++++++++++++++++++++++++++++++++
 8 files changed, 1131 insertions(+), 37 deletions(-)
 create mode 100644 tracecmd/trace-timesync.c
include/trace-cmd/trace-cmd.h +17 -4
index 5552396..905f3ec 100644
@@ -341,16 +341,29 @@ bool tracecmd_msg_done(struct tracecmd_msg_handle *msg_handle);
 void tracecmd_msg_set_done(struct tracecmd_msg_handle *msg_handle);
 
 int tracecmd_msg_send_trace_req(struct tracecmd_msg_handle *msg_handle,
-				int argc, char **argv, bool use_fifos);
+				int argc, char **argv, bool use_fifos,
+				bool do_tsync);
 int tracecmd_msg_recv_trace_req(struct tracecmd_msg_handle *msg_handle,
-				int *argc, char ***argv, bool *use_fifos);
+				int *argc, char ***argv, bool *use_fifos,
+				bool *do_tsync);
 
 int tracecmd_msg_send_trace_resp(struct tracecmd_msg_handle *msg_handle,
 				 int nr_cpus, int page_size,
-				 unsigned int *ports, bool use_fifos);
+				 unsigned int *ports, bool use_fifos,
+				 bool do_tsync);
 int tracecmd_msg_recv_trace_resp(struct tracecmd_msg_handle *msg_handle,
 				 int *nr_cpus, int *page_size,
-				 unsigned int **ports, bool *use_fifos);
+				 unsigned int **ports, bool *use_fifos,
+				 bool *do_tsync);
+
+struct tracecmd_clock_sync;
+
+int tracecmd_msg_rcv_time_sync(struct tracecmd_msg_handle *msg_handle,
+			       struct tracecmd_clock_sync *clock_sync,
+			       long long *offset, long long *timestamp);
+int tracecmd_msg_snd_time_sync(struct tracecmd_msg_handle *msg_handle,
+			       struct tracecmd_clock_sync *clock_sync,
+			       long long *offset, long long *timestamp);
 
 /* --- Plugin handling --- */
 extern struct tep_plugin_option trace_ftrace_options[];
tracecmd/Makefile +1 -0
index d3e3080..8a73bf7 100644
@@ -32,6 +32,7 @@ TRACE_CMD_OBJS += trace-list.o
 TRACE_CMD_OBJS += trace-output.o
 TRACE_CMD_OBJS += trace-usage.o
 TRACE_CMD_OBJS += trace-msg.o
+TRACE_CMD_OBJS += trace-timesync.o
 
 ifeq ($(VSOCK_DEFINED), 1)
 TRACE_CMD_OBJS += trace-agent.o
tracecmd/include/trace-local.h +32 -1
index 62f5e47..7688b02 100644
@@ -26,6 +26,7 @@ extern int quiet;
 typedef unsigned long long u64;
 
 struct buffer_instance;
+struct tracecmd_clock_sync;
 
 /* for local shared information with trace-cmd executable */
 
@@ -101,7 +102,7 @@ void trace_usage(int argc, char **argv);
 
 int trace_record_agent(struct tracecmd_msg_handle *msg_handle,
 		       int cpus, int *fds,
-		       int argc, char **argv, bool use_fifos);
+		       int argc, char **argv, bool use_fifos, bool do_tsync);
 
 struct hook_list;
 
@@ -214,6 +215,12 @@ struct buffer_instance {
 	int			port;
 	int			*fds;
 	bool			use_fifos;
+	bool			do_tsync;
+
+	struct tracecmd_clock_sync *clock_sync;
+	int			time_sync_count;
+	long long		*time_sync_ts;
+	long long		*time_sync_offsets;
 };
 
 extern struct buffer_instance top_instance;
@@ -235,6 +242,30 @@ void update_first_instance(struct buffer_instance *instance, int topt);
 void show_instance_file(struct buffer_instance *instance, const char *name);
 
 int count_cpus(void);
+
+struct tracecmd_time_sync_event {
+	int			id;
+	int			cpu;
+	int			pid;
+	unsigned long long	ts;
+};
+
+int tracecmd_clock_get_peer(struct tracecmd_clock_sync *clock_context,
+			    unsigned int *remote_cid, unsigned int *remote_port);
+bool tracecmd_time_sync_check(void);
+void tracecmd_clock_context_free(struct buffer_instance *instance);
+int tracecmd_clock_find_event(struct tracecmd_clock_sync *clock, int cpu,
+			      struct tracecmd_time_sync_event *event);
+void tracecmd_clock_synch_enable(struct tracecmd_clock_sync *clock_context);
+void tracecmd_clock_synch_disable(struct tracecmd_clock_sync *clock_context);
+void tracecmd_clock_synch_calc_reset(struct tracecmd_clock_sync *clock_context);
+void tracecmd_clock_synch_calc_probe(struct tracecmd_clock_sync *clock_context,
+				     long long ts_local, long long ts_remote);
+int tracecmd_clock_synch_calc(struct tracecmd_clock_sync *clock_context,
+			       long long *offset_ret, long long *time_ret);
+void sync_time_with_host_v3(struct buffer_instance *instance);
+void sync_time_with_guest_v3(struct buffer_instance *instance);
+
 void write_tracing_on(struct buffer_instance *instance, int on);
 char *get_instance_dir(struct buffer_instance *instance);
 int write_instance_file(struct buffer_instance *instance,
tracecmd/trace-agent.c +10 -3
index 7389f72..2189bc1 100644
@@ -116,6 +116,7 @@ static void agent_handle(int sd, int nr_cpus, int page_size)
 	char **argv = NULL;
 	int argc = 0;
 	bool use_fifos;
+	bool do_tsync;
 	int *fds;
 	int ret;
 
@@ -128,7 +129,8 @@ static void agent_handle(int sd, int nr_cpus, int page_size)
 	if (!msg_handle)
 		die("Failed to allocate message handle");
 
-	ret = tracecmd_msg_recv_trace_req(msg_handle, &argc, &argv, &use_fifos);
+	ret = tracecmd_msg_recv_trace_req(msg_handle, &argc, &argv,
+					  &use_fifos, &do_tsync);
 	if (ret < 0)
 		die("Failed to receive trace request");
 
@@ -137,13 +139,18 @@ static void agent_handle(int sd, int nr_cpus, int page_size)
 
 	if (!use_fifos)
 		make_vsocks(nr_cpus, fds, ports);
+	if (do_tsync) {
+		do_tsync = tracecmd_time_sync_check();
+		if (!do_tsync)
+			warning("Failed to negotiate timestamps synchronization with the host");
+	}
 
 	ret = tracecmd_msg_send_trace_resp(msg_handle, nr_cpus, page_size,
-					   ports, use_fifos);
+					   ports, use_fifos, do_tsync);
 	if (ret < 0)
 		die("Failed to send trace response");
 
-	trace_record_agent(msg_handle, nr_cpus, fds, argc, argv, use_fifos);
+	trace_record_agent(msg_handle, nr_cpus, fds, argc, argv, use_fifos, do_tsync);
 
 	free(argv[0]);
 	free(argv);
tracecmd/trace-msg.c +186 -22

316 lines not on this page · raw message

tracecmd/trace-output.c +1 -1
index d57539c..fc338ca 100644
@@ -960,7 +960,7 @@ tracecmd_add_option(struct tracecmd_output *handle,
 {
 	struct iovec vect;
 
-	vect.iov_base = data;
+	vect.iov_base = (void *)data;
 	vect.iov_len = size;
 	return tracecmd_add_option_v(handle, id, &vect, 1);
 }
tracecmd/trace-record.c +79 -6

167 lines not on this page · raw message

tracecmd/trace-timesync.c new file +805 -0

805 lines not on this page · raw message

Signature · 1 line
2.20.1
#12 lore raw

Re: [PATCH v10 9/9] trace-cmd [POC]: Implemented timestamps synch algorithm, using vsock events.

On Tue, Mar 26, 2019 at 5:07 PM Tzvetomir Stoyanov <[email protected]> wrote:
<snip>

tracecmd/trace-msg.c static int msg_write(int fd, struct tracecmd_msg *msg)

Quoted 19 lines
tracecmd/trace-msg.c +7 -4
@@ -120,10 +146,13 @@ static const char *cmd_to_name(int cmd)
 struct tracecmd_msg {
        struct tracecmd_msg_header              hdr;
        union {
-               struct tracecmd_msg_tinit       tinit;
-               struct tracecmd_msg_rinit       rinit;
-               struct tracecmd_msg_trace_req   trace_req;
-               struct tracecmd_msg_trace_resp  trace_resp;
+               struct tracecmd_msg_tinit               tinit;
+               struct tracecmd_msg_rinit               rinit;
+               struct tracecmd_msg_trace_req           trace_req;
+               struct tracecmd_msg_trace_resp          trace_resp;
+               struct tracecmd_msg_tsync_stop          ts_stop;
+               struct tracecmd_msg_tsync_req           ts_req;
+               struct tracecmd_msg_tsync_resp          ts_resp;
        };
        union {
                struct tracecmd_msg_opt         *opt;
@@ -161,6 +190,7 @@ static int msg_write(int fd, struct tracecmd_msg *msg)

The second union in tracecmd_msg is already gone in master and this series needs to get rebased.

<snip>

tracecmd/trace-output.c tracecmd_add_option(struct tracecmd_output *handle, · line 966

Quoted 13 lines
tracecmd/trace-output.c +1 -1
index d57539c..fc338ca 100644
@@ -960,7 +960,7 @@ tracecmd_add_option(struct tracecmd_output *handle,
 {
        struct iovec vect;
 
-       vect.iov_base = data;
+       vect.iov_base = (void *)data;
        vect.iov_len = size;
        return tracecmd_add_option_v(handle, id, &vect, 1);
 }

This fix needs to be folded in patch 7 where this code is introduced rather than here.

tracecmd/trace-record.c static bool has_local_instances(void) · line 5634

Quoted 9 lines
tracecmd/trace-record.c +1 -0
index 72a8ed3..14f3cf9 100644
@@ -5609,6 +5631,42 @@ static bool has_local_instances(void)
        return false;
 }
 
+//#define TSYNC_DEBUG

Remove? This define can be passed on the command line to make.

Quoted 13 lines
tracecmd/trace-record.c +13 -0
+static void write_guest_time_shift(struct buffer_instance *instance)
+{
+       struct tracecmd_output *handle;
+       struct iovec vector[3];
+       char *file;
+       int fd;
+
+       if (!instance->time_sync_count)
+               return;
+
+       file = get_guest_file(output_file, instance->name);
+       fd = open(file, O_RDWR);
+       if (fd < 0)

tracecmd/trace-record.c static bool has_local_instances(void) · line 5657

Quoted 10 lines
tracecmd/trace-record.c +10 -0
+               die("error opening %s", file);
+       put_temp_file(file);
+       handle = tracecmd_get_output_handle_fd(fd);
+       vector[0].iov_len = sizeof(instance->time_sync_count);
+       vector[0].iov_base = &instance->time_sync_count;
+       vector[1].iov_len = sizeof(long long) * instance->time_sync_count;
+       vector[1].iov_base = instance->time_sync_ts;
+       vector[2].iov_len = sizeof(long long) * instance->time_sync_count;
+       vector[2].iov_base = instance->time_sync_offsets;
+       tracecmd_add_option_v(handle, TRACECMD_OPTION_TIME_SHIFT, vector, 3);

This seems to be the only use of the newly added tracecmd_add_option_v. I'm not convinced that having struct iovec interface for tracecmd options is a good pattern since
a) the iovec concatenation in tracecmd_add_option_v adds an extra allocation for the most common case when there's a single piece of data passed
b) it never actually gets passed to readv/writev
c) the above is not really portable since there's no guarantee that sizeof(int) = 4

I think defining a function here that massages time sync samples into TIME_SHIFT option format (in a portable way) and dropping the patch that adds tracecmd_add_option_v altogether is a better way to go.

<snip>

Quoted 18 lines
tracecmd/trace-timesync.c new file +12 -0
new file mode 100644
index 0000000..074c9d0
@@ -0,0 +1,805 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2019, VMware, Tzvetomir Stoyanov <[email protected]>
+ *
+ */
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <linux/vm_sockets.h>
+#include "trace-local.h"

<snip>

Quoted 14 lines
tracecmd/trace-timesync.c new file +14 -0
+
+static void get_vsocket_params(int fd, unsigned int *lcid, unsigned int *lport,
+                              unsigned int *rcid, unsigned int *rport)
+{
+       struct sockaddr_vm addr;
+       socklen_t addr_len = sizeof(addr);
+
+       memset(&addr, 0, sizeof(addr));
+       if (getsockname(fd, (struct sockaddr *)&addr, &addr_len))
+               return;
+       if (addr.svm_family != AF_VSOCK)
+               return;
+       *lport = addr.svm_port;
+       *lcid = addr.svm_cid;

tracecmd/trace-timesync.c line 476

Quoted 10 lines
tracecmd/trace-timesync.c new file +10 -0
+
+       memset(&addr, 0, sizeof(addr));
+       addr_len = sizeof(addr);
+       if (getpeername(fd, (struct sockaddr *)&addr, &addr_len))
+               return;
+       if (addr.svm_family != AF_VSOCK)
+               return;
+       *rport = addr.svm_port;
+       *rcid = addr.svm_cid;
+}

This function can silently ignore errors if getsockname/getpeername fail or file descriptor happens to not be a vsocket. Can you change it return error and have callers check for it?

Thank you Ceco,

-- Slavi