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)."
This commit is contained in:
David Zarzycki 2021-01-07 08:38:18 -05:00 committed by BenBE
parent f4404effa4
commit 37e186fd66
11 changed files with 31 additions and 4 deletions

View File

@ -528,7 +528,13 @@ static Htop_Reaction actionHelp(State* st) {
mvaddstr(line++, 0, "Swap bar: "); mvaddstr(line++, 0, "Swap bar: ");
addattrstr(CRT_colors[BAR_BORDER], "["); addattrstr(CRT_colors[BAR_BORDER], "[");
addattrstr(CRT_colors[SWAP], "used"); 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"); addattrstr(CRT_colors[BAR_SHADOW], " used/total");
#endif
addattrstr(CRT_colors[BAR_BORDER], "]"); addattrstr(CRT_colors[BAR_BORDER], "]");
attrset(CRT_colors[DEFAULT_COLOR]); attrset(CRT_colors[DEFAULT_COLOR]);
mvaddstr(line++, 0, "Type and layout of header meters are configurable in the setup screen."); mvaddstr(line++, 0, "Type and layout of header meters are configurable in the setup screen.");

6
CRT.c
View File

@ -144,6 +144,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[BAR_BORDER] = A_BOLD, [BAR_BORDER] = A_BOLD,
[BAR_SHADOW] = A_BOLD | ColorPairGrayBlack, [BAR_SHADOW] = A_BOLD | ColorPairGrayBlack,
[SWAP] = ColorPair(Red, Black), [SWAP] = ColorPair(Red, Black),
[SWAP_CACHE] = ColorPair(Yellow, Black),
[GRAPH_1] = A_BOLD | ColorPair(Cyan, Black), [GRAPH_1] = A_BOLD | ColorPair(Cyan, Black),
[GRAPH_2] = ColorPair(Cyan, Black), [GRAPH_2] = ColorPair(Cyan, Black),
[MEMORY_USED] = ColorPair(Green, Black), [MEMORY_USED] = ColorPair(Green, Black),
@ -229,6 +230,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[BAR_BORDER] = A_BOLD, [BAR_BORDER] = A_BOLD,
[BAR_SHADOW] = A_DIM, [BAR_SHADOW] = A_DIM,
[SWAP] = A_BOLD, [SWAP] = A_BOLD,
[SWAP_CACHE] = A_NORMAL,
[GRAPH_1] = A_BOLD, [GRAPH_1] = A_BOLD,
[GRAPH_2] = A_NORMAL, [GRAPH_2] = A_NORMAL,
[MEMORY_USED] = A_BOLD, [MEMORY_USED] = A_BOLD,
@ -314,6 +316,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[BAR_BORDER] = ColorPair(Blue, White), [BAR_BORDER] = ColorPair(Blue, White),
[BAR_SHADOW] = ColorPair(Black, White), [BAR_SHADOW] = ColorPair(Black, White),
[SWAP] = ColorPair(Red, White), [SWAP] = ColorPair(Red, White),
[SWAP_CACHE] = ColorPair(Yellow, White),
[GRAPH_1] = A_BOLD | ColorPair(Blue, White), [GRAPH_1] = A_BOLD | ColorPair(Blue, White),
[GRAPH_2] = ColorPair(Blue, White), [GRAPH_2] = ColorPair(Blue, White),
[MEMORY_USED] = ColorPair(Green, White), [MEMORY_USED] = ColorPair(Green, White),
@ -399,6 +402,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[BAR_BORDER] = ColorPair(Blue, Black), [BAR_BORDER] = ColorPair(Blue, Black),
[BAR_SHADOW] = ColorPairGrayBlack, [BAR_SHADOW] = ColorPairGrayBlack,
[SWAP] = ColorPair(Red, Black), [SWAP] = ColorPair(Red, Black),
[SWAP_CACHE] = ColorPair(Yellow, Black),
[GRAPH_1] = A_BOLD | ColorPair(Cyan, Black), [GRAPH_1] = A_BOLD | ColorPair(Cyan, Black),
[GRAPH_2] = ColorPair(Cyan, Black), [GRAPH_2] = ColorPair(Cyan, Black),
[MEMORY_USED] = ColorPair(Green, 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_BORDER] = A_BOLD | ColorPair(Yellow, Blue),
[BAR_SHADOW] = ColorPair(Cyan, Blue), [BAR_SHADOW] = ColorPair(Cyan, Blue),
[SWAP] = ColorPair(Red, Blue), [SWAP] = ColorPair(Red, Blue),
[SWAP_CACHE] = A_BOLD | ColorPair(Yellow, Blue),
[GRAPH_1] = A_BOLD | ColorPair(Cyan, Blue), [GRAPH_1] = A_BOLD | ColorPair(Cyan, Blue),
[GRAPH_2] = ColorPair(Cyan, Blue), [GRAPH_2] = ColorPair(Cyan, Blue),
[MEMORY_USED] = A_BOLD | ColorPair(Green, 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_BORDER] = A_BOLD | ColorPair(Green, Black),
[BAR_SHADOW] = ColorPair(Cyan, Black), [BAR_SHADOW] = ColorPair(Cyan, Black),
[SWAP] = ColorPair(Red, Black), [SWAP] = ColorPair(Red, Black),
[SWAP_CACHE] = ColorPair(Yellow, Black),
[GRAPH_1] = A_BOLD | ColorPair(Green, Black), [GRAPH_1] = A_BOLD | ColorPair(Green, Black),
[GRAPH_2] = ColorPair(Green, Black), [GRAPH_2] = ColorPair(Green, Black),
[MEMORY_USED] = ColorPair(Green, Black), [MEMORY_USED] = ColorPair(Green, Black),

1
CRT.h
View File

@ -65,6 +65,7 @@ typedef enum ColorElements_ {
BATTERY, BATTERY,
TASKS_RUNNING, TASKS_RUNNING,
SWAP, SWAP,
SWAP_CACHE,
PROCESS, PROCESS,
PROCESS_SHADOW, PROCESS_SHADOW,
PROCESS_TAG, PROCESS_TAG,

View File

@ -65,9 +65,10 @@ typedef struct ProcessList_ {
unsigned long long int usedMem; unsigned long long int usedMem;
unsigned long long int buffersMem; unsigned long long int buffersMem;
unsigned long long int cachedMem; unsigned long long int cachedMem;
unsigned long long int totalSwap; unsigned long long int totalSwap;
unsigned long long int usedSwap; unsigned long long int usedSwap;
unsigned long long int freeSwap; unsigned long long int cachedSwap;
int cpuCount; int cpuCount;

View File

@ -14,7 +14,8 @@ in the source distribution for its full text.
static const int SwapMeter_attributes[] = { static const int SwapMeter_attributes[] = {
SWAP SWAP,
SWAP_CACHE
}; };
static void SwapMeter_updateValues(Meter* this, char* buffer, size_t size) { 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)); Meter_humanUnit(buffer, this->values[0], sizeof(buffer));
RichString_appendAscii(out, CRT_colors[METER_TEXT], " used:"); RichString_appendAscii(out, CRT_colors[METER_TEXT], " used:");
RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer); 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 = { const MeterClass SwapMeter_class = {
@ -48,7 +55,7 @@ const MeterClass SwapMeter_class = {
}, },
.updateValues = SwapMeter_updateValues, .updateValues = SwapMeter_updateValues,
.defaultMode = BAR_METERMODE, .defaultMode = BAR_METERMODE,
.maxItems = 1, .maxItems = 2,
.total = 100.0, .total = 100.0,
.attributes = SwapMeter_attributes, .attributes = SwapMeter_attributes,
.name = "Swap", .name = "Swap",

View File

@ -205,6 +205,7 @@ void Platform_setSwapValues(Meter* this) {
const ProcessList* pl = this->pl; const ProcessList* pl = this->pl;
this->total = pl->totalSwap; this->total = pl->totalSwap;
this->values[0] = pl->usedSwap; this->values[0] = pl->usedSwap;
this->values[1] = NAN;
} }
char* Platform_getProcessEnv(pid_t pid) { char* Platform_getProcessEnv(pid_t pid) {

View File

@ -225,6 +225,7 @@ void Platform_setSwapValues(Meter* this) {
const ProcessList* pl = this->pl; const ProcessList* pl = this->pl;
this->total = pl->totalSwap; this->total = pl->totalSwap;
this->values[0] = pl->usedSwap; this->values[0] = pl->usedSwap;
this->values[1] = NAN;
} }
void Platform_setZfsArcValues(Meter* this) { void Platform_setZfsArcValues(Meter* this) {

View File

@ -1508,6 +1508,7 @@ static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) {
switch (buffer[1]) { switch (buffer[1]) {
case 'w': case 'w':
tryRead("SwapTotal:", &this->totalSwap); tryRead("SwapTotal:", &this->totalSwap);
tryRead("SwapCached:", &this->cachedSwap);
tryRead("SwapFree:", &swapFree); tryRead("SwapFree:", &swapFree);
break; break;
case 'h': case 'h':
@ -1524,7 +1525,7 @@ static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) {
this->usedMem = this->totalMem - freeMem; this->usedMem = this->totalMem - freeMem;
this->cachedMem = this->cachedMem + sreclaimable - shmem; this->cachedMem = this->cachedMem + sreclaimable - shmem;
this->usedSwap = this->totalSwap - swapFree; this->usedSwap = this->totalSwap - swapFree - this->cachedSwap;
fclose(file); fclose(file);
} }

View File

@ -301,6 +301,7 @@ void Platform_setSwapValues(Meter* this) {
const ProcessList* pl = this->pl; const ProcessList* pl = this->pl;
this->total = pl->totalSwap; this->total = pl->totalSwap;
this->values[0] = pl->usedSwap; this->values[0] = pl->usedSwap;
this->values[1] = pl->cachedSwap;
} }
void Platform_setZramValues(Meter* this) { void Platform_setZramValues(Meter* this) {

View File

@ -213,6 +213,7 @@ void Platform_setSwapValues(Meter* this) {
const ProcessList* pl = this->pl; const ProcessList* pl = this->pl;
this->total = pl->totalSwap; this->total = pl->totalSwap;
this->values[0] = pl->usedSwap; this->values[0] = pl->usedSwap;
this->values[1] = NAN;
} }
char* Platform_getProcessEnv(pid_t pid) { char* Platform_getProcessEnv(pid_t pid) {

View File

@ -223,6 +223,7 @@ void Platform_setSwapValues(Meter* this) {
const ProcessList* pl = this->pl; const ProcessList* pl = this->pl;
this->total = pl->totalSwap; this->total = pl->totalSwap;
this->values[0] = pl->usedSwap; this->values[0] = pl->usedSwap;
this->values[1] = NAN;
} }
void Platform_setZfsArcValues(Meter* this) { void Platform_setZfsArcValues(Meter* this) {