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.
nicereniceps -o nituned-admtuned-adm listtuned-adm recommendtuned-adm profiletuned-adm activetuned-adm verify
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.
| Command | Does |
|---|---|
nice -n 10 command | Start a command with nice value 10 |
sudo nice -n -5 command | Start with a higher priority (root only) |
renice -n 10 -p PID | Change a running process |
renice -n 5 -u alex | Change every process of a user |
ps -o pid,ni,comm -p PID | See the value (NI column) |
top then r | Renice 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.
| Profile | Optimised for |
|---|---|
balanced | General use, saves some power when idle |
throughput-performance | Servers doing a lot of work: disk and network throughput |
virtual-guest | A virtual machine (default when tuned detects one) |
latency-performance | Low response time at the cost of power |
network-latency, network-throughput | Network-heavy workloads |
powersave | Laptops 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.
- 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=nishows values between -20 and 19. A lower number means higher priority.Hint
NIis the nice value. Most processes are0.Solution
ps -eo pid,ni,comm --sort=ni | head -5 ps -eo pid,ni,comm --sort=-ni | head -5 - Practice
Start a job with lower priority
Start
sha256sum /dev/zeroin the background with a nice value of 15. Confirm the value inps. Then start a second one at nice value -5 as root and confirm. Kill both.Check your result
ps -o pid,ni,comm -p PIDshows15for the first and-5for 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 - Practice
Change a running process
Start a
sha256sum /dev/zeroas 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 deniedwhen 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 - Practice
Apply the recommended tuned profile
Make sure
tunedis 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 activeprintsCurrent active profile: <recommended>, for a VM usuallyvirtual-guest, andtuned-adm recommendprints the same name.systemctl is-enabled tunedprintsenabled.Hint
sudo dnf install -y tuned; sudo systemctl enable --now tuned.tuned-adm recommendandtuned-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 - 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 verifyends withVerification succeeded, current system settings match the preset profile.cat /etc/tuned/active_profileprints the profile name, and it is unchanged after the reboot.Hint
tuned-adm verifycompares 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)