glasswall.multiprocessing.memory_usage
1from typing import Optional 2 3import psutil 4 5 6def bytes_to_gigibytes(num_bytes: int) -> float: 7 return num_bytes / (1024.0 ** 3) 8 9 10def get_total_memory_usage_in_gib(pid: Optional[int]) -> float: 11 """ Calculate the total memory usage of a process and its child processes in gigibytes (GiB). 12 13 Args: 14 pid (int, optional): The process ID for which memory usage is to be calculated. If None, returns 0.0. 15 16 Returns: 17 float: The total memory usage of the process and its children in GiB. If pid is None, returns 0.0. 18 """ 19 if pid is None: 20 return 0.0 21 22 try: 23 psutil_process = psutil.Process(pid) 24 total_memory = psutil_process.memory_info().rss 25 26 # Include memory usage of child processes 27 for child in psutil_process.children(recursive=True): 28 total_memory += child.memory_info().rss 29 30 return bytes_to_gigibytes(total_memory) 31 32 except psutil.NoSuchProcess: 33 # Handle the case where the process does not exist 34 return 0.0 35 36 37def get_available_memory_bytes() -> int: 38 """ Returns the available memory in bytes. """ 39 return psutil.virtual_memory().available 40 41 42def get_available_memory_gib() -> float: 43 """ Returns the available memory in gigabytes. """ 44 return bytes_to_gigibytes(get_available_memory_bytes())
def
bytes_to_gigibytes(num_bytes: int) -> float:
def
get_total_memory_usage_in_gib(pid: Optional[int]) -> float:
13def get_total_memory_usage_in_gib(pid: Optional[int]) -> float: 14 """ Calculate the total memory usage of a process and its child processes in gigibytes (GiB). 15 16 Args: 17 pid (int, optional): The process ID for which memory usage is to be calculated. If None, returns 0.0. 18 19 Returns: 20 float: The total memory usage of the process and its children in GiB. If pid is None, returns 0.0. 21 """ 22 if pid is None: 23 return 0.0 24 25 try: 26 psutil_process = psutil.Process(pid) 27 total_memory = psutil_process.memory_info().rss 28 29 # Include memory usage of child processes 30 for child in psutil_process.children(recursive=True): 31 total_memory += child.memory_info().rss 32 33 return bytes_to_gigibytes(total_memory) 34 35 except psutil.NoSuchProcess: 36 # Handle the case where the process does not exist 37 return 0.0
Calculate the total memory usage of a process and its child processes in gigibytes (GiB).
Args: pid (int, optional): The process ID for which memory usage is to be calculated. If None, returns 0.0.
Returns: float: The total memory usage of the process and its children in GiB. If pid is None, returns 0.0.
def
get_available_memory_bytes() -> int:
40def get_available_memory_bytes() -> int: 41 """ Returns the available memory in bytes. """ 42 return psutil.virtual_memory().available
Returns the available memory in bytes.
def
get_available_memory_gib() -> float:
45def get_available_memory_gib() -> float: 46 """ Returns the available memory in gigabytes. """ 47 return bytes_to_gigibytes(get_available_memory_bytes())
Returns the available memory in gigabytes.