Changing scheduling parameters in Linux

Process scheduling within Linux is done by the kernel, following different aspects. Usually it's goal is to share all resources fairly among all running processes.

Sometimes, there's a need to tell the kernel explicitly to prior some process, bind some process to a special CPU and so on.

Changing process priorities is commonly known:

nice -n 10 make

runs the program make with a priority of 10 (-20 meaning most favorable and 19 least favorable scheduling). If you want to change a priority of a running program, renice is your choice.

schedtool

Nowadays multiprocessor machines get more and more in common. So it's sometimes desirable to bind a process to one or more CPUs. This is done by schedtool, which is not part of common distros. You can install it on Gentoo by

emerge -va schedtool

schedtool allows you to view, and change the process CPU affinity. Try

schedtool PID

where PID is the process ID. For example, process 6702 of my system shows:

PID  6702: PRIO   0, POLICY N: SCHED_NORMAL  , NICE   0, AFFINITY 0x3

meaning it's CPU affinity mask is 0x03. Each bit of the affinity mask corresponds to a CPU in the system and a bit value of 1 meaning this CPU is allowed to use. So, 0x03 = 0b00000011 means that CPU0 and CPU1 are allowed to use. You can change the CPU affinity by

schedtool -a AFFINITY_MASK PID

where PID is the process ID and AFFINITY_MASK the affinity mask (must be in hexadecimal) or a list of (decimal) comma separated CPU numbers. Be careful not to use affinity 0x00 especially on viral processes. 🙂

schedtool also allows the change of schedule policy for a single job. This is useful e.g. for long-running non-interactive jobs. You can enable the SCHED_BATCH policy for them by

schedtool -B PID

where PID is the process ID. Notice that you need a least kernel 2.6.16 for SCHED_BATCH support. There are different schedule policies for Real Time or Idle applications. See man schedtool for detailed information.

taskset

Another program for setting process's CPU affinity is taskset. It's included in util-linux-ng, which is part of most common distros. taskset is only capable of changing/viewing CPU affinity, not priorities or scheduling policies.

To view a process CPU affinity try

taskset -p PID

where PID is the process ID. For the same process as above my systems shows:

pid 6702's current affinity mask: 3

You can change CPU affinity easily by

taskset -p AFFINITY_MASK PID

where PID is the process ID and AFFINITY_MASK the affinity mask. If you wish the use a command seperated CPU list you have to

taskset -c -p CPU_LIST PID

where PID is the process ID and CPU_LIST the CPU list like 1,3,4-6.

Unlike schedtool taskset is capable of starting programs with a specific CPU affinity mask by

taskset AFFINITY_MASK COMMAND

where AFFINITY_MASK is the affinity mask and COMMAND and command to execute. Seed man taskset for detailed information.

One thought on “Changing scheduling parameters in Linux

  1. Very nice, I now know the purposes of nice, renice, schedtool and taskset.

    I'm looking for a way to 'tame' kswap0, and think renice may do it. Some people are using schedtool for this purpose, but renice is part of my default installation (Linux Mint Rosa) and seems to do the same, so far as Priority Setting is concerned.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.