From patchwork Sun May 22 00:39: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: 12858083 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 1542EC433EF for ; Sun, 22 May 2022 00:39:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237800AbiEVAjk (ORCPT ); Sat, 21 May 2022 20:39:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232548AbiEVAjj (ORCPT ); Sat, 21 May 2022 20:39:39 -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 CEB6646652 for ; Sat, 21 May 2022 17:39: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 699E660EF6 for ; Sun, 22 May 2022 00:39:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CC5DBC34117; Sun, 22 May 2022 00:39:37 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nsZdA-000p8m-Pz; Sat, 21 May 2022 20:39:36 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 1/4] trace-cmd kvm timesync: Use stat() in kvm_scaling_check_vm_cpu() Date: Sat, 21 May 2022 20:39:32 -0400 Message-Id: <20220522003935.196466-2-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220522003935.196466-1-rostedt@goodmis.org> References: <20220522003935.196466-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)" kvm_scaling_check_vm_cpu() uses read_ll_from_file() to determine if the kvm scaling and fraction files exist. The read_ll_from_file() does not return if the contents of the files are legit or not. No need to read them in this file. Just use stat(). Signed-off-by: Steven Rostedt (Google) --- lib/trace-cmd/trace-timesync-kvm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/trace-cmd/trace-timesync-kvm.c b/lib/trace-cmd/trace-timesync-kvm.c index 12a22d4c4d6a..a645fa2cc70b 100644 --- a/lib/trace-cmd/trace-timesync-kvm.c +++ b/lib/trace-cmd/trace-timesync-kvm.c @@ -75,22 +75,22 @@ static int read_ll_from_file(char *file, long long *res) static bool kvm_scaling_check_vm_cpu(char *vname, char *cpu) { - long long scaling, frac; bool has_scaling = false; bool has_frac = false; + struct stat st; char *path; int ret; if (asprintf(&path, "%s/%s/%s", vname, cpu, KVM_DEBUG_SCALING_FILE) < 0) return false; - ret = read_ll_from_file(path, &scaling); + ret = stat(path, &st); free(path); if (!ret) has_scaling = true; if (asprintf(&path, "%s/%s/%s", vname, cpu, KVM_DEBUG_FRACTION_FILE) < 0) return false; - ret = read_ll_from_file(path, &frac); + ret = stat(path, &st); free(path); if (!ret) has_frac = true; From patchwork Sun May 22 00:39: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: 12858085 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 7D874C433EF for ; Sun, 22 May 2022 00:39:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242581AbiEVAjn (ORCPT ); Sat, 21 May 2022 20:39:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239316AbiEVAjm (ORCPT ); Sat, 21 May 2022 20:39:42 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE21D4664E for ; Sat, 21 May 2022 17:39: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 sin.source.kernel.org (Postfix) with ESMTPS id 3F261CE09E9 for ; Sun, 22 May 2022 00:39:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B693DC385A5; Sun, 22 May 2022 00:39:37 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nsZdA-000p8p-Qj; Sat, 21 May 2022 20:39:36 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 2/4] trace-cmd kvm timesync: Check for one valid VM Date: Sat, 21 May 2022 20:39:33 -0400 Message-Id: <20220522003935.196466-3-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220522003935.196466-1-rostedt@goodmis.org> References: <20220522003935.196466-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)" Currently if there are no valid VMs the kvm supported check can return true. Check for at least one valid VM, and that echa VM has one valid VCPU. Also comment the code as it is very confusing to why the scaling checks return true or false. Signed-off-by: Steven Rostedt (Google) --- lib/trace-cmd/trace-timesync-kvm.c | 46 +++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/trace-cmd/trace-timesync-kvm.c b/lib/trace-cmd/trace-timesync-kvm.c index a645fa2cc70b..671eafaf62b8 100644 --- a/lib/trace-cmd/trace-timesync-kvm.c +++ b/lib/trace-cmd/trace-timesync-kvm.c @@ -73,6 +73,11 @@ static int read_ll_from_file(char *file, long long *res) return 0; } +/* + * Returns true if both scaling and fraction exist or both do + * not exist. false if one exists without the other or if there + * is a memory error. + */ static bool kvm_scaling_check_vm_cpu(char *vname, char *cpu) { bool has_scaling = false; @@ -101,46 +106,66 @@ static bool kvm_scaling_check_vm_cpu(char *vname, char *cpu) return true; } +/* + * Returns true if a VCPU exists with a tsc-offset file and that + * the scaling files for ratio and fraction both exist or both + * do not exist. False if there is no VM with a tsc-offset or + * there is only one of the two scaling files, or there's a + * memory issue. + */ static bool kvm_scaling_check_vm(char *name) { struct dirent *entry; char *vdir; DIR *dir; + bool valid = false; if (asprintf(&vdir, "%s/%s", KVM_DEBUG_FS, name) < 0) - return true; + return false; dir = opendir(vdir); if (!dir) { free(vdir); - return true; + return false; } while ((entry = readdir(dir))) { - if (entry->d_type == DT_DIR && !strncmp(entry->d_name, "vcpu", 4) && - !kvm_scaling_check_vm_cpu(vdir, entry->d_name)) - break; + if (entry->d_type == DT_DIR && !strncmp(entry->d_name, "vcpu", 4)) { + if (!kvm_scaling_check_vm_cpu(vdir, entry->d_name)) + break; + valid = true; + } } closedir(dir); free(vdir); - return entry == NULL; + return valid && entry == NULL; } + +/* + * Returns true if all VMs have a tsc-offset file and that + * the scaling files for ratio and fraction both exist or both + * do not exist. False if a VM with a tsc-offset or there is only + * one of the two scaling files, or no VM exists or there's a memory issue. + */ static bool kvm_scaling_check(void) { struct dirent *entry; DIR *dir; + bool valid = false; dir = opendir(KVM_DEBUG_FS); if (!dir) return true; while ((entry = readdir(dir))) { - if (entry->d_type == DT_DIR && isdigit(entry->d_name[0]) && - !kvm_scaling_check_vm(entry->d_name)) - break; + if (entry->d_type == DT_DIR && isdigit(entry->d_name[0])) { + if (!kvm_scaling_check_vm(entry->d_name)) + break; + valid = true; + } } closedir(dir); - return entry == NULL; + return valid && entry == NULL; } static bool kvm_support_check(bool guest) @@ -148,6 +173,7 @@ static bool kvm_support_check(bool guest) struct stat st; int ret; + /* The kvm files are only in the host so we can ignore guests */ if (guest) return true; From patchwork Sun May 22 00:39: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: 12858086 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 CF556C4332F for ; Sun, 22 May 2022 00:39:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239316AbiEVAjn (ORCPT ); Sat, 21 May 2022 20:39:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240763AbiEVAjm (ORCPT ); Sat, 21 May 2022 20:39:42 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9D994665F for ; Sat, 21 May 2022 17:39: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 sin.source.kernel.org (Postfix) with ESMTPS id 4D4E5CE0ADC for ; Sun, 22 May 2022 00:39:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9922C385B8; Sun, 22 May 2022 00:39:37 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nsZdA-000p8s-RN; Sat, 21 May 2022 20:39:36 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 3/4] trace-cmd record: Set the proper role when connected to a proxy Date: Sat, 21 May 2022 20:39:34 -0400 Message-Id: <20220522003935.196466-4-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220522003935.196466-1-rostedt@goodmis.org> References: <20220522003935.196466-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)" When a guest uses trace-cmd record to connect to the agent proxy on the host, it needs to set its role to "GUEST" when initializing the time synchronization protocols. Signed-off-by: Steven Rostedt (Google) --- tracecmd/trace-record.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index b43f3272dfc6..ce20402af7c2 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -3905,7 +3905,11 @@ static void connect_to_agent(struct common_record_context *ctx, die("Failed to connect to host %s:%u", instance->name, instance->port); } else { - role = TRACECMD_TIME_SYNC_ROLE_HOST; + /* If connecting to a proxy, then this is the guest */ + if (is_proxy(ctx->instance)) + role = TRACECMD_TIME_SYNC_ROLE_GUEST; + else + role = TRACECMD_TIME_SYNC_ROLE_HOST; sd = trace_vsock_open(instance->cid, instance->port); if (sd < 0) die("Failed to connect to vsocket @%u:%u", From patchwork Sun May 22 00:39: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: 12858087 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 22A9FC433FE for ; Sun, 22 May 2022 00:39:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240763AbiEVAjn (ORCPT ); Sat, 21 May 2022 20:39:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238629AbiEVAjm (ORCPT ); Sat, 21 May 2022 20:39:42 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CDB146655 for ; Sat, 21 May 2022 17:39: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 29332B8091D for ; Sun, 22 May 2022 00:39:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BF6D6C34116; Sun, 22 May 2022 00:39:37 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nsZdA-000p8v-S2; Sat, 21 May 2022 20:39:36 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 4/4] trace-cmd: Mount debugfs if needed for KVM data Date: Sat, 21 May 2022 20:39:35 -0400 Message-Id: <20220522003935.196466-5-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220522003935.196466-1-rostedt@goodmis.org> References: <20220522003935.196466-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)" The KVM offset and multiplier are in the debugfs file system. If it is not currently mounted, then try to mount it with the new tracefs API tracefs_debug_dir() which will return the debugfs file system path, and even mount it if possible (or NULL if it could not). This also removes the hard coded path for /sys/kernel/debug/kvm and removes the stat() of the directory as tracefs_debug_dir() will only return mount locations which are directories. Set libtracefs minimum version to 1.4 as that is what will have the tarcefs_debug_dir() in it. Signed-off-by: Steven Rostedt (Google) --- Makefile | 2 +- lib/trace-cmd/trace-timesync-kvm.c | 47 ++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index abc4ac723db4..3452649229df 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ export LIBTRACECMD_VERSION VERSION_FILE = ltc_version.h LIBTRACEEVENT_MIN_VERSION = 1.5 -LIBTRACEFS_MIN_VERSION = 1.3 +LIBTRACEFS_MIN_VERSION = 1.4 MAKEFLAGS += --no-print-directory diff --git a/lib/trace-cmd/trace-timesync-kvm.c b/lib/trace-cmd/trace-timesync-kvm.c index 671eafaf62b8..1db63d94f545 100644 --- a/lib/trace-cmd/trace-timesync-kvm.c +++ b/lib/trace-cmd/trace-timesync-kvm.c @@ -16,7 +16,6 @@ #include "tracefs.h" #include "trace-tsync-local.h" -#define KVM_DEBUG_FS "/sys/kernel/debug/kvm" #define KVM_DEBUG_OFFSET_FILE "tsc-offset" #define KVM_DEBUG_SCALING_FILE "tsc-scaling-ratio" #define KVM_DEBUG_FRACTION_FILE "tsc-scaling-ratio-frac-bits" @@ -106,6 +105,24 @@ static bool kvm_scaling_check_vm_cpu(char *vname, char *cpu) return true; } +static const char *kvm_debug_dir(void) +{ + const char *debugfs; + static char *kvm_dir; + + if (kvm_dir) + return kvm_dir; + + debugfs = tracefs_debug_dir(); + if (!debugfs) + return NULL; + + if (asprintf(&kvm_dir, "%s/kvm", debugfs) < 0) + return NULL; + + return kvm_dir; +} + /* * Returns true if a VCPU exists with a tsc-offset file and that * the scaling files for ratio and fraction both exist or both @@ -116,11 +133,16 @@ static bool kvm_scaling_check_vm_cpu(char *vname, char *cpu) static bool kvm_scaling_check_vm(char *name) { struct dirent *entry; + const char *kvm; char *vdir; DIR *dir; bool valid = false; - if (asprintf(&vdir, "%s/%s", KVM_DEBUG_FS, name) < 0) + kvm = kvm_debug_dir(); + if (!kvm) + return false; + + if (asprintf(&vdir, "%s/%s", kvm, name) < 0) return false; dir = opendir(vdir); @@ -150,10 +172,15 @@ static bool kvm_scaling_check_vm(char *name) static bool kvm_scaling_check(void) { struct dirent *entry; + const char *kvm; DIR *dir; bool valid = false; - dir = opendir(KVM_DEBUG_FS); + kvm = kvm_debug_dir(); + if (!kvm) + return false; + + dir = opendir(kvm); if (!dir) return true; @@ -170,18 +197,14 @@ static bool kvm_scaling_check(void) static bool kvm_support_check(bool guest) { - struct stat st; - int ret; + const char *kvm; /* The kvm files are only in the host so we can ignore guests */ if (guest) return true; - ret = stat(KVM_DEBUG_FS, &st); - if (ret < 0) - return false; - - if (!S_ISDIR(st.st_mode)) + kvm = kvm_debug_dir(); + if (!kvm) return false; return kvm_scaling_check(); @@ -242,7 +265,7 @@ static int kvm_open_debug_files(struct kvm_clock_sync *kvm, int pid) DIR *dir; int i; - dir = opendir(KVM_DEBUG_FS); + dir = opendir(kvm_debug_dir()); if (!dir) goto error; if (asprintf(&pid_str, "%d-", pid) <= 0) @@ -251,7 +274,7 @@ static int kvm_open_debug_files(struct kvm_clock_sync *kvm, int pid) if (!(entry->d_type == DT_DIR && !strncmp(entry->d_name, pid_str, strlen(pid_str)))) continue; - asprintf(&vm_dir_str, "%s/%s", KVM_DEBUG_FS, entry->d_name); + asprintf(&vm_dir_str, "%s/%s", kvm_debug_dir(), entry->d_name); break; } closedir(dir);