tools/power x86_energy_perf_policy: Input/output error in a VM

I've encountered an issue with x86_energy_perf_policy. If I run it on a
machine that I'm told is a qemu-kvm virtual machine running inside a
privileged container, I get the following error:

x86_energy_perf_policy: /dev/cpu/0/msr offset 0x1ad read failed: Input/output error

I get the same error in a Digital Ocean droplet, so that might be a
similar environment.

I created the following patch which is intended to give a more
user-friendly message. It's based on a patch for turbostat from Prarit
Bhargava that was posted some time ago. The patch is "[v2] turbostat:
Running on virtual machine is not supported" [1].

Given my limited knowledge of the topic, I can't say with confidence
that this is the right solution, though (that's why this is not an
official patch submission). Also, I'm not sure what the convention with
exit codes is in this tool. Also, instead of the error message, perhaps
the tool should just not print anything in this case, which is how it
behaves in a "regular" VM?

[1] https://patchwork.kernel.org/patch/9868587/

Signed-off-by: Ondřej Lysoněk <olysonek@redhat.com>
Signed-off-by: Len Brown <len.brown@intel.com>
This commit is contained in:
Ondřej Lysoněk 2020-03-27 08:27:12 +01:00 committed by Len Brown
parent c315a09b1b
commit 0936cdfbb5

View File

@ -622,6 +622,57 @@ void cmdline(int argc, char **argv)
}
}
/*
* Open a file, and exit on failure
*/
FILE *fopen_or_die(const char *path, const char *mode)
{
FILE *filep = fopen(path, "r");
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) {
fclose(cpuinfo);
err(-ENOMEM, "buffer malloc fail");
}
if (!fread(buffer, 1024, 1, cpuinfo)) {
fclose(cpuinfo);
free(buffer);
err(1, "Reading /proc/cpuinfo failed");
}
flags = strstr(buffer, "flags");
rewind(cpuinfo);
fseek(cpuinfo, flags - buffer, SEEK_SET);
if (!fgets(buffer, 4096, cpuinfo)) {
fclose(cpuinfo);
free(buffer);
err(1, "Reading /proc/cpuinfo failed");
}
fclose(cpuinfo);
hypervisor = strstr(buffer, "hypervisor");
free(buffer);
if (hypervisor)
err(-1,
"not supported on this virtual machine");
}
int get_msr(int cpu, int offset, unsigned long long *msr)
{
@ -635,8 +686,10 @@ int get_msr(int cpu, int offset, unsigned long long *msr)
err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
retval = pread(fd, msr, sizeof(*msr), offset);
if (retval != sizeof(*msr))
if (retval != sizeof(*msr)) {
err_on_hypervisor();
err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
}
if (debug > 1)
fprintf(stderr, "get_msr(cpu%d, 0x%X, 0x%llX)\n", cpu, offset, *msr);
@ -1086,18 +1139,6 @@ int update_cpu_msrs(int cpu)
return 0;
}
/*
* Open a file, and exit on failure
*/
FILE *fopen_or_die(const char *path, const char *mode)
{
FILE *filep = fopen(path, "r");
if (!filep)
err(1, "%s: open failed", path);
return filep;
}
unsigned int get_pkg_num(int cpu)
{
FILE *fp;