📝
Linux ps / top の pri, priority (メモ)
Twitter で Linux ps コマンドの PRI (pri) と NI (nice)値が矛盾しているかも!?という事に気付いたのでメモ。
結論としては
-
psにはPRIで表示される値にpriとpriorityがあるpri = 19 - nicepriority = 20+ nice
-
man psの説明は矛盾しているので修正された:https://man7.org/linux/man-pages/man1/ps.1.html
背景
-
man niceでは NI (nice) は値が大きいほうが優先度低いと記載(-20 から 19) -
man psでは PRI (pri) も値が大きいほうが優先度低いと記載 (Linux 5.4.0-109-generic) - しかし、
ps -o 'ni,pri'を比較すると、pri = 19 - NI (nice)となっており "nice が大きいと pri が低くなる" という矛盾があった - 実は
ps -o 'priority'というフィールドもあり、priority = 20 + NI (nice)で、こちらは "NI & PRI どちらも値が大きい方が優先度低い" と合致する -
pri,priorityはそれぞれ/proc/<PID>/statの異なるフィールドを参照している -
man psの説明は矛盾しているので修正された - なお、
topコマンドのPRはpriority (20 + nice)を利用
man ps 出力
>> Linux 5.4.0-109-generic
> man nice
Niceness values range from -20 (most favorable to the process) to 19 (least favorable to the process).
> man ps
pri PRI priority of the process. Higher number means lower priority.
ps 出力
$ ps axww -T -o 'pid,tid,ni,pri,priority,pcpu,cmd' | head
PID TID NI PRI PRI %CPU CMD
1 1 0 19 20 0.0 /lib/systemd/systemd --system --deserialize 35
2 2 0 19 20 0.0 [kthreadd]
3 3 -20 39 0 0.0 [rcu_gp]
4 4 -20 39 0 0.0 [rcu_par_gp]
6 6 -20 39 0 0.0 [kworker/0:0H-kblockd]
10 10 -20 39 0 0.0 [mm_percpu_wq]
11 11 0 19 20 0.0 [ksoftirqd/0]
12 12 0 19 20 0.0 [rcu_sched]
13 13 - 139 -100 0.0 [migration/0]

Discussion