RFR: 8334489: Add function os::used_memory
Johan Sjölen
jsjolen at openjdk.org
Wed Jun 19 16:43:09 UTC 2024
On Wed, 19 Jun 2024 12:30:30 GMT, Severin Gehwolf <sgehwolf at openjdk.org> wrote:
>> A function os::used_memory is needed for GC ergonomics.
>>
>> A naïve implementation is:
>>
>> ```c++
>> julong os::used_memory() {
>> return os::physical_memory() - os::available_memory();
>> }
>>
>>
>> This function does not work well in a containerized environment, as the amount of physical memory may change. To understand why, we must look under the hood.
>>
>> ```c++
>> julong os::used_memory() {
>> return os::physical_memory() - os::available_memory();
>> }
>>
>> // can be expanded into
>>
>> julong os::used_memory() {
>> // The os::physical_memory() call
>> julong mem_physical1 = OSContainer::memory_limit_in_bytes();
>> // The os::available_memory() call
>> julong mem_used = OSContainer::memory_usage_in_bytes();
>> julong mem_physical2 = OSContainer::memory_limit_in_bytes();
>>
>> // Uh-oh: mem_physical1 may differ from mem_physical2 at this point
>> // That means that this number is wrong
>> return mem_physical1 - (mem_physical2 - mem_used);
>> }
>>
>>
>> The fix is to expose OSContainer::memory_usage_in_bytes if it's available, as this call does not depend on OSContainer::memory_limit_in_bytes. The default implementation will use the naïve implementation.
>
> src/hotspot/os/linux/os_linux.cpp line 275:
>
>> 273:
>> 274: julong os::used_memory() {
>> 275: if (OSContainer::is_containerized()) {
>
> I'm curious. What's the intended use case of this? https://bugs.openjdk.org/browse/JDK-8329758 perhaps? If this is getting used more broadly, https://bugs.openjdk.org/browse/JDK-8261242, would seem to get bumped in priority as that still affects physical Linux systems.
I think you hit the nail on the head regarding intended use case. It's probably important that that issue gets resolved, yes.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/19772#discussion_r1646504917
More information about the hotspot-runtime-dev
mailing list