summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnavion <arnavion@gmail.com>2019-08-09 21:34:48 -0700
committerArnavion <arnavion@gmail.com>2019-08-09 21:34:48 -0700
commit4b0600d8f8f1f6aaae6fc2e59e8697ff8779cf64 (patch)
treedb0ed0d99d936058105a431371a3137a5f87dd40
parent402e46bb82964366746b86d77eb5afa69c279539 (diff)
Add new display option to also show CPU frequency in CPU meters.
The option is only implemented on Linux. On other platforms, and on Linuxes that do not expose the relevant sysfs file, the frequency will be 0. The "CPU average" meter does not show a frequency, only the individual per-CPU meters.
-rw-r--r--CPUMeter.c20
-rw-r--r--CPUMeter.h3
-rw-r--r--DisplayOptionsPanel.c1
-rw-r--r--Settings.c5
-rw-r--r--Settings.h1
-rw-r--r--linux/Platform.c16
6 files changed, 43 insertions, 3 deletions
diff --git a/CPUMeter.c b/CPUMeter.c
index de5490df..6f9419b5 100644
--- a/CPUMeter.c
+++ b/CPUMeter.c
@@ -28,7 +28,8 @@ typedef enum {
CPU_METER_STEAL = 5,
CPU_METER_GUEST = 6,
CPU_METER_IOWAIT = 7,
- CPU_METER_ITEMCOUNT = 8, // number of entries in this enum
+ CPU_METER_FREQUENCY = 8,
+ CPU_METER_ITEMCOUNT = 9, // number of entries in this enum
} CPUMeterValues;
}*/
@@ -63,7 +64,22 @@ static void CPUMeter_updateValues(Meter* this, char* buffer, int size) {
}
memset(this->values, 0, sizeof(double) * CPU_METER_ITEMCOUNT);
double percent = Platform_setCPUValues(this, cpu);
- xSnprintf(buffer, size, "%5.1f%%", percent);
+ if (cpu != 0 && this->pl->settings->showCPUFrequency) {
+ /* Initial frequency is in KHz. Divide it by 1024 till it's less than 1024, and emit unit accordingly */
+ double cpuFrequency = this->values[CPU_METER_FREQUENCY];
+ char unit = 'K';
+ if (cpuFrequency > 1024) {
+ cpuFrequency /= 1024;
+ unit = 'M';
+ }
+ if (cpuFrequency > 1024) {
+ cpuFrequency /= 1024;
+ unit = 'G';
+ }
+ xSnprintf(buffer, size, "%5.1f%% %.1f%cHz", percent, cpuFrequency, unit);
+ } else {
+ xSnprintf(buffer, size, "%5.1f%%", percent);
+ }
}
static void CPUMeter_display(Object* cast, RichString* out) {
diff --git a/CPUMeter.h b/CPUMeter.h
index 2f163968..6f8599a8 100644
--- a/CPUMeter.h
+++ b/CPUMeter.h
@@ -20,7 +20,8 @@ typedef enum {
CPU_METER_STEAL = 5,
CPU_METER_GUEST = 6,
CPU_METER_IOWAIT = 7,
- CPU_METER_ITEMCOUNT = 8, // number of entries in this enum
+ CPU_METER_FREQUENCY = 8,
+ CPU_METER_ITEMCOUNT = 9, // number of entries in this enum
} CPUMeterValues;
diff --git a/DisplayOptionsPanel.c b/DisplayOptionsPanel.c
index 0ff54e33..0c741684 100644
--- a/DisplayOptionsPanel.c
+++ b/DisplayOptionsPanel.c
@@ -97,5 +97,6 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager*
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Count CPUs from 0 instead of 1"), &(settings->countCPUsFromZero)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Update process names on every refresh"), &(settings->updateProcessNames)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Add guest time in CPU meter percentage"), &(settings->accountGuestInCPUMeter)));
+ Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Also show CPU frequency"), &(settings->showCPUFrequency)));
return this;
}
diff --git a/Settings.c b/Settings.c
index db2fa066..a268ff4d 100644
--- a/Settings.c
+++ b/Settings.c
@@ -45,6 +45,7 @@ typedef struct Settings_ {
bool countCPUsFromZero;
bool detailedCPUTime;
+ bool showCPUFrequency;
bool treeView;
bool showProgramPath;
bool hideThreads;
@@ -223,6 +224,8 @@ static bool Settings_read(Settings* this, const char* fileName) {
this->detailedCPUTime = atoi(option[1]);
} else if (String_eq(option[0], "cpu_count_from_zero")) {
this->countCPUsFromZero = atoi(option[1]);
+ } else if (String_eq(option[0], "show_cpu_frequency")) {
+ this->showCPUFrequency = atoi(option[1]);
} else if (String_eq(option[0], "update_process_names")) {
this->updateProcessNames = atoi(option[1]);
} else if (String_eq(option[0], "account_guest_in_cpu_meter")) {
@@ -312,6 +315,7 @@ bool Settings_write(Settings* this) {
fprintf(fd, "header_margin=%d\n", (int) this->headerMargin);
fprintf(fd, "detailed_cpu_time=%d\n", (int) this->detailedCPUTime);
fprintf(fd, "cpu_count_from_zero=%d\n", (int) this->countCPUsFromZero);
+ fprintf(fd, "show_cpu_frequency=%d\n", (int) this->showCPUFrequency);
fprintf(fd, "update_process_names=%d\n", (int) this->updateProcessNames);
fprintf(fd, "account_guest_in_cpu_meter=%d\n", (int) this->accountGuestInCPUMeter);
fprintf(fd, "color_scheme=%d\n", (int) this->colorScheme);
@@ -340,6 +344,7 @@ Settings* Settings_new(int cpuCount) {
this->highlightMegabytes = false;
this->detailedCPUTime = false;
this->countCPUsFromZero = false;
+ this->showCPUFrequency = false;
this->updateProcessNames = false;
this->cpuCount = cpuCount;
this->showProgramPath = true;
diff --git a/Settings.h b/Settings.h
index d9dc0683..54b15fce 100644
--- a/Settings.h
+++ b/Settings.h
@@ -36,6 +36,7 @@ typedef struct Settings_ {
bool countCPUsFromZero;
bool detailedCPUTime;
+ bool showCPUFrequency;
bool treeView;
bool showProgramPath;
bool hideThreads;
diff --git a/linux/Platform.c b/linux/Platform.c
index ab90ca74..e92d0c51 100644
--- a/linux/Platform.c
+++ b/linux/Platform.c
@@ -192,6 +192,22 @@ double Platform_setCPUValues(Meter* this, int cpu) {
}
percent = CLAMP(percent, 0.0, 100.0);
if (isnan(percent)) percent = 0.0;
+
+ v[CPU_METER_FREQUENCY] = 0;
+ if (this->pl->settings->showCPUFrequency) {
+ char filename[63+1];
+ xSnprintf(filename, 63, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", cpu - 1);
+ FILE* fd = fopen(filename, "r");
+ if (fd) {
+ unsigned int cpuFrequency;
+ int n = fscanf(fd, "%u", &cpuFrequency);
+ fclose(fd);
+ if (n > 0) {
+ v[CPU_METER_FREQUENCY] = cpuFrequency;
+ }
+ }
+ }
+
return percent;
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy