20 years of java - oraclecr.openjdk.java.net/~vlivanov/talks/2015_java_20_years_minsk.pdfjava...

Post on 22-Jun-2020

5 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

20 years of Java

Vladimir Ivanov HotSpot JVM Compile r Oracle Corp.

Twitter: @iwan0www OpenJDK: vlivanov

28.11.2015

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

20 years of Java (The Platform)

Vladimir Ivanov HotSpot JVM Compile r Oracle Corp.

Twitter: @iwan0www OpenJDK: vlivanov

3 Copyright © 2015, 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.

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

About me

1996 2002 2010 2015 2005

JDK 1.0

Wrote my first program in Java.

Joined Sun Microsystems

(SPBDC) to work on HotSpot JVM Now

Became part of Oracle Corp.

after acquisition

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

1991: Project Green

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

1991: *7 (StarSeven) Oak programming language

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

1991: *7 (StarSeven) Duke

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

1994: WebRunner/HotJava browser

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

1995: First release: 1.0a2

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

Intel Pentium P54CS, 0.35µm, 200MHz, 66MHz FSB

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

Sun Enterprise 10000 64 UltraSPARC II CPUs, 400 MHz, 16x4GB RAM

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

1996: JDK 1.0

§ 6 packages –  java.applet –  java.awt –  java.io –  java.lang –  java.net –  java.util

January, 23

§ # of classes –  java/ 225 –  sun/ 379

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

1996: picoJava

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

1997: JDK 1.1 February, 19

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

1998: J2SE 1.2 December, 8

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

1999: J2SE, J2ME, J2EE

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

1999: J2SE, J2ME, J2EE

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

2000: J2SE 1.3 May, 8

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

1998: JCP

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

2002 February, 6

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

First release

1995 1997 2002

Java Releases

1996 1998

JDK 1.0

JDK 1.1

J2SE 1.2

2000

J2SE 1.3

J2SE 1.4

Inner classes JavaBeans

JDBC RMI

Reflection JNI

Swing JIT compiler Java Plug-in Collections

HotSpot JVM JNDI

Java Sound JVMDI JVMPI

Regular Expressions IPv6 support

Logging XML, XSLT

Java Web Start

January, 23

February, 19

December, 8

May, 8

February, 6

March

AWT Network

I/O

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

2004: J2SE 5.0 September, 30

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

2004: J2SE 5.0 September, 30

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

2006: Java SE 6

§ Scripting Language Support (JSR 223) §  Java Compiler API (JSR 199) § Support for pluggable annotations (JSR 269) § Numerous performance improvements

–  C1: new linear register allocator –  C1: IR in SSA form

December, 11

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

Java SE 6u

§ Escape analysis in C2 –  6u23

§ NUMA GC Enhancements –  Java SE 6u2

§ Compressed OOPs –  6u23

§ Tiered compilation

HotSpot Express

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

2007 May, 8

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

2007: May JavaFX 1.0 on December, 2008

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

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

2010

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

2011: Java SE 7

§  JVM support for dynamic languages (JSR 292) § Small language changes

–  e.g. strings in switch, try-with-resources § New file I/O library (defined by JSR 203)

July, 28

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

2014: Java SE 8

§  JSR 335: Language-level support for lambda expressions

§  JSR 223: Project Nashorn, a JavaScript runtime §  JSR 308: Annotation on Java Types §  JSR 310: Date and Time API §  JEP 122: Remove the permanent generation

March, 18

32 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

2014: Java SE 8 March, 18

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

2016: Java SE 9 ETA: September, 22

Project  Jigsaw   Scalability  Performance  Security  

http://openjdk.java.net  

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

Project Jigsaw Scalability

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

Java 8

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

authjdbc jaxpnaming

jaxwscompiler

rowsetxmldsig

crypto

corba

desktop

tools.jre

httpserver

tools

tools.jaxws tools.base

devtools

javafx

prefssctp jx.annotationsinstrument

54MB 30 16 11 nashorn

rmi

compact1

JRE

compact3

compact2

JDK

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

naming.rmi  

base  charsets  

se  

compact3  

compact2  

compact1  

logging   scrip:ng  

security.acl  

security.sasl    

sql  rmi  

xml  xml.crypto  

prefs  desktop  

ac:va:on  

corba  

compiler   management  

sql.rowset  

security.jgss  

instrument  

naming  

xml.ws  

xml.soap  

xml.bind  

annota:ons.common  

hBpserver  

rmic  

javadoc  

xml.ws  

xml.bind  

compiler  

hotspot.agent  

jconsole   jcmd  jdi  

aBach  

jvmstat  

naming  

smartcardio  crypto.ec  

crypto.pkcs11  

hprof.agent  jdwp.agent  

localedata  sctp  

zipfs  security.auth  

scripting. nashorn

Project Jigsaw Performance

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

Project Jigsaw

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

tlslogging

authjdbc

jta

jaxpnaming

jaxwscompiler

rowset

kerberosmanagement

xmldsig

crypto

cosnaming management.iiop

corba

desktop

tools.jre

httpserver

tools

tools.jaxws tools.base

devtools

javafx

prefssctp jx.annotations

nashorn

instrument base

script

rmi

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

Project Jigsaw

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

tlslogging

authjdbc

jta

jaxpnaming

jaxwscompiler

rowset

kerberosmanagement

xmldsig

crypto

cosnaming management.iiop

corba

desktop

tools.jre

httpserver

tools

tools.jaxws tools.base

devtools

javafx

prefssctp jx.annotations

nashorn

instrument base

script

rmi

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

Project Jigsaw

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

tlslogging

authjdbc

jta

jaxpnaming

jaxwscompiler

rowset

kerberosmanagement

xmldsig

crypto

cosnaming management.iiop

corba

desktop

tools.jre

httpserver

tools

tools.jaxws tools.base

devtools

javafx

prefssctp jx.annotations

nashorn

instrument base

script

rmi

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

Project Jigsaw

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

crypto

javafx

nashorn

base

script

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

j l i n k

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

*.jmod $JRE/bin/java

/ l i b / . . .

* . j a r

* .c lass

JVM image

. j a r

.jmod

Fat binary

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

Project Jigsaw Opportunities for performance improvements

rt.jar

nashorn.jar, jfxrt.jar,sunjce_provider.jar

myapp.jar

crypto

javafx

nashornbase

script

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

Project Jigsaw

§  Improved class loading architecture –  fast class lookup

Opportunities for performance improvements crypto

javafx

nashornbase

script

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

Project Jigsaw

§  Improved class loading architecture –  fast class lookup

Opportunities for performance improvements crypto

javafx

nashornbase

script

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

Project Jigsaw

§  Improved class loading architecture –  fast class lookup

§ Aggressive inlining

Opportunities for performance improvements crypto

javafx

nashornbase

script

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

Project Jigsaw

§  Improved class loading architecture –  fast class lookup

§ Aggressive inlining § Ahead-Of-Time compilation

Opportunities for performance improvements crypto

javafx

nashornbase

script

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

Project Jigsaw

§  Improved class loading architecture –  fast class lookup

§ Aggressive inlining § Ahead-Of-Time compilation §  JVM-specific memory images

–  e.g. Application Class Data Sharing (AppCDS)

Opportunities for performance improvements crypto

javafx

nashornbase

script

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

Project Jigsaw

§  Improved class loading architecture –  fast class lookup

§ Aggressive inlining § Ahead-Of-Time compilation §  JVM-specific memory images

–  e.g. Application Class Data Sharing (AppCDS)

§ Removal of unused fields/methods/classes

Opportunities for performance improvements crypto

javafx

nashornbase

script

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

sun.* * . in te rna l .*

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

Project Jigsaw Security

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

sun.misc.Unsafe?!

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

Project Jigsaw Security

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

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

John Rose JVM Architect, Oracle Corporation

Project Jigsaw & sun.misc.Unsafe

@ JVM Language Summit 2014

52 Copyright © 2015, 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 Corporation

Project Jigsaw & sun.misc.Unsafe

@ JVM Language Summit 2014

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

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

JEP 260: Encapsulate Most Internal APIs

§ Replacement exists in JDK 8 –  Encapsulate/remove the method in JDK 9

§ Replacement exists in JDK 9 –  Deprecate in JDK9

§ No replacement exists in JDK 9 –  (For widely used internal API) Leave accessible

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

Critical Internal APIs

§  sun.misc.Cleaner §  sun.misc.{Signal,SignalHandler} §  sun.misc.Unsafe §  sun.reflect.Reflection::getCallerClass §  sun.reflect.ReflectionFactory

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

sun.misc.Unsafe

Use case Example methods

Concurrency primitives Unsafe.compareAndSwap*

Serialization Unsafe.allocateInstance (ReflectionFactory.newConstructorForSerialization)

Efficient memory management, layout, and access

Unsafe.allocate/freeMemory Unsafe.get*/put* (and JNI)

Interoperate across the JVM boundary

Unsafe.get*/put* (and JNI)

57 Copyright © 2015, 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

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

sun.misc.Unsafe (JDK 9)

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

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

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

JEP 193: Variable Handles

1.  Safe, performant, enhanced atomics –  access to field and array elements

2.  Fence operations for fine-grained control of memory ordering –  replacements for Unsafe::{full,store,load}Fence and more

3.  Reachability fence –  java.lang.ref.Reference::reachabilityFence

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

Charles Oliver Nutter JRuby Lead Developer

“The most fundamental change to Java since its inception.”

Java 7: Method Handles

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

Method Handles

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

(1) Variable Handles

§  like method handles for data –  Abstracts over location

§  static fields, instance fields, arrays, off heap –  Supports explicit fences and atomic operations

§ Safer than Unsafe, as fast as method handles

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

Are sun.misc.Unsafe methods really fast?

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

–  Array index vs raw offset long[] base = new long[…]; int idx = …; long offset = (((long) idx) << SCALE + OFFSET) long value = Unsafe.getLong(base, offset);

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

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

(1) Variable Handles

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

(2) Memory Fences

a = ?, b = ?

Thread #1 Thread #2

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

(2) Memory Fences

[a,b] = [0,0], [0,1], [1,0], [1,1]

Thread #1 Thread #2

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

(2) Memory Fences

Thread #1 Thread #2

[a,b] = [0,0], [0,1], [1,0], [1,1]

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

(3) Finalization

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

(3) Premature Finalization

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

(3) Premature Finalization

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

Beyond Java 9

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

Project Valhalla

Project Panama

Specialized Generics Value Types

Foreign Function Interface Data Layout Control Arrays 2.0

http://openjdk.java.net  

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

Project Valhalla “The hall of valor value”

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

Value-based Class

final  class  Point  {          public  final  int  x;          public  final  int  y;  }      

@since 8

“Use of identity-sensitive operations on instances of value-based classes may have unpredictable effects and should be avoided.”

ValueBased javadoc

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

Value-based Class

Point[] points =

Arrays

h header

x y

x y

x y

x y

x y

x y

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

Value-based Class

Point[] points =

Arrays

h header

x y

x y

x y

x y

x y

x y

for  (Point  p  :  points)  {      sum  +=  p.x  +  p.y;  }  

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

Value-based Class

int[]  xPoints  =  

Arrays

h header

assert(xPoints.length  ==  yPoints.length);    for  (int  i  =  0;  i  <  xPoints.length;  i++)  {      sum  +=  xPoint[i]  +  yPoint[i];  }  

h header

x x x x x x

y y y y y y int[]  yPoints  =  

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

Value-based Class

int[]  points  =  

Arrays

h header

assert(points.length  %  2  ==  0);    for  (int  i  =  0;  i  <  points.length;  i+=2)  {      sum  +=  points[i]  +  points[i+1];  }  

x y x y x y x y x y x y

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

“Codes like a class, works like an int!”

John Rose, Brian Goetz, Guy Steele

“State of the Values”

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

Value Types Arrays

value  class  Point  {          public  final  int  x;          public  final  int  y;  }  

Point[]  points  =   h header x y x y x y x y x y x y

for  (Point  p  :  points)  {      sum  +=  points.x  +  points.y;  }  

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

Motivation

§ Smaller footprint –  no object header

§ Better locality –  no dereference

§ Simpler semantics –  no identity, no aliasing

§ No object allocation

Value Types

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

Value Types

value  class  Point  {          public  final  int  x;          public  final  int  y;  }    class  Rectangle  {          public  final  Point  corner1;          public  final  Point  corner2;  }    

Concurrent side effects

x xy yh header  

Rectangle  r  =    

x y

x y

h header  

Rectangle  r  =    

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

Value Types

value  class  Point  {          public  final  int  x;          public  final  int  y;  }    class  Rectangle  {          public                    Point  corner1;          public  volatile  Point  corner2;  }    

Concurrent side effects

x xy yh header  

Rectangle  r  =    

x y

x y

h header  

Rectangle  r  =    

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

Use cases

§ Numerics: complex, decimal, rarely-big-num, etc. § Native types: int128_t, vectors, unsigned, safe native pointers § Algebraic data: optional (no box), choice-of, unit (no bits) § Tuples: multiple-value return! (requires specialization machinery also) § Cursors: unboxed iterators, STL-style bounds § Flat data: values naturally represent pointer-poor data structures

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

Selected details

Point[] <:? Object[]

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

Selected details

Point[] <: Object[]

Point[]  points  =   h header x y x y x y x y x y x y

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

Generic Specialization Motivation

ArrayList<Integer>

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

Generic Specialization Motivation

ArrayList<int>

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

Objects

§ Object, String, …, MyClass, …, Integer, Long, …

–  8 primitive types (+ reference)

§  boolean, byte, short, char, int, long, float, double

vs Primitives

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

Objects

§ Object, String, …, MyClass, …, Integer, Long, …

–  8 primitive types (+ reference)

§  boolean, byte, short, char, int, long, float, double

–  Value types

§  User-defined (!!!)

vs Non-objects

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

Generic Specialization Motivation

ArrayList<Point>

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

Generic Specialization

class  Box<T>    {      T            val;        public  Box(T            val)  {  this.val  =  val;  }      public  T            get()        {  return  val;  }    }      

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

Generic Specialization

class  Box<T>    {      T            val;        public  Box(T            val)  {  this.val  =  val;  }      public  T            get()        {  return  val;  }    }      

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

Generic Specialization

class  Box<Object>    {      Object  val;        public  Box(Object  val)  {  this.val  =  val;  }      public  Object  get()        {  return  val;  }    }      

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

Generic Specialization

class  Box<      int>    {      int        val;        public  Box(int        val)  {  this.val  =  val;  }      public  int        get()        {  return  val;  }    }      

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

Generic Specialization

class  Box<any  T>    {      T            val;        public  Box(T            val)  {  this.val  =  val;  }      public  T            get()        {  return  val;  }    }      

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

Generic Specialization

class  Box  extends  Object  {            private  final  Object  t;              public  Object  get()  {                0:  aload_0                1:  getfield  #2  //Field  t:LObject;                4:  areturn          }  }  

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

Generic Specialization

class  Box  extends  Object  {            private  final        int  t;              public        int  get()  {                0:  aload_0                1:  getfield  #2  //Field  t:I;                4:  ireturn          }  }  

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

Generic Specialization

class  Box  extends  Object  {            private  final  Object*T  t;              public  Object*T  get()  {                0:  aload_0                1:  getfield  #2  //Field  t:Lobject*T;                4:  areturn*T          }  }  

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

Generic Specialization

Box<Integer> :> Box

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

Generic Specialization

Box<int> :>? Box

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

Generic Specialization

Box<int> :> Box

Box<int> :> Box<?> Box<Object> :> Box<?>

Box :> Box<?>

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

Project Panama “Bridging the gap”

105 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

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

JNI @since 1.1

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

JNI

A victim of its own success?

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

JNI Usage scenario

109 Copyright © 2015, 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(); }

110 Copyright © 2015, 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

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

Better JNI Easier, safer, faster!

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

sun.misc.Unsafe § Anti-JNI

113 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Unsafe.getUnsafe().

putInt(new Object(), 0, 0)

114 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Unsafe.getUnsafe().

putInt(null, 0, 0)

115 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Better JNI Easier, safer, faster!

116 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Better JNI

pid_t get_pid();

117 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Better JNI

public  interface  GetPid  {          long  getpid();  }    

Easier

118 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Better JNI

public  interface  GetPid  {          long  getpid();  }    GetPid  getpid  =  Library.load(GetPid.class,  “c”  /*  lib_name  */  );  

Easier

119 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Better JNI

public  interface  GetPid  {          long  getpid();  }    GetPid  getpid  =  Library.load(GetPid.class,  “c”  /*lib_name*/  );    getpid.getpid();  

Easier

120 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Better JNI Faster

callq 0x1057b2eb0 ; getpid entry

121 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Better JNI gettimeofday

/*  time.h  */    struct  {          time_t            tv_sec;              suseconds_t  tv_usec;    }  timeval;    

int  gettimeofday(struct  timeval*  tv,  struct  timezone*  tz);  

   struct  {          int  tz_minuteswest;              int  tz_dsttime;    }  timezone;    

122 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Better JNI $ jextract time.h

interface  Time  {    interface  Timeval  {          long  tv_sec$get();          void  tv_sec$set(long);          long  tv_usec$get();          void  tv_usec$set(long);      }    

int  gettimeofday(Timeval,  Timezone);  

interface  Timezone  {          long  tz_...$get();          void  tz_...$set(long);          long  tz_...$get();          void  tz_...$set(long);      }    

123 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Better JNI Runtime Library  lib    =  Library.create(“c”);    Time  time  =  lib.create(Time.class);    Timeval    tval  =  lib.create(Timeval.class);  Timezone  tzone  =  null;    int  res  =  time.gettimeofday(tval,  tzone);  if  (res  ==  0)  {      long  tv_sec    =  tval.tv_sec$get();      long  tv_usec  =  tval.tv_usec$get();  }  

124 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Better JNI Resources

Timeval  tval;  try  {      tval  =  lib.create(Timeval.class);        int  res  =  time.gettimeofday(tval,  null);      if  (res  ==  0)  {          long  tv_sec    =  tval.tv_sec$get();          long  tv_usec  =  tval.tv_usec$get();      }  }  finally  {      lib.free(tval);      tval  =  null;  }  

125 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Better JNI Resources

try  (Timeval  tval  =  lib.create(Timeval.class))  {      int  res  =  time.gettimeofday(tval,  null);      if  (res  ==  0)  {          long  tv_sec    =  tval.tv_sec$get();          long  tv_usec  =  tval.tv_usec$get();      }  }  

126 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Better JNI

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

Safer

127 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Better JNI

128 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Project Valhalla

http://openjdk.java.net  

Project Panama

Specialized Generics Value Types

Foreign Function Interface Data Layout Control Arrays 2.0

129 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Project Valhalla

http://openjdk.java.net  

valhalla-­‐dev@openjdk.java.net  http://hg.openjdk.java.net/valhalla/valhalla  

Project Panama panama-­‐dev@openjdk.java.net  http://hg.openjdk.java.net/panama/panama  

130 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Duke

1995 2015

131 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

132 Copyright © 2015, 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.

133 Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Graphic Section Divider

top related