panama - openjdkcr.openjdk.java.net/~jrose/panama/panama-status-2015-1216.pdf · copyright©%201...
TRANSCRIPT
Panama Overview and Status Update
Mikael Vidstedt December, 2015
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Problem / OpportuniGes • NaGve libraries exist and won’t go away – OS entries, networking, GPU dispatch, BLAS/LAPACK, AVX intrinsics
• InteracGng with them from Java is hard at best – JNI API is complex, slow and hard to secure – True for code, as well as for data
• Other/compeGng runGmes already have, or are in the process of adding, foreign funcGon support – .NET/C#, JRuby, etc.
• Provide alternaGve to Unsafe
2
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
“If non-‐Java programmers find some library useful and easy to access, it should be similarly accessible to Java programmers.”
John Rose, JVM Architect, Oracle Corporation
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Project “Panama” -‐ Easier, safer, faster JNI! • Foreign funcGons + foreign data (FFI) • Flexible naGve data formats
• Modern naGve types like vector
• Significant overlap with Valhalla – Value Types, New Generics, Arrays 2.0, VarHandles
• TentaGvely targeGng JDK 10
4
“Bridging the gap”
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Disclaimer • The project is sGll in the exploratory/research phase • Beware: Syntax, names, workflows can and will change!
5
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JNI today
6
User supplied files
foo.h
libfoo.so
foo.h
DEVELOPER
Foo.h
javah
Foo.java DEVELOPER
libjnifoo.so
Foo.c
gcc
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Build/link Gme (or even at runGme)
User supplied files
Panama Workflow (build Gme)
7
foo.h
libfoo.so
jextract (aka. ”the groveller”)
foo.jar
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Panama Workflow (run Gme)
8
foo.jar
libfoo.so
java –cp profit.Main
Library.loadLibrary(”foo”)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
unistd.class
unistd.h
jextract -‐ Header file -‐> Java interface
package mypackage; @Header(path="unistd.h") public interface unistd { @C(file="unistd.h", line=3, column=1,
USR="c:@F@getpid") @NativeType(ctype="pid_t (void)", size=1) @CallingConvention(1) public int getpid(); }
9
#ifndef _UNISTD_H #define _UNISTD_H pid_t getpid(void); #endif // _UNISTD_H
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
jextract(header file “groveller”)
Java code (interfaces)
User API(Java carrier
names & carrier types)
JVM/runtime
layout DSL
metadata classes / jar
interfacesplatform config.
Java Apps
Java Apps
Java Apps
Panama Workflow (block diagram)
10
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Example: getpid() import java.nicl.*; import sys.unistd; public class HelloGetpid { public void testGetpid() { // Weave a class for the unistd interface, and return an instance unistd unistd = NativeLibrary.createImpl(unistd.class); // Call the system getpid() function to get the pid of the current process int pid = unistd.getpid(); // Print the pid System.out.println("pid: " + pid); } }
11
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Example: prinn() import java.nicl.*; import sys.stdio; public class HelloPrintf { public testPrintf() { // Weave a class for the stdio interface, and return an instance stdio stdio = NativeLibrary.createImpl(stdio.class); // Create a scope for tracking native allocations, using try-‐with-‐resources try (Scope scope = new Scope()) { // Convert the Java string to a native (NUL terminated) string Pointer<Byte> str = Transformer.toCStrPtr("Hello, World!\n", scope); // Call printf stdio.printf(str); } } }
12
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Example: “civilized” prinn() import java.nicl.*; import sys.stdio; public class HelloCivilizedPrintf { public testCivilizedPrintf() { // Weave a class for the stdio interface, and return an instance stdio stdio = NativeLibrary.createCivilizedImpl(stdio.class); stdio.printf("Hello, World!\n"); } }
13
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Example: qsort() private static int NOOF_ELEMENTS = 10; public void testQsort() { stdlib stdlib = NativeLibrary.createImpl(stdlib.class); // Create a native array, and init the elems in decreasing order ({9, 8, ..., 0}) NativeIntArray arr = new NativeIntArray(NOOF_ELEMENTS); for (int i = 0; i < NOOF_ELEMENTS; i++) { arr.setAt(i, NOOF_ELEMENTS -‐ 1 -‐ i); } // Sort the array elements, smallest value first stdlib.qsort(arr.getPtr(), arr.size(), arr.getElemSize(), (e1, e2) -‐> { return e1.castTo(Pointer<int>.class).deref() – e2.castTo(Pointer<int>.class).deref(); // NOTE: made up syntax }); // The array elements are now sorted in increasing order ({0, 1, 2, ..., 9}) }
14
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Status • Work focused on linux/x64 – Really: System V ABI, which includes OSX and Solaris
• Support in place for many/most C funcGons, types and structs – int, float, pointer, vector argument/return types – varargs – struct-‐by-‐value argument/return types – Callbacks (funcGon pointers)
15
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
• Berer type safety and security – Permission/access model – NaGve types vs. Java carrier types
• NaGve resource management – Scoped allocaGon, Java heap <-‐> naGve heap copying, …
• Support for more planorms/ABIs
• C++ support – Overloads, classes, excepGons, operators, inline, templates
• Performance
• Flexible and targeted "civilizaGon" – Binding rules, argument conversion, scoping, …
• Bindings for: C++, etc., etc. • ...
16
Areas of ExploraGon (sample)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement The preceding is intended to outline our general product direcGon. It is intended for informaGon purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or funcGonality, and should not be relied upon in making purchasing decisions. The development, release, and Gming of any features or funcGonality described for Oracle’s products remains at the sole discreGon of Oracle.