LinuxCommandLibrary

ionice

Set I/O scheduling priority for a process

TLDR

Run a command with the given scheduling class and priority

$ ionice [[-c|--class]] [scheduling_class] [[-n|--classdata]] [priority] [command]
copy

Set I/O scheduling class of a running process with a specific [p]id, [P]gid or [u]id
$ ionice [[-c|--class]] [scheduling_class] -[p|P|u] [id]
copy

Run a command with custom I/O scheduling class and priority
$ ionice [[-c|--class]] [scheduling_class] [[-n|--classdata]] [priority] [command]
copy

Ignore failure to set the requested priority
$ ionice [[-t|--ignore]] [[-n|--classdata]] [priority] [[-p|--pid]] [pid]
copy

Run the command even in case it was not possible to set the desired priority (this can happen due to insufficient privileges or an old kernel version)
$ ionice [[-t|--ignore]] [[-n|--classdata]] [priority] [[-p|--pid]] [pid]
copy

Print the I/O scheduling class and priority of a running process
$ ionice [[-p|--pid]] [pid]
copy

SYNOPSIS

ionice [options] [command [arguments...]]
ionice -t [-p pid...]

PARAMETERS

-c, --class class
    Set the I/O scheduling class. class can be idle, best-effort, or realtime.

-n, --classdata level
    Set the I/O scheduling priority level (0-7). Lower numbers indicate higher priority.

-p, --pid pid...
    Operate on specified process IDs instead of a new command.

-t, --show-all
    Display the I/O scheduling class and priority for all running processes (requires root).

-u, --uid uid
    Run the command as a specified user ID.

-g, --gid gid
    Run the command as a specified group ID.

-P, --process
    Act on the specific process (PID) rather than its entire process group when combined with -p and a command.

-A, --action
    An alias for --process, setting I/O priority for a specific process rather than its group.

-T, --no-prefix
    When showing all processes (with -t), suppress the "I/O scheduling class for..." prefix.

-V, --version
    Display version information and exit.

-h, --help
    Display a help message and exit.

DESCRIPTION

The ionice command allows users to get or set the I/O scheduling class and priority for a program. It is an essential tool for managing disk I/O contention, ensuring that critical applications receive sufficient bandwidth while background tasks don't monopolize disk access.

Linux I/O schedulers, particularly CFQ (Completely Fair Queuing) and BFQ (Budget Fair Queuing), categorize I/O requests into different classes. ionice supports three main classes: idle, best-effort, and realtime. The idle class provides the lowest priority, allowing I/O only when no other requests are pending. The best-effort class is the default for most processes and can be assigned a priority level from 0 (highest) to 7 (lowest). The realtime class offers the highest priority, guaranteeing a certain amount of I/O bandwidth, also with levels from 0 to 7.

By appropriately assigning I/O priorities, administrators and users can prevent "I/O starvation" for interactive applications and ensure smooth system performance, especially on systems with heavy disk activity or multiple competing processes.

CAVEATS

The effectiveness of ionice depends on the underlying Linux I/O scheduler. It primarily works with CFQ (Completely Fair Queuing) and BFQ (Budget Fair Queuing) schedulers. Other schedulers (like NOOP or Deadline) may not fully respect I/O priorities.

Using the realtime I/O class improperly can lead to I/O starvation for other processes, potentially making the system unresponsive. Setting realtime priority often requires root privileges.

ionice controls disk I/O, not network I/O or CPU scheduling; for CPU scheduling, use nice or renice.

I/O SCHEDULING CLASSES EXPLAINED

Idle Class: Processes in this class only get disk I/O time when no other process is requesting I/O. This is suitable for very low-priority background tasks like file indexing or backups that should not interfere with interactive usage.

Best-Effort Class: This is the default class. Processes in this class share I/O bandwidth based on their assigned priority level (0-7). A process with priority 0 gets more I/O bandwidth than a process with priority 7. Most user applications fall into this category.

Realtime Class: Processes in this class are given guaranteed I/O bandwidth and are largely unaffected by other I/O requests. This is for critical applications where I/O latency and throughput are paramount, such as multimedia streaming or real-time data acquisition. Use with caution as it can starve other processes.

CHECKING I/O SCHEDULER

To determine the active I/O scheduler for a device, you can inspect /sys/block/<device>/queue/scheduler. For example, to check sda:
cat /sys/block/sda/queue/scheduler. The output will typically show the active scheduler in square brackets, e.g., [bfq] mq-deadline none.

HISTORY

ionice is part of the util-linux project, a collection of essential Linux utilities. It was introduced to provide user-space control over the kernel's I/O scheduling capabilities, which became more sophisticated with the development of I/O schedulers like CFQ. Its usage became more widespread as multi-user systems and I/O-intensive applications demanded finer-grained control over disk resource allocation.

SEE ALSO

nice(1), renice(8), ps(1), iostat(1), lsblk(8)

Copied to clipboard