PSUtilProfiler#

class caput.util.profiler.PSUtilProfiler(use_profiler: bool = True, label: str = '', logger: logging.Logger | None = None, comm: mpi4py.MPI.Comm | None = None, path: os.PathLike | None = None)[source]#

Bases: psutil.Process

A context manager that profiles using psutil.

To access the profiling data the context manager object must be created and bound to a variable before the with statement.

Dumps results into csv file, one for each rank.

>>> p = PSUtilProfiler(label="task-label")
>>> with p:
...     print("do some task in here")
do some task in here
>>> print(p.usage)
{...}

start and stop can be used to use the PSUtilProfiler outside of cotnext management.

>>> p = PSUtilProfiler(label="task-label")
>>> p.start()
>>> print("do some task in here")
do some task in here
>>> p.stop()
>>> print(p.usage)
{...}
Parameters:
use_profilerbool

Whether to run the profiler or not.

labelstr, optional

Default description of what is being profiled.

loggerlogging.Logger | None, optional

If no None, the values of the IO done counters will be logged at INFO level.

commMPI.Comm | None, optional

An optional MPI communicator. This is only used for labelling the output files.

pathos.PathLike | None, optional

The optional directory path under which to write the profile csvs. If not set use the current directory.

property cpu_count: int[source]#

Number of cores available to this process.

property usage: dict[str, float][source]#

The memory and cpu usage within the block.

Returns:
usagedict

Usage dictionary with the following keys:

cpu_timesdict

dict version of psutil.cpu_times. Process CPU times since start was called in seconds.

cpu_percentfloat

Process CPU utilization since start was called as a percentage. Can be >100 if multiple threads run on different cores. See PSUtil.cpu_count for available cores.

disk_iodict

dict version of psutil.io_counters (on Linux) or psutil.disk_io_counters (on MacOS). Difference since start was called.

memorystr

Difference of memory in use by this process since start was called. If negative, less memory is in use now.

used_memorystr

Current used memory at the time of the task’s end.

available_memorystr

Current memory available to the system at the time of the task’s end.

Methods#

monitor(→ None)

Track peak memory.

start(→ None)

Start profiling.

stop(→ None)

Stop profiling.