From 797c708f2d8f1656c863ff57ed1cd18c3cc788d6 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Tue, 29 Dec 2020 00:09:01 +0100 Subject: [PATCH] [v0.94][x86] Consider missing frequency file in x86_64 as a bug if no hypervisor is present. Took this idea from issue #37. Add if hypervisor is present to debug mode to prevent more confusions in the future --- src/arm/midr.c | 8 ++++---- src/common/udev.c | 22 ++++++++++++++++------ src/common/udev.h | 6 ++++-- src/x86/cpuid.c | 34 ++++++++++++++++++++++++++++++---- 4 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/arm/midr.c b/src/arm/midr.c index 64d175f..2a45e77 100644 --- a/src/arm/midr.c +++ b/src/arm/midr.c @@ -52,7 +52,7 @@ struct frequency* get_frequency_info(uint32_t core) { struct frequency* freq = malloc(sizeof(struct frequency)); freq->base = UNKNOWN_FREQ; - freq->max = get_max_freq_from_file(core); + freq->max = get_max_freq_from_file(core, false); return freq; } @@ -186,7 +186,7 @@ struct cpuInfo* get_cpu_info() { midr_array[i] = midr_array[0]; } - freq_array[i] = get_max_freq_from_file(i); + freq_array[i] = get_max_freq_from_file(i, false); if(freq_array[i] == UNKNOWN_FREQ) { printWarn("Unable to fetch max frequency for core %d. This is probably because the core is offline", i); freq_array[i] = freq_array[0]; @@ -306,7 +306,7 @@ void print_debug(struct cpuInfo* cpu) { for(int i=0; i < ncores; i++) { printf("[Core %d] ", i); - long freq = get_max_freq_from_file(i); + long freq = get_max_freq_from_file(i, false); uint32_t midr = get_midr_from_cpuinfo(i, &success); if(!success) { printWarn("Unable to fetch MIDR for core %d. This is probably because the core is offline", i); @@ -317,7 +317,7 @@ void print_debug(struct cpuInfo* cpu) { } if(freq == UNKNOWN_FREQ) { printWarn("Unable to fetch max frequency for core %d. This is probably because the core is offline", i); - printf("%ld MHz\n", get_max_freq_from_file(0)); + printf("%ld MHz\n", get_max_freq_from_file(0, false)); } else { printf("%ld MHz\n", freq); diff --git a/src/common/udev.c b/src/common/udev.c index 6fb21ff..037f6e4 100644 --- a/src/common/udev.c +++ b/src/common/udev.c @@ -28,11 +28,21 @@ char* read_file(char* path, int* len) { return buf; } -long get_freq_from_file(char* path) { +long get_freq_from_file(char* path, bool hv_present) { int filelen; char* buf; if((buf = read_file(path, &filelen)) == NULL) { - printWarn("Could not open '%s'", path); + #ifdef ARCH_X86 + if(hv_present) { + printWarn("Could not open '%s'", path); + } + else { + perror("open"); + printBug("Could not open '%s'", path); + } + #elif ARCH_ARM + printWarn("Could not open '%s'", path); + #endif return UNKNOWN_FREQ; } @@ -59,14 +69,14 @@ long get_freq_from_file(char* path) { return ret/1000; } -long get_max_freq_from_file(uint32_t core) { +long get_max_freq_from_file(uint32_t core, bool hv_present) { char path[_PATH_FREQUENCY_MAX_LEN]; sprintf(path, "%s%s/cpu%d%s%s", _PATH_SYS_SYSTEM, _PATH_SYS_CPU, core, _PATH_FREQUENCY, _PATH_FREQUENCY_MAX); - return get_freq_from_file(path); + return get_freq_from_file(path, hv_present); } -long get_min_freq_from_file(uint32_t core) { +long get_min_freq_from_file(uint32_t core, bool hv_present) { char path[_PATH_FREQUENCY_MAX_LEN]; sprintf(path, "%s%s/cpu%d%s%s", _PATH_SYS_SYSTEM, _PATH_SYS_CPU, core, _PATH_FREQUENCY, _PATH_FREQUENCY_MIN); - return get_freq_from_file(path); + return get_freq_from_file(path, hv_present); } diff --git a/src/common/udev.h b/src/common/udev.h index 5cc7fbc..863fc2d 100644 --- a/src/common/udev.h +++ b/src/common/udev.h @@ -10,6 +10,8 @@ #include #include +#include "cpu.h" + #define _PATH_SYS_SYSTEM "/sys/devices/system" #define _PATH_SYS_CPU "/cpu" #define _PATH_FREQUENCY "/cpufreq" @@ -20,7 +22,7 @@ #define DEFAULT_FILE_SIZE 4096 char* read_file(char* path, int* len); -long get_max_freq_from_file(uint32_t core); -long get_min_freq_from_file(uint32_t core); +long get_max_freq_from_file(uint32_t core, bool hv_present); +long get_min_freq_from_file(uint32_t core, bool hv_present); #endif diff --git a/src/x86/cpuid.c b/src/x86/cpuid.c index e4503c2..f6286a1 100755 --- a/src/x86/cpuid.c +++ b/src/x86/cpuid.c @@ -661,13 +661,28 @@ struct frequency* get_frequency_info(struct cpuInfo* cpu) { if(cpu->maxLevels < 0x00000016) { #ifdef _WIN32 - printErr("Can't read frequency information from cpuid (needed level is 0x%.8X, max is 0x%.8X)", 0x00000016, cpu->maxLevels); + if(cpu->hv->present) { + printWarn("Can't read frequency information from cpuid (needed level is 0x%.8X, max is 0x%.8X)", 0x00000016, cpu->maxLevels); + } + else { + printErr("Can't read frequency information from cpuid (needed level is 0x%.8X, max is 0x%.8X)", 0x00000016, cpu->maxLevels); + } freq->base = UNKNOWN_FREQ; freq->max = UNKNOWN_FREQ; #else printWarn("Can't read frequency information from cpuid (needed level is 0x%.8X, max is 0x%.8X). Using udev", 0x00000016, cpu->maxLevels); freq->base = UNKNOWN_FREQ; - freq->max = get_max_freq_from_file(0); + freq->max = get_max_freq_from_file(0, cpu->hv->present); + + if(freq->max == 0) { + if(cpu->hv->present) { + printWarn("Read max CPU frequency and got 0 MHz"); + } + else { + printBug("Read max CPU frequency and got 0 MHz"); + } + freq->max = UNKNOWN_FREQ; + } #endif } else { @@ -682,11 +697,21 @@ struct frequency* get_frequency_info(struct cpuInfo* cpu) { freq->max = ebx; if(freq->base == 0) { - printWarn("Read base CPU frequency and got 0 MHz"); + if(cpu->hv->present) { + printWarn("Read base CPU frequency and got 0 MHz"); + } + else { + printBug("Read base CPU frequency and got 0 MHz"); + } freq->base = UNKNOWN_FREQ; } if(freq->max == 0) { - printWarn("Read max CPU frequency and got 0 MHz"); + if(cpu->hv->present) { + printWarn("Read max CPU frequency and got 0 MHz"); + } + else { + printBug("Read max CPU frequency and got 0 MHz"); + } freq->max = UNKNOWN_FREQ; } } @@ -870,6 +895,7 @@ char* get_str_fma(struct cpuInfo* cpu) { void print_debug(struct cpuInfo* cpu) { printf("%s\n", cpu->cpu_name); + if(cpu->hv->present) printf("- Hypervisor: %s\n", cpu->hv->hv_name); printf("- Max standard level: 0x%.8X\n", cpu->maxLevels); printf("- Max extended level: 0x%.8X\n", cpu->maxExtendedLevels);