This commit is contained in:
Sergey Krashevich 2023-04-28 03:02:46 +03:00
parent 2494d46eb6
commit 1ff72b82c5
No known key found for this signature in database
GPG Key ID: 625171324E7D3856

View File

@ -745,18 +745,27 @@ def get_cgroups_version() -> str:
cgroup_path = "/sys/fs/cgroup" cgroup_path = "/sys/fs/cgroup"
try: if not os.path.ismount(cgroup_path):
stat_info = os.stat(cgroup_path) logger.debug(f"{cgroup_path} is not a mount point.")
value = os.statvfs(cgroup_path).f_type return "unknown"
if value == os.fsencode("cgroup2fs"): try:
with open("/proc/mounts", "r") as f:
mounts = f.readlines()
for mount in mounts:
mount_info = mount.split()
if mount_info[1] == cgroup_path:
fs_type = mount_info[2]
if fs_type == "cgroup2fs" or fs_type == "cgroup2":
return "cgroup2" return "cgroup2"
elif value == os.fsencode("tmpfs"): elif fs_type == "tmpfs":
return "cgroup" return "cgroup"
else: else:
logger.debug( logger.debug(
f"Could not determine cgroups version: unhandled filesystem {value}" f"Could not determine cgroups version: unhandled filesystem {fs_type}"
) )
break
except Exception as e: except Exception as e:
logger.debug(f"Could not determine cgroups version: {e}") logger.debug(f"Could not determine cgroups version: {e}")
@ -785,32 +794,32 @@ def get_docker_memlimit_bytes() -> int:
def get_cpu_stats() -> dict[str, dict]: def get_cpu_stats() -> dict[str, dict]:
"""Get cpu usages for each process id.""" """Get cpu usages for each process id"""
usages = {} usages = {}
docker_memlimit = get_docker_memlimit_bytes() / 1024 docker_memlimit = get_docker_memlimit_bytes() / 1024
total_mem = os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES") / 1024
for pid in os.listdir("/proc"): for pid in os.listdir("/proc"):
if pid.isdigit(): if pid.isdigit():
try: try:
with open(f"/proc/{pid}/stat", "r") as f: with open(f"/proc/{pid}/stat", "r") as f:
stat_info = f.read().split() stats = f.readline().split()
utime = int(stats[13])
stime = int(stats[14])
cpu_usage = round((utime + stime) / os.sysconf("SC_CLK_TCK"))
with open("/proc/meminfo", "r") as f: with open(f"/proc/{pid}/statm", "r") as f:
mem_info = f.readlines() mem_stats = f.readline().split()
total_mem = int(mem_info[0].split()[1]) mem_res = int(mem_stats[1]) * os.sysconf("SC_PAGE_SIZE") / 1024
mem_res = int(stat_info[23]) / 1024
if docker_memlimit > 0: if docker_memlimit > 0:
mem_pct = round((float(mem_res) / float(docker_memlimit)) * 100, 1) mem_pct = round((mem_res / docker_memlimit) * 100, 1)
else: else:
mem_pct = round((float(mem_res) / float(total_mem)) * 100, 1) mem_pct = round((mem_res / total_mem) * 100, 1)
cpu_total_time = sum(int(x) for x in stat_info[13:17])
cpu_usage = round(cpu_total_time / os.sysconf("SC_CLK_TCK"), 1)
usages[pid] = { usages[pid] = {
"cpu": cpu_usage, "cpu": str(round(cpu_usage, 2)),
"mem": mem_pct, "mem": f"{mem_pct}",
} }
except: except:
continue continue