CLR CLR What What is is a a virtual virtual machine machine? - - PowerPoint PPT Presentation

clr clr
SMART_READER_LITE
LIVE PREVIEW

CLR CLR What What is is a a virtual virtual machine machine? - - PowerPoint PPT Presentation

Introduction: : Introduction CLR CLR What What is is a a virtual virtual machine machine? ? A new new virtual virtual machine machine A CLI CLI and and CLR CLR JIT JIT execution execution and and


slide-1
SLIDE 1

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 1 1

CLR CLR A A new new virtual virtual machine machine

João Ferreira João Ferreira joao.ferreira@progmat.com joao.ferreira@progmat.com

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 2 2

Introduction Introduction: :

  • What

What is is a a virtual virtual machine machine? ?

  • CLI

CLI and and CLR CLR

  • JIT

JIT execution execution and and performance performance on

  • n CLR

CLR

  • Unix

Unix with with “.NET” “.NET”

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 3 3

Virtual Virtual Machines Machines

  • Why

Why “virtual”? “virtual”?

  • Emulation

Emulation software software that that translates translates from from

  • ne
  • ne language

language to to another another

HIGH LEVEL LANGUAGE LOW LEVEL LANGUAGE

C Assembly

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 4 4

Virtual Machines Virtual Machines

  • Problems

Problems with with native native compilers compilers

  • Machine

Machine arquitecture arquitecture dependence dependence

  • Operating

Operating system system conventions conventions

  • Compiler

Compiler specific specific issues issues

  • To solve

To solve this this... ...

  • Develop

Develop a a language language that that executes as executes as bytecode bytecode (UNCOL, (UNCOL, Lisp Lisp, , P P-

  • Code

Code, , etc etc.) .)

  • Failure

Failure not not running running at at native native speed speed! !

slide-2
SLIDE 2

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 5 5

Virtual Virtual Machines Machines

Source Code (High Level Language) Compiler produces bytecode VIRTUAL MACHINE MISC HARDWARE/SOFTWARE

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 6 6

New New era era of

  • f VM

VM

  • JVM

JVM and and CLR CLR

  • Use

Use JIT JIT ( (Just Just-

  • In

In-

  • Time

Time) to ) to produce produce well well-

  • ptimized
  • ptimized native

native machine machine code code

  • Bytecode

Bytecode ( (portability portability) ) + +

  • native

native code code generation generation ( (speed speed) ) + +

  • Bytecode

Bytecode verifier verifier

  • Stack

Stack based based – – doesn’t doesn’t now now the the mean mean of

  • f

registers registers

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 7 7

Common Common Language Language Runtime Runtime

  • Microsoft marketing

Microsoft marketing .NET ( .NET (DotNet DotNet) )

  • Is

Is nothing nothing more more than than a virtual a virtual machine machine, , however however.. ..

  • Based

Based on

  • n CLI

CLI a standard a standard designed designed from from the the scracth scracth to to support support multiple multiple languages languages

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 8 8

Common Common Language Language Infraestructure Infraestructure

  • International

International Standard (ECMA) Standard (ECMA)

  • CLI

CLI Specification Specification

CTS (Common Type System) CLS (Common Language Specification) CIL (Common Intermediate Language) VES (Virtual Execution System)

slide-3
SLIDE 3

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 9 9

CTS ( CTS (Common Common Type Type Specification Specification) )

  • A

A rich rich type type system system that that supports supports the the types types and and operations

  • perations found

found in in many many programming programming languages languages

  • To

To support support a a wide wide range range of

  • f programming

programming languages languages

  • Defines a

Defines a set set of

  • f rules

rules for for types types

  • Designed

Designed for for object

  • bject oriented
  • riented,

, procedural procedural and and functional functional languages languages

  • More

More than than 15 15 languages languages implemented implemented CTS CTS

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 10 10

CLS ( CLS (Common Common Language Language Specification Specification) )

  • A

A subset subset of

  • f CTS

CTS

  • Defines

Defines the the rules rules for for each each individual individual language language interop interop with with each each other

  • ther

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 11 11

CIL ( CIL (Common Common Intermediate Intermediate Language Language) )

  • Also

Also known known as as IL IL or

  • r MSIL (Microsoft)

MSIL (Microsoft)

  • A

A language language more “ more “higher higher” ” than than native native instruction instruction set set

  • Based

Based on

  • n metadata

metadata – – self self describing describing

  • An

An assembly assembly oriented

  • riented language

language

  • Can

Can create create instances instances of

  • f objects
  • bjects
  • Call

Call virtual virtual methods methods

  • Work

Work with with arrays arrays

  • Throw

Throw and and catch catch exceptions exceptions! !

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 12 12

CIL CIL Example Example

ldarg num load argument no. num onto the stack … …, value

ldarg.1

public static void TestMethod (int a, int b) // arguments { int c; int d; int e; // locals c = a + b; d = 10; e = c + d; }

ldloc indx load local variable no. indx onto the stack … …, value

ldloc.1

slide-4
SLIDE 4

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 13 13

CIL CIL Example Example

ldc num load numeric constant … …, num

ldc.i4 10

starg num store a value in an argument slot …, value …

starg.1

stloc indx pop value from stack to local variable …, value …

stloc.0

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 14 14 public static void TestMethod (int a, int b) { int c; int d; int e; c = a + b; d = 10; e = c + d; } .method public hidebysig static void TestMethod(int32 a,int32 b) cil managed { // Code size 12 (0xc) .maxstack 2 .locals init ([0] int32 c, [1] int32 d, [2] int32 e) IL_0000: ldarg.0 IL_0001: ldarg.1 IL_0002: add IL_0003: stloc.0 IL_0004: ldc.i4.s 10 IL_0006: stloc.1 IL_0007: ldloc.0 IL_0008: ldloc.1 IL_0009: add IL_000a: stloc.2 IL_000b: ret } // end of method Class1::TestMethod

evaluation stack

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 15 15

VES (Virtual VES (Virtual Execution Execution System System) )

  • Implements

Implements and and enforces enforces CTS CTS

  • Will

Will load load and and run run programs programs written written for for the the CLI CLI ( (with with IL IL code code) )

  • Handles

Handles all all the the major major overheads

  • verheads of
  • f

traditional traditional programming programming models models

  • How

How it it will will load load? ?

  • JIT

JIT

  • Install

Install time time compilers compilers

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 16 16

CLR CLR

  • Is

Is the the Microsoft VES (Virtual Microsoft VES (Virtual Execution Execution System System) for CLI ) for CLI

  • CLR

CLR will will use: use:

  • Managed

Managed modules modules

  • Assemblies

Assemblies

  • JIT

JIT compilers compilers To To run run CLI CLI-

  • compliant

compliant programms programms into into Win32 Win32 and and Intel Intel x86 x86 architectures architectures. .

slide-5
SLIDE 5

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 17 17

CLR CLR Execution Execution Model Model

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 18 18

During During Development Development... ...

  • Ok

Ok.. Some .. Some code code... ...written written with with notepad notepad: :

using using System System; ; class class Hello Hello { { public public static static void void Main( Main() ) { { System.Console.WriteLine( System.Console.WriteLine(" "Hello Hello world world!"); !"); System.Console.WriteLine(“Bye System.Console.WriteLine(“Bye world world!”); !”); } } } }

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 19 19

Compiling Compiling the the source source code code

  • csc

csc ola.il

  • la.il (

(managed managed code code) )

  • Result

Result: : ola.exe

  • la.exe

Assembly Assembly! !

  • Resulting

Resulting file file is is a PE ( a PE (Portable Portable Executable Executable) )

Managed module (IL and metadata) Managed module (IL and metadata) Resource file (.jpeg, .gif, etc.) Resource file (.jpeg, .gif, etc.)

Tool combining multiple managed modules and resource files into an assembly C# compiler (CSC.exe), Visual Basic Compiler (VBC.exe), Assembly Linker (AL.exe) …

Assembly (Manifest describes the set of files in the assembly) Managed module (IL and metadata) Managed module (IL and metadata) Resource file (.jpeg, .gif, etc.) Resource file (.jpeg, .gif, etc.)

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 20 20

User User executes executes ola.exe

  • la.exe... CLR

... CLR starts starts! !

PE header .text section .idata section CLR header IL Metadata Ola EXE JMP _CorExeMain DLL: MSCorLib.dll Function: _CorExeMain

Process's primary Process's primary thread starts thread starts

Managed EXE MSCorEE.dll Address Space 1. 1. MSCorEE MSCorEE examines CLR header to get Main method's metadata token. examines CLR header to get Main method's metadata token. 2. 2. MSCorEE MSCorEE examines the Main's metadata to get location of IL within the E examines the Main's metadata to get location of IL within the EXE. XE. 3. 3. MSCorEE MSCorEE compiles Main's IL to native CPU. compiles Main's IL to native CPU. 4. 4. MSCorEE MSCorEE jumps to Main's (using primary thread) jumps to Main's (using primary thread) – – the application runs. the application runs.

slide-6
SLIDE 6

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 21 21

Ok Ok.. .. We We got got the the IL.. IL.. And And now now?? ??

. .method method public public hidebysig hidebysig static static void void Main( Main() ) cil cil managed managed { { . .entrypoint entrypoint // // Code Code size size 21 (0x15) 21 (0x15) . .maxstack maxstack 1 1 IL_0000: IL_0000: ldstr ldstr " "Hello Hello world world!" !" IL_0005: IL_0005: call call void void [ [mscorlib mscorlib] ]System.Console System.Console:: ::WriteLine(string WriteLine(string) ) IL_000a: IL_000a: ldstr ldstr " "Bye Bye world world!" !" IL_000f: IL_000f: call call void void [ [mscorlib mscorlib] ]System.Console System.Console:: ::WriteLine(string WriteLine(string) ) IL_0014: IL_0014: ret ret } // } // end end of

  • f method

method Hello Hello:: ::Main Main

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 22 22

JIT JIT will will translate translate IL to CPU IL to CPU instructions instructions

MSCorEE.dll

  • la.exe

static void Main() { Console.WriteLine("Hello Console.WriteLine("Hello World World"); "); Console.WriteLine("Bye World"); }

Method Structure to Console

JitCompiler static void WriteLine() JitCompiler static void WriteLine(string) JitCompiler (remaining members) 1. JITCompiler function { 2. In the assembly that implements the type (Console) look up the method (Writeline) being called in metadata. 3. From the metadata, get the IL for this method. 4. Allocate the block of memory. 5. Compile the IL into native CPU instructions; the native code is saved in the memory allocated in step 4. 6. Modify the method's entry in the Type's table so that it now points to the memory block allocated in step 4. 7. Jump to the native code contained inside the memory block. 8. } Native CPU instructions

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 23 23

Already Already Jited Jited? ?

MSCorEE.dll

1. JITCompiler function { 2. In the assembly that implements the type (Console) look up the method (Writeline) being called in metadata. 3. From the metadata, get the IL for this method. 4. Allocate the block of memory. 5. Compile the IL into native CPU instructions; the native code is saved in the memory allocated in step 4. 6. Modify the method's entry in the Type's table so that it now points to the memory block allocated in step 4. 7. Jump to the native code contained inside the memory block. 8. }

Managed.exe

static void Main() { Console.WriteLine("Hello"); Console.WriteLine("GoodBye"); }

Console

JitCompiler static void WriteLine() Native static void WriteLine(string) JitCompiler (remaining members) Native CPU instructions

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 24 24

JIT Performance JIT Performance and and Issues Issues

  • Platform Independence

Platform Independence

  • Realized when high

Realized when high-

  • level language

level language compilers compilers convert source code convert source code to platform to platform agnostic agnostic MSIL code MSIL code

  • The application or

The application or software component software component is is distributed in this form distributed in this form

  • JIT compiles to native code either at

JIT compiles to native code either at runtime runtime or at

  • r at install time

install time

slide-7
SLIDE 7

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 25 25

JIT Performance JIT Performance and and Issues Issues

  • Language Interoperability

Language Interoperability

  • Occurs when

Occurs when different language different language compilers compilers compile to compile to language language-

  • agnostic MSIL code

agnostic MSIL code

  • Metadata

Metadata and the and the Common Type System Common Type System play a major role in cross play a major role in cross-

  • language and

language and platform independence platform independence

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 26 26

JIT Performance JIT Performance and and Issues Issues

  • Runtime Stack Manipulation

Runtime Stack Manipulation

  • The

The JIT Compiler JIT Compiler populates important data populates important data structures for object tracking and specific structures for object tracking and specific stack stack-

  • frame construction

frame construction

  • The JIT Compiler can be used to

The JIT Compiler can be used to identify identify specific code elements as they are specific code elements as they are consumed consumed, i.e., exception handlers and , i.e., exception handlers and security descriptors (Verifier) security descriptors (Verifier)

  • Doesn’t need registers

Doesn’t need registers

  • CPU independent

CPU independent

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 27 27

JIT performance JIT performance and and issues issues

  • Small Memory Footprint

Small Memory Footprint

  • JIT compilation takes advantage of the

JIT compilation takes advantage of the possibility that possibility that some code may never be some code may never be used used

  • The JIT Compiler compiles methods

The JIT Compiler compiles methods only as

  • nly as

needed needed

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 28 28

JIT Performance JIT Performance and and Issues Issues

  • JIT compiler knows more about the

JIT compiler knows more about the execution execution environment environment than an unmanaged compiler than an unmanaged compiler would know would know

  • JIT compiler can take

JIT compiler can take advantage of advantage of instructions instructions offered by the chip that the

  • ffered by the chip that the

unmanaged compiler knows nothing about unmanaged compiler knows nothing about

  • JIT compiler could detect that a

JIT compiler could detect that a certain test is certain test is always false always false, and short , and short-

  • circuit

circuit

  • The CLR could

The CLR could profile profile the code’s execution the code’s execution and and recompile recompile the IL on the fly reducing the IL on the fly reducing branching, etc. branching, etc.

slide-8
SLIDE 8

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 29 29

The The problems problems... ...

  • Unmanaged code

Unmanaged code is pre is pre-

  • compiled and

compiled and can just execute can just execute

  • Managed code requires

Managed code requires 2 compilation 2 compilation phases phases

  • Compiler produces IL

Compiler produces IL

  • IL compiled to native code at runtime,

IL compiled to native code at runtime, requiring more memory to be allocated, and requiring more memory to be allocated, and additional CPU cycles additional CPU cycles

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 30 30

Alternatives Alternatives to JIT to JIT

  • NGEN.EXE

NGEN.EXE – – Install Install Time Time tool tool to to create create a a native native image image into into the the native native cache cache

  • Native

Native Image Image a file a file containing containing compiled compiled processor processor-

  • specific

specific machine machine code code

  • Good

Good for for heavy heavy startup startup applications applications

  • Use

Use it it at at client client-

  • side

side

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 31 31

Unix Unix with with .NET .NET

Mono Project Mono Project www.go www.go-

  • mono.com

mono.com

  • DotGnu

DotGnu

  • http://www.gnu.org/projects/dotgnu

http://www.gnu.org/projects/dotgnu

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 32 32

What What they they did did? ?

  • Designed

Designed a a compiler compiler generating generating IL IL code code (ECMA 334) (ECMA 334) – – C# C# taking taking note note of

  • f CTS

CTS and and CLS CLS rules rules

  • A

A VES VES with with JIT’s JIT’s

  • A

A base base class class library library resides resides in in the the `mcs' `mcs' module module in in the the directoy directoy `class' `class'. . Each Each directory directory in in the the directory directory represents represents the the assembly assembly where where the the code code belongs belongs to, to, and and inside inside each each directory directory they they divide divide the the code code based based on

  • n the

the namespace namespace they they implement implement. .

slide-9
SLIDE 9

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 33 33

DotGnu DotGnu

  • Created

Created ilrun ilrun interpret interpret programms programms in in the the CIL CIL bytecode bytecode format format ( (Ecma Ecma 335) 335)

  • CIL

CIL converted converted to to CVM CVM ( (Converted Converted Virtual Virtual Machine Machine) )

  • Cscc

Cscc – – compiler compiler ANSI C ANSI C and and Ecma Ecma 334 334

(c) João Ferreira (c) João Ferreira -

  • ICCA 2004

ICCA 2004 34 34

Conclusion Conclusion

  • CLI

CLI is is a standard a standard – – if if you you want want portability portability, , all all languages languages should should be be CLI CLI-

  • compliant

compliant

  • Want

Want run run CLI? CLI?

  • make

make your your own

  • wn VES!

VES!

  • Make

Make your your JIT! JIT!