upload_memory.py recreates the replicas and the balancer for every run of 10, 20 and 40 concurrent uploads of 1 and 10 MiB with 64 slots (effectively no limit) and with 3, drives them through lab4_seed.py from the prepared tooling, and keeps per-run JSON plus a summary with the cgroup memory.peak of each replica and of the balancer and the oom_kill counters. The seed script now also records memory.current before the load and reads X-Instance-Id regardless of case. The first run showed that slots alone only move a waiting body out of the worker: with 3 slots and 40 uploads of 10 MiB the kernel grew the socket buffers of HAProxy to about 110 MB and the 128 MiB balancer was killed. HAProxy now caps its client and server socket buffers at 64 KiB and the API replicas cap tcp_rmem and tcp_wmem at 256 KiB, which keeps the balancer at 30 MiB. glibc raised its mmap threshold after the first large free and kept freed 10 MiB bodies in the heap, so the API fixes MALLOC_MMAP_THRESHOLD_ at 128 KiB. With these settings 40 concurrent uploads of 10 MiB peak at 323 MiB of 512 per replica and all 80 succeed, while without the slot limit the same load reaches 512 MiB, a worker is killed and only 31 of 80 succeed. UPLOAD_CONCURRENCY stays at 3. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27
118 lines
4.1 KiB
Python
Executable File
118 lines
4.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import argparse
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "scripts"))
|
|
seed = __import__("lab4_seed")
|
|
FORMAT = "{:>5} {:>5} {:>4} {:<18} {:>7} {:>7} {:>8} {:>8} {:>7} {:>8} {:>6}"
|
|
COLUMNS = FORMAT.format(
|
|
"slots",
|
|
"size",
|
|
"conc",
|
|
"codes",
|
|
"p50_ms",
|
|
"p95_ms",
|
|
"api_idle",
|
|
"api_peak",
|
|
"api_oom",
|
|
"lb_peak",
|
|
"lb_oom",
|
|
)
|
|
|
|
|
|
OVERRIDES = ("UPLOAD_CONCURRENCY", "MALLOC_MMAP_THRESHOLD_")
|
|
|
|
|
|
def recreate(slots):
|
|
env = dict(os.environ, UPLOAD_CONCURRENCY=str(slots))
|
|
if not slots:
|
|
env = {k: v for k, v in os.environ.items() if k not in OVERRIDES}
|
|
cmd = ["docker", "compose", "up", "-d", "--no-deps", "--force-recreate", "--wait", "api", "lb"]
|
|
subprocess.run(cmd, cwd=ROOT, env=env, check=True, capture_output=True)
|
|
subprocess.run([str(ROOT / "scripts/lb-wait.sh")], cwd=ROOT, check=True)
|
|
|
|
|
|
def balancer():
|
|
cid = seed.run(["docker", "compose", "ps", "-q", "lb"]).strip()
|
|
events = dict(row.split() for row in seed.cgroup(cid, "memory.events").splitlines())
|
|
peak = seed.cgroup(cid, "memory.peak")
|
|
return {
|
|
"cgroup_memory_peak_mib": round(int(peak) / 1048576, 1),
|
|
"oom_kill": int(events["oom_kill"]),
|
|
}
|
|
|
|
|
|
def line(report):
|
|
memory = report["memory"].values()
|
|
codes = ",".join(f"{k}x{v}" for k, v in sorted(report["status_codes"].items()))
|
|
return FORMAT.format(
|
|
report["upload_concurrency"],
|
|
f"{report['body_bytes'] / 1048576:.1f}M",
|
|
report["concurrency"],
|
|
codes,
|
|
report["latency_ms"]["p50"],
|
|
report["latency_ms"]["p95"],
|
|
max(m["idle_mib"] for m in memory),
|
|
max(m["cgroup_memory_peak_mib"] or 0 for m in memory),
|
|
sum(m["oom_kill"] for m in memory),
|
|
report["lb"]["cgroup_memory_peak_mib"],
|
|
report["lb"]["oom_kill"],
|
|
)
|
|
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser(description="Peak memory of API replicas under concurrent uploads")
|
|
p.add_argument("--base-url", default=os.environ.get("BASE_URL", "http://localhost:8080"))
|
|
p.add_argument("--slots", type=int, nargs="+", default=[64, 3])
|
|
p.add_argument("--sizes", type=int, nargs="+", default=[1 << 20, 10 << 20])
|
|
p.add_argument("--levels", type=int, nargs="+", default=[10, 20, 40])
|
|
p.add_argument("--rounds", type=int, default=2, help="uploads per concurrent client")
|
|
p.add_argument("--out", default="evidence/upload-memory")
|
|
args = p.parse_args()
|
|
out = ROOT / args.out
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
commit = seed.run(["git", "rev-parse", "--short", "HEAD"]).strip()
|
|
threshold = os.environ.get("MALLOC_MMAP_THRESHOLD_", "131072")
|
|
lines = [
|
|
f"# {time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())} commit {commit}; api x2: 1 CPU,"
|
|
f" 512 MiB, 2 workers; MALLOC_MMAP_THRESHOLD_={threshold}; {args.rounds} uploads"
|
|
" per client; peaks from cgroup memory.peak, MiB",
|
|
COLUMNS,
|
|
]
|
|
print("\n".join(lines), flush=True)
|
|
try:
|
|
for slots in args.slots:
|
|
for size in args.sizes:
|
|
for level in args.levels:
|
|
recreate(slots)
|
|
run = argparse.Namespace(
|
|
base_url=args.base_url,
|
|
timeout=60,
|
|
size=size,
|
|
count=level * args.rounds,
|
|
concurrency=level,
|
|
sample=True,
|
|
service="api",
|
|
link=0,
|
|
ids_out=None,
|
|
)
|
|
report = {"upload_concurrency": slots, **seed.over_http(run)}
|
|
report["lb"] = balancer()
|
|
name = f"slots{slots}-{size >> 20}mib-c{level}.json"
|
|
(out / name).write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
|
|
lines.append(line(report))
|
|
print(lines[-1], flush=True)
|
|
finally:
|
|
recreate(None)
|
|
(out / "summary.txt").write_text("\n".join(lines) + "\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|