$ show pytorch/kineto
Kineto is a production-grade C++ library for GPU and CPU performance profiling, primarily embedded in PyTorch's profiler. It provides low-overhead timeline tracing of GPU kernels, memory operations, synchronization events, and CPU-side activities, with support for CUDA (CUPTI), ROCm (rocprofiler-sdk), and Intel XPU (PTI) backends. The library supports both synchronous (manual start/stop) and asynchronous (on-demand, daemon-triggered) profiling modes, and emits Chrome Trace JSON and in-memory trace buffers for downstream analysis. It is not a standalone application; it is a linkable library with a public C API and plugin interfaces for custom profilers and loggers. Architecturally, Kineto is organized around a central ActivityProfilerController that drives a warmup/collect/process state machine, delegating platform-specific capture to backend profilers derived from GenericActivityProfiler. Configuration flows through a ConfigLoader that polls files or an IPC fabric daemon, and activity data flows from vendor callback/activity APIs through correlation-ID-based flow tracking into typed metadata and trace spans. The codebase demonstrates strong software engineering practices: extensive unit tests, CI matrices for CPU/CUDA/ROCm/XPU, clean interface abstractions, and reusable patterns such as observer-based logging, factory-based output registration, and type-safe metadata visitors. However, heavy conditional compilation, vendor SDK version guards, and platform-specific ifdefs introduce maintenance complexity and moderate technical drift risk.
The LLM identified 5 files worth highlighting for reuse:
Defines a type-safe metadata system using templated MetadataField<T> and an ITypedMetadataVisitor interface. This decouples metadata storage from serialization, allowing JSON, recording, and other visitors to process structured activity metadata without type erasure or stringly-typed dictionaries. It is self-contained and could be extracted for any telemetry or event system needing strongly-typed, extensible metadata.
Provides fast conversion between hardware timestamps (TSC/ARM) and wall-clock time using a calibrated approximate clock. This is a non-obvious, self-contained utility that solves a recurring problem in low-overhead tracing and profiling: converting high-frequency hardware counters to epoch-based timestamps without expensive syscalls. It can be reused in any performance instrumentation project.
Implements a protocol-based factory for activity loggers, allowing output formats to be registered and selected by prefix (e.g., 'chrome trace', 'memory buffer'). This enables extensible trace serialization without modifying core profiling logic. The pattern is generic and reusable for any system needing pluggable output sinks.
A minimal, header-only RAII scope guard that executes a cleanup lambda when the scope exits. It is a classic, dependency-free utility that improves exception safety and resource management. Easily extractable into any C++ project.
A reader-writer lock protected switchboard for CUPTI callbacks, efficiently routing callback events to registered handlers while minimizing contention. It demonstrates a non-trivial concurrency pattern for high-frequency event dispatch and could be adapted for other vendor SDKs or high-throughput callback systems.