energymon package
Bindings for the native interface defined in energymon.h
- class energymon.energymon
Bases:
StructureBinding to
energymonstruct.- fexclusive
Structure/Union member
- ffinish
Structure/Union member
- finit
Structure/Union member
- finterval
Structure/Union member
- fprecision
Structure/Union member
- fread
Structure/Union member
- fsource
Structure/Union member
- state
Structure/Union member
Submodules
energymon.context module
Context management for using an energymon.
- class energymon.context.EnergyMon(lib: Union[str, CDLL] = 'energymon-default', func_get: str = 'energymon_get_default')
Bases:
objectA wrapper class and context manager for an
energymon.As a context manager, it is both reentrant and reusable.
Methods raise exceptions if the underlying native functions return an error.
- __init__(lib: Union[str, CDLL] = 'energymon-default', func_get: str = 'energymon_get_default')
Create a new instance.
- Parameters:
lib (Union[str, ctypes.CDLL]) – The library name (a
str) or the library handler (actypes.CDLL).func_get (str, optional) – The native “getter” function name to use.
- finish() None
Finish the underlying
energymon. If not already initialized, this is a no-op.Only call this method if not using the pattern:
with EnergyMon(...) as context:.
- get_interval_us() int
Get the refresh interval in microseconds.
- Returns:
The refresh interval in microseconds. This value should be greater than 0. If there is no minimum interval, returns 1.
- Return type:
- get_precision_uj() int
Get the best possible possible read precision in microjoules.
- Returns:
The best possible possible read precision in microjoules. If
0 < precision <= 1, returns 1. If the precision is unknown, returns 0.- Return type:
- get_source() str
Get a human-readable description of the energy monitoring source.
Initialization is not required to use this method.
- Returns:
A human-readable description of the energy monitoring source.
- Return type:
- get_uj() int
Get the total energy in microjoules.
- Returns:
The total energy in microjoules.
- Return type:
energymon.util module
Utilities for using an energymon.
See documentation in the native energymon.h header file for additional details.
- energymon.util.get_energymon(lib, func_get: str = 'energymon_get_default') energymon
Create an
energymonand “get” it (populate its function pointers), but do not initialize it.- Parameters:
lib (ctypes library) – An
energymonlibrary loaded byctypes(e.g., aCDLL).func_get (str) – The library function name used to populate the
energymonstruct.
- Returns:
An uninitialized
energymoninstance.- Return type:
- Raises:
AttributeError – If the getter function is not found.
OSError – If the underlying function returns an error. This is allowed by the API but should not occur under normal conditions.
Notes
Assumes a standard “get” function prototype:
int (get) (energymon*). All known instances use this prototype, but the energymon API doesn’t actually define this.
- energymon.util.get_interval_us(em: energymon) int
Get the refresh interval in microseconds.
- Parameters:
em (energymon) – The
energymonmust be initialized.- Returns:
The refresh interval in microseconds. This value should be greater than 0. If there is no minimum interval, returns 1.
- Return type:
- Raises:
OSError – If the underlying function returns an error. This is allowed by the API but should not occur under normal conditions.
- energymon.util.get_precision_uj(em: energymon) int
Get the best possible possible read precision in microjoules.
For implementations that read from power sensors, this is a function of the precision of the power readings and the refresh interval.
- Parameters:
em (energymon) – The
energymonmust be initialized.- Returns:
The best possible possible read precision in microjoules. If
0 < precision <= 1, returns 1. If the precision is unknown, returns 0.- Return type:
- Raises:
OSError – If the underlying function returns an error. This is allowed by the API but should not occur under normal conditions.
- energymon.util.get_source(em: energymon, maxlen=256, encoding='UTF-8', errors='strict') str
Get a human-readable description of the energy monitoring source.
- energymon.util.is_exclusive(em: energymon) bool
Get whether the implementation requires exclusive access.
In such cases it may be beneficial to run in a separate process and expose energy data over shared memory (or other means) so multiple applications can use the data source simultaneously.
- energymon.util.load_energymon_library(name: str = 'energymon-default')
Load an energymon library by name (no leading ‘lib’ or trailing extensions or version number).
Recommend setting
LD_LIBRARY_PATHon Linux/POSIX,DYLD_LIBRARY_PATHon OSX, andPATHon Windows.- Parameters:
name (str, optional) – The library name to search for.
- Returns:
The loaded shared library.
- Return type:
A
ctypeslibrary, e.g., aCDLL