'cpu'에 해당되는 글 1건

  1. 2009.04.29 How to get cpu usage in Linux on shell programme


kernel version of Linux machine is 2.6. Os is red hat enterpise 3.

 /home/www/work]# cat /proc/stat
cpu  91470110 1515303 13832985 1975547688 57093076 457452 0
cpu0 48348024 680662 5550500 986377548 28583984 417596 0
cpu1 43122086 834640 8282484 989170140 28509091 39856 0
intr 4229107630 2324183029 21 0 2 2 0 0 0 36387 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1802215968 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 102672140 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 79 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 99239585008
btime 1230083813
processes 15607577
procs_running 1
procs_blocked 0


Above cpu value is made of sum betwen cpu1, cpu2. After booting, size of consumed jiffies is indicated.
First column is cpu number, second column is user, third filed is user in low priority (nice), fourth column is idle.
Therefore, It is possible to get cpu usage through those values. 

cpu usage = (idle jiffies)*100 / (idle jiffies + use jiffies + system jiffies + low prio jiffies) 

So, I made a script which can get cpu usage.
# CPU value
#cat /proc/stat | grep -e '^cpu ' | grep -v grep | awk '{print $0}' > cpudata
ncpuinfo=`cat /proc/stat | grep -e '^cpu ' | grep -v grep | awk '{print $0}'`
cpuinfo=(`echo $ncpuinfo | tr '.' ' '`)

total=`expr ${cpuinfo[1]} + ${cpuinfo[3]} + ${cpuinfo[4]}`
echo $total, ${cpuinfo[4]}


* Reference
  /proc/stat in man proc
     
   cpu  3357 0 4313 1362393
   The number of jiffies (1/100ths of a second) that the system spent in user mode, user mode with low priority (nice), system mode, and the idle task, respectively.  The last value should be 100 times the  second  entry in the uptime pseudo-file.

Posted by 김용환 '김용환'
TAG ,