Skip to content

Commit

Permalink
add ffmpeg bandwidth stats (blakeblackshear#6492)
Browse files Browse the repository at this point in the history
* add ffmpeg bandwidth stats

* add ffmpeg bandwidth stats

* Change column name

Co-authored-by: Nicolas Mowen <[email protected]>

* fix lint formatting

---------

Co-authored-by: Korneliusz Jarzębski <[email protected]>
Co-authored-by: Nicolas Mowen <[email protected]>
  • Loading branch information
3 people committed May 18, 2023
1 parent b568a29 commit 17e8a46
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docker/install_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ apt-get -qq install --no-install-recommends -y \
unzip locales tzdata libxml2 xz-utils \
python3-pip \
curl \
jq
jq \
nethogs

mkdir -p -m 600 /root/.gnupg

Expand Down
11 changes: 10 additions & 1 deletion frigate/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from frigate.types import StatsTrackingTypes, CameraMetricsTypes
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.util import get_cpu_stats, get_bandwidth_stats
from frigate.object_detection import ObjectDetectProcess

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -101,6 +101,7 @@ async def run_tasks() -> None:
[
asyncio.create_task(set_gpu_stats(config, stats, hwaccel_errors)),
asyncio.create_task(set_cpu_stats(stats)),
asyncio.create_task(set_bandwidth_stats(stats)),
]
)

Expand All @@ -118,6 +119,14 @@ async def set_cpu_stats(all_stats: dict[str, Any]) -> None:
all_stats["cpu_usages"] = cpu_stats


async def set_bandwidth_stats(all_stats: dict[str, Any]) -> None:
"""Set bandwidth from nethogs."""
bandwidth_stats = get_bandwidth_stats()

if bandwidth_stats:
all_stats["bandwidth_usages"] = bandwidth_stats


async def set_gpu_stats(
config: FrigateConfig, all_stats: dict[str, Any], hwaccel_errors: list[str]
) -> None:
Expand Down
29 changes: 29 additions & 0 deletions frigate/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,35 @@ def get_cpu_stats() -> dict[str, dict]:
return usages


def get_bandwidth_stats() -> dict[str, dict]:
"""Get bandwidth usages for each ffmpeg process id"""
usages = {}
top_command = ["nethogs", "-t", "-v0", "-c5", "-d1"]

p = sp.run(
top_command,
encoding="ascii",
capture_output=True,
)

if p.returncode != 0:
return usages
else:
lines = p.stdout.split("\n")
for line in lines:
stats = list(filter(lambda a: a != "", line.strip().split("\t")))
try:
if re.search("^ffmpeg/([0-9]+)/", stats[0]):
process = stats[0].split("/")
usages[process[1]] = {
"bandwidth": round(float(stats[2]), 1),
}
except:
continue

return usages


def get_amd_gpu_stats() -> dict[str, str]:
"""Get stats using radeontop."""
radeontop_command = ["radeontop", "-d", "-", "-l", "1"]
Expand Down
5 changes: 5 additions & 0 deletions web/src/routes/System.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default function System() {
const {
cpu_usages,
gpu_usages,
bandwidth_usages,
detectors,
service = {},
detection_fps: _,
Expand Down Expand Up @@ -343,6 +344,7 @@ export default function System() {
<Th>FPS</Th>
<Th>CPU %</Th>
<Th>Memory %</Th>
<Th>Network Bandwidth</Th>
</Tr>
</Thead>
<Tbody>
Expand All @@ -360,13 +362,15 @@ export default function System() {
<Td>{cameras[camera]['camera_fps'] || '- '}</Td>
<Td>{cpu_usages[cameras[camera]['ffmpeg_pid']]?.['cpu'] || '- '}%</Td>
<Td>{cpu_usages[cameras[camera]['ffmpeg_pid']]?.['mem'] || '- '}%</Td>
<Td>{bandwidth_usages[cameras[camera]['ffmpeg_pid']]?.['bandwidth'] || '- '}KB/s</Td>
</Tr>
<Tr key="capture" index="1">
<Td>Capture</Td>
<Td>{cameras[camera]['capture_pid'] || '- '}</Td>
<Td>{cameras[camera]['process_fps'] || '- '}</Td>
<Td>{cpu_usages[cameras[camera]['capture_pid']]?.['cpu'] || '- '}%</Td>
<Td>{cpu_usages[cameras[camera]['capture_pid']]?.['mem'] || '- '}%</Td>
<Td>-</Td>
</Tr>
<Tr key="detect" index="2">
<Td>Detect</Td>
Expand All @@ -387,6 +391,7 @@ export default function System() {

<Td>{cpu_usages[cameras[camera]['pid']]?.['cpu'] || '- '}%</Td>
<Td>{cpu_usages[cameras[camera]['pid']]?.['mem'] || '- '}%</Td>
<Td>-</Td>
</Tr>
</Tbody>
</Table>
Expand Down

0 comments on commit 17e8a46

Please sign in to comment.