Skip to content

Linux Thread Scheduling

Charlie FG edited this page Dec 26, 2015 · 17 revisions

Linux uses various policies and priorities for scheduling the threads, allowing the scheduler to decide where to allocate CPU time. StrangeIO defaults to the SCHED_OTHER policy, which is the normal policy and processes don't require any particular privileges to run at that level.

When StrangeIO is configured to run at very low latencies the usual policy can cause switching to the thread to be too slow, resulting in broken audio pipes and popping. The solution is to allow StrangeIO to run specific threads in real time policies - guaranteed high priority thread switching instead of unpredictably being queued after other tasks at the schedulers discretion. The other result is the CPU time allocation will sky-rocket, giving higher through-put at the expense of other processes.

Configuring real-time policy

You have two options:

  1. Enable a user to change the scheduling policy
  2. Create a group that can change the scheduling policy

Create Group (Optional)

If you go for option 2 -- first create a group and then add the users who will have privileges to that group:

groupadd strangeio
usermod -aG strangeio user

Set Policy

The user or group needs to have the privilege to run real-time threads. You can give a user or group the ability by adding a configuration file or by editing the limits.conf file. (Fedora, for example, has config files in /etc/security/limits.d/):

vim /etc/sucurity/limits.d/96-strangeio.conf
or
vim /etc/security/limits.conf

You probably have to be elevated by sudo.

User Option

Add the following line:

user - rtprio 20

substituting user with the username. This says that the hard and soft real-time thread priority limit is 20 (out of 99).

Group Option

Add the following line:

@strangeio - rtprio 20

substituting strangeio with the group name. This says that the hard and soft real-time thread priority limit is 20 (out of 99).

Checking Policy

Then check that the policy limits are in place (you may need to restart the system) you can use the following command:

ulimit -r
20

Which tells us the real-time priority limit is 20.

Configuring StrangeIO

Finally we can configure StrangeIO. In the system.linux category, we can set sched_policy to SCHED_FIFO and the sched_priority to something like 3. The system category looks something like this:

"linux": {
    "sched_policy" : "SCHED_FIFO",
    "sched_priority" : 3
}

Which should enable critical threads to be scheduled with a real-time policy SCHED_FIFO. A bare-bones rack will print:

StrangeIO [rack]: Set schedule policy: 1/3

If it correctly set the policy, otherwise it will give an error about the operation not being permitted.

Notes

A warning about using real-time scheduling policy:

If there is code in the thread that ends up in a busy loop the system will choke, as the loop is given all the CPU time in the world. However if there is code that ends up in a busy loop - the catastrophic level of resource usage is the least of your worries.

Clone this wiki locally