這篇文章主要介紹了Java CPU性能分析工具代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、小程序開發(fā)、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了紅河哈尼免費建站歡迎大家使用!
背景
有處理過生產(chǎn)問題的同學基本都能遇到系統(tǒng)忽然緩慢,CPU突然飆升,甚至整個應用請求不可用。當出現(xiàn)這種情況下,在不影響數(shù)據(jù)準確性的前提下,我們應該盡快導出jstack和內存信息,然后重啟系統(tǒng),盡快回復系統(tǒng)的可用性,避免用戶體驗過差。本文針對CPU飆升問題,提供該問題的排查思路,從而能夠快速定位到某線程甚至某快代碼導致CPU飆升,從而提供處理該問題的思路。
排查過程
通過以上步驟可以查找出導致cpu飆升的相關代碼位置,然后對代碼進行code review即可。
工具封裝
以上步驟已經(jīng)封裝為腳本文件,通過以下腳本文件只需要指定進程ID即pid即可導出默認前5條導致CPU率過高的堆棧信息。
已上傳github : 點我進入
./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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)頁題目:JavaCPU性能分析工具代碼實例
文章位置:http://aaarwkj.com/article4/gjccoe.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、網(wǎng)站建設、品牌網(wǎng)站設計、、自適應網(wǎng)站、面包屑導航
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)