Skip to content

Commit

Permalink
Improved cpu speed detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed Oct 7, 2023
1 parent 84d415c commit 402496c
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/tools/my_cpuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,45 @@
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;
if(1==fscanf(f, "%d", &r))
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)
Expand Down

0 comments on commit 402496c

Please sign in to comment.