Presentation is loading. Please wait.

Presentation is loading. Please wait.

INF 212 ANALYSIS OF PROG. LANGS Virtual Machines Instructors: Crista Lopes Copyright © Instructors.

Similar presentations


Presentation on theme: "INF 212 ANALYSIS OF PROG. LANGS Virtual Machines Instructors: Crista Lopes Copyright © Instructors."— Presentation transcript:

1 INF 212 ANALYSIS OF PROG. LANGS Virtual Machines Instructors: Crista Lopes Copyright © Instructors.

2 What is a Virtual Machine?  Runs as a normal application inside an OS and supports a single process.  Created when the process is started and destroyed when it exits.  A software implementation of a machine (i.e. a computer) that executes programs like a physical machine.

3 Types of Virtual Machines  Application Virtual Machines  Allows application byte code to be run on different architectures and operating systems.  Eg. JVM, CLR, Dalvik, Squeak (Smalltalk), etc…  System (Platform) Virtual Machines  Emulation of entire physical machine  Eg. VMWare, VirtualBox, etc…  We will be focusing on process virtual machines!!

4 Example -- JVM

5 Benefits of VMs  Compatibility: Virtual machines are compatible with various hardware platforms.  Isolation: Virtual machines are isolated from each other as if physically separated.  Encapsulation: Virtual machines encapsulate a complete computing environment.  Hardware Independence: Virtual machines run independently of underlying hardware.

6 Machine Model  Stacks vs Registers  Stack-based machine: Operands are pushed and popped off the stack.  Register-based machine: Operands stored in register.  The most popular VMs use stack architectures.  Costs of executing VM instructions  Dispatching the instruction  Accessing the operands  Performing the computation

7 Machine Model (cont)  Costs of executing a VM instruction  Dispatching the instruction A given task can often be expressed using fewer register machine instructions than stack ones.  Accessing the operands Stack code is smaller than register code, and requires fewer memory fetches to execute. This is the main reason why stack architectures are popular for VMs.

8 Machine Model (cont)  Costs of executing a VM instruction (cont)  Performing the computation Usually the smallest part of cost. Has to be performed regardless of intermediate representation. However, eliminating invariant and common expressions is much easier on a register machine.

9 Memory Management  Managed (eg. JVM)  Safe automatic memory management.  Disallow manually constructed pointers to memory.  Unmanaged (eg. LLVM)  Allow direct use and manipulation of pointers.  But no automated garbage collection.

10 Memory Management (cont)  Hybrid (eg. Dot.NET)  Offering both controlled use of memory, while also offering an “unsafe” mode that allows direct manipulation of pointers in ways that can violate type boundaries and permission.

11 Just-In-Time Compilation  AKA Dynamic Translation  A method to improve the runtime performance of computer programs  Hybrid of two previous approaches  Interpretation: Translated from a high-level language to machine code continuously during every execution  Static (ahead-of-time) compilation: Translated into machine code before execution, and only requires this translation once.

12 Just-In-Time Compilation  JIT compilers in JRE (JVM) and.NET runtimes

13 Just-In-Time Compilation (cont)  At the time of code execution, the JIT compiler will compile some or all of it to native machine code for better performance.  Can be done per-file, per-function or even on any arbitrary code fragment.  The compiled code is cached and reused later without needing to be recompiled (unlike interpretation).

14 Just-In-Time Compilation(cont)  Offers other advantages over statically compiled code at development time, such as handling of late- bound data types and the ability to enforce security guarantees.  Most VMs rely on JIT compilation for high speed code execution  JIT for Android: Dalvik JIT 

15 Comparison of Various VMs

16 Smalltalk  Brief History  Developed by Alan Kay et al. in Xerox PARC (Palo Alto Research Center Incorporated)  Generally recognized as the second Object Programming Language (OPL) (After Simula)  The first Pure OPL  Variants: Smalltalk-71, Smalltalk-72, Smalltalk-76, Smalltalk-80(First made available outside PARC), Squeak (Most popular version today)

17 Smalltalk (cont)  Two major components  The virtual image Stores the heap and all the objects in it on disk In Java, the heap conceptually starts out empty and is discarded after program termination  The virtual machine Reads the image from disk into memory and executes the code it contains.

18 Smalltalk (cont)  The image can be saved at the user’s discretion (“taking a snapshot”).  If the system crashes, the user may restart the system, going back to the exact state of the heap from the snapshot.  This feature is more commonly seen nowadays in system VMs instead of process VMs.  The image makes Smalltalk more portable than Java

19 Java  James Gosling begins work on Java project (Originally named “Oak” for the oak tree outside his office.)  Sun releases first public implementation as Java 1.0  JDK 1.1 release downloads tops 2 million  Java 2 is released by Sun  Approximately 4.5 million developers use Java technology  Sun makes all of Java’s core code available under open-source distribution terms.

20 Java  Java is a general-purpose, concurrent, class-based, object oriented language that is specifically designed to have as few implementation dependencies as possible.  It is intended to let application developers “write once, run anywhere”.  Currently one of the most popular programming languages in use.

21 Java  Why this design?  Familiarity Bytecode interpreter/compilers were used before Eg. Pascal “pcode”; Smalltalk bytecode  Minimize machine-dependency Do optimization on bytecode when possible Keep bytecode interpreter simple  Portability Transmit bytecode across network “Compile once, run anywhere”

22 Java Code Overview

23 JVM Architecture

24 Class Load Subsystem  Bootstrap class loader  User-defined class loader

25 Method Area & Heap

26 Method Area  Type Information  Constant pool  Field information  Method table  Method information  Class variable  Reference to class loader and class

27 Method Area Type Information Constant Pool Field Information Method Information Class Variables Reference to class loader and class Method Table  Fully qualified type’s name.  Fully qualified direct super class name.  Whether class or an interface  Type’s modifiers  List of fully qualified names of any direct super interfaces

28 Method Area Type Information Constant Pool Field Information Method Information Class Variables Reference to class loader and class Method Table  Ordered set of constants  string  integer  floating point  final variables  Symbolic references to  Types  Fields  Methods

29 Method Area Type Information Constant Pool Field Information Method Information Class Variables Reference to class loader and class Method Table  Field’s name  Field’s type  Field’s modifiers (subset)  public  private  protected  static  final  volatile  transient

30 Method Area Type Information Constant Pool Field Information Method Information Class Variables Reference to class loader and class Method Table  Method’s name  Method’s return type  Number and type of parameters  Modifiers (subset)  public  private  protected  static  final  synchronized  native  abstract

31 Method Area Type Information Constant Pool Field Information Method Information Class Variables Reference to class loader and class Method Table  Ordered set of class variables  Static variables

32 Method Area Type Information Constant Pool Field Information Method Information Class Variables Reference to class loader and class Method Table  Reference to class loader is used for dynamic linking.  Instance java.lang.Class is created every type for the following info.  getName();  getSuperClass();  isInterface();  getInterfaces();  getClassLoader();

33 Method Area Type Information Constant Pool Field Information Method Information Class Variables Reference to class loader and class Method Table  User for quick ref. to method.  Contains name and index in symbol ref. array

34 Example class abc { public int a = 10; String str; abc() { str = “string1”; } public void print() { System.out.print(a+” “+str); } astr print Symbol ref. array 10 “string1” Constant pool abc java.lang.Object Isclass=true modifier=4 Type info nameTypeModifier a int 5 0 str String 4 1 index Field info nameret.type npar modifierparlist void 0 1 print void 0 5 codeptr Method info Method Table null Class variables nameIndex 2 print3

35 Example

36 Heap  Objects and arrays are allocated in a single, shared heap.  Each application has its own heap (isolation).  However, two different threads of the same application could trample on each other’s heap data.  Used when memory allocated with new operator.  Runtime environment automatically frees up memory on heap occupied by objects that are no longer referenced (garbage collection).

37 Heap (Object Representation)

38 Heap (Arrays as Objects)

39 PC register & Java Stacks & Native Mehtod Stacks

40 Java Stack  Java stack stores a thread’s state in discrete frames.  Each frame contains  Local variables area.  Operant stack  Frame data

41 Local Variable Area  Organized as a zero-based array of cells.  Variables area accessed through their indices.  Values of type int, float, reference, and return address occupy one cell.  Values of type byte, short, and char also occupy one cell.  Values of type long and double occupy two consecutive cells in the array.

42 Example class Example3a { public static int runClassMethod(int i, long l, float f, double d, Object o, byte b) { return 0; } public int runInstanceMethod(char c, double d, short s, boolean b) { return 0; }

43 Operand Stack iload_0 // push the int in local variable 0 iload_1 // push the int in local variable 1 iadd // pop two ints, add them, push result istore_2 // pop int, store into local variable 2

44 Java.class File  10 basic sections to the Java Class File structure:  Magic Number  Version of Class File Format  Constant Pool  Access Flags  This Class  Super Class  Interfaces  Fields  Methods  Attributes

45 Java.class File  Magic Number (4 bytes)  Class files are identified by the following 4 byte header : CAFEBABE  Version of Class File Format (4 bytes)  minor version number of the class file format being used(2 bytes)  major version number of the class file format being used(2 bytes) J2SE 6.0 = 50 (0x32 hex) J2SE 5.0 = 49 (0x31 hex) JDK 1.4 = 48 (0x30 hex) JDK 1.3 = 47 (0x2F hex) JDK 1.2 = 46 (0x2E hex) JDK 1.1 = 45 (0x2D hex)

46 Java.class File  Constant Pool(2 bytes)  number of entries in the following constant pool table, say N  At least one greater than the actual number of entries (N-1 )  Constant Pool[1]….[N-1]  Access Flag(2 bytes)  flags that represent modifiers of the class or interface defined by this file. ACC_PUBLIC is 0x0001 ACC_FINAL is 0x0010 both public and final is (ACC_PUBLIC | ACC_FINAL)

47 Java.class File  This Class(2 bytes)  index into the constant pool to a "Class"-type entry  Super Class(2 bytes)  index into the constant pool to a "Class"-type entry  Interfaces(2 bytes)  the number of interfaces implemented by this class. (N)

48 Java.class File  Fields(2 bytes)  the number of fields (class or instance variables) declared by this class.  Methods(2 bytes)  the number of methods defined by this class. The count does not include any methods inherited from superclasses, only those methods explicitly defined in this class.  the instance initialization method, (), is generated by the compiler.  Attributes(2 bytes)  The number of attributes (N)

49 Conceptual Stack Frame Structure  Each time a method is invoked a new stack frame is created. The frame consists of an operand stack, an array of local variables, and a reference to the runtime constant pool of the class of the current method.

50 Bytecode- opcode  Basic Opcode  const (push constant onto the stack) iconst_1: push integer constant value 1 onto the stack  Store(pop to local variables) istore_1: store the integer in local position one  Load(push variable onto the stack) iload_1: p ush integer from local variable position one  Java opcodes generally indicate the type of their operands.  Opcodes iload, lload, fload, and dload push local variables of type int, long, float, and double, respectively, onto the stack

51 ByteCode  javap -c  print out the bytecode  javap -c -s –verbose  Print out the constant pool  bytecode-fundamentals.html bytecode-fundamentals.html

52 Bytecode  Example-Example.java public class Example { public int plus(int a) { int b = 1; return a + b; }

53 Bytecode  Javap –c Example

54 ByteCode  Local variable Table for Example.java

55 References   cs.cs.unc.edu/COMP144/lect32a.ppt cs.cs.unc.edu/COMP144/lect32a.ppt  Overview.html Overview.html  20Architecture.ppt 20Architecture.ppt


Download ppt "INF 212 ANALYSIS OF PROG. LANGS Virtual Machines Instructors: Crista Lopes Copyright © Instructors."

Similar presentations


Ads by Google