SYCL Monitor: an Intel load that cannot be measured yet is unknown, not idle; the old sampler fills gaps (#5994 review)

The fdinfo load of 88787f851 answered 0 % (idle) when every client was new (no
earlier reading to compare) and when a client's counters went back (a reset), and
read only the real /proc. Now:
- a client counts only when it has an earlier reading and both of its counters
  moved forward; when no client does, the load is unknown (None), never 0 %;
- a client open in several descriptors counts once; other cards and other drivers
  are left out; a zero engine capacity is skipped;
- the docstring says what the number covers: the DRM clients this user can see
  (the engine in a container; on a host also other programs of the same user),
  never the whole card;
- a PC that still runs the old root sampler (/run/gpustat.json, fresh within 15 s)
  keeps its load and VRAM readings where this process cannot read them itself;
- the proc root is an attribute, so the reader can be tested without a card.
Test: tools/test_xe_telemetry.py (py-tools), a fake /proc and sampler file: 11/11;
on 88787f851 6 error (its reader cannot be pointed at a fixture) and 2 fail.
Not run: an Arc card (no p1006 runs in this lane).
This commit is contained in:
Niko1221 committed 2026-11-16 15:10:19 +01:00
1 parent 88787f8512
commit 0f6994f521
2 files changed
+176 -11

No files matched your search

+37 -11
View File
@@ -139,6 +139,8 @@ class _XeGpu:
"""The Intel Arc readings in NVML's shape: VRAM from sysman, load from the DRM clients this process can see,
temperature, power and the PCIe link from sysfs. No root, no sampler outside the process."""
STAT = "/run/gpustat.json" # the old root sampler (a fallback only, #5994 review)
def __init__(self, index=0):
import glob
self.dev = self.hwmon = self.zes = None
@@ -157,6 +159,7 @@ class _XeGpu:
if self.dev:
self.zes = _Zes(self.pci)
self._cycles = {} # client id -> (busy cycles, total cycles) of the last read
self._proc = "/proc" # where the DRM clients' fdinfo is read (tests point it at a fixture)
self._e = None # (t, card energy uJ) for power
self._pci = None # (rx bytes, tx bytes, ts us) of the last read, for the traffic rate
@@ -193,13 +196,15 @@ class _XeGpu:
return None, None, None
def _busy(self):
"""The render and compute load of the DRM clients this process can read in /proc/<pid>/fdinfo: the share of
the cycles they consumed out of the cycles the card ran, as a percentage. None until two reads can be
compared (and after that, when every client is new). Card-wide load needs other users' fdinfo, so this is
the engine's own load and says nothing about a second user of the card."""
"""The render and compute load of the DRM clients this process can read in <proc>/<pid>/fdinfo, as a percentage of
the cycles the card ran. Scope: the clients this user can see (in a container that is the engine; on a host it
can include other programs of the same user), never the whole card, which needs other users' fdinfo.
Unknown (None) until a client has two readings whose counters moved forward: a client seen for the first time,
or one whose counters went back (a reset, a reused id), says nothing about the load yet and is never taken as
idle (#5994 review). A client open in several file descriptors is counted once."""
import glob
now = {}
for f in glob.glob("/proc/[0-9]*/fdinfo/*"):
for f in glob.glob(os.path.join(self._proc, "[0-9]*", "fdinfo", "*")):
try:
with open(f, encoding="ascii", errors="replace") as fh:
text = fh.read()
@@ -215,17 +220,30 @@ class _XeGpu:
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)
except (ValueError, IndexError):
except (ValueError, IndexError, ZeroDivisionError):
continue
cid = kv.get("drm-client-id")
if cid and total:
now[cid] = (busy, total)
prev, self._cycles = self._cycles, 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)))
# only clients with an earlier reading whose counters both moved forward give a delta
moved = [c for c in now if c in prev and now[c][1] > prev[c][1] and now[c][0] >= prev[c][0]]
if not moved:
return None
span = max(now[c][1] - prev[c][1] for c in moved)
used = sum(now[c][0] - prev[c][0] for c in moved)
return max(0.0, min(100.0, 100.0 * used / span))
def _stat(self):
"""The old root sampler's file (sycl/tools/gpustat.py, /run/gpustat.json) when it is fresh: a PC that still runs it
keeps its readings for what this process cannot read itself (VRAM without sysman, the load before a delta)."""
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, TypeError, AttributeError):
return None
def _temps(self):
"""The GPU's own sensor (label "pkg") as the GPU temperature, as NVIDIA's core and AMD's edge readings are;
@@ -254,6 +272,14 @@ class _XeGpu:
out["util"] = self._busy()
used, total = self.zes.read() if self.zes else (None, None)
out["mem_used"], out["mem_total"] = used, total
if out["util"] is None or out["mem_used"] is None:
st = self._stat()
if st:
if out["util"] is None and st.get("busy_pct") is not None:
out["util"] = st["busy_pct"]
if out["mem_used"] is None and 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") or 0) * 2**20 or out["mem_total"]
out["temp"], out["vram_temp"] = self._temps()
out["power"] = out["power_limit"] = None
if self.hwmon:
+139
View File
@@ -0,0 +1,139 @@
"""tools/test_xe_telemetry.py - the Intel Arc load read from DRM fdinfo (#5994), against a fake /proc; no card, no Level Zero.
python -m pytest tools/test_xe_telemetry.py (py-tools runs it)
What the load means (#5994 review): the share of the card's cycles used by the DRM clients this process can see; unknown (None)
until a client has two readings whose counters moved forward. A client seen for the first time, or whose counters went back, is
never taken as idle; a client open in several file descriptors counts once; other cards and drivers are left out.
"""
from __future__ import annotations
import json
import os
import sys
import tempfile
import time
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "sycl" / "serve"))
import xe_telemetry # noqa: E402
PCI = "0000:03:00.0"
def fdinfo(cid, rcs, ccs, total, pdev=PCI, driver="xe", cap_ccs=1):
return (f"pos:\t0\nflags:\t02100002\ndrm-driver:\t{driver}\ndrm-client-id:\t{cid}\ndrm-pdev:\t{pdev}\n"
f"drm-cycles-rcs:\t{rcs}\ndrm-total-cycles-rcs:\t{total}\ndrm-cycles-ccs:\t{ccs}\ndrm-total-cycles-ccs:\t{total}\n"
f"drm-engine-capacity-ccs:\t{cap_ccs}\n")
class FakeProc:
def __init__(self, root):
self.root = Path(root)
self.n = 0
def clear(self):
for p in sorted(self.root.rglob("*"), reverse=True):
p.unlink() if p.is_file() else p.rmdir()
def put(self, pid, fd, text):
d = self.root / str(pid) / "fdinfo"
d.mkdir(parents=True, exist_ok=True)
(d / str(fd)).write_text(text, encoding="ascii")
class Load(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.proc = FakeProc(self.tmp.name)
self.gpu = xe_telemetry._XeGpu(index=99) # no such card: no sysfs, no Level Zero
self.gpu.pci = PCI
self.gpu._proc = self.tmp.name
def tearDown(self):
self.tmp.cleanup()
def read(self, clients):
self.proc.clear()
for i, (pid, fd, text) in enumerate(clients):
self.proc.put(pid, fd, text)
return self.gpu._busy()
def test_first_reading_is_unknown_then_a_delta_gives_the_load(self):
self.assertIsNone(self.read([(10, 5, fdinfo(1, 0, 0, 1000))]))
self.assertAlmostEqual(self.read([(10, 5, fdinfo(1, 250, 250, 2000))]), 50.0)
def test_an_idle_client_with_a_delta_is_zero_not_unknown(self):
self.read([(10, 5, fdinfo(1, 100, 0, 1000))])
self.assertEqual(self.read([(10, 5, fdinfo(1, 100, 0, 2000))]), 0.0)
def test_only_new_clients_is_unknown_not_idle(self):
self.read([(10, 5, fdinfo(1, 0, 0, 1000))])
self.assertIsNone(self.read([(11, 5, fdinfo(2, 900, 0, 1000))]), "a client seen for the first time says nothing yet")
def test_a_counter_that_went_back_is_a_new_client(self):
self.read([(10, 5, fdinfo(1, 800, 0, 5000))])
self.assertIsNone(self.read([(10, 5, fdinfo(1, 10, 0, 100))]), "a reset is not a negative or zero load")
self.assertAlmostEqual(self.read([(10, 5, fdinfo(1, 60, 0, 200))]), 50.0)
def test_a_client_open_in_two_descriptors_counts_once(self):
self.read([(10, 5, fdinfo(1, 0, 0, 1000)), (10, 6, fdinfo(1, 0, 0, 1000))])
self.assertAlmostEqual(self.read([(10, 5, fdinfo(1, 300, 0, 2000)), (10, 6, fdinfo(1, 300, 0, 2000))]), 30.0)
def test_two_clients_add_up_and_stay_within_100(self):
self.read([(10, 5, fdinfo(1, 0, 0, 1000)), (11, 3, fdinfo(2, 0, 0, 1000))])
self.assertAlmostEqual(self.read([(10, 5, fdinfo(1, 400, 0, 2000)), (11, 3, fdinfo(2, 300, 0, 2000))]), 70.0)
self.assertEqual(self.read([(10, 5, fdinfo(1, 2400, 0, 3000)), (11, 3, fdinfo(2, 2300, 0, 3000))]), 100.0)
def test_other_cards_drivers_and_unreadable_entries_are_left_out(self):
clients = [(10, 5, fdinfo(1, 0, 0, 1000)), (12, 1, fdinfo(7, 0, 0, 1000, pdev="0000:04:00.0")),
(13, 1, fdinfo(8, 0, 0, 1000, driver="i915")), (14, 1, "not drm\n")]
self.read(clients)
later = [(10, 5, fdinfo(1, 100, 0, 2000)), (12, 1, fdinfo(7, 1000, 0, 2000, pdev="0000:04:00.0")),
(13, 1, fdinfo(8, 1000, 0, 2000, driver="i915")), (14, 1, "not drm\n")]
self.assertAlmostEqual(self.read(later), 10.0)
def test_engine_capacity_divides_a_class_s_cycles(self):
self.read([(10, 5, fdinfo(1, 0, 0, 1000, cap_ccs=4))])
self.assertAlmostEqual(self.read([(10, 5, fdinfo(1, 0, 400, 2000, cap_ccs=4))]), 10.0)
def test_no_clients_is_unknown(self):
self.assertIsNone(self.read([]))
self.assertIsNone(self.read([]))
class OldSampler(unittest.TestCase):
"""A PC that still runs the root sampler (sycl/tools/gpustat.py) keeps its readings for what this process cannot read."""
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.gpu = xe_telemetry._XeGpu(index=99)
self.gpu.pci = PCI
self.gpu._proc = os.path.join(self.tmp.name, "proc")
self.gpu.STAT = os.path.join(self.tmp.name, "gpustat.json")
def tearDown(self):
self.tmp.cleanup()
def write(self, ts):
Path(self.gpu.STAT).write_text(json.dumps({"ts": ts, "busy_pct": 37, "vram_used_mb": 2048, "vram_total_mb": 16384}))
def test_a_fresh_sampler_fills_what_is_missing(self):
self.write(time.time())
out = self.gpu.read()
self.assertEqual(out["util"], 37)
self.assertEqual(out["mem_used"], 2048 * 2**20)
self.assertEqual(out["mem_total"], 16384 * 2**20)
def test_an_old_or_missing_sampler_leaves_them_unknown(self):
self.write(time.time() - 60)
out = self.gpu.read()
self.assertIsNone(out["util"])
self.assertIsNone(out["mem_used"])
os.unlink(self.gpu.STAT)
self.assertIsNone(self.gpu.read()["mem_used"])
if __name__ == "__main__":
unittest.main()