Skip to content
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

8285447: StackWalker minimal batch size should be optimized for getCallerClass #15642

Closed
wants to merge 4 commits into from

Conversation

mlchung
Copy link
Member

@mlchung mlchung commented Sep 8, 2023

Typically it will find the caller class at the second stack frame from the frame of executing StackWalker::getCallerClass. The initial size of the buffer can be changed from 8 to 4 (the top 2 elements are reserved for implementation use). If it fetches another batch, getCallerClass may be invoked via core reflection, so subsequent batches can be increased to a larger size. This PR also moves the benchmark for getCallerClass in its own file because it does not need to test with different depth and can be separated from StackWalkBench.


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-8285447: StackWalker minimal batch size should be optimized for getCallerClass (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 15642

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 8, 2023

👋 Welcome back mchung! 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 8, 2023

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

  • core-libs

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 core-libs core-libs-dev@openjdk.org label Sep 8, 2023
@openjdk
Copy link

openjdk bot commented Sep 8, 2023

@mlchung Please do not rebase or force-push to an active PR as it invalidates existing review comments. Note for future reference, the bots always squash all changes into a single commit automatically as part of the integration. See OpenJDK Developers’ Guide for more information.

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

mlbridge bot commented Sep 8, 2023

Webrevs

Copy link
Member

@simonis simonis left a comment

Choose a reason for hiding this comment

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

In general looks good. But once you on this, I suggest to add the following additional optimizations:

  • FrameBuffer.START_POS is 2 but as far as I can see, currently only the 0th element is reserved for passing a "magic" object passed between the JVM and Java code. So this should be set to 1.
  • MIN_BATCH_SIZE should be defined as in terms of FrameBuffer.START_POS (i.e. FrameBuffer.START_POS + 1)
  • StackFrameTraverser.batchSize() should be changed to really honor the estimateDepth of StackWalker.getInstance(.., estimateDepth) (currently it is always SMALL_BATCH == 8 even if the caller specifies a smaller number):
-                int initialBatchSize = Math.max(walker.estimateDepth(), SMALL_BATCH);
+                int initialBatchSize = Math.max(walker.estimateDepth() + FrameBuffer.START_POS, MIN_BATCH_SIZE);
  • In CallerClassFinder.initFrameBuffer() you can then use this.frameBuffer = new ClassFrameBuffer(walker, FrameBuffer.START_POS + 2).

With these changes you would:

  • save one more frame for the getCallerClass() case
  • save more frames for any user supplied estimateDepth values smaller than 8
  • don't implicitly expose FrameBuffer.START_POS to user space. Currently the user supplied estimateDepth value will be implicitly subtracted by FrameBuffer.START_POS
  • make all the internal size computations depend explicitly on FrameBuffer.START_POS for better maintainability.

*
* So start the initial batch size with the minimum size.
* If it fetches the second batch, getCallerClass may be invoked via
* core reflection, can increase the next batch size.
Copy link
Member

Choose a reason for hiding this comment

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

This sentence reads strange to me. Maybe "If it fetches the second batch, getCallerClass may be invoked via core reflection, so the next batch size will be increased."?

Copy link
Member Author

Choose a reason for hiding this comment

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

I updated and moved the comment to batchSize method.

@mlchung
Copy link
Member Author

mlchung commented Sep 11, 2023

In general looks good. But once you on this, I suggest to add the following additional optimizations:

  • FrameBuffer.START_POS is 2 but as far as I can see, currently only the 0th element is reserved for passing a "magic" object passed between the JVM and Java code. So this should be set to 1.
    ....

With these changes you would:

  • save one more frame for the getCallerClass() case

Note that this only affects the number of elements allocated. getCallerClass only walks 2 frames in the first batch regardless of the number of reserved elements.

Benchmark                        Mode  Cnt  Score   Error  Units
CallerClassBench.getCallerClass  avgt   15  0.361 ? 0.003  us/op   // num of reserved elements = 1
CallerClassBench.getCallerClass  avgt   15  0.370 ? 0.009  us/op  // num of reserved elements = 2

OTOH, as the implementation only needs one reserved element, I think it's good to change the number of reserved elements to 1.

Copy link
Member

@simonis simonis 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. Thanks for doing the proposed changes.

@@ -764,6 +763,10 @@ protected void initFrameBuffer() {

@Override
protected int batchSize(int lastBatchFrameCount) {
// this method is only called when the caller class is not found in
Copy link
Member

Choose a reason for hiding this comment

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

Minor nit: start sentence with an uppercase letter.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll leave it as is.

@openjdk
Copy link

openjdk bot commented Sep 12, 2023

@mlchung 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:

8285447: StackWalker minimal batch size should be optimized for getCallerClass

Reviewed-by: simonis

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 30 new commits pushed to the master branch:

  • fc3e826: 8314832: Few runtime/os tests ignore vm flags
  • 347beb2: 8315998: Remove dead ClassLoaderDataGraphKlassIteratorStatic
  • 50158f3: 8316002: Remove unnecessary seen_dead_loader in ClassLoaderDataGraph::do_unloading
  • 6f2684b: 8315948: JDK-8315818 broke Xcomp on libgraal
  • 8b4f9a8: 8315990: Amend problemlisted tests to proper position
  • 455c471: 8313277: Resolve multiple definition of 'normalize' when statically linking JDK native libraries with user code
  • 3b422d0: 8316094: Problemlist compiler/rangechecks/TestRangeCheckHoistingScaledIV.java
  • f55e799: 8316038: Fix doc typos in java.io.Console and java.util.Scanner
  • 9480078: 8315550: G1: Fix -Wconversion warnings in g1NUMA
  • d08258f: 8315938: Deprecate for removal Unsafe methods that have standard APIs for many releases
  • ... and 20 more: https://git.openjdk.org/jdk/compare/a62c48b87e814b5b1f4c8089f9ff354156f92f69...master

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 ready Pull request is ready to be integrated label Sep 12, 2023
@mlchung
Copy link
Member Author

mlchung commented Sep 12, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Sep 12, 2023

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

  • fc3e826: 8314832: Few runtime/os tests ignore vm flags
  • 347beb2: 8315998: Remove dead ClassLoaderDataGraphKlassIteratorStatic
  • 50158f3: 8316002: Remove unnecessary seen_dead_loader in ClassLoaderDataGraph::do_unloading
  • 6f2684b: 8315948: JDK-8315818 broke Xcomp on libgraal
  • 8b4f9a8: 8315990: Amend problemlisted tests to proper position
  • 455c471: 8313277: Resolve multiple definition of 'normalize' when statically linking JDK native libraries with user code
  • 3b422d0: 8316094: Problemlist compiler/rangechecks/TestRangeCheckHoistingScaledIV.java
  • f55e799: 8316038: Fix doc typos in java.io.Console and java.util.Scanner
  • 9480078: 8315550: G1: Fix -Wconversion warnings in g1NUMA
  • d08258f: 8315938: Deprecate for removal Unsafe methods that have standard APIs for many releases
  • ... and 20 more: https://git.openjdk.org/jdk/compare/a62c48b87e814b5b1f4c8089f9ff354156f92f69...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Sep 12, 2023

@mlchung Pushed as commit d75d977.

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

Comment on lines +44 to +45
static final StackWalker INST = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);

Copy link
Member

Choose a reason for hiding this comment

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

Could DROP_METHOD_INFO also be used here?

Copy link
Member Author

@mlchung mlchung Sep 12, 2023

Choose a reason for hiding this comment

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

yes but it's irrelevant in this benchmark as getCallerClass should be independent to DROP_METHOD_INFO option. The implementation always drops method info in the implementation. It does not affect the result.

Copy link
Member

Choose a reason for hiding this comment

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

Ah - understood, thanks.

@mlchung mlchung deleted the JDK-8285447 branch October 4, 2023 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated
3 participants