# 1. 查看主进程占用cpu高 top # 1647 java # 2. 按照线程占用cpu由高到低进行排查: ps -mp 1647 -o THREAD,tid,time | sort -rn |head -6 # USER %CPU PRI SCNT WCHAN USER SYSTEM TID TIME # root 1.6 - - - - - - 00:02:07 # root 0.1 19 - futex_ - - 1767 00:00:11 # root 0.1 19 - futex_ - - 1766 00:00:11 # root 0.1 19 - futex_ - - 1765 00:00:10 # root 0.0 19 - skb_re - - 5048 00:00:00 # 3. 将线程号转化为16进制: printf "%x " 1767 6e7 # 4. 通过主进程搜索所有的线程执行的内容 # 注意点: # 项目运行的用户 # 使用的jdk版本下的jstack去查看 flag="0x" sudo -u root /opt/jdk1.8.0_191/bin/jstack 1647 |grep ${flag}6e7 -A 30 ### 将整个流程写成脚本,查找tomcat占用CPU高的线程日志 pid=$(ps -ef|grep tomcat|grep -v grep|awk '{print $2}'|head -1) flag="0x" jstack="/opt/jdk1.8.0_191/bin/jstack" for tpid in `ps -mp ${pid} -o THREAD,tid,time | sort -rn |head -6|awk '$8 ~ /[0-9]+/ {print $8}'` do tpid_16hex=$(printf "%x " ${tpid}) echo "------------------------------------- tpid:[ ${tpid} ] -----------------------------------------------------" sudo -u root ${jstack} ${pid} |grep ${flag}${tpid_16hex} -A 40 done