Skip to contentSkip to conversation

perf tools: get version from uname(2), not /proc

PATCH In review Matt Mullins 4 participants 6 messages Oct 6, 2015 – Oct 8, 2015 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf tools: get version from uname(2), not /proc

Tools in kmod (e.g. modprobe) compose the module path from the release from uname(2). Because we use the UNAME26 personality, we need perf to find modules located at the same path as the system tools.

---
 tools/perf/util/machine.c | 28 ++++++----------------------
 1 file changed, 6 insertions(+), 22 deletions(-)
tools/perf/util/machine.c +6 -22
index 5ef90be2a249..51199bc271e9 100644
@@ -11,6 +11,7 @@
 #include "vdso.h"
 #include <stdbool.h>
 #include <symbol/kallsyms.h>
+#include <sys/utsname.h>
 #include "unwind.h"
 #include "linux/hash.h"
 
@@ -903,31 +904,14 @@ static void map_groups__fixup_end(struct map_groups *mg)
 		__map_groups__fixup_end(mg, i);
 }
 
-static char *get_kernel_version(const char *root_dir)
+static char *get_kernel_version(void)
 {
-	char version[PATH_MAX];
-	FILE *file;
-	char *name, *tmp;
-	const char *prefix = "Linux version ";
+	struct utsname utsname;
 
-	sprintf(version, "%s/proc/version", root_dir);
-	file = fopen(version, "r");
-	if (!file)
+	if (uname(&utsname))
 		return NULL;
 
-	version[0] = '\0';
-	tmp = fgets(version, sizeof(version), file);
-	fclose(file);
-
-	name = strstr(version, prefix);
-	if (!name)
-		return NULL;
-	name += strlen(prefix);
-	tmp = strchr(name, ' ');
-	if (tmp)
-		*tmp = '\0';
-
-	return strdup(name);
+	return strdup(utsname.release);
 }
 
 static bool is_kmod_dso(struct dso *dso)
@@ -1027,7 +1011,7 @@ static int machine__set_modules_path(struct machine *machine)
 	char *version;
 	char modules_path[PATH_MAX];
 
-	version = get_kernel_version(machine->root_dir);
+	version = get_kernel_version();
 	if (!version)
 		return -1;
 
Signature · 1 line
2.1.0
#1 lore raw
Quoted 3 lines · Matt Mullins

Tools in kmod (e.g. modprobe) compose the module path from the release from uname(2). Because we use the UNAME26 personality, we need perf to find modules located at the same path as the system tools.

I guess it's easy to google this up, but could you please state in the changelog what's the difference between the current version and the UNAME26 one?

Also state (and check) this change wouldn't affect other parts of the code that use this version (if there's any).

thanks,
jirka

Quoted 67 lines
---
 tools/perf/util/machine.c | 28 ++++++----------------------
 1 file changed, 6 insertions(+), 22 deletions(-)
tools/perf/util/machine.c +6 -22
index 5ef90be2a249..51199bc271e9 100644
@@ -11,6 +11,7 @@
 #include "vdso.h"
 #include <stdbool.h>
 #include <symbol/kallsyms.h>
+#include <sys/utsname.h>
 #include "unwind.h"
 #include "linux/hash.h"
 
@@ -903,31 +904,14 @@ static void map_groups__fixup_end(struct map_groups *mg)
 		__map_groups__fixup_end(mg, i);
 }
 
-static char *get_kernel_version(const char *root_dir)
+static char *get_kernel_version(void)
 {
-	char version[PATH_MAX];
-	FILE *file;
-	char *name, *tmp;
-	const char *prefix = "Linux version ";
+	struct utsname utsname;
 
-	sprintf(version, "%s/proc/version", root_dir);
-	file = fopen(version, "r");
-	if (!file)
+	if (uname(&utsname))
 		return NULL;
 
-	version[0] = '\0';
-	tmp = fgets(version, sizeof(version), file);
-	fclose(file);
-
-	name = strstr(version, prefix);
-	if (!name)
-		return NULL;
-	name += strlen(prefix);
-	tmp = strchr(name, ' ');
-	if (tmp)
-		*tmp = '\0';
-
-	return strdup(name);
+	return strdup(utsname.release);
 }
 
 static bool is_kmod_dso(struct dso *dso)
@@ -1027,7 +1011,7 @@ static int machine__set_modules_path(struct machine *machine)
 	char *version;
 	char modules_path[PATH_MAX];
 
-	version = get_kernel_version(machine->root_dir);
+	version = get_kernel_version();
 	if (!version)
 		return -1;
 
Signature · 1 line
2.1.0
reviewer
#2 lore raw
Quoted 11 lines · Jiri Olsa
Quoted 3 lines · Matt Mullins

Tools in kmod (e.g. modprobe) compose the module path from the release from uname(2). Because we use the UNAME26 personality, we need perf to find modules located at the same path as the system tools.

I guess it's easy to google this up, but could you please state in the changelog what's the difference between the current version and the UNAME26 one?

Also state (and check) this change wouldn't affect other parts of the code that use this version (if there's any).

Isn't the machine root dir for guests? uname() won't work for them.

#3 lore raw
Quoted 6 lines · Adrian Hunter
Quoted 5 lines · Jiri Olsa
Quoted 3 lines · Matt Mullins

Tools in kmod (e.g. modprobe) compose the module path from the release from uname(2). Because we use the UNAME26 personality, we need perf to find modules located at the same path as the system tools.

Quoted 6 lines
Quoted 6 lines

I guess it's easy to google this up, but could you please state in the changelog what's the difference between the current version and the UNAME26 one?

Also state (and check) this change wouldn't affect other parts of the code that use this version (if there's any).

This is the only caller of get_kernel_version.

Quoted 1 line

Isn't the machine root dir for guests? uname() won't work for them.

Aye, this would break --guestmount. Would it make sense to use uname() iff root_dir is the empty string, or is that too much special-casing for good taste?

reviewer
#4 lore raw
Quoted 14 lines · Matt Mullins
Quoted 6 lines · Adrian Hunter
Quoted 5 lines · Jiri Olsa
Quoted 3 lines · Matt Mullins

Tools in kmod (e.g. modprobe) compose the module path from the release from uname(2). Because we use the UNAME26 personality, we need perf to find modules located at the same path as the system tools.

Quoted 6 lines
Quoted 6 lines

I guess it's easy to google this up, but could you please state in the changelog what's the difference between the current version and the UNAME26 one?

Also state (and check) this change wouldn't affect other parts of the code that use this version (if there's any).

Quoted 7 lines

This is the only caller of get_kernel_version.

Quoted 1 line

Isn't the machine root dir for guests? uname() won't work for them.

Aye, this would break --guestmount. Would it make sense to use uname() iff root_dir is the empty string, or is that too much special-casing for good taste?

There is machine__is_host(), but what about /proc/sys/kernel/osrelease or does that have the same problem?

#5 lore raw
In reply to Matt Mullins
Quoted 3 lines · Matt Mullins

Tools in kmod (e.g. modprobe) compose the module path from the release from uname(2). Because we use the UNAME26 personality, we need perf to find modules located at the same path as the system tools.

May be easier to just reset the personality.

-Andi