This document provides an overview of upcoming technologies beyond the Java Virtual Machine (JVM). It begins with introductions and then discusses several topics:
- There are many open-source JVMs beyond Oracle's HotSpot such as JamVM, Maxine, and JikesRVM.
- Reasons for using the JVM include its large standard library and ease of portability compared to alternative virtual machines. However, startup time can be slow.
- Techniques for improving JVM startup time are discussed, such as saving JIT-compiled code and using the Drip tool to pre-initialize JVMs.
- Native interoperability is explored through the Java Native Interface (JNI
2. 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
3. Goals
ā¢ Get you excited about the future of JVM
ā¢ Show you there are very few unsolvables
ā¢ Convince you to get involved
4. 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
13. Java-Heavy JDK
ā¢ + Less native code to maintain
ā¢ + Easier portability
ā¢ + Easier to swap out native side
ā¢ ā Takes longer to warm up
14. Save JITed Code?
ā¢ Code will change across runs
ā¢ Often has speciļ¬c memory addresses
ā¢ May optimize object layout differently
ā¢ Which JIT output?
ā¢ Client, Server, Tiered (1-4)
16. 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/ļ¬atland/drip
17. $ export JAVACMD=`which drip`
!
$ time jruby -e 1
!
real 0m1.655s
user 0m4.486s
sys 0m0.231s
!
system ~/projects/jruby $ time jruby -e 1
!
real 0m0.577s
user 0m0.052s
sys 0m0.065s
18. $ export DRIP_INIT_CLASS=org.jruby.main.DripMain
!
$ export DRIP_INIT=""
!
$ time jruby -e 1
!
real 0m0.580s
user 0m0.052s
sys 0m0.063s
!
system ~/projects/jruby $ time jruby -e 1
!
real 0m0.155s
user 0m0.049s
sys 0m0.058s
31. ...but if you have to call
native libraries, you
might as well enjoy it.
32. 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
33. A Java API for binding
native libraries and
native memory
34. Justiļ¬cations
ā¢ 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, ...)
40. 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
41. jnr-fļ¬
ā¢ User-oriented API
ā¢ Roughly equivalent to what JNA gives you
ā¢ Functions, structs, callbacks, memory
ā¢ https://github.com/jnr/jnr-fļ¬
43. jnr-posix
ā¢ Pre-bound set of POSIX functions
ā¢ Mostly driven by what JRuby, Jython use
ā¢ Goal: 100% of POSIX bound to Java
44. 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);
45. POSIX posix = POSIXFactory.getPOSIX(!
new MyPOSIXHandler(this),!
isNativeEnabled);
46. 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();!
}
48. 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;!
}
49. jnr-unixsocket
ā¢ UNIX sockets for NIO
ā¢ Built atop jnr-enxio
ā¢ Fully selectable, etc
ā¢ https://github.com/jnr/jnr-unixsocket
53. JNR getpid
JNR getpid @IgnoreError
getpid calls, 100M times
2000ms
1500ms
1000ms
500ms
0ms
54. But There's More to Do
JNR getpid
JNI
JNR @IgnoreError
getpid calls, 100M times
2000ms
1500ms
1000ms
500ms
0ms
GCC -O3
55. 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
57. 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
60. Goals of JSR 292
ā¢ A user-deļ¬nable bytecode
ā¢ Full freedom to deļ¬ne VM behavior
ā¢ Fast method pointers + adapters
ā¢ Optimizable like normal Java code
ā¢ Avoid future modiļ¬cations
61. A User-deļ¬nable
Bytecode
You decide how the JVM implements it
+
Method Pointers
and Adapters
Faster than reļ¬ection, with user-deļ¬ned
argument, ļ¬ow, and exception handling
66. invokevirtual!
1. Conļ¬rm object is of correct type
2. Conļ¬rm arguments are of correct type
3. Look up method on Java class
invokestatic
4. Cache method
5. Invoke method
invokestatic!
1. Conļ¬rm arguments are of correct type
2. Look up method on Java class
3. Cache method
4. Invoke method
invokevirtual
invokeinterface!
1. Conļ¬rm objectās type implements interface
2. Conļ¬rm arguments are of correct type
3. Look up method on Java class
invokeinterface
4. Cache method
5. Invoke method
invokespecial!
1. Conļ¬rm object is of correct type
2. Conļ¬rm arguments are of correct type
3. Conļ¬rm 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
69. Indy Languages
ā¢ New language impls
ā¢ JavaScript: Dyn.js and Nashorn
ā¢ Redline Smalltalk
ā¢ Improved language performance
ā¢ JRuby, Groovy, Jython
ā¢ Java features too!
73. Caveat Emptor
ā¢ Indy was really slow in ļ¬rst 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ā¦
76. 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
77. 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/
79. 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
81. Typical JVM Language
ā¢ Design your language
ā¢ Maybe write an interpreter
ā¢ Compile to JVM bytecode
ā¢ Pray that the JVM optimizes it right
ā¢ PROFIT
82. What We Want
ā¢ Design your language
ā¢ Write an interpreter
ā¢ PROFIT
83. Trufļ¬e
ā¢ Language framework built on Graal
ā¢ Designed to fulļ¬ll the dream
ā¢ Implement interpreter
ā¢ Trufļ¬e feeds that to backend
ā¢ No compiler expertise needed
ā¢ https://wiki.openjdk.java.net/display/Graal/Trufļ¬e+FAQ+and+Guidelines
87. 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
88. Thank you!
ā¢ Charles Oliver Nutter
ā¢ @headius, headius@headius.com
ā¢ http://blog.headius.com