Add support for cpu usage stats

This commit is contained in:
Nick Mowen 2022-11-09 13:43:00 -07:00
parent 77d86de177
commit da5e800e6c
2 changed files with 35 additions and 0 deletions

View File

@ -14,6 +14,7 @@ from frigate.config import FrigateConfig
from frigate.const import RECORD_DIR, CLIPS_DIR, CACHE_DIR from frigate.const import RECORD_DIR, CLIPS_DIR, CACHE_DIR
from frigate.types import StatsTrackingTypes, CameraMetricsTypes from frigate.types import StatsTrackingTypes, CameraMetricsTypes
from frigate.version import VERSION from frigate.version import VERSION
from frigate.util import get_cpu_stats
from frigate.object_detection import ObjectDetectProcess from frigate.object_detection import ObjectDetectProcess
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -116,6 +117,8 @@ def stats_snapshot(stats_tracking: StatsTrackingTypes) -> dict[str, Any]:
} }
stats["detection_fps"] = round(total_detection_fps, 2) stats["detection_fps"] = round(total_detection_fps, 2)
stats["cpu_usages"] = get_cpu_stats()
stats["service"] = { stats["service"] = {
"uptime": (int(time.time()) - stats_tracking["started"]), "uptime": (int(time.time()) - stats_tracking["started"]),
"version": VERSION, "version": VERSION,

View File

@ -1,6 +1,8 @@
import copy import copy
import datetime import datetime
import logging import logging
import subprocess as sp
import json
import re import re
import signal import signal
import traceback import traceback
@ -679,6 +681,36 @@ def escape_special_characters(path: str) -> str:
return path return path
def get_cpu_stats() -> dict[str, dict]:
"""Get cpu usages for each process id"""
usages = {}
top_command = ["top", "-b", "-n", "1"]
p = sp.run(
top_command,
encoding="ascii",
capture_output=True,
)
if p.returncode != 0:
logger.error(p.stderr)
return usages
else:
lines = p.stdout.split("\n")
for line in lines:
stats = list(filter(lambda a: a != "", line.strip().split(" ")))
try:
usages[stats[0]] = {
"cpu": stats[8],
"mem": stats[9],
}
except:
continue
return usages
class FrameManager(ABC): class FrameManager(ABC):
@abstractmethod @abstractmethod
def create(self, name, size) -> AnyStr: def create(self, name, size) -> AnyStr: