Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1695710 > unrolled thread

[PATCH] turbostat: Running on virtual machine is not supported

Started byPrarit Bhargava <prarit@redhat.com>
First post2017-07-25 15:00 +0200
Last post2017-07-28 14:00 +0200
Articles 5 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] turbostat: Running on virtual machine is not supported Prarit Bhargava <prarit@redhat.com> - 2017-07-25 15:00 +0200
    Re: [PATCH] turbostat: Running on virtual machine is not supported Henrique de Moraes Holschuh <hmh@hmh.eng.br> - 2017-07-25 18:00 +0200
      Re: [PATCH] turbostat: Running on virtual machine is not supported Len Brown <lenb@kernel.org> - 2017-07-27 05:10 +0200
        Re: [PATCH] turbostat: Running on virtual machine is not supported Prarit Bhargava <prarit@redhat.com> - 2017-07-27 13:50 +0200
        Re: [PATCH] turbostat: Running on virtual machine is not supported Prarit Bhargava <prarit@redhat.com> - 2017-07-28 14:00 +0200

#1695710 — [PATCH] turbostat: Running on virtual machine is not supported

FromPrarit Bhargava <prarit@redhat.com>
Date2017-07-25 15:00 +0200
Subject[PATCH] turbostat: Running on virtual machine is not supported
Message-ID<u77Bo-2Vd-25@gated-at.bofh.it>
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.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
Cc: Len Brown <len.brown@intel.com>
Cc: Len Brown <lenb@kernel.org>
---
 tools/power/x86/turbostat/turbostat.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
index 0dafba2c1e7d..ca1ea68bc4e8 100644
--- a/tools/power/x86/turbostat/turbostat.c
+++ b/tools/power/x86/turbostat/turbostat.c
@@ -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();
-- 
1.8.5.5

[toc] | [next] | [standalone]


#1695904

FromHenrique de Moraes Holschuh <hmh@hmh.eng.br>
Date2017-07-25 18:00 +0200
Message-ID<u7apB-4Hw-41@gated-at.bofh.it>
In reply to#1695710
On Tue, 25 Jul 2017, Prarit Bhargava wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#1697685

FromLen Brown <lenb@kernel.org>
Date2017-07-27 05:10 +0200
Message-ID<u7Hlv-GZ-5@gated-at.bofh.it>
In reply to#1695904
Henrique,

I like your suggestion, thanks!

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

thanks,
-Len


On Tue, Jul 25, 2017 at 11:59 AM, Henrique de Moraes Holschuh
<hmh@hmh.eng.br> wrote:
> On Tue, 25 Jul 2017, Prarit Bhargava wrote:
>> 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



-- 
Len Brown, Intel Open Source Technology Center

[toc] | [prev] | [next] | [standalone]


#1697916

FromPrarit Bhargava <prarit@redhat.com>
Date2017-07-27 13:50 +0200
Message-ID<u7PsJ-5A3-1@gated-at.bofh.it>
In reply to#1697685

On 07/26/2017 11:09 PM, Len Brown wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#1698718

FromPrarit Bhargava <prarit@redhat.com>
Date2017-07-28 14:00 +0200
Message-ID<u8c5Y-3fm-15@gated-at.bofh.it>
In reply to#1697685

On 07/26/2017 11:09 PM, Len Brown wrote:
> Henrique,
> 
> I like your suggestion, thanks!
> 
> BTW. let's discuss (and patch) turbostat on linux-pm, rather than on lkml.
> 
> thanks,
> -Len
> 
> 
> On Tue, Jul 25, 2017 at 11:59 AM, Henrique de Moraes Holschuh
> <hmh@hmh.eng.br> wrote:
>> On Tue, 25 Jul 2017, Prarit Bhargava wrote:
>>> 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.

>>
>> --
>>   Henrique Holschuh
> 
> 
> 

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web