Download Inlining Java Native Calls at Runtime

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Inlining Java Native Calls at Runtime
(CASCON 2005 – 4th Workshop on Compiler Driven Performance)
Levon Stepanian, Angela Demke Brown
Computer Systems Group
Department of Computer Science, University of Toronto
Allan Kielstra, Gita Koblents, Kevin Stoodley
IBM Toronto Software Lab
In a nutshell
• Runtime native function inlining into Java
• Optimizing transformations on inlined JNI calls
• Opaque and binary-compatible while boosting
performance
Java Code
Native Function Call
Native Code
In a nutshell
• Runtime native function inlining into Java
• Optimizing transformations on inlined JNI calls
• Opaque and binary-compatible while boosting
performance
Java Code
Native Code
inlined
In a nutshell
• Runtime native function inlining into Java
• Optimizing transformations on inlined JNI calls
• Opaque and binary-compatible while boosting
performance
Java Code
Native Code
inlined
+
optimized
Motivation
• The JNI
•
•
•
•
Java’s interoperability API
Callouts and callbacks
Opaque
Binary-compatible
Java App
JVM + JIT
JNI
Callout
Callback
Native (Host)
Environment
Native
App/Lib
Motivation
• The JNI
• Pervasive
• Legacy codes
• Performance-critical, architecture-dependent
• Features unavailable in Java (files, sockets
etc.)
Motivation
• Callouts run to 2 to 3x slower than Java calls
• Callback overheads are an order of magnitude larger
• JVM handshaking requirements for threads leaving and reentering JVM context
• i.e. stack switching, reference collection, exception handling
• JIT compiler can’t predict side-effects of native
function call
Our Solution
• JIT compiler based optimization that inlines
native code into Java
• JIT compiler transforms inlined JNI
function calls to constants, cheaper
operations
• Inlined code exposed to JIT compiler
optimizations
Infrastructure
• IBM TR JIT Compiler + IBM J9 VM
• Native IL to JIT IL conversion mechanism
• Exploit Native IL stored in native libraries
• W-Code to TR-IL at runtime
TR JIT
+
Machine
code
Static
compiler IL
Outline
•
•
•
•
Background Information 
Method
Results
Future Work
Sample Java Class
class SetFieldXToFive{
public int x;
public native foo();
static{
System.loadLibrary(…);
}
}
Sample Java Class
class SetFieldXToFive{
public int x;
public native foo();
static{
System.loadLibrary(…);
}
}
Sample Native Code
GOAL: obj.x = 5
JNIEXPORT void JNICALL Java_SetFieldXToFive_foo
(JNIEnv * env, jobject obj){
jclass cls = (*env)->GetObjectClass(env,obj);
jfieldID fid =
(*env)->GetFieldID(env,cls,“x","I");
if (fid == NULL)
return;
(*env)->SetIntField(env,obj,fid,5);
}
Sample Native Code
GOAL: obj.x = 5
JNIEXPORT void JNICALL Java_SetFieldXToFive_foo
(JNIEnv * env, jobject obj){
SetFieldXToFive
jclass cls = (*env)->GetObjectClass(env,obj);
jfieldID fid =
(*env)->GetFieldID(env,cls,“x","I");
if (fid == NULL)
return;
(*env)->SetIntField(env,obj,fid,5);
}
Sample Native Code
GOAL: obj.x = 5
JNIEXPORT void JNICALL Java_SetFieldXToFive_foo
(JNIEnv * env, jobject obj){
jclass cls = (*env)->GetObjectClass(env,obj);
jfieldID fid =
(*env)->GetFieldID(env,cls,“x","I");
if (fid == NULL)
return;
(*env)->SetIntField(env,obj,fid,5);
}
Sample Native Code
GOAL: obj.x = 5
JNIEXPORT void JNICALL Java_SetFieldXToFive_foo
(JNIEnv * env, jobject obj){
jclass cls = (*env)->GetObjectClass(env,obj);
jfieldID fid =
(*env)->GetFieldID(env,cls,“x","I");
if (fid == NULL)
return;
(*env)->SetIntField(env,obj,fid,5);
}
Sample Native Code
GOAL: obj.x = 5
JNIEXPORT void JNICALL Java_SetFieldXToFive_foo
(JNIEnv * env, jobject obj){
jclass cls = (*env)->GetObjectClass(env,obj);
jfieldID fid =
(*env)->GetFieldID(env,cls,“x","I");
if (fid == NULL)
return;
(*env)->SetIntField(env,obj,fid,5);
}
Native Inlining Overview
1. Inliner detects a native callsite
2. Extracts and converts Native IL to JIT IL
3. Identifies inlined JNI calls
4. Transforms inlined JNI calls
5. Finishes inlining
Method – Step 1
TR JIT
1. Inliner detects a
native callsite
Inliner
Java Code
Call to obj.foo()
foo(){…}
(Native code)
Method – Step 2
1. Inliner detects a
native callsite
Native IL
2. Extracts and converts
Native IL to JIT IL
JIT IL
Method – Step 3
1. Inliner detects a
native callsite
JIT IL
/* call to GetObjectClass */
…
/* call to GetFieldID */
…
/* call to SetFieldID */
…
Pre-constructed
IL shapes
2. Extracts and converts
Native IL to JIT IL
3. Identifies inlined JNI
calls
Method – Step 4
jclass cls =
(*env)->GetObjectClass(env,obj);
1. Inliner detects a
native callsite
2. Extracts and converts
Native IL to JIT IL
jfieldID fid =
(*env)->GetFieldID(env,cls,“x","I");
if (fid == NULL)
return;
(*env)->SetIntField(env,obj,fid,5);
3. Identifies inlined JNI
calls
4. Transforms inlined
JNI calls
Method – Step 4
jclass cls =SetFieldXToFive class data
Constant:
(*env)->GetObjectClass(env,obj);
structure
1. Inliner detects a
native callsite
2. Extracts and converts
Native IL to JIT IL
jfieldID fid =
(*env)->GetFieldID(env,cls,“x","I");
Constant:
Offset of field “x”
if (fid == NULL)
return;
(*env)->SetIntField(env,obj,fid,5);
JIT
IL: obj.x = 5
3. Identifies inlined JNI
calls
4. Transforms inlined
JNI calls
The Big Picture
TR JIT
Inliner
Java Code
Call to obj.foo()
Before Native
Inlining &
Callback
Transformations
foo(){…}
(Native code)
1. Inliner detects a
native callsite
2. Extracts and converts
Native IL to JIT IL
3. Identifies inlined JNI
calls
4. Transforms inlined
JNI calls
5. Finishes inlining
The Big Picture
TR JIT
Inliner
Java Code
obj.x = 5
After Native
Inlining &
Callback
Transformations
foo(){…}
(Native code)
1. Inliner detects a
native callsite
2. Extracts and converts
Native IL to JIT IL
3. Identifies inlined JNI
calls
4. Transforms inlined
JNI calls
5. Finishes inlining
The Big Picture
TR JIT
Inliner
After Native
Inlining &
Callback
Transformations
1. Inliner detects a
native callsite
2. Extracts and converts
Native IL to JIT IL
Java Code
3. Identifies inlined JNI
calls
obj.x = 5
4. Transforms inlined
JNI calls
5. Finishes inlining
Outline
•
•
•
•
Background Information 
Method 
Results
Future Work
Experimental Setup
• Native function microbenchmarks
• Average of 300 million runs
• 1.4 GHz Power4 setup
• Prototype implementation
Cost of IL Conversion
Time per Opcode (microsecs.)
• 5.3 microseconds per W-Code
7
6
5
4
3
2
1
0
ip
z
b
2
fty
a
cr
p
ga
c
gc
p
i
gz
k
cf ser
m
m ar
b
rl
p
e
p
f
ol
tw
SPEC CINT2000 Benchmarks
x
rte
o
v
r
vp
Inlining Null Callouts
• Null native method microbenchmarks
• Varying numbers of args (0, 1, 3, 5)
• Complete removal of call/return overhead
• Gain back 2 to 3x slowdown
• confirmed our expectations
Inlining Non-Null Callouts
Speedup (X)
Microbenchmark Test
hash
Instance
5.5
Static
1.8
•smaller speedups for natives performing work
•instance vs. static speedup
Inlining & Transforming Callbacks
Speedup (X)
Microbenchmark Test
CallVoidMethod
Instance
12.9
•Reclaim order of magnitude overhead
Static
11.8
Speedup (X)
Data-Copy Speedups
Array Length
• Transformed GetIntArrayRegion
Exposing Inlined Code To JIT Optimizations
Microbenchmark Test
GetArrayLength
FindClass
GetMethodID
NewCharArray
GetArrayLength
Speedup (X)
93.4
Conclusion
• Runtime native function inlining into Java code
• Optimizing transformations on inlined Java Native
Interface (JNI) calls
• JIT optimize inlined native code
• Opaque and binary-compatible while boosting
performance
• Future Work
• Engineering issues
• Heuristics
• Larger interoperability framework
Fin