10. Things We Like About
JVM
• Bytecode intermediate form
• Freedom from native libraries
• Optimizing JIT
• It just works (usually)
11. Things That Make
JRuby Difficult
• Bytecode intermediate form
• Optimizing JIT
• Freedom from native libraries
• It just works (usually)
12. 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
24. 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)
28. 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
29. 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
30. $ 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
59. 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
63. 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
64. jnr-ffi
• User-oriented API
• Roughly equivalent to what JNA gives you
• Functions, structs, callbacks, memory
• https://github.com/jnr/jnr-ffi
65. jnr-posix
• Pre-bound set of POSIX functions
• Mostly driven by what JRuby, Jython use
• Goal: 100% of POSIX bound to Java
66. 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);
67. POSIX posix = POSIXFactory.getPOSIX(!
new MyPOSIXHandler(this),!
isNativeEnabled);
68. 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();!
}
73. 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;!
}
74. jnr-unixsocket
• UNIX sockets for NIO
• Built atop jnr-enxio
• Fully selectable, etc
• https://github.com/jnr/jnr-unixsocket
75. 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, ...)
77. 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
80. JNR getpid
JNR getpid @IgnoreError
getpid calls, 100M times
2000ms
1500ms
1000ms
500ms
0ms
81. But There's More to Do
JNR getpid
JNI
JNR @IgnoreError
getpid calls, 100M times
2000ms
1500ms
1000ms
500ms
0ms
GCC -O3
82. 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
84. 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
86. 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
92. 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
95. Indy Languages
• New language impls
• JavaScript: Dyn.js and Nashorn
• Redline Smalltalk
• Improved language performance
• JRuby, Groovy, Jython
• Java features too!
97. 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
98. 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…
101. 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
102. 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/
104. 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
106. What We Want
• Design your language
• Write an interpreter
• PROFIT
107. 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