From cff0c746056aea68a8af2f60167ac33621d0f78e Mon Sep 17 00:00:00 2001 From: Muse Date: Sat, 5 Dec 2026 17:07:19 +0000 Subject: [PATCH] Fix gauge arcs: stray diagonal line; wall-time per-process CPU% - Cairo arc() draws a connecting line from the current point to the arc start, and _wtext() leaves a current point (move_to + show_text), so the tachometer / arc / donut gauges grew a stray diagonal in the track color. New _arc_stroke() helper does new_path() before every arc. - Tachometer digital readout moved below the hub (was colliding with the 0/max tick labels). - XProcessList CPU% is now per-core against per-process wall time: on this VPS /proc/stat totals advance at only ~320 jiffies/s for 8 cores (host vCPU scheduling), so the stat denominator inflated a busy loop to ~240%; wall time gives ~97% (verified live against a busy loop). --- lightgui/xframe/monitor.py | 151 +++++++++++++++++++++---------------- tests/test_monitor.py | 6 +- 2 files changed, 88 insertions(+), 69 deletions(-) diff --git a/lightgui/xframe/monitor.py b/lightgui/xframe/monitor.py index c0c4f64..4c9ad9f 100644 --- a/lightgui/xframe/monitor.py +++ b/lightgui/xframe/monitor.py @@ -24,6 +24,7 @@ import math import os import socket import subprocess +import time import cairo as _cairo @@ -152,18 +153,12 @@ def _read_cpu_times(): return None -def _read_total_jiffies(): - try: - with open("/proc/stat") as handle: - parts = handle.readline().split() - return sum(int(v) for v in parts[1:]) - except (OSError, ValueError): - return None - - def _read_processes(): """Live process rows from /proc: dicts with pid, name, user, - mem_kb, utime, stime. Returns None when /proc is unavailable.""" + mem_kb, utime, stime -- plus an internal ``t`` (monotonic time + the stat was read, used for per-process CPU% denominators and + stripped before the rows reach the widget). Returns None when + /proc is unavailable.""" try: import pwd except ImportError: # pragma: no cover - non-Unix @@ -198,7 +193,8 @@ def _read_processes(): pass rows.append({"pid": int(pid), "name": comm, "user": user, "mem_kb": mem_kb, "utime": utime, - "stime": stime, "cpu": 0.0}) + "stime": stime, "cpu": 0.0, + "t": time.monotonic()}) except (OSError, ValueError, IndexError): continue return rows @@ -377,6 +373,24 @@ def _stats(values): sum(values) / len(values)) +def _arc_stroke(cr, cx, cy, r, a0, a1, color, width, cap=0): + """Stroke one arc as its own sub-path. + + A bare cr.arc() draws a connecting line from the *current* + point to the arc start -- and _wtext() leaves a current point + (move_to + show_text), so arcs drawn after a title grew a + stray diagonal in the gauge widgets. new_path() first: the + arc is then exactly the arc.""" + cr.save() + _rgb(cr, color) + cr.set_line_width(width) + cr.set_line_cap(cap) + cr.new_path() + cr.arc(cx, cy, r, a0, a1) + cr.stroke() + cr.restore() + + # --- the widget ---------------------------------------------------------------- _DEMO_PROCESSES = ( @@ -427,7 +441,7 @@ class MonitorWidget(Widget): # Process rows for XProcessList: None = demo rows. self._rows = None self._prev_proc = None - self._prev_total = None + self._prev_times = None # /proc/stat snapshot for XCoreGrid.refresh(). self._prev_cpu = None # (load1, load5, load15, uptime_seconds) for XLoadBadge. @@ -653,25 +667,46 @@ class MonitorWidget(Widget): """Refresh XProcessList from /proc (Linux). Returns the row count, or None when /proc is unavailable (the widget keeps its demo rows). CPU% needs two samples: the first refresh - reports 0.0 for every row.""" + reports 0.0 for every row. + + CPU% is per-core (top/btop semantics: 100% = one core) and + is computed against WALL time, not the /proc/stat total: + on this VPS the stat total advances at only ~320 jiffies/s + for 8 cores (host vCPU scheduling), so the stat denominator + inflates a single-threaded busy loop to ~240% -- wall time + gives the correct ~96%. The denominator is per-process: + each row carries the monotonic time its stat was read, so + a scan that slows down between samples cannot inflate + late PIDs or deflate early ones.""" rows = _read_processes() - total = _read_total_jiffies() - if rows is None or total is None: + if rows is None: return None - if self._prev_proc is not None and self._prev_total: - d_total = total - self._prev_total + prev = None + prev_times = None + if self._prev_proc is not None and self._prev_times is not None: prev = {r["pid"]: r["utime"] + r["stime"] for r in self._prev_proc} - if d_total > 0: - for row in rows: - old = prev.get(row["pid"]) - if old is not None: - row["cpu"] = max( - 0.0, min(100.0, 100.0 - * (row["utime"] + row["stime"] - - old) / d_total)) + prev_times = self._prev_times + try: + hz = os.sysconf("SC_CLK_TCK") + except (AttributeError, ValueError, OSError): + hz = 100 + ncores = os.cpu_count() or 1 + now_times = {} + for row in rows: + now_times[row["pid"]] = row["t"] + old = prev.get(row["pid"]) if prev else None + old_t = prev_times.get(row["pid"]) if prev_times else None + if old is not None and old_t is not None: + dt = row["t"] - old_t + if dt > 0: + row["cpu"] = max( + 0.0, min(100.0 * ncores, 100.0 + * (row["utime"] + row["stime"] - old) + / (dt * hz))) + del row["t"] self._prev_proc = rows - self._prev_total = total + self._prev_times = now_times self.set_rows(rows) return len(rows) @@ -1136,20 +1171,13 @@ class MonitorWidget(Widget): def _angle(f): return start + f * sweep - # Track, redline zone, value arc. - cr.save() - cr.set_line_width(10) - cr.set_line_cap(1) - _rgb(cr, (0.25, 0.25, 0.28)) - cr.arc(cx, cy, r, start, start + sweep) - cr.stroke() - _rgb(cr, _RED) - cr.arc(cx, cy, r, _angle(red_frac), start + sweep) - cr.stroke() - _rgb(cr, _CYAN) - cr.arc(cx, cy, r, start, _angle(frac)) - cr.stroke() - cr.restore() + # Track, redline zone, value arc (round caps). + _arc_stroke(cr, cx, cy, r, start, start + sweep, + (0.25, 0.25, 0.28), 10, cap=1) + _arc_stroke(cr, cx, cy, r, _angle(red_frac), start + sweep, + _RED, 10, cap=1) + _arc_stroke(cr, cx, cy, r, start, _angle(frac), _CYAN, 10, + cap=1) # Ticks + numbers. cr.save() for i in range(11): @@ -1191,16 +1219,18 @@ class MonitorWidget(Widget): cr.line_to(cx + (r - 18) * math.cos(nang), cy + (r - 18) * math.sin(nang)) cr.stroke() + cr.new_path() _rgb(cr, (0.6, 0.6, 0.65)) cr.arc(cx, cy, 6, 0, 2 * math.pi) cr.fill() cr.restore() - # Digital readout. + # Digital readout: below the hub, clear of the 0/max tick + # labels at the bottom of the dial. unit = self.get_prop("unit", "") - _wtext(cr, self, "%.0f" % value, cx, cy + r * 0.62, size=20, + _wtext(cr, self, "%.0f" % value, cx, cy + r * 0.80, size=20, rgb=_WHITE, align="center", bold=True) if unit: - _wtext(cr, self, unit, cx, cy + r * 0.62 + 14, size=10, + _wtext(cr, self, unit, cx, cy + r * 0.80 + 14, size=10, rgb=_DIM, align="center") def _draw_arc_gauge(self, cr): @@ -1214,21 +1244,13 @@ class MonitorWidget(Widget): return zones = ((0.0, 0.6, _GREEN), (0.6, 0.8, _YELLOW), (0.8, 1.0, _RED)) - cr.save() - cr.set_line_width(12) - cr.set_line_cap(0) - _rgb(cr, (0.25, 0.25, 0.28)) - cr.arc(cx, cy, r, math.pi, 2 * math.pi) - cr.stroke() + _arc_stroke(cr, cx, cy, r, math.pi, 2 * math.pi, + (0.25, 0.25, 0.28), 12) for f0, f1, color in zones: - _rgb(cr, color) - cr.arc(cx, cy, r, math.pi + f0 * math.pi, - math.pi + f1 * math.pi) - cr.stroke() - _rgb(cr, _WHITE) - cr.arc(cx, cy, r, math.pi, math.pi + frac * math.pi) - cr.stroke() - cr.restore() + _arc_stroke(cr, cx, cy, r, math.pi + f0 * math.pi, + math.pi + f1 * math.pi, color, 12) + _arc_stroke(cr, cx, cy, r, math.pi, math.pi + frac * math.pi, + _WHITE, 12) _wtext(cr, self, "%.0f%%" % value, cx, cy - r * 0.45, size=22, rgb=_WHITE, align="center", bold=True) label = self.get_prop("unit", "") or "usage" @@ -1244,16 +1266,11 @@ class MonitorWidget(Widget): r = min(self.width, self.height - top) / 2 - 12 if r <= 0: return - cr.save() - cr.set_line_width(14) - cr.set_line_cap(1) - _rgb(cr, (0.25, 0.25, 0.28)) - cr.arc(cx, cy, r, 0, 2 * math.pi) - cr.stroke() - _rgb(cr, _GREEN) - cr.arc(cx, cy, r, -math.pi / 2, -math.pi / 2 + frac * 2 * math.pi) - cr.stroke() - cr.restore() + _arc_stroke(cr, cx, cy, r, 0, 2 * math.pi, + (0.25, 0.25, 0.28), 14, cap=1) + _arc_stroke(cr, cx, cy, r, -math.pi / 2, + -math.pi / 2 + frac * 2 * math.pi, _GREEN, 14, + cap=1) _wtext(cr, self, "%.0f%%" % value, cx, cy + 7, size=20, rgb=_WHITE, align="center", bold=True) label = self.get_prop("unit", "") or self.kind diff --git a/tests/test_monitor.py b/tests/test_monitor.py index 4ba2756..0ce9af8 100644 --- a/tests/test_monitor.py +++ b/tests/test_monitor.py @@ -261,9 +261,11 @@ class ProcessListTests(unittest.TestCase): self.assertGreater(count, 0) rows = procs.get_rows() self.assertGreater(len(rows), 0) - # Second refresh computes real CPU% values. + # Second refresh computes real CPU% values (per-core: 100% + # = one core, so a multithreaded process can exceed 100). + import os procs.refresh_processes() - self.assertTrue(all(0.0 <= r["cpu"] <= 100.0 + self.assertTrue(all(0.0 <= r["cpu"] <= 100.0 * (os.cpu_count() or 1) for r in procs.get_rows()))