Slide 1

Slide 1 text

Beyond JVM A tour of upcoming technologies

Slide 2

Slide 2 text

Reminders • Green/yellow/red rating system after talk • Extended Q/A by blue chairs at 12:15 • If not green…stop by and tell me why

Slide 3

Slide 3 text

Me • Charles Oliver Nutter • Red Hat (yes, I have one; no, I don’t wear it) • JRuby and JVM languages • JVM hacking and spelunking • @headius

Slide 4

Slide 4 text

Goals • Get you excited about the future of JVM • Show you there are very few unsolvables • Convince you to get involved

Slide 5

Slide 5 text

What is “JVM”? • The JVM is software that runs JVM bytecode • Java, Scala, Groovy, JRuby, Clojure, … • OpenJDK contains Sun’s JVM “HotSpot” • Oracle’s JDK is based on OpenJDK • Many other JVMs exist for many platforms • Some just replace HotSpot

Slide 6

Slide 6 text

OSS JVMs • Avian • Azul Zulu • CACAO • Dalvik • GCJ • HaikuVM • HotSpot • IcedTea • IKVM.NET • Jamiga • JamVM • Jaos • Jato VM • Jelatine JVM • JESSICA • Jikes RVM (Jikes Research Virtual Machine) • JOP • Juice • Jupiter • JwiK • Kaffe • leDos • MateVM • Maxine • Mika VM • miniMV • Mysaifu • NanoVM • RoboVM • SableVM • Squawk virtual machine • SuperWaba • TakaTuka • TinyVM. • VM02

Slide 7

Slide 7 text

Why JVM?

Slide 8

Slide 8 text

Why Not JVM?

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

OpenJDK

Slide 13

Slide 13 text

OpenJDK Timeline • For years, Sun’s JDK is closed source • Sun Microsystems starts OSS process in 2005 • Official announcement at JavaOne 2006 • HotSpot OSSed 12 Nov 2006 • JDK OSSed 8 May 2007

Slide 14

Slide 14 text

A 2006 report prepared for the EU by UNU-MERIT stated that Sun was the largest corporate contributor to open source movements in the world.

Slide 15

Slide 15 text

According to this report, Sun's open source contributions exceed the combined total of the next five largest commercial contributors.

Slide 16

Slide 16 text

Unfortunately…

Slide 17

Slide 17 text

Sun Becomes Oracle • Sun broke open the OSS piñata • Java, Solaris, OpenOffice, NetBeans, VirtualBox, ZFS, Dtrace, and more • Unable to capitalize on OSS • Hardware can’t make up the difference • Oracle takes over Sun…oh no!!!

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Java SE 7 Updates 0 22.5 45 67.5 90 7u1 7u2 7u3 7u4 7u5 7u6 7u7 7u9 7u10 7u11 7u13 7u15 7u17 7u21 7u25 7u40 7u45 Time since last release Flurry of exploits New numbering 9mo of security releases

Slide 24

Slide 24 text

The OpenJDK landscape looks different this year. Oracle's complete grip on OpenJDK is slowly loosening. They're still the dominant player and perhaps always will be, but things are more level than they were.! ! - Andrew Haley of Red Hat! ! Upon nomination to the OpenJDK governing board for 2013

Slide 25

Slide 25 text

Truths of OpenJDK • It is really truly OSS, under GPL+CPE • You can fork it • You can distribute builds of it • You can contribute to it • Oracle is learning how to do OSS

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Java 5 was released September 30, 2004

Slide 28

Slide 28 text

Java 6 was released December 11, 2006

Slide 29

Slide 29 text

Java 7 was released July 28, 2011

Slide 30

Slide 30 text

Java 7 Features • Strings in switch statements • More numeric literal forms • Type inference for generic instances • try-with-resources statement • Multiple-catch

Slide 31

Slide 31 text

static String chooseGreeting(String language) {" switch (language) {" case "Java": return "I love to hate you!";" case "Scala": return "I love you, I think!";" case "Clojure": return "(love I you)";" case "Groovy": return "I love ?: you";" case "Ruby": return "I.love? you # => true";" default: return "Who are you?";" }" }

Slide 32

Slide 32 text

Java 8 Features • Lambda expressions • Type annotations

Slide 33

Slide 33 text

Java 8 Features • Lambda expressions • Type annotations

Slide 34

Slide 34 text

public static void doSort(List input) {" Collections.sort(input,! (a,b)->Integer.compare(a.length(), b.length()));" }" " public static String getInitials(List input) {" return input.stream()" .map(x->x.substring(0,1))" .collect(Collectors.joining());" }

Slide 35

Slide 35 text

Java 9 Features? • Modularization? • Dynamic invocation? • Value types? • Reified generics? • Coroutines? (working patch available!) • Tail calls? (working patch available!)

Slide 36

Slide 36 text

Alternative Languages

Slide 37

Slide 37 text

High-Profile Languages • Clojure • Groovy • Scala • JRuby

Slide 38

Slide 38 text

Newcomers • Kotlin • Ceylon • Xtend • Mirah

Slide 39

Slide 39 text

Classics • Jython • Rhino • BeanShell

Slide 40

Slide 40 text

Ported Languages • Ada • AWK • BASIC • BBx • Boo • C • COBOL • ColdFusion • Railo • Open BlueDragon • Common Lisp • Component Pascal • Erlang • Forth • Go • Haxe • JavaScript • Logo • Lua • Yeti ML • Oberon-2 • OCaml • Object Pascal • PHP • Prolog • Python • R • REXX • Ruby • Scheme • Smalltalk • Tcl

Slide 41

Slide 41 text

New Languages • Alef++ • Ateji PX • BBj • BeanShell • Ceylon • CAL • E • Fantom • Flow Java. • Fortress • Frege • Frink • Golo • Gosu • Hecl • Ioke • KBML • Kotlin • Jabaco • Jaskell • Jelly • Join Java • Joy • Judoscript • Libretto. • Mirah • N.A.M.E. Basic. • NetLogo • Nice • Noop • ObjectScript • PHP.reboot • Pizza • Pnuts • Processing • Stab • Sleep • V • X10 • Xtend • Zest

Slide 42

Slide 42 text

JVM is not just Java

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Native Interop

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

User Code JNI call JNI impl Target Library Java C/native

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

/* DO NOT EDIT THIS FILE - it is machine generated */" #include " /* 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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

$ 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

Slide 52

Slide 52 text

Nobody enjoys calling native libraries...

Slide 53

Slide 53 text

...but if you have to call native libraries, you might as well enjoy it.

Slide 54

Slide 54 text

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 • Maven artifacts for everything

Slide 55

Slide 55 text

A Java API for binding native libraries and native memory

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

User Code JNI call JNI impl Target Library Java C/native

Slide 58

Slide 58 text

User Code JNI call JNI impl Target Library Java C/native

Slide 59

Slide 59 text

User Code JNR stub JNI call JNI impl libffi Target Library Java C/native

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Layered Runtime jffi jnr-ffi libffi jnr-posix jnr-constants ! jnr-enxio jnr-x86asm jnr-unixsocket etc etc

Slide 62

Slide 62 text

JNR 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

Slide 63

Slide 63 text

jnr-ffi • User-oriented API • Roughly equivalent to what JNA gives you • Functions, structs, callbacks, memory • https://github.com/jnr/jnr-ffi

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

jnr-posix • Pre-bound set of POSIX functions • Mostly driven by what JRuby, Jython use • Goal: 100% of POSIX bound to Java

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

jnr-unixsocket • UNIX sockets for NIO • Built atop jnr-enxio • Fully selectable, etc • https://github.com/jnr/jnr-unixsocket

Slide 72

Slide 72 text

How Does It Perform?

Slide 73

Slide 73 text

getpid calls, 100M times 1ms 10ms 100ms 1000ms 10000ms 100000ms JNA getpid JNR getpid

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

getpid calls, 100M times 0ms 500ms 1000ms 1500ms 2000ms JNR getpid JNR getpid @IgnoreError

Slide 76

Slide 76 text

getpid calls, 100M times 0ms 500ms 1000ms 1500ms 2000ms JNR getpid JNI JNR @IgnoreError GCC -O3 But There's More to Do

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

Recap • OpenJDK is awesome • Java is evolving • JVM languages are everywhere • Even native code is accessible

Slide 79

Slide 79 text

27 April, 2011

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

What is invokedynamic

Slide 82

Slide 82 text

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 Vars Flow Control Allocation Boolean and Numeric

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

+ Method Pointers and Adapters Faster than reflection, with user-defined argument, flow, and exception handling A User-definable Bytecode You decide how the JVM implements it

Slide 86

Slide 86 text

invokedynamic user-def’d bytecode invokedynamic opcode method pointers MethodHandles

Slide 87

Slide 87 text

VM Operations Method Lookup Type Checking Branch Method Cache Method Invocation Target Object Object’s Class void foo() static void bar() instanceof obj.foo() JVM void foo() Call Site

Slide 88

Slide 88 text

// Static" System.currentTimeMillis()" Math.log(1.0)" " // Virtual" "hello".toUpperCase()" System.out.println()" " // Interface" myList.add("happy happy")" myRunnable.run()" " // Special" new ArrayList()" super.equals(other)

Slide 89

Slide 89 text

// 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.:()V" invokespecial java/lang/Object.equals:(java/lang/Object)Z invokestatic invokevirtual invokeinterface invokespecial

Slide 90

Slide 90 text

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 invokespecial invokedynamic! 1. Call bootstrap handle (your code) 2. Bootstrap prepares CallSite + MethodHandle 3. MethodHandle invoked now and future (until CallSite changes)

Slide 91

Slide 91 text

method handles invokedynamic bytecode bootstrap m ethod target method

Slide 92

Slide 92 text

How Do You Benefit?

Slide 93

Slide 93 text

Indy Languages • New language impls • JavaScript: Dyn.js and Nashorn • Redline Smalltalk • Improved language performance • JRuby, Groovy, Jython • Java features too!

Slide 94

Slide 94 text

Times Faster than Ruby 1.9.3 0 1.25 2.5 3.75 5 base64 richards neural redblack 4.32 3.66 3.44 2.658 1.565 1.914 1.538 1.346 JRuby/Java 6 JRuby/Java 7

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

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…

Slide 97

Slide 97 text

Feeling Helpless?

Slide 98

Slide 98 text

Out of our control Written in C++ JVM Bytecode JVM Language Bytecode Interpreter Bytecode JIT Native Code

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

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/

Slide 101

Slide 101 text

Graal Intermediate Representation JVM Language Graal Optimizer Native Code Your Transformations Your Optimizations Plain Java APIs Under your control

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

The Dream • Design your language • ??? • PROFIT

Slide 104

Slide 104 text

Without JVM • Design your language • Work out memory model • Create an optimizing compiler • Spend ten years debugging it • PROFIT

Slide 105

Slide 105 text

With JVM • Design your language • Maybe write an interpreter • Compile to JVM bytecode • Pray that the JVM optimizes it right • PROFIT

Slide 106

Slide 106 text

What We Want • Design your language • ??? • PROFIT

Slide 107

Slide 107 text

What We Want • Design your language • Write an interpreter • PROFIT

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

Traditional Approach Any JVM Custom made bytecode compiler Guest Language AST interpreter in pure Java Bytecode emitted by guest language

Slide 110

Slide 110 text

Truffle Atop JVM Any JVM Truffle Guest Language AST interpreter in pure Java Bytecode based on interpreter flow

Slide 111

Slide 111 text

Truffle Atop Graal Graal JVM Truffle Guest Language AST interpreter in pure Java Direct access to compiler internals, IR, machine code cache, on stack replacement, etc etc

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

The Final Word • JVM is a powerful platform • Java and other languages are evolving • The JVM is adapting to our needs • New tools breaking JVM’s boundaries

Slide 115

Slide 115 text

Thank you! • Charles Oliver Nutter • @headius, headius@headius.com • http://blog.headius.com