2022-09-05 10:57:09 +02:00
|
|
|
#include "memory.h"
|
2023-05-11 20:39:54 +08:00
|
|
|
#include "util/mallocHelper.h"
|
2022-09-05 10:57:09 +02:00
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2022-09-08 14:51:31 +02:00
|
|
|
#include <string.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
|
|
|
{
|
|
|
|
|
FILE* meminfo = fopen("/proc/meminfo", "r");
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
uint32_t memTotal = 0,
|
|
|
|
|
shmem = 0,
|
|
|
|
|
memFree = 0,
|
|
|
|
|
buffers = 0,
|
|
|
|
|
cached = 0,
|
2022-10-18 23:46:42 +08:00
|
|
|
sReclaimable = 0;
|
2022-09-05 10:57:09 +02:00
|
|
|
|
|
|
|
|
while (getline(&line, &len, meminfo) != EOF)
|
|
|
|
|
{
|
2022-10-18 23:46:42 +08:00
|
|
|
if(!sscanf(line, "MemTotal: %u", &memTotal))
|
|
|
|
|
if(!sscanf(line, "Shmem: %u", &shmem))
|
|
|
|
|
if(!sscanf(line, "MemFree: %u", &memFree))
|
|
|
|
|
if(!sscanf(line, "Buffers: %u", &buffers))
|
|
|
|
|
if(!sscanf(line, "Cached: %u", &cached))
|
|
|
|
|
sscanf(line, "SReclaimable: %u", &sReclaimable);
|
2022-09-05 10:57:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(meminfo);
|
|
|
|
|
|
2022-12-28 15:55:31 +08:00
|
|
|
ram->bytesTotal = memTotal * (uint64_t) 1024;
|
|
|
|
|
if(ram->bytesTotal == 0)
|
2023-05-11 20:39:54 +08:00
|
|
|
return "Failed to read MemTotal";
|
2022-09-20 15:12:24 +02:00
|
|
|
else
|
2022-12-28 15:55:31 +08:00
|
|
|
ram->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024;
|
2023-05-11 20:39:54 +08:00
|
|
|
|
|
|
|
|
return NULL;
|
2022-09-05 10:57:09 +02:00
|
|
|
}
|