2013. 12. 26. 15:35

[Linux/Ubuntu 시스템 관리] CPU 사용량 모니터링 (iostat, dstat)

[Research/Ubuntu] - [Linux/Ubuntu 시스템 관리] 메모리 체크 (free, top, vmstat, slabinfo, slabtop)

에 이어 이제는 CPU 사용량 모니터링에 대해 알아본다. (몇몇 명령은 sudp apt-get install sysstat이 필요하다)


~$ iostat -c 3

Linux 2.6.38-8-generic (greenfish-virtual-machine) 12/26/2013 _i686_ (1 CPU)


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.22    0.04    0.17    0.20    0.00   99.36


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.33    0.00    0.33    0.00    0.00   99.33


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.00    0.00    0.00    0.00    0.00  100.00


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.00    0.00    0.00    0.00    0.00  100.00


...

위 예는, iostat을 이용하여 cpu 사용량을 3초 주기로 출력하고 있다. 


다른 terminal에서, 다음 명령 (find-in-files, find in files)을 실행하여 cpu와 i/o에 부하를 준다.

~$ sudo find / -type f -name "*" | xargs grep 'int'

...


그 상황에서 다른 terminal에서 다음과 같이 cpu 사용량을 체크한다.

~$ iostat -c 3

Linux 2.6.38-8-generic (greenfish-virtual-machine) 12/26/2013 _i686_ (1 CPU)


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.28    0.04    0.22    0.24    0.00   99.22


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

          19.73    0.00   21.40   58.86    0.00    0.00


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

          14.67    0.00   13.00   70.67    0.00    1.67


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

          16.23    0.00   19.21   53.31    0.00   11.26


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

          15.05    0.00   13.04   71.57    0.00    0.33


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

          12.67    0.00   14.67   69.00    0.00    3.67


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

          16.39    0.00    9.36   53.85    0.00   20.40

...

비교해보면, %iowait 수치가 상승함이 확인된다.


-t 옵션을 사용하여 time stamp를 출력하도록 한다. 1 5를 추가하면, 1초에 한번, 총 5번 출력하도록 한다.

~$ iostat -c -t

Linux 2.6.38-8-generic (greenfish-virtual-machine) 12/26/2013 _i686_ (1 CPU)


12/26/2013 03:40:00 PM

avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.30    0.04    0.24    0.33    0.00   99.09


~$ iostat -c -t 1 5

Linux 2.6.38-8-generic (greenfish-virtual-machine) 12/26/2013 _i686_ (1 CPU)


12/26/2013 03:40:22 PM

avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.30    0.04    0.24    0.33    0.00   99.09


12/26/2013 03:40:23 PM

avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.00    0.00    0.00    0.00    0.00  100.00


12/26/2013 03:40:24 PM

avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.00    0.00    0.00    1.98    0.00   98.02


12/26/2013 03:40:25 PM

avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           1.00    0.00    0.00    0.00    0.00   99.00


12/26/2013 03:40:26 PM

avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           1.00    0.00    0.00    0.00    0.00   99.00


~$


다음과 같이 dstat도 활용할 수 있다(필요한 경우, sudo apt-get install dstat를 해야한다.)

~$ dstat -t -c 3

----system---- ----total-cpu-usage----

     time     |usr sys idl wai hiq siq

26-12 15:47:12|  0   0  99   0   0   0

26-12 15:47:15|  0   0 100   0   0   0

26-12 15:47:18|  8   7  73  12   0   0

26-12 15:47:21| 22  19  44  14   0   1

26-12 15:47:24| 15  26  24  31   0   4

26-12 15:47:27|  8  24  38  27   0   3

26-12 15:47:30|  0   0  97   3   0   0

26-12 15:47:33|  0   0 100   0   0   0

26-12 15:47:36|  1   0  99   0   0   0 

26-12 15:47:37|  0   0 100   0   0   0^C

~$


혹은 다음과 같이 top을 이용할 수 있다.

~$ top


다음과 같은 화면이 발생한다.

top - 15:55:25 up  4:42,  2 users,  load average: 0.00, 0.03, 0.06

Tasks: 132 total,   2 running, 129 sleeping,   0 stopped,   1 zombie

Cpu(s):  3.3%us,  0.0%sy,  0.0%ni, 96.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

Mem:    508000k total,   460408k used,    47592k free,    80768k buffers

Swap:   522236k total,     8436k used,   513800k free,   127084k cached


  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            

 4542 greenfis  20   0 91132  13m  10m R  4.0  2.8   0:00.30 gnome-terminal     

 1667 root      20   0  169m  28m 6112 S  2.0  5.8   0:29.97 Xorg               

  313 root      20   0     0    0    0 S  0.3  0.0   0:00.37 jbd2/sdb5-8        

 1996 greenfis  20   0 81076  11m 9352 S  0.3  2.4   0:10.89 wnck-applet        

    1 root      20   0  2916 1528 1100 S  0.0  0.3   0:01.26 init               

    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd           

    3 root      20   0     0    0    0 S  0.0  0.0   0:00.24 ksoftirqd/0        

    6 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0        

    7 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 cpuset             

    8 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 khelper            

    9 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 netns              

   10 root      20   0     0    0    0 S  0.0  0.0   0:00.03 sync_supers        

   11 root      20   0     0    0    0 S  0.0  0.0   0:00.00 bdi-default        

   12 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 kintegrityd        

   13 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 kblockd            

   14 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 kacpid             

   15 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 kacpi_notify       


위 화면에서 Shift+p를 누르면, 아래와 같이 cpu 사용량으로 정렬되어 표기된다.

top - 15:56:30 up  4:43,  2 users,  load average: 0.00, 0.02, 0.06

Tasks: 132 total,   2 running, 129 sleeping,   0 stopped,   1 zombie

Cpu(s):  3.3%us,  0.3%sy,  0.0%ni, 96.3%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

Mem:    508000k total,   478784k used,    29216k free,    80812k buffers

Swap:   522236k total,     8436k used,   513800k free,   145340k cached


  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            

 4542 greenfis  20   0 91272  14m  10m R  4.3  2.8   0:00.85 gnome-terminal     

 1667 root      20   0  169m  28m 6120 S  2.0  5.8   0:30.48 Xorg               

 1444 root      20   0 25312 2416 2088 S  0.3  0.5   0:09.75 vmtoolsd           

 1926 greenfis  20   0 95508  12m 9212 S  0.3  2.6   0:13.23 vmtoolsd           

 1996 greenfis  20   0 81076  11m 9352 S  0.3  2.4   0:10.94 wnck-applet        

 4615 greenfis  20   0  2632 1140  860 R  0.3  0.2   0:00.02 top                

    1 root      20   0  2916 1528 1100 S  0.0  0.3   0:01.26 init               

    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd           

    3 root      20   0     0    0    0 S  0.0  0.0   0:00.24 ksoftirqd/0        

    6 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0        

    7 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 cpuset             

    8 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 khelper            

    9 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 netns              

   10 root      20   0     0    0    0 S  0.0  0.0   0:00.03 sync_supers        

   11 root      20   0     0    0    0 S  0.0  0.0   0:00.00 bdi-default        

   12 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 kintegrityd        

   13 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 kblockd            


만약, cpu processor의 정보를 구하고자 한다면, 다음과 같이 진행하면 된다.

~$ cat /proc/cpuinfo

processor : 0

vendor_id : GenuineIntel

cpu family : 6

model : 58

model name : Intel(R) Core(TM) i5-3570 CPU @ 3.40GHz

stepping : 9

cpu MHz : 3801.000

cache size : 6144 KB

fdiv_bug : no

hlt_bug : no

f00f_bug : no

coma_bug : no

fpu : yes

fpu_exception : yes

cpuid level : 13

wp : yes

flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss nx rdtscp lm constant_tsc up arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt aes hypervisor lahf_lm ida arat epb xsaveopt pln pts dts

bogomips : 7602.00

clflush size : 64

cache_alignment : 64

address sizes : 40 bits physical, 48 bits virtual

power management:


~$