System utils
PeakMemoryProfiler
¶
A context manager that monitors and tracks the peak memory usage of a process (and optionally its children) over a period of time. The memory usage can be reported in various units (bytes, MB, or GB).
Example:
Class Attributes:
Attributes:
| Name | Type | Description |
|---|---|---|
pid |
The PID of the process being monitored. Defaults to the current process. |
|
interval |
Time interval (in seconds) between memory checks. Defaults to 0.1. |
|
include_children |
Whether memory usage from child processes is included. Defaults to True. |
|
unit |
The unit used to report memory usage (either 'bytes', 'MB', or 'GB'). Defaults to 'MB'. |
|
max_memory |
The peak memory usage observed during the monitoring period. |
|
monitoring_thread |
Thread used for monitoring memory usage. |
|
_stop_monitoring |
Event used to signal when to stop monitoring. |
Source code in magenpy/utils/system_utils.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
__enter__()
¶
Starts monitoring memory usage when entering the context block.
Returns:
| Type | Description |
|---|---|
|
Returns the instance of PeakMemoryProfiler, so that we can access peak memory later. |
Source code in magenpy/utils/system_utils.py
__exit__(exc_type, exc_value, traceback)
¶
Stops the memory monitoring when exiting the context block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
The exception type if an exception was raised in the block. |
required | |
exc_value
|
The exception instance if an exception was raised. |
required | |
traceback
|
The traceback object if an exception was raised. |
required |
Source code in magenpy/utils/system_utils.py
__init__(pid=None, interval=0.1, include_children=True, unit='MB')
¶
Initializes the PeakMemoryProfiler instance with the provided parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pid
|
The PID of the process to monitor. Defaults to None (current process). |
None
|
|
interval
|
The interval (in seconds) between memory checks. Defaults to 0.1. |
0.1
|
|
include_children
|
Whether to include memory usage from child processes. Defaults to True. |
True
|
|
unit
|
The unit in which to report memory usage. Options are 'bytes', 'MB', or 'GB'. Defaults to 'MB'. |
'MB'
|
Source code in magenpy/utils/system_utils.py
get_curr_memory()
¶
Get the current memory usage of the monitored process and its children.
Returns:
| Type | Description |
|---|---|
float
|
The current memory usage in the specified unit (bytes, MB, or GB). |
Source code in magenpy/utils/system_utils.py
get_peak_memory()
¶
Get the peak memory usage observed during the monitoring period.
Returns:
| Type | Description |
|---|---|
float
|
The peak memory usage in the specified unit (bytes, MB, or GB). |
available_cpu()
¶
Returns:
| Type | Description |
|---|---|
|
The number of available cores on the system minus 1. |
delete_temp_files(prefix)
¶
Delete temporary files with the given prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
A string with the prefix of the temporary files to delete. |
required |
Source code in magenpy/utils/system_utils.py
get_filenames(path, extension=None)
¶
Obtain valid and full path names given the provided path or prefix and extensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
A string with the path prefix or full path. |
required | |
extension
|
The extension for the class of files to search for. |
None
|
Returns:
| Type | Description |
|---|---|
|
A list of strings with the full paths of the files/folders. |
Source code in magenpy/utils/system_utils.py
get_memory_usage()
¶
Returns:
| Type | Description |
|---|---|
|
The current memory usage of the running process in Mega Bytes (MB) |
Source code in magenpy/utils/system_utils.py
glob_hf_path(path)
¶
Get the list of files/folders in the provided Hugging Face path. This works with wildcards.
Source code in magenpy/utils/system_utils.py
glob_s3_path(path)
¶
Get the list of files/folders in the provided AWS S3 path. This works with wildcards.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
A string with the S3 path to list files/folders from. |
required |
Returns:
| Type | Description |
|---|---|
|
A list of strings with the full paths of the files/folders. |
Source code in magenpy/utils/system_utils.py
is_cmd_tool(name)
¶
Check whether name is on PATH and marked as executable.
From: https://stackoverflow.com/a/34177358
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
A string with the name of the command-line tool. |
required |
Returns:
| Type | Description |
|---|---|
|
True if the command-line tool is available, False otherwise. |
Source code in magenpy/utils/system_utils.py
is_path_writable(path)
¶
Check whether the user has write-access to the provided path.
This function supports checking for nested directories (i.e.,
we iterate upwards until finding a parent directory that currently
exists, and we check the write-access of that directory).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
A string with the path to check. |
required |
Returns:
| Type | Description |
|---|---|
|
True if the path is writable, False otherwise. |
Source code in magenpy/utils/system_utils.py
makedir(dirs)
¶
Create directories on the filesystem, recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dirs
|
A string or list of strings with the paths to create. |
required |
Source code in magenpy/utils/system_utils.py
run_shell_script(cmd)
¶
Run the shell script given the command prompt in cmd.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
A string with the shell command to run. |
required |
Returns:
| Type | Description |
|---|---|
|
The result of the shell command. |
Source code in magenpy/utils/system_utils.py
setup_logger(loggers=None, modules=None, log_file=None, log_format=None, log_level='WARNING', clear_file=False)
¶
Set up the logger with the provided configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loggers
|
A list of logger instances to apply the logger configurations to. |
None
|
|
modules
|
A list of modules to apply the logger configurations to. This allows for setting up different logger configs for different modules. |
None
|
|
log_file
|
A string with the path to the log file. |
None
|
|
log_format
|
A string with the format of the log messages. |
None
|
|
log_level
|
A string with the logging level. |
'WARNING'
|
|
clear_file
|
A boolean flag to clear the log file before writing. |
False
|
Source code in magenpy/utils/system_utils.py
valid_url(path)
¶
Check whether the provided path is a valid URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
A string with the URL to check. |
required |
Returns:
| Type | Description |
|---|---|
|
True if the URL is valid, False otherwise. |