Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751943AbdGYMz0 (ORCPT ); Tue, 25 Jul 2017 08:55:26 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55516 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750839AbdGYMzZ (ORCPT ); Tue, 25 Jul 2017 08:55:25 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 9DDF5C003401 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=prarit@redhat.com From: Prarit Bhargava To: linux-kernel@vger.kernel.org Cc: Prarit Bhargava , Len Brown , Len Brown Subject: [PATCH] turbostat: Running on virtual machine is not supported Date: Tue, 25 Jul 2017 08:55:24 -0400 Message-Id: <1500987324-507-1-git-send-email-prarit@redhat.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 25 Jul 2017 12:55:25 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1866 Lines: 74 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 Cc: Len Brown Cc: Len Brown --- 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