cgroup_monitor package#

Submodules#

cgroup_monitor.v1_manager module#

class cgroup_monitor.v1_manager.CGroupManager(cgroup_name, cgroup_base_path='/sys/fs/cgroup', helper_script=None)[source]#

Bases: object

add_process(pid, sudo=False)[source]#

Add a process to the cgroup.

Parameters: - pid (int): Process ID to add to the cgroup. - sudo (bool): Whether to use sudo to run the command. Default is True.

Returns: - returncode (int): Return whether the command was successful.

create_cgroup()[source]#

If non-existant, create cgroup and take ownership. Note, this command requires sudo privileges.

Parameters: - None

Returns: - returncode (int): Return whether the command was successful.

delete_cgroup(sudo=False)[source]#

Delete the cgroup directories.

Parameters: - sudo (bool): Whether to use sudo to run the command. Default is True.

Returns: - returncode (int): Return whether the command was successful.

set_cpu_limit(quota, period=100000, sudo=False)[source]#

Set the CPU limits.

Parameters: - quota (int): CPU quota in microseconds. - period (int): CPU period in microseconds. Default is 100000. - sudo (bool): Whether to use sudo to run the command. Default is True.

Returns: - returncode (int): Return whether the command was successful.

set_memory_limit(limit, sudo=False)[source]#

Set the memory limit in bytes.

Parameters: - limit (int): Memory limit in bytes. - sudo (bool): Whether to use sudo to run the command. Default is True.

Returns: - returncode (int): Return whether the command was successful.

set_memory_swap_limit(limit, sudo=False)[source]#

Set the memory+swap limit in bytes. Note: This sets maximum amount for the sum of memory and swap usage.

Parameters: - limit (int): Memory+swap limit in bytes. - sudo (bool): Whether to use sudo to run the command.

Returns: - returncode (int): Return whether the command was successful.

cgroup_monitor.v1_monitor module#

class cgroup_monitor.v1_monitor.CGroupMonitor(cgroup_name='', cgroup_base_path='/sys/fs/cgroup')[source]#

Bases: object

get_cpu_limit()[source]#

Get the CPU limit in microseconds.

Parameters: - None

Returns: - quota (int): CPU quota in microseconds. - period (int): CPU period in microseconds.

get_cpu_usage_us()[source]#

Get the cumulative CPU usage in microseconds.

Parameters: - None

Returns: - usage_usec (int): CPU usage in microseconds.

get_last_n_stats(n=1, info_level=0)[source]#

Get the last n stats recorded.

Parameters: - n (int): Number of stats to retrieve. Default is 1. - info_level (int): Level of information to return. Default is 0.

  • 0: Return average and max usage stats.

  • 1: Return detailed stats including all recorded values.

Returns: - stats (dict): Dictionary containing average and max usage stats.

get_memory_limit()[source]#

Get the memory limit in bytes.

Parameters: - None

Returns: - memory_limit (int): Memory limit in bytes.

get_memory_usage()[source]#

Get the current memory usage in bytes.

Parameters: - None

Returns: - memory_usage (int): Memory usage in bytes.

get_swap_limit()[source]#

Get the memory+swap limit in bytes.

Parameters: - None

Returns: - memory_swap_limit (int): Memory+swap limit in bytes.

start_monitor(interval=1)[source]#

Start monitoring CPU and memory usage.

Parameters: - interval (int): Monitoring interval in seconds. Default is 1.

Uses time.sleep() and can be float value for higher precision.

Returns: - None

stop_monitor(info_level=0)[source]#

Stop monitoring and return average and max usage stats. ``` :returns:

dict = {

“average_cpu_usage_percent”: float, “max_cpu_usage_percent”: float, “average_memory_usage_gib”: float, “max_memory_usage_gib”: float, “average_memory_usage_percent”: float, “max_memory_usage_percent”: float, “monitoring_duration_s”: float

}

```

Parameters: - info_level (int): Level of information to return. Default is 0.

  • 0: Return average and max usage stats.

  • 1: Return detailed stats including all recorded values.

Returns: - stats (dict): Dictionary containing average and max usage stats.

cgroup_monitor.v2_manager module#

class cgroup_monitor.v2_manager.CGroupManager(cgroup_name, cgroup_base_path='/sys/fs/cgroup', helper_script=None)[source]#

Bases: object

add_process(pid, sudo=False)[source]#

Add a process to the cgroup.

Parameters: - pid (int): Process ID to add to the cgroup. - sudo (bool): Whether to use sudo to run the command.

Returns: - returncode (int): Return whether the command was successful.

create_cgroup()[source]#

If non-existant, create cgroup and take ownership. Note, this command requires sudo privileges.

Parameters: - None

Returns: - returncode (int): Return whether the command was successful.

delete_cgroup(sudo=False)[source]#

Delete the cgroup.

Parameters: - sudo (bool): Whether to use sudo to run the command.

Returns: - returncode (int): Return whether the command was successful.

set_cpu_limit(quota, period=100000, sudo=False)[source]#

Set the CPU limits. Sets the CPU using quota*period.

Parameters: - quota (int): CPU quota in number of cores. - period (int): CPU period in microseconds. Default is 100000. - sudo (bool): Whether to use sudo to run the command. Default is False.

Returns: - returncode (int): Return whether the command was successful.

set_memory_limit(limit, sudo=False)[source]#

Set the memory limit in bytes.

Parameters: - limit (int): Memory limit in bytes. - sudo (bool): Whether to use sudo to run the command.

Returns: - returncode (int): Return whether the command was successful.

set_memory_swap_limit(limit, sudo=False)[source]#

Set the memory+swap limit in bytes. Note: This sets maximum amount for the sum of memory and swap usage.

Parameters: - limit (int): Memory+swap limit in bytes. - sudo (bool): Whether to use sudo to run the command.

Returns: - returncode (int): Return whether the command was successful.

cgroup_monitor.v2_monitor module#

class cgroup_monitor.v2_monitor.CGroupMonitor(cgroup_name='', cgroup_base_path='/sys/fs/cgroup')[source]#

Bases: object

get_cpu_limit()[source]#

Get the CPU quota and period.

Parameters: - None

Returns: - quota (int or None): CPU quota. - period (int): CPU period in microseconds.

get_cpu_usage_us()[source]#

Get the cumulative CPU usage in microseconds.

Parameters: - None

Returns: - usage_usec (int): CPU usage in microseconds.

get_last_n_stats(n=1, info_level=0)[source]#

Get the last n stats recorded.

Parameters: - n (int): Number of stats to retrieve. Default is 1. - info_level (int): Level of information to return. Default is 0.

  • 0: Return average and max usage stats.

  • 1: Return detailed stats including all recorded values.

Returns: - stats (dict): Dictionary containing average and max usage stats.

get_memory_limit()[source]#

Get the memory limit in bytes.

Parameters: - None

Returns: - memory_limit (int): Memory limit in bytes.

get_memory_usage()[source]#

Get the current memory usage in bytes.

Parameters: - None

Returns: - memory_usage (int): Memory usage in bytes.

get_swap_limit()[source]#

Get the memory+swap limit in bytes.

Parameters: - None

Returns: - memory_swap_limit (int): Memory+Swap limit in bytes.

start_monitor(interval=1.0)[source]#

Start monitoring CPU and memory usage.

Parameters: - interval (float): Monitoring interval in seconds. Default is 1 second.

Uses time.sleep() and can be float value for higher precision.

Returns: - None

stop_monitor(info_level=0)[source]#

Stop monitoring and return average and max usage stats. ``` :returns:

dict = {

“average_cpu_usage_percent”: float, “max_cpu_usage_percent”: float, “average_memory_usage_gib”: float, “max_memory_usage_gib”: float, “average_memory_usage_percent”: float, “max_memory_usage_percent”: float, “monitoring_duration_s”: float

}

```

Parameters: - info_level (int): Level of information to return. Default is 0.

  • 0: Return average and max usage stats.

  • 1: Return detailed stats including all recorded values.

Returns: - stats (dict): Dictionary containing average and max usage stats.

Module contents#

class cgroup_monitor.CGroupManager(cgroup_name, cgroup_base_path='/sys/fs/cgroup', helper_script=None)[source]#

Bases: object

add_process(pid, sudo=False)[source]#

Add a process to the cgroup.

Parameters: - pid (int): Process ID to add to the cgroup. - sudo (bool): Whether to use sudo to run the command.

Returns: - returncode (int): Return whether the command was successful.

create_cgroup()[source]#

If non-existant, create cgroup and take ownership. Note, this command requires sudo privileges.

Parameters: - None

Returns: - returncode (int): Return whether the command was successful.

delete_cgroup(sudo=False)[source]#

Delete the cgroup.

Parameters: - sudo (bool): Whether to use sudo to run the command.

Returns: - returncode (int): Return whether the command was successful.

set_cpu_limit(quota, period=100000, sudo=False)[source]#

Set the CPU limits. Sets the CPU using quota*period.

Parameters: - quota (int): CPU quota in number of cores. - period (int): CPU period in microseconds. Default is 100000. - sudo (bool): Whether to use sudo to run the command. Default is False.

Returns: - returncode (int): Return whether the command was successful.

set_memory_limit(limit, sudo=False)[source]#

Set the memory limit in bytes.

Parameters: - limit (int): Memory limit in bytes. - sudo (bool): Whether to use sudo to run the command.

Returns: - returncode (int): Return whether the command was successful.

set_memory_swap_limit(limit, sudo=False)[source]#

Set the memory+swap limit in bytes. Note: This sets maximum amount for the sum of memory and swap usage.

Parameters: - limit (int): Memory+swap limit in bytes. - sudo (bool): Whether to use sudo to run the command.

Returns: - returncode (int): Return whether the command was successful.

class cgroup_monitor.CGroupMonitor(cgroup_name='', cgroup_base_path='/sys/fs/cgroup')[source]#

Bases: object

get_cpu_limit()[source]#

Get the CPU quota and period.

Parameters: - None

Returns: - quota (int or None): CPU quota. - period (int): CPU period in microseconds.

get_cpu_usage_us()[source]#

Get the cumulative CPU usage in microseconds.

Parameters: - None

Returns: - usage_usec (int): CPU usage in microseconds.

get_last_n_stats(n=1, info_level=0)[source]#

Get the last n stats recorded.

Parameters: - n (int): Number of stats to retrieve. Default is 1. - info_level (int): Level of information to return. Default is 0.

  • 0: Return average and max usage stats.

  • 1: Return detailed stats including all recorded values.

Returns: - stats (dict): Dictionary containing average and max usage stats.

get_memory_limit()[source]#

Get the memory limit in bytes.

Parameters: - None

Returns: - memory_limit (int): Memory limit in bytes.

get_memory_usage()[source]#

Get the current memory usage in bytes.

Parameters: - None

Returns: - memory_usage (int): Memory usage in bytes.

get_swap_limit()[source]#

Get the memory+swap limit in bytes.

Parameters: - None

Returns: - memory_swap_limit (int): Memory+Swap limit in bytes.

start_monitor(interval=1.0)[source]#

Start monitoring CPU and memory usage.

Parameters: - interval (float): Monitoring interval in seconds. Default is 1 second.

Uses time.sleep() and can be float value for higher precision.

Returns: - None

stop_monitor(info_level=0)[source]#

Stop monitoring and return average and max usage stats. ``` :returns:

dict = {

“average_cpu_usage_percent”: float, “max_cpu_usage_percent”: float, “average_memory_usage_gib”: float, “max_memory_usage_gib”: float, “average_memory_usage_percent”: float, “max_memory_usage_percent”: float, “monitoring_duration_s”: float

}

```

Parameters: - info_level (int): Level of information to return. Default is 0.

  • 0: Return average and max usage stats.

  • 1: Return detailed stats including all recorded values.

Returns: - stats (dict): Dictionary containing average and max usage stats.