@@ -107,6 +107,9 @@ def run(self):
107
107
"""
108
108
start_time = time .time ()
109
109
self ._extraneous_process_ids .add (self .pid )
110
+ get_memory_maps = lambda process : process .memory_maps (grouped = False )
111
+ get_rss = lambda process : process .memory_info ().rss
112
+ get_n_threads = lambda process : process .num_threads ()
110
113
get_cpu_percent = lambda process : process .cpu_percent ()
111
114
try :
112
115
main_process = psutil .Process (self ._main_process_id )
@@ -124,15 +127,19 @@ def run(self):
124
127
try :
125
128
descendent_processes = [
126
129
process for process in main_process .children (recursive = True ) if process .pid not in self ._extraneous_process_ids ]
127
- combined_processes = [main_process ] + descendent_processes
128
130
# The first call to cpu_percent returns a meaningless value of 0.0 and should be ignored.
129
131
# And it's recommended to wait a specified amount of time after the first call to cpu_percent.
130
132
# See https://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_percent
131
- self ._map_processes (processes = combined_processes , map_func = get_cpu_percent )
133
+ self ._map_processes (processes = [ main_process ] + descendent_processes , map_func = get_cpu_percent )
132
134
# Get the maximum RAM usage.
133
- self ._update_ram (rss_values = self ._resource_usage .max_ram .main , processes = [main_process ])
134
- self ._update_ram (rss_values = self ._resource_usage .max_ram .descendents , processes = descendent_processes )
135
- self ._update_ram (rss_values = self ._resource_usage .max_ram .combined , processes = combined_processes )
135
+ ram_map_func = get_memory_maps if self ._is_linux else get_rss
136
+ main_ram = self ._map_processes ([main_process ], map_func = ram_map_func )
137
+ descendents_ram = self ._map_processes (descendent_processes , map_func = ram_map_func )
138
+ combined_ram = main_ram + descendents_ram
139
+ kwarg = 'memory_maps_list' if self ._is_linux else 'rss_list'
140
+ self ._update_ram (rss_values = self ._resource_usage .max_ram .main , ** {kwarg : main_ram })
141
+ self ._update_ram (rss_values = self ._resource_usage .max_ram .descendents , ** {kwarg : descendents_ram })
142
+ self ._update_ram (rss_values = self ._resource_usage .max_ram .combined , ** {kwarg : combined_ram })
136
143
self ._resource_usage .max_ram .system = max (
137
144
self ._resource_usage .max_ram .system , psutil .virtual_memory ().used * self ._ram_coefficient )
138
145
# Get the maximum GPU RAM usage if available.
@@ -156,26 +163,28 @@ def run(self):
156
163
n_hardware_units = self ._resource_usage .gpu_utilization .n_expected_gpus )
157
164
158
165
# Get the mean and maximum CPU usages.
159
- self ._update_n_threads (processes = [main_process ], attr = 'main' )
160
- self ._update_n_threads (processes = descendent_processes , attr = 'descendents' )
161
- self ._update_n_threads (processes = combined_processes , attr = 'combined' )
166
+ main_n_threads = self ._map_processes ([main_process ], map_func = get_n_threads )
167
+ descendent_n_threads = self ._map_processes (descendent_processes , map_func = get_n_threads )
168
+ self ._update_n_threads (n_threads_list = main_n_threads , attr = 'main' )
169
+ self ._update_n_threads (n_threads_list = descendent_n_threads , attr = 'descendents' )
170
+ self ._update_n_threads (n_threads_list = main_n_threads + descendent_n_threads , attr = 'combined' )
162
171
# noinspection PyTypeChecker
163
172
system_core_percentages : list [float ] = psutil .cpu_percent (percpu = True )
164
173
cpu_utilization = self ._resource_usage .cpu_utilization
165
174
self ._update_processing_unit_utilization (
166
175
current_percentages = system_core_percentages , processing_unit_percentages = cpu_utilization .system ,
167
176
percent_key = 'cpu_system' , n_hardware_units = cpu_utilization .system_core_count )
168
177
time .sleep (_TrackingProcess ._CPU_PERCENT_INTERVAL )
169
- main_percentage = main_process . cpu_percent ( )
178
+ main_percentage = self . _map_processes ([ main_process ], map_func = get_cpu_percent )
170
179
descendent_percentages = self ._map_processes (processes = descendent_processes , map_func = get_cpu_percent )
171
180
self ._update_processing_unit_utilization (
172
- current_percentages = [ main_percentage ] , processing_unit_percentages = cpu_utilization .main , percent_key = 'cpu_main' ,
181
+ current_percentages = main_percentage , processing_unit_percentages = cpu_utilization .main , percent_key = 'cpu_main' ,
173
182
n_hardware_units = cpu_utilization .n_expected_cores )
174
183
self ._update_processing_unit_utilization (
175
184
current_percentages = descendent_percentages , processing_unit_percentages = cpu_utilization .descendents ,
176
185
percent_key = 'cpu_descendents' , n_hardware_units = cpu_utilization .n_expected_cores )
177
186
self ._update_processing_unit_utilization (
178
- current_percentages = [ main_percentage ] + descendent_percentages , processing_unit_percentages = cpu_utilization .combined ,
187
+ current_percentages = main_percentage + descendent_percentages , processing_unit_percentages = cpu_utilization .combined ,
179
188
percent_key = 'cpu_combined' , n_hardware_units = cpu_utilization .n_expected_cores )
180
189
# Update compute time.
181
190
self ._resource_usage .compute_time .time = (time .time () - start_time ) * self ._time_coefficient
@@ -197,10 +206,8 @@ def _map_processes(self, processes: list[psutil.Process], map_func: typ.Callable
197
206
self ._log_warning ('Attempted to obtain usage information of a process that no longer exists.' ) # pragma: nocover
198
207
return mapped_list
199
208
200
- def _update_ram (self , rss_values : RSSValues , processes : list [psutil .Process ]):
201
- if self ._is_linux :
202
- memory_maps_list : list [list ] = self ._map_processes (
203
- processes , map_func = lambda process : process .memory_maps (grouped = False ))
209
+ def _update_ram (self , rss_values : RSSValues , memory_maps_list : list [list ] | None = None , rss_list : list [int ] | None = None ):
210
+ if memory_maps_list is not None :
204
211
private_rss = 0
205
212
path_to_shared_rss = dict [str , float ]()
206
213
for memory_maps in memory_maps_list :
@@ -218,7 +225,7 @@ def _update_ram(self, rss_values: RSSValues, processes: list[psutil.Process]):
218
225
rss_values .shared_rss = max (rss_values .shared_rss , shared_rss )
219
226
total_rss = private_rss + shared_rss
220
227
else :
221
- total_rss = sum (self . _map_processes ( processes , map_func = lambda process : process . memory_info (). rss ) )
228
+ total_rss = sum (rss_list )
222
229
total_rss *= self ._ram_coefficient
223
230
rss_values .total_rss = max (rss_values .total_rss , total_rss )
224
231
@@ -254,8 +261,7 @@ def _update_processing_unit_utilization(
254
261
max_percent : float = getattr (processing_unit_percentages , f'max_{ percent_type } _percent' )
255
262
setattr (processing_unit_percentages , f'max_{ percent_type } _percent' , max (max_percent , percent ))
256
263
257
- def _update_n_threads (self , processes : list [psutil .Process ], attr : str ):
258
- n_threads_list = self ._map_processes (processes , map_func = lambda process : process .num_threads ())
264
+ def _update_n_threads (self , n_threads_list : list [int ], attr : str ):
259
265
n_threads = sum (n_threads_list )
260
266
attr = f'{ attr } _n_threads'
261
267
max_n_threads = getattr (self ._resource_usage .cpu_utilization , attr )
0 commit comments