.NET Profilers aren’t scaryshaun.wilde@commonvision.com.aushaun_wilde@hotmail.com@scubamunkiEmail:Email:Twitter:
Who am I?Specialize in providing project teams with the necessary frameworks and personnel during the critical early stages of the project.shaun.wilde@commonvision.com.aushaun_wilde@hotmail.com@scubamunkiEmail:Email:Twitter:
CoverageEye.NETOriginally hosted on GotDotNetPartCoverOriginally hosted on SourceForgeForked to GitHubhttps://github.com/sawilde/partcover.net4OpenCoverHosted on GitHubhttps://github.com/sawilde/opencoverHow did I get here?
Profilers: What are they good for?Quick review of some commercial and open source .NET profilersOverview of the profiler APICode and DemosBasics of IL rewritingCode and Demos IIIntroducing new classes and methodsCode and Demos IIIExtras (If time permits).NET Profilers aren’t (that) scary
Profilers: What are they good for?
Monitor Load/Unload EventsApplication DomainsAssembliesModulesClassesJIT Compilation EventsMonitor threads, remotingand native/managed transitionsMonitor GC eventsMonitor exceptionsMonitor method enter/leaveMonitor the runtime
Get the names and signatures of classes and methodsRead the IL for methods (functions)Memory allocationsTypesGarbage CollectionCall graphsInterrogate the runtime
Dynamically create new classes and methodsRewrite the IL of existing methodsAllocate memoryWork with the GCInteract with the runtime
CommercialRedgate ANTSNCover(was originally Open Source)JetBrainsdotCover/dotTraceCLRProfiler4 (Microsoft with Source Code)Commercial and Open Source Profilers*(*Obviously this list is not complete)
Open SourceNprofhttp://code.google.com/p/nprof/SlimTunehttp://code.google.com/p/slimtune/PartCoverhttps://github.com/sawilde/partcover.net4OpenCoverhttps://github.com/sawilde/opencoverCommercial and Open Source Profilers*(*Obviously this list is not complete)
System.Reflection.Emit.NET FrameworkMono.Cecilhttps://github.com/mono/cecilOther IL (Re)Writing Methods
Important InterfacesImplementation and RegistrationOverview of the Profiler API
In-process COM objecti.e. a DLLNative codei.e. unmanaged codeFree threadedi.e. the developer is responsible for any required synchronisation 32/64 bitLike for likeImplementation
.NET Runtime through the ages
ICorProfilerCallback69 MethodsICorProfilerInfo33 MethodsIMetaDataImport64 MethodsIMetaDataEmit49 Methods.NET1
ICorProfilerCallback28 MethodsICorProfilerInfo221 MethodsIMetaDataImport264 MethodsIMetaDataEmit249 Methods.NET2
ICorProfilerCallback33 MethodsICorProfilerInfo314 Methods.NET4
How much?
Register the COM ObjectUse Environment variablesc:>set COR_ENABLE_PROFILING=1c:>set COR_PROFILER=MyProfiler.ProfilerInstantiation
NET4 profilers can be used to profile .NET2 assemblies
Need to be side-by-side CLR awarePick onePick firstPick all/manyRunSxShttp://archive.msdn.microsoft.com/RunSxS.NET4
Silverlight supportCORECLR_ENABLE_PROFILINGCORECLR_PROFILERCORECLR_PROFILER_PATH.NET4
Creating an ATL COM object with required interfacesDemo 1
Create host and target processesLaunch Target with ProfilerSimple TargetDemo 2
Requesting and handling eventsICorProfilerInfo::SetEventMaskCOR_PRF_MONITOR_MODULE_LOADSCOR_PRF_MONITOR_JIT_COMPILATIONCOR_PRF_DISABLE_INLININGCOR_PRF_DISABLE_OPTIMIZATIONSDemo 3
Getting assembly and method namesInterfacesICorProfilerInfo3IMetaDataImport2Demo 4
What is a Method?HeadersSectionsClausesThe Method BodyOperationsBranchesThe StackDebug vs. ReleaseBasics of IL Rewriting
Tiny and Fat Methods
Tiny and Fat Sections
Tiny and Fat Clauses
5 Types of ClausesCOR_ILEXCEPTION_CLAUSE_NONECOR_ILEXCEPTION_CLAUSE_FILTERCOR_ILEXCEPTION_CLAUSE_FINALLYCOR_ILEXCEPTION_CLAUSE_FAULTCOR_ILEXCEPTION_CLAUSE_DUPLICATEDClauses
Try/CatchCode 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
Variation on Try/CatchVB.NET onlyCOR_ILEXCEPTION_CLAUSE_FILTER
Try/FinallyCode in handler block always called regardless of how the associated try block is exited.COR_ILEXCEPTION_CLAUSE_FINALLY
Try/FaultCode 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
A Mystery// duplicated clause..  this clause was duplicated down to a funclet which was pulled out of lineCOR_ILEXCEPTION_CLAUSE_DUPLICATED
The Method BodyOperationsBranchesThe StackDebug vs. ReleaseThe Method Body
Contains all the information needed to interpret and write  ILCanonical NameString NameStack BehaviourParameter SizeLengthBytesControl FlowOPCODE.DEF
OPCODE.DEF
OPCODE.DEF
Is there a difference?Debug vs. Release
HeadersStack sizeMethod SizeClausesOffsetsLengthsBranchesSize of JumpConsiderations when adding IL
Make …Tiny headers FatTiny sections FatTiny clauses FatShort branches Long Cheat…Use the parser from OpenCoverCommon approach when adding IL
Leverage the OpenCover parserDemo 5
Add a new TypeExceptionDefine Constructor (.ctor)Throw new TypeAdd extra IL to TargetMethodAdding new classes and methods I
InterfacesIMetaDataEmit2IMetaDataAssemblyEmitAdding New Classes and Methods
IL DASM
ComplicatedIntensiveHmmmm……
Sequence PointsPDB FilesIL RewritingPoints out of syncDebugging

.NET Profilers and IL Rewriting - DDD Melbourne 2