commit d23cf6a32033b00128f0aab14166cf46e168194e
parent 19369f8e00efa6fc74132bb37ae3108b9798afa2
Author: sam-barr <samfbarr@outlook.com>
Date: Wed, 29 Apr 2020 08:50:50 -0500
Merge pull request #51 from dwzg/fallback_cpufreq
Add fallback method for CPU frequency
Diffstat:
| M | paleofetch.c | | | 44 | ++++++++++++++++++++++++++++---------------- |
1 file changed, 28 insertions(+), 16 deletions(-)
diff --git a/paleofetch.c b/paleofetch.c
@@ -352,29 +352,41 @@ static char *get_cpu() {
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, "%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 (cpufreq == 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 /= 10;
+ }
+ if (prec == 0) prec = 1; // we don't want zero decimal places
/* remove unneeded information */
for (int i = 0; i < COUNT(cpu_remove); ++i) {