LINUX下查看CPU使用率的命令

1.top

p6Hb-g"dZ I+jsW0

使用权限:所有使用者

H ?m9fEl0

使用方式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b]

$K"y9RZhp0

说明:即时显示process的动态51Testing软件测试网d-Q*B"K"c+dY%H

d :改变显示的更新速度,或是在交谈式指令列( interactive command)按s51Testing软件测试网 }nIa|u5v2GD!q

q :没有任何延迟的显示速度,如果使用者是有superuser的权限,则top将会以最高的优先序执行

!z M7w rv0

c :切换显示模式,共有两种模式,一是只显示执行档的名称,另一种是显示完整的路径与名称S :累积模式,会将己完成或消失的子行程( dead child process )的CPU time累积起来

9^NhsP[*cQ0

s :安全模式,将交谈式指令取消,避免潜在的危机

g)e!x P2D5B0l0

i :不显示任何闲置(idle)或无用(zombie)的行程51Testing软件测试网:}1j b `)px'CU(MN:[

n :更新的次数,完成后将会退出top51Testing软件测试网.S2`~6x'V X

b :批次档模式,搭配"n"参数一起使用,可以用来将top的结果输出到档案内

wNIPCn0

 

h{8d!A]0P0

范例:

7EH2|']9VFu#B&E0

显示更新十次后退出;

NZSzQ*{0

top -n 10

bD~!U6d:H0

51Testing软件测试网lwM$sa:@6}

使用者将不能利用交谈式指令来对行程下命令:

i:idWv/L3q0

top -s

*a(S-o&[ nbg0

51Testing软件测试网gE:it't

将更新显示二次的结果输入到名称为top.log的档案里:51Testing软件测试网5~'i a9F ~;q

top -n 2 -b < top.log

7~ y C)oYg0

另附一个命令简介linux traceroutewindows tracert两个命令相当,跟踪网络路由51Testing软件测试网Z1t/Z&]s

51Testing软件测试网{z5S$xD$D

2.vmstat51Testing软件测试网[}4IM6`^/rU

正如我们之前讨论的任何系统的性能比较都是基于基线的,并且监控CPU的性能就是以上3点,运行队列、CPU使用率和上下文切换。以下是一些对于CPU很普遍的性能要求:51Testing软件测试网-TUI.hbE

1.对于每一个CPU来说运行队列不要超过3,例如,如果是双核CPU就不要超过6;51Testing软件测试网*tYC}B/S.V"X

2.如果CPU在满负荷运行,应该符合下列分布,

*Hsi`f E5_*{I0

a) User Time:65%~70%51Testing软件测试网.h'w2l6q[_'Y4Q2`

b) System Time:30%~35%51Testing软件测试网/P6BI#cJ

c) Idle:0%~5%51Testing软件测试网 \6W?,bOIE9O U$M@U

3. mpstat51Testing软件测试网'Iu,B0^ G3{

对于上下文切换要结合CPU使用率来看,如果CPU使用满足上述分布,大量的上下文切换也是可以接受的。

$st \F4I,O WB0

常用的监视工具有:vmstat, top,dstat和mpstat.

5Z`P6DD6r%ft0

# vmstat 1

(iJ(?@2G+m0

procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----51Testing软件测试网T7OmE:Va

r b swpd free buff cache si so bi bo in cs us sy id wa51Testing软件测试网@SND(Sl'a!Y

0 0 104300 16800 95328 72200 0 0 5 26 7 14 4 1 95 051Testing软件测试网z@1w+ue| `

0 0 104300 16800 95328 72200 0 0 0 24 1021 64 1 1 98 0

,P#m0c{2bY/\+t|0

0 0 104300 16800 95328 72200 0 0 0 0 1009 59 1 1 98 0

Z,?N)X5Mi0

r表示运行队列的大小,

u5NH|1lo S0

b表示由于IO等待而block的线程数量,

r&j(n0u,Hm8ex c0

in表示中断的数量,51Testing软件测试网$z_!\,hZ#xC

cs表示上下文切换的数量,51Testing软件测试网3N h Je Q~b)i5^ W

us表示用户CPU时间,

g%jdz-u5hNX0

sys表示系统CPU时间,51Testing软件测试网|Vnfd}

wa表示由于IO等待而是CPU处于idle状态的时间,51Testing软件测试网#c!lk3Z1xH5~

id表示CPU处于idle状态的总时间。

5VW:Oe6`b&~0

dstat可以给出每一个设备产生的中断数:

g3\a&Qm0

# dstat -cip 151Testing软件测试网#NhN%D]$LKY

----total-cpu-usage---- ----interrupts--- ---procs---

j4lQ5f^M0

usr sys idl wai hiq siq| 15 169 185 |run blk new

PoiNa2N0

6 1 91 2 0 0| 12 0 13 | 0 0 051Testing软件测试网,O*@D GMC

1 0 99 0 0 0| 0 0 6 | 0 0 051Testing软件测试网J)z;fHtKq6?'R j

0 0 100 0 0 0| 18 0 2 | 0 0 0

N v2m?`|9i:{A0

0 0 100 0 0 0| 0 0 3 | 0 0 0

pD&W)]&ZQ dQ0

我们可以看到这里有3个设备号15,169和185.设备名和设备号的关系我们可以参考文件/proc/interrupts,这里185代表网卡eth1.

2`L;n"Ya&@0

# cat /proc/interrupts51Testing软件测试网2Q!m nkb~Ex

CPU051Testing软件测试网!zcdi6?Q"KJ l

0: 1277238713 IO-APIC-edge timer51Testing软件测试网jEo3y F$gZ3n/Wi

6: 5 IO-APIC-edge floppy51Testing软件测试网,`9~8| y_QJj

7: 0 IO-APIC-edge parport051Testing软件测试网 _W I0J#S`#\d

8: 1 IO-APIC-edge rtc51Testing软件测试网YU@Mzf

9: 1 IO-APIC-level acpi51Testing软件测试网j'_ ^V6d_

14: 6011913 IO-APIC-edge ide051Testing软件测试网C7?"u^+DOq

15: 15761438 IO-APIC-edge ide1

H0S3aAr[E*e |3E;b0

169: 26 IO-APIC-level Intel 82801BA-ICH251Testing软件测试网$c:t'O;RZ]#CS:~*I&S

185: 16785489 IO-APIC-level eth151Testing软件测试网2EFC @.XP$~z

193: 0 IO-APIC-level uhci_hcd:usb151Testing软件测试网rhWC A1d~

mpstat可以显示每个CPU的运行状况,比如系统有4个CPU。我们可以看到:51Testing软件测试网(O4o`E ^c!ic

# mpstat –P ALL 1

O0{cQ.EC%z5g0

Linux 2.4.21-20.ELsmp (localhost.localdomain) 05/23/2006

-LU%MG j+JCTp0

05:17:31 PM CPU %user %nice %system %idle intr/s

UP'L4MS g x0F$T1M0

05:17:32 PM all 0.00 0.00 3.19 96.53 13.27

F3y9IJ s.]zG0

05:17:32 PM 0 0.00 0.00 0.00 100.00 0.0051Testing软件测试网D#o2jl)L:Y

05:17:32 PM 1 1.12 0.00 12.73 86.15 13.2751Testing软件测试网}0X,?9c_7kV8R]I

05:17:32 PM 2 0.00 0.00 0.00 100.00 0.00

){{s+llW YFh0

05:17:32 PM 3 0.00 0.00 0.00 100.00 0.00

'A1xjM ?_D'V0

总结的说,CPU性能监控包含以下方面:51Testing软件测试网 ie\]@'K-w.sl5n{

检查系统的运行队列,确保每一个CPU的运行队列不大于3.51Testing软件测试网SilXC5cq

确保CPU使用分布满足70/30原则(用户70%,系统30%)。

U([5Mpv0

如果系统时间过长,可能是因为频繁的调度和改变优先级。51Testing软件测试网IK ?X.g

CPU Bound进程总是会被惩罚(降低优先级)而IO Bound进程总会被奖励(提高优先级)。51Testing软件测试网G-M xK*Z*FWPA:{

51Testing软件测试网C^3f*aEL oE

4.prstat命令51Testing软件测试网+\MU K%NiD

要显示系统上当前运行的进程和项目的各种统计信息,请使用带有-J选项的prstat命令:51Testing软件测试网4eL&D(z;Rf

 

.y^} J*H\5G:C0

%prstat -J51Testing软件测试网Of}0q/d H9Jq"Z`

PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/NLWP

5r.X0a/aE6Q&b0

21634 jtd 5512K 4848K cpu0 44 0 0:00.00 0.3% prstat/1

z!\O(C'F+CW"L&L0

324 root 29M 75M sleep 59 0 0:08.27 0.2% Xsun/1

D.taW2E0

15497 jtd 48M 41M sleep 49 0 0:08.26 0.1% adeptedit/1

M+H;v_r$DU0

328 root 2856K 2600K sleep 58 0 0:00.00 0.0% mibiisa/1151Testing软件测试网-|9[]/HlW`9db3T2i

1979 jtd 1568K 1352K sleep 49 0 0:00.00 0.0% csh/1

(N I.P|U+{8eB#A0

1977 jtd 7256K 5512K sleep 49 0 0:00.00 0.0% dtterm/1

?[R_e_5\8YA9c8S8w0

192 root 3680K 2856K sleep 58 0 0:00.36 0.0% automountd/5

J'V9{L D\^'t0

1845 jtd 24M 22M sleep 49 0 0:00.29 0.0% dtmail/1151Testing软件测试网of%|:e_6l

1009 jtd 9864K 8384K sleep 49 0 0:00.59 0.0% dtwm/8

0|1K6lu,vA2OE O L*v2[0

114 root 1640K 704K sleep 58 0 0:01.16 0.0% in.routed/151Testing软件测试网([%n^4g [$r0k

180 daemon 2704K 1944K sleep 58 0 0:00.00 0.0% statd/4

]?2J'Pc)`*h(T0

145 root 2120K 1520K sleep 58 0 0:00.00 0.0% ypbind/151Testing软件测试网)B w JHLuG4T E

181 root 1864K 1336K sleep 51 0 0:00.00 0.0% lockd/151Testing软件测试网+]Fr6r&Zj F

173 root 2584K 2136K sleep 58 0 0:00.00 0.0% inetd/151Testing软件测试网 g~?N5y+dm!A4Xx?Q

135 root 2960K 1424K sleep 0 0 0:00.00 0.0% keyserv/451Testing软件测试网j `b P[A*r#]

PROJID NPROC SIZE RSS MEMORY TIME CPU PROJECT51Testing软件测试网vSh Zr ~%fr}@Z

10 52 400M 271M 68% 0:11.45 0.4% booksite51Testing软件测试网+g&C/QQ3QA

0 35 113M 129M 32% 0:10.46 0.2% system51Testing软件测试网D;qc+OP6Y6f,rEO

 

6L/cU c t0

Total: 87 processes, 205 lwps, load averages: 0.05, 0.02, 0.02

Y1i(Q:KT w!P9Oy:X0

要显示系统上当前运行的进程和任务的各种统计信息,请使用带有-T选项的prstat命令:

k!D8l)P.gm0

51Testing软件测试网PI,A:ap l(z

%prstat -T51Testing软件测试网)Y#k:]L;b8T|:XKi

PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/NLWP51Testing软件测试网G`U-Dv-_V({6_@

23023 root 26M 20M sleep 59 0 0:03:18 0.6% Xsun/151Testing软件测试网#~ M:f*[W g2W

23476 jtd 51M 45M sleep 49 0 0:04:31 0.5% adeptedit/1

q(R0w_BP:Z0

23432 jtd 6928K 5064K sleep 59 0 0:00:00 0.1% dtterm/151Testing软件测试网t?'~9i^&Mcy&{%XE

28959 jtd 26M 18M sleep 49 0 0:00:18 0.0% .netscape.bin/1

Bb:Uv8NC*OM%Z0

23116 jtd 9232K 8104K sleep 59 0 0:00:27 0.0% dtwm/551Testing软件测试网!Z [k`C

29010 jtd 5144K 4664K cpu0 59 0 0:00:00 0.0% prstat/151Testing软件测试网U }{"R BiW

200 root 3096K 1024K sleep 59 0 0:00:00 0.0% lpsched/151Testing软件测试网1S4P!v-tZ

161 root 2120K 1600K sleep 59 0 0:00:00 0.0% lockd/2

iQ$]9mVF-hJzo0

170 root 5888K 4248K sleep 59 0 0:03:10 0.0% automountd/3

,Z`6r}%i0]&_ w0

132 root 2120K 1408K sleep 59 0 0:00:00 0.0% ypbind/1

5wX2}8x%[-?;I0

162 daemon 2504K 1936K sleep 59 0 0:00:00 0.0% statd/2

$r*}$G7dch \0

146 root 2560K 2008K sleep 59 0 0:00:00 0.0% inetd/151Testing软件测试网5Q)S#vV@W#h

122 root 2336K 1264K sleep 59 0 0:00:00 0.0% keyserv/2

U8Kh Zth^k0

119 root 2336K 1496K sleep 59 0 0:00:02 0.0% rpcbind/1

#mbwWi%sC0

104 root 1664K 672K sleep 59 0 0:00:03 0.0% in.rdisc/151Testing软件测试网$U8fF[2QZq,tTG8d

TASKID NPROC SIZE RSS MEMORY TIME CPU PROJECT 51Testing软件测试网-Ox5e].An#sy

222 30 229M 161M 44% 0:05:54 0.6% group.staff 51Testing软件测试网?o|T`"\cO~1?

223 1 26M 20M 5.3% 0:03:18 0.6% group.staff 51Testing软件测试网jR-Xs8H}NJ

12 1 61M 33M 8.9% 0:00:31 0.0% group.staff

8e B y@q A _:z0

1 33 85M 53M 14% 0:03:33 0.0% system

-r&q:y [1g-l @0

 

0}~uXOmT%W0

Total: 65 processes, 154 lwps, load averages: 0.04, 0.05, 0.06

JFMp'g5`g0

注–

8JJ n$Bc0

-J-T选项不能一起使用。51Testing软件测试网m{ j2y W WN

 

f"]7C e-h7_0

转载于:https://www.cnblogs.com/shineshqw/articles/2349713.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值