Use automated shell scripts to find detailed thread information for CPU use

Source: Internet
Author: User

After adding some interceptor code, the CPU alarm shows that CPU utilization exceeds 100%.

To find out exactly what code consumes too much resources, find a blog post from the Web, reproduced as follows:

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

After knowing which Java process CPU usage is too high:
1. Use the command jstack PID command to print out the CPU-intensive thread stacks, such as Jstack 12012 > 12012.txt
2. Use the Top-h-P PID command to see which thread is consuming high CPU on the corresponding process. Like what:


As you can see, threads with a thread number of 12025 occupy a high CPU, and then you can find the thread in 12012.txt to see why it's so busy.
Or you can use the PS command ps-mp pid-o thread,tid,time or PS-LFP pid to view busy thread information


The purpose of this command is to obtain some information about the thread that corresponds to a process.
For example, if you want to analyze some of the running bottlenecks of a Java process, you can use this command to find the CPU-consuming time for all current threads, and the thread number is the TID column.

3. To find the directory thread in the Jstack dump file, note that the thread number is represented by the number of 16 processes, so we can find 12025 corresponding 16 process number 2EF9


The yellow text in the figure is the thread ID value
Nid: The TID thread number under the corresponding Linux operating system, which is the 16 binary number that was previously converted
Tid: This should be the only address location in the JVM's JMM memory specification













































Follow the blogger's steps and indeed find the detailed threading information (thank you very much blogger!). )。

It's just a little cumbersome, and in times of emergency, every second counts, and the method obviously wastes a lot of time.

So you want to write an automated script based on the steps of the blogger.

After many times of execution and analysis, Kung Fu is a conscientious, and finally wrote the conditions of the shell script,

Call the script directly, without any parameters, executable; The script can be used in various production environments where the author works.

The script content is now posted as follows, and comments are added to the code for your reference; The script does not necessarily meet a variety of environments, please modify it yourself.

The author's level is limited, the mistake is unavoidable. Welcome criticism.

#!/bin/bash########################################################### find thread details that consume too much CPU resources # # # # # 2014-11-03 Allen add########################################################## #Step1 Print CPU-intensive process Idtop-b-d3-n1-u Hotel |  awk '/pid/,0 ' >./_pid_out.outv_pid= ' awk ' nr==2 {print $} './_pid_out.out ' #Step2 a thread that consumes too much CPU in the print process idtop-b-d3-n1-h-P $v _pid | awk '/pid/,0 ' >/_tid_out.outv_tid= ' awk ' nr==2 {print $} './_tid_out.out ' #Step3 to convert thread ID to 16-in #echo ' ibase=10;obase= $v _tid ' | bcv_tid16= ' printf%x $v _tid ' echo ' thread Id[hexadecimal] is:0x${v_tid16} "echo" "#Step4 Print Line stacks echo" for CPU-intensive processes wait 5 seco NDS, please ... "Jstack $v _pid >./_thread_stack.outsleep 5s#step5 find specific code for thread execution in _thread_stack.out, print a row and its next 30 rows, and highlight matches cat./_thread_stack.out | Grep-n--color=auto-a 30-i 0x${v_tid16} #cleanrm-rf./_pid_out.outrm-rf./_tid_out.out

Use automated shell scripts to find detailed thread information for CPU use

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.