From 6981d61eaf1387b88643aa40adbd7dc5b0dfa9a2 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Mon, 1 Nov 2021 13:07:48 +0100 Subject: [PATCH] [v1.00][X86] Show unknown string when the number of cores cannot be retrieved (like #119) --- src/arm/midr.c | 12 ++++++------ src/common/cpu.c | 2 +- src/common/cpu.h | 6 +++--- src/common/udev.c | 6 +++--- src/ppc/ppc.c | 2 +- src/x86/apic.c | 9 +++++++-- src/x86/cpuid.c | 22 +++++++++++++--------- 7 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/arm/midr.c b/src/arm/midr.c index ba0deef..83b46a9 100644 --- a/src/arm/midr.c +++ b/src/arm/midr.c @@ -45,7 +45,7 @@ struct cache* get_cache_info(struct cpuInfo* cpu) { struct frequency* get_frequency_info(uint32_t core) { struct frequency* freq = emalloc(sizeof(struct frequency)); - freq->base = UNKNOWN_FREQ; + freq->base = UNKNOWN_DATA; freq->max = get_max_freq_from_file(core); return freq; @@ -81,7 +81,7 @@ int64_t get_peak_performance(struct cpuInfo* cpu) { //First check we have consistent data for(int i=0; i < cpu->num_cpus; ptr = ptr->next_cpu, i++) { - if(get_freq(ptr->freq) == UNKNOWN_FREQ) { + if(get_freq(ptr->freq) == UNKNOWN_DATA) { return -1; } } @@ -204,7 +204,7 @@ struct cpuInfo* get_cpu_info_linux(struct cpuInfo* cpu) { } freq_array[i] = get_max_freq_from_file(i); - if(freq_array[i] == UNKNOWN_FREQ) { + if(freq_array[i] == UNKNOWN_DATA) { printWarn("Unable to fetch max frequency for core %d. This is probably because the core is offline", i); freq_array[i] = freq_array[0]; } @@ -256,7 +256,7 @@ void fill_cpu_info_firestorm_icestorm(struct cpuInfo* cpu) { ice->topo->cach = ice->cach; ice->topo->total_cores = 4; ice->freq = malloc(sizeof(struct frequency)); - ice->freq->base = UNKNOWN_FREQ; + ice->freq->base = UNKNOWN_DATA; ice->freq->max = 2064; ice->hv = malloc(sizeof(struct hypervisor)); ice->hv->present = false; @@ -272,7 +272,7 @@ void fill_cpu_info_firestorm_icestorm(struct cpuInfo* cpu) { fire->topo->cach = fire->cach; fire->topo->total_cores = 4; fire->freq = malloc(sizeof(struct frequency)); - fire->freq->base = UNKNOWN_FREQ; + fire->freq->base = UNKNOWN_DATA; fire->freq->max = 3200; fire->hv = malloc(sizeof(struct hypervisor)); fire->hv->present = false; @@ -368,7 +368,7 @@ void print_debug(struct cpuInfo* cpu) { else { printf("0x%.8X ", midr); } - if(freq == UNKNOWN_FREQ) { + if(freq == UNKNOWN_DATA) { 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)); } diff --git a/src/common/cpu.c b/src/common/cpu.c index f075e94..5cbbf9d 100644 --- a/src/common/cpu.c +++ b/src/common/cpu.c @@ -150,7 +150,7 @@ char* get_str_freq(struct frequency* freq) { char* string = emalloc(sizeof(char)*size); memset(string, 0, sizeof(char)*size); - if(freq->max == UNKNOWN_FREQ || freq->max < 0) + if(freq->max == UNKNOWN_DATA || freq->max < 0) snprintf(string,strlen(STRING_UNKNOWN)+1,STRING_UNKNOWN); else if(freq->max >= 1000) snprintf(string,size,"%.3f "STRING_GIGAHERZ,(float)(freq->max)/1000); diff --git a/src/common/cpu.h b/src/common/cpu.h index 8da5998..c8bd6a7 100644 --- a/src/common/cpu.h +++ b/src/common/cpu.h @@ -34,7 +34,7 @@ enum { HV_VENDOR_INVALID }; -#define UNKNOWN_FREQ -1 +#define UNKNOWN_DATA -1 #define CPU_NAME_MAX_LENGTH 64 typedef int32_t VENDOR; @@ -71,8 +71,8 @@ struct topology { int32_t total_cores; struct cache* cach; #if defined(ARCH_X86) || defined(ARCH_PPC) - uint32_t physical_cores; - uint32_t logical_cores; + int32_t physical_cores; + int32_t logical_cores; uint32_t sockets; uint32_t smt_supported; // Number of SMT that CPU supports (equal to smt_available if SMT is enabled) #ifdef ARCH_X86 diff --git a/src/common/udev.c b/src/common/udev.c index 07cb568..0505ad0 100644 --- a/src/common/udev.c +++ b/src/common/udev.c @@ -33,7 +33,7 @@ long get_freq_from_file(char* path) { char* buf; if((buf = read_file(path, &filelen)) == NULL) { printWarn("Could not open '%s'", path); - return UNKNOWN_FREQ; + return UNKNOWN_DATA; } char* end; @@ -42,7 +42,7 @@ long get_freq_from_file(char* path) { if(errno != 0) { printBug("strtol: %s", strerror(errno)); free(buf); - return UNKNOWN_FREQ; + return UNKNOWN_DATA; } // We will be getting the frequency in KHz @@ -50,7 +50,7 @@ long get_freq_from_file(char* path) { // greater than 10 GHz or less than 100 MHz if(ret > 10000 * 1000 || ret < 100 * 1000) { printBug("Invalid data was read from file '%s': %ld\n", path, ret); - return UNKNOWN_FREQ; + return UNKNOWN_DATA; } free(buf); diff --git a/src/ppc/ppc.c b/src/ppc/ppc.c index ad5dead..ddd9c33 100644 --- a/src/ppc/ppc.c +++ b/src/ppc/ppc.c @@ -139,7 +139,7 @@ int64_t get_peak_performance(struct cpuInfo* cpu, struct topology* topo, int64_t */ //First check we have consistent data - if(freq == UNKNOWN_FREQ) { + if(freq == UNKNOWN_DATA) { return -1; } diff --git a/src/x86/apic.c b/src/x86/apic.c index e777601..2ce13b3 100644 --- a/src/x86/apic.c +++ b/src/x86/apic.c @@ -324,7 +324,7 @@ bool fill_apic_ids(uint32_t* apic_ids, int n, bool x2apic_id) { for(int i=0; i < n; i++) { if(!bind_to_cpu(i)) { - printErr("Failed binding to CPU %d", i); + printErr("Failed binding the process to CPU %d", i); return false; } apic_ids[i] = get_apic_id(x2apic_id); @@ -385,8 +385,13 @@ bool get_topology_from_apic(struct cpuInfo* cpu, struct topology* topo) { get_cache_topology_from_apic(topo); - if(!fill_apic_ids(apic_ids, topo->total_cores, x2apic_id)) + if(!fill_apic_ids(apic_ids, topo->total_cores, x2apic_id)) { + topo->logical_cores = UNKNOWN_DATA; + topo->physical_cores = UNKNOWN_DATA; + topo->smt_available = 1; + topo->smt_supported = 1; return false; + } for(int i=0; i < topo->total_cores; i++) { apic_id = apic_ids[i]; diff --git a/src/x86/cpuid.c b/src/x86/cpuid.c index 46c213c..9978521 100644 --- a/src/x86/cpuid.c +++ b/src/x86/cpuid.c @@ -203,7 +203,7 @@ int64_t get_peak_performance(struct cpuInfo* cpu, struct topology* topo, int64_t #endif //First, check we have consistent data - if(freq == UNKNOWN_FREQ) { + if(freq == UNKNOWN_DATA || topo->logical_cores == UNKNOWN_DATA) { return -1; } @@ -710,16 +710,16 @@ struct frequency* get_frequency_info(struct cpuInfo* cpu) { if(cpu->maxLevels < 0x00000016) { #if defined (_WIN32) || defined (__APPLE__) printWarn("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; + freq->base = UNKNOWN_DATA; + freq->max = UNKNOWN_DATA; #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->base = UNKNOWN_DATA; freq->max = get_max_freq_from_file(0); if(freq->max == 0) { printWarn("Read max CPU frequency from udev and got 0 MHz"); - freq->max = UNKNOWN_FREQ; + freq->max = UNKNOWN_DATA; } #endif } @@ -736,7 +736,7 @@ struct frequency* get_frequency_info(struct cpuInfo* cpu) { if(freq->base == 0) { printWarn("Read base CPU frequency from CPUID and got 0 MHz"); - freq->base = UNKNOWN_FREQ; + freq->base = UNKNOWN_DATA; } if(freq->max == 0) { printWarn("Read max CPU frequency from CPUID and got 0 MHz"); @@ -746,10 +746,10 @@ struct frequency* get_frequency_info(struct cpuInfo* cpu) { if(freq->max == 0) { printWarn("Read max CPU frequency from udev and got 0 MHz"); - freq->max = UNKNOWN_FREQ; + freq->max = UNKNOWN_DATA; } #else - freq->max = UNKNOWN_FREQ; + freq->max = UNKNOWN_DATA; #endif } } @@ -771,7 +771,11 @@ char* get_str_topology(struct cpuInfo* cpu, struct topology* topo, bool dual_soc int topo_sockets = dual_socket ? topo->sockets : 1; char* string; - if(topo->smt_supported > 1) { + if(topo->logical_cores == UNKNOWN_DATA) { + string = emalloc(sizeof(char) * (strlen(STRING_UNKNOWN) + 1)); + strcpy(string, STRING_UNKNOWN); + } + else if(topo->smt_supported > 1) { // 4 for digits, 21 for ' cores (SMT disabled)' which is the longest possible output uint32_t max_size = 4+21+1; string = emalloc(sizeof(char) * max_size);