LoginSignup
5
5

More than 5 years have passed since last update.

chrtコマンドの使い方

Last updated at Posted at 2018-05-20

1 chrt コマンドとは?

リアルタイムスケジューリングの属性(ポリシー,優先度)の表示/変更をするコマンドです。

2 環境

VMware Workstation 14 Playerのゲストマシン(1台)を使いました。

2.1 ゲストマシン

OS版数
[root@centos74 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

[root@centos74 ~]# uname -r
3.10.0-693.el7.x86_64

ゲストマシンが搭載するCPUの数は4個です。

搭載CPU数
[root@centos74 ~]# lscpu -a --extended
CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE
0   0    0      0    0:0:0:0       yes
1   0    1      1    1:1:1:1       yes
2   0    2      2    2:2:2:2       yes
3   0    3      3    3:3:3:3       yes

3 インストール方法

[root@centos74 ~]# yum -y install util-linux
[root@centos74 ~]# chrt -V
chrt from util-linux 2.23.2

4 オプション一覧

[root@centos74 ~]# 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).

5 スケジューリングポリシー

ここで使うスケジューリングポリシーについて簡単に説明をします。

SCHED_FIFOとSCHED_RRはともにリアルタイムスケジューリングです。
「リアルタイムスケジューリング」は、OS外部からの入力に対して、応答時間を保証します。

両者の違いは、SCHED_RRはタイムスライスを持っている、ということです。
つまり、タイムスライスを使い切ると、次のプロセスにCPUを明け渡します。
一方、SCHED_FIFOのプロセスは、CPUを獲得すると、次の条件が成立するまで動作し続けます。
・自発的にCPUを手放す
・プロセスが終了する
・自分より優先度の高いプロセスがCPUをプリエンプトする

SCHED_OTHERは、時分割スケジューリングです。
リアルタイムスケジューリングに比べ、低優先度で実行されます。

6 優先度の最大/最小値を表示する方法(-m)

優先度の最大/最小値を表示します。
たとえば、SCHED_FIFO,SCHED_RRでは、1の優先度が最も高く、99の優先度が最も低くなります。
優先度が高いプロセスは、低いプロセスに比べ、CPUへの割り当てが優先されます。

[root@centos74 ~]# chrt -m
SCHED_OTHER min/max priority    : 0/0
SCHED_FIFO min/max priority     : 1/99
SCHED_RR min/max priority       : 1/99
SCHED_BATCH min/max priority    : 0/0
SCHED_IDLE min/max priority     : 0/0
SCHED_DEADLINE min/max priority : 0/0

7 スケジューリングポリシー、優先度を指定してプロセスを実行する方法

7.1 SCHED_FIFO(優先度1)のプロセスを実行する方法

実行例
優先度1のSCHED_FIFOとしてstress-ngプロセスを起動する。
[root@centos74 ~]# chrt -f 1 stress-ng -c 1 -kq&
[1] 6156

プロセスの状態を確認する。stress-ngプロセスは、優先度1のSCHED_FIFOとして動作していることがわかる。
[root@centos74 ~]# ps -C stress-ng -o comm,pid,ppid,cls,rtprio,%cpu
COMMAND            PID   PPID CLS RTPRIO %CPU
stress-ng         6156   1788  FF      1  0.0
stress-ng         6158   6156  FF      1 93.8
後始末
[root@centos74 ~]# jobs
[1]+  実行中               chrt -f 1 stress-ng -c 1 -kq &
[root@centos74 ~]# kill %1
[root@centos74 ~]#
[1]+  終了                  chrt -f 1 stress-ng -c 1 -kq

7.2 SCHED_RR(優先度99)のプロセスを実行する方法

実行例
優先度99のSCHED_RRとしてstress-ngプロセスを起動する。
[root@centos74 ~]# chrt -r 99 stress-ng -c 1 -kq&
[1] 6370

プロセスの状態を確認する。stress-ngプロセスは、優先度99のSCHED_RRとして動作していることがわかる。
[root@centos74 ~]# ps -C stress-ng -o comm,pid,ppid,cls,rtprio,%cpu
COMMAND            PID   PPID CLS RTPRIO %CPU
stress-ng         6370   1788  RR     99  0.1
stress-ng         6371   6370  RR     99 95.4
後始末
[root@centos74 ~]# jobs
[1]+  実行中               chrt -r 99 stress-ng -c 1 -kq &
[root@centos74 ~]# kill %1
[root@centos74 ~]#
[1]+  終了                  chrt -r 99 stress-ng -c 1 -kq

7.3 SCHED_OTHER(NICE値=0)のプロセスを実行する方法

実行例
SCHED_OTHER(NICE値=0)としてstress-ngプロセスを起動する。
[root@centos74 ~]# chrt -o 0 stress-ng -c 1 -kq&
[1] 7590

プロセスの状態を確認する。stress-ngプロセスは、NICE値0のSCHED_RRとして動作していることがわかる。
[root@centos74 ~]# ps -C stress-ng -o comm,pid,ppid,cls,rtprio,nice,%cpu
COMMAND            PID   PPID CLS RTPRIO  NI %CPU
stress-ng         7590   2589  TS      -   0  0.0
stress-ng         7591   7590  TS      -   0  100
後始末
[root@centos74 ~]# jobs
[1]+  実行中               chrt -o 0 stress-ng -c 1 -kq &
[root@centos74 ~]# kill %1
[root@centos74 ~]#
[1]+  終了                  chrt -o 0 stress-ng -c 1 -kq

7.4 親プロセスとは異なるポリシーで子プロセスを動作させる方法(-R)

SCHED_FIFO,SCHED_RRで動作しているプロセスがfork(2)で子プロセスを起動する際、
子プロセスのスケジューリングポリシーをSCHED_OTHER(特権が不要)で起動します。

実行例
[root@centos74 ~]# chrt -R -f 1 stress-ng -c 1 -kq&
[1] 8667

プロセスの状態を確認する。
親プロセスはSCHED_FIFO、子プロセスはSCHED_OTHERで動作していることがわかる。
[root@centos74 ~]# ps -C stress-ng -o comm,pid,ppid,cls,rtprio,nice,%cpu
COMMAND            PID   PPID CLS RTPRIO  NI %CPU
stress-ng         8667   2589  FF      1   -  0.0
stress-ng         8668   8667  TS      -   0 97.4
後始末
[root@centos74 ~]# jobs
[1]+  実行中               chrt -R -f 1 stress-ng -c 1 -kq &
[root@centos74 ~]# kill %1
[root@centos74 ~]#
[1]+  終了                  chrt -R -f 1 stress-ng -c 1 -kq

8 動作中プロセスの優先度、ポリシーを変更する方法

8.1 優先度を変更する方法

初期状態
[root@centos74 ~]# chrt -f 1 stress-ng -c 1 -kq&
[1] 4871

プロセスの状態を確認する。優先度が1(★)で動作していることがわかる。
[root@centos74 ~]# ps -C stress-ng -o comm,pid,ppid,cls,rtprio,nice,%cpu
COMMAND            PID   PPID CLS RTPRIO  NI %CPU
stress-ng         4871   4762  FF      1   -  0.1
stress-ng         4872   4871  FF    ★1   -  104
優先度変更後の状態
動作中プロセス(PID=4872)の優先度を99に変更する。
[root@centos74 ~]# chrt -f -p 99 4872

プロセスの状態を確認する。優先度が99(★)に変更されたことがわかる。
[root@centos74 ~]# ps -C stress-ng -o comm,pid,ppid,cls,rtprio,nice,%cpu
COMMAND            PID   PPID CLS RTPRIO  NI %CPU
stress-ng         4871   4762  FF      1   -  0.0
stress-ng         4872   4871  FF   ★99   - 98.4

9 その他

ここでは、SCHED_OTHERで起動したプロセスのNICE値を変更してみます。
動作中プロセスのNICE値の変更は、reniceコマンドを使います。
nice値は -20〜19の数値をとります。-20が優先度が最も高く 19が最も低い優先度になります。

プロセスの起動(NICE値を0で起動)
[root@centos74 ~]# chrt -o 0 stress-ng -c 1 -kq&
[1] 12225

プロセスの状態を確認する。NICE値が0であることがわかる。
[root@centos74 ~]# ps -C stress-ng -o comm,pid,ppid,cls,rtprio,nice,%cpu
COMMAND            PID   PPID CLS RTPRIO  NI %CPU
stress-ng        12225   2589  TS      -   0  0.3
stress-ng        12227  12225  TS      -   0  103
優先度の変更(NICE値を-20に変更)
NICE値を-20に変更する。
[root@centos74 ~]# renice -n -20 -p 12227
12227 (process ID) old priority 0, new priority -20

プロセスの状態を確認する。NICE値が-20に変更されたことがわかる。
[root@centos74 ~]# ps -C stress-ng -o comm,pid,ppid,cls,rtprio,nice,%cpu
COMMAND            PID   PPID CLS RTPRIO  NI %CPU
stress-ng        12225   2589  TS      -   0  0.0
stress-ng        12227  12225  TS      - -20  101
優先度の変更(NICE値を19に変更)
NICE値を19に変更する。
[root@centos74 ~]# renice -n 19 -p 12227
12227 (process ID) old priority -20, new priority 19

プロセスの状態を確認する。NICE値が19に変更されたことがわかる。
[root@centos74 ~]# ps -C stress-ng -o comm,pid,ppid,cls,rtprio,nice,%cpu
COMMAND            PID   PPID CLS RTPRIO  NI %CPU
stress-ng        12225   2589  TS      -   0  0.0
stress-ng        12227  12225  TS      -  19 99.7

Z 参考情報

Research and evaluation of SCHED_DEADLINE
4.2. CPU のスケジューリング

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5