Skip to content

[GR-49300] Break the initialization cycle in NIO/cgroups code. #7553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 7, 2023

Conversation

graalvmbot
Copy link
Collaborator

No description provided.

First, we use a separate accessor for page-alignedness as it doesn't
need the more sophisticated initialization of the directMemory field.

Next, ensure PhysicalMemory initialization is serialized and when it is,
set directMemory to a static value so that the container code can finish
initialization without introducing a cyle. The final directMemory value
based on the heap size is then published to JDK code by setting the VM
init level to 1. Therefore, application code would use the non-static
value as the upper bound.

Closes: #556
@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Oct 5, 2023
Copy link
Collaborator

@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.

Thanks @christianhaeubl! This is sufficient for our use-case. +1 from me.

@christianhaeubl
Copy link
Member

christianhaeubl commented Oct 6, 2023

@jerboaa: I pushed another small fix, see the top commit.

Comment on lines +59 to +77
/**
* {@code java.nio.Bits} caches the max. direct memory size in the field {@code MAX_MEMORY}. We
* disable this cache and always call {@link DirectMemoryAccessors#getDirectMemory()} instead, which
* uses our own cache. Otherwise, it could happen that {@code MAX_MEMORY} caches a temporary value
* that is used during early VM startup, before {@link PhysicalMemory} is fully initialized.
*/
final class MaxMemoryAccessor {
// Checkstyle: stop

static long getMAX_MEMORY() {
return DirectMemoryAccessors.getDirectMemory();
}

static void setMAX_MEMORY(long value) {
/* Nothing to do. */
}

// Checkstyle: resume
}
Copy link
Collaborator

@jerboaa jerboaa Oct 6, 2023

Choose a reason for hiding this comment

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

Instead of going through the accessor an alternative could be to leaverage the init hooks in JDK code and actually set the init level to 1 once we know the real MaxDirectMemorySize (based on the heap size). For example here: https://github.com/oracle/graal/pull/7553/files#diff-db72af86eee732c8a7e3e53319b32fdd92ee0ec00735f413a02858c9dc0aff4dR126. That would mimic what HotSpot itself is doing. FWIW, that's what my original code was intending to do.

Both approaches work.

Copy link
Member

Choose a reason for hiding this comment

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

The problem is that Native Image is already multi-threaded when this code is executed. Modifying the init level may have unexpected effects on other threads.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, thanks.

@graalvmbot graalvmbot merged commit 7e7dae5 into master Oct 7, 2023
@graalvmbot graalvmbot deleted the chaeubl/GR-49300 branch October 7, 2023 01:38
@jerboaa
Copy link
Collaborator

jerboaa commented Oct 9, 2023

@christianhaeubl Thanks for getting this fix in! Much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants