about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2023-10-07 11:57:32 +0200
committerptitSeb <sebastien.chev@gmail.com>2023-10-07 11:57:32 +0200
commit5fa02ef1e3f2680eb388c971670e83564d8d976c (patch)
treea47b091f2be4e68c51c343ed8e08e9d4d286417a /src
parentcfe8f8f6e13e94f7c6114a755ac004cbb544668a (diff)
downloadbox64-5fa02ef1e3f2680eb388c971670e83564d8d976c.tar.gz
box64-5fa02ef1e3f2680eb388c971670e83564d8d976c.zip
Improved cpu speed detection
Diffstat (limited to 'src')
-rw-r--r--src/tools/my_cpuid.c30
1 files changed, 28 insertions, 2 deletions
diff --git a/src/tools/my_cpuid.c b/src/tools/my_cpuid.c
index c80a5312..7ccf6b84 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,7 +18,34 @@ 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;