Files
fastfetch/src/detection/memory/memory_linux.c
T

46 lines
1.4 KiB
C
Raw Normal View History

#include "memory.h"
2023-11-24 10:12:34 +08:00
#include "common/io/io.h"
2023-05-11 20:39:54 +08:00
#include "util/mallocHelper.h"
2023-11-24 10:12:34 +08:00
#include <inttypes.h>
2023-05-11 20:39:54 +08:00
const char* ffDetectMemory(FFMemoryResult* ram)
{
char buf[PROC_FILE_BUFFSIZ];
ssize_t nRead = ffReadFileData("/proc/meminfo", sizeof(buf) - 1, buf);
if(nRead < 0)
return "ffReadFileData(\"/proc/meminfo\", sizeof(buf)-1, buf)";
buf[nRead] = '\0';
2023-11-24 10:12:34 +08:00
uint64_t memTotal = 0,
shmem = 0,
memFree = 0,
buffers = 0,
cached = 0,
2022-10-18 23:46:42 +08:00
sReclaimable = 0;
char *token = NULL;
if((token = strstr(buf, "MemTotal:")) != NULL)
2023-12-04 20:34:41 +09:00
memTotal = strtoul(token + strlen("MemTotal:"), NULL, 10);
if((token = strstr(buf, "MemFree:")) != NULL)
2023-12-04 20:34:41 +09:00
memFree = strtoul(token + strlen("MemFree:"), NULL, 10);
if((token = strstr(buf, "Buffers:")) != NULL)
2023-12-04 20:34:41 +09:00
buffers = strtoul(token + strlen("Buffers:"), NULL, 10);
if((token = strstr(buf, "Cached:")) != NULL)
2023-12-04 20:34:41 +09:00
cached = strtoul(token + strlen("Cached:"), NULL, 10);
if((token = strstr(buf, "Shmem:")) != NULL)
2023-12-04 20:34:41 +09:00
shmem = strtoul(token + strlen("Shmem:"), NULL, 10);
if((token = strstr(buf, "SReclaimable:")) != NULL)
2023-12-04 20:34:41 +09:00
sReclaimable = strtoul(token + strlen("SReclaimable:"), NULL, 10);
2023-11-24 10:12:34 +08:00
ram->bytesTotal = memTotal * 1024lu;
ram->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * 1024lu;
2023-05-11 20:39:54 +08:00
return NULL;
}