sleepguard.monitors package

Submodules

sleepguard.monitors.base module

class sleepguard.monitors.base.UtilizationMonitor(poll_period: float, log_history_size: int, threshold: float, *args, **kwargs)[source]

Bases: Thread, ABC

A base class for all system utilization monitoring threads.

Parameters:
  • poll_period – The number of seconds between logging the system resource.

  • log_history_size – The number of values to store in the resource utilization logs. The average utilization will be calculated from these values.

  • threshold – The value at which sleep will be inhibited if the average utilization value exceeds it.

get_average_utilization() float[source]

Get the average utilization of the monitored resource over the threshold_period.

Returns:

get_current_logs() list[float][source]

Get the current logged values as a list.

Returns:

abstract get_instantaneous_value() float[source]

Return the instantaneous value of the monitored system resource.

Returns:

is_above_threshold() bool[source]

Returns true if the value from get_average_utilization is above the threshold.

Returns:

property is_running: bool

Checks if the thread is running (not stopped).

Returns:

property is_stopped: bool

Checks if the thread is stopped.

Returns:

property poll_period: float

How often the value of the system resource is polled.

Returns:

run()[source]

The main thread loop.

Returns:

stop()[source]

Terminate the execution of this monitor.

property threshold: float

The value at which sleep will be inhibited if the average utilization value exceeds it.

Returns:

sleepguard.monitors.cpu module

class sleepguard.monitors.cpu.CpuLoadMonitor(poll_period: float, log_history_size: int, threshold: float, *args, **kwargs)[source]

Bases: UtilizationMonitor

Keeps track of the average CPU load over the given threshold_period.

Parameters:
  • poll_period – The number of seconds between logging the system resource.

  • log_history_size – The number of values to store in the resource utilization logs. The average utilization will be calculated from these values.

  • threshold – The value at which sleep will be inhibited if the average utilization value exceeds it.

get_average_utilization() float[source]

Get the load average over the threshold_period.

Returns:

get_instantaneous_value() float[source]

Returns the current load average reported by the OS.

Returns:

run()[source]

The main thread loop.

Returns:

class sleepguard.monitors.cpu.CpuUtilizationMonitor(poll_period: float, log_history_size: int, threshold: float, *args, **kwargs)[source]

Bases: UtilizationMonitor

Keeps track of the average CPU percent utilization over the given threshold_period.

Parameters:
  • poll_period – The number of seconds between logging the system resource.

  • log_history_size – The number of values to store in the resource utilization logs. The average utilization will be calculated from these values.

  • threshold – The value at which sleep will be inhibited if the average utilization value exceeds it.

get_instantaneous_value() float[source]

Return the instantaneous value of the monitored system resource.

Returns:

sleepguard.monitors.factory module

class sleepguard.monitors.factory.UtilizationMonitorFactory[source]

Bases: object

A factory class for creating UtilizationMonitor instances.

static from_criteria(criteria: MonitorCriteria, raise_on_error: bool = False) UtilizationMonitor | None[source]

Create an instance of a UtilizationMonitor from the given criteria.

Parameters:
  • criteria – The MonitorCriteria to used to create the instance.

  • raise_on_error – If True, an exception will be raised if the instance cannot be created, for example an unsupported GPU is detected when using the GpuUtilizationMonitor. Otherwise, None is returned.

Returns:

sleepguard.monitors.gpu module

class sleepguard.monitors.gpu.GpuUtilizationMonitor(poll_period: float, log_history_size: int, threshold: float, *args, **kwargs)[source]

Bases: UtilizationMonitor

Keeps track of the average GPU percent utilization over the given threshold_period.

Parameters:
  • poll_period – The number of seconds between logging the system resource.

  • log_history_size – The number of values to store in the resource utilization logs. The average utilization will be calculated from these values.

  • threshold – The value at which sleep will be inhibited if the average utilization value exceeds it.

static get_device_types() list[Literal['amd', 'nvidia']][source]

Get the list of available GPU types installed on the system.

Returns:

get_instantaneous_value() float[source]

Returns the max utilization over all the detected GPU devices.

Returns:

sleepguard.monitors.net module

class sleepguard.monitors.net.NetUtilizationMonitor(poll_period: float, log_history_size: int, threshold: float, *args, **kwargs)[source]

Bases: UtilizationMonitor

Keeps track of the average total number of bytes sent and received per second over the given threshold_period.

Parameters:
  • poll_period – The number of seconds between logging the system resource.

  • log_history_size – The number of values to store in the resource utilization logs. The average utilization will be calculated from these values.

  • threshold – The value at which sleep will be inhibited if the average utilization value exceeds it.

get_instantaneous_value() float[source]

Return the instantaneous value of the monitored system resource.

Returns:

run()[source]

The main thread loop.

Returns:

Module contents

Utility methods and classes for monitoring system utilization.

class sleepguard.monitors.MonitorCriteria(*args, **kwargs)[source]

Bases: UserDict

The criteria used for monitoring a given system resource and determining if it should prevent the system from sleeping.

Note

This is implemented as a dictionary because simple-parsing does not support lists of dataclasses.

as_kwargs() dict[str, int | float][source]

Return the criteria as kwargs suitable for constructing a UtilizationMonitor instance.

Returns:

property log_history_size: int
property poll_period: float
property system_resource: Literal['cpu_load', 'cpu_util', 'gpu_util', 'net_util']
property threshold: float
sleepguard.monitors.SystemResource

The valid types of system resources to monitor.

alias of Literal[‘cpu_load’, ‘cpu_util’, ‘gpu_util’, ‘net_util’]

sleepguard.monitors.debug(fn: Callable)[source]

A decorator that logs the given member function’s return value. ‘ :param fn: :return: