Skip to contentSkip to conversation

turbostat: Running on virtual machine is not supported

PATCH Superseded Prarit Bhargava 3 participants 7 messages Jul 25, 2017 – Aug 5, 2017 Turbostat utility

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 turbostat: Running on virtual machine is not supported

When running turbostat on a virtual machine the error

turbostat: msr 0 offset 0xe2 read failed: Input/output error

is output to the user.

/dev/msr and perf do not work on a virtual machine. turbostat is dependent on that support so turbostat does not work either.

A common way of determining if the system is a virtual machine is to search /proc/cpuinfo flags entry for "hypervisor". turbostat must output a proper error message when found.

---
 tools/power/x86/turbostat/turbostat.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
tools/power/x86/turbostat/turbostat.c +34 -0
index 0dafba2c1e7d..ca1ea68bc4e8 100644
@@ -5088,6 +5088,34 @@ void cmdline(int argc, char **argv)
 	}
 }
 
+int has_hypervisor(void)
+{
+	FILE *cpuinfo;
+	char *flags, *hypervisor;
+	char *buffer;
+
+	/* On VMs /proc/cpuinfo contains a "flags" entry for hypervisor */
+	cpuinfo = fopen_or_die("/proc/cpuinfo", "ro");
+
+	buffer = malloc(4096);
+	if (!buffer)
+		err(-ENOMEM, "buffer malloc fail");
+
+	fread(buffer, 1024, 1, cpuinfo);
+
+	flags = strstr(buffer, "flags");
+	rewind(cpuinfo);
+	fseek(cpuinfo, flags - buffer, SEEK_SET);
+	fgets(buffer, 4096, cpuinfo);
+	fclose(cpuinfo);
+
+	hypervisor = strstr(buffer, "hypervisor");
+
+	free(buffer);
+
+	return !!hypervisor;
+}
+
 int main(int argc, char **argv)
 {
 	outf = stderr;
@@ -5097,6 +5125,12 @@ int main(int argc, char **argv)
 	if (!quiet)
 		print_version();
 
+	if (has_hypervisor()) {
+		fprintf(outf,
+			"turbostat is not supported on virtual machines.\n");
+		return -ENXIO;
+	}
+
 	probe_sysfs();
 
 	turbostat_init();
Signature · 1 line
1.8.5.5
#1 lore raw
Quoted 3 lines · Prarit Bhargava

A common way of determining if the system is a virtual machine is to search /proc/cpuinfo flags entry for "hypervisor". turbostat must output a proper error message when found.

Maybe you could output that message only if it fails to both use /dev/msr and perf *and* it is under a virtual machine? That would have better forward compatibility, maybe someday /dev/msr or perf will work inside a VM for what turbostat needs...

Signature · 1 line
  Henrique Holschuh
maintainer
#2 lore raw

Henrique,

I like your suggestion, thanks!

BTW. let's discuss (and patch) turbostat on linux-pm, rather than on lkml.

thanks,
-Len

Quoted 12 lines · Henrique de Moraes Holschuh
Quoted 3 lines · Prarit Bhargava

A common way of determining if the system is a virtual machine is to search /proc/cpuinfo flags entry for "hypervisor". turbostat must output a proper error message when found.

Maybe you could output that message only if it fails to both use /dev/msr and perf *and* it is under a virtual machine? That would have better forward compatibility, maybe someday /dev/msr or perf will work inside a VM for what turbostat needs...

--
Henrique Holschuh

Signature · 1 line
Len Brown, Intel Open Source Technology Center
#3 lore raw
Quoted 5 lines · Len Brown

Henrique,

I like your suggestion, thanks!

BTW. let's discuss (and patch) turbostat on linux-pm, rather than on lkml.

Sure -- Len, can you add an entry to RHMAINTAINERS for that? I use the get_maintainer.pl script to apply all cc's.

P.

#5 lore raw
In reply to Len Brown
Quoted 4 lines · Len Brown

Henrique,

I like your suggestion, thanks!

Quoted 17 lines

BTW. let's discuss (and patch) turbostat on linux-pm, rather than on lkml.

thanks,
-Len

Quoted 9 lines · Henrique de Moraes Holschuh
Quoted 3 lines · Prarit Bhargava

A common way of determining if the system is a virtual machine is to search /proc/cpuinfo flags entry for "hypervisor". turbostat must output a proper error message when found.

Maybe you could output that message only if it fails to both use /dev/msr and perf *and* it is under a virtual machine? That would have better forward compatibility, maybe someday /dev/msr or perf will work inside a VM for what turbostat needs...

To answer Len's previous question: I was wrong in my description. perf does work (mostly) under virt. So this would only be for the msr code. I'll post a v2 to linux-pm, and take Henrique's suggestion to only trigger on an msr read failure.

P.

Quoted 6 lines
Quoted 3 lines

--
Henrique Holschuh

maintainer
#4 lore raw
In reply to Prarit Bhargava
Quoted 1 line

Len, can you add an entry to RHMAINTAINERS for that?

done.

thx.
Len

From a71ec6b6c6ce93fa07f2c6df1dfa69871f08cb5b Mon Sep 17 00:00:00 2001
Message-Id: <a71ec6b6c6ce93fa07f2c6df1dfa69871f08cb5b.1501915126.git.len.brown@intel.com>
From: Len Brown <[email protected]>
Date: Sat, 5 Aug 2017 02:36:50 -0400
Subject: [PATCH 01/14] MAINTAINERS: add turbostat utility
Reply-To: Len Brown <[email protected]>
Organization: Intel Open Source Technology Center

---
 MAINTAINERS | 9 +++++++++
 1 file changed, 9 insertions(+)
MAINTAINERS +9 -0
index 7a28acd7f525..cb94ef6412b1 100644
@@ -13033,6 +13033,15 @@ S:	Maintained
 F:	drivers/tc/
 F:	include/linux/tc.h
 
+TURBOSTAT UTILITY
+M:	"Len Brown" <[email protected]>
+L:	[email protected]
+B:	https://bugzilla.kernel.org
+Q:	https://patchwork.kernel.org/project/linux-pm/list/
+T:	git git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux.git turbostat
+S:	Supported
+F:	tools/power/x86/turbostat/
+
 UBI FILE SYSTEM (UBIFS)
 M:	Richard Weinberger <[email protected]>
 M:	Artem Bityutskiy <[email protected]>
Signature · 1 line
2.14.0-rc0
maintainer
#6 lore raw
In reply to Prarit Bhargava
Quoted 1 line

perf does work (mostly) under virt.

Let's hold off on this patch for a bit till the port to perf is ready, since this logic is going to change slightly.

I think something like this:

1. make the debug info (the stuff hidden with --quiet) optional don't bail out if it fails. (but continue to bail out of anything that is needed to provide a column fails)

2. attempt the faster of /dev/msr or perf first, and try the 2nd only when the 1st doesn't work.

(currently /dev/msr is faster, but I'm not done w/ the perf work)

3. When neither /dev/msr or perf work, then check for virt to see if it may be the cause.

Print the error message about permissions for perf and /dev/msr and
virt, as appropriate.

thanks,
Len Brown, Intel Open Source Technology Center