68 lines
3.9 KiB
Python
68 lines
3.9 KiB
Python
"""Container resource sampling shared by the lab2 HTTP measurements."""
|
|
import subprocess
|
|
import threading
|
|
import time
|
|
|
|
|
|
class Sampler:
|
|
"""PID 1 RSS and cgroup-v2 CPU/memory. Only samples inside the phase are used."""
|
|
|
|
files = ("/proc/1/status", "/sys/fs/cgroup/cpu.stat",
|
|
"/sys/fs/cgroup/memory.current", "/sys/fs/cgroup/memory.events")
|
|
|
|
def __init__(self, name, interval):
|
|
self.name, self.interval = name, interval
|
|
self.stop_event = threading.Event()
|
|
self.samples, self.errors = [], []
|
|
self.thread = threading.Thread(target=self.loop, daemon=True)
|
|
|
|
def loop(self):
|
|
while not self.stop_event.is_set():
|
|
started = time.monotonic()
|
|
try:
|
|
result = subprocess.run(["docker", "exec", self.name, "awk",
|
|
'FNR == 1 {print "FILE " FILENAME} {print}', *self.files],
|
|
timeout=4, capture_output=True, text=True)
|
|
if result.returncode:
|
|
raise RuntimeError("resource sampling failed (container stopped or cgroup v2 unavailable)")
|
|
sections, current = {}, None
|
|
for line in result.stdout.splitlines():
|
|
if line.startswith("FILE "):
|
|
current = line[5:]
|
|
sections[current] = []
|
|
elif current:
|
|
sections[current].append(line)
|
|
status = dict(line.split(":", 1) for line in sections[self.files[0]] if ":" in line)
|
|
cpu = dict(line.split() for line in sections[self.files[1]])
|
|
events = dict(line.split() for line in sections[self.files[3]])
|
|
self.samples.append({"started": started, "finished": time.monotonic(),
|
|
"rss_bytes": int(status["VmRSS"].split()[0]) * 1024,
|
|
"cgroup_memory_bytes": int(sections[self.files[2]][0]),
|
|
"cpu_usage_usec": int(cpu["usage_usec"]),
|
|
"oom_kill_count": int(events.get("oom_kill", 0))})
|
|
except (RuntimeError, ValueError, KeyError, subprocess.SubprocessError) as exc:
|
|
self.errors.append(str(exc))
|
|
self.stop_event.wait(max(0, self.interval - (time.monotonic() - started)))
|
|
|
|
def finish(self, started, ended, cpus):
|
|
self.stop_event.set()
|
|
self.thread.join(timeout=5)
|
|
samples = [s for s in self.samples if s["started"] >= started and s["finished"] <= ended]
|
|
if self.errors or len(samples) < 2:
|
|
raise RuntimeError(self.errors[0] if self.errors else "fewer than two resource samples; increase duration")
|
|
first, last = samples[0], samples[-1]
|
|
window = last["finished"] - first["finished"]
|
|
cores = (last["cpu_usage_usec"] - first["cpu_usage_usec"]) / 1e6 / window
|
|
rows = [{"seconds": round(s["finished"] - started, 4),
|
|
"rss_bytes": s["rss_bytes"], "cgroup_memory_bytes": s["cgroup_memory_bytes"],
|
|
"cpu_usage_usec": s["cpu_usage_usec"], "oom_kill_count": s["oom_kill_count"]}
|
|
for s in samples]
|
|
return {"sample_interval_seconds": self.interval, "sample_count": len(rows),
|
|
"sample_window_seconds": window, "mean_cpu_cores": cores,
|
|
"mean_cpu_percent_of_limit": 100 * cores / cpus,
|
|
"rss_first_bytes": first["rss_bytes"],
|
|
"rss_peak_observed_bytes": max(s["rss_bytes"] for s in samples),
|
|
"cgroup_memory_peak_observed_bytes": max(s["cgroup_memory_bytes"] for s in samples),
|
|
"oom_kill_count": max(s["oom_kill_count"] for s in samples), "samples": rows,
|
|
"method": "RSS: /proc/1/status VmRSS. CPU: delta cgroup-v2 usage_usec / sample window. cgroup memory includes cache and sampling processes; it is not RSS. Peaks are sampled, not exact maxima."}
|