From 70b9e969153478be239bb3160cfc48e1bc901ce4 Mon Sep 17 00:00:00 2001 From: James Leece Date: Tue, 10 Jan 2023 23:40:10 +0000 Subject: [PATCH 1/3] Fix mem usage reporting when using docker limits --- frigate/util.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/frigate/util.py b/frigate/util.py index 6fe00b5932..cb111aa833 100755 --- a/frigate/util.py +++ b/frigate/util.py @@ -737,6 +737,58 @@ def escape_special_characters(path: str) -> str: # path does not have user:pass return path +def get_cgroups_version() -> str: + """Determine what version of cgroups is enabled""" + + stat_command = ["stat", "-fc", "%T", "/sys/fs/cgroup"] + + p = sp.run( + stat_command, + encoding="ascii", + capture_output=True, + ) + + if p.returncode == 0: + value: str = p.stdout.strip().lower() + + if value == "cgroup2fs": + return "cgroup2" + elif value == "tmpfs": + return "cgroup" + else: + logger.debug(f"Could not determine cgroups version: unhandled filesystem {value}") + else: + logger.debug(f"Could not determine cgroups version: {p.stderr}") + + return "unknown" + + +def get_docker_memlimit_bytes() -> int: + """Get mem limit in bytes set in docker if present. Returns -1 if no limit detected""" + + # check running a supported cgroups version + if (get_cgroups_version() == "cgroup2"): + + memlimit_command = ["cat", "/sys/fs/cgroup/memory.max"] + + p = sp.run( + memlimit_command, + encoding="ascii", + capture_output=True, + ) + + if p.returncode == 0: + value: str = p.stdout.strip() + + if value.isnumeric(): + return int(value) + elif value.lower() == "max": + return -1 + else: + logger.debug(f"Unable to get docker memlimit: {p.stderr}") + + return -1 + def get_cpu_stats() -> dict[str, dict]: """Get cpu usages for each process id""" @@ -744,6 +796,8 @@ def get_cpu_stats() -> dict[str, dict]: # -n=2 runs to ensure extraneous values are not included top_command = ["top", "-b", "-n", "2"] + docker_memlimit = get_docker_memlimit_bytes() / 1024 + p = sp.run( top_command, encoding="ascii", @@ -759,9 +813,18 @@ def get_cpu_stats() -> dict[str, dict]: for line in lines: stats = list(filter(lambda a: a != "", line.strip().split(" "))) try: + + if docker_memlimit > 0: + memRes = int(stats[5]) + memPct = str( + round((float(memRes) / float(docker_memlimit)) * 100, 1) + ) + else: + memPct = stats[9] + usages[stats[0]] = { "cpu": stats[8], - "mem": stats[9], + "mem": memPct, } except: continue From 2c02459bd60b95b69664563845436487a55c7274 Mon Sep 17 00:00:00 2001 From: leccelecce <24962424+leccelecce@users.noreply.github.com> Date: Wed, 11 Jan 2023 22:56:10 +0000 Subject: [PATCH 2/3] format code --- frigate/util.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/frigate/util.py b/frigate/util.py index cb111aa833..ae20c73fe2 100755 --- a/frigate/util.py +++ b/frigate/util.py @@ -737,9 +737,10 @@ def escape_special_characters(path: str) -> str: # path does not have user:pass return path + def get_cgroups_version() -> str: """Determine what version of cgroups is enabled""" - + stat_command = ["stat", "-fc", "%T", "/sys/fs/cgroup"] p = sp.run( @@ -756,10 +757,12 @@ def get_cgroups_version() -> str: elif value == "tmpfs": return "cgroup" else: - logger.debug(f"Could not determine cgroups version: unhandled filesystem {value}") + logger.debug( + f"Could not determine cgroups version: unhandled filesystem {value}" + ) else: logger.debug(f"Could not determine cgroups version: {p.stderr}") - + return "unknown" @@ -767,7 +770,7 @@ def get_docker_memlimit_bytes() -> int: """Get mem limit in bytes set in docker if present. Returns -1 if no limit detected""" # check running a supported cgroups version - if (get_cgroups_version() == "cgroup2"): + if get_cgroups_version() == "cgroup2": memlimit_command = ["cat", "/sys/fs/cgroup/memory.max"] @@ -786,7 +789,7 @@ def get_docker_memlimit_bytes() -> int: return -1 else: logger.debug(f"Unable to get docker memlimit: {p.stderr}") - + return -1 From 0f10831a0dca2f30d587bb748d2ba9380fb7ed18 Mon Sep 17 00:00:00 2001 From: leccelecce <24962424+leccelecce@users.noreply.github.com> Date: Wed, 11 Jan 2023 23:21:51 +0000 Subject: [PATCH 3/3] wip --- frigate/util.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/frigate/util.py b/frigate/util.py index ae20c73fe2..6de537a0dd 100755 --- a/frigate/util.py +++ b/frigate/util.py @@ -818,16 +818,16 @@ def get_cpu_stats() -> dict[str, dict]: try: if docker_memlimit > 0: - memRes = int(stats[5]) - memPct = str( - round((float(memRes) / float(docker_memlimit)) * 100, 1) + mem_res = int(stats[5]) + mem_pct = str( + round((float(mem_res) / float(docker_memlimit)) * 100, 1) ) else: - memPct = stats[9] + mem_pct = stats[9] usages[stats[0]] = { "cpu": stats[8], - "mem": memPct, + "mem": mem_pct, } except: continue