Java CPU性能分析工具代碼實(shí)例
這篇文章主要介紹了Java CPU性能分析工具代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
背景
有處理過生產(chǎn)問題的同學(xué)基本都能遇到系統(tǒng)忽然緩慢,CPU突然飆升,甚至整個應(yīng)用請求不可用。當(dāng)出現(xiàn)這種情況下,在不影響數(shù)據(jù)準(zhǔn)確性的前提下,我們應(yīng)該盡快導(dǎo)出jstack和內(nèi)存信息,然后重啟系統(tǒng),盡快回復(fù)系統(tǒng)的可用性,避免用戶體驗(yàn)過差。本文針對CPU飆升問題,提供該問題的排查思路,從而能夠快速定位到某線程甚至某快代碼導(dǎo)致CPU飆升,從而提供處理該問題的思路。
排查過程
- 通過top命令查看cpu飆升的java進(jìn)程pid
- 通過ps -mp [pid] -o THREAD,tid,time查看該進(jìn)程下所擁有的線程及各個線程占用cpu的使用率,并且記錄CPU使用率過高的線程ID號
- 將線程ID號轉(zhuǎn)換為16進(jìn)程的數(shù)值記為tid_hex
- 使用jdk自帶jstack監(jiān)控命令
- 使用命令jstack [pid] | grep tid_hex -A100命令輸出該線程的堆棧信息
- 根據(jù)堆棧信息分析代碼。
通過以上步驟可以查找出導(dǎo)致cpu飆升的相關(guān)代碼位置,然后對代碼進(jìn)行code review即可。
工具封裝
以上步驟已經(jīng)封裝為腳本文件,通過以下腳本文件只需要指定進(jìn)程ID即pid即可導(dǎo)出默認(rèn)前5條導(dǎo)致CPU率過高的堆棧信息。
已上傳github : 點(diǎn)我進(jìn)入
./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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
客戶端Socket與服務(wù)端ServerSocket串聯(lián)實(shí)現(xiàn)網(wǎng)絡(luò)通信
這篇文章主要為大家介紹了客戶端Socket與服務(wù)端ServerSocket串聯(lián)實(shí)現(xiàn)網(wǎng)絡(luò)通信的內(nèi)容詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03java多線程編程同步器Future和FutureTask解析及代碼示例
這篇文章主要介紹了java多線程編程同步器Future和FutureTask解析及代碼示例,對二者進(jìn)行了詳細(xì)介紹,分析了future的源碼,最后展示了相關(guān)實(shí)例代碼,具有一定參考價值 ,需要的朋友可以了解下。2017-11-11Springboot @Value注入boolean設(shè)置默認(rèn)值方式
這篇文章主要介紹了Springboot @Value注入boolean設(shè)置默認(rèn)值方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03springboot如何獲取application.yml里值的方法
這篇文章主要介紹了springboot如何獲取application.yml里的值,文章圍繞主題相關(guān)自資料展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-04-04El表達(dá)式使用問題javax.el.ELException:Failed to parse the expression
今天小編就為大家分享一篇關(guān)于Jsp El表達(dá)式使用問題javax.el.ELException:Failed to parse the expression的解決方式,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12不寫mybatis的@Param有的報錯有的卻不報錯問題分析
這篇文章主要為大家介紹了不寫mybatis的@Param有的報錯有的卻不報錯問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09feign遠(yuǎn)程調(diào)用無法傳遞對象屬性405的問題
這篇文章主要介紹了feign遠(yuǎn)程調(diào)用無法傳遞對象屬性405的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Eclipse使用maven搭建spring mvc圖文教程
這篇文章主要為大家分享了Eclipse使用maven搭建spring mvc圖文教程,感興趣的小伙伴們可以參考一下2016-05-05Java中List轉(zhuǎn)Map的幾種常見方式與對比
JavaList轉(zhuǎn)Map是一個非常常用的技術(shù),對于Java開發(fā)人員來講,掌握該技術(shù)可以幫助我們更加高效地操作List集合中的對象,這篇文章主要給大家介紹了關(guān)于Java中List轉(zhuǎn)Map的幾種常見方式與對比的相關(guān)資料,需要的朋友可以參考下2024-02-02