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

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

Jan 15, 2016

Download

Documents

Jayde Walden
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: INF 212 ANALYSIS OF PROG. LANGS Virtual Machines Instructors: Crista Lopes Copyright © Instructors.

INF 212ANALYSIS OF PROG. LANGSVirtual Machines

Instructors: Crista LopesCopyright © Instructors.

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

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.

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

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!!

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

Example -- JVM

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

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.

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

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

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

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.

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

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.

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

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.

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

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.

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

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.

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

Just-In-Time Compilation

JIT compilers in JRE (JVM) and .NET runtimes

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

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).

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

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 http://www.youtube.com/watch?v=Ls0tM-

c4Vfo

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

Comparison of Various VMs

http://en.wikipedia.org/wiki/Comparison_of_application_virtual_machines

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

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)

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

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.

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

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

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

Java

1991 - James Gosling begins work on Java project (Originally named “Oak” for the oak tree outside his office.)

1995 - Sun releases first public implementation as Java 1.0

1998 - JDK 1.1 release downloads tops 2 million 1999 - Java 2 is released by Sun 2005 - Approximately 4.5 million developers use

Java technology 2007 - Sun makes all of Java’s core code

available under open-source distribution terms.

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

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.

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

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”

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

Java Code Overview

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

JVM Architecture

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

Class Load Subsystem

Bootstrap class loader User-defined class loader

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

Method Area & Heap

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

Method Area

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

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

Method Area

Type Informatio

n

Constant Pool

Field Informatio

n

Method Informatio

n

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

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

Method Area

Type Informatio

n

Constant Pool

Field Informatio

n

Method Informatio

n

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

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

Method Area

Type Informatio

n

Constant Pool

Field Informatio

n

Method Informatio

n

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

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

Method Area

Type Informatio

n

Constant Pool

Field Informatio

n

Method Informatio

n

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

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

Method Area

Type Informatio

n

Constant Pool

Field Informatio

n

Method Informatio

n

Class Variables

Reference to class loader

and class

Method Table

Ordered set of class variables Static variables

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

Method Area

Type Informatio

n

Constant Pool

Field Informatio

n

Method Informatio

n

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

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

Method Area

Type Informatio

n

Constant Pool

Field Informatio

n

Method Informatio

n

Class Variables

Reference to class loader

and class

Method Table

User for quick ref. to method.

Contains name and index in symbol ref. array

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

Example

class abc { public int a = 10;  String str;  abc() {     str = “string1”;  }

  public void print() {     System.out.print(a+” “+str);   }}

a str <init> print

Symbol ref. array

10     “string1”

Constant poolabcjava.lang.ObjectIsclass=truemodifier=4

Type info

name Type Modifier

a        int          5             0

str      String      4              1

indexField info

name ret.type npar modifier parlist

<init>     void        0        1 print      void       0         5

codeptr

Method infoMethod Table

null

Class variablesname Index

<init> 2

print 3

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

Example

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

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).

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

Heap (Object Representation)

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

Heap (Arrays as Objects)

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

PC register & Java Stacks & Native Mehtod Stacks

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

Java Stack

Java stack stores a thread’s state in discrete frames.

Each frame contains Local variables area. Operant stack Frame data

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

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.

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

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; }}

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

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

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

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

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

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)

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

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)

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

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)

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

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, <init>(), is generated by the compiler.

Attributes(2 bytes) The number of attributes (N)

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

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.

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

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: push 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

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

ByteCode

 javap -c print out the bytecode

javap -c -s –verbose Print out the constant pool

http://arhipov.blogspot.com/2011/01/java-bytecode-fundamentals.html

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

Bytecode

Example-Example.java

public class Example

{

public int plus(int a)

{

int b = 1;

return a + b;

}

}

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

Bytecode

Javap –c Example

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

ByteCode

Local variable Table for Example.java

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

References

http://www.artima.com/insidejvm/ed2/ http://rockfish-cs.cs.unc.edu/COMP144/

lect32a.ppt http://web.cecs.pdx.edu/~harry/

musings/SmalltalkOverview.html http://www.cse.iitk.ac.in/users/vkirankr/

Memory%20Architecture.ppt