From 6becc8f3340b3010f9eeeb4596be0a705ff3de9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 28 Dec 2022 17:27:35 +0800 Subject: [PATCH] Swap: improve performance on Linux We can't do the same thing with memory because there is no `cachedram` in `struct sysinfo` --- src/detection/swap/swap_linux.c | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/src/detection/swap/swap_linux.c b/src/detection/swap/swap_linux.c index 487b68bcf..ee623c0c4 100644 --- a/src/detection/swap/swap_linux.c +++ b/src/detection/swap/swap_linux.c @@ -1,34 +1,13 @@ #include "swap.h" -#include -#include +#include void ffDetectSwap(FFMemoryStorage* swap) { - FILE* meminfo = fopen("/proc/meminfo", "r"); - if(meminfo == NULL) - { - ffStrbufAppendS(&swap->error, "Failed to open /proc/meminfo"); - return; - } + struct sysinfo info; + if(sysinfo(&info) != 0) + ffStrbufAppendS(&swap->error, "sysinfo() failed"); - char* line = NULL; - size_t len = 0; - - uint32_t swapTotal = 0, - swapFree = 0; - - while (getline(&line, &len, meminfo) != EOF) - { - if(!sscanf(line, "SwapTotal: %u", &swapTotal)) - sscanf(line, "SwapFree: %u", &swapFree); - } - - if(line != NULL) - free(line); - - fclose(meminfo); - - swap->bytesTotal = swapTotal * (uint64_t) 1024; - swap->bytesUsed = (swapTotal - swapFree) * (uint64_t) 1024; + swap->bytesTotal = info.totalswap * (uint64_t) info.mem_unit; + swap->bytesUsed = (info.totalswap - info.freeswap) * (uint64_t) info.mem_unit; }