SlideShare una empresa de Scribd logo
1 de 40
Dissecting the Hotspot JVM

Martin Toshev @martin_fmi
Ivan St. Ivanov @ivan_stefanov
Agenda
Virtual Machine Basics
The Hotspot JVM
Understanding the Hotspot Source Code

Debugging Hotspot
DISCLAIMER
Why this presentation ?
Feeling comfortable with the JVM source code allows
you to:
•

contribute to and improve the JVM itself

•

fork and customize the JVM for whatever reason you
want (experimental, commercial,
academic …)
Why this presentation ?
Feeling comfortable with the JVM source code allows
you to:
•

be a step ahead of Chuck Norris
Virtual Machine Basics
A typical virtual machine for an interpreted language
provides:
•

Compilation of source language into VM specific
bytecode

•

Data structures to contains instructions and operands
(the data the instructions process)
Virtual Machine Basics
A typical virtual machine for an interpreted language
provides:
•

A call stack for function call operations

•

An ‘Instruction Pointer’ (IP) pointing to the next
instruction to execute
Virtual Machine Basics
A typical virtual machine for an interpreted language
provides:
•

A virtual ‘CPU’ – the instruction dispatcher that:
o Fetches the next instruction (addressed by the
instruction pointer)
o Decodes the operands
o Executes the instruction
The Hotspot JVM
Provides:
•

bytecode execution - using an interpreter, two runtime
compilers or On-Stack Replacement

•

storage allocation and garbage collection

•

runtimes - start up, shut down, class loading, threads,
interaction with OS and others
The Hotspot JVM
Architecture:
The Hotspot JVM
Architecture:
The Hotspot JVM
The Hotspot JVM
Three phases of class-loading:
– Loading

– Linking
– Initialization
The Hotspot JVM
Architecture:
The Hotspot JVM
The Hotspot JVM
The Hotspot JVM
0 iload_0
1 iload_1
2 imul
3 istore_3
4 iload_3
5 iload_2
6 imul
7 istore 4
9 iload 4
11ireturn
The Hotspot JVM
The Hotspot JVM
The Hotspot JVM
The Hotspot JVM
Heap memory:
new Data()
Data

Class<Data>
Class<Class>
The Hotspot JVM
Mark word contains:
–
–
–
–
–

Identity hash code
age
lock record address
monitor address
state (unlocked, light-weight locked, heavy-weight
locked, marked for GC)
– biased / biasable (includes other fields such as
thread ID)
The Hotspot JVM
Architecture:
The Hotspot JVM
Execution engine:
while(true) {
bytecode b = bytecodeStream[pc++];
switch(b) {
case iconst_1: push(1); break;
case iload_0: push(local(0)); break;
case iadd: push(pop() + pop()); break;
}
}
The Hotspot JVM
Execution engine:
while(true) {
bytecode b = bytecodeStream[pc++];
switch(b) {
case iconst_1: push(1); break;
case iload_0: push(local(0)); break;
case iadd: push(pop() + pop()); break;
}
}
NOT that simple …
The Hotspot JVM
Different execution techniques:
– interpreting
– just-in-time (JIT) compilation
– adaptive optimization (determines "hot spots" by
monitoring execution)
The Hotspot JVM
JIT compilation:
– triggered asynchronously by counter overflow for a
method/loop (interpreted counts method entries and
loopback branches)
– produces generated code and relocation info
(transferred on next method entry)
– in case JIT-compiled code calls not-yet-JIT-compiled
code control is transferred to the interpreter
The Hotspot JVM
JIT compilation:
– compiled code may be forced back into interpreted
bytecode (deoptimization)

– is complemented by On-Stack Replacement (turn
dynamically interpreted to JIT compiled code and viseversa - dynamic optimization/deoptimization)
– is more optimized for server VM (but hits start-up
time compared to client VM)
The Hotspot JVM
JIT compilation flow (performed during normal
bytecode execution):
1) bytecode is turned into a graph

2) the graph is turned into a linear sequence of
operations that manipulate an infinite loop of virtual
registers (each node places its result in a virtual register)
The Hotspot JVM
JIT compilation flow (performed during normal
bytecode execution):
3) physical registers are allocated for virtual registers
(the program stack might be used in case virtual
registers exceed physical registers)
4) code for each operation is generated using its
allocated registers
The Hotspot JVM
Typical execution flow (when using the java/javaw
launcher):
1. Parse the command line options
2. Establish the heap sizes and the compiler type (client or
server)
3. Establish the environment variables such as CLASSPATH
4. If the java Main-Class is not specified on the command
line fetch the Main-Class name from the JAR's manifest
5. Create the VM using JNI_CreateJavaVM in a newly
created thread (non primordial thread)
The Hotspot JVM
Typical execution flow (when using the java/javaw
launcher):
6. Once the VM is created and initialized, load the MainClass
7. Invoke the main method in the VM using
CallStaticVoidMethod
8. Once the main method completes check and clear any
pending exceptions that may have occurred and also pass
back the exit status
9. Detach the main thread using DetachCurrentThread,
by doing so we decrement the thread count so
the DestroyJavaVM can be called safely
Demo
Summary

No JVMs were injured during this presentation …
Q&A

Thank you !
Resources
The Java Virtual Machine Specification Java SE7 Edition
http://docs.oracle.com/javase/specs/jvms/se7/jvms7.pdf
Mani Sarkar 's collection of Hotspot-related links
https://gist.github.com/neomatrix369/5743225
Hotspot documentation
http://openjdk.java.net/groups/hotspot/
Resources
Hacking Hotspot in Eclipse under Ubuntu 12.04
http://neomatrix369.wordpress.com/2013/03/12/hotspot-isin-focus-again-aka-hacking-hotspot-in-eclipse-juno-underubuntu-12-04/
Garner R., The Design and Construction of High Performance
Garbage Collectors, PhD thesis
https://digitalcollections.anu.edu.au/handle/1885/9053
Resources
Shi Y., Virtual Machine Shutdown: Stack versus Registers,
PhD Thesis
https://www.cs.tcd.ie/publications/techreports/reports.07/TCD-CS-2007-49.pdf
Compiler and JVM Research at JKU
http://www.ssw.uni-linz.ac.at/Research/Projects/JVM/
Resources
Xavier Leroy, Java bytecode verification: algorithms and
formalizations
http://pauillac.inria.fr/~xleroy/publi/bytecode-verification-JAR.pdf

FOSDEM 2007
http://openjdk.java.net/groups/hotspot/docs/FOSDEM-2007HotSpot.pdf
Resources
The Architecture of the Java Virtual Machine
http://www.artima.com/insidejvm/ed2/jvm2.html

Más contenido relacionado

La actualidad más candente

The Real Thing: Java Virtual Machine
The Real Thing: Java Virtual MachineThe Real Thing: Java Virtual Machine
The Real Thing: Java Virtual MachineFrontech
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunningguest1f2740
 
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia "What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia Vladimir Ivanov
 
자바 성능 강의
자바 성능 강의자바 성능 강의
자바 성능 강의Terry Cho
 
JavaPerformanceChapter_3
JavaPerformanceChapter_3JavaPerformanceChapter_3
JavaPerformanceChapter_3Saurav Basu
 
Detecting hardware virtualization rootkits
Detecting hardware virtualization rootkitsDetecting hardware virtualization rootkits
Detecting hardware virtualization rootkitsEdgar Barbosa
 
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMUSFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMULinaro
 
Cli jbug
Cli jbugCli jbug
Cli jbugmaeste
 
Using Flame Graphs
Using Flame GraphsUsing Flame Graphs
Using Flame GraphsIsuru Perera
 
Software Profiling: Java Performance, Profiling and Flamegraphs
Software Profiling: Java Performance, Profiling and FlamegraphsSoftware Profiling: Java Performance, Profiling and Flamegraphs
Software Profiling: Java Performance, Profiling and FlamegraphsIsuru Perera
 
UVM Update: Register Package
UVM Update: Register PackageUVM Update: Register Package
UVM Update: Register PackageDVClub
 
JMC/JFR: Kotlin spezial
JMC/JFR: Kotlin spezialJMC/JFR: Kotlin spezial
JMC/JFR: Kotlin spezialMiro Wengner
 
Java Virtual Machine - Internal Architecture
Java Virtual Machine - Internal ArchitectureJava Virtual Machine - Internal Architecture
Java Virtual Machine - Internal Architecturesubnesh
 
Java Performance & Profiling
Java Performance & ProfilingJava Performance & Profiling
Java Performance & ProfilingIsuru Perera
 

La actualidad más candente (20)

Inside the JVM
Inside the JVMInside the JVM
Inside the JVM
 
Inside the jvm
Inside the jvmInside the jvm
Inside the jvm
 
The Real Thing: Java Virtual Machine
The Real Thing: Java Virtual MachineThe Real Thing: Java Virtual Machine
The Real Thing: Java Virtual Machine
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunning
 
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia "What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
 
자바 성능 강의
자바 성능 강의자바 성능 강의
자바 성능 강의
 
JavaPerformanceChapter_3
JavaPerformanceChapter_3JavaPerformanceChapter_3
JavaPerformanceChapter_3
 
Detecting hardware virtualization rootkits
Detecting hardware virtualization rootkitsDetecting hardware virtualization rootkits
Detecting hardware virtualization rootkits
 
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMUSFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
 
Cli jbug
Cli jbugCli jbug
Cli jbug
 
What's Inside a JVM?
What's Inside a JVM?What's Inside a JVM?
What's Inside a JVM?
 
Java performance tuning
Java performance tuningJava performance tuning
Java performance tuning
 
Using Flame Graphs
Using Flame GraphsUsing Flame Graphs
Using Flame Graphs
 
JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović
JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor BuzatovićJavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović
JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović
 
Software Profiling: Java Performance, Profiling and Flamegraphs
Software Profiling: Java Performance, Profiling and FlamegraphsSoftware Profiling: Java Performance, Profiling and Flamegraphs
Software Profiling: Java Performance, Profiling and Flamegraphs
 
UVM Update: Register Package
UVM Update: Register PackageUVM Update: Register Package
UVM Update: Register Package
 
JMC/JFR: Kotlin spezial
JMC/JFR: Kotlin spezialJMC/JFR: Kotlin spezial
JMC/JFR: Kotlin spezial
 
Java Virtual Machine - Internal Architecture
Java Virtual Machine - Internal ArchitectureJava Virtual Machine - Internal Architecture
Java Virtual Machine - Internal Architecture
 
Java Performance & Profiling
Java Performance & ProfilingJava Performance & Profiling
Java Performance & Profiling
 
Java Micro-Benchmarking
Java Micro-BenchmarkingJava Micro-Benchmarking
Java Micro-Benchmarking
 

Similar a Dissecting the Hotspot JVM

Introduction of jvm|Java Training In Jaipur | Java Training Jaipur | Java Tra...
Introduction of jvm|Java Training In Jaipur | Java Training Jaipur | Java Tra...Introduction of jvm|Java Training In Jaipur | Java Training Jaipur | Java Tra...
Introduction of jvm|Java Training In Jaipur | Java Training Jaipur | Java Tra...Rhythm Suiwal
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesCharles Nutter
 
02-Java Technology Details.ppt
02-Java Technology Details.ppt02-Java Technology Details.ppt
02-Java Technology Details.pptJyothiAmpally
 
How to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machineHow to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machineChun-Yu Wang
 
Enabling Java: Windows on Arm64 - A Success Story!
Enabling Java: Windows on Arm64 - A Success Story!Enabling Java: Windows on Arm64 - A Success Story!
Enabling Java: Windows on Arm64 - A Success Story!Monica Beckwith
 
Android Training Chandigarh
Android Training ChandigarhAndroid Training Chandigarh
Android Training ChandigarhSheetal Sharma
 
Introduction to .NET Micro Framework Development
Introduction to .NET Micro Framework DevelopmentIntroduction to .NET Micro Framework Development
Introduction to .NET Micro Framework Developmentchristopherfairbairn
 
The State of Managed Runtimes 2013, by Attila Szegedi
The State of Managed Runtimes 2013, by Attila SzegediThe State of Managed Runtimes 2013, by Attila Szegedi
The State of Managed Runtimes 2013, by Attila SzegediZeroTurnaround
 
Advanced java training in bangalore
Advanced java training in bangaloreAdvanced java training in bangalore
Advanced java training in bangaloresiyaram ray
 
Advanced java-training-in-bangalore
Advanced java-training-in-bangaloreAdvanced java-training-in-bangalore
Advanced java-training-in-bangaloresiyaram ray
 
An Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeAn Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeOmar Bashir
 

Similar a Dissecting the Hotspot JVM (20)

Introduction of jvm|Java Training In Jaipur | Java Training Jaipur | Java Tra...
Introduction of jvm|Java Training In Jaipur | Java Training Jaipur | Java Tra...Introduction of jvm|Java Training In Jaipur | Java Training Jaipur | Java Tra...
Introduction of jvm|Java Training In Jaipur | Java Training Jaipur | Java Tra...
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for Dummies
 
Oop lecture2
Oop lecture2Oop lecture2
Oop lecture2
 
JVM Bytecodes
JVM BytecodesJVM Bytecodes
JVM Bytecodes
 
What is Java? Presentation On Introduction To Core Java By PSK Technologies
What is Java? Presentation On Introduction To Core Java By PSK TechnologiesWhat is Java? Presentation On Introduction To Core Java By PSK Technologies
What is Java? Presentation On Introduction To Core Java By PSK Technologies
 
02-Java Technology Details.ppt
02-Java Technology Details.ppt02-Java Technology Details.ppt
02-Java Technology Details.ppt
 
How to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machineHow to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machine
 
Java introduction
Java introductionJava introduction
Java introduction
 
Enabling Java: Windows on Arm64 - A Success Story!
Enabling Java: Windows on Arm64 - A Success Story!Enabling Java: Windows on Arm64 - A Success Story!
Enabling Java: Windows on Arm64 - A Success Story!
 
Android Training Chandigarh
Android Training ChandigarhAndroid Training Chandigarh
Android Training Chandigarh
 
Introduction to .NET Micro Framework Development
Introduction to .NET Micro Framework DevelopmentIntroduction to .NET Micro Framework Development
Introduction to .NET Micro Framework Development
 
Java unit 1
Java unit 1Java unit 1
Java unit 1
 
The State of Managed Runtimes 2013, by Attila Szegedi
The State of Managed Runtimes 2013, by Attila SzegediThe State of Managed Runtimes 2013, by Attila Szegedi
The State of Managed Runtimes 2013, by Attila Szegedi
 
Java hot spot
Java hot spotJava hot spot
Java hot spot
 
Advanced java training in bangalore
Advanced java training in bangaloreAdvanced java training in bangalore
Advanced java training in bangalore
 
Advanced java-training-in-bangalore
Advanced java-training-in-bangaloreAdvanced java-training-in-bangalore
Advanced java-training-in-bangalore
 
Understanding the Dalvik Virtual Machine
Understanding the Dalvik Virtual MachineUnderstanding the Dalvik Virtual Machine
Understanding the Dalvik Virtual Machine
 
An Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeAn Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and Runtime
 
Jvm internals
Jvm internalsJvm internals
Jvm internals
 
A Life of breakpoint
A Life of breakpointA Life of breakpoint
A Life of breakpoint
 

Último

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Último (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Dissecting the Hotspot JVM

  • 1. Dissecting the Hotspot JVM Martin Toshev @martin_fmi Ivan St. Ivanov @ivan_stefanov
  • 2. Agenda Virtual Machine Basics The Hotspot JVM Understanding the Hotspot Source Code Debugging Hotspot
  • 4. Why this presentation ? Feeling comfortable with the JVM source code allows you to: • contribute to and improve the JVM itself • fork and customize the JVM for whatever reason you want (experimental, commercial, academic …)
  • 5. Why this presentation ? Feeling comfortable with the JVM source code allows you to: • be a step ahead of Chuck Norris
  • 6. Virtual Machine Basics A typical virtual machine for an interpreted language provides: • Compilation of source language into VM specific bytecode • Data structures to contains instructions and operands (the data the instructions process)
  • 7. Virtual Machine Basics A typical virtual machine for an interpreted language provides: • A call stack for function call operations • An ‘Instruction Pointer’ (IP) pointing to the next instruction to execute
  • 8. Virtual Machine Basics A typical virtual machine for an interpreted language provides: • A virtual ‘CPU’ – the instruction dispatcher that: o Fetches the next instruction (addressed by the instruction pointer) o Decodes the operands o Executes the instruction
  • 9. The Hotspot JVM Provides: • bytecode execution - using an interpreter, two runtime compilers or On-Stack Replacement • storage allocation and garbage collection • runtimes - start up, shut down, class loading, threads, interaction with OS and others
  • 13. The Hotspot JVM Three phases of class-loading: – Loading – Linking – Initialization
  • 17. The Hotspot JVM 0 iload_0 1 iload_1 2 imul 3 istore_3 4 iload_3 5 iload_2 6 imul 7 istore 4 9 iload 4 11ireturn
  • 21. The Hotspot JVM Heap memory: new Data() Data Class<Data> Class<Class>
  • 22. The Hotspot JVM Mark word contains: – – – – – Identity hash code age lock record address monitor address state (unlocked, light-weight locked, heavy-weight locked, marked for GC) – biased / biasable (includes other fields such as thread ID)
  • 24. The Hotspot JVM Execution engine: while(true) { bytecode b = bytecodeStream[pc++]; switch(b) { case iconst_1: push(1); break; case iload_0: push(local(0)); break; case iadd: push(pop() + pop()); break; } }
  • 25. The Hotspot JVM Execution engine: while(true) { bytecode b = bytecodeStream[pc++]; switch(b) { case iconst_1: push(1); break; case iload_0: push(local(0)); break; case iadd: push(pop() + pop()); break; } } NOT that simple …
  • 26. The Hotspot JVM Different execution techniques: – interpreting – just-in-time (JIT) compilation – adaptive optimization (determines "hot spots" by monitoring execution)
  • 27. The Hotspot JVM JIT compilation: – triggered asynchronously by counter overflow for a method/loop (interpreted counts method entries and loopback branches) – produces generated code and relocation info (transferred on next method entry) – in case JIT-compiled code calls not-yet-JIT-compiled code control is transferred to the interpreter
  • 28. The Hotspot JVM JIT compilation: – compiled code may be forced back into interpreted bytecode (deoptimization) – is complemented by On-Stack Replacement (turn dynamically interpreted to JIT compiled code and viseversa - dynamic optimization/deoptimization) – is more optimized for server VM (but hits start-up time compared to client VM)
  • 29. The Hotspot JVM JIT compilation flow (performed during normal bytecode execution): 1) bytecode is turned into a graph 2) the graph is turned into a linear sequence of operations that manipulate an infinite loop of virtual registers (each node places its result in a virtual register)
  • 30. The Hotspot JVM JIT compilation flow (performed during normal bytecode execution): 3) physical registers are allocated for virtual registers (the program stack might be used in case virtual registers exceed physical registers) 4) code for each operation is generated using its allocated registers
  • 31. The Hotspot JVM Typical execution flow (when using the java/javaw launcher): 1. Parse the command line options 2. Establish the heap sizes and the compiler type (client or server) 3. Establish the environment variables such as CLASSPATH 4. If the java Main-Class is not specified on the command line fetch the Main-Class name from the JAR's manifest 5. Create the VM using JNI_CreateJavaVM in a newly created thread (non primordial thread)
  • 32. The Hotspot JVM Typical execution flow (when using the java/javaw launcher): 6. Once the VM is created and initialized, load the MainClass 7. Invoke the main method in the VM using CallStaticVoidMethod 8. Once the main method completes check and clear any pending exceptions that may have occurred and also pass back the exit status 9. Detach the main thread using DetachCurrentThread, by doing so we decrement the thread count so the DestroyJavaVM can be called safely
  • 33. Demo
  • 34. Summary No JVMs were injured during this presentation …
  • 36. Resources The Java Virtual Machine Specification Java SE7 Edition http://docs.oracle.com/javase/specs/jvms/se7/jvms7.pdf Mani Sarkar 's collection of Hotspot-related links https://gist.github.com/neomatrix369/5743225 Hotspot documentation http://openjdk.java.net/groups/hotspot/
  • 37. Resources Hacking Hotspot in Eclipse under Ubuntu 12.04 http://neomatrix369.wordpress.com/2013/03/12/hotspot-isin-focus-again-aka-hacking-hotspot-in-eclipse-juno-underubuntu-12-04/ Garner R., The Design and Construction of High Performance Garbage Collectors, PhD thesis https://digitalcollections.anu.edu.au/handle/1885/9053
  • 38. Resources Shi Y., Virtual Machine Shutdown: Stack versus Registers, PhD Thesis https://www.cs.tcd.ie/publications/techreports/reports.07/TCD-CS-2007-49.pdf Compiler and JVM Research at JKU http://www.ssw.uni-linz.ac.at/Research/Projects/JVM/
  • 39. Resources Xavier Leroy, Java bytecode verification: algorithms and formalizations http://pauillac.inria.fr/~xleroy/publi/bytecode-verification-JAR.pdf FOSDEM 2007 http://openjdk.java.net/groups/hotspot/docs/FOSDEM-2007HotSpot.pdf
  • 40. Resources The Architecture of the Java Virtual Machine http://www.artima.com/insidejvm/ed2/jvm2.html

Notas del editor

  1. magicThe magic item supplies the magic number identifying the class file format;it has the value 0xCAFEBABE.minor_version, major_versionThe values of the minor_version and major_version items are the minor andmajor version numbers of this class file.constant_pool_countThe value of the constant_pool_count item is equal to the number of entriesin the constant_pool table plus one.constant_pool[]The constant_pool is a table of structures representing various stringconstants, class and interface names, field names, and other constants that arereferred to within the ClassFile structure and its substructures.access_flagsThe value of the access_flags item is a mask of flags used to denote accesspermissions to and properties of this class or interface.this_classThe value of the this_class item must be a valid index into theconstant_pool table.super_classFor a class, the value of the super_class item either must be zero ormust be a valid index into the constant_pool table.interfaces_countThe value of the interfaces_count item gives the number of directsuperinterfaces of this class or interface type.interfaces[]Each value in the interfaces array must be a valid index intothe constant_pool table.fields_countThe value of the fields_count item gives the number of field_infostructures in the fields table. The field_info structures represent allfields, both class variables and instance variables, declared by this class orinterface type.fields[]Each value in the fields table must be a field_info structure givinga complete description of a field in this class or interface.methods_countThe value of the methods_count item gives the number of method_infostructures in the methods table.methods[]Each value in the methods table must be a method_info structure givinga complete description of a method in this class or interface. attributes_countThe value of the attributes_count item gives the number of attributes in the attributes table of this class.attributes[]Each value of the attributes table must be an attribute_info (§4.7)structure.
  2. Loading: finding and importing the binary data for a typeLinking: performing verification, preparation, and (optionally) resolutionVerification: ensuring the correctness of the imported typePreparation: allocating memory for class variables and initializing the memory to default valuesResolution: transforming symbolic references from the type into direct references.Initialization: Invoking Java code that initializes class variables to their proper starting values.
  3. A new frame is created and added (pushed) to the top of stack for every method invocation. The frame is removed (popped) when the method returns normally or if an uncaught exception is thrown during the method invocation. Each frame contains:Local variable arrayReturn valueOperand stackReference to runtime constant pool for class of the current method
  4. Some things to note here:In case the method is not static then this is passed as the first argument implicitly.The JVM is a stack-based virtual machine.
  5. A compiled class file consists of the following structure:ClassFile{ u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_infocontant_pool[constant_pool_count – 1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; }
  6. Objects that are logically considered as part of the JVM mechanics are not created on the Heap.The non-heap memory includes:Permanent Generation that contains the method area and interned stringsCode Cache used for compilation and storage of methods that have been compiled to native code by the JIT compilerThe method area stores per-class information such as:Class Loader ReferenceRun Time Constant PoolNumeric constantsField referencesMethod ReferencesAttributesField data(per field)NameTypeModifiersAttributesMethod data (per method)NameReturn TypeParameter Types (in order)ModifiersAttributesMethod code(Per method)BytecodesOperand stack sizeLocal variable sizeLocal variable tableException table (per exception handler - start point, end point,PC offset for handler code, constant pool index for exception class being caught)
  7. The Heap is used to allocate class instances and arrays at runtime. Arrays and objects can never be stored on the stack because a frame is not designed to change in size after it has been created. The frame only stores references that point to objects or arrays on the heap. Unlike primitive variables and references in the local variable array (in each frame) objects are always stored on the heap so they are not removed when a method ends. Instead objects are only removed by the garbage collector.To support garbage collection the heap is divided into three sections:Young Generation - Often split between Eden and SurvivorOld Generation (also called Tenured Generation)Permanent GenerationObjects and Arrays are never explicitly de-allocated instead the garbage collector automatically reclaims them.Typically this works as follows:1) New objects and arrays are created into the young generation2) Minor garbage collection will operate in the young generation. Objects, that are still alive, will be moved from the eden space to the survivor space.3) Major garbage collection, which typically causes the application threads to pause, will move objects between generations. Objects, that are still alive, will be moved from the young generation to the old (tenured) generation.4) The permanent generation is collected every time the old generation is collected. They are both collected when either becomes full.mark word - The first word of every object header. Usually a set of bitfields including synchronization state and identity hash code. May also be a pointer (with characteristic low bit encoding) to synchronization related information. During GC, may contain GC state bits.klass pointer - The second word of every object header. Points to another object (a metaobject) which describes the layout and behavior of the original object. For Java objects, the &quot;klass&quot; contains a C++ style &quot;vtable&quot;.Note that both Java objects and VM-internal objects have a common object header format.The Young GenerationThe young generation must support fast allocation, but we expect most of those objects to become unreachable fairly quickly. We would like not to have to expend any effort on the unreachable objects, since there are so many of them. So when our young generations need to be collected, we identify the lreachable objects in them and copy them out of the young generation. Once we&apos;ve copied out the reachable objects, the objects remaining in the young generation are unreachable, and the space for them can be recovered without attention to each individual unreachable object. This operation of copying the reachable objects out of a generation we call “scavenging”, so we often refer to our young generation collectors as “scavengers”. An advantage of scavenging is that it is fast. A disadvantage is that it requires room of a second copy of all the reachable objects from the scavenged generation.The Old GenerationOnce objects have been scavenged out of the young generation we expect them to remain reachable for at least a while, and to reference and be referenced by other objects that remain reachable. To collect the space in the old generation we need algorithms that can&apos;t depend on high density of unreachable objects. In particular we can&apos;t count on having enough space to scavenge such objects somewhere else. Copying objects in the old generation can be expensive, both because one has to move the objects, and because one has to update all the references to the object to point to the new location of the object. On the other hand, copying objects means that one can accumulate the recovered space into one large region, from which allocation is faster (which speeds up scavenging of the young generation), and allows us to return excess storage to the operating system, which is polite.The Permanent GenerationIn addition to the objects created by the Java application, there are objects created and used by the HotSpot virtual machine whose storage it is convenient to have allocated and recovered by the storage manager. To avoid confusing things, such objects are allocated in a separate generation, the so-called “permanent” generation. In fact, the objects in it are not “permanent”, but that&apos;s what it has been called historically. For example, information about loaded classes is stored in the permanent generation, and recovered when those classes are no longer reachable from the application.Garbage collectors: concurrent: the ability to work while application threads are still running parallel: the ability to do work across multiple thread of execution=&gt; collectors can be parallel but not concurrent;concurrent but not parallel or both concurrent and parallelSafepointing:at a safepoints threads cannot modify the Java heap or stackused to support GC, deoptimization, Java thread suspension, JVM Tool Interface operations (e.g. heap dumps)works by polling (VM thread poisons/un-poisons polling page and threads poll for a safepoint)
  8. note 1: &apos;klass&apos; is the Hotspot term for class metadata addressnote 2: array instances share the same &apos;klass&apos; hierarchy (instead of fields have a field denoting the &apos;size&apos; of the array and an array of itemsKlass (or Klass word):pointer to the metadata of the object’s classvtable of the methods of the classconstant vtable offset for methods
  9. When object is locked, mark word copied into lock record in frame on thread stack. Lock records track objects locked by currently executing methods.Walk thread stack to find thread&apos;s locked objects.The monitor address is the address of the object&apos;s wait queue.
  10. The bytecode is first interpreted while the JIT is translating it in the background. Once the JIT compilation is complete, the JVM switches to using that code instead of the interpreter. 
  11. The bytecode is first interpreted while the JIT is translating it in the background. Once the JIT compilation is complete, the JVM switches to using that code instead of the interpreter. 
  12. The bytecode is first interpreted while the JIT is translating it in the background. Once the JIT compilation is complete, the JVM switches to using that code instead of the interpreter. 
  13. The bytecode is first interpreted while the JIT is translating it in the background. Once the JIT compilation is complete, the JVM switches to using that code instead of the interpreter. 
  14. JNI_CreateJavaVMThe JNI invocation method performs, the following:Ensures that no two threads call this method at the same time and that no two VM instances are created in the same process. Noting that a VM cannot be created in the same process space once a point in initialization is reached, “point of no return”. This is so because the VM creates static data structures that cannot be re-initialized, at this time.Checks to make sure the JNI version is supported, and the ostream is initialized for gc logging. The OS modules are initialized such as the random number generator, the current pid, high-resolution time, memory page sizes, and the guard pages.The arguments and properties passed in are parsed and stored away for later use. The standard java system properties are initialized.The OS modules are further created and initialized, based on the parsed arguments and properties, are initialized for synchronization, stack, memory, and safepoint pages. At this time other libraries such as libzip, libhpi, libjava, libthread are loaded, signal handlers are initialized and set, and the thread library is initialized.The output stream logger is initialized. Any agent libraries (hprof, jdi) required are initialized and started.The thread states and the thread local storage (TLS), which holds several thread specific data required for the operation of threads, are initialized.The global data is initialized as part of the I phase, such as event log, OS synchronization primitives, perfMemory (performance memory), chunkPool (memory allocator).At this point, we can create Threads. The Java version of the main thread is created and attached to the current OS thread. However this thread will not be yet added to the known list of the Threads. The Java level synchronization is initialized and enabled.The rest of the global modules are initialized such as theBootClassLoader, CodeCache, Interpreter, Compiler, JNI, SystemDictionary, and Universe. Noting that, we have reached our “point of no return”, ie. We can no longer create another VM in the same process address space.The main thread is added to the list, by first locking the Thread_Lock. The Universe, a set of required global data structures, is sanity checked. The VMThread, which performs all the VM&apos;s critical functions, is created. At this point the appropriate JVMTI events are posted to notify the current state.The following 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 System classes, are loaded and initialized. At this point, the VM is initialized and operational, but not yet fully functional.The Signal Handler thread is started, the compilers are initialized and the CompileBroker thread is started. The other helper threads StatSampler and WatcherThreads are started, at this time the VM is fully functional, the JNIEnv is populated and returned to the caller, and the VM is ready to service new JNI requests.
  15. DestroyJavaVMThis method can be called from the launcher to tear down the VM, it can also be called by the VM itself when a very serious error occurs.The tear down of the VM takes the following steps:Wait until we are the last non-daemon thread to execute, noting that the VM is still functional.Call java.lang.Shutdown.shutdown(), which will invoke Java level shutdown hooks, run finalizers if finalization-on-exit.Call before_exit(), prepare for VM exit run VM level shutdown hooks (they are registered through JVM_OnExit()), stop the Profiler,StatSampler, Watcher and GC threads. Post the status events to JVMTI/PI, disable JVMPI, and stop the Signal thread.Call JavaThread::exit(), to release JNI handle blocks, remove stack guard pages, and remove this thread from Threads list. From this point on we cannot execute any more Java code.Stop VM thread, it will bring the remaining VM to a safepoint and stop the compiler threads. At a safepoint, care should that we should not use anything that could get blocked by a Safepoint.Disable tracing at JNI/JVM/JVMPI barriers.Set _vm_exited flag for threads that are still running native code.Delete this thread.Call exit_globals(), which deletes IO and PerfMemoryresources.Return to caller.