.NET Profilers aren’t scary<br />shaun.wilde@commonvision.com.au<br />shaun_wilde@hotmail.com<br />@scubamunki<br />Email:...
Who am I?<br />Specialize in providing project teams with the necessary frameworks and personnel during the critical early...
CoverageEye.NET<br />Originally hosted on GotDotNet<br />PartCover<br />Originally hosted on SourceForge<br />Forked to Gi...
Profilers: What are they good for?<br />Quick review of some commercial and open source .NET profilers<br />Overview of th...
Profilers: What are they good for?<br />
Monitor Load/Unload Events<br />Application Domains<br />Assemblies<br />Modules<br />Classes<br />JIT Compilation Events<...
Get the names and signatures of classes and methods<br />Read the IL for methods (functions)<br />Memory allocations<br />...
Dynamically create new classes and methods<br />Rewrite the IL of existing methods<br />Allocate memory<br />Work with the...
Commercial<br />Redgate ANTS<br />NCover(was originally Open Source)<br />JetBrainsdotCover/dotTrace<br />CLRProfiler4 (Mi...
Open Source<br />Nprof<br />http://code.google.com/p/nprof/<br />SlimTune<br />http://code.google.com/p/slimtune/<br />Par...
System.Reflection.Emit<br />.NET Framework<br />Mono.Cecil<br />https://github.com/mono/cecil<br />Other IL (Re)Writing Me...
Important Interfaces<br />Implementation and Registration<br />Overview of the Profiler API<br />
In-process COM object<br />i.e. a DLL<br />Native code<br />i.e. unmanaged code<br />Free threaded<br />i.e. the developer...
.NET Runtime through the ages<br />
ICorProfilerCallback<br />69 Methods<br />ICorProfilerInfo<br />33 Methods<br />IMetaDataImport<br />64 Methods<br />IMeta...
ICorProfilerCallback2<br />8 Methods<br />ICorProfilerInfo2<br />21 Methods<br />IMetaDataImport2<br />64 Methods<br />IMe...
ICorProfilerCallback3<br />3 Methods<br />ICorProfilerInfo3<br />14 Methods<br />.NET4<br />
How much?<br />
Register the COM Object<br />Use Environment variables<br />c:>set COR_ENABLE_PROFILING=1<br />c:>set COR_PROFILER=MyProfi...
<ul><li>NET4 profilers can be used to profile .NET2 assemblies
Need to be side-by-side CLR aware</li></ul>Pick one<br />Pick first<br />Pick all/many<br />RunSxS<br />http://archive.msd...
Silverlight support<br />CORECLR_ENABLE_PROFILING<br />CORECLR_PROFILER<br />CORECLR_PROFILER_PATH<br />.NET4<br />
Creating an ATL COM object with required interfaces<br />Demo 1<br />
Create host and target processes<br />Launch Target with Profiler<br />Simple Target<br />Demo 2<br />
Requesting and handling events<br />ICorProfilerInfo::SetEventMask<br />COR_PRF_MONITOR_MODULE_LOADS<br />COR_PRF_MONITOR_...
Getting assembly and method names<br />Interfaces<br />ICorProfilerInfo3<br />IMetaDataImport2<br />Demo 4<br />
What is a Method?<br />Headers<br />Sections<br />Clauses<br />The Method Body<br />Operations<br />Branches<br />The Stac...
Tiny and Fat Methods<br />
Tiny and Fat Sections<br />
Tiny and Fat Clauses<br />
5 Types of Clauses<br />COR_ILEXCEPTION_CLAUSE_NONE<br />COR_ILEXCEPTION_CLAUSE_FILTER<br />COR_ILEXCEPTION_CLAUSE_FINALLY...
Try/Catch<br />Code in handler block is called if an exception of the type expected is thrown from code that is contained ...
Variation on Try/Catch<br />VB.NET only<br />COR_ILEXCEPTION_CLAUSE_FILTER<br />
Try/Finally<br />Code in handler block always called regardless of how the associated try block is exited.<br />COR_ILEXCE...
Try/Fault<br />Code in handler block is only called if an exception has occurred in the corresponding try block.<br />The ...
A Mystery<br />// duplicated clause..  this clause was duplicated down to a funclet which was pulled out of line<br />COR_...
The Method Body<br />Operations<br />Branches<br />The Stack<br />Debug vs. Release<br />The Method Body<br />
Contains all the information needed to interpret and write  IL<br />Canonical Name<br />String Name<br />Stack Behaviour<b...
OPCODE.DEF<br />
OPCODE.DEF<br />
Is there a difference?<br />Debug vs. Release<br />
Headers<br />Stack size<br />Method Size<br />Clauses<br />Offsets<br />Lengths<br />Branches<br />Size of Jump<br />Consi...
Make …<br />Tiny headers Fat<br />Tiny sections Fat<br />Tiny clauses Fat<br />Short branches Long <br />Cheat…<br />Use t...
Leverage the OpenCover parser<br />Demo 5<br />
Add a new Type<br />Exception<br />Define Constructor (.ctor)<br />Throw new Type<br />Add extra IL to TargetMethod<br />A...
Interfaces<br />IMetaDataEmit2<br />IMetaDataAssemblyEmit<br />Adding New Classes and Methods<br />
IL DASM<br />
Complicated<br />Intensive<br />Hmmmm……<br />
Sequence Points<br />PDB Files<br />IL Rewriting<br />Points out of sync<br />Debugging<br />
Upcoming SlideShare
Loading in …5
×

.NET Profilers and IL Rewriting - DDD Melbourne 2

3,410 views

Published on

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.

Published in: Technology
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total views
3,410
On SlideShare
0
From Embeds
0
Number of Embeds
55
Actions
Shares
0
Downloads
21
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

.NET Profilers and IL Rewriting - DDD Melbourne 2

  1. 1. .NET Profilers aren’t scary<br />shaun.wilde@commonvision.com.au<br />shaun_wilde@hotmail.com<br />@scubamunki<br />Email:<br />Email:<br />Twitter:<br />
  2. 2. Who am I?<br />Specialize in providing project teams with the necessary frameworks and personnel during the critical early stages of the project.<br />shaun.wilde@commonvision.com.au<br />shaun_wilde@hotmail.com<br />@scubamunki<br />Email:<br />Email:<br />Twitter:<br />
  3. 3. CoverageEye.NET<br />Originally hosted on GotDotNet<br />PartCover<br />Originally hosted on SourceForge<br />Forked to GitHub<br />https://github.com/sawilde/partcover.net4<br />OpenCover<br />Hosted on GitHub<br />https://github.com/sawilde/opencover<br />How did I get here?<br />
  4. 4. Profilers: What are they good for?<br />Quick review of some commercial and open source .NET profilers<br />Overview of the profiler API<br />Code and Demos<br />Basics of IL rewriting<br />Code and Demos II<br />Introducing new classes and methods<br />Code and Demos III<br />Extras (If time permits)<br />.NET Profilers aren’t (that) scary<br />
  5. 5. Profilers: What are they good for?<br />
  6. 6. Monitor Load/Unload Events<br />Application Domains<br />Assemblies<br />Modules<br />Classes<br />JIT Compilation Events<br />Monitor threads, remotingand native/managed transitions<br />Monitor GC events<br />Monitor exceptions<br />Monitor method enter/leave<br />Monitor the runtime<br />
  7. 7. Get the names and signatures of classes and methods<br />Read the IL for methods (functions)<br />Memory allocations<br />Types<br />Garbage Collection<br />Call graphs<br />Interrogate the runtime<br />
  8. 8. Dynamically create new classes and methods<br />Rewrite the IL of existing methods<br />Allocate memory<br />Work with the GC<br />Interact with the runtime<br />
  9. 9. Commercial<br />Redgate ANTS<br />NCover(was originally Open Source)<br />JetBrainsdotCover/dotTrace<br />CLRProfiler4 (Microsoft with Source Code)<br />Commercial and Open Source Profilers*<br />(*Obviously this list is not complete)<br />
  10. 10. Open Source<br />Nprof<br />http://code.google.com/p/nprof/<br />SlimTune<br />http://code.google.com/p/slimtune/<br />PartCover<br />https://github.com/sawilde/partcover.net4<br />OpenCover<br />https://github.com/sawilde/opencover<br />Commercial and Open Source Profilers*<br />(*Obviously this list is not complete)<br />
  11. 11. System.Reflection.Emit<br />.NET Framework<br />Mono.Cecil<br />https://github.com/mono/cecil<br />Other IL (Re)Writing Methods<br />
  12. 12. Important Interfaces<br />Implementation and Registration<br />Overview of the Profiler API<br />
  13. 13. In-process COM object<br />i.e. a DLL<br />Native code<br />i.e. unmanaged code<br />Free threaded<br />i.e. the developer is responsible for any required synchronisation <br />32/64 bit<br />Like for like<br />Implementation<br />
  14. 14.
  15. 15. .NET Runtime through the ages<br />
  16. 16. ICorProfilerCallback<br />69 Methods<br />ICorProfilerInfo<br />33 Methods<br />IMetaDataImport<br />64 Methods<br />IMetaDataEmit<br />49 Methods<br />.NET1<br />
  17. 17. ICorProfilerCallback2<br />8 Methods<br />ICorProfilerInfo2<br />21 Methods<br />IMetaDataImport2<br />64 Methods<br />IMetaDataEmit2<br />49 Methods<br />.NET2<br />
  18. 18. ICorProfilerCallback3<br />3 Methods<br />ICorProfilerInfo3<br />14 Methods<br />.NET4<br />
  19. 19. How much?<br />
  20. 20. Register the COM Object<br />Use Environment variables<br />c:>set COR_ENABLE_PROFILING=1<br />c:>set COR_PROFILER=MyProfiler.Profiler<br />Instantiation<br />
  21. 21. <ul><li>NET4 profilers can be used to profile .NET2 assemblies
  22. 22. Need to be side-by-side CLR aware</li></ul>Pick one<br />Pick first<br />Pick all/many<br />RunSxS<br />http://archive.msdn.microsoft.com/RunSxS<br />.NET4<br />
  23. 23. Silverlight support<br />CORECLR_ENABLE_PROFILING<br />CORECLR_PROFILER<br />CORECLR_PROFILER_PATH<br />.NET4<br />
  24. 24. Creating an ATL COM object with required interfaces<br />Demo 1<br />
  25. 25.
  26. 26.
  27. 27.
  28. 28.
  29. 29.
  30. 30. Create host and target processes<br />Launch Target with Profiler<br />Simple Target<br />Demo 2<br />
  31. 31.
  32. 32.
  33. 33.
  34. 34.
  35. 35.
  36. 36.
  37. 37. Requesting and handling events<br />ICorProfilerInfo::SetEventMask<br />COR_PRF_MONITOR_MODULE_LOADS<br />COR_PRF_MONITOR_JIT_COMPILATION<br />COR_PRF_DISABLE_INLINING<br />COR_PRF_DISABLE_OPTIMIZATIONS<br />Demo 3<br />
  38. 38.
  39. 39.
  40. 40.
  41. 41.
  42. 42. Getting assembly and method names<br />Interfaces<br />ICorProfilerInfo3<br />IMetaDataImport2<br />Demo 4<br />
  43. 43.
  44. 44.
  45. 45.
  46. 46.
  47. 47.
  48. 48.
  49. 49.
  50. 50. What is a Method?<br />Headers<br />Sections<br />Clauses<br />The Method Body<br />Operations<br />Branches<br />The Stack<br />Debug vs. Release<br />Basics of IL Rewriting<br />
  51. 51. Tiny and Fat Methods<br />
  52. 52.
  53. 53.
  54. 54. Tiny and Fat Sections<br />
  55. 55.
  56. 56. Tiny and Fat Clauses<br />
  57. 57.
  58. 58. 5 Types of Clauses<br />COR_ILEXCEPTION_CLAUSE_NONE<br />COR_ILEXCEPTION_CLAUSE_FILTER<br />COR_ILEXCEPTION_CLAUSE_FINALLY<br />COR_ILEXCEPTION_CLAUSE_FAULT<br />COR_ILEXCEPTION_CLAUSE_DUPLICATED<br />Clauses<br />
  59. 59. Try/Catch<br />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)<br />COR_ILEXCEPTION_CLAUSE_NONE<br />
  60. 60. Variation on Try/Catch<br />VB.NET only<br />COR_ILEXCEPTION_CLAUSE_FILTER<br />
  61. 61. Try/Finally<br />Code in handler block always called regardless of how the associated try block is exited.<br />COR_ILEXCEPTION_CLAUSE_FINALLY<br />
  62. 62. Try/Fault<br />Code in handler block is only called if an exception has occurred in the corresponding try block.<br />The exception continues on…<br />IL Only?<br />COR_ILEXCEPTION_CLAUSE_FAULT<br />
  63. 63. A Mystery<br />// duplicated clause.. this clause was duplicated down to a funclet which was pulled out of line<br />COR_ILEXCEPTION_CLAUSE_DUPLICATED<br />
  64. 64.
  65. 65. The Method Body<br />Operations<br />Branches<br />The Stack<br />Debug vs. Release<br />The Method Body<br />
  66. 66. Contains all the information needed to interpret and write IL<br />Canonical Name<br />String Name<br />Stack Behaviour<br />Parameter Size<br />Length<br />Bytes<br />Control Flow<br />OPCODE.DEF<br />
  67. 67.
  68. 68.
  69. 69. OPCODE.DEF<br />
  70. 70. OPCODE.DEF<br />
  71. 71. Is there a difference?<br />Debug vs. Release<br />
  72. 72.
  73. 73.
  74. 74. Headers<br />Stack size<br />Method Size<br />Clauses<br />Offsets<br />Lengths<br />Branches<br />Size of Jump<br />Considerations when adding IL<br />
  75. 75. Make …<br />Tiny headers Fat<br />Tiny sections Fat<br />Tiny clauses Fat<br />Short branches Long <br />Cheat…<br />Use the parser from OpenCover<br />Common approach when adding IL<br />
  76. 76. Leverage the OpenCover parser<br />Demo 5<br />
  77. 77.
  78. 78.
  79. 79.
  80. 80.
  81. 81.
  82. 82.
  83. 83. Add a new Type<br />Exception<br />Define Constructor (.ctor)<br />Throw new Type<br />Add extra IL to TargetMethod<br />Adding new classes and methods I<br />
  84. 84. Interfaces<br />IMetaDataEmit2<br />IMetaDataAssemblyEmit<br />Adding New Classes and Methods<br />
  85. 85.
  86. 86.
  87. 87.
  88. 88. IL DASM<br />
  89. 89.
  90. 90.
  91. 91.
  92. 92.
  93. 93.
  94. 94.
  95. 95.
  96. 96.
  97. 97. Complicated<br />Intensive<br />Hmmmm……<br />
  98. 98.
  99. 99.
  100. 100.
  101. 101.
  102. 102.
  103. 103.
  104. 104. Sequence Points<br />PDB Files<br />IL Rewriting<br />Points out of sync<br />Debugging<br />
  105. 105.
  106. 106.
  107. 107.
  108. 108. The End<br />Demo code<br />https://github.com/sawilde/DDD2011_ProfilerDemo<br />

×