Skip to content

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

Tell a reused PID from the process the GPU row describes

Scott Hanselman37633a19a3e9
diff --git a/README.md b/README.md
index 6ff5caf..3508053 100644
--- a/README.md
+++ b/README.md
@@ -542,7 +542,7 @@ Other Hyper-V GPU-PV configurations have not been validated.
A **System** panel under the GPU header, and **RAM** and **CPU%** columns in the process table, show what
each process costs the *machine*, as the OS reports it. The numbers come from psutil; on Windows the process
list comes from `NtQuerySystemInformation`, because psutil's per-process calls take about 6 s per pass for
-~1,000 processes there. `s` sorts by them. Full diagnostics also shows a per-thread strip, 60-sample CPU and
+~1,000 processes there. `s` sorts by them, unknown values last. Full diagnostics also shows a per-thread strip, 60-sample CPU and
RAM histories, and the top processes by RAM and CPU; slim monitoring keeps two lines (the RAM and CPU bars).
`--no-system` removes the panel, the columns and the sampling.
@@ -554,8 +554,9 @@ These are system-memory figures, never GPU bytes. They are not added to any GPU
- **CPU%** is a share of the whole machine, like Task Manager: one busy thread on a 32-thread machine is about
3%. It is a rate between two samples, so a new process (or one whose PID was reused) shows `…` for one
refresh. Below 0.5% it is blank.
-- **`n/a`** means the OS would not say (a protected process), or the row is a Windows process listed from WSL,
- whose PID is not a Linux PID.
+- **`n/a`** means the OS would not say (a protected process), the PID now belongs to a different process than the
+ one the GPU row describes (compared by creation time when both are known), or the row is a Windows process
+ listed from WSL, whose PID is not a Linux PID.
- Inside WSL the panel describes the Linux VM, and its border says so.
- When the numbers cannot be read, the panel, `--once` and `--json` say why, and the GPU view is unaffected.
That covers 32-bit Python on Windows and a Windows build whose process-list layout does not match: the layout
diff --git a/src/gputaskman/app.py b/src/gputaskman/app.py
index 412c933..dcc2031 100644
--- a/src/gputaskman/app.py
+++ b/src/gputaskman/app.py
@@ -213,7 +213,8 @@ class GpuTaskManager(App):
SORTS = [("VRAM", lambda r: -(r.gpu.shown_vram or 0)), ("GPU%", lambda r: -r.gpu.gpu_pct),
("spill", lambda r: -r.gpu.shared), ("paged", lambda r: -r.gpu.paged_out),
("name", lambda r: r.name.lower())]
- SYSTEM_SORTS = [("RAM", lambda r: -(r.ram or 0)), ("CPU%", lambda r: -(r.cpu or 0))]
+ SYSTEM_SORTS = [("RAM", lambda r: (r.ram is None, -(r.ram or 0))), # unknown last; a measured 0 is not unknown
+ ("CPU%", lambda r: (r.cpu is None, -(r.cpu or 0)))]
CHART_COLORS = {
"GPU": "#4ade80",
"SM clock": "#fb923c",
@@ -365,7 +366,7 @@ class GpuTaskManager(App):
self.hist_ram.append(100 * host.ram_used / host.ram_total if host and host.ram_total else None)
for snap in snapshots:
for row in snap.procs: # Windows (W:) / outside (H:) rows have no local pid: RAM/CPU stay unknown
- hp = host.proc(row.pid) if host and not row.host else None
+ hp = host.proc(row.pid, row.detail.create_time) if host and not row.host else None
row.ram, row.cpu = (hp.rss, hp.cpu_pct) if hp else (None, None)
multi = len(snapshots) > 1
if self.snapshots and multi != (len(self.snapshots) > 1) and self.selected_key is not None:
diff --git a/src/gputaskman/host.py b/src/gputaskman/host.py
index c53a91c..c0680ba 100644
--- a/src/gputaskman/host.py
+++ b/src/gputaskman/host.py
@@ -16,6 +16,9 @@ from dataclasses import dataclass, field
import psutil
+SAME_PROCESS_SECS = 2.0 # psutil and the NT list give one creation time to ~1 microsecond; this absorbs clock steps
+
+
@dataclass
class HostProc:
pid: int
@@ -23,6 +26,7 @@ class HostProc:
rss: int | None # resident / working set bytes
private: int | None # Windows private bytes (commit charged to the process); None elsewhere
cpu_pct: float | None # share of the WHOLE machine (0-100), like Task Manager; None until a second reading
+ created: float | None = None # unix seconds; tells one process from the next that reuses its pid
@dataclass
@@ -40,8 +44,13 @@ class HostSample:
denied: int = 0 # processes whose memory we were not allowed to read
cost_ms: float = 0.0
- def proc(self, pid: int | None) -> HostProc | None:
- return self._by_pid.get(pid) if pid is not None else None
+ def proc(self, pid: int | None, created: float = 0.0) -> HostProc | None:
+ """This pid's reading. A caller that knows when its process started (0.0: it doesn't) gets only that
+ process: a pid can be reused between the GPU sample and this one."""
+ p = self._by_pid.get(pid) if pid is not None else None
+ if p and created and p.created is not None and abs(p.created - created) > SAME_PROCESS_SECS:
+ return None
+ return p
def __post_init__(self) -> None:
self._by_pid = {p.pid: p for p in self.procs}
@@ -85,7 +94,7 @@ class HostSampler:
continue
seen.add(ident)
procs.append(HostProc(p.pid, name, mi.rss, getattr(mi, "private", None),
- cpu / self._logical if ident in self._rated else None))
+ cpu / self._logical if ident in self._rated else None, ident[1]))
self._rated = seen
return self._finish(t0, per_core, vm, sw, procs, denied)
@@ -103,7 +112,7 @@ class HostSampler:
cpu = None
if wall and prev is not None and cpu100ns >= prev:
cpu = (cpu100ns - prev) / 1e7 / wall / self._logical * 100
- procs.append(HostProc(pid, name, ws, private, cpu))
+ procs.append(HostProc(pid, name, ws, private, cpu, created))
self._last_cpu, self._last_wall = last, now
return procs
@@ -177,7 +186,8 @@ def _check_nt_layout(rows: dict) -> None:
if row is not None:
name, ws, _private, _cpu, created = row
rss = me.memory_info().rss
- if name.lower() == me.name().lower() and abs(created - me.create_time()) < 2 and rss / 2 <= ws <= rss * 2:
+ if (name.lower() == me.name().lower() and abs(created - me.create_time()) < SAME_PROCESS_SECS
+ and rss / 2 <= ws <= rss * 2):
return
raise OSError("NtQuerySystemInformation process layout not recognised on this Windows build")
diff --git a/tests/test_host.py b/tests/test_host.py
index 9bb5c37..fed75ce 100644
--- a/tests/test_host.py
+++ b/tests/test_host.py
@@ -40,6 +40,15 @@ class SampleTests(unittest.TestCase):
self.assertEqual(d["top_cpu"][0]["cpu_pct"], 0.0)
self.assertNotIn("mhz", d["cpu"]) # psutil reports a nominal frequency, not a measurement
+ def test_a_pid_taken_by_another_process_is_not_returned(self):
+ h = sample(procs=[host.HostProc(7, "new.exe", 10, 5, 1.0, 6000.0), host.HostProc(8, "p.exe", 10, 5, 1.0, 5000.0),
+ host.HostProc(9, "q.exe", 10, 5, 1.0, None)])
+ self.assertIsNone(h.proc(7, 5000.0)) # pid 7 now belongs to a process that started later
+ self.assertEqual(h.proc(7, 6000.0).name, "new.exe")
+ self.assertEqual(h.proc(8, 5000.000002).name, "p.exe") # psutil and the NT list differ by microseconds
+ self.assertEqual(h.proc(7).name, "new.exe") # a caller with no creation time (pid 0 or 4) keeps the pid match
+ self.assertEqual(h.proc(9, 5000.0).name, "q.exe") # a reading with no creation time cannot disagree
+
def test_json_keeps_unknown_as_null_and_rounds_rates(self):
h = sample(procs=[host.HostProc(2, "b", 10, None, 12.3456)])
d = host.to_dict(h)
@@ -75,6 +84,7 @@ class NtPathTests(unittest.TestCase):
def test_cpu_is_a_rate_that_needs_two_readings(self):
first = self.read({7: ("a.exe", 100, 50, 10 * SECOND, 5000.0)})
self.assertIsNone(first.proc(7).cpu_pct)
+ self.assertEqual(first.proc(7).created, 5000.0) # lets the caller tell this process from a reuse of its pid
second = self.read({7: ("a.exe", 100, 50, 12 * SECOND, 5000.0)}, seconds=4)
# 2 s of CPU time in 4 s of wall clock on 4 logical CPUs: 12.5% of the whole machine
self.assertAlmostEqual(second.proc(7).cpu_pct, 12.5)
@@ -182,8 +192,10 @@ class PsutilPathTests(unittest.TestCase):
h = self.sampler.sample()
self.assertEqual(h.denied, 1)
self.assertIsNone(h.proc(1).rss) # not readable: unknown, not 0
+ self.assertIsNone(h.proc(1).created)
self.assertIsNone(h.proc(2))
self.assertEqual(h.proc(3).rss, 7)
+ self.assertEqual(h.proc(3).created, 1.0)
class LiveTests(unittest.TestCase):
diff --git a/tests/test_system.py b/tests/test_system.py
index a8fa1e6..1dd8b98 100644
--- a/tests/test_system.py
+++ b/tests/test_system.py
@@ -101,6 +101,23 @@ class SystemUiTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(table.get_cell(foreign.key, "CPU%").plain, "n/a")
self.assertEqual(table.get_cell("10", "RAM").plain, "6.00 GB")
+ async def test_a_pid_that_now_belongs_to_another_process_shows_n_a(self):
+ s = snapshot()
+ s.procs[0].detail.create_time = 1000.0 # llama-server as the GPU sample saw it
+ s.procs[1].detail.create_time = 2000.0
+ sample = host_sample(procs=[HostProc(10, "other.exe", 6 * GiB, 5 * GiB, 12.0, created=1500.0), # pid 10 reused
+ HostProc(20, "dwm.exe", 300 * MiB, 200 * MiB, 3.0, created=2000.0)])
+ with patch.object(GpuTaskManager, "poll"):
+ app = GpuTaskManager()
+ async with app.run_test(size=(140, 42)) as pilot:
+ app.apply_all([s], sample)
+ await pilot.pause()
+ table = app.query_one("#procs")
+ self.assertEqual(table.get_cell("10", "RAM").plain, "n/a")
+ self.assertEqual(table.get_cell("10", "CPU%").plain, "n/a")
+ self.assertEqual(table.get_cell("20", "RAM").plain, "300 MB") # the same process still reads
+ self.assertEqual(table.get_cell("20", "CPU%").plain, "3")
+
async def test_processes_the_os_would_not_describe_show_n_a(self):
s = snapshot()
s.procs.append(row(30, "protected.exe")) # not in the host sample
@@ -136,13 +153,15 @@ class SystemUiTests(unittest.IsolatedAsyncioTestCase):
await pilot.pause()
self.assertEqual(table.get_cell("10", "RAM").plain, "6.00 GB")
- async def test_a_sampler_that_cannot_start_says_why(self):
- self.sampler.side_effect = OSError("layout not recognised")
+ async def test_a_sampler_that_cannot_start_says_why_and_is_tried_again(self):
+ self.sampler.side_effect = [OSError("layout not recognised"), self.sampler.return_value]
with patch.object(GpuTaskManager, "poll"):
app = GpuTaskManager()
async with app.run_test(size=(140, 42)):
self.assertIsNone(app._sample_host())
self.assertEqual(app.host_error, "OSError: layout not recognised")
+ self.assertIsNotNone(app._sample_host()) # the failure was not cached
+ self.assertIsNone(app.host_error)
async def test_no_system_removes_the_panel_columns_and_sorts(self):
with patch.object(GpuTaskManager, "poll"):
@@ -160,18 +179,39 @@ class SystemUiTests(unittest.IsolatedAsyncioTestCase):
async def test_ram_and_cpu_sorts_rank_unknown_last(self):
s = snapshot()
s.procs.insert(0, row(30, "protected.exe")) # not in the host sample
+ s.procs.append(row(40, "idle.exe")) # measured: nothing resident, no CPU time. That is not unknown
+ sample = host_sample(procs=[HostProc(10, "llama-server.exe", 6 * GiB, 5 * GiB, 12.0),
+ HostProc(20, "dwm.exe", 300 * MiB, 200 * MiB, None),
+ HostProc(40, "idle.exe", 0, 0, 0.0)])
with patch.object(GpuTaskManager, "poll"):
app = GpuTaskManager()
async with app.run_test(size=(140, 42)) as pilot:
- app.apply_all([s], host_sample())
+ app.apply_all([s], sample)
await pilot.pause()
for _ in GpuTaskManager.SORTS:
await pilot.press("s")
self.assertEqual(app.query_one("#procs").border_subtitle.split(" ·")[0], "sort: RAM")
- self.assertEqual([r.pid for r in app.visible_rows(s)], [10, 20, 30])
+ self.assertEqual([r.pid for r in app.visible_rows(s)], [10, 20, 40, 30])
await pilot.press("s")
self.assertEqual(app.query_one("#procs").border_subtitle.split(" ·")[0], "sort: CPU%")
- self.assertEqual(app.visible_rows(s)[0].pid, 10)
+ self.assertEqual([r.pid for r in app.visible_rows(s)], [10, 40, 30, 20])
+
+ async def test_cpu_below_half_a_percent_is_blank_and_unknown_is_not(self):
+ s = snapshot()
+ s.procs += [row(40, "quiet.exe"), row(50, "busy.exe")]
+ sample = host_sample(procs=[HostProc(10, "llama-server.exe", 6 * GiB, 5 * GiB, 12.0),
+ HostProc(20, "dwm.exe", 300 * MiB, 200 * MiB, None),
+ HostProc(40, "quiet.exe", 10 * MiB, MiB, 0.49),
+ HostProc(50, "busy.exe", 10 * MiB, MiB, 0.6)])
+ with patch.object(GpuTaskManager, "poll"):
+ app = GpuTaskManager()
+ async with app.run_test(size=(140, 42)) as pilot:
+ app.apply_all([s], sample)
+ await pilot.pause()
+ table = app.query_one("#procs")
+ self.assertEqual(table.get_cell("40", "CPU%").plain, "")
+ self.assertEqual(table.get_cell("50", "CPU%").plain, "1")
+ self.assertEqual(table.get_cell("20", "CPU%").plain, "…")
async def test_panel_title_says_when_it_describes_the_wsl_vm(self):
with patch.object(GpuTaskManager, "poll"), patch("gputaskman.app.BACKEND", "wsl"):