The browser page at http://127.0.0.1:8080 is now the Strata app, in the Strata design language (serve/web, same origin, no CDN): - Chat: streaming answers, collapsible thinking, Markdown with code copy, images when enabled, sampling/thinking settings, export, new chat. - Monitor: live model state (reading with progress / generating at tok/s), GPU load, VRAM, temperature, power, PCIe traffic, CPU, RAM, disk, the context fill and the last requests, each with a minute of history. - About: the model and engine settings, this PC, the API addresses. Server: GET /metrics (API key when set), /web/* app files, /fonts/* only when installed locally (not shipped; the page falls back to the system font). serve/telemetry.py reads NVML via ctypes and psutil (new setup dependency) with fallbacks. The engine prints an INFO line before READY (kv, expert slots, speculation); older servers ignore it and the server works with older engines. Based on the dashboard idea in PR #22 by code-martin.
247 lines
10 KiB
Python
247 lines
10 KiB
Python
"""serve/telemetry.py - hardware readings for the web app's Monitor tab (idea from PR #22 by code-martin).
|
|
|
|
A background thread samples once a second and keeps the last 60 readings of each series for the sparklines:
|
|
- GPU: NVIDIA's own NVML library (nvml.dll / libnvidia-ml.so.1, installed with every driver) through ctypes, so no
|
|
pip package is needed: load, VRAM, temperature, power, PCIe link and throughput.
|
|
- CPU, RAM, disk: `psutil` when it is installed (setup installs it); without it the CPU and RAM readings fall back to
|
|
the OS (Windows GlobalMemoryStatusEx / GetSystemTimes, Linux /proc) and the disk rate is absent.
|
|
Anything that cannot be read is None; nothing here can stop the server.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import collections
|
|
import ctypes
|
|
import os
|
|
import platform
|
|
import sys
|
|
import threading
|
|
import time
|
|
|
|
HISTORY = 60
|
|
|
|
|
|
# ------------------------------------------------------------------------------------------------ NVML
|
|
class _Nvml:
|
|
class Util(ctypes.Structure):
|
|
_fields_ = [("gpu", ctypes.c_uint), ("memory", ctypes.c_uint)]
|
|
|
|
class Mem(ctypes.Structure):
|
|
_fields_ = [("total", ctypes.c_ulonglong), ("free", ctypes.c_ulonglong), ("used", ctypes.c_ulonglong)]
|
|
|
|
def __init__(self, index=0):
|
|
self.lib = self.dev = None
|
|
names = ["nvml.dll", os.path.join(os.environ.get("ProgramFiles", r"C:\Program Files"),
|
|
"NVIDIA Corporation", "NVSMI", "nvml.dll")] if os.name == "nt" \
|
|
else ["libnvidia-ml.so.1", "libnvidia-ml.so"]
|
|
for n in names:
|
|
try:
|
|
self.lib = ctypes.CDLL(n)
|
|
break
|
|
except OSError:
|
|
continue
|
|
if self.lib is None:
|
|
return
|
|
try:
|
|
init = getattr(self.lib, "nvmlInit_v2", None) or self.lib.nvmlInit
|
|
if init() != 0:
|
|
self.lib = None
|
|
return
|
|
h = ctypes.c_void_p()
|
|
get = getattr(self.lib, "nvmlDeviceGetHandleByIndex_v2", None) or self.lib.nvmlDeviceGetHandleByIndex
|
|
if get(ctypes.c_uint(index), ctypes.byref(h)) != 0:
|
|
self.lib = None
|
|
return
|
|
self.dev = h
|
|
except (AttributeError, OSError):
|
|
self.lib = None
|
|
|
|
def ok(self):
|
|
return self.lib is not None and self.dev is not None
|
|
|
|
def _uint(self, fn, *args):
|
|
v = ctypes.c_uint()
|
|
try:
|
|
return v.value if getattr(self.lib, fn)(self.dev, *args, ctypes.byref(v)) == 0 else None
|
|
except (AttributeError, OSError):
|
|
return None
|
|
|
|
def name(self):
|
|
buf = ctypes.create_string_buffer(96)
|
|
try:
|
|
if self.lib.nvmlDeviceGetName(self.dev, buf, ctypes.c_uint(96)) == 0:
|
|
return buf.value.decode(errors="replace")
|
|
except (AttributeError, OSError):
|
|
pass
|
|
return None
|
|
|
|
def read(self):
|
|
out = {}
|
|
u = self.Util()
|
|
try:
|
|
if self.lib.nvmlDeviceGetUtilizationRates(self.dev, ctypes.byref(u)) == 0:
|
|
out["util"] = u.gpu
|
|
except (AttributeError, OSError):
|
|
pass
|
|
m = self.Mem()
|
|
try:
|
|
if self.lib.nvmlDeviceGetMemoryInfo(self.dev, ctypes.byref(m)) == 0:
|
|
out["mem_used"], out["mem_total"] = m.used, m.total
|
|
except (AttributeError, OSError):
|
|
pass
|
|
out["temp"] = self._uint("nvmlDeviceGetTemperature", ctypes.c_uint(0)) # NVML_TEMPERATURE_GPU
|
|
mw = self._uint("nvmlDeviceGetPowerUsage")
|
|
out["power"] = mw / 1000.0 if mw is not None else None
|
|
lim = self._uint("nvmlDeviceGetEnforcedPowerLimit")
|
|
out["power_limit"] = lim / 1000.0 if lim is not None else None
|
|
out["pcie_gen"] = self._uint("nvmlDeviceGetCurrPcieLinkGeneration") # drops at idle (power saving)
|
|
out["pcie_gen_max"] = self._uint("nvmlDeviceGetMaxPcieLinkGeneration")
|
|
out["pcie_width"] = self._uint("nvmlDeviceGetCurrPcieLinkWidth")
|
|
rx = self._uint("nvmlDeviceGetPcieThroughput", ctypes.c_uint(1)) # NVML_PCIE_UTIL_RX_BYTES, KB/s
|
|
tx = self._uint("nvmlDeviceGetPcieThroughput", ctypes.c_uint(0))
|
|
out["pcie_rx_mb"] = rx / 1024.0 if rx is not None else None
|
|
out["pcie_tx_mb"] = tx / 1024.0 if tx is not None else None
|
|
return out
|
|
|
|
|
|
# ------------------------------------------------------------------------------------------------ CPU / RAM
|
|
def _cpu_name():
|
|
if os.name == "nt":
|
|
try:
|
|
import winreg
|
|
k = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"HARDWARE\DESCRIPTION\System\CentralProcessor\0")
|
|
return winreg.QueryValueEx(k, "ProcessorNameString")[0].strip()
|
|
except OSError:
|
|
pass
|
|
elif os.path.exists("/proc/cpuinfo"):
|
|
for line in open("/proc/cpuinfo", encoding="utf-8", errors="replace"):
|
|
if line.startswith("model name"):
|
|
return line.split(":", 1)[1].strip()
|
|
return platform.processor() or None
|
|
|
|
|
|
class _CpuRamFallback:
|
|
"""CPU load and RAM without psutil."""
|
|
|
|
def __init__(self):
|
|
self.prev = self._times()
|
|
|
|
def _times(self):
|
|
if os.name == "nt":
|
|
idle, kern, user = (ctypes.c_ulonglong() for _ in range(3))
|
|
if ctypes.windll.kernel32.GetSystemTimes(ctypes.byref(idle), ctypes.byref(kern), ctypes.byref(user)):
|
|
return idle.value, kern.value + user.value # kernel time includes idle
|
|
return None
|
|
try:
|
|
f = [int(x) for x in open("/proc/stat").readline().split()[1:]]
|
|
return f[3] + f[4], sum(f)
|
|
except (OSError, ValueError):
|
|
return None
|
|
|
|
def cpu(self):
|
|
cur = self._times()
|
|
prev, self.prev = self.prev, cur
|
|
if not cur or not prev or cur[1] == prev[1]:
|
|
return None
|
|
return max(0.0, min(100.0, 100.0 * (1 - (cur[0] - prev[0]) / (cur[1] - prev[1]))))
|
|
|
|
@staticmethod
|
|
def ram():
|
|
if os.name == "nt":
|
|
class MS(ctypes.Structure):
|
|
_fields_ = [("dwLength", ctypes.c_ulong), ("dwMemoryLoad", ctypes.c_ulong),
|
|
("ullTotalPhys", ctypes.c_ulonglong), ("ullAvailPhys", ctypes.c_ulonglong),
|
|
("ullTotalPageFile", ctypes.c_ulonglong), ("ullAvailPageFile", ctypes.c_ulonglong),
|
|
("ullTotalVirtual", ctypes.c_ulonglong), ("ullAvailVirtual", ctypes.c_ulonglong),
|
|
("ullAvailExtendedVirtual", ctypes.c_ulonglong)]
|
|
m = MS()
|
|
m.dwLength = ctypes.sizeof(MS)
|
|
if ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(m)):
|
|
return m.ullTotalPhys - m.ullAvailPhys, m.ullTotalPhys
|
|
return None, None
|
|
try:
|
|
info = dict(line.split(":", 1) for line in open("/proc/meminfo"))
|
|
total = int(info["MemTotal"].split()[0]) * 1024
|
|
avail = int(info["MemAvailable"].split()[0]) * 1024
|
|
return total - avail, total
|
|
except (OSError, KeyError, ValueError):
|
|
return None, None
|
|
|
|
|
|
# ------------------------------------------------------------------------------------------------ the sampler
|
|
class Telemetry:
|
|
def __init__(self, extra=None):
|
|
"""`extra()` -> dict of more series to record each second (the server's tok/s)."""
|
|
self.extra = extra
|
|
self.lock = threading.Lock()
|
|
self.now: dict = {}
|
|
self.hist = collections.defaultdict(lambda: collections.deque(maxlen=HISTORY))
|
|
self.gpu = _Nvml()
|
|
try:
|
|
import psutil # noqa: F401
|
|
self.ps = sys.modules["psutil"]
|
|
except ImportError:
|
|
self.ps = None
|
|
self.fallback = _CpuRamFallback()
|
|
self.static = {
|
|
"gpu_name": self.gpu.name() if self.gpu.ok() else None,
|
|
"cpu_name": _cpu_name(),
|
|
"cores": (self.ps.cpu_count(logical=False) if self.ps else None) or None,
|
|
"threads": os.cpu_count(),
|
|
"psutil": self.ps is not None,
|
|
}
|
|
self._disk_prev = None
|
|
threading.Thread(target=self._loop, daemon=True).start()
|
|
|
|
def _disk(self):
|
|
if not self.ps:
|
|
return None, None
|
|
try:
|
|
c = self.ps.disk_io_counters()
|
|
except (OSError, RuntimeError):
|
|
return None, None
|
|
t = time.time()
|
|
prev, self._disk_prev = self._disk_prev, (t, c.read_bytes, c.write_bytes)
|
|
if prev is None or t <= prev[0]:
|
|
return None, None
|
|
dt = t - prev[0]
|
|
return (c.read_bytes - prev[1]) / dt / 2**20, (c.write_bytes - prev[2]) / dt / 2**20
|
|
|
|
def sample(self):
|
|
s = {}
|
|
if self.gpu.ok():
|
|
g = self.gpu.read()
|
|
s.update({f"gpu_{k}": v for k, v in g.items()})
|
|
if self.ps:
|
|
try:
|
|
s["cpu"] = self.ps.cpu_percent(interval=None)
|
|
vm = self.ps.virtual_memory()
|
|
s["ram_used"], s["ram_total"] = vm.total - vm.available, vm.total
|
|
except (OSError, RuntimeError):
|
|
pass
|
|
else:
|
|
s["cpu"] = self.fallback.cpu()
|
|
s["ram_used"], s["ram_total"] = self.fallback.ram()
|
|
s["disk_read_mb"], s["disk_write_mb"] = self._disk()
|
|
if self.extra:
|
|
try:
|
|
s.update(self.extra())
|
|
except Exception: # noqa: BLE001 - telemetry must never take the server down
|
|
pass
|
|
return s
|
|
|
|
def _loop(self):
|
|
while True:
|
|
s = self.sample()
|
|
with self.lock:
|
|
self.now = s
|
|
for k in ("gpu_util", "gpu_mem_used", "gpu_temp", "gpu_power", "gpu_pcie_rx_mb", "cpu", "ram_used",
|
|
"disk_read_mb", "tok_s"):
|
|
v = s.get(k)
|
|
self.hist[k].append(round(v, 2) if isinstance(v, float) else v)
|
|
time.sleep(1.0)
|
|
|
|
def snapshot(self):
|
|
with self.lock:
|
|
return {"now": dict(self.now), "history": {k: list(v) for k, v in self.hist.items()},
|
|
"static": dict(self.static)}
|