@@ -113,6 +113,7 @@ def cpu_mem_benchmark(cpu_threads: Optional[int] = 5,
113113 cpu_work_per_thread = int (cpu_work / cpu_threads )
114114
115115 cpu_procs = []
116+ mem_procs = []
116117 cpu_prog = [
117118 f"{ this_dir .joinpath ('cpu-benchmark' )} " , f"{ cpu_work_per_thread } " ]
118119 mem_prog = ["stress-ng" , "--vm" , f"{ mem_threads } " ,
@@ -125,11 +126,11 @@ def cpu_mem_benchmark(cpu_threads: Optional[int] = 5,
125126 cpu_procs .append (cpu_proc )
126127
127128 if mem_threads > 0 :
128- mem_proc = subprocess .Popen (mem_prog )
129+ mem_proc = subprocess .Popen (mem_prog , preexec_fn = os . setsid )
129130 if core :
130131 os .sched_setaffinity (mem_proc .pid , {core })
131-
132- return cpu_procs
132+ mem_procs . append ( mem_proc )
133+ return cpu_procs , mem_procs
133134
134135
135136def io_read_benchmark_user_input_data_size (inputs ,
@@ -294,29 +295,34 @@ def main():
294295 print (f"[WfBench] { args .name } acquired core { core } " )
295296
296297 mem_threads = int (10 - 10 * args .percent_cpu )
297- cpu_procs = cpu_mem_benchmark (cpu_threads = int (10 * args .percent_cpu ),
298- mem_threads = mem_threads ,
299- cpu_work = sys .maxsize if args .time else int (args .cpu_work ),
300- core = core ,
301- total_mem = args .mem )
302-
298+ cpu_procs , mem_procs = cpu_mem_benchmark (cpu_threads = int (10 * args .percent_cpu ),
299+ mem_threads = mem_threads ,
300+ cpu_work = sys .maxsize if args .time else int (args .cpu_work ),
301+ core = core ,
302+ total_mem = args .mem )
303+
303304 procs .extend (cpu_procs )
304-
305305 if args .time :
306306 time .sleep (int (args .time ))
307307 for proc in procs :
308308 os .killpg (os .getpgid (proc .pid ), signal .SIGTERM )
309309 else :
310310 for proc in procs :
311- # if isinstance(proc, multiprocessing.Process):
312- # proc.join()
313311 if isinstance (proc , subprocess .Popen ):
314312 proc .wait ()
315313 if io_proc is not None and io_proc .is_alive ():
316314 io_proc .terminate ()
317315 io_proc .join ()
318- mem_kill = subprocess .Popen (["killall" , "stress-ng" ])
319- mem_kill .wait ()
316+
317+
318+ for mem_proc in mem_procs :
319+ try :
320+ os .kill (mem_proc .pid , signal .SIGKILL ) # Force kill if SIGTERM fails
321+ except subprocess .TimeoutExpired :
322+ print ("Memory process did not terminate; force-killing." )
323+ # As a fallback, use pkill if any remaining instances are stuck
324+ subprocess .Popen (["pkill" , "-f" , "stress-ng" ]).wait ()
325+
320326 print ("[WfBench] Completed CPU and Memory Benchmarks!\n " )
321327
322328 # NOTE: If you would like to run only IO add time.sleep(2)
0 commit comments