24
Dalvik VM & JIT Ankit Somani July 25, 2010

Dalvik Vm & Jit

Embed Size (px)

Citation preview

Page 1: Dalvik Vm & Jit

Dalvik VM & JITAnkit Somani

July 25, 2010

Page 2: Dalvik Vm & Jit

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:

Page 3: Dalvik Vm & Jit

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 :

Page 4: Dalvik Vm & Jit

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

Page 5: Dalvik Vm & Jit

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?

Page 6: Dalvik Vm & Jit

.Dex file anatomy

Page 7: Dalvik Vm & Jit

Conversion of Java Byte Code to Dalvik Byte Code

Page 8: Dalvik Vm & Jit

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

}

Page 9: Dalvik Vm & Jit

Shared Constant Pool (.Class File)

Page 10: Dalvik Vm & Jit

Shared Constant Pool (.Dex File)

Page 11: Dalvik Vm & Jit

Shared Constant Pool (Memory Saved Via)

minimal repetition per-type pools (implicit typing) implicit labeling

*After the Obfuscation & compression it will be much lesser.

Page 12: Dalvik Vm & Jit

Example #1: Source

public static long sumArray(int[] arr) {long sum = 0;for (int i : arr) {

sum += i;}return sum;

}

Page 13: Dalvik Vm & Jit

Example #1: .Class

Page 14: Dalvik Vm & Jit

Example #1: .Dex

Page 15: Dalvik Vm & Jit

Comparison b/w Dalvik VM versus JVM

Memory Usage Comparison Architecture Comparison Supported Libraries Comparison

Reliability Comparison Multiple instance and JIT Comparison

Page 16: Dalvik Vm & Jit

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

Page 17: Dalvik Vm & Jit

When to compileinstall time, launch time, method invoke time, instruction fetch time

What to compilewhole program, shared library, page, method, trace, single instruction

JIT Types

Android needs a combination that meet the needs of a mobileMinimal additional memory usage Coexist with Dalvik’s container-based security model Quick delivery of performance boost Smooth transition between interpretation & compiled code

Page 18: Dalvik Vm & Jit

Android system_server example

Page 19: Dalvik Vm & Jit

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 JITThe two styles can co-existA mobile device looks more like a server when it’s plugged inBest of both worldsTrace JIT when running on batteryMethod JIT in background while charging

Page 20: Dalvik Vm & Jit

Dalvik Trace JIT Flow

Page 21: Dalvik Vm & Jit

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

Page 22: Dalvik Vm & Jit

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

Page 23: Dalvik Vm & Jit
Page 24: Dalvik Vm & Jit

Ankit Somani [email protected]