Upload
odnoklassnikiru
View
2.211
Download
3
Embed Size (px)
DESCRIPTION
Что делает JVM? Компилирует код и выполняет сборку мусора, — скажете вы и будете совершенно правы. Тем не менее, Java приложения могут работать даже при полном отсутсвии JIT и GC. Виртуальная машина состоит из большого числа компонентов, благодаря которым исполнение Java программ становится возможным. Из доклада вы узнаете, что представляет собой байткод, где лежат переменные, что содержится в class-файлах, кто ловит исключения, насколько дороги JNI методы, как работает синхронизация и многое другое.
Citation preview
Java Runtime: повседневные обязанности JVM
Андрей Паньгин ведущий разработчик проекта Одноклассники
JVM Iceberg
1
JVM Iceberg
2
Launcher • java.exe – just a simple C program
1. Locate JRE
2. Select version
3. Parse arguments
4. JNI_CreateJavaVM
5. JNIEnv::FindClass
6. JNIEnv::GetStaticMethodID (main)
7. JNIEnv::CallStaticVoidMethod
3
Other launchers • Differ only by Main Class
– javac com.sun.tools.javac.Main
– javadoc com.sun.tools.javadoc.Main
– javah com.sun.tools.javah.Main
– jconsole sun.tools.jconsole.JConsole
– jmap sun.tools.jmap.JMap
– …
• Even -version calls Java
– sun.misc.Version.print()
4
Class loading • Class loading != Class initialization
• ClassLoader prepares byte[] definition
• Real loading is done inside VM
– ClassLoader.defineClass0
– Parses, verifies and creates internal VM structures
• Unreferenced classes may be unloaded
– -XX:+CMSClassUnloadingEnabled
5
Class Metadata • Constant Pool, Interfaces, Methods, Fields
• Exceptions, Annotations, vtables, itables
• Java 8: PermGen Metaspace
– java.lang.OutOfMemoryError: PermGen space
• Field reordering
– -XX:+CompactFields, -XX:FieldsAllocationStyle=1
6
Class data sharing • Snapshot of commonly used classes
– jre/lib/classlist
• Mapped into memory directly from disk
– classes.jsa
• Shared between multiple JVM instances
• Improves start-up time, reduces footprint
– -XX:+UseSharedSpaces
– -XX:+DumpSharedSpaces
7
Bytecode verification • Java 6 Split Verifier
– Inferencing verifier (javac) + Type checking (run-time)
– StackMapTable attribute in .class
• Skip verification
– -XX:-BytecodeVerificationRemote
– -XX:-BytecodeVerificationLocal
– Inherit sun.reflect.MagicAccessorImpl
8
Class initialization • When?
• 12-step procedure described in JLS §12.4
• Basically, the invocation of <clinit>
(static initializer)
9
Bytecodes • Stack machine
• 203 Java bytecodes
10
Stack manipulation iconst, fconst, bipush, pop, dup, swap
Type conversion i2l, f2i, l2d
Arithmetic / logic iadd, isub, imul, ixor, ishr
Local variables iload, aload, istore, fstore
Arrays iaload, aaload, iastore, aastore, arraylength
Fields getfield, putfield, getstatic, putstatic
Branches ifeq, ifgt, goto, tableswitch, lookupswitch
Method calls invokevirtual, invokeinterface, invokestatic, return
Allocation new, anewarray, multianewarray
Other monitorenter, monitorexit, instanceof, athrow
JVM specific bytecodes • fast_igetfield, fast_iputfield
– -XX:+RewriteBytecodes
• fast_iload2, fast_aaccess_0
– -XX:+RewriteFrequentPairs
• fast_binaryswitch
• return_register_finalizer
11
Bytecode interpreter • C++ and Assembler (template) interpreter
• Generated at VM start-up
• Run-time code profiling
– -XX:CompileThreshold=10000
• Run-time CP resolution and bytecode rewriting
• Optimizations
– Dispatch table, top-of-stack caching
12
Stack • Java (expression) vs. Native (execution)
– -XX:ThreadStackSize=320
• Guard pages
– -XX:StackShadowPages=20
– -XX:StackYellowPages=2
– -XX:StackRedPages=1
13
Frame
Empty
Shadow
Frame
Frame
Stack frame
14
old SP old FP
Method
Monitors
Locals
Expression stack
Return addr
SP
Previous frame
Expression stack
FP
Frame types • Interpreted, Compiled, Native
– Different layout
• Inlining
– 1 frame for multiple nested methods
• Deoptimization
– When optimistic assumptions fail
– (exceptions, class hierarchy changes etc.)
15
Threads • Java threads & VM threads
• States: in_java, in_vm, in_native, blocked
• Thread pointer in register
– Fast Thread.currentThread()
• Priority policy (0 – normal, 1 – aggressive)
– -XX:ThreadPriorityPolicy=N
• TLAB allocation
– -XX:+UseTLAB, -XX:TLABSize=0
16
Synchronization • Simple uncontended lock – CAS
• Biased locking
– -XX:+UseBiasedLocking
– -XX:BiasedLockingStartupDelay=4000
– -XX:BiasedLockingBulkRebiasThreshold=20
– -XX:BiasedLockingBulkRevokeThreshold=40
• Contended lock optimizations
– Spin Yield Park
17
Java-level locks • java.util.concurrent.locks.LockSupport
• park() / unpark()
• OS-level primitives
– Mutex + Condition variable
18
wait / notify • What’s wrong with this code?
19
void setCompleted() { synchronized (lock) { completed = true; lock.notifyAll(); } }
void waitForCompletion() { synchronized (lock) { if (!completed) { lock.wait(); } } }
void setCompleted() { synchronized (lock) { completed = true; lock.notifyAll(); } }
wait / notify • What’s wrong with this code?
• -XX:+FilterSpuriousWakeups
20
void setCompleted() { synchronized (lock) { completed = true; lock.notifyAll(); } }
void waitForCompletion() { synchronized (lock) { if (!completed) { lock.wait(); } } }
void setCompleted() { synchronized (lock) { completed = true; lock.notifyAll(); } }
Object header
21
unused hashCode 0 age 0 01
Unlocked
Displaced header ptr 00
Thin lock
Inflated lock ptr 10
Inflated lock
JavaThread epoch 0 age 1 01
Biased lock Stack
Monitor
Safepoints • When?
– GC phases
– Thread dump
– Deoptimization
– Revoke/rebias BiasedLock
• How?
– Interpreted: switch dispatch table
– Compiled: page polling
– Native: on return and on JNI calls
• -XX:+PrintSafepointStatistics
22
Native methods • System.loadLibrary()
• Lazy linking
• Expensive invocation
1. Create stack frame
2. Set up arguments according to native calling convention
3. Pass JNIEnv* and jclass
4. Lock / unlock if synchronized
5. Trace method entry / method exit
6. Check for safepoint
7. Check exceptions
23
JNI functions • Executed in VM context
• Check for safepoint
• jobject == index in thread-local JNIHandles array
• Optional verification
– -XX:+CheckJNICalls
24
Exceptions • Which code is faster?
25
for (;;) { int index = getNextIndex(); if (index >= arr.length) { break; } sum += array[index]; }
try { for (;;) { int index = getNextIndex(); sum += arr[index]; } } catch (IndexOutOfBoundsException e) { // break }
Exceptions • Which code is faster?
• try-catch is free (exception tables)
• throw is expensive
(find handler, unwind stack, release locks, build stack trace)
26
for (;;) { int index = getNextIndex(); if (index >= arr.length) { break; } sum += array[index]; }
try { for (;;) { int index = getNextIndex(); sum += arr[index]; } } catch (IndexOutOfBoundsException e) { // break }
Reflection • getDeclaredFields(), getDeclaredMethods()
– VM Internal structures Java representation
• Field getters and setters
– sun.misc.Unsafe
• Method.invoke()
1. Native implementation
2. Dynamic bytecode generation (up to 10x faster)
-Dsun.reflect.inflationThreshold=15
27
MethodAccessor example • void Point.move(float x, float y, boolean relative);
28
class PointMove_MethodAccessor implements MethodAccessor { public Object invoke(Object target, Object[] args) { float x = ((Float) args[0]).floatValue(); float y = ((Float) args[1]).floatValue(); boolean relative = ((Boolean) args[2]).booleanValue(); try { ((Point) target).move(x, y, relative); } catch (Throwable t) { throw new InvocationTargetException(t); } return null; } }
Troubleshooting • Signal handler (SIGSEGV, SIGILL, SIGFPE…)
• Not all SEGV are fatal
– Polling page
– Implicit NullPointerException, StackOverflowError
• hs_err.log
– Threads, Stack frames, Memory map
– Registers, Top of stack, Instructions
– -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly
29
Still much to learn
30
Thank you! • OpenJDK sources
– http://hg.openjdk.java.net
• Contacts
• Open Source @ Odnoklassniki
– https://github.com/odnoklassniki
• Career @ Odnoklassniki
– http://v.ok.ru
31