32
Java Runtime: повседневные обязанности JVM Андрей Паньгин ведущий разработчик проекта Одноклассники

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

Embed Size (px)

DESCRIPTION

Что делает JVM? Компилирует код и выполняет сборку мусора, — скажете вы и будете совершенно правы. Тем не менее, Java приложения могут работать даже при полном отсутсвии JIT и GC. Виртуальная машина состоит из большого числа компонентов, благодаря которым исполнение Java программ становится возможным. Из доклада вы узнаете, что представляет собой байткод, где лежат переменные, что содержится в class-файлах, кто ловит исключения, насколько дороги JNI методы, как работает синхронизация и многое другое.

Citation preview

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

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

Андрей Паньгин ведущий разработчик проекта Одноклассники

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

JVM Iceberg

1

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

JVM Iceberg

2

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

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

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

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

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

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

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

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

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

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

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

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

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

Class initialization • When?

• 12-step procedure described in JLS §12.4

• Basically, the invocation of <clinit>

(static initializer)

9

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

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

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

JVM specific bytecodes • fast_igetfield, fast_iputfield

– -XX:+RewriteBytecodes

• fast_iload2, fast_aaccess_0

– -XX:+RewriteFrequentPairs

• fast_binaryswitch

• return_register_finalizer

11

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

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

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

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

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

Stack frame

14

old SP old FP

Method

Monitors

Locals

Expression stack

Return addr

SP

Previous frame

Expression stack

FP

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

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

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

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

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

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

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

Java-level locks • java.util.concurrent.locks.LockSupport

• park() / unpark()

• OS-level primitives

– Mutex + Condition variable

18

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

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(); } }

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

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(); } }

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

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

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

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

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

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

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

JNI functions • Executed in VM context

• Check for safepoint

• jobject == index in thread-local JNIHandles array

• Optional verification

– -XX:+CheckJNICalls

24

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

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 }

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

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 }

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

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

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

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; } }

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

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

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

Still much to learn

30

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

Thank you! • OpenJDK sources

– http://hg.openjdk.java.net

• Contacts

[email protected]

• Open Source @ Odnoklassniki

– https://github.com/odnoklassniki

• Career @ Odnoklassniki

– http://v.ok.ru

31