0.2.0: card Memoria toccabile con dettaglio RAM per servizio/container
This commit is contained in:
1 parent
99aba8fa03
commit
714c4d5189
8 files changed
+237
-10
No files matched your search
@@ -32,6 +32,8 @@ DEFAULT_PORT = 8792
|
||||
# riavvii. Creato con una riga di root, vedi README.
|
||||
BORG_MARKER = "/var/lib/borg-last-run"
|
||||
|
||||
CGROUP_ROOT = Path("/sys/fs/cgroup")
|
||||
|
||||
|
||||
# ── helper ────────────────────────────────────────────────────────────────
|
||||
def run(cmd: list[str], timeout: float = 4.0) -> str:
|
||||
@@ -95,6 +97,75 @@ def cpu_info(sample: float = 0.1) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _cgroup_anon(path: Path) -> int | None:
|
||||
"""Memoria anonima (heap, stack: RAM vera) di un cgroup, in byte."""
|
||||
try:
|
||||
with open(path / "memory.stat") as fh:
|
||||
for line in fh:
|
||||
if line.startswith("anon "):
|
||||
return int(line.split()[1])
|
||||
except OSError:
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
def memory_consumers(limit: int = 12, min_mb: int = 16) -> list[dict]:
|
||||
"""Chi occupa la RAM, ordinato dal più grande.
|
||||
|
||||
/proc è montato con hidepid=invisible: i processi degli altri utenti sono
|
||||
invisibili, quindi il dettaglio per PID non è ottenibile senza root. I
|
||||
cgroup v2 invece sono leggibili e danno la memoria anonima per unità o
|
||||
contenitore. Sottraggo i discendenti, altrimenti un genitore conterebbe
|
||||
anche i figli e la somma sballerebbe.
|
||||
"""
|
||||
anon: dict[str, int] = {}
|
||||
stack = [CGROUP_ROOT]
|
||||
while stack:
|
||||
path = stack.pop()
|
||||
try:
|
||||
stack.extend(p for p in path.iterdir() if p.is_dir())
|
||||
except OSError:
|
||||
pass
|
||||
value = _cgroup_anon(path)
|
||||
if value:
|
||||
anon[str(path)] = value
|
||||
|
||||
# system.slice/docker-<id>.scope -> nome del contenitore
|
||||
docker_names = {}
|
||||
for line in run(["docker", "ps", "--format", "{{.ID}} {{.Names}}"]).splitlines():
|
||||
parts = line.split()
|
||||
if len(parts) >= 2:
|
||||
docker_names[parts[0]] = parts[1]
|
||||
|
||||
rows = []
|
||||
for path, value in anon.items():
|
||||
children = sum(
|
||||
v for p, v in anon.items() if p != path and p.startswith(path + "/")
|
||||
)
|
||||
exclusive = value - children
|
||||
if exclusive < min_mb * 1024 * 1024:
|
||||
continue
|
||||
leaf = path.rsplit("/", 1)[-1]
|
||||
kind = "service"
|
||||
name = leaf
|
||||
if leaf.startswith("docker-") and leaf.endswith(".scope"):
|
||||
cid = leaf[len("docker-") : -len(".scope")]
|
||||
name = docker_names.get(cid[:12], cid[:12])
|
||||
kind = "container"
|
||||
elif leaf.endswith(".service"):
|
||||
name = leaf[: -len(".service")]
|
||||
elif leaf.endswith(".scope"):
|
||||
name = leaf[: -len(".scope")]
|
||||
kind = "session"
|
||||
elif leaf.endswith(".slice"):
|
||||
name = leaf[: -len(".slice")]
|
||||
kind = "slice"
|
||||
rows.append({"name": name, "kind": kind, "bytes": exclusive})
|
||||
|
||||
rows.sort(key=lambda row: -row["bytes"])
|
||||
return rows[:limit]
|
||||
|
||||
|
||||
def memory_info() -> dict:
|
||||
mem = {}
|
||||
for line in Path("/proc/meminfo").read_text().splitlines():
|
||||
@@ -109,6 +180,7 @@ def memory_info() -> dict:
|
||||
"percent": pct(total - avail, total),
|
||||
"swap_total_bytes": swap_total,
|
||||
"swap_used_bytes": swap_used,
|
||||
"consumers": memory_consumers(),
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user