Swap: improve performance on Linux

We can't do the same thing with memory because there is no `cachedram` in `struct sysinfo`
This commit is contained in:
李通洲
2022-12-28 17:27:35 +08:00
parent 90359d227e
commit 6becc8f334
+6 -27
View File
@@ -1,34 +1,13 @@
#include "swap.h"
#include <stdlib.h>
#include <string.h>
#include <sys/sysinfo.h>
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;
}