TNF at the Johannes Kepler University Linz Head of the Institute: Prof. Dr. Dr. h.c. Hanspeter Mössenböck Main research topics: — Languages, Compilers, Virtual Machines — Automated Software Engineering — Optimization Systems - OptLet framework 30 employees, only 1/3 paid by government 700,000 € third-party funds More information: http://ssw.jku.at/, http://ssw.jku.at/Research/Projects/ 4
Engineering — Software Product Lines • Siemens VAI and Siemens AG - Corporate Technology, Germany — Capture-Replay-Analysis of Real-Time Systems • KEBA AG, Linz — Dynamic Plugin-Architectures • BMD Systemhaus GmbH, Steyr New research topics planned for 2013, together with Dynatrace and others... 5
research In close collaboration with, and mostly funded by, Oracle (Sun Microsystems) — Since 2001 — 2.5 PhD students — 2 - 3 part-time master students — Oracle Labs full-time employee in Linz (Thomas Würthinger) “Generalization of Just-in-Time Trace Compilation for Java” funded by FWF Coco/R, Visualization of Compiler Data Structures, ... 6
— JavaScript technique applied to Java Garbage Collection for High-End Mobile Devices (G1 lite) — Multi-tasking client Java environments for high-end mobile devices Graal (for HotSpot and Maxine) — Just-in-Time compiler for Java and other languages, written in Java — Modular, extensible, high peak performance, dynamic — More optimization potential due to novel Intermediate Representation (IR) — Competitive JavaScript, Python, ... performance 7
Fujitsu, ...), Startups (OptLet) Projects developed in Linz for the HotSpot JVM: — Static Single Assignment form IR for HotSpot client compiler — Escape Analysis (Thomas Kotzmann) — Linear Scan Register Allocation (J2SE 1.6, Christian Wimmer) — Dynamic Code Evolution (Thomas Würthinger, ongoing, http://ssw.jku.at/dcevm/) — Coroutines? (maybe...) 8
subroutines — Multiple entry/exit points — Remembers position and local variables Available in different shapes: — Stackful / stackless — First-class coroutines — Symmetric / asymmetric A B
alternative to threads Natural control abstraction for numerous problems — Scanner/parser (Conway 1963) — Non-parallel problems — Do not expose parallelism where there is none! Inversion of algorithms — Converting callback-based algorithms (e.g. XML parser) into iterative algorithms — Can be done manually, but coroutines do it for free!
easier programming and more performance — Threads are too expensive, especially if you need lots of them • Standard solution: use thread pools to process work units • Requires slicing the program into small pieces — Hide the complexity of asynchronous I/O — Move running applications between Virtual Machines
implementations need to emulate coroutines — Using threads • Synchronize multiple threads so that they look like coroutines — Compile-time transformations • Complex compilers • Methods split into smaller chunks – less optimization opportunities • Local variables on heap — Continuation-passing style
features / bytecode — Stackful, first-class, symmetric and asymmetric — Possibility to serialize coroutines (although unsafe) Performance and feature trade-off — Fast switching — Many coroutines — Low implementation complexity — Fully featured
to do in a safe manner => Coroutines for the HotSpotTM JVM — Coroutines: part of the Multi-Language Virtual Machine (MLVM) effort — Source available in the MLVM repositories — Binaries available from http://ssw.jku.at/General/Staff/LS/coro/ Prototype! JSR work is ongoing...
Coroutine(Runnable target); // some constructors omitted.. public boolean isFinished(); public static void yield(); public static void yieldTo(Coroutine target); protected void run(); } Java API
public AsymCoroutine(); // some constructors omitted.. public boolean isFinished(); public InT ret(OutT value); public InT ret(); // short for ret(null) public OutT call(InT input); public OutT call(); // short for call(null) protected OutT run(InT value); @Override public Iterator<OutT> iterator(); } Java API
public AsymCoroutine(AsymRunnable<? super InT, ? extends OutT> target); // ... } public interface AsymRunnable<InT, OutT> { public OutT run(AsymCoroutine<? extends InT, ? super OutT> coro, InT value); } Java API - with AsymRunnable
One coroutine per session: perfect fit! Many coroutines running on one thread Coroutines consume less resources, are much faster than threads But what if a coroutine blocks? — Due to IO: disk, DB, … — Other coroutines on thread are blocked! => Other thread needs to take over
thread Problems: locking and native code — Not allowed when migrating coroutines Best-effort: no migration if coroutine is running or about to run “Coroutine stealing” implementation boolean result = coroutine.steal(); API (symmetric and asymmetric):
per session — Easy to specify user interaction — Very fast switching — Blocking sessions solved by migration What to do with inactive sessions? — Memory starts to fill up – sessions shouldn't be thrown away! Solution: serialize session/coroutine to disk, DB, ...
operation... Replace a new coroutine's content — Creates interpreter frames — Replaced with compiled frames by OSR coroutine.deserialize(frames); API (symmetric and asymmetric):
applications will need custom logic — Extra logic for java.lang.reflect.Method objects — Business logic: how to serialize DB references, etc. Lots of other use cases: portable agents, compilers, ...
run Stacks are expensive! — Memory and address space (>64kb) — OS resources “Sharing” of stacks — Expensive context switch (copying) Compromise: allow a certain number of stacks per thread (currently: 100)
repository — Patch to the OpenJDK source code Binary available from http://ssw.jku.at/General/Staff/LS/coro/ Efforts to create Coroutine JSR are underway
Stadler: Serializable Coroutines for the HotSpot™ Java Virtual Machine Master's thesis, Johannes Kepler University Linz, February 2011 http://ssw.jku.at/Research/Papers/Stadler11Master/ Lukas Stadler, Thomas Würthinger, Christian Wimmer: Efficient Coroutines for the Java Platform 8th International Conference on Principles and Practice of Programming in Java, September 2010