Use an automated shell script to find detailed thread information used by the CPU.

Source: Internet
Author: User
Tags high cpu usage

Use an automated shell script to find detailed thread information used by the CPU.

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


High-score c code: cpu usage of each thread

Zhidao.baidu.com/question/24626798.html? Si = 1

Zhidao.baidu.com/question/34507790.html? Si = 2

Zhidao.baidu.com/question/40315074.html? Si = 4

Linux multithreading consumes 999% of the CPU

1. Anti-virus software faults

The new versions of KV, Kingsoft, and rising all add random monitoring of web pages, plug-ins, and emails, which undoubtedly increases the burden on the system. Solution: Basically there is no reasonable solution. Try to use the least Monitoring Service. Upgrade your hardware.
2. the driver has not been certified, resulting in 100% CPU resource usage

A large number of Beta drivers are flooding the Internet, leading to hard-to-find fault causes. Processing Method: especially for graphics card drivers, it is recommended that you use a Microsoft-certified or officially released driver and strictly check the model and version.

3. caused by viruses and Trojans

A large number of worms rapidly replicate within the system, resulting in a high CPU usage rate. Solution: use reliable anti-virus software to thoroughly clean up the system memory and local hard disk, and enable the system setting software to check for programs with startup exceptions. Update and Upgrade anti-virus software and firewall regularly to enhance anti-virus awareness and master the correct anti-virus knowledge.

4. Right-click Control Panel-Administrative Tools-SERVICE-rising realtime monitor service and choose manual.

5. Start->; Run->; msconfig->; Start, close unnecessary startup items, and restart.

6. view the "svchost" process.

Svchost.exe is a core process of Windows XP. Svchost.exe not only appears in Windows XP, but also exists in the Windows System Using ntinner. Generally, in Windows 2000, the number of svchost.exe processes is 2, while in Windows XP, the number of svchost.exe processes has increased to 4 and more.

7. View network connections. This is mainly for NICs.

8. View Network Connections

When a computer with Windows XP installed as a server receives a connection request from port 445, it allocates memory and a small amount of CPU resources to provide services for these connections. When the load is too heavy, the CPU usage may be too high, because there is an inherent trade-off between the number of work items and the response capability. Determine the appropriate MaxWorkItems settings to improve the system response capability. If the set value is incorrect, the server's response capability may be affected, or a user excludes too many system resources.
To solve this problem, we can modify the Registry: in the Registry Editor, expand the [HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServiceslanmanserver] branch, and create a New DWORD Value named "maxworkitems" in the right window. Double-click the value. In the displayed window, type the following values and save and exit:

9. Check if the right-click Windows XP causes 100% CPU usage

In resource manager, When you right-click a directory or file, you may encounter the following problems:
Any file copy operation may stop at that time
The network connection speed will be significantly reduced
All stream input/output operations, such as using Windows Media Player to listen to music, may be caused by music distortion:
When you right-click a file or directory in the resource manager, the CPU usage increases to 100% when the shortcut menu is displayed, when you close the shortcut menu, the normal level is returned.

Solution:

Method 1:
Disable "use transition effects for menus and Tools"
1. Click "start"-"Control Panel"
2. Double-click "display" in "Control Panel"
3. Click the "appearance" tab in the "display" attribute.
4. On the "appearance" tab, click "effect"
5. In the "effect" dialog box, clear the check box before "use transition effect for menus and Tools" and click "OK" twice.

Method 2:
When you right-click a file or directory, use the left mouse button to select your target file or directory. Then use the mouse to the right... the remaining full text>

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.