• CPU飙升问题排查


    1.查看CPU占用率高的进程
      top命令查看进程信息
      ps -eo pid,pcpu | sort -n -k pid

    2.确定高cpu消耗的线程
      top -H
      ps H -eo pid,tid,pcpu | sort -n -k 3

    3.查看进程的线程
      pstree -p pid
      查看线程的相信信息:cat /proc/进程号/task/线程号/status

    4.实时线程进程的资源占用信息
      top -H -p pid

    5.查看该进程下的所有线程
      ps -efL | grep pid
      根据pid,查找占用cpu较高的线程
      ps -mp pid -o THREAD,tid,time
    6.使用jstack定位线程堆栈信息,精确找到异常代码
      查看某进程下占用CPU较高的线程:ps p 替换进程号 -L -o pcpu,pid,tid,time,tname,cmd

    %CPU   PID   TID     TIME TTY      CMD
             0.0  1538  1538 00:00:00 ?        java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
            97.1  1538  1539 04:39:56 ?        java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
             0.1  1538  1540 00:00:25 ?        java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
             0.0  1538  1541 00:00:00 ?        java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
             0.0  1538  1542 00:00:00 ?        java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
             0.0  1538  1543 00:00:00 ?        java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
             0.1  1538  1544 00:00:33 ?        java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
             0.0  1538  1545 00:00:12 ?        java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
             0.0  1538  1546 00:00:00 ?        java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
             0.5  1538  1547 00:01:43 ?        java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
             0.0  1538  7170 00:00:00 ?        java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8

      将获取的线程号(十进制数)转换成十六进制
      printf "%x " 1539
      结果:603
      结合进程号和线程号,利用jstack查到异常代码所在行
      jstack pid 1538| grep 0x603 -A 10

    [root@cloud ~]# jstack -l 1538| grep 0x603 -A 10
            "main" #1 prio=5 os_prio=0 tid=0x00007f0088008800 nid=0x603 runnable [0x00007f008ca04000]
               java.lang.Thread.State: RUNNABLE
                at com.xxxx.xxxx.tester.Pppoe.main(Unknown Source)
     
               Locked ownable synchronizers:
                - None
     
            "VM Thread" os_prio=0 tid=0x00007f008806d000 nid=0x604 runnable         "VM Periodic Task Thread" os_prio=0 tid=0x00007f00880c0000 nid=0x60b waiting on condition

    此时,就可以看到占用CPU的程序了。

    将信息输出到文件
    jstack -l 1538 > 1538.pid
    使用vi查看。
    注意:需要在多个时间段提出多个 Thread Dump信息,然后综合进行对比分析,单独分析一个文件是没有意义的
    使用脚本

    #!/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
    " >> "$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
        
  • 相关阅读:
    gitlab: git clone/pull / push: The project you were looking for could not be found
    转载: MySQL启动出错InnoDB: Check that you do not already have another mysqld process解决方法
    root用户删除文件,提示:Operation not permitted
    使用dockerfile打包新镜像
    kubernets创建Deployment
    代理全家福
    Spring事务传播详解
    [FFmpeg]Centos7 yum安装
    [Redis]存放字典
    [Docker]开放2375端口
  • 原文地址:https://www.cnblogs.com/sglx/p/15218886.html
Copyright © 2020-2023  润新知