SlideShare a Scribd company logo
1 of 49
Virtual Machines
Guest lecture — Adrian Lienhard
Birds-eye view
© Adrian Lienhard 1.2
A virtual machine is an abstract computing
architecture supporting a programming
language in a hardware-independent fashion
A virtual machine is an abstract computing
architecture supporting a programming
language in a hardware-independent fashion
Z1, 1938
© Adrian Lienhard 3
Roadmap
> Introduction
> The heap store
> Interpreter
> Automatic memory management
> Threading System
> Optimizations
© Adrian Lienhard 4
Implementing a Programming Language
© Adrian Lienhard 5
How are VMs implemented?
Typically using an efficient and portable language
such as C, C++, or assembly code
Pharo VM platform-independent part written in Slang:
– subset of Smalltalk, translated to C
– core: 600 methods or 8k LOC in Slang
– Slang allows one to simulate VM in Smalltalk
© Adrian Lienhard 6
Main Components of a VM
The heap store
Interpreter
Automatic memory management
Threading System
© Adrian Lienhard 7
Pros and Cons of the VM Approach
Pros
> Platform independence of application code
“Write once, run anywhere”
> Simpler programming model
> Security
> Optimizations for different hardware architectures
Cons
> Execution overhead
> Not suitable for system programming
© Adrian Lienhard 8
Roadmap
> Introduction
> The heap store
> Interpreter
> Automatic memory management
> Threading System
> Optimizations
QuickTime™ and a
decompressor
are needed to see this picture.
© Adrian Lienhard 9
Object Memory Layout
32-bit direct-pointer scheme
Reality is more complex:
– 1-word header for instances of compact classes
– 2-word header for normal objects
– 3-word header for large objects
© Adrian Lienhard 10
Different Object Formats
> fixed pointer fields
> indexable types:
– indexable pointer fields (e.g., Array)
– indexable weak pointer fields (e.g., WeakArray)
– indexable word fields (e.g., Bitmap)
– indexable byte fields (e.g., ByteString)
Object format (4bit)
0 no fields
1 fixed fields only
2 indexable pointer fields only
3 both fixed and indexable pointer fields
4 both fixed and indexable weak fields
6 indexable word fields only
8-11 indexable byte fields only
12-15 ...
SystemNavigation>>allObjectsDo: aBlock
| object endMarker |
object := self someObject.
endMarker := Object new.
[endMarker == object]
whileFalse: [aBlock value: object.
object := object nextObject]
“Answer the first object on the heap”
anObject someObject
“Answer the next object on the heap”
anObject nextObject
Excludes small
integers!
Iterating Over All Objects in Memory
© Adrian Lienhard 12
Roadmap
> Introduction
> The heap store
> Interpreter
> Automatic memory management
> Threading System
> Optimizations
Stack vs. Register VMs
Stack machines
– Smalltalk, Java and most other VMs
– Simple to implement for different hardware architectures
Register machines
– only few register VMs, e.g., Parrot VM (Perl6)
– potentially faster than stack machines
VM provides a virtual processor that
interprets bytecode instructions
VM provides a virtual processor that
interprets bytecode instructions
© Adrian Lienhard 14
Interpreter State and Loop
Interpreter state
– instruction pointer (ip): points to current bytecode
– stack pointer (sp): topmost item in the operand stack
– current active method or block context
– current active receiver and method
Interpreter loop
1. branch to appropriate bytecode routine
2. fetch next bytecode
3. increment instruction pointer
4. execute the bytecode routine
5. return to 1.
© Adrian Lienhard 15
Method Contexts
method header:
– primitive index
– number of args
– number of temps
– large context flag
– number of literals
© Adrian Lienhard 16
Stack Manipulating Bytecode Routine
Example: bytecode <70> self
Interpreter>>pushReceiverBytecode
self fetchNextBytecode.
self push: receiver
Interpreter>>push: anObject
sp := sp + BytesPerWord.
self longAt: sp put: anObject
© Adrian Lienhard 17
Stack Manipulating Bytecode Routine
Example: bytecode <01> pushRcvr: 1
Interpreter>>pushReceiverVariableBytecode
self fetchNextBytecode.
self pushReceiverVariable: (currentBytecode bitAnd: 16rF)
Interpreter>>pushReceiverVariable: fieldIndex
self push: (
self fetchPointer: fieldIndex ofObject: receiver)
Interpreter>>fetchPointer: fieldIndex ofObject: oop
^ self longAt: oop + BaseHeaderSize + (fieldIndex * BytesPerWord)
© Adrian Lienhard 18
Message Sending Bytecode Routine
1. find selector, receiver and its class
2. lookup message in the class’ method dictionary
3. if method not found, repeat this lookup in successive
superclasses; if superclass is nil, instead send
#doesNotUnderstand:
4. create a new method context and set it up
5. activate the context and start executing the instructions
in the new method
Example: bytecode <E0> send: hello
© Adrian Lienhard 19
Message Sending Bytecode Routine
Interpreter>>sendLiteralSelectorBytecode
selector := self literal: (currentBytcode bitAnd: 16rF).
argumentCount := ((currentBytecode >> 4) bitAnd: 3) - 1.
rcvr := self stackValue: argumentCount.
class := self fetchClassOf: rcvr.
self findNewMethod.
self executeNewMethod.
self fetchNewBytecode
Example: bytecode <E0> send: hello
This routine (bytecodes 208-255)
can use any of the first 16 literals
and pass up to 2 arguments
E0(hex) = 224(dec)
= 1110 0000(bin)
E0 AND F = 0
=> literal frame at 0
((E0 >> 4) AND 3) - 1 = 1
=> 1 argument
© Adrian Lienhard 20
Primitives
Primitive methods trigger a VM routine
and are executed without a new
method context unless they fail
> Improve performance (arithmetics, at:, at:put:, ...)
> Do work that can only be done in VM (new object creation,
process manipulation, become, ...)
> Interface with outside world (keyboard input, networking, ...)
> Interact with VM plugins (named primitives)
ProtoObject>>nextObject
<primitive: 139>
self primitiveFailed
© Adrian Lienhard 21
Roadmap
> Introduction
> The heap store
> Interpreter
> Automatic memory management
> Threading System
> Optimizations
© Adrian Lienhard 22
Automatic Memory Management
Challenges
– Fast allocation
– Fast program execution
Tell when an object is no longer used
and then recycle the memory
Tell when an object is no longer used
and then recycle the memory
– Small predictable pauses
– Scalable to large heaps
– Minimal space usage
© Adrian Lienhard 23
Main Approaches
1. Reference Counting
2. Mark and Sweep
© Adrian Lienhard 24
Reference Counting GC
Idea
> For each store operation increment count field in header
of newly stored object
> Decrement if object is overwritten
> If count is 0, collect object and decrement the counter of
each object it pointed to
Problems
> Run-time overhead of counting (particularly on stack)
> Inability to detect cycles (need additional GC technique)
© Adrian Lienhard 25
Reference Counting GC
© Adrian Lienhard 26
Mark and Sweep GC
Idea
> Suspend current process
> Mark phase: trace each accessible object leaving a
mark in the object header (start at known root objects)
> Sweep phase: all objects with no mark are collected
> Remove all marks and resume current process
Problems
> Need to “stop the world”
> Slow for large heaps generational collectors
> Fragmentation compacting collectors
© Adrian Lienhard 27
Mark and Sweep GC
© Adrian Lienhard 28
Generational Collectors
Idea
> Partition objects in generations
> Create objects in young generation
> Tenuring: move live objects from young to old generation
> Incremental GC: frequently collect young generation (very fast)
> Full GC: infrequently collect young+old generation (slow)
Difficulty
> Need to track pointers from old to new space
Most new objects live very short lives,
most older objects live forever [Ungar 87]
Most new objects live very short lives,
most older objects live forever [Ungar 87]
© Adrian Lienhard 29
Generational Collectors: Remembered Set
Write barrier: remember objects with old-young pointers:
> On each store check whether
storee (object2) is young and
storand (object1) is old
> If true, add storand to remembered set
> When marking young generation, use objects in remembered set as
additional roots
object1.f := object2
© Adrian Lienhard 30
Compacting Collectors
–
© Adrian Lienhard 31
The Pharo GC
Pharo: mark and sweep compacting collector
with two generations
– Cooperative, i.e., not concurrent
– Single threaded
© Adrian Lienhard 32
When Does the GC Run?
– Incremental GC on allocation count or memory needs
– Full GC on memory needs
– Tenure objects if survivor threshold exceeded
“Incremental GC after this many allocations”
SmalltalkImage current vmParameterAt: 5
“Tenure when more than this many objects survive”
SmalltalkImage current vmParameterAt: 6
4000
2000
© Adrian Lienhard 33
VM Memory Statistics
SmalltalkImage current
vmStatisticsReportString
memory 20,245,028 bytes
old 14,784,388 bytes (73.0%)
young 117,724 bytes (0.6%)
used 14,902,112 bytes (73.6%)
free 5,342,916 bytes (26.4%)
GCs 975 (48ms between GCs)
full 0 totalling 0ms (0.0% uptime)
incr 975 totalling 267ms (1.0% uptime), avg 0.0ms
tenures 14 (avg 69 GCs/tenure)
Since last view 90 (54ms between GCs)
uptime 4.8s
full 0 totalling 0ms (0.0% uptime)
incr 90 totalling 29ms (1.0% uptime), avg 0.0ms
tenures 1 (avg 90 GCs/tenure)
© Adrian Lienhard 34
Memory System API
“Force GC”
Smalltalk garbageCollectMost
Smalltalk garbageCollect
“Is object in remembered set, is it young?”
Smalltalk rootTable includes: anObject
Smalltalk isYoung: anObject
“Various settings and statistics”
SmalltalkImage current getVMParameters
”Do an incremental GC after this many allocations"
SmalltalkImage current vmParameterAt: 5 put: 4000.
”Tenure when more than this many objects survive the GC"
SmalltalkImage current vmParameterAt: 6 put: 2000.
”Grow/shrink headroom"
SmalltalkImage current vmParameterAt: 25 put: 4*1024*1024.
SmalltalkImage current vmParameterAt: 24 put: 8*1024*1024.
© Adrian Lienhard 35
Finding Memory Leaks
– maybe object is just not GCed yet (force a full GC!)
– find the objects and then explore who references them
PointerFinder on:
AssignmentNode someInstance
PointerExplorer new
openExplorerFor:
AssignmentNode someInstance
PointerFinder finds a path
from a root to some object
I have objects that do not get collected. What’s wrong?I have objects that do not get collected. What’s wrong?
© Adrian Lienhard 36
Roadmap
> Introduction
> The heap store
> Interpreter
> Automatic memory management
> Threading System
> Optimizations
© Adrian Lienhard 37
Threading System
Native threads
© Adrian Lienhard 38
Pharo: Green Threads
Each process has its own execution stack, ip, sp, ...
There is always one (and only one) running process
Each process behaves as if it owns the entire VM
Each process can be interrupted (context switching)
© Adrian Lienhard 39
Representing Processes and Run Queues
© Adrian Lienhard 40
Context Switching
1. store the current ip and sp registers to the current context
2. store the current context in the old process’ suspendedContext
3. change Processor to point to newProcess
4. load ip and sp registers from new process’ suspendedContext
Interpreter>>transferTo: newProcess
When you perform a context switch,
which process should run next?
When you perform a context switch,
which process should run next?
© Adrian Lienhard 41
Process Scheduler
> Cooperative between processes of the same priority
> Preemptive between processes of different priorities
Context is switched to the first process with highest priority when:
– current process waits on a semaphore
– current process is suspended or terminated
– Processor yield is sent
Context is switched if the following process has a higher priority:
– process is resumed or created by another process
– process is resumed from a signaled semaphore
When a process is interrupted, it moves to the back of its run queue
© Adrian Lienhard 42
Example: Semaphores and Scheduling
here := false.
lock := Semaphore forMutualExclusion.
[lock critical: [here := true]] fork.
lock critical: [
self assert: here not.
Processor yield.
self assert: here not].
Processor yield.
self assert: here When is the forked
process activated?
When is the forked
process activated?
© Adrian Lienhard 43
Roadmap
> Introduction
> The heap store
> Interpreter
> Automatic memory management
> Threading System
> Optimizations
© Adrian Lienhard 44
Many Optimizations...
> Method cache for faster lookup: receiver's class + method selector
> Method context cache (as much as 80% of objects created are
context objects!)
> Interpreter loop: 256 way case statement to dispatch bytecodes
> Quick returns: methods that simply return a variable or known
constant are compiled as a primitive method
> Small integers are tagged pointers: value is directly encoded in field
references. Pointer is tagged with low-order bit equal to 1. The
remaining 31 bit encode the signed integer value.
> ...
© Adrian Lienhard 45
Optimization: JIT (not in Pharo)
Idea of Just In Time Compilation
> Translate unit (method, loop, ...) into native machine code at runtime
> Store native code in a buffer on the heap
Challenges
> Run-time overhead of compilation
> Machine code takes a lot of space (4-8x compared to bytecode)
> Deoptimization is very tricky
Adaptive compilation: gather statistics to compile only units that
are heavily used (hot spots)
References
> Virtual Machines, Iain D. Craig, Springer, 2006
> Back to the Future – The Story of Squeak, A Practical Smalltalk
Written in Itself, Ingalls, Kaehler, Maloney, Wallace, Kay, OOPSLA ‘97
> Smalltalk-80, the Language and Its Implementation (the Blue Book),
Goldberg, Robson, Addison-Wesley, ‘83
http://stephane.ducasse.free.fr/FreeBooks/BlueBook/Bluebook.pdf
> The Java Virtual Machine Specification, Second Edition,
http://java.sun.com/docs/books/jvms/
> Stacking them up: a Comparison of Virtual Machines, Gough, IEEE‘01
> Virtual Machine Showdown: Stack Versus Registers, Shi, Gregg,
Beatty, Ertl, VEE’05
© Adrian Lienhard 46
© Adrian Lienhard 47
What you should know!
What is the difference between the operand stack and
the execution stack?
How do bytecode routines and primitives differ?
Why is the object format encoded in a complicated 4bit
pattern instead of using regular boolean values?
Why is the object address not suitable as a hash value?
What happens if an object is only weakly referenced?
Why is it hard to build a concurrent mark sweep GC?
What does cooperative multithreading mean?
How do you protect code from concurrent execution?
© Adrian Lienhard 48
Can you answer these questions?
There is a lot of similarity between VM and OS design.
What are the common components?
Why is accessing the 16th instance variable of an object
more efficient than the 17th?
Which disastrous situation could occur if a local C
pointer variable exists when a new object is allocated?
Why does #allObjectsDo: not include small integers?
What is the largest possible small integer?
© Adrian Lienhard 1.49
Attribution-ShareAlike 3.0 Unported
You are free:
to Share — to copy, distribute and transmit the work
to Remix — to adapt the work
Under the following conditions:
Attribution. You must attribute the work in the manner specified by the author or
licensor (but not in any way that suggests that they endorse you or your use of the
work).
Share Alike. If you alter, transform, or build upon this work, you may distribute the
resulting work only under the same, similar or a compatible license.
For any reuse or distribution, you must make clear to others the license terms of this work.
The best way to do this is with a link to this web page.
Any of the above conditions can be waived if you get permission from the copyright holder.
Nothing in this license impairs or restricts the author's moral rights.
License
http://creativecommons.org/licenses/by-sa/3.0/

More Related Content

What's hot

Grand Central Dispatch Design Patterns
Grand Central Dispatch Design PatternsGrand Central Dispatch Design Patterns
Grand Central Dispatch Design PatternsRobert Brown
 
Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCDrsebbe
 
An introduction to Rust: the modern programming language to develop safe and ...
An introduction to Rust: the modern programming language to develop safe and ...An introduction to Rust: the modern programming language to develop safe and ...
An introduction to Rust: the modern programming language to develop safe and ...Claudio Capobianco
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatchcqtt191
 
Rust: Unlocking Systems Programming
Rust: Unlocking Systems ProgrammingRust: Unlocking Systems Programming
Rust: Unlocking Systems ProgrammingC4Media
 
Objective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchObjective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchMatteo Battaglio
 
Effective java - concurrency
Effective java - concurrencyEffective java - concurrency
Effective java - concurrencyfeng lee
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency GotchasAlex Miller
 
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugVk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugketan_patel25
 
Ti1220 Lecture 2: Names, Bindings, and Scopes
Ti1220 Lecture 2: Names, Bindings, and ScopesTi1220 Lecture 2: Names, Bindings, and Scopes
Ti1220 Lecture 2: Names, Bindings, and ScopesEelco Visser
 
Java 8 - Stamped Lock
Java 8 - Stamped LockJava 8 - Stamped Lock
Java 8 - Stamped LockHaim Yadid
 
(Greach 2015) Dsl'ing your Groovy
(Greach 2015) Dsl'ing your Groovy(Greach 2015) Dsl'ing your Groovy
(Greach 2015) Dsl'ing your GroovyAlonso Torres
 
Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22nikomatsakis
 
Variables: names, bindings, type, scope
Variables: names, bindings, type, scopeVariables: names, bindings, type, scope
Variables: names, bindings, type, scopesuthi
 
Rust Intro @ Roma Rust meetup
Rust Intro @ Roma Rust meetup Rust Intro @ Roma Rust meetup
Rust Intro @ Roma Rust meetup Claudio Capobianco
 
The Rust Programming Language: an Overview
The Rust Programming Language: an OverviewThe Rust Programming Language: an Overview
The Rust Programming Language: an OverviewRoberto Casadei
 

What's hot (20)

03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
Grand Central Dispatch Design Patterns
Grand Central Dispatch Design PatternsGrand Central Dispatch Design Patterns
Grand Central Dispatch Design Patterns
 
Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCD
 
An introduction to Rust: the modern programming language to develop safe and ...
An introduction to Rust: the modern programming language to develop safe and ...An introduction to Rust: the modern programming language to develop safe and ...
An introduction to Rust: the modern programming language to develop safe and ...
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatch
 
8 - OOP - Smalltalk Syntax
8 - OOP - Smalltalk Syntax8 - OOP - Smalltalk Syntax
8 - OOP - Smalltalk Syntax
 
Rust: Unlocking Systems Programming
Rust: Unlocking Systems ProgrammingRust: Unlocking Systems Programming
Rust: Unlocking Systems Programming
 
Objective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchObjective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central Dispatch
 
4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)
 
Effective java - concurrency
Effective java - concurrencyEffective java - concurrency
Effective java - concurrency
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugVk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
 
Ti1220 Lecture 2: Names, Bindings, and Scopes
Ti1220 Lecture 2: Names, Bindings, and ScopesTi1220 Lecture 2: Names, Bindings, and Scopes
Ti1220 Lecture 2: Names, Bindings, and Scopes
 
Java 8 - Stamped Lock
Java 8 - Stamped LockJava 8 - Stamped Lock
Java 8 - Stamped Lock
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 
(Greach 2015) Dsl'ing your Groovy
(Greach 2015) Dsl'ing your Groovy(Greach 2015) Dsl'ing your Groovy
(Greach 2015) Dsl'ing your Groovy
 
Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22
 
Variables: names, bindings, type, scope
Variables: names, bindings, type, scopeVariables: names, bindings, type, scope
Variables: names, bindings, type, scope
 
Rust Intro @ Roma Rust meetup
Rust Intro @ Roma Rust meetup Rust Intro @ Roma Rust meetup
Rust Intro @ Roma Rust meetup
 
The Rust Programming Language: an Overview
The Rust Programming Language: an OverviewThe Rust Programming Language: an Overview
The Rust Programming Language: an Overview
 

Viewers also liked (20)

Stoop 415-design points
Stoop 415-design pointsStoop 415-design points
Stoop 415-design points
 
Stoop 390-instruction stream
Stoop 390-instruction streamStoop 390-instruction stream
Stoop 390-instruction stream
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
01 intro
01 intro01 intro
01 intro
 
Stoop 431-visitor
Stoop 431-visitorStoop 431-visitor
Stoop 431-visitor
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop 300-block optimizationinvw
Stoop 300-block optimizationinvwStoop 300-block optimizationinvw
Stoop 300-block optimizationinvw
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop 422-naming idioms
Stoop 422-naming idiomsStoop 422-naming idioms
Stoop 422-naming idioms
 
Double Dispatch
Double DispatchDouble Dispatch
Double Dispatch
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
05 seaside
05 seaside05 seaside
05 seaside
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
7 - OOP - OO Concepts
7 - OOP - OO Concepts7 - OOP - OO Concepts
7 - OOP - OO Concepts
 
Stoop 434-composite
Stoop 434-compositeStoop 434-composite
Stoop 434-composite
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
Stoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesignStoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesign
 

Similar to 12 virtualmachine

Virtual Machines Lecture
Virtual Machines LectureVirtual Machines Lecture
Virtual Machines Lecturelienhard
 
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...chen yuki
 
2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)
2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)
2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)JiandSon
 
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...Maarten Balliauw
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Maarten Balliauw
 
Building source code level profiler for C++.pdf
Building source code level profiler for C++.pdfBuilding source code level profiler for C++.pdf
Building source code level profiler for C++.pdfssuser28de9e
 
Exploring .NET memory management - A trip down memory lane - Copenhagen .NET ...
Exploring .NET memory management - A trip down memory lane - Copenhagen .NET ...Exploring .NET memory management - A trip down memory lane - Copenhagen .NET ...
Exploring .NET memory management - A trip down memory lane - Copenhagen .NET ...Maarten Balliauw
 
Extend Redis with Modules
Extend Redis with ModulesExtend Redis with Modules
Extend Redis with ModulesItamar Haber
 
It's always sunny with OpenJ9
It's always sunny with OpenJ9It's always sunny with OpenJ9
It's always sunny with OpenJ9DanHeidinga
 
Get more than a cache back! The Microsoft Azure Redis Cache (NDC Oslo)
Get more than a cache back! The Microsoft Azure Redis Cache (NDC Oslo)Get more than a cache back! The Microsoft Azure Redis Cache (NDC Oslo)
Get more than a cache back! The Microsoft Azure Redis Cache (NDC Oslo)Maarten Balliauw
 
MattsonTutorialSC14.pptx
MattsonTutorialSC14.pptxMattsonTutorialSC14.pptx
MattsonTutorialSC14.pptxgopikahari7
 
How to write clean & testable code without losing your mind
How to write clean & testable code without losing your mindHow to write clean & testable code without losing your mind
How to write clean & testable code without losing your mindAndreas Czakaj
 
Using Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systemsUsing Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systemsSerge Stinckwich
 
JVM Memory Model - Yoav Abrahami, Wix
JVM Memory Model - Yoav Abrahami, WixJVM Memory Model - Yoav Abrahami, Wix
JVM Memory Model - Yoav Abrahami, WixCodemotion Tel Aviv
 
Valgrind tutorial
Valgrind tutorialValgrind tutorial
Valgrind tutorialSatabdi Das
 
iland Internet Solutions: Leveraging Cassandra for real-time multi-datacenter...
iland Internet Solutions: Leveraging Cassandra for real-time multi-datacenter...iland Internet Solutions: Leveraging Cassandra for real-time multi-datacenter...
iland Internet Solutions: Leveraging Cassandra for real-time multi-datacenter...DataStax Academy
 
Leveraging Cassandra for real-time multi-datacenter public cloud analytics
Leveraging Cassandra for real-time multi-datacenter public cloud analyticsLeveraging Cassandra for real-time multi-datacenter public cloud analytics
Leveraging Cassandra for real-time multi-datacenter public cloud analyticsJulien Anguenot
 
Tips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native codeTips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native codeKenneth Geisshirt
 

Similar to 12 virtualmachine (20)

Virtual Machines Lecture
Virtual Machines LectureVirtual Machines Lecture
Virtual Machines Lecture
 
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
 
2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)
2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)
2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)
 
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
 
Heap & thread dump
Heap & thread dumpHeap & thread dump
Heap & thread dump
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
 
Building source code level profiler for C++.pdf
Building source code level profiler for C++.pdfBuilding source code level profiler for C++.pdf
Building source code level profiler for C++.pdf
 
Exploring .NET memory management - A trip down memory lane - Copenhagen .NET ...
Exploring .NET memory management - A trip down memory lane - Copenhagen .NET ...Exploring .NET memory management - A trip down memory lane - Copenhagen .NET ...
Exploring .NET memory management - A trip down memory lane - Copenhagen .NET ...
 
Extend Redis with Modules
Extend Redis with ModulesExtend Redis with Modules
Extend Redis with Modules
 
It's always sunny with OpenJ9
It's always sunny with OpenJ9It's always sunny with OpenJ9
It's always sunny with OpenJ9
 
Jvm memory model
Jvm memory modelJvm memory model
Jvm memory model
 
Get more than a cache back! The Microsoft Azure Redis Cache (NDC Oslo)
Get more than a cache back! The Microsoft Azure Redis Cache (NDC Oslo)Get more than a cache back! The Microsoft Azure Redis Cache (NDC Oslo)
Get more than a cache back! The Microsoft Azure Redis Cache (NDC Oslo)
 
MattsonTutorialSC14.pptx
MattsonTutorialSC14.pptxMattsonTutorialSC14.pptx
MattsonTutorialSC14.pptx
 
How to write clean & testable code without losing your mind
How to write clean & testable code without losing your mindHow to write clean & testable code without losing your mind
How to write clean & testable code without losing your mind
 
Using Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systemsUsing Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systems
 
JVM Memory Model - Yoav Abrahami, Wix
JVM Memory Model - Yoav Abrahami, WixJVM Memory Model - Yoav Abrahami, Wix
JVM Memory Model - Yoav Abrahami, Wix
 
Valgrind tutorial
Valgrind tutorialValgrind tutorial
Valgrind tutorial
 
iland Internet Solutions: Leveraging Cassandra for real-time multi-datacenter...
iland Internet Solutions: Leveraging Cassandra for real-time multi-datacenter...iland Internet Solutions: Leveraging Cassandra for real-time multi-datacenter...
iland Internet Solutions: Leveraging Cassandra for real-time multi-datacenter...
 
Leveraging Cassandra for real-time multi-datacenter public cloud analytics
Leveraging Cassandra for real-time multi-datacenter public cloud analyticsLeveraging Cassandra for real-time multi-datacenter public cloud analytics
Leveraging Cassandra for real-time multi-datacenter public cloud analytics
 
Tips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native codeTips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native code
 

More from The World of Smalltalk (11)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
06 debugging
06 debugging06 debugging
06 debugging
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 
Stoop 439-decorator
Stoop 439-decoratorStoop 439-decorator
Stoop 439-decorator
 

Recently uploaded

Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 

Recently uploaded (20)

Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 

12 virtualmachine

  • 1. Virtual Machines Guest lecture — Adrian Lienhard
  • 2. Birds-eye view © Adrian Lienhard 1.2 A virtual machine is an abstract computing architecture supporting a programming language in a hardware-independent fashion A virtual machine is an abstract computing architecture supporting a programming language in a hardware-independent fashion Z1, 1938
  • 3. © Adrian Lienhard 3 Roadmap > Introduction > The heap store > Interpreter > Automatic memory management > Threading System > Optimizations
  • 4. © Adrian Lienhard 4 Implementing a Programming Language
  • 5. © Adrian Lienhard 5 How are VMs implemented? Typically using an efficient and portable language such as C, C++, or assembly code Pharo VM platform-independent part written in Slang: – subset of Smalltalk, translated to C – core: 600 methods or 8k LOC in Slang – Slang allows one to simulate VM in Smalltalk
  • 6. © Adrian Lienhard 6 Main Components of a VM The heap store Interpreter Automatic memory management Threading System
  • 7. © Adrian Lienhard 7 Pros and Cons of the VM Approach Pros > Platform independence of application code “Write once, run anywhere” > Simpler programming model > Security > Optimizations for different hardware architectures Cons > Execution overhead > Not suitable for system programming
  • 8. © Adrian Lienhard 8 Roadmap > Introduction > The heap store > Interpreter > Automatic memory management > Threading System > Optimizations
  • 9. QuickTime™ and a decompressor are needed to see this picture. © Adrian Lienhard 9 Object Memory Layout 32-bit direct-pointer scheme Reality is more complex: – 1-word header for instances of compact classes – 2-word header for normal objects – 3-word header for large objects
  • 10. © Adrian Lienhard 10 Different Object Formats > fixed pointer fields > indexable types: – indexable pointer fields (e.g., Array) – indexable weak pointer fields (e.g., WeakArray) – indexable word fields (e.g., Bitmap) – indexable byte fields (e.g., ByteString) Object format (4bit) 0 no fields 1 fixed fields only 2 indexable pointer fields only 3 both fixed and indexable pointer fields 4 both fixed and indexable weak fields 6 indexable word fields only 8-11 indexable byte fields only 12-15 ...
  • 11. SystemNavigation>>allObjectsDo: aBlock | object endMarker | object := self someObject. endMarker := Object new. [endMarker == object] whileFalse: [aBlock value: object. object := object nextObject] “Answer the first object on the heap” anObject someObject “Answer the next object on the heap” anObject nextObject Excludes small integers! Iterating Over All Objects in Memory
  • 12. © Adrian Lienhard 12 Roadmap > Introduction > The heap store > Interpreter > Automatic memory management > Threading System > Optimizations
  • 13. Stack vs. Register VMs Stack machines – Smalltalk, Java and most other VMs – Simple to implement for different hardware architectures Register machines – only few register VMs, e.g., Parrot VM (Perl6) – potentially faster than stack machines VM provides a virtual processor that interprets bytecode instructions VM provides a virtual processor that interprets bytecode instructions
  • 14. © Adrian Lienhard 14 Interpreter State and Loop Interpreter state – instruction pointer (ip): points to current bytecode – stack pointer (sp): topmost item in the operand stack – current active method or block context – current active receiver and method Interpreter loop 1. branch to appropriate bytecode routine 2. fetch next bytecode 3. increment instruction pointer 4. execute the bytecode routine 5. return to 1.
  • 15. © Adrian Lienhard 15 Method Contexts method header: – primitive index – number of args – number of temps – large context flag – number of literals
  • 16. © Adrian Lienhard 16 Stack Manipulating Bytecode Routine Example: bytecode <70> self Interpreter>>pushReceiverBytecode self fetchNextBytecode. self push: receiver Interpreter>>push: anObject sp := sp + BytesPerWord. self longAt: sp put: anObject
  • 17. © Adrian Lienhard 17 Stack Manipulating Bytecode Routine Example: bytecode <01> pushRcvr: 1 Interpreter>>pushReceiverVariableBytecode self fetchNextBytecode. self pushReceiverVariable: (currentBytecode bitAnd: 16rF) Interpreter>>pushReceiverVariable: fieldIndex self push: ( self fetchPointer: fieldIndex ofObject: receiver) Interpreter>>fetchPointer: fieldIndex ofObject: oop ^ self longAt: oop + BaseHeaderSize + (fieldIndex * BytesPerWord)
  • 18. © Adrian Lienhard 18 Message Sending Bytecode Routine 1. find selector, receiver and its class 2. lookup message in the class’ method dictionary 3. if method not found, repeat this lookup in successive superclasses; if superclass is nil, instead send #doesNotUnderstand: 4. create a new method context and set it up 5. activate the context and start executing the instructions in the new method Example: bytecode <E0> send: hello
  • 19. © Adrian Lienhard 19 Message Sending Bytecode Routine Interpreter>>sendLiteralSelectorBytecode selector := self literal: (currentBytcode bitAnd: 16rF). argumentCount := ((currentBytecode >> 4) bitAnd: 3) - 1. rcvr := self stackValue: argumentCount. class := self fetchClassOf: rcvr. self findNewMethod. self executeNewMethod. self fetchNewBytecode Example: bytecode <E0> send: hello This routine (bytecodes 208-255) can use any of the first 16 literals and pass up to 2 arguments E0(hex) = 224(dec) = 1110 0000(bin) E0 AND F = 0 => literal frame at 0 ((E0 >> 4) AND 3) - 1 = 1 => 1 argument
  • 20. © Adrian Lienhard 20 Primitives Primitive methods trigger a VM routine and are executed without a new method context unless they fail > Improve performance (arithmetics, at:, at:put:, ...) > Do work that can only be done in VM (new object creation, process manipulation, become, ...) > Interface with outside world (keyboard input, networking, ...) > Interact with VM plugins (named primitives) ProtoObject>>nextObject <primitive: 139> self primitiveFailed
  • 21. © Adrian Lienhard 21 Roadmap > Introduction > The heap store > Interpreter > Automatic memory management > Threading System > Optimizations
  • 22. © Adrian Lienhard 22 Automatic Memory Management Challenges – Fast allocation – Fast program execution Tell when an object is no longer used and then recycle the memory Tell when an object is no longer used and then recycle the memory – Small predictable pauses – Scalable to large heaps – Minimal space usage
  • 23. © Adrian Lienhard 23 Main Approaches 1. Reference Counting 2. Mark and Sweep
  • 24. © Adrian Lienhard 24 Reference Counting GC Idea > For each store operation increment count field in header of newly stored object > Decrement if object is overwritten > If count is 0, collect object and decrement the counter of each object it pointed to Problems > Run-time overhead of counting (particularly on stack) > Inability to detect cycles (need additional GC technique)
  • 25. © Adrian Lienhard 25 Reference Counting GC
  • 26. © Adrian Lienhard 26 Mark and Sweep GC Idea > Suspend current process > Mark phase: trace each accessible object leaving a mark in the object header (start at known root objects) > Sweep phase: all objects with no mark are collected > Remove all marks and resume current process Problems > Need to “stop the world” > Slow for large heaps generational collectors > Fragmentation compacting collectors
  • 27. © Adrian Lienhard 27 Mark and Sweep GC
  • 28. © Adrian Lienhard 28 Generational Collectors Idea > Partition objects in generations > Create objects in young generation > Tenuring: move live objects from young to old generation > Incremental GC: frequently collect young generation (very fast) > Full GC: infrequently collect young+old generation (slow) Difficulty > Need to track pointers from old to new space Most new objects live very short lives, most older objects live forever [Ungar 87] Most new objects live very short lives, most older objects live forever [Ungar 87]
  • 29. © Adrian Lienhard 29 Generational Collectors: Remembered Set Write barrier: remember objects with old-young pointers: > On each store check whether storee (object2) is young and storand (object1) is old > If true, add storand to remembered set > When marking young generation, use objects in remembered set as additional roots object1.f := object2
  • 30. © Adrian Lienhard 30 Compacting Collectors –
  • 31. © Adrian Lienhard 31 The Pharo GC Pharo: mark and sweep compacting collector with two generations – Cooperative, i.e., not concurrent – Single threaded
  • 32. © Adrian Lienhard 32 When Does the GC Run? – Incremental GC on allocation count or memory needs – Full GC on memory needs – Tenure objects if survivor threshold exceeded “Incremental GC after this many allocations” SmalltalkImage current vmParameterAt: 5 “Tenure when more than this many objects survive” SmalltalkImage current vmParameterAt: 6 4000 2000
  • 33. © Adrian Lienhard 33 VM Memory Statistics SmalltalkImage current vmStatisticsReportString memory 20,245,028 bytes old 14,784,388 bytes (73.0%) young 117,724 bytes (0.6%) used 14,902,112 bytes (73.6%) free 5,342,916 bytes (26.4%) GCs 975 (48ms between GCs) full 0 totalling 0ms (0.0% uptime) incr 975 totalling 267ms (1.0% uptime), avg 0.0ms tenures 14 (avg 69 GCs/tenure) Since last view 90 (54ms between GCs) uptime 4.8s full 0 totalling 0ms (0.0% uptime) incr 90 totalling 29ms (1.0% uptime), avg 0.0ms tenures 1 (avg 90 GCs/tenure)
  • 34. © Adrian Lienhard 34 Memory System API “Force GC” Smalltalk garbageCollectMost Smalltalk garbageCollect “Is object in remembered set, is it young?” Smalltalk rootTable includes: anObject Smalltalk isYoung: anObject “Various settings and statistics” SmalltalkImage current getVMParameters ”Do an incremental GC after this many allocations" SmalltalkImage current vmParameterAt: 5 put: 4000. ”Tenure when more than this many objects survive the GC" SmalltalkImage current vmParameterAt: 6 put: 2000. ”Grow/shrink headroom" SmalltalkImage current vmParameterAt: 25 put: 4*1024*1024. SmalltalkImage current vmParameterAt: 24 put: 8*1024*1024.
  • 35. © Adrian Lienhard 35 Finding Memory Leaks – maybe object is just not GCed yet (force a full GC!) – find the objects and then explore who references them PointerFinder on: AssignmentNode someInstance PointerExplorer new openExplorerFor: AssignmentNode someInstance PointerFinder finds a path from a root to some object I have objects that do not get collected. What’s wrong?I have objects that do not get collected. What’s wrong?
  • 36. © Adrian Lienhard 36 Roadmap > Introduction > The heap store > Interpreter > Automatic memory management > Threading System > Optimizations
  • 37. © Adrian Lienhard 37 Threading System Native threads
  • 38. © Adrian Lienhard 38 Pharo: Green Threads Each process has its own execution stack, ip, sp, ... There is always one (and only one) running process Each process behaves as if it owns the entire VM Each process can be interrupted (context switching)
  • 39. © Adrian Lienhard 39 Representing Processes and Run Queues
  • 40. © Adrian Lienhard 40 Context Switching 1. store the current ip and sp registers to the current context 2. store the current context in the old process’ suspendedContext 3. change Processor to point to newProcess 4. load ip and sp registers from new process’ suspendedContext Interpreter>>transferTo: newProcess When you perform a context switch, which process should run next? When you perform a context switch, which process should run next?
  • 41. © Adrian Lienhard 41 Process Scheduler > Cooperative between processes of the same priority > Preemptive between processes of different priorities Context is switched to the first process with highest priority when: – current process waits on a semaphore – current process is suspended or terminated – Processor yield is sent Context is switched if the following process has a higher priority: – process is resumed or created by another process – process is resumed from a signaled semaphore When a process is interrupted, it moves to the back of its run queue
  • 42. © Adrian Lienhard 42 Example: Semaphores and Scheduling here := false. lock := Semaphore forMutualExclusion. [lock critical: [here := true]] fork. lock critical: [ self assert: here not. Processor yield. self assert: here not]. Processor yield. self assert: here When is the forked process activated? When is the forked process activated?
  • 43. © Adrian Lienhard 43 Roadmap > Introduction > The heap store > Interpreter > Automatic memory management > Threading System > Optimizations
  • 44. © Adrian Lienhard 44 Many Optimizations... > Method cache for faster lookup: receiver's class + method selector > Method context cache (as much as 80% of objects created are context objects!) > Interpreter loop: 256 way case statement to dispatch bytecodes > Quick returns: methods that simply return a variable or known constant are compiled as a primitive method > Small integers are tagged pointers: value is directly encoded in field references. Pointer is tagged with low-order bit equal to 1. The remaining 31 bit encode the signed integer value. > ...
  • 45. © Adrian Lienhard 45 Optimization: JIT (not in Pharo) Idea of Just In Time Compilation > Translate unit (method, loop, ...) into native machine code at runtime > Store native code in a buffer on the heap Challenges > Run-time overhead of compilation > Machine code takes a lot of space (4-8x compared to bytecode) > Deoptimization is very tricky Adaptive compilation: gather statistics to compile only units that are heavily used (hot spots)
  • 46. References > Virtual Machines, Iain D. Craig, Springer, 2006 > Back to the Future – The Story of Squeak, A Practical Smalltalk Written in Itself, Ingalls, Kaehler, Maloney, Wallace, Kay, OOPSLA ‘97 > Smalltalk-80, the Language and Its Implementation (the Blue Book), Goldberg, Robson, Addison-Wesley, ‘83 http://stephane.ducasse.free.fr/FreeBooks/BlueBook/Bluebook.pdf > The Java Virtual Machine Specification, Second Edition, http://java.sun.com/docs/books/jvms/ > Stacking them up: a Comparison of Virtual Machines, Gough, IEEE‘01 > Virtual Machine Showdown: Stack Versus Registers, Shi, Gregg, Beatty, Ertl, VEE’05 © Adrian Lienhard 46
  • 47. © Adrian Lienhard 47 What you should know! What is the difference between the operand stack and the execution stack? How do bytecode routines and primitives differ? Why is the object format encoded in a complicated 4bit pattern instead of using regular boolean values? Why is the object address not suitable as a hash value? What happens if an object is only weakly referenced? Why is it hard to build a concurrent mark sweep GC? What does cooperative multithreading mean? How do you protect code from concurrent execution?
  • 48. © Adrian Lienhard 48 Can you answer these questions? There is a lot of similarity between VM and OS design. What are the common components? Why is accessing the 16th instance variable of an object more efficient than the 17th? Which disastrous situation could occur if a local C pointer variable exists when a new object is allocated? Why does #allObjectsDo: not include small integers? What is the largest possible small integer?
  • 49. © Adrian Lienhard 1.49 Attribution-ShareAlike 3.0 Unported You are free: to Share — to copy, distribute and transmit the work to Remix — to adapt the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights. License http://creativecommons.org/licenses/by-sa/3.0/