RFR: 8360651: Create OSContainer API for memory limit [v2]
Stefan Johansson
sjohanss at openjdk.org
Thu Sep 18 10:39:58 UTC 2025
On Mon, 8 Sep 2025 17:54:10 GMT, Severin Gehwolf <sgehwolf at openjdk.org> wrote:
>> Please review this small addition to add a new `OSContainer::has_memory_limit()` API (Linux only - as with the entire OSContainer API) in preparation for [JDK-8350596](https://bugs.openjdk.org/browse/JDK-8350596) which proposes to increase the default `MaxRAMPercentage` when this new API returns true. The patch is pretty trivial. It's only the testing which amounts to the most lines in this patch.
>>
>> Testing:
>> - [x] GHA
>> - [x] Hotspot container tests on x86_64 Linux on cgroup v1 and cgroup v2 (including the new tests).
>>
>> Thoughts?
>
> Severin Gehwolf has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains four additional commits since the last revision:
>
> - Merge branch 'master' into jdk-8360651-mem-limit-api
> - MemoryLimitTest whitespace fixes.
> - TestContainerMemory whitespace fixes.
> - 8360651: Create OSContainer API for memory limit
If you agree with my cleanup suggestion below feel free to incorporate it. Otherwise I'm ok with the change as is.
src/hotspot/os/linux/osContainer_linux.cpp line 81:
> 79: bool any_mem_cpu_limit_present = false;
> 80: bool controllers_read_only = cgroup_subsystem->is_containerized();
> 81: _has_memory_limit = cgroup_subsystem->memory_limit_in_bytes() > 0;
I found those local `bool` a bit confusing and I don't really think they are needed. Played around a bit with also adding a _has_cpu_limit and ended up with the code below. To me that's a bit more readable, but a slightly larger change also adding has_cpu_limit. What do you think?
* Step 1.) covers the basic in container use-cases. Step 2.) ensures
* that limits enforced by other means (e.g. systemd slice) are properly
* detected.
*/
check_and_set_limits();
const char *reason;
if (cgroup_subsystem->is_containerized()) {
// in-container case
_is_containerized = true;
reason = " because all controllers are mounted read-only (container case)";
} else {
// We can be in one of two cases:
// 1.) On a physical Linux system without any limit
// 2.) On a physical Linux system with a limit enforced by other means (like systemd slice)
if (has_cpu_limit() || has_memory_limit()) {
_is_containerized = true;
reason = " because either a cpu or a memory limit is present";
} else {
// Not containerized
reason = " because no cpu or memory limit is present";
}
}
log_debug(os, container)("OSContainer::init: is_containerized() = %s%s",
_is_containerized ? "true" : "false",
reason);
}
void OSContainer::check_and_set_limits() {
// Check for CPU limit
if (os::Linux::active_processor_count() != cgroup_subsystem->active_processor_count()) {
_has_cpu_limit = true;
}
// Check for memory limit
if (cgroup_subsystem->memory_limit_in_bytes() > 0) {
_has_memory_limit = true;
}
}
-------------
Marked as reviewed by sjohanss (Reviewer).
PR Review: https://git.openjdk.org/jdk/pull/26020#pullrequestreview-3238630631
PR Review Comment: https://git.openjdk.org/jdk/pull/26020#discussion_r2358359868
More information about the hotspot-dev
mailing list