ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

How to bind a node to a specific cpu core?

asked 2015-02-09 00:25:39 -0500

ZiyangLI gravatar image

Dear all,

I am tyring to run a node with a specific cpu core but have no idea how to do that.

What's more, I also want to specify the cpu core of callback functions. I know I can use asynchronous spinner to control the number of threads, but it seems there is no way to manually set which core the thread uses.

Look forward to any help!


edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2015-02-09 01:37:48 -0500

gvdhoorn gravatar image

updated 2015-02-09 01:46:19 -0500

Without being able to get access to the underlying threads, I think it will be difficult (impossible) to do for individual threads, but for entire processes this should be doable using taskset and a launch-prefix (taskset is from the util-linux package on Debian/Ubuntu).

Something like launch-prefix="taskset -c 1" added to a node element in a launch file should work (I haven't tested it though). That binds the node to cpu 2.

Note that according to man/1/taskset, your user needs to have CAP_SYS_NICE.

Note also that the taskset approach is not ROS specific. The launch-prefix is ROS specific (see roslaunch/XML/node - Attributes).

Final note: the fact that you've bound task X to cpu Y does not mean that other tasks cannot be run on cpu Y, obviously.

edit flag offensive delete link more


taskset 1 (without the -c) worked for me in Ubuntu 18.04

lucasw gravatar image lucasw  ( 2020-01-02 17:44:54 -0500 )edit

The -c option is slightly more specific and supports "list format" (which is convenient when working with a (highly) multi-core CPU):

-c, --cpu-list          display and specify cpus in list format

Without -c, taskset will also work, but doesn't support lists.

It's the same on all versions of Ubuntu afaik.

gvdhoorn gravatar image gvdhoorn  ( 2020-01-03 06:15:47 -0500 )edit

I tested both the list and mask cpu setting as a launch-prefix in melodic and they work

lucasw gravatar image lucasw  ( 2020-01-03 07:18:14 -0500 )edit

answered 2015-02-09 02:22:50 -0500

See also the RT-PREEMPT Wiki on the Cpuset management utility which would be helpful if you're really serious about explicitly specifying what runs on which core. It should be noted that a plain Linux Kernel and plain ROS are probably not the right toosl for the job if you're looking for short deadlines and hard real-time guarantees, however.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-02-09 00:25:39 -0500

Seen: 4,896 times

Last updated: Feb 09 '15