Summary
On B200 and B300 with CUPTI 13.4.92 and driver 590.48.01, Hardware Event System (HES) kernel tracing works on a GPU until one
process drives it at a high kernel rate (~1.8M kernels/s, 2 streams of CUDA graphs). Records then stop part-way through that run.
From then on, every new process on that GPU gets no usable HES kernel records (CUDA graph kernels: 0 records; eager kernels: 0 records
or records with start = end = 0), even at a low rate with frequent flushing and a clean shutdown. No CUPTI_CB_DOMAIN_STATE callback is raised and
cuptiActivityGetNumDroppedRecords stays 0. The state persisted for 2+ days across many processes; only a GPU reset is expected to clear it.
Software (non-HES) tracing on the same GPU keeps working normally, as does the workload itself.
Environment
- GPUs: NVIDIA B200 (sm_100) and B300 (sm_103). Checked on a B200 host: bare metal, MIG off, no MPS, no vGPU, confidential compute off,
compute mode Default - Driver: 590.48.01. On a GPU already in the stuck state, user-mode 595.71.05 / 610.57.04 / 615.71.10 (cuda-compat on kernel module 590)
did not bring HES back - CUPTI: 13.4.92 (nvidia-cuda-cupti wheel) with the wheel’s libnvperf_host.so on the library path; Linux x86_64, Docker container
- Tracer: CUDA_INJECTION64_PATH library (repro/min_client.c); cuptiActivityEnableHWTrace(1) in InitializeInjection,
cuptiActivityRegisterCallbacks, CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL; a helper thread calls cuptiActivityFlushAll(0) every 10 ms;
at exit: disable kinds, forced flush, cuptiActivityEnableHWTrace(0), cuptiFinalize (all return CUPTI_SUCCESS)
Reproduction (on a GPU that has not run HES before)
Build (CUDA 13.x toolchain, CUPTI 13.4.92 headers/lib in $CUPTI; -arch=sm_103 on B300):
nvcc -O2 -arch=sm_100 -o stress stress.cu
gcc -O2 -shared -fPIC -pthread -o libmin.so min_client.c -I$CUPTI/include -I/usr/local/cuda/include -L$CUPTI/lib -l:libcupti.so.13
export LD_LIBRARY_PATH=$CUPTI/lib:$LD_LIBRARY_PATH # libcupti.so.13 + libnvperf_host.so
export CUDA_INJECTION64_PATH=$PWD/libmin.so MIN_KERNEL_ONLY=1 MIN_DEFAULT_ATTRS=1 MIN_HES=early MIN_FLUSH_MS=10 MIN_LOG_S=1 MIN_CLEAN_EXIT=1
export STRESS_BLOCKING_SYNC=1
stress <seconds> <streams> <kernels per graph> 0 <target kernels/s, 0 = flat out> 1 replays CUDA graphs of tiny kernels and prints the
launch count; min_client prints records delivered, kernel records, zero-timestamp records and dropped records.
Observed on one B200 (each step a new process, same GPU):
| step | command | kernels launched | HES kernel records |
|---|---|---|---|
| 1 | ./stress 30 1 200 0 100000 1 |
3,000,000 | 3,000,000 (mean 639.7 ns, 0 zero timestamps) |
| 2 | same again | 3,000,000 | 3,000,000 |
| 3 | ./stress 30 1 200 0 0 1 (1 stream, flat out, ~1.18M/s) |
35,392,000 | 35,392,000 |
| 4 | ./stress 30 2 200 0 0 1 (2 streams, flat out, ~1.82M/s) |
54,681,600 | 5,520,081, then nothing |
| 5 | ./stress 30 4 200 0 0 1 |
91,238,400 | 0 |
| 6 | ./stress 30 1 200 0 0 1 again |
35,340,800 | 0 |
| 7 | ./stress 30 2 200 0 0 1 again |
54,675,200 | 0 |
On stuck GPUs, eager launches (STRESS_EAGER=1) gave either 0 records or exact counts with start = end = 0 for every record
(e.g. 9,092,400 of 9,092,400 on another stuck B200). The stuck state persisted in every later process (one B200 was checked again
~2 days later: 0 of 3,000,000 at 100k kernels/s). With HES off, the same commands deliver exact counts with valid timestamps on the stuck
GPUs. Running steps 1–3 only (never above ~1.2M kernels/s) keeps HES working
across many processes. Earlier, with flushing every 1 s or no flushing, a single flat-out 1-stream run was enough to cause the stuck
state on a fresh GPU (it delivered 2.83M records on B300 / 7.28M on B200 first).
Most recent occurrence, on another fresh B200 with a tracer that flushes every 50 ms: 2 streams flat out (~1.57M kernels/s) delivered
2,793,815 of 47,001,600 kernel records, then nothing; the next process got 0 of 2,000,000 at 100k kernels/s. A check in that tracer that
stops tracing once more than 500k records arrive within one second never triggered, so delivered records never reached the launch rate
before HES stopped. In total 4 GPUs (3 B200, 1 B300) are now in this state.
Also seen: NVIDIA’s activity_trace_async -e 1 sample (CUDA 12.9 samples, built and run with that toolkit’s CUPTI) on a stuck B200
shows output identical to -e 0, including OVERHEAD CUPTI_INSTRUMENTATION records.
Expected
Records beyond HES buffer capacity may be dropped (documented), but HES should recover once the overload ends, at the latest in the
next process, and the drop should be visible (dropped-record count or a STATE callback).
Questions
- Is this a known issue, and is there a fix or workaround (driver or CUPTI version)?
- Is there a way to reset a GPU’s HES state without resetting the GPU?
- Is there a supported way to detect this from a tracer (it currently looks like an idle GPU)?
- Is there a documented maximum sustained kernel rate for HES, and is the 128 MB hardware buffer going to be configurable?
- Is there a supported way for a tracer to see HES approaching overflow (e.g. a buffer fill level), so it can stop tracing in time?
Repro sources
stress.cu
// GPU workload for the injector tests (no CUPTI here: the injector is loaded with CUDA_INJECTION64_PATH).
// CUDA graphs of tiny kernels plus a memcpy node (the CUPTI 13.0 buffer-leak case), replayed on S streams for D seconds; prints the
// number of kernels launched, so the snapshot's launch count can be checked against it.
// stress <seconds> [streams=2] [kernels_per_graph=200] [sleep_us_between_replays=0] [target_kernels_per_s=0]
// [memcpy_node=1] (STRESS_EAGER=1: launch the same kernels one by one instead of through graphs)
// stress <seconds> low <interval_ms> one eager kernel every interval (low-rate test)
// A kernel name per stream (stress_kernel_s0, _s1 ...) lets the snapshot be checked per kernel too.
#include <cuda_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define CK(x) \
do { \
cudaError_t e_ = (x); \
if (e_ != cudaSuccess) { \
fprintf(stderr, "%s:%d %s: %s\n", __FILE__, __LINE__, #x, cudaGetErrorString(e_)); \
exit(1); \
} \
} while (0)
template <int S>
__global__ void stress_kernel(float *x) { x[threadIdx.x + 32 * S] += 1.0f; }
typedef void (*Kernel)(float *);
static Kernel kernels[4] = {stress_kernel<0>, stress_kernel<1>, stress_kernel<2>, stress_kernel<3>};
static double now_s(void) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return t.tv_sec + t.tv_nsec / 1e9;
}
int main(int argc, char **argv) {
double seconds = argc > 1 ? atof(argv[1]) : 10;
float *x, *y;
const char *blocking = getenv("STRESS_BLOCKING_SYNC"); // 1: sleep in synchronize instead of spinning (CPU % = real work)
if (blocking && *blocking == '1') CK(cudaSetDeviceFlags(cudaDeviceScheduleBlockingSync));
CK(cudaMalloc(&x, 4096 * sizeof(float))); // CUDA is initialized from here on
const char *delay = getenv("STRESS_START_DELAY_S"); // e.g. let a tracer finish its setup before graph capture
if (delay && atoi(delay) > 0) sleep((unsigned)atoi(delay));
CK(cudaMalloc(&y, 4096 * sizeof(float)));
if (argc > 2 && !strcmp(argv[2], "low")) { // low rate: one eager kernel per interval
int interval_ms = argc > 3 ? atoi(argv[3]) : 100;
long n = 0;
double t0 = now_s();
while (now_s() - t0 < seconds) {
kernels[0]<<<1, 32>>>(x);
CK(cudaDeviceSynchronize());
n++;
usleep(interval_ms * 1000);
}
printf("launched %ld kernels (stress_kernel<0>) in %.1f s, one every %d ms\n", n, now_s() - t0, interval_ms);
return 0;
}
int streams = argc > 2 ? atoi(argv[2]) : 2, per_graph = argc > 3 ? atoi(argv[3]) : 200;
int sleep_us = argc > 4 ? atoi(argv[4]) : 0;
double target = argc > 5 ? atof(argv[5]) : 0; // pace replays to this many kernels per second (0 = as fast as possible)
int memcpy_node = argc > 6 ? atoi(argv[6]) : 1;
if (streams < 1 || streams > 4) streams = 2;
cudaStream_t st[4];
cudaGraphExec_t ge[4];
for (int s = 0; s < streams; s++) {
CK(cudaStreamCreateWithFlags(&st[s], cudaStreamNonBlocking));
cudaGraph_t g;
CK(cudaStreamBeginCapture(st[s], cudaStreamCaptureModeThreadLocal));
for (int i = 0; i < per_graph; i++) kernels[s]<<<1, 32, 0, st[s]>>>(x);
if (memcpy_node) CK(cudaMemcpyAsync(y + 1024 * s, x + 1024 * s, 1024, cudaMemcpyDeviceToDevice, st[s]));
CK(cudaStreamEndCapture(st[s], &g));
CK(cudaGraphInstantiate(&ge[s], g, 0));
}
const char *eager_env = getenv("STRESS_EAGER");
int eager = eager_env && *eager_env == '1';
const char *report_env = getenv("STRESS_REPORT_S"); // progress line every N s (default 60), rate over that interval
double report_s = report_env && atof(report_env) > 0 ? atof(report_env) : 60;
long replays = 0, replays_at_report = 0;
double t0 = now_s(), last_report = t0;
while (now_s() - t0 < seconds) {
for (int s = 0; s < streams; s++) {
if (!eager) {
CK(cudaGraphLaunch(ge[s], st[s]));
continue;
}
for (int i = 0; i < per_graph; i++) kernels[s]<<<1, 32, 0, st[s]>>>(x);
if (memcpy_node) CK(cudaMemcpyAsync(y + 1024 * s, x + 1024 * s, 1024, cudaMemcpyDeviceToDevice, st[s]));
}
replays++;
if (replays % 64 == 0) CK(cudaStreamSynchronize(st[0])); // keep the queue short
if (sleep_us) usleep(sleep_us);
double t = now_s();
if (target > 0) { // ahead of the target rate: wait until the launched kernels match it
double ahead = replays * (double)per_graph * streams / target - (t - t0);
if (ahead > 0.0005) usleep((useconds_t)(ahead * 1e6));
t = now_s();
}
if (t - last_report >= report_s) {
double interval_kernels = (replays - replays_at_report) * (double)per_graph * streams;
printf("[%5.0f s] %.0f kernels/s\n", t - t0, interval_kernels / (t - last_report));
fflush(stdout);
last_report = t;
replays_at_report = replays;
}
}
CK(cudaDeviceSynchronize());
double dt = now_s() - t0;
printf("launched %ld kernels per stream x %d streams = %ld kernels in %.1f s (%.0f kernels/s)\n",
replays * per_graph, streams, replays * per_graph * streams, dt, replays * (double)per_graph * streams / dt);
return 0;
}
min_client.c
// Minimal CUPTI client for the worker-thread CPU diagnosis (2026-10-22): no stats, no flush, no snapshot. Loaded like
// a tracer through CUDA_INJECTION64_PATH. Enables kernel + memcpy + memset activity with the same device buffer
// settings as our production tracer, and returns each buffer untouched apart from counting its records. Host buffers as in the production tracer:
// 16 MB, mmap'd, up to 8 kept for reuse. Once a minute it logs its worker thread's CPU time and records delivered.
// MIN_DEFAULT_ATTRS=1 leave CUPTI's device buffer settings at their defaults (no cuptiActivitySetAttribute)
// MIN_KERNEL_ONLY=1 enable only CONCURRENT_KERNEL (use with a graph without memcpy node: the CUPTI 13.0 buffer leak)
// MIN_GRAPH_TRACE=1 enable only GRAPH_TRACE (one record per graph launch, no per-kernel records)
// MIN_NO_KINDS=1 register the callbacks and set the attributes, but enable no activity kind
// MIN_PREALLOC=N device buffers to pre-allocate (default 3, as our production tracer)
// MIN_OFF_S=N with MIN_CYCLE_S: stay disabled N s after each disable (time-sliced tracing)
// MIN_CYCLE_S=N every N s a helper thread disables all kinds, forces a flush and enables them again
// MIN_HW_TRACE=1 a helper thread retries cuptiActivityEnableHWTrace(1) every 100 ms (up to 10 s) until the driver
// is initialized, and only then enables the activity kinds (run stress with STRESS_START_DELAY_S)
// MIN_TS_CALLBACK=1 supply timestamps through cuptiActivityRegisterTimestampCallback (CLOCK_REALTIME)
// MIN_HES=early|init HES via cuptiActivityEnableHWTrace(1): early = in InitializeInjection (as our production tracer), init = from the
// CUPTI_CBID_RESOURCE_CU_INIT_FINISHED callback (the header's recommendation), kinds enabled after it.
// Both subscribe to CUPTI_CB_DOMAIN_STATE (fallback errors are logged) and log CUPTI_ACTIVITY_ATTR_ENABLE_HES.
// MIN_KIND_KERNEL=1 CUPTI_ACTIVITY_KIND_KERNEL (serialized) instead of CONCURRENT_KERNEL
// MIN_HOST_PINNED=1 CUPTI_ACTIVITY_ATTR_MEM_ALLOCATION_TYPE_HOST_PINNED; MIN_PER_THREAD=0|1 PER_THREAD_ACTIVITY_BUFFER;
// MIN_ZEROED=1 ZEROED_OUT_ACTIVITY_BUFFER (buffers are zeroed in the request callback); MIN_DEVBUF_MB / MIN_POOL;
// MIN_THREAD_ID=system|default cuptiSetThreadIdType
// MIN_FLUSH_MS=N a helper thread calls cuptiActivityFlushAll every N ms (MIN_FLUSH_FORCED=1: with FLUSH_FORCED)
// MIN_LOG_S=N log records / kernel records / bad timestamps delivered so far every N s
// MIN_CLEAN_EXIT=1 at exit: disable kinds, forced flush, cuptiActivityEnableHWTrace(0) (if HES), cuptiFinalize
// Build: gcc -shared -fPIC -o build/libmin.so tests/min_client.c -I$CUPTI/include -L$CUPTI/lib -l:libcupti.so.13
#define _GNU_SOURCE
#include <cuda.h>
#include <cupti_callbacks.h>
#include <cupti_activity.h> // not <cupti.h>: no callback / API metadata headers, which a newer CUPTI ties to its own
#include <cupti_result.h> // CUDA runtime headers (CUPTI 13.4 from a wheel against the image's CUDA 13.0 headers)
#include <cupti_version.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
#define BUF (16u << 20)
#define KEEP 8
static atomic_ullong records, dropped;
static atomic_long worker_tid;
static int kernel_only, graph_trace;
static long off_s;
static int zeroed, hes_init;
static atomic_ullong kernel_records, kernel_ns, bad_ts;
static CUpti_SubscriberHandle sub;
static CUpti_ActivityKind KINDS[] = {CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL, CUPTI_ACTIVITY_KIND_MEMCPY,
CUPTI_ACTIVITY_KIND_MEMSET};
static pthread_mutex_t pool_lock = PTHREAD_MUTEX_INITIALIZER;
static uint8_t *pool[KEEP];
static int pool_n;
static uint64_t ns(clockid_t c) {
struct timespec t;
clock_gettime(c, &t);
return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec;
}
static uint64_t CUPTIAPI realtime_ns(void) { return ns(CLOCK_REALTIME); }
static void CUPTIAPI requested(uint8_t **buf, size_t *size, size_t *max_records) {
pthread_mutex_lock(&pool_lock);
uint8_t *b = pool_n ? pool[--pool_n] : NULL;
pthread_mutex_unlock(&pool_lock);
if (!b) {
b = mmap(NULL, BUF, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (b == MAP_FAILED) b = NULL;
}
if (b && zeroed) memset(b, 0, BUF);
*buf = b;
*size = b ? BUF : 0;
*max_records = 0;
}
static void CUPTIAPI completed(CUcontext ctx, uint32_t stream, uint8_t *buf, size_t size, size_t valid) {
(void)size;
size_t d = 0;
if (cuptiActivityGetNumDroppedRecords(ctx, stream, &d) == CUPTI_SUCCESS && d) atomic_fetch_add(&dropped, d);
static atomic_int logged;
if (!atomic_exchange(&logged, 1)) {
atomic_store(&worker_tid, (long)syscall(SYS_gettid));
fprintf(stderr, "[min] completed runs on thread %ld\n", (long)syscall(SYS_gettid));
}
CUpti_Activity *r = NULL;
unsigned long long n = 0;
unsigned long long kn = 0, kns = 0, bad = 0;
while (cuptiActivityGetNextRecord(buf, valid, &r) == CUPTI_SUCCESS) {
n++;
if (r->kind == CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL || r->kind == CUPTI_ACTIVITY_KIND_KERNEL) {
const CUpti_ActivityKernel13 *k = (const CUpti_ActivityKernel13 *)r;
kn++;
if (k->start && k->end > k->start) kns += k->end - k->start;
else bad++;
}
}
atomic_fetch_add(&kernel_records, kn), atomic_fetch_add(&kernel_ns, kns), atomic_fetch_add(&bad_ts, bad);
unsigned long long total = atomic_fetch_add(&records, n) + n;
static pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER; // a forced flush calls this from another thread
static uint64_t t0, next, cpu_prev;
static unsigned long long rec_prev;
uint64_t now = ns(CLOCK_MONOTONIC);
pthread_mutex_lock(&log_lock);
if (!t0) t0 = now, next = now + 60000000000ull;
if (now >= next && atomic_load(&worker_tid) == syscall(SYS_gettid)) { // the worker's own CPU time only
uint64_t cpu = ns(CLOCK_THREAD_CPUTIME_ID);
double dcpu = (cpu - cpu_prev) / 1e9, drec = (double)(total - rec_prev);
fprintf(stderr, "[min] %3.0f min: worker cpu %.1f s in the last minute, %.0fk records, %.3f us per record\n",
(now - t0) / 60e9, dcpu, drec / 1e3, drec ? dcpu * 1e6 / drec : 0.0);
cpu_prev = cpu, rec_prev = total, next += 60000000000ull;
}
pthread_mutex_unlock(&log_lock);
pthread_mutex_lock(&pool_lock);
int kept = pool_n < KEEP;
if (kept) pool[pool_n++] = buf;
pthread_mutex_unlock(&pool_lock);
if (!kept) munmap(buf, BUF);
}
static int enable_all(int on) {
if (graph_trace)
return (on ? cuptiActivityEnable(CUPTI_ACTIVITY_KIND_GRAPH_TRACE) : cuptiActivityDisable(CUPTI_ACTIVITY_KIND_GRAPH_TRACE)) !=
CUPTI_SUCCESS;
int n = kernel_only ? 1 : 3, bad = 0;
for (int i = 0; i < n; i++) bad |= (on ? cuptiActivityEnable(KINDS[i]) : cuptiActivityDisable(KINDS[i])) != CUPTI_SUCCESS;
return bad;
}
static void *cycler(void *arg) {
long period = (long)arg;
for (int i = 1;; i++) {
sleep((unsigned)period);
int bad = enable_all(0);
cuptiActivityFlushAll(CUPTI_ACTIVITY_FLAG_FLUSH_FORCED);
unsigned long long at_off = atomic_load(&records);
if (off_s > 0) sleep((unsigned)off_s);
cuptiActivityFlushAll(CUPTI_ACTIVITY_FLAG_FLUSH_FORCED);
unsigned long long at_on = atomic_load(&records);
bad |= enable_all(1);
fprintf(stderr, "[min] cycle %d: disabled, forced flush, %ld s off, enabled%s; records %llu at disable, %llu at enable, dropped %llu\n",
i, off_s, bad ? " (an enable/disable FAILED)" : "", at_off, at_on, (unsigned long long)atomic_load(&dropped));
}
return NULL;
}
static void *hw_trace_then_enable(void *arg) {
(void)arg;
CUptiResult r = CUPTI_ERROR_NOT_INITIALIZED;
int attempt = 0;
for (; attempt < 100 && r == CUPTI_ERROR_NOT_INITIALIZED; attempt++) {
if (attempt) usleep(100000);
r = cuptiActivityEnableHWTrace(1);
}
const char *msg = NULL;
cuptiGetResultString(r, &msg);
int bad = enable_all(1);
fprintf(stderr, "[min] cuptiActivityEnableHWTrace(1) -> %d (%s) after %d attempt(s); then activity kinds enabled%s\n",
(int)r, msg ? msg : "?", attempt, bad ? " (an enable FAILED)" : "");
return NULL;
}
static void log_hes_attr(const char *when) {
uint8_t v = 255;
size_t sz = sizeof v;
CUptiResult r = cuptiActivityGetAttribute_v2(sub, CUPTI_ACTIVITY_ATTR_ENABLE_HES, &sz, &v);
const char *msg = NULL;
cuptiGetResultString(r, &msg);
fprintf(stderr, "[min] %s: CUPTI_ACTIVITY_ATTR_ENABLE_HES = %d (query: %s)\n", when, (int)v, msg ? msg : "?");
}
static void CUPTIAPI on_callback(void *ud, CUpti_CallbackDomain domain, CUpti_CallbackId cbid, const void *data) {
(void)ud;
if (domain == CUPTI_CB_DOMAIN_STATE) {
const CUpti_StateData *sd = data;
const char *msg = NULL;
cuptiGetResultString(sd->notification.result, &msg);
fprintf(stderr, "[min] CUPTI state callback %u: %s: %s\n", cbid, msg ? msg : "?",
sd->notification.message ? sd->notification.message : "");
} else if (domain == CUPTI_CB_DOMAIN_RESOURCE && cbid == CUPTI_CBID_RESOURCE_CU_INIT_FINISHED && hes_init) {
CUptiResult r = cuptiActivityEnableHWTrace(1);
const char *msg = NULL;
cuptiGetResultString(r, &msg);
int bad = enable_all(1);
fprintf(stderr, "[min] CU_INIT_FINISHED: cuptiActivityEnableHWTrace(1) -> %s; kinds enabled%s\n", msg ? msg : "?",
bad ? " (an enable FAILED)" : "");
log_hes_attr("after CU_INIT_FINISHED");
}
}
static void set_u8(CUpti_ActivityAttribute a, uint8_t v, const char *name) {
size_t sz = sizeof v;
CUptiResult r = cuptiActivitySetAttribute(a, &sz, &v);
const char *msg = NULL;
cuptiGetResultString(r, &msg);
fprintf(stderr, "[min] set %s = %d: %s\n", name, (int)v, msg ? msg : "?");
}
static void *flusher(void *arg) {
long ms = (long)arg;
const char *ff = getenv("MIN_FLUSH_FORCED");
uint32_t flag = ff && *ff == '1' ? CUPTI_ACTIVITY_FLAG_FLUSH_FORCED : 0;
for (;;) {
usleep((useconds_t)ms * 1000);
cuptiActivityFlushAll(flag);
}
return NULL;
}
static void *logger(void *arg) {
long s = (long)arg;
for (int i = 1;; i++) {
sleep((unsigned)s);
fprintf(stderr, "[min] t=%ld s: records %llu, kernel records %llu, bad timestamps %llu, dropped %llu\n", i * s,
(unsigned long long)atomic_load(&records), (unsigned long long)atomic_load(&kernel_records),
(unsigned long long)atomic_load(&bad_ts), (unsigned long long)atomic_load(&dropped));
}
return NULL;
}
static void at_exit(void) {
cuptiActivityFlushAll(CUPTI_ACTIVITY_FLAG_FLUSH_FORCED);
unsigned long long kn = atomic_load(&kernel_records);
fprintf(stderr, "[min] records delivered: %llu, dropped: %llu, kernel records %llu, mean kernel %.1f ns, bad timestamps %llu\n",
(unsigned long long)atomic_load(&records), (unsigned long long)atomic_load(&dropped), kn,
kn ? (double)atomic_load(&kernel_ns) / kn : 0.0, (unsigned long long)atomic_load(&bad_ts));
if (sub) log_hes_attr("at exit");
const char *ce = getenv("MIN_CLEAN_EXIT");
if (ce && *ce == '1') {
int bad = enable_all(0);
cuptiActivityFlushAll(CUPTI_ACTIVITY_FLAG_FLUSH_FORCED);
CUptiResult hr = getenv("MIN_HES") ? cuptiActivityEnableHWTrace(0) : CUPTI_SUCCESS;
CUptiResult fr = cuptiFinalize();
const char *m1 = NULL, *m2 = NULL;
cuptiGetResultString(hr, &m1), cuptiGetResultString(fr, &m2);
fprintf(stderr, "[min] clean exit: kinds disabled%s, EnableHWTrace(0): %s, cuptiFinalize: %s\n", bad ? " (FAILED)" : "",
m1 ? m1 : "?", m2 ? m2 : "?");
}
}
__attribute__((visibility("default"))) int InitializeInjection(void) {
const char *ts = getenv("MIN_TS_CALLBACK");
int use_ts = ts && *ts == '1';
if (use_ts && cuptiActivityRegisterTimestampCallback(realtime_ns) != CUPTI_SUCCESS)
fprintf(stderr, "[min] timestamp callback NOT registered\n");
const char *da = getenv("MIN_DEFAULT_ATTRS");
int default_attrs = da && *da == '1';
if (!default_attrs) { // as our production tracer: 16 MB device buffers, at most 32, 3 pre-allocated
size_t v = BUF;
cuptiActivitySetAttribute(CUPTI_ACTIVITY_ATTR_DEVICE_BUFFER_SIZE, &(size_t){sizeof v}, &v);
v = 32;
cuptiActivitySetAttribute(CUPTI_ACTIVITY_ATTR_DEVICE_BUFFER_POOL_LIMIT, &(size_t){sizeof v}, &v);
const char *pa = getenv("MIN_PREALLOC");
v = pa ? (size_t)atol(pa) : 3;
cuptiActivitySetAttribute(CUPTI_ACTIVITY_ATTR_DEVICE_BUFFER_PRE_ALLOCATE_VALUE, &(size_t){sizeof v}, &v);
}
const char *hp = getenv("MIN_HOST_PINNED"), *pt = getenv("MIN_PER_THREAD"), *zo = getenv("MIN_ZEROED");
const char *db = getenv("MIN_DEVBUF_MB"), *pl = getenv("MIN_POOL"), *ti = getenv("MIN_THREAD_ID");
const char *he = getenv("MIN_HES"), *kk = getenv("MIN_KIND_KERNEL");
if (hp) set_u8(CUPTI_ACTIVITY_ATTR_MEM_ALLOCATION_TYPE_HOST_PINNED, (uint8_t)atoi(hp), "MEM_ALLOCATION_TYPE_HOST_PINNED");
if (pt) set_u8(CUPTI_ACTIVITY_ATTR_PER_THREAD_ACTIVITY_BUFFER, (uint8_t)atoi(pt), "PER_THREAD_ACTIVITY_BUFFER");
if (zo && *zo == '1') zeroed = 1, set_u8(CUPTI_ACTIVITY_ATTR_ZEROED_OUT_ACTIVITY_BUFFER, 1, "ZEROED_OUT_ACTIVITY_BUFFER");
if (db) {
size_t v = (size_t)atol(db) << 20;
cuptiActivitySetAttribute(CUPTI_ACTIVITY_ATTR_DEVICE_BUFFER_SIZE, &(size_t){sizeof v}, &v);
}
if (pl) {
size_t v = (size_t)atol(pl);
cuptiActivitySetAttribute(CUPTI_ACTIVITY_ATTR_DEVICE_BUFFER_POOL_LIMIT, &(size_t){sizeof v}, &v);
}
if (ti) cuptiSetThreadIdType(!strcmp(ti, "system") ? CUPTI_ACTIVITY_THREAD_ID_TYPE_SYSTEM : CUPTI_ACTIVITY_THREAD_ID_TYPE_DEFAULT);
if (kk && *kk == '1') KINDS[0] = CUPTI_ACTIVITY_KIND_KERNEL;
cuptiActivityRegisterCallbacks(requested, completed);
if (he) {
CUptiResult sr = cuptiSubscribe(&sub, on_callback, NULL);
if (sr == CUPTI_SUCCESS) {
cuptiEnableDomain(1, sub, CUPTI_CB_DOMAIN_STATE);
cuptiEnableCallback(1, sub, CUPTI_CB_DOMAIN_RESOURCE, CUPTI_CBID_RESOURCE_CU_INIT_FINISHED);
} else {
fprintf(stderr, "[min] cuptiSubscribe failed: %d\n", (int)sr);
}
hes_init = !strcmp(he, "init");
if (!strcmp(he, "early")) {
CUptiResult r = cuptiActivityEnableHWTrace(1);
const char *msg = NULL;
cuptiGetResultString(r, &msg);
fprintf(stderr, "[min] early cuptiActivityEnableHWTrace(1) -> %s\n", msg ? msg : "?");
log_hes_attr("after early enable");
}
}
const char *ko = getenv("MIN_KERNEL_ONLY"), *cy = getenv("MIN_CYCLE_S"), *hw = getenv("MIN_HW_TRACE");
kernel_only = ko && *ko == '1';
const char *gt = getenv("MIN_GRAPH_TRACE");
graph_trace = gt && *gt == '1';
int hw_trace = hw && *hw == '1', e = 0;
const char *nk = getenv("MIN_NO_KINDS"), *of = getenv("MIN_OFF_S");
int no_kinds = nk && *nk == '1';
off_s = of ? atol(of) : 0;
pthread_t hw_t;
if (hw_trace) pthread_create(&hw_t, NULL, hw_trace_then_enable, NULL); // kinds only after the HW trace attempt
else if (!no_kinds && !hes_init) e = enable_all(1);
const char *fm = getenv("MIN_FLUSH_MS");
pthread_t ft;
if (fm && atol(fm) > 0) pthread_create(&ft, NULL, flusher, (void *)atol(fm));
const char *ls = getenv("MIN_LOG_S");
pthread_t lt;
if (ls && atol(ls) > 0) pthread_create(<, NULL, logger, (void *)atol(ls));
long cycle_s = cy ? atol(cy) : 0;
pthread_t t;
if (cycle_s > 0) pthread_create(&t, NULL, cycler, (void *)cycle_s);
atexit(at_exit);
uint32_t version = 0;
cuptiGetVersion(&version);
fprintf(stderr, "[min] CUPTI API %u (headers %u), minimal client: %s, %s device buffer settings, timestamp callback %s, "
"cycle every %ld s, HW trace %s, enable %s\n", version, (unsigned)CUPTI_API_VERSION, graph_trace ? "graph trace only" : kernel_only ? "kernel only" : "kernel+memcpy+memset",
default_attrs ? "CUPTI's default" : "production-tracer", use_ts ? "on" : "off", cycle_s,
hw_trace ? "attempted from a helper thread" : "not attempted", hw_trace ? "deferred" : e ? "FAILED" : "ok");
return 1;
}