Tuesday, August 20, 2013

Resource Management : Vmstat

Vmstat is a command available in linux which displays information about the memory, swap and CPU utilization of the system in real time.

A Basic executions of vmstat gives,

LocalMach:root002-~ $ vmstat
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                 0   36570164 1584768 12768668 0     0           0      6            0   1         1 0  99 0    0

The data provided above was an average usage of system resources since last reboot. In the above output, there is
0 - nothing is swapped from disk
36570164 – amount of free system memory
1584768 - amount of system data that has yet to be flushed to disk.
12768668 - amount of data that has been read from the disk

LocalMach:root002-~ $ 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
1 0           0 36442616 3303552 25479784    0  0         0   6           0  0             1 0 99 0   0

In the above output we asked vmstat to display information about the number of active and inactive pages. The amount of inactive pages indicates how much of the memory could be swapped to disk and how much is currently being used. In this case, we can see that 33.3552 of memory is active, and only 25479784 is considered inactive

Often vmstat is used with an interval and a count like,

vmstat [interval] [count]

The above output provides you a lot of information like

procs Section
  • r field: Total number of runnable process. The More the load on system more number of process are waiting to get CPU
  • b field: Total number of blocked process or uninterruptable sleeping process. These processes are most likely waiting for a I/O operations or could be any other task. (should be close to Zero)
The value for these fields should be either ‘0’ or close to ‘0’.If there is a high value ,then the system don’t have enough resources like CPU, memory or I/O.

Memory section (in KB)
  • Swpd field: Used swap space
  • Free field: Available free RAM
  • Buff field: RAM used for buffers. This is used to store file metadata such as i-node and data from Raw block devices.
  • Cache field: RAM used for file system cache .This is used for the file data itself.
Swap Section (in KB/Sec)
  • Si field: Amount of memory swapped from disk per second
  • So field: Amount of memory swapped to disk per second

The data under these fields show the amount of swapping going on. If it is more than we need to either reduce the memory demand or increase the Physical Ram.

For Web servers or Data base Servers its very bad, since a trip to the disk may often cause a Performance impact for transactions going.

I/O Section ( in Blocks/Sec)
  • Bi field: Blocks received from disk
  • Bo field: Blocks sent to disk.

This shows us the flow of data to and from the disks. if there are large number of process are blocking and high I/O , then there is an Issue with the I/O. bi and bo represent the data transfers between virtual memory and block devices. It depends on your workload

System Section ( Per Second)
  • In field: Number of interrupts per second.
  • Cs field: Number of context switches per second.

CPU Section
  • Us field: Time spend running user code. (non-kernel code , application specific code comes under this)
  • Sy field: Time spent running kernel code such as root process.
  • Id field: Idle time.
  • Wa field: Time spent waiting for the IO

The information is very important in identifying the information about the CPU. We have one more command ‘top’ which gives the same information. The ‘top’ command gives information about single CPU ,But vmstat gives information about all the CPU.

We can also use vmstat like ,

LocalMach:root002-~ $ vmstat -a -n 10 3
procs -----------memory----------               ---swap-- -----io---- --system-- -----cpu------
r b           swpd free           inact     active       si so         bi bo        in cs        us sy id wa st
1 0               0 36551776 3229768 25445280    0 0           0 6          0 1            1 0 99 0 0
0 0               0 36551972 3229800 25445272    0 0           0 88    277 2602        1 0 99 0 0
0 0               0 36551848 3229856 25445340    0 0           0 100 1265 2545       0 0 100 0 0

The vmstat example above means we want it to delay 10 seconds before the next update and count the update 3 times. The '-n' option is used so that the header does not displayed in every update.

In Order to get the memory Details we can use,

LocalMach:root002-~ $ vmstat -s
65970668 total memory
29418976 used memory
25445424 active memory
3230004 inactive memory
36551692 free memory
1584872 buffer memory
12786880 swap cache
4194296 total swap
0 used swap
4194296 free swap
39488615 non-nice user cpu ticks
178225 nice user cpu ticks
3318192 system cpu ticks
2963059383 idle cpu ticks
41192 IO-wait cpu ticks
94896 IRQ cpu ticks
803239 softirq cpu ticks
0 stolen cpu ticks
10891932 pages paged in
193698804 pages paged out
0 pages swapped in
0 pages swapped out
2664606290 interrupts
723606365 CPU context switches
1346657309 boot time
24413327 forks

In the default operation, vmstat displays memory statistics in kilobytes. vmstat considers a single kilobyte equal to 1024 bytes. vmstat can also display reports with memory sizes reported in megabytes. vmstat reports with the argument "-S m" will consider a single megabyte equal to 1000 kilobytes as follows:

vmstat -S k 1 10
vmstat -S m 1 10

vmstat can provide information about how the Linux kernel allocates its memory. The Linux kernel has a series of "slabs" to hold its dynamic data structures. vmstat displays each of the slabs (Cache), shows how many of the elements are being used (Num), shows how many are allocated (Total), shows the size of each element (Size), and shows the amount of memory in pages (Pages) that the total slab is using.

LocalMach:root002-~ $ vmstat -S k
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           0 37423276 1622970 1...    0    0      0 6          0  1          1 0  99  0  0

The same information can be obtained in MB using, vmstat –S M.

LocalMach:root002-~ $ vmstat -f
24447726 forks

Resource Usage

localMach:root002-sa $ vmstat -m
Cache Num Total Size Pages
rpc_buffers 8 8 2048 2
rpc_tasks 8 10 384 10
rpc_inode_cache 6 10 768 5
ip_fib_alias 10 59 64 59
ip_fib_hash 10 59 64 59
iser_descriptors 0 0 128 30
ib_mad 0 0 448 8
fib6_nodes 5 59 64 59
ip6_dst_cache 4 24 320 12
ndisc_cache 1 15 256 15
RAWv6 7 8 960 4
UDPv6 6 8 896 4
tw_sock_TCPv6 0 0 192 20
request_sock_TCPv6 0 0 192 20

Disk Statistics

localMach:root002-sa $ vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
ram0 0 0 0 0 0 0 0 0 0 0
ram1 0 0 0 0 0 0 0 0 0 0
ram2 0 0 0 0 0 0 0 0 0 0
ram3 0 0 0 0 0 0 0 0 0 0
ram4 0 0 0 0 0 0 0 0 0 0
ram5 0 0 0 0 0 0 0 0 0 0
ram6 0 0 0 0 0 0 0 0 0 0
ram7 0 0 0 0 0 0 0 0 0 0
ram8 0 0 0 0 0 0 0 0 0 0
ram9 0 0 0 0 0 0 0 0 0 0
ram10 0 0 0 0 0 0 0 0 0 0
ram11 0 0 0 0 0 0 0 0 0 0
ram12 0 0 0 0 0 0 0 0 0 0
ram13 0 0 0 0 0 0 0 0 0 0
ram14 0 0 0 0 0 0 0 0 0 0


Disk Table
(! 1018)-> vmstat -D
31 disks
2 partitions
1139802 total reads
214276 merged reads
36688696 read sectors
8146064 milli reading
2168133 writes
742101 merged writes
23021306 written sectors
169686644 milli writing
0 inprogress IO
13502 milli spent IO

Happy learning , More To Come :-)