Fix RKNN default perms for binaries and libraries (#25000)

* Fix RKNN default perms for binaries and libraries

* Add docs to handle specific rknn mounts
This commit is contained in:
Nicolas Mowen
2026-11-17 15:03:11 -06:00
committed by GitHub
parent 97f2096534
commit 8a7d89170b
6 changed files with 199 additions and 21 deletions
+108
View File
@@ -0,0 +1,108 @@
"""Tests for reading Rockchip GPU and NPU load as root and as a non-root user."""
import builtins
import os
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from frigate.util import services
from frigate.util.services import get_rockchip_gpu_stats, get_rockchip_npu_stats
NPU_LOAD = "NPU load: Core0: 10%, Core1: 20%, Core2: 30%,\n"
RGA_LOAD = (
"num of scheduler = 2\n"
"================= load ==================\n"
"scheduler[0]: rga3\n\t load = 40%\n"
"scheduler[1]: rga2\n\t load = 60%\n"
)
class TestRockchipStats(unittest.TestCase):
def setUp(self):
services._rockchip_load_warning_logged = False
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.debugfs = Path(tmp.name, "debugfs")
self.bound = Path(tmp.name, "rockchip")
self.debugfs.mkdir()
for name, value in (
("ROCKCHIP_DEBUGFS_DIR", str(self.debugfs)),
("ROCKCHIP_LOAD_DIR", str(self.bound)),
):
patcher = patch.object(services, name, value)
patcher.start()
self.addCleanup(patcher.stop)
warn = patch.object(services.logger, "warning")
self.warn = warn.start()
self.addCleanup(warn.stop)
def _write(self, base: Path, driver: str, content: str) -> None:
Path(base, driver).mkdir(parents=True, exist_ok=True)
Path(base, driver, "load").write_text(content)
def _deny_debugfs(self):
"""Fail opens under debugfs the way its root-only directory does."""
debugfs = str(self.debugfs)
def guarded_open(path, *args, **kwargs):
if os.fspath(path).startswith(debugfs):
raise PermissionError(13, "Permission denied", path)
return builtins.open(path, *args, **kwargs)
return patch("frigate.util.services.open", guarded_open, create=True)
def test_reads_debugfs_when_accessible(self):
self._write(self.debugfs, "rknpu", NPU_LOAD)
self._write(self.debugfs, "rkrga", RGA_LOAD)
self.assertEqual(20.0, get_rockchip_npu_stats()["npu"])
self.assertEqual("50.0%", get_rockchip_gpu_stats()["gpu"])
self.warn.assert_not_called()
def test_missing_drivers_report_nothing_without_warning(self):
self.assertIsNone(get_rockchip_npu_stats())
self.assertIsNone(get_rockchip_gpu_stats())
self.warn.assert_not_called()
def test_reads_bound_files_when_debugfs_is_denied(self):
self._write(self.debugfs, "rknpu", NPU_LOAD)
self._write(self.debugfs, "rkrga", RGA_LOAD)
self._write(self.bound, "rknpu", NPU_LOAD)
self._write(self.bound, "rkrga", RGA_LOAD)
with self._deny_debugfs():
self.assertEqual(20.0, get_rockchip_npu_stats()["npu"])
self.assertEqual("50.0%", get_rockchip_gpu_stats()["gpu"])
self.warn.assert_not_called()
def test_warns_once_when_denied_and_nothing_is_bound(self):
self._write(self.debugfs, "rknpu", NPU_LOAD)
self._write(self.debugfs, "rkrga", RGA_LOAD)
with self._deny_debugfs():
for _ in range(2):
self.assertIsNone(get_rockchip_npu_stats())
self.assertIsNone(get_rockchip_gpu_stats())
self.warn.assert_called_once()
def test_binding_only_the_present_driver_does_not_warn(self):
# a board without RGA in debugfs binds only the NPU load file
self._write(self.debugfs, "rknpu", NPU_LOAD)
self._write(self.bound, "rknpu", NPU_LOAD)
with self._deny_debugfs():
self.assertEqual(20.0, get_rockchip_npu_stats()["npu"])
self.assertIsNone(get_rockchip_gpu_stats())
self.warn.assert_not_called()
if __name__ == "__main__":
unittest.main()
+62 -15
View File
@@ -744,12 +744,60 @@ def get_openvino_npu_stats() -> dict[str, str] | None:
return None
ROCKCHIP_DEBUGFS_DIR = "/sys/kernel/debug"
# debugfs is root-only, so a non-root install binds the driver load files here
ROCKCHIP_LOAD_DIR = "/rockchip"
_rockchip_load_warning_logged = False
def _read_rockchip_load(driver: str) -> str | None:
"""Read a Rockchip driver's load file.
The kernel makes debugfs readable only by root, so when Frigate isn't
root the file is read from where the compose file binds it instead.
Args:
driver: The driver's debugfs directory, rknpu or rkrga
Returns:
The file's contents, or None if neither location could be read
"""
global _rockchip_load_warning_logged
denied = False
for base in (ROCKCHIP_DEBUGFS_DIR, ROCKCHIP_LOAD_DIR):
try:
with open(os.path.join(base, driver, "load")) as f:
return f.read()
except PermissionError:
denied = True
except OSError:
continue
# a board without one of the drivers binds only the file it has, so
# warn only when nothing is bound at all
if (
denied
and not _rockchip_load_warning_logged
and not os.path.isdir(ROCKCHIP_LOAD_DIR)
):
logger.warning(
"Rockchip GPU and NPU usage stats are unavailable because %s is only "
"readable by root; bind the load files into the container to enable "
"them, see https://docs.frigate.video/frigate/installation#rockchip-platform",
ROCKCHIP_DEBUGFS_DIR,
)
_rockchip_load_warning_logged = True
return None
def get_rockchip_gpu_stats() -> dict[str, str | float] | None:
"""Get GPU stats using rk."""
try:
with open("/sys/kernel/debug/rkrga/load") as f:
content = f.read()
except FileNotFoundError:
content = _read_rockchip_load("rkrga")
if content is None:
return None
load_values = []
@@ -776,18 +824,17 @@ def get_rockchip_gpu_stats() -> dict[str, str | float] | None:
def get_rockchip_npu_stats() -> dict[str, float | str] | None:
"""Get NPU stats using rk."""
try:
with open("/sys/kernel/debug/rknpu/load") as f:
npu_output = f.read()
npu_output = _read_rockchip_load("rknpu")
if "Core0:" in npu_output:
# multi core NPU
core_loads = re.findall(r"Core\d+:\s*(\d+)%", npu_output)
else:
# single core NPU
core_loads = re.findall(r"NPU load:\s+(\d+)%", npu_output)
except FileNotFoundError:
core_loads = None
if npu_output is None:
return None
if "Core0:" in npu_output:
# multi core NPU
core_loads = re.findall(r"Core\d+:\s*(\d+)%", npu_output)
else:
# single core NPU
core_loads = re.findall(r"NPU load:\s+(\d+)%", npu_output)
if not core_loads:
return None