objwatch.wrappers.cpu_memory_wrapper module¶
- class objwatch.wrappers.cpu_memory_wrapper.CPUMemoryWrapper[source]¶
Bases:
ABCWrapperCPUMemoryWrapper extends ABCWrapper to log memory statistics for the system’s CPU memory usage.
This class gathers memory statistics about the system’s memory usage using the psutil.virtual_memory() method. The following statistics are tracked, expressed in bytes:
- Main metrics:
total: total physical memory (exclusive swap).
available: the memory that can be given instantly to processes without the system going into swap. This is calculated by summing different memory metrics that vary depending on the platform. It is supposed to be used to monitor actual memory usage in a cross platform fashion.
percent: the percentage usage calculated as (total - available) / total * 100.
- Other metrics:
used: memory used, calculated differently depending on the platform and designed for informational purposes only. total - free does not necessarily match used.
free: memory not being used at all (zeroed) that is readily available; note that this doesn’t reflect the actual memory available (use available instead). total - used does not necessarily match free.
active (UNIX): memory currently in use or very recently used, and so it is in RAM.
inactive (UNIX): memory that is marked as not used.
buffers (Linux, BSD): cache for things like file system metadata.
cached (Linux, BSD): cache for various things.
shared (Linux, BSD): memory that may be simultaneously accessed by multiple processes.
slab (Linux): in-kernel data structures cache.
wired (BSD, macOS): memory that is marked to always stay in RAM. It is never moved to disk.
If you want to capture specific CPU memory metrics, you can configure the mem_types variable before starting the tracking. For example, to capture the total memory, available memory, and memory usage percentage, you can add the following code before instantiation of objwatch wrappers:
`python from objwatch.wrappers import CPUMemoryWrapper CPUMemoryWrapper.mem_types = ["total", "available", "percent"] `For the latest help and more detailed information, please refer to the official psutil documentation at: https://psutil.readthedocs.io/en/latest/index.html#psutil.virtual_memory
- __init__()[source]¶
Initialize the CPUMemoryWrapper with optional memory types to capture stats.
- Parameters:
mem_types (
List[str]) – A list of memory types to capture.
- _capture_memory() dict[source]¶
Capture the current system memory statistics.
- Returns:
A dictionary of memory stats from psutil.
- Return type:
- wrap_call(func_name: str, frame: FrameType) str[source]¶
Wrap the function call to log memory stats before the function is executed.
- wrap_return(func_name: str, result: Any) str[source]¶
Wrap the function return to log memory stats after the function is executed.
- _abc_impl = <_abc._abc_data object>¶