Wednesday, January 11, 2017

Understanding Linux vmstat command

vmstat is a linux/unix performance analysis tool. It gives you information about the processors, memory and possible bottlenecks. Lets see how to use and what the output indicates.

usage:
[root@main ~]# 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 11932960 377768 203520 64211448    1    2   108   117    0    0  0  1 98  1  0   
[root@main ~]#

If you give vmstart with interval as first argument and number of repetitions as second, output will be like as below.
[root@main ~]# 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 11932564 433468 203528 64211756    1    2   108   117    0    0  0  1 98  1  0   
 0  0 11932564 433576 203528 64211852    0    0     0   112 1634 3199  0  0 99  1  0   
 0  0 11932564 433576 203528 64211856    0    0     0     0 2148 3216  0  0 100  0  0   
 0  0 11932564 433576 203528 64211856    0    0     0     0 1810 3100  0  0 100  0  0   
 0  0 11932564 433576 203528 64211856    0    0     0     0 1415 3014  0  0 100  0  0   
[root@main ~]#

If you give only interval, it will give output in specified interval for infinate times.
[root@main ~]# vmstat 1
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 11932328 433096 203536 64212164    1    2   108   117    0    0  0  1 98  1  0   
 0  0 11932328 432948 203536 64212164    0    0     0    32 1962 3203  0  0 99  1  0   
 0  0 11932328 433072 203536 64212164    0    0     0     0 1408 2983  0  0 100  0  0   
 0  0 11932328 432824 203536 64212164    0    0     0     0 1731 2987  0  0 100  0  0   
 0  0 11932328 432824 203536 64212164    0    0     0     0 1407 2931  0  0 100  0  0   
 1  0 11932308 432824 203536 64212184    0    0     0    76 1925 3145  0  0 99  0  0   
 0  0 11932308 432912 203536 64212192    0    0     0     0 1461 2996  0  0 100  0  0   
 0  0 11932308 432912 203536 64212192    0    0     0     0 1807 3073  0  0 100  0  0   
 0  0 11932308 432536 203536 64212192    0    0     0     0 1597 2941  0  0 100  0  0   
 0  0 11932308 432520 203536 64212192    0    0     0     0 1640 3012  0  0 99  0  0   
^C
[root@main ~]#

Fields explained as follows:
Procs:
r : The number of processes waiting for run time or placed in run queue or are already executing.
b : The number of processes in uninterruptible sleep. (b=blocked queue, waiting for resource (e.g. filesystem I/O blocked, inode lock))
If runnable threads (r) divided by the number of CPU is greater than one -> possible CPU bottleneck

Memory:
swpd: shows how many blocks are swapped out to disk (paged). The amount of Virtual memory used.
free: The amount of Idle Memory
buff: Memory used as buffers, like before/after I/O operations
cache: Memory used as cache by the Operating System

Swap:
si: Amount of memory swapped in from disk (/s). This shows page-ins
so: Amount of memory swapped to disk (/s). This shows page-outs. The so column is zero consistently, indicating there are no page-outs.
In Ideal condition, si and so should be at 0 most of the time, and we definitely don’t like to see more than 10 blocks per second.

IO:
bi: Blocks received from block device
bo: Blocks sent to a block device

System:
in: The number of interrupts per second, including the clock.
cs: The number of context switches per second.
A context switch occurs when the currently running thread is different from the previously running thread.

CPU:
us: % of CPU time spent in user mode (not using kernel code, not able to access to kernel resources). 
sy: % of CPU time spent running kernel code. (system time)
id: % of CPU  idle time
wa: % of CPU time spent waiting for IO.