CLR: Under the Hood - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

CLR: Under the Hood

Description:

IL The language for execution. Independent of CPU and platform ... Code bloat can be a problem. 4. Execution. JIT Optimizations. Class Fib ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 43
Provided by: adminCu
Category:
Tags: clr | bloat | hood | under

less

Transcript and Presenter's Notes

Title: CLR: Under the Hood


1
CLR Under the Hood
Brad Abrams brada_at_Microsoft.com Program
Managerhttp//blogs.msdn.com/brada
2
Health Warning
  • This talk dives deep!
  • Examines internal data structures
  • They will change!
  • Internals knowledge not needed to write .NET apps

3
Quick Refresher The Unmanaged World
C source
VB6 source
Compiler
Compiler
.obj
PCode/x86 .exe
Linker
VBRun
.exe
Hardware Platform
Loader
Hardware Platform
4
Quick Refresher The CLR
Compilation
Code (IL)
Source Code
Metadata
At installation or the first time each method is
called
5
Agenda
QA
6
1. Compilation
HelloAtlanta.cs
using Systemclass Output private int
year public Output(int year)
this.year year public void
SayHello() Console.WriteLine("Hello,
its the year " year) class
HelloAtlanta static void Main(string
args) new Output(2004).SayHello()
new Output(2005).SayHello()
7
Agenda
QA
8
2. Packaging Assemblies
9
2. MetadataIL Intermediate Language
  • IL The language for execution
  • Independent of CPU and platform
  • Created by Microsoft, external commercial and
    academic language/compiler writers
  • Stack based virtual machine

1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
Evaluation Stack
10
2. MetadataIL Intermediate Language
  • IL The language for execution
  • Independent of CPU and platform
  • Created by Microsoft, external commercial and
    academic language/compiler writers
  • Stack based

1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0001
Evaluation Stack
11
2. MetadataIL Intermediate Language
  • IL The language for execution
  • Independent of CPU and platform
  • Created by Microsoft, external commercial and
    academic language/compiler writers
  • Stack based

1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0002
0000 0001
Evaluation Stack
12
2. MetadataIL Intermediate Language
  • IL The language for execution
  • Independent of CPU and platform
  • Created by Microsoft, external commercial and
    academic language/compiler writers
  • Stack based

1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0003
Evaluation Stack
13
2. MetadataIL Intermediate Language
  • IL The language for execution
  • Independent of CPU and platform
  • Created by Microsoft, external commercial and
    academic language/compiler writers
  • Stack based

1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0003
0000 0003
Evaluation Stack
14
2. MetadataIL Intermediate Language
  • IL The language for execution
  • Independent of CPU and platform
  • Created by Microsoft, external commercial and
    academic language/compiler writers
  • Stack based

1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0006
Evaluation Stack
15
2. MetadataIL Intermediate Language
  • IL The language for execution
  • Independent of CPU and platform
  • Created by Microsoft, external commercial and
    academic language/compiler writers
  • Stack based

1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0004
0000 0006
Evaluation Stack
16
2. MetadataIL Intermediate Language
  • IL The language for execution
  • Independent of CPU and platform
  • Created by Microsoft, external commercial and
    academic language/compiler writers
  • Stack based

1 2 3 4 2 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0002
Evaluation Stack
17
2. MetadataIL Verification
  • When processing IL, runtime verifies the IL to
    make sure its safe
  • Every IL construct is called with the correct
    amount of stack parameters
  • Every method is called with the correct type and
    number of parameters
  • Helps prevents buffer overflows, underruns
  • Helps prevent security holes
  • Safety allows multiple managed applications in
    the same process

18
Agenda
QA
19
3. Loading and LayoutStartup logic
  • OS hands the image to a CLR shim (mscoree.dll)
  • Which starts the runtime
  • Runtime performs the following
  • Loads assembly in to memory and sets up the MD
    reader
  • Resolves immediate dependencies
  • Slurps metadata, creates internal data structures
  • Starts execution at Assembly entry point

20
Agenda
QA
21
4. ExecutionInvocation
Objects in GC heap
Private execution engine memory
78abed08 System.Object.ToString()
78a7fe50 System.Object.Equals(System.Object)
78a74de8 System.Object.GetHashCode()
78abed58 System.Object.Finalize()
03690ceb Output..ctor(Int32)
03690cfb Output.SayHello()
03790118 Output.SayHello()
MethodTable(runtime info)
2004
03690cfb Prestub Dispatch
03790118 push edipush esipush ebx SayHello()
Native Code
JIT Compiler
newobj instance void Output.ctor(int32) call
instance void OutputSayHello()
22
4. ExecutionInvocation JIT output
Output.SayHello()
push edi push esimov edi,ecx mov
esi,dword ptr ds01AF201Chmov
ecx,788F34E8hcall FD34FF55 //
JIT_DbgIsJustMyCodemov edx,eaxmov eax,dword
ptr edi4mov dword ptr edx4,eaxmov
ecx,esicall 7530FA52mov esi,eaxcmp dword
ptr ds01AF1070h,0jne 0000000Cmov
ecx,1call 752E8A85 // System.String.Concatmov
ecx,dword ptr ds01AF1070hmov edx,esicall
dword ptr ds037B0010h // System.Console.WriteLi
ne pop esi pop ediret
23
4. ExecutionJIT Optimizations
  • Register Allocation
  • locals, temps, evaluation stack
  • Loop unroll
  • Dead code elimination
  • define SOMETHING 0 if (SOMETHING gt 10) a
    x // dead code statement
  • Constant and Copy propagation
  • Processor specific code generation

24
4. ExecutionJIT Optimizations
  • Range check elimination

//Range check will be eliminatedfor (int i 0
i lt myArray.Length i) Console.WriteLine(m
yArrayi.ToString()) //Range check will NOT
be eliminatedfor (int i 0 i lt myArray.Length
- y i) Console.WriteLine(myArrayi
x.ToString())
25
Agenda
QA
26
5. Runtime servicesA look at the Garbage
Collector
  • Reference tracking (tracing) GC
  • Large object heap objects over 80k
  • Generational (three gen)
  • Mark sweep and compact

27
Agenda
QA
28
More Information
  • My Blog http//blogs.msdn.com/brada

Applied Microsoft .NET Framework Programming
Shared Source CLI Essentials
Compiling for the .NET CLR
Common Language Infrastructure Annotated Standard

The SLAR
Inside MS .NET IL Assembler
29
Questions?
30
BACKUP
31
New runtime features for V2.0
  • Generics
  • 64 bit (Itanium and x86-64)
  • ReflectionOnly context
  • Delegate Relaxation
  • Lightweight Code Generation
  • NGen/NGen services
  • Stub based dispatch
  • MDbg
  • Edit and Continue
  • BCL Enhancements

32
Generational GC
New Heap Begins with New Generation
Accessible References Keep Objects Alive
Preserves / Compacts Referenced Objects
Objects Left Merge with Older Generation
New Allocations Rebuild New Generation
33
Generational GC
  • Generations Dynamically Tuned
  • CPU Cache Size
  • Acceptable Fragmentation
  • Older Generations are Larger / More Stable
  • Require Collection Less Often
  • Are More Expensive to Collect
  • Can Have References to Newer Objects

34
What is Generics?
  • Type checking, no boxing, no downcasts
  • Increased sharing (typed collections)
  • Instantiated at run-time, not compile-time
  • Work for both reference and value types
  • Code shared for reference types
  • Exact run-time type information

35
What is Generics?
36
C Generics
public class ListltTgt private T elements
private int count public void Add(T
element) if (count elements.Length)
Resize(count 2) elementscount
element public T thisint index
get return elementsindex set
elementsindex value public
int Count get return count
Listltintgt intList new Listltintgt() intList.Add(
1) // No boxing intList.Add(2) // No
boxing intList.Add("Three") // Compile-time
error int i intList0 // No cast required
37
Bits bytes under the hood
  • New metadata tables
  • Flags defines variance and special constraints
  • GenericParamConstraint defines type constraints

38
Implementation choices
  • Code sharing
  • Modula 3 and ML use code sharing
  • Type identity can be a problem
  • Runtime type checking required
  • Code specialization
  • C templates uses specialization
  • Code bloat can be a problem

39
4. ExecutionJIT Optimizations
  • JIT Inlining (method example)

Class Fib Shared Sub NextFib (ByRef i As
Integer, ByRef j As Integer) Dim k As
Integer i j i j j k End
Sub Shared Sub Main() Dim fib1 As
Integer 1 Dim fib2 As Integer 1
Dim n As Integer For n 3 To 36
NextFib (fib1, fib2) Next n End
SubEnd Class
40
4. ExecutionJIT No inline SLOW
push ebp mov ebp,esp sub esp,0Ch
push esi
Prolog
xor eax,eax mov ebp-4,eax mov
ebp-8,eax xor esi,esi nop
Zero vars
mov dword ptr ebp-4,1 mov dword ptr
ebp-8,1 mov esi,3
Init vars
lea ecx,ebp-4 lea edx,ebp-8 call
003E50D4h gt nop
nop
Call NextFib
n
add esi,1 jno 00000009
Overflow Handler
xor ecx,ecx call 764618ED
cmp esi,24h jle FFFFFFE3 nop
Next n
nop pop esi mov esp,ebp
pop ebp ret
Epilog
41
4. ExecutionNextFib() method
push ebp mov ebp,esp sub esp,0Ch push
edi push esi push ebx mov edi,ecx mov
esi,edx xor ebx,ebx nop mov eax,edi add
eax,esi jno 00000009 xor ecx,ecx
call 764618B7 mov ebx,eax mov eax,esi mov
edi,eax mov esi,ebx nop pop ebx pop
esi pop edi mov esp,ebp pop ebp ret
42
4. ExecutionJIT Inlining FAST!
sub esp,8 push edi push esi
Prolog
xor eax,eax mov esp8,eax mov espC,eax
Zero vars
mov esp8,1 mov esp0C,1 mov edi,3
Init vars
lea esi,esp8 lea ecx,espC mov eax,esi
mov edx,ecx add eax,edx jo 16 mov
esi,edx mov ecs,eax
Calc next
add edi,1 jo 0D cmp edi,24h jle
FFFFFFE4
Next n
pop esi pop edi add esp,8 ret
Epilog
Write a Comment
User Comments (0)
About PowerShow.com