# HG changeset patch # User redestad # Date 1571264342 -7200 # Node ID 21a92562f0c24b62ae2a5569256840b68a151db1 # Parent 562bf187808921c14f8fb35b2d0f349c7d9d8cac 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation Reviewed-by: bobv, sgehwolf diff -r 562bf1878089 -r 21a92562f0c2 src/hotspot/os/linux/osContainer_linux.cpp --- a/src/hotspot/os/linux/osContainer_linux.cpp Thu Oct 17 00:00:13 2019 +0200 +++ b/src/hotspot/os/linux/osContainer_linux.cpp Thu Oct 17 00:19:02 2019 +0200 @@ -131,14 +131,34 @@ * hierarchy. If set to true consider also memory.stat * file if everything else seems unlimited */ bool _uses_mem_hierarchy; + volatile jlong _memory_limit_in_bytes; + volatile jlong _next_check_counter; public: CgroupMemorySubsystem(char *root, char *mountpoint) : CgroupSubsystem::CgroupSubsystem(root, mountpoint) { _uses_mem_hierarchy = false; + _memory_limit_in_bytes = -1; + _next_check_counter = min_jlong; + } bool is_hierarchical() { return _uses_mem_hierarchy; } void set_hierarchical(bool value) { _uses_mem_hierarchy = value; } + + bool should_check_memory_limit() { + return os::elapsed_counter() > _next_check_counter; + } + jlong memory_limit_in_bytes() { return _memory_limit_in_bytes; } + void set_memory_limit_in_bytes(jlong value) { + _memory_limit_in_bytes = value; + // max memory limit is unlikely to change, but we want to remain + // responsive to configuration changes. A very short (20ms) grace time + // between re-read avoids excessive overhead during startup without + // significantly reducing the VMs ability to promptly react to reduced + // memory availability + _next_check_counter = os::elapsed_counter() + (NANOSECS_PER_SEC/50); + } + }; CgroupMemorySubsystem* memory = NULL; @@ -461,6 +481,16 @@ * OSCONTAINER_ERROR for not supported */ jlong OSContainer::memory_limit_in_bytes() { + if (!memory->should_check_memory_limit()) { + return memory->memory_limit_in_bytes(); + } + jlong memory_limit = read_memory_limit_in_bytes(); + // Update CgroupMemorySubsystem to avoid re-reading container settings too often + memory->set_memory_limit_in_bytes(memory_limit); + return memory_limit; +} + +jlong OSContainer::read_memory_limit_in_bytes() { GET_CONTAINER_INFO(julong, memory, "/memory.limit_in_bytes", "Memory Limit is: " JULONG_FORMAT, JULONG_FORMAT, memlimit); diff -r 562bf1878089 -r 21a92562f0c2 src/hotspot/os/linux/osContainer_linux.hpp --- a/src/hotspot/os/linux/osContainer_linux.hpp Thu Oct 17 00:00:13 2019 +0200 +++ b/src/hotspot/os/linux/osContainer_linux.hpp Thu Oct 17 00:19:02 2019 +0200 @@ -36,6 +36,7 @@ private: static bool _is_initialized; static bool _is_containerized; + static jlong read_memory_limit_in_bytes(); public: static void init();