Files
mtusi2026lab4/scripts/research_upload_memory.py

60 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Sample API cgroups at 50ms while ten clients upload through LB."""
import json
from pathlib import Path
import subprocess
import uuid
ROOT=Path(__file__).resolve().parents[1]
COMPOSE=str(ROOT/'scripts/compose.sh')
MONITOR=r'''
import json,time,sys
from pathlib import Path
stop=Path(sys.argv[1]); group=Path('/sys/fs/cgroup')
rows=[]
print('READY',flush=True)
for i in range(2400):
rows.append({'at_unix':time.time(),'memory_current_bytes':int((group/'memory.current').read_text()),
'lifetime_memory_peak_bytes':int((group/'memory.peak').read_text()),
'cpu_stat':(group/'cpu.stat').read_text()})
if stop.exists():
stop.unlink()
break
time.sleep(.05)
print(json.dumps(rows),flush=True)
'''
def main():
ids=subprocess.check_output([COMPOSE,'ps','-q','api'],cwd=ROOT,text=True).split()
assert len(ids)>=2
monitors=[]
try:
for cid in ids:
stop='/tmp/lab4-memory-probe-'+uuid.uuid4().hex
proc=subprocess.Popen(['docker','exec',cid,'python','-u','-c',MONITOR,stop],text=True,stdout=subprocess.PIPE)
assert proc.stdout.readline().strip()=='READY'
monitors.append((cid,stop,proc))
output=subprocess.check_output([COMPOSE,'run','--rm','-T','--no-deps','ops','python','tests/concurrent_uploads.py'],cwd=ROOT,text=True,timeout=100)
workload=json.loads(output)
finally:
for cid,stop,proc in monitors:
subprocess.run(['docker','exec',cid,'touch',stop],check=True,stdout=subprocess.DEVNULL)
traces=[]
for cid,stop,proc in monitors:
data=json.loads(proc.stdout.read())
assert proc.wait(timeout=10)==0
traces.append({'container_id':cid,'samples':data,'sampled_peak_bytes':max(s['memory_current_bytes'] for s in data)})
for phase in workload['phases']:
phase['api_memory']=[{'container_id':t['container_id'],
'sampled_peak_mib':round(max(s['memory_current_bytes'] for s in t['samples'] if phase['started_unix']<=s['at_unix']<=phase['finished_unix'])/1048576,3)} for t in traces]
result={'status':'PASS','sample_interval_seconds':.05,
'note':'memory.current includes page cache, kernel charges and a small sampler process; sampled peak is not a heap profile. lifetime_memory_peak includes prior requests.',
'workload':workload,'api_traces':traces}
target=ROOT/'evidence/upload-memory.json'
target.write_text(json.dumps(result,indent=2)+'\n')
print(json.dumps({'status':'PASS','phases':workload['phases']},indent=2))
if __name__=='__main__':
main()