MSDN Documentation

.NET Profiler API Samples

Profiler API Samples

Explore the following code samples to understand how to leverage the .NET Profiler API for performance analysis and diagnostics.

Core Profiling Scenarios

CPU Usage Profiling

Overview

This sample focuses on collecting comprehensive CPU usage data. It utilizes the profiler API to hook into the .NET runtime and record information about which functions are executing and for how long. This is crucial for identifying performance bottlenecks in your application.

Key Concepts

Code Snippet

// Simplified C# example
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Diagnostics.Runtime.Profiling;

public class CpuProfiler
{
    private Dictionary<int, FunctionTiming> _timings = new Dictionary<int, FunctionTiming>();
    private Stopwatch _stopwatch = Stopwatch.StartNew();

    public void Start()
    {
        // ... Initialize profiler ...
        // Subscribe to FunctionEnter/FunctionExit events
    }

    private void OnFunctionEnter(FunctionInfo funcInfo)
    {
        // Record entry time
        if (!_timings.ContainsKey(funcInfo.Id))
        {
            _timings[funcInfo.Id] = new FunctionTiming { Function = funcInfo };
        }
        _timings[funcInfo.Id].LastEnterTime = _stopwatch.Elapsed;
    }

    private void OnFunctionExit(FunctionInfo funcInfo)
    {
        if (_timings.ContainsKey(funcInfo.Id))
        {
            var timing = _timings[funcInfo.Id];
            var duration = _stopwatch.Elapsed - timing.LastEnterTime;
            timing.TotalInclusiveTime += duration;
            timing.CallCount++;
            // ... Calculate exclusive time ...
        }
    }

    public void Stop()
    {
        // ... Report collected data ...
    }
}

public class FunctionTiming
{
    public FunctionInfo Function;
    public TimeSpan TotalInclusiveTime;
    public int CallCount;
    public TimeSpan LastEnterTime;
    // ... other metrics
}

Related API References

Microsoft.Diagnostics.Runtime.Profiling.ICorProfilerCallback

Microsoft.Diagnostics.Runtime.Profiling.FunctionInfo

Memory Allocation Profiling

Overview

This sample demonstrates how to track memory allocations within your .NET application. By profiling allocations, you can pinpoint areas that are creating excessive objects, leading to increased garbage collection pressure and potential memory leaks.

Key Concepts

Code Snippet

// Simplified C# example
using System;
using Microsoft.Diagnostics.Runtime.Profiling;

public class MemoryProfiler
{
    private Dictionary<string, long> _allocationSizes = new Dictionary<string, long>();

    public void Start()
    {
        // ... Initialize profiler ...
        // Subscribe to ObjectAllocated event
    }

    private void OnObjectAllocated(ObjectInfo objInfo)
    {
        if (!_allocationSizes.ContainsKey(objInfo.Type.Name))
        {
            _allocationSizes[objInfo.Type.Name] = 0;
        }
        _allocationSizes[objInfo.Type.Name] += objInfo.Size;
        // ... potentially store allocation site info ...
    }

    public void Stop()
    {
        // ... Report allocation data by type and size ...
    }
}

Related API References

Microsoft.Diagnostics.Runtime.Profiling.ICorProfilerCallback2

Microsoft.Diagnostics.Runtime.Profiling.ObjectInfo

Method Instrumentation

Overview

Method instrumentation allows you to dynamically modify method bodies at runtime. This sample shows how to insert code to measure the execution time of specific methods or to count how many times they are invoked. This is a powerful technique for fine-grained performance analysis.

Key Concepts

Code Snippet

// Simplified C# example (conceptual, actual IL manipulation is complex)
using System;
using System.Reflection.Emit;
using Microsoft.Diagnostics.Runtime.Profiling;

public class MethodInstrumenter
{
    public void InstrumentMethod(MethodInfo methodInfo)
    {
        // ... Get Method Body ...
        // ... Insert IL instructions for timing/counting ...
        // Example: Before original code, add instruction to get current time
        // Example: After original code, add instruction to record duration and increment counter
        // ... Update Method Body ...
    }

    public void Start()
    {
        // ... Initialize profiler ...
        // Subscribe to JITCompilationStarted event
        // Call InstrumentMethod for methods of interest
    }
}

Related API References

Microsoft.Diagnostics.Runtime.Profiling.ICorProfilerCallback3

System.Reflection.Emit.ILGenerator