Dalvik Vm & Jit

ankitsomani007 1,122 views 24 slides Aug 12, 2010
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

No description available for this slideshow.


Slide Content

Dalvik VM & JIT Ankit Somani July 25, 2010

What is VM ? A virtual machine ( VM ) is a software implementation of a machine (i.e. a computer ) that executes programs like a physical machine. A set of registers A stack (optional) An execution environment A garbage-collected heap A constant pool A method storage area An instruction set Basic Parts:

Types of VM ? 1. Stack based VM (uses instructions to load in a stack for execution) 2. Register based VM (uses instructions to be encoded in source and destination registers) Based on its architecture : 1. System Virtual Machine (supports execution of a complete operating system) 2. Process Virtual Machine (supports execution of a single process) Based on its Working &Functionality :

Dalvik architecture is register based Can run on slow CPU, with little ram & in OS with lesser or even without swap space It is optimized to use less space The interpreter is simplified for faster execution It executes its own Dalvik byte code rather than Java byte code Dalvik Virtual Machine

Dalvik (Register based) take average 47 % less executed VM instruction then JVM (Stack based). Register code is 25% larger than the corresponding stack code. This increased cost of fetching more VM instructions due to larger code size involves only 1.07% extra real machine loads per VM instruction. Which is negligible. Some Marketing Reasons too . Why android choose Dalvik?

.Dex file anatomy

Conversion of Java Byte Code to Dalvik Byte Code

Shared Constant Pool public interface Zapper { public String zap(String s, Object o); } public class Blort implements Zapper { public String zap(String s, Object o) { ...; } } public class ZapUser { public void useZap (Zapper z) { z.zap(...); } }

Shared Constant Pool (.Class File)

Shared Constant Pool (.Dex File)

Shared Constant Pool (Memory Saved Via) minimal repetition per-type pools (implicit typing) implicit labeling *After the Obfuscation & compression it will be much lesser.

Example #1: Source public static long sumArray ( int [] arr ) { long sum = 0; for ( int i : arr ) { sum += i ; } return sum; }

Example #1: .Class

Example #1: .Dex

Comparison b/w Dalvik VM versus JVM Memory Usage Comparison Architecture Comparison Supported Libraries Comparison Reliability Comparison Multiple instance and JIT Comparison

JIT (Just in Time) Compilation What is JIT ? Just-in-time compilation ( JIT ), also known as dynamic translation , is a technique for improving the runtime performance of a computer program . A hybrid approach, with translation occurring continuously, as with interpreters, but with caching of translated code to minimize performance degradation

When to compile install time, launch time, method invoke time, instruction fetch time What to compile whole program, shared library, page, method, trace, single instruction JIT Types Android needs a combination that meet the needs of a mobile Minimal additional memory usage Coexist with Dalvik’s container-based security model Quick delivery of performance boost Smooth transition between interpretation & compiled code

Android system_server example

Trace JIT Trace : String of Instructions Minimizing memory usage critical for mobile devices Important to deliver performance boost quickly User might give up on new app if we wait too long to JIT Leave open the possibility of supplementing with method based JIT The two styles can co-exist A mobile device looks more like a server when it’s plugged in Best of both worlds Trace JIT when running on battery Method JIT in background while charging

Dalvik Trace JIT Flow

Dalvik JIT v1.0 Overview Tight integration with interpreter Useful to think of the JIT as an extension of the interpreter Interpreter profiles and triggers trace selection mode when a potential trace head goes hot Trace request is built during interpretation Trace requests handed off to compiler thread, which compiles and optimizes into native code Compiled traces chained together in translation cache

Dalvik JIT v1.0 Features Per-process translation caches (sharing only within security sandboxes) Simple traces - generally 1 to 2 basic blocks long Local optimizations Register promotion Load/store elimination Redundant null-check elimination Loop optimizations Simple loop detection Invariant code motion Induction variable optimization

Questions ?

Thanks Ankit Somani [email protected]