Skip to contentSkip to conversation

trace-cmd: Clean ups for agent communications

PATCH New Steven Rostedt 1 participant 5 messages Apr 28, 2022

lore
Share & tools
lore
#0 lore raw

cover trace-cmd: Clean ups for agent communications

From: "Steven Rostedt (Google)" <[email protected]>

In preparation to have an agent proxy, these are some clean up patches to the agent code that should help simplify the modifications.

Steven Rostedt (Google) (4):
trace-cmd: Move add_guest_info() into trace-vm.c
trace-cmd: Simplify add_guest()
trace-cmd: Move find_tasks() into add_guest()
trace-cmd: Move find_pid_by_cid() into add_guest()

 tracecmd/include/trace-local.h |   1 +
 tracecmd/trace-record.c        |  89 +------------------------
 tracecmd/trace-vm.c            | 117 ++++++++++++++++++++++++++++-----
 3 files changed, 104 insertions(+), 103 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/4 trace-cmd: Move add_guest_info() into trace-vm.c

From: "Steven Rostedt (Google)" <[email protected]>

In preparation for the agent proxy, move add_guest_info() into trace-vm.c and rename it to trace_add_guest_info(). This will be used by the agent when acting as a proxy.

---
 tracecmd/include/trace-local.h |  1 +
 tracecmd/trace-record.c        | 54 +---------------------------------
 tracecmd/trace-vm.c            | 52 ++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 53 deletions(-)
tracecmd/include/trace-local.h +1 -0
index e3fec1319880..f3b805fa7bad 100644
@@ -339,6 +339,7 @@ bool trace_have_guests_pid(void);
 void read_qemu_guests(void);
 int get_guest_pid(unsigned int guest_cid);
 int get_guest_vcpu_pid(unsigned int guest_cid, unsigned int guest_vcpu);
+void trace_add_guest_info(struct tracecmd_output *handle, struct buffer_instance *instance);
 
 /* moved from trace-cmd.h */
 void tracecmd_remove_instances(void);
tracecmd/trace-record.c +1 -53

66 lines not on this page · raw message

tracecmd/trace-vm.c +52 -0

55 lines not on this page · raw message

Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/4 trace-cmd: Simplify add_guest()

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

Simplify the function add_guest by using a local variable to point to the array element instead of dereferencing the array every time.

---
 tracecmd/trace-vm.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
tracecmd/trace-vm.c +11 -7
index 281843b223f6..f0513950aa26 100644
@@ -56,18 +56,22 @@ bool trace_have_guests_pid(void)
 
 static struct trace_guest *add_guest(unsigned int cid, const char *name)
 {
+	struct trace_guest *guest;
+
 	guests = realloc(guests, (guests_len + 1) * sizeof(*guests));
 	if (!guests)
 		die("allocating new guest");
-	memset(&guests[guests_len], 0, sizeof(struct trace_guest));
-	guests[guests_len].name = strdup(name);
-	if (!guests[guests_len].name)
+
+	guest = &guests[guests_len++];
+
+	memset(guest, 0, sizeof(*guest));
+	guest->name = strdup(name);
+	if (!guest->name)
 		die("allocating guest name");
-	guests[guests_len].cid = cid;
-	guests[guests_len].pid = -1;
-	guests_len++;
+	guest->cid = cid;
+	guest->pid = -1;
 
-	return &guests[guests_len - 1];
+	return guest;
 }
 
 static struct tracefs_instance *start_trace_connect(void)
Signature · 1 line
2.35.1
#3 lore raw

PATCH 3/4 trace-cmd: Move find_tasks() into add_guest()

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

In order to simplify the logic a bit, move the find_tasks() code (to find the PIDs on the host that map to the guest) into the add_guest() function where the guest is first added.

---
 tracecmd/trace-record.c | 35 -----------------------------------
 tracecmd/trace-vm.c     | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 35 deletions(-)
tracecmd/trace-record.c +0 -35
index d4b5d4d6490f..a4a0d63d907d 100644
@@ -3215,38 +3215,6 @@ static int do_accept(int sd)
 	return -1;
 }
 
-/* Find all the tasks associated with the guest pid */
-static void find_tasks(struct trace_guest *guest)
-{
-	struct dirent *dent;
-	char *path;
-	DIR *dir;
-	int ret;
-	int tasks = 0;
-
-	ret = asprintf(&path, "/proc/%d/task", guest->pid);
-	if (ret < 0)
-		return;
-
-	dir = opendir(path);
-	free(path);
-	if (!dir)
-		return;
-
-	while ((dent = readdir(dir))) {
-		int *pids;
-		if (!(dent->d_type == DT_DIR && is_digits(dent->d_name)))
-			continue;
-		pids = realloc(guest->task_pids, sizeof(int) * (tasks + 2));
-		if (!pids)
-			break;
-		pids[tasks++] = strtol(dent->d_name, NULL, 0);
-		pids[tasks] = -1;
-		guest->task_pids = pids;
-	}
-	closedir(dir);
-}
-
 static char *parse_guest_name(char *gname, int *cid, int *port,
 			      struct addrinfo **res)
 {
@@ -3287,9 +3255,6 @@ static char *parse_guest_name(char *gname, int *cid, int *port,
 		guest = trace_get_guest(*cid, gname);
 	if (guest) {
 		*cid = guest->cid;
-		/* Mapping not found, search for them */
-		if (!guest->cpu_pid)
-			find_tasks(guest);
 		return guest->name;
 	}
 
tracecmd/trace-vm.c +34 -0
index f0513950aa26..bf2b0695d09b 100644
@@ -54,6 +54,38 @@ bool trace_have_guests_pid(void)
 	return true;
 }
 
+/* Find all the tasks associated with the guest pid */
+static void find_tasks(struct trace_guest *guest)
+{
+	struct dirent *dent;
+	char *path;
+	DIR *dir;
+	int ret;
+	int tasks = 0;
+
+	ret = asprintf(&path, "/proc/%d/task", guest->pid);
+	if (ret < 0)
+		return;
+
+	dir = opendir(path);
+	free(path);
+	if (!dir)
+		return;
+
+	while ((dent = readdir(dir))) {
+		int *pids;
+		if (!(dent->d_type == DT_DIR && is_digits(dent->d_name)))
+			continue;
+		pids = realloc(guest->task_pids, sizeof(int) * (tasks + 2));
+		if (!pids)
+			break;
+		pids[tasks++] = strtol(dent->d_name, NULL, 0);
+		pids[tasks] = -1;
+		guest->task_pids = pids;
+	}
+	closedir(dir);
+}
+
 static struct trace_guest *add_guest(unsigned int cid, const char *name)
 {
 	struct trace_guest *guest;
@@ -71,6 +103,8 @@ static struct trace_guest *add_guest(unsigned int cid, const char *name)
 	guest->cid = cid;
 	guest->pid = -1;
 
+	find_tasks(guest);
+
 	return guest;
 }
 
Signature · 1 line
2.35.1
#4 lore raw

PATCH 4/4 trace-cmd: Move find_pid_by_cid() into add_guest()

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

To simplify the code even more, move the find_pid_by_cid() into add_guest() to map the main pid process to the cid.

---
 tracecmd/trace-vm.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
tracecmd/trace-vm.c +5 -8
index bf2b0695d09b..cabe4baffecb 100644
@@ -86,6 +86,8 @@ static void find_tasks(struct trace_guest *guest)
 	closedir(dir);
 }
 
+static void find_pid_by_cid(struct trace_guest *guest);
+
 static struct trace_guest *add_guest(unsigned int cid, const char *name)
 {
 	struct trace_guest *guest;
@@ -103,6 +105,7 @@ static struct trace_guest *add_guest(unsigned int cid, const char *name)
 	guest->cid = cid;
 	guest->pid = -1;
 
+	find_pid_by_cid(guest);
 	find_tasks(guest);
 
 	return guest;
@@ -341,11 +344,8 @@ struct trace_guest *trace_get_guest(unsigned int cid, const char *name)
 
 	if (cid > 0) {
 		guest = get_guest_by_cid(cid);
-		if (!guest && name) {
+		if (!guest && name)
 			guest = add_guest(cid, name);
-			if (guest)
-				find_pid_by_cid(guest);
-		}
 	}
 	return guest;
 }
@@ -355,7 +355,6 @@ struct trace_guest *trace_get_guest(unsigned int cid, const char *name)
 #define VM_CID_ID	"address='"
 static void read_guest_cid(char *name)
 {
-	struct trace_guest *guest;
 	char *cmd = NULL;
 	char line[512];
 	char *cid;
@@ -377,9 +376,7 @@ static void read_guest_cid(char *name)
 		cid_id = strtol(cid + strlen(VM_CID_ID), NULL, 10);
 		if ((cid_id == INT_MIN || cid_id == INT_MAX) && errno == ERANGE)
 			continue;
-		guest = add_guest(cid_id, name);
-		if (guest)
-			find_pid_by_cid(guest);
+		add_guest(cid_id, name);
 		break;
 	}
 
Signature · 1 line
2.35.1