Download Slides

Document related concepts
no text concepts found
Transcript
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.
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!!
Example -- JVM
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.
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
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
 Stack
the operands
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.
Machine Model (cont)

Costs of executing a VM instruction (cont)
 Performing
 Usually
the computation
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.
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.
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.
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.
Just-In-Time Compilation

JIT compilers in JRE (JVM) and .NET runtimes
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).
Just-In-Time Compilation (cont)



Offers other advantages over statically compiled
code at development time, such as handling of latebound 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
Comparison of Various VMs
http://en.wikipedia.org/wiki/Comparison_of_application_virtual_machines
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)
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.
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
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.
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.
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”
Java Code Overview
JVM Architecture
Class Load Subsystem


Bootstrap class loader
User-defined class loader
Method Area & Heap
Method Area







Type Information
Constant pool
Field information
Method table
Method information
Class variable
Reference to class loader and class
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
Method Area

Type
Information
Method
Information
Constant
Pool
Class
Variables
Field
Information
Reference to
class loader
and class
Method
Table

Ordered set of constants

string

integer

floating point

final variables
Symbolic references to

Types

Fields

Methods
Method Area
Type
Information
Method
Information
Constant
Pool
Class
Variables
Field
Information
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
Method Area

Method’s name

Method’s return type

Type
Information
Method
Information
Constant
Pool
Class
Variables
Field
Information
Reference to
class loader
and class
Method
Table

Number and type of
parameters
Modifiers (subset)

public

private

protected

static

final

synchronized

native

abstract
Method Area

Ordered set of class
variables

Type
Information
Method
Information
Constant
Pool
Class
Variables
Field
Information
Reference to
class loader
and class
Method
Table
Static variables
Method Area


Type
Information
Method
Information
Constant
Pool
Class
Variables
Field
Information
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();
Method Area


Type
Information
Method
Information
Constant
Pool
Class
Variables
Field
Information
Reference to
class loader
and class
Method
Table
User for quick ref. to
method.
Contains name and index
in symbol ref. array
Example
class abc {
public int a = 10;
String str;
abc() {
str = “string1”;
}
Type info
abc
java.lang.Object
Isclass=true
modifier=4
public void print() {
System.out.print(a+” “+str);
}
}
Method info
Constant pool
Symbol ref. array
a
void
void
0
0
1
5
<init>
print
“string1”
10
Field info
name
a
str
name ret.type npar modifier parlist codeptr
<init>
print
str
Type Modifier
int
5
String
4
Method Table
name
Index
<init>
2
print
3
index
0
1
Class
variables
null
Example
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).
Heap (Object Representation)
Heap (Arrays as Objects)
PC register & Java Stacks & Native
Mehtod Stacks
Java Stack


Java stack stores a thread’s state in discrete frames.
Each frame contains
 Local
variables area.
 Operant stack
 Frame data
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.
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;
}
}
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
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
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)
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)
Java .class File

This Class(2 bytes)


Super Class(2 bytes)


index into the constant pool to a "Class"-type entry
index into the constant pool to a "Class"-type entry
Interfaces(2 bytes)
 the number of interfaces implemented by this class. (N)
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)
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.
Bytecode- opcode

Basic Opcode
 const (push constant onto the stack)


Store(pop to local variables)


istore_1: store the integer in local position one
Load(push variable onto the stack)


iconst_1: push integer constant value 1 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
ByteCode

javap -c
 print

javap -c -s –verbose


out the bytecode
Print out the constant pool
http://arhipov.blogspot.com/2011/01/javabytecode-fundamentals.html
Bytecode

Example-Example.java
public class Example
{
public int plus(int a)
{
int b = 1;
return a + b;
}
}
Bytecode

Javap –c Example
ByteCode

Local variable Table for Example.java
References




http://www.artima.com/insidejvm/ed2/
http://rockfishcs.cs.unc.edu/COMP144/lect32a.ppt
http://web.cecs.pdx.edu/~harry/musings/Smalltalk
Overview.html
http://www.cse.iitk.ac.in/users/vkirankr/Memory%
20Architecture.ppt