Java Runtime: повседневные обязанности JVM

  • View

  • Download

Embed Size (px)


JVM? , . , Java JIT GC. , Java . , , , class-, , JNI , .


  • 1.Java Runtime: JVM

2. JVM Iceberg1 3. JVM Iceberg2 4. 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 5. Other launchers Differ only by Main Class javac javadoc javah Even -version calls Java sun.misc.Version.print() 4 6. 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:+CMSClassUnloadingEnabled5 7. 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 8. 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 9. 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 10. Class initialization When? 12-step procedure described in JLS 12.4 Basically, the invocation of (static initializer)9 11. Bytecodes Stack machine 203 Java bytecodes Stack manipulation iconst, fconst, bipush, pop, dup, swap Type conversioni2l, f2i, l2d Arithmetic / logic iadd, isub, imul, ixor, ishr Local variablesiload, 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 Othermonitorenter, monitorexit, instanceof, athrow 10 12. JVM specific bytecodes fast_igetfield, fast_iputfield -XX:+RewriteBytecodes fast_iload2, fast_aaccess_0 -XX:+RewriteFrequentPairs fast_binaryswitch return_register_finalizer 11 13. 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 caching12 14. Stack Java (expression) vs. Native (execution) -XX:ThreadStackSize=320 Guard pages Empty -XX:StackShadowPages=20 -XX:StackYellowPages=2 Shadow -XX:StackRedPages=1 Frame Frame Frame13 15. Stack frame SP Expression stackMonitors Methodold SPFPold FP Return addr Locals Expression stackPrevious frame 14 16. 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 17. 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 18. 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 19. Java-level locks java.util.concurrent.locks.LockSupport park() / unpark() OS-level primitives Mutex + Condition variable18 20. wait / notify Whats wrong with this code? void waitForCompletion() { void setCompleted() { synchronized (lock) {synchronized (lock) { if (!completed) {completed = true; lock.wait(); lock.notifyAll(); }} }} }19 21. wait / notify Whats wrong with this code? void waitForCompletion() { void setCompleted() { synchronized (lock) {synchronized (lock) { if (!completed) {completed = true; lock.wait(); lock.notifyAll(); }} }} } -XX:+FilterSpuriousWakeups20 22. Object header Unlocked unusedhashCode 0 age 0 01 Monitor Thin lock Displaced header ptr 00 Inflated lock Inflated lock ptr10 Stack Biased lock JavaThreadepoch 0age 1 01 21 23. 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:+PrintSafepointStatistics22 24. 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 exceptions23 25. JNI functions Executed in VM context Check for safepoint jobject == index in thread-local JNIHandles array Optional verification -XX:+CheckJNICalls 24 26. Exceptions Which code is faster? for (;;) {try { int index = getNextIndex(); for (;;) { if (index >= arr.length) {int index = getNextIndex(); break;sum += arr[index]; } } sum += array[index];} catch (IndexOutOfBoundsException e) { } // break } 25 27. Exceptions Which code is faster? for (;;) { try { int index = getNextIndex();for (;;) { if (index >= arr.length) { int index = getNextIndex(); break; sum += arr[index]; }} sum += array[index]; } catch (IndexOutOfBoundsException e) { }// break} try-catch is free (exception tables) throw is expensive(find handler, unwind stack, release locks, build stack trace)26 28. 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 29. MethodAccessor example void Point.move(float x, float y, boolean relative); 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; } }28 30. 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 31. Still much to learn30 32. Thank you! OpenJDK sources Contacts Open Source @ Odnoklassniki Career @ Odnoklassniki 31