diff mbox series

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

Message ID 20190222142836.12596-7-tstoyanov@vmware.com (mailing list archive)
State Superseded
Headers show
Series trace-cmd: Timetamps sync between host and guest machines, relying on vsock events. | expand

Commit Message

Tzvetomir Stoyanov Feb. 22, 2019, 2:28 p.m. UTC
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.

Signed-off-by: Tzvetomir Stoyanov <tstoyanov@vmware.com>
---
 tracecmd/include/trace-local.h |  3 ++
 tracecmd/trace-record.c        | 57 +++++++++++++++++++++++++++++++++-
 2 files changed, 59 insertions(+), 1 deletion(-)

Comments

Slavomir Kaslev Feb. 22, 2019, 5:07 p.m. UTC | #1
On Fri, Feb 22, 2019 at 04:28:35PM +0200, Tzvetomir Stoyanov wrote:
> 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.
> 
> Signed-off-by: Tzvetomir Stoyanov <tstoyanov@vmware.com>
> ---
>  tracecmd/include/trace-local.h |  3 ++
>  tracecmd/trace-record.c        | 57 +++++++++++++++++++++++++++++++++-
>  2 files changed, 59 insertions(+), 1 deletion(-)
> 
> diff --git a/tracecmd/include/trace-local.h b/tracecmd/include/trace-local.h
> index d7bdb1f..ef6848f 100644
> --- a/tracecmd/include/trace-local.h
> +++ b/tracecmd/include/trace-local.h
> @@ -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.

> +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 */
> diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
> index c57ed6b..59f0197 100644
> --- a/tracecmd/trace-record.c
> +++ b/tracecmd/trace-record.c
> @@ -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;
> @@ -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

> +		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;
> -- 
> 2.20.1
>
Steven Rostedt Feb. 22, 2019, 7:25 p.m. UTC | #2
On Fri, 22 Feb 2019 19:07:09 +0200
Slavomir Kaslev <kaslevs@vmware.com> 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
diff mbox series

Patch

diff --git a/tracecmd/include/trace-local.h b/tracecmd/include/trace-local.h
index d7bdb1f..ef6848f 100644
--- a/tracecmd/include/trace-local.h
+++ b/tracecmd/include/trace-local.h
@@ -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 */
diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index c57ed6b..59f0197 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -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;
@@ -2830,6 +2832,49 @@  static void read_qemu_guests(void)
 		if (!is_qemu)
 			goto next;
 
+		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;