From patchwork Fri Jul 28 11:58:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prarit Bhargava X-Patchwork-Id: 9868581 X-Patchwork-Delegate: lenb@kernel.org Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C91AC60382 for ; Fri, 28 Jul 2017 11:58:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AD122288C9 for ; Fri, 28 Jul 2017 11:58:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A20A5288D0; Fri, 28 Jul 2017 11:58:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1BD48288C9 for ; Fri, 28 Jul 2017 11:58:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751786AbdG1L61 (ORCPT ); Fri, 28 Jul 2017 07:58:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55086 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751782AbdG1L60 (ORCPT ); Fri, 28 Jul 2017 07:58:26 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 325EA5B4B7; Fri, 28 Jul 2017 11:58:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 325EA5B4B7 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=prarit@redhat.com Received: from praritdesktop.bos.redhat.com (prarit-guest.khw.lab.eng.bos.redhat.com [10.16.186.145]) by smtp.corp.redhat.com (Postfix) with ESMTP id 95FDA600C2; Fri, 28 Jul 2017 11:58:25 +0000 (UTC) From: Prarit Bhargava To: linux-pm@vger.kernel.org Cc: Prarit Bhargava , Len Brown , Len Brown , Henrique de Moraes Holschuh Subject: [PATCH v2] turbostat: Running on virtual machine is not supported Date: Fri, 28 Jul 2017 07:58:18 -0400 Message-Id: <1501243098-31262-1-git-send-email-prarit@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Fri, 28 Jul 2017 11:58:26 +0000 (UTC) Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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. When an error is returned from an msr read, turbostat must check to see if the system is a virtual machine and return an error to the user. [v2]: Only check for VM if msr read fails Signed-off-by: Prarit Bhargava Cc: Len Brown Cc: Len Brown Cc: Henrique de Moraes Holschuh --- tools/power/x86/turbostat/turbostat.c | 57 +++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c index 0dafba2c1e7d..97c51bdce2ef 100644 --- a/tools/power/x86/turbostat/turbostat.c +++ b/tools/power/x86/turbostat/turbostat.c @@ -333,14 +333,58 @@ int get_msr_fd(int cpu) return fd; } +/* + * Open a file, and exit on failure + */ +FILE *fopen_or_die(const char *path, const char *mode) +{ + FILE *filep = fopen(path, mode); + + if (!filep) + err(1, "%s: open failed", path); + return filep; +} + +void err_on_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); + + if (hypervisor) + err(-1, + "turbostat is not supported on this virtual machine."); +} + int get_msr(int cpu, off_t offset, unsigned long long *msr) { ssize_t retval; retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); - if (retval != sizeof *msr) + if (retval != sizeof *msr) { + err_on_hypervisor(); err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); + } return 0; } @@ -1453,17 +1497,6 @@ static unsigned long long rdtsc(void) } /* - * Open a file, and exit on failure - */ -FILE *fopen_or_die(const char *path, const char *mode) -{ - FILE *filep = fopen(path, mode); - - if (!filep) - err(1, "%s: open failed", path); - return filep; -} -/* * snapshot_sysfs_counter() * * return snapshot of given counter