Title: Introduction to Common Language Runtime Profiling
1Introduction to Common Language Runtime Profiling
- Brian Dowds
- Microsoft Visual C .NET
- Support Team
2The Dilemma
- Win32 and COM programming benefits came with
support costs - Limited ability to peek inside a process to
investigate runtime problems - Crashes, hangs, heap corruption
- Memory leaks, mismatched reference counts
- Build-your-own instrumentation
- Detours technology, many, many printf calls,
custom crash handlers
3The Remedy
- Microsoft .NET Common Language Runtime Profiling
Interfaces - Create one COM object to profile any .NET managed
application - Common language runtime provides dozens of
built-in metrics about runtime behavior
4Agenda
- Brief discussion of the common language runtime
- Details about the profiling interfaces
- Inside a profiler object
- Details of profiling managed code
- Examples using .NET SDK samples
- Summary
- QA
5Common Language Runtime in Brief
- Provides many services to managed applications
- Automatic memory management
- Security, interoperability with unmanaged code
- Cross-language debugging support
- Garbage collection for managed resources
- Common type system
- Interoperation with classic COM
6Common Language Runtime
- Offers easy access to runtime information through
two new interfaces - Use them with a profiling object that implements
one interface, and calls methods on the other - Collect runtime information about your
application from up to 58 new runtime events
7Common Language Runtime Profiling Diagram
Common Language Runtime
Managed Code Application
ICorProfilerInfo
Profiling Object
ICorProfilerCallBack
Log File
8The Profiling Interfaces
- ICorProfilerInfo
- Instantiated by the common language runtime
- Available from IUnknown pointer returned through
your profilers Initialize method - 33 methods provide detailed information about the
target process - ICorProfilerCallback
- You implement it in your profiler
- Common language runtime calls methods and passes
in data about particular runtime events
9The Profiling Interfaces (2)
- Defined in Corprof.IDL
- See Profiling.doc in the .NET Tool Developers
Guide for full details(Program Files\Microsoft
Visual Studio .NET\FrameworkSDK\Tool Developers
Guide\docs)
10Inside a Profiler Object
- Served up by a classic COM in-proc server
- Yes, its un-managed code.
- Profiler object inherits from and implements all
58 methods of ICorProfilerCallback. - Common language runtime calls obtain an IUnknown
pointer to ICorProfilerInfo through the
Initialize method.
11Inside a Profiler Object (2)
- Profiler calls QueryInterface to obtain an
ICorProfilerInfo pointer, and stores it for use. - Profiler then calls methods on ICorProfilerInfo
pointer to learn details about common language
runtime events. - Great example profilers available
- Matt Pietreks DNProfiler from MSDN December,
2001 Under The Hood - HST_Profiler and GCP_Profiler from the .NET Tool
Developers Guide samples
12DNProfiler Code Snippet
- HRESULT CProfilerCallbackClassLoadFinished(UINT
classId, HRESULT hrStatus) -
- ChangeNestingLevel( -1 )
- ProfilerPrintf( "ClassLoadFinished\n" )
- return E_NOTIMPL
13SDK HSTProfiler Code Snippet
- void ProfilerCallbackEnter( FunctionID
functionID ) -
- TimeTracker timer( static_castltPrfInfo gt(
g_pCallbackObject ) ) - try
-
- g_pCallbackObject-gtUpdateCallStack(
functionID, PUSH ) -
- catch ( BaseException exception )
-
- exception-gtReportFailure()
- delete exception
- g_pCallbackObject-gtFailure()
-
- // ProfilerCallbackEnter
14ICorProfilerCallback
- At run time, common language runtime calls
methods on your implementation of
ICorProfilerCallback. - Implementation complexity varies, depending on
requirements - Can be as basic as returning E_NOTIMPL
- Bulk up a method with code to call every
appropriate method of ICorProfileInfo
15ICorProfilerInfo
- Obtain details about classes, threads, functions,
stack frames, and so on. - Currently, there are 33 methods available to a
profiler. See Profiling.doc in the tool
developers guide for full documentation. - Return values are either S_OK or CORPROF_E_ as
defined in CorError.h
16Often-Called ICorProfilerInfo Methods
- GetModuleInfo returns moduleID, module name,
ModuleToken, and AssemblyID, among other data. - GetThreadInfo returns a pointer to the Win32
thread ID for a specific thread. - SetEventMask indicates the event categories that
are interesting to the Profiler. Use this to
limit callbacks from the common language runtime
to selected categories.
17Visual Basic .NET and Visual C Profiler Objects
- Not possible to create a profiler in these
languages - Managed code cant profile managed code
- Why?
18Details of Profiling Managed Code
- Environment variables control profiling
- set Cor_Enable_Profiling0x1 for ON
- set Cor_Enable_Profiling0x0 for OFF
- Specify which profiler to use
- set COR_PROFILER
- GUID_OF_MY_PROFILER
- set COR_PROFILERMyProfilerProgID
19Remote Profiling
- Remote profiling is currently not supported
- Profiler object must be located on the target
machine - Must register at least ICorProfilerCallback
interface with the common language runtime on the
local machine - Tool developers can create collector agents
that run on remote systems to batch profiling data
20Is it Possible to Profile Unmanaged Code?
- Minimal support provided
- Enumerate callstack chains with ICorDebugChain
methods - Discover whether callstack belongs to managed or
unmanaged code with ICorDebugChainIsManaged
21Profiling Examples
- Output generated by two sample Profilers
- Matt Pietreks DNProfiler object
- SDK Sample HSTProfiler
- .NET Platform SDK samples used
- MDI
- HelloWorldControl
22MDI Profile Output
- JITCompilationStarted
- System.Runtime.Remoting.Proxies.ProxyAttribute.
ctor - JITCompilationFinished
- ObjectAllocated ArrayListEnumeratorSimple
- ObjectAllocated System.String
- ManagedToUnmanagedTransition WndProcInvoke
- UnmanagedToManagedTransition System.Windows.Forms
.UnsafeNativeMethods - SendMessage
23HelloWorldControl Profile Output
- Thread ID 0x000007ec
- Functions Type and Name
- bool System.Windows.Forms.ControlContains(
System.Windows.Forms.Control ) - Times Called 5
- Exclusive Time 0.000017
- Inclusive Time 0.000311
- Callee Time 0.000099
- Suspended Time 0.000000
- Profiler Time 0.000195
24Summary
- Brief discussion of the common language runtime
- Details about the profiling interfaces
- Inside a profiler object
- Details of profiling managed code
- Examples using .NET SDK samples
25Where to Get More Information
- Two great documents from the Tool Developers
Guide that ships with the .NET Framework SDK - Profile.doc
- Debugref.doc
26- Thank you for joining us for todays Microsoft
Support - WebCast.
- For information about all upcoming Support
WebCasts - and access to the archived content (streaming
media - files, PowerPoint slides, and transcripts),
please visit - http//support.microsoft.com/webcasts/
- We sincerely appreciate your feedback. Please
send any - comments or suggestions regarding the Support
- WebCasts to feedback_at_microsoft.com and include
- Support WebCasts in the subject line.