Skip to content

Conversation

@caspernorrbin
Copy link
Member

@caspernorrbin caspernorrbin commented Sep 24, 2025

Hi everyone,

The current memory-related code paths in Linux are unclear and convoluted, with responsibilities and data flow crossing between os::Linux and various container-related layers.

For example, consider the call sequence for os::available_memory():

os::available_memory()
        |
        v
os::Linux::available_memory()
        |--------------------------------------------
        v                                           v    
OSContainer::memory_limit_in_bytes()       or return host physical memory
        |
        v
CgroupSubsystem::memory_limit_in_bytes()
        |--------------------------------------------
        v                                           v
return os::Linux::physical_memory()        or return cgroup v1/v2 limit

This structure is difficult to follow. Calls move between os::Linux and container subsystems in a confusing manner. Ideally, each component should be responsible only for its relevant functionality:

  • os::Linux should focus solely on actual machine memory values.
  • CgroupSubsystem should focus exclusively on cgroup memory limits.
  • The selection of which value to use should occur at the os layer, based on whether the environment is containerized.

A revised structure separates these responsibilities:

os::available_memory()
        |--------------------------------------------
        v                                           v
OSContainer::memory_limit_in_bytes()       or os::Linux::available_memory()                       
        |--------------------------------------------
        v                                           v
CgroupSubsystem::memory_limit_in_bytes()   os::Linux::physical_memory()
        |
        v
return bounded cgroup v1/v2 limit

With these changes:

  • os::Linux only retrieves machine values.
  • CgroupSubsystem works exclusively with cgroup limits.
  • OSContainer fetches and passes bounds for the cgroup values.
  • The decision of container or machine value is done in the os layer.

The concrete code changes include:

  • Moving container selection logic from os::Linux::{available/free}_memory() to os:{available/free}_memory(), so os::Linux now only deals with machine values (was already the case for os::physical_memory()).
  • Moving os::Linux::available_memory_in_container() to OSContainer instead, removing container-specific logic from os::Linux. Also refactored to use the new bool and reference interface introduced in JDK-8357086.
  • Moving accessing host values from CgroupSubsystem to OSContainer, and abstracting CgroupSubsystem to use more generic limits (e.g. upper_mem_bound) instead of a direct system reference (e.g. host_mem).

Note: I intentionally kept the julong parameter types unchanged. I believe it's better to update all types simultaneously in JDK-8365606 instead to ensure the change is complete and consistent.

Testing:

  • Oracle tiers 1-3.
  • Container tests on cgroup v1 and v2 systems.

Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed (2 reviews required, with at least 1 Reviewer, 1 Author)

Issue

  • JDK-8292984: Refactor internal container-related interfaces for clarity (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/27470/head:pull/27470
$ git checkout pull/27470

Update a local copy of the PR:
$ git checkout pull/27470
$ git pull https://git.openjdk.org/jdk.git pull/27470/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 27470

View PR using the GUI difftool:
$ git pr show -t 27470

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/27470.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 24, 2025

👋 Welcome back cnorrbin! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Sep 24, 2025

@caspernorrbin This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8292984: Refactor internal container-related interfaces for clarity

Reviewed-by: sgehwolf, eosterlund

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 39 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the hotspot-runtime hotspot-runtime-dev@openjdk.org label Sep 24, 2025
@openjdk
Copy link

openjdk bot commented Sep 24, 2025

@caspernorrbin The following label will be automatically applied to this pull request:

  • hotspot-runtime

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the rfr Pull request is ready for review label Sep 24, 2025
@mlbridge
Copy link

mlbridge bot commented Sep 24, 2025

Webrevs

Copy link
Contributor

@jerboaa jerboaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but should probably wait until JDK-8367485 is integrated.

@jerboaa
Copy link
Contributor

jerboaa commented Sep 25, 2025

/reviewers 2

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Sep 25, 2025
@openjdk
Copy link

openjdk bot commented Sep 25, 2025

@jerboaa
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 1 Reviewer, 1 Author).

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Sep 25, 2025
@caspernorrbin
Copy link
Member Author

Looks good to me, but should probably wait until JDK-8367485 is integrated.

Thank you for reviewing! I've now merged those changes in.

julong phys_mem = static_cast<julong>(os::Linux::physical_memory());
log_trace(os, container)("total physical memory: " JULONG_FORMAT, phys_mem);
jlong mem_limit = contrl->controller()->read_memory_limit_in_bytes(phys_mem);
jlong mem_limit = contrl->controller()->read_memory_limit_in_bytes(upper_bound);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes the logging at the trace level for the upper bound. Intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was intentional. Both cgroup v1/v2 already log the upper bound in read_memory_limit_in_bytes() at debug level, along with more information. It felt unnecessary logging the same value twice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

Copy link
Contributor

@jerboaa jerboaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine.

Copy link
Contributor

@fisk fisk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 2, 2025
@caspernorrbin
Copy link
Member Author

Thank you for the reviews!

/integrate

@openjdk
Copy link

openjdk bot commented Oct 2, 2025

Going to push as commit 5252262.
Since your change was applied there have been 39 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Oct 2, 2025
@openjdk openjdk bot closed this Oct 2, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Oct 2, 2025
@openjdk
Copy link

openjdk bot commented Oct 2, 2025

@caspernorrbin Pushed as commit 5252262.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotspot-runtime hotspot-runtime-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

3 participants