SlideShare a Scribd company logo
Java Performance
Chapter 3
Saurav Basu
6/3/2020
1
Organization
1. Strategies, Approaches, and Methodologies
2. Operating System Performance Monitoring
3. JVM Overview
4. JVM Performance Monitoring
5. Java Application Profiling
6. Java Application Profiling - Tips & Tricks
7. Tuning the JVM - Step by Step
8. Benchmarking Java Applications
9. Benchmarking Multitiered Applications
10. Web Application Performance
11. Web Services Performance
12. Java Persistence & Enterprise Java Beans Performance
2
JVM Overview
3
● HotSpot VM - Architecture
● VM - Runtime
● JIT Compiler
● Memory Manager
JVM Components
JVM Overview
4
● HotSpot VM - Architecture
● VM - Runtime
● JIT Compiler
● Memory Manager
JVM Components
JVM Overview
5
HotSpot VM - Architecture
Java Native
Interface
Java
Method
Libraries
Class
Loader
Loading
Linking
Initialization
Optimizer
Interpreter
Code Generator
Target Code
Generator
(Metaspace)
Method
area
Registers
Heap
area
Stack Area
Native
Stack
Execution Engine
Run Time Data Area
JVM Overview
6
HotSpot VM Runtime - Architecture
VM Runtime
(Metaspace)
JVM Overview
7
HotSpot VM Architecture - Symbol Table
JVM Overview
8
HotSpot VM Architecture - Symbol Table
Symbol
Keys
JVM Overview
9
HotSpot VM Runtime - Symbol Table
Symbol
Dictionary
JVM Overview
10
HotSpot VM - Architecture
Parameter 32 bit 64 bit
#of JVMs More m/cs Less m/cs
Administration Harder Easier
DataSet Size Smaller Larger
Performance High High with Compressed OOPs
Memory Footprint Low High
32 bit vs 64 bit - JVM
- Choose a 64 bit JVM only if latency and GC pauses induced due to higher
memory footprint do not impact the application SLA
JVM Overview
11
HotSpot VM - Architecture
Compressed Object Pointers 64bit JVM
JVM Overview
12
● HotSpot VM - Architecture
● VM - Runtime
● Memory Manager
● JIT Compiler
JVM Components
JVM Overview
13
VM - Runtime
Responsibilities
1. Command Line Parsing
2. VM - LifeCycle
3. Class Loading
4. ByteCode Interpreter
5. Exception Handling
6. Synchronization
7. Thread Management
8. Java Native Interface
JVM Overview
14
VM - Runtime
Responsibilities
1. Command Line Parsing
2. VM - LifeCycle
3. Class Loading
4. ByteCode Interpreter
5. Exception Handling
6. Synchronization
7. Thread Management
8. Java Native Interface
JVM Overview
15
VM - Runtime
Command Line Parsing
Sno. Category Description
1. Standard Supported on all JVM Releases
2. Non Standard (-X prefix) Subject to change without
notice
3. Developer (-XX prefix) Require privileged access to
system configuration parameters
Subject to change without notice
JVM Overview
16
VM - Runtime
Command Line Parsing
Sno. Category Example
1. Boolean -XX:+AggressiveOpts
-XX:-AggressiveOpts
(Aggresive Optimization)
2. Non Boolean -XX:OptionName=<N>
JVM Overview
17
VM - Runtime
Responsibilities
1. Command Line Parsing
2. VM - LifeCycle
3. Class Loading
4. ByteCode Interpreter
5. Exception Handling
6. Synchronization
7. Thread Management
8. Java Native Interface
JVM Overview
18
VM - Runtime
Loading
Linking
Initialization
Verification
Preparation
Resolution
Load
Instantiation
Garbage Collection
Finalization
Execute Unloading
Unload
VM-LifeCycle
JVM Overview
19
VM - Runtime
VM-LifeCycle
JVM Overview
20
VM - Life Cycle (Load)
SNo. Launcher Command OS
1. java Unix/Linux
2. java / javaw Windows
3. javaws Web browser for applets
JVM Overview
21
VM - Life Cycle (Launcher)
Parse command line options
Establish java heap size
& JIT compiler type
Establish LD_LIBRARY_PATH
and CLASSPATH
Fetches Main-Class
Name from JAR manifest
Invoke Main using JNI method
CallStaticVoidMethod passing
marshaled arguments from the
command line.
Load Main class and get main
method’s attributes from the
Java Main-Class
Create hotspot VM in a new
non primordial thread using
JNI_CreateJavaVM
JVM Overview
22
VM - Life Cycle (JNI_CreateJVM) - (1)
Ensure no 2 threads call this
method at the same time
Check
a) JNI version is
supported
b) output stream is
initialized for
garbage collection
logging
Initialize Os Modules
a) Random Number
Generator
b) Process Id
c) Hi resolutionTimer
d) Memory Pages
e) Guard Pages
Guard pages are no
access memory pages
used to bound
memory region
access
(Ex. os puts guard
pages at top of each
thread stack to ensure
reference to end of
stack are trapped
Only one HotSpot VM can be
created in a process space
once a certain point in
initialization is reached. To
the engineers who develop
the HotSpot VM this stage
of launching a HotSpot VM is
referred to as the “point of no
return.”
Parse Command Line
Arguments
JVM Overview
23
VM - Life Cycle (JNI_CreateJVM) - (2)
Libraries such as libzip,
libhpi, libjava, and libthread
are loaded
System properties are
initialized, such as
java.version, java.vendor,
os.name
Modules for
synchronization, stack,
memory, & safepoint pages
are initialized
Signal handlers are initialized
and set
Thread library is initializedThe thread library is
initialized
JVM Overview
24
VM - Life Cycle (JNI_CreateJVM) - (3)
Thread library is initialized
The output stream logger
is initialized
Agent libraries (hprof, jdi), if
any are being used, are
initialized and started
The thread states and the
thread local storage,
which holds thread
specific data required for
the operation of threads,
are initialized
A portion of the HotSpot
VM global data is initialized
such as the event log, OS
synchronization primitives,
perfMemory (performance
statistics memory), and
chunkPool(memory
allocator)
JVM Overview
25
VM - Life Cycle (JNI_CreateJVM) - (4)
The Java version of the
main thread is created
and attached to the
current operating system
thread.
Java level synchronization
is initialized and enabled
Bootclassloader, code
cache, interpreter, JIT
compiler, Java Native
Interface, system
dictionary, and
universe are initialized
Java main thread is now added to the known list of
threads. The universe, a set of required global data
structures, is sanity checked. The HotSpot VMThread,
which performs all the HotSpot VM’s critical functions, is
created. At this point the appropriate JVMTI events are
posted to notify the current state of the HotSpot VM
JVM Overview
26
VM - Life Cycle (JNI_CreateJVM) - (5)
The following Java classes java.lang.String, java.lang.System,
java.lang.Thread, java.lang.ThreadGroup, java.lang.reflect.Method,
java.lang.ref.Finalizer, java.lang.Class, and the rest of the Java System
classes are loaded and initialized. At this point, the HotSpot VM is
initialized and operational, but not quite fully functional
The HotSpot VM’s signal handler thread is started, the JIT compiler is
initialized, and the HotSpot’s compile broker thread is started. Other
HotSpot VM helper threads such as watcher threads and stat sampler are
started. At this time the HotSpot VM is fully functional
Finally, the JNIEnv is populated and returned to the caller and the HotSpot
VM is ready to service new JNI requests
JVM Overview
27
VM - Life Cycle (JNI_DestroyJVM) - (1)
Wait until there is only one nondaemon thread executing noting that the
HotSpot VM is still functional
Call the Java method java.lang.Shutdown.shutdown(), which invokes the
Java level shutdown hooks and runs Java object finalizers if finalization-
on-exit is true
Prepare for HotSpot VM exit by running HotSpot VM level shutdown hooks
(those that were registered through JVM_OnExit()), stop the following
HotSpot VM threads: profiler, stat sampler, watcher, and garbage collector
threads. Post status events to JVMTI, disable JVMTI, and stop the Signal
thread
Call the HotSpot method JavaThread::exit() to release Java Native
Interface handle blocks, remove guard pages, and remove the current
thread from known threads list. From this point on the HotSpot VM cannot
execute any Java code
JVM Overview
28
VM - Life Cycle (JNI_DestroyJVM) - (2)
Stop the HotSpot VM thread. This causes the HotSpot VM to bring the
remaining HotSpot VM threads to a safepoint and stop the JIT compiler
threads
Disable tracing at the Java Native Interface, HotSpot VM, and JVMTI
barriers
Set HotSpot “vm exited” flag for threads that may be running in native code
Delete the current thread
Delete or remove any input/output streams and release PerfMemory
(performance statistics memory) resources.
Finally return to the caller
JVM Overview
29
VM - Runtime
Responsibilities
1. Command Line Parsing
2. VM - LifeCycle
3. Class Loading
4. ByteCode Interpreter
5. Exception Handling
6. Synchronization
7. Thread Management
8. Java Native Interface
JVM Overview
30
VM - Class Loading
Definition
The overall process of mapping a class or
interface name to a class object.
JVM Overview
31
VM - Class Loading
LOADING
INITIALIZATION
LINKING
VERIFYING
PREPARING
RESOLVING
Bringing Binary data
from class into JVM
Incorporating Binary
data into JVM
runtime state
Class variables given
proper initial values
Ensure class is properly formed & fit
for use by JVM
Allocation of memory needed by
class
Transforming symbolic references
into direct references
JVM Overview
32
VM - Class Loading
JVM Overview
33
VM - Class Loading
Class Metadata in HotSpot
JVM Overview
34
VM - Class Loading
Class Metadata in HotSpot
JVM Overview
35
VM - Class Loading
Class Metadata in HotSpot
JVM Overview
36
VM - Class Loading
Class Metadata in HotSpot (64 bit)
JVM Overview
37
VM - Class Loading
Class Metadata in HotSpot
JVM Overview
38
VM - Class Loading
Class Metadata in HotSpot
SNo. Compressed Class Space Metaspace
1
Class Meta Data (C++ Klass Object)
Method Code
2 Runtime Constant Pool (Symbolic Ref.)
3 Code Cache (compiled bytecodes)
JVM Overview
39
VM - Class Loading
Internal Class Loading Data
Sno. DictionaryName Functionality
1. SystemDictionary class name/initiating loader
pairs
2. PlaceHolderDictionary contains classes that
are currently being loaded.
It is used for
ClassCircularityError
checking and
for parallel class loading
3. LoaderConstraintTable tracks constraints for type
safety checking.
JVM Overview
40
VM - Class Loading
ByteCodeVerification
Sno. Check
1. Type correctness: the arguments of an instruction are always of the
types expected by the instruction.
2. No stack overflow or underflow: an instruction never pops an argument
off an empty stack,nor pushes a result on a full stack.
3. Rules for accessing private data and methods are not violated
4. Local variable accesses fall within the runtime stack
5. The runtime stack does not overflow
JVM Overview
41
VM - Class Loading
ByteCodeVerification
JVM Overview
42
VM - Class Loading
ByteCodeVerification
JVM Overview
43
VM - Class Loading
ByteCodeVerification
Transfer Functions for
Abstract Interpretation
of Java Byte Code
JVM Overview
44
VM - Class Loading
ClassData Sharing
JVM Overview
45
VM - Class Loading
Class Data Sharing
JVM-1 JVM-2 JVM-3
Shared Class
Data
(classes.jsa)
-improved startup time of new jvm
-reduced memory footprint
JVM Overview
46
VM - Class Loading
Class Data Sharing
java -Xshare:dump
JVM Overview
47
VM - Runtime
Responsibilities
1. Command Line Parsing
2. VM - LifeCycle
3. Class Loading
4. ByteCode Interpreter
5. Exception Handling
6. Synchronization
7. Thread Management
8. Java Native Interface
JVM Overview
48
VM - Class Loading
ByteCode Interpreter
BCP
JVM Overview
49
VM - Class Loading
ByteCode Interpreter
JVM Overview
50
VM - Runtime
Responsibilities
1. Command Line Parsing
2. VM - LifeCycle
3. Class Loading
4. ByteCode Interpreter
5. Exception Handling
6. Synchronization
7. Thread Management
8. Java Native Interface
JVM Overview
51
VM - Exception Handling
Exception Handler Search Inputs:-
- current method
- current bytecode
- exception object
JVM Overview
52
VM - Runtime
Responsibilities
1. Command Line Parsing
2. VM - LifeCycle
3. Class Loading
4. ByteCode Interpreter
5. Exception Handling
6. Synchronization
7. Thread Management
8. Java Native Interface
JVM Overview
53
VM - Synchronization
Object Monitor Mapping Table
Lookup Cost
Look up Table Contention
Cons
Easy to implement
Pros
JVM Overview
54
VM - Synchronization
Direct Mapping Method
Overhead of Heavy weight
OS Monitors
Cons
Improved Scalability: No
Mapping Table needed
Pros
JVM Overview
55
VM - Synchronization
All Java Objects
Java Objects which are used for
locks
Java Objects whose locks
are moslty used by a
single thread
Observation
JVM Overview
56
VM - Synchronization - Biased Locks
-Kawachiya Analaysis & Acceleration
JVM Overview
57
VM - Synchronization
Thin Lock (Biased Locks)
Implementation Complexity
Cons
Optimized for Thread
Locality Access Pattern
Pros
Biased Object
JVM Overview
58
VM - Synchronization - Biased Locks
● Optimize Clock Cycles used for Lock Operations
● Observation: Most objects held by a single JVM thread
● Strategy: Create “Fast Path” for Common Use Case
JVM Overview
59
VM - Synchronization - Biased Locks
11
Unused(25) Hashcode(31) Unused(1) Age(4) 0 01 Ptr to metadata0(62) 1 01
TId(54) Epoch(2) Unused(1) Age(4) 1 01 Ptr_to_lock_record(62) 00
Ptr_to_heavyweight_monitor(62) 10
Normal, UnlockedBiasable, Unlocked
(Light weight Locked)
(Heavy Weight Locked)
(Marked for GC)
Inital lock
(CAS)
Rebias
(bulk)
lock/unlock
Revoke bias
Thin Lock
Inflate Lock
Recursive lock
unlock
Allocate Object
Biasing
enabled
Biasing
disabled
unlocked
locked
Fast Path
JVM Overview
60
VM - Runtime
Responsibilities
1. Command Line Parsing
2. VM - LifeCycle
3. Class Loading
4. ByteCode Interpreter
5. Exception Handling
6. Synchronization
7. Thread Management
8. Java Native Interface
JVM Overview
61
VM - Runtime
Thread Management - Lifecycle
JVM Overview
62
VM - Runtime
Thread Management - Internal VM Thread
JVM
Periodic Task
Garbage collection
JIT compiler
Signal dispatcher
JVM Overview
63
VM - Runtime
Thread Management - Safe Points
Mechanism to pause thread execution to perform JVM housekeeping.
JVM Overview
64
VM - Runtime
Thread Management - Safe Points
Why?
● Garbage collection
● Code deoptimization
● Flushing code cache
● Class redefinition (e.g. hot swap or instrumentation)
● Biased lock revocation
● Various debug operation (e.g. deadlock check or stacktrace
dump)
JVM Overview
65
VM - Runtime
Thread Management - Safe Points
How?
JIT inserts safepoint checks in code at certain points :-
● Between every 2 bytecodes (interpreter mode)
● Back edge of non counted loops
● Method Exit
● JNI call exit
Safepoints are initiated using a cooperative, polling-based
mechanism. In simplistic terms, every so often a thread asks “should I
block for a safepoint?”
JVM Overview
66
VM - Runtime
Thread Management - Safe Points
JVM Overview
67
VM - Runtime
Thread Management - Safe Points
JVM Overview
68
VM - Runtime
Thread Management - Safe Points
JVM Overview
69
VM - Runtime
Thread Management - Safe Points
JVM Overview
70
VM - Runtime
Responsibilities
1. Command Line Parsing
2. VM - LifeCycle
3. Class Loading
4. ByteCode Interpreter
5. Exception Handling
6. Synchronization
7. Thread Management
8. Java Native Interface
JVM Overview
71
VM - Runtime
JNI
JVM Overview
72
VM - Runtime
JNI - Data Transfer
Performance Impact
JVM Overview
73
VM - Runtime
JNI - (Performance Impact)
Cons
● Loss of Cross Platform Portability
● Loss of Inbuilt Type Safety Security
● Additional Security Check JVM to JNI Data Copy Impacts
Application Performance
JVM Overview
74
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Header
Thread
java -XX:ErrorFile=/var/log/java/java_error%p.log
Process
System
JVM Overview
75
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Header
JVM Overview
76
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Header
JVM Overview
77
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Thread
JVM Overview
78
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Thread
JVM Overview
79
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Thread
JVM Overview
80
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Thread
JVM Overview
81
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Thread
JVM Overview
82
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Thread
Frame
Type
JVM Overview
83
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Process
Theadlist
JVM Overview
84
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Process
JVM Overview
85
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Process
Mutex(es)
JVM Overview
86
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Process
Heap Summary
JVM Overview
87
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Process
Memory Map
JVM Overview
88
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Process
JVM Overview
89
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Process
JVM Overview
90
VM - Runtime
Fatal Error Investigation (hs_error_pid)
Process
JVM Overview
91
VM - Runtime
Fatal Error Investigation (hs_error_pid)
System
JVM Overview
92
● HotSpot VM - Architecture
● VM - Runtime
● Memory Manager
● JIT Compiler
JVM Components
JVM Overview
93
JVM - Memory Manager
JVM Overview
94
JVM - Memory Manager
JVM Overview
95
JVM - Memory Manager
JVM Overview
96
JVM - Memory Manager
JVM Overview
97
JVM - Memory Manager (Copying GC)
JVM Overview
98
JVM - Memory Manager (Copying GC)
Cons
Premature Promotions
Promotion Failure
JVM Overview
99
JVM - Memory Manager (Copying GC)
XX:+PrintGCDetails -XX:+PrintGCTimeStamps
JVM Overview
100
JVM - Memory Manager (Copying GC)
XX:+PrintGCDetails -XX:+PrintGCTimeStamps
Avg. ~92 MB/sec
JVM Overview
101
JVM - Memory Manager (Copying GC)
XX:+PrintGCDetails -XX:+PrintGCTimeStamps
Avg. ~140 .95MB/sec
JVM Overview
102
JVM - Memory Manager (Copying GC)
XX:+PrintGCDetails -XX:+PrintGCTimeStamps
Promotion Rate :(Old After - Old Before)/(timeinms)
Allocation Rate :(Young Before(current GC) - Young After
(prev GC))/(timeinms)
Promotion Rate --> Allocation Rate = Performance Problem
Ex: 92MB/s ---> 140 MB/s = Performance Issue
JVM Overview
103
JVM - Memory Manager (Fast Allocator)
Eden
JVM Overview
104
JVM - Memory Manager- Types
Serial GC
Parallel GC
Mostly Concurrent GC
Garbage First GC (G1)
JVM Overview
105
JVM - Memory Manager- Types
Serial GC
Compacting Mark & Sweep Mark & Sweep (no compaction)
JVM Overview
106
JVM - Memory Manager- Types
Serial GC
JVM Overview
107
JVM - Memory Manager- Types
Serial GC
➢ Application heap size < 100 MB
➢ HIgh number of JVMS on same m/c
➢ No hard pause time requirements (ex < 2secs)
java -XX:+UseSerialGC
When?
JVM Overview
108
JVM - Memory Manager- Types
Theads
Parallel GC
JVM Overview
109
JVM - Memory Manager- Types
➢ Batch processing applications (Ex. Scientific Computing)
➢ High throughput requirement
➢ GC Latency can be met by improving GC efficiency on multi core processor
java -XX:+UseParallelGC
When?
Parallel GC
JVM Overview
110
JVM - Memory Manager- Types
Theads
Mostly Concurrent GC (CMS)
JVM Overview
111
JVM - Memory Manager- Types
➢ Rapid Response Time (ex. Web Servers, Data Tracking Servers)
java -XX:+UseConcMarkSweepGC
When?
Mostly Concurrent GC (CMS)
JVM Overview
112
JVM - Memory Manager- Types
Theads
Garbage First GC (G1)
JVM Overview
113
JVM - Memory Manager- Types
Garbage Collector Types : Comparison
JVM Overview
114
● HotSpot VM - Architecture
● VM - Runtime
● Memory Manager
● JIT Compiler
JVM Components
JVM Overview
115
● Code Generation Overview
● Class Hierarchy Analysis
● Compilation Policy
● Deoptimization
● Client JIT Compiler
● Server JIT Compiler
● SSA - Program Dependence Graph
● Future Enhancements
JIT Compiler
JVM Overview
116
● Code Generation Overview
● Class Hierarchy Analysis
● Compilation Policy
● Deoptimization
● Client JIT Compiler
● Server JIT Compiler
● SSA - Program Dependence Graph
● Future Enhancements
JIT Compiler
JVM Overview
117
JIT Compiler
Code Generation Overview
Intermediate
Representation
Source
Representation
Machine
Representation
Front End Back End
Optimizer
JVM Overview
118
JIT Compiler
Code Generation Overview
Intermediate
Representation
Structural Linear Hybrid
1. AST
2. DAG
1. 3 Address Code
2. Stack M/c Code
Control Flow
Graph
JVM Overview
119
JIT Compiler
Code Generation Overview
Structural IR: Abstract Syntax Tree
JVM Overview
120
JIT Compiler
Code Generation Overview
Structural IR: DAG Example
JVM Overview
121
JIT Compiler
Code Generation Overview
Linear IR: 3 Address Code Example
JVM Overview
122
JIT Compiler
Code Generation Overview
Stack Machine Code
JVM Overview
123
JIT Compiler
Code Generation Overview
Control Flow Graph
JVM Overview
124
JIT Compiler
Code Generation Overview
1. Constant Propagation
2. Value Range Propagation
3. Sparse Conditional Constant Propagation
4. Dead Code Elimination
5. Global Value Numbering
6. Partial Redundancy Elimination
7. Strength Reduction
8. Register Allocation
Compiler Optimizations
JVM Overview
125
JIT Compiler
Code Generation Overview
Constant Propagation
JVM Overview
126
JIT Compiler
Code Generation Overview
Constant Propagation
JVM Overview
127
JIT Compiler
Code Generation Overview
Intermediate
Representation
(IR)
Static Single
Assignment
(SSA)
Named Form
JVM Overview
128
JIT Compiler
Code Generation Overview
Static Single Assignment - Example
JVM Overview
129
JIT Compiler
Code Generation Overview
Static Single Assignment - Example
Common
SubExpression
JVM Overview
130
● Code Generation Overview
● Class Hierarchy Analysis
● Compilation Policy
● Deoptimization
● Client JIT Compiler
● Server JIT Compiler
● SSA - Program Dependence Graph
● Future Enhancements
JIT Compiler
JVM Overview
131
JIT Compiler
Class Hierarchy Analysis
Inlining critical to Performance!
Java Compiler constructs the Call graph using Class Hierarchy
Analysis (CHA) to enable method Inlining
JVM Overview
132
JIT Compiler
Class Hierarchy Analysis
JVM Overview
133
JIT Compiler
Class Hierarchy Analysis
Call Graph Algorithms (Further Reading)
Reachability Analysis (RA)
Class Hierarchy Analysis (CHA)
Rapid Type Analysis (RTA)
Class Type Analysis (CTA)
Accuracy,
Cost
JVM Overview
134
JIT Compiler
Class Hierarchy Analysis
Call Graph Algorithms
Reachability Analysis (RA)
JVM Overview
135
JIT Compiler
Class Hierarchy Analysis
Call Graph Algorithms
Class Hierarchy Analysis (CHA)
JVM Overview
136
JIT Compiler
Class Hierarchy Analysis
Call Graph Algorithms
Rapid Type Analysis (RTA)
JVM Overview
137
JIT Compiler
Class Hierarchy Analysis
Call Graph Algorithms
Class Type Analysis (CTA)
JVM Overview
138
● Code Generation Overview
● Class Hierarchy Analysis
● Compilation Policy
● Deoptimization
● Client JIT Compiler
● Server JIT Compiler
● SSA - Program Dependence Graph
● Future Enhancements
JIT Compiler
JVM Overview
139
JIT Compiler
Compilation Policy
JVM Overview
140
JIT Compiler
Compilation Policy
Backedge CounterInvocation Counter
Compilation Tier
JVM Overview
141
JIT Compiler
Compilation Policy
JVM Overview
142
● Code Generation Overview
● Class Hierarchy Analysis
● Compilation Policy
● Deoptimization
● Client JIT Compiler
● Server JIT Compiler
● SSA - Program Dependence Graph
● Future Enhancements
JIT Compiler
JVM Overview
143
JIT Compiler
●Changing an optimized stack frame to an
unoptimized one.
●Throwing away code with invalid optimistic
optimizations, and replacing it by less-optimized,
more robust code.
Deoptimization
JVM Overview
144
JIT Compiler
Deoptimization
JVM Overview
145
● Code Generation Overview
● Class Hierarchy Analysis
● Compilation Policy
● Deoptimization
● Client JIT Compiler
● Server JIT Compiler
● SSA - Program Dependence Graph
● Future Enhancements
JIT Compiler
JVM Overview
146
JIT Compiler
Client JIT Compiler
Does not need a profiler
Fast code generation of acceptable quality
Rock solid and proved optimization
Compilation threshold 1.5k invocation
JVM Overview
147
● Code Generation Overview
● Class Hierarchy Analysis
● Compilation Policy
● Deoptimization
● Client JIT Compiler
● Server JIT Compiler
● SSA - Program Dependence Graph
● Future Enhancements
JIT Compiler
JVM Overview
148
JIT Compiler
Server JIT Compiler
Does need a profiler
Highly optimized code
Many aggressive and speculative optimizations
Compilation threshold 10k invocations
JVM Overview
149
● Code Generation Overview
● Class Hierarchy Analysis
● Compilation Policy
● Deoptimization
● Client JIT Compiler
● Server JIT Compiler
● SSA - Program Dependence Graph
● Future Enhancements
JIT Compiler
JVM Overview
150
JIT Compiler
SSA Program Dependence Graph
1. SSA Graph
➢ Global Value Numbering
➢ Constant Propogation
➢ Register Optimization
1. Uncommon Traps
2. Iteration Splitting
3. Machine Code Generation
JVM Overview
151
JIT Compiler
SSA Program Dependence Graph
SSA Graph
JVM Overview
152
JIT Compiler
SSA Program Dependence Graph
Global Value Numbering
Constant Propagation
Register Optimization
SSA Graph
JVM Overview
153
JIT Compiler
SSA Program Dependence Graph
Global Value Numbering
Categorize expression in program that compute same value
Eliminate redundant computation
SSA Graph
JVM Overview
154
JIT Compiler
SSA Program Dependence Graph
Global Value Numbering
FInput Pool Output Pool
Optimizing function
SSA Graph
JVM Overview
155
JIT Compiler
SSA Program Dependence Graph
Global Value Numbering
Pool A partition of a set of expressions
SSA Graph
JVM Overview
156
JIT Compiler
SSA Program Dependence Graph
Global Value Numbering
PT : {∅}
PU : {a,b,a+b,r}
PU : {a,b | a+b,r}
PT : {∅}
PV : {a,b |a+b,r | x | r+x} PV : {a,b |a+b,r | x | r+x}
PW : {a,b | a+b, r | x | r+x | (a+b)+x} PW : {a,b | a+b, r | x | r+x, (a+b)+x}
Partition Restructure
SSA Graph
JVM Overview
157
JIT Compiler
SSA Program Dependence Graph
Discover values that are constant in all possible executions of a
program and to propagate these constant values as far forward
through the program as possible
SSA Graph
Constant Propagation
JVM Overview
158
JIT Compiler
SSA Program Dependence Graph
Constant Propagation
SSA Graph
JVM Overview
159
JIT Compiler
SSA Program Dependence Graph
Constant Propagation
SSA Graph
JVM Overview
160
JIT Compiler
SSA Program Dependence Graph
Constant Propogation
Pₙⁱ : Propogated Constant Pool at Entry Point to Node n at ith iteration
SSA Graph
JVM Overview
161
JIT Compiler
SSA Program Dependence Graph
Constant Propogation
Pₙⁱ : Propagated Constant Pool at Entry Point to Node n at ith iteration
SSA Graph
JVM Overview
162
JIT Compiler
SSA Program Dependence Graph
Constant Propogation
Pₙ : Propagated Constant Pool at Entry Point to Node N
SSA Graph
JVM Overview
163
JIT Compiler
SSA Program Dependence Graph
Constant Propagation
Pₙⁱ : Propagated Constant Pool at Entry Point to Node n at ith iteration
SSA Graph
JVM Overview
164
JIT Compiler
SSA Program Dependence Graph
Register Optimization
SSA Graph
JVM Overview
165
JIT Compiler
SSA Program Dependence Graph
Register Optimization
SSA Graph
JVM Overview
166
JIT Compiler
SSA Program Dependence Graph
Register Optimization
redundant
No change in Pool
JVM Overview
167
JIT Compiler
SSA Program Dependence Graph
Uncommon Traps
Uncommon trap is a situation when a speculative precondition in the compiled
code fails. It usually results in deoptimization - switching from compiled code
back to interpreter
JVM Overview
168
JIT Compiler
SSA Program Dependence Graph
Uncommon Traps - Why?
1. Unloaded or Uninitialized Class
1. Unreached Path
JVM Overview
169
JIT Compiler
SSA Program Dependence Graph
Uncommon Traps - Why?
TraceDeoptimization
JVM Overview
170
JIT Compiler
SSA Program Dependence Graph
Iteration Splitting

More Related Content

PPTX
It pro dev_birbilis_20101127_en
PDF
PDF
10 examples of hot spot jvm options in java
DOCX
Introduction to java programming tutorial
PDF
What Your Jvm Has Been Trying To Tell You
PDF
De Java 8 a Java 17
PDF
EAP6 performance Tuning
PDF
OSGi-enabled Java EE Applications using GlassFish at JCertif 2011
It pro dev_birbilis_20101127_en
10 examples of hot spot jvm options in java
Introduction to java programming tutorial
What Your Jvm Has Been Trying To Tell You
De Java 8 a Java 17
EAP6 performance Tuning
OSGi-enabled Java EE Applications using GlassFish at JCertif 2011

What's hot (19)

PPTX
Java byte code & virtual machine
PDF
Glass Fishv3 March2010
PPTX
Java bytecode and classes
PPTX
Android Training Chandigarh
PPT
JVM- Java Virtual Machine
PPTX
JAVA BYTE CODE
PPT
Java Virtual Machine
PDF
Lec 3 01_aug13
PDF
JavaOne 2012 CON 3961 Innovative Testing Techniques Using Bytecode Instrument...
PDF
JavaOne 2012 CON3978 Scripting Languages on the JVM
PPTX
Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose
PDF
What is-java
PDF
Sun Java EE 6 Overview
PDF
Hints and Tips for Modularizing Existing Enterprise Applications (OSGi Commun...
PPT
Java
PPTX
Java 9 Modularity and Project Jigsaw
PDF
Java EE 6 Component Model Explained
PDF
Java modularity: life after Java 9
PPTX
J2EE Struts with Hibernate Framework
Java byte code & virtual machine
Glass Fishv3 March2010
Java bytecode and classes
Android Training Chandigarh
JVM- Java Virtual Machine
JAVA BYTE CODE
Java Virtual Machine
Lec 3 01_aug13
JavaOne 2012 CON 3961 Innovative Testing Techniques Using Bytecode Instrument...
JavaOne 2012 CON3978 Scripting Languages on the JVM
Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose
What is-java
Sun Java EE 6 Overview
Hints and Tips for Modularizing Existing Enterprise Applications (OSGi Commun...
Java
Java 9 Modularity and Project Jigsaw
Java EE 6 Component Model Explained
Java modularity: life after Java 9
J2EE Struts with Hibernate Framework
Ad

Similar to JavaPerformanceChapter_3 (20)

PPTX
Dissecting the Hotspot JVM
PDF
Java Virtual Machine - Internal Architecture
PDF
VMworld 2013: Virtualizing and Tuning Large Scale Java Platforms
PPTX
Lecture 2 Java Virtual Machine .pptx
PPT
Classloader leak detection in websphere application server
ODP
SHARE 2014, Pittsburgh CICS and Liberty applications
ODP
SHARE 2014, Pittsburgh CICS and Liberty applications
PDF
A Brief study on JVM A Brief study on JVM
PDF
An Introduction to Java Compiler and Runtime
PPT
Jvm Performance Tunning
PPT
Jvm Performance Tunning
PDF
Isomorphic JavaScript with Nashorn
PDF
Java Programming - 01 intro to java
PPT
JVM, byte codes & jvm languages
PPTX
Java Virtual Machine (JVM) and just in time compilation
PDF
Jvm internal detail
PPTX
Introduction of jvm|Java Training In Jaipur | Java Training Jaipur | Java Tra...
PPTX
What is Java? Presentation On Introduction To Core Java By PSK Technologies
Dissecting the Hotspot JVM
Java Virtual Machine - Internal Architecture
VMworld 2013: Virtualizing and Tuning Large Scale Java Platforms
Lecture 2 Java Virtual Machine .pptx
Classloader leak detection in websphere application server
SHARE 2014, Pittsburgh CICS and Liberty applications
SHARE 2014, Pittsburgh CICS and Liberty applications
A Brief study on JVM A Brief study on JVM
An Introduction to Java Compiler and Runtime
Jvm Performance Tunning
Jvm Performance Tunning
Isomorphic JavaScript with Nashorn
Java Programming - 01 intro to java
JVM, byte codes & jvm languages
Java Virtual Machine (JVM) and just in time compilation
Jvm internal detail
Introduction of jvm|Java Training In Jaipur | Java Training Jaipur | Java Tra...
What is Java? Presentation On Introduction To Core Java By PSK Technologies
Ad

More from Saurav Basu (12)

PPTX
JavaPerformanceChapter_12
PPTX
JavaPerformanceChapter_9
PPTX
JavaPerformanceChapter_11
PPTX
JavaPerformanceChapter_10
PPTX
JavaPerformanceChapter_8
PPTX
Java PerformanceChapter_7
PPTX
JavaPerformanceChapter_6
PPTX
JavaPerformanceChapter_5
PPTX
JavaPerformanceChapter_4
PPTX
JavaPerformanceChapter_2
PPTX
JavaPerformanceChapter_1
PPTX
Application Deployment Architecture
JavaPerformanceChapter_12
JavaPerformanceChapter_9
JavaPerformanceChapter_11
JavaPerformanceChapter_10
JavaPerformanceChapter_8
Java PerformanceChapter_7
JavaPerformanceChapter_6
JavaPerformanceChapter_5
JavaPerformanceChapter_4
JavaPerformanceChapter_2
JavaPerformanceChapter_1
Application Deployment Architecture

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
A Presentation on Artificial Intelligence
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
KodekX | Application Modernization Development
PDF
Modernizing your data center with Dell and AMD
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Understanding_Digital_Forensics_Presentation.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectral efficient network and resource selection model in 5G networks
Encapsulation_ Review paper, used for researhc scholars
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
MYSQL Presentation for SQL database connectivity
Digital-Transformation-Roadmap-for-Companies.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
A Presentation on Artificial Intelligence
Review of recent advances in non-invasive hemoglobin estimation
KodekX | Application Modernization Development
Modernizing your data center with Dell and AMD
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
The AUB Centre for AI in Media Proposal.docx
Chapter 3 Spatial Domain Image Processing.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Unlocking AI with Model Context Protocol (MCP)
Understanding_Digital_Forensics_Presentation.pptx

JavaPerformanceChapter_3

  • 2. Organization 1. Strategies, Approaches, and Methodologies 2. Operating System Performance Monitoring 3. JVM Overview 4. JVM Performance Monitoring 5. Java Application Profiling 6. Java Application Profiling - Tips & Tricks 7. Tuning the JVM - Step by Step 8. Benchmarking Java Applications 9. Benchmarking Multitiered Applications 10. Web Application Performance 11. Web Services Performance 12. Java Persistence & Enterprise Java Beans Performance 2
  • 3. JVM Overview 3 ● HotSpot VM - Architecture ● VM - Runtime ● JIT Compiler ● Memory Manager JVM Components
  • 4. JVM Overview 4 ● HotSpot VM - Architecture ● VM - Runtime ● JIT Compiler ● Memory Manager JVM Components
  • 5. JVM Overview 5 HotSpot VM - Architecture Java Native Interface Java Method Libraries Class Loader Loading Linking Initialization Optimizer Interpreter Code Generator Target Code Generator (Metaspace) Method area Registers Heap area Stack Area Native Stack Execution Engine Run Time Data Area
  • 6. JVM Overview 6 HotSpot VM Runtime - Architecture VM Runtime (Metaspace)
  • 7. JVM Overview 7 HotSpot VM Architecture - Symbol Table
  • 8. JVM Overview 8 HotSpot VM Architecture - Symbol Table Symbol Keys
  • 9. JVM Overview 9 HotSpot VM Runtime - Symbol Table Symbol Dictionary
  • 10. JVM Overview 10 HotSpot VM - Architecture Parameter 32 bit 64 bit #of JVMs More m/cs Less m/cs Administration Harder Easier DataSet Size Smaller Larger Performance High High with Compressed OOPs Memory Footprint Low High 32 bit vs 64 bit - JVM - Choose a 64 bit JVM only if latency and GC pauses induced due to higher memory footprint do not impact the application SLA
  • 11. JVM Overview 11 HotSpot VM - Architecture Compressed Object Pointers 64bit JVM
  • 12. JVM Overview 12 ● HotSpot VM - Architecture ● VM - Runtime ● Memory Manager ● JIT Compiler JVM Components
  • 13. JVM Overview 13 VM - Runtime Responsibilities 1. Command Line Parsing 2. VM - LifeCycle 3. Class Loading 4. ByteCode Interpreter 5. Exception Handling 6. Synchronization 7. Thread Management 8. Java Native Interface
  • 14. JVM Overview 14 VM - Runtime Responsibilities 1. Command Line Parsing 2. VM - LifeCycle 3. Class Loading 4. ByteCode Interpreter 5. Exception Handling 6. Synchronization 7. Thread Management 8. Java Native Interface
  • 15. JVM Overview 15 VM - Runtime Command Line Parsing Sno. Category Description 1. Standard Supported on all JVM Releases 2. Non Standard (-X prefix) Subject to change without notice 3. Developer (-XX prefix) Require privileged access to system configuration parameters Subject to change without notice
  • 16. JVM Overview 16 VM - Runtime Command Line Parsing Sno. Category Example 1. Boolean -XX:+AggressiveOpts -XX:-AggressiveOpts (Aggresive Optimization) 2. Non Boolean -XX:OptionName=<N>
  • 17. JVM Overview 17 VM - Runtime Responsibilities 1. Command Line Parsing 2. VM - LifeCycle 3. Class Loading 4. ByteCode Interpreter 5. Exception Handling 6. Synchronization 7. Thread Management 8. Java Native Interface
  • 18. JVM Overview 18 VM - Runtime Loading Linking Initialization Verification Preparation Resolution Load Instantiation Garbage Collection Finalization Execute Unloading Unload VM-LifeCycle
  • 19. JVM Overview 19 VM - Runtime VM-LifeCycle
  • 20. JVM Overview 20 VM - Life Cycle (Load) SNo. Launcher Command OS 1. java Unix/Linux 2. java / javaw Windows 3. javaws Web browser for applets
  • 21. JVM Overview 21 VM - Life Cycle (Launcher) Parse command line options Establish java heap size & JIT compiler type Establish LD_LIBRARY_PATH and CLASSPATH Fetches Main-Class Name from JAR manifest Invoke Main using JNI method CallStaticVoidMethod passing marshaled arguments from the command line. Load Main class and get main method’s attributes from the Java Main-Class Create hotspot VM in a new non primordial thread using JNI_CreateJavaVM
  • 22. JVM Overview 22 VM - Life Cycle (JNI_CreateJVM) - (1) Ensure no 2 threads call this method at the same time Check a) JNI version is supported b) output stream is initialized for garbage collection logging Initialize Os Modules a) Random Number Generator b) Process Id c) Hi resolutionTimer d) Memory Pages e) Guard Pages Guard pages are no access memory pages used to bound memory region access (Ex. os puts guard pages at top of each thread stack to ensure reference to end of stack are trapped Only one HotSpot VM can be created in a process space once a certain point in initialization is reached. To the engineers who develop the HotSpot VM this stage of launching a HotSpot VM is referred to as the “point of no return.” Parse Command Line Arguments
  • 23. JVM Overview 23 VM - Life Cycle (JNI_CreateJVM) - (2) Libraries such as libzip, libhpi, libjava, and libthread are loaded System properties are initialized, such as java.version, java.vendor, os.name Modules for synchronization, stack, memory, & safepoint pages are initialized Signal handlers are initialized and set Thread library is initializedThe thread library is initialized
  • 24. JVM Overview 24 VM - Life Cycle (JNI_CreateJVM) - (3) Thread library is initialized The output stream logger is initialized Agent libraries (hprof, jdi), if any are being used, are initialized and started The thread states and the thread local storage, which holds thread specific data required for the operation of threads, are initialized A portion of the HotSpot VM global data is initialized such as the event log, OS synchronization primitives, perfMemory (performance statistics memory), and chunkPool(memory allocator)
  • 25. JVM Overview 25 VM - Life Cycle (JNI_CreateJVM) - (4) The Java version of the main thread is created and attached to the current operating system thread. Java level synchronization is initialized and enabled Bootclassloader, code cache, interpreter, JIT compiler, Java Native Interface, system dictionary, and universe are initialized Java main thread is now added to the known list of threads. The universe, a set of required global data structures, is sanity checked. The HotSpot VMThread, which performs all the HotSpot VM’s critical functions, is created. At this point the appropriate JVMTI events are posted to notify the current state of the HotSpot VM
  • 26. JVM Overview 26 VM - Life Cycle (JNI_CreateJVM) - (5) The following Java classes java.lang.String, java.lang.System, java.lang.Thread, java.lang.ThreadGroup, java.lang.reflect.Method, java.lang.ref.Finalizer, java.lang.Class, and the rest of the Java System classes are loaded and initialized. At this point, the HotSpot VM is initialized and operational, but not quite fully functional The HotSpot VM’s signal handler thread is started, the JIT compiler is initialized, and the HotSpot’s compile broker thread is started. Other HotSpot VM helper threads such as watcher threads and stat sampler are started. At this time the HotSpot VM is fully functional Finally, the JNIEnv is populated and returned to the caller and the HotSpot VM is ready to service new JNI requests
  • 27. JVM Overview 27 VM - Life Cycle (JNI_DestroyJVM) - (1) Wait until there is only one nondaemon thread executing noting that the HotSpot VM is still functional Call the Java method java.lang.Shutdown.shutdown(), which invokes the Java level shutdown hooks and runs Java object finalizers if finalization- on-exit is true Prepare for HotSpot VM exit by running HotSpot VM level shutdown hooks (those that were registered through JVM_OnExit()), stop the following HotSpot VM threads: profiler, stat sampler, watcher, and garbage collector threads. Post status events to JVMTI, disable JVMTI, and stop the Signal thread Call the HotSpot method JavaThread::exit() to release Java Native Interface handle blocks, remove guard pages, and remove the current thread from known threads list. From this point on the HotSpot VM cannot execute any Java code
  • 28. JVM Overview 28 VM - Life Cycle (JNI_DestroyJVM) - (2) Stop the HotSpot VM thread. This causes the HotSpot VM to bring the remaining HotSpot VM threads to a safepoint and stop the JIT compiler threads Disable tracing at the Java Native Interface, HotSpot VM, and JVMTI barriers Set HotSpot “vm exited” flag for threads that may be running in native code Delete the current thread Delete or remove any input/output streams and release PerfMemory (performance statistics memory) resources. Finally return to the caller
  • 29. JVM Overview 29 VM - Runtime Responsibilities 1. Command Line Parsing 2. VM - LifeCycle 3. Class Loading 4. ByteCode Interpreter 5. Exception Handling 6. Synchronization 7. Thread Management 8. Java Native Interface
  • 30. JVM Overview 30 VM - Class Loading Definition The overall process of mapping a class or interface name to a class object.
  • 31. JVM Overview 31 VM - Class Loading LOADING INITIALIZATION LINKING VERIFYING PREPARING RESOLVING Bringing Binary data from class into JVM Incorporating Binary data into JVM runtime state Class variables given proper initial values Ensure class is properly formed & fit for use by JVM Allocation of memory needed by class Transforming symbolic references into direct references
  • 32. JVM Overview 32 VM - Class Loading
  • 33. JVM Overview 33 VM - Class Loading Class Metadata in HotSpot
  • 34. JVM Overview 34 VM - Class Loading Class Metadata in HotSpot
  • 35. JVM Overview 35 VM - Class Loading Class Metadata in HotSpot
  • 36. JVM Overview 36 VM - Class Loading Class Metadata in HotSpot (64 bit)
  • 37. JVM Overview 37 VM - Class Loading Class Metadata in HotSpot
  • 38. JVM Overview 38 VM - Class Loading Class Metadata in HotSpot SNo. Compressed Class Space Metaspace 1 Class Meta Data (C++ Klass Object) Method Code 2 Runtime Constant Pool (Symbolic Ref.) 3 Code Cache (compiled bytecodes)
  • 39. JVM Overview 39 VM - Class Loading Internal Class Loading Data Sno. DictionaryName Functionality 1. SystemDictionary class name/initiating loader pairs 2. PlaceHolderDictionary contains classes that are currently being loaded. It is used for ClassCircularityError checking and for parallel class loading 3. LoaderConstraintTable tracks constraints for type safety checking.
  • 40. JVM Overview 40 VM - Class Loading ByteCodeVerification Sno. Check 1. Type correctness: the arguments of an instruction are always of the types expected by the instruction. 2. No stack overflow or underflow: an instruction never pops an argument off an empty stack,nor pushes a result on a full stack. 3. Rules for accessing private data and methods are not violated 4. Local variable accesses fall within the runtime stack 5. The runtime stack does not overflow
  • 41. JVM Overview 41 VM - Class Loading ByteCodeVerification
  • 42. JVM Overview 42 VM - Class Loading ByteCodeVerification
  • 43. JVM Overview 43 VM - Class Loading ByteCodeVerification Transfer Functions for Abstract Interpretation of Java Byte Code
  • 44. JVM Overview 44 VM - Class Loading ClassData Sharing
  • 45. JVM Overview 45 VM - Class Loading Class Data Sharing JVM-1 JVM-2 JVM-3 Shared Class Data (classes.jsa) -improved startup time of new jvm -reduced memory footprint
  • 46. JVM Overview 46 VM - Class Loading Class Data Sharing java -Xshare:dump
  • 47. JVM Overview 47 VM - Runtime Responsibilities 1. Command Line Parsing 2. VM - LifeCycle 3. Class Loading 4. ByteCode Interpreter 5. Exception Handling 6. Synchronization 7. Thread Management 8. Java Native Interface
  • 48. JVM Overview 48 VM - Class Loading ByteCode Interpreter BCP
  • 49. JVM Overview 49 VM - Class Loading ByteCode Interpreter
  • 50. JVM Overview 50 VM - Runtime Responsibilities 1. Command Line Parsing 2. VM - LifeCycle 3. Class Loading 4. ByteCode Interpreter 5. Exception Handling 6. Synchronization 7. Thread Management 8. Java Native Interface
  • 51. JVM Overview 51 VM - Exception Handling Exception Handler Search Inputs:- - current method - current bytecode - exception object
  • 52. JVM Overview 52 VM - Runtime Responsibilities 1. Command Line Parsing 2. VM - LifeCycle 3. Class Loading 4. ByteCode Interpreter 5. Exception Handling 6. Synchronization 7. Thread Management 8. Java Native Interface
  • 53. JVM Overview 53 VM - Synchronization Object Monitor Mapping Table Lookup Cost Look up Table Contention Cons Easy to implement Pros
  • 54. JVM Overview 54 VM - Synchronization Direct Mapping Method Overhead of Heavy weight OS Monitors Cons Improved Scalability: No Mapping Table needed Pros
  • 55. JVM Overview 55 VM - Synchronization All Java Objects Java Objects which are used for locks Java Objects whose locks are moslty used by a single thread Observation
  • 56. JVM Overview 56 VM - Synchronization - Biased Locks -Kawachiya Analaysis & Acceleration
  • 57. JVM Overview 57 VM - Synchronization Thin Lock (Biased Locks) Implementation Complexity Cons Optimized for Thread Locality Access Pattern Pros Biased Object
  • 58. JVM Overview 58 VM - Synchronization - Biased Locks ● Optimize Clock Cycles used for Lock Operations ● Observation: Most objects held by a single JVM thread ● Strategy: Create “Fast Path” for Common Use Case
  • 59. JVM Overview 59 VM - Synchronization - Biased Locks 11 Unused(25) Hashcode(31) Unused(1) Age(4) 0 01 Ptr to metadata0(62) 1 01 TId(54) Epoch(2) Unused(1) Age(4) 1 01 Ptr_to_lock_record(62) 00 Ptr_to_heavyweight_monitor(62) 10 Normal, UnlockedBiasable, Unlocked (Light weight Locked) (Heavy Weight Locked) (Marked for GC) Inital lock (CAS) Rebias (bulk) lock/unlock Revoke bias Thin Lock Inflate Lock Recursive lock unlock Allocate Object Biasing enabled Biasing disabled unlocked locked Fast Path
  • 60. JVM Overview 60 VM - Runtime Responsibilities 1. Command Line Parsing 2. VM - LifeCycle 3. Class Loading 4. ByteCode Interpreter 5. Exception Handling 6. Synchronization 7. Thread Management 8. Java Native Interface
  • 61. JVM Overview 61 VM - Runtime Thread Management - Lifecycle
  • 62. JVM Overview 62 VM - Runtime Thread Management - Internal VM Thread JVM Periodic Task Garbage collection JIT compiler Signal dispatcher
  • 63. JVM Overview 63 VM - Runtime Thread Management - Safe Points Mechanism to pause thread execution to perform JVM housekeeping.
  • 64. JVM Overview 64 VM - Runtime Thread Management - Safe Points Why? ● Garbage collection ● Code deoptimization ● Flushing code cache ● Class redefinition (e.g. hot swap or instrumentation) ● Biased lock revocation ● Various debug operation (e.g. deadlock check or stacktrace dump)
  • 65. JVM Overview 65 VM - Runtime Thread Management - Safe Points How? JIT inserts safepoint checks in code at certain points :- ● Between every 2 bytecodes (interpreter mode) ● Back edge of non counted loops ● Method Exit ● JNI call exit Safepoints are initiated using a cooperative, polling-based mechanism. In simplistic terms, every so often a thread asks “should I block for a safepoint?”
  • 66. JVM Overview 66 VM - Runtime Thread Management - Safe Points
  • 67. JVM Overview 67 VM - Runtime Thread Management - Safe Points
  • 68. JVM Overview 68 VM - Runtime Thread Management - Safe Points
  • 69. JVM Overview 69 VM - Runtime Thread Management - Safe Points
  • 70. JVM Overview 70 VM - Runtime Responsibilities 1. Command Line Parsing 2. VM - LifeCycle 3. Class Loading 4. ByteCode Interpreter 5. Exception Handling 6. Synchronization 7. Thread Management 8. Java Native Interface
  • 71. JVM Overview 71 VM - Runtime JNI
  • 72. JVM Overview 72 VM - Runtime JNI - Data Transfer Performance Impact
  • 73. JVM Overview 73 VM - Runtime JNI - (Performance Impact) Cons ● Loss of Cross Platform Portability ● Loss of Inbuilt Type Safety Security ● Additional Security Check JVM to JNI Data Copy Impacts Application Performance
  • 74. JVM Overview 74 VM - Runtime Fatal Error Investigation (hs_error_pid) Header Thread java -XX:ErrorFile=/var/log/java/java_error%p.log Process System
  • 75. JVM Overview 75 VM - Runtime Fatal Error Investigation (hs_error_pid) Header
  • 76. JVM Overview 76 VM - Runtime Fatal Error Investigation (hs_error_pid) Header
  • 77. JVM Overview 77 VM - Runtime Fatal Error Investigation (hs_error_pid) Thread
  • 78. JVM Overview 78 VM - Runtime Fatal Error Investigation (hs_error_pid) Thread
  • 79. JVM Overview 79 VM - Runtime Fatal Error Investigation (hs_error_pid) Thread
  • 80. JVM Overview 80 VM - Runtime Fatal Error Investigation (hs_error_pid) Thread
  • 81. JVM Overview 81 VM - Runtime Fatal Error Investigation (hs_error_pid) Thread
  • 82. JVM Overview 82 VM - Runtime Fatal Error Investigation (hs_error_pid) Thread Frame Type
  • 83. JVM Overview 83 VM - Runtime Fatal Error Investigation (hs_error_pid) Process Theadlist
  • 84. JVM Overview 84 VM - Runtime Fatal Error Investigation (hs_error_pid) Process
  • 85. JVM Overview 85 VM - Runtime Fatal Error Investigation (hs_error_pid) Process Mutex(es)
  • 86. JVM Overview 86 VM - Runtime Fatal Error Investigation (hs_error_pid) Process Heap Summary
  • 87. JVM Overview 87 VM - Runtime Fatal Error Investigation (hs_error_pid) Process Memory Map
  • 88. JVM Overview 88 VM - Runtime Fatal Error Investigation (hs_error_pid) Process
  • 89. JVM Overview 89 VM - Runtime Fatal Error Investigation (hs_error_pid) Process
  • 90. JVM Overview 90 VM - Runtime Fatal Error Investigation (hs_error_pid) Process
  • 91. JVM Overview 91 VM - Runtime Fatal Error Investigation (hs_error_pid) System
  • 92. JVM Overview 92 ● HotSpot VM - Architecture ● VM - Runtime ● Memory Manager ● JIT Compiler JVM Components
  • 93. JVM Overview 93 JVM - Memory Manager
  • 94. JVM Overview 94 JVM - Memory Manager
  • 95. JVM Overview 95 JVM - Memory Manager
  • 96. JVM Overview 96 JVM - Memory Manager
  • 97. JVM Overview 97 JVM - Memory Manager (Copying GC)
  • 98. JVM Overview 98 JVM - Memory Manager (Copying GC) Cons Premature Promotions Promotion Failure
  • 99. JVM Overview 99 JVM - Memory Manager (Copying GC) XX:+PrintGCDetails -XX:+PrintGCTimeStamps
  • 100. JVM Overview 100 JVM - Memory Manager (Copying GC) XX:+PrintGCDetails -XX:+PrintGCTimeStamps Avg. ~92 MB/sec
  • 101. JVM Overview 101 JVM - Memory Manager (Copying GC) XX:+PrintGCDetails -XX:+PrintGCTimeStamps Avg. ~140 .95MB/sec
  • 102. JVM Overview 102 JVM - Memory Manager (Copying GC) XX:+PrintGCDetails -XX:+PrintGCTimeStamps Promotion Rate :(Old After - Old Before)/(timeinms) Allocation Rate :(Young Before(current GC) - Young After (prev GC))/(timeinms) Promotion Rate --> Allocation Rate = Performance Problem Ex: 92MB/s ---> 140 MB/s = Performance Issue
  • 103. JVM Overview 103 JVM - Memory Manager (Fast Allocator) Eden
  • 104. JVM Overview 104 JVM - Memory Manager- Types Serial GC Parallel GC Mostly Concurrent GC Garbage First GC (G1)
  • 105. JVM Overview 105 JVM - Memory Manager- Types Serial GC Compacting Mark & Sweep Mark & Sweep (no compaction)
  • 106. JVM Overview 106 JVM - Memory Manager- Types Serial GC
  • 107. JVM Overview 107 JVM - Memory Manager- Types Serial GC ➢ Application heap size < 100 MB ➢ HIgh number of JVMS on same m/c ➢ No hard pause time requirements (ex < 2secs) java -XX:+UseSerialGC When?
  • 108. JVM Overview 108 JVM - Memory Manager- Types Theads Parallel GC
  • 109. JVM Overview 109 JVM - Memory Manager- Types ➢ Batch processing applications (Ex. Scientific Computing) ➢ High throughput requirement ➢ GC Latency can be met by improving GC efficiency on multi core processor java -XX:+UseParallelGC When? Parallel GC
  • 110. JVM Overview 110 JVM - Memory Manager- Types Theads Mostly Concurrent GC (CMS)
  • 111. JVM Overview 111 JVM - Memory Manager- Types ➢ Rapid Response Time (ex. Web Servers, Data Tracking Servers) java -XX:+UseConcMarkSweepGC When? Mostly Concurrent GC (CMS)
  • 112. JVM Overview 112 JVM - Memory Manager- Types Theads Garbage First GC (G1)
  • 113. JVM Overview 113 JVM - Memory Manager- Types Garbage Collector Types : Comparison
  • 114. JVM Overview 114 ● HotSpot VM - Architecture ● VM - Runtime ● Memory Manager ● JIT Compiler JVM Components
  • 115. JVM Overview 115 ● Code Generation Overview ● Class Hierarchy Analysis ● Compilation Policy ● Deoptimization ● Client JIT Compiler ● Server JIT Compiler ● SSA - Program Dependence Graph ● Future Enhancements JIT Compiler
  • 116. JVM Overview 116 ● Code Generation Overview ● Class Hierarchy Analysis ● Compilation Policy ● Deoptimization ● Client JIT Compiler ● Server JIT Compiler ● SSA - Program Dependence Graph ● Future Enhancements JIT Compiler
  • 117. JVM Overview 117 JIT Compiler Code Generation Overview Intermediate Representation Source Representation Machine Representation Front End Back End Optimizer
  • 118. JVM Overview 118 JIT Compiler Code Generation Overview Intermediate Representation Structural Linear Hybrid 1. AST 2. DAG 1. 3 Address Code 2. Stack M/c Code Control Flow Graph
  • 119. JVM Overview 119 JIT Compiler Code Generation Overview Structural IR: Abstract Syntax Tree
  • 120. JVM Overview 120 JIT Compiler Code Generation Overview Structural IR: DAG Example
  • 121. JVM Overview 121 JIT Compiler Code Generation Overview Linear IR: 3 Address Code Example
  • 122. JVM Overview 122 JIT Compiler Code Generation Overview Stack Machine Code
  • 123. JVM Overview 123 JIT Compiler Code Generation Overview Control Flow Graph
  • 124. JVM Overview 124 JIT Compiler Code Generation Overview 1. Constant Propagation 2. Value Range Propagation 3. Sparse Conditional Constant Propagation 4. Dead Code Elimination 5. Global Value Numbering 6. Partial Redundancy Elimination 7. Strength Reduction 8. Register Allocation Compiler Optimizations
  • 125. JVM Overview 125 JIT Compiler Code Generation Overview Constant Propagation
  • 126. JVM Overview 126 JIT Compiler Code Generation Overview Constant Propagation
  • 127. JVM Overview 127 JIT Compiler Code Generation Overview Intermediate Representation (IR) Static Single Assignment (SSA) Named Form
  • 128. JVM Overview 128 JIT Compiler Code Generation Overview Static Single Assignment - Example
  • 129. JVM Overview 129 JIT Compiler Code Generation Overview Static Single Assignment - Example Common SubExpression
  • 130. JVM Overview 130 ● Code Generation Overview ● Class Hierarchy Analysis ● Compilation Policy ● Deoptimization ● Client JIT Compiler ● Server JIT Compiler ● SSA - Program Dependence Graph ● Future Enhancements JIT Compiler
  • 131. JVM Overview 131 JIT Compiler Class Hierarchy Analysis Inlining critical to Performance! Java Compiler constructs the Call graph using Class Hierarchy Analysis (CHA) to enable method Inlining
  • 132. JVM Overview 132 JIT Compiler Class Hierarchy Analysis
  • 133. JVM Overview 133 JIT Compiler Class Hierarchy Analysis Call Graph Algorithms (Further Reading) Reachability Analysis (RA) Class Hierarchy Analysis (CHA) Rapid Type Analysis (RTA) Class Type Analysis (CTA) Accuracy, Cost
  • 134. JVM Overview 134 JIT Compiler Class Hierarchy Analysis Call Graph Algorithms Reachability Analysis (RA)
  • 135. JVM Overview 135 JIT Compiler Class Hierarchy Analysis Call Graph Algorithms Class Hierarchy Analysis (CHA)
  • 136. JVM Overview 136 JIT Compiler Class Hierarchy Analysis Call Graph Algorithms Rapid Type Analysis (RTA)
  • 137. JVM Overview 137 JIT Compiler Class Hierarchy Analysis Call Graph Algorithms Class Type Analysis (CTA)
  • 138. JVM Overview 138 ● Code Generation Overview ● Class Hierarchy Analysis ● Compilation Policy ● Deoptimization ● Client JIT Compiler ● Server JIT Compiler ● SSA - Program Dependence Graph ● Future Enhancements JIT Compiler
  • 140. JVM Overview 140 JIT Compiler Compilation Policy Backedge CounterInvocation Counter Compilation Tier
  • 142. JVM Overview 142 ● Code Generation Overview ● Class Hierarchy Analysis ● Compilation Policy ● Deoptimization ● Client JIT Compiler ● Server JIT Compiler ● SSA - Program Dependence Graph ● Future Enhancements JIT Compiler
  • 143. JVM Overview 143 JIT Compiler ●Changing an optimized stack frame to an unoptimized one. ●Throwing away code with invalid optimistic optimizations, and replacing it by less-optimized, more robust code. Deoptimization
  • 145. JVM Overview 145 ● Code Generation Overview ● Class Hierarchy Analysis ● Compilation Policy ● Deoptimization ● Client JIT Compiler ● Server JIT Compiler ● SSA - Program Dependence Graph ● Future Enhancements JIT Compiler
  • 146. JVM Overview 146 JIT Compiler Client JIT Compiler Does not need a profiler Fast code generation of acceptable quality Rock solid and proved optimization Compilation threshold 1.5k invocation
  • 147. JVM Overview 147 ● Code Generation Overview ● Class Hierarchy Analysis ● Compilation Policy ● Deoptimization ● Client JIT Compiler ● Server JIT Compiler ● SSA - Program Dependence Graph ● Future Enhancements JIT Compiler
  • 148. JVM Overview 148 JIT Compiler Server JIT Compiler Does need a profiler Highly optimized code Many aggressive and speculative optimizations Compilation threshold 10k invocations
  • 149. JVM Overview 149 ● Code Generation Overview ● Class Hierarchy Analysis ● Compilation Policy ● Deoptimization ● Client JIT Compiler ● Server JIT Compiler ● SSA - Program Dependence Graph ● Future Enhancements JIT Compiler
  • 150. JVM Overview 150 JIT Compiler SSA Program Dependence Graph 1. SSA Graph ➢ Global Value Numbering ➢ Constant Propogation ➢ Register Optimization 1. Uncommon Traps 2. Iteration Splitting 3. Machine Code Generation
  • 151. JVM Overview 151 JIT Compiler SSA Program Dependence Graph SSA Graph
  • 152. JVM Overview 152 JIT Compiler SSA Program Dependence Graph Global Value Numbering Constant Propagation Register Optimization SSA Graph
  • 153. JVM Overview 153 JIT Compiler SSA Program Dependence Graph Global Value Numbering Categorize expression in program that compute same value Eliminate redundant computation SSA Graph
  • 154. JVM Overview 154 JIT Compiler SSA Program Dependence Graph Global Value Numbering FInput Pool Output Pool Optimizing function SSA Graph
  • 155. JVM Overview 155 JIT Compiler SSA Program Dependence Graph Global Value Numbering Pool A partition of a set of expressions SSA Graph
  • 156. JVM Overview 156 JIT Compiler SSA Program Dependence Graph Global Value Numbering PT : {∅} PU : {a,b,a+b,r} PU : {a,b | a+b,r} PT : {∅} PV : {a,b |a+b,r | x | r+x} PV : {a,b |a+b,r | x | r+x} PW : {a,b | a+b, r | x | r+x | (a+b)+x} PW : {a,b | a+b, r | x | r+x, (a+b)+x} Partition Restructure SSA Graph
  • 157. JVM Overview 157 JIT Compiler SSA Program Dependence Graph Discover values that are constant in all possible executions of a program and to propagate these constant values as far forward through the program as possible SSA Graph Constant Propagation
  • 158. JVM Overview 158 JIT Compiler SSA Program Dependence Graph Constant Propagation SSA Graph
  • 159. JVM Overview 159 JIT Compiler SSA Program Dependence Graph Constant Propagation SSA Graph
  • 160. JVM Overview 160 JIT Compiler SSA Program Dependence Graph Constant Propogation Pₙⁱ : Propogated Constant Pool at Entry Point to Node n at ith iteration SSA Graph
  • 161. JVM Overview 161 JIT Compiler SSA Program Dependence Graph Constant Propogation Pₙⁱ : Propagated Constant Pool at Entry Point to Node n at ith iteration SSA Graph
  • 162. JVM Overview 162 JIT Compiler SSA Program Dependence Graph Constant Propogation Pₙ : Propagated Constant Pool at Entry Point to Node N SSA Graph
  • 163. JVM Overview 163 JIT Compiler SSA Program Dependence Graph Constant Propagation Pₙⁱ : Propagated Constant Pool at Entry Point to Node n at ith iteration SSA Graph
  • 164. JVM Overview 164 JIT Compiler SSA Program Dependence Graph Register Optimization SSA Graph
  • 165. JVM Overview 165 JIT Compiler SSA Program Dependence Graph Register Optimization SSA Graph
  • 166. JVM Overview 166 JIT Compiler SSA Program Dependence Graph Register Optimization redundant No change in Pool
  • 167. JVM Overview 167 JIT Compiler SSA Program Dependence Graph Uncommon Traps Uncommon trap is a situation when a speculative precondition in the compiled code fails. It usually results in deoptimization - switching from compiled code back to interpreter
  • 168. JVM Overview 168 JIT Compiler SSA Program Dependence Graph Uncommon Traps - Why? 1. Unloaded or Uninitialized Class 1. Unreached Path
  • 169. JVM Overview 169 JIT Compiler SSA Program Dependence Graph Uncommon Traps - Why? TraceDeoptimization
  • 170. JVM Overview 170 JIT Compiler SSA Program Dependence Graph Iteration Splitting