From 2e1958a20be07ca15f55925d2321c0be97f2d5ec Mon Sep 17 00:00:00 2001 From: Nick Mowen Date: Sat, 24 Sep 2022 19:29:02 -0600 Subject: [PATCH] Start to add support for intel GPU stats --- frigate/stats.py | 27 ++++++++++++++++----------- frigate/util.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/frigate/stats.py b/frigate/stats.py index 11a100549..7ebd87dba 100644 --- a/frigate/stats.py +++ b/frigate/stats.py @@ -13,7 +13,7 @@ from frigate.comms.dispatcher import Dispatcher from frigate.config import FrigateConfig from frigate.const import DRIVER_AMD, DRIVER_ENV_VAR, RECORD_DIR, CLIPS_DIR, CACHE_DIR from frigate.types import StatsTrackingTypes, CameraMetricsTypes -from frigate.util import get_amd_gpu_stats, get_nvidia_gpu_stats +from frigate.util import get_amd_gpu_stats, get_intel_gpu_stats, get_nvidia_gpu_stats from frigate.version import VERSION from frigate.util import get_cpu_stats from frigate.object_detection import ObjectDetectProcess @@ -85,12 +85,12 @@ def get_temperatures() -> dict[str, float]: def get_gpu_stats(config: FrigateConfig) -> dict[str, str]: """Parse GPUs from hwaccel args and use for stats.""" - hwaccel_args = set(map(lambda camera: camera.ffmpeg.hwaccel_args, config.cameras.values())) + hwaccel_args = set( + map(lambda camera: camera.ffmpeg.hwaccel_args, config.cameras.values()) + ) stats: dict[str, dict] = {} for args in hwaccel_args: - gpu: dict[str, str] = {} - if "cuvid" in args: # nvidia GPU nvidia_usage = get_nvidia_gpu_stats() @@ -99,23 +99,28 @@ def get_gpu_stats(config: FrigateConfig) -> dict[str, str]: stats[nvidia_usage["name"]] = nvidia_usage elif "qsv" in args: # intel QSV GPU - gpu["name"] = "intel-qsv" - gpu["gpu_usage"] = "100" - gpu["memory_usage"] = "200" + intel_usage = get_intel_gpu_stats() + + if intel_usage: + stats["intel-qsv"] = intel_usage elif "vaapi" in args: driver = os.environ.get(DRIVER_ENV_VAR) if driver == DRIVER_AMD: + # AMD VAAPI GPU amd_usage = get_amd_gpu_stats() if amd_usage: stats["amd-vaapi"] = amd_usage else: - gpu["name"] = "intel-vaapi" - gpu["usage"] = "100" - gpu["memory"] = "200" + # intel VAAPI GPU + intel_usage = get_intel_gpu_stats() + + if intel_usage: + stats["intel-qsv"] = intel_usage elif "v4l2m2m" in args: - gpu["name"] = "RPi" + # RPi v4l2m2m + stats["rpi-v4l2m2m"] = {} return stats diff --git a/frigate/util.py b/frigate/util.py index 7e22a18e2..1001440e0 100755 --- a/frigate/util.py +++ b/frigate/util.py @@ -812,6 +812,40 @@ def get_amd_gpu_stats() -> dict[str, str]: return results +def get_intel_gpu_stats() -> dict[str, str]: + """Get stats using intel_gpu_top.""" + radeontop_command = [ + "timeout", + "1s", + "intel_gpu_top", + "-J", + "-o", + "-", + "-s", + "1" + ] + + p = sp.run( + radeontop_command, + encoding="ascii", + capture_output=True, + ) + + if p.returncode != 0: + logger.error(p.stderr) + return None + else: + usages = p.stdout.split(",") + results: dict[str, str] = {} + + for hw in usages: + if "gpu" in hw: + results["gpu_usage"] = f"{hw.strip().split(' ')[1].split(' ')[0]} %" + elif "vram" in hw: + results["memory_usage"] = f"{hw.strip().split(' ')[1].split(' ')[0]} %" + + return results + def get_nvidia_gpu_stats() -> dict[str, str]: """Get stats using nvidia-smi."""