78
78
_producer_cpu_usage = 0.0
79
79
_recorder_cpu_usage = 0.0
80
80
81
+ _producer_cpu_affinity = []
82
+ _recorder_cpu_affinity = []
83
+
81
84
82
85
def _parse_arguments (args = sys .argv [4 :]):
83
86
"""Parse benchmark and producers config file paths."""
@@ -141,9 +144,11 @@ def _launch_sequence(transport):
141
144
142
145
def _rosbag_proc_started (event , context ):
143
146
"""Register current rosbag2 PID so we can terminate it when producer exits."""
144
- global _rosbag_pid , _rosbag_process
147
+ global _rosbag_pid , _rosbag_process , _recorder_cpu_affinity
145
148
_rosbag_pid = event .pid
146
149
_rosbag_process = psutil .Process (_rosbag_pid )
150
+ if len (_recorder_cpu_affinity ) > 0 :
151
+ _rosbag_process .cpu_affinity (_recorder_cpu_affinity )
147
152
148
153
149
154
def _rosbag_ready_check (event ):
@@ -202,9 +207,11 @@ def _results_writer_exited(event, context):
202
207
def _producer_node_started (event , context ):
203
208
"""Log current benchmark progress on producer start."""
204
209
global _producer_idx , _producer_pid , _producer_process , _producer_cpu_usage
205
- global _cpu_usage_per_core
210
+ global _cpu_usage_per_core , _producer_cpu_affinity
206
211
_producer_pid = event .pid
207
212
_producer_process = psutil .Process (_producer_pid )
213
+ if len (_producer_cpu_affinity ) > 0 :
214
+ _producer_process .cpu_affinity (_producer_cpu_affinity )
208
215
_producer_process .cpu_percent ()
209
216
_producer_cpu_usage = 0.0
210
217
psutil .cpu_percent (None , True )
@@ -357,6 +364,7 @@ def _producer_node_exited(event, context):
357
364
def generate_launch_description ():
358
365
"""Generate launch description for ros2 launch system."""
359
366
global _producer_nodes , _bench_cfg_path , _producers_cfg_path
367
+ global _producer_cpu_affinity , _recorder_cpu_affinity
360
368
_bench_cfg_path , _producers_cfg_path = _parse_arguments ()
361
369
362
370
# Parse yaml config for benchmark
@@ -376,6 +384,17 @@ def generate_launch_description():
376
384
transport = not benchmark_params .get ('no_transport' )
377
385
preserve_bags = benchmark_params .get ('preserve_bags' )
378
386
387
+ # CPU affinity for producers and recorder
388
+ _producer_cpu_affinity = benchmark_params .get ('producers_cpu_affinity' , [])
389
+ _recorder_cpu_affinity = benchmark_params .get ('recorder_cpu_affinity' , [])
390
+ if not transport :
391
+ _producer_cpu_affinity = _recorder_cpu_affinity
392
+ print ('Warning! With no_transport = True, producer_cpu_affinity will be ignored' )
393
+
394
+ if len (_producer_cpu_affinity ) > 0 :
395
+ # Set CPU affinity for current process to avoid impact on the recorder
396
+ psutil .Process ().cpu_affinity (_producer_cpu_affinity )
397
+
379
398
# Producers options
380
399
producers_params = bench_cfg ['benchmark' ]['parameters' ]
381
400
@@ -577,7 +596,8 @@ def __generate_cross_section_parameter(i,
577
596
'sigkill_timeout' , default = 60 ),
578
597
sigterm_timeout = launch .substitutions .LaunchConfiguration (
579
598
'sigterm_timeout' , default = 60 ),
580
- cmd = ['ros2' , 'bag' , 'record' , '-e' , r'\/.*_benchmarking_node\/.*' ] + rosbag_args
599
+ cmd = ['ros2' , 'bag' , 'record' , '-e' ,
600
+ r'\/.*_benchmarking_node\/.*' ] + rosbag_args
581
601
)
582
602
583
603
# Fill up list with rosbag record process and result writers actions
0 commit comments