有處理過生產問題的同窗基本都能遇到系統突然緩慢,CPU忽然飆升,甚至整個應用請求不可用。當出現這種狀況下,在不影響數據準確性的前提下,咱們應該儘快導出jstack和內存信息,而後重啓系統,儘快回覆系統的可用性,避免用戶體驗過差。本文針對CPU飆升問題,提供該問題的排查思路,從而可以快速定位到某線程甚至某快代碼致使CPU飆升,從而提供處理該問題的思路。java
經過以上步驟能夠查找出致使cpu飆升的相關代碼位置,而後對代碼進行code review便可。git
./java-thread-top.sh -p pid
#!/bin/bash # @Function # Find out the highest cpu consumed threads of java processes, and print the stack of these threads. # @github https://github.com/cjunn/script_tool/ # @author cjunn # @date Sun Jan 12 2020 21:08:58 GMT+0800 # pid=''; count=5; function usage(){ readonly PROG="`basename $0`" cat <<EOF Usage: ${PROG} [OPTION] Find out the highest cpu consumed threads of java processes, and print the stack of these threads. Example: ${PROG} -p <pid> -c 5 # show top 5 busy java threads info Output control: -p, --pid <java pid> find out the highest cpu consumed threads from the specified java process. default from all java process. -c, --count <num> set the thread count to show, default is 5. Miscellaneous: -h, --help display this help and exit. EOF } #1.Collect script parameters #2.Check whether PID exists if [ $# -gt 0 ]; then while true; do case "$1" in -c|--count) count="$2" shift 2 ;; -p|--pid) pid="$2" shift 2 ;; -h|--help) usage exit 0; ;; --) shift break ;; *) shift if [ -z "$1" ] ; then break fi ;; esac done fi if [ ! -n "$pid" ] ;then echo "error: -p is empty" exit 1; fi function worker(){ #1.Query all threads according to PID. #2.Delete header and first line information. #3.According to the second column of CPU to sort, reverse display. #4.Delete the count + 1 to last column based on the count value. #5.Get CPU utilization, TID value, thread used time, and assign them to CPU, TID, time respectively. #6.Perform hex conversion on TID. #7.Use JDK to monitor all threads of jstack output PID. #8.Use awk to regularly query the thread information of tid_hex required. #9.Display the stack information of count before thread busy. local whilec=0; ps -mp $pid -o THREAD,tid,time | sed '1,2d' | sort -k 2 -n -r |sed $[$count+1]',$d' | awk '{print $2,$8,$9}' | while read cpu tid time do tid_hex=$(printf "%x" $tid); echo "====================== tid:${tid} tid_hex:${tid_hex} cpu:${cpu} time:${time} ======================"; jstack $pid | awk 'BEGIN {RS = "\n\n+";ORS = "\n\n"} /'${tid_hex}'/ {print $0}' echo ""; whilec=$[$whilec+1]; done if [ $whilec -eq 0 ] ; then echo "error : thread not found, make sure pid exists."; fi } worker