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).
This commit is contained in:
1 parent
54c60188f0
commit
cff0c74605
2 files changed
+88
-69
No files matched your search
+84
-67
@@ -24,6 +24,7 @@ import math
|
|||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import time
|
||||||
|
|
||||||
import cairo as _cairo
|
import cairo as _cairo
|
||||||
|
|
||||||
@@ -152,18 +153,12 @@ def _read_cpu_times():
|
|||||||
return None
|
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():
|
def _read_processes():
|
||||||
"""Live process rows from /proc: dicts with pid, name, user,
|
"""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:
|
try:
|
||||||
import pwd
|
import pwd
|
||||||
except ImportError: # pragma: no cover - non-Unix
|
except ImportError: # pragma: no cover - non-Unix
|
||||||
@@ -198,7 +193,8 @@ def _read_processes():
|
|||||||
pass
|
pass
|
||||||
rows.append({"pid": int(pid), "name": comm, "user": user,
|
rows.append({"pid": int(pid), "name": comm, "user": user,
|
||||||
"mem_kb": mem_kb, "utime": utime,
|
"mem_kb": mem_kb, "utime": utime,
|
||||||
"stime": stime, "cpu": 0.0})
|
"stime": stime, "cpu": 0.0,
|
||||||
|
"t": time.monotonic()})
|
||||||
except (OSError, ValueError, IndexError):
|
except (OSError, ValueError, IndexError):
|
||||||
continue
|
continue
|
||||||
return rows
|
return rows
|
||||||
@@ -377,6 +373,24 @@ def _stats(values):
|
|||||||
sum(values) / len(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 ----------------------------------------------------------------
|
# --- the widget ----------------------------------------------------------------
|
||||||
|
|
||||||
_DEMO_PROCESSES = (
|
_DEMO_PROCESSES = (
|
||||||
@@ -427,7 +441,7 @@ class MonitorWidget(Widget):
|
|||||||
# Process rows for XProcessList: None = demo rows.
|
# Process rows for XProcessList: None = demo rows.
|
||||||
self._rows = None
|
self._rows = None
|
||||||
self._prev_proc = None
|
self._prev_proc = None
|
||||||
self._prev_total = None
|
self._prev_times = None
|
||||||
# /proc/stat snapshot for XCoreGrid.refresh().
|
# /proc/stat snapshot for XCoreGrid.refresh().
|
||||||
self._prev_cpu = None
|
self._prev_cpu = None
|
||||||
# (load1, load5, load15, uptime_seconds) for XLoadBadge.
|
# (load1, load5, load15, uptime_seconds) for XLoadBadge.
|
||||||
@@ -653,25 +667,46 @@ class MonitorWidget(Widget):
|
|||||||
"""Refresh XProcessList from /proc (Linux). Returns the row
|
"""Refresh XProcessList from /proc (Linux). Returns the row
|
||||||
count, or None when /proc is unavailable (the widget keeps
|
count, or None when /proc is unavailable (the widget keeps
|
||||||
its demo rows). CPU% needs two samples: the first refresh
|
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()
|
rows = _read_processes()
|
||||||
total = _read_total_jiffies()
|
if rows is None:
|
||||||
if rows is None or total is None:
|
|
||||||
return None
|
return None
|
||||||
if self._prev_proc is not None and self._prev_total:
|
prev = None
|
||||||
d_total = total - self._prev_total
|
prev_times = None
|
||||||
|
if self._prev_proc is not None and self._prev_times is not None:
|
||||||
prev = {r["pid"]: r["utime"] + r["stime"]
|
prev = {r["pid"]: r["utime"] + r["stime"]
|
||||||
for r in self._prev_proc}
|
for r in self._prev_proc}
|
||||||
if d_total > 0:
|
prev_times = self._prev_times
|
||||||
for row in rows:
|
try:
|
||||||
old = prev.get(row["pid"])
|
hz = os.sysconf("SC_CLK_TCK")
|
||||||
if old is not None:
|
except (AttributeError, ValueError, OSError):
|
||||||
row["cpu"] = max(
|
hz = 100
|
||||||
0.0, min(100.0, 100.0
|
ncores = os.cpu_count() or 1
|
||||||
* (row["utime"] + row["stime"]
|
now_times = {}
|
||||||
- old) / d_total))
|
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_proc = rows
|
||||||
self._prev_total = total
|
self._prev_times = now_times
|
||||||
self.set_rows(rows)
|
self.set_rows(rows)
|
||||||
return len(rows)
|
return len(rows)
|
||||||
|
|
||||||
@@ -1136,20 +1171,13 @@ class MonitorWidget(Widget):
|
|||||||
def _angle(f):
|
def _angle(f):
|
||||||
return start + f * sweep
|
return start + f * sweep
|
||||||
|
|
||||||
# Track, redline zone, value arc.
|
# Track, redline zone, value arc (round caps).
|
||||||
cr.save()
|
_arc_stroke(cr, cx, cy, r, start, start + sweep,
|
||||||
cr.set_line_width(10)
|
(0.25, 0.25, 0.28), 10, cap=1)
|
||||||
cr.set_line_cap(1)
|
_arc_stroke(cr, cx, cy, r, _angle(red_frac), start + sweep,
|
||||||
_rgb(cr, (0.25, 0.25, 0.28))
|
_RED, 10, cap=1)
|
||||||
cr.arc(cx, cy, r, start, start + sweep)
|
_arc_stroke(cr, cx, cy, r, start, _angle(frac), _CYAN, 10,
|
||||||
cr.stroke()
|
cap=1)
|
||||||
_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()
|
|
||||||
# Ticks + numbers.
|
# Ticks + numbers.
|
||||||
cr.save()
|
cr.save()
|
||||||
for i in range(11):
|
for i in range(11):
|
||||||
@@ -1191,16 +1219,18 @@ class MonitorWidget(Widget):
|
|||||||
cr.line_to(cx + (r - 18) * math.cos(nang),
|
cr.line_to(cx + (r - 18) * math.cos(nang),
|
||||||
cy + (r - 18) * math.sin(nang))
|
cy + (r - 18) * math.sin(nang))
|
||||||
cr.stroke()
|
cr.stroke()
|
||||||
|
cr.new_path()
|
||||||
_rgb(cr, (0.6, 0.6, 0.65))
|
_rgb(cr, (0.6, 0.6, 0.65))
|
||||||
cr.arc(cx, cy, 6, 0, 2 * math.pi)
|
cr.arc(cx, cy, 6, 0, 2 * math.pi)
|
||||||
cr.fill()
|
cr.fill()
|
||||||
cr.restore()
|
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", "")
|
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)
|
rgb=_WHITE, align="center", bold=True)
|
||||||
if unit:
|
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")
|
rgb=_DIM, align="center")
|
||||||
|
|
||||||
def _draw_arc_gauge(self, cr):
|
def _draw_arc_gauge(self, cr):
|
||||||
@@ -1214,21 +1244,13 @@ class MonitorWidget(Widget):
|
|||||||
return
|
return
|
||||||
zones = ((0.0, 0.6, _GREEN), (0.6, 0.8, _YELLOW),
|
zones = ((0.0, 0.6, _GREEN), (0.6, 0.8, _YELLOW),
|
||||||
(0.8, 1.0, _RED))
|
(0.8, 1.0, _RED))
|
||||||
cr.save()
|
_arc_stroke(cr, cx, cy, r, math.pi, 2 * math.pi,
|
||||||
cr.set_line_width(12)
|
(0.25, 0.25, 0.28), 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()
|
|
||||||
for f0, f1, color in zones:
|
for f0, f1, color in zones:
|
||||||
_rgb(cr, color)
|
_arc_stroke(cr, cx, cy, r, math.pi + f0 * math.pi,
|
||||||
cr.arc(cx, cy, r, math.pi + f0 * math.pi,
|
math.pi + f1 * math.pi, color, 12)
|
||||||
math.pi + f1 * math.pi)
|
_arc_stroke(cr, cx, cy, r, math.pi, math.pi + frac * math.pi,
|
||||||
cr.stroke()
|
_WHITE, 12)
|
||||||
_rgb(cr, _WHITE)
|
|
||||||
cr.arc(cx, cy, r, math.pi, math.pi + frac * math.pi)
|
|
||||||
cr.stroke()
|
|
||||||
cr.restore()
|
|
||||||
_wtext(cr, self, "%.0f%%" % value, cx, cy - r * 0.45, size=22,
|
_wtext(cr, self, "%.0f%%" % value, cx, cy - r * 0.45, size=22,
|
||||||
rgb=_WHITE, align="center", bold=True)
|
rgb=_WHITE, align="center", bold=True)
|
||||||
label = self.get_prop("unit", "") or "usage"
|
label = self.get_prop("unit", "") or "usage"
|
||||||
@@ -1244,16 +1266,11 @@ class MonitorWidget(Widget):
|
|||||||
r = min(self.width, self.height - top) / 2 - 12
|
r = min(self.width, self.height - top) / 2 - 12
|
||||||
if r <= 0:
|
if r <= 0:
|
||||||
return
|
return
|
||||||
cr.save()
|
_arc_stroke(cr, cx, cy, r, 0, 2 * math.pi,
|
||||||
cr.set_line_width(14)
|
(0.25, 0.25, 0.28), 14, cap=1)
|
||||||
cr.set_line_cap(1)
|
_arc_stroke(cr, cx, cy, r, -math.pi / 2,
|
||||||
_rgb(cr, (0.25, 0.25, 0.28))
|
-math.pi / 2 + frac * 2 * math.pi, _GREEN, 14,
|
||||||
cr.arc(cx, cy, r, 0, 2 * math.pi)
|
cap=1)
|
||||||
cr.stroke()
|
|
||||||
_rgb(cr, _GREEN)
|
|
||||||
cr.arc(cx, cy, r, -math.pi / 2, -math.pi / 2 + frac * 2 * math.pi)
|
|
||||||
cr.stroke()
|
|
||||||
cr.restore()
|
|
||||||
_wtext(cr, self, "%.0f%%" % value, cx, cy + 7, size=20,
|
_wtext(cr, self, "%.0f%%" % value, cx, cy + 7, size=20,
|
||||||
rgb=_WHITE, align="center", bold=True)
|
rgb=_WHITE, align="center", bold=True)
|
||||||
label = self.get_prop("unit", "") or self.kind
|
label = self.get_prop("unit", "") or self.kind
|
||||||
|
|||||||
@@ -261,9 +261,11 @@ class ProcessListTests(unittest.TestCase):
|
|||||||
self.assertGreater(count, 0)
|
self.assertGreater(count, 0)
|
||||||
rows = procs.get_rows()
|
rows = procs.get_rows()
|
||||||
self.assertGreater(len(rows), 0)
|
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()
|
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()))
|
for r in procs.get_rows()))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user