mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 15:28:40 +00:00
The Intel reader (serve/telemetry.py _Intel, #5353) took load and VRAM only from the root sampler (gpustat.py / gpustat.service), so a plain install showed both empty. Taken from PR #5994 (Rob Riggs), without its sampler removal: the sampler stays and still wins when it runs. - Load: the GPU busy time of the processes this user owns, from /proc/<pid>/fdinfo (xe: drm-cycles / drm-total-cycles; i915: drm-engine-* ns over wall time). - VRAM: Level Zero sysman (libze_loader.so.1, _Zes, matched to the card by PCI address) for the total; on i915 sysman reports used = 0 while the engine holds gigabytes, so the used figure there is the engine's own fdinfo memory (drm-resident-local*). xe keeps sysman's used figure, as PR #5994 measured on a B70. - PCIe rx / tx from sysman where the host grants Intel PMT; None otherwise, never 0. - No reading is None, never 0. sycl/serve/xe_telemetry.py now re-exports the shared reader. Tests: serve/test_telemetry_intel.py 12 passed (6 new RootlessLoadAndVram cases fail with 309e0b0c6's telemetry.py); serve monitor / telemetry tests 53 passed; tests/sycl/test_intel_two_card_spec.py 12 passed (l620-20261116-1737-f209). Live on the A750 (i915, p1006-20261116-1737-aa80) as an ordinary user while our engine decoded 512 tokens: load 54-100 %, VRAM used 7,079,215,104 of 8,522,825,728 bytes (sysman total, fdinfo drm-resident-local0 6,913,296 KiB), 61-66 C, 104 W of 180 W, PCIe gen 4 x16; PCIe traffic None (PMT is root-only on this host). Co-authored-by: Rob Riggs <colorado.rob@gmail.com>
This commit is contained in:
1 parent
abcc03f29e
commit
6ebbfdfa8b
3 files changed
+292
-123
No files matched your search
+171
-4
@@ -352,18 +352,116 @@ ARC_NAMES = {"e223": "Arc Pro B70", "e221": "Arc Pro B60", "e211": "Arc Pro B60"
|
||||
"56a6": "Arc A310", "5690": "Arc A770M"}
|
||||
|
||||
|
||||
class _ZesPciAddress(ctypes.Structure):
|
||||
_fields_ = [("domain", ctypes.c_uint32), ("bus", ctypes.c_uint32), ("device", ctypes.c_uint32), ("function", ctypes.c_uint32)]
|
||||
|
||||
|
||||
class _ZesPciSpeed(ctypes.Structure):
|
||||
_fields_ = [("gen", ctypes.c_int32), ("width", ctypes.c_int32), ("max_bandwidth", ctypes.c_int64)]
|
||||
|
||||
|
||||
class _ZesPciProps(ctypes.Structure): # zes_pci_properties_t
|
||||
_fields_ = [("stype", ctypes.c_uint32), ("pnext", ctypes.c_void_p), ("address", _ZesPciAddress), ("max_speed", _ZesPciSpeed),
|
||||
("bw", ctypes.c_uint8), ("pkt", ctypes.c_uint8), ("replay", ctypes.c_uint8)]
|
||||
|
||||
|
||||
class _ZesMemState(ctypes.Structure): # zes_mem_state_t
|
||||
_fields_ = [("stype", ctypes.c_uint32), ("pnext", ctypes.c_void_p), ("health", ctypes.c_int32), ("free", ctypes.c_uint64),
|
||||
("size", ctypes.c_uint64)]
|
||||
|
||||
|
||||
class _ZesPciStats(ctypes.Structure): # zes_pci_stats_t: monotonic counters, timestamp in microseconds
|
||||
_fields_ = [("timestamp", ctypes.c_uint64), ("replayCounter", ctypes.c_uint64), ("packetCounter", ctypes.c_uint64),
|
||||
("rxCounter", ctypes.c_uint64), ("txCounter", ctypes.c_uint64), ("speed", _ZesPciSpeed)]
|
||||
|
||||
|
||||
class _Zes:
|
||||
"""#5988 (PR #5994, Rob Riggs): an Intel card's VRAM and PCIe counters through Level Zero's management API (sysman,
|
||||
libze_loader.so.1, installed with the GPU runtime), matched to the card by its PCI address so a PC with two Intel cards reports
|
||||
the one asked for. Runs as the calling user (xpu-smi stats reads the same API without root). The PCIe counters come from Intel
|
||||
PMT, whose sysfs node is root-only unless the host grants it: then sysman refuses and the reading is None, never a zero."""
|
||||
|
||||
STYPE_PCI_PROPERTIES, STYPE_MEM_STATE = 0x2, 0x1E
|
||||
|
||||
def __init__(self, pci, lib=None):
|
||||
self.lib, self.mems, self.dev = None, [], None
|
||||
try:
|
||||
self.lib = lib or ctypes.CDLL("libze_loader.so.1")
|
||||
if self.lib.zesInit(ctypes.c_uint32(0)) != 0:
|
||||
return
|
||||
n = ctypes.c_uint32(0)
|
||||
if self.lib.zesDriverGet(ctypes.byref(n), None) != 0 or n.value == 0:
|
||||
return
|
||||
drivers = (ctypes.c_void_p * n.value)()
|
||||
self.lib.zesDriverGet(ctypes.byref(n), drivers)
|
||||
for drv in drivers:
|
||||
m = ctypes.c_uint32(0)
|
||||
if self.lib.zesDeviceGet(ctypes.c_void_p(drv), ctypes.byref(m), None) != 0:
|
||||
continue
|
||||
devs = (ctypes.c_void_p * m.value)()
|
||||
self.lib.zesDeviceGet(ctypes.c_void_p(drv), ctypes.byref(m), devs)
|
||||
for dev in devs:
|
||||
props = _ZesPciProps(stype=self.STYPE_PCI_PROPERTIES)
|
||||
if self.lib.zesDevicePciGetProperties(ctypes.c_void_p(dev), ctypes.byref(props)) != 0:
|
||||
continue
|
||||
a = props.address
|
||||
if f"{a.domain:04x}:{a.bus:02x}:{a.device:02x}.{a.function:x}" != pci:
|
||||
continue
|
||||
self.dev = ctypes.c_void_p(dev)
|
||||
k = ctypes.c_uint32(0)
|
||||
if self.lib.zesDeviceEnumMemoryModules(ctypes.c_void_p(dev), ctypes.byref(k), None) != 0:
|
||||
continue
|
||||
mods = (ctypes.c_void_p * k.value)()
|
||||
self.lib.zesDeviceEnumMemoryModules(ctypes.c_void_p(dev), ctypes.byref(k), mods)
|
||||
self.mems = list(mods)
|
||||
except (OSError, AttributeError, TypeError):
|
||||
self.mems = []
|
||||
|
||||
def read(self):
|
||||
"""(used, total) bytes over the card's memory modules, or (None, None) when sysman cannot say."""
|
||||
free = total = 0
|
||||
if self.lib is None or not self.mems:
|
||||
return None, None
|
||||
for mod in self.mems:
|
||||
st = _ZesMemState(stype=self.STYPE_MEM_STATE)
|
||||
try:
|
||||
if self.lib.zesMemoryGetState(ctypes.c_void_p(mod), ctypes.byref(st)) != 0:
|
||||
return None, None
|
||||
except (OSError, AttributeError, TypeError):
|
||||
return None, None
|
||||
free += st.free
|
||||
total += st.size
|
||||
return (total - free, total) if total else (None, None)
|
||||
|
||||
def pci_stats(self):
|
||||
"""(rx bytes, tx bytes, timestamp in us) of the card's PCIe link, or None (sysman refused or has none)."""
|
||||
if self.lib is None or self.dev is None:
|
||||
return None
|
||||
st = _ZesPciStats()
|
||||
try:
|
||||
if self.lib.zesDevicePciGetStats(self.dev, ctypes.byref(st)) != 0:
|
||||
return None
|
||||
except (OSError, AttributeError, TypeError):
|
||||
return None
|
||||
return st.rxCounter, st.txCounter, st.timestamp
|
||||
|
||||
|
||||
class _Intel:
|
||||
"""#5353: an Intel Arc card's readings from sysfs, for every route that starts serve/server.py (the packaged Terminal and app,
|
||||
the container), not only sycl/serve/server_intel.py. The discrete Intel cards in sysfs order (as setup numbers them): the xe
|
||||
driver's (a tile0 folder) and the i915 driver's (an Arc A-series has a hwmon; an integrated Intel GPU has neither, so a UHD
|
||||
iGPU beside the card is not taken for it). Temperature (xe's "pkg" sensor, else temp1), power (from the energy counter between
|
||||
two readings), power limit and the PCIe link are world-readable; load and VRAM need root on both drivers (per-client fdinfo),
|
||||
so they come only from the optional root sampler's /run/gpustat.json (sycl/tools/gpustat.py) and are otherwise absent."""
|
||||
two readings), power limit and the PCIe link are world-readable sysfs. #5988 (PR #5994, Rob Riggs): VRAM and PCIe traffic from
|
||||
Level Zero sysman (_Zes), and the load of the GPU clients this user can see in /proc/<pid>/fdinfo (the engine the server
|
||||
started: xe counts cycles, i915 nanoseconds), so no root and no helper service are needed. The card-wide load across other
|
||||
users' processes would need root: this is the engine's own load. A fresh /run/gpustat.json from the optional root sampler
|
||||
(sycl/tools/gpustat.py) still wins for load and VRAM. A reading no source gives is None, never a zero."""
|
||||
STAT = "/run/gpustat.json"
|
||||
|
||||
def __init__(self, index=0, sysfs=None, stat=None):
|
||||
def __init__(self, index=0, sysfs=None, stat=None, proc=None, zes=None):
|
||||
import glob
|
||||
root = sysfs or "/sys"
|
||||
self.proc = proc or "/proc"
|
||||
self.stat_path = stat or self.STAT
|
||||
self.dev = None
|
||||
cards = []
|
||||
@@ -382,6 +480,12 @@ class _Intel:
|
||||
for n in sorted(glob.glob(f"{self.dev}/hwmon/hwmon*/name")) if self.dev else []:
|
||||
self.hwmon = os.path.dirname(n)
|
||||
self._e = None # (time, energy in uJ) of the last reading
|
||||
self.pci = os.path.basename(self.dev) if self.dev else "" # "0000:03:00.0", as fdinfo's drm-pdev names it
|
||||
self.zes = zes if zes is not None else (_Zes(self.pci) if self.dev and sys.platform.startswith("linux") else None)
|
||||
self._busy_prev = {} # client id -> (busy, total) of the last read
|
||||
self._fd_mem = None # bytes the DRM clients this user can see hold on the card (fdinfo), last read
|
||||
self.driver = ("xe" if os.path.isdir(f"{self.dev}/tile0") else "i915") if self.dev else None
|
||||
self._pci = None # (rx bytes, tx bytes, ts us) of the last read
|
||||
|
||||
def ok(self):
|
||||
return self.dev is not None and sys.platform.startswith("linux")
|
||||
@@ -426,6 +530,53 @@ class _Intel:
|
||||
return g(p, "current"), min(x for x in (card, slot) if x) if (card or slot) else None, w
|
||||
return None, None, None
|
||||
|
||||
def _busy(self):
|
||||
"""#5988 (PR #5994): the load of the DRM clients on this card that this user can read in <proc>/<pid>/fdinfo, as a
|
||||
percentage between two reads. xe: render + compute cycles (per engine capacity) over the total cycles; i915: render +
|
||||
compute busy nanoseconds over the wall time. None until two reads can be compared."""
|
||||
import glob
|
||||
now, wall, mem = {}, time.monotonic_ns(), {}
|
||||
for f in glob.glob(f"{self.proc}/[0-9]*/fdinfo/*"):
|
||||
try:
|
||||
with open(f, encoding="ascii", errors="replace") as fh:
|
||||
text = fh.read()
|
||||
except OSError:
|
||||
continue
|
||||
if f"drm-pdev:\t{self.pci}" not in text:
|
||||
continue
|
||||
kv = {}
|
||||
for line in text.splitlines():
|
||||
k, _, v = line.partition(":")
|
||||
kv.setdefault(k, v.strip())
|
||||
cid = kv.get("drm-client-id")
|
||||
try:
|
||||
if kv.get("drm-driver") == "xe":
|
||||
busy = sum(int(kv.get(f"drm-cycles-{e}", "0")) / int(kv.get(f"drm-engine-capacity-{e}", "1") or 1) for e in ("rcs", "ccs"))
|
||||
total = int(kv.get("drm-total-cycles-ccs") or kv.get("drm-total-cycles-rcs") or 0)
|
||||
elif kv.get("drm-driver") == "i915":
|
||||
busy = sum(int((kv.get(f"drm-engine-{e}") or "0 ns").split()[0]) for e in ("render", "compute"))
|
||||
total = wall
|
||||
else:
|
||||
continue
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
if cid and total:
|
||||
now[(f.split("/fdinfo/")[0], cid)] = (busy, total)
|
||||
if cid: # the client's memory on this card (i915: drm-resident-local0, xe: drm-resident-vram0), "N KiB"
|
||||
for k, v in kv.items():
|
||||
if re.fullmatch(r"drm-resident-(local|vram)\d+", k):
|
||||
parts = v.split()
|
||||
if parts and parts[0].isdigit():
|
||||
unit = {"KiB": 2**10, "MiB": 2**20, "GiB": 2**30}.get(parts[1] if len(parts) > 1 else "", 1)
|
||||
mem[(f.split("/fdinfo/")[0], cid, k)] = int(parts[0]) * unit
|
||||
self._fd_mem = sum(mem.values()) if mem else None
|
||||
prev, self._busy_prev = self._busy_prev, now
|
||||
spans = [now[c][1] - prev[c][1] for c in now if c in prev and now[c][1] > prev[c][1]]
|
||||
if not spans:
|
||||
return 0.0 if (now and prev) else None
|
||||
used = sum(max(0, now[c][0] - prev[c][0]) for c in now if c in prev)
|
||||
return max(0.0, min(100.0, 100.0 * used / max(spans)))
|
||||
|
||||
def read(self):
|
||||
out = {"util": None, "mem_used": None, "mem_total": None, "temp": None, "power": None, "power_limit": None}
|
||||
st = self._stat()
|
||||
@@ -437,6 +588,16 @@ class _Intel:
|
||||
out["temp"] = st.get("temp_pkg")
|
||||
out["power"] = st.get("power_w")
|
||||
out["power_limit"] = st.get("power_cap_w")
|
||||
busy = self._busy() # read every time, so the next read has a span to compare
|
||||
if out["util"] is None:
|
||||
out["util"] = busy
|
||||
if out["mem_used"] is None:
|
||||
used, total = self.zes.read() if self.zes is not None else (None, None)
|
||||
if self.driver == "i915":
|
||||
# sysman on i915 (Arc A-series) reports the card's size but no use: free == size, so "used 0" while an engine holds
|
||||
# gigabytes (measured on an A750, p1006 11-16). The engine's own DRM clients' resident memory is the reading there.
|
||||
used = self._fd_mem
|
||||
out["mem_used"], out["mem_total"] = used, total
|
||||
if self.hwmon:
|
||||
if out["temp"] is None:
|
||||
files = os.listdir(self.hwmon)
|
||||
@@ -456,7 +617,13 @@ class _Intel:
|
||||
out["power_limit"] = int(v) / 1e6
|
||||
break
|
||||
out["pcie_gen"], out["pcie_gen_max"], out["pcie_width"] = self._link()
|
||||
out["pcie_rx_mb"] = out["pcie_tx_mb"] = None # neither driver exposes PCIe traffic counters
|
||||
out["pcie_rx_mb"] = out["pcie_tx_mb"] = None # sysfs has no PCIe traffic counters; sysman has them when the host allows
|
||||
st = self.zes.pci_stats() if self.zes is not None else None
|
||||
prev, self._pci = self._pci, st
|
||||
if st and prev and st[2] > prev[2] and st[0] >= prev[0] and st[1] >= prev[1]:
|
||||
dt = (st[2] - prev[2]) / 1e6
|
||||
out["pcie_rx_mb"] = (st[0] - prev[0]) / dt / 2**20
|
||||
out["pcie_tx_mb"] = (st[1] - prev[1]) / dt / 2**20
|
||||
return out
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
Before, serve/telemetry.py knew NVML, amdgpu and Metal only; the Intel reader lived in sycl/serve/xe_telemetry.py and only the source
|
||||
route through sycl/serve/server_intel.py installed it, so the container, the packaged Terminal and the app showed no GPU readings on an
|
||||
Arc. The fake trees: an xe Arc Pro B70 beside a UHD iGPU (card0, no hwmon), and an i915 Arc A750 (no tile0, hwmon "i915")."""
|
||||
Arc. The fake trees: an xe Arc Pro B70 beside a UHD iGPU (card0, no hwmon), and an i915 Arc A750 (no tile0, hwmon "i915").
|
||||
#5988 (from PR #5994): the engine's load from its DRM clients' fdinfo (xe cycles, i915 nanoseconds) and VRAM / PCIe traffic from Level
|
||||
Zero sysman (a fake library object here), without root or the sampler."""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
@@ -16,6 +18,15 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from serve import telemetry # noqa: E402
|
||||
|
||||
|
||||
class NoZes:
|
||||
"""sysman with nothing to say (no Level Zero, or the card not found)."""
|
||||
def read(self):
|
||||
return None, None
|
||||
|
||||
def pci_stats(self):
|
||||
return None
|
||||
|
||||
|
||||
def write(p, text):
|
||||
p.parent.mkdir(parents=True, exist_ok=True)
|
||||
p.write_text(text)
|
||||
@@ -68,7 +79,7 @@ class IntelSysfsReader(unittest.TestCase):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
root = Path(d)
|
||||
h = b70_tree(root)
|
||||
r = telemetry._Intel(0, sysfs=str(root), stat=str(root / "no-gpustat.json"))
|
||||
r = telemetry._Intel(0, sysfs=str(root), stat=str(root / "no-gpustat.json"), proc=str(root / "proc"), zes=NoZes())
|
||||
self.assertTrue(r.ok(), "the B70 (xe, tile0), not the UHD iGPU at card0")
|
||||
self.assertEqual(r.name(), "Intel Arc Pro B70")
|
||||
first = r.read()
|
||||
@@ -82,13 +93,13 @@ class IntelSysfsReader(unittest.TestCase):
|
||||
write(h / "energy1_input", "51000000\n") # +50 J
|
||||
second = r.read()
|
||||
self.assertAlmostEqual(second["power"], 50.0, delta=5.0)
|
||||
self.assertFalse(telemetry._Intel(1, sysfs=str(root)).ok(), "only one discrete Intel card here")
|
||||
self.assertFalse(telemetry._Intel(1, sysfs=str(root), zes=NoZes()).ok(), "only one discrete Intel card here")
|
||||
|
||||
def test_a750_on_i915_is_found_and_read(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
root = Path(d)
|
||||
a750_tree(root)
|
||||
r = telemetry._Intel(0, sysfs=str(root), stat=str(root / "no-gpustat.json"))
|
||||
r = telemetry._Intel(0, sysfs=str(root), stat=str(root / "no-gpustat.json"), proc=str(root / "proc"), zes=NoZes())
|
||||
self.assertTrue(r.ok(), "an i915 Arc has no tile0; its hwmon marks it as a discrete card")
|
||||
self.assertEqual(r.name(), "Intel Arc A750")
|
||||
out = r.read()
|
||||
@@ -102,10 +113,107 @@ class IntelSysfsReader(unittest.TestCase):
|
||||
b70_tree(root)
|
||||
stat = root / "gpustat.json"
|
||||
stat.write_text(json.dumps({"ts": time.time(), "busy_pct": 37, "vram_used_mb": 1024, "vram_total_mb": 32768}))
|
||||
out = telemetry._Intel(0, sysfs=str(root), stat=str(stat)).read()
|
||||
out = telemetry._Intel(0, sysfs=str(root), stat=str(stat), proc=str(root / "proc"), zes=NoZes()).read()
|
||||
self.assertEqual((out["util"], out["mem_used"], out["mem_total"]), (37, 1024 * 2**20, 32768 * 2**20))
|
||||
stat.write_text(json.dumps({"ts": time.time() - 60, "busy_pct": 99}))
|
||||
self.assertIsNone(telemetry._Intel(0, sysfs=str(root), stat=str(stat)).read()["util"], "a stale sampler file is not read")
|
||||
self.assertIsNone(telemetry._Intel(0, sysfs=str(root), stat=str(stat), proc=str(root / "proc"), zes=NoZes()).read()["util"],
|
||||
"a stale sampler file is not read")
|
||||
|
||||
|
||||
def fdinfo(root, pid, fd, text):
|
||||
write(root / "proc" / str(pid) / "fdinfo" / str(fd), text)
|
||||
|
||||
|
||||
class FakeZes:
|
||||
"""sysman with a card: 30 of 32 GiB free memory, PCIe counters that move 2 GiB in and 1 GiB out per second."""
|
||||
def __init__(self):
|
||||
self.t = 1_000_000
|
||||
|
||||
def read(self):
|
||||
return 2 * 2**30, 32 * 2**30
|
||||
|
||||
def pci_stats(self):
|
||||
self.t += 1_000_000
|
||||
k = self.t // 1_000_000
|
||||
return k * 2 * 2**30, k * 2**30, self.t
|
||||
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux") and hasattr(os, "symlink"), "sysfs trees with symlinks: Linux")
|
||||
class RootlessLoadAndVram(unittest.TestCase):
|
||||
def test_xe_load_from_the_engines_fdinfo_cycles(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
root = Path(d)
|
||||
b70_tree(root)
|
||||
other = "drm-driver:\txe\ndrm-pdev:\t0000:09:00.0\ndrm-client-id:\t7\ndrm-cycles-ccs:\t999\ndrm-total-cycles-ccs:\t1000\n"
|
||||
fdinfo(root, 222, 4, other) # another card's client: not counted
|
||||
def mine(busy, total):
|
||||
fdinfo(root, 111, 5, f"drm-driver:\txe\ndrm-pdev:\t0000:03:00.0\ndrm-client-id:\t42\n"
|
||||
f"drm-cycles-rcs:\t0\ndrm-cycles-ccs:\t{busy}\ndrm-total-cycles-ccs:\t{total}\n")
|
||||
mine(1000, 10000)
|
||||
r = telemetry._Intel(0, sysfs=str(root), stat=str(root / "none.json"), proc=str(root / "proc"), zes=NoZes())
|
||||
self.assertIsNone(r.read()["util"], "one read: nothing to compare yet")
|
||||
mine(1000 + 4000, 10000 + 10000) # 4000 busy of 10000 cycles
|
||||
self.assertAlmostEqual(r.read()["util"], 40.0, places=3)
|
||||
mine(5000, 30000) # idle
|
||||
self.assertEqual(r.read()["util"], 0.0)
|
||||
|
||||
def test_i915_load_from_busy_nanoseconds_over_wall_time(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
root = Path(d)
|
||||
a750_tree(root)
|
||||
def mine(ns):
|
||||
fdinfo(root, 333, 6, f"drm-driver:\ti915\ndrm-pdev:\t0000:2d:00.0\ndrm-client-id:\t9\n"
|
||||
f"drm-engine-render:\t0 ns\ndrm-engine-compute:\t{ns} ns\n")
|
||||
mine(0)
|
||||
clock = iter([10_000_000_000, 11_000_000_000]) # one second of wall time
|
||||
r = telemetry._Intel(0, sysfs=str(root), stat=str(root / "none.json"), proc=str(root / "proc"), zes=NoZes())
|
||||
with mock.patch.object(telemetry.time, "monotonic_ns", side_effect=lambda: next(clock)):
|
||||
self.assertIsNone(r.read()["util"])
|
||||
mine(250_000_000) # 0.25 s busy
|
||||
self.assertAlmostEqual(r.read()["util"], 25.0, places=3)
|
||||
|
||||
def test_vram_and_pcie_traffic_from_sysman(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
root = Path(d)
|
||||
b70_tree(root)
|
||||
r = telemetry._Intel(0, sysfs=str(root), stat=str(root / "none.json"), proc=str(root / "proc"), zes=FakeZes())
|
||||
first = r.read()
|
||||
self.assertEqual((first["mem_used"], first["mem_total"]), (2 * 2**30, 32 * 2**30))
|
||||
self.assertIsNone(first["pcie_rx_mb"], "traffic needs two counter readings")
|
||||
second = r.read()
|
||||
self.assertAlmostEqual(second["pcie_rx_mb"], 2048.0, places=3)
|
||||
self.assertAlmostEqual(second["pcie_tx_mb"], 1024.0, places=3)
|
||||
|
||||
def test_i915_vram_is_the_engines_fdinfo_memory_not_sysmans_zero(self):
|
||||
"""sysman on i915 reports the size but free == size (used 0 while an engine holds gigabytes: an A750, p1006 11-16)."""
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
root = Path(d)
|
||||
a750_tree(root)
|
||||
fdinfo(root, 444, 7, "drm-driver:\ti915\ndrm-pdev:\t0000:2d:00.0\ndrm-client-id:\t3\ndrm-engine-render:\t0 ns\n"
|
||||
"drm-engine-compute:\t0 ns\ndrm-total-local0:\t6291456 KiB\ndrm-resident-local0:\t6291456 KiB\n"
|
||||
"drm-resident-system0:\t1024 KiB\n")
|
||||
fdinfo(root, 444, 8, "drm-driver:\ti915\ndrm-pdev:\t0000:2d:00.0\ndrm-client-id:\t3\ndrm-resident-local0:\t6291456 KiB\n")
|
||||
out = telemetry._Intel(0, sysfs=str(root), stat=str(root / "none.json"), proc=str(root / "proc"), zes=FakeZes()).read()
|
||||
self.assertEqual(out["mem_used"], 6 * 2**30, "one client (two fds of it) holding 6 GiB of local memory; system memory not counted")
|
||||
self.assertEqual(out["mem_total"], 32 * 2**30, "the size still comes from sysman")
|
||||
empty = Path(d) / "empty"
|
||||
a750_tree(empty)
|
||||
out = telemetry._Intel(0, sysfs=str(empty), stat=str(empty / "none.json"), proc=str(empty / "proc"), zes=FakeZes()).read()
|
||||
self.assertIsNone(out["mem_used"], "no engine client: no reading, never sysman's 0")
|
||||
|
||||
def test_the_sampler_still_wins_for_load_and_vram(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
root = Path(d)
|
||||
b70_tree(root)
|
||||
stat = root / "gpustat.json"
|
||||
stat.write_text(json.dumps({"ts": time.time(), "busy_pct": 37, "vram_used_mb": 1024, "vram_total_mb": 32768}))
|
||||
out = telemetry._Intel(0, sysfs=str(root), stat=str(stat), proc=str(root / "proc"), zes=FakeZes()).read()
|
||||
self.assertEqual((out["util"], out["mem_used"]), (37, 1024 * 2**20))
|
||||
|
||||
def test_a_missing_level_zero_is_no_reading_not_an_error(self):
|
||||
z = telemetry._Zes("0000:03:00.0", lib=mock.Mock(zesInit=mock.Mock(return_value=0x78000001)))
|
||||
self.assertEqual(z.read(), (None, None))
|
||||
self.assertIsNone(z.pci_stats())
|
||||
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "the Intel fallback is Linux only")
|
||||
|
||||
+7
-113
@@ -1,117 +1,11 @@
|
||||
"""Intel Arc (xe driver) readings for the server's Monitor tab, in the shape of serve/telemetry.py's readers.
|
||||
"""Intel Arc readings for the server's Monitor tab: the shared reader in serve/telemetry.py.
|
||||
|
||||
Kept out of serve/telemetry.py so the SYCL port touches no shared file: sycl/serve/server_intel.py installs it as
|
||||
telemetry.gpu_reader when there is no NVIDIA card. Load and VRAM need root (xe's only VRAM accounting is per-client
|
||||
fdinfo), so they come from a root sampler's /run/gpustat.json (sycl/tools/gpustat.py); temperature, power and the
|
||||
PCIe link are read from sysfs, so they show without it.
|
||||
sycl/serve/server_intel.py installs _XeGpu as telemetry.gpu_reader whenever an Intel Arc is there (also on a PC with an NVIDIA card:
|
||||
the SYCL engine runs on the Intel one, #2001). The reader itself moved to serve/telemetry.py (_Intel, #5353), so every route that
|
||||
starts serve/server.py reads the card the same way: temperature, power, power limit and the PCIe link from sysfs; VRAM and PCIe
|
||||
traffic from Level Zero sysman; the engine's load from its DRM clients' fdinfo (#5988, from PR #5994); the optional root sampler's
|
||||
/run/gpustat.json first when it is there. xe (B-series) and i915 (A-series) cards alike.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
# the cards sycl/setup_intel.py names by PCI id (INTEL_ARC)
|
||||
ARC_NAMES = {"e223": "Arc Pro B70", "e221": "Arc Pro B60", "e211": "Arc Pro B60", "e20b": "Arc B580", "e20c": "Arc B570",
|
||||
"e212": "Arc B50", "56a0": "Arc A770", "56a1": "Arc A750", "56a2": "Arc A580", "56a5": "Arc A380",
|
||||
"56a6": "Arc A310", "5690": "Arc A770M"}
|
||||
|
||||
|
||||
class _XeGpu:
|
||||
"""The Intel Arc readings in NVML's shape. Load and VRAM come from /run/gpustat.json (a root sampler: the only
|
||||
VRAM accounting xe exposes is per-client fdinfo, readable by root only); temperature, power and the PCIe link are
|
||||
read from sysfs too, so they show without the sampler."""
|
||||
STAT = "/run/gpustat.json"
|
||||
|
||||
def __init__(self, index=0):
|
||||
import glob
|
||||
self.dev = None
|
||||
cards = [] # #2001: the Intel cards in sysfs order, as setup numbers them
|
||||
for d in sorted(glob.glob("/sys/class/drm/card[0-9]*/device")):
|
||||
try:
|
||||
if open(f"{d}/vendor").read().strip() == "0x8086" and os.path.isdir(f"{d}/tile0"):
|
||||
cards.append(os.path.realpath(d))
|
||||
except OSError:
|
||||
continue
|
||||
if 0 <= index < len(cards):
|
||||
self.dev = cards[index]
|
||||
self.hwmon = None
|
||||
for n in glob.glob(f"{self.dev}/hwmon/hwmon*/name") if self.dev else []:
|
||||
self.hwmon = os.path.dirname(n)
|
||||
self._e = None # (t, card energy uJ) for power without the sampler
|
||||
|
||||
def ok(self):
|
||||
return self.dev is not None and sys.platform.startswith("linux")
|
||||
|
||||
def name(self):
|
||||
st = self._stat()
|
||||
if (st or {}).get("name"):
|
||||
return st["name"]
|
||||
try: # the PCI id's name (setup_intel.INTEL_ARC), not a generic one
|
||||
with open(f"{self.dev}/device") as f:
|
||||
return "Intel " + ARC_NAMES.get(f.read().strip().lower().replace("0x", ""), "Arc GPU")
|
||||
except (OSError, TypeError):
|
||||
return "Intel Arc GPU"
|
||||
|
||||
def _stat(self):
|
||||
try:
|
||||
import json
|
||||
with open(self.STAT) as f:
|
||||
st = json.load(f)
|
||||
return st if time.time() - float(st.get("ts", 0)) < 15 else None
|
||||
except (OSError, ValueError):
|
||||
return None
|
||||
|
||||
def _rd(self, p):
|
||||
try:
|
||||
return open(p).read().strip()
|
||||
except OSError:
|
||||
return None
|
||||
|
||||
def _link(self):
|
||||
"""The link the card trained at: its own functions sit behind an internal x1 switch, so the first port up
|
||||
the path wider than x1; the max is what card AND slot allow (the root port caps it)."""
|
||||
gen = {"2.5": 1, "5.0": 2, "8.0": 3, "16.0": 4, "32.0": 5, "64.0": 6}
|
||||
d, chain = self.dev, []
|
||||
while d and d.startswith("/sys/devices/pci") and os.path.exists(f"{d}/current_link_speed"):
|
||||
chain.append(d)
|
||||
d = os.path.dirname(d)
|
||||
for p in chain:
|
||||
w = int(self._rd(f"{p}/current_link_width") or 0)
|
||||
if w > 1:
|
||||
g = lambda q, k: gen.get((self._rd(f"{q}/{k}_link_speed") or "").split(" ")[0])
|
||||
card, slot = g(p, "max"), g(chain[-1], "max")
|
||||
return g(p, "current"), min(x for x in (card, slot) if x) if (card or slot) else None, w
|
||||
return None, None, None
|
||||
|
||||
def read(self):
|
||||
out = {}
|
||||
st = self._stat()
|
||||
if st:
|
||||
out["util"] = st.get("busy_pct")
|
||||
if st.get("vram_used_mb") is not None:
|
||||
out["mem_used"] = st["vram_used_mb"] * 2**20
|
||||
out["mem_total"] = st.get("vram_total_mb", 0) * 2**20 or None
|
||||
out["temp"] = st.get("temp_pkg")
|
||||
out["power"] = st.get("power_w")
|
||||
out["power_limit"] = st.get("power_cap_w")
|
||||
out["vram_temp"] = st.get("temp_vram_max") or st.get("temp_vram")
|
||||
if self.hwmon:
|
||||
if out.get("temp") is None:
|
||||
for lab in os.listdir(self.hwmon):
|
||||
if lab.endswith("_label") and self._rd(f"{self.hwmon}/{lab}") == "pkg":
|
||||
v = self._rd(f"{self.hwmon}/{lab[:-6]}_input")
|
||||
out["temp"] = int(v) / 1000 if v else None
|
||||
if out.get("power") is None:
|
||||
e, t = self._rd(f"{self.hwmon}/energy1_input"), time.time()
|
||||
if e and e.isdigit():
|
||||
if self._e and t > self._e[0] and int(e) >= self._e[1]:
|
||||
out["power"] = (int(e) - self._e[1]) / 1e6 / (t - self._e[0])
|
||||
self._e = (t, int(e))
|
||||
if out.get("power_limit") is None:
|
||||
cap = self._rd(f"{self.hwmon}/power1_cap")
|
||||
out["power_limit"] = int(cap) / 1e6 if cap and cap.isdigit() and int(cap) > 0 else None
|
||||
out["pcie_gen"], out["pcie_gen_max"], out["pcie_width"] = self._link()
|
||||
out["pcie_rx_mb"] = out["pcie_tx_mb"] = None # xe exposes no PCIe traffic counters
|
||||
return out
|
||||
from serve.telemetry import ARC_NAMES, _Intel as _XeGpu # noqa: F401
|
||||
Reference in new issue
Block a user