From 37e186fd6626e83f51d0267052677355575964d4 Mon Sep 17 00:00:00 2001 From: David Zarzycki Date: Thu, 7 Jan 2021 08:38:18 -0500 Subject: [PATCH] Linux: Add SwapCached to the swap meter According to the Linux kernel documentation, "SwapCached" tracks "memory that once was swapped out, is swapped back in but still also is in the swapfile (if memory is needed it doesn't need to be swapped out AGAIN because it is already in the swapfile. This saves I/O)." --- Action.c | 6 ++++++ CRT.c | 6 ++++++ CRT.h | 1 + ProcessList.h | 3 ++- SwapMeter.c | 11 +++++++++-- dragonflybsd/Platform.c | 1 + freebsd/Platform.c | 1 + linux/LinuxProcessList.c | 3 ++- linux/Platform.c | 1 + openbsd/Platform.c | 1 + solaris/Platform.c | 1 + 11 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Action.c b/Action.c index 578882db..0b5c8990 100644 --- a/Action.c +++ b/Action.c @@ -528,7 +528,13 @@ static Htop_Reaction actionHelp(State* st) { mvaddstr(line++, 0, "Swap bar: "); addattrstr(CRT_colors[BAR_BORDER], "["); addattrstr(CRT_colors[SWAP], "used"); +#ifdef HTOP_LINUX + addattrstr(CRT_colors[BAR_SHADOW], "/"); + addattrstr(CRT_colors[SWAP_CACHE], "cache"); + addattrstr(CRT_colors[BAR_SHADOW], " used/total"); +#else addattrstr(CRT_colors[BAR_SHADOW], " used/total"); +#endif addattrstr(CRT_colors[BAR_BORDER], "]"); attrset(CRT_colors[DEFAULT_COLOR]); mvaddstr(line++, 0, "Type and layout of header meters are configurable in the setup screen."); diff --git a/CRT.c b/CRT.c index 85f29626..150e27b5 100644 --- a/CRT.c +++ b/CRT.c @@ -144,6 +144,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = { [BAR_BORDER] = A_BOLD, [BAR_SHADOW] = A_BOLD | ColorPairGrayBlack, [SWAP] = ColorPair(Red, Black), + [SWAP_CACHE] = ColorPair(Yellow, Black), [GRAPH_1] = A_BOLD | ColorPair(Cyan, Black), [GRAPH_2] = ColorPair(Cyan, Black), [MEMORY_USED] = ColorPair(Green, Black), @@ -229,6 +230,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = { [BAR_BORDER] = A_BOLD, [BAR_SHADOW] = A_DIM, [SWAP] = A_BOLD, + [SWAP_CACHE] = A_NORMAL, [GRAPH_1] = A_BOLD, [GRAPH_2] = A_NORMAL, [MEMORY_USED] = A_BOLD, @@ -314,6 +316,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = { [BAR_BORDER] = ColorPair(Blue, White), [BAR_SHADOW] = ColorPair(Black, White), [SWAP] = ColorPair(Red, White), + [SWAP_CACHE] = ColorPair(Yellow, White), [GRAPH_1] = A_BOLD | ColorPair(Blue, White), [GRAPH_2] = ColorPair(Blue, White), [MEMORY_USED] = ColorPair(Green, White), @@ -399,6 +402,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = { [BAR_BORDER] = ColorPair(Blue, Black), [BAR_SHADOW] = ColorPairGrayBlack, [SWAP] = ColorPair(Red, Black), + [SWAP_CACHE] = ColorPair(Yellow, Black), [GRAPH_1] = A_BOLD | ColorPair(Cyan, Black), [GRAPH_2] = ColorPair(Cyan, Black), [MEMORY_USED] = ColorPair(Green, Black), @@ -484,6 +488,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = { [BAR_BORDER] = A_BOLD | ColorPair(Yellow, Blue), [BAR_SHADOW] = ColorPair(Cyan, Blue), [SWAP] = ColorPair(Red, Blue), + [SWAP_CACHE] = A_BOLD | ColorPair(Yellow, Blue), [GRAPH_1] = A_BOLD | ColorPair(Cyan, Blue), [GRAPH_2] = ColorPair(Cyan, Blue), [MEMORY_USED] = A_BOLD | ColorPair(Green, Blue), @@ -569,6 +574,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = { [BAR_BORDER] = A_BOLD | ColorPair(Green, Black), [BAR_SHADOW] = ColorPair(Cyan, Black), [SWAP] = ColorPair(Red, Black), + [SWAP_CACHE] = ColorPair(Yellow, Black), [GRAPH_1] = A_BOLD | ColorPair(Green, Black), [GRAPH_2] = ColorPair(Green, Black), [MEMORY_USED] = ColorPair(Green, Black), diff --git a/CRT.h b/CRT.h index 4806994c..d54ac121 100644 --- a/CRT.h +++ b/CRT.h @@ -65,6 +65,7 @@ typedef enum ColorElements_ { BATTERY, TASKS_RUNNING, SWAP, + SWAP_CACHE, PROCESS, PROCESS_SHADOW, PROCESS_TAG, diff --git a/ProcessList.h b/ProcessList.h index a2e17a16..cb17f0d7 100644 --- a/ProcessList.h +++ b/ProcessList.h @@ -65,9 +65,10 @@ typedef struct ProcessList_ { unsigned long long int usedMem; unsigned long long int buffersMem; unsigned long long int cachedMem; + unsigned long long int totalSwap; unsigned long long int usedSwap; - unsigned long long int freeSwap; + unsigned long long int cachedSwap; int cpuCount; diff --git a/SwapMeter.c b/SwapMeter.c index e39cfd9f..81f006ec 100644 --- a/SwapMeter.c +++ b/SwapMeter.c @@ -14,7 +14,8 @@ in the source distribution for its full text. static const int SwapMeter_attributes[] = { - SWAP + SWAP, + SWAP_CACHE }; static void SwapMeter_updateValues(Meter* this, char* buffer, size_t size) { @@ -38,6 +39,12 @@ static void SwapMeter_display(const Object* cast, RichString* out) { Meter_humanUnit(buffer, this->values[0], sizeof(buffer)); RichString_appendAscii(out, CRT_colors[METER_TEXT], " used:"); RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer); + +#ifdef HTOP_LINUX + Meter_humanUnit(buffer, this->values[1], sizeof(buffer)); + RichString_appendAscii(out, CRT_colors[METER_TEXT], " cache:"); + RichString_appendAscii(out, CRT_colors[SWAP_CACHE], buffer); +#endif } const MeterClass SwapMeter_class = { @@ -48,7 +55,7 @@ const MeterClass SwapMeter_class = { }, .updateValues = SwapMeter_updateValues, .defaultMode = BAR_METERMODE, - .maxItems = 1, + .maxItems = 2, .total = 100.0, .attributes = SwapMeter_attributes, .name = "Swap", diff --git a/dragonflybsd/Platform.c b/dragonflybsd/Platform.c index 8ce216c1..2abf4de3 100644 --- a/dragonflybsd/Platform.c +++ b/dragonflybsd/Platform.c @@ -205,6 +205,7 @@ void Platform_setSwapValues(Meter* this) { const ProcessList* pl = this->pl; this->total = pl->totalSwap; this->values[0] = pl->usedSwap; + this->values[1] = NAN; } char* Platform_getProcessEnv(pid_t pid) { diff --git a/freebsd/Platform.c b/freebsd/Platform.c index c74ab6da..82dd8a21 100644 --- a/freebsd/Platform.c +++ b/freebsd/Platform.c @@ -225,6 +225,7 @@ void Platform_setSwapValues(Meter* this) { const ProcessList* pl = this->pl; this->total = pl->totalSwap; this->values[0] = pl->usedSwap; + this->values[1] = NAN; } void Platform_setZfsArcValues(Meter* this) { diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index a8514e28..49b2d0e1 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -1508,6 +1508,7 @@ static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) { switch (buffer[1]) { case 'w': tryRead("SwapTotal:", &this->totalSwap); + tryRead("SwapCached:", &this->cachedSwap); tryRead("SwapFree:", &swapFree); break; case 'h': @@ -1524,7 +1525,7 @@ static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) { this->usedMem = this->totalMem - freeMem; this->cachedMem = this->cachedMem + sreclaimable - shmem; - this->usedSwap = this->totalSwap - swapFree; + this->usedSwap = this->totalSwap - swapFree - this->cachedSwap; fclose(file); } diff --git a/linux/Platform.c b/linux/Platform.c index 23163212..4b17bd01 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -301,6 +301,7 @@ void Platform_setSwapValues(Meter* this) { const ProcessList* pl = this->pl; this->total = pl->totalSwap; this->values[0] = pl->usedSwap; + this->values[1] = pl->cachedSwap; } void Platform_setZramValues(Meter* this) { diff --git a/openbsd/Platform.c b/openbsd/Platform.c index 8ee81411..e3a14894 100644 --- a/openbsd/Platform.c +++ b/openbsd/Platform.c @@ -213,6 +213,7 @@ void Platform_setSwapValues(Meter* this) { const ProcessList* pl = this->pl; this->total = pl->totalSwap; this->values[0] = pl->usedSwap; + this->values[1] = NAN; } char* Platform_getProcessEnv(pid_t pid) { diff --git a/solaris/Platform.c b/solaris/Platform.c index 968e0133..45f16238 100644 --- a/solaris/Platform.c +++ b/solaris/Platform.c @@ -223,6 +223,7 @@ void Platform_setSwapValues(Meter* this) { const ProcessList* pl = this->pl; this->total = pl->totalSwap; this->values[0] = pl->usedSwap; + this->values[1] = NAN; } void Platform_setZfsArcValues(Meter* this) {