commit ad0b6685f5d1229e896e8fa5da5d849a410a6b68
parent 4f685ba862b3148f0cf3d4c686e86ae992adb03d
Author: dwzg <dennis@wtzg.de>
Date: Sun, 26 Apr 2020 00:56:32 +0200
Add fallback method for CPU frequency
Diffstat:
| M | paleofetch.c | | | 45 | ++++++++++++++++++++++++++++++--------------- |
1 file changed, 30 insertions(+), 15 deletions(-)
diff --git a/paleofetch.c b/paleofetch.c
@@ -350,28 +350,43 @@ char *get_cpu() {
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, "%d", &cpu_freq);
- cpu_freq /= 1000; // convert kHz to MHz
- freq = cpu_freq / 1000.0; // convert MHz to GHz and cast to double
- while (cpu_freq % 10 == 0) {
- --prec;
- cpu_freq /= 10;
+
+ if (cpufreq != NULL) {
+
+ if (getline(&line, &len, cpufreq) != -1) {
+ sscanf(line, "%d", &cpu_freq);
+ cpu_freq /= 1000; // convert kHz to MHz
+ } else {
+ fclose(cpufreq);
+ free(line);
+ goto cpufreq_fallback;
}
- if (prec == 0) prec = 1; // we don't want zero decimal places
} else {
- freq = 0.0; // cpuinfo_max_freq not available?
+cpufreq_fallback:
+ cpufreq = fopen("/proc/cpuinfo", "r"); /* read from cpu info */
+ if(cpuinfo == NULL) {
+ status = -1;
+ halt_and_catch_fire("Unable to open cpuinfo");
+ }
+
+ while (getline(&line, &len, cpufreq) != -1) {
+ if (sscanf(line, "cpu MHz : %lf", &freq) > 0) break;
+ }
+
+ cpu_freq = (int) freq;
}
-
+
free(line);
fclose(cpufreq);
+
+ freq = cpu_freq / 1000.0; // convert MHz to GHz and cast to double
+ while (cpu_freq % 10 == 0) {
+ --prec;
+ cpu_freq /= 1;
+ }
+ if (prec == 0) prec = 1; // we don't want zero decimal places
/* remove unneeded information */
for (int i = 0; i < COUNT(cpu_remove); ++i) {