Files
fastfetch/src/modules/cpu.c
T

56 lines
1.6 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
2021-02-27 18:30:05 +01:00
void ffPrintCPU(FFinstance* instance)
2021-02-18 22:25:36 +01:00
{
2021-02-27 18:30:05 +01:00
if(ffPrintCachedValue(instance, "CPU"))
2021-02-22 16:52:51 +01:00
return;
2021-02-18 22:25:36 +01:00
char name[256];
ffParsePropFile("/proc/cpuinfo", "model name%*s %[^\n]", name);
2021-02-19 16:23:41 +01:00
if(name[0] == '\0')
{
2021-02-27 18:30:05 +01:00
ffPrintError(instance, "CPU", "\"model name%*s %[^\\n]\" not found in \"/proc/cpuinfo\"");
2021-02-19 16:23:41 +01:00
return;
}
2021-02-18 22:25:36 +01:00
2021-02-19 16:23:41 +01:00
FILE* frequencyFile = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq", "r");
if(frequencyFile == NULL)
{
2021-02-27 18:30:05 +01:00
ffPrintError(instance, "CPU", "fopen(\"/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq\", \"r\") == NULL");
2021-02-19 16:23:41 +01:00
return;
}
uint32_t frequency;
int scanned = fscanf(frequencyFile, "%u", &frequency);
if(scanned != 1)
{
2021-02-27 18:30:05 +01:00
ffPrintError(instance, "CPU", "fscanf(frequencyFile, \"%s\", frequency) != 1");
2021-02-19 16:23:41 +01:00
return;
}
fclose(frequencyFile);
2021-02-18 22:25:36 +01:00
2021-02-19 16:23:41 +01:00
frequency /= 1000; //to MHz
double ghz = (double) frequency / 1000.0; //to GHz
2021-02-18 22:25:36 +01:00
2021-03-19 20:57:07 +01:00
int numProcs = get_nprocs();
ffPrintLogoAndKey(instance, "CPU");
FFstrbuf cpu;
ffStrbufInit(&cpu);
if(ffStrbufIsEmpty(&instance->config.cpuFormat))
{
ffStrbufSetF(&cpu, "%s (%i) @ %.9gGHz", name, numProcs, ghz);
}
else
{
ffParseFormatString(&cpu, &instance->config.cpuFormat, 3,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, name},
(FFformatarg){FF_FORMAT_ARG_TYPE_INT, &numProcs},
(FFformatarg){FF_FORMAT_ARG_TYPE_DOUBLE, &ghz}
);
}
2021-02-22 20:53:12 +01:00
2021-03-19 20:57:07 +01:00
ffPrintAndSaveCachedValue(instance, "CPU", cpu.chars);
ffStrbufDestroy(&cpu);
2021-02-18 22:25:36 +01:00
}