RFR: 8305770: os::Linux::available_memory() should refer MemAvailable in /proc/meminfo [v2]
Thomas Stuefe
stuefe at openjdk.org
Mon May 8 07:47:30 UTC 2023
On Fri, 5 May 2023 14:27:25 GMT, Yasumasa Suenaga <ysuenaga at openjdk.org> wrote:
>> `os::Linux::available_memory()` returns available memory from cgroups or sysinfo(2). In case of the process which run on out of container, that value is based on `freeram` from sysinfo(2).
>>
>> `freeram` is equivalent to `MemFree` in `/proc/meminfo` [1]. However it means just a free RAM. We should use `MemAvailable` when we want to know how much memory is available for the process [2]. `MemAvailable` is available in modern Linux kernel, and it has been backported some older kernels (e.g. RHEL). In `sar` from sysstat, it refers that value and shows it as `kbavail` [3].
>>
>> AFAIK PhysicalMemory event in JFR depends on `os::Linux::available_memory()`, and it is used in automated analysis in JMC. So the JFR/JMC user could misunderstand physical memory was exhausted even if the memory was available enough.
>>
>> [1] https://github.com/torvalds/linux/blob/c9c3395d5e3dcc6daee66c6908354d47bf98cb0c/fs/proc/meminfo.c#L59
>> [2] https://docs.kernel.org/filesystems/proc.html?highlight=memavailable
>> [3] https://github.com/sysstat/sysstat/blob/ac1df71ca252c158e8d418ded93e5ed52f5e8765/rd_stats.c#L325-L328
>
> Yasumasa Suenaga has updated the pull request incrementally with one additional commit since the last revision:
>
> Introduce os::free_memory
Changes requested by stuefe (Reviewer).
src/hotspot/os/linux/os_linux.cpp line 224:
> 222:
> 223: julong os::Linux::available_memory() {
> 224: julong avail_mem = 0UL;
I would use a signed value, e.g. ssize_t, instead, and use -1 as a marker value. 0 may possibly be a real value you read.
src/hotspot/os/linux/os_linux.cpp line 245:
> 243: julong mem_available;
> 244: if (fscanf(fp, "MemAvailable: " JULONG_FORMAT " kB", &mem_available) == 1) {
> 245: avail_mem = mem_available;
I think this mem_available var is not needed, you can pass avail_mem directly.
src/hotspot/os/linux/os_linux.cpp line 253:
> 251: if (avail_mem == 0UL) {
> 252: avail_mem = free_memory();
> 253: }
Don't you need to multiply the returned value with 1024?
-------------
PR Review: https://git.openjdk.org/jdk/pull/13398#pullrequestreview-1416274312
PR Review Comment: https://git.openjdk.org/jdk/pull/13398#discussion_r1187119446
PR Review Comment: https://git.openjdk.org/jdk/pull/13398#discussion_r1187131817
PR Review Comment: https://git.openjdk.org/jdk/pull/13398#discussion_r1187132435
More information about the hotspot-compiler-dev
mailing list