From b965417bf78e20fe1bdde3678361f8fbec86f458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Tue, 10 Aug 2021 21:32:05 +0200 Subject: [PATCH] Add combined memory and swap meter Closes: #699 --- Makefile.am | 2 + MemorySwapMeter.c | 104 ++++++++++++++++++++++++++++++++++++++++ MemorySwapMeter.h | 15 ++++++ darwin/Platform.c | 2 + dragonflybsd/Platform.c | 2 + freebsd/Platform.c | 2 + linux/Platform.c | 2 + netbsd/Platform.c | 2 + openbsd/Platform.c | 2 + pcp/Platform.c | 2 + solaris/Platform.c | 2 + unsupported/Platform.c | 2 + 12 files changed, 139 insertions(+) create mode 100644 MemorySwapMeter.c create mode 100644 MemorySwapMeter.h diff --git a/Makefile.am b/Makefile.am index e5f51745..d5bd7648 100644 --- a/Makefile.am +++ b/Makefile.am @@ -52,6 +52,7 @@ myhtopsources = \ LoadAverageMeter.c \ MainPanel.c \ MemoryMeter.c \ + MemorySwapMeter.c \ Meter.c \ MetersPanel.c \ NetworkIOMeter.c \ @@ -109,6 +110,7 @@ myhtopheaders = \ Macros.h \ MainPanel.h \ MemoryMeter.h \ + MemorySwapMeter.h \ Meter.h \ MetersPanel.h \ NetworkIOMeter.h \ diff --git a/MemorySwapMeter.c b/MemorySwapMeter.c new file mode 100644 index 00000000..ba5032c9 --- /dev/null +++ b/MemorySwapMeter.c @@ -0,0 +1,104 @@ +/* +htop - MemorySwapMeter.c +(C) 2021 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + +#include "MemorySwapMeter.h" + +#include +#include + +#include "CRT.h" +#include "MemoryMeter.h" +#include "Object.h" +#include "Platform.h" +#include "RichString.h" +#include "SwapMeter.h" + + +typedef struct MemorySwapMeterData_ { + Meter* memoryMeter; + Meter* swapMeter; +} MemorySwapMeterData; + +static void MemorySwapMeter_updateValues(Meter* this) { + MemorySwapMeterData* data = this->meterData; + + Meter_updateValues(data->memoryMeter); + Meter_updateValues(data->swapMeter); +} + +static void MemorySwapMeter_draw(Meter* this, int x, int y, int w) { + MemorySwapMeterData* data = this->meterData; + + int colwidth = (w - 2) / 2 + 1; + + assert(data->memoryMeter->draw); + data->memoryMeter->draw(data->memoryMeter, x, y, colwidth); + assert(data->swapMeter->draw); + data->swapMeter->draw(data->swapMeter, x + w / 2, y, colwidth); +} + +static void MemorySwapMeter_init(Meter* this) { + MemorySwapMeterData* data = this->meterData; + + if (!data) { + data = this->meterData = xMalloc(sizeof(MemorySwapMeterData)); + data->memoryMeter = NULL; + data->swapMeter = NULL; + } + + if (!data->memoryMeter) + data->memoryMeter = Meter_new(this->pl, 0, (const MeterClass*) Class(MemoryMeter)); + if (!data->swapMeter) + data->swapMeter = Meter_new(this->pl, 0, (const MeterClass*) Class(SwapMeter)); + + if (Meter_initFn(data->memoryMeter)) + Meter_init(data->memoryMeter); + if (Meter_initFn(data->swapMeter)) + Meter_init(data->swapMeter); + + if (this->mode == 0) + this->mode = BAR_METERMODE; + + this->h = MAXIMUM(Meter_modes[data->memoryMeter->mode]->h, Meter_modes[data->swapMeter->mode]->h); +} + +static void MemorySwapMeter_updateMode(Meter* this, int mode) { + MemorySwapMeterData* data = this->meterData; + + this->mode = mode; + + Meter_setMode(data->memoryMeter, mode); + Meter_setMode(data->swapMeter, mode); + + this->h = MAXIMUM(Meter_modes[data->memoryMeter->mode]->h, Meter_modes[data->swapMeter->mode]->h); +} + +static void MemorySwapMeter_done(Meter* this) { + MemorySwapMeterData* data = this->meterData; + + Meter_delete((Object*)data->swapMeter); + Meter_delete((Object*)data->memoryMeter); + + free(data); +} + +const MeterClass MemorySwapMeter_class = { + .super = { + .extends = Class(Meter), + .delete = Meter_delete, + }, + .updateValues = MemorySwapMeter_updateValues, + .defaultMode = CUSTOM_METERMODE, + .name = "MemorySwap", + .uiName = "Memory & Swap", + .description = "Combined memory and swap usage", + .caption = "M&S", + .draw = MemorySwapMeter_draw, + .init = MemorySwapMeter_init, + .updateMode = MemorySwapMeter_updateMode, + .done = MemorySwapMeter_done +}; diff --git a/MemorySwapMeter.h b/MemorySwapMeter.h new file mode 100644 index 00000000..b4180a1e --- /dev/null +++ b/MemorySwapMeter.h @@ -0,0 +1,15 @@ +#ifndef HEADER_MemorySwapMeter +#define HEADER_MemorySwapMeter +/* +htop - MemorySwapMeter.h +(C) 2021 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + +#include "Meter.h" + + +extern const MeterClass MemorySwapMeter_class; + +#endif diff --git a/darwin/Platform.c b/darwin/Platform.c index d4d15dad..2630e2d4 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -28,6 +28,7 @@ in the source distribution for its full text. #include "LoadAverageMeter.h" #include "Macros.h" #include "MemoryMeter.h" +#include "MemorySwapMeter.h" #include "ProcessLocksScreen.h" #include "SwapMeter.h" #include "SysArchMeter.h" @@ -95,6 +96,7 @@ const MeterClass* const Platform_meterTypes[] = { &LoadMeter_class, &MemoryMeter_class, &SwapMeter_class, + &MemorySwapMeter_class, &TasksMeter_class, &BatteryMeter_class, &HostnameMeter_class, diff --git a/dragonflybsd/Platform.c b/dragonflybsd/Platform.c index 30fb0031..62a1fb62 100644 --- a/dragonflybsd/Platform.c +++ b/dragonflybsd/Platform.c @@ -23,6 +23,7 @@ in the source distribution for its full text. #include "HostnameMeter.h" #include "LoadAverageMeter.h" #include "MemoryMeter.h" +#include "MemorySwapMeter.h" #include "ProcessList.h" #include "SwapMeter.h" #include "SysArchMeter.h" @@ -81,6 +82,7 @@ const MeterClass* const Platform_meterTypes[] = { &LoadAverageMeter_class, &LoadMeter_class, &MemoryMeter_class, + &MemorySwapMeter_class, &SwapMeter_class, &TasksMeter_class, &UptimeMeter_class, diff --git a/freebsd/Platform.c b/freebsd/Platform.c index e27ba9a6..527a455e 100644 --- a/freebsd/Platform.c +++ b/freebsd/Platform.c @@ -35,6 +35,7 @@ in the source distribution for its full text. #include "LoadAverageMeter.h" #include "Macros.h" #include "MemoryMeter.h" +#include "MemorySwapMeter.h" #include "Meter.h" #include "NetworkIOMeter.h" #include "ProcessList.h" @@ -100,6 +101,7 @@ const MeterClass* const Platform_meterTypes[] = { &LoadMeter_class, &MemoryMeter_class, &SwapMeter_class, + &MemorySwapMeter_class, &TasksMeter_class, &UptimeMeter_class, &BatteryMeter_class, diff --git a/linux/Platform.c b/linux/Platform.c index 66fcb5f3..7d1c190a 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -36,6 +36,7 @@ in the source distribution for its full text. #include "MainPanel.h" #include "Meter.h" #include "MemoryMeter.h" +#include "MemorySwapMeter.h" #include "NetworkIOMeter.h" #include "Object.h" #include "Panel.h" @@ -195,6 +196,7 @@ const MeterClass* const Platform_meterTypes[] = { &LoadMeter_class, &MemoryMeter_class, &SwapMeter_class, + &MemorySwapMeter_class, &SysArchMeter_class, &HugePageMeter_class, &TasksMeter_class, diff --git a/netbsd/Platform.c b/netbsd/Platform.c index dbcb7eb3..e2566aee 100644 --- a/netbsd/Platform.c +++ b/netbsd/Platform.c @@ -42,6 +42,7 @@ in the source distribution for its full text. #include "LoadAverageMeter.h" #include "Macros.h" #include "MemoryMeter.h" +#include "MemorySwapMeter.h" #include "Meter.h" #include "ProcessList.h" #include "Settings.h" @@ -149,6 +150,7 @@ const MeterClass* const Platform_meterTypes[] = { &LoadMeter_class, &MemoryMeter_class, &SwapMeter_class, + &MemorySwapMeter_class, &TasksMeter_class, &UptimeMeter_class, &BatteryMeter_class, diff --git a/openbsd/Platform.c b/openbsd/Platform.c index 65133122..a62381f0 100644 --- a/openbsd/Platform.c +++ b/openbsd/Platform.c @@ -32,6 +32,7 @@ in the source distribution for its full text. #include "LoadAverageMeter.h" #include "Macros.h" #include "MemoryMeter.h" +#include "MemorySwapMeter.h" #include "Meter.h" #include "ProcessList.h" #include "Settings.h" @@ -98,6 +99,7 @@ const MeterClass* const Platform_meterTypes[] = { &LoadMeter_class, &MemoryMeter_class, &SwapMeter_class, + &MemorySwapMeter_class, &TasksMeter_class, &UptimeMeter_class, &BatteryMeter_class, diff --git a/pcp/Platform.c b/pcp/Platform.c index d5a76b55..c2af2e4a 100644 --- a/pcp/Platform.c +++ b/pcp/Platform.c @@ -29,6 +29,7 @@ in the source distribution for its full text. #include "LoadAverageMeter.h" #include "Macros.h" #include "MemoryMeter.h" +#include "MemorySwapMeter.h" #include "Meter.h" #include "NetworkIOMeter.h" #include "ProcessList.h" @@ -73,6 +74,7 @@ const MeterClass* const Platform_meterTypes[] = { &LoadMeter_class, &MemoryMeter_class, &SwapMeter_class, + &MemorySwapMeter_class, &TasksMeter_class, &UptimeMeter_class, &BatteryMeter_class, diff --git a/solaris/Platform.c b/solaris/Platform.c index 4923b790..74391959 100644 --- a/solaris/Platform.c +++ b/solaris/Platform.c @@ -24,6 +24,7 @@ in the source distribution for its full text. #include "Meter.h" #include "CPUMeter.h" #include "MemoryMeter.h" +#include "MemorySwapMeter.h" #include "SwapMeter.h" #include "TasksMeter.h" #include "LoadAverageMeter.h" @@ -97,6 +98,7 @@ const MeterClass* const Platform_meterTypes[] = { &LoadMeter_class, &MemoryMeter_class, &SwapMeter_class, + &MemorySwapMeter_class, &TasksMeter_class, &BatteryMeter_class, &HostnameMeter_class, diff --git a/unsupported/Platform.c b/unsupported/Platform.c index d8da9453..0f897406 100644 --- a/unsupported/Platform.c +++ b/unsupported/Platform.c @@ -20,6 +20,7 @@ in the source distribution for its full text. #include "LoadAverageMeter.h" #include "Macros.h" #include "MemoryMeter.h" +#include "MemorySwapMeter.h" #include "SwapMeter.h" #include "SysArchMeter.h" #include "TasksMeter.h" @@ -43,6 +44,7 @@ const MeterClass* const Platform_meterTypes[] = { &LoadMeter_class, &MemoryMeter_class, &SwapMeter_class, + &MemorySwapMeter_class, &TasksMeter_class, &BatteryMeter_class, &HostnameMeter_class,