From ca5677a77f61e18ec8679826834036123c9f4636 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Thu, 12 Aug 2021 10:39:26 +0200 Subject: [PATCH] [v0.99][X86] Improve CPU abbreviate code --- src/common/cpu.c | 6 ++++-- src/common/global.c | 13 ++++++++++++ src/common/global.h | 1 + src/x86/cpuid.c | 49 ++++++++++++++++++++++++++------------------- src/x86/cpuid.h | 3 +-- 5 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/common/cpu.c b/src/common/cpu.c index a1f6a42..661d10d 100644 --- a/src/common/cpu.c +++ b/src/common/cpu.c @@ -34,9 +34,11 @@ int64_t get_freq(struct frequency* freq) { #if defined(ARCH_X86) || defined(ARCH_PPC) char* get_str_cpu_name(struct cpuInfo* cpu, bool fcpuname) { - if(cpu->cpu_vendor == CPU_VENDOR_INTEL && !fcpuname) { - abbreviate_intel_cpu_name(&cpu->cpu_name); + #ifdef ARCH_X86 + if(!fcpuname) { + return get_str_cpu_name_abbreviated(cpu); } + #endif return cpu->cpu_name; } diff --git a/src/common/global.c b/src/common/global.c index 8b9f538..1ae06dd 100644 --- a/src/common/global.c +++ b/src/common/global.c @@ -73,6 +73,19 @@ int max(int a, int b) { return a > b ? a : b; } +char *strremove(char *str, const char *sub) { + char *p, *q, *r; + if (*sub && (q = r = strstr(str, sub)) != NULL) { + size_t len = strlen(sub); + while ((r = strstr(p = r + len, sub)) != NULL) { + memmove(q, p, r - p); + q += r - p; + } + memmove(q, p, strlen(p) + 1); + } + return str; +} + void* emalloc(size_t size) { void* ptr = malloc(size); diff --git a/src/common/global.h b/src/common/global.h index f0e9118..1a40fb1 100644 --- a/src/common/global.h +++ b/src/common/global.h @@ -11,6 +11,7 @@ void printWarn(const char *fmt, ...); void printErr(const char *fmt, ...); void printBug(const char *fmt, ...); int max(int a, int b); +char *strremove(char *str, const char *sub); void* emalloc(size_t size); void* ecalloc(size_t nmemb, size_t size); diff --git a/src/x86/cpuid.c b/src/x86/cpuid.c index dab8027..5dfa96d 100644 --- a/src/x86/cpuid.c +++ b/src/x86/cpuid.c @@ -116,7 +116,7 @@ char* get_str_cpu_name_internal() { return name; } -void abbreviate_intel_cpu_name(char** name) { +bool abbreviate_intel_cpu_name(char** name) { char* old_name = *name; char* new_name = ecalloc(strlen(old_name) + 1, sizeof(char)); @@ -125,41 +125,39 @@ void abbreviate_intel_cpu_name(char** name) { char* aux_ptr = NULL; // 1. Remove "(R)" - if(strncmp(old_name_ptr, "Intel(R)", 8) != 0) return; + old_name_ptr = strstr(old_name_ptr, "Intel(R)"); + if(old_name_ptr == NULL) return false; strcpy(new_name_ptr, "Intel"); - new_name_ptr += 5; - old_name_ptr += 8; + new_name_ptr += strlen("Intel"); + old_name_ptr += strlen("Intel(R)"); // 2. Remove "(R)" or "(TM)" aux_ptr = strstr(old_name_ptr, "("); - if(aux_ptr == NULL) return; + if(aux_ptr == NULL) return false; strncpy(new_name_ptr, old_name_ptr, aux_ptr-old_name_ptr); new_name_ptr += aux_ptr-old_name_ptr; strcpy(new_name_ptr, " "); new_name_ptr++; old_name_ptr = strstr(aux_ptr, ")"); - if(old_name_ptr == NULL) return; - old_name_ptr += 2; + if(old_name_ptr == NULL) return false; + old_name_ptr++; + while(*old_name_ptr == ' ') old_name_ptr++; - // 3. Skip "CPU" (if exists) - if(strncmp(old_name_ptr, "CPU", 3) == 0) { - aux_ptr = strstr(old_name_ptr, " "); - if(aux_ptr == NULL) return; - old_name_ptr = aux_ptr + 1; - } - - // 4. Copy the CPU name - aux_ptr = strstr(old_name_ptr, "CPU"); - if(aux_ptr == NULL) { - // Name contains no "CPU" at the end, so ends with @ - aux_ptr = strstr(old_name_ptr, "@"); - if(aux_ptr == NULL) return; - } + // 3. Copy the CPU name + aux_ptr = strstr(old_name_ptr, "@"); + if(aux_ptr == NULL) return false; strncpy(new_name_ptr, old_name_ptr, (aux_ptr-1)-old_name_ptr); + // 4. Remove dummy strings in Intel CPU names + strremove(new_name, " CPU"); + strremove(new_name, " Dual"); + strremove(new_name, " 0"); + free(old_name); *name = new_name; + + return true; } struct uarch* get_cpu_uarch(struct cpuInfo* cpu) { @@ -748,6 +746,15 @@ struct frequency* get_frequency_info(struct cpuInfo* cpu) { } // STRING FUNCTIONS +char* get_str_cpu_name_abbreviated(struct cpuInfo* cpu) { + if(cpu->cpu_vendor == CPU_VENDOR_INTEL) { + if(!abbreviate_intel_cpu_name(&cpu->cpu_name)) { + printWarn("Failed to abbreviate CPU name"); + } + } + return cpu->cpu_name; +} + char* get_str_topology(struct cpuInfo* cpu, struct topology* topo, bool dual_socket) { int topo_sockets = dual_socket ? topo->sockets : 1; char* string; diff --git a/src/x86/cpuid.h b/src/x86/cpuid.h index 89a559a..d78517a 100644 --- a/src/x86/cpuid.h +++ b/src/x86/cpuid.h @@ -12,8 +12,7 @@ char* get_str_avx(struct cpuInfo* cpu); char* get_str_sse(struct cpuInfo* cpu); char* get_str_fma(struct cpuInfo* cpu); char* get_str_topology(struct cpuInfo* cpu, struct topology* topo, bool dual_socket); - -void abbreviate_intel_cpu_name(char** name); +char* get_str_cpu_name_abbreviated(struct cpuInfo* cpu); void print_debug(struct cpuInfo* cpu); void print_raw(struct cpuInfo* cpu);