249 lines
8.3 KiB
Python
249 lines
8.3 KiB
Python
#!/usr/bin/env python3
|
|
# Memory Monitor - monitor (Stage 2, btop-widget project).
|
|
#
|
|
# One 1024x768 window of live memory data from /proc/meminfo, refreshed
|
|
# every 500 ms. Every visible element is one of the new XFrame monitor
|
|
# widgets: XSysBox (big used %), XDonutGauge x2 (memory / swap used),
|
|
# XStackBar (used / cached / buffers / free), XHistoryGraph (used % and
|
|
# cached % over the last 60 s), XUsageBar x3 (memory used / cached /
|
|
# swap used). No raw Cairo drawing for UI elements: the shell only
|
|
# paints the window background and calls each widget's draw(cr).
|
|
#
|
|
# Run with:
|
|
# PYTHONPATH=/root/monitor-wt/memory-monitor/lightgui_python \
|
|
# python3 examples/monitor_memory_monitor/main.py
|
|
# (the designer sets PYTHONPATH when it runs this script; run it by hand
|
|
# with the PYTHONPATH above from the repo root.)
|
|
|
|
import sys
|
|
|
|
import gi
|
|
|
|
gi.require_version("Gtk", "3.0")
|
|
gi.require_version("Gdk", "3.0")
|
|
|
|
from gi.repository import GLib, Gtk # noqa: E402
|
|
from lightgui.xframe.core import create, parse_color # noqa: E402
|
|
|
|
|
|
# Design-time geometry + effective anchors per widget:
|
|
# name -> (top, left, right, bottom, x, y, width, height)
|
|
_ANCHORS = {
|
|
'XSysBox_001': (True, True, False, False, 16, 16, 500, 190),
|
|
'XDonutGauge_001': (True, True, False, False, 532, 16, 230, 190),
|
|
'XDonutGauge_002': (True, True, False, False, 778, 16, 230, 190),
|
|
'XStackBar_001': (True, True, False, False, 16, 222, 992, 60),
|
|
'XHistoryGraph_001': (True, True, False, False, 16, 298, 992, 310),
|
|
'XUsageBar_001': (True, True, False, False, 16, 624, 992, 36),
|
|
'XUsageBar_002': (True, True, False, False, 16, 664, 992, 36),
|
|
'XUsageBar_003': (True, True, False, False, 16, 704, 992, 36),
|
|
}
|
|
|
|
|
|
def _apply_anchors(widgets, width, height):
|
|
"""Reposition anchored widgets for a drawing area of
|
|
*width* x *height* (Flag 1: minimal anchor layout).
|
|
Computed from the design geometry every time, so a
|
|
resize is idempotent — no drift across resizes."""
|
|
design_w, design_h = _DESIGN_SIZE
|
|
dx = width - design_w
|
|
dy = height - design_h
|
|
for widget in widgets:
|
|
top, left, right, bottom, ox, oy, ow, oh = \
|
|
_ANCHORS[widget.name]
|
|
x, y, w, h = ox, oy, ow, oh
|
|
if right and not left:
|
|
x = ox + dx
|
|
elif right and left:
|
|
w = ow + dx
|
|
if bottom and not top:
|
|
y = oy + dy
|
|
elif bottom and top:
|
|
h = oh + dy
|
|
widget.move_to(x, y)
|
|
widget.resize_to(w, h)
|
|
|
|
|
|
def _read_meminfo():
|
|
"""Live memory figures from /proc/meminfo. 'Used' follows the
|
|
`free` convention: total - free - buffers - cached. Returns a
|
|
dict of GiB figures and percentages, or None when unavailable."""
|
|
try:
|
|
with open("/proc/meminfo") as f:
|
|
vals = {}
|
|
for line in f:
|
|
parts = line.split()
|
|
if len(parts) >= 2:
|
|
vals[parts[0].rstrip(":")] = float(parts[1])
|
|
except OSError:
|
|
return None
|
|
total = vals.get("MemTotal", 0.0)
|
|
if total <= 0:
|
|
return None
|
|
gib = 1024.0 * 1024.0
|
|
total_gib = total / gib
|
|
free_gib = vals.get("MemFree", 0.0) / gib
|
|
avail_gib = vals.get("MemAvailable", 0.0) / gib
|
|
cached_gib = vals.get("Cached", 0.0) / gib
|
|
buffers_gib = vals.get("Buffers", 0.0) / gib
|
|
used_gib = max(0.0, total_gib - free_gib - buffers_gib - cached_gib)
|
|
swap_total = vals.get("SwapTotal", 0.0) / gib
|
|
swap_used = max(0.0, swap_total - vals.get("SwapFree", 0.0) / gib)
|
|
return {
|
|
"total": total_gib,
|
|
"free": free_gib,
|
|
"avail": avail_gib,
|
|
"cached": cached_gib,
|
|
"buffers": buffers_gib,
|
|
"used": used_gib,
|
|
"used_pct": used_gib / total_gib * 100.0,
|
|
"cached_pct": cached_gib / total_gib * 100.0,
|
|
"swap_total": swap_total,
|
|
"swap_used": swap_used,
|
|
"swap_pct": swap_used / swap_total * 100.0 if swap_total > 0 else 0.0,
|
|
}
|
|
|
|
|
|
def _build_widgets():
|
|
"""Create every placed widget with its props."""
|
|
widgets = []
|
|
|
|
sysbox = create('XSysBox', 'XSysBox_001', 16, 16, 500, 190)
|
|
sysbox.set_prop("label", "MEMORY")
|
|
sysbox.set_prop("unit", "%")
|
|
sysbox.set_prop("minimum", "0")
|
|
sysbox.set_prop("maximum", "100")
|
|
widgets.append(sysbox)
|
|
|
|
donut_mem = create('XDonutGauge', 'XDonutGauge_001', 532, 16, 230, 190)
|
|
donut_mem.set_prop("label", "Memory used")
|
|
donut_mem.set_prop("unit", "used")
|
|
widgets.append(donut_mem)
|
|
|
|
donut_swap = create('XDonutGauge', 'XDonutGauge_002', 778, 16, 230, 190)
|
|
donut_swap.set_prop("label", "Swap used")
|
|
donut_swap.set_prop("unit", "swap")
|
|
widgets.append(donut_swap)
|
|
|
|
stack = create('XStackBar', 'XStackBar_001', 16, 222, 992, 60)
|
|
stack.set_prop("label", "Memory")
|
|
stack.set_segments([
|
|
("Used", 0.0, (0.13, 0.77, 0.37)),
|
|
("Cached", 0.0, (0.95, 0.75, 0.20)),
|
|
("Buffers", 0.0, (0.20, 0.75, 0.85)),
|
|
("Free", 0.0, (0.78, 0.78, 0.78)),
|
|
])
|
|
widgets.append(stack)
|
|
|
|
history = create('XHistoryGraph', 'XHistoryGraph_001', 16, 298, 992, 310)
|
|
history.set_prop("label", "Memory history")
|
|
history.set_prop("series-count", "2")
|
|
history.set_prop("series-names", "Used %|Cached %")
|
|
history.set_prop("minimum", "0")
|
|
history.set_prop("maximum", "100")
|
|
widgets.append(history)
|
|
|
|
bar_mem = create('XUsageBar', 'XUsageBar_001', 16, 624, 992, 36)
|
|
bar_mem.set_prop("label", "Memory used")
|
|
widgets.append(bar_mem)
|
|
|
|
bar_cached = create('XUsageBar', 'XUsageBar_002', 16, 664, 992, 36)
|
|
bar_cached.set_prop("label", "Cached")
|
|
widgets.append(bar_cached)
|
|
|
|
bar_swap = create('XUsageBar', 'XUsageBar_003', 16, 704, 992, 36)
|
|
bar_swap.set_prop("label", "Swap used")
|
|
widgets.append(bar_swap)
|
|
|
|
return widgets
|
|
|
|
|
|
_DESIGN_SIZE = (1024, 768)
|
|
_BACKGROUND = ''
|
|
_WIDGETS = []
|
|
_LAST_SIZE = [None]
|
|
_AREA = []
|
|
|
|
|
|
def _on_draw(area, cr):
|
|
bg = parse_color(_BACKGROUND, None)
|
|
if bg is None:
|
|
bg = (1.0, 1.0, 1.0)
|
|
cr.set_source_rgb(*bg)
|
|
cr.paint()
|
|
for widget in _WIDGETS:
|
|
widget.draw(cr)
|
|
return False
|
|
|
|
|
|
def _on_area_resize(area, allocation):
|
|
size = (allocation.width, allocation.height)
|
|
if size == _LAST_SIZE[0]:
|
|
return False
|
|
_LAST_SIZE[0] = size
|
|
_apply_anchors(_WIDGETS, size[0], size[1])
|
|
area.queue_draw()
|
|
return False
|
|
|
|
|
|
def _tick():
|
|
"""Feed one live /proc/meminfo sample into every widget (500 ms)."""
|
|
m = _read_meminfo()
|
|
if m is not None and len(_WIDGETS) == 8:
|
|
sysbox, donut_mem, donut_swap, stack, history, \
|
|
bar_mem, bar_cached, bar_swap = _WIDGETS
|
|
sysbox.set_prop(
|
|
"label",
|
|
"MEMORY \u2014 total %.1f GiB \u00b7 avail %.1f GiB"
|
|
% (m["total"], m["avail"]))
|
|
sysbox.set_value(m["used_pct"])
|
|
sysbox.push(m["used_pct"])
|
|
donut_mem.set_prop("unit", "used %.1f GiB" % m["used"])
|
|
donut_mem.set_value(m["used_pct"])
|
|
donut_swap.set_prop(
|
|
"unit", "swap %.1f / %.1f GiB" % (m["swap_used"], m["swap_total"]))
|
|
donut_swap.set_value(m["swap_pct"])
|
|
stack.set_segments([
|
|
("Used", m["used"], (0.13, 0.77, 0.37)),
|
|
("Cached", m["cached"], (0.95, 0.75, 0.20)),
|
|
("Buffers", m["buffers"], (0.20, 0.75, 0.85)),
|
|
("Free", m["free"], (0.78, 0.78, 0.78)),
|
|
])
|
|
history.set_prop(
|
|
"label",
|
|
"Memory history \u2014 used %.1f GiB" % m["used"])
|
|
history.push(m["used_pct"], series=0)
|
|
history.push(m["cached_pct"], series=1)
|
|
bar_mem.set_value(m["used_pct"])
|
|
bar_cached.set_value(m["cached_pct"])
|
|
bar_swap.set_value(m["swap_pct"])
|
|
if _AREA:
|
|
_AREA[0].queue_draw()
|
|
return True
|
|
|
|
|
|
def main():
|
|
window = Gtk.Window(title='Memory Monitor - monitor')
|
|
window.set_default_size(1024, 768)
|
|
window.connect("destroy", Gtk.main_quit)
|
|
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
|
|
spacing=0)
|
|
window.add(vbox)
|
|
area = Gtk.DrawingArea()
|
|
area.set_hexpand(True)
|
|
area.set_vexpand(True)
|
|
area.connect("draw", _on_draw)
|
|
area.connect("size-allocate", _on_area_resize)
|
|
vbox.pack_start(area, True, True, 0)
|
|
_WIDGETS.extend(_build_widgets())
|
|
_AREA.append(area)
|
|
_tick()
|
|
GLib.timeout_add(500, _tick)
|
|
window.show_all()
|
|
Gtk.main()
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|