net profilers and il rewriting - ddd melbourne 2

107
.NET Profilers aren’t scary [email protected] [email protected] @scubamunki Email: Email: Twitter:

Upload: shaun-wilde

Post on 04-Nov-2014

17 views

Category:

Technology


5 download

DESCRIPTION

A presentation on writing your own .NET profiler to use for IL rewriting as presented at DDD Melbourne 2.Uses ATL (C++) and code from OpenCover.

TRANSCRIPT

Page 1: NET Profilers and IL Rewriting - DDD Melbourne 2

.NET Profilers aren’t scary

[email protected] [email protected]@scubamunki

Email:Email:

Twitter:

Page 2: NET Profilers and IL Rewriting - DDD Melbourne 2

Who am I?

[email protected] [email protected]@scubamunki

Email:Email:

Twitter:

Specialize in providing project teams with the necessary frameworks and personnel during the critical early stages of the project.

Page 3: NET Profilers and IL Rewriting - DDD Melbourne 2

CoverageEye.NET◦ Originally hosted on GotDotNet

PartCover◦ Originally hosted on SourceForge◦ Forked to GitHub

https://github.com/sawilde/partcover.net4

OpenCover◦ Hosted on GitHub

https://github.com/sawilde/opencover

How did I get here?

Page 4: NET Profilers and IL Rewriting - DDD Melbourne 2

Profilers: What are they good for?◦ Quick review of some commercial and open

source .NET profilers Overview of the profiler API Code and Demos Basics of IL rewriting Code and Demos II Introducing new classes and methods Code and Demos III Extras (If time permits)

.NET Profilers aren’t (that) scary

Page 5: NET Profilers and IL Rewriting - DDD Melbourne 2

Profilers: What are they good for?

Page 6: NET Profilers and IL Rewriting - DDD Melbourne 2

Monitor Load/Unload Events◦ Application Domains◦ Assemblies◦ Modules◦ Classes

JIT Compilation Events Monitor threads, remoting and

native/managed transitions Monitor GC events Monitor exceptions Monitor method enter/leave

Monitor the runtime

Page 7: NET Profilers and IL Rewriting - DDD Melbourne 2

Get the names and signatures of classes and methods

Read the IL for methods (functions) Memory allocations

◦ Types◦ Garbage Collection

Call graphs

Interrogate the runtime

Page 8: NET Profilers and IL Rewriting - DDD Melbourne 2

Dynamically create new classes and methods

Rewrite the IL of existing methods Allocate memory Work with the GC

Interact with the runtime

Page 9: NET Profilers and IL Rewriting - DDD Melbourne 2

Commercial

Redgate ANTS

NCover (was originally Open Source)

JetBrains dotCover/dotTrace

CLRProfiler4 (Microsoft with Source Code)

Commercial and Open Source Profilers*

(*Obviously this list is not complete)

Page 10: NET Profilers and IL Rewriting - DDD Melbourne 2

Open Source

Nprof http://code.google.com/p/nprof/

SlimTune http://code.google.com/p/slimtune/

PartCover https://github.com/sawilde/partcover.net4

OpenCover https://github.com/sawilde/opencover

Commercial and Open Source Profilers*

(*Obviously this list is not complete)

Page 11: NET Profilers and IL Rewriting - DDD Melbourne 2

System.Reflection.Emit◦ .NET Framework

Mono.Cecil◦ https://github.com/mono/cecil

Other IL (Re)Writing Methods

Page 12: NET Profilers and IL Rewriting - DDD Melbourne 2

Important Interfaces Implementation and Registration

Overview of the Profiler API

Page 13: NET Profilers and IL Rewriting - DDD Melbourne 2

In-process COM object◦ i.e. a DLL

Native code◦ i.e. unmanaged code

Free threaded◦ i.e. the developer is responsible for any required

synchronisation 32/64 bit

◦ Like for like

Implementation

Page 14: NET Profilers and IL Rewriting - DDD Melbourne 2

I Cor Pr ofi l er I nf o2/ 3

I Unknown

Runt i me

I Cor Pr ofi l er Cal l back2/ 3

I Unknown

Pr ofi l er

Tar get Appl i cat i on

Launchi ng Appl i cat i onCOR_ENABLE_PROFI LI NG=1

COR_PROFI LER=<CLSI D or PROGI D of Pr ofi l er >

Cust om Appl i cat i on

Command Li ne

Page 15: NET Profilers and IL Rewriting - DDD Melbourne 2

.NET Runtime through the ages

Page 16: NET Profilers and IL Rewriting - DDD Melbourne 2

ICorProfilerCallback◦ 69 Methods

ICorProfilerInfo◦ 33 Methods

IMetaDataImport◦ 64 Methods

IMetaDataEmit◦ 49 Methods

.NET1

Page 17: NET Profilers and IL Rewriting - DDD Melbourne 2

ICorProfilerCallback2◦ 8 Methods

ICorProfilerInfo2◦ 21 Methods

IMetaDataImport2◦ 64 Methods

IMetaDataEmit2◦ 49 Methods

.NET2

Page 18: NET Profilers and IL Rewriting - DDD Melbourne 2

ICorProfilerCallback3◦ 3 Methods

ICorProfilerInfo3◦ 14 Methods

.NET4

Page 19: NET Profilers and IL Rewriting - DDD Melbourne 2

Methods

How much?

Page 20: NET Profilers and IL Rewriting - DDD Melbourne 2

Register the COM Object

Use Environment variables

c:>set COR_ENABLE_PROFILING=1c:>set COR_PROFILER=MyProfiler.Profiler

Instantiation

Page 21: NET Profilers and IL Rewriting - DDD Melbourne 2

NET4 profilers can be used to profile .NET2 assemblies

Need to be side-by-side CLR awarePick onePick firstPick all/many

◦ RunSxS http://archive.msdn.microsoft.com/RunSxS

.NET4

Page 22: NET Profilers and IL Rewriting - DDD Melbourne 2

Silverlight support

CORECLR_ENABLE_PROFILINGCORECLR_PROFILERCORECLR_PROFILER_PATH

.NET4

Page 23: NET Profilers and IL Rewriting - DDD Melbourne 2

Creating an ATL COM object with required interfaces

Demo 1

Page 24: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 25: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 26: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 27: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 28: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 29: NET Profilers and IL Rewriting - DDD Melbourne 2

Create host and target processes

Launch Target with Profiler

Simple Target

Demo 2

Page 30: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 31: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 32: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 33: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 34: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 35: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 36: NET Profilers and IL Rewriting - DDD Melbourne 2

Requesting and handling events◦ ICorProfilerInfo::SetEventMask

COR_PRF_MONITOR_MODULE_LOADS

COR_PRF_MONITOR_JIT_COMPILATION

COR_PRF_DISABLE_INLINING

COR_PRF_DISABLE_OPTIMIZATIONS

Demo 3

Page 37: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 38: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 39: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 40: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 41: NET Profilers and IL Rewriting - DDD Melbourne 2

Getting assembly and method names

Interfaces◦ ICorProfilerInfo3◦ IMetaDataImport2

Demo 4

Page 42: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 43: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 44: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 45: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 46: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 47: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 48: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 49: NET Profilers and IL Rewriting - DDD Melbourne 2

What is a Method?◦ Headers◦ Sections◦ Clauses

The Method Body◦ Operations◦ Branches◦ The Stack◦ Debug vs. Release

Basics of IL Rewriting

Page 50: NET Profilers and IL Rewriting - DDD Melbourne 2

Tiny and Fat Methods

1 0

CODE SIZE TYPE

Method with Tiny Header

Page 51: NET Profilers and IL Rewriting - DDD Melbourne 2

HEAD

ERM

ETHO

DSECTIO

NS

Method with Fat Header

Page 52: NET Profilers and IL Rewriting - DDD Melbourne 2

*

CODE SIZE

TYPE

LOCAL VARIABLES SIGNATURE TOKEN

HEADER SIZE FLAGS

0 0 1 1 * 1 1

MAX STACK

MO

RE SECTION

S

INITIALIZE LO

CAL VARIABLES

Page 53: NET Profilers and IL Rewriting - DDD Melbourne 2

Tiny and Fat Sections

EH TABLE

FAT FO

RMAT

MO

RE SECTIO

NS

* 0 1

KINDDATA SIZE RESERVED

Tiny Section

Page 54: NET Profilers and IL Rewriting - DDD Melbourne 2

EH TABLE

FAT FO

RMAT

MO

RE SECTIO

NS

* 1 1

KIND DATA SIZE

Fat Section

Page 55: NET Profilers and IL Rewriting - DDD Melbourne 2

Tiny and Fat Clauses

FLAGS

TRY OFFSET

TRY LENGTH

HANDLEROFFSET

HANDLERLENGTH

CLASSTOKEN/OFFSET

TINY CLAUSE

Page 56: NET Profilers and IL Rewriting - DDD Melbourne 2

FLAGS

TRY OFFSET

TRY LENGTH

HANDLEROFFSET

HANDLERLENGTH

CLASSTOKEN/OFFSET

FAT CLAUSE

Page 57: NET Profilers and IL Rewriting - DDD Melbourne 2

5 Types of Clauses◦ COR_ILEXCEPTION_CLAUSE_NONE◦ COR_ILEXCEPTION_CLAUSE_FILTER◦ COR_ILEXCEPTION_CLAUSE_FINALLY◦ COR_ILEXCEPTION_CLAUSE_FAULT◦ COR_ILEXCEPTION_CLAUSE_DUPLICATED

Clauses

Page 58: NET Profilers and IL Rewriting - DDD Melbourne 2

Try/Catch Code in handler block is called if an

exception of the type expected is thrown from code that is contained in the try block. (needs rewording)

COR_ILEXCEPTION_CLAUSE_NONE

Page 59: NET Profilers and IL Rewriting - DDD Melbourne 2

Variation on Try/Catch VB.NET only

COR_ILEXCEPTION_CLAUSE_FILTER

Page 60: NET Profilers and IL Rewriting - DDD Melbourne 2

Try/Finally Code in handler block always called

regardless of how the associated try block is exited.

COR_ILEXCEPTION_CLAUSE_FINALLY

Page 61: NET Profilers and IL Rewriting - DDD Melbourne 2

Try/Fault Code in handler block is only called if an

exception has occurred in the corresponding try block.

The exception continues on… IL Only?

COR_ILEXCEPTION_CLAUSE_FAULT

Page 62: NET Profilers and IL Rewriting - DDD Melbourne 2

A Mystery

// duplicated clause.. this clause was duplicated down to a funclet which was pulled out of line

COR_ILEXCEPTION_CLAUSE_DUPLICATED

Page 63: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 64: NET Profilers and IL Rewriting - DDD Melbourne 2

The Method Body◦ Operations◦ Branches◦ The Stack◦ Debug vs. Release

The Method Body

Page 65: NET Profilers and IL Rewriting - DDD Melbourne 2

Contains all the information needed to interpret and write IL◦ Canonical Name◦ String Name◦ Stack Behaviour◦ Parameter Size◦ Length◦ Bytes◦ Control Flow

OPCODE.DEF

Page 66: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 67: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 68: NET Profilers and IL Rewriting - DDD Melbourne 2

OPCODE.DEF

Page 69: NET Profilers and IL Rewriting - DDD Melbourne 2

OPCODE.DEF

Page 70: NET Profilers and IL Rewriting - DDD Melbourne 2

Is there a difference?

Debug vs. Release

Page 71: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 72: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 73: NET Profilers and IL Rewriting - DDD Melbourne 2

Headers◦ Stack size◦ Method Size

Clauses◦ Offsets◦ Lengths

Branches◦ Size of Jump

Considerations when adding IL

Page 74: NET Profilers and IL Rewriting - DDD Melbourne 2

Make …◦ Tiny headers Fat◦ Tiny sections Fat◦ Tiny clauses Fat◦ Short branches Long

Cheat…◦ Use the parser from OpenCover

Common approach when adding IL

Page 75: NET Profilers and IL Rewriting - DDD Melbourne 2

Leverage the OpenCover parser

Demo 5

Page 76: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 77: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 78: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 79: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 80: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 81: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 82: NET Profilers and IL Rewriting - DDD Melbourne 2

Add a new Type◦ Exception◦ Define Constructor (.ctor)

Throw new Type◦ Add extra IL to TargetMethod

Adding new classes and methods I

Page 83: NET Profilers and IL Rewriting - DDD Melbourne 2

Interfaces◦ IMetaDataEmit2◦ IMetaDataAssemblyEmit

Adding New Classes and Methods

Page 84: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 85: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 86: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 87: NET Profilers and IL Rewriting - DDD Melbourne 2

IL DASM

Page 88: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 89: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 90: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 91: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 92: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 93: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 94: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 95: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 96: NET Profilers and IL Rewriting - DDD Melbourne 2

Complicated Intensive

Hmmmm……

Page 97: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 98: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 99: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 100: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 101: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 102: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 103: NET Profilers and IL Rewriting - DDD Melbourne 2

Sequence Points◦ PDB Files

IL Rewriting◦ Points out of sync

Debugging

Page 104: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 105: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 106: NET Profilers and IL Rewriting - DDD Melbourne 2
Page 107: NET Profilers and IL Rewriting - DDD Melbourne 2

The End

Demo code◦ https://github.com/sawilde/DDD2011_ProfilerDemo