Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Datadog Engineering 18 min read Deep dive hardMaster

How we built an async-aware Python profiler

Datadog's Python profiler now reconstructs parent-child relationships between asyncio tasks so flame graphs show "stacked stacks": the logical application path rather than independent per-task stacks. It samples each thread without taking the GIL, walks coroutine frames for every task, builds a dependency tree from which task awaits which, and stitches leaf-task frames onto their awaiting parents. Tasks that outlive their creator are attributed to the creator while it runs and to the awaiter afterwards. Because per-task sampling scales with O(n_tasks) and could push adaptive sampling down to one sample per second, the team also reworked the profiler's sampling path and cut overhead by more than 60% while improving profile fidelity.

Assumes: Python asyncio tasks and coroutinessampling profilers and flame graphsCPython GIL

Related posts

  • How we wrote a Python profiler

    Datadog Engineering mediumAdept

    Datadog 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.

  • .NET Continuous Profiler: CPU and wall time profiling

    Datadog Engineering hardMaster

    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.

  • Android profiling now uses Perfetto via ProfilingManager

    Sentry mediumAdept

    Sentry Android SDK 8.51.0 switches profiling on Android 15+ (API 35+) from in-process Debug.startMethodTracing to Perfetto-based stack sampling via the system ProfilingManager API, while older devices fall back to the legacy profiler. Running out of process avoids an ART tracer crash triggered by native threads that exit without detaching, shifts most CPU and memory overhead into a system process, and yields more representative profiles. Android rate-limits profiling requests, so even a 1.0 session sample rate won't capture every session; an adb device_config flag disables the limit for local testing, and a new enableLegacyProfiling option can be set to false to avoid the legacy tracer entirely.

  • .NET Continuous Profiler: Under the hood

    Datadog Engineering hardMaster

    Datadog'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.

  • Strobelight: A profiling service built on open source technology

    Meta Engineering mediumAdept

    Strobelight is Meta's fleet-wide profiling orchestrator, running 42 profilers (mostly eBPF-based, plus ad-hoc bpftrace scripts) on every production host with safeguards against PMU counter conflicts, concurrency limits and queuing. Default continuous profiling uses dynamic sampling that retunes run probability daily per service to hit target sample counts, with sample weights normalizing data for cross-host and cross-service comparison. LBR data feeds Meta's FDO pipeline (CSSPGO and BOLT), cutting CPU cycles up to 20% for top services, and one efficiency win alone saved an estimated 15,000 servers annually. Stack Schemas and thread-local Strobemeta enrich stacks with tags and request metadata, while a centralized symbolization service built on DWARF, gsym and blazesym avoids on-host debug-info parsing, relying on frame pointers in all user-space binaries.

The quest for software engineering knowledge.

Look:
Layout: