2 of 3

Processes, Tuning and LogsLesson 2 of 3

Process priority and tuned profiles

Give important work more CPU with nice and renice, and apply a tuned profile that fits what the system does.

  • nice
  • renice
  • ps -o ni
  • tuned-adm
  • tuned-adm list
  • tuned-adm recommend
  • tuned-adm profile
  • tuned-adm active
  • tuned-adm verify

40 min5 exercises

After this lesson you can

  • Start a command with a chosen nice value, and change the value of a running process, knowing the limits for ordinary users.
  • List, choose and apply a tuned profile, and use the recommended one.
  • Verify that a profile is active, persists across reboot, and is applied by the running daemon.

The idea

Priority: nice values

When more processes want the CPU than there are cores, the scheduler decides who runs. You influence it with the nice value, from -20 (most favoured) to 19 (most polite). The default is 0. A nicer process (higher number) yields to others.

The rule to remember: a lower number means higher priority. Ordinary users can only make their own processes nicer; only root can lower a nice value or set a negative one.

CommandDoes
nice -n 10 commandStart a command with nice value 10
sudo nice -n -5 commandStart with a higher priority (root only)
renice -n 10 -p PIDChange a running process
renice -n 5 -u alexChange every process of a user
ps -o pid,ni,comm -p PIDSee the value (NI column)
top then rRenice interactively

Nice affects CPU share, not disk or network. It matters only when the CPU is contended; on an idle machine a nice-19 process still gets all the CPU.

Tuning: tuned profiles

Linux has hundreds of kernel settings (disk scheduler, CPU frequency policy, virtual memory, network buffers). tuned applies a coherent set of them, a profile, matched to the machine’s job. It runs as a service and applies the profile at boot.

ProfileOptimised for
balancedGeneral use, saves some power when idle
throughput-performanceServers doing a lot of work: disk and network throughput
virtual-guestA virtual machine (default when tuned detects one)
latency-performanceLow response time at the cost of power
network-latency, network-throughputNetwork-heavy workloads
powersaveLaptops and low power

tuned-adm recommend inspects the machine (virtual or physical, type) and names the profile Red Hat suggests. That is the right answer for “set the recommended profile”.

Try it

ps -eo pid,ni,comm --sort=ni | head -4
    PID  NI COMMAND
     21 -20 kworker/R-kblockd
     22 -20 kworker/R-ata_sff
    731  -7 rtkit-daemon

Start and change

nice -n 15 sha256sum /dev/zero &
ps -o pid,ni,comm -C sha256sum
[1] 3011
    PID  NI COMMAND
   3011  15 sha256sum
renice -n 10 -p 3011
renice -n 0 -p 3011
3011 (process ID) old priority 15, new priority 10
renice: failed to set priority for 3011 (process ID): Permission denied

Raising the value (more polite) worked, lowering it back did not: a normal user cannot undo being nice. Root can: sudo renice -n 0 -p 3011.

Choose a tuned profile

sudo dnf install -y tuned
sudo systemctl enable --now tuned
tuned-adm active
tuned-adm recommend
Current active profile: virtual-guest
virtual-guest
tuned-adm list | head -5
Available profiles:
- balanced                    - General non-specialized tuned profile
- latency-performance         - Optimize for deterministic performance at the cost of increased power consumption
- powersave                   - Optimize for low power consumption
- throughput-performance      - Broadly applicable tuning that provides excellent performance across a variety of common server workloads
sudo tuned-adm profile throughput-performance
tuned-adm active
sudo tuned-adm verify
cat /etc/tuned/active_profile
Current active profile: throughput-performance
Verification succeeded, current system settings match the preset profile.
See tuned log file ('/var/log/tuned/tuned.log') for details.
throughput-performance

tuned-adm profile takes effect immediately and is saved in /etc/tuned/active_profile, so it is applied again at boot as long as the tuned service is enabled. If tuned is not enabled, the setting is lost on reboot, which is the usual trap in a “must persist” task.

Going further

A custom profile is a directory in /etc/tuned/profiles/NAME/ containing tuned.conf; it can start with [main] and include=balanced and override a few [sysctl] or [cpu] settings. tuned-adm profile_info NAME prints a profile’s contents, and tuned-adm profile A B merges two. tuned-adm verify is also a check for drift after someone changes a sysctl by hand. Priority has more levers than nice: chrt sets real-time scheduling policies, taskset pins a process to CPUs, and systemd units can set Nice= and CPUWeight= for services.

Practice

0 of 5 done

Do these on your own system. They get harder. Check each result, then tick it off.

  1. Warm-up

    Read the nice value of running processes

    Show PID, nice value and command for all processes, sorted by nice value. Say what the lowest and highest possible values are and which one means "more important".

    Check your result

    ps -eo pid,ni,comm --sort=ni shows values between -20 and 19. A lower number means higher priority.

    Hint

    NI is the nice value. Most processes are 0.

    Solution
    ps -eo pid,ni,comm --sort=ni | head -5
    ps -eo pid,ni,comm --sort=-ni | head -5
  2. Practice

    Start a job with lower priority

    Start sha256sum /dev/zero in the background with a nice value of 15. Confirm the value in ps. Then start a second one at nice value -5 as root and confirm. Kill both.

    Check your result

    ps -o pid,ni,comm -p PID shows 15 for the first and -5 for the second.

    Hint

    nice -n 15 command &. Negative values need root: sudo nice -n -5 command &.

    Solution
    nice -n 15 sha256sum /dev/zero &
    sudo nice -n -5 sha256sum /dev/zero &
    ps -o pid,ni,user,comm -C sha256sum
    pkill sha256sum; sudo pkill sha256sum
  3. Practice

    Change a running process

    Start a sha256sum /dev/zero as a normal user. Change its nice value to 10, then try to set it back to 0 as that same user, and observe. Do it again as root. Explain the rule.

    Check your result

    The normal user can raise the value to 10 (lower priority) but gets Permission denied when lowering it back. Root can set any value.

    Hint

    renice -n 10 -p PID. Normal users may only make their own processes nicer, never less nice.

    Solution
    sha256sum /dev/zero & pid=$!
    renice -n 10 -p $pid
    renice -n 0 -p $pid
    sudo renice -n 0 -p $pid
    ps -o pid,ni,comm -p $pid
    kill $pid
  4. Practice

    Apply the recommended tuned profile

    Make sure tuned is installed and enabled at boot. Show the available profiles and the active one. Ask tuned for the recommended profile for this system and apply it. Verify.

    Check your result

    tuned-adm active prints Current active profile: <recommended>, for a VM usually virtual-guest, and tuned-adm recommend prints the same name. systemctl is-enabled tuned prints enabled.

    Hint

    sudo dnf install -y tuned; sudo systemctl enable --now tuned. tuned-adm recommend and tuned-adm profile NAME.

    Solution
    sudo dnf install -y tuned
    sudo systemctl enable --now tuned
    tuned-adm list
    tuned-adm active
    tuned-adm recommend
    sudo tuned-adm profile $(tuned-adm recommend)
    tuned-adm active
  5. Challenge

    Prove the profile is applied and persists

    Switch to throughput-performance, verify the daemon applied it, confirm the profile name is stored where it survives a reboot, reboot, and check again. Then switch back to the recommended profile.

    Check your result

    tuned-adm verify ends with Verification succeeded, current system settings match the preset profile. cat /etc/tuned/active_profile prints the profile name, and it is unchanged after the reboot.

    Hint

    tuned-adm verify compares live settings to the profile. The active profile is stored in /etc/tuned/active_profile.

    Solution
    sudo tuned-adm profile throughput-performance
    sudo tuned-adm verify
    cat /etc/tuned/active_profile
    sudo reboot
    # after reboot
    tuned-adm active
    sudo tuned-adm profile $(tuned-adm recommend)