ablog

不器用で落着きのない技術者のメモ

vmstat の sys は soft や irq も含む

Linux で mpstat だとカーネルモードで使用されたCPU使用率は sys、soft(ソフトウェア割込み)、irq(ハードウェア割込み) が別々に表示される。vmstat は sy だけなので、たぶん soft と irq も含まれるんだろうと思っていた。vmstat のソースを見るとやはり、sys + soft + irq の合計を sy として表示しているみたい。

上が vmstat で下が mpstat。

static void new_format(void) {
  const char format[]="%2u %2u %6lu %6lu %6lu %6lu %4u %4u %5u %5u %4u %4u %2u %2u %2u %2u\n";
  unsigned int tog=0; /* toggle switch for cleaner code */
  unsigned int i;
  unsigned int hz = Hertz;
  unsigned int running,blocked,dummy_1,dummy_2;
  jiff cpu_use[2], cpu_nic[2], cpu_sys[2], cpu_idl[2], cpu_iow[2], cpu_xxx[2], cpu_yyy[2], cpu_zzz[2];
  jiff duse, dsys, didl, diow, dstl, Div, divo2;
  unsigned long pgpgin[2], pgpgout[2], pswpin[2], pswpout[2];
  unsigned int intr[2], ctxt[2];
  unsigned int sleep_half; 
  unsigned long kb_per_page = sysconf(_SC_PAGESIZE) / 1024ul;
  int debt = 0;  // handle idle ticks running backwards

  sleep_half=(sleep_time/2);
  new_header();
  meminfo();

  getstat(cpu_use,cpu_nic,cpu_sys,cpu_idl,cpu_iow,cpu_xxx,cpu_yyy,cpu_zzz,
	  pgpgin,pgpgout,pswpin,pswpout,
	  intr,ctxt,
	  &running,&blocked,
	  &dummy_1, &dummy_2);

  duse= *cpu_use + *cpu_nic; 
  dsys= *cpu_sys + *cpu_xxx + *cpu_yyy; ★ココ
  didl= *cpu_idl;
  diow= *cpu_iow;
  dstl= *cpu_zzz;
  Div= duse+dsys+didl+diow+dstl;
  divo2= Div/2UL;
  printf(format,
	 running, blocked,
	 unitConvert(kb_swap_used), unitConvert(kb_main_free),
	 unitConvert(a_option?kb_inactive:kb_main_buffers),
	 unitConvert(a_option?kb_active:kb_main_cached),
	 (unsigned)( (*pswpin  * unitConvert(kb_per_page) * hz + divo2) / Div ),
	 (unsigned)( (*pswpout * unitConvert(kb_per_page) * hz + divo2) / Div ),
	 (unsigned)( (*pgpgin                * hz + divo2) / Div ),
	 (unsigned)( (*pgpgout               * hz + divo2) / Div ),
	 (unsigned)( (*intr                  * hz + divo2) / Div ),
	 (unsigned)( (*ctxt                  * hz + divo2) / Div ),
	 (unsigned)( (100*duse                    + divo2) / Div ),
	 (unsigned)( (100*dsys                    + divo2) / Div ),
	 (unsigned)( (100*didl                    + divo2) / Div ),
	 (unsigned)( (100*diow                    + divo2) / Div ) /* ,
	 (unsigned)( (100*dstl                    + divo2) / Div ) */
  );

vmstat は /proc/stat や /proc/vmstat を参照している。/proc/vmstat はメモリ関連の情報で、CPU使用率に関しては /proc/stat から取得しているはず。

$ strace -e open vmstat            
open("/etc/ld.so.cache", O_RDONLY)      = 3
open("/lib64/libproc-3.2.8.so", O_RDONLY) = 3
open("/lib64/libc.so.6", O_RDONLY)      = 3
open("/sys/devices/system/cpu/online", O_RDONLY|O_CLOEXEC) = 3
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
open("/proc/meminfo", O_RDONLY)         = 3
open("/proc/stat", O_RDONLY)            = 4 ★
open("/proc/vmstat", O_RDONLY)          = 50  0      0 6821880  90060 3629228    0    0   220    55  350 2657 10 28 62  0  0	

proc の man を見ると、Linux 2.6 から /proc/stat に iowait、irq、softirq 列が追加されたと書かれている。

$ man proc
/proc/stat
       kernel/system statistics.  Varies with architecture.  Common entries include:

cpu  3357 0 4313 1362393
The  amount of time, measured in units of USER_HZ (1/100ths of a second on most architectures, use sysconf(_SC_CLK_TCK) to obtain the right value), that the system spent in user mode, user mode with low priority (nice), system mode, and the idle task, respec-tively.  The last value should be USER_HZ times the second entry in the uptime pseudo-file.In  Linux  2.6  this line includes three additional columns: iowait - time waiting for I/O to complete (since 2.5.41); irq - time servicing interrupts (since 2.6.0-test4); softirq - time servicing softirqs (since 2.6.0-test4).

関連


追記(2014/10/07):
vmstat のソースを見ていて知らないオプションがいっぱいあることに気づいた(ソース見なくてもmanに書いてるんだけどw)。

  • タイムスタンプを表示する(-t)
$ vmstat -t
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ---timestamp---
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 6704388 239748 3797564    0    0   158    51 1634 1506 15 49 36  0  0	2014-10-07 05:54:28 JST
  • active/inactive メモリを表示する(-a) … 2.5.41 kernel 以降
$ 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      0 6713308 3491976 1247468    0    0   155    50 1629 1523 15 48 37  0  0	
  • slab情報を表示する(-m)
$ vmstat -m
Cache                       Num  Total   Size  Pages
nf_conntrack_ffff8801f273d540      0      0    304     13
nf_conntrack_ffff8801f2601540      0      0    304     13
pid_3                         2     20    192     20
pid_2                        32     60    128     30
fuse_request                  0      0    608      6
fuse_inode                    0      0    768      5
bridge_fdb_cache              0      0     64     59
以下略)
  • ヘッダを最初の1回だけ表示する(-n)
$ vmstat -n 1
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 6743848 241216 3798400    0    0   146    48 1612 1567 14 46 40  0  0	
 0  0      0 6735156 241216 3807348    0    0     0     0 4722 8343  3  8 89  0  0	
  • ディスク統計を表示する(-d) … 2.5.70以降
$ vmstat -d               
disk- ------------reads------------ ------------writes----------- -----IO------
       total merged sectors      ms  total merged sectors      ms    cur    sec
(中略)
sda   136520    942 7201465   39103 176310   6609 2387666  156767      0     51
dm-0  136636      0 7194476  122411 182914      0 2387648 1200311      0     77
dm-1   36489      0 1350978   68501  77956      0  666896   85420      0     24
dm-2     324      0    2592     495      0      0       0       0      0      0
dm-3   99719      0 5840074   53613  91486      0 1720752 1115300      0     53
$ vmstat -p /dev/sda2  
sda2          reads   read sectors  writes    requested writes
              135779    7196329     165870    2463464
  • ワイド表示(-w)
$ 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          0    6809616     242080    3800744    0    0   139    46 1594 1601  13 44  42  0  0
  • KB や MB 表示にする(-S k,K,m,M)
$ vmstat -S M
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   6639    236   3709    0    0   134    45 1583   20 13 43 44  0  0	
  • ちなみにバージョンは 3.2.8 です
$ vmstat -V  
procps version 3.2.8