1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
"""Brief side-channel probe: does ring-submit latency distinguish code paths?
Over USB each fence poll is a USB round trip, so resolution is coarse, but a
path difference of even one extra poll should separate the medians.
"""
import ctypes, time, statistics
from tinygrad import Device
from tinygrad.runtime.autogen.am import am
d = Device["AMD"]; adev = d.iface.dev_impl; psp = adev.psp
SZ = ctypes.sizeof(am.struct_psp_gfx_cmd_resp); C = 28
N = 25
def timed(cid, words):
adev.vram.view(psp.cmd_paddr, SZ)[:] = b"\x00" * SZ
c = am.struct_psp_gfx_cmd_resp(cmd_id=cid)
buf = bytearray(memoryview(c).cast('B'))
for i, v in words.items():
buf[C+i*4:C+4+i*4] = int(v & 0xFFFFFFFF).to_bytes(4, 'little')
cmd = am.struct_psp_gfx_cmd_resp.from_buffer_copy(bytes(buf))
t0 = time.perf_counter()
try: psp._ring_submit(cmd)
except Exception: pass
return (time.perf_counter() - t0) * 1e3
cases = [
("0x7e invalid cmd", 0x7e, {}),
("0x0d accepted idx=5", 0x0d, {0: 5, 1: 0, 2: 1}),
("0x0d REJECTED idx=0xF", 0x0d, {0: 0xF}),
("0x0d accepted idx=0", 0x0d, {0: 0}),
("0x0c accepted idx=0", 0x0c, {0: 0}),
("0x0c REJECTED idx=4", 0x0c, {0: 4}),
("0x12 (no validation)", 0x12, {}),
("0x24 (no validation)", 0x24, {}),
("0x30 (no validation)", 0x30, {}),
("0x0f GET_FW_ATTESTATION", 0x0f, {}),
]
for _ in range(5): timed(0x7e, {}) # warm up
print(f"{'case':<26s} {'median':>9s} {'min':>9s} {'p90':>9s} (ms, n={N})")
res = {}
for label, cid, w in cases:
ts = sorted(timed(cid, w) for _ in range(N))
med = statistics.median(ts)
res[label] = med
print(f"{label:<26s} {med:>9.3f} {ts[0]:>9.3f} {ts[int(.9*len(ts))]:>9.3f}")
base = res["0x7e invalid cmd"]
print(f"\nrelative to the invalid-command baseline ({base:.3f} ms):")
for k, v in res.items():
print(f" {k:<26s} {v-base:+8.3f} ms {'<-- separable' if abs(v-base) > 0.5 else ''}")
print("\nsOS alive:", psp.is_sos_alive())
|