Native call framework prototype
Vladimir Ivanov
vladimir.x.ivanov at oracle.com
Thu Feb 19 18:23:26 UTC 2015
Hi,
As you, probably, already noticed, I've just pushed raw prototype of
java.lang.invoke-based native function invocation framework, inspired by
[1].
The first version is a raw prototype which is able to call getpid (see
jdk/test/java/lang/invoke/NativeCallTest.java):
MethodHandle mh =
LOOKUP.findNative(null, "getpid", MethodType.methodType(int.class));
int pid = (int) mh.invokeExact();
NativeMethodHandle and NativeEntryPoint (MemberName counterpart) are
used to represent native function.
The implementation relies on 2 VM primitives:
MethodHandle.linkToNative() & Unsafe.findNativeAddress()
Native part is mostly a skeleton for future work:
* linkToNative adapter generation is present only on x64;
* supported adapter signature is trivial, just enough to call getpid()
* generated adapter doesn't do thread state transition before calling
native function (it corresponds to privileged "raw" invocation mode in [2]);
On the other hand, Java part is in much better shape. No access &
security checks yet, but once VM can generate more adapters, everything
should "just work" :-)
The next steps are:
- extend linkToNative adapter to work on all Java signatures in raw
invocation mode
- introduce "state transitioning" calling mode (like JNI does) and
different calling conventions support; the idea is to encapsulate such
info in NativeEntryPoint
- JIT-compiler support for "raw" mode: issue direct native call
instead of using linkToNative adapter when possible
- try some simple Java<->C type conversions (like String <-> char*);
they can be done on LambdaForm level
- try commoning thread state transitions in JIT-compiled code in
order to reduce native call overhead
Best regards,
Vladimir Ivanov
[1] http://cr.openjdk.java.net/~jrose/panama/native-call-primitive.html
[2] https://blogs.oracle.com/jrose/entry/the_isthmus_in_the_vm
More information about the panama-dev
mailing list