アットウィキロゴ

vmstat_vmstat.c_new_format

new_format


source

vmstat.c

line

174 - 268

処理の流れ


  • 初期化

    • 出力時のフォーマット指定
   175   const char format[]="%2u %2u %6lu %6lu %6lu %6lu %4u %4u %5u %5u %4u %5u %2u %2u %2u %2u\n";

    • トグルスイッチ
   176   unsigned int tog=0; /* toggle switch for cleaner code */

   177   unsigned int i;

   178   unsigned int hz = Hertz;
   179   unsigned int running,blocked,dummy_1,dummy_2;
   180   jiff cpu_use[2], cpu_nic[2], cpu_sys[2], cpu_idl[2], cpu_iow[2], cpu_xxx[2], cpu_yyy[2];
   181   jiff duse, dsys, didl, diow, Div, divo2;
   182   unsigned long pgpgin[2], pgpgout[2], pswpin[2], pswpout[2];
   183   unsigned int intr[2], ctxt[2];
   184   unsigned int sleep_half;

   185   unsigned long kb_per_page = sysconf(_SC_PAGESIZE) / 1024ul;

   186   int debt = 0;  // handle idle ticks running backwards

    • スリープ時間?
   188   sleep_half=(sleep_time/2);
   189   new_header();
   190   meminfo();

    • getstat()を呼び出して、値を取得
   192   getstat(cpu_use,cpu_nic,cpu_sys,cpu_idl,cpu_iow,cpu_xxx,cpu_yyy,
   193           pgpgin,pgpgout,pswpin,pswpout,
   194           intr,ctxt,
   195           &running,&blocked,
   196           &dummy_1, &dummy_2);

    • vmstat実行時の cpu列1カラム「us」の値を計算
 カーネルコード以外の実行に使用した時間 (ユーザー時間、nice 時間を含む)。

   198   duse= *cpu_use + *cpu_nic;

    • vmstat実行時の cpu列2カラム「sy」の値を計算
 カーネルコードの実行に使用した時間 (システム時間)。

   199   dsys= *cpu_sys + *cpu_xxx + *cpu_yyy;

   200   didl= *cpu_idl;
   201   diow= *cpu_iow;
   202   Div= duse+dsys+didl+diow;
   203   divo2= Div/2UL;

    • 初回出力(初回時はシステム起動後の累積値)
   204   printf(format,

    • Procs
r: running プロセス数
b: 割り込み不可能なスリープ状態にあるプロセス数

   205          running, blocked,

    • Memory
swpd: 仮想メモリの量。
free: 空きメモリの量。
buff: バッファに用いられているメモリの量。
cache: キャッシュに用いられているメモリの量。
inact: アクティブでないメモリの量 (-a オプション)。
active: アクティブなメモリの量 (-a オプション)。

   206          unitConvert(kb_swap_used), unitConvert(kb_main_free),
   207          unitConvert(a_option?kb_inactive:kb_main_buffers),
   208          unitConvert(a_option?kb_active:kb_main_cached),

    • Swap
si: ディスクからスワップインされているメモリの量 (/s)。
so: ディスクにスワップしているメモリの量 (/s)。

   209          (unsigned)( (*pswpin  * unitConvert(kb_per_page) * hz + divo2) / Div ),
   210          (unsigned)( (*pswpout * unitConvert(kb_per_page) * hz + divo2) / Div ),

    • IO
bi: ブロックデバイスから受け取ったブロック (blocks/s)。
bo: ブロックデバイスに送られたブロック (blocks/s)。

   211          (unsigned)( (*pgpgin                * hz + divo2) / Div ),
   212          (unsigned)( (*pgpgout               * hz + divo2) / Div ),

    • System
in: 一秒あたりの割り込み回数。クロック割り込みも含む。
cs: 一秒あたりのコンテキストスイッチの回数。

   213          (unsigned)( (*intr                  * hz + divo2) / Div ),
   214          (unsigned)( (*ctxt                  * hz + divo2) / Div ),

    • CPU
これらは CPU の総時間に対するパーセンテージである。
us: カーネルコード以外の実行に使用した時間 (ユーザー時間、nice 時間を含む)。
sy: カーネルコードの実行に使用した時間 (システム時間)。
id: アイドル時間。Linux 2.5.41 以前では、IO 待ち時間を含んでいる。
wa: IO 待ち時間。Linux 2.5.41 以前では、0 と表示される。

   215          (unsigned)( (100*duse                    + divo2) / Div ),
   216          (unsigned)( (100*dsys                    + divo2) / Div ),
   217          (unsigned)( (100*didl                    + divo2) / Div ),
   218          (unsigned)( (100*diow                    + divo2) / Div )

    • 2回目以降の出力
   221   for(i=1;i<num_updates;i++) { /* \\\\\\\\\\\\\\\\\\\\ main loop ////////////////// */

    • sleep

vmstat.c:642 にある「if *1 == 0)」により、引数から取得した sleep_time だけSleepする。

   222     sleep(sleep_time);

   223     if (moreheaders && ((i%height)==0)) new_header();

    • toggle switch 切り替え
 計算の度に数値を反転(0->1 1->0)させる
   224     tog= !tog;

   226     meminfo();

   228     getstat(cpu_use+tog,cpu_nic+tog,cpu_sys+tog,cpu_idl+tog,cpu_iow+tog,cpu_xxx+tog,cpu_yyy+
   228 tog,
   229           pgpgin+tog,pgpgout+tog,pswpin+tog,pswpout+tog,
   230           intr+tog,ctxt+tog,
   231           &running,&blocked,
   232           &dummy_1,&dummy_2);
   233
   234     duse= cpu_use[tog]-cpu_use[!tog] + cpu_nic[tog]-cpu_nic[!tog];
   235     dsys= cpu_sys[tog]-cpu_sys[!tog] + cpu_xxx[tog]-cpu_xxx[!tog] + cpu_yyy[tog]-cpu_yyy[!to
   235 g];
   236     didl= cpu_idl[tog]-cpu_idl[!tog];
   237     diow= cpu_iow[tog]-cpu_iow[!tog];
   238
   239     /* idle can run backwards for a moment -- kernel "feature" */
   240     if(debt){
   241       didl = (int)didl + debt;
   242       debt = 0;
   243     }
   244     if( (int)didl < 0 ){
   245       debt = (int)didl;
   246       didl = 0;
   247     }
   248
   249     Div= duse+dsys+didl+diow;
   250     divo2= Div/2UL;
   251     printf(format,
   252            running, blocked,
   253            unitConvert(kb_swap_used),unitConvert(kb_main_free),
   254            unitConvert(a_option?kb_inactive:kb_main_buffers),
   255            unitConvert(a_option?kb_active:kb_main_cached),
   256            (unsigned)( ( (pswpin [tog] - pswpin [!tog])*unitConvert(kb_per_page)+sleep_half
   256 )/sleep_time ), /*si*/
   257            (unsigned)( ( (pswpout[tog] - pswpout[!tog])*unitConvert(kb_per_page)+sleep_half
   257 )/sleep_time ), /*so*/
   258            (unsigned)( (  pgpgin [tog] - pgpgin [!tog]             +sleep_half )/sleep_time
   258 ), /*bi*/
   259            (unsigned)( (  pgpgout[tog] - pgpgout[!tog]             +sleep_half )/sleep_time
   259 ), /*bo*/
   260            (unsigned)( (  intr   [tog] - intr   [!tog]             +sleep_half )/sleep_time
   260 ), /*in*/
   261            (unsigned)( (  ctxt   [tog] - ctxt   [!tog]             +sleep_half )/sleep_time
   261 ), /*cs*/
   262            (unsigned)( (100*duse+divo2)/Div ), /*us*/
   263            (unsigned)( (100*dsys+divo2)/Div ), /*sy*/
   264            (unsigned)( (100*didl+divo2)/Div ), /*id*/
   265            (unsigned)( (100*diow+divo2)/Div )  /*wa*/
   266     );
   267   }
   268 }


概要


static void new_format(void)

function


   174 static void new_format(void) {
   175   const char format[]="%2u %2u %6lu %6lu %6lu %6lu %4u %4u %5u %5u %4u %5u %2u %2u %2u %2u\n
   175 ";
   176   unsigned int tog=0; /* toggle switch for cleaner code */
   177   unsigned int i;
   178   unsigned int hz = Hertz;
   179   unsigned int running,blocked,dummy_1,dummy_2;
   180   jiff cpu_use[2], cpu_nic[2], cpu_sys[2], cpu_idl[2], cpu_iow[2], cpu_xxx[2], cpu_yyy[2];
   181   jiff duse, dsys, didl, diow, Div, divo2;
   182   unsigned long pgpgin[2], pgpgout[2], pswpin[2], pswpout[2];
   183   unsigned int intr[2], ctxt[2];
   184   unsigned int sleep_half;
   185   unsigned long kb_per_page = sysconf(_SC_PAGESIZE) / 1024ul;
   186   int debt = 0;  // handle idle ticks running backwards
   187
   188   sleep_half=(sleep_time/2);
   189   new_header();
   190   meminfo();
   191
   192   getstat(cpu_use,cpu_nic,cpu_sys,cpu_idl,cpu_iow,cpu_xxx,cpu_yyy,
   193           pgpgin,pgpgout,pswpin,pswpout,
   194           intr,ctxt,
   195           &running,&blocked,
   196           &dummy_1, &dummy_2);
   197
   198   duse= *cpu_use + *cpu_nic;
   199   dsys= *cpu_sys + *cpu_xxx + *cpu_yyy;
   200   didl= *cpu_idl;
   201   diow= *cpu_iow;
   202   Div= duse+dsys+didl+diow;
   203   divo2= Div/2UL;
   204   printf(format,
   205          running, blocked,
   206          unitConvert(kb_swap_used), unitConvert(kb_main_free),
   207          unitConvert(a_option?kb_inactive:kb_main_buffers),
   208          unitConvert(a_option?kb_active:kb_main_cached),
   209          (unsigned)( (*pswpin  * unitConvert(kb_per_page) * hz + divo2) / Div ),
   210          (unsigned)( (*pswpout * unitConvert(kb_per_page) * hz + divo2) / Div ),
   211          (unsigned)( (*pgpgin                * hz + divo2) / Div ),
   212          (unsigned)( (*pgpgout               * hz + divo2) / Div ),
   213          (unsigned)( (*intr                  * hz + divo2) / Div ),
   214          (unsigned)( (*ctxt                  * hz + divo2) / Div ),
   215          (unsigned)( (100*duse                    + divo2) / Div ),
   216          (unsigned)( (100*dsys                    + divo2) / Div ),
   217          (unsigned)( (100*didl                    + divo2) / Div ),
   218          (unsigned)( (100*diow                    + divo2) / Div )
   219   );
   220
   221   for(i=1;i<num_updates;i++) { /* \\\\\\\\\\\\\\\\\\\\ main loop ////////////////// */
   222     sleep(sleep_time);
   223     if (moreheaders && ((i%height)==0)) new_header();
   224     tog= !tog;
   225
   226     meminfo();
   227
   228     getstat(cpu_use+tog,cpu_nic+tog,cpu_sys+tog,cpu_idl+tog,cpu_iow+tog,cpu_xxx+tog,cpu_yyy+
   228 tog,
   229           pgpgin+tog,pgpgout+tog,pswpin+tog,pswpout+tog,
   230           intr+tog,ctxt+tog,
   231           &running,&blocked,
   232           &dummy_1,&dummy_2);
   233
   234     duse= cpu_use[tog]-cpu_use[!tog] + cpu_nic[tog]-cpu_nic[!tog];
   235     dsys= cpu_sys[tog]-cpu_sys[!tog] + cpu_xxx[tog]-cpu_xxx[!tog] + cpu_yyy[tog]-cpu_yyy[!to
   235 g];
   236     didl= cpu_idl[tog]-cpu_idl[!tog];
   237     diow= cpu_iow[tog]-cpu_iow[!tog];
   238
   239     /* idle can run backwards for a moment -- kernel "feature" */
   240     if(debt){
   241       didl = (int)didl + debt;
   242       debt = 0;
   243     }
   244     if( (int)didl < 0 ){
   245       debt = (int)didl;
   246       didl = 0;
   247     }
   248
   249     Div= duse+dsys+didl+diow;
   250     divo2= Div/2UL;
   251     printf(format,
   252            running, blocked,
   253            unitConvert(kb_swap_used),unitConvert(kb_main_free),
   254            unitConvert(a_option?kb_inactive:kb_main_buffers),
   255            unitConvert(a_option?kb_active:kb_main_cached),
   256            (unsigned)( ( (pswpin [tog] - pswpin [!tog])*unitConvert(kb_per_page)+sleep_half
   256 )/sleep_time ), /*si*/
   257            (unsigned)( ( (pswpout[tog] - pswpout[!tog])*unitConvert(kb_per_page)+sleep_half
   257 )/sleep_time ), /*so*/
   258            (unsigned)( (  pgpgin [tog] - pgpgin [!tog]             +sleep_half )/sleep_time
   258 ), /*bi*/
   259            (unsigned)( (  pgpgout[tog] - pgpgout[!tog]             +sleep_half )/sleep_time
   259 ), /*bo*/
   260            (unsigned)( (  intr   [tog] - intr   [!tog]             +sleep_half )/sleep_time
   260 ), /*in*/
   261            (unsigned)( (  ctxt   [tog] - ctxt   [!tog]             +sleep_half )/sleep_time
   261 ), /*cs*/
   262            (unsigned)( (100*duse+divo2)/Div ), /*us*/
   263            (unsigned)( (100*dsys+divo2)/Div ), /*sy*/
   264            (unsigned)( (100*didl+divo2)/Div ), /*id*/
   265            (unsigned)( (100*diow+divo2)/Div )  /*wa*/
   266     );
   267   }
   268 }
最終更新:2009年02月22日 19:40
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。

*1 sleep_time = atoi(*argv