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
Skip a reused pid's cached psutil object instead of rating it
Scott Hanselman1a3564277187
diff --git a/AGENTS.md b/AGENTS.mdindex 0caee5c..7f345a6 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -81,7 +81,7 @@ identify everything immediately; the TUI spreads scans across refreshes.| Add a GPU vendor or backend | `dxgi.py`, `collector.py`, a new module | Pair devices by LUID or PCI bus ID, never by name or index; load vendor DLLs by absolute system path || Add a column or JSON field | `report.py` and `app.py` | Follow the null rules above; update the README tables. If it is a measured byte count, consider adding it to `diff.py` `MEASURED` || Change snapshot comparison | `diff.py` | Works only on saved `--json` files, never live state. Compare measured fields directly; keep runtime-reported and estimated values in their own sections; an absent row is `not listed`, not zero. Refuse only when both snapshots name conflicting adapters (vendor, PCI address, a non-Windows key); when they cannot be compared, warn (`name-only`) instead, because a Windows LUID changes at boot and `diff` has no override flag |-| Change system RAM/CPU | `host.py` | Machine memory and load as the OS reports them: never GPU bytes, never added to `Snapshot.attributed`, and `diff` ignores it. Unknown is `None`/`null` and a failure carries a reason, never zeros. The JSON key is `system` because `host` already means the WSL Windows host. Windows uses the NT process list (psutil per process takes about 6 s per pass for 1,000 processes), checked against psutil at startup; CPU rates are keyed by `(pid, create_time)` so a reused PID never inherits another process's history |+| Change system RAM/CPU | `host.py` | Machine memory and load as the OS reports them: never GPU bytes, never added to `Snapshot.attributed`, and `diff` ignores it. Unknown is `None`/`null` and a failure carries a reason, never zeros. The JSON key is `system` because `host` already means the WSL Windows host. Windows uses the NT process list (psutil per process takes about 6 s per pass for 1,000 processes), checked against psutil at startup; CPU rates are keyed by `(pid, create_time)` so a reused PID never inherits another process's history (on the psutil path, `is_running()` is what makes psutil drop its cached object for a reused PID; keep that call) || Change WSL matching | `wslhost.py` `ContextTracker` | Keep `tests/test_wslhost.py` replays passing; they are measured sequences |## Verifydiff --git a/README.md b/README.mdindex 359e8cc..994888f 100644--- a/README.md+++ b/README.md@@ -757,7 +757,7 @@ Identification is designed to stay cheap. Measured on one Windows machine:| macOS: `ioreg` device stats + Metal clients | ~10 ms | Every refresh || macOS: memory-map scan (`proc_pidinfo` region walk) | ~4 ms per process | Same policy as the memory-map scan above || macOS: `footprint` GPU memory | ~70 ms per process, background thread | Busy and new processes every ~2 s, idle ones every 30 s (54 runs in 40 s with 27 GPU processes, M4 Mac mini) |-| System RAM/CPU: process list + psutil totals | Windows (`NtQuerySystemInformation`): ~28 ms median for 995 processes (Windows 11, RTX 4080 SUPER machine, 32 threads); the contributor measured ~6 ms for ~480 processes (Windows 11, i9-13900K). Linux under WSL2 (psutil): ~3.4 ms median for 44 processes, ~9.5 ms for the first sample of 123 | Every refresh; `--no-system` turns it off. psutil's per-process `memory_info`/`cpu_times` took ~6 s per pass for 995 processes on Windows, so it is only the non-Windows path. macOS unmeasured |+| System RAM/CPU: process list + psutil totals | Windows (`NtQuerySystemInformation`): ~28 ms median for 995 processes (Windows 11, RTX 4080 SUPER machine, 32 threads); the contributor measured ~6 ms for ~480 processes (Windows 11, i9-13900K). Linux under WSL2 (psutil): ~3.4 ms median for 44 processes, ~9.5 ms for the first sample of 123; the per-pid reuse check (`is_running()`) adds ~0.3 ms for 44 processes, about 7 µs each | Every refresh; `--no-system` turns it off. psutil's per-process `memory_info`/`cpu_times` took ~6 s per pass for 995 processes on Windows, so it is only the non-Windows path. macOS unmeasured |With two llama-servers running, a steady refresh costs roughly 15-30 ms of CPU (about 340 ms beforeprocess details were cached).diff --git a/src/gputaskman/host.py b/src/gputaskman/host.pyindex c0680ba..e3ff6be 100644--- a/src/gputaskman/host.py+++ b/src/gputaskman/host.py@@ -79,8 +79,10 @@ class HostSampler:if _nt_query is not None:return self._finish(t0, per_core, vm, sw, self._sample_nt(), 0)procs, denied, seen = [], 0, set()- for p in psutil.process_iter(): # psutil keeps these objects between calls and replaces one whose pid was reused+ for p in psutil.process_iter(): # psutil keeps these objects between calls, even for a pid that was reusedtry:+ if not p.is_running(): # a reused pid: skipped this once, then psutil builds a fresh object (no CPU history)+ continuewith p.oneshot():name = p.name()mi = p.memory_info()diff --git a/tests/test_host.py b/tests/test_host.pyindex fed75ce..91a9718 100644--- a/tests/test_host.py+++ b/tests/test_host.py@@ -140,13 +140,16 @@ class LayoutCheckTests(unittest.TestCase):class FakeProc:- def __init__(self, pid, name="p.exe", created=1.0, rss=10, cpu=40.0, denied=False, gone=False):+ def __init__(self, pid, name="p.exe", created=1.0, rss=10, cpu=40.0, denied=False, gone=False, running=True):self.pid, self._name, self._created, self.rss = pid, name, created, rss- self.cpu, self.denied, self.gone = cpu, denied, gone+ self.cpu, self.denied, self.gone, self.running = cpu, denied, gone, runningdef oneshot(self):return contextlib.nullcontext()+ def is_running(self):+ return self.running+def name(self):if self.gone:raise psutil.NoSuchProcess(self.pid)@@ -187,6 +190,18 @@ class PsutilPathTests(unittest.TestCase):self.procs = [FakeProc(1, created=2.0, cpu=100.0)]self.assertIsNone(self.sampler.sample().proc(1).cpu_pct)+ def test_a_cached_object_for_a_reused_pid_is_skipped_until_psutil_replaces_it(self):+ self.procs = [FakeProc(1, created=1.0, cpu=100.0)]+ self.sampler.sample()+ self.assertAlmostEqual(self.sampler.sample().proc(1).cpu_pct, 25.0)+ # psutil keeps handing back the old process's object (its counters are the old process's)+ self.procs = [FakeProc(1, created=1.0, cpu=-20.0, running=False)]+ self.assertIsNone(self.sampler.sample().proc(1))+ self.procs = [FakeProc(1, created=9.0, cpu=0.0)] # the replacement: its first reading is only a baseline+ self.assertIsNone(self.sampler.sample().proc(1).cpu_pct)+ self.procs = [FakeProc(1, created=9.0, cpu=100.0)]+ self.assertAlmostEqual(self.sampler.sample().proc(1).cpu_pct, 25.0)+def test_protected_and_vanished_processes_are_unknown_or_skipped(self):self.procs = [FakeProc(1, denied=True), FakeProc(2, gone=True), FakeProc(3, rss=7)]h = self.sampler.sample()