Portage niceness

From Gentoo Wiki
Jump to:navigation Jump to:search

This article describes some knobs available for system administrators to help manage Portage's resource usage.

Background

Priority and Nice Values

The priority value (PR) of a process ranges from 0 to 139, giving high to low priority respectively. Real time process occupy 0 to 99 and user processes range of 100 to 139.

User process priority is defined in terms of the nice level (NI) plus 20 (NI + 20). The nice level therefore ranges from -20 to 19, which corresponds to a user process priority of 0 to 39 and a PR value of 100 to 139. For example, giving a process a nice value of 0 translates into a PR of 120.

Controlling Priority

Linux has a few options to control system responsiveness by limiting a process' use of resources, including nice (which is POSIX, not Linux-exclusive), ionice, and chrt. The interaction between these is complicated and it's hard to reason about usually.

But in short:

  • nice controls priority with regard to the CPU scheduler
  • ionice controls priority with regard to the disk I/O scheduler
  • chrt is like an extended nice - it can change attributes of the process(es) which the CPU scheduler utilizes, rather than just the simplistic 'niceness' level, like priority/task class.

Resources online also cover the distinction between nice and chrt.

In any case, all three are valuable tools in making Portage run smoothly in the background without interfering with daily use. Anecdotally, chrt seems to make the most difference.

Configuration

Note
The /etc/portage/make.conf variables mentioned here don't allow running multiple commands by themselves, hence the use of a wrapper script. This issue is tracked as bug #565418.

Enable both PORTAGE_NICENESS and PORTAGE_IONICE_COMMAND in /etc/portage/make.conf:

FILE /etc/portage/make.conf
# Lowest priority
PORTAGE_NICENESS="19"
# Call the wrapper script which handles ionice and chrt (priority)
PORTAGE_IONICE_COMMAND="/usr/local/bin/io-priority \${PID}"

Create the wrapper script to handle both ionice and chrt:

FILE /usr/local/bin/io-priority
#!/bin/bash
PID=${1}

# Could use `ionice -c 2 -n 7 -p ${PID}` to be slightly less aggressive.
ionice -c 3 -p ${PID}
chrt -p -i 0 ${PID}

Make the script executable so emerge can use it:

root #chmod +x /usr/local/bin/io-priority

See also