commit 31c9406b4646c018b16c013f81366372ff5053f5
parent d2174ff8515f933fff4818281fc55620ea71d6d2
Author: sam-barr <samfbarr@outlook.com>
Date: Thu, 23 Apr 2020 10:49:59 -0500
Merge pull request #27 from dwzg/cpu_frequency
Add detection of CPU max. frequency
Diffstat:
1 file changed, 27 insertions(+), 7 deletions(-)
diff --git a/paleofetch.c b/paleofetch.c
@@ -345,6 +345,7 @@ char *get_cpu() {
char *line = NULL;
size_t len; /* unused */
int num_cores = 0;
+ double freq;
/* read the model name into cpu_model, and increment num_cores every time model name is found */
while(getline(&line, &len, cpuinfo) != -1) {
@@ -353,17 +354,36 @@ char *get_cpu() {
free(line);
fclose(cpuinfo);
+ FILE *cpufreq = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r");
+
+ if (cpufreq == NULL) {
+ status = -1;
+ halt_and_catch_fire("Unable to open cpufreq");
+ }
+
+ line = NULL;
+
+ if (getline(&line, &len, cpufreq) != -1) {
+ sscanf(line, "%lf", &freq);
+ freq /= 1e6; // convert kHz to GHz
+ } else {
+ freq = 0.0; // cpuinfo_max_freq not available?
+ }
+
+ free(line);
+ fclose(cpufreq);
+
+ /* remove unneeded information */
+ remove_substring(cpu_model, "(R)", 3);
+ remove_substring(cpu_model, "(TM)", 4);
+ remove_substring(cpu_model, "Core", 4);
+ remove_substring(cpu_model, "CPU", 3);
+
char *cpu = malloc(BUF_SIZE);
- snprintf(cpu, BUF_SIZE, "%s(%d)", cpu_model, num_cores);
+ snprintf(cpu, BUF_SIZE, "%s (%d) @ %.1fGHz", cpu_model, num_cores, freq);
free(cpu_model);
- /* remove unneeded information */
- remove_substring(cpu, "(R)", 3);
- remove_substring(cpu, "(TM)", 4);
- remove_substring(cpu, "Core", 4);
- remove_substring(cpu, "CPU", 3);
truncate_spaces(cpu);
-
return cpu;
}