Merge pull request #11052 from jdgleaver/unix-free-mem

(Linux/Android) Fix reporting of free memory
This commit is contained in:
Autechre 2020-07-20 17:40:16 +02:00 committed by GitHub
commit 11972c900c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -119,6 +119,14 @@ static const char *proc_acpi_ac_adapter_path = "/proc/acpi/ac_adapter";
static char unix_cpu_model_name[64] = {0}; static char unix_cpu_model_name[64] = {0};
#endif #endif
/* /proc/meminfo parameters */
#define PROC_MEMINFO_PATH "/proc/meminfo"
#define PROC_MEMINFO_MEM_AVAILABLE_TAG "MemAvailable:"
#define PROC_MEMINFO_MEM_FREE_TAG "MemFree:"
#define PROC_MEMINFO_BUFFERS_TAG "Buffers:"
#define PROC_MEMINFO_CACHED_TAG "Cached:"
#define PROC_MEMINFO_SHMEM_TAG "Shmem:"
#if (defined(__linux__) || defined(__unix__)) && !defined(ANDROID) #if (defined(__linux__) || defined(__unix__)) && !defined(ANDROID)
static int speak_pid = 0; static int speak_pid = 0;
#endif #endif
@ -2131,35 +2139,85 @@ static uint64_t frontend_unix_get_mem_total(void)
static uint64_t frontend_unix_get_mem_free(void) static uint64_t frontend_unix_get_mem_free(void)
{ {
#if defined(ANDROID) || (!defined(__linux__) && !defined(__OpenBSD__))
char line[256]; char line[256];
uint64_t total = 0; unsigned long mem_available = 0;
uint64_t freemem = 0; unsigned long mem_free = 0;
uint64_t buffers = 0; unsigned long buffers = 0;
uint64_t cached = 0; unsigned long cached = 0;
FILE* data = fopen("/proc/meminfo", "r"); unsigned long shmem = 0;
if (!data) bool mem_available_found = false;
bool mem_free_found = false;
bool buffers_found = false;
bool cached_found = false;
bool shmem_found = false;
FILE* meminfo_file = NULL;
line[0] = '\0';
/* Open /proc/meminfo */
meminfo_file = fopen(PROC_MEMINFO_PATH, "r");
if (!meminfo_file)
return 0; return 0;
while (fgets(line, sizeof(line), data)) /* Parse lines
* (Note: virtual filesystem, so don't have to
* worry about buffering file reads) */
while (fgets(line, sizeof(line), meminfo_file))
{ {
if (sscanf(line, "MemTotal: " STRING_REP_USIZE " kB", (size_t*)&total) == 1) /* If 'MemAvailable' is found, we can return immediately */
total *= 1024; if (!mem_available_found)
if (sscanf(line, "MemFree: " STRING_REP_USIZE " kB", (size_t*)&freemem) == 1) if (string_starts_with_size(line, PROC_MEMINFO_MEM_AVAILABLE_TAG,
freemem *= 1024; STRLEN_CONST(PROC_MEMINFO_MEM_AVAILABLE_TAG)))
if (sscanf(line, "Buffers: " STRING_REP_USIZE " kB", (size_t*)&buffers) == 1) {
buffers *= 1024; mem_available_found = true;
if (sscanf(line, "Cached: " STRING_REP_USIZE " kB", (size_t*)&cached) == 1) sscanf(line, PROC_MEMINFO_MEM_AVAILABLE_TAG " %lu kB", &mem_available);
cached *= 1024; break;
}
if (!mem_free_found)
if (string_starts_with_size(line, PROC_MEMINFO_MEM_FREE_TAG,
STRLEN_CONST(PROC_MEMINFO_MEM_FREE_TAG)))
{
mem_free_found = true;
sscanf(line, PROC_MEMINFO_MEM_FREE_TAG " %lu kB", &mem_free);
}
if (!buffers_found)
if (string_starts_with_size(line, PROC_MEMINFO_BUFFERS_TAG,
STRLEN_CONST(PROC_MEMINFO_BUFFERS_TAG)))
{
buffers_found = true;
sscanf(line, PROC_MEMINFO_BUFFERS_TAG " %lu kB", &buffers);
}
if (!cached_found)
if (string_starts_with_size(line, PROC_MEMINFO_CACHED_TAG,
STRLEN_CONST(PROC_MEMINFO_CACHED_TAG)))
{
cached_found = true;
sscanf(line, PROC_MEMINFO_CACHED_TAG " %lu kB", &cached);
}
if (!shmem_found)
if (string_starts_with_size(line, PROC_MEMINFO_SHMEM_TAG,
STRLEN_CONST(PROC_MEMINFO_SHMEM_TAG)))
{
shmem_found = true;
sscanf(line, PROC_MEMINFO_SHMEM_TAG " %lu kB", &shmem);
}
} }
fclose(data); /* Close /proc/meminfo */
return freemem - buffers - cached; fclose(meminfo_file);
#else meminfo_file = NULL;
unsigned long long ps = sysconf(_SC_PAGESIZE);
unsigned long long pn = sysconf(_SC_AVPHYS_PAGES); /* Use 'accurate' free memory value, if available */
return ps * pn; if (mem_available_found)
#endif return (uint64_t)mem_available * 1024;
/* ...Otherwise, use estimate */
return (uint64_t)((mem_free + buffers + cached) - shmem) * 1024;
} }
/*#include <valgrind/valgrind.h>*/ /*#include <valgrind/valgrind.h>*/