Skip to contentSkip to conversation

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

PATCH Superseded Tzvetomir Stoyanov 3 participants 13 messages Feb 22, 2019

v4 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

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

[
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 v7 patch series "Add VM kernel tracing over vsockets and FIFOs"

Tzvetomir Stoyanov (7):
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 make_instances() and tracecmd_remove_instances()
trace-cmd: Find and store pids of tasks, which run virtual CPUs of
given VM
trace-cmd [POC]: Implemented timestamps synch algorithm, using vsock
events.

 include/trace-cmd/trace-cmd.h  |   8 +-
 lib/trace-cmd/trace-input.c    |  18 +-
 lib/trace-cmd/trace-util.c     |  99 ++++---
 tracecmd/include/trace-local.h |  16 ++
 tracecmd/include/trace-msg.h   |  10 +
 tracecmd/trace-listen.c        |   3 +
 tracecmd/trace-msg.c           | 477 ++++++++++++++++++++++++++++++++-
 tracecmd/trace-read.c          |   4 +-
 tracecmd/trace-record.c        | 267 +++++++++++++++---
 9 files changed, 816 insertions(+), 86 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH v4 1/7 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    | 99 +++++++++++++++++++++++------------
 2 files changed, 68 insertions(+), 33 deletions(-)
include/trace-cmd/trace-cmd.h +2 -0
index 27ba89d..0067882 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,
+						char **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 +66 -33

122 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH v4 1/7] trace-cmd: Implemented new lib API: tracecmd_local_events_system()

Quoted 15 lines · Tzvetomir Stoyanov

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    | 99 +++++++++++++++++++++++------------
 2 files changed, 68 insertions(+), 33 deletions(-)
include/trace-cmd/trace-cmd.h +0 -0
index 27ba89d..0067882 100644

include/trace-cmd/trace-cmd.h void tracecmd_unload_plugins(struct tep_plugin_list *list, struct tep_handle *pe · line 36

Quoted 5 lines
include/trace-cmd/trace-cmd.h +2 -0
@@ -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,
+						char **sys_names);

Can we have sys_names be const char ** to imply that it's not getting modified in here?

Quoted 60 lines
include/trace-cmd/trace-cmd.h +0 -0
 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 +4 -33
index b5aea39..0e925ed 100644
@@ -1119,43 +1119,15 @@ static int read_header(struct tep_handle *pevent, const char *events_dir)
 	return ret;
 }
 
-/**
- * tracecmd_local_events - create a pevent from the events on system
- * @tracing_dir: The directory that contains the events.
- *
- * Returns a pevent structure that contains the pevents local to
- * the system.
- */
-struct tep_handle *tracecmd_local_events(const char *tracing_dir)
-{
-	struct tep_handle *pevent = NULL;
-
-	pevent = tep_alloc();
-	if (!pevent)
-		return NULL;
-
-	if (tracecmd_fill_local_events(tracing_dir, pevent)) {
-		tep_free(pevent);
-		pevent = NULL;
-	}
-
-	return pevent;
-}
-
-/**
- * tracecmd_fill_local_events - Fill a pevent with the events on system
- * @tracing_dir: The directory that contains the events.
- * @pevent: Allocated pevent which will be filled
- *
- * Returns whether the operation succeeded
- */
-int tracecmd_fill_local_events(const char *tracing_dir, struct tep_handle *pevent)
+static int tracecmd_fill_local_events_system(const char *tracing_dir,
+					     struct tep_handle *pevent,
+					     char **sys_names)
 {
+	int ret, i, failure = 0;
 	struct dirent *dent;
 	char *events_dir;
 	struct stat st;
 	DIR *dir;
-	int ret, failure = 0;
 
 	if (!tracing_dir)
 		return -1;
@@ -1189,7 +1161,16 @@ int tracecmd_fill_local_events(const char *tracing_dir, struct tep_handle *peven
 		if (strcmp(name, ".") == 0 ||
 		    strcmp(name, "..") == 0)
 			continue;
-

lib/trace-cmd/trace-util.c int tracecmd_fill_local_events(const char *tracing_dir, struct tep_handle *peven · line 1173

Quoted 10 lines
lib/trace-cmd/trace-util.c +10 -0
+		if (sys_names) {
+			i = 0;
+			while (sys_names[i]) {
+				if (strcmp(name, sys_names[i]) == 0)
+					break;
+				i++;
+			}
+			if (sys_names[i] == NULL)
+				continue;
+		}

I think this would be better done in a separate function, say:

static bool contains(const char *name, const char **names)

{
	while (*names) {
		if (strcmp(name, *names++) == 0)
			return true;
	}
	return false;
}

and then simply have

if (sys_names && !contains(name, sys_names))

	continue;

here.

Quoted 65 lines
lib/trace-cmd/trace-util.c +52 -0
 		sys = append_file(events_dir, name);
 		ret = stat(sys, &st);
 		if (ret < 0 || !S_ISDIR(st.st_mode)) {
@@ -1217,6 +1198,58 @@ int tracecmd_fill_local_events(const char *tracing_dir, struct tep_handle *peven
 	return ret;
 }
 
+/**
+ * tracecmd_local_events_system - create a tep from the events of the specified subsystem.
+ *
+ * @tracing_dir: The directory that contains the events.
+ * @sys_name: Array of system names, to load the events from.
+ * The last element from the array must be NULL
+ *
+ * Returns a tep structure that contains the tep local to
+ * the system.
+ */
+struct tep_handle *tracecmd_local_events_system(const char *tracing_dir,
+						char **sys_names)
+{
+	struct tep_handle *tep = NULL;
+
+	tep = tep_alloc();
+	if (!tep)
+		return NULL;
+
+	if (tracecmd_fill_local_events_system(tracing_dir, tep, sys_names)) {
+		tep_free(tep);
+		tep = NULL;
+	}
+
+	return tep;
+}
+
+/**
+ * tracecmd_local_events - create a pevent from the events on system
+ * @tracing_dir: The directory that contains the events.
+ *
+ * Returns a pevent structure that contains the pevents local to
+ * the system.
+ */
+struct tep_handle *tracecmd_local_events(const char *tracing_dir)
+{
+	return tracecmd_local_events_system(tracing_dir, NULL);
+}
+
+/**
+ * tracecmd_fill_local_events - Fill a pevent with the events on system
+ * @tracing_dir: The directory that contains the events.
+ * @pevent: Allocated pevent which will be filled
+ *
+ * Returns whether the operation succeeded
+ */
+int tracecmd_fill_local_events(const char *tracing_dir,
+			       struct tep_handle *pevent)
+{
+	return tracecmd_fill_local_events_system(tracing_dir, pevent, NULL);
+}
+
 /**
  * tracecmd_local_plugins - returns an array of available tracer plugins
  * @tracing_dir: The directory that contains the tracing directory
Signature · 1 line
2.20.1
#3 lore raw

Re: [PATCH v4 1/7] trace-cmd: Implemented new lib API: tracecmd_local_events_system()

Quoted 27 lines · On Fri, 22 Feb 2019 17:51:04 +0200 Slavomir Kaslev <[email protected]> wrote:
Quoted 5 lines
lib/trace-cmd/trace-util.c +0 -1
@@ -1189,7 +1161,16 @@ int tracecmd_fill_local_events(const char *tracing_dir, struct tep_handle *peven
 		if (strcmp(name, ".") == 0 ||
 		    strcmp(name, "..") == 0)
 			continue;
-

lib/trace-cmd/trace-util.c int tracecmd_fill_local_events(const char *tracing_dir, struct tep_handle *peven · line 1173

Quoted 10 lines
lib/trace-cmd/trace-util.c +10 -0
+		if (sys_names) {
+			i = 0;
+			while (sys_names[i]) {
+				if (strcmp(name, sys_names[i]) == 0)
+					break;
+				i++;
+			}
+			if (sys_names[i] == NULL)
+				continue;
+		}  

I think this would be better done in a separate function, say:

static bool contains(const char *name, const char **names)

{
	while (*names) {
		if (strcmp(name, *names++) == 0)
			return true;
	}
	return false;
}
Quoted 4 lines

and then simply have

if (sys_names && !contains(name, sys_names))

	continue;

Actually, I would argue just to make it a for loop.

	for (i = 0; sys_names[i]; i++) {
		if (strcmp(name, sys_names[i]) == 0)
			goto found;
	}
	continue;

found:

But if you really want the helper function, then have it check sys_names too.

static bool contains(const char *name, const char * const * names)

{
	if (!names)
		return false;
	for (; *names; names++)
		if (strcmp(name, *names) == 0)
			return true;
	return false;
}

if (!contains(name, sys_names))

-- Steve

Quoted 9 lines

here.

Quoted 6 lines
lib/trace-cmd/trace-util.c +0 -0
 		sys = append_file(events_dir, name);
 		ret = stat(sys, &st);
 		if (ret < 0 || !S_ISDIR(st.st_mode)) {
@@ -1217,6 +1198,58 @@ int tracecmd_fill_local_events(const char *tracing_dir, struct tep_handle *peven
 	return ret;
 }
#4 lore raw

PATCH v4 2/7 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 0067882..3f0ab4f 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
#5 lore raw

PATCH v4 3/7 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
#6 lore raw

Re: [PATCH v4 3/7] trace-cmd: Fix tracecmd_read_page_record() to read more than one event

Quoted 17 lines · 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 +0 -0
index be6720c..0a6e820 100644

lib/trace-cmd/trace-input.c tracecmd_read_page_record(struct tep_handle *pevent, void *page, int size, · line 1686

Quoted 10 lines
lib/trace-cmd/trace-input.c +2 -2
@@ -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);

Looks good to me with a nit: the following check is not necessary

lib/trace-cmd/trace-input.c tracecmd_read_page_record(struct tep_handle *pevent, void *page, int size, · line 1688

Quoted 2 lines
lib/trace-cmd/trace-input.c +2 -0
+			if (ptr == last_record->data)
+				break;

since the while () condition above will be already false if this holds. Or am I missing something here?

Thank you,

-- Slavi

Quoted 17 lines
lib/trace-cmd/trace-input.c +4 -2
+		}
 		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
#7 lore raw

PATCH v4 4/7 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
#8 lore raw

PATCH v4 5/7 trace-cmd: Refactored make_instances() and tracecmd_remove_instances()

In reply to Tzvetomir Stoyanov

In order to reuse the code which creates / deletes tracing instances, these two functions are refactotred. A new ones are implemented:
make_instance() and tracecmd_remove_instance().

---
 tracecmd/trace-record.c | 54 +++++++++++++++++++++++------------------
 1 file changed, 31 insertions(+), 23 deletions(-)
tracecmd/trace-record.c +31 -23
index ef6319f..c57ed6b 100644
@@ -4316,49 +4316,57 @@ static void clear_func_filters(void)
 	}
 }
 
-static void make_instances(void)
+static void make_instance(struct buffer_instance *instance)
 {
-	struct buffer_instance *instance;
 	struct stat st;
 	char *path;
 	int ret;
 
+	path = get_instance_dir(instance);
+	ret = stat(path, &st);
+	if (ret < 0) {
+		ret = mkdir(path, 0777);
+		if (ret < 0)
+			die("mkdir %s", path);
+	} else
+		/* Don't delete instances that already exist */
+		instance->flags |= BUFFER_FL_KEEP;
+	tracecmd_put_tracing_file(path);
+
+}
+
+static void make_instances(void)
+{
+	struct buffer_instance *instance;
+
 	for_each_instance(instance) {
 		if (is_guest(instance))
 			continue;
+		make_instance(instance);
+	}
+}
 
-		path = get_instance_dir(instance);
-		ret = stat(path, &st);
-		if (ret < 0) {
-			ret = mkdir(path, 0777);
-			if (ret < 0)
-				die("mkdir %s", path);
-		} else
-			/* Don't delete instances that already exist */
-			instance->flags |= BUFFER_FL_KEEP;
-		tracecmd_put_tracing_file(path);
+static void tracecmd_remove_instance(struct buffer_instance *instance)
+{
+	char *path;
+
+	if (instance->tracing_on_fd > 0) {
+		close(instance->tracing_on_fd);
+		instance->tracing_on_fd = 0;
 	}
+	path = get_instance_dir(instance);
+	tracecmd_put_tracing_file(path);
 }
 
 void tracecmd_remove_instances(void)
 {
 	struct buffer_instance *instance;
-	char *path;
-	int ret;
 
 	for_each_instance(instance) {
 		/* Only delete what we created */
 		if (is_guest(instance) || (instance->flags & BUFFER_FL_KEEP))
 			continue;
-		if (instance->tracing_on_fd > 0) {
-			close(instance->tracing_on_fd);
-			instance->tracing_on_fd = 0;
-		}
-		path = get_instance_dir(instance);
-		ret = rmdir(path);
-		if (ret < 0)
-			die("rmdir %s", path);
-		tracecmd_put_tracing_file(path);
+		tracecmd_remove_instance(instance);
 	}
 }
 
Signature · 1 line
2.20.1
#9 lore raw

PATCH v4 6/7 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_pids(), to retrieve VCPU-task mapping for given VM.

---
 tracecmd/include/trace-local.h |  3 ++
 tracecmd/trace-record.c        | 57 +++++++++++++++++++++++++++++++++-
 2 files changed, 59 insertions(+), 1 deletion(-)
tracecmd/include/trace-local.h +3 -0
index d7bdb1f..ef6848f 100644
@@ -236,6 +236,9 @@ void show_instance_file(struct buffer_instance *instance, const char *name);
 
 int count_cpus(void);
 
+#define VCPUS_MAX	256
+int *get_guest_vcpu_pids(int cid);
+
 /* 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 +56 -1

83 lines not on this page · raw message

Signature · 1 line
2.20.1
#10 lore raw

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

Quoted 17 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_pids(), to retrieve VCPU-task mapping for given VM.

---
 tracecmd/include/trace-local.h |  3 ++
 tracecmd/trace-record.c        | 57 +++++++++++++++++++++++++++++++++-
 2 files changed, 59 insertions(+), 1 deletion(-)
tracecmd/include/trace-local.h +0 -0
index d7bdb1f..ef6848f 100644

tracecmd/include/trace-local.h void show_instance_file(struct buffer_instance *instance, const char *name); · line 239

Quoted 4 lines
tracecmd/include/trace-local.h +1 -0
@@ -236,6 +236,9 @@ void show_instance_file(struct buffer_instance *instance, const char *name);
 
 int count_cpus(void);
 
+#define VCPUS_MAX	256

Is VCPUS_MAX intended to be used somewhere outside of trace-record.c? If not, I think we should keep it there locally and not expose it.

Quoted 30 lines
tracecmd/include/trace-local.h +2 -0
+int *get_guest_vcpu_pids(int cid);
+
 /* 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 +3 -1
index c57ed6b..59f0197 100644
@@ -2752,6 +2752,7 @@ struct guest {
 	char *name;
 	int cid;
 	int pid;
+	int cpu_pid[VCPUS_MAX];
 };
 
 static struct guest *guests;
@@ -2772,9 +2773,10 @@ static char *get_qemu_guest_name(char *arg)
 static void read_qemu_guests(void)
 {
 	static bool initialized;
+	struct dirent *entry_t;
 	struct dirent *entry;
 	char path[PATH_MAX];
-	DIR *dir;
+	DIR *dir, *dir_t;
 
 	if (initialized)
 		return;

tracecmd/trace-record.c static void read_qemu_guests(void) · line 2834

Quoted 3 lines
tracecmd/trace-record.c +0 -0
@@ -2830,6 +2832,49 @@ static void read_qemu_guests(void)
 		if (!is_qemu)
 			goto next;
 

I think the following section should go into separate function since this one is already getting too long. Then here we can just do:

		read_qemu_guest_vcpus(&guest);

Where read_qemu_guest_vcpus can be something like that:

static void read_qemu_guest_vcpus(struct guest *guest)

{
	struct dirent *entry;
	char path[PATH_MAX];
	size_t buf_len = 0;
	char *buf = NULL;
	int vcpu;
	DIR *dir;
	FILE *f;

	snprintf(path, sizeof(path), "/proc/%d/task", guest->pid);
	dir = opendir(path);
	if (!dir)
		return;

	for (entry = readdir(dir); entry; entry = readdir(dir)) {
		if (!(entry->d_type == DT_DIR && is_digits(entry->d_name)))
			continue;

		snprintf(path, sizeof(path), "/proc/%d/task/%s/comm",
			 guest->pid, entry->d_name);
		f = fopen(path, "r");
		if (!f)
			continue;

		if (getline(&buf, &buf_len, f) < 0)
			goto next;

		if (strncmp(buf, "CPU ", 4) != 0)
			goto next;

		vcpu = atoi(buf + 4);
		if (!(vcpu >= 0 && vcpu < VCPUS_MAX))
			goto next;

		guest->cpu_pid[vcpu] = atoi(entry->d_name);

next:

		fclose(f);
	}

	free(buf);
}

Thank you,

-- Slavi

Quoted 66 lines
tracecmd/trace-record.c +53 -0
+		snprintf(path, sizeof(path), "/proc/%s/task", entry->d_name);
+		dir_t = opendir(path);
+		if (dir_t) {
+			unsigned int vcpu;
+			char *buf = NULL;
+			char *cpu_str;
+			FILE *ft;
+			size_t n;
+			int j;
+
+			for (entry_t = readdir(dir_t); entry_t; entry_t = readdir(dir_t)) {
+				if (!(entry_t->d_type == DT_DIR &&
+				    is_digits(entry_t->d_name)))
+					continue;
+				snprintf(path, sizeof(path),
+					 "/proc/%s/task/%s/comm",
+					 entry->d_name, entry_t->d_name);
+				ft = fopen(path, "r");
+				if (!ft)
+					continue;
+				getline(&buf, &n, ft);
+				if (buf && strncmp(buf, "CPU ", 4) == 0) {
+					cpu_str = buf;
+					while (*cpu_str != '\0' &&
+						isdigit(*cpu_str) == 0)
+						cpu_str++;
+					if (*cpu_str != '\0') {
+						j = 0;
+						while (cpu_str[j] != '\0' &&
+						       isdigit(cpu_str[j]) != 0)
+							j++;
+						cpu_str[j] = '\0';
+						vcpu = atoi(cpu_str);
+						if (vcpu < VCPUS_MAX)
+							guest.cpu_pid[vcpu] = atoi(entry_t->d_name);
+					}
+				}
+				free(buf);
+				fclose(ft);
+				buf = NULL;
+			}
+		}
+
 		guests = realloc(guests, (guests_len + 1) * sizeof(*guests));
 		if (!guests)
 			die("Can not allocate guest buffer");
@@ -2875,6 +2920,16 @@ static char *parse_guest_name(char *guest, int *cid, int *port)
 	return guest;
 }
 
+int *get_guest_vcpu_pids(int cid)
+{
+	int i;
+
+	for (i = 0; i < guests_len; i++)
+		if (cid == guests[i].cid)
+			return guests[i].cpu_pid;
+	return NULL;
+}
+
 static void set_prio(int prio)
 {
 	struct sched_param sp;
Signature · 1 line
2.20.1
#11 lore raw

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

Quoted 2 lines · On Fri, 22 Feb 2019 19:07:09 +0200 Slavomir Kaslev <[email protected]> wrote:

I think the following section should go into separate function since this one is already getting too long. Then here we can just do:

Thanks for reviewing these Slavomir.

Tvzetomir, can you update the patch series and send out a new version with these updates when you get a chance.

Thanks!

-- Steve

#12 lore raw

PATCH v4 7/7 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  |   4 +
 tracecmd/include/trace-local.h |  13 +
 tracecmd/trace-listen.c        |   3 +
 tracecmd/trace-msg.c           | 477 ++++++++++++++++++++++++++++++++-
 tracecmd/trace-record.c        | 156 ++++++++++-
 5 files changed, 634 insertions(+), 19 deletions(-)
include/trace-cmd/trace-cmd.h +4 -0
index 3f0ab4f..5469dee 100644
@@ -346,6 +346,10 @@ int tracecmd_msg_recv_trace_resp(struct tracecmd_msg_handle *msg_handle,
 				 int *nr_cpus, int *page_size,
 				 unsigned int **ports, bool *use_fifos);
 
+int tracecmd_msg_rcv_time_sync(struct tracecmd_msg_handle *msg_handle);
+int tracecmd_msg_snd_time_sync(struct tracecmd_msg_handle *msg_handle,
+			       char *clock_str, long long *toffset);
+
 /* --- Plugin handling --- */
 extern struct tep_plugin_option trace_ftrace_options[];
 
tracecmd/include/trace-local.h +13 -0
index ef6848f..948cb06 100644
@@ -236,6 +236,19 @@ void show_instance_file(struct buffer_instance *instance, const char *name);
 
 int count_cpus(void);
 
+struct clock_synch_event_descr {
+	char *file;
+	char *set;
+	char *reset;
+};
+struct buffer_instance *clock_synch_enable(char *clock, unsigned int cid,
+					   struct clock_synch_event_descr *events);
+void clock_synch_disable(struct buffer_instance *instance,
+			 struct clock_synch_event_descr *events);
+struct tep_handle *clock_synch_get_tep(struct buffer_instance *instance,
+				       char **systems);
+void get_vsocket_params(int fd, unsigned int *lcid, unsigned int *lport,
+			unsigned int *rcid, unsigned int *rport);
 #define VCPUS_MAX	256
 int *get_guest_vcpu_pids(int cid);
 
tracecmd/trace-listen.c +3 -0
index 8bd7bad..7103a88 100644
@@ -404,6 +404,9 @@ static int communicate_with_client(struct tracecmd_msg_handle *msg_handle)
 
 		msg_handle->version = V3_PROTOCOL;
 
+		/* time sync with the v3 client */
+		tracecmd_msg_rcv_time_sync(msg_handle);
+
 		/* read the CPU count, the page size, and options */
 		if ((pagesize = tracecmd_msg_initial_setting(msg_handle)) < 0)
 			goto out;
tracecmd/trace-msg.c +465 -12

523 lines not on this page · raw message

tracecmd/trace-record.c +149 -7

192 lines not on this page · raw message

Signature · 1 line
2.20.1