.NET Continuous Profiler: CPU and wall time profiling
Datadog's .NET continuous profiler samples managed threads every 10 ms instead of relying on ETW, perf or enter/leave hooks, which cost too much for 24/7 production use. CPU samples are taken only from runnable threads, with CPU time read via NtQueryInformationThread or /proc. Wall time samples add extra threads tied to trace spans for Code Hotspots. Stack walking uses signal handlers plus libunwind on Linux, and on Windows uses thread suspension with DoStackSnapshot (32-bit) or RtlVirtualUnwind (64-bit), with a watcher thread and LD_PRELOAD trampolines to avoid deadlocks. Profiling the profiler showed std::ifstream reads of /proc allocating 8 KB per sample; switching to low-level C reads cut CPU and removed the allocations.
Related posts
- .NET Continuous Profiler: Memory usage
Datadog Engineering hardMasterDatadog's .NET continuous profiler measures memory in three ways. It pulls per-thread CPU time for the .NET Server GC/BGC threads (available since .NET 5) to show garbage collector cost. It samples allocations via the CLR's AllocationTick event, fired every 100 KB per SOH/LOH/POH, and walks the stack on the current thread. It tracks surviving objects with weak handles through a new ICorProfilerInfo13 API added in .NET 7, patching the MethodTable slot with the ClassID so a handle can be created before the object exists. Validation showed the fixed 100 KB threshold produces unrepresentative samples, so values are not upscaled until the CLR adopts Poisson-distributed sampling thresholds like the Go and Java profilers.
- .NET Continuous Profiler: Under the hood
Datadog Engineering hardMasterDatadog's .NET continuous profiler runs in-process as native C++ and Rust code loaded through the CLR's ICorProfilerCallback interface. This avoids TraceEvent and sidecars, which would add GC pressure, complicate deployment and prevent synchronous handling of events like allocations. Separate CPU, wall time, exception, lock contention, allocation and live-heap profilers feed samples to a shared Rust aggregator, which merges samples with identical stacks and labels into .pprof files. A native loader multiplexes the single allowed profiler callback across Datadog products, compiler-generated frame names are cleaned up for readability, and the runtime version is detected by probing which ICorProfilerInfo interface version QueryInterface returns.
- .NET Continuous Profiler: Exception and lock contention
Datadog Engineering hardMasterDatadog's .NET continuous profiler captures thrown exceptions through ICorProfilerCallback::ExceptionThrown and reads each exception's _message field by resolving System.Exception's ClassID from CoreLib metadata and computing the field offset with GetClassLayout. Lock contention durations come from CLR ContentionStart/ContentionStop events, delivered synchronously via EventPipeEventDelivered since .NET 5, with lock owner info added in .NET 8. Both event streams are subsampled with a PID-controller-based adaptive sampler: always at least one exception per type, and contentions bucketed by duration. Sampled values are then upscaled, using a count ratio for exceptions and a duration ratio for locks, since total wait time is what drives latency.
- How we wrote a Python profiler
Datadog Engineering mediumAdeptDatadog built an always-on statistical profiler for Python because deterministic profilers like cProfile can add 2-3x overhead, too much for production. The design borrows from JDK Flight Recorder and splits into recorder, collectors, exporter and scheduler. A Cython-based stack collector samples every thread's stack 100 times per second and lowers its polling rate under heavy load to cap overhead. Memory and lock collectors add allocation and lock-contention data, and profiles are aggregated into compact pprof (protobuf) files uploaded every minute through the Datadog Agent.
- Unbiased Java CPU profiling with JFR in JDK 25
Datadog Engineering mediumAdeptJDK 25 adds an experimental JFR CPU-time sampling event (CPUTimeSample) that gives unbiased CPU profiles without relying on the unsupported HotSpot AsyncGetCallTrace API. The existing ExecutionSample event samples a rotating subset of threads rather than in proportion to CPU time, which can underreport hotspots, notably in reactive workloads. Profilers such as Datadog's therefore combined JFR with SIGPROF-driven AsyncGetCallTrace and vmstructs walking, trading stability for accuracy. After the AsyncGetStackTrace JEP stalled, Oracle, SAP, Amazon and Datadog engineers built on cooperative stack walking, which records lightweight execution points and unwinds only at safepoints. Native frame support and safe on-demand stack capture are next.