Find the Java thread that consumes the most CPU

The ps command

Command:

Ps-mp pid -o THREAD,tid,time, or ps-lFP PIDCopy the code

Results show:

 

 

The main function of this command is to obtain some information about the thread of a process. For example, if you want to analyze some bottlenecks in a Java process, you can use this command to find the CPU time of all current threads, which is the last column here.

TID: 30834 = TID: 30834

Printf “%x\n” 30834 first converts to hexadecimal and continues with the jstack command to dump the current JVM process stack. Using the Grep command, you can find the hexadecimal thread ID, and quickly find the code that consumes the most CPU.

 

 

To give a simple explanation, the following thread information is displayed in jStack:

800 nid=0x7d9b waiting on condition [0x0000000046f66000]
"DboServiceProcessor-4-thread-295" daemon prio=10 tid=0x00002aab047a9800 nid=0x7d9b waiting on condition [0x0000000046f66000]Copy the code

Nid: indicates the TID of the Linux operating system, which is the hexadecimal number converted before

Tid: This should be the only address location in the JVM’s JMM memory specification, if you need to analyze some JVM memory data in detail. I’m not at that level myself, so let go

The top command

Command:

top -Hp pidCopy the code

The results show:

 

As a result, you can track the most CPU-consuming threads in a given process in real time. Then use the previous method to extract the corresponding thread stack information.

Determine I/O bottlenecks

The mpstat command

Run the mpstat -p ALL 1 1000 command

The results show:

 

 

Notice the % ioWAIT column here, the amount of time the CPU spends waiting for I/O operations. A persistently high value is usually the result of an I/O bottleneck.

Using this parameter, you can see whether the CURRENT I/O operation has a bottleneck

Iostat command

Command:

iostat -m -x 1 1000Copy the code

 

 

You can also observe the %iowait data on the CPU. Iostat also provides some more detailed I/O status data, such as:

Avgqu-sz: The average queue length of The requests that were issued to The device. Load of CPU (await) The average milliseconds for I/O requests issued to The device to be served.Copy the code

SVCTM and %util both represent the ratio of the time spent processing the I/O request to the CPU. These two parameters are not primary when determining whether there is a bottleneck

R/S W /s and rMB/ S wMB/s both represent some states of I/O processed by the current system. The former is TPS and the latter is throughput. This is also a measure of a system’s performance

Pid command

Command:

pidstat -p pid -u -d -t -w -h 1 1000Copy the code

The results show:

 

 

Quite a practical command, can be based on the process analysis of the corresponding performance data, including CPU,I/O,IR, CS, etc., can facilitate the developer more detailed observation of the operating status of the system. However, pidstat appears to be available in newer versions of the 2.6 kernel, requiring the installation of the sysstat package.

Sudo apt-get install sysstat

The SAR command

Command:

sar -x pid 1 1000Copy the code

 

 

SAR can also specify the corresponding PID, focusing on a few fixed parameters, which is not as powerful as pidstat. No I/O or IR information is displayed.

The SAR function overrides mpSTAT and IOSTAT functions.

Dstat command

Command:

dstat -y --tcp 1 1000Copy the code

 

 

Using dstat – TCP, you can easily view the current TCP status without netstat – NAT

Other commands

  • Netstat -natp: view the corresponding network link. Pay attention to recv-q, send-q, and State.
  • Lsof -p pid: searches for the file handle corresponding to the PID
  • Lsof -i: 80: checks which process occupies the corresponding port
  • Lsof/TMP /1. TXT: finds the process that occupies the file
  • Tcpdump/Wireshark: a packet capture analysis tool
  • Jstat/jmap/jstack/JPS and other Java monitoring commands

The last

If you want to do some performance tuning, be sure to use tools to focus on the state. Through Linux commands you can easily observe the CPU, I/O, network and other peripheral states, most of the time can already solve most of the problems. Some health monitoring within the JVM requires fine-grained observations with specialized tools.


Read this article from tree garden blogs, the original link: http://www.cnblogs.com/haochuang/archive/2011/11/18/2253410.html, if you want to reprint, please contact the original author