Files
fastfetch/src/modules/cpu.c
T

40 lines
1.1 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
#include <float.h>
void ffPrintCPU(FFstate* state)
{
2021-02-22 16:52:51 +01:00
if(ffPrintCachedValue(state, "CPU"))
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-20 19:08:23 +01:00
ffPrintError(state, "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-20 19:08:23 +01:00
ffPrintError(state, "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-20 19:08:23 +01:00
ffPrintError(state, "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-02-22 16:52:51 +01:00
char value[1024];
sprintf(value, "%s (%i) @ %.9gGHz", name, get_nprocs(), ghz);
2021-02-22 20:53:12 +01:00
ffPrintAndSaveCachedValue(state, "CPU", value);
2021-02-18 22:25:36 +01:00
}