RFR: 8357086: os::xxx functions returning memory size should return size_t [v13]
Anton Artemov
duke at openjdk.org
Thu Jul 31 08:59:58 UTC 2025
On Wed, 30 Jul 2025 15:03:48 GMT, Stefan Karlsson <stefank at openjdk.org> wrote:
>> I think we have discussed it above. In `os::free_swap_space(size_t& value)` implementation in `os_linux.cpp` the value returned by `OSContainer::memory_and_swap_limit_in_bytes()` is kept in a signed variable of `jlong` type. Then, if that value is negative, `host_free_swap_val` value will be returned, as that one is non-negative.
>
> My comment above is for the `total_swap_space` function and not the `free_swap_space` function.
Right, I looked at another place, sorry.
In `total_swap_space` one wants to compute the total swap space out of two values, memory_limit and swap_limit, by the following formula:
total_swap_space = (memory_limit + swap_limit) - memory_limit
the 1st summand (in braces) is returned by `OSContainer::memory_and_swap_limit_in_bytes()`, the 2nd by `OSContainer::memory_limit_in_bytes()`. The problem is that `swap_limit` can be unbounded (represented by -1 in the container code). So we end up in returning `static_cast<size_t>(-1)`, which would be 2^64-1 on 64bit platform and is the same as `std::numeric_limits<size_t>::max()`.
In my opinion, it is a good representation of the _unbounded_ value.
There are only 2 places where `os::total_swap_space()` is used, both in JFR-related code. In both cases, the `size_t` values obtained from `os::total_swap_space()` is converted to a signed 64bit value (either jlong or s8, which is the same) with `static_cast`. If `size_t` has the same bitness as `jlong`, then one will get back -1 after conversion, so nothing is changed from the JFR perspective. However, if `size_t` is 32bit, then such conversion will not return -1, but instead a positive value of 2^32-1. It has to be addressed separately.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/25450#discussion_r2244782230
More information about the hotspot-dev
mailing list