From c597421438e8a40b378eab466e791a80a7bc0cb0 Mon Sep 17 00:00:00 2001 From: Who? <155328415+coddrago@users.noreply.github.com> Date: Tue, 24 Jun 2025 15:19:43 +0700 Subject: [PATCH] Update utils.py --- heroku/utils.py | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/heroku/utils.py b/heroku/utils.py index 431abfd..8309f06 100644 --- a/heroku/utils.py +++ b/heroku/utils.py @@ -1544,34 +1544,35 @@ def get_topic(message: Message) -> typing.Optional[int]: def get_ram_usage() -> float: - """Returns current process tree memory usage in MB""" + """Returns total memory usage of all processes in MB""" try: import psutil - current_process = psutil.Process(os.getpid()) - mem = current_process.memory_info()[0] / 2.0**20 - for child in current_process.children(recursive=True): - mem += child.memory_info()[0] / 2.0**20 + total_mem = 0 + for process in psutil.process_iter(['memory_info']): + try: + total_mem += process.memory_info()[0] / 2.0**20 + except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): + continue - return round(mem, 1) + return round(total_mem, 1) except Exception: return 0 - -def get_cpu_usage() -> float: - """Returns current process tree CPU usage in %""" +def get_cpu_usage(): try: import psutil - - current_process = psutil.Process(os.getpid()) - cpu = current_process.cpu_percent() - for child in current_process.children(recursive=True): - cpu += child.cpu_percent() - - return round(cpu, 1) + num_cores = psutil.cpu_count(logical=True) + cpu = 0.0 + for proc in psutil.process_iter(): + try: + cpu += proc.cpu_percent() + except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): + pass + normalized_cpu = cpu / num_cores + return f"{normalized_cpu:.2f}" except Exception: - return 0 - + return "0.00" init_ts = time.perf_counter()