okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ef8071303
main
1#!/usr/bin/env python3 2"""Turn a telemetry.jsonl from monitor.py into memory-summary.json. 3 4Reports the observed peaks and ranges of the engine's own memory counters, in the 5units it reports them in (bytes), so the report can state where each number came from. 6""" 7import argparse 8import json 9import statistics 10from pathlib import Path 11 12FIELDS = ("gpu_mem_used" ,"gpu_mem_total" ,"gpu_util" ,"gpu_temp_c" ,"gpu_power_w" , 13"gpu_pcie_gen" ,"gpu_pcie_width" ,"ram_used" ,"ram_total" ,"disk_read_mb" , 14"vram_free_mib" ,"expert_cache_mib" ) 15 16GIB = 1024 ** 3 17 18 19def main (): 20parser = argparse .ArgumentParser (description = __doc__ ) 21parser .add_argument ("telemetry" ,type = Path ) 22parser .add_argument ("--out" ,type = Path ,required = True ) 23args = parser .parse_args () 24 25samples ,broken = [],0 26for line in args .telemetry .read_text (encoding = "utf-8" ).splitlines (): 27if not line .strip (): 28continue 29try : 30row = json .loads (line ) 31except json .JSONDecodeError : 32broken += 1 # one line can be lost where a sampler restarts 33continue 34if "error" not in row : 35samples .append (row ) 36 37times = [row ["t" ]for row in samples ] 38summary = { 39"source" :str (args .telemetry .name ), 40"samples" :len (samples ), 41"unparseable_lines" :broken , 42"sampled_seconds" :round (max (times )- min (times ),1 )if times else 0 , 43"note" : ("Each sample is one GET /metrics, so the values are the engine's own view. " 44"Sampled peaks can miss shorter spikes between samples." ), 45"fields" : {}, 46 } 47for field in FIELDS : 48values = [row [field ]for row in samples if row .get (field )is not None ] 49if not values : 50continue 51entry = {"median" :statistics .median (values ),"min" :min (values ),"max" :max (values )} 52if field in ("gpu_mem_used" ,"gpu_mem_total" ,"ram_used" ,"ram_total" ): 53entry ["max_gib" ]= round (max (values )/ GIB ,2 ) 54entry ["median_gib" ]= round (statistics .median (values )/ GIB ,2 ) 55summary ["fields" ][field ]= entry 56 57args .out .write_text (json .dumps (summary ,indent = 2 )+ "\n" ,encoding = "utf-8" ) 58json .dumps (summary ["fields" ],indent = 2 )) 59 60 61if __name__ == "__main__" : 62main ()