forked from mtusi2026/mtusi2026lab4
35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
"""20 ms cgroup-v2 memory sampler; stop by EOF on stdin. No privileged access."""
|
|
import json
|
|
from pathlib import Path
|
|
import threading
|
|
import time
|
|
|
|
done = threading.Event()
|
|
def wait():
|
|
import sys
|
|
sys.stdin.read()
|
|
done.set()
|
|
threading.Thread(target=wait,daemon=True).start()
|
|
root = Path('/sys/fs/cgroup')
|
|
def read(name):
|
|
path = root/name
|
|
return int(path.read_text()) if path.exists() else None
|
|
def rss():
|
|
lines = Path('/proc/1/status').read_text().splitlines()
|
|
return int(next(x.split()[1] for x in lines if x.startswith('VmRSS:')))*1024
|
|
baseline = read('memory.current')
|
|
peak_before = read('memory.peak')
|
|
peak = baseline
|
|
rss_peak = rss()
|
|
samples = 0
|
|
start = time.monotonic()
|
|
while not done.is_set():
|
|
peak = max(peak,read('memory.current'))
|
|
rss_peak = max(rss_peak,rss())
|
|
samples += 1
|
|
done.wait(0.02)
|
|
print(json.dumps(dict(baseline_bytes=baseline,sampled_peak_bytes=peak,
|
|
kernel_lifetime_peak_before_bytes=peak_before,kernel_lifetime_peak_after_bytes=read('memory.peak'),
|
|
api_rss_peak_bytes=rss_peak,sample_count=samples,interval_seconds=0.02,
|
|
elapsed_seconds=time.monotonic()-start)))
|