ºìÁªLinuxÃÅ»§
Linux°ïÖú

linuxÏÂ/proc/stat ¼ÆËãCPUÀûÓÃÂÊ

·¢²¼Ê±¼ä:2008-09-27 10:14:00À´Ô´:ºìÁª×÷Õß:pkbfu
Ò»°ãÀ´Ëµ¶ÔÓÚÐèÒª´óÁ¿cpu¼ÆËãµÄ½ø³Ì£¬µ±Ç°¶ËѹÁ¦Ô½´óʱ£¬CPUÀûÓÃÂÊÔ½¸ß¡£µ«¶ÔÓÚI/OÍøÂçÃܼ¯Ð͵Ľø³Ì£¬¼´Ê¹ÇëÇóºÜ¶à£¬·þÎñÆ÷µÄCPUÒ²²»Ò»¶¨ºÜµ½£¬ÕâʱµÄ·þÎñÆ¿¾±Ò»°ãÊÇÔÚ´ÅÅ̵ÄI/OÉÏ¡£±È½Ï³¤¼ûµÄ¾ÍÊÇ£¬´óÎļþƵ·±¶ÁдµÄcpu¿ªÏúԶСÓÚСÎļþƵ·±¶ÁдµÄ¿ªÏú¡£ÒòΪÔÚI/OÍÌÍÂÁ¿Ò»¶¨Ê±£¬Ð¡ÎļþµÄ¶Áд¸ü¼ÓƵ·±£¬ÐèÒª¸ü¶àµÄcpuÀ´´¦ÀíI/OµÄÖжϡ£

ÔÚLinux/UnixÏ£¬CPUÀûÓÃÂÊ·ÖΪÓû§Ì¬£¬ÏµÍ³Ì¬ºÍ¿ÕÏÐ̬£¬·Ö±ð±íʾCPU´¦ÓÚÓû§Ì¬Ö´ÐеÄʱ¼ä£¬ÏµÍ³ÄÚºËÖ´ÐеÄʱ¼ä£¬ºÍ¿ÕÏÐϵͳ½ø³ÌÖ´ÐеÄʱ¼ä¡£Æ½Ê±Ëù˵µÄCPUÀûÓÃÂÊÊÇÖ¸£ºCPUÖ´ÐзÇϵͳ¿ÕÏнø³ÌµÄʱ¼ä / CPU×ܵÄÖ´ÐÐʱ¼ä¡£

ÔÚLinuxµÄÄÚºËÖУ¬ÓÐÒ»¸öÈ«¾Ö±äÁ¿£ºJiffies¡£ Jiffies´ú±íʱ¼ä¡£ËüµÄµ¥Î»ËæÓ²¼þƽ̨µÄ²»Í¬¶ø²»Í¬¡£ÏµÍ³ÀﶨÒåÁËÒ»¸ö³£ÊýHZ£¬´ú±íÿÃëÖÖ×îСʱ¼ä¼ä¸ôµÄÊýÄ¿¡£ÕâÑùjiffiesµÄµ¥Î»¾ÍÊÇ1/HZ¡£Intelƽ̨jiffiesµÄµ¥Î»ÊÇ1/100Ã룬Õâ¾ÍÊÇϵͳËùÄÜ·Ö±æµÄ×îСʱ¼ä¼ä¸ôÁË¡£Ã¿¸öCPUʱ¼äƬ£¬Jiffies¶¼Òª¼Ó1¡£ CPUµÄÀûÓÃÂʾÍÊÇÓÃÖ´ÐÐÓû§Ì¬+ϵͳ̬µÄJiffies³ýÒÔ×ܵÄJifffiesÀ´±íʾ¡£

ÔÚLinuxϵͳÖУ¬¿ÉÒÔÓÃ/proc/statÎļþÀ´¼ÆËãcpuµÄÀûÓÃÂÊ(ÏêϸµÄ½âÊͿɲο¼£ºhttp://www.linuxhowtos.org/System/procstat.htm)¡£Õâ¸öÎļþ°üº¬ÁËËùÓÐCPU»î¶¯µÄÐÅÏ¢£¬¸ÃÎļþÖеÄËùÓÐÖµ¶¼ÊÇ´ÓϵͳÆô¶¯¿ªÊ¼ÀۼƵ½µ±Ç°Ê±¿Ì¡£

È磺

ÒýÓÃ:
[sailorhzr@builder ~]$ cat /proc/stat
cpu 432661 13295 86656 422145968 171474 233 5346
cpu0 123075 2462 23494 105543694 16586 0 4615
cpu1 111917 4124 23858 105503820 69697 123 371
cpu2 103164 3554 21530 105521167 64032 106 334
cpu3 94504 3153 17772 105577285 21158 4 24
intr 1065711094 1057275779 92 0 6 6 0 4 0 3527 0 0 0 70 0 20 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 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 7376958 0 0 0 0 0 0 0 1054602 0 0 0 0 0 0 0 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 19067887
btime 1139187531
processes 270014
procs_running 1
procs_blocked 0


Êä³ö½âÊÍ
CPU ÒÔ¼°CPU0¡¢CPU1¡¢CPU2¡¢CPU3ÿÐеÄÿ¸ö²ÎÊýÒâ˼£¨ÒÔµÚÒ»ÐÐΪÀý£©Îª£º

²ÎÊý ½âÊÍ
user (432661) ´ÓϵͳÆô¶¯¿ªÊ¼ÀۼƵ½µ±Ç°Ê±¿Ì£¬Óû§Ì¬µÄCPUʱ¼ä£¨µ¥Î»£ºjiffies£© £¬²»°üº¬ niceֵΪ¸º½ø³Ì¡£1jiffies=0.01Ãë
nice (13295) ´ÓϵͳÆô¶¯¿ªÊ¼ÀۼƵ½µ±Ç°Ê±¿Ì£¬niceֵΪ¸ºµÄ½ø³ÌËùÕ¼ÓõÄCPUʱ¼ä£¨µ¥Î»£ºjiffies£©
system (86656) ´ÓϵͳÆô¶¯¿ªÊ¼ÀۼƵ½µ±Ç°Ê±¿Ì£¬ºËÐÄʱ¼ä£¨µ¥Î»£ºjiffies£©
idle (422145968) ´ÓϵͳÆô¶¯¿ªÊ¼ÀۼƵ½µ±Ç°Ê±¿Ì£¬³ýÓ²ÅÌIOµÈ´ýʱ¼äÒÔÍâÆäËüµÈ´ýʱ¼ä£¨µ¥Î»£ºjiffies£©
iowait (171474) ´ÓϵͳÆô¶¯¿ªÊ¼ÀۼƵ½µ±Ç°Ê±¿Ì£¬Ó²ÅÌIOµÈ´ýʱ¼ä£¨µ¥Î»£ºjiffies£© £¬
irq (233) ´ÓϵͳÆô¶¯¿ªÊ¼ÀۼƵ½µ±Ç°Ê±¿Ì£¬Ó²ÖжÏʱ¼ä£¨µ¥Î»£ºjiffies£©
softirq (5346) ´ÓϵͳÆô¶¯¿ªÊ¼ÀۼƵ½µ±Ç°Ê±¿Ì£¬ÈíÖжÏʱ¼ä£¨µ¥Î»£ºjiffies£©

CPUʱ¼ä=user+system+nice+idle+iowait+irq+softirq

¡°intr¡±ÕâÐиø³öÖжϵÄÐÅÏ¢£¬µÚÒ»¸öΪ×ÔϵͳÆô¶¯ÒÔÀ´£¬·¢ÉúµÄËùÓеÄÖжϵĴÎÊý£»È»ºóÿ¸öÊý¶ÔÓ¦Ò»¸öÌض¨µÄÖжÏ×ÔϵͳÆô¶¯ÒÔÀ´Ëù·¢ÉúµÄ´ÎÊý¡£
¡°ctxt¡±¸ø³öÁË×ÔϵͳÆô¶¯ÒÔÀ´CPU·¢ÉúµÄÉÏÏÂÎĽ»»»µÄ´ÎÊý¡£
¡°btime¡±¸ø³öÁË´ÓϵͳÆô¶¯µ½ÏÖÔÚΪֹµÄʱ¼ä£¬µ¥Î»ÎªÃë¡£
¡°processes (total_forks) ×ÔϵͳÆô¶¯ÒÔÀ´Ëù´´½¨µÄÈÎÎñµÄ¸öÊýÄ¿¡£
¡°procs_running¡±£ºµ±Ç°ÔËÐжÓÁеÄÈÎÎñµÄÊýÄ¿¡£
¡°procs_blocked¡±£ºµ±Ç°±»×èÈûµÄÈÎÎñµÄÊýÄ¿¡£

ÄÇôCPUÀûÓÃÂÊ¿ÉÒÔʹÓÃÒÔÏÂÁ½¸ö·½·¨¡£ÏÈÈ¡Á½¸ö²ÉÑùµã£¬È»ºó¼ÆËãÆä²îÖµ£º

ÒýÓÃ:
cpu usage=(idle2-idle1)/(cpu2-cpu1)*100
cpu usage=[(user_2 +sys_2+nice_2) - (user_1 + sys_1+nice_1)]/(total_2 - total_1)*100


ÒÔÏÂÓ÷ֱðÓÃbashºÍperl×öµÄÒ»¸öcpuÀûÓÃÂʵļÆË㣺

±¾ÈË×¢:ÒÔÏ´úÂëÔò²ÉÓù«Ê½Îª:

ÒýÓÃ:
total_0USER[0]+NICE[0]+SYSTEM[0]+IDLE[0]+IOWAIT[0]+IRQ[0]+SOFTIRQ[0]
total_1=USER[1]+NICE[1]+SYSTEM[1]+IDLE[1]+IOWAIT[1]+IRQ[1]+SOFTIRQ[1]
cpu usage=(IDLE[0]-IDLE[1]) / (total_0-total_1) * 100


###bash ´úÂë(±¾ÈËÌṩ.¿ÉÓÃ)[code]#!/bin/sh

##echo user nice system idle iowait irq softirq
CPULOG_1=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
SYS_IDLE_1=$(echo $CPULOG_1 | awk '{print $4}')
Total_1=$(echo $CPULOG_1 | awk '{print $1+$2+$3+$4+$5+$6+$7}')

sleep 5

CPULOG_2=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
SYS_IDLE_2=$(echo $CPULOG_2 | awk '{print $4}')
Total_2=$(echo $CPULOG_2 | awk '{print $1+$2+$3+$4+$5+$6+$7}')

SYS_IDLE=`expr $SYS_IDLE_2 - $SYS_IDLE_1`

Total=`expr $Total_2 - $Total_1`
SYS_USAGE=`expr $SYS_IDLE/$Total*100 |bc -l`

SYS_Rate=`expr 100-$SYS_USAGE |bc -l`

Disp_SYS_Rate=`expr "scale=3; $SYS_Rate/1" |bc`
echo $Disp_SYS_Rate%







###perl ´úÂë(ÍøÓÑÌṩ.¿ÉÓÃ)


#!/usr/bin/perl

use warnings;

$SLEEPTIME=5;

if (-e "/tmp/stat") {
unlink "/tmp/stat";
}
open (JIFF_TMP, ">>/tmp/stat") || die "Can't open /proc/stat file!\n";
open (JIFF, "/proc/stat") || die "Can't open /proc/stat file!\n";
@jiff_0=;
print JIFF_TMP $jiff_0[0] ;
close (JIFF);

sleep $SLEEPTIME;

open (JIFF, "/proc/stat") || die "Can't open /proc/stat file!\n";
@jiff_1=;
print JIFF_TMP $jiff_1[0];
close (JIFF);
close (JIFF_TMP);

@USER=`awk '{print \$2}' "/tmp/stat"`;
@NICE=`awk '{print \$3}' "/tmp/stat"`;
@SYSTEM=`awk '{print \$4}' "/tmp/stat"`;
@IDLE=`awk '{print \$5}' "/tmp/stat"`;
@IOWAIT=`awk '{print \$6}' "/tmp/stat"`;
@IRQ=`awk '{print \$7}' "/tmp/stat"`;
@SOFTIRQ=`awk '{print \$8}' "/tmp/stat"`;

$JIFF_0=$USER[0]+$NICE[0]+$SYSTEM[0]+$IDLE[0]+$IOWAIT[0]+$IRQ[0]+$SOFTIRQ[0];
$JIFF_1=$USER[1]+$NICE[1]+$SYSTEM[1]+$IDLE[1]+$IOWAIT[1]+$IRQ[1]+$SOFTIRQ[1];

$SYS_IDLE=($IDLE[0]-$IDLE[1]) / ($JIFF_0-$JIFF_1) * 100;
$SYS_USAGE=100 - $SYS_IDLE;

printf ("The CPU usage is %1.2f%%\n",$SYS_USAGE);[/code]
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. cwqing1973 ÓÚ 2008-09-27 15:40:32·¢±í:

    Óù¦ÁË£¬×öѧÎʾÍÊÇÒªÓÐÕâÑùµÄ¾«Éñ£¡ÒÔºó¶à°ï°ïºìÁªÉϵÄÅóÓÑ£¬µ±È»Ò²°üÀ¨ÎÒÔÚÄÚ°¡£¡Ð»Ð»ÁË¡£