Skip to content

Commit 6becc8f

Browse files
committed
Swap: improve performance on Linux
We can't do the same thing with memory because there is no `cachedram` in `struct sysinfo`
1 parent 90359d2 commit 6becc8f

File tree

1 file changed

+6
-27
lines changed

1 file changed

+6
-27
lines changed

src/detection/swap/swap_linux.c

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,13 @@
11
#include "swap.h"
22

3-
#include <stdlib.h>
4-
#include <string.h>
3+
#include <sys/sysinfo.h>
54

65
void ffDetectSwap(FFMemoryStorage* swap)
76
{
8-
FILE* meminfo = fopen("/proc/meminfo", "r");
9-
if(meminfo == NULL)
10-
{
11-
ffStrbufAppendS(&swap->error, "Failed to open /proc/meminfo");
12-
return;
13-
}
7+
struct sysinfo info;
8+
if(sysinfo(&info) != 0)
9+
ffStrbufAppendS(&swap->error, "sysinfo() failed");
1410

15-
char* line = NULL;
16-
size_t len = 0;
17-
18-
uint32_t swapTotal = 0,
19-
swapFree = 0;
20-
21-
while (getline(&line, &len, meminfo) != EOF)
22-
{
23-
if(!sscanf(line, "SwapTotal: %u", &swapTotal))
24-
sscanf(line, "SwapFree: %u", &swapFree);
25-
}
26-
27-
if(line != NULL)
28-
free(line);
29-
30-
fclose(meminfo);
31-
32-
swap->bytesTotal = swapTotal * (uint64_t) 1024;
33-
swap->bytesUsed = (swapTotal - swapFree) * (uint64_t) 1024;
11+
swap->bytesTotal = info.totalswap * (uint64_t) info.mem_unit;
12+
swap->bytesUsed = (info.totalswap - info.freeswap) * (uint64_t) info.mem_unit;
3413
}

0 commit comments

Comments
 (0)