From 402496c93eed1141b885cdf8fbcf61a2ffa64fe4 Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Sat, 7 Oct 2023 11:58:38 +0200 Subject: [PATCH] Improved cpu speed detection --- src/tools/my_cpuid.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/tools/my_cpuid.c b/src/tools/my_cpuid.c index 50ae2e804..554736e39 100644 --- a/src/tools/my_cpuid.c +++ b/src/tools/my_cpuid.c @@ -11,7 +11,6 @@ int get_cpuMhz() { int MHz = 0; -#ifdef __arm__ FILE *f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r"); if(f) { int r; @@ -19,12 +18,38 @@ int get_cpuMhz() MHz = r/1000; fclose(f); } -#endif + if(!MHz) { + // try with lscpu, grabbing the max frequency + FILE* f = popen("lscpu | grep \"CPU max MHz:\" | sed -r 's/CPU max MHz:\\s{1,}//g'", "r"); + if(f) { + char tmp[200] = ""; + ssize_t s = fread(tmp, 1, 200, f); + pclose(f); + if(s>0) { + // worked! (unless it's saying "lscpu: command not found" or something like that) + if(!strstr(tmp, "lscpu")) { + // trim ending + while(strlen(tmp) && tmp[strlen(tmp)-1]=='\n') + tmp[strlen(tmp)-1] = 0; + // incase multiple cpu type are present, there will be multiple lines + while(strchr(tmp, '\n')) + *strchr(tmp,'\n') = ' '; + // cut the float part (so '.' or ','), it's not needed + if(strchr(tmp, '.')) + *strchr(tmp, '.')= '\0'; + if(strchr(tmp, ',')) + *strchr(tmp, ',')= '\0'; + int mhz; + if(sscanf(tmp, "%d", &mhz)==1) + MHz = mhz; + } + } + } + } if(!MHz) MHz = 1000; // default to 1Ghz... return MHz; -} -int getNCpu(); // defined in wrappedlibc.c +}int getNCpu(); // defined in wrappedlibc.c const char* getCpuName(); // same void my_cpuid(x86emu_t* emu, uint32_t tmp32u)