linux-CPU分析

第一种:用top命令 中的cpu 信息观察

Cpu(s): 0.3% us, 1.0% sy, 0.0% ni, 98.7% id, 0.0% wa, 0.0% hi, 0.0% si
Tasks: 29 total 进程总数

  1 running 正在运行的进程数
  28 sleeping 睡眠的进程数
  0 stopped 停止的进程数
  0 zombie 僵尸进程数
  Cpu(s):
  0.3% us 用户空间占用CPU百分比
  1.0% sy 内核空间占用CPU百分比
  0.0% ni 用户进程空间内改变过优先级的进程占用CPU百分比
  98.7% id 空闲CPU百分比
  0.0% wa 等待输入输出的CPU时间百分比
  0.0% hi
  0.0% si
  0.0% wa 的百分比可以大致的体现出当前的磁盘io请求是否频繁。如果 wa的数量比较大,说明等待输入输出的的io比较多。

CPU使用率较低但负载较高,问题描述:
Linux 系统没有业务程序运行,通过 top观察,类似如下图所示,CPU 很空闲,但是 load average 却非常高:

linux-CPU分析

另外一个查看负载命令uptime

[spuser@tzddbackend01 ServerAgent-2.2.3]$ uptime
09:33:12 up 60 days, 17:00,  1 user,  load average: 0.01, 0.03, 0.05

处理办法:
load average 是对 CPU 负载的评估,其值一般与cpu的核心数相当为好,其值越高,说明其任务队列越长,处于等待执行的任务越多。
出现此种情况时,可能是由于僵死进程导致的。可以通过指令 ps -axjf 查看是否存在 D 状态进程。
D 状态是指不可中断的睡眠状态。该状态的进程无法被 kill,也无法自行退出。只能通过恢复其依赖的资源或者重启系统来解决。

CPU占用过高分析,可以用top命令查看哪一个进程占用cpu高 或者哪一个占用内存大

Tasks: 161 total,   1 running, 160 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.7 us,  0.3 sy,  0.0 ni, 96.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16267564 total,   271608 free,  9033932 used,  6962024 buff/cache
KiB Swap:  8388604 total,  8364736 free,    23868 used.  5913400 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                                                                              
29433 wuuser    20   0 7805852 1.132g  19216 S   0.7  7.3   6:07.13 java                                                                                                                                                                 
29355 wuuser    20   0 6909304 1.093g  17248 S   0.3  7.0   2:29.96 java                                                                                                                                                                 
29558 wuuser    20   0 7763476 974788  15860 S   0.3  6.0   1:54.47 java                                                                                                                                                                 
29945 wuuser    20   0 7793536 1.202g  21344 S   0.3  7.7   4:31.02 java

可以看到占用最高的是29433

用top -H -p pid命令查看进程内各个线程占用的CPU百分比top -H -p 29433cpu消耗情况

Threads: 132 total,   0 running, 132 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.1 us,  0.0 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16267564 total,   271024 free,  9034476 used,  6962064 buff/cache
KiB Swap:  8388604 total,  8364736 free,    23868 used.  5913044 avail Mem 
  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                                                                                                               
29433 wuuser    20   0 7805852 1.132g  19216 S  0.0  7.3   0:00.00 java                                                                                                                                                                  
29436 wuuser    20   0 7805852 1.132g  19216 S  0.0  7.3   0:08.91 java                                                                                                                                                                  
29437 wuuser    20   0 7805852 1.132g  19216 S  0.0  7.3   0:00.34 java                                                                                                                                                                  
29438 wuuser    20   0 7805852 1.132g  19216 S  0.0  7.3   0:00.35 java                                                                                                                                                                  
29439 wuuser    20   0 7805852 1.132g  19216 S  0.0  7.3   0:00.34 java                                                                                                                                                                  
29440 wuuser    20   0 7805852 1.132g  19216 S  0.0  7.3   0:00.34 java                                                                                                                                                                  
29441 wuuser    20   0 7805852 1.132g  19216 S  0.0  7.3   0:00.34 java

上面看到线程29436占用高时长多

接着 使用printf “%x\n” 线程号将异常线程号转化为16进制printf “%x\n” 29436
72fc
接着使用gstack 29433|grep 72fc -A90来定位出现异常的代码.

"main" prio=10 tid=0x00007f16fc00e000 nid=0x72fc in Object.wait() [0x00007f17056d9000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x0000000778044670> (a java.lang.Class for com.wu.mortgage.service.OrderServiceStartup)
        at java.lang.Object.wait(Object.java:503)
        at com.wu.mortgage.service.OrderServiceStartup.main(OrderServiceStartup.java:47)
        - locked <0x0000000778044670> (a java.lang.Class for com.wu.mortgage.service.OrderServiceStartup)
"VM Thread" prio=10 tid=0x00007f16fc14a000 nid=0x7309 runnable 
"Gang worker#0 (Parallel GC Threads)" prio=10 tid=0x00007f16fc01f800 nid=0x72fd runnable 
"Gang worker#1 (Parallel GC Threads)" prio=10 tid=0x00007f16fc021800 nid=0x72fe runnable 
"Gang worker#2 (Parallel GC Threads)" prio=10 tid=0x00007f16fc023000 nid=0x72ff runnable 
"Gang worker#3 (Parallel GC Threads)" prio=10 tid=0x00007f16fc025000 nid=0x7300 runnable 
"Gang worker#4 (Parallel GC Threads)" prio=10 tid=0x00007f16fc027000 nid=0x7301 runnable 
"Gang worker#5 (Parallel GC Threads)" prio=10 tid=0x00007f16fc029000 nid=0x7302 runnable 
"Gang worker#6 (Parallel GC Threads)" prio=10 tid=0x00007f16fc02b000 nid=0x7303 runnable 
"Gang worker#7 (Parallel GC Threads)" prio=10 tid=0x00007f16fc02d000 nid=0x7304 runnable 
"Concurrent Mark-Sweep GC Thread" prio=10 tid=0x00007f16fc106800 nid=0x7307 runnable 
"Gang worker#0 (Parallel CMS Threads)" prio=10 tid=0x00007f16fc102000 nid=0x7305 runnable 
"Gang worker#1 (Parallel CMS Threads)" prio=10 tid=0x00007f16fc104000 nid=0x7306 runnable 
"VM Periodic Task Thread" prio=10 tid=0x00007f16fcbed800 nid=0x7314 waiting on condition 
JNI global references: 522

load average值的含义

Linux系统中的Load是对当前CPU工作量的度量,简单的说是进程队列的长度。
Load Average 就是一段时间 (1 分钟、5分钟、15分钟) 内平均 Load 。
上边0.31,0.30,0.31表示:
第一位0.31:表示最近1分钟平均负载
第二位0.30:表示最近5分钟平均负载
第三位0.31:表示最近15分钟平均负载
注:linux系统是5秒钟进行一次Load采样

1) 单核处理器
假设我们的系统是单CPU单内核的,把它比喻成是一条单向马路,把CPU任务比作汽车。当车不多的时候,load <1;当车占满整个马路的时候 load=1;当马路都站满了,而且马路外还堆满了汽车的时候,load>1
2) 多核处理器
我们经常会发现服务器Load > 1但是运行仍然不错,那是因为服务器是多核处理器(Multi-core)。
假设我们服务器CPU是2核,那么将意味我们拥有2条马路,我们的Load = 2时,所有马路都跑满车辆。

3)什么样的Load average值要提高警惕
0.7 < load < 1: 此时是不错的状态,如果进来更多的汽车,你的马路仍然可以应付。 load = 1: 你的马路即将拥堵,而且没有更多的资源额外的任务,赶紧看看发生了什么吧。 load > 5: 非常严重拥堵,我们的马路非常繁忙,每辆车都无法很快的运行

4)三种Load值,应该看哪个
通常我们先看15分钟load,如果load很高,再看1分钟和5分钟负载,查看是否有下降趋势。
1分钟负载值 > 1,那么我们不用担心,但是如果15分钟负载都超过1,我们要赶紧看看发生了什么事情。所以我们要根据实际情况查看这三个值

查看物理CPU个数

cat /proc/cpuinfo| grep “physical id”| sort| uniq| wc -l

查看每个物理CPU中core的个数(即核数)

cat /proc/cpuinfo| grep “cpu cores”| uniq

查看CPU型号

cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c

[spuser@tzddbackend01 ServerAgent-2.2.3]$ cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
      1  Intel(R) Xeon(R) Bronze 3106 CPU @ 1.70GHz
[spuser@tzddbackend01 ServerAgent-2.2.3]$ 

文章来源:www.rakbuluo.com/p73

原文链接:,转发请注明来源!

发表评论

  • 33 Responses to “linux-CPU分析”
    • Kbctboop

      cialis on sale <a href="http://mycialedst.com/#">how does cialis work</a> cialis without prescription http://mycialedst.com/ - generic cialis us pharmacy ’

      回复
    • Fbdhprest

      best online pharmacy for cialis <a href="http://21cialismen.com/#">cheep cialis canada</a> cialis 5mg australia http://21cialismen.com/ - cialis order ’

      回复
    • Nlbxprest

      cialis in canada pharmacy <a href="http://cialijomen.com/#">cialis use in dogs</a> cialis&dapoxetine http://cialijomen.com/ - cialis 20 mg sale ’

      回复
    • LcxAniny

      generic viagra <a href="http://genqpviag.com/#">liquid viagra</a> australian viagra http://genqpviag.com/ - viagra pay by paypal ’

      回复
    • Jvcbxblafe

      cheap name brand cialis <a href="http://phrcialiled.com/#">brand cialis australia</a> cialis prescription online http://phrcialiled.com/ - name brand cialis ’

      回复
    • FsbxAniny

      viagra 100 mg best price <a href="http://genericrxxx.com/#">buying viagra in toronto</a> viagrapatent expiration date http://genericrxxx.com/ - viagra super active generic ’

      回复
    • JbdcAniny

      viagra cialis levitra <a href="http://llviabest.com/#">low price generic viagra</a> viagra buy paypal http://llviabest.com/ - viagra gold 800mg reviews ’

      回复
    • Klioboop

      viagra cheap <a href="http://bestviagrx.com/#">viagra toronto</a> buy viagra in canadГЎ viagra made in india <a href="http://viarxhd.com/#">viagra purchase online usa</a> brand viagra online viagra brand name <a href="http://viagetpill.com/#">pfsier viagra from canada</a> branded viagra free viagra and cialis samples <a href="http://pillexrx.com/#">viagra online order mastercard accepted</a> viagra online paypal viagra canada <a href="http://usggrxmed.com/#">buy female viagra australia</a> viagra buy online

      回复
    • Fgnhprest

      viagras 100mg <a href="http://ltdviagragogo.com/#">using viagra</a> order viagra thru paypal account how can i buy viagra online in canada <a href="http://hdgenericviagraxk.com/#">xp viagra</a> viagra 100 mg buy viagra in canada <a href="http://buygenericviarga.com/#">viagra express delivery</a> viagra no prescrittion newsletter viagra <a href="http://buyhdpillvia.com/#">i want to try viagra</a> paypal to buy viagra cialis with dapoxetine or viagra with dapoxetine better <a href="http://genericviagrix.com/#">how much for a bottle of viagra</a> online exteze and viagra

      回复
    • Nlbxprest

      viagra online stores <a href="http://viashoprx.com/#">what are the price of viagra at walmart</a> online viagra in united states viagra online in australia <a href="http://xviagarrx.com/#">where can you buy cheap viagra</a> generic cheap viagra viagra soft tabs uk <a href="http://viagraviagria.com/#">viagra for sale in florida</a> buyviagra use paypal to buy viagra <a href="http://asgenviagria.com/#">cialis or viagra</a> buy viagra online mastercard order viagra thru paypal account <a href="http://viagengen.com/#">where can you buy cheap viagra</a> buy viagra canada

      回复
    • FnrdAniny

      how much does viagra cost with out insurance <a href="https://llviabest.com/#">buying viagra from canada</a> what to say to get prescribed viagra

      回复
    • FnrhAniny

      viagra by pfizer <a href="https://genqpviag.com/#">buy viagra oneline</a> canadian pharmacy viagra cialis

      回复
    • Dvscprest

      solve my physics homework <a href="https://dissertationhelpvfh.com/#">scientific research essay</a> do my homework app

      回复
    • FsfAniny

      college essay font <a href="https://essayhelpbgs.com/#">college essay citation</a> nursing essay

      回复
    • FtghAniny

      writing essay service <a href="https://thesiswritinghelpsjj.com/#">homework help 7th grade math</a> research paper bibliography

      回复
    • Fgsprest

      write a good thesis statement for an essay <a href="https://writemypaperbuyhrd.com/#">can someone write my essay</a> essay writing rubrics

      回复
    • FqfAniny

      architectural thesis proposals <a href="https://essaywritingservicesjy.com/#">learn how to write essay</a> great college essay

      回复
    • Jgscblafe

      do my english homework for me <a href="https://thesisbyd.com/#">pay to write paper</a> online math homework

      回复
    • FnsbAniny

      my algebra homework <a href="https://customessaywriterbyz.com/#">essay writers cheap</a> social work personal statement

      回复
    • Kwgdboop

      website that write essays for you <a href="https://researchpaperssfk.com/#">postgraduate essay writing</a> dissertation editing

      回复
    • FmsgAniny

      viagra fotos lustige <a href="https://siviagmen.com/#">viagra meglio del cialis</a> customs australia viagra

      回复
    • FbsbAniny

      viagra pfizer historia <a href="https://genericrxxx.com/#">where can i buy viagra in edinburgh</a> viagra in turkije kopen

      回复
    • Jsweblafe

      sildenafil 50 coupon <a href="https://mygoviagar.com/#">lowest price viagra 100mg</a> sildenafil 10 mg

      回复
    • Kbrgboop

      can i buy real viagra online <a href="https://miraclevigra.com/#">buy sildenafil canada</a> canadian pharmacy viagra 200 mg

      回复
    • Devkprest

      viagra softtabs <a href="https://paradiseviagira.com/#">how to buy viagra online</a> getting viagra in cape town

      回复
    • FbsgAniny

      imagens engraçadas de viagra <a href="https://buybuyviamen.com/#">indian made viagra</a> less expensive alternative to viagra

      回复
    • FhsbAniny

      viagra online newsletter <a href="https://buybuyviamen.com/#">https://buybuyviamen.com</a> valor de viagra en chile

      回复
    • Frbhprest

      viagra25mg <a href="https://paradiseviagira.com/#">https://paradiseviagira.com/</a> viagrasupplierscapetown

      回复
    • Jivhblafe

      buah viagra <a href="https://purevigra.com/#">prezzi ufficiali viagra</a> risico's gebruik viagra

      回复
    • Kmrfboop

      robber who broke into hair salon beaten viagra <a href="https://purevigra.com/#">https://purevigra.com/</a> how much is viagra with insurance

      回复
    • KmrfAniny

      us online pharmacy viagra http://usggrxmed.com/ <a href="http://usggrxmed.com/#">viagra generic online</a> buying viagra online legal

      回复
    • JildcAniny

      viagra online sales http://fmedrx.com/ <a href="http://fmedrx.com/#">online pharmacy generic viagra</a> discount viagra online

      回复
    • Stephenpeaky

      best place to buy generic viagra online <a href=" http://sild100fast.com/# ">viagra without a doctor prescription</a> is viagra over the counter

      回复