2022-09-05 10:57:09 +02:00
|
|
|
#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"
|
2022-09-05 10:57:09 +02:00
|
|
|
|
2023-11-24 10:12:34 +08:00
|
|
|
#include <inttypes.h>
|
2022-09-05 10:57:09 +02:00
|
|
|
|
2023-05-11 20:39:54 +08:00
|
|
|
const char* ffDetectMemory(FFMemoryResult* ram)
|
2022-09-05 10:57:09 +02:00
|
|
|
{
|
2023-11-24 10:12:34 +08:00
|
|
|
FF_AUTO_CLOSE_FILE FILE* meminfo = fopen("/proc/meminfo", "r");
|
2022-09-05 10:57:09 +02:00
|
|
|
if(meminfo == NULL)
|
2023-05-11 20:39:54 +08:00
|
|
|
return "Failed to open /proc/meminfo";
|
2022-09-05 10:57:09 +02:00
|
|
|
|
2023-05-11 20:39:54 +08:00
|
|
|
char* FF_AUTO_FREE line = NULL;
|
2022-09-05 10:57:09 +02:00
|
|
|
size_t len = 0;
|
|
|
|
|
|
2023-11-24 10:12:34 +08:00
|
|
|
uint64_t memTotal = 0,
|
2022-09-05 10:57:09 +02:00
|
|
|
shmem = 0,
|
|
|
|
|
memFree = 0,
|
|
|
|
|
buffers = 0,
|
|
|
|
|
cached = 0,
|
2022-10-18 23:46:42 +08:00
|
|
|
sReclaimable = 0;
|
2023-11-24 10:12:34 +08:00
|
|
|
uint8_t count = 0;
|
2022-09-05 10:57:09 +02:00
|
|
|
|
|
|
|
|
while (getline(&line, &len, meminfo) != EOF)
|
|
|
|
|
{
|
2023-11-24 10:12:34 +08:00
|
|
|
switch (line[0])
|
|
|
|
|
{
|
|
|
|
|
case 'B':
|
|
|
|
|
if (sscanf(line, "Buffers: %" PRIu64, &buffers) > 0)
|
|
|
|
|
if (++count >= 6) goto done;
|
|
|
|
|
break;
|
|
|
|
|
case 'C':
|
|
|
|
|
if (sscanf(line, "Cached: %" PRIu64, &cached) > 0)
|
|
|
|
|
if (++count >= 6) goto done;
|
|
|
|
|
break;
|
|
|
|
|
case 'M':
|
2023-11-25 19:40:36 +08:00
|
|
|
if(sscanf(line, "MemTotal: %" PRIu64, &memTotal) > 0 || sscanf(line, "MemFree: %" PRIu64, &memFree) > 0)
|
2023-11-24 10:12:34 +08:00
|
|
|
if (++count >= 6) goto done;
|
|
|
|
|
break;
|
|
|
|
|
case 'S':
|
2023-11-25 19:40:36 +08:00
|
|
|
if(sscanf(line, "Shmem: %" PRIu64, &shmem) > 0 || sscanf(line, "SReclaimable: %" PRIu64, &sReclaimable) > 0)
|
2023-11-24 10:12:34 +08:00
|
|
|
if (++count >= 6) goto done;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-09-05 10:57:09 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-24 10:12:34 +08:00
|
|
|
done:
|
|
|
|
|
ram->bytesTotal = memTotal * 1024lu;
|
|
|
|
|
ram->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * 1024lu;
|
2023-05-11 20:39:54 +08:00
|
|
|
|
|
|
|
|
return NULL;
|
2022-09-05 10:57:09 +02:00
|
|
|
}
|