Saturday, December 26, 2015

How To Avoid Sudden Outburst Of Backup Shell Script or Program Disk I/O on Linux

A sudden outburst of violent disk I/O activity can bring down your email or web server. Usually, a web, mysql, or mail server serving millions and millions pages (requests) per months are prone to this kind of problem. Backup activity can increase current system load too. To avoid this kind of sudden outburst problem, run your script with scheduling class and priority. Linux comes with various utilities to manage this kind of madness.

CFQ scheduler

You need Linux kernels 2.6.13+ with the CFQ IO scheduler. CFQ (Completely Fair Queuing) is an I/O scheduler for the Linux kernel, which is default in 2.6.18+ kernel. RHEL 4/ 5 and SuSE Linux has all scheduler built into kernel so no need to rebuild your kernel. To find out your scheduler name, enter:
# for d in /sys/block/sd[a-z]/queue/scheduler; do echo "$d => $(cat $d)" ; done
Sample output for each disk:
/sys/block/sda/queue/scheduler => noop anticipatory deadline [cfq]
/sys/block/sdb/queue/scheduler => noop anticipatory deadline [cfq]
/sys/block/sdc/queue/scheduler => noop anticipatory deadline [cfq] 
CFQ is default and recommended for good performance.

Old good nice program

You can run a program with modified scheduling priority using nice command (19 = least favorable):
# nice -n19 /path/to/
Sample cronjob:
@midnight /bin/nice -n19 /path/to/

Say hello to ionice utility

The ionice command provide better control as compare to nice command for the I/O scheduling class and priority of a program or script. It supports following three scheduling classes (quoting from the man page):
  • Idle : A program running with idle io priority will only get disk time when no other program has asked for disk io for a defined grace period. The impact of idle io processes on normal system activity should be zero. This scheduling class does not take a priority argument.
  • Best effort : This is the default scheduling class for any process that hasn’t asked for a specific io priority. Programs inherit the CPU nice setting for io priorities. This class takes a priority argument from 0-7, with lower number being higher priority. Programs running at the same best effort priority are served in a round-robin fashion. This is usually recommended for most application.
  • Real time : The RT scheduling class is given first access to the disk, regardless of what else is going on in the system. Thus the RT class needs to be used with some care, as it can starve other processes. As with the best effort class, 8 priority levels are defined denoting how big a time slice a given process will receive on each scheduling window. This is should be avoided for all heavily loaded system.


The syntax is:
ionice options  PID
ionice options -p PID
ionice -c1 -n0  PID

How do I use the ionice command on Linux?

Linux refers the scheduling class using following number system and priorities:
Scheduling classNumberPossible priority
real time18 priority levels are defined denoting how big a time slice a given process will receive on each scheduling window
best-effort20-7, with lower number being higher priority
idle3Nil ( does not take a priority argument)


To display the class and priority of the running process, enter:
# ionice -p {PID}
# ionice -p 1

Sample output:
none: prio 0
Dump full web server disk / mysql or pgsql database backup using best effort scheduling (2) and 7 priority:
# /usr/bin/ionice -c2 -n7 /root/scripts/nas.backup.full
Open another terminal and watch disk I/O network stats using atop/tip or top or your favorite monitoring tool:
# atop
Sample cronjob:
@weekly /usr/bin/ionice -c2 -n7 /root/scripts/nas.backup.full >/dev/null 2>&1
You can set process with PID 1004 as an idle io process, enter:
# ionice -c3 -p 1004
Runs script as a best-effort program with highest priority, enter:
# ionice -c2 -n0 /path/to/
Type the following command to run 'zsh' as a best-effort program with highest priority.
# ionice -c 2 -n 0 zsh
Finally, you can combine both nice and ionice together:
# nice -n 19 ionice -c2 -n7 /path/to/shell.script
Related: chrt command to set / manipulate real time attributes of a Linux process and taskset command to retrieve or set a processes's CPU affinity.
To see help on options type:
$ ionice --help
Sample outputs:
Sets or gets the IO scheduling class and priority of processes.
 ionice [options] -p ...
 ionice [options] -P ...
 ionice [options] -u ...
 ionice [options] 
 -c, --class     name or number of scheduling class,
                          0: none, 1: realtime, 2: best-effort, 3: idle
 -n, --classdata   priority (0..7) in the specified scheduling class,
                          only for the realtime and best-effort classes
 -p, --pid ...     act on these already running processes
 -P, --pgid ...   act on already running processes in these groups
 -t, --ignore           ignore failures
 -u, --uid ...     act on already running processes owned by these users
 -h, --help     display this help and exit
 -V, --version  output version information and exit

Other suggestion to improve disk I/O

  1. Use hardware RAID controller.
  2. Use fast SCSI / SA-SCSI / SAS 15k speed disk.
  3. Use fast SSD based storage (costly option).
  4. Use slave / passive server to backup MySQL
Recommended readings:

No comments:

Post a Comment