Swap (Linux): don't use syscall

Fix #620
This commit is contained in:
李通洲
2023-11-24 10:12:22 +08:00
parent 41fed1da5f
commit 919bd8231e
+25 -6
View File
@@ -1,15 +1,34 @@
#include "swap.h"
#include <sys/sysinfo.h>
#include "common/io/io.h"
#include "util/mallocHelper.h"
#include <inttypes.h>
const char* ffDetectSwap(FFSwapResult* swap)
{
struct sysinfo info;
if(sysinfo(&info) != 0)
return "sysinfo() failed";
// #620
FF_AUTO_CLOSE_FILE FILE* meminfo = fopen("/proc/meminfo", "r");
if (!meminfo) return "fopen(\"/proc/meminfo\", \"r\") failed";
swap->bytesTotal = info.totalswap * (uint64_t) info.mem_unit;
swap->bytesUsed = (info.totalswap - info.freeswap) * (uint64_t) info.mem_unit;
uint64_t swapTotal = 0, swapFree = 0;
char* FF_AUTO_FREE line = NULL;
size_t len = 0;
uint8_t count = 0;
while (getline(&line, &len, meminfo) != EOF)
{
if (line[0] == 'S')
{
if(sscanf(line, "SwapTotal: %" PRIu64, &swapTotal) > 0 || sscanf(line, "SwapFree: %" PRIu64, &swapFree) > 0)
if (++count >= 2) goto done;
}
}
done:
swap->bytesTotal = swapTotal * 1024lu;
swap->bytesUsed = (swapTotal - swapFree) * 1024lu;
return NULL;
}