Skip to contentSkip to conversation

trace-cmd: Fix up kvm time synchronization

PATCH New Steven Rostedt 1 participant 5 messages May 22, 2022 Kbuild

lore
Share & tools
lore
#0 lore raw

cover trace-cmd: Fix up kvm time synchronization

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

Fix the kvm time synchronization so that it works with the agent proxy. That includes using "stat()" instead of just reading the content of the file to see if they exist. Have the kvm time sync functions test for a valid VM instead of just seeing if there are no failures, as no VMs or VCPUs would pass when it should not. Set the proper role for the agent proxy.

Finally, mount the debugfs file system if it is not already mounted, otherwise it will say that KVM time synchronization is not supported when it actually is.

Steven Rostedt (Google) (4):
trace-cmd kvm timesync: Use stat() in kvm_scaling_check_vm_cpu() trace-cmd kvm timesync: Check for one valid VM trace-cmd record: Set the proper role when connected to a proxy
trace-cmd: Mount debugfs if needed for KVM data

 Makefile                           |  2 +-
 lib/trace-cmd/trace-timesync-kvm.c | 99 ++++++++++++++++++++++--------
 tracecmd/trace-record.c            |  6 +-
 3 files changed, 80 insertions(+), 27 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/4 trace-cmd kvm timesync: Use stat() in kvm_scaling_check_vm_cpu()

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

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().

---
 lib/trace-cmd/trace-timesync-kvm.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/trace-cmd/trace-timesync-kvm.c +3 -3
index 12a22d4c4d6a..a645fa2cc70b 100644
@@ -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;
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/4 trace-cmd kvm timesync: Check for one valid VM

In reply to Steven Rostedt

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

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.

---
 lib/trace-cmd/trace-timesync-kvm.c | 46 +++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 10 deletions(-)
lib/trace-cmd/trace-timesync-kvm.c +36 -10
index a645fa2cc70b..671eafaf62b8 100644
@@ -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;
 
Signature · 1 line
2.35.1
#3 lore raw

PATCH 3/4 trace-cmd record: Set the proper role when connected to a proxy

In reply to Steven Rostedt

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

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.

---
 tracecmd/trace-record.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tracecmd/trace-record.c +5 -1
index b43f3272dfc6..ce20402af7c2 100644
@@ -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",
Signature · 1 line
2.35.1
#4 lore raw

PATCH 4/4 trace-cmd: Mount debugfs if needed for KVM data

In reply to Steven Rostedt

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

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.

---
 Makefile                           |  2 +-
 lib/trace-cmd/trace-timesync-kvm.c | 47 ++++++++++++++++++++++--------
 2 files changed, 36 insertions(+), 13 deletions(-)
Makefile +1 -1
index abc4ac723db4..3452649229df 100644
@@ -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
 
lib/trace-cmd/trace-timesync-kvm.c +35 -12

101 lines not on this page · raw message

Signature · 1 line
2.35.1