Files
fastfetch/src/modules/cpu.c
T

54 lines
1.5 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-03-27 18:14:19 +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();
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
{
ffStrbufSetF(&cpu, "%s (%i) @ %.9gGHz", name, numProcs, ghz);
}
else
2021-03-27 18:14:19 +01:00
{
2021-03-19 20:57:07 +01:00
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-03-27 18:14:19 +01:00
ffPrintAndSaveCachedValue(instance, "CPU", &cpu);
2021-03-19 20:57:07 +01:00
ffStrbufDestroy(&cpu);
2021-03-27 18:14:19 +01:00
}