linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Prarit Bhargava <prarit@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: Prarit Bhargava <prarit@redhat.com>,
	Len Brown <len.brown@intel.com>, Len Brown <lenb@kernel.org>
Subject: [PATCH] turbostat: Running on virtual machine is not supported
Date: Tue, 25 Jul 2017 08:55:24 -0400	[thread overview]
Message-ID: <1500987324-507-1-git-send-email-prarit@redhat.com> (raw)

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

             reply	other threads:[~2017-07-25 12:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-25 12:55 Prarit Bhargava [this message]
2017-07-25 15:59 ` [PATCH] turbostat: Running on virtual machine is not supported Henrique de Moraes Holschuh
2017-07-27  3:09   ` Len Brown
2017-07-27 11:44     ` Prarit Bhargava
2017-08-05  6:39       ` Len Brown
2017-07-28 11:54     ` Prarit Bhargava

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1500987324-507-1-git-send-email-prarit@redhat.com \
    --to=prarit@redhat.com \
    --cc=len.brown@intel.com \
    --cc=lenb@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).