|Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.|
PrioritiesDescription: Setting thread or process scheduling priorities.
Keywords: ecl threads priorities
Tutorial Level: INTERMEDIATE
The ecl defines abstract levels to standardise the priority api across platforms. These are defined in the Priority enum which has values for:
1 using ecl::BackgroundPriority; 2 using ecl::LowPriority; 3 using ecl::NormalPriority; 4 using ecl::HighPriority; 5 using ecl::CriticalPriority; 6 // Posix real time scheduling priorities only (not available on windows). 7 using ecl::RealTimePriority4; 8 using ecl::RealTimePriority3; 9 using ecl::RealTimePriority2; 10 using ecl::RealTimePriority1; // highest priority 11
The threading classes typically allow configuration from their methods, however you can set/reset the priority for the current thread at any point by calling set_priority.
Calling the set_priority function in the main thread will configure your process' priority (this setting should get passed on to its children by default on most systems).
Higher Level Priorities
Typically linux systems do not come pre-configured for higher level priorities - typically anything higher than NormalPriority requires special permissions. If these are not set, the set_priority function will either throw an exception (in debug mode) or return false when not granted permission to do so.
To configure your system to do so, edit /etc/security/limits.conf. An example of editing this file for a user snorri to enable both realtime priorities to level 95 and the maximum niceness setting on a typical linux box:
Real Time Scheduling
This is just some notes (for curiosity) with reagards to priorities corresponding to RealTimePriority4 or above.
Posix systems will often have various real time priority scheduling capabilities. Note that this is not real time linux. Rather, threads working under this sort of scheduling are pulled from the usual userland scheduling manager where they are typically assigned a niceness value.
A single thread pulled into real time scheduling is guaranteed to get cpu time whenever it needs it. There is no weighting, or favours done for threads who've waited a long time. It's a guarantee. The rest of the threads on the system will then be spliced time when the real time thread is sleeping or idle.
Be careful with this! We've found it can greatly enhance control loops for motors to ensure that they work sufficiently in most systems without the need to go to real time linux. However you must take care that these threads don't start starving the remaining threads on your system.
In addition, a thread working at RealTimePriority2 is guaranteed to get cpu time over a thread working at RealTimePriority1. Two threads at the same real time priority level will go into a round robin.
You can check that your threads are running at realtime priority by running the following command:
> ps -C <process_name> -m -o pid,rtprio,ni,pri,vsize,rssize,pmem,pcpu,comm