okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
818845ea1
main
1"""Intel Arc (xe driver) readings for the server's Monitor tab, in the shape of serve/telemetry.py's readers. 2 3Kept out of serve/telemetry.py so the SYCL port touches no shared file: sycl/serve/server_intel.py installs it as 4telemetry.gpu_reader when there is no NVIDIA card. Load and VRAM need root (xe's only VRAM accounting is per-client 5fdinfo), so they come from a root sampler's /run/gpustat.json (sycl/tools/gpustat.py); temperature, power and the 6PCIe link are read from sysfs, so they show without it. 7""" 8from __future__import annotations 9 10import os 11import sys 12import time 13 14 15# the cards sycl/setup_intel.py names by PCI id (INTEL_ARC) 16ARC_NAMES = {"e223" :"Arc Pro B70" ,"e221" :"Arc Pro B60" ,"e211" :"Arc Pro B60" ,"e20b" :"Arc B580" ,"e20c" :"Arc B570" , 17"e212" :"Arc B50" ,"56a0" :"Arc A770" ,"56a1" :"Arc A750" ,"56a2" :"Arc A580" ,"56a5" :"Arc A380" , 18"56a6" :"Arc A310" ,"5690" :"Arc A770M" } 19 20 21class _XeGpu : 22"""The Intel Arc readings in NVML's shape. Load and VRAM come from /run/gpustat.json (a root sampler: the only 23VRAM accounting xe exposes is per-client fdinfo, readable by root only); temperature, power and the PCIe link are 24read from sysfs too, so they show without the sampler.""" 25STAT = "/run/gpustat.json" 26 27def __init__ (self ,index = 0 ): 28import glob 29self .dev = None 30cards = []# #2001: the Intel cards in sysfs order, as setup numbers them 31for d in sorted (glob .glob ("/sys/class/drm/card[0-9]*/device" )): 32try : 33if open (f" { d } /vendor" ).read ().strip ()== "0x8086" and os .path .isdir (f" { d } /tile0" ): 34cards .append (os .path .realpath (d )) 35except OSError : 36continue 37if 0 <= index < len (cards ): 38self .dev = cards [index ] 39self .hwmon = None 40for n in glob .glob (f" { self . dev } /hwmon/hwmon*/name" )if self .dev else []: 41self .hwmon = os .path .dirname (n ) 42self ._e = None # (t, card energy uJ) for power without the sampler 43 44def ok (self ): 45return self .dev is not None and sys .platform .startswith ("linux" ) 46 47def name (self ): 48st = self ._stat () 49if (st or {}).get ("name" ): 50return st ["name" ] 51try :# the PCI id's name (setup_intel.INTEL_ARC), not a generic one 52with open (f" { self . dev } /device" )as f : 53return "Intel " + ARC_NAMES .get (f .read ().strip ().lower ().replace ("0x" ,"" ),"Arc GPU" ) 54except (OSError ,TypeError ): 55return "Intel Arc GPU" 56 57def _stat (self ): 58try : 59import json 60with open (self .STAT )as f : 61st = json .load (f ) 62return st if time .time ()- float (st .get ("ts" ,0 ))< 15 else None 63except (OSError ,ValueError ): 64return None 65 66def _rd (self ,p ): 67try : 68return open (p ).read ().strip () 69except OSError : 70return None 71 72def _link (self ): 73"""The link the card trained at: its own functions sit behind an internal x1 switch, so the first port up 74the path wider than x1; the max is what card AND slot allow (the root port caps it).""" 75gen = {"2.5" :1 ,"5.0" :2 ,"8.0" :3 ,"16.0" :4 ,"32.0" :5 ,"64.0" :6 } 76d ,chain = self .dev , [] 77while d and d .startswith ("/sys/devices/pci" )and os .path .exists (f" { d } /current_link_speed" ): 78chain .append (d ) 79d = os .path .dirname (d ) 80for p in chain : 81w = int (self ._rd (f" { p } /current_link_width" )or 0 ) 82if w > 1 : 83g = lambda q ,k :gen .get ((self ._rd (f" { q } / { k } _link_speed" )or "" ).split (" " )[0 ]) 84card ,slot = g (p ,"max" ),g (chain [- 1 ],"max" ) 85return g (p ,"current" ),min (x for x in (card ,slot )if x )if (card or slot )else None ,w 86return None ,None ,None 87 88def read (self ): 89out = {} 90st = self ._stat () 91if st : 92out ["util" ]= st .get ("busy_pct" ) 93if st .get ("vram_used_mb" )is not None : 94out ["mem_used" ]= st ["vram_used_mb" ]* 2 ** 20 95out ["mem_total" ]= st .get ("vram_total_mb" ,0 )* 2 ** 20 or None 96out ["temp" ]= st .get ("temp_pkg" ) 97out ["power" ]= st .get ("power_w" ) 98out ["power_limit" ]= st .get ("power_cap_w" ) 99out ["vram_temp" ]= st .get ("temp_vram_max" )or st .get ("temp_vram" ) 100if self .hwmon : 101if out .get ("temp" )is None : 102for lab in os .listdir (self .hwmon ): 103if lab .endswith ("_label" )and self ._rd (f" { self . hwmon } / { lab } " )== "pkg" : 104v = self ._rd (f" { self . hwmon } / { lab [: - 6 ] } _input" ) 105out ["temp" ]= int (v )/ 1000 if v else None 106if out .get ("power" )is None : 107e ,t = self ._rd (f" { self . hwmon } /energy1_input" ),time .time () 108if e and e .isdigit (): 109if self ._e and t > self ._e [0 ]and int (e )>= self ._e [1 ]: 110out ["power" ]= (int (e )- self ._e [1 ])/ 1e6 / (t - self ._e [0 ]) 111self ._e = (t ,int (e )) 112if out .get ("power_limit" )is None : 113cap = self ._rd (f" { self . hwmon } /power1_cap" ) 114out ["power_limit" ]= int (cap )/ 1e6 if cap and cap .isdigit ()and int (cap )> 0 else None 115out ["pcie_gen" ],out ["pcie_gen_max" ],out ["pcie_width" ]= self ._link () 116out ["pcie_rx_mb" ]= out ["pcie_tx_mb" ]= None # xe exposes no PCIe traffic counters 117return out