#!/usr/bin/env python3 """Single-trace report of a CUPTI window (tools/cupti-trace output directory). python3 scripts/profile/lct_report.py [--top N] [--gap-us 50] [--t0 MS --t1 MS] (MS relative to the window start) Sections: window summary; per-kernel-family time; copies (kind, memory kinds, sizes, achieved GB/s); compute-stream idle gaps with the host-side reason (blocking sync call, copy in flight, launch-bound); copy/compute overlap; API time by call. """ import argparse import bisect import os import sys from collections import defaultdict sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import lct as L def fmtb(n): for u, d in (("GiB", 1 << 30), ("MiB", 1 << 20), ("KiB", 1 << 10)): if n >= d: return "%.1f%s" % (n / d, u) return "%dB" % n def bucket(b): for u, d in (("1M+", 1 << 20), ("64K-1M", 1 << 16), ("4K-64K", 1 << 12)): if b >= d: return u return "<4K" def pick_compute_stream(tr): t = defaultdict(int) for k in tr.kernels: t[(k.dev, k.stream)] += k.end - k.start return max(t.items(), key=lambda kv: kv[1])[0] if t else None def overlap(u, starts, a, b): """Overlap of [a,b] with a disjoint sorted interval list (starts = its start points).""" i = max(0, bisect.bisect_right(starts, a) - 1) t = 0 while i < len(u) and u[i][0] < b: t += max(0, min(b, u[i][1]) - max(a, u[i][0])) i += 1 return t def gaps_with_reasons(tr, stream, min_gap_ns): """Idle gaps of the GPU kernel timeline with the cause. stream=None: union of all streams (the GPU is idle), else one stream. Returns [(a, b, why, prev_kernel, next_kernel, copy_ns)] where copy_ns is the overlap of the gap with any H2D/D2H copy.""" ks = sorted((k for k in tr.kernels if stream is None or (k.dev, k.stream) == stream), key=lambda k: k.start) apis = sorted(tr.apis, key=lambda a: a.start) starts = [a.start for a in apis] copies_u = L.union([(c.start, c.end) for c in tr.copies if c.ckind in (1, 2)]) cu_starts = [x[0] for x in copies_u] gaps = [] if ks: cur_end, prev = ks[0].end, ks[0] for k in ks[1:]: if k.start - cur_end >= min_gap_ns: gaps.append((cur_end, k.start, prev, k)) if k.end > cur_end: cur_end, prev = k.end, k res = [] for a, b, prev, nxt in gaps: best, best_ov, launch_cover = None, 0, 0 i = max(0, bisect.bisect_left(starts, a - 50_000_000)) while i < len(apis) and apis[i].start < b: x = apis[i] i += 1 ov = min(b, x.end) - max(a, x.start) if ov <= 0: continue nm = tr.names.get(x.name, "?") if "Launch" in nm or "Graph" in nm: launch_cover += ov if any(sx in nm for sx in ("Synchronize", "Wait", "MemcpyDtoH", "MemcpyHtoD_v2", "cudaMemcpy_")) and ov > best_ov: best, best_ov = (nm, x.thread), ov cov = overlap(copies_u, cu_starts, a, b) if cov > 0.5 * (b - a): why = "PCIe copy running, no kernel" elif best and best_ov > 0.5 * (b - a): why = "host-sync %s (thr %s)" % (best[0], tr.threads.get(best[1], best[1])) elif launch_cover > 0.3 * (b - a): why = "launch-bound (cpu issuing)" elif best: why = "host-sync(partial) %s" % best[0] else: why = "unattributed (cpu between calls / no api record)" res.append((a, b, why, prev, nxt, cov)) return res def main(): ap = argparse.ArgumentParser() ap.add_argument("dir") ap.add_argument("--top", type=int, default=25) ap.add_argument("--gap-us", type=float, default=50) ap.add_argument("--t0", type=float, default=None) ap.add_argument("--t1", type=float, default=None) ap.add_argument("--stream", type=int, default=None, help="restrict the gap analysis to one stream id (default: union of all streams)") ap.add_argument("--stamps", default=None, help="job .full log with stamped PREFILL-PROFILE lines (see lct.chunk_windows)") ap.add_argument("--chunk", type=int, default=None, help="with --stamps: clip to the Nth PREFILL-PROFILE chunk inside the window (0-based)") a = ap.parse_args() tr = L.Trace(a.dir) if a.stamps and a.chunk is not None: cw = L.chunk_windows(a.stamps, a.dir) c = cw[a.chunk] a.t0, a.t1 = c["t0_ms"], c["t1_ms"] print("chunk %d: first=%s tokens=%s total_ms=%s -> window %.1f..%.1f ms" % (a.chunk, c["first"], c["tokens"], c["total_ms"], a.t0, a.t1)) if a.t0 is not None or a.t1 is not None: lo = tr.t0 + int((a.t0 or 0) * 1e6) hi = tr.t0 + int(a.t1 * 1e6) if a.t1 is not None else tr.t1 tr.clip(lo, hi) W = (tr.t1 - tr.t0) / 1e6 print("== window %s: %.1f ms, label=%r reason=%s" % (a.dir, W, tr.meta.get("label"), tr.meta.get("stop_reason"))) print(" kernels %d, copies %d, memsets %d, syncs %d, api %d, graphs %d; unix start %.3f" % ( len(tr.kernels), len(tr.copies), len(tr.memsets), len(tr.syncs), len(tr.apis), len(tr.graphs), (tr.t0 + tr.real_off) / 1e9)) ku = L.union([(k.start, k.end) for k in tr.kernels]) print(" GPU kernel-busy (any stream): %.1f ms = %.1f%% of window" % (L.total(ku) / 1e6, 100 * L.total(ku) / max(1, tr.t1 - tr.t0))) streams = defaultdict(lambda: [0, 0]) for k in tr.kernels: s = streams[(k.dev, k.stream)] s[0] += 1 s[1] += k.end - k.start for key, (n, t) in sorted(streams.items(), key=lambda kv: -kv[1][1])[:8]: print(" stream dev%d/%d: %d kernels, %.1f ms of kernel time" % (key[0], key[1], n, t / 1e6)) print("\n== kernel families (sum of kernel durations; concurrent kernels overlap)") fam = defaultdict(lambda: [0, 0]) for k in tr.kernels: f = fam[L.family(tr.kname(k))] f[0] += 1 f[1] += k.end - k.start tot = sum(v[1] for v in fam.values()) or 1 print(" %-52s %8s %10s %9s %6s" % ("family", "count", "total ms", "mean us", "share")) for n, (c, t) in sorted(fam.items(), key=lambda kv: -kv[1][1])[: a.top]: print(" %-52s %8d %10.2f %9.1f %5.1f%%" % (n[:52], c, t / 1e6, t / 1e3 / c, 100 * t / tot)) print("\n== copies") grp = defaultdict(lambda: [0, 0, 0]) for c in tr.copies: g = grp[(L.CKIND.get(c.ckind, c.ckind), L.MKIND.get(c.skind, c.skind), L.MKIND.get(c.dkind, c.dkind), c.dev, c.stream)] g[0] += 1 g[1] += c.bytes g[2] += c.end - c.start print(" %-38s %7s %10s %9s %9s" % ("kind src->dst dev/stream", "count", "MB", "busy ms", "GB/s")) for key, (n, b, t) in sorted(grp.items(), key=lambda kv: -kv[1][1])[:12]: print(" %-38s %7d %10.1f %9.2f %9.1f" % ("%s %s->%s d%d/s%d" % key, n, b / 1e6, t / 1e6, b / max(1, t))) h2d = [c for c in tr.copies if c.ckind == 1] if h2d: sizes = sorted(c.bytes for c in h2d) gbs = sorted(c.bytes / max(1, c.end - c.start) for c in h2d if c.bytes >= 65536) print(" H2D: %d copies, %.1f MB; size p10/p50/p90/max = %s / %s / %s / %s" % ( len(h2d), sum(sizes) / 1e6, fmtb(L.percentile(sizes, 10)), fmtb(L.percentile(sizes, 50)), fmtb(L.percentile(sizes, 90)), fmtb(sizes[-1]))) if gbs: print(" H2D copies >= 64 KiB: GB/s p10/p50/p90 = %.1f / %.1f / %.1f (per copy, own start..end)" % ( L.percentile(gbs, 10), L.percentile(gbs, 50), L.percentile(gbs, 90))) hu = L.union([(c.start, c.end) for c in h2d]) print(" H2D union busy %.1f ms (%.1f%% of window), aggregate %.1f GB/s over the union" % ( L.total(hu) / 1e6, 100 * L.total(hu) / max(1, tr.t1 - tr.t0), sum(c.bytes for c in h2d) / max(1, L.total(hu)))) buckets = defaultdict(lambda: [0, 0]) for c in h2d: b = buckets[bucket(c.bytes)] b[0] += 1 b[1] += c.bytes print(" H2D size histogram (count): " + ", ".join("%s:%d" % (k, v[0]) for k, v in sorted(buckets.items(), key=lambda kv: -kv[1][1]))) cs = None if a.stream is not None: cs = next((sx for sx in streams if sx[1] == a.stream), None) ks = [k for k in tr.kernels if cs is None or (k.dev, k.stream) == cs] if ks: print("\n== GPU kernel timeline (%s) idle gaps >= %.0f us" % ("stream dev%d/%d" % cs if cs else "union of all streams", a.gap_us)) span = max(k.end for k in ks) - min(k.start for k in ks) busy = L.total(L.union([(k.start, k.end) for k in ks])) print(" span %.1f ms, busy %.1f ms, idle %.1f ms (%.1f%%)" % (span / 1e6, busy / 1e6, (span - busy) / 1e6, 100 * (span - busy) / max(1, span))) gp = gaps_with_reasons(tr, cs, a.gap_us * 1e3) why = defaultdict(lambda: [0, 0]) for g in gp: w = why[g[2].split("(thr")[0].strip()] w[0] += 1 w[1] += g[1] - g[0] print(" gaps >= %.0f us: %d, total %.1f ms (idle in smaller gaps: %.1f ms)" % (a.gap_us, len(gp), sum(g[1] - g[0] for g in gp) / 1e6, ((span - busy) - sum(g[1] - g[0] for g in gp)) / 1e6)) for r, (n, t) in sorted(why.items(), key=lambda kv: -kv[1][1])[:10]: print(" %-60s %6d gaps %9.2f ms" % (r[:60], n, t / 1e6)) print(" 10 longest gaps (ms from window start: length, reason, prev kernel -> next kernel):") for g in sorted(gp, key=lambda g: g[0] - g[1])[:10]: print(" @%9.2f %7.2f ms %s | %s -> %s" % ((g[0] - tr.t0) / 1e6, (g[1] - g[0]) / 1e6, g[2][:60], L.family(tr.kname(g[3]))[:28], L.family(tr.kname(g[4]))[:28])) cu = L.union([(c.start, c.end) for c in tr.copies if c.ckind in (1, 2)]) cku = L.union([(k.start, k.end) for k in ks]) if cu: ov = L.intersect_total(cu, cku) print(" copy/compute overlap: %.1f%% of H2D+D2H copy-busy time runs while a kernel executes (%.1f of %.1f ms)" % ( 100 * ov / max(1, L.total(cu)), ov / 1e6, L.total(cu) / 1e6)) print(" copy-only time (no kernel running): %.1f ms" % ((L.total(cu) - ov) / 1e6)) print("\n== API/sync (CPU) time by call (sum of call durations)") ap_ = defaultdict(lambda: [0, 0, 0]) for x in tr.apis: g = ap_[tr.names.get(x.name, "?")] g[0] += 1 g[1] += x.end - x.start g[2] = max(g[2], x.end - x.start) for n, (c, t, mx) in sorted(ap_.items(), key=lambda kv: -kv[1][1])[:15]: print(" %-44s %8d calls %10.2f ms mean %8.1f us max %8.1f us" % (n[:44], c, t / 1e6, t / 1e3 / c, mx / 1e3)) sy = defaultdict(lambda: [0, 0]) for y in tr.syncs: g = sy[L.SYNCTYPE.get(y.type, y.type)] g[0] += 1 g[1] += y.end - y.start for n, (c, t) in sy.items(): print(" sync activity %-22s %6d %9.2f ms" % (n, c, t / 1e6)) if __name__ == "__main__": main()