yow sydney 2013 - beyond jvm · things that make! jruby difficult • startup time sucks! • jni...

115
Beyond JVM or, How to Boil the Ocean in Seven Years

Upload: others

Post on 27-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Beyond JVMor, How to Boil the Ocean in Seven Years

Page 2: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core
Page 3: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core
Page 4: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

+

Page 5: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

We Like the JVM

Page 6: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

No platform-specific compilation needed.

Page 7: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

No native library dependencies

Page 8: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Optimizing JIT compiler

Page 9: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Magical black box that runs our code

Page 10: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Things We Like About JVM

• Bytecode intermediate form

• Freedom from native libraries

• Optimizing JIT

• It just works (usually)

Page 11: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Things That Make JRuby Difficult

• Bytecode intermediate form

• Optimizing JIT

• Freedom from native libraries

• It just works (usually)

Page 12: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 13: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core
Page 14: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core
Page 15: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core
Page 16: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core
Page 17: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core
Page 18: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

There must be a way!

Page 19: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Startup Time

Page 20: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core
Page 21: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Java-Heavy JDK

• + Less native code to maintain

• + Easier portability

• + Easier to swap out native side

• – Takes longer to warm up

Page 22: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

JVM Bytecode

JVM Language

Bytecode Interpreter

Bytecode JIT

Native Code

Time

Page 23: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

JVM Bytecode

JVM Language

Bytecode Interpreter

Bytecode JIT

Native Code

Page 24: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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)

Page 25: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

JRuby Startup

-e 1

gem --help

rake -T

0 2.5 5 7.5 10

C Ruby JRuby

Page 26: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Tweaking Flags

• -client mode

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

• -X-C to disable JRuby’s compiler

• Heap sizes, code verification, etc etc

Page 27: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

JRuby Startup

-e 1

gem --help

rake -T

0 2.5 5 7.5 10

C Ruby JRuby JRuby (best)

Page 28: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 29: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 30: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

$ export JAVACMD=`which drip`!$ time jruby -e 1!real 0m1.655s user 0m4.486s sys0m0.231s!$ time jruby -e 1!real 0m0.577s user 0m0.052s sys0m0.065s

Page 31: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

JRuby Startup

-e 1

gem --help

rake -T

0 2.5 5 7.5 10

C Ruby JRuby JRuby (best) JRuby (drip)

Page 32: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

$ export DRIP_INIT_CLASS=org.jruby.main.DripMain!$ export DRIP_INIT=""!$ time jruby -e 1!real 0m0.580s user 0m0.052s sys0m0.063s!$ time jruby -e 1!real 0m0.155s user 0m0.049s sys0m0.058s

Page 33: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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;!

    }!

}!

Page 34: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

!    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()) {!

Page 35: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

!    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()) {!

Page 36: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

!    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()) {!

Page 37: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

        }!!        // 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;!    }!}!

Page 38: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

        }!!        // 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;!    }!}!

Page 39: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

        }!!        // 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;!    }!}!

Page 40: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

        }!!        // 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;!    }!}!

Page 41: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

        }!!        // 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;!    }!}!

Page 42: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

JRuby Startup

-e 1

gem --help

rake -T

0 2.5 5 7.5 10

C Ruby JRuby JRuby (best) JRuby (drip)JRuby (drip init)

Page 43: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

$ cat dripmain.rb# Preload some code Rails always needsrequire File.expand_path('../config/application', __FILE__)

Page 44: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

JRuby Startup

rake -T

0 2.5 5 7.5 10

C Ruby JRuby JRuby (best)JRuby (drip) JRuby (drip init) JRuby (dripmain)

Page 45: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

JRuby Startup

rake -T

0 0.275 0.55 0.825 1.1

C Ruby JRuby (dripmain)

Page 46: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Native Interop

Page 47: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

????

JVM World

Native World

Page 48: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core
Page 49: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core
Page 50: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

JNI

Page 51: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

User Code

JNI call

JNI impl

Target Library

Java

C/native

Page 52: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

public class GetPidJNI {! public static native long getpid();! ! public static void main( String[] args ) {! getpid();! }! ! static {! System.load(! System.getProperty("user.dir") +! "/getpidjni.dylib");! }!}

JNI

Page 53: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

/* 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

JNI

Page 54: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

#include "com_headius_jnr_presentation_GetPidJNI.h"! !jlong JNICALL Java_com_headius_jnr_1presentation_GetPidJNI_getpid! (JNIEnv *env, jclass c) {! ! return getpid();!}

JNI

Page 55: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

$ 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.0-SNAPSHOT.jar com.headius.jnr_presentation.GetPidJNI

JNI

Page 56: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

There Must Be A Better Way

Page 57: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

User Code

JNI call

JNI impl

Target Library

Java

C/native

Page 58: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

User Code

JNI call

JNI impl

Target Library

Java

C/native

Page 59: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 60: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

User Code

JNR stub

JNI call

JNI impl

libffi

Target Library

Java

C/native

Page 61: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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();! }!}

JNR

Page 62: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Layered Runtime

jffi

jnr-ffi

libffi

jnr-posix

jnr-constants

!

jnr-enxio jnr-x86asmjnr-unixsocket

etc etc

Page 63: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 64: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

jnr-ffi

• User-oriented API

• Roughly equivalent to what JNA gives you

• Functions, structs, callbacks, memory

• https://github.com/jnr/jnr-ffi

Page 65: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

jnr-posix

• Pre-bound set of POSIX functions

• Mostly driven by what JRuby, Jython use

• Goal: 100% of POSIX bound to Java

Page 66: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

public int chmod(String string, int i);!public int chown(String string, int i, int i1);!public int execv(String string, String[] strings);!public int execve(String string, String[] strings, String[] strings1);!public int fork();!public int seteuid(int i);!public int getgid();!public String getlogin();!public int getpgid();!public int getpgid(int i);!public int getpgrp();!public int getpid();!public int getppid();!public Passwd getpwent();!public Passwd getpwuid(int i);!public Passwd getpwnam(String string);!public Group getgrgid(int i);!public Group getgrnam(String string);!public int getuid();!public boolean isatty(FileDescriptor fd);!public int kill(int i, int i1);!public int symlink(String string, String string1);!public int link(String string, String string1);!public String readlink(String string) throws IOException;!public String getenv(String string);!public int setenv(String string, String string1, int i);!public int unsetenv(String string);!public int getpriority(int i, int i1);!public int setpriority(int i, int i1, int i2);!public int setuid(int i);!public FileStat stat(String string);!public int stat(String string, FileStat fs);!public int umask(int i);!public Times times();!public int utimes(String string, long[] longs, long[] longs1);!public int waitpid(int i, int[] ints, int i1);!public int wait(int[] ints);!public int errno();!public void errno(int i);!public int posix_spawnp(String string, List<? extends SpawnFileAction> list, List<? extends CharSequence> list1, List<? extends CharSequence> list2);

Page 67: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

POSIX posix = POSIXFactory.getPOSIX(! new MyPOSIXHandler(this),! isNativeEnabled);

Page 68: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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();!}

Page 69: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

public int chmod(String string, int i);!public int chown(String string, int i, int i1);!public int kill(int i, int i1);!public int getpriority(int i, int i1);!public int setpriority(int i, int i1, int i2);!public int waitpid(int i, int[] ints, int i1);!public int wait(int[] ints);!public int errno();!public void errno(int i);

Page 70: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

jnr-constants

• C/preprocessor constants in enum form

• Generator code to run on each platform

• Several pregenerated sets for jnr-posix

Page 71: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

public int open(String string, int flags, int mode);!public int socket(int i, int i1);

Page 72: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 73: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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;!}

Page 74: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

jnr-unixsocket

• UNIX sockets for NIO

• Built atop jnr-enxio

• Fully selectable, etc

• https://github.com/jnr/jnr-unixsocket

Page 75: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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, ...)

Page 76: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

How Does It Perform?

Page 77: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 78: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

getpid calls, 100M times

1ms

10ms

100ms

1000ms

10000ms

100000ms

JNA getpid JNR getpid

Page 79: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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();! }!}

@IgnoreError

Page 80: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

getpid calls, 100M times

0ms

500ms

1000ms

1500ms

2000ms

JNR getpid JNR getpid @IgnoreError

Page 81: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

getpid calls, 100M times

0ms

500ms

1000ms

1500ms

2000ms

JNR getpid JNI JNR @IgnoreError GCC -O3

But There's More to Do

Page 82: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 83: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Language Performance

Page 84: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 85: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

JVM Opcodes

Invocation invokevirtual!

invokeinterface!invokestatic!invokespecial

Field Access getfield!setfield!getstatic!setstatic

Array Access *aload!*astore!

b,s,c,i,l,d,f,a

Stack Local VarsFlow Control

AllocationBoolean and Numeric

Page 86: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 87: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 88: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 89: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Invoke// Static!System.currentTimeMillis()!Math.log(1.0)! !// Virtual!"hello".toUpperCase()!System.out.println()! !// Interface!myList.add("happy happy")!myRunnable.run()

Page 90: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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)

Page 91: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

// 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

invokestatic

invokevirtual

invokeinterface

invokespecial

Page 92: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

invokestatic!1. Confirm arguments are of correct type 2. Look up method on Java class 3. Cache method 4. Invoke method

invokevirtual!1. Confirm object is of correct type 2. Confirm arguments are of correct type 3. Look up method on Java class 4. Cache method 5. Invoke method

invokeinterface!1. Confirm object’s type implements interface 2. Confirm arguments are of correct type 3. Look up method on Java class 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

invokestatic

invokevirtual

invokeinterface

invokespecialinvokedynamic!1. Call your bootstrap code 2. Bootstrap wires up a target function 3. Target function invoked directly until you change it

Page 93: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

method handles

invokedynamic bytecode

bootstrap method

target method

Page 94: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Does It Work?

Page 95: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Indy Languages

• New language impls

• JavaScript: Dyn.js and Nashorn

• Redline Smalltalk

• Improved language performance

• JRuby, Groovy, Jython

• Java features too!

Page 96: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Times Faster than Ruby 1.9.3

0

1.25

2.5

3.75

5

base64 richards neural redblack

4.32

3.663.44

2.658

1.565

1.914

1.5381.346

JRuby/Java 6 JRuby/Java 7

Page 97: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

red/black tree, pure Ruby versus native

ruby-2.0.0 + Ruby

ruby-2.0.0 + C ext

jruby + Ruby

Runtime per iteration

0 0.75 1.5 2.25 3

0.29s

0.51s

2.48s

Page 98: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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…

Page 99: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

All That C++

Page 100: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Out of our control Written in C++

JVM Bytecode

JVM Language

Bytecode Interpreter

Bytecode JIT

Native Code

Page 101: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 102: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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/

Page 103: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Plain Java APIs

Under your control

Graal Intermediate

Representation

JVM Language

Graal Optimizer

Native Code

Your Optimizations

Page 104: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 105: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

The Dream

• Design your language

• ???

• PROFIT

Page 106: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

What We Want

• Design your language

• Write an interpreter

• PROFIT

Page 107: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

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

Page 108: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Truffle AST

JVM Language

Graal Intermediate

Representation

Graal Optimizer

Native Code

All we need

Page 109: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core
Page 110: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core
Page 111: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

What Have We Learned?

Page 112: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

The JVM has its problems, but we can

fix them.

Page 113: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

OpenJDK and all these solutions are really, truly, open source.

Page 114: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Nothing is impossible.

Page 115: YOW Sydney 2013 - Beyond JVM · Things That Make! JRuby Difficult • Startup time sucks! • JNI is a massive pain to use! • Hard to get unusual languages to optimize! • Core

Thank you!

• Charles Oliver Nutter

• @headius, [email protected]

• http://blog.headius.com