Use an automated shell script to find detailed thread information for CPU usage

Source: Internet
Author: User
Tags high cpu usage

Use an automated shell script to find detailed thread information for CPU usage

After some interceptor code is added to the project, the CPU alarm shows that the CPU usage exceeds 100%;

If you want to find out which code consumes too much resources, find a blog post on the Internet and repost it as follows:

Http://blog.csdn.net/guixunlong/article/details/8450897

After you know which Java Process has a high CPU usage:
1. Run the jstack PID command to print out the thread stack of processes with high CPU usage, for example, jstack 12012> 12012.txt.
2. Run the top-H-p PID command to check which thread occupies too much CPU. For example:


It can be seen that the CPU usage of a thread with the thread number 12025is very high, and you can find the thread in 12012.txt to see why it is so busy.
You can also run the ps command ps-mp pid-o THREAD, tid, time, or ps-Lfp pid to view busy THREAD information.


The role of this command is to obtain information about the threads in the corresponding process.
For example, if you want to analyze the running bottleneck of a java Process, you can use this command to find the CPU usage time of all the current threads. The Thread number is in the tid column.

3. Find the directory thread in the file dumped by jstack dump. Note that the number of 16 processes is used to indicate the thread number. Therefore, we can find the number of 16 processes corresponding to 12025 2EF9.


The yellow text in the figure is the thread id value.
Nid: The tid thread number in the Linux operating system, that is, the hexadecimal number converted previously
Tid: this should be the unique address location in the jmm memory specification of jvm.













































 

According to the blogger's steps, the detailed thread information is indeed found (Thank you very much !).

However, the steps are a little complicated. In an emergency, every second is required. This method will obviously waste a lot of time.

So I want to write an automated script based on the master's steps.

After many executions and analyses, I was very careful and finally wrote a shell script that met the conditions,

Directly call this script, which can be executed without any parameters. This script can be used in various production environments where the author works.

The script content is pasted as follows, and comments are added to the Code for your reference. This script does not necessarily meet various environments. Please modify it by yourself.

My level is limited, and errors are inevitable. Thank you for your criticism.

#! /Bin/bash #################################### ###################### find detailed information about threads that occupy too much CPU resources ###### allen add ####################################### ################### Step1 print the IDtop-B-d3-n1-u hotel | awk' /PID /, 0'>. /_ pid_out.outv_pid = 'awk' NR = 2 {print $1 }'. /_ pid_out.out '# Step2 prints the IDtop-B-d3-n1-H-p $ v_pid | awk'/PID/, 0'>. /_ tid_out.outv_tid = 'awk' NR = 2 {print $1 }'. /_ tid_out.out '# Step 3 convert the thread ID to hexadecimal # echo 'ibase = 10; obase = 16; $ v_tid '| bcv_tid16 = 'printf % x $ v_tid' echo "thread id [hexadecimal] is: 0x $ {v_tid16} "echo" "# Step4 print the thread stack echo" wait 5 seconds, please... "jstack $ v_pid>. /_ thread_stack.outsleep 5 s # Step5 search for the code executed by the thread in _ thread_stack.out, print the modified line and the last 30 lines, and highlight the Matching content cat. /_ thread_stack.out | grep-n -- color = auto-A 30-I 0x $ {v_tid16} # cleanrm-rf. /_ pid_out.outrm-rf. /_ tid_out.out

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.