A GPU task manager for Windows, Linux, and WSL: per-process VRAM, GPU activity, and LLM model/KV-cache insights (Windows AMD and NVIDIA).
Python
Keep WSL VRAM matches honest and learn idle contexts at startup
Scott Hanselman3338e0b039a2
diff --git a/README.md b/README.mdindex 5f4cf78..72e9d29 100644--- a/README.md+++ b/README.md@@ -172,7 +172,7 @@ gputaskman follows the contexts from sample to sample and labels them with evide| Marker | Meaning | Established by ||---|---|---|-| `*` matched | Windows' exact size; the owner is inferred, and only one explanation fits | A new Linux GPU process and a new context appearing together; elimination; contexts seen while no Linux process in this distro used the GPU (remembered for this WSL boot in `~/.cache/gputaskman/`); this monitor's own 0-byte context being newest at startup |+| `*` matched | Windows' exact size; the owner is inferred, and only one explanation fits (or every one that fits gives the same size) | A new Linux GPU process and a new context appearing together; elimination; contexts seen when gputaskman starts while no other Linux process in this distro uses the GPU (remembered for this WSL boot in `~/.cache/gputaskman/`); this monitor's own 0-byte context being newest at startup; candidate contexts that are exactly the same size || `?` guess | Start-order guess for processes that were already running | Newer process ↔ newer context, outside contexts assumed oldest || `n/a` | No context could be assigned | For example, a process that has not created its GPU context yet |diff --git a/src/gputaskman/report.py b/src/gputaskman/report.pyindex 4ea29c4..71a9083 100644--- a/src/gputaskman/report.py+++ b/src/gputaskman/report.py@@ -38,7 +38,8 @@ def vram_label(g) -> str:INFERRED_NOTE = ("* matched: the size is exact (Windows' counter for a WSL VM GPU context, or NVML for a host PID "- "seen from a container); which local process owns it is inferred, and only one explanation fits.")+ "seen from a container); which local process owns it is inferred, and only one explanation fits "+ "(or every one that fits gives the same size).")ORDER_NOTE = ("? order guess: the size is exact but several owners fit, so newer processes are paired with newer ""GPU contexts / PIDs and outside ones are assumed oldest. Becomes * with evidence, such as a process ""starting while gputaskman runs.")@@ -111,15 +112,16 @@ def to_text(s: Snapshot, limit: int = 30) -> str:f"PCIe rx {format_metric(rx, '.0f')} MB/s "f"tx {format_metric(tx, '.0f')} MB/s")out.append("")- out.append(f"{'PID':>7} {'PROCESS':<24} {'KIND':<10} {'RUNTIME':<24} {'VRAM':>9} {'SPILL':>8} {'PAGED':>8} {'GPU%':>5} INSIDE")- for p in s.procs[:limit]:+ shown = s.procs[:limit]+ w = max([7] + [len(p.pid_label) for p in shown]) # host PIDs like W:120880 are wider+ out.append(f"{'PID':>{w}} {'PROCESS':<24} {'KIND':<10} {'RUNTIME':<24} {'VRAM':>9} {'SPILL':>8} {'PAGED':>8} {'GPU%':>5} INSIDE")+ for p in shown:spill = gb(p.gpu.shared) if p.gpu.shared >= 1 << 20 else ""paged = gb(p.gpu.paged_out) if p.gpu.paged_out >= 1 << 20 else ""pid = p.pid_labelpct = f"{p.gpu.gpu_pct:.0f}" if p.gpu.attributed else "n/a"- out.append(f"{pid:>7} {p.name[:24]:<24} {p.kind:<10} {p.runtime[:24]:<24} {vram_label(p.gpu):>9} {spill:>8} "+ out.append(f"{pid:>{w}} {p.name[:24]:<24} {p.kind:<10} {p.runtime[:24]:<24} {vram_label(p.gpu):>9} {spill:>8} "f"{paged:>8} {pct:>5} {p.inside}")- shown = s.procs[:limit]if any(p.gpu.inferred_by == "consistent" for p in shown):out.append(INFERRED_NOTE)if any(p.gpu.inferred_by == "order" for p in shown):@@ -128,7 +130,7 @@ def to_text(s: Snapshot, limit: int = 30) -> str:for p in s.procs):out.append(f"Linux VRAM split: {s.wsl_split}")if s.backend == "wsl" and not any(not p.host for p in s.procs):- out.append(f"{'':>7} {WSL_EMPTY}")+ out.append(f"{'':>{w}} {WSL_EMPTY}")for p in s.procs:if p.handy:out.append("")diff --git a/src/gputaskman/wslhost.py b/src/gputaskman/wslhost.pyindex c9372e4..36982f6 100644--- a/src/gputaskman/wslhost.py+++ b/src/gputaskman/wslhost.py@@ -305,8 +305,8 @@ class ContextTracker:Contexts are aligned sample to sample (survivors keep their order). A match becomes *sure* only fromevidence: a new Linux GPU process and a new context appearing together, elimination (as many unsure- contexts as unsure processes, and just one process), contexts seen while no Linux process holds the GPU- (foreign; cached per WSL boot), or this monitor's own 0-byte context being the newest at startup.+ contexts as unsure processes, and just one process), contexts seen at startup while no other Linux process+ holds the GPU (foreign; cached per WSL boot), or this monitor's own 0-byte context being the newest at startup.Everything else is an order guess (newest process <-> newest context, foreign assumed oldest),recomputed each sample and never used as evidence."""@@ -453,6 +453,9 @@ class ContextTracker:elif first:if self.self_pid in start and cur and cur[0].value <= self.TOL_MIN:cur[0].label, cur[0].sure = self.self_pid, True # our NVML context: newest when we start+ if set(start) == {self.self_pid} and cur and cur[0].label == self.self_pid:+ for e in cur[1:]: # nothing else in this distro holds the GPU: the rest is foreign+ e.label, e.sure = FOREIGN, Truefor v in self.cached_foreign:free = [e for e in reversed(cur) if e.label is None and abs(e.value - v) <= self._tol(v)]if free:@@ -482,8 +485,9 @@ class ContextTracker:if len(free) == len(unsure) == 1 and not recent:free[0].label, free[0].sure = unsure[0], Trueelse:+ # Exactly equal only: within tolerance, a swapped owner would show the wrong bytes.same = (len(free) == len(unsure) > 1 and not recent- and all(abs(e.value - free[0].value) <= self._tol(free[0].value) for e in free))+ and all(e.value == free[0].value for e in free))for pid, e in zip(unsure, free): # newest process <-> newest context; leftovers are foreign# Equal sizes make the shown bytes right, not the owner: never carried as evidence.e.label, e.same = pid, samediff --git a/tests/test_wslhost.py b/tests/test_wslhost.pyindex dcc9a38..0f92373 100644--- a/tests/test_wslhost.py+++ b/tests/test_wslhost.py@@ -177,12 +177,18 @@ class TrackerTests(unittest.TestCase):def test_new_process_waits_for_its_own_context_instead_of_eliminating(self):t = self.make()- h = [("S", 900)]- self.check(self.step(t, [0.0, 0.15], h), {}) # 0.15: WSLg, not yet known as foreign+ self.check(self.step(t, [0.0, 2.0, 0.15], [("S", 900), ("B", 10)]), {"B": (2.0, "?")})+ h = [("S", 900)] # B exits; 0.15 (WSLg) was never seen without B, so it isn't known as foreign+ self.check(self.step(t, [0.0, 0.15], h), {})h.append(("A", 1001))self.check(self.step(t, [0.0, 0.15], h), {"A": (0.15, "?")}) # A's context isn't there yetself.check(self.step(t, [6.0, 0.0, 0.15], h), {"A": (6.0, "*")})+ def test_startup_with_only_this_monitor_teaches_foreign(self):+ self.step(self.make(), [0.0, 0.15], [("S", 900)]) # only gputaskman holds the GPU: 0.15 is foreign+ t = self.make() # a later start in the same boot, with a job already running+ self.check(self.step(t, [0.0, 2.0, 0.15], [("S", 900), ("X", 5)]), {"X": (2.0, "*")})+def test_a_reused_pid_is_a_new_process(self):t = self.make()self.check(self.step(t, [0.0, 1.0], [("S", 900), ("P", 10)]), {"P": (1.0, "*")})@@ -249,6 +255,11 @@ class TrackerTests(unittest.TestCase):self.check(self.step(t, [0.0, 0.4, 0.4], [("S", 900), ("F1", 5), ("F2", 6)]),{"F1": (0.4, "*"), "F2": (0.4, "*")})+ def test_nearly_equal_sizes_stay_guesses(self):+ t = self.make() # 0.40 vs 0.42 GiB: within tolerance, but swapping owners changes the shown bytes+ self.check(self.step(t, [0.0, 0.4, 0.42], [("S", 900), ("F1", 5), ("F2", 6)]),+ {"F1": (0.42, "?"), "F2": (0.4, "?")})+def test_different_adapter_resets_history(self):self.make().update(RTX, at([0.4]), [], now=self.clock)t = self.make()