13
13
import moptipy .version as ver
14
14
from moptipy .api import logging
15
15
from moptipy .utils .logger import InMemoryLogger , Logger , KeyValueLogSection , \
16
- CSV_SEPARATOR
16
+ CSV_SEPARATOR , KEY_VALUE_SEPARATOR , SCOPE_SEPARATOR
17
17
from moptipy .utils .path import Path
18
18
19
19
20
+ def __cpu_affinity (proc : Optional [psutil .Process ] = None ) -> Optional [str ]:
21
+ """
22
+ Get the CPU affinity.
23
+
24
+ :param proc: the process handle
25
+ :return: the CPU affinity string.
26
+ """
27
+ if proc is None :
28
+ proc = psutil .Process ()
29
+ if proc is None :
30
+ return None
31
+ cpua = proc .cpu_affinity ()
32
+ if cpua :
33
+ cpua = CSV_SEPARATOR .join (map (str , cpua ))
34
+ if len (cpua ) > 0 :
35
+ return cpua
36
+ return None
37
+
38
+
20
39
# noinspection PyBroadException
21
40
def __make_sys_info () -> str :
22
41
"""
@@ -131,10 +150,9 @@ def __get_mem_size() -> Optional[int]:
131
150
__v (k , logging .KEY_NODE_NAME , platform .node ())
132
151
proc = psutil .Process ()
133
152
__v (k , logging .KEY_PROCESS_ID , hex (proc .pid ))
134
- cpua = proc . cpu_affinity ( )
153
+ cpua = __cpu_affinity ( proc )
135
154
if cpua :
136
- __v (k , logging .KEY_CPU_AFFINITY ,
137
- CSV_SEPARATOR .join (map (str , cpua )))
155
+ __v (k , logging .KEY_CPU_AFFINITY , cpua )
138
156
del proc , cpua
139
157
140
158
# see https://stackoverflow.com/questions/166506/.
@@ -151,8 +169,11 @@ def __get_mem_size() -> Optional[int]:
151
169
152
170
with kv .scope (logging .SCOPE_VERSIONS ) as k :
153
171
__v (k , "moptipy" , ver .__version__ )
154
- for package in ["numpy" , "numba" , "matplotlib" ,
155
- "psutil" , "scikit-learn" ]:
172
+ for package in ["cycler" , "fonttools" , "joblib" , "kiwisolver" ,
173
+ "llvmlite" , "matplotlib" , "numba" , "numpy" ,
174
+ "packaging" , "Pillow" , "psutil" , "pyparsing" ,
175
+ "python-dateutil" , "scikit-learn" , "scipy" ,
176
+ "six" , "threadpoolctl" ]:
156
177
__v (k , package .replace ("-" , "" ),
157
178
ilm .version (package ).strip ())
158
179
@@ -214,7 +235,23 @@ def __make_mhz_str(tpl: Tuple[int, ...]) -> str:
214
235
215
236
def refresh_sys_info ():
216
237
"""Refresh the system information."""
217
- __SYS_INFO [0 ] = __make_sys_info ()
238
+ sys_info_str = __SYS_INFO [0 ]
239
+ start = f"\n { logging .SCOPE_SESSION } { SCOPE_SEPARATOR } " \
240
+ f"{ logging .KEY_CPU_AFFINITY } { KEY_VALUE_SEPARATOR } "
241
+ start_i = sys_info_str .find (start )
242
+ if start_i < 0 :
243
+ return # no affinity, don't need to update
244
+ start_i += len (start )
245
+ end_i = sys_info_str .find ("\n " , start_i )
246
+ if end_i <= start_i :
247
+ raise ValueError (f"Empty { logging .KEY_CPU_AFFINITY } ?" )
248
+ affinity = __cpu_affinity ()
249
+ if affinity is None :
250
+ raise ValueError (
251
+ f"first affinity query is { sys_info_str [start_i :end_i ]} ,"
252
+ f" but second one is None?" )
253
+ sys_info_str = f"{ sys_info_str [:start_i ]} { affinity } { sys_info_str [end_i :]} "
254
+ __SYS_INFO [0 ] = sys_info_str
218
255
219
256
220
257
def log_sys_info (logger : Logger ) -> None :
0 commit comments