目录
一、进程优先级和nice的概念
二、显示nice级别
三、更改nice级别
一、进程优先级和nice的概念
CPU管理进程的是按照时间片的方式来划分CPU的资源。CPU可以把1s划分成若干份,轮询时间片。通过设置进程的优先级,让某些重要的进程优先并且长时间获取CPU资源。
手段是设置进程的优先级的方式,进程尽可能多获取CPU的资源,分为静态优先级和动态优先级。
- 静态优先级:-20到19 共40个优先级。默认的,进程继承其父进程的nice级别,通常为0。nice值越高,优先级越低;nice值越低,优先级越高。
- 动态优先级:1-99
静态优先级
- 只有root用户才能设置负nice级别,或者降低现有进程的nice,来提高优先级。
- 普通用户只能设置正nice级别,只能提升现有进程的nice,也就是只能降低优先级不能提高优先级。
查看cpu信息用lscpu。
[[email protected] ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 1
On-line CPU(s) list: 0
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 13
Model name: QEMU Virtual CPU version 1.5.3
Stepping: 3
CPU MHz: 2394.454
BogoMIPS: 4788.90
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 4096K
NUMA node0 CPU(s): 0
二、显示nice级别
top命令显示与nice级别有关的两列。 NI 列显示实际的nice级别,也就是-20到19。PR列将nice级别映射到0到39。
top - 09:50:36 up 3 min, 3 users, load average: 0.14, 0.22, 0.11
Tasks: 122 total, 2 running, 120 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
KiB Mem: 1885296 total, 520472 used, 1364824 free, 688 buffers
KiB Swap: 0 total, 0 used, 0 free. 215100 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
260 root 20 0 0 0 0 S 0.3 0.0 0:00.47 xfsaild/vda1
1 root 20 0 52328 6480 3664 S 0.0 0.3 0:04.20 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.08 ksoftirqd/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.35 kworker/0:0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
6 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/u2:0
ps命令默认不显示nice级别,可以通过指定选项查看nice级别。比如ps axo pid,comm,nice --sort=nice,输出pid、名字、nice级别,并按照nice级别升序(优先级高的排前面)排列。
[[email protected] ~]# ps axo pid,comm,nice --sort=nice
PID COMMAND NI
5 kworker/0:0H -20
13 khelper -20
15 netns -20
16 writeback -20
17 kintegrityd -20
18 bioset -20
……
三、更改nice级别
启动时设置进程优先级。nice -n <priority_level> <command>。比如,以nice=15执行top(top默认nice=0)。
[[email protected] ~]# nice -n 15 top
top - 10:00:22 up 13 min, 3 users, load average: 0.00, 0.04, 0.07
Tasks: 120 total, 1 running, 119 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 1885296 total, 563488 used, 1321808 free, 688 buffers
KiB Swap: 0 total, 0 used, 0 free. 244620 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1728 root 20 0 0 0 0 S 0.3 0.0 0:00.27 kworker/0:2
1763 root 35 15 123628 1488 1040 R 0.3 0.1 0:00.11 top
更改现有进程的nice级别,结合ps、top去查看PID。renice -n <priority_level> <PID>。比如把上面那个top的nice改成-2(上面那个PID=1763)。
[[email protected] ~]# renice -n -2 1763
1763 (process ID) old priority 15, new priority -2
——————————————————————————————————————————————————————————————————————————
top - 10:01:23 up 14 min, 3 users, load average: 0.00, 0.04, 0.07
Tasks: 121 total, 2 running, 119 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 1885296 total, 564120 used, 1321176 free, 688 buffers
KiB Swap: 0 total, 0 used, 0 free. 244760 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11 root 20 0 0 0 0 R 0.3 0.0 0:02.02 rcuos/0
260 root 20 0 0 0 0 S 0.3 0.0 0:01.19 xfsaild/vda1
1728 root 20 0 0 0 0 S 0.3 0.0 0:00.37 kworker/0:2
1763 root 18 -2 123628 1488 1040 R 0.3 0.1 0:00.29 top
1 root 20 0 52328 6480 3664 S 0.0 0.3 0:04.25 systemd