Skip to content

Commit a9c5c28

Browse files
committed
Memory (Linux): Use MemAvailable if available
1 parent 0182e32 commit a9c5c28

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/detection/memory/memory_linux.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,43 @@ const char* ffDetectMemory(FFMemoryResult* ram)
1313
buf[nRead] = '\0';
1414

1515
uint64_t memTotal = 0,
16+
memAvailable = 0,
1617
shmem = 0,
1718
memFree = 0,
1819
buffers = 0,
1920
cached = 0,
2021
sReclaimable = 0;
21-
22+
2223
char *token = NULL;
2324
if((token = strstr(buf, "MemTotal:")) != NULL)
2425
memTotal = strtoul(token + strlen("MemTotal:"), NULL, 10);
26+
else
27+
return "MemTotal not found in /proc/meminfo";
28+
29+
if((token = strstr(buf, "MemAvailable:")) != NULL)
30+
memAvailable = strtoul(token + strlen("MemAvailable:"), NULL, 10);
31+
else
32+
{
33+
if((token = strstr(buf, "MemFree:")) != NULL)
34+
memFree = strtoul(token + strlen("MemFree:"), NULL, 10);
35+
36+
if((token = strstr(buf, "Buffers:")) != NULL)
37+
buffers = strtoul(token + strlen("Buffers:"), NULL, 10);
38+
39+
if((token = strstr(buf, "Cached:")) != NULL)
40+
cached = strtoul(token + strlen("Cached:"), NULL, 10);
41+
42+
if((token = strstr(buf, "Shmem:")) != NULL)
43+
shmem = strtoul(token + strlen("Shmem:"), NULL, 10);
44+
45+
if((token = strstr(buf, "SReclaimable:")) != NULL)
46+
sReclaimable = strtoul(token + strlen("SReclaimable:"), NULL, 10);
2547

26-
if((token = strstr(buf, "MemFree:")) != NULL)
27-
memFree = strtoul(token + strlen("MemFree:"), NULL, 10);
28-
29-
if((token = strstr(buf, "Buffers:")) != NULL)
30-
buffers = strtoul(token + strlen("Buffers:"), NULL, 10);
31-
32-
if((token = strstr(buf, "Cached:")) != NULL)
33-
cached = strtoul(token + strlen("Cached:"), NULL, 10);
34-
35-
if((token = strstr(buf, "Shmem:")) != NULL)
36-
shmem = strtoul(token + strlen("Shmem:"), NULL, 10);
37-
38-
if((token = strstr(buf, "SReclaimable:")) != NULL)
39-
sReclaimable = strtoul(token + strlen("SReclaimable:"), NULL, 10);
48+
memAvailable = memFree + buffers + cached + sReclaimable - shmem;
49+
}
4050

4151
ram->bytesTotal = memTotal * 1024lu;
42-
ram->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * 1024lu;
52+
ram->bytesUsed = (memTotal - memAvailable) * 1024lu;
4353

4454
return NULL;
4555
}

0 commit comments

Comments
 (0)