okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6e1085c1d
main
1"""Read-only, one-second sysfs/proc snapshots for attributing benchmark drift.""" 2from pathlib import Path 3import threading 4import time 5 6from single_gpu import BDF 7 8 9class ResourceSampler : 10def __init__ (self ,pid ): 11device = Path ("/sys/bus/pci/devices" )/ BDF 12paths = [device / name for name in ("gpu_busy_percent" ,"mem_busy_percent" ,"current_link_speed" , 13"current_link_width" ,"pp_dpm_sclk" ,"pp_dpm_mclk" )] 14for hwmon in (device / "hwmon" ).glob ("hwmon*" ): 15paths .extend (hwmon / name for name in ("temp1_input" ,"temp2_input" ,"temp3_input" ,"freq1_input" , 16"freq2_input" ,"power1_average" ,"fan1_input" )) 17paths += [Path (f"/proc/ { pid } /io" ),Path (f"/proc/ { pid } /schedstat" ),Path (f"/proc/ { pid } /stat" ), 18Path ("/proc/loadavg" )] 19paths += [Path ("/proc/pressure" )/ kind for kind in ("cpu" ,"io" ,"memory" )] 20paths += list (Path ("/sys/block" ).glob ("nvme*/stat" )) 21self .paths = [p for p in paths if p .exists ()] 22self .samples = [] 23self .stop = threading .Event () 24self .thread = threading .Thread (target = self .run ,daemon = True ) 25self .thread .start () 26 27def run (self ): 28while not self .stop .is_set (): 29begin = time .perf_counter () 30values = {} 31for path in self .paths : 32try : 33values [str (path )]= path .read_text ().strip () 34except OSError as exc : 35values [str (path )]= {"error" :str (exc )} 36self .samples .append ({"monotonic_s" :begin ,"unix_s" :time .time (),"values" :values , 37"read_ms" : (time .perf_counter ()- begin )* 1000 }) 38self .stop .wait (1 ) 39 40def close (self ): 41self .stop .set () 42self .thread .join () 43return {"scope" :"read-only system snapshots, raw sysfs units; shared host; no clock/power changes" , 44"interval_s" :1 ,"samples" :self .samples }