Setting real-time priorities to services using systemd

Systemd supports the setup of real-time priority to services launched during the boot process. This is done using the following Directives in the Service section:

  • CPUSchedulingPolicy=
    Sets the CPU scheduling policy for executed processes. Takes one of other, batch, idle, fifo or rr.
  • 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.

Example

This is the mcelog service unity:

[Unit]
Description=Machine Check Exception Logging Daemon

[Service]
ExecStart=/usr/sbin/mcelog --ignorenodev --daemon --foreground

[Install]
WantedBy=multi-user.target

By adding the following directives in Service section:

CPUSchedulingPolicy=fifo
CPUSchedulingPriority=20

For example:

[Unit]
Description=Machine Check Exception Logging Daemon

[Service]
ExecStart=/usr/sbin/mcelog --ignorenodev --daemon --foreground
CPUSchedulingPolicy=fifo
CPUSchedulingPriority=20

[Install]
WantedBy=multi-user.target

When restarted, the mcelog service will start with the priority set by systemd:

# tuna --show_threads | grep mcelog
4680 FIFO 20 0,1 2 0 mcelog