Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix intel gpu stats #4581

Merged
merged 3 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions frigate/test/test_gpu_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ def test_nvidia_gpu_stats(self, sp):
@patch("subprocess.run")
def test_intel_gpu_stats(self, sp):
process = MagicMock()
process.returncode = 0
process.returncode = 124
process.stdout = self.intel_results
sp.return_value = process
intel_stats = get_intel_gpu_stats()
assert intel_stats == {
"gpu": "10.73 %",
"gpu": "1.34 %",
"mem": "- %",
}
44 changes: 28 additions & 16 deletions frigate/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ def get_intel_gpu_stats() -> dict[str, str]:
"""Get stats using intel_gpu_top."""
intel_gpu_top_command = [
"timeout",
"0.1s",
"0.5s",
"intel_gpu_top",
"-J",
"-o",
Expand All @@ -812,27 +812,39 @@ def get_intel_gpu_stats() -> dict[str, str]:
capture_output=True,
)

if p.returncode != 0:
# timeout has a non-zero returncode when timeout is reached
if p.returncode != 124:
logger.error(p.stderr)
return None
else:
readings = json.loads(f"[{p.stdout}]")
reading = "".join(p.stdout.split())
results: dict[str, str] = {}

for reading in readings:
if reading.get("engines", {}).get("Video/0", {}).get(
"busy", 0
) or reading.get("engines", {}).get("Video/1", {}).get("busy", 0):
gpu_usage = round(
float(reading.get("engines", {}).get("Video/0", {}).get("busy", 0))
+ float(
reading.get("engines", {}).get("Video/1", {}).get("busy", 0)
),
2,
)
results["gpu"] = f"{gpu_usage} %"
break
# render is used for qsv
render = []
for result in re.findall('"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('"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

Expand Down