Giter VIP home page Giter VIP logo

exampleprofiler's Introduction

An Example CLR Profiler

This repository contains the minimum code required to create a profiler for .NET applications.

The example profiler waits for Garbage Collections, and then writes a message to the standard output of the application it is profiling (the Console, in the case of the example application included).

This code is intended to be used as scaffolding for writing a new CLR profiler and for learning about how these profilers work.

As CLR profilers must be un-managed code, this repository is primarily C++.

Running the example

  • Clone the repository
  • Build the solution
  • Open a new Command Prompt window
  • Run RunExampleApplication.bat from the root folder

The batch file first sets some environment variables (see below), before launching a simple Console Application.

The Console Application's runtime will automatically load the profiler, the code in this sample application has no knowledge of the profiler.

Every 2 seconds, the sample application forces a Garbage Collection. The profiler will receive a notification for this, and write Profiler: Garbage Collection to the Console.

Note: The example batch file assumes you are running on x64 architecture. If you have compiled the application for x86, modify the contents of the batch file to point to the correct output folder

How CLR profilers work

A CLR profiler is loaded into the runtime of the application to be profiled.

Enabling profiling in a .NET application is typically achieved by setting the following environment variables.

COR_ENABLE_PROFILING = 1                                       # Switch on profiling
COR_PROFILER         = {DF9EDC4B-25C1-4925-A3FB-6AAEB3E2FACD}  # CLSID of the profiler
COR_PROFILER_PATH    = c:\MyProfiler\MyProfiler.dll            # Path to the profiler's DLL

Setting these in a Command Prompt window will cause any application launched from that window to load the profiler. If these are set at machine level, any .NET application will run the profiler specified.

Once the application to be profiled is launched, the CLR will locate the profiler DLL and attempt to load it. If successful, the profiler can subscribe for callbacks when significant events occur (Entering/Leaving methods, Garbage Collections etc.).

The profiler can also modify aspects of the application being profiled. For example, it is possible to subscribe to callbacks that allow you to modify the code in a method.

Note: It is possible to attach a profiler to an already running .NET process. In this case, however, the scope of what the profiler can do is severely restricted. For brevity, this sample does not include code for attaching to a running process.

Further reading

This code has been assembled through a combination of MSDN documentation and Microsoft's open-source profiler implementations.

exampleprofiler's People

Contributors

dsaunders avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.