From 05a63d80b9c1e312512c707ccf6b255c16a9edf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Sj=C3=B6len?= Date: Mon, 24 Jun 2024 07:51:01 +0000 Subject: [PATCH] 8334489: Add function os::used_memory Reviewed-by: eosterlund, dholmes, stuefe --- src/hotspot/share/runtime/os.cpp | 17 +++++++++++++++++ src/hotspot/share/runtime/os.hpp | 1 + 2 files changed, 18 insertions(+) diff --git a/src/hotspot/share/runtime/os.cpp b/src/hotspot/share/runtime/os.cpp index bdf93e1d3b4..9860251fc33 100644 --- a/src/hotspot/share/runtime/os.cpp +++ b/src/hotspot/share/runtime/os.cpp @@ -78,6 +78,10 @@ #include "utilities/macros.hpp" #include "utilities/powerOfTwo.hpp" +#ifdef LINUX +#include "osContainer_linux.hpp" +#endif + #ifndef _WINDOWS # include #endif @@ -2064,6 +2068,19 @@ static void assert_nonempty_range(const char* addr, size_t bytes) { p2i(addr), p2i(addr) + bytes); } +julong os::used_memory() { +#ifdef LINUX + if (OSContainer::is_containerized()) { + jlong mem_usage = OSContainer::memory_usage_in_bytes(); + if (mem_usage > 0) { + return mem_usage; + } + } +#endif + return os::physical_memory() - os::available_memory(); +} + + bool os::commit_memory(char* addr, size_t bytes, bool executable) { assert_nonempty_range(addr, bytes); bool res = pd_commit_memory(addr, bytes, executable); diff --git a/src/hotspot/share/runtime/os.hpp b/src/hotspot/share/runtime/os.hpp index ce7a07d4c43..f3f44ddb2e6 100644 --- a/src/hotspot/share/runtime/os.hpp +++ b/src/hotspot/share/runtime/os.hpp @@ -336,6 +336,7 @@ class os: AllStatic { // than "free" memory (`MemFree` in `/proc/meminfo`) because Linux can free memory // aggressively (e.g. clear caches) so that it becomes available. static julong available_memory(); + static julong used_memory(); static julong free_memory(); static jlong total_swap_space();