Get intel stats manually if parsing fails

This commit is contained in:
Nicolas Mowen 2024-10-25 06:20:56 -06:00
parent 4dadf6d353
commit acdd43e71c

View File

@ -257,6 +257,40 @@ def get_amd_gpu_stats() -> dict[str, str]:
def get_intel_gpu_stats() -> dict[str, str]: def get_intel_gpu_stats() -> dict[str, str]:
"""Get stats using intel_gpu_top.""" """Get stats using intel_gpu_top."""
def get_stats_manually(output: str) -> dict[str, str]:
"""Find global stats via regex when json fails to parse."""
reading = "".join(output)
results: dict[str, str] = {}
# render is used for qsv
render = []
for result in re.findall(r'"Render/3D/0":{[a-z":\d.,%]+}', reading):
packet = json.loads(result[14:])
single = packet.get("busy", 0.0)
render.append(float(single))
if render:
render_avg = sum(render) / len(render)
else:
render_avg = 1
# video is used for vaapi
video = []
for result in re.findall(r'"Video/\d":{[a-z":\d.,%]+}', reading):
packet = json.loads(result[10:])
single = packet.get("busy", 0.0)
video.append(float(single))
if video:
video_avg = sum(video) / len(video)
else:
video_avg = 1
results["gpu"] = f"{round((video_avg + render_avg) / 2, 2)}%"
results["mem"] = "-%"
return results
intel_gpu_top_command = [ intel_gpu_top_command = [
"timeout", "timeout",
"0.5s", "0.5s",
@ -284,22 +318,8 @@ def get_intel_gpu_stats() -> dict[str, str]:
try: try:
data = json.loads(f"[{output}]") data = json.loads(f"[{output}]")
except json.JSONDecodeError: except json.JSONDecodeError:
data = None return get_stats_manually(output)
# json is incomplete, remove characters until we get to valid json
while True:
while output and output[-1] != "}":
output = output[:-1]
if not output:
return {"gpu": "", "mem": ""}
try:
data = json.loads(f"[{output}]")
break
except json.JSONDecodeError:
output = output[:-1]
continue
results: dict[str, str] = {} results: dict[str, str] = {}
render = {"global": []} render = {"global": []}