From patchwork Thu Apr 28 15:06:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 12830875 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0CC97C433F5 for ; Thu, 28 Apr 2022 15:06:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230392AbiD1PJz (ORCPT ); Thu, 28 Apr 2022 11:09:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231387AbiD1PJx (ORCPT ); Thu, 28 Apr 2022 11:09:53 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A078126AD3 for ; Thu, 28 Apr 2022 08:06:38 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4406161E56 for ; Thu, 28 Apr 2022 15:06:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 99F8FC385AE; Thu, 28 Apr 2022 15:06:37 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nk5j2-003JH6-8i; Thu, 28 Apr 2022 11:06:36 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 1/4] trace-cmd: Move add_guest_info() into trace-vm.c Date: Thu, 28 Apr 2022 11:06:32 -0400 Message-Id: <20220428150635.789051-2-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220428150635.789051-1-rostedt@goodmis.org> References: <20220428150635.789051-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" 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. Signed-off-by: Steven Rostedt (Google) --- tracecmd/include/trace-local.h | 1 + tracecmd/trace-record.c | 54 +--------------------------------- tracecmd/trace-vm.c | 52 ++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 53 deletions(-) diff --git a/tracecmd/include/trace-local.h b/tracecmd/include/trace-local.h index e3fec1319880..f3b805fa7bad 100644 --- a/tracecmd/include/trace-local.h +++ b/tracecmd/include/trace-local.h @@ -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); diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 27c4e7ba6f3f..d4b5d4d6490f 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -4297,58 +4297,6 @@ static void append_buffer(struct tracecmd_output *handle, } } -static void -add_guest_info(struct tracecmd_output *handle, struct buffer_instance *instance) -{ - struct trace_guest *guest; - const char *name; - char *buf, *p; - int size; - int pid; - int i; - - if (is_network(instance)) { - name = instance->name; - } else { - guest = trace_get_guest(instance->cid, NULL); - if (!guest) - return; - name = guest->name; - } - - size = strlen(name) + 1; - size += sizeof(long long); /* trace_id */ - size += sizeof(int); /* cpu count */ - size += instance->cpu_count * 2 * sizeof(int); /* cpu,pid pair */ - - buf = calloc(1, size); - if (!buf) - return; - p = buf; - strcpy(p, name); - p += strlen(name) + 1; - - memcpy(p, &instance->trace_id, sizeof(long long)); - p += sizeof(long long); - - memcpy(p, &instance->cpu_count, sizeof(int)); - p += sizeof(int); - for (i = 0; i < instance->cpu_count; i++) { - pid = -1; - if (!is_network(instance)) { - if (i < guest->cpu_max) - pid = guest->cpu_pid[i]; - } - memcpy(p, &i, sizeof(int)); - p += sizeof(int); - memcpy(p, &pid, 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) { @@ -4679,7 +4627,7 @@ static void record_data(struct common_record_context *ctx) for_all_instances(instance) { if (is_guest(instance)) - add_guest_info(handle, instance); + trace_add_guest_info(handle, instance); } if (ctx->tsc2nsec.mult) { diff --git a/tracecmd/trace-vm.c b/tracecmd/trace-vm.c index 57dbef8d42e4..281843b223f6 100644 --- a/tracecmd/trace-vm.c +++ b/tracecmd/trace-vm.c @@ -386,3 +386,55 @@ int get_guest_vcpu_pid(unsigned int guest_cid, unsigned int guest_vcpu) } return -1; } + +void +trace_add_guest_info(struct tracecmd_output *handle, struct buffer_instance *instance) +{ + struct trace_guest *guest; + const char *name; + char *buf, *p; + int size; + int pid; + int i; + + if (is_network(instance)) { + name = instance->name; + } else { + guest = trace_get_guest(instance->cid, NULL); + if (!guest) + return; + name = guest->name; + } + + size = strlen(name) + 1; + size += sizeof(long long); /* trace_id */ + size += sizeof(int); /* cpu count */ + size += instance->cpu_count * 2 * sizeof(int); /* cpu,pid pair */ + + buf = calloc(1, size); + if (!buf) + return; + p = buf; + strcpy(p, name); + p += strlen(name) + 1; + + memcpy(p, &instance->trace_id, sizeof(long long)); + p += sizeof(long long); + + memcpy(p, &instance->cpu_count, sizeof(int)); + p += sizeof(int); + for (i = 0; i < instance->cpu_count; i++) { + pid = -1; + if (!is_network(instance)) { + if (i < guest->cpu_max) + pid = guest->cpu_pid[i]; + } + memcpy(p, &i, sizeof(int)); + p += sizeof(int); + memcpy(p, &pid, sizeof(int)); + p += sizeof(int); + } + + tracecmd_add_option(handle, TRACECMD_OPTION_GUEST, size, buf); + free(buf); +} From patchwork Thu Apr 28 15:06:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 12830878 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EFD95C4332F for ; Thu, 28 Apr 2022 15:06:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344429AbiD1PJ5 (ORCPT ); Thu, 28 Apr 2022 11:09:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231387AbiD1PJ4 (ORCPT ); Thu, 28 Apr 2022 11:09:56 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99D3A29803 for ; Thu, 28 Apr 2022 08:06:41 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 8450ECE2B5A for ; Thu, 28 Apr 2022 15:06:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9729CC385A9; Thu, 28 Apr 2022 15:06:37 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nk5j2-003JH9-9P; Thu, 28 Apr 2022 11:06:36 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 2/4] trace-cmd: Simplify add_guest() Date: Thu, 28 Apr 2022 11:06:33 -0400 Message-Id: <20220428150635.789051-3-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220428150635.789051-1-rostedt@goodmis.org> References: <20220428150635.789051-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" Simplify the function add_guest by using a local variable to point to the array element instead of dereferencing the array every time. Signed-off-by: Steven Rostedt (Google) --- tracecmd/trace-vm.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/tracecmd/trace-vm.c b/tracecmd/trace-vm.c index 281843b223f6..f0513950aa26 100644 --- a/tracecmd/trace-vm.c +++ b/tracecmd/trace-vm.c @@ -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) From patchwork Thu Apr 28 15:06:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 12830877 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3EBEC433EF for ; Thu, 28 Apr 2022 15:06:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348675AbiD1PJ5 (ORCPT ); Thu, 28 Apr 2022 11:09:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344429AbiD1PJz (ORCPT ); Thu, 28 Apr 2022 11:09:55 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F78F28E12 for ; Thu, 28 Apr 2022 08:06:40 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id E86E0B82DCD for ; Thu, 28 Apr 2022 15:06:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A51A9C385B0; Thu, 28 Apr 2022 15:06:37 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nk5j2-003JHC-9y; Thu, 28 Apr 2022 11:06:36 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 3/4] trace-cmd: Move find_tasks() into add_guest() Date: Thu, 28 Apr 2022 11:06:34 -0400 Message-Id: <20220428150635.789051-4-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220428150635.789051-1-rostedt@goodmis.org> References: <20220428150635.789051-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" 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. Signed-off-by: Steven Rostedt (Google) --- tracecmd/trace-record.c | 35 ----------------------------------- tracecmd/trace-vm.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index d4b5d4d6490f..a4a0d63d907d 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -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; } diff --git a/tracecmd/trace-vm.c b/tracecmd/trace-vm.c index f0513950aa26..bf2b0695d09b 100644 --- a/tracecmd/trace-vm.c +++ b/tracecmd/trace-vm.c @@ -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; } From patchwork Thu Apr 28 15:06:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 12830874 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B623C433EF for ; Thu, 28 Apr 2022 15:06:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230353AbiD1PJy (ORCPT ); Thu, 28 Apr 2022 11:09:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230392AbiD1PJx (ORCPT ); Thu, 28 Apr 2022 11:09:53 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A07C228E12 for ; Thu, 28 Apr 2022 08:06:38 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 45F8A61F14 for ; Thu, 28 Apr 2022 15:06:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E724C385AA; Thu, 28 Apr 2022 15:06:37 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nk5j2-003JHF-Ag; Thu, 28 Apr 2022 11:06:36 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 4/4] trace-cmd: Move find_pid_by_cid() into add_guest() Date: Thu, 28 Apr 2022 11:06:35 -0400 Message-Id: <20220428150635.789051-5-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220428150635.789051-1-rostedt@goodmis.org> References: <20220428150635.789051-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" To simplify the code even more, move the find_pid_by_cid() into add_guest() to map the main pid process to the cid. Signed-off-by: Steven Rostedt (Google) --- tracecmd/trace-vm.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tracecmd/trace-vm.c b/tracecmd/trace-vm.c index bf2b0695d09b..cabe4baffecb 100644 --- a/tracecmd/trace-vm.c +++ b/tracecmd/trace-vm.c @@ -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; }