3. 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
4. Goals
• Get you excited about the future of JVM
• Show you there are very few unsolvables
• Convince you to get involved
5. 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. 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
14. 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.
15. According to this report, Sun's open
source contributions exceed the
combined total of the next five
largest commercial contributors.
17. 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!!!
23. Java SE 7 Updates
Time since last release
New numbering
90
Flurry of exploits
67.5
45
22.5
9mo of security releases
0
45
7u
40
7u
25
7u
21
7u
17
7u
15
7u
13
7u
11
7u
10
7u
9
7u
7
7u
6
7u
5
7u
4
7u
3
7u
2
7u
1
7u
24. 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
25. 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
30. Java 7 Features
• Strings in switch statements
• More numeric literal forms
• Type inference for generic instances
• try-with-resources statement
• Multiple-catch
31. 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?";"
}"
}
53. ...but if you have to call
native libraries, you
might as well enjoy it.
54. 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
55. A Java API for binding
native libraries and
native memory
56. 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, ...)
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();"
}
70. 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;!
}
71. jnr-unixsocket
• UNIX sockets for NIO
• Built atop jnr-enxio
• Fully selectable, etc
• https://github.com/jnr/jnr-unixsocket
75. JNR getpid
JNR getpid @IgnoreError
getpid calls, 100M times
2000ms
1500ms
1000ms
500ms
0ms
76. But There's More to Do
JNR getpid
JNI
JNR @IgnoreError
getpid calls, 100M times
2000ms
1500ms
1000ms
500ms
0ms
GCC -O3
77. 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
78. Recap
• OpenJDK is awesome
• Java is evolving
• JVM languages are everywhere
• Even native code is accessible
80. 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
84. 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
85. A User-definable
Bytecode
You decide how the JVM implements it
+
Method Pointers
and Adapters
Faster than reflection, with user-defined
argument, flow, and exception handling
90. 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 bootstrap handle (your code)
2. Bootstrap prepares CallSite + MethodHandle
3. MethodHandle invoked now and future (until CallSite changes)
93. Indy Languages
• New language impls
• JavaScript: Dyn.js and Nashorn
• Redline Smalltalk
• Improved language performance
• JRuby, Groovy, Jython
• Java features too!
95. 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
96. 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…
99. 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
100. 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/
102. 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
107. What We Want
• Design your language
• Write an interpreter
• PROFIT
108. 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
110. Truffle Atop JVM
Guest Language
AST interpreter in pure Java
Truffle
Bytecode based on interpreter flow
Any JVM
111. Truffle Atop Graal
Guest Language
AST interpreter in pure Java
Truffle
Direct access to compiler internals,
IR, machine code cache,
on stack replacement, etc etc
Graal JVM
114. 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
115. Thank you!
• Charles Oliver Nutter
• @headius, headius@headius.com
• http://blog.headius.com