LinuxサーバのCPU使用率を確認する。

IT技術

はじめに

LinuxサーバのCPU使用率を確認する方法を記載します。
また、CPU使用率が高い場合、どのプロセスがCPUを使っているのか特定する方法も記載します。

CPU使用率の確認方法

vmstatコマンドの場合

[root@testserver ~]# vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0   5900  83624     12 300628    0    1    69    18   58   93  1  0 95  0  3
 0  0   5900  83672     12 300628    0    0     0     0   65   96  0  0 94  0  6
 0  0   5900  83672     12 300628    0    0     0     0   66  108  0  0 100  0  0
 0  0   5900  83640     12 300628    0    0     0     0   68   94  0  0 100  0  0
 0  0   5900  83640     12 300628    0    0     0     0   55   97  0  0 100  0  0
[root@testserver ~]#

引数1つ目が「何秒間隔で繰り返すか」、引数2つ目が「繰り返す回数」を設定しています。
上の場合だと、「1秒間隔で5回繰り返し」になっています。

id列はアイドル状態(CPUを使っていない)を表しています。
上の場合94〜100%となっているので、CPU使用率は低いと判断できます。

ちなみに、CPU使用率が高い状態だと下のようになります。

[root@testserver ~]# vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0   6924  93476     12 290960    0    1    58    16   61   89  1  0 95  0  3
 1  0   6924  93476     12 290960    0    0     0     0  898  110 24  1 50  0 26
 1  0   6924  93476     12 290960    0    0     0     0  973  151 24  0 49  0 26
 1  0   6924  93476     12 290960    0    0     0     0  912  122 24  0 50  0 26
 1  0   6924  93476     12 290960    0    0     0     0  866   99 23  0 48  0 29
[root@testserver ~]#

id列の値が48〜50%まで下がり、us列の値が23〜24%に上昇しています。
us列はユーザプロセスの使用率を表しています。
何らかのプロセスがCPUを使っているということですね。
(このプロセスの特定方法は後述します。)

sarコマンドの場合

sarコマンドも、vmstatコマンドと同様に確認できます。

[root@testserver ~]# sar -u 1 5
Linux 5.4.17-2102.202.5.el7uek.x86_64 (testserver)      07/03/2021      _x86_64_        (2 CPU)
02:21:08 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
02:21:09 AM     all      0.46      0.00      0.46      0.00      7.80     91.28
02:21:10 AM     all      0.00      0.00      0.00      0.00      0.00    100.00
02:21:11 AM     all      0.48      0.00      0.48      0.00      4.81     94.23
02:21:12 AM     all      0.50      0.00      0.00      0.00      0.00     99.50
02:21:13 AM     all      0.00      0.00      0.00      0.00      0.00    100.00
Average:        all      0.29      0.00      0.19      0.00      2.63     96.88
[root@testserver ~]#

どのプロセスがCPUを使っているか

topコマンドで、プロセスごとのCPU使用率が確認できます。

[root@testserver ~]# top -c
root     29726  4455  0 02:20 pts/0    00:00:00 grep --color=auto yes
top - 02:27:56 up  1:19,  1 user,  load average: 0.01, 0.10, 0.11
Tasks: 124 total,   2 running,  68 sleeping,   0 stopped,   0 zombie
%Cpu(s): 24.1 us,  0.5 sy,  0.0 ni, 49.6 id,  0.0 wa,  0.0 hi,  0.3 si, 25.5 st
KiB Mem :   703104 total,    91368 free,   318896 used,   292840 buff/cache
KiB Swap:  8388604 total,  8381680 free,     6924 used.   278572 avail Mem 
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                      
31506 root      20   0  109536    648    572 R  49.2  0.1   0:11.21 yes                          
    1 root      20   0  151704   8648   6356 S   0.3  1.2   0:02.73 /usr/lib/systemd/systemd --+ 
 2336 root      20   0 1447640 107612  19168 S   0.3 15.3   0:04.52 /usr/libexec/oracle-cloud-a+ 
 3427 opc       20   0  160964   4540   3180 S   0.3  0.6   0:00.04 sshd: opc@pts/0              
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 [kthreadd]                   
    3 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 [rcu_gp]                     
    4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 [rcu_par_gp]                 
〜〜〜省略〜〜〜
[root@testserver ~]# 

上からCPU使用率が高い順に並んでいます。
上の場合、「yes」プロセスが最もCPUを使っているプロセスというわけです。

-cオプションを使うと、COMMAND列がフルパス表示になって分かりやすいので、筆者は基本的に使用しています。

タイトルとURLをコピーしました