Improve Intel GPU stats (#14073)

* Fix formatting

* Fix test
This commit is contained in:
Nicolas Mowen 2024-09-30 15:41:14 -06:00 committed by GitHub
parent 18bf7f93fa
commit 4dc4704bb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 23 deletions

View File

@ -39,7 +39,8 @@ class TestGpuStats(unittest.TestCase):
process.stdout = self.intel_results process.stdout = self.intel_results
sp.return_value = process sp.return_value = process
intel_stats = get_intel_gpu_stats() intel_stats = get_intel_gpu_stats()
print(f"the intel stats are {intel_stats}")
assert intel_stats == { assert intel_stats == {
"gpu": "1.34%", "gpu": "1.13%",
"mem": "-%", "mem": "-%",
} }

View File

@ -279,35 +279,66 @@ def get_intel_gpu_stats() -> dict[str, str]:
logger.error(f"Unable to poll intel GPU stats: {p.stderr}") logger.error(f"Unable to poll intel GPU stats: {p.stderr}")
return None return None
else: else:
reading = "".join(p.stdout.split()) data = json.loads(f'[{"".join(p.stdout.split())}]')
results: dict[str, str] = {} results: dict[str, str] = {}
render = {"global": []}
video = {"global": []}
# render is used for qsv for block in data:
render = [] global_engine = block.get("engines")
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: if global_engine:
render_avg = sum(render) / len(render) render_frame = global_engine.get("Render/3D/0", {}).get("busy")
else: video_frame = global_engine.get("Video/0", {}).get("busy")
render_avg = 1
# video is used for vaapi if render_frame is not None:
video = [] render["global"].append(float(render_frame))
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: if video_frame is not None:
video_avg = sum(video) / len(video) video["global"].append(float(video_frame))
else:
video_avg = 1
results["gpu"] = f"{round((video_avg + render_avg) / 2, 2)}%" clients = block.get("clients", {})
if clients and len(clients):
for client_block in clients.values():
key = client_block["pid"]
if render.get(key) is None:
render[key] = []
video[key] = []
client_engine = client_block.get("engine-classes", {})
render_frame = client_engine.get("Render/3D", {}).get("busy")
video_frame = client_engine.get("Video", {}).get("busy")
if render_frame is not None:
render[key].append(float(render_frame))
if video_frame is not None:
video[key].append(float(video_frame))
for key in render.keys():
print(
f"new way has {key} :: {round(((sum(render[key]) / len(render[key])) + (sum(video[key]) / len(video[key]))) / 2, 2)}% total"
)
results["gpu"] = (
f"{round(((sum(render['global']) / len(render['global'])) + (sum(video['global']) / len(video['global']))) / 2, 2)}%"
)
results["mem"] = "-%" results["mem"] = "-%"
if len(render.keys()) > 1:
results["clients"] = {}
for key in render.keys():
if key == "global":
continue
results["clients"][key] = (
f"{round(((sum(render[key]) / len(render[key])) + (sum(video[key]) / len(video[key]))) / 2, 2)}%"
)
return results return results