CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR...

31
CLR Initialization

Transcript of CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR...

Page 1: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

CLR Initialization

Page 2: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Module Overview

CLR Initialization

Getting to Main Method

PE Layout & CLR Headers

Process Initialization & EE Shim

EE Startup

CLR Artifacts & Loader Heaps

The Managed Object

Common Slow Startups

Review

2

Page 3: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Getting to Main Method

OS Loader

EE Shim

CLR

JIT

Main method

• Process initialization phase

• OS loader finds and load the

Execution Engine Shim

• EE Shim determines

version, bitness, path etc.

• Loads CLR (Mscorwks.dll

/ clr.dll)

• CLR starts up

• Prepares to run

managed code

• JIT gets loaded and

compiles mscorlib.dll

etc.

• Main method finally

runs

3

Page 4: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

MetadataMetadata: Every managed module contains binary

metadata tables

PE Header

CLR Header

IL

Metadata Header

PE Header: Info about exe/dll type

• Machine type (x86/x64)

• CUI/GUI?

• Timestamp etc.,

CLR Header: Info used by CLR

• Version of CLR required

• MetaDataToken of entry Method

• Location and size of metadata, Resources

etc.,

IL: Code produced @ compile time

• IL is a higher-level, CPU independent language

Metadata Header: Contains version of the assembly

• Flags and headers that describe location,

length of tables & heaps within metadata

Portable Executable Layout (EXE/DLL)

4

Page 5: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Metadata Metadata•Every managed module contains

binary metadata tables

PE Header

CLR Header

IL

Metadata Header

Intermediate Language•Code that the compiler produced when

the source is compiled.

•IL is a higher-level, CPU independent

language

Metadata header•Contains version of the assembly

•Contains other flags and headers

which describe location and length of

tables & heaps within the metadata

Physical Layout of a Managed Assembly

Whether it is an exe, dll, ocx etc.

Not of much importance to .Net

CLR version (metadata header)

Localization

Size, resource, module’s entry point e.g., main()

CPU independent language (similar to ByteCode)

Version of assembly

Info about classes defined & classes referenced

Info about module and resources etc.

Page 6: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

PE Layout

Raw Output

dumpbin.exe /headers <EXE>

HEX disassembly of <EXE>

FILE HEADER VALUES

14C machine (x86)

3 number of sections

4C26E437 time date stamp Sun Jun 27

01:40:07 2010

0 file pointer to symbol table

0 number of symbols

E0 size of optional header

102 characteristics

Executable

32 bit word machine

OPTIONAL HEADER VALUES

10B magic # (PE32)

8.00 linker version

24000 size of code

2000 size of initialized data

0 size of uninitialized data

254BE entry point (110254BE)

2000 base of code

26000 base of data

2A000 size of image

1000 size of headers

0 checksum

2 subsystem (Windows GUI)

6

Page 7: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

CLR Header

Within PE Layout

How to find Main method?

Raw output from ildasm.exe

----- CLR Header:

Header size: 0x00000048

Major runtime version: 0x0002

Minor runtime version: 0x0005

0x00021de0 [0x00003624] address [size] of Metadata Directory:

Flags: 0x00000001

Entry point token: 0x06000053

TypeDef #10 (0200000b)

-------------------------------------------------------

TypDefName: NetDebugDemos.Startup (0200000B)

Flags : [Public] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit] (00100001)

Extends : 01000002 [TypeRef] System.Object

Method #2 (06000053) [ENTRYPOINT]

-------------------------------------------------------

MethodName: Main (06000053)

Flags : [Private] [Static] [HideBySig] [ReuseSlot] (00000091)

RVA : 0x0000377f

ImplFlags : [IL] [Managed] (00000000)

CallCnvntn: [DEFAULT]

ReturnType: Void

No arguments.

7

Page 8: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Demo: Inspect PE Layout

PE Headers

CLR Headers

Page 9: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Process Initialization

Loading EE Shim

OS Loader finds process’ entry point

Transfer control to Execution Engine (EE) shim loader (mscoree.dll)

EE Shim determines version, bitness, path of CLR

Uses default Concurrent GC unless config file or

CorBindToRuntimeHost is used e.g. ASP.NET

(Background GC/ CLRCreateInstance for 4.0)0:000> kL

Child-SP RetAddr Call Site

00000000`0012f820 000007fe`f9121812 KERNELBASE!LoadLibraryExW+0x19c

00000000`0012f8b0 000007fe`f9123cf8 mscoreei!RuntimeDesc::LoadLibrary+0x82

00000000`0012fa20 000007fe`f9123ca9 mscoreei!RuntimeDesc::LoadMainRuntimeModuleHelper+0x4a

00000000`0012fa50 000007fe`f9123a85 mscoreei!RuntimeDesc::LoadMainRuntimeModule+0x4a

00000000`0012fae0 000007fe`f9123962 mscoreei!RuntimeDesc::EnsureLoaded+0xa5

00000000`0012fb70 000007fe`f9123904 mscoreei!RuntimeDesc::GetProcAddressInternal+0x22

00000000`0012fba0 000007fe`f9123450 mscoreei!CLRRuntimeInfoImpl::GetProcAddress+0x5c

00000000`0012fc30 000007fe`f91232f1 mscoreei!GetCorExeMainEntrypoint+0x161

00000000`0012fcf0 000007fe`f91b5b21 mscoreei!_CorExeMain+0x31

00000000`0012fd20 00000000`76aff56d MSCOREE!_CorExeMain_Exported+0x57

00000000`0012fd50 00000000`770d2cc1 KERNEL32!BaseThreadInitThunk+0xd

00000000`0012fd80 00000000`00000000 ntdll!RtlUserThreadStart+0x1d

clr.dll /

mscorwks.dll

9

Page 10: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

EE Startup

Majority of CLR initialization done here

Initializes ETW tracing, GC & Loader Heaps

Creates System, Shared & Default domain(s)

Loads mscorlib.dll & creates fundamental types e.g. System.Object

0:000> kL

Child-SP RetAddr Call Site

00000000`0022fac0 000007fe`f89b629e clr!EEStartup+0x16

00000000`0022fb00 000007fe`f89cd62e clr!EnsureEEStarted+0xe6

00000000`0022fb70 000007fe`f896ba45 clr!_CorExeMainInternal+0x7f

00000000`0022fbe0 000007fe`f9123309 clr!_CorExeMain+0x15

00000000`0022fc20 000007fe`f91b5b21 mscoreei!_CorExeMain+0x41

00000000`0022fc50 00000000`76aff56d MSCOREE!_CorExeMain_Exported+0x57

00000000`0022fc80 00000000`770d2cc1 KERNEL32!BaseThreadInitThunk+0xd

00000000`0022fcb0 00000000`00000000 ntdll!RtlUserThreadStart+0x1d

10

Page 11: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Demo:Break on EE Startup

clr!EEStartup

Page 12: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

CLR Artifacts

Internal data structures to track objects

EEClass

- “Mold” for every unique type

- Describes a type’s field & assembly from etc.

MethodTable

- EEClass + MethodTable = CLR’s internal representation of objects

MethodDesc

- CLR’s internal structure to track methods in a type

- E.g. tracks state of a method if Jitted or not

Note

Class = design time

Type = runtime

12

Page 13: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

AppDomain

What is it?

Can be thought of as “process within process”

“Sandboxing” code for isolation purposes

Cost is “cheaper” than creating a new process

Types of AppDomains

- System Domain

• Responsible creating shared & default domain

- Shared Domain

• Domain neutral code lives here

- Default Domain

• Application code gets loaded & run here

13

Page 14: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

The .NET Runtime in a Process (CLR)

e.g. w3wp.exe

System

Domain

Shared

Domain

AppDomain (!dumpdomain)

AppDomain

#1

AppDomain

#2

14

Page 15: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

CLR: Assembly & Module

w3wp.exe

Assembly/Module(!dumpassembly/!dumpmodule)

System

Domain

Shared

Domain

Assembly

(Foo1.dll)

Assembly

(Foo2.dll)

Assembly

(Foo3.dll)

AppDomain (!dumpdomain)

AppDomain

#1

AppDomain

#2

15

Page 16: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

CLR: EEClass

w3wp.exe

Assembly/Module(!dumpassembly/!dumpmodule)

System

Domain

Shared

Domain

Assembly

(Foo1.dll)

Assembly

(Foo2.dll)

Assembly

(Foo3.dll)

AppDomain (!dumpdomain)

EEClass (!dumpclass)

EEClass

(Class3)

EEClass

(Class2)

EEClass

(Class1)

AppDomain

#1

AppDomain

#2EEClass

(Class4)

16

Page 17: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

CLR: MethodTable

w3wp.exe

Assembly/Module(!dumpassembly/!dumpmodule)

System

Domain

Shared

Domain

Assembly

(Foo1.dll)

Assembly

(Foo2.dll)

Assembly

(Foo3.dll)

AppDomain (!dumpdomain)

EEClass (!dumpclass)

EEClass

(Class3)

EEClass

(Class2)

EEClass

(Class1)

AppDomain

#1

AppDomain

#2EEClass

(Class4)

MethodTable (!dumpmt)

17

Page 18: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

CLR: MethodDesc

w3wp.exe

Assembly/Module(!dumpassembly/!dumpmodule)

System

Domain

Shared

Domain

Assembly

(Foo1.dll)

Assembly

(Foo2.dll)

Assembly

(Foo3.dll)

AppDomain (!dumpdomain)

EEClass (!dumpclass)

EEClass

(Class3)

EEClass

(Class2)

EEClass

(Class1)

AppDomain

#1

AppDomain

#2EEClass

(Class4)

MethodTable (!dumpmt)

MethodDesc (FunctionOne)

MethodDesc (FunctionTwo)

MethodDesc (FunctionThree)

18

Page 19: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Multi-Domain ApplicationsLoaderOptimizationAttribute or

AppDomainSetup.LoaderOptimization

SingleDomain

• No Assembly sharing between Domains

• Ngen´ed Image gets used only for the first Domain

• Increased Memory Usage, increased Application Load Time, unexpected

Jitting

• Default behavior – “safest” way

MultiDomain

• As much as possible will be loaded Domain Neutral.

• LoaderOptimizationAttribute on Main()

– No real “control” over loading possible anymore

– Better: AppDomainSetup usage

MultiDomainHost –

• Only Assemblies within the GAC will be shared

• Eg: ASP.NET

Page 20: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Loader Heaps

Store CLR Artifacts

What are CLR artifacts?

- E.g. EEClass, MethodTable & MethodDesc

Common Loader Heaps

- HighFrequencyHeap

• MethodTable, MethodDesc & FieldDesc

- LowFrequencyHeap

• EEClass

0:005> !eeheap -loader

Loader Heap:

--------------------------------------

System Domain: 000007fef9065710

LowFrequencyHeap: 000007ff00010000(2000:1000) Size: 0x1000 (4096) bytes.

HighFrequencyHeap: 000007ff00012000(8000:1000) Size: 0x1000 (4096) bytes.

StubHeap: 000007ff0001a000(2000:2000) Size: 0x2000 (8192) bytes.

20

Page 21: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Demo:Loader Heap & AppDomain

Load SOS

System, Shared & Default domain

Loader Heap Sizes

Page 22: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

The Managed Object

Tying it all together!

Chunk of memory with instance data

Lives in GC Heap i.e. separate from all other CLR artifacts

CLR identifies its type through EEClass, MethodTable etc.

System.Object = basic building block for all other managed objects

An object in memory needs to keep info such as:

- Type

- Instance contents (fields)

- Other data structures bound to objects like hash codes & locks

22

Page 23: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Demo:Managed objects

MethodTable - dd <objadd> L1

!DumpObj

• String

• Array

• Object Properties

Page 24: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Slow Startup

Common Scenarios

Cold Startup

- Slow disk I/O

Warm Startup

- Many dynamic assemblies e.g. ASP.NET & Serializers

- Large cache initialization

- Contentions during warm startup

24

Page 25: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Identify Slow Startups

1st Run

after boot?

Re-run app to

ensure not cold

startup issue

Collect perf counters

&/or profiler traces to

further isolate issue

Slow

Startup?

Collect ETW

traces on I/O

performance

Yes

No

Yes No

25

Page 26: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Performance Counters

Which counters to look at?

.NET CLR Jit

- # of IL Bytes Jitted

- # of Methods Jitted

- % Time in Jit

.NET CLR Loading

- Bytes in Loader Heap

LogicalDisk

- % Idle Time

- Current Disk Queue Length

26

Page 27: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

ETW Trace

Disk I/O Performance

1. xperf -on

PROC_THREAD+LOADER+CSWITCH+FILENAME+FILE_IO+FILE_IO

_INIT+DRIVERS -f kernel.etl -stackwalk

CSwitch+DiskReadInit+DiskWriteInit+DiskFlushInit+FileCreate+FileCle

anup+FileClose+FileRead+FileWrite -BufferSize 1024 -MaxBuffers

1024 -MaxFile 1024 -FileMode Circular

2. <Reproduce issue>

3. xperf -d ColdStartupIssue.etl

27

Page 28: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Review

1. Name the different parts within the layout of a managed assembly.

2. What are AppDomains, MethodTables, and MethodDescs?

3. Name the SOS command that obtains information from each of these.

28

Page 29: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Reference

Microsoft PE and COFF Specification

- http://msdn.microsoft.com/en-us/windows/hardware/gg463125

An In-Depth Look into the Win32 Portable Executable File Format Part 2

- http://msdn.microsoft.com/en-us/magazine/cc301808.aspx

Drill Into .NET Framework Internals to See How the CLR Creates

Runtime Objects

- http://msdn.microsoft.com/en-us/magazine/cc163791.aspx

A Model for cold startup time of an application on Windows

- http://blogs.msdn.com/b/vancem/archive/2007/04/09/a-model-for-cold-startup-

time-of-an-application-on-windows.aspx

NGen: Measuring Warm Startup Performance with Xperf

- http://blogs.msdn.com/b/clrcodegeneration/archive/2010/04/27/ngen-

measuring-warm-startup-performance-with-xperf.aspx

29

Page 30: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

Reference (Cont.)

Windows Performance Analysis Tools

- http://msdn.microsoft.com/en-us/performance/cc825801.aspx

30

Page 31: CLR Initialization - Microsoft Azuredebugdiagtraining.azurewebsites.net/slides/02_Intro_CLR...CLR JIT Main method • Process initialization phase • OS loader finds and load the

© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.

The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market

conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation.

MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

31