258 lines
12 KiB
Python
258 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
"""Port-vs-Original ledger from two CUPTI windows (tools/cupti-trace output directories).
|
|
|
|
python3 scripts/profile/lct_ledger.py PORT_DIR ORIG_DIR [--map scripts/profile/steps.map]
|
|
[--p-t0 MS --p-t1 MS] [--o-t0 MS --o-t1 MS] (windows relative to each trace's start; default: whole trace)
|
|
[--per-chunk] split each window into compute bursts and print a per-burst table (chunk analysis)
|
|
[--md] markdown tables
|
|
|
|
For each trace: kernel time per STEP (kernel names mapped to engine-neutral steps by steps.map; unmatched kernels keep their
|
|
family name), copies by kind with achieved GB/s, compute-stream idle time by cause, copy/compute overlap, API/sync time on the host.
|
|
Kernel time is the sum of kernel durations (concurrent kernels counted separately); the wall figures are the compute-stream span,
|
|
busy and idle time.
|
|
"""
|
|
import argparse
|
|
import os
|
|
import re
|
|
import sys
|
|
from collections import defaultdict
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
import lct as L
|
|
from lct_report import gaps_with_reasons, pick_compute_stream
|
|
|
|
|
|
def load_map(path):
|
|
rules = []
|
|
if path and os.path.exists(path):
|
|
for line in open(path):
|
|
line = line.rstrip("\n")
|
|
if not line.strip() or line.startswith("#"):
|
|
continue
|
|
pat, _, step = line.partition("\t")
|
|
rules.append((re.compile(pat), step.strip()))
|
|
return rules
|
|
|
|
|
|
def canon(fam):
|
|
"""The port's kernel names carry a leleka_ prefix and a prefill_ infix that the Original's do not."""
|
|
return re.sub(r"leleka_|prefill_", "", fam)
|
|
|
|
|
|
def step_of(rules, name):
|
|
fam = canon(L.family(name))
|
|
for rx, step in rules:
|
|
if rx.search(fam):
|
|
return step
|
|
return None
|
|
|
|
|
|
def summarize(tr, rules, gap_us):
|
|
s = {}
|
|
steps = defaultdict(lambda: [0, 0])
|
|
for k in tr.kernels:
|
|
n = tr.kname(k)
|
|
st = step_of(rules, n) or ("? " + canon(L.family(n)))
|
|
steps[st][0] += 1
|
|
steps[st][1] += k.end - k.start
|
|
s["steps"] = steps
|
|
cs = pick_compute_stream(tr)
|
|
s["cs"] = cs
|
|
ks = sorted(tr.kernels, key=lambda k: k.start) # the GPU timeline: union of all streams
|
|
s["span"] = (ks[-1].end - ks[0].start) if ks else 0
|
|
s["busy"] = L.total(L.union([(k.start, k.end) for k in ks]))
|
|
s["kernel_sum_all"] = sum(k.end - k.start for k in tr.kernels)
|
|
s["kernels"] = len(tr.kernels)
|
|
gp = gaps_with_reasons(tr, None, gap_us * 1e3) if ks else []
|
|
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]
|
|
s["gaps"] = why
|
|
s["gap_total"] = sum(g[1] - g[0] for g in gp)
|
|
s["gaps_n"] = len(gp)
|
|
small = 0
|
|
for a, b in zip(ks, ks[1:]):
|
|
d = b.start - a.end
|
|
if 0 < d < gap_us * 1e3:
|
|
small += d
|
|
s["small_gap_total"] = small
|
|
cp = defaultdict(lambda: [0, 0, 0])
|
|
for c in tr.copies:
|
|
key = "%s %s->%s" % (L.CKIND.get(c.ckind, c.ckind), L.MKIND.get(c.skind, c.skind), L.MKIND.get(c.dkind, c.dkind))
|
|
cp[key][0] += 1
|
|
cp[key][1] += c.bytes
|
|
cp[key][2] += c.end - c.start
|
|
s["copies"] = cp
|
|
h2d = [c for c in tr.copies if c.ckind == 1]
|
|
hu = L.union([(c.start, c.end) for c in h2d])
|
|
s["h2d_bytes"] = sum(c.bytes for c in h2d)
|
|
s["h2d_union"] = L.total(hu)
|
|
s["h2d_n"] = len(h2d)
|
|
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])
|
|
s["copy_busy"] = L.total(cu)
|
|
s["copy_overlap"] = L.intersect_total(cu, cku)
|
|
api = defaultdict(lambda: [0, 0])
|
|
for x in tr.apis:
|
|
a = api[tr.names.get(x.name, "?")]
|
|
a[0] += 1
|
|
a[1] += x.end - x.start
|
|
s["api"] = api
|
|
s["win"] = tr.t1 - tr.t0
|
|
return s
|
|
|
|
|
|
def bursts(tr, cs, split_ms):
|
|
"""Compute-stream bursts separated by idle gaps >= split_ms."""
|
|
ks = sorted((k for k in tr.kernels if (k.dev, k.stream) == cs), key=lambda k: k.start)
|
|
out = []
|
|
cur = None
|
|
for k in ks:
|
|
if cur and k.start - cur[1] < split_ms * 1e6:
|
|
cur[1] = max(cur[1], k.end)
|
|
cur[2] += 1
|
|
cur[3] += k.end - k.start
|
|
else:
|
|
if cur:
|
|
out.append(cur)
|
|
cur = [k.start, k.end, 1, k.end - k.start]
|
|
if cur:
|
|
out.append(cur)
|
|
return out
|
|
|
|
|
|
def marker_window(tr, family, n, end_gap_ms=30.0):
|
|
"""(t0_ms, t1_ms) relative to the trace start of the chunk that begins at the n-th kernel of `family`: it ends at the next
|
|
marker, or, for the last one, at the last kernel before the first idle gap of the whole GPU >= end_gap_ms (decode follows)."""
|
|
starts = sorted(k.start for k in tr.kernels if L.family(tr.kname(k)) == family)
|
|
if n >= len(starts):
|
|
raise SystemExit("marker %s occurs %d times, chunk %d requested" % (family, len(starts), n))
|
|
a = starts[n]
|
|
if n + 1 < len(starts):
|
|
b = starts[n + 1]
|
|
else:
|
|
ks = sorted((k for k in tr.kernels if k.start >= a), key=lambda k: k.start)
|
|
cur = ks[0].end
|
|
b = cur
|
|
for k in ks[1:]:
|
|
if k.start - cur >= end_gap_ms * 1e6 and k.start - a > 300e6:
|
|
break
|
|
cur = max(cur, k.end)
|
|
b = cur
|
|
return (a - tr.t0) / 1e6, (b - tr.t0) / 1e6
|
|
|
|
|
|
def ms(x):
|
|
return "%.1f" % (x / 1e6)
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("port")
|
|
ap.add_argument("orig")
|
|
ap.add_argument("--map", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "steps.map"))
|
|
ap.add_argument("--p-t0", type=float)
|
|
ap.add_argument("--p-t1", type=float)
|
|
ap.add_argument("--o-t0", type=float)
|
|
ap.add_argument("--o-t1", type=float)
|
|
ap.add_argument("--gap-us", type=float, default=50)
|
|
ap.add_argument("--top", type=int, default=40)
|
|
ap.add_argument("--per-chunk", type=float, default=0, help="print compute bursts split at gaps >= this many ms")
|
|
ap.add_argument("--p-marker", help="kernel family that starts a chunk in the port trace")
|
|
ap.add_argument("--o-marker", help="kernel family that starts a chunk in the original trace")
|
|
ap.add_argument("--chunk", type=int, default=None, help="with --p-marker/--o-marker: which chunk (0-based) to compare")
|
|
a = ap.parse_args()
|
|
rules = load_map(a.map)
|
|
# explicit windows are applied while loading (a 180k trace has millions of records)
|
|
pw = (a.p_t0 or 0, a.p_t1) if a.chunk is None and a.p_t1 is not None else None
|
|
ow = (a.o_t0 or 0, a.o_t1) if a.chunk is None and a.o_t1 is not None else None
|
|
P = L.Trace(a.port, window_ms=pw)
|
|
O = L.Trace(a.orig, window_ms=ow)
|
|
if a.chunk is not None:
|
|
if a.p_marker:
|
|
a.p_t0, a.p_t1 = marker_window(P, a.p_marker, a.chunk)
|
|
if a.o_marker:
|
|
a.o_t0, a.o_t1 = marker_window(O, a.o_marker, a.chunk)
|
|
print("chunk %d: port window %.1f..%.1f ms (%.1f ms), original window %.1f..%.1f ms (%.1f ms)\n" % (
|
|
a.chunk, a.p_t0, a.p_t1, a.p_t1 - a.p_t0, a.o_t0, a.o_t1, a.o_t1 - a.o_t0))
|
|
for tr, t0, t1, pre in ((P, a.p_t0, a.p_t1, pw), (O, a.o_t0, a.o_t1, ow)):
|
|
if pre is None and (t0 is not None or t1 is not None):
|
|
lo = tr.t0 + int((t0 or 0) * 1e6)
|
|
hi = tr.t0 + int(t1 * 1e6) if t1 is not None else tr.t1
|
|
tr.clip(lo, hi)
|
|
sp, so = summarize(P, rules, a.gap_us), summarize(O, rules, a.gap_us)
|
|
|
|
print("| metric | port | original | port - original |")
|
|
print("| --- | ---: | ---: | ---: |")
|
|
|
|
def row(name, x, y, f=ms, unit=""):
|
|
print("| %s | %s%s | %s%s | %s%s |" % (name, f(x), unit, f(y), unit, f(x - y), unit))
|
|
|
|
row("window (ms)", sp["win"], so["win"])
|
|
row("GPU kernel timeline span (ms)", sp["span"], so["span"])
|
|
row("GPU busy: union of all streams (ms)", sp["busy"], so["busy"])
|
|
row("GPU idle in gaps >= %d us (ms)" % a.gap_us, sp["gap_total"], so["gap_total"])
|
|
row("GPU idle in gaps < %d us (ms)" % a.gap_us, sp["small_gap_total"], so["small_gap_total"])
|
|
row("kernel launches", sp["kernels"], so["kernels"], f=lambda v: "%d" % v)
|
|
row("sum of kernel durations, all streams (ms)", sp["kernel_sum_all"], so["kernel_sum_all"])
|
|
row("H2D copies", sp["h2d_n"], so["h2d_n"], f=lambda v: "%d" % v)
|
|
row("H2D bytes (GB)", sp["h2d_bytes"], so["h2d_bytes"], f=lambda v: "%.2f" % (v / 1e9))
|
|
row("H2D union busy (ms)", sp["h2d_union"], so["h2d_union"])
|
|
print("| H2D GB/s over the union | %.1f | %.1f | %.1f |" % (
|
|
sp["h2d_bytes"] / max(1, sp["h2d_union"]), so["h2d_bytes"] / max(1, so["h2d_union"]),
|
|
sp["h2d_bytes"] / max(1, sp["h2d_union"]) - so["h2d_bytes"] / max(1, so["h2d_union"])))
|
|
print("| copy busy overlapped by compute | %.0f%% | %.0f%% | |" % (
|
|
100 * sp["copy_overlap"] / max(1, sp["copy_busy"]), 100 * so["copy_overlap"] / max(1, so["copy_busy"])))
|
|
row("copy-only time, no kernel running (ms)", sp["copy_busy"] - sp["copy_overlap"], so["copy_busy"] - so["copy_overlap"])
|
|
|
|
print("\n| step | port n | port ms | orig n | orig ms | delta ms |")
|
|
print("| --- | ---: | ---: | ---: | ---: | ---: |")
|
|
keys = set(sp["steps"]) | set(so["steps"])
|
|
order = sorted(keys, key=lambda k: -(sp["steps"].get(k, [0, 0])[1] - so["steps"].get(k, [0, 0])[1]))
|
|
shown = 0
|
|
rest = [0, 0, 0, 0]
|
|
for k in order:
|
|
pn, pt = sp["steps"].get(k, [0, 0])
|
|
on, ot = so["steps"].get(k, [0, 0])
|
|
if shown < a.top or abs(pt - ot) > 5e6:
|
|
print("| %s | %d | %s | %d | %s | %s |" % (k[:60], pn, ms(pt), on, ms(ot), ms(pt - ot)))
|
|
shown += 1
|
|
else:
|
|
rest[0] += pn; rest[1] += pt; rest[2] += on; rest[3] += ot
|
|
if rest[0] or rest[2]:
|
|
print("| (others) | %d | %s | %d | %s | %s |" % (rest[0], ms(rest[1]), rest[2], ms(rest[3]), ms(rest[1] - rest[3])))
|
|
|
|
print("\n| copy kind | port n | port MB | port busy ms | port GB/s | orig n | orig MB | orig busy ms | orig GB/s |")
|
|
print("| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |")
|
|
for k in sorted(set(sp["copies"]) | set(so["copies"])):
|
|
pn, pb, pt = sp["copies"].get(k, [0, 0, 0])
|
|
on, ob, ot = so["copies"].get(k, [0, 0, 0])
|
|
print("| %s | %d | %.1f | %s | %.1f | %d | %.1f | %s | %.1f |" % (k, pn, pb / 1e6, ms(pt), pb / max(1, pt), on, ob / 1e6, ms(ot), ob / max(1, ot)))
|
|
|
|
print("\n| compute-idle cause (gaps >= %d us) | port gaps | port ms | orig gaps | orig ms |" % a.gap_us)
|
|
print("| --- | ---: | ---: | ---: | ---: |")
|
|
for k in sorted(set(sp["gaps"]) | set(so["gaps"]), key=lambda k: -(sp["gaps"].get(k, [0, 0])[1] + so["gaps"].get(k, [0, 0])[1])):
|
|
pn, pt = sp["gaps"].get(k, [0, 0])
|
|
on, ot = so["gaps"].get(k, [0, 0])
|
|
print("| %s | %d | %s | %d | %s |" % (k[:70], pn, ms(pt), on, ms(ot)))
|
|
|
|
print("\n| host API (CPU) | port calls | port ms | orig calls | orig ms |")
|
|
print("| --- | ---: | ---: | ---: | ---: |")
|
|
keys = set(sp["api"]) | set(so["api"])
|
|
for k in sorted(keys, key=lambda k: -max(sp["api"].get(k, [0, 0])[1], so["api"].get(k, [0, 0])[1]))[:20]:
|
|
pn, pt = sp["api"].get(k, [0, 0])
|
|
on, ot = so["api"].get(k, [0, 0])
|
|
print("| %s | %d | %s | %d | %s |" % (k[:50], pn, ms(pt), on, ms(ot)))
|
|
|
|
if a.per_chunk:
|
|
for nm, tr, s in (("port", P, sp), ("original", O, so)):
|
|
print("\n%s compute bursts (split at idle gaps >= %.1f ms): start ms, length ms, kernels, busy ms" % (nm, a.per_chunk))
|
|
for b in bursts(tr, s["cs"], a.per_chunk):
|
|
print(" @%9.1f %8.1f ms %7d kernels busy %8.1f" % ((b[0] - tr.t0) / 1e6, (b[1] - b[0]) / 1e6, b[2], b[3] / 1e6))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|