Skip to content

Conversation

@minborg
Copy link
Contributor

@minborg minborg commented Feb 7, 2025

Going forward, converting older JDK code to use the relatively new FFM API requires system calls that can provide errno and the likes to explicitly allocate a MemorySegment to capture potential error states. This can lead to negative performance implications if not designed carefully and also introduces unnecessary code complexity.

Hence, this PR proposes to add a JDK internal method handle adapter that can be used to handle system calls with errno, GetLastError, and WSAGetLastError.

It relies on an efficient carrier-thread-local cache of memory regions to allide allocations.

Here are some benchmarks that ran on a platform thread and virtual threads respectively:

Benchmark                                                  Mode  Cnt   Score   Error  Units
CaptureStateUtilBench.OfVirtual.adaptedSysCallFail         avgt   30  24.193 ? 0.268  ns/op
CaptureStateUtilBench.OfVirtual.adaptedSysCallSuccess      avgt   30   8.268 ? 0.080  ns/op
CaptureStateUtilBench.OfVirtual.explicitAllocationFail     avgt   30  42.076 ? 1.003  ns/op
CaptureStateUtilBench.OfVirtual.explicitAllocationSuccess  avgt   30  21.801 ? 0.138  ns/op
CaptureStateUtilBench.OfVirtual.tlAllocationFail           avgt   30  23.265 ? 0.087  ns/op
CaptureStateUtilBench.OfVirtual.tlAllocationSuccess        avgt   30   8.285 ? 0.155  ns/op

CaptureStateUtilBench.adaptedSysCallFail                   avgt   30  23.033 ? 0.423  ns/op
CaptureStateUtilBench.adaptedSysCallSuccess                avgt   30   3.676 ? 0.104  ns/op  // <- Happy path using an internal pool

CaptureStateUtilBench.explicitAllocationFail               avgt   30  42.023 ? 0.736  ns/op
CaptureStateUtilBench.explicitAllocationSuccess            avgt   30  22.013 ? 0.648  ns/op  // <- Allocating memory upon each invocation

CaptureStateUtilBench.tlAllocationFail                     avgt   30  22.050 ? 0.233  ns/op
CaptureStateUtilBench.tlAllocationSuccess                  avgt   30   3.756 ? 0.056  ns/op  // <- Using the pool explicitly from Java code

Adapted system call:

        return (int) ADAPTED_HANDLE.invoke(0, 0); // Uses a MH-internal pool

Explicit allocation:

        try (var arena = Arena.ofConfined()) {
            return (int) HANDLE.invoke(arena.allocate(4), 0, 0);
        }

Thread Local allocation:

        try (var arena = POOLS.take()) {
            return (int) HANDLE.invoke(arena.allocate(4), 0, 0); // Uses a manually specified pool
        }

The adapted system call exhibits a ~6x performance improvement over the existing "explicit allocation" scheme for the happy path on platform threads. Because there needs to be sharing across threads for virtual-tread-capable carrier threads, these are a bit slower ("only" ~2.5x faster).

This PR relates to another PR we had to back out. This PR attempts to ensure, that the problems encountered there do not surface in this PR.

Tested and passed tiers 1-3.


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 2 Reviewers)

Issue

  • JDK-8347408: Create an internal method handle adapter for system calls with errno (Enhancement - P4)

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 23517

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Feb 7, 2025

👋 Welcome back pminborg! 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 Feb 7, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Feb 7, 2025

@minborg 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 Feb 7, 2025
@minborg minborg marked this pull request as ready for review February 7, 2025 15:23
@openjdk openjdk bot added the rfr Pull request is ready for review label Feb 7, 2025
@mlbridge
Copy link

mlbridge bot commented Feb 7, 2025

@minborg
Copy link
Contributor Author

minborg commented Feb 12, 2025

/reviewers 2 reviewer

@openjdk
Copy link

openjdk bot commented Feb 12, 2025

@minborg
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 2 Reviewers).

@minborg
Copy link
Contributor Author

minborg commented Feb 13, 2025

Unfortunately, this PR would evolve in a direction that is hard to maintain if appropriate changes were made to cover some virtual thread corner cases. So, we need to rethink the overall solution and come back with another PR. Thanks for your feedback so far.

@minborg minborg closed this Feb 13, 2025
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 rfr Pull request is ready for review

Development

Successfully merging this pull request may close these issues.

1 participant