Skip to content

Commit

Permalink
Show CPU temperature in CPU meter
Browse files Browse the repository at this point in the history
Show the CPU temperature in the CPU meter, like CPU frequency, instead
of using an extra Meter.
  • Loading branch information
cgzones committed Nov 16, 2020
1 parent 309f1d7 commit 1b225cd
Show file tree
Hide file tree
Showing 19 changed files with 220 additions and 23 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/ci.yml
Expand Up @@ -12,11 +12,11 @@ jobs:
- name: Bootstrap
run: ./autogen.sh
- name: Configure
run: ./configure --enable-werror --enable-linux-affinity --disable-taskstats --disable-unicode
run: ./configure --enable-werror --enable-linux-affinity --disable-taskstats --disable-unicode --disable-sensors
- name: Build
run: make -k
- name: Distcheck
run: make distcheck DISTCHECK_CONFIGURE_FLAGS="--enable-werror --enable-linux-affinity --disable-taskstats --disable-unicode"
run: make distcheck DISTCHECK_CONFIGURE_FLAGS="--enable-werror --enable-linux-affinity --disable-taskstats --disable-unicode --disable-sensors"

build-ubuntu-latest-minimal-clang:
runs-on: ubuntu-latest
Expand All @@ -34,26 +34,26 @@ jobs:
- name: Bootstrap
run: ./autogen.sh
- name: Configure
run: ./configure --enable-werror --enable-linux-affinity --disable-taskstats --disable-unicode
run: ./configure --enable-werror --enable-linux-affinity --disable-taskstats --disable-unicode --disable-sensors
- name: Build
run: make -k
- name: Distcheck
run: make distcheck DISTCHECK_CONFIGURE_FLAGS="--enable-werror --enable-linux-affinity --disable-taskstats --disable-unicode"
run: make distcheck DISTCHECK_CONFIGURE_FLAGS="--enable-werror --enable-linux-affinity --disable-taskstats --disable-unicode --disable-sensors"

build-ubuntu-latest-full-featured-gcc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Dependencies
run: sudo apt-get install libncursesw5-dev libhwloc-dev libnl-3-dev libnl-genl-3-dev
run: sudo apt-get install libncursesw5-dev libhwloc-dev libnl-3-dev libnl-genl-3-dev libsensors4-dev
- name: Bootstrap
run: ./autogen.sh
- name: Configure
run: ./configure --enable-werror --enable-openvz --enable-cgroup --enable-vserver --enable-ancient-vserver --enable-taskstats --enable-unicode --enable-hwloc --enable-setuid --enable-delayacct
run: ./configure --enable-werror --enable-openvz --enable-cgroup --enable-vserver --enable-ancient-vserver --enable-taskstats --enable-unicode --enable-hwloc --enable-setuid --enable-delayacct --enable-sensors
- name: Build
run: make -k
- name: Distcheck
run: make distcheck DISTCHECK_CONFIGURE_FLAGS='--enable-werror --enable-openvz --enable-cgroup --enable-vserver --enable-ancient-vserver --enable-taskstats --enable-unicode --enable-hwloc --enable-setuid --enable-delayacct'
run: make distcheck DISTCHECK_CONFIGURE_FLAGS='--enable-werror --enable-openvz --enable-cgroup --enable-vserver --enable-ancient-vserver --enable-taskstats --enable-unicode --enable-hwloc --enable-setuid --enable-delayacct --enable-sensors'

build-ubuntu-latest-full-featured-clang:
runs-on: ubuntu-latest
Expand All @@ -67,15 +67,15 @@ jobs:
sudo add-apt-repository 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-10 main' -y
sudo apt-get update -q
- name: Install Dependencies
run: sudo apt-get install clang-10 libncursesw5-dev libhwloc-dev libnl-3-dev libnl-genl-3-dev
run: sudo apt-get install clang-10 libncursesw5-dev libhwloc-dev libnl-3-dev libnl-genl-3-dev libsensors4-dev
- name: Bootstrap
run: ./autogen.sh
- name: Configure
run: ./configure --enable-werror --enable-openvz --enable-cgroup --enable-vserver --enable-ancient-vserver --enable-taskstats --enable-unicode --enable-hwloc --enable-setuid --enable-delayacct
run: ./configure --enable-werror --enable-openvz --enable-cgroup --enable-vserver --enable-ancient-vserver --enable-taskstats --enable-unicode --enable-hwloc --enable-setuid --enable-delayacct --enable-sensors
- name: Build
run: make -k
- name: Distcheck
run: make distcheck DISTCHECK_CONFIGURE_FLAGS='--enable-werror --enable-openvz --enable-cgroup --enable-vserver --enable-ancient-vserver --enable-taskstats --enable-unicode --enable-hwloc --enable-setuid --enable-delayacct'
run: make distcheck DISTCHECK_CONFIGURE_FLAGS='--enable-werror --enable-openvz --enable-cgroup --enable-vserver --enable-ancient-vserver --enable-taskstats --enable-unicode --enable-hwloc --enable-setuid --enable-delayacct --enable-sensors'

build-ubuntu-latest-clang-analyzer:
runs-on: ubuntu-latest
Expand All @@ -89,11 +89,11 @@ jobs:
sudo add-apt-repository 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-11 main' -y
sudo apt-get update -q
- name: Install Dependencies
run: sudo apt-get install clang-11 clang-tools-11 libncursesw5-dev libhwloc-dev libnl-3-dev libnl-genl-3-dev
run: sudo apt-get install clang-11 clang-tools-11 libncursesw5-dev libhwloc-dev libnl-3-dev libnl-genl-3-dev libsensors4-dev
- name: Bootstrap
run: ./autogen.sh
- name: Configure
run: scan-build-11 -analyze-headers --status-bugs ./configure --enable-debug --enable-werror --enable-openvz --enable-cgroup --enable-vserver --enable-ancient-vserver --enable-taskstats --enable-unicode --enable-hwloc --enable-setuid --enable-delayacct
run: scan-build-11 -analyze-headers --status-bugs ./configure --enable-debug --enable-werror --enable-openvz --enable-cgroup --enable-vserver --enable-ancient-vserver --enable-taskstats --enable-unicode --enable-hwloc --enable-setuid --enable-delayacct --enable-sensors
- name: Build
run: scan-build-11 -analyze-headers --status-bugs make -j"$(nproc)"

Expand Down
55 changes: 45 additions & 10 deletions CPUMeter.c
Expand Up @@ -56,25 +56,44 @@ static void CPUMeter_updateValues(Meter* this, char* buffer, int size) {
return;
}
memset(this->values, 0, sizeof(double) * CPU_METER_ITEMCOUNT);

char cpuUsageBuffer[8] = { 0 };
char cpuFrequencyBuffer[16] = { 0 };
char cpuTemperatureBuffer[16] = { 0 };

double percent = Platform_setCPUValues(this, cpu);

if (this->pl->settings->showCPUUsage) {
xSnprintf(cpuUsageBuffer, sizeof(cpuUsageBuffer), "%5.1f%%", percent);
}

if (this->pl->settings->showCPUFrequency) {
double cpuFrequency = this->values[CPU_METER_FREQUENCY];
char cpuFrequencyBuffer[16];
if (isnan(cpuFrequency)) {
xSnprintf(cpuFrequencyBuffer, sizeof(cpuFrequencyBuffer), "N/A");
} else {
xSnprintf(cpuFrequencyBuffer, sizeof(cpuFrequencyBuffer), "%4uMHz", (unsigned)cpuFrequency);
}
if (this->pl->settings->showCPUUsage) {
xSnprintf(buffer, size, "%5.1f%% %s", percent, cpuFrequencyBuffer);
} else {
xSnprintf(buffer, size, "%s", cpuFrequencyBuffer);
}
} else if (this->pl->settings->showCPUUsage) {
xSnprintf(buffer, size, "%5.1f%%", percent);
} else if (size > 0) {
buffer[0] = '\0';
}

#ifdef HAVE_LIBSENSORS
if (this->pl->settings->showCPUTemperature) {
double cpuTemperature = this->values[CPU_METER_TEMPERATURE];
if (isnan(cpuTemperature))
xSnprintf(cpuTemperatureBuffer, sizeof(cpuTemperatureBuffer), "N/A");
else if (this->pl->settings->degreeFahrenheit)
xSnprintf(cpuTemperatureBuffer, sizeof(cpuTemperatureBuffer), "%3d%sF", (int)(cpuTemperature * 9 / 5 + 32), CRT_degreeSign);
else
xSnprintf(cpuTemperatureBuffer, sizeof(cpuTemperatureBuffer), "%d%sC", (int)cpuTemperature, CRT_degreeSign);
}
#endif

xSnprintf(buffer, size, "%s%s%s%s%s",
cpuUsageBuffer,
(cpuUsageBuffer[0] && (cpuFrequencyBuffer[0] || cpuTemperatureBuffer[0])) ? " " : "",
cpuFrequencyBuffer,
(cpuFrequencyBuffer[0] && cpuTemperatureBuffer[0]) ? " " : "",
cpuTemperatureBuffer);
}

static void CPUMeter_display(const Object* cast, RichString* out) {
Expand Down Expand Up @@ -127,6 +146,22 @@ static void CPUMeter_display(const Object* cast, RichString* out) {
RichString_append(out, CRT_colors[CPU_GUEST], buffer);
}
}

#ifdef HAVE_LIBSENSORS
if (this->pl->settings->showCPUTemperature) {
char cpuTemperatureBuffer[10];
double cpuTemperature = this->values[CPU_METER_TEMPERATURE];
if (isnan(cpuTemperature)) {
xSnprintf(cpuTemperatureBuffer, sizeof(cpuTemperatureBuffer), "N/A");
} else if (this->pl->settings->degreeFahrenheit) {
xSnprintf(cpuTemperatureBuffer, sizeof(cpuTemperatureBuffer), "%5.1f%sF", cpuTemperature * 9 / 5 + 32, CRT_degreeSign);
} else {
xSnprintf(cpuTemperatureBuffer, sizeof(cpuTemperatureBuffer), "%5.1f%sC", cpuTemperature, CRT_degreeSign);
}
RichString_append(out, CRT_colors[METER_TEXT], "temp:");
RichString_append(out, CRT_colors[METER_VALUE], cpuTemperatureBuffer);
}
#endif
}

static void AllCPUsMeter_getRange(Meter* this, int* start, int* count) {
Expand Down
3 changes: 2 additions & 1 deletion CPUMeter.h
Expand Up @@ -19,7 +19,8 @@ typedef enum {
CPU_METER_GUEST = 6,
CPU_METER_IOWAIT = 7,
CPU_METER_FREQUENCY = 8,
CPU_METER_ITEMCOUNT = 9, // number of entries in this enum
CPU_METER_TEMPERATURE = 9,
CPU_METER_ITEMCOUNT = 10, // number of entries in this enum
} CPUMeterValues;

extern const MeterClass CPUMeter_class;
Expand Down
19 changes: 19 additions & 0 deletions CRT.c
Expand Up @@ -74,6 +74,23 @@ const char* const* CRT_treeStr = CRT_treeStrAscii;

int CRT_delay;

const char* CRT_degreeSign;

static const char* initDegreeSign(void) {
#ifdef HAVE_LIBNCURSESW
if (CRT_utf8)
return "\xc2\xb0";
#endif

static char buffer[4];
// this might fail if the current locale does not support wide characters
int r = snprintf(buffer, sizeof(buffer), "%lc", 176);
if (r > 0)
return buffer;

return "";
}

const int* CRT_colors;

int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
Expand Down Expand Up @@ -718,6 +735,8 @@ void CRT_init(int delay, int colorScheme, bool allowUnicode) {
if (CRT_pageSize == -1)
CRT_fatalError("Fatal error: Can not get PAGE_SIZE by sysconf(_SC_PAGESIZE)");
CRT_pageSizeKB = CRT_pageSize / 1024;

CRT_degreeSign = initDegreeSign();
}

void CRT_done() {
Expand Down
1 change: 1 addition & 0 deletions CRT.h
Expand Up @@ -129,6 +129,7 @@ void CRT_handleSIGSEGV(int signal) ATTR_NORETURN;
#define KEY_RECLICK KEY_F(22)
#define KEY_ALT(x) (KEY_F(64 - 26) + ((x) - 'A'))

extern const char* CRT_degreeSign;

#ifdef HAVE_LIBNCURSESW

Expand Down
4 changes: 4 additions & 0 deletions DisplayOptionsPanel.c
Expand Up @@ -92,6 +92,10 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager*
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 percentage numerically"), &(settings->showCPUUsage)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Also show CPU frequency"), &(settings->showCPUFrequency)));
#ifdef HAVE_LIBSENSORS
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Also show CPU temperature"), &(settings->showCPUTemperature)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Show temperature in degree Fahrenheit"), &(settings->degreeFahrenheit)));
#endif
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Enable the mouse"), &(settings->enableMouse)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Highlight new and old processes"), &(settings->highlightChanges)));
#ifdef HAVE_LIBHWLOC
Expand Down
14 changes: 14 additions & 0 deletions Settings.c
Expand Up @@ -177,6 +177,12 @@ static bool Settings_read(Settings* this, const char* fileName, int initialCpuCo
this->showCPUUsage = atoi(option[1]);
} else if (String_eq(option[0], "show_cpu_frequency")) {
this->showCPUFrequency = atoi(option[1]);
#ifdef HAVE_LIBSENSORS
} else if (String_eq(option[0], "show_cpu_temperature")) {
this->showCPUTemperature = atoi(option[1]);
} else if (String_eq(option[0], "degree_fahrenheit")) {
this->degreeFahrenheit = atoi(option[1]);
#endif
} 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")) {
Expand Down Expand Up @@ -277,6 +283,10 @@ bool Settings_write(Settings* this) {
fprintf(fd, "cpu_count_from_one=%d\n", (int) this->countCPUsFromOne);
fprintf(fd, "show_cpu_usage=%d\n", (int) this->showCPUUsage);
fprintf(fd, "show_cpu_frequency=%d\n", (int) this->showCPUFrequency);
#ifdef HAVE_LIBSENSORS
fprintf(fd, "show_cpu_temperature=%d\n", (int) this->showCPUTemperature);
fprintf(fd, "degree_fahrenheit=%d\n", (int) this->degreeFahrenheit);
#endif
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);
Expand Down Expand Up @@ -309,6 +319,10 @@ Settings* Settings_new(int initialCpuCount) {
this->countCPUsFromOne = false;
this->showCPUUsage = true;
this->showCPUFrequency = false;
#ifdef HAVE_LIBSENSORS
this->showCPUTemperature = false;
this->degreeFahrenheit = false;
#endif
this->updateProcessNames = false;
this->showProgramPath = true;
this->highlightThreads = true;
Expand Down
4 changes: 4 additions & 0 deletions Settings.h
Expand Up @@ -38,6 +38,10 @@ typedef struct Settings_ {
bool detailedCPUTime;
bool showCPUUsage;
bool showCPUFrequency;
#ifdef HAVE_LIBSENSORS
bool showCPUTemperature;
bool degreeFahrenheit;
#endif
bool treeView;
bool showProgramPath;
bool shadowOtherUsers;
Expand Down
11 changes: 11 additions & 0 deletions configure.ac
Expand Up @@ -308,6 +308,16 @@ then
])
fi

AC_ARG_ENABLE(sensors, [AS_HELP_STRING([--enable-sensors], [enable libsensors support for reading temperature data.])],, enable_sensors="check")
if test "x$enable_sensors" = xyes; then
AC_CHECK_LIB([sensors], [sensors_get_value], [], [missing_libraries="$missing_libraries libsensors"])
AC_CHECK_HEADERS([sensors/sensors.h], [], [missing_headers="$missing_headers $ac_header"])
elif test "x$enable_sensors" = xcheck; then
enable_sensors=yes
AC_CHECK_LIB([sensors], [sensors_get_value], [], [enable_sensors=no])
AC_CHECK_HEADERS([sensors/sensors.h], [], [enable_sensors=no])
fi

AM_CFLAGS="\
-Wall\
-Wcast-align\
Expand Down Expand Up @@ -386,6 +396,7 @@ AC_MSG_RESULT([
(Linux) taskstats: $enable_taskstats
(Linux) affinity: $enable_linux_affinity
(Linux) delay accounting: $enable_delayacct
(Linux) sensors: $enable_sensors
unicode: $enable_unicode
hwloc: $enable_hwloc
setuid: $enable_setuid
Expand Down
1 change: 1 addition & 0 deletions darwin/Platform.c
Expand Up @@ -224,6 +224,7 @@ double Platform_setCPUValues(Meter* mtr, int cpu) {
total = mtr->values[CPU_METER_NICE] + mtr->values[CPU_METER_NORMAL] + mtr->values[CPU_METER_KERNEL];

mtr->values[CPU_METER_FREQUENCY] = NAN;
mtr->values[CPU_METER_TEMPERATURE] = NAN;

return CLAMP(total, 0.0, 100.0);
}
Expand Down
1 change: 1 addition & 0 deletions dragonflybsd/Platform.c
Expand Up @@ -179,6 +179,7 @@ double Platform_setCPUValues(Meter* this, int cpu) {
percent = isnan(percent) ? 0.0 : CLAMP(percent, 0.0, 100.0);

v[CPU_METER_FREQUENCY] = NAN;
v[CPU_METER_TEMPERATURE] = NAN;

return percent;
}
Expand Down
1 change: 1 addition & 0 deletions freebsd/Platform.c
Expand Up @@ -190,6 +190,7 @@ double Platform_setCPUValues(Meter* this, int cpu) {
percent = CLAMP(percent, 0.0, 100.0);

v[CPU_METER_FREQUENCY] = NAN;
v[CPU_METER_TEMPERATURE] = NAN;

return percent;
}
Expand Down
12 changes: 12 additions & 0 deletions htop.c
Expand Up @@ -36,6 +36,10 @@ in the source distribution for its full text.
#include "XUtils.h"


#ifdef HAVE_LIBSENSORS
#include <sensors/sensors.h>
#endif

//#link m

static void printVersionFlag(void) {
Expand Down Expand Up @@ -317,6 +321,10 @@ int main(int argc, char** argv) {

CRT_init(settings->delay, settings->colorScheme, flags.allowUnicode);

#ifdef HAVE_LIBSENSORS
sensors_init(NULL);
#endif

MainPanel* panel = MainPanel_new();
ProcessList_setPanel(pl, (Panel*) panel);

Expand Down Expand Up @@ -357,6 +365,10 @@ int main(int argc, char** argv) {
attroff(CRT_colors[RESET_COLOR]);
refresh();

#ifdef HAVE_LIBSENSORS
sensors_cleanup();
#endif

CRT_done();
if (settings->changed)
Settings_write(settings);
Expand Down

0 comments on commit 1b225cd

Please sign in to comment.