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

63 lines
1.9 KiB
C
Raw Normal View History

#include "memory.h"
2026-01-06 09:48:38 +08:00
#include "common/io.h"
#include "common/mallocHelper.h"
2023-11-24 10:12:34 +08:00
#include <inttypes.h>
2026-03-29 09:28:49 +08:00
const char* ffDetectMemory(FFMemoryResult* ram) {
char buf[PROC_FILE_BUFFSIZ];
2024-10-18 16:15:48 +08:00
ssize_t nRead = ffReadFileData("/proc/meminfo", ARRAY_SIZE(buf) - 1, buf);
2026-03-29 09:28:49 +08:00
if (nRead < 0) {
2024-10-18 16:15:48 +08:00
return "ffReadFileData(\"/proc/meminfo\", ARRAY_SIZE(buf)-1, buf)";
2026-03-29 09:28:49 +08:00
}
buf[nRead] = '\0';
2023-11-24 10:12:34 +08:00
uint64_t memTotal = 0,
memAvailable = 0,
shmem = 0,
memFree = 0,
buffers = 0,
cached = 0,
2022-10-18 23:46:42 +08:00
sReclaimable = 0;
2026-03-29 09:28:49 +08:00
char* token = NULL;
if ((token = strstr(buf, "MemTotal:")) != NULL) {
2023-12-04 20:34:41 +09:00
memTotal = strtoul(token + strlen("MemTotal:"), NULL, 10);
2026-03-29 09:28:49 +08:00
} else {
return "MemTotal not found in /proc/meminfo";
2026-03-29 09:28:49 +08:00
}
2026-03-29 09:28:49 +08:00
if ((token = strstr(buf, "MemAvailable:")) != NULL) {
memAvailable = strtoul(token + strlen("MemAvailable:"), NULL, 10);
2026-03-29 09:28:49 +08:00
}
if (memAvailable == 0 || memAvailable >= memTotal) // MemAvailable can be unreasonable. #1988
{
2026-03-29 09:28:49 +08:00
if ((token = strstr(buf, "MemFree:")) != NULL) {
memFree = strtoul(token + strlen("MemFree:"), NULL, 10);
2026-03-29 09:28:49 +08:00
}
2026-03-29 09:28:49 +08:00
if ((token = strstr(buf, "Buffers:")) != NULL) {
buffers = strtoul(token + strlen("Buffers:"), NULL, 10);
2026-03-29 09:28:49 +08:00
}
2026-03-29 09:28:49 +08:00
if ((token = strstr(buf, "Cached:")) != NULL) {
cached = strtoul(token + strlen("Cached:"), NULL, 10);
2026-03-29 09:28:49 +08:00
}
2026-03-29 09:28:49 +08:00
if ((token = strstr(buf, "Shmem:")) != NULL) {
shmem = strtoul(token + strlen("Shmem:"), NULL, 10);
2026-03-29 09:28:49 +08:00
}
2026-03-29 09:28:49 +08:00
if ((token = strstr(buf, "SReclaimable:")) != NULL) {
sReclaimable = strtoul(token + strlen("SReclaimable:"), NULL, 10);
2026-03-29 09:28:49 +08:00
}
memAvailable = memFree + buffers + cached + sReclaimable - shmem;
}
2023-11-24 10:12:34 +08:00
ram->bytesTotal = memTotal * 1024lu;
ram->bytesUsed = (memTotal - memAvailable) * 1024lu;
2023-05-11 20:39:54 +08:00
return NULL;
}