Files
fastfetch/src/modules/cpu.c
T

137 lines
4.4 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
2021-03-28 13:02:40 +02:00
static double getGhz(const char* file)
{
FF_STRBUF_CREATE(content);
ffGetFileContent(file, &content);
if(content.length == 0)
return 0;
uint32_t herz;
if(sscanf(content.chars, "%u", &herz) != 1)
return 0;
ffStrbufDestroy(&content);
herz /= 1000; //to MHz
return (double) herz / 1000.0; //to GHz
}
2021-02-27 18:30:05 +01:00
void ffPrintCPU(FFinstance* instance)
2021-02-18 22:25:36 +01:00
{
2021-04-03 11:39:59 +02:00
if(ffPrintCachedValue(instance, &instance->config.cpuKey, "CPU"))
2021-02-22 16:52:51 +01:00
return;
2021-03-28 13:02:40 +02:00
char name[256]; name[0] = '\0';
char vendor[256]; vendor[0] = '\0';
int physicalCores = 0;
2021-03-27 18:14:19 +01:00
2021-03-28 13:02:40 +02:00
FILE* cpuinfo = fopen("/proc/cpuinfo", "r");
char* line = NULL;
size_t len = 0;
while(getline(&line, &len, cpuinfo) != -1)
2021-02-19 16:23:41 +01:00
{
2021-03-28 13:02:40 +02:00
sscanf(line, "model name%*s %[^\n]", name);
sscanf(line, "vendor_id%*s %[^\n]", vendor);
sscanf(line, "cpu cores%*s %i", &physicalCores);
//Stop after the first CPU
if(strstr(line, "flags") != NULL)
break;
2021-02-19 16:23:41 +01:00
}
2021-02-18 22:25:36 +01:00
2021-03-28 13:02:40 +02:00
if(line != NULL)
free(line);
fclose(cpuinfo);
FFstrbuf namePretty;
ffStrbufInitA(&namePretty, 256);
ffStrbufAppendS(&namePretty, name);
ffStrbufRemoveStrings(&namePretty, 11,
"(R)", "(r)", "(TM)", "(tm)", " CPU", " FPU", " Processor", " Dual-Core", " Quad-Core", " Six-Core", " Eight-Core"
);
2021-04-02 16:21:52 +02:00
ffStrbufSubstrBeforeFirstC(&namePretty, '@'); //Cut the speed output in the name as we append our own
2021-03-28 13:02:40 +02:00
ffStrbufTrimRight(&namePretty, ' '); //If we removed the @ in previous step there was most likely a space before it
double biosLimit = getGhz("/sys/devices/system/cpu/cpu0/cpufreq/bios_limit");
double scalingMaxFreq = getGhz("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq");
double scalingMinFreq = getGhz("/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq");
double infoMaxFreq = getGhz("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq");
double infoMinFreq = getGhz("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq");
2021-02-18 22:25:36 +01:00
2021-03-28 13:02:40 +02:00
int numProcsOnline = get_nprocs();
int numProcsAvailable = get_nprocs_conf();
2021-03-19 20:57:07 +01:00
FF_STRBUF_CREATE(cpu);
2021-03-19 20:57:07 +01:00
2021-03-23 19:48:52 +01:00
if(instance->config.cpuFormat.length == 0)
2021-03-19 20:57:07 +01:00
{
2021-03-28 13:02:40 +02:00
//The current get_nprocs* returns 1 on failure. It also makes no sense to have a (1) as count
int numProcs = numProcsOnline;
if(numProcs <= 1)
numProcs = numProcsAvailable;
if(numProcs <= 1)
numProcs = physicalCores;
double ghz = biosLimit;
if(ghz == 0)
ghz = scalingMaxFreq;
if(ghz == 0)
ghz = infoMaxFreq;
if(ghz == 0)
ghz = scalingMinFreq;
if(ghz == 0)
ghz = infoMinFreq;
if(namePretty.length > 0)
ffStrbufAppend(&cpu, &namePretty);
else if(name[0] != '\0')
ffStrbufAppendS(&cpu, name);
else if(vendor[0] != '\0')
{
ffStrbufAppendS(&cpu, vendor);
ffStrbufAppendS(&cpu, " unknown processor");
}
else if(numProcs > 1 || ghz > 0)
ffStrbufAppendS(&cpu, " unknwon processor");
else
{
2021-04-03 11:39:59 +02:00
ffPrintError(instance, &instance->config.cpuKey, "CPU", "No CPU info found in /proc/cpuinfo");
2021-03-28 13:02:40 +02:00
ffStrbufDestroy(&cpu);
ffStrbufDestroy(&namePretty);
return;
}
if(numProcs > 1)
ffStrbufAppendF(&cpu, " (%i)", numProcs);
if(ghz > 0)
ffStrbufAppendF(&cpu, " @ %.9gGHz", ghz);
2021-03-19 20:57:07 +01:00
}
else
2021-03-27 18:14:19 +01:00
{
2021-03-28 13:02:40 +02:00
ffParseFormatString(&cpu, &instance->config.cpuFormat, 11,
2021-03-19 20:57:07 +01:00
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, name},
2021-03-28 13:02:40 +02:00
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &namePretty},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, vendor},
(FFformatarg){FF_FORMAT_ARG_TYPE_INT, &numProcsOnline},
(FFformatarg){FF_FORMAT_ARG_TYPE_INT, &numProcsAvailable},
(FFformatarg){FF_FORMAT_ARG_TYPE_INT, &physicalCores},
(FFformatarg){FF_FORMAT_ARG_TYPE_DOUBLE, &biosLimit},
(FFformatarg){FF_FORMAT_ARG_TYPE_DOUBLE, &scalingMaxFreq},
(FFformatarg){FF_FORMAT_ARG_TYPE_DOUBLE, &scalingMinFreq},
(FFformatarg){FF_FORMAT_ARG_TYPE_DOUBLE, &infoMaxFreq},
(FFformatarg){FF_FORMAT_ARG_TYPE_DOUBLE, &infoMinFreq}
2021-03-19 20:57:07 +01:00
);
}
2021-03-27 18:14:19 +01:00
2021-04-03 11:39:59 +02:00
ffPrintAndSaveCachedValue(instance, &instance->config.cpuKey, "CPU", &cpu);
2021-03-19 20:57:07 +01:00
ffStrbufDestroy(&cpu);
2021-03-28 13:02:40 +02:00
ffStrbufDestroy(&namePretty);
2021-03-27 18:14:19 +01:00
}