Linuxのリソース関連(vmstat)

お絵かき 技術 Linux

致命的な事実述べると、既に6年間以上IT業界に在籍しているのに、未だにリソース管理やキャパシティプランニングがよく分かっていない。うん。冗談ではない。
何故突然こんな事を言ったのかと言えば、LPIC Level2の勉強でも始めようかと思った際に、”vmstat”コマンドが出てきた時に、「このコマンドなんだっけ?」と素で考えてしまったからだ。
分からないことは調べて理解する。ついでにブログの記事にする。とても生産的だ。

vmstatについて

まずはいつも通りvmstatのマニュアルを表示してみます。

NAME
vmstat - Report virtual memory statistics
SYNOPSIS
vmstat [-a] [-n] [-t] [-S unit] [delay [ count]]
vmstat [-s] [-n] [-S unit]
vmstat [-m] [-n] [delay [ count]]
vmstat [-d] [-n] [delay [ count]]
vmstat [-p disk partition] [-n] [delay [ count]]
vmstat [-f]
vmstat [-V]
DESCRIPTION
vmstat reports information about processes, memory, paging, block IO, traps, and cpu activity.
The first report produced gives averages since the last reboot.  Additional reports give information on a sampling period of length delay.  The process and memory reports are instantaneous in
either case.
Options
The -a switch displays active/inactive memory, given a 2.5.41 kernel or better.
The -f switch displays the number of forks since boot.  This includes the fork, vfork, and clone system calls, and is equivalent to the total number of tasks created. Each process  is  repre-
sented by one or more tasks, depending on thread usage.  This display does not repeat.
The -t switch adds timestamp to the output.
The -m switch displays slabinfo.
The -n switch causes the header to be displayed only once rather than periodically.
The -s switch displays a table of various event counters and memory statistics. This display does not repeat.
delay is the delay between updates in seconds.  If no delay is specified, only one report is printed with the average values since boot.
count is the number of updates.  If no count is specified and delay is defined, count defaults to infinity.
The -d reports disk statistics (2.5.70 or above required)
The -w enlarges field width for big memory sizes
The -p followed by some partition name for detailed statistics (2.5.70 or above required)
The -S followed by k or K or m or M switches outputs between 1000, 1024, 1000000, or 1048576 bytes
The -V switch results in displaying version information.

なるほど、わからん。
とりあえず実行すると、負荷状況が確認出来るのだけは知っている。後、オプション”w”つけるとmemoryの表示が広くなるらしい。

# vmstat
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  11568 700784 196128 302588    0    0     0     3    1    0  0  0 100  0  0
# vmstat -w
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      11568     700800     196128     302588    0    0     0     3    1    0   0  0 100  0  0

リソース状況がなんとなく見れますね。各項目の説明は以下通り。雑な約なので意味合い違ったらすいません。

   Procs
r: 待ちプロセス
b: 割り込み不可でスリープになったプロセス
Memory
swpd: swap領域
free: 空き領域
buff: バッファメモリ
cache: キャッシュメモリ
Swap
si: スワップイン(/s).
so: スワップアウト (/s).
IO
bi: ブロックデバイスから受けっ取ったブロックの量(blocks/s).
bo: ブロックデバイスから送られたブロックの量 (blocks/s).
System
in: 割り込み処理の数
cs: コンテキストスイッチ(プログラムの実行切り替え)の回数
CPU
us: ユーザーが使用した時間
sy: システム(カーネル)が使用した時間
id: cpuが何もしてなかった時間
wa: IO待ち時間
st: (イマイチよくわからないのでスルー)

こんな項目がわかるみたいですね。正直私には理解出来ない箇所が多々あります。せめて、もちっと理解を深めるためにオプションつけて色々実行してみましょう。

自動更新と回数指定

例えば、10秒ごとに更新した値を3回まで表示する場合、こんな指定をします。※-nはヘッダファイルを最初しか表示しないオプションです。

vmstat -n -w 10 3
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      11568     700784     196128     302584    0    0     0     3    1    0   0  0 100  0  0
0  0      11568     700504     196128     302584    0    0     0    18  112  178   0  0  99  0  0
0  0      11568     700652     196128     302584    0    0     0     5   74  140   0  0 100  0  0

後ろの3が回数指定なので、これを取っ払うとずっと更新してくれます。

“-a”オプションと”-t”オプション

-aオプションを付与すると、キャッシュメモリの状態を表示してくれます。具体的には”inact”,”active”の2項目がbuff/cacheの代わりに表示されます。

vmstat -a
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa st
0  0  11568 700808 121932 959456    0    0     0     3    1    0  0  0 100  0  0

inactはすぐに開放出来るので、実質freeとinactの合計が空きメモリになります。

-tオプションは時間を付与してくれます。

# vmstat -t 3
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ---timestamp---
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
0  0  11568 700792 196128 302612    0    0     0     3    1    0  0  0 100  0  0       2016-11-25 14:39:42 JST
0  0  11568 700792 196128 302612    0    0     0     0   69  133  0  0 100  0  0       2016-11-25 14:39:45 JST
0  0  11568 700792 196128 302612    0    0     0     0   66  128  0  0 100  0  0       2016-11-25 14:39:48 JST

-sはメモリの統計表示を実施する。

# vmstat -s
1921992  total memory
1789476  used memory
615140  active memory
960936  inactive memory
132516  free memory
199768  buffer memory
1344048  swap cache
1048572  total swap
4632  used swap
1043940  free swap
317856 non-nice user cpu ticks
1267 nice user cpu ticks
121819 system cpu ticks
639891154 idle cpu ticks
49693 IO-wait cpu ticks
54 IRQ cpu ticks
1265 softirq cpu ticks
0 stolen cpu ticks
1757444 pages paged in
16035857 pages paged out
526 pages swapped in
1248 pages swapped out
102233773 interrupts
50613478 CPU context switches
1481077671 boot time
965999 forks 

-Sは後ろにつける「k,K,m,M」に応じて表示単位を変えます(小文字1000,大文字1024)

# vmstat -Sk
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   4743 135696 204562 1376305    0    0     0     3    3    1  0  0 100  0  0
# vmstat -SK
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   4632 132516 199768 1344048    0    0     0     3    3    1  0  0 100  0  0
# vmstat -Sm
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      4    135    204   1376    0    0     0     3    3    1  0  0 100  0  0
# vmstat -SM
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      4    129    195   1312    0    0     0     3    3    1  0  0 100  0  0

本題(今回はここまで)

ここまでコマンドとオプションを適当に紹介してきましたが、結局「こいつは結局どうやって動いているのか?」というのが全然分かりません。数字の結果だけバラバラと見せられてもいまいち納得出来なかったので、近々詳細を調べてみようと思います。

とりあえず本日はここまで。久しぶりの更新でした。

【プロフィール】

【無音鈴鹿】
お絵かきが好きなエンジニアの成れの果て

Twitter


【広告】



【広告】