mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-18 03:23:01 +03:00
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:
@@ -29,11 +29,11 @@ ENV FRIGATE_IMAGE_VARIANT=rk
|
||||
COPY docker/rockchip/COCO /COCO
|
||||
COPY docker/rockchip/conv2rknn.py /opt/conv2rknn.py
|
||||
|
||||
ADD https://github.com/MarcA711/rknn-toolkit2/releases/download/v2.3.2/librknnrt.so /usr/lib/
|
||||
ADD --chmod=644 https://github.com/MarcA711/rknn-toolkit2/releases/download/v2.3.2/librknnrt.so /usr/lib/
|
||||
|
||||
ADD --chmod=111 https://github.com/MarcA711/Rockchip-FFmpeg-Builds/releases/download/6.1-11/ffmpeg /usr/lib/ffmpeg/6.0/bin/
|
||||
ADD --chmod=111 https://github.com/MarcA711/Rockchip-FFmpeg-Builds/releases/download/6.1-11/ffprobe /usr/lib/ffmpeg/6.0/bin/
|
||||
ADD --chmod=111 https://github.com/MarcA711/Rockchip-FFmpeg-Builds/releases/download/7.1-1/ffmpeg /usr/lib/ffmpeg/7.0/bin/
|
||||
ADD --chmod=111 https://github.com/MarcA711/Rockchip-FFmpeg-Builds/releases/download/7.1-1/ffprobe /usr/lib/ffmpeg/7.0/bin/
|
||||
ADD --chmod=755 https://github.com/MarcA711/Rockchip-FFmpeg-Builds/releases/download/6.1-11/ffmpeg /usr/lib/ffmpeg/6.0/bin/
|
||||
ADD --chmod=755 https://github.com/MarcA711/Rockchip-FFmpeg-Builds/releases/download/6.1-11/ffprobe /usr/lib/ffmpeg/6.0/bin/
|
||||
ADD --chmod=755 https://github.com/MarcA711/Rockchip-FFmpeg-Builds/releases/download/7.1-1/ffmpeg /usr/lib/ffmpeg/7.0/bin/
|
||||
ADD --chmod=755 https://github.com/MarcA711/Rockchip-FFmpeg-Builds/releases/download/7.1-1/ffprobe /usr/lib/ffmpeg/7.0/bin/
|
||||
ENV DEFAULT_FFMPEG_VERSION="6.0"
|
||||
ENV INCLUDED_FFMPEG_VERSIONS="${DEFAULT_FFMPEG_VERSION}:${INCLUDED_FFMPEG_VERSIONS}"
|
||||
|
||||
@@ -26,4 +26,4 @@ ENV FRIGATE_IMAGE_VARIANT=synaptics
|
||||
|
||||
COPY --from=synap1680-wheels /rootfs/usr/local/lib/*.so /usr/lib
|
||||
|
||||
ADD https://raw.githubusercontent.com/synaptics-astra/synap-release/v1.5.0/models/dolphin/object_detection/coco/model/mobilenet224_full80/model.synap /synaptics/mobilenet.synap
|
||||
ADD --chmod=644 https://raw.githubusercontent.com/synaptics-astra/synap-release/v1.5.0/models/dolphin/object_detection/coco/model/mobilenet224_full80/model.synap /synaptics/mobilenet.synap
|
||||
|
||||
@@ -385,6 +385,8 @@ One Docker specific trap: published ports are inserted ahead of the host firewal
|
||||
|
||||
`telemetry.stats.network_bandwidth` uses nethogs, which needs `CAP_NET_ADMIN` and `CAP_NET_RAW` and therefore root. The stat is turned off automatically when Frigate isn't running as root, with one warning in the log. Use `FRIGATE_ROOT_SERVICES=frigate` (or `FRIGATE_RUN_AS_ROOT=true`) if you need it.
|
||||
|
||||
Rockchip NPU and GPU usage come from `/sys/kernel/debug`, which the kernel makes readable only by root. To keep these stats, bind the two load files into the container as shown in the [Rockchip installation steps](/frigate/installation#rockchip-platform). Without them, Frigate logs one warning and leaves the stats out.
|
||||
|
||||
go2rtc's ffmpeg processes no longer appear in Intel GPU stats. Frigate reads per-process GPU usage from `/proc/<pid>/fdinfo`, which the kernel won't let one user read for another user's processes, so anything go2rtc spawns is invisible to it. Overall GPU utilization is unaffected.
|
||||
|
||||
If you mount your own TLS certificate at `/etc/letsencrypt/live/frigate`, the private key has to be readable by the runtime user, which runs nginx. Frigate hands the key to that user at startup if the mount is writable; on a read-only mount, make the key readable by uid 1000 (or your `PUID`) yourself.
|
||||
|
||||
@@ -525,6 +525,27 @@ or add these options to your `docker run` command:
|
||||
--volume /sys/:/sys/:ro
|
||||
```
|
||||
|
||||
Frigate reads NPU and GPU (RGA) usage from `/sys/kernel/debug`, which the kernel makes readable only by root. Frigate's services don't run as root, so to show these stats on the System page, bind the two load files into the container. This works with or without `privileged: true`:
|
||||
|
||||
```yaml
|
||||
volumes:
|
||||
- /sys/kernel/debug/rknpu/load:/rockchip/rknpu/load:ro
|
||||
- /sys/kernel/debug/rkrga/load:/rockchip/rkrga/load:ro
|
||||
```
|
||||
|
||||
or for `docker run`:
|
||||
|
||||
```
|
||||
--volume /sys/kernel/debug/rknpu/load:/rockchip/rknpu/load:ro \
|
||||
--volume /sys/kernel/debug/rkrga/load:/rockchip/rkrga/load:ro
|
||||
```
|
||||
|
||||
These lines are optional, and everything else works without them. Only add a line for a file that exists on your host, because Docker won't start the container if one is missing. Check with:
|
||||
|
||||
```
|
||||
$ sudo ls /sys/kernel/debug/rknpu/load /sys/kernel/debug/rkrga/load
|
||||
```
|
||||
|
||||
#### Configuration
|
||||
|
||||
Next, you should configure [hardware object detection](/configuration/object_detectors#rockchip-platform) and [hardware video processing](/configuration/hardware_acceleration_video#rockchip-platform).
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user