From 4b0ef3f79643dc038d1cbf34740a9aabccb97a4e Mon Sep 17 00:00:00 2001 From: Maas-Maarten Zeeman Date: Tue, 3 Sep 2024 15:10:47 +0200 Subject: [PATCH 1/2] os_mon: Fix use available mem for system_memory_high_watermark alarm --- lib/os_mon/c_src/memsup.c | 13 +++++++++---- lib/os_mon/test/memsup_SUITE.erl | 3 +++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/os_mon/c_src/memsup.c b/lib/os_mon/c_src/memsup.c index 96f662da1940..ad4a193d99ba 100644 --- a/lib/os_mon/c_src/memsup.c +++ b/lib/os_mon/c_src/memsup.c @@ -446,9 +446,10 @@ get_extended_mem_apple(memory_ext *me) { } me->free = vm_stat.free_count * mach_page_size; + me->available = (vm_stat.inactive_count + vm_stat.free_count) * mach_page_size; me->total = total_memory_size; me->pagesize = 1; - me->flag = F_MEM_TOTAL | F_MEM_FREE; + me->flag = F_MEM_TOTAL | F_MEM_FREE | F_MEM_AVAIL; } #endif @@ -508,7 +509,11 @@ get_basic_mem(unsigned long *tot, unsigned long *used, unsigned long *pagesize){ } *tot = me.total; *pagesize = me.pagesize; - *used = me.total - me.free; + if (me.flag & F_MEM_AVAIL) { + *used = me.total - me.available; + } else { + *used = me.total - me.free; + } #elif defined(BSD4_4) struct vmtotal vt; long pgsz; @@ -535,9 +540,9 @@ get_basic_mem(unsigned long *tot, unsigned long *used, unsigned long *pagesize){ #elif defined(__APPLE__) { memory_ext me; - me.free = 0; + me.available = 0; get_extended_mem_apple(&me); - *used = me.total - me.free; + *used = me.total - me.available; *tot = total_memory_size; *pagesize = 1; } diff --git a/lib/os_mon/test/memsup_SUITE.erl b/lib/os_mon/test/memsup_SUITE.erl index 1f66ea0afac4..262d3a669625 100644 --- a/lib/os_mon/test/memsup_SUITE.erl +++ b/lib/os_mon/test/memsup_SUITE.erl @@ -758,6 +758,9 @@ improved_system_memory_data(Config) when is_list(Config) -> _ -> {comment, "No available_memory present in result"} end; + {unix,darwin} -> + true = AvailableMemoryPresent, + {comment, "available_memory present in result"}; _ -> ok end. From 6a41f5b8fbf1af3e5c69022fdb3ff0bf9aae51aa Mon Sep 17 00:00:00 2001 From: Maas-Maarten Zeeman Date: Thu, 17 Oct 2024 11:36:35 +0200 Subject: [PATCH 2/2] Add doc explaining calculation of system_memory_high_watermark depends on os --- lib/os_mon/src/memsup.erl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/os_mon/src/memsup.erl b/lib/os_mon/src/memsup.erl index be8188989cfd..6bd6f7df0672 100644 --- a/lib/os_mon/src/memsup.erl +++ b/lib/os_mon/src/memsup.erl @@ -29,7 +29,9 @@ Periodically performs a memory check: - If more than a certain amount of available system memory is allocated, as reported by the underlying operating system, the alarm - `{system_memory_high_watermark, []}` is set. + `{system_memory_high_watermark, []}` is set. How the amount of available + memory is determined depends on the underlying OS and may change as better + values become available. - If any Erlang process `Pid` in the system has allocated more than a certain amount of total system memory, the alarm `{process_memory_high_watermark, Pid}` is set.