Linux下限制进程资源使用


通常linux下限制cpu使用有三种方法:
nice/renice:调整进程使用cpu的优先级
cpulimit :不修改进程的nice值,通过暂停进程一段时间,来限制cpu使用
cgroups:内核提供的机制,可以限制、记录、隔离一组进程所使用的cpu、内存、磁盘、网络等资源,是docker等容器的基础技术之一。
限制磁盘io :
ionice : 调整io调度的优先级
cgroups
这里只说nice和ionice,实际上nice和ionice只是改变优先级,并没有真正的限制。3种控制进程运行时间的方法:

使用 nice 命令手动降低任务的优先级。
使用 cpulimit 命令不断的暂停进程,以控制进程所占用处理能力不超过特定限制。
使用linux内建的control groups(控制组)功能,它提供了限制进程资源消耗的机制。

1、nice
1.1 进程优先级
要理解nice值,首先要说明一下优先级的概念,先来看一下进程的信息:
# ps -efl
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
5 S freeoa 5019 1608 -5 80 0 - 119325 ep_pol Mar23 ? 00:03:55 /usr/sbin/httpd
PRI 指进程优先级,优先级小的进程会获得比较多的CPU时间,程序就会被优先处理
NI 即为nice值
两者关系为:PRI(new)=PRI(default)+nice
其中nice值可以用户指定,nice的默认值为0,root可用范围从-20到19,普通用户只能用0到19,值越小PRI(new)越小,CPU执行优先级越高。同时可以知道:只有root能提高优先级,普通用户只能降低优先级。使用nice命令(不带任何参数时)可以将进程的nice值设置为10。这样调度器就会将此进程视为较低优先级的进程,从而减少cpu资源的分配。nice 还有一个关联命令叫做 renice,它可以在运行时调整进程的 nice 值。使用 renice 命令时,要先找出进程的 PID。
1.2 设置程序启动时的优先级
nice 只有一个参数 : -n,启动程序时指定优先级:
nice -n -20 /opt/backup.sh #优先级最高
nice -n 19 /opt/backup.sh #优先级最低
1.3 设置程序运行时的优先级
对于已经在运行的程序,如果需要调整优先级,需要用renice命令,设置正在运行程序nice的值为15:
# renice -n +15 5319
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
5 S freeoa 5319 1607 -5 95 15 - 119325 ep_pol Mar23 ? 00:03:55 /usr/sbin/httpd
root用户可以为其它用户或组设置nice值:
# renice -n +15 -u freeoa #设置freeoa用户的所有进程nice值为15
还可以为进程组设置nice值,查看进程组:
# ps -efj
UID PID PPID PGID SID C STIME TTY TIME CMD
freeoa 5296 1607 1607 1607 0 Mar23 ? 00:03:54 /usr/sbin/httpd
freeoa 5319 1607 1607 1607 0 Mar23 ? 00:03:55 /usr/sbin/httpd
freeoa 5394 1607 1607 1607 0 Mar23 ? 00:03:55 /usr/sbin/httpd
apache的进程组(PGID)为:1607,设置nice值为-8:
# renice -n -8 -g 1607
命令用法:nice [选项] [命令 [参数]...]
以指定的优先级运行命令,这会影响相应进程的调度。如果不指定命令,程序会显示当前的优先级。优先级的范围是从 -20(最大优先级) 到 19 (最小优先级)。-n, --adjustment=N 对优先级数值加上指定整数N(默认为10)
--help 显示此帮助信息并退出
--version 显示版本信息并退出
注意:shell内含自己的nice 程序版本,它会覆盖这里所提及的相应版本,请查阅您的shell 文档获知它所支持的选项。
2、ionice
Linux默认IO调度器使用CFQ调度算法,支持用ionice命令为程序指定io调度策略和优先级,IO调度策略分为三种:
Idle:其他进程没有磁盘IO时,才进行磁盘IO
Best Effort:缺省调度策略,可以设置0-7的优先级,数值越小优先级越高,同优先级的进程采用round-robin算法调度
Real Time:立即访问磁盘,无视其它进程IO
None 即 Best Effort,进程未指定策略和优先级时显示为none,会使用依据cpu nice设置计算出优先级
策略:0 - none, 1 - Real Time, 2 - Best Effort, 3 - idle
ionice command provide more control as compare to nice command. This program sets the io scheduling class and priority for 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.
Linux refers the scheduling class using following number system and priorities:
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 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 or top or your favorite monitoring tool:
# atop
cronjob:
@weekly /usr/bin/ionice -c2 -n7 /root/scripts/backup.full >/dev/null 2>&1
You can set process with PID 1004 as an idle io process, enter:
# ionice -c3 -p 1004
Runs rsync.sh script as a best-effort program with highest priority, enter:
# ionice -c2 -n0 /path/to/rsync.sh
Finally, you can combine both nice and ionice together:
# nice -n 19 ionice -c2 -n7 /path/to/shell.script
使用idle策略:
ionice -c 3 -p pid
使用Real Time策略:
ionice -c 1 -p pid
使用Best Effort策略,并指定优先级最低:
ionice -c 2 -n 7 -p pid
ionice 是一个可以对另一个程序设置或获取 I/O 调度级别和优先级的有用程序。如果没有给出参数或者只有 -p 参数,那么 ionice 将会查询该进程的当前的 I/O 调度级别以及优先级。
如果我们给出命令名称,如rm命令,它将使用给定的参数运行此命令。要获取或设置调度参数,请指定[进程的 PID],如下:
# ionice -p PID
要指定名字或者调度的数字,使用(0 表示无、1 表示实时、2 表示尽力、3 表示空闲)下面的命令。以下命令表示rm会属于空闲 I/O 级别,并且只在其他进程不使用的时候使用 I/O:
---- Deleting Huge Files in Linux -----
# ionice -c 3 rm /var/logs/syslog
# ionice -c 3 rm -rf /var/log/apache
如果系统中没有很多空闲时间,那么我们希望使用尽力调度级别,并且使用低优先级:
# ionice -c 2 -n 6 rm /var/logs/syslog
# ionice -c 2 -n 6 rm -rf /var/log/apache
命令功能:ionice – 获取或设置程序的IO调度与优先级。
命令格式:
ionice [[-c class] [-n classdata] [-t]] -p PID [PID]…
ionice [-c class] [-n classdata] [-t] COMMAND [ARG]…
IO调度策略:
ionice将磁盘IO调度分为三类:
ilde:空闲磁盘调度,该调度策略是在当前系统没有其他进程需要进行磁盘IO时,才能进行磁盘;因此该策略对当前系统的影响基本为0;当然该调度策略不能带有任何优先级参数;目前,普通用户是可以使用该调度策略(自从内核2.6.25开始)。
Best effort:是缺省的磁盘IO调度策略:
(1)该调度策略可以指定优先级参数(范围是0~7,数值越小,优先级越高)
(2)针对处于同一优先级的程序将采round-robin方式
(3)对于best effort调度策略,8个优先级等级可以说明在给定的一个调度窗口中时间片的大小
(4)目前,普调用户(非root用户)是可以使用该调度策略
(5)在内核2.6.26之前,没有设置IO优先级的进程会使用"none"作为调度策略,但是这种策略使得进程看起来像是采用了best effort调度策略,因为其优先级是通过关于cpu nice有关的公式计算得到的:io_priority = (cpu_nice + 20) /5
(6)在内核2.6.26之后,如果当前系统使用的是CFQ调度器,那么如果进程没有设置IO优先级级别,将采用与内核2.6.26之前版本同样的方式,推到出io优先级级别
Real time:实时调度策略,如果设置了该磁盘IO调度策略,则立即访问磁盘,不管系统中其他进程是否有IO。因此使用实时调度策略,需要注意的是,该访问策略可能会使得其他进程处于等待状态。
参数说明:
-c class:class表示调度策略,其中0 for none, 1 for real time, 2 for best-effort, 3 for idle。
-n classdata:classdata表示IO优先级级别,对于best effort和real time,classdata可以设置为0~7。
-p pid:指定要查看或设置的进程号或者线程号,如果没有指定pid参数,ionice will run the listed program with the given parameters。
-t:忽视设置优先级时产生的错误。
3、同时限制cpu和磁盘io的优先级
为了对生产环境造成影响最小,设置备份脚本运行时,cpu和磁盘io的优先级都最低:
/usr/bin/nice -n 19 /usr/bin/ionice -c2 -n7 /bin/sh /opt/backup.sh
对于一些运行时会造成系统满载的脚本,例如数据库备份会影响当时其他服务的响应速度,可以通过ionice和nice对其IO优先级和CPU优先级进行调整,例如降低"/usr/local/bin/backup.sh"的IO优先级,让其他进程顺畅运行:
/usr/bin/ionice -c2 -n7 /usr/local/bin/backup.sh
其中:
-c: scheduling class, 0: none, 1: realtime, 2: best-effort, 3: idle
-n: class data (0-7, lower being higher prio)
降低其CPU优先级, 可以通过:
/usr/bin/nice -n 19 /usr/local/bin/backup.sh
其中:-n, --adjustment=N add integer N to the niceness (default 10), nicenesses range from -20 (most favorable scheduling) to 19 (least favorable).
nice和ionice可以一起使用,例如:
/usr/bin/nice -n 19 /usr/bin/ionice -c2 -n7 /usr/local/bin/backup.sh
4、cpulimit
限制其对CPU的使用率,可使用cpulimit工具来实现。cpulimit 的使用方式和 nice 命令类似,但是需要用户使用 -l 选项显式地定义进程的 cpu 使用率上限值。
cpulimit --limit=10 /bin/gzip vzdump-2018_06_26.tar.gzip
cpulimit 命令的工作原理是为进程预设一个 cpu 占用率限制,并实时监控进程是否超出此限,若超出则让该进程暂停运行一段时间。cpulimit 使用 SIGSTOP 和 SIGCONT 这两个信号来控制进程,它不会修改进程的 nice 值,而是通过监控进程的 cpu 占用率来做出动态调整。cpulimit 的优势是可以控制进程的cpu使用率的上限值。但与 nice 相比也有缺点,那就是即使 cpu 是空闲的,进程也不能完全使用整个 cpu 资源。
cpulimit 还可以在运行时对进程进行动态限制,使用 -p 选项来指定进程的 PID,下面是一个实例:
cpulimit -l 50 -p 1237
其中1237是进程的 PID。
cpulimit工具通过以不同的时间间隔来暂停/继续进程的运行状态来控制进程的CPU使用率,以将其保持在所定义的上限之下。
tar -c myData | gzip -c > myData.gz & cpulimit -l 10 -e gzip
5、cgroups
功能最为强大的目前当数控制组(cgroups)。它是 Linux 内核提供的一种机制,利用它可以指定一组进程的资源分配。具体来说,使用 cgroups,用户能够限定一组进程的 cpu 占用率、系统内存消耗、网络带宽,以及这几种资源的组合。
对比nice和cpulimit,cgroups 的优势在于它可以控制一组进程,不像前者仅能控制单进程。nice 和 cpulimit 只能限制 cpu 使用率,而 cgroups 则可以限制其他进程资源的使用。对 cgroups 善加利用就可以控制好整个子系统的资源消耗。就拿 CoreOS 作为例子,这是一个专为大规模服务器部署而设计的最简化的 Linux 发行版本,它的 upgrade 进程就是使用 cgroups 来管控,这样系统在下载和安装升级版本时也不会影响到系统的性能。
下面做一下演示,我们将创建两个控制组(cgroups),并对其分配不同的 cpu 资源。这两个控制组分别命名为'cpulimited'和'lesscpulimited'。这里使用了Mathomatic toolkit中的质数生成器来模拟CPU负载。
使用 cgcreate 命令来创建控制组,如下所示:
cgcreate -g cpu:/cpulimited
cgcreate -g cpu:/lesscpulimited
其中'-g cpu'选项用于设定 cpu 的使用上限。除 cpu 外,cgroups 还提供 cpuset、memory、blkio 等控制器。cpuset 控制器与 cpu 控制器的不同在于:cpu 控制器只能限制一个 cpu 核的使用率,而 cpuset 可以控制多个 cpu 核。
cpu 控制器中的 cpu.shares 属性用于控制 cpu 使用率,它的默认值是 1024,我们将 lesscpulimited 控制组的 cpu.shares 设为1024(默认值),而 cpulimited 设为512,配置后内核就会按照2:1的比例为这两个控制组分配资源。
要设置 cpulimited 组的 cpu.shares 为 512,输入以下命令:
cgset -r cpu.shares=512 cpulimited
使用 cgexec 命令来启动控制组的运行,为了测试这两个控制组,先用cpulimited 控制组来启动 matho-primes 进程,命令行如下:
cgexec -g cpu:cpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null &
打开 top 可以看到,matho-primes 进程占用了所有的 cpu 资源。
因为只有一个进程在系统中运行,不管将其放到哪个控制组中启动,它都会尽可能多的使用cpu资源。cpu资源限制只有在两个进程争夺cpu资源时才会生效,那么现在我们就启动第二个 matho-primes 进程,这一次在 lesscpulimited 控制组中来启动它:
cgexec -g cpu:lesscpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null &
再打开 top 就可以看到,cpu.shares 值大的控制组会得到更多的 cpu 运行时间。
现在我们再在 cpulimited 控制组中增加一个 matho-primes 进程:
cgexec -g cpu:cpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null &
可以看到两个控制组的 cpu 的占用率比例仍然为2:1。其中cpulimited控制组中的两个 matho-primes 进程获得的cpu时间基本相当,而另一组(lesscpulimited)中的matho-primes进程显然获得了更多的运行时间片,它所占用的时间片大致为前者之和。
6、参考来源
linux的cpu和磁盘io优先级设置
使用 nice、cpulimit 和 cgroups 限制 cpu 占用率
7、制造与查看系统负载
System Load View and Maker in Linux
用于查看系统负载的相关命令工具
uptime,vmstat,top,mpstat,Glances
系统负载制造程序一览
GNU coreutils 工具集中有一个yes指令,直接运行它它能不断的每行输出字母'y',且其所在的cpu核也会近似于满负载。
用法:yes [字符串]...
或:yes 选项
Repeatedly output a line with all specified STRING(s), or 'y'.
--help 显示此帮助信息并退出
--version 显示版本信息并退出
可查看在线帮助。
将其放入后台一直运行,制造一个核心的使用率。
$ yes > /dev/null &
yes指令是单进程的,它可以在单核上制造100%的cpu使用率,多核环境下可以开N个进程实例,理论上可以制造N倍于核心的负载(N必须小于核心数量)。可以使用killall来中止这些yes指令进程。
$ killall yes
同样还可以使用dd命令实现同样的效果
to produce 100% CPU load usage is:
$ dd if=/dev/zero of=/dev/null
当然还是stress、stress-ng工具比较直接可操作性好,在4个核心上为每核心制造约50%的负载使用率
stress-ng --cpu 4 --cpu-load 50
使用top查看负载总体情况。
使用mpstat查看负载总体情况。
mpstat to Display CPU Activity
The first line is a set of column labels. The second line is the value for each column:
%usr – % CPU usage at the user level
%nice – % CPU usage for user processes labeled “nice”
%sys – % CPU usage at the system (Linux kernel) level
%iowait – % CPU usage idling waiting on a disk read/write
%irq – % CPU usage handling hardware interrupts
%soft – % CPU usage handing software interrupts
%steal – % CPU usage being forced to wait for a hypervisor handling other virtual processors
%guest – % CPU usage spent running a virtual processor
%idle – % CPU usage on idle time (no processes, and not waiting on a disk read/write)
图形化工具(Graphical Utility)
graphical monitoring tool:gnome-system-monitor
三大资源使用限制工具比较
nice是对系统进行一次性调整的好工具。
当需要运行CPU密集型作业时,cpulimit非常有用,而空闲的CPU时间对于系统的响应性至关重要。
cgroups是瑞士军刀级的限制方案,提供最大的灵活性。
nice/renice:调整进程使用cpu的优先级
cpulimit :不修改进程的nice值,通过暂停进程一段时间,来限制cpu使用
cgroups:内核提供的机制,可以限制、记录、隔离一组进程所使用的cpu、内存、磁盘、网络等资源,是docker等容器的基础技术之一。
限制磁盘io :
ionice : 调整io调度的优先级
cgroups
这里只说nice和ionice,实际上nice和ionice只是改变优先级,并没有真正的限制。3种控制进程运行时间的方法:

使用 nice 命令手动降低任务的优先级。
使用 cpulimit 命令不断的暂停进程,以控制进程所占用处理能力不超过特定限制。
使用linux内建的control groups(控制组)功能,它提供了限制进程资源消耗的机制。

1、nice
1.1 进程优先级
要理解nice值,首先要说明一下优先级的概念,先来看一下进程的信息:
# ps -efl
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
5 S freeoa 5019 1608 -5 80 0 - 119325 ep_pol Mar23 ? 00:03:55 /usr/sbin/httpd
PRI 指进程优先级,优先级小的进程会获得比较多的CPU时间,程序就会被优先处理
NI 即为nice值
两者关系为:PRI(new)=PRI(default)+nice
其中nice值可以用户指定,nice的默认值为0,root可用范围从-20到19,普通用户只能用0到19,值越小PRI(new)越小,CPU执行优先级越高。同时可以知道:只有root能提高优先级,普通用户只能降低优先级。使用nice命令(不带任何参数时)可以将进程的nice值设置为10。这样调度器就会将此进程视为较低优先级的进程,从而减少cpu资源的分配。nice 还有一个关联命令叫做 renice,它可以在运行时调整进程的 nice 值。使用 renice 命令时,要先找出进程的 PID。
1.2 设置程序启动时的优先级
nice 只有一个参数 : -n,启动程序时指定优先级:
nice -n -20 /opt/backup.sh #优先级最高
nice -n 19 /opt/backup.sh #优先级最低
1.3 设置程序运行时的优先级
对于已经在运行的程序,如果需要调整优先级,需要用renice命令,设置正在运行程序nice的值为15:
# renice -n +15 5319
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
5 S freeoa 5319 1607 -5 95 15 - 119325 ep_pol Mar23 ? 00:03:55 /usr/sbin/httpd
root用户可以为其它用户或组设置nice值:
# renice -n +15 -u freeoa #设置freeoa用户的所有进程nice值为15
还可以为进程组设置nice值,查看进程组:
# ps -efj
UID PID PPID PGID SID C STIME TTY TIME CMD
freeoa 5296 1607 1607 1607 0 Mar23 ? 00:03:54 /usr/sbin/httpd
freeoa 5319 1607 1607 1607 0 Mar23 ? 00:03:55 /usr/sbin/httpd
freeoa 5394 1607 1607 1607 0 Mar23 ? 00:03:55 /usr/sbin/httpd
apache的进程组(PGID)为:1607,设置nice值为-8:
# renice -n -8 -g 1607
命令用法:nice [选项] [命令 [参数]...]
以指定的优先级运行命令,这会影响相应进程的调度。如果不指定命令,程序会显示当前的优先级。优先级的范围是从 -20(最大优先级) 到 19 (最小优先级)。-n, --adjustment=N 对优先级数值加上指定整数N(默认为10)
--help 显示此帮助信息并退出
--version 显示版本信息并退出
注意:shell内含自己的nice 程序版本,它会覆盖这里所提及的相应版本,请查阅您的shell 文档获知它所支持的选项。
2、ionice
Linux默认IO调度器使用CFQ调度算法,支持用ionice命令为程序指定io调度策略和优先级,IO调度策略分为三种:
Idle:其他进程没有磁盘IO时,才进行磁盘IO
Best Effort:缺省调度策略,可以设置0-7的优先级,数值越小优先级越高,同优先级的进程采用round-robin算法调度
Real Time:立即访问磁盘,无视其它进程IO
None 即 Best Effort,进程未指定策略和优先级时显示为none,会使用依据cpu nice设置计算出优先级
策略:0 - none, 1 - Real Time, 2 - Best Effort, 3 - idle
ionice command provide more control as compare to nice command. This program sets the io scheduling class and priority for 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.
Linux refers the scheduling class using following number system and priorities:
Scheduling class | Number | Possible priority |
real time | 1 | 8 priority levels are defined denoting how big a time slice a given process will receive on each scheduling window |
best-effort | 2 | 0-7, with lower number being higher priority |
idle | 3 | Nil ( 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 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 or top or your favorite monitoring tool:
# atop
cronjob:
@weekly /usr/bin/ionice -c2 -n7 /root/scripts/backup.full >/dev/null 2>&1
You can set process with PID 1004 as an idle io process, enter:
# ionice -c3 -p 1004
Runs rsync.sh script as a best-effort program with highest priority, enter:
# ionice -c2 -n0 /path/to/rsync.sh
Finally, you can combine both nice and ionice together:
# nice -n 19 ionice -c2 -n7 /path/to/shell.script
使用idle策略:
ionice -c 3 -p pid
使用Real Time策略:
ionice -c 1 -p pid
使用Best Effort策略,并指定优先级最低:
ionice -c 2 -n 7 -p pid
ionice 是一个可以对另一个程序设置或获取 I/O 调度级别和优先级的有用程序。如果没有给出参数或者只有 -p 参数,那么 ionice 将会查询该进程的当前的 I/O 调度级别以及优先级。
如果我们给出命令名称,如rm命令,它将使用给定的参数运行此命令。要获取或设置调度参数,请指定[进程的 PID],如下:
# ionice -p PID
要指定名字或者调度的数字,使用(0 表示无、1 表示实时、2 表示尽力、3 表示空闲)下面的命令。以下命令表示rm会属于空闲 I/O 级别,并且只在其他进程不使用的时候使用 I/O:
---- Deleting Huge Files in Linux -----
# ionice -c 3 rm /var/logs/syslog
# ionice -c 3 rm -rf /var/log/apache
如果系统中没有很多空闲时间,那么我们希望使用尽力调度级别,并且使用低优先级:
# ionice -c 2 -n 6 rm /var/logs/syslog
# ionice -c 2 -n 6 rm -rf /var/log/apache
命令功能:ionice – 获取或设置程序的IO调度与优先级。
命令格式:
ionice [[-c class] [-n classdata] [-t]] -p PID [PID]…
ionice [-c class] [-n classdata] [-t] COMMAND [ARG]…
IO调度策略:
ionice将磁盘IO调度分为三类:
ilde:空闲磁盘调度,该调度策略是在当前系统没有其他进程需要进行磁盘IO时,才能进行磁盘;因此该策略对当前系统的影响基本为0;当然该调度策略不能带有任何优先级参数;目前,普通用户是可以使用该调度策略(自从内核2.6.25开始)。
Best effort:是缺省的磁盘IO调度策略:
(1)该调度策略可以指定优先级参数(范围是0~7,数值越小,优先级越高)
(2)针对处于同一优先级的程序将采round-robin方式
(3)对于best effort调度策略,8个优先级等级可以说明在给定的一个调度窗口中时间片的大小
(4)目前,普调用户(非root用户)是可以使用该调度策略
(5)在内核2.6.26之前,没有设置IO优先级的进程会使用"none"作为调度策略,但是这种策略使得进程看起来像是采用了best effort调度策略,因为其优先级是通过关于cpu nice有关的公式计算得到的:io_priority = (cpu_nice + 20) /5
(6)在内核2.6.26之后,如果当前系统使用的是CFQ调度器,那么如果进程没有设置IO优先级级别,将采用与内核2.6.26之前版本同样的方式,推到出io优先级级别
Real time:实时调度策略,如果设置了该磁盘IO调度策略,则立即访问磁盘,不管系统中其他进程是否有IO。因此使用实时调度策略,需要注意的是,该访问策略可能会使得其他进程处于等待状态。
参数说明:
-c class:class表示调度策略,其中0 for none, 1 for real time, 2 for best-effort, 3 for idle。
-n classdata:classdata表示IO优先级级别,对于best effort和real time,classdata可以设置为0~7。
-p pid:指定要查看或设置的进程号或者线程号,如果没有指定pid参数,ionice will run the listed program with the given parameters。
-t:忽视设置优先级时产生的错误。
3、同时限制cpu和磁盘io的优先级
为了对生产环境造成影响最小,设置备份脚本运行时,cpu和磁盘io的优先级都最低:
/usr/bin/nice -n 19 /usr/bin/ionice -c2 -n7 /bin/sh /opt/backup.sh
对于一些运行时会造成系统满载的脚本,例如数据库备份会影响当时其他服务的响应速度,可以通过ionice和nice对其IO优先级和CPU优先级进行调整,例如降低"/usr/local/bin/backup.sh"的IO优先级,让其他进程顺畅运行:
/usr/bin/ionice -c2 -n7 /usr/local/bin/backup.sh
其中:
-c: scheduling class, 0: none, 1: realtime, 2: best-effort, 3: idle
-n: class data (0-7, lower being higher prio)
降低其CPU优先级, 可以通过:
/usr/bin/nice -n 19 /usr/local/bin/backup.sh
其中:-n, --adjustment=N add integer N to the niceness (default 10), nicenesses range from -20 (most favorable scheduling) to 19 (least favorable).
nice和ionice可以一起使用,例如:
/usr/bin/nice -n 19 /usr/bin/ionice -c2 -n7 /usr/local/bin/backup.sh
4、cpulimit
限制其对CPU的使用率,可使用cpulimit工具来实现。cpulimit 的使用方式和 nice 命令类似,但是需要用户使用 -l 选项显式地定义进程的 cpu 使用率上限值。
cpulimit --limit=10 /bin/gzip vzdump-2018_06_26.tar.gzip
cpulimit 命令的工作原理是为进程预设一个 cpu 占用率限制,并实时监控进程是否超出此限,若超出则让该进程暂停运行一段时间。cpulimit 使用 SIGSTOP 和 SIGCONT 这两个信号来控制进程,它不会修改进程的 nice 值,而是通过监控进程的 cpu 占用率来做出动态调整。cpulimit 的优势是可以控制进程的cpu使用率的上限值。但与 nice 相比也有缺点,那就是即使 cpu 是空闲的,进程也不能完全使用整个 cpu 资源。
cpulimit 还可以在运行时对进程进行动态限制,使用 -p 选项来指定进程的 PID,下面是一个实例:
cpulimit -l 50 -p 1237
其中1237是进程的 PID。
cpulimit工具通过以不同的时间间隔来暂停/继续进程的运行状态来控制进程的CPU使用率,以将其保持在所定义的上限之下。
tar -c myData | gzip -c > myData.gz & cpulimit -l 10 -e gzip
5、cgroups
功能最为强大的目前当数控制组(cgroups)。它是 Linux 内核提供的一种机制,利用它可以指定一组进程的资源分配。具体来说,使用 cgroups,用户能够限定一组进程的 cpu 占用率、系统内存消耗、网络带宽,以及这几种资源的组合。
对比nice和cpulimit,cgroups 的优势在于它可以控制一组进程,不像前者仅能控制单进程。nice 和 cpulimit 只能限制 cpu 使用率,而 cgroups 则可以限制其他进程资源的使用。对 cgroups 善加利用就可以控制好整个子系统的资源消耗。就拿 CoreOS 作为例子,这是一个专为大规模服务器部署而设计的最简化的 Linux 发行版本,它的 upgrade 进程就是使用 cgroups 来管控,这样系统在下载和安装升级版本时也不会影响到系统的性能。
下面做一下演示,我们将创建两个控制组(cgroups),并对其分配不同的 cpu 资源。这两个控制组分别命名为'cpulimited'和'lesscpulimited'。这里使用了Mathomatic toolkit中的质数生成器来模拟CPU负载。
使用 cgcreate 命令来创建控制组,如下所示:
cgcreate -g cpu:/cpulimited
cgcreate -g cpu:/lesscpulimited
其中'-g cpu'选项用于设定 cpu 的使用上限。除 cpu 外,cgroups 还提供 cpuset、memory、blkio 等控制器。cpuset 控制器与 cpu 控制器的不同在于:cpu 控制器只能限制一个 cpu 核的使用率,而 cpuset 可以控制多个 cpu 核。
cpu 控制器中的 cpu.shares 属性用于控制 cpu 使用率,它的默认值是 1024,我们将 lesscpulimited 控制组的 cpu.shares 设为1024(默认值),而 cpulimited 设为512,配置后内核就会按照2:1的比例为这两个控制组分配资源。
要设置 cpulimited 组的 cpu.shares 为 512,输入以下命令:
cgset -r cpu.shares=512 cpulimited
使用 cgexec 命令来启动控制组的运行,为了测试这两个控制组,先用cpulimited 控制组来启动 matho-primes 进程,命令行如下:
cgexec -g cpu:cpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null &
打开 top 可以看到,matho-primes 进程占用了所有的 cpu 资源。
因为只有一个进程在系统中运行,不管将其放到哪个控制组中启动,它都会尽可能多的使用cpu资源。cpu资源限制只有在两个进程争夺cpu资源时才会生效,那么现在我们就启动第二个 matho-primes 进程,这一次在 lesscpulimited 控制组中来启动它:
cgexec -g cpu:lesscpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null &
再打开 top 就可以看到,cpu.shares 值大的控制组会得到更多的 cpu 运行时间。
现在我们再在 cpulimited 控制组中增加一个 matho-primes 进程:
cgexec -g cpu:cpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null &
可以看到两个控制组的 cpu 的占用率比例仍然为2:1。其中cpulimited控制组中的两个 matho-primes 进程获得的cpu时间基本相当,而另一组(lesscpulimited)中的matho-primes进程显然获得了更多的运行时间片,它所占用的时间片大致为前者之和。
6、参考来源
linux的cpu和磁盘io优先级设置
使用 nice、cpulimit 和 cgroups 限制 cpu 占用率
7、制造与查看系统负载
System Load View and Maker in Linux
用于查看系统负载的相关命令工具
uptime,vmstat,top,mpstat,Glances
系统负载制造程序一览
GNU coreutils 工具集中有一个yes指令,直接运行它它能不断的每行输出字母'y',且其所在的cpu核也会近似于满负载。
用法:yes [字符串]...
或:yes 选项
Repeatedly output a line with all specified STRING(s), or 'y'.
--help 显示此帮助信息并退出
--version 显示版本信息并退出
可查看在线帮助。
将其放入后台一直运行,制造一个核心的使用率。
$ yes > /dev/null &
yes指令是单进程的,它可以在单核上制造100%的cpu使用率,多核环境下可以开N个进程实例,理论上可以制造N倍于核心的负载(N必须小于核心数量)。可以使用killall来中止这些yes指令进程。
$ killall yes
同样还可以使用dd命令实现同样的效果
to produce 100% CPU load usage is:
$ dd if=/dev/zero of=/dev/null
当然还是stress、stress-ng工具比较直接可操作性好,在4个核心上为每核心制造约50%的负载使用率
stress-ng --cpu 4 --cpu-load 50
使用top查看负载总体情况。
使用mpstat查看负载总体情况。
mpstat to Display CPU Activity
The first line is a set of column labels. The second line is the value for each column:
%usr – % CPU usage at the user level
%nice – % CPU usage for user processes labeled “nice”
%sys – % CPU usage at the system (Linux kernel) level
%iowait – % CPU usage idling waiting on a disk read/write
%irq – % CPU usage handling hardware interrupts
%soft – % CPU usage handing software interrupts
%steal – % CPU usage being forced to wait for a hypervisor handling other virtual processors
%guest – % CPU usage spent running a virtual processor
%idle – % CPU usage on idle time (no processes, and not waiting on a disk read/write)
图形化工具(Graphical Utility)
graphical monitoring tool:gnome-system-monitor
三大资源使用限制工具比较
nice是对系统进行一次性调整的好工具。
当需要运行CPU密集型作业时,cpulimit非常有用,而空闲的CPU时间对于系统的响应性至关重要。
cgroups是瑞士军刀级的限制方案,提供最大的灵活性。