Dalvik Vm & Jit

2,774 views

Published on

  • Be the first to comment

Dalvik Vm & Jit

  1. 1. Dalvik VM & JIT<br />Ankit Somani <br />July 25, 2010<br />
  2. 2. What is VM ? <br />A virtual machine (VM) is a software implementation of a machine (i.e. a computer) that executes programs like a physical machine.<br />Basic Parts:<br /><ul><li> A set of registers
  3. 3. A stack (optional)
  4. 4. An execution environment
  5. 5. A garbage-collected heap
  6. 6. A constant pool
  7. 7. A method storage area
  8. 8. An instruction set</li></li></ul><li> Types of VM ? <br /><ul><li> Based on its Working &Functionality : </li></ul>1. System Virtual Machine (supports execution of a complete operating system)<br />2. Process Virtual Machine (supports execution of a single process)<br /><ul><li> Based on its architecture :</li></ul>1. Stack based VM (uses instructions to load in a stack for execution)<br />2. Register based VM (uses instructions to be encoded in source and destination<br />registers)<br />
  9. 9. Dalvik Virtual Machine<br /><ul><li>Dalvik architecture is register based
  10. 10. Can run on slow CPU, with little ram & in OS with lesser or even without swap space
  11. 11. It is optimized to use less space
  12. 12. The interpreter is simplified for faster execution
  13. 13. It executes its own Dalvik byte code rather than Java byte code</li></li></ul><li> Why android choose Dalvik?<br /><ul><li>Dalvik (Register based) take average 47 % less executed VM instruction then JVM (Stack based).
  14. 14. Register code is 25% larger than the corresponding stack code.
  15. 15. 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.
  16. 16. Some Marketing Reasons too.</li></li></ul><li> .Dex file anatomy<br />
  17. 17. Conversion of Java Byte Code to Dalvik Byte Code<br />
  18. 18. Shared Constant Pool<br />public interface Zapper {<br /> public String zap(String s, Object o);<br />}<br />public class Blort implements Zapper {<br /> public String zap(String s, Object o) {<br /> ...;<br /> }<br />}<br />public class ZapUser {<br /> public void useZap(Zapper z) {<br /> z.zap(...);<br /> }<br />}<br />
  19. 19. Shared Constant Pool (.Class File)<br />
  20. 20. Shared Constant Pool (.Dex File)<br />
  21. 21. Shared Constant Pool (Memory Saved Via)<br /><ul><li> minimal repetition
  22. 22. per-type pools (implicit typing)
  23. 23. implicit labeling</li></ul>*After the Obfuscation & compression it will be much lesser.<br />
  24. 24. Example #1: Source<br />public static long sumArray(int[] arr) {<br /> long sum = 0;<br /> for (inti : arr) {<br /> sum += i;<br /> }<br /> return sum;<br />}<br />
  25. 25. Example #1: .Class<br />
  26. 26. Example #1: .Dex<br />
  27. 27. Comparison b/w Dalvik VM versus JVM<br /><ul><li> Memory Usage Comparison
  28. 28. Architecture Comparison
  29. 29. Supported Libraries Comparison
  30. 30. Reliability Comparison
  31. 31. Multiple instance and JIT Comparison</li></li></ul><li> JIT (Just in Time) Compilation<br /><ul><li> What is JIT ?</li></ul>Just-in-time compilation (JIT), also known as dynamic translation, is a technique for improving the runtime performance of a computer program.<br />A hybrid approach, with translation occurring continuously, as with interpreters, but with caching of translated code to minimize performance degradation<br />
  32. 32. JIT Types<br /><ul><li>When to compile
  33. 33. install time, launch time, method invoke time, instruction fetch time
  34. 34. What to compile
  35. 35. whole program, shared library, page, method, trace, single instruction
  36. 36. Android needs a combination that meet the needs of a mobile
  37. 37. Minimal additional memory usage
  38. 38. Coexist with Dalvik’s container-based security model
  39. 39. Quick delivery of performance boost
  40. 40. Smooth transition between interpretation & compiled code</li></li></ul><li>Android system_server example<br />
  41. 41. Trace JIT <br /><ul><li> Trace : String of Instructions
  42. 42. Minimizing memory usage critical for mobile devices
  43. 43. Important to deliver performance boost quickly
  44. 44. User might give up on new app if we wait too long to JIT
  45. 45. Leave open the possibility of supplementing with method based JIT
  46. 46. The two styles can co-exist
  47. 47. A mobile device looks more like a server when it’s plugged in
  48. 48. Best of both worlds
  49. 49. Trace JIT when running on battery
  50. 50. Method JIT in background while charging</li></li></ul><li> Dalvik Trace JIT Flow<br />
  51. 51. Dalvik JIT v1.0 Overview<br /><ul><li> Tight integration with interpreter
  52. 52. Useful to think of the JIT as an extension of the interpreter
  53. 53. Interpreter profiles and triggers trace selection mode when a potential trace head goes hot
  54. 54. Trace request is built during interpretation
  55. 55. Trace requests handed off to compiler thread, which compiles and optimizes into native code
  56. 56. Compiled traces chained together in translation cache</li></li></ul><li>Dalvik JIT v1.0 Features<br /><ul><li> Per-process translation caches (sharing only within security sandboxes)
  57. 57. Simple traces - generally 1 to 2 basic blocks long
  58. 58. Local optimizations
  59. 59. Register promotion
  60. 60. Load/store elimination
  61. 61. Redundant null-check elimination
  62. 62. Loop optimizations
  63. 63. Simple loop detection
  64. 64. Invariant code motion
  65. 65. Induction variable optimization</li></li></ul><li>Questions ?<br />
  66. 66. Thanks<br />Ankit Somani <br />ankitsomani007@gmail.com<br />

×