From 71f5a80d9e312375eff28d2fbb7d8add81f6793c Mon Sep 17 00:00:00 2001 From: Ivan Shapovalov Date: Tue, 27 Dec 2022 05:16:54 +0400 Subject: Linux: implement zswap support On Linux, use zswap to populate "compressed memory" metrics added in the previous commit. Fixes #104. --- linux/LinuxProcessList.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'linux/LinuxProcessList.c') diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index a3472c53..2ecdefc3 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -1733,6 +1733,7 @@ errorReadingProcess: } static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) { + LinuxProcessList *lpl = (LinuxProcessList *)this; memory_t availableMem = 0; memory_t freeMem = 0; memory_t totalMem = 0; @@ -1743,6 +1744,8 @@ static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) { memory_t swapCacheMem = 0; memory_t swapFreeMem = 0; memory_t sreclaimableMem = 0; + memory_t zswapCompMem = 0; + memory_t zswapOrigMem = 0; FILE* file = fopen(PROCMEMINFOFILE, "r"); if (!file) @@ -1787,6 +1790,10 @@ static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) { break; } break; + case 'Z': + tryRead("Zswap:", zswapCompMem); + tryRead("Zswapped:", zswapOrigMem); + break; } #undef tryRead @@ -1812,6 +1819,8 @@ static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) { this->totalSwap = swapTotalMem; this->usedSwap = swapTotalMem - swapFreeMem - swapCacheMem; this->cachedSwap = swapCacheMem; + lpl->zswap.usedZswapComp = zswapCompMem; + lpl->zswap.usedZswapOrig = zswapOrigMem; } static void LinuxProcessList_scanHugePages(LinuxProcessList* this) { @@ -1870,6 +1879,24 @@ static void LinuxProcessList_scanHugePages(LinuxProcessList* this) { closedir(dir); } +static inline void LinuxProcessList_scanZswapInfo(LinuxProcessList *this) { + long max_pool_percent = 0; + int r; + char buf[256]; + + r = xReadfile("/sys/module/zswap/parameters/max_pool_percent", buf, 256); + if (r <= 0) { + return; + } + max_pool_percent = strtol(buf, NULL, 10); + if (max_pool_percent < 0 || max_pool_percent > 100) { + return; + } + + this->zswap.totalZswapPool = this->super.totalMem * max_pool_percent / 100; + /* the rest of the metrics are set in LinuxProcessList_scanMemoryInfo() */ +} + static inline void LinuxProcessList_scanZramInfo(LinuxProcessList* this) { memory_t totalZram = 0; memory_t usedZramComp = 0; @@ -2222,6 +2249,7 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { LinuxProcessList_scanHugePages(this); LinuxProcessList_scanZfsArcstats(this); LinuxProcessList_scanZramInfo(this); + LinuxProcessList_scanZswapInfo(this); double period = LinuxProcessList_scanCPUTime(super); -- cgit v1.2.3