美文网首页
CPU飙高了,这样玩

CPU飙高了,这样玩

作者: 陈兄 | 来源:发表于2019-01-25 14:09 被阅读0次
    image

    服务CPU飙高了

    项目运行时,JVM占用的CPU飙高,跟我们预期的不符,CPU在干什么呢?是什么让CPU如此忙碌呢?让我们来一次来追踪一下。

    1、查找JVM进程ID:ps aux |grep java

    2、根据pid查找占用CPU较高的线程:ps -mp pid -o THREAD,tid,time

      [xxxxxxxxx-22027 ~]$ ps -mp 10605 -o THREAD,tid,time
      USER     %CPU PRI SCNT WCHAN  USER SYSTEM   TID     TIME
      root      697   -    - -         -      -     - 41-23:49:14
      root      0.0  19    - futex_    -      - 10605 00:00:00
      root      0.0  19    - futex_    -      - 10606 00:05:15
      root      0.0  19    - poll_s    -      - 10607 00:00:03
      root     99.6  33    - ?         -      - 10608 5-23:54:02
      root     99.6  33    - ?         -      - 10609 6-00:01:38
      root     99.6  33    - ?         -      - 10610 6-00:00:17
      root     99.6  33    - ?         -      - 10611 6-00:01:01
      root     99.6  33    - ?         -      - 10612 5-23:58:03
      root     99.6  33    - ?         -      - 10613 5-23:54:01
      root     99.6  33    - ?         -      - 10614 5-23:54:51
    

    3、利用tid转换成16进制的数字:printf “%x\n” tid

      [xxxxxxxxx-22027 ~]$ printf "%x\n" 10608
      2970
    

    4、使用jstack命令,查询线程信息,从而定位到具体线程和代码:jstack pid | grep 16进制数字 -A 30

      checking pid(10605)
      ******************************************************************* ThreadId=0 **************************************************************************
      2017-11-08 11:54:54
      Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.55-b03 mixed mode):
    
      "Attach Listener" daemon prio=10 tid=0x00007ffa98002000 nid=0x28e29 waiting on condition [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
      "scheduler-10" prio=10 tid=0x00007ffa7000e000 nid=0x28c99 waiting on condition [0x00007ff9bbfbf000]
       java.lang.Thread.State: WAITING (parking)
      at sun.misc.Unsafe.park(Native Method)
      - parking to wait for  <0x00000000cc91c8c0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
      at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
      at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
      at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1085)
      at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:807)
      at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1068)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:745)
    

    可直接查出原因。
    觉得以上操作麻烦的话,可直接执行此脚本。

      #!/bin/bash
      #
      # 当JVM占用CPU特别高时,查看CPU正在做什么
      # 可输入两个参数:1、pid Java进程ID,必须参数  2、打印线程ID上下文行数,可选参数,默认打印10行
      #
    
      pid=$1
    
      if test -z $pid
      then
      echo "pid can not be null!"
      exit
      else
      echo "checking pid($pid)"
      fi
    
      if test -z "$(jps -l | cut -d '' -f 1 | grep $pid)"
      then
      echo "process of $pid is not exists"
      exit
      fi
    
      lineNum=$2
      if test -z $lineNum
      then
        lineNum=10
      fi
    
      jstack $pid >> "$pid".bak
    
      ps -mp $pid -o THREAD,tid,time | sort -k2r | awk '{if ($1 !="USER" && $2 != "0.0" && $8 !="-") print $8;}' | xargs printf "%x\n" >> "$pid".tmp
    
      tidArray="$( cat $pid.tmp)"
    
      for tid in $tidArray
      do
        echo "******************************************************************* ThreadId=$tid **************************************************************************"
        cat "$pid".bak | grep $tid -A $lineNum
      done
    
      rm -rf $pid.bak
      rm -rf $pid.tmp
    
    
    

    本文转自 简书【http://www.jianshu.com/p/90579ec3113f

    相关文章

      网友评论

          本文标题:CPU飙高了,这样玩

          本文链接:https://www.haomeiwen.com/subject/zrbmjqtx.html