okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
707fd49eb
main
1#!/usr/bin/env python3 2"""Sample host RAM/swap and GPU memory once per second while the measurements run (one JSON line per sample). 3 4python monitor.py telemetry.jsonl & # stop it with kill when done 5""" 6import json 7import subprocess 8import sys 9import time 10from pathlib import Path 11 12FIELDS = "memory.used,memory.total,utilization.gpu,power.draw,temperature.gpu,pcie.link.gen.current" 13with Path (sys .argv [1 ]if len (sys .argv )> 1 else "telemetry.jsonl" ).open ("a" )as output : 14while True : 15memory = {} 16for line in Path ("/proc/meminfo" ).read_text ().splitlines (): 17key ,value = line .split (":" ,1 ) 18if key in ("MemTotal" ,"MemAvailable" ,"Cached" ,"SwapTotal" ,"SwapFree" ): 19memory [key + "_KiB" ]= int (value .strip ().split ()[0 ]) 20gpu = subprocess .check_output (["nvidia-smi" ,"--query-gpu=" + FIELDS ,"--format=csv,noheader,nounits" ], 21text = True ).strip () 22ps = subprocess .run (["ps" ,"-C" ,"strata,strata-vision" ,"-o" ,"comm=,rss=" ],text = True , 23capture_output = True ).stdout .split ("\n" ) 24rss = {f [0 ]:int (f [1 ])for f in (l .split ()for l in ps )if len (f )== 2 }# KiB, resident set 25output .write (json .dumps ({"epoch_s" :round (time .time (),2 ),"memory" :memory ,"gpu" :gpu , 26"rss_KiB" :rss })+ "\n" ) 27output .flush () 28time .sleep (1 )