All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org
Subject: [PATCH v22 08/13] trace-cmd: Add guest information in host's trace.dat file
Date: Wed,  4 Mar 2020 11:12:15 +0200	[thread overview]
Message-ID: <20200304091220.30936-9-tz.stoyanov@gmail.com> (raw)
In-Reply-To: <20200304091220.30936-1-tz.stoyanov@gmail.com>

New trace.dat option is introduced: TRACECMD_OPTION_GUEST.
Written in the host's trace.dat file, it contains information about
guests, traced at the same time: guest trace ID, number of VCPUs and
PIDs of the host tasks, running those VCPU. The data is stored in
the file as:
	Guest name, null terminated string
	long long (8 bytes) trace-id
	int (4 bytes) number of guest CPUs
	array of size number of guest CPUs:
		int (4 bytes) Guest CPU id
		int (4 bytes) Host PID, running the guest CPU

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 include/trace-cmd/trace-cmd.h |   5 ++
 lib/trace-cmd/trace-input.c   | 128 ++++++++++++++++++++++++++++++++++
 tracecmd/trace-dump.c         |  62 ++++++++++++++++
 tracecmd/trace-record.c       |  61 ++++++++++++++++
 4 files changed, 256 insertions(+)

diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h
index ac46637e..0375f500 100644
--- a/include/trace-cmd/trace-cmd.h
+++ b/include/trace-cmd/trace-cmd.h
@@ -111,6 +111,7 @@ enum {
 	TRACECMD_OPTION_PROCMAPS,
 	TRACECMD_OPTION_TRACEID,
 	TRACECMD_OPTION_TIME_SHIFT,
+	TRACECMD_OPTION_GUEST,
 };
 
 enum {
@@ -154,6 +155,10 @@ void tracecmd_set_flag(struct tracecmd_input *handle, int flag);
 void tracecmd_clear_flag(struct tracecmd_input *handle, int flag);
 unsigned long tracecmd_get_flags(struct tracecmd_input *handle);
 unsigned long long tracecmd_get_traceid(struct tracecmd_input *handle);
+int tracecmd_get_guest_cpumap(struct tracecmd_input *handle,
+			      unsigned long long trace_id,
+			      char **name,
+			      int *vcpu_count, int **cpu_pid);
 unsigned long long tracecmd_get_tsync_peer(struct tracecmd_input *handle);
 int tracecmd_enable_tsync(struct tracecmd_input *handle, bool enable);
 
diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c
index 2f0274d9..01a1def4 100644
--- a/lib/trace-cmd/trace-input.c
+++ b/lib/trace-cmd/trace-input.c
@@ -82,6 +82,14 @@ struct ts_offset_sample {
 	long long	offset;
 };
 
+struct guest_trace_info {
+	struct guest_trace_info	*next;
+	char			*name;
+	unsigned long long	trace_id;
+	int			vcpu_count;
+	int			*cpu_pid;
+};
+
 struct host_trace_info {
 	bool			sync_enable;
 	unsigned long long	trace_id;
@@ -115,6 +123,7 @@ struct tracecmd_input {
 	char *			trace_clock;
 	struct input_buffer_instance	*buffers;
 	int			parsing_failures;
+	struct guest_trace_info	*guest;
 
 	struct tracecmd_ftrace	finfo;
 
@@ -2197,6 +2206,86 @@ static void procmap_free(struct pid_addr_maps *maps)
 	free(maps);
 }
 
+static void trace_guests_free(struct tracecmd_input *handle)
+{
+	struct guest_trace_info *guest;
+
+	while (handle->guest) {
+		guest = handle->guest;
+		handle->guest = handle->guest->next;
+		free(guest->name);
+		free(guest->cpu_pid);
+		free(guest);
+	}
+}
+
+static int trace_guest_load(struct tracecmd_input *handle, char *buf, int size)
+{
+	struct guest_trace_info *guest = NULL;
+	int cpu;
+	int i;
+
+	guest = calloc(1, sizeof(struct guest_trace_info));
+	if (!guest)
+		goto error;
+
+	/*
+	 * Guest name, null terminated string
+	 * long long (8 bytes) trace-id
+	 * int (4 bytes) number of guest CPUs
+	 * array of size number of guest CPUs:
+	 *	int (4 bytes) Guest CPU id
+	 *	int (4 bytes) Host PID, running the guest CPU
+	 */
+
+	guest->name = strndup(buf, size);
+	if (!guest->name)
+		goto error;
+	buf += strlen(guest->name) + 1;
+	size -= strlen(guest->name) + 1;
+
+	if (size < sizeof(long long))
+		goto error;
+	guest->trace_id = tep_read_number(handle->pevent, buf, sizeof(long long));
+	buf += sizeof(long long);
+	size -= sizeof(long long);
+
+	if (size < sizeof(int))
+		goto error;
+	guest->vcpu_count = tep_read_number(handle->pevent, buf, sizeof(int));
+	buf += sizeof(int);
+	size -= sizeof(int);
+
+	guest->cpu_pid = calloc(guest->vcpu_count, sizeof(int));
+	if (!guest->cpu_pid)
+		goto error;
+
+	for (i = 0; i < guest->vcpu_count; i++) {
+		if (size < 2 * sizeof(int))
+			goto error;
+		cpu = tep_read_number(handle->pevent, buf, sizeof(int));
+		buf += sizeof(int);
+		if (cpu >= guest->vcpu_count)
+			goto error;
+		guest->cpu_pid[cpu] = tep_read_number(handle->pevent,
+						      buf, sizeof(int));
+		buf += sizeof(int);
+		size -= 2 * sizeof(int);
+	}
+
+	guest->next = handle->guest;
+	handle->guest = guest;
+	return 0;
+
+error:
+	if (guest) {
+		free(guest->cpu_pid);
+		free(guest->name);
+		free(guest);
+	}
+	return -1;
+}
+
 /* Needs to be a constant, and 4K should be good enough */
 #define STR_PROCMAP_LINE_MAX	4096
 static int trace_pid_map_load(struct tracecmd_input *handle, char *buf)
@@ -2468,6 +2557,9 @@ static int handle_options(struct tracecmd_input *handle)
 			handle->trace_id = tep_read_number(handle->pevent,
 							   &cpus, 8);
 			break;
+		case TRACECMD_OPTION_GUEST:
+			trace_guest_load(handle, buf, size);
+			break;
 		default:
 			warning("unknown option %d", option);
 			break;
@@ -3122,6 +3214,7 @@ void tracecmd_close(struct tracecmd_input *handle)
 	handle->pid_maps = NULL;
 
 	trace_tsync_offset_free(&handle->host);
+	trace_guests_free(handle);
 
 	if (handle->flags & TRACECMD_FL_BUFFER_INSTANCE)
 		tracecmd_close(handle->parent);
@@ -3586,6 +3679,41 @@ unsigned long long tracecmd_get_traceid(struct tracecmd_input *handle)
 	return handle->trace_id;
 }
 
+/**
+ * tracecmd_get_guest_cpumap - get the mapping of guest VCPU to host process
+ * @handle: input handle for the trace.dat file
+ * @trace_id: ID of the guest tracing session
+ * @name: return, name of the guest
+ * @vcpu_count: return, number of VPUs
+ * @cpu_pid: return, array with guest VCPU to host process mapping
+ *
+ * Returns @name of the guest, number of VPUs (@vcpu_count)
+ * and array @cpu_pid with size @vcpu_count. Array index is VCPU id, array
+ * content is PID of the host process, running this VCPU.
+ *
+ * This information is stored in host trace.dat file
+ */
+int tracecmd_get_guest_cpumap(struct tracecmd_input *handle,
+			      unsigned long long trace_id,
+			      char **name,
+			      int *vcpu_count, int **cpu_pid)
+{
+	struct guest_trace_info	*guest = handle->guest;
+
+	while (guest) {
+		if (guest->trace_id == trace_id)
+			break;
+		guest = guest->next;
+	}
+	if (!guest)
+		return -1;
+
+	*name = guest->name;
+	*vcpu_count = guest->vcpu_count;
+	*cpu_pid = guest->cpu_pid;
+	return 0;
+}
+
 /**
  * tracecmd_get_tsync_peer - get the trace session id of the peer host
  * @handle: input handle for the trace.dat file
diff --git a/tracecmd/trace-dump.c b/tracecmd/trace-dump.c
index b4beb8b2..ffb1c6b1 100644
--- a/tracecmd/trace-dump.c
+++ b/tracecmd/trace-dump.c
@@ -412,6 +412,65 @@ out:
 	free(offsets);
 }
 
+void dump_option_guest(int fd, int size)
+{
+	unsigned long long trace_id;
+	char *buf, *p;
+	int cpu, pid;
+	int cpus;
+	int i;
+
+	do_print(OPTIONS, "\t\t[Option GUEST, %d bytes]\n", size);
+
+	/*
+	 * Guest name, null terminated string
+	 * long long (8 bytes) trace-id
+	 * int (4 bytes) number of guest CPUs
+	 * array of size number of guest CPUs:
+	 *	int (4 bytes) Guest CPU id
+	 *	int (4 bytes) Host PID, running the guest CPU
+	 */
+	buf = calloc(1, size);
+	if (!buf)
+		return;
+	if (read_file_bytes(fd, buf, size))
+		goto out;
+
+	p = buf;
+	do_print(OPTIONS, "%s [Guest name]\n", p);
+	size -= strlen(buf) + 1;
+	p += strlen(buf) + 1;
+
+	if (size < sizeof(long long))
+		goto out;
+	trace_id = tep_read_number(tep, p, sizeof(long long));
+	size -= sizeof(long long);
+	p += sizeof(long long);
+	do_print(OPTIONS, "0x%llX [trace id]\n", trace_id);
+
+	if (size < sizeof(int))
+		goto out;
+	cpus = tep_read_number(tep, p, sizeof(int));
+	size -= sizeof(int);
+	p += sizeof(int);
+	do_print(OPTIONS, "%d [Guest CPUs]\n", cpus);
+
+	for (i = 0; i < cpus; i++) {
+		if (size < 2 * sizeof(int))
+			goto out;
+		cpu = tep_read_number(tep, p, sizeof(int));
+		size -= sizeof(int);
+		p += sizeof(int);
+		pid = tep_read_number(tep, p, sizeof(int));
+		size -= sizeof(int);
+		p += sizeof(int);
+		do_print(OPTIONS, "  %d %d [guest cpu, host pid]\n", cpu, pid);
+	}
+
+out:
+	free(buf);
+}
+
 static void dump_options(int fd)
 {
 	unsigned short option;
@@ -468,6 +527,9 @@ static void dump_options(int fd)
 		case TRACECMD_OPTION_TIME_SHIFT:
 			dump_option_timeshift(fd, size);
 			break;
+		case TRACECMD_OPTION_GUEST:
+			dump_option_guest(fd, size);
+			break;
 		default:
 			do_print(OPTIONS, " %d %d\t[Unknown option, size - skipping]\n",
 				 option, size);
diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index ef9d7e3e..f1e2a391 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -3061,6 +3061,19 @@ static int set_vcpu_pid_mapping(struct guest *guest, int cpu, int pid)
 	return 0;
 }
 
+static struct guest *get_guest_info(unsigned int guest_cid)
+{
+	int i;
+
+	if (!guests)
+		return NULL;
+
+	for (i = 0; i < guests_len; i++)
+		if (guest_cid == guests[i].cid)
+			return guests + i;
+	return NULL;
+}
+
 static char *get_qemu_guest_name(char *arg)
 {
 	char *tok, *end = arg;
@@ -3875,6 +3888,49 @@ static void append_buffer(struct tracecmd_output *handle,
 	}
 }
 
+static void
+add_guest_info(struct tracecmd_output *handle, struct buffer_instance *instance)
+{
+	struct guest *guest = get_guest_info(instance->cid);
+	char *buf, *p;
+	int size;
+	int i;
+
+	if (!guest)
+		return;
+	for (i = 0; i < guest->cpu_max; i++)
+		if (!guest->cpu_pid[i])
+			break;
+
+	size = strlen(guest->name) + 1;
+	size +=  sizeof(long long);	/* trace_id */
+	size +=  sizeof(int);		/* cpu count */
+	size += i * 2 * sizeof(int);	/* cpu,pid pair */
+
+	buf = calloc(1, size);
+	if (!buf)
+		return;
+	p = buf;
+	strcpy(p, guest->name);
+	p += strlen(guest->name) + 1;
+
+	memcpy(p, &instance->trace_id, sizeof(long long));
+	p += sizeof(long long);
+
+	memcpy(p, &i, sizeof(int));
+	p += sizeof(int);
+	for (i = 0; i < guest->cpu_max; i++) {
+		if (!guest->cpu_pid[i])
+			break;
+		memcpy(p, &i, sizeof(int));
+		p += sizeof(int);
+		memcpy(p, &guest->cpu_pid[i], sizeof(int));
+		p += sizeof(int);
+	}
+
+	tracecmd_add_option(handle, TRACECMD_OPTION_GUEST, size, buf);
+	free(buf);
+}
 
 static void
 add_pid_maps(struct tracecmd_output *handle, struct buffer_instance *instance)
@@ -4159,6 +4215,11 @@ static void record_data(struct common_record_context *ctx)
 			add_pid_maps(handle, instance);
 		}
 
+		for_all_instances(instance) {
+			if (is_guest(instance))
+				add_guest_info(handle, instance);
+		}
+
 		tracecmd_append_cpu_data(handle, local_cpu_count, temp_files);
 
 		for (i = 0; i < max_cpu_count; i++)
-- 
2.24.1


  parent reply	other threads:[~2020-03-04  9:12 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-04  9:12 [PATCH v22 00/13] Timestamp synchronization of host - guest tracing session Tzvetomir Stoyanov (VMware)
2020-03-04  9:12 ` [PATCH v22 01/13] trace-cmd: Find and store pids of tasks, which run virtual CPUs of given VM Tzvetomir Stoyanov (VMware)
2020-03-04 21:09   ` Steven Rostedt
2020-03-04  9:12 ` [PATCH v22 02/13] trace-cmd: Implement new API tracecmd_add_option_v() Tzvetomir Stoyanov (VMware)
2020-03-04  9:12 ` [PATCH v22 03/13] trace-cmd: Add new API to generate a unique ID of the tracing session Tzvetomir Stoyanov (VMware)
2020-03-04  9:12 ` [PATCH v22 04/13] trace-cmd: Store the session tracing ID in the trace.dat file Tzvetomir Stoyanov (VMware)
2020-03-04  9:12 ` [PATCH v22 05/13] trace-cmd: Add definitions of htonll() and ntohll() Tzvetomir Stoyanov (VMware)
2020-03-05 15:48   ` [PATCH] trace-cmd: Make functions in trace-write-local.h inline Steven Rostedt
2020-03-04  9:12 ` [PATCH v22 06/13] trace-cmd: Exchange tracing IDs between host and guest Tzvetomir Stoyanov (VMware)
2020-03-04  9:12 ` [PATCH v22 07/13] trace-cmd: Implement new option in trace.dat file: TRACECMD_OPTION_TIME_SHIFT Tzvetomir Stoyanov (VMware)
2020-03-05 17:46   ` [PATCH] trace-cmd: Adjust host_trace_info structure to be better packed Steven Rostedt
2020-03-04  9:12 ` Tzvetomir Stoyanov (VMware) [this message]
2020-03-05 18:44   ` [PATCH] trace-cmd: Make name and cpu_pid into const pointers to tracecmd_get_guest_cpumap() Steven Rostedt
2020-03-04  9:12 ` [PATCH v22 09/13] trace-cmd: Add host trace clock as guest trace argument Tzvetomir Stoyanov (VMware)
2020-03-04  9:12 ` [PATCH v22 10/13] trace-cmd: Refactor few trace-cmd internal functions Tzvetomir Stoyanov (VMware)
2020-03-04  9:12 ` [PATCH v22 11/13] trace-cmd: Basic infrastructure for host - guest timestamp synchronization Tzvetomir Stoyanov (VMware)
2020-03-04  9:12 ` [PATCH v22 12/13] trace-cmd: [POC] PTP-like algorithm " Tzvetomir Stoyanov (VMware)
2020-03-04  9:12 ` [PATCH v22 13/13] trace-cmd: Debug scripts for " Tzvetomir Stoyanov (VMware)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200304091220.30936-9-tz.stoyanov@gmail.com \
    --to=tz.stoyanov@gmail.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.