working with native libraries in java - jug ru...

Post on 27-May-2020

7 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Working with Native Libraries in Java

Vladimir Ivanov HotSpot JVM Compile r Oracle Corp.

Twitter: @iwan0www OpenJDK: vlivanov

23.04.2016

2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Safe Harbor Statement

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

3 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Why?

§ LAPACK –  Linear Algebra Package

4 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Why?

§ LAPACK –  Linear Algebra Package –  written in Fortran 90 –  highly optimized

§  “The original goal of the LAPACK was to … run efficiently on shared-memory vector and parallel processors.”

5 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Why?

§ LAPACK (Linear Algebra Package) –  invoke library code –  pass data into library –  access data from Java

6 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

§ Existing –  Java Native Interface (JNI) & JNR library –  java.nio.DirectByteBuffer –  sun.misc.Unsafe (get*/set*)

§  JDK9 –  j.l.i.VarHandle views over ByteBuffers

§ Future –  Project Panama

7 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Native Code

8 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNI @since 1.1

9 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNI

classLibC{staticnativelonggetpid();}

jlongJNICALLJava_LibC_getpid(JNIEnv*env,jclassc){ returngetpid();}

Usage scenario

10 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNI

jlongJNICALLJava_...(JNIEnv*env, jclasscls, jobjectobj){jmethodIDmid=env->GetMethodID(cls,“m”,“(I)J”);jlongresult=env->CallLongMethod(obj,mid,10);

Upcall

11 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNI

jlongJNICALLJava_...(JNIEnv*env, jclasscls, jobjectobj){jfieldIDfid=env->GetFieldID(cls,“f”,“J”);jlongresult=env->GetLongField(obj,fid);jlongresult=env->SetLongField(obj,fid,10);

Data access

12 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNI

§ Operations on –  Classes –  Strings –  Arrays –  Monitors

Native API: JNIEnv

13 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Java Frame

Java Heap Native Memory

Native Frame

GC roots

14 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Java Heap Native Memory

Java Frame Native Frame

GC roots

15 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

jobjectraw ptr address

Java Heap

Native Memory

ptr

16 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

jobjectraw ptr address

Java Heap

Native Memory

ptr

17 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Java Heap Native MemoryH

andl

es

Java Frame Native Frame

GC roots

18 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Java

Native

Java Heap

Native Memory

VM

Thread State

Anatomy of JNI call

19 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Java

Native

Java Heap

Native Memory

VM

Thread State

Anatomy of JNI call Safepoints

20 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNI

§ Pros –  seamless integration

§  looks like a Java method –  rich native API to interact with Java

§ Cons –  manual binding –  invocation overhead

21 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNI

A victim of its own success?

22 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNI Sum elements

jintJNICALLJava_...(JNIEnv*env,jclassc,jobjectarr){jintlen=(*env)->GetArrayLength(env,arr);jbyte*a=(*env)->GetPrimitiveArrayCritical(env,arr,0);…returnsum;}

empty sum 1 sum 103 sum 106 JNI 11.4±0.3 ns 178±7.1 ns 798±32 ns 641±51 µs

23 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Critical JNI Sum elements

jintJNICALLJavaCritical_...(jintlength,jbyte*first){...returnsum;}

empty sum 1 sum 103 sum 106 JNI 11.4±0.3 ns 178.0±7.1 ns 798±32 ns 641±51 µs CriticalJNI 11.4±0.3 ns 17.2±0.8 ns 680±22 ns 636±12 µs

24 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Critical JNI

§ only static, non-synchronized methods supported § no JNIEnv* § arguments: primitives or primitive arrays

–  [I => (length, I*) –  null => (0, NULL)

§ no object arguments

25 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

intprintf(constchar*format,...)

26 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

voidqsort(void*base,size_tnel,size_twidth,int(*cmp)(constvoid*,constvoid*));

27 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNR Java Native Runtime

28 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNR

public interface LibC { @pid_t long getpid(); } LibC lib = LibraryLoader .create(LibC.class) .load("c");

libc.getpid()

Usage scenario

JNRJava

Native

bindings

Interfaces

libffi

Target

User-defined

generated on-the-fly

29 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

DEMO

§ native call –  getpid

§  structs –  gettimeofday

§ qsort –  upcalls

30 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNR

§ Pros –  automatic binding of native methods

§ Cons –  manual interface extraction

§  doesn’t scale –  still uses JNI to perform native calls

31 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI Easier, safer, faster!

32 Copyright © 2016, 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

33 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Project Panama “Bridging the gap”

34 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

35 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI

pid_t get_pid();

36 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI

publicinterfaceLibC{longgetpid();}LibClibc=Library .load(LibC.class,“c”);libc.getpid();

Easier

j.l.iJava

Native

bindings

Interfaces

JVM stubs

Target

User-defined

Library

generated on-the-fly

produced by jextract

37 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI

publicinterfaceLibC{longgetpid();}LibClibc=Library.load(LibC.class,“c”/*lib_name*/);libc.getpid();

Easier

38 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI

publicinterfaceLibC{longgetpid();}LibClibc=Library.load(LibC.class,“c”/*lib_name*/);libc.getpid();

Easier

39 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI Faster

callq 0x1057b2eb0 ; getpid entry

40 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI

MethodTypemt=MethodType.methodType(int.class);//pid_tMethodHandlemh=MethodHandles.lookup().findNative("getpid",mt);intpid=(int)mh.invokeExact();

Faster

getpid JNI 13.7 ± 0.5 ns Direct call 3.4 ± 0.2 ns

41 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI

§ no crashes § no leaks § no hangs § no privilege escalation § no unguarded casts

Safer

42 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI Trust Levels

Untrusted

43 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI Trust Levels

Trusted

44 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI Trust Levels

Privileged

45 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI

46 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI gettimeofday

/*time.h*/struct{time_ttv_sec;suseconds_ttv_usec;}timeval;

intgettimeofday(structtimeval*tv,structtimezone*tz);

struct{inttz_minuteswest;inttz_dsttime;}timezone;

47 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Carrier Types

§ Javabooleanbyteshortcharintlong

§ Ccharshortfloatintlonglonglong

?

48 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Carrier Types

§ Javaboolean(uint8_t)byte(int8_t)short(int16_t)char(uint16_t)int(int32_t)long(int64_t)

§ Ccharshortfloatintlonglonglong

?

49 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI $ jextract time.h

interfaceTime{interfaceTimeval{longtv_sec$get();voidtv_sec$set(long);longtv_usec$get();voidtv_usec$set(long);}

intgettimeofday(Timeval,Timezone);

interfaceTimezone{inttz_...$get();voidtz_...$set(int);inttz_...$get();voidtz_...$set(int);}

50 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Foreign Layouts

§ Native data requires special address arithmetic –  Native layouts should not be built into the JVM (sorry, no native classes) –  Native types are unsafe, so trusted code must manage the bits

§ Solution: A metadata-driven Layout API

§ As a bonus, layouts other than C and Java are naturally supported –  Network protocols, specialized in-memory data stores, mapped files, etc.

50

51 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI Data Layout

interfaceTimeval{…@Offset(offset=0L)longtv_sec$get();…@Offset(offset=64L)longtv_usec$get();…§ work on Layout Definition Language (LDL) is in progress

–  https://github.com/J9Java/panama-docs/blob/master/StateOfTheLDL.html

52 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI Runtime Librarylib=Library.create(“c”);Timetime=lib.create(Time.class);Timevaltval=lib.create(Timeval.class);intres=time.gettimeofday(tval,null);if(res==0){longtv_sec=tval.tv_sec$get();longtv_usec=tval.tv_usec$get();}

53 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI Resources

Timevaltval;try{tval=lib.create(Timeval.class);intres=time.gettimeofday(tval,null);if(res==0){longtv_sec=tval.tv_sec$get();longtv_usec=tval.tv_usec$get();}else{/*errorhandling*/}}finally{lib.free(tval);tval=null;}

54 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI Resources

interfaceTimevalextendsAutoCloseable{…}try(Timevaltval=lib.create(Timeval.class)){intres=time.gettimeofday(tval,null);if(res==0){longtv_sec=tval.tv_sec$get();longtv_usec=tval.tv_usec$get();}else{/*errorhandling*/}}

55 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI “Civilizer”

interfaceTimeval{voidgettimeofday(Timeval,Timezone)throwsErrNo;}try(Timevaltval=lib.create(Timeval.class)){time.gettimeofday(tval,null);//throwsexceptionlongtv_sec=tval.tv_sec$get();longtv_usec=tval.tv_usec$get();}

56 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI Variadic Function

int printf(const char *format, ...)

57 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI jextract + Civilizer

//int printf(const char *format, ...) interfaceStdio{…//Rawintprintf(Pointer<Byte>format,byte[]args);//Civilizedvoidprintf(Stringformat,Object…args);

58 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Optimize checks

void run(MyClass obj) { obj.nativeFunc1(); // checks & state trans. obj.nativeFunc2(); // checks & state trans. obj.nativeFunc3(); // checks & state trans. }

58

59 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Optimize checks

void run(MyClass obj) { obj.f1(); // NPE obj.f2(); // NPE obj.f3(); // NPE }

60 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Optimize checks

void run(MyClass obj) { if (obj == null) jump throwNPE_stub; call MyClass::f(obj); call MyClass::f1(obj); call MyClass::f3(obj); }

60

61 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Optimize checks

void run(MyClass obj) { obj.nativeFunc1(); // checks & state trans. obj.nativeFunc2(); // checks & state trans. obj.nativeFunc3(); // checks & state trans. }

61

62 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Optimize checks

void run(MyClass obj) { if (!performChecks()) jump failed_stub; call transJavaToNative(); MyClass::nativeFunc1(env, obj); MyClass::nativeFunc2(env, obj); MyClass::nativeFunc3(env, obj); call transNativeToJava(); }

62

63 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Java

Java Heap

Native Memory

VM

Native

Thread State

64 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Java

Java Heap

Native Memory

VM

Native

Thread State

65 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Better JNI

§ Native access between the JVM and native APIs –  Native code via FFIs –  Native data via safely-wrapped access functions –  Tooling for header file API extraction and API metadata storage

§ Wrapper interposition mechanisms, based on JVM interfaces –  add (or delete) wrappers for specialized safety invariants

§ Basic bindings for selected native APIs

Easier, Safer, Faster!

65

66 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Native Data

67 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

NIO @since 1.4

68 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

NIO

§  Provides access to the low-level I/O operations –  Buffers for bulk memory operations

§  on-heap and off-heap –  Character set encoders and decoders –  Channels, a new primitive I/O abstraction –  File interface

§  supports locks and memory mapping of files –  Multiplexed, non-blocking I/O

“New I/O”

69 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

java.nio.Buffer

§  java.nio.ByteBuffer / CharBuffer / … –  MappedByteBuffer extends ByteBuffer

§  memory-mapped region of a file –  DirectByteBuffer extends MappedByteBuffer

§  malloc’ed native memory –  HeapByteBuffer

§  backed by byte[]

70 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

java.nio.DirectByteBuffer

ByteBufferdbb=ByteBuffer.allocateDirect(size);dbb.rewind();//resetpositionwhile(dbb.hasRemaining()){byteb=dbb.get();}

Usage

71 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

java.nio.Buffer

§ < 2GiB –  ByteBuffer.allocateDirect(int size)

§ Stateful –  Buffer.position –  not thread-safe

§ Resource deallocation –  GC-based (Cleaner) memory management

§ Zeroing –  on initialization

§ Bounds checking

72 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

sun.misc.Unsafe Anti-JNI

73 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

sun.misc.Unsafe

Use case Example methods

Concurrency primitives compareAndSwap*

Serialization allocateInstance

Efficient memory management, layout, and access

allocateMemory/freeMemory get*/put*

Interoperate across the JVM boundary get*/put*

… …

74 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

sun.misc.Unsafe

§ Unsafe.get*/put* –  getInt(Object base, long offset) –  putInt(Object base, long offset, int value);

§ double-register addressing mode –  getInt(o, offset) == o + offset –  getInt(null, address) == address

§  long allocateMemory(long size)

75 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

sun.misc.Unsafe

§ Absolute addresses –  byte getByte(long address) –  int getInt(long address) –  …

§ Object getObject(long address)?

§  long getAddress(long address)

76 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

UNSAFE.putInt(new Object(), 0L, 0)

77 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

UNSAFE.putInt(null, 0L, 0)

78 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

UNSAFE.getObject(addr)

79 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Unsafe =? Fast

80 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Unsafe != Fast

81 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Unsafe != Fast

§ public native Object allocateInstance(Class<?> cls) throws …;

§ Array index vs raw offset long[]base=newlong[…];intidx=…;longoffset=(((long)idx)<<SCALE+OFFSET)longvalue=Unsafe.getLong(base,offset);

§  JDK-8078629: “VM should constant fold Unsafe.get*() loads from final fields”

82 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

- How many of you have used the Unsafe API? …

John Rose, JVM Architect, Oracle JVM Language Summit 2014

82

83 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

- How many of you have used the Unsafe API? …

- A lot of you. Gosh. I'm sorry.

John Rose, JVM Architect, Oracle JVM Language Summit 2014

83

84 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

85 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

sun.misc.Unsafe

Use case Example methods

Concurrency primitives compareAndSwap*

Serialization allocateInstance (ReflectionFactory.newConstructorForSerialization)

Efficient memory management, layout, and access

allocateMemory/freeMemory get*/put*

Interoperate across the JVM boundary

get*/put*

86 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

sun.misc.Unsafe

Use case Replacement

Concurrency primitives JEP 193 Variable Handles

Serialization Reboot JEP 187 Serialization Improvements

Efficient memory management, layout, and access

Project Panama, Project Valhalla, Arrays 2.0, Better GC

Interoperate across the JVM boundary

Project Panama, JEP 191 Foreign Function Interface

87 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

java.lang.invoke.

VarHandle @since 9

JEP 193: Variable Handles

88 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

VarHandle

MethodHandles.Lookup:VarHandlebyteBufferViewVarHandle(Class<?>viewArrayClass,booleanbigEndian){…}

“Produces a VarHandle giving access to elements of a ByteBuffer viewed as if it were an array of elements of a different primitive component type to that of byte, such as int[] or long[].”

ByteBuffer View

89 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

VarHandle

VarHandleVH=MethodHandles.byteBufferViewVarHandle(int[].class,ByteOrder.nativeOrder()==ByteOrder.BIG_ENDIAN);ByteBuffer dbb = ByteBuffer.allocateDirect(size); int v = (int)VH.get(dbb, idx);

ByteBuffer View

90 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

java.nio.ByteBuffer vs VarHandle View

DirectByteBuffer VarHandle Size < 2 GiB < 2 GiB State Yes No Resource management GC-based No (delegates to DBB) Zeroing Yes No (delegates to DBB) Bound checks Yes (optimized) Yes (optimized)

91 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Optimized Bounds Checks

//nullcheck+(indexu<array.length)returnarray[index];

int[]

92 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Optimized Bounds Checks

//boundsandnullcheckif(index<0||index>=array.length)thrownew…();longoffset=BASE+(((long)index)<<2);returnUNSAFE.getInt(array,offset);

int[]

93 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Optimized Bounds Checks

//boundsandnullcheckindex=Objects.checkIndex(index,array.length);longoffset=BASE+(((long)index)<<2);returnUNSAFE.getInt(array,offset);

@HotSpotIntrinsicCandidatepublicstaticintcheckIndex(intindex,intlength,…);

int[]

94 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

“Если не Unsafe, то кто: восход VarHandles”

Алексей Шипилёв, Oracle

Сегодня, 17:20, Зал №1

95 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

§ Existing –  Java Native Interface (JNI) & JNR library –  java.nio.DirectByteBuffer –  sun.misc.Unsafe (get*/set*)

§  JDK9 –  j.l.i.VarHandle views over ByteBuffers

§ Future –  Project Panama

96 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

http://openjdk.java.net

Project Panama Foreign Function Interface Data Layout Control Vector API Arrays 2.0

97 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

http://openjdk.java.net

Project Panama panama-dev@openjdk.java.nethttp://hg.openjdk.java.net/panama/panama

98 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Safe Harbor Statement

The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

99 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Graphic Section Divider

100 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Vector API

bits Java x86 reg 8 byte al 16 short ax 32 int eax 64 long rax 128 - xmm0 256 - ymm0 512 - zmm0

101 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Vector API

§ MIMD (SPMD), SIMD, SIMT (GPGPU)

102 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNI Usage scenario

103 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNI

Java: public class GetPid { public static native long getpid(); }

C/C++:

get_pid.h: JNIEXPORT jlong JNICALL Java_GetPid_getpid (JNIEnv *, jclass); get_pid.c: jlong JNICALL Java_GetPidJNI_getpid(JNIEnv *env, jclass c) {

return getpid(); }

104 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

JNI: Method Invocation

1.  if (GC_locker::needs_gc()) SharedRuntime::block_for_jni_critical()

2.  transition to thread_in_native 3.  unpack array arguments

4.  call native entry point

Runtime checks before and after native call

1.  call native entry point 2.  check for safepoint in progress

3.  check if any thread suspend flags are set –  call into JVM and possibly unlock the

JNI critical if a GC was suppressed while in the critical native

4.  transition to thread_in_Java

5.  return

105 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

§  Java Heap § Native Heap

106 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

§ Panama –  Vector API –  Arrays 2.0 –  Layout

107 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Java

Code

Data

Native

Code

Data

108 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

GC roots

Java Heap Native Memory

109 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

GC roots

Java Heap Native Memory

110 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

GC roots

Java Heap Native Memory

111 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

java.nio.ByteBuffer

public final ByteBuffer alignedSlice(int unitSize) { /** * Creates a new byte buffer whose content is a shared and aligned * subsequence of this buffer's content.

public final int alignmentOffset(int index, int unitSize) {

§  /** * Returns the memory address, pointing to the byte at the given index, * modulus the given unit size. * * @apiNote * This method may be utilized to determine if unit size bytes from an * index can be accessed atomically, if supported by the native platform. *

@since 9

112 Copyright © 2016, Oracle and/or its affiliates. All rights reserved

Java

Code

Data

Native

Code

Data

top related