From 135344ec4615ee7ef8fd711f02cefdda218ba76c Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Tue, 2 May 2023 05:30:57 +0300 Subject: [PATCH] test refactor process stats --- frigate/app.py | 12 +++++++++++- frigate/stats.py | 8 ++++++++ frigate/util.py | 8 +------- web/src/routes/System.jsx | 8 +++++--- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/frigate/app.py b/frigate/app.py index a07a4465f..7e348621a 100644 --- a/frigate/app.py +++ b/frigate/app.py @@ -8,6 +8,7 @@ import signal import sys from typing import Optional from types import FrameType +import psutil import traceback from peewee_migrate import Router @@ -58,6 +59,7 @@ class FrigateApp: self.plus_api = PlusApi() self.camera_metrics: dict[str, CameraMetricsTypes] = {} self.record_metrics: dict[str, RecordMetricsTypes] = {} + self.processes: dict[str, int] def set_environment_vars(self) -> None: for key, value in self.config.environment_vars.items(): @@ -171,6 +173,12 @@ class FrigateApp: migrate_db.close() + def init_go2rtc(self) -> None: + for proc in psutil.process_iter(['pid', 'name']): + if proc.info['name'] == 'go2rtc': + self.processes["go2rtc"] = proc.info['pid'] + + def init_recording_manager(self) -> None: recording_process = mp.Process( target=manage_recordings, @@ -179,6 +187,7 @@ class FrigateApp: ) recording_process.daemon = True self.recording_process = recording_process + self.processes["recording"] = recording_process.pid recording_process.start() logger.info(f"Recording process started: {recording_process.pid}") @@ -191,7 +200,7 @@ class FrigateApp: def init_stats(self) -> None: self.stats_tracking = stats_init( - self.config, self.camera_metrics, self.detectors + self.config, self.camera_metrics, self.detectors, self.processes ) def init_web_server(self) -> None: @@ -412,6 +421,7 @@ class FrigateApp: self.init_database() self.init_onvif() self.init_recording_manager() + self.init_go2rtc() self.bind_database() self.init_dispatcher() except Exception as e: diff --git a/frigate/stats.py b/frigate/stats.py index 1ceb5f30d..c9b31bcc1 100644 --- a/frigate/stats.py +++ b/frigate/stats.py @@ -46,6 +46,7 @@ def stats_init( config: FrigateConfig, camera_metrics: dict[str, CameraMetricsTypes], detectors: dict[str, ObjectDetectProcess], + processes: dict[str, int], ) -> StatsTrackingTypes: stats_tracking: StatsTrackingTypes = { "camera_metrics": camera_metrics, @@ -53,6 +54,7 @@ def stats_init( "started": int(time.time()), "latest_frigate_version": get_latest_version(config), "last_updated": int(time.time()), + "processes": processes, } return stats_tracking @@ -260,6 +262,12 @@ def stats_snapshot( "mount_type": get_fs_type(path), } + stats["processes"] = {} + for name, pid in stats_tracking["processes"].items(): + stats["processes"][name] = { + "pid": pid, + } + return stats diff --git a/frigate/util.py b/frigate/util.py index b26e28c9f..a921d2b50 100755 --- a/frigate/util.py +++ b/frigate/util.py @@ -817,13 +817,7 @@ def get_cpu_stats() -> dict[str, dict]: else: mem_pct = round((mem_res / total_mem) * 100, 1) - idx = pid - if stats[1] == "(go2rtc)": - idx = "go2rtc" - if stats[1].startswith("(frigate.r"): - idx = "recording" - - usages[idx] = { + usages[pid] = { "cpu": str(round(cpu_usage, 2)), "mem": f"{mem_pct}", } diff --git a/web/src/routes/System.jsx b/web/src/routes/System.jsx index 0320e237b..7fa5adcb8 100644 --- a/web/src/routes/System.jsx +++ b/web/src/routes/System.jsx @@ -29,12 +29,14 @@ export default function System() { detectors, service = {}, detection_fps: _, + processes, ...cameras } = stats || initialStats || emptyObject; const detectorNames = Object.keys(detectors || emptyObject); const gpuNames = Object.keys(gpu_usages || emptyObject); const cameraNames = Object.keys(cameras || emptyObject); + const processesNames = Object.keys(processes || emptyObject); const onHandleFfprobe = async (camera, e) => { if (e) { @@ -347,7 +349,7 @@ export default function System() { Other Processes
- {['go2rtc', 'recording'].map((process) => ( + {processesNames.map((process) => (
{process}
@@ -362,8 +364,8 @@ export default function System() { - {cpu_usages[process]?.['cpu'] || '- '}% - {cpu_usages[process]?.['mem'] || '- '}% + {cpu_usages[processes[process]['pid']]?.['cpu'] || '- '}% + {cpu_usages[processes[process]['pid']]?.['mem'] || '- '}%