ros-controls / realtime_tools

Contains a set of tools that can be used from a hard realtime thread, without breaking the realtime behavior.
https://control.ros.org
BSD 3-Clause "New" or "Revised" License
140 stars 76 forks source link

Add support to parse multiple cores for setting CPU affinity #208

Closed saikishor closed 3 days ago

saikishor commented 4 days ago

This PR adds a new method that helps to set multiple CPUs for affinity instead of only one and this will help in choosing couple of CPUs to run the controller_manager etc

codecov-commenter commented 4 days ago

Codecov Report

Attention: Patch coverage is 76.47059% with 4 lines in your changes missing coverage. Please review.

Project coverage is 72.72%. Comparing base (069df6d) to head (9ac21d4).

Files with missing lines Patch % Lines
src/realtime_helpers.cpp 76.47% 0 Missing and 4 partials :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #208 +/- ## ========================================== + Coverage 72.55% 72.72% +0.17% ========================================== Files 7 7 Lines 419 429 +10 Branches 68 71 +3 ========================================== + Hits 304 312 +8 Misses 73 73 - Partials 42 44 +2 ``` | [Flag](https://app.codecov.io/gh/ros-controls/realtime_tools/pull/208/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ros-controls) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/ros-controls/realtime_tools/pull/208/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ros-controls) | `72.72% <76.47%> (+0.17%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ros-controls#carryforward-flags-in-the-pull-request-comment) to find out more. | [Files with missing lines](https://app.codecov.io/gh/ros-controls/realtime_tools/pull/208?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ros-controls) | Coverage Δ | | |---|---|---| | [src/realtime\_helpers.cpp](https://app.codecov.io/gh/ros-controls/realtime_tools/pull/208?src=pr&el=tree&filepath=src%2Frealtime_helpers.cpp&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ros-controls#diff-c3JjL3JlYWx0aW1lX2hlbHBlcnMuY3Bw) | `39.79% <76.47%> (+4.56%)` | :arrow_up: |
firesurfer commented 3 days ago

I just saw the PR by chance and was interested as I authored the original one. I am wondering in which (realtime) scenario would you like to prefer a set of cores? From my understanding every time a thread is executed on the different core we suffer from

a) Context switch overhead b) Additional cache misses

Both brings in an uncertainty which we do not want in RT scenarios.

The only case I can imagine this could help is modern CPUs with multiple dies inside (e.g. most Ryzen processors). But the OS scheduler will already take care of trying not to move threads between cores on different dies per default.

saikishor commented 3 days ago

@firesurfer You can set it at the startup as we do in the ros2_control_node or in the configure of controllers and hardware components

firesurfer commented 3 days ago

@firesurfer You can set it at the startup as we do in the ros2_control_node or in the configure of controllers and hardware components

I am aware of that. But I just wanted to understand why it is an advantage in a RT scenario to use the group of cores instead of tying the thread to a single core.

saikishor commented 3 days ago

I am aware of that. But I just wanted to understand why it is an advantage in a RT scenario to use the group of cores instead of tying the thread to a single core.

By default, it uses mostly Core 0 as other. If you can use a set of performance cores, it would be better for the RT application, if your application needs more computational power than one core, then this definitely helps