mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
CPU: fix cpu freq detection in WSL
`/sys/devices/system/cpu/cpufreq/policy0/` doesn't exist in WSL
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void parseCpuInfo(FFCPUResult* cpu, FFstrbuf* physicalCoresBuffer)
|
||||
static void parseCpuInfo(FFCPUResult* cpu, FFstrbuf* physicalCoresBuffer, FFstrbuf* cpuMHz)
|
||||
{
|
||||
FILE* cpuinfo = fopen("/proc/cpuinfo", "r");
|
||||
if(cpuinfo == NULL)
|
||||
@@ -25,6 +25,7 @@ static void parseCpuInfo(FFCPUResult* cpu, FFstrbuf* physicalCoresBuffer)
|
||||
ffParsePropLine(line, "model name :", &cpu->name) ||
|
||||
ffParsePropLine(line, "vendor_id :", &cpu->vendor) ||
|
||||
ffParsePropLine(line, "cpu cores :", physicalCoresBuffer) ||
|
||||
ffParsePropLine(line, "cpu MHz :", cpuMHz) ||
|
||||
(cpu->name.length == 0 && ffParsePropLine(line, "Hardware :", &cpu->name)) //For Android devices
|
||||
);
|
||||
}
|
||||
@@ -39,16 +40,19 @@ static double getGHz(const char* file)
|
||||
{
|
||||
FFstrbuf content;
|
||||
ffStrbufInit(&content);
|
||||
ffReadFileBuffer(file, &content);
|
||||
double herz = ffStrbufToDouble(&content);
|
||||
ffStrbufDestroy(&content);
|
||||
if(ffAppendFileBuffer(file, &content))
|
||||
{
|
||||
double herz = ffStrbufToDouble(&content);
|
||||
ffStrbufDestroy(&content);
|
||||
|
||||
//ffStrbufToDouble failed
|
||||
if(herz != herz)
|
||||
return 0;
|
||||
//ffStrbufToDouble failed
|
||||
if(herz != herz)
|
||||
return 0;
|
||||
|
||||
herz /= 1000.0; //to MHz
|
||||
return herz / 1000.0; //to GHz
|
||||
herz /= 1000.0; //to MHz
|
||||
return herz / 1000.0; //to GHz
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static double getFrequency(const char* info, const char* scaling)
|
||||
@@ -91,7 +95,10 @@ void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu, bool cached)
|
||||
FFstrbuf physicalCoresBuffer;
|
||||
ffStrbufInit(&physicalCoresBuffer);
|
||||
|
||||
parseCpuInfo(cpu, &physicalCoresBuffer);
|
||||
FFstrbuf cpuMHz;
|
||||
ffStrbufInit(&cpuMHz);
|
||||
|
||||
parseCpuInfo(cpu, &physicalCoresBuffer, &cpuMHz);
|
||||
|
||||
cpu->coresPhysical = ffStrbufToUInt16(&physicalCoresBuffer, 1);
|
||||
|
||||
@@ -104,8 +111,16 @@ void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu, bool cached)
|
||||
#endif
|
||||
|
||||
#define BP "/sys/devices/system/cpu/cpufreq/policy0/"
|
||||
cpu->frequencyMin = getFrequency(BP"cpuinfo_min_freq", BP"scaling_min_freq");
|
||||
cpu->frequencyMax = getFrequency(BP"cpuinfo_max_freq", BP"scaling_max_freq");
|
||||
if(ffFileExists(BP, S_IFDIR))
|
||||
{
|
||||
cpu->frequencyMin = getFrequency(BP"cpuinfo_min_freq", BP"scaling_min_freq");
|
||||
cpu->frequencyMax = getFrequency(BP"cpuinfo_max_freq", BP"scaling_max_freq");
|
||||
}
|
||||
else
|
||||
{
|
||||
cpu->frequencyMin = cpu->frequencyMax = ffStrbufToDouble(&cpuMHz) / 1000;
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&physicalCoresBuffer);
|
||||
ffStrbufDestroy(&cpuMHz);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user