Skip to main content

CUPTI Agent and Online Injection

libxprobe-cupti.so runs inside a CUDA target process to collect Runtime/Driver API callbacks, kernel/memcpy/memset activity, and ASCII NVTX range boundaries. Users normally drive it through xprobe measure --pid; it is not a daemon or persistent profiling session.

Release packages ship separate builds linked to libcupti.so.12 and libcupti.so.13. measure picks the major from the target's mapped CUDA libraries (or from the single installed CUPTI major when the target doesn't expose one) and fails before ptrace attach on conflict, unsupported major, or missing .so.

Measurement lifecycle​

  1. Validate & inject: validate is read-only. When it reports injection_required, measure warns on stderr and uses Linux x86_64 ptrace to call target dlopen, dlsym, and xprobe_cupti_agent_start. Loading opens a mode-0600 Unix control socket without enabling CUPTI yet.
  2. ARM: measure sends ARM with record capacity and up to eight endpoint filters. The Agent allocates fresh storage, installs filters, and enables only the required callbacks and activity families.
    • API names/domains, memcpy directions, and exact/prefix/suffix/contains kernel or NVTX patterns under 128 bytes filter in the Agent hot path before consuming capacity.
    • Complex kernel regexes use a wider Agent filter and apply full regex matching in Rust.
  3. SNAPSHOT: flushes pending CUPTI activity and returns only contiguous records after the caller's checked offset.
  4. STOP / CLOSE: flushes the final delta and disables CUPTI callbacks/activity.
    • STOP keeps the control socket open so a preloaded Agent can handle later captures.
    • CLOSE (used after automatic injection) also removes the private socket.
    • Neither path calls dlclose: unloading callback code in a live CUDA process is unsafe.

Prerequisites: target and xprobe must share a mount namespace, and ptrace permissions (Yama, CAP_SYS_PTRACE, seccomp, LSM) apply. Every remote ptrace call saves and restores registers and touched stack memory on both success and failure.

NVTX ranges​

Online attach cannot retrofit an already initialized NVTX dispatch table. Set NVTX_INJECTION64_PATH to the matching Agent .so before the target's first NVTX call.

  • Startup installs NVTX routing and a dormant CUPTI subscriber kept alive for the process lifetime (CUDA 13 continues referencing it across captures).
  • ARM enables only the six ASCII/Ex range callbacks (StartA/StartEx/End, PushA/PushEx/Pop) with name matching before timestamping or table insertion; STOP disables those callbacks without unsubscribing.
  • Only ASCII range names, IDs, kind, and thread IDs are copied (no payload, color, category, registered strings, wide strings, or env vars).
  • Because CUPTI tracks push levels only while callbacks are enabled, a pop of a range pushed before ARM reports no level and is ignored.

Aggregate mode​

An aggregate ARM allocates a fixed --max-groups hash table instead of per-event records:

  • Kernels group by (name, device_id); memcpy by (direction, src_memory, dst_memory, device_id); memset by device_id.
  • Activity callbacks update count, min/max/total duration, and bytes in place.
  • The CLI reads the table once at STOP; table saturation fails explicitly instead of silently sampling.

Capture window edges & start_unknown​

  • Activity that ended before ARM is excluded; activity still running at ARM keeps its earlier start so overlapping work isn't hidden.
  • Activity still running at STOP keeps a start record so the stream and GPU stay busy through the capture end rather than looking idle. CUPTI may report no start for it, or one not yet converted to host time, so the Agent writes the record without a time and flags it (XPROBE_CUPTI_RECORD_START_UNKNOWN); the clock check skips it. The CLI sets its time to the stream's last activity end, or to the capture start if nothing else ran, and adds the start_unknown attribute. It is never used as a duration, gap, or latency endpoint.
  • Records with unavailable timestamps after the first timed activity count as dropped.

Control and capture ABI​

Defined in cupti/include/xprobe/cupti_agent.h:

Control protocol v6 (xprobe_cupti_control_request, 1192 bytes)​

  • Native-endian request carrying magic (XPCTRL\0), version (6), command (ARM/SNAPSHOT/STOP/CLOSE), record_capacity, record_offset, capture_mode (EXACT=0, AGGREGATE=1, RING=2), filter_count, and up to 8 fixed xprobe_cupti_filter entries.
  • Exact ARM requires offset 0; subsequent snapshots return records at/after record_offset followed by EOF. Aggregate mode uses offset 0 on every command.

Ring mode (--trigger-*)​

  • Keeps the newest record_capacity exact records in a ring without stopping at the limit.
  • Each slot carries its logical index; snapshots copy under a sequence lock so overwritten slots are discarded along with older records.
  • The response header's record_offset is the first returned record; a value above the requested offset counts overwritten records. Sets XPROBE_CUPTI_FEATURE_RING_RECORDS.

Capture ABI v4 (xprobe_cupti_output_header, 88 bytes)​

  • Followed by an optional device table (XPROBE_CUPTI_FEATURE_DEVICE_TABLE: count, entry size, and 48-byte xprobe_cupti_device_entry records mapping each CUDA ordinal to its GPU/MIG UUID, read outside CUDA init and Agent locks without recording the Agent's own driver calls).
  • Followed by 200-byte exact records (xprobe_cupti_record) or aggregate records (xprobe_cupti_aggregate_record).
  • Header tracks capture state, stop reason, capacity, observed/retained counts, Agent and CUPTI drops, unknown records, record sizes, feature flags, and payload offset.
  • Capacity comes directly from --max-events / --max-groups (no 2^16 cap). Reaching the limit freezes capture and fails explicitly.
  • If STOP fails to disable every callback/activity kind, it reports stop reason 5 (XPROBE_CUPTI_STOP_CLEANUP_ERROR) and keeps the socket open so the CLI returns CLEANUP_FAILED and retries CLOSE.

Record semantics & clock normalization​

  • Peer-to-peer copies: collected via CUPTI MEMCPY2 records (XPROBE_CUPTI_FEATURE_PEER_MEMCPY); they carry stream_id and no runtime correlation ID.
  • NVTX records: carry 64-bit range ID, thread/process kind, start_tid, and name_complete.
  • Clock alignment: activity timestamps normalize to CLOCK_MONOTONIC via the CUPTI timestamp callback or CUDA 12 clock calibration. The Agent sets XPROBE_CUPTI_FEATURE_HOST_MONOTONIC_TIMESTAMPS only after verifying an activity ended between ARM and the check, or a kernel started no earlier than its launch call. If alignment cannot be proven, GPU-only durations remain usable in raw cupti time and cross CPU/GPU subtraction returns CLOCK_ALIGNMENT_FAILED. Because CUPTI provides no numeric error bound, normalized results emit CLOCK_ERROR_UNAVAILABLE.

Hot-path rules​

  • ARM enables only the activity kinds and Runtime/Driver callback IDs needed by the validated endpoints.
  • Callbacks and activity decoders check filters before reading time or writing records, and stop immediately once capacity is reached.
  • Hot paths never allocate, perform file/socket I/O, symbolize names, or take blocking locks. Flushing and socket I/O run on the control thread.
  • CUPTI Range Profiling metrics and replay passes are out of scope for this timeline collector.