Chapter 27. Tuning scheduling policy
In Red Hat Enterprise Linux, the smallest unit of process execution is called a thread. The system scheduler determines which processor runs a thread, and for how long the thread runs. However, because the scheduler’s primary concern is to keep the system busy, it may not schedule threads optimally for application’sategories of scheduling policies performance.
For example, if an application on a NUMA system is running on Node A when a processor on Node B becomes available. To keep the processor on Node B busy, the scheduler moves one of the application’s threads to Node B. However, the application thread still requires access to memory on Node A. But, this memory will take longer to access because the thread is now running on Node B and Node A memory is no longer local to the thread. Thus, it may take longer for the thread to finish running on Node B than it would have taken to wait for a processor on Node A to become available, and then to execute the thread on the original node with local memory access.
27.1. Categories of scheduling policies Copy linkLink copied to clipboard!
Performance-sensitive applications often benefit from the designer or administrator determining where threads are run. The Linux scheduler implements a number of scheduling policies that determine where and for how long a thread runs. The following are the two major categories of scheduling policies:
- Normal policies
- Normal threads are used for tasks of normal priority.
- Realtime policies
- Real-time policies are used for time-sensitive tasks that must be completed without interruptions. Real-time threads are not subject to time slicing. This means the thread runs until they block, exit, voluntarily yield, or are preempted by a higher priority thread.
The lowest priority realtime thread is scheduled before any thread with a normal policy. For more information, see Static priority scheduling with SCHED_FIFO, Round robin priority scheduling with SCHED_RR, and the sched(7)
, sched_setaffinity(2)
, sched_getaffinity(2)
, sched_setscheduler(2)
, and sched_getscheduler(2)
man pages on your system.
27.2. Static priority scheduling with SCHED_FIFO Copy linkLink copied to clipboard!
The SCHED_FIFO
, also called static priority scheduling, is a real-time policy that defines a fixed priority for each thread. This policy allows administrators to improve event response time and reduce latency. Do not execute this policy for an extended period of time for time sensitive tasks. When SCHED_FIFO
is in use, the scheduler scans the list of all the SCHED_FIFO
threads in order of priority and schedules the highest priority thread that is ready to run. The priority level of a SCHED_FIFO
thread can be any integer from 1 to 99, where 99 is treated as the highest priority. Start with a lower number and increase priority only when you identify latency issues.
Because real-time threads are not subject to time slicing, avoid setting a priority of 99. This keeps your process at the same priority level as migration and watchdog threads; if your thread goes into a computational loop and these threads are blocked, they will not be able to run. Systems with a single processor will eventually hang in this situation.
Administrators can limit SCHED_FIFO
bandwidth to prevent realtime application programmers from initiating realtime tasks that monopolize the processor. The following are some of the parameters used in this policy:
/proc/sys/kernel/sched_rt_period_us
- This parameter defines the time period, in microseconds, that is considered to be one hundred percent of the processor bandwidth. The default value is 1000000 μs or 1 second.
/proc/sys/kernel/sched_rt_runtime_us
- This parameter defines the time period, in microseconds, that is devoted to running real-time threads. The default value is 950000 μs or 0.95 seconds.
27.3. Round robin priority scheduling with SCHED_RR Copy linkLink copied to clipboard!
The SCHED_RR
is a round-robin variant of the SCHED_FIFO
. This policy is useful when multiple threads need to run at the same priority level. Like SCHED_FIFO
, SCHED_RR
is a realtime policy that defines a fixed priority for each thread. The scheduler scans the list of all SCHED_RR
threads in order of priority and schedules the highest priority thread that is ready to run. However, unlike SCHED_FIFO
, threads that have the same priority are scheduled in a round-robin style within a certain time slice. You can set the value of this time slice in milliseconds with the sched_rr_timeslice_ms
kernel parameter in the /proc/sys/kernel/sched_rr_timeslice_ms
file. The lowest value is 1 millisecond.
27.4. Normal scheduling with SCHED_OTHER Copy linkLink copied to clipboard!
The SCHED_OTHER
is the default scheduling policy. This policy uses the Completely Fair Scheduler (CFS) to allow fair processor access to all threads scheduled with this policy. This policy is most useful when there are a large number of threads or when data throughput is a priority, as it allows more efficient scheduling of threads over time. When this policy is in use, the scheduler creates a dynamic priority list based partly on the niceness value of each process thread. Administrators can change the niceness value of a process, but cannot change the scheduler’s dynamic priority list directly.
27.5. Setting scheduler policies Copy linkLink copied to clipboard!
You can check and adjust scheduler policies and priorities by using the chrt
command line tool. It can start new processes with the desired properties or change the properties of a running process. It can also be used for setting the policy at runtime.
Procedure
View the process ID (PID) of the active processes:
ps
# ps
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Use the
--pid
or-p
option with theps
command to view the details of the particular PID. Check the scheduling policy, PID, and priority of a particular process:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the scheduling policy of a process, for example:
To set the process with PID 1000 to
SCHED_FIFO
, with a priority of 50:chrt -f -p 50 1000
# chrt -f -p 50 1000
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To set the process with PID 1000 to
SCHED_OTHER
, with a priority of 0:chrt -o -p 0 1000
# chrt -o -p 0 1000
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To set the process with PID 1000 to
SCHED_RR
, with a priority of 10:chrt -r -p 10 1000
# chrt -r -p 10 1000
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To start a new application with a particular policy and priority, specify the name of the application:
chrt -f 36 /bin/my-app
# chrt -f 36 /bin/my-app
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
27.6. Policy options for the chrt command Copy linkLink copied to clipboard!
You can view and set the scheduling policy of processes by using the chrt command. The following table describes the appropriate policy options, which can be used to set the scheduling policy of a process.
Short option | Long option | Description |
---|---|---|
|
|
Set schedule to |
|
|
Set schedule to |
|
|
Set schedule to |
27.7. Changing the priority of services during the boot process Copy linkLink copied to clipboard!
You can set up real-time priorities for services launched during the boot process by using the systemd
service. The unit configuration directives are used to change the priority of a service during the boot process. The boot process priority can be changed by using the following directives in the service section:
-
CPUSchedulingPolicy=
: Sets the CPU scheduling policy for executed processes. It is used to set other, fifo, and rr policies. -
CPUSchedulingPriority=
: Sets the CPU scheduling priority for executed processes. The available priority range depends on the selected CPU scheduling policy. For real-time scheduling policies, an integer between 1 (lowest priority) and 99 (highest priority) can be used.
You can change the priority of a service during the boot process and by using the mcelog
service.
Prerequisites
-
You have installed and enabled
tuned
. For more information, see Installing and enabling TuneD.
Procedure
View the scheduling priorities of running threads:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a supplementary mcelog service configuration directory file and insert the policy name and priority in this file:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Reload the
systemd
scripts configuration:systemctl daemon-reload
# systemctl daemon-reload
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
mcelog
service:systemctl restart mcelog
# systemctl restart mcelog
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Display the
mcelog
priority set bysystemd
issue:tuna -t mcelog -P
# tuna -t mcelog -P thread ctxt_switches pid SCHED_ rtpri affinity voluntary nonvoluntary cmd 826 FIFO 20 0,1,2,3 13 0 mcelog
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
27.8. Priority map Copy linkLink copied to clipboard!
Priorities are defined in groups, with some groups dedicated to certain kernel functions. For real-time scheduling policies, an integer between 1
(lowest priority) and 99
(highest priority) is used.
The following table describes the priority range, which can be used while setting the scheduling policy of a process.
Priority | Threads | Description |
---|---|---|
1 | Low priority kernel threads |
This priority is usually reserved for the tasks that need to be just above |
2 - 49 | Available for use | The range used for typical application priorities. |
50 | Default hard-IRQ value | |
51 - 98 | High priority threads | Use this range for threads that execute periodically and must have quick response times. Do not use this range for CPU-bound threads as you will starve interrupts. |
99 | Watchdogs and migration | System threads that must run at the highest priority. |
27.9. TuneD cpu-partitioning profile Copy linkLink copied to clipboard!
For tuning Red Hat Enterprise Linux for latency-sensitive workloads, use the cpu-partitioning
TuneD profile. In Red Hat Enterprise Linux 9 and later, you can perform low-latency tuning more efficiently by using the cpu-partitioning TuneD profile. This profile is easily customizable according to the requirements for individual low-latency applications. The following figure is an example to demonstrate how to use the cpu-partitioning profile. This example uses the CPU and node layout.
You can configure the cpu-partitioning profile in the /etc/tuned/cpu-partitioning-variables.conf
file by using the following configuration options:
- Isolated CPUs with load balancing
In the
cpu-partitioning
figure, the blocks numbered from 4 to 23, are the default isolated CPUs. The kernel scheduler’s process load balancing is enabled on these CPUs. It is designed for low-latency processes with multiple threads that need the kernel scheduler load balancing. You can configure the cpu-partitioning profile in the/etc/tuned/cpu-partitioning-variables.conf
file by using theisolated_cores=cpu-list
option, which lists CPUs to isolate that will use the kernel scheduler load balancing.The list of isolated CPUs is comma-separated or you can specify a range using a dash, such as 3-5. This option is mandatory. Any CPU missing from this list is automatically considered a housekeeping CPU.
- Isolated CPUs without load balancing
In the cpu-partitioning figure, the blocks numbered 2 and 3, are the isolated CPUs that do not provide any additional kernel scheduler process load balancing.
You can configure the
cpu-partitioning
profile in the/etc/tuned/cpu-partitioning-variables.conf
file by using theno_balance_cores=cpu-list
option, which lists CPUs to isolate that will not use the kernel scheduler load balancing.Specifying the
no_balance_cores
option is optional, however any CPUs in this list must be a subset of the CPUs listed in theisolated_cores
list.Application threads using these CPUs need to be pinned individually to each CPU.- Housekeeping CPUs
-
Any CPU not isolated in the
cpu-partitioning-variables.conf
file is automatically considered a housekeeping CPU. On the housekeeping CPUs, all services, daemons, user processes, movable kernel threads, interrupt handlers, and kernel timers are permitted to execute.
27.10. Using the TuneD cpu-partitioning profile for low-latency tuning Copy linkLink copied to clipboard!
You can tune a system for low-latency by using the TuneD’s cpu-partitioning profile. The application in this case uses:
- One dedicated reader thread that reads data from the network will be pinned to CPU 2.
- A large number of threads that process this network data will be pinned to CPUs 4-23.
- A dedicated writer thread that writes the processed data to the network will be pinned to CPU 3.
Prerequisites
-
You have installed the
cpu-partitioning
TuneD profile by using thednf install tuned-profiles-cpu-partitioning
command as root.
Procedure
Edit the
/etc/tuned/cpu-partitioning-variables.conf
file with the following changes:Comment the
isolated_cores=${f:calc_isolated_cores:1}
line:isolated_cores=${f:calc_isolated_cores:1}
# isolated_cores=${f:calc_isolated_cores:1}
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following information for isolated CPUS:
All isolated CPUs: Isolated CPUs without the kernel’s scheduler load balancing:
# All isolated CPUs: isolated_cores=2-23 # Isolated CPUs without the kernel’s scheduler load balancing: no_balance_cores=2,3
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the cpu-partitioning TuneD profile:
tuned-adm profile cpu-partitioning
# tuned-adm profile cpu-partitioning
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Reboot the system.
After rebooting, the system is tuned for low-latency, according to the isolation in the cpu-partitioning figure. The application can use taskset to pin the reader and writer threads to CPUs 2 and 3, and the remaining application threads on CPUs 4-23.
Verification
Verify that the isolated CPUs are not reflected in the Cpus_allowed_list field:
cat /proc/self/status | grep Cpu Cpus_allowed: 003 Cpus_allowed_list: 0-1
# cat /proc/self/status | grep Cpu Cpus_allowed: 003 Cpus_allowed_list: 0-1
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To see affinity of all processes, enter:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteTuneD cannot change the affinity of some processes, mostly kernel processes. In this example, processes with PID 4 and 9 remain unchanged.
27.11. Customizing the cpu-partitioning TuneD profile Copy linkLink copied to clipboard!
You can extend the TuneD profile to make additional tuning changes. For example, the cpu-partitioning
profile sets the CPUs to use cstate=1
. To use the cpu-partitioning
profile but to additionally change the CPU cstate
from cstate1
to cstate0
, the following procedure describes a new TuneD profile named my_profile
, which inherits the cpu-partitioning
profile and then sets C state-0
.
Procedure
Create the
/etc/tuned/my_profile
directory:mkdir /etc/tuned/profiles/my_profile
# mkdir /etc/tuned/profiles/my_profile
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
tuned.conf
file in this directory, and add the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the new profile.
tuned-adm profile my_profile
# tuned-adm profile my_profile
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIn the shared example, a reboot is not required. However, if the changes in the my_profile profile require a reboot to take effect, then reboot your machine.