RFR: 8347408: Create an internal method handle adapter for system calls with errno [v7]

Per Minborg pminborg at openjdk.org
Thu Feb 13 11:33:22 UTC 2025


On Wed, 12 Feb 2025 16:04:53 GMT, Per Minborg <pminborg at openjdk.org> wrote:

>> 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); // ...
>
> Per Minborg has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Add holding a reference to the original arena in the taken arena

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.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/23517#issuecomment-2656315310


More information about the core-libs-dev mailing list