一文辨析,性能分析 top 命令中进程 NI 和 PR

本贴最后更新于 977 天前,其中的信息可能已经时过境迁

分析linux服务器性能,首先想到的命令肯定是 top, 通过它,我们可以看到当前服务器资源使用情况和进程运行资源占用情况.

Snipaste20220225162154.png

在查看进程资源占用情况时,有两列大家是最难区分,PR(priority)进程优先级和NI(nice)优先级切换等级,都是优先级,有什么区别呢?

如果,仔细观察,还会发现PR列的值是: rt 或大于等于0的数字; NI列的值是:[-20, 19]之间的数字,这些又代表什么意思呢?

因为这些问题搞不清,所以这两列很难理解,下面,我就来给大家说说:

首先,还是老规矩,在不知道linux命令的结果信息是什么意思的时候,可以查看命令的帮助信息,info top 查看帮助

Snipaste20220225173244.png

NI  --  Nice Value
           The  nice  value  of  the  task.   A negative nice value means
           higher priority, whereas a positive  nice  value  means  lower
           priority.   Zero  in this field simply means priority will not
           be adjusted in determining a task's dispatch-ability.
           这项任务的优先价值。负的nice值意味着更高的优先级,而正的nice值意味着更低的优先级。
           该字段中的0表示在确定任务的调度能力时不会调整优先级。

Snipaste20220225173346.png

PR  --  Priority 优先事项
           The scheduling priority of the task.  If you see `rt' in  this
           field, it means the task is running under real time scheduling
           priority.

           Under linux, real time priority is somewhat  misleading  since
           traditionally  the  operating itself was not preemptible.  And
           while the 2.6 kernel can be made mostly preemptible, it is not
           always so.
           任务的调度优先级。如果在该字段中看到“rt”,则表示任务正在实时调度优先级下运行。
   
           在linux下,实时优先级有些误导,因为传统上操作本身是不可抢占的。
	   虽然2.6内核基本上是可抢占的,但并不总是如此。

估计看了帮助,还是有些愣,接下来,我们就来讲解一下:

nice --help
Usage: nice [OPTION] [COMMAND [ARG]...]
Run COMMAND with an adjusted niceness, which affects process scheduling.
With no COMMAND, print the current niceness.  Niceness values range from
-20 (most favorable to the process) to 19 (least favorable to the process).

Mandatory arguments to long options are mandatory for short options too.
  -n, --adjustment=N   add integer N to the niceness (default 10)
      --help     display this help and exit
      --version  output version information and exit

NOTE: your shell may have its own version of nice, which usually supersedes
the version described here.  Please refer to your shell's documentation
for details about the options it supports.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'nice invocation'

nice -n 优先级整数 进程命令 可以在启动进程时指定进程用户态的优先级NI

renice --help

Usage:
 renice [-n] <priority> [-p|--pid] <pid>...
 renice [-n] <priority>  -g|--pgrp <pgid>...
 renice [-n] <priority>  -u|--user <user>...

Options:
 -g, --pgrp <id>        interpret argument as process group ID
 -n, --priority <num>   specify the nice increment value
 -p, --pid <id>         interpret argument as process ID (default)
 -u, --user <name|id>   interpret argument as username or user ID
 -h, --help             display help text and exit
 -V, --version          display version information and exit

For more information see renice(1).

renice -n 优先级整数 -p 进程id 重新更改进程的用户态优先级NI

注意: nice的范围是 [-20, 19], 所以在使用这些命令时,优先级整数范围别写错了。

nice和renice命令,可以修改进程的优先级,但是,再怎么改,这个进程还是普通任务进程,如果,你想把一个任务指定为实时任务进程,则使用 chrt命令

chrt --help
Show or change the real-time scheduling attributes of a process.

Set policy:
 chrt [options] <priority> <command> [<arg>...]
 chrt [options] --pid <priority> <pid>

Get policy:
 chrt [options] -p <pid>

Policy options:策略
 -b, --batch          set policy to SCHED_BATCH
 -d, --deadline       set policy to SCHED_DEADLINE
 -f, --fifo           set policy to SCHED_FIFO
 -i, --idle           set policy to SCHED_IDLE
 -o, --other          set policy to SCHED_OTHER
 -r, --rr             set policy to SCHED_RR (default)

Scheduling options:
 -R, --reset-on-fork       set SCHED_RESET_ON_FORK for FIFO or RR
 -T, --sched-runtime <ns>  runtime parameter for DEADLINE
 -P, --sched-period <ns>   period parameter for DEADLINE
 -D, --sched-deadline <ns> deadline parameter for DEADLINE

Other options:
 -a, --all-tasks      operate on all the tasks (threads) for a given pid
 -m, --max            show min and max valid priorities
 -p, --pid            operate on existing given pid
 -v, --verbose        display status information

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see chrt(1).

chrt -f -p 进程id 修改某个进程为实时任务进程,设置进程的调度策略为SCHED_FIFO,进程用户态优先级NI为某个整数

好了,现在对进程的NI和PR有没有进一步的认识了呢?希望,学有所获!

回帖
请输入回帖内容 ...