Beyond JVM
or, How to Boil the Ocean in Seven Years
+
We Like the JVM
No platform-specific
compilation needed.
No native library
dependencies
Optimizing JIT compiler
Magical black box that
runs our code
Things We Like About	

JVM
• Bytecode intermediate form	

• Freedom from native libraries	

• Optimizing JIT	

• It just w...
Things That Make	

JRuby Difficult
• Bytecode intermediate form	

• Optimizing JIT	

• Freedom from native libraries	

• It...
Things That Make	

JRuby Difficult
• Startup time sucks	

• JNI is a massive pain to use	

• Hard to get unusual languages ...
There must be a way!
Startup Time
Java-Heavy JDK
• + Less native code to maintain	

• + Easier portability	

• + Easier to swap out native side	

• – Takes ...
JVM
Language

JVM
Bytecode

Bytecode
Interpreter

Bytecode
JIT
Time

Native
Code
JVM
Language

JVM
Bytecode

Bytecode
Interpreter

Bytecode
JIT

Native
Code
Save JITed Code?
• Code will change across runs	

• Often has specific memory addresses	

• May optimize object layout diff...
JRuby Startup
C Ruby

JRuby

-e 1

gem --help

rake -T

0

2.5

5

7.5

10
Tweaking Flags
• -client mode	


• -XX:+TieredCompilation -XX:TieredStopAtLevel=1	


• -X-C to disable JRuby’s compiler	

...
JRuby Startup
C Ruby

JRuby

JRuby (best)

-e 1

gem --help

rake -T

0

2.5

5

7.5

10
Nailgun?
• Keep a single JVM running in background	

• Toss commands over to it	

• It stays hot, so code starts faster	

...
Drip
• Start a new JVM after each command	

• Pre-boot JVM plus optional code	

• Analyze command line for differences	

•...
$ export JAVACMD=`which drip`	
!
$ time jruby -e 1	
!
real	 0m1.655s	
user	 0m4.486s	
sys	0m0.231s	
!
$ time jruby -e 1	
!...
JRuby Startup
C Ruby

JRuby

JRuby (best)

JRuby (drip)

-e 1

gem --help

rake -T

0

2.5

5

7.5

10
$ export DRIP_INIT_CLASS=org.jruby.main.DripMain	
!
$ export DRIP_INIT=""	
!
$ time jruby -e 1	
!
real	 0m0.580s	
user	 0m...
public class DripMain {!
    public static RubyInstanceConfig DRIP_CONFIG;!
    public static Ruby DRIP_RUNTIME;!

!

    ...
 public static final String JRUBY_DRIP_WARMUP_ENV = "JRUBY_DRI
 public static final String JRUBY_DRIP_WARMUP_DEFAULT = "1 ...
 public static final String JRUBY_DRIP_WARMUP_ENV = "JRUBY_DRI
 public static final String JRUBY_DRIP_WARMUP_DEFAULT = "1 ...
 public static final String JRUBY_DRIP_WARMUP_ENV = "JRUBY_DRI
 public static final String JRUBY_DRIP_WARMUP_DEFAULT = "1 ...
     // preboot actual runtime!
     Ruby.clearGlobalRuntime();!
     File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);!
...
     // preboot actual runtime!
     Ruby.clearGlobalRuntime();!
     File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);!
...
     // preboot actual runtime!
     Ruby.clearGlobalRuntime();!
     File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);!
...
     // preboot actual runtime!
     Ruby.clearGlobalRuntime();!
     File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);!
...
     // preboot actual runtime!
     Ruby.clearGlobalRuntime();!
     File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);!
...
JRuby Startup
C Ruby
JRuby (drip init)

JRuby

JRuby (best)

JRuby (drip)

-e 1

gem --help

rake -T

0

2.5

5

7.5

10
$ cat dripmain.rb	
# Preload some code Rails always needs	
require File.expand_path('../config/application', __FILE__)
JRuby Startup
C Ruby
JRuby (drip)

JRuby
JRuby (drip init)

JRuby (best)
JRuby (dripmain)

rake -T

0

2.5

5

7.5

10
JRuby Startup
C Ruby

JRuby (dripmain)

rake -T

0

0.275

0.55

0.825

1.1
Native Interop
JVM World

????
Native World
JNI
User Code

Java

JNI call

C/native

JNI impl
Target Library
JNI
public class GetPidJNI {!
public static native long getpid();!
!
public static void main( String[] args ) {!
getpid();...
JNI
/* DO NOT EDIT THIS FILE - it is machine generated */!
#include <jni.h>!
/* Header for class com_headius_jnr_presentat...
JNI
#include "com_headius_jnr_presentation_GetPidJNI.h"!
 !
jlong JNICALL Java_com_headius_jnr_1presentation_GetPidJNI_get...
JNI
$ gcc -I $JAVA_HOME/include -I
$JAVA_HOME/include/darwin -L
$JAVA_HOME/jre/lib/ -dynamiclib -ljava
-o getpidjni.dylib
...
There Must Be	

A Better Way
User Code

Java

JNI call

C/native

JNI impl
Target Library
User Code

Java

JNI call

C/native

JNI impl
Target Library
Java Native Runtime
• Java API	

• for calling Native code	

• supported by a rich Runtime library	

• You may be familiar...
User Code
JNR stub

Java

JNI call

C/native

JNI impl
libffi
Target Library
JNR
import jnr.ffi.LibraryLoader;!
import jnr.ffi.annotations.IgnoreError;!
 !
public class GetPidJNRExample {!
public int...
Layered Runtime
etc etc
jnr-posix	


jnr-unixsocket!
jnr-enxio

jnr-constants

jnr-ffi
jffi
libffi

jnr-x86asm
jffi Platforms
• Darwin (OS X): universal (+ppc?)	

• Linux: i386, x86_64, arm, ppc, ppc64, s390x	

• Windows: i386, x86_64...
jnr-ffi
• User-oriented API	

• Roughly equivalent to what JNA gives you	

• Functions, structs, callbacks, memory	

• http...
jnr-posix
• Pre-bound set of POSIX functions	

• Mostly driven by what JRuby, Jython use	

• Goal: 100% of POSIX bound to ...
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
pub...
POSIX posix = POSIXFactory.getPOSIX(!
new MyPOSIXHandler(this),!
isNativeEnabled);
public interface POSIXHandler {!
public void error(Errno errno, String string);!
public void unimplementedError(String str...
public
public
public
public
public
public
public
public
public

int chmod(String string, int i);!
int chown(String string,...
jnr-constants
• C/preprocessor constants in enum form	

• Generator code to run on each platform	

• Several pregenerated ...
public int open(String string, int flags, int mode);!
public int socket(int i, int i1);
jnr-enxio
• Extended Native X-platform IO	

• NIO-compatible JNR-backed IO library	

• Read, write, select (kqueue, epoll,...
public class NativeSocketChannel!
extends AbstractSelectableChannel!
implements ByteChannel, NativeSelectableChannel {!
pu...
jnr-unixsocket
• UNIX sockets for NIO	

• Built atop jnr-enxio	

• Fully selectable, etc	

• https://github.com/jnr/jnr-un...
What Else?
• NIO, NIO.2	

• Native IO, symlinks, FS-walking, 	

• Unmanaged memory	

• Selectable stdio, process IO	

• Lo...
How Does It Perform?
Performance
• Generated code leading to JNI call	

• Generated assembly version of native part	

• jnr-x86asm: Generate an...
JNA getpid

JNR getpid

getpid calls, 100M times
100000ms

10000ms

1000ms

100ms

10ms

1ms
@IgnoreError
import jnr.ffi.LibraryLoader;!
import jnr.ffi.annotations.IgnoreError;!
 !
public class GetPidJNRExample {!
p...
JNR getpid

JNR getpid @IgnoreError

getpid calls, 100M times
2000ms

1500ms

1000ms

500ms

0ms
But There's More to Do
JNR getpid

JNI

JNR @IgnoreError

getpid calls, 100M times
2000ms

1500ms

1000ms

500ms

0ms

GCC...
JVM Help is Coming
• Standard FFI API in JDK	

• JIT intelligence	

• Drop JNI overhead where possible	

• Bind native cal...
Language Performance
History
• JVM authors mentioned non-Java languages	

• Language authors have targeted JVM	

• Hundreds of JVM languages no...
JVM
Opcodes
Invocation	


Field Access	


invokevirtual!
invokeinterface!
invokestatic!
invokespecial

getfield!
setfield!...
Goals of JSR 292
• A user-definable bytecode	

• Full freedom to define VM behavior	

• Fast method pointers + adapters	

• ...
Invoke

// Static!
System.currentTimeMillis()!
Math.log(1.0)
Invoke

// Static!
System.currentTimeMillis()!
Math.log(1.0)!
 !
// Virtual!
"hello".toUpperCase()!
System.out.println()
Invoke

// Static!
System.currentTimeMillis()!
Math.log(1.0)!
 !
// Virtual!
"hello".toUpperCase()!
System.out.println()!
...
Invoke

// Static!
System.currentTimeMillis()!
Math.log(1.0)!
 !
// Virtual!
"hello".toUpperCase()!
System.out.println()!
...
// Static!
invokestatic java/lang/System.currentTimeMillis:()J!
invokestatic java/lang/Math.log:(D)D!
!
// Virtual!
invoke...
invokevirtual!
1. Confirm object is of correct type	

2. Confirm arguments are of correct type	

3. Look up method on Java c...
invokedynamic bytecode
target method

bo
ot
st

ra
p

m

et
ho
d

method handles
Does It Work?
Indy Languages
• New language impls	

• JavaScript: Dyn.js and Nashorn	

• Redline Smalltalk	

• Improved language perform...
JRuby/Java 6

JRuby/Java 7

Times Faster than Ruby 1.9.3
5
4.32

3.75

3.66
3.44

2.5

2.658

1.914

1.25

0

1.346

base6...
red/black tree, pure Ruby versus native

ruby-2.0.0 + Ruby

2.48s

ruby-2.0.0 + C ext

0.51s

jruby + Ruby

0.29s

0

0.75...
Caveat Emptor
• Indy was really slow in first Java 7 release	

• Got fast in 7u2...and turned out broken	

• Rewritten for ...
All That C++
JVM
Language

JVM
Bytecode

Out of our control	

Written in C++

Bytecode
Interpreter

Bytecode
JIT

Native
Code
What If…
• The JVM’s JIT optimizer were written in Java	

• You could customize how the JIT works for
your language or lib...
Graal
• A 100% Java-based JIT framework	

• Grew out of the 100% Java “Maxine” JVM	

• Backends to assembly or HotSpot IR	...
JVM
Language
Plain Java APIs

Graal	

Intermediate
Representation

Graal
Optimizer

Your
Optimizations
Under your control
...
However…
• Not everyone is a compiler writer	

• Graal’s IR is low-level and nontrivial	

• Need to understand JVM interna...
The Dream
• Design your language	

• ???	

• PROFIT
What We Want
• Design your language	

• Write an interpreter	

• PROFIT
Truffle
• Language framework built on Graal	

• Designed to fulfill the dream	

• Implement interpreter	

• Truffle feeds tha...
JVM
Language

Truffle	

AST
All we need

Graal
Intermediate
Representation

Graal
Optimizer

Native
Code
What Have We Learned?
The JVM has its
problems, but we can
fix them.
OpenJDK and all these
solutions are really,
truly, open source.
Nothing is impossible.
Thank you!
• Charles Oliver Nutter	

• @headius, headius@headius.com	

• http://blog.headius.com
Beyond JVM - YOW! Sydney 2013
Beyond JVM - YOW! Sydney 2013
Beyond JVM - YOW! Sydney 2013
Beyond JVM - YOW! Sydney 2013
Beyond JVM - YOW! Sydney 2013
Beyond JVM - YOW! Sydney 2013
Beyond JVM - YOW! Sydney 2013
Beyond JVM - YOW! Sydney 2013
Beyond JVM - YOW! Sydney 2013
Beyond JVM - YOW! Sydney 2013
Beyond JVM - YOW! Sydney 2013
Beyond JVM - YOW! Sydney 2013
Upcoming SlideShare
Loading in …5
×

Beyond JVM - YOW! Sydney 2013

2,361 views

Published on

"Beyond JVM; or How to Boil the Ocean in Seven Years" as delivered at YOW! 2013 in Sydney.

Published in: Technology, Education

Beyond JVM - YOW! Sydney 2013

  1. 1. Beyond JVM or, How to Boil the Ocean in Seven Years
  2. 2. +
  3. 3. We Like the JVM
  4. 4. No platform-specific compilation needed.
  5. 5. No native library dependencies
  6. 6. Optimizing JIT compiler
  7. 7. Magical black box that runs our code
  8. 8. Things We Like About JVM • Bytecode intermediate form • Freedom from native libraries • Optimizing JIT • It just works (usually)
  9. 9. Things That Make JRuby Difficult • Bytecode intermediate form • Optimizing JIT • Freedom from native libraries • It just works (usually)
  10. 10. Things That Make JRuby Difficult • Startup time sucks • JNI is a massive pain to use • Hard to get unusual languages to optimize • Core is in C++ that we can’t touch
  11. 11. There must be a way!
  12. 12. Startup Time
  13. 13. Java-Heavy JDK • + Less native code to maintain • + Easier portability • + Easier to swap out native side • – Takes longer to warm up
  14. 14. JVM Language JVM Bytecode Bytecode Interpreter Bytecode JIT Time Native Code
  15. 15. JVM Language JVM Bytecode Bytecode Interpreter Bytecode JIT Native Code
  16. 16. Save JITed Code? • Code will change across runs • Often has specific memory addresses • May optimize object layout differently • Which JIT output? • Client, Server, Tiered (1-4)
  17. 17. JRuby Startup C Ruby JRuby -e 1 gem --help rake -T 0 2.5 5 7.5 10
  18. 18. Tweaking Flags • -client mode • -XX:+TieredCompilation -XX:TieredStopAtLevel=1 • -X-C to disable JRuby’s compiler • Heap sizes, code verification, etc etc
  19. 19. JRuby Startup C Ruby JRuby JRuby (best) -e 1 gem --help rake -T 0 2.5 5 7.5 10
  20. 20. Nailgun? • Keep a single JVM running in background • Toss commands over to it • It stays hot, so code starts faster • Hard to clean up all state (e.g. threads) • Can’t get access to user’s terminal
  21. 21. Drip • Start a new JVM after each command • Pre-boot JVM plus optional code • Analyze command line for differences • Age out unused instances • https://github.com/flatland/drip
  22. 22. $ export JAVACMD=`which drip` ! $ time jruby -e 1 ! real 0m1.655s user 0m4.486s sys 0m0.231s ! $ time jruby -e 1 ! real 0m0.577s user 0m0.052s sys 0m0.065s
  23. 23. JRuby Startup C Ruby JRuby JRuby (best) JRuby (drip) -e 1 gem --help rake -T 0 2.5 5 7.5 10
  24. 24. $ export DRIP_INIT_CLASS=org.jruby.main.DripMain ! $ export DRIP_INIT="" ! $ time jruby -e 1 ! real 0m0.580s user 0m0.052s sys 0m0.063s ! $ time jruby -e 1 ! real 0m0.155s user 0m0.049s sys 0m0.058s
  25. 25. public class DripMain {!     public static RubyInstanceConfig DRIP_CONFIG;!     public static Ruby DRIP_RUNTIME;! !     public static final String JRUBY_DRIP_WARMUP_ENV = "JRUBY_DRIP_WARMUP";!     public static final String JRUBY_DRIP_WARMUP_DEFAULT = "1 + 1";!     public static final String JRUBY_DRIP_PREBOOT_FILE = "./dripmain.rb";! !     public static void main(String[] args) throws IOException {!         // warmup JVM first!         Ruby ruby = Ruby.newInstance();! !         String envWarmup = System.getenv(JRUBY_DRIP_WARMUP_ENV);!         if (envWarmup != null && envWarmup.length() > 0) {!             ruby.evalScriptlet(envWarmup);!         } else {!             ruby.evalScriptlet(JRUBY_DRIP_WARMUP_DEFAULT);!         }! !         // preboot actual runtime!         Ruby.clearGlobalRuntime();!         File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);! !         RubyInstanceConfig config = new RubyInstanceConfig();!         ruby = Ruby.newInstance(config);! !         if (dripMain.exists()) {!             FileInputStream fis = new FileInputStream(dripMain);!             try {!                 ruby.getLoadService().load(dripMain.getAbsolutePath(), false);!             } finally {!                 fis.close();!             }!         }! !         // use config and runtime from preboot process!         DRIP_CONFIG = config;!         DRIP_RUNTIME = ruby;!     }! }!
  26. 26.  public static final String JRUBY_DRIP_WARMUP_ENV = "JRUBY_DRI  public static final String JRUBY_DRIP_WARMUP_DEFAULT = "1 + 1  public static final String JRUBY_DRIP_PREBOOT_FILE = "./dripm  public static void main(String[] args) throws IOException {!      // warmup JVM first!      Ruby ruby = Ruby.newInstance();!      String envWarmup = System.getenv(JRUBY_DRIP_WARMUP_ENV);!      if (envWarmup != null && envWarmup.length() > 0) {!          ruby.evalScriptlet(envWarmup);!      } else {!          ruby.evalScriptlet(JRUBY_DRIP_WARMUP_DEFAULT);!      }!      // preboot actual runtime!      Ruby.clearGlobalRuntime();!      File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);!      RubyInstanceConfig config = new RubyInstanceConfig();!      ruby = Ruby.newInstance(config);!
  27. 27.  public static final String JRUBY_DRIP_WARMUP_ENV = "JRUBY_DRI  public static final String JRUBY_DRIP_WARMUP_DEFAULT = "1 + 1  public static final String JRUBY_DRIP_PREBOOT_FILE = "./dripm  public static void main(String[] args) throws IOException {!      // warmup JVM first!      Ruby ruby = Ruby.newInstance();!      String envWarmup = System.getenv(JRUBY_DRIP_WARMUP_ENV);!      if (envWarmup != null && envWarmup.length() > 0) {!          ruby.evalScriptlet(envWarmup);!      } else {!          ruby.evalScriptlet(JRUBY_DRIP_WARMUP_DEFAULT);!      }!      // preboot actual runtime!      Ruby.clearGlobalRuntime();!      File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);!      RubyInstanceConfig config = new RubyInstanceConfig();!      ruby = Ruby.newInstance(config);!
  28. 28.  public static final String JRUBY_DRIP_WARMUP_ENV = "JRUBY_DRI  public static final String JRUBY_DRIP_WARMUP_DEFAULT = "1 + 1  public static final String JRUBY_DRIP_PREBOOT_FILE = "./dripm  public static void main(String[] args) throws IOException {!      // warmup JVM first!      Ruby ruby = Ruby.newInstance();!      String envWarmup = System.getenv(JRUBY_DRIP_WARMUP_ENV);!      if (envWarmup != null && envWarmup.length() > 0) {!          ruby.evalScriptlet(envWarmup);!      } else {!          ruby.evalScriptlet(JRUBY_DRIP_WARMUP_DEFAULT);!      }!      // preboot actual runtime!      Ruby.clearGlobalRuntime();!      File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);!      RubyInstanceConfig config = new RubyInstanceConfig();!      ruby = Ruby.newInstance(config);!
  29. 29.      // preboot actual runtime!      Ruby.clearGlobalRuntime();!      File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);!      RubyInstanceConfig config = new RubyInstanceConfig();!      ruby = Ruby.newInstance(config);!      if (dripMain.exists()) {!          FileInputStream fis = new FileInputStream(dripMain);!          try {!              ruby.getLoadService().load(dripMain.getAbsolutePa          } finally {!              fis.close();!          }!      }!      // use config and runtime from preboot process!      DRIP_CONFIG = config;!      DRIP_RUNTIME = ruby;!  }!
  30. 30.      // preboot actual runtime!      Ruby.clearGlobalRuntime();!      File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);!      RubyInstanceConfig config = new RubyInstanceConfig();!      ruby = Ruby.newInstance(config);!      if (dripMain.exists()) {!          FileInputStream fis = new FileInputStream(dripMain);!          try {!              ruby.getLoadService().load(dripMain.getAbsolutePa          } finally {!              fis.close();!          }!      }!      // use config and runtime from preboot process!      DRIP_CONFIG = config;!      DRIP_RUNTIME = ruby;!  }!
  31. 31.      // preboot actual runtime!      Ruby.clearGlobalRuntime();!      File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);!      RubyInstanceConfig config = new RubyInstanceConfig();!      ruby = Ruby.newInstance(config);!      if (dripMain.exists()) {!          FileInputStream fis = new FileInputStream(dripMain);!          try {!              ruby.getLoadService().load(dripMain.getAbsolutePa          } finally {!              fis.close();!          }!      }!      // use config and runtime from preboot process!      DRIP_CONFIG = config;!      DRIP_RUNTIME = ruby;!  }!
  32. 32.      // preboot actual runtime!      Ruby.clearGlobalRuntime();!      File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);!      RubyInstanceConfig config = new RubyInstanceConfig();!      ruby = Ruby.newInstance(config);!      if (dripMain.exists()) {!          FileInputStream fis = new FileInputStream(dripMain);!          try {!              ruby.getLoadService().load(dripMain.getAbsolutePa          } finally {!              fis.close();!          }!      }!      // use config and runtime from preboot process!      DRIP_CONFIG = config;!      DRIP_RUNTIME = ruby;!  }!
  33. 33.      // preboot actual runtime!      Ruby.clearGlobalRuntime();!      File dripMain = new File(JRUBY_DRIP_PREBOOT_FILE);!      RubyInstanceConfig config = new RubyInstanceConfig();!      ruby = Ruby.newInstance(config);!      if (dripMain.exists()) {!          FileInputStream fis = new FileInputStream(dripMain);!          try {!              ruby.getLoadService().load(dripMain.getAbsolutePa          } finally {!              fis.close();!          }!      }!      // use config and runtime from preboot process!      DRIP_CONFIG = config;!      DRIP_RUNTIME = ruby;!  }!
  34. 34. JRuby Startup C Ruby JRuby (drip init) JRuby JRuby (best) JRuby (drip) -e 1 gem --help rake -T 0 2.5 5 7.5 10
  35. 35. $ cat dripmain.rb # Preload some code Rails always needs require File.expand_path('../config/application', __FILE__)
  36. 36. JRuby Startup C Ruby JRuby (drip) JRuby JRuby (drip init) JRuby (best) JRuby (dripmain) rake -T 0 2.5 5 7.5 10
  37. 37. JRuby Startup C Ruby JRuby (dripmain) rake -T 0 0.275 0.55 0.825 1.1
  38. 38. Native Interop
  39. 39. JVM World ???? Native World
  40. 40. JNI
  41. 41. User Code Java JNI call C/native JNI impl Target Library
  42. 42. JNI public class GetPidJNI {! public static native long getpid();! ! public static void main( String[] args ) {! getpid();! }! ! static {! System.load(! System.getProperty("user.dir") +! "/getpidjni.dylib");! }! }
  43. 43. JNI /* DO NOT EDIT THIS FILE - it is machine generated */! #include <jni.h>! /* Header for class com_headius_jnr_presentation_GetPidJNI */!  ! #ifndef _Included_com_headius_jnr_presentation_GetPidJNI! #define _Included_com_headius_jnr_presentation_GetPidJNI! #ifdef __cplusplus! extern "C" {! #endif! /*! * Class: com_headius_jnr_presentation_GetPidJNI! * Method: getpid! * Signature: ()J! */! JNIEXPORT jlong JNICALL Java_com_headius_jnr_1presentation_GetPidJNI_getpid! (JNIEnv *, jclass);!  ! #ifdef __cplusplus! }! #endif! #endif
  44. 44. JNI #include "com_headius_jnr_presentation_GetPidJNI.h"!  ! jlong JNICALL Java_com_headius_jnr_1presentation_GetPidJNI_getpid! (JNIEnv *env, jclass c) {!  ! return getpid();! }
  45. 45. JNI $ gcc -I $JAVA_HOME/include -I $JAVA_HOME/include/darwin -L $JAVA_HOME/jre/lib/ -dynamiclib -ljava -o getpidjni.dylib com_headius_jnr_presentation_GetPidJNI. c! ! $ java -Djava.library.path=`pwd` -cp target/jnr_presentation-1.0SNAPSHOT.jar com.headius.jnr_presentation.GetPidJNI
  46. 46. There Must Be A Better Way
  47. 47. User Code Java JNI call C/native JNI impl Target Library
  48. 48. User Code Java JNI call C/native JNI impl Target Library
  49. 49. Java Native Runtime • Java API • for calling Native code • supported by a rich Runtime library • You may be familiar with JNA • Foreign Function Interface (FFI) • https://github.com/jnr
  50. 50. User Code JNR stub Java JNI call C/native JNI impl libffi Target Library
  51. 51. JNR import jnr.ffi.LibraryLoader;! import jnr.ffi.annotations.IgnoreError;!  ! public class GetPidJNRExample {! public interface GetPid {! long getpid();! }! ! public static void main( String[] args ) {! GetPid getpid = LibraryLoader! .create(GetPid.class)! .load("c");! ! getpid.getpid();! }! }
  52. 52. Layered Runtime etc etc jnr-posix jnr-unixsocket! jnr-enxio jnr-constants jnr-ffi jffi libffi jnr-x86asm
  53. 53. jffi Platforms • Darwin (OS X): universal (+ppc?) • Linux: i386, x86_64, arm, ppc, ppc64, s390x • Windows: i386, x86_64 • FreeBSD, OpenBSD: i386, x86_64 • SunOS: i386, x86_64, sparc, sparcv9 • AIX: ppc • OpenVMS, AS/400: builds out there somewhere • If your platform isn't here, contribute a build
  54. 54. jnr-ffi • User-oriented API • Roughly equivalent to what JNA gives you • Functions, structs, callbacks, memory • https://github.com/jnr/jnr-ffi
  55. 55. jnr-posix • Pre-bound set of POSIX functions • Mostly driven by what JRuby, Jython use • Goal: 100% of POSIX bound to Java
  56. 56. public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public List<? int chmod(String string, int i);! int chown(String string, int i, int i1);! int execv(String string, String[] strings);! int execve(String string, String[] strings, String[] strings1);! int fork();! int seteuid(int i);! int getgid();! String getlogin();! int getpgid();! int getpgid(int i);! int getpgrp();! int getpid();! int getppid();! Passwd getpwent();! Passwd getpwuid(int i);! Passwd getpwnam(String string);! Group getgrgid(int i);! Group getgrnam(String string);! int getuid();! boolean isatty(FileDescriptor fd);! int kill(int i, int i1);! int symlink(String string, String string1);! int link(String string, String string1);! String readlink(String string) throws IOException;! String getenv(String string);! int setenv(String string, String string1, int i);! int unsetenv(String string);! int getpriority(int i, int i1);! int setpriority(int i, int i1, int i2);! int setuid(int i);! FileStat stat(String string);! int stat(String string, FileStat fs);! int umask(int i);! Times times();! int utimes(String string, long[] longs, long[] longs1);! int waitpid(int i, int[] ints, int i1);! int wait(int[] ints);! int errno();! void errno(int i);! int posix_spawnp(String string, List<? extends SpawnFileAction> list, extends CharSequence> list1, List<? extends CharSequence> list2);
  57. 57. POSIX posix = POSIXFactory.getPOSIX(! new MyPOSIXHandler(this),! isNativeEnabled);
  58. 58. public interface POSIXHandler {! public void error(Errno errno, String string);! public void unimplementedError(String string);! public void warn(WARNING_ID wrngd, String string, Object[] os);! public boolean isVerbose();! public File getCurrentWorkingDirectory();! public String[] getEnv();! public InputStream getInputStream();! public PrintStream getOutputStream();! public int getPID();! public PrintStream getErrorStream();! }
  59. 59. public public public public public public public public public int chmod(String string, int i);! int chown(String string, int i, int i1);! int kill(int i, int i1);! int getpriority(int i, int i1);! int setpriority(int i, int i1, int i2);! int waitpid(int i, int[] ints, int i1);! int wait(int[] ints);! int errno();! void errno(int i);
  60. 60. jnr-constants • C/preprocessor constants in enum form • Generator code to run on each platform • Several pregenerated sets for jnr-posix
  61. 61. public int open(String string, int flags, int mode);! public int socket(int i, int i1);
  62. 62. jnr-enxio • Extended Native X-platform IO • NIO-compatible JNR-backed IO library • Read, write, select (kqueue, epoll, etc) • Low-level fcntl control • https://github.com/jnr/jnr-enxio
  63. 63. public class NativeSocketChannel! extends AbstractSelectableChannel! implements ByteChannel, NativeSelectableChannel {! public NativeSocketChannel(int fd);! public NativeSocketChannel(int fd, int ops);! public final int validOps();! public final int getFD();! public int read(ByteBuffer dst) throws IOException;! public int write(ByteBuffer src) throws IOException! public void shutdownInput() throws IOException;! public void shutdownOutput() throws IOException;! }
  64. 64. jnr-unixsocket • UNIX sockets for NIO • Built atop jnr-enxio • Fully selectable, etc • https://github.com/jnr/jnr-unixsocket
  65. 65. What Else? • NIO, NIO.2 • Native IO, symlinks, FS-walking, • Unmanaged memory • Selectable stdio, process IO • Low-level or other sockets (UNIX, ICMP, ...) • New APIs (graphics, crypto, OS, ...)
  66. 66. How Does It Perform?
  67. 67. Performance • Generated code leading to JNI call • Generated assembly version of native part • jnr-x86asm: Generate and link ASM • Used internally by jnr • https://github.com/jnr/jnr-x86asm
  68. 68. JNA getpid JNR getpid getpid calls, 100M times 100000ms 10000ms 1000ms 100ms 10ms 1ms
  69. 69. @IgnoreError import jnr.ffi.LibraryLoader;! import jnr.ffi.annotations.IgnoreError;!  ! public class GetPidJNRExample {! public interface GetPid {! @IgnoreError! long getpid();! }! ! public static void main( String[] args ) {! GetPid getpid = LibraryLoader! .create(GetPid.class)! .load("c");! ! getpid.getpid();! }! }
  70. 70. JNR getpid JNR getpid @IgnoreError getpid calls, 100M times 2000ms 1500ms 1000ms 500ms 0ms
  71. 71. But There's More to Do JNR getpid JNI JNR @IgnoreError getpid calls, 100M times 2000ms 1500ms 1000ms 500ms 0ms GCC -O3
  72. 72. JVM Help is Coming • Standard FFI API in JDK • JIT intelligence • Drop JNI overhead where possible • Bind native call directly at call site • Security policies, segv protection, etc • Time for an FFI JSR
  73. 73. Language Performance
  74. 74. History • JVM authors mentioned non-Java languages • Language authors have targeted JVM • Hundreds of JVM languages now • But JVM was a mismatch for many of them • Usually required tricks that defeated JVM optimizations • Or required features JDK could not provide
  75. 75. JVM Opcodes Invocation Field Access invokevirtual! invokeinterface! invokestatic! invokespecial getfield! setfield! getstatic! setstatic Stack Flow Control Boolean and Numeric Array Access *aload! *astore! b,s,c,i,l,d,f,a Local Vars Allocation
  76. 76. Goals of JSR 292 • A user-definable bytecode • Full freedom to define VM behavior • Fast method pointers + adapters • Optimizable like normal Java code • Avoid future modifications
  77. 77. Invoke // Static! System.currentTimeMillis()! Math.log(1.0)
  78. 78. Invoke // Static! System.currentTimeMillis()! Math.log(1.0)!  ! // Virtual! "hello".toUpperCase()! System.out.println()
  79. 79. Invoke // Static! System.currentTimeMillis()! Math.log(1.0)!  ! // Virtual! "hello".toUpperCase()! System.out.println()!  ! // Interface! myList.add("happy happy")! myRunnable.run()
  80. 80. Invoke // Static! System.currentTimeMillis()! Math.log(1.0)!  ! // Virtual! "hello".toUpperCase()! System.out.println()!  ! // Interface! myList.add("happy happy")! myRunnable.run()!  ! // Constructor and super! new ArrayList()! super.equals(other)
  81. 81. // Static! invokestatic java/lang/System.currentTimeMillis:()J! invokestatic java/lang/Math.log:(D)D! ! // Virtual! invokevirtual java/lang/String.toUpperCase:()Ljava/lang/String;! invokevirtual java/io/PrintStream.println:()V! ! // Interface! invokeinterface java/util/List.add:(Ljava/lang/Object;)Z! invokeinterface java/lang/Runnable.add:()V! ! // Special! invokespecial java/util/ArrayList.<init>:()V! invokespecial java/lang/Object.equals:(java/lang/Object)Z
  82. 82. invokevirtual! 1. Confirm object is of correct type 2. Confirm arguments are of correct type 3. Look up method on Java class invokestatic 4. Cache method 5. Invoke method invokestatic! 1. Confirm arguments are of correct type 2. Look up method on Java class 3. Cache method 4. Invoke method invokevirtual invokeinterface! 1. Confirm object’s type implements interface 2. Confirm arguments are of correct type 3. Look up method on Java class invokeinterface 4. Cache method 5. Invoke method invokespecial! 1. Confirm object is of correct type 2. Confirm arguments are of correct type 3. Confirm target method is visible 4. Look up method on Java class 5. Cache method 6. Invoke method invokespecial invokedynamic! 1. Call your bootstrap code 2. Bootstrap wires up a target function 3. Target function invoked directly until you change it
  83. 83. invokedynamic bytecode target method bo ot st ra p m et ho d method handles
  84. 84. Does It Work?
  85. 85. Indy Languages • New language impls • JavaScript: Dyn.js and Nashorn • Redline Smalltalk • Improved language performance • JRuby, Groovy, Jython • Java features too!
  86. 86. JRuby/Java 6 JRuby/Java 7 Times Faster than Ruby 1.9.3 5 4.32 3.75 3.66 3.44 2.5 2.658 1.914 1.25 0 1.346 base64 1.565 1.538 richards neural redblack
  87. 87. red/black tree, pure Ruby versus native ruby-2.0.0 + Ruby 2.48s ruby-2.0.0 + C ext 0.51s jruby + Ruby 0.29s 0 0.75 1.5 Runtime per iteration 2.25 3
  88. 88. Caveat Emptor • Indy was really slow in first Java 7 release • Got fast in 7u2...and turned out broken • Rewritten for 7u40 • Slow to warm up • Still some issues (memory use, etc) • Java 8 due in March…
  89. 89. All That C++
  90. 90. JVM Language JVM Bytecode Out of our control Written in C++ Bytecode Interpreter Bytecode JIT Native Code
  91. 91. What If… • The JVM’s JIT optimizer were written in Java • You could customize how the JIT works for your language or library • JITed code could directly make native calls
  92. 92. Graal • A 100% Java-based JIT framework • Grew out of the 100% Java “Maxine” JVM • Backends to assembly or HotSpot IR • Directly control code generation • Build a language without using JVM bytecode • http://openjdk.java.net/projects/graal/
  93. 93. JVM Language Plain Java APIs Graal Intermediate Representation Graal Optimizer Your Optimizations Under your control Native Code
  94. 94. However… • Not everyone is a compiler writer • Graal’s IR is low-level and nontrivial • Need to understand JVM internals • Need some understanding of CPU
  95. 95. The Dream • Design your language • ??? • PROFIT
  96. 96. What We Want • Design your language • Write an interpreter • PROFIT
  97. 97. Truffle • Language framework built on Graal • Designed to fulfill the dream • Implement interpreter • Truffle feeds that to backend • No compiler expertise needed • https://wiki.openjdk.java.net/display/Graal/Truffle+FAQ+and+Guidelines
  98. 98. JVM Language Truffle AST All we need Graal Intermediate Representation Graal Optimizer Native Code
  99. 99. What Have We Learned?
  100. 100. The JVM has its problems, but we can fix them.
  101. 101. OpenJDK and all these solutions are really, truly, open source.
  102. 102. Nothing is impossible.
  103. 103. Thank you! • Charles Oliver Nutter • @headius, headius@headius.com • http://blog.headius.com

×