Skip to content

8231269: CompileTask::is_unloaded is slow due to JNIHandles type checks #24018

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

Open
wants to merge 43 commits into
base: master
Choose a base branch
from

Conversation

shipilev
Copy link
Member

@shipilev shipilev commented Mar 12, 2025

JDK-8163511 made the CompileTask improvement to avoid blocking class unloading if a relevant compile task is in queue. Current code does a sleight-of-hand to make sure the the method* in CompileTask are still valid before using them. Still a noble goal, so we keep trying to do this.

The code tries to switch weak JNI handle with a strong one when it wants to capture the holder to block unloading. Since we are reusing the same field, we have to do type checks like JNIHandles::is_weak_global_handle(_method_holder). Unfortunately, that type-check goes all the way to OopStorage allocation code to verify the handle is really allocated in the relevant OopStorage. This takes internal OopStorage locks, and thus is slow.

This issue is clearly visible in Leyden, when there are lots of CompileTask-s in the queue, dumped by AOT code loader. It also does not help that CompileTask::select_task is effectively quadratic in number of methods in queue, so we end up calling CompileTask::is_unloaded very often.

It is possible to mitigate this issue by splitting the related fields into weak and strong ones. But as Kim mentions in the bug, we should not be using JNI handles here at all, and instead go directly for relevant OopStorage-s. This is what this PR does, among other things that should hopefully make the whole mechanics clearer.

Additional testing:

  • Linux x86_64 server fastdebug, compiler/classUnloading, 100x still passes; these tests are sensitive to bugs in this code
  • Linux x86_64 server fastdebug, all
  • Linux AArch64 server fastdebug, all

Progress

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

Issue

  • JDK-8231269: CompileTask::is_unloaded is slow due to JNIHandles type checks (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24018

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Mar 12, 2025

👋 Welcome back shade! 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 Mar 12, 2025

@shipilev This change is no longer ready for integration - check the PR body for details.

@openjdk
Copy link

openjdk bot commented Mar 12, 2025

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

  • hotspot-compiler

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.

@shipilev
Copy link
Member Author

shipilev commented Mar 17, 2025

You can model the impact it has on Leyden-style scenarios by producing many compile tasks with -Xcomp. For example, on my 5950X desktop and simple "Hello World" program that involves lots of javac compilation:

Benchmark 1: build/linux-x86_64-server-release/images/jdk/bin/java \ 
   -Xcomp -XX:TieredStopAtLevel=1 Hello.java

# Before
  Time (mean ± σ):      1.756 s ±  0.003 s    [User: 1.650 s, System: 0.122 s]
  Range (min … max):    1.752 s …  1.761 s    10 runs

# After
  Time (mean ± σ):      1.742 s ±  0.005 s    [User: 1.636 s, System: 0.123 s]
  Range (min … max):    1.735 s …  1.748 s    10 runs

The effect is mostly due to avoiding OopStorage locks mentioned in PR body.

@shipilev shipilev changed the title 8231269: CompileTask::is_unloaded is slow due to JNIHandles 8231269: CompileTask::is_unloaded is slow due to JNIHandles type checks Mar 17, 2025
@shipilev shipilev force-pushed the JDK-8231269-compile-task-weaks branch from a3e0f91 to 052f54b Compare March 18, 2025 10:20
@shipilev shipilev force-pushed the JDK-8231269-compile-task-weaks branch from 052f54b to cc8345e Compare March 18, 2025 11:43
@shipilev shipilev marked this pull request as ready for review March 18, 2025 14:25
@openjdk openjdk bot added the rfr Pull request is ready for review label Mar 18, 2025
@mlbridge
Copy link

mlbridge bot commented Mar 18, 2025

@dean-long
Copy link
Member

/label hotspot-gc

@openjdk
Copy link

openjdk bot commented Mar 26, 2025

@dean-long
The hotspot-gc label was successfully added.

Copy link
Contributor

@iwanowww iwanowww left a comment

Choose a reason for hiding this comment

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

Good catch, Aleksey!

What do you think about making 1 step further and encapsulating weak/strong reference handling into a helper class?

Also, as an optimization idea: seems like weak + strong handles form a union (none -> weak -> strong). So, once a strong reference is captured, corresponding weak handle can be cleared straight away.

@shipilev
Copy link
Member Author

shipilev commented Mar 31, 2025

What do you think about making 1 step further and encapsulating weak/strong reference handling into a helper class?

Yes. I think @veresov would want to have some of this for persistent profiles JEP and TrainingData.

I pushed the WIP thing into PR. That only covers the "method unload blocker" part. But I think it should really go further, and encapsulate Method* completely, since it is not safe to touch Method* when its holder is not blocked for unload. We dodge the problems now by obsessively checking is_unloading() all over the place, but we need to guarantee this more mechanically. I'll take a look at that tomorrow.

Also, as an optimization idea: seems like weak + strong handles form a union (none -> weak -> strong). So, once a strong reference is captured, corresponding weak handle can be cleared straight away.

It turns out to be necessary to avoid touching peek() when in the wrong thread state, when we encapsulate Method* as well.

@iwanowww
Copy link
Contributor

Nice! I really like how it shapes out.

@shipilev
Copy link
Member Author

Pushed the Method* encapsulation. SA needs fixes now, but I'll test how well this works on other tests.

Copy link
Contributor

@iwanowww iwanowww left a comment

Choose a reason for hiding this comment

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

I feel uneasy about all the complications introduced by coordination between accessors.
It looks like supporting concurrent release operation adds a lot of complexity.
Weak -> strong transition is monotonic, so shouldn't need as much care.

What do you think about making release operation part of CompileTask recycling (e.g., in UnloadableMethodHandle destructor)? By the time it happens, there should not be any other users of the task. (Otherwise, recycling concurrently accessed task is unsafe anyway).

Copy link
Member

@xmas92 xmas92 left a comment

Choose a reason for hiding this comment

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

Just a drive by comment.

Not sure what our opinion is w.r.t. mutable, but how do we feel about typing the spin lock as mutable and keep is_safe() and method*() const. We can then keep the old signature for CompileTask::is_unloaded() CompileTask::method() and ArenaStatCounter::ArenaStatCounter(...).

@shipilev
Copy link
Member Author

Not sure what our opinion is w.r.t. mutable, but how do we feel about typing the spin lock as mutable and keep is_safe() and method*() const.

I like this a lot! Dropping const just to satisfy spin lock (an implementation detail) felt really awkward. New version uses mutable.

@openjdk
Copy link

openjdk bot commented Jun 5, 2025

@shipilev this pull request can not be integrated into master due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout JDK-8231269-compile-task-weaks
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push

@openjdk openjdk bot added the merge-conflict Pull request has merge conflict with target branch label Jun 5, 2025
@bridgekeeper
Copy link

bridgekeeper bot commented Jun 24, 2025

@shipilev This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply issue a /touch or /keepalive command to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@openjdk openjdk bot removed the merge-conflict Pull request has merge conflict with target branch label Jul 9, 2025
@kimbarrett
Copy link

Not sure what our opinion is w.r.t. mutable, but how do we feel about typing the spin lock as mutable and keep is_safe() and method*() const.

I like this a lot! Dropping const just to satisfy spin lock (an implementation detail) felt really awkward. New version uses mutable.

Just a drive-by reply. mutable is a C++98 (and before, I think) feature, with many uses in HotSpot. Using it here
seems fine to me.

@kimbarrett
Copy link

This is a cleaner way to do this. I believe it's what we discussed with Kim. He can confirm.

Yes, I think this looks like the sort of thing I had in mind when we were discussing it back whenever that was.

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

Successfully merging this pull request may close these issues.

7 participants