2022-09-04 22:20:12 +02:00
|
|
|
#include "cpu.h"
|
2023-01-28 12:40:12 +01:00
|
|
|
#include "common/io/io.h"
|
2022-09-04 22:20:12 +02:00
|
|
|
#include "common/properties.h"
|
2022-09-25 15:23:00 +08:00
|
|
|
#include "detection/temps/temps_linux.h"
|
2022-09-04 22:20:12 +02:00
|
|
|
|
2022-12-28 17:18:08 +08:00
|
|
|
#include <sys/sysinfo.h>
|
2022-09-04 22:20:12 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2023-01-08 02:33:35 -06:00
|
|
|
static void parseCpuInfo(FFCPUResult* cpu, FFstrbuf* physicalCoresBuffer, FFstrbuf* cpuMHz, FFstrbuf* cpuIsa, FFstrbuf* cpuUarch)
|
2022-09-04 22:20:12 +02:00
|
|
|
{
|
|
|
|
|
FILE* cpuinfo = fopen("/proc/cpuinfo", "r");
|
|
|
|
|
if(cpuinfo == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
char* line = NULL;
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
|
|
while(getline(&line, &len, cpuinfo) != -1)
|
|
|
|
|
{
|
|
|
|
|
//Stop after the first CPU
|
2023-01-08 02:33:35 -06:00
|
|
|
if(*line == '\0' || *line == '\n')
|
2022-09-04 22:20:12 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
(void)(
|
|
|
|
|
ffParsePropLine(line, "model name :", &cpu->name) ||
|
|
|
|
|
ffParsePropLine(line, "vendor_id :", &cpu->vendor) ||
|
|
|
|
|
ffParsePropLine(line, "cpu cores :", physicalCoresBuffer) ||
|
2022-11-24 23:29:46 +08:00
|
|
|
ffParsePropLine(line, "cpu MHz :", cpuMHz) ||
|
2023-01-08 02:33:35 -06:00
|
|
|
ffParsePropLine(line, "isa :", cpuIsa) ||
|
|
|
|
|
ffParsePropLine(line, "uarch :", cpuUarch) ||
|
2022-09-04 22:20:12 +02:00
|
|
|
(cpu->name.length == 0 && ffParsePropLine(line, "Hardware :", &cpu->name)) //For Android devices
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(line != NULL)
|
|
|
|
|
free(line);
|
|
|
|
|
|
|
|
|
|
fclose(cpuinfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static double getGHz(const char* file)
|
|
|
|
|
{
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY content = ffStrbufCreate();
|
2022-11-24 23:29:46 +08:00
|
|
|
if(ffAppendFileBuffer(file, &content))
|
|
|
|
|
{
|
|
|
|
|
double herz = ffStrbufToDouble(&content);
|
2022-09-04 22:20:12 +02:00
|
|
|
|
2022-11-24 23:29:46 +08:00
|
|
|
//ffStrbufToDouble failed
|
|
|
|
|
if(herz != herz)
|
|
|
|
|
return 0;
|
2022-09-04 22:20:12 +02:00
|
|
|
|
2022-11-24 23:29:46 +08:00
|
|
|
herz /= 1000.0; //to MHz
|
|
|
|
|
return herz / 1000.0; //to GHz
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2022-09-04 22:20:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static double getFrequency(const char* info, const char* scaling)
|
|
|
|
|
{
|
|
|
|
|
double frequency = getGHz(info);
|
|
|
|
|
if(frequency > 0.0)
|
|
|
|
|
return frequency;
|
|
|
|
|
|
|
|
|
|
return getGHz(scaling);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 14:03:57 +08:00
|
|
|
static double detectCPUTemp(void)
|
2022-09-04 22:20:12 +02:00
|
|
|
{
|
2023-01-21 06:18:19 +08:00
|
|
|
const FFTempsResult* temps = ffDetectTemps();
|
2022-09-04 22:20:12 +02:00
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < temps->values.length; i++)
|
|
|
|
|
{
|
|
|
|
|
FFTempValue* value = ffListGet(&temps->values, i);
|
|
|
|
|
|
|
|
|
|
if(
|
|
|
|
|
ffStrbufFirstIndexS(&value->name, "cpu") < value->name.length ||
|
|
|
|
|
ffStrbufCompS(&value->name, "k10temp") == 0 ||
|
|
|
|
|
ffStrbufCompS(&value->name, "coretemp") == 0
|
|
|
|
|
) return value->value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FF_CPU_TEMP_UNSET;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 02:33:35 -06:00
|
|
|
static void parseIsa(FFstrbuf* cpuIsa)
|
|
|
|
|
{
|
|
|
|
|
if(ffStrbufStartsWithS(cpuIsa, "rv"))
|
|
|
|
|
{
|
|
|
|
|
// RISC-V ISA string example: "rv64imafdch_zicsr_zifencei".
|
|
|
|
|
// The _z parts are not important for CPU showcasing, so we remove them.
|
|
|
|
|
if(ffStrbufContainC(cpuIsa, '_'))
|
|
|
|
|
ffStrbufSubstrBeforeFirstC(cpuIsa, '_');
|
|
|
|
|
// Then we replace "imafd" with "g" since "g" is a shorthand.
|
|
|
|
|
if(ffStrbufContainS(cpuIsa, "imafd"))
|
|
|
|
|
{
|
|
|
|
|
// Remove 4 of the 5 characters and replace the remaining one with "g".
|
|
|
|
|
ffStrbufRemoveSubstr(cpuIsa, 4, 8);
|
|
|
|
|
cpuIsa->chars[4] = 'g';
|
|
|
|
|
}
|
|
|
|
|
// The final ISA output of the above example is "rv64gch".
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-17 11:39:10 +08:00
|
|
|
void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu)
|
2022-09-04 22:20:12 +02:00
|
|
|
{
|
2023-03-15 18:17:24 +08:00
|
|
|
if(instance->config.cpu.temp)
|
2023-01-21 14:03:57 +08:00
|
|
|
cpu->temperature = detectCPUTemp();
|
2022-09-26 11:06:21 +08:00
|
|
|
else
|
|
|
|
|
cpu->temperature = FF_CPU_TEMP_UNSET;
|
2022-09-24 15:59:36 +02:00
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY physicalCoresBuffer = ffStrbufCreate();
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY cpuMHz = ffStrbufCreate();
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY cpuIsa = ffStrbufCreate();
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY cpuUarch = ffStrbufCreate();
|
2023-01-08 02:33:35 -06:00
|
|
|
|
|
|
|
|
parseCpuInfo(cpu, &physicalCoresBuffer, &cpuMHz, &cpuIsa, &cpuUarch);
|
2022-09-04 22:20:12 +02:00
|
|
|
|
|
|
|
|
cpu->coresPhysical = ffStrbufToUInt16(&physicalCoresBuffer, 1);
|
2022-09-19 19:31:29 +02:00
|
|
|
|
2022-12-28 17:18:08 +08:00
|
|
|
cpu->coresLogical = (uint16_t) get_nprocs_conf();
|
|
|
|
|
cpu->coresOnline = (uint16_t) get_nprocs();
|
2022-09-04 22:20:12 +02:00
|
|
|
|
|
|
|
|
#define BP "/sys/devices/system/cpu/cpufreq/policy0/"
|
2023-01-28 11:52:23 +01:00
|
|
|
if(ffPathExists(BP, FF_PATHTYPE_DIRECTORY))
|
2022-11-24 23:29:46 +08:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2022-09-04 22:20:12 +02:00
|
|
|
|
2023-01-08 02:33:35 -06:00
|
|
|
if(cpuUarch.length > 0)
|
|
|
|
|
{
|
|
|
|
|
if(cpu->name.length > 0)
|
|
|
|
|
ffStrbufAppendC(&cpu->name, ' ');
|
|
|
|
|
ffStrbufAppend(&cpu->name, &cpuUarch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(cpuIsa.length > 0)
|
|
|
|
|
{
|
|
|
|
|
parseIsa(&cpuIsa);
|
|
|
|
|
if(cpu->name.length > 0)
|
|
|
|
|
ffStrbufAppendC(&cpu->name, ' ');
|
|
|
|
|
ffStrbufAppend(&cpu->name, &cpuIsa);
|
|
|
|
|
}
|
2022-09-04 22:20:12 +02:00
|
|
|
}
|