Last active
August 29, 2015 14:12
-
-
Save vafrederico/c427af1f253fb29ad04c to your computer and use it in GitHub Desktop.
Adds a CPU Frequency meter to htop. After patching, run ./autogen.sh before configuring and compiling.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff -rupN htop-1.0.2/CPUFreqMeter.c htop-1.0.2-new/CPUFreqMeter.c | |
--- htop-1.0.2/CPUFreqMeter.c 1969-12-31 21:00:00.000000000 -0300 | |
+++ htop-1.0.2-new/CPUFreqMeter.c 2015-01-04 18:08:24.678220500 -0200 | |
@@ -0,0 +1,115 @@ | |
+/* | |
+htop - CPUFreqMeter.c | |
+(C) 2015 Vinicius Frederico | |
+Released under the GNU GPL, see the COPYING file | |
+in the source distribution for its full text. | |
+*/ | |
+ | |
+#include "CPUFreqMeter.h" | |
+ | |
+#include "ProcessList.h" | |
+#include "CRT.h" | |
+ | |
+#define _GNU_SOURCE | |
+#include <stdio.h> | |
+#include <stdlib.h> | |
+#include <string.h> | |
+#include <limits.h> | |
+ | |
+/*{ | |
+#include "Meter.h" | |
+}*/ | |
+ | |
+ | |
+static void CPUFreqMeter_setValues(Meter* this, char* buffer, int lenBuffer) { | |
+ | |
+ FILE* fd = fopen(PROCDIR "/cpuinfo", "r"); | |
+ if(fd == NULL) return 1; | |
+ | |
+ size_t read, len; | |
+ char *line = NULL; | |
+ char *entry = NULL; | |
+ char *tstart = NULL, *tend = NULL; | |
+ int cpufreq; | |
+ | |
+ sprintf(buffer, ""); | |
+ while ((read = getline(&line, &len, fd)) != -1) { | |
+ // this line contains cpu frequency | |
+ entry = strstr(line, "cpu MHz"); | |
+ if (entry == NULL) continue; | |
+ | |
+ // find the begin of the cpu frequency | |
+ tstart = strchr(entry, ':'); | |
+ if (tstart == NULL) continue; | |
+ tstart += 2; // jump over the ": " | |
+ | |
+ // find the end of the cpu frequency. | |
+ tend = strchr(tstart, '.'); // just the integer | |
+ if (tend == NULL) continue; | |
+ | |
+ // convert the string into an integer, this is necessary for further steps | |
+ cpufreq = strtol(tstart, &tend, 10); | |
+ if (cpufreq == LONG_MAX || cpufreq == LONG_MIN) continue; | |
+ if (tstart == tend) continue; | |
+ | |
+ sprintf(buffer, "%s%d MHz ", buffer, cpufreq); | |
+ } | |
+ | |
+ free(line); | |
+ fclose(fd); | |
+} | |
+ | |
+static void CPUFreqMeter_display(Object* cast, RichString* out) { | |
+ | |
+ FILE* fd = fopen(PROCDIR "/cpuinfo", "r"); | |
+ if(fd == NULL) return 1; | |
+ | |
+ int textColor = CRT_colors[METER_TEXT]; | |
+ int cpuFreqColor = CRT_colors[METER_VALUE]; | |
+ | |
+ size_t read, len; | |
+ char *line = NULL; | |
+ char *entry = NULL; | |
+ char *tstart = NULL, *tend = NULL; | |
+ int cpufreq; | |
+ while ((read = getline(&line, &len, fd)) != -1) { | |
+ // this line contains cpu frequency | |
+ entry = strstr(line, "cpu MHz"); | |
+ if (entry == NULL) continue; | |
+ | |
+ // find the begin of the cpu frequency | |
+ tstart = strchr(entry, ':'); | |
+ if (tstart == NULL) continue; | |
+ tstart += 2; // jump over the ": " | |
+ | |
+ // find the end of the cpu frequency. | |
+ tend = strchr(tstart, '.'); // just the integer | |
+ if (tend == NULL) continue; | |
+ | |
+ // convert the string into an integer, this is necessary for further steps | |
+ cpufreq = strtol(tstart, &tend, 10); | |
+ if (cpufreq == LONG_MAX || cpufreq == LONG_MIN) continue; | |
+ if (tstart == tend) continue; | |
+ | |
+ // output the cpu frequency | |
+ char buffer[20]; | |
+ sprintf(buffer, "%d", cpufreq); | |
+ RichString_append(out, cpuFreqColor, buffer); | |
+ RichString_append(out, textColor, " MHz "); | |
+ } | |
+ | |
+ free(line); | |
+ fclose(fd); | |
+} | |
+ | |
+MeterType CPUFreqMeter = { | |
+ .setValues = CPUFreqMeter_setValues, | |
+ .display = CPUFreqMeter_display, | |
+ .mode = TEXT_METERMODE, | |
+ .items = 1, | |
+ .total = 100.0, | |
+ .attributes = NULL, | |
+ .name = "CPUFrequency", | |
+ .uiName = "CPU Frequency", | |
+ .caption = "CPU Frequency: " | |
+}; | |
diff -rupN htop-1.0.2/Header.c htop-1.0.2-new/Header.c | |
--- htop-1.0.2/Header.c 2011-12-26 19:52:26.000000000 -0200 | |
+++ htop-1.0.2-new/Header.c 2015-01-04 18:08:24.678978900 -0200 | |
@@ -14,6 +14,7 @@ in the source distribution for its full | |
#include "TasksMeter.h" | |
#include "LoadAverageMeter.h" | |
#include "UptimeMeter.h" | |
+#include "CPUFreqMeter.h" | |
#include "BatteryMeter.h" | |
#include "ClockMeter.h" | |
#include "HostnameMeter.h" | |
diff -rupN htop-1.0.2/Makefile.am htop-1.0.2-new/Makefile.am | |
--- htop-1.0.2/Makefile.am 2012-10-03 17:18:30.000000000 -0300 | |
+++ htop-1.0.2-new/Makefile.am 2015-01-04 18:08:55.035839600 -0200 | |
@@ -22,6 +22,8 @@ IOPriorityPanel.c SignalsPanel.c String. | |
UptimeMeter.c UsersTable.c Vector.c AvailableColumnsPanel.c AffinityPanel.c \ | |
HostnameMeter.c OpenFilesScreen.c Affinity.c IOPriority.c | |
+myhtopsources += CPUFreqMeter.c | |
+ | |
myhtopheaders = AvailableColumnsPanel.h AvailableMetersPanel.h \ | |
CategoriesPanel.h CheckItem.h ClockMeter.h ColorsPanel.h ColumnsPanel.h \ | |
IOPriorityPanel.h CPUMeter.h CRT.h DisplayOptionsPanel.h FunctionBar.h \ | |
@@ -31,6 +33,8 @@ ScreenManager.h Settings.h SignalsPanel. | |
SwapMeter.h TasksMeter.h TraceScreen.h UptimeMeter.h UsersTable.h Vector.h \ | |
Process.h AffinityPanel.h HostnameMeter.h OpenFilesScreen.h Affinity.h IOPriority.h | |
+myhtopheaders += CPUFreqMeter.h | |
+ | |
SUFFIXES = .h | |
BUILT_SOURCES = $(myhtopheaders) | |
diff -rupN htop-1.0.2/Meter.c htop-1.0.2-new/Meter.c | |
--- htop-1.0.2/Meter.c 2012-02-02 21:06:38.000000000 -0200 | |
+++ htop-1.0.2-new/Meter.c 2015-01-04 18:08:24.680595200 -0200 | |
@@ -13,6 +13,7 @@ in the source distribution for its full | |
#include "TasksMeter.h" | |
#include "LoadAverageMeter.h" | |
#include "UptimeMeter.h" | |
+#include "CPUFreqMeter.h" | |
#include "BatteryMeter.h" | |
#include "ClockMeter.h" | |
#include "HostnameMeter.h" | |
@@ -127,6 +128,7 @@ MeterType* Meter_types[] = { | |
&SwapMeter, | |
&TasksMeter, | |
&UptimeMeter, | |
+ &CPUFreqMeter, | |
&BatteryMeter, | |
&HostnameMeter, | |
&AllCPUsMeter, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment