In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to analyze ThreadDump problems. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.
1. Overview
With the increasing size of the system and the increasing complexity of the code structure, the acceptance-tested system may perform badly in the actual production environment, or it may be very good. Therefore, through the QA can not guarantee that the system will not have memory leaks, often the greater the traffic, the faster the leakage, resulting in a system crash. For example, at a certain point in time, the system has been having TimeOut, or the system suddenly has a sharp decline in processing speed and other problems. It is very difficult for developers, many people are completely confused, basically slapping their heads and guessing. In fact, the main techniques for finding this kind of problem location are memory leak location, thread stack analysis and so on.
Overview of thread stack
Thread stack is the so-called thread call stack (all independent). In the Java thread stack JVM thread state, the snapshot contains the running status of all threads at the current time, including the call stack of each thread, lock holding and other information. Each virtual machine provides a back door to Thread Dump to help us export stack information. With the help of the thread stack, we can quickly narrow the scope of the problem, find a breakthrough, and hit the target.
Thread stack generally contains information: 1, thread name, ID, the number of threads 2, thread running state, lock state (which thread is holding the lock, which thread is waiting) 3, function call hierarchy. Includes the full class name, method name, number of lines of source code
The amount of thread stack information depends on the complexity of your system. With the help of stack information, many problems can be analyzed, such as thread deadlock, lock competition, dead loop, identifying time-consuming operations and so on. He is the most effective method for stability problem analysis and performance analysis in multithreaded scenarios, and in most cases it can be analyzed without even knowing the system.
Of course, the thread stack is not omnipotent, because it is an instantaneous snapshot, so it is impossible to track history for information that has disappeared without leaving a trace. This situation can only be analyzed with the help of monitoring systems and logs. Such as which threads are used by the connections in the connection pool and are not released. That is to say, the thread stack analyzes non-functional problems.
Thread stack is good at analyzing the following problems: 1, the system CPU is too high for no reason 2, the system hangs, no response 3, the system runs slower and slower 4, thread deadlock, dead cycle, starvation and so on. 5. The system fails due to too many threads (such as unable to create threads, etc.)
Output stack 1, unix/Linux uses kill-3 pid for dump 2, you can view dump 3 directly using VisualVM, etc., and those above JDK1.5 can use Thread.getStackTrace () to control stack automatic printing
two。 About Thread
Only when you put it into practice can you better understand its concept, the previous DEMO.
/ * * Created by QIANG on 2017-9-29 * / public class ThreadTest {Object obj1 = new Object (); Object obj2 = new Object (); public void fun1 () {synchronized (obj1) {fun2 ();}} public void fun2 () {synchronized (obj2) {while (true) {System.out.println ("I'm going to keep running.") Public static void main (String [] args) {ThreadTest aa = new ThreadTest (); aa.fun1 ();}}
Here I am using VisualVM to print the thread stack, in the complete stack information we can see a variety of system threads, limited to space, leaving only the information I pay attention to.
2017-10-19 10:46:09Full thread dump Java HotSpot (TM) 64-Bit Server VM (25.144-b01 mixed mode): "main" # 1 prio=5 os_prio=0 tid=0x0000000002737000 nid=0x5d1c runnable [0x000000000270f000] java.lang.Thread.State: RUNNABLE at java.io.FileOutputStream.writeBytes (Native Method) at java.io.FileOutputStream.write (FileOutputStream.java:326) at java.io.BufferedOutputStream.flushBuffer (BufferedOutputStream.java:82) at java.io .BufferedOutputStream.flush (BufferedOutputStream.java:140)-locked (a java.io.BufferedOutputStream) at java.io.PrintStream.write (PrintStream.java:482)-locked (a java.io.PrintStream) at sun.nio.cs.StreamEncoder.writeBytes (StreamEncoder.java:221) at sun.nio.cs.StreamEncoder.implFlushBuffer (StreamEncoder.java:291) at sun.nio.cs.StreamEncoder.flushBuffer (StreamEncoder.java) Locked (a java.io.OutputStreamWriter) at java.io.OutputStreamWriter.flushBuffer (OutputStreamWriter.java:185) at java.io.PrintStream.write (PrintStream.java:527)-eliminated (a java.io.PrintStream) at java.io.PrintStream.print (PrintStream.java:669) at java.io.PrintStream.println (PrintStream.java:806)-locked (a java. Io.PrintStream) at com.ecej.test.Thread.ThreadTest.fun2 (ThreadTest.java:21)-locked (a java.lang.Object) at com.ecej.test.Thread.ThreadTest.fun1 (ThreadTest.java:14)-locked (a java.lang.Object) at com.ecej.test.Thread.ThreadTest.main (ThreadTest.java:28) Locked ownable synchronizers:-None "VM Thread" os_ Prio=2 tid=0x0000000017928800 nid=0x3160 runnable "VM Periodic Task Thread" os_prio=2 tid=0x000000001a2d8000 nid=0x3dac waiting on condition
The thread stack is viewed from the bottom up, and it is clear at a glance what the current system has done with this information. From the main thread stack, the sentence-locked (a java.lang.Object) means that the thread already has the lock, and the lock ID is 0x0000000081e1c0c0. The ID is automatically generated by the system. You just need to know that the same ID represents the same lock every time you print the stack.
Analyze the first line of the stack message "main" (thread name) # 1 prio=5 (thread priority) os_prio=0 tid=0x0000000002737000 (thread ID) nid=0x5d1c (thread corresponds to local thread ID) runnable (thread state) [0x000000000270f000] (thread occupied memory address)
Nid=0x5d1c (thread corresponds to local thread ID) is a bit confusing, because when we start a thread in java, it must correspond to a local thread in JVM, that is to say, the number of local threads is the same as the number of Java thread stack threads. Under linux, we can use the following ways:
Use ps-ef | grep java to get the Java process ID.
Use pstack to get stack 6 of the local thread of the Java virtual machine.
If you get Thread 8 (Thread 4067802000 (LWP 3356)), where Thread 4067802000 (LWP 3356) is represented as a local thread ID. Here LWP corresponds to nid (native thread id) in ThreadDump, but nid is represented in hexadecimal. The analysis shows that the Java thread and the local thread are the same thing, and the local thread is the real thread entity.
Continue to analyze the identity, where "runable" indicates that the current thread is running. This runable logo can only indicate that this thread is running in JVM. Students who are familiar with threads must know that this state does not mean that PU is really being consumed. A thread in Runable can only mean that it is not blocked on java's wait\ sleep or waiting on the lock. But if the thread calls the local method and the local method is waiting, JVM doesn't know what's going on in the native code, and although the current thread is actually blocking, it actually shows the runable state, which doesn't consume CPU. Look at an example:
Java.lang.Thread.State: RUNNABLE at java.net.SocketInputStream.socketRead0 (Native Method)
In practice, socket's native methods are blocked most of the time unless there is data in the buffer. Therefore, when we analyze which thread is consuming a large amount of CPU, we cannot use the word "runnable" as a basis to determine whether the thread consumes CPU. We often find functions with the words ".init" or ".clinit" in the thread stack, such as the following information at java.util.logging.LogManager. (LogManager.java:156)
"." Indicates that class initialization is currently being performed. "" The constructor of the object being executed. Here are two initializations:
Class initialization: the compiler collects the class's variable initialization statements and static initializers of the type into the cllinit method, which can only be called by JVM and ensures thread safety. You must ensure that its superclass has been initialized before re-initialization.
Not all classes have a () method, and the class does not have a () method under the following conditions: the class declares neither any class variables nor static initialization statements; the class declares class variables, but does not explicitly use class variable initialization statements or static initialization statements to initialize This class only contains class variable initialization statements for static final variables, and class variable initialization statements are compile-time constant expressions
To put it bluntly, the class initializes static variables, and final static goes directly into the constant pool, not in this step. This phase is actually the last stage of class loading. The initialization of the object represents the beginning of the life cycle of the object, but calls the constructor, including its contents.
3. About locks
Learn the basics of unlocking before you read the stack, and you all know the big difference between wait ()\ sleep (). The similarity is that both release CPU, but the difference is that wait () releases the lock while sleep () does not.
From the stack information of the above example, we can see the important information of the lock: 1. When a thread holds a lock, the thread stack will print-locked (a java.io.InputStreamReader) 2, when a thread is waiting for another thread to release the lock,-waiting to lock 3 will be printed in the thread stack, when a thread holds a lock but executes to the lock's wait (). Locked is printed first in the thread stack, and then-waiting on is printed.
From the interpretation, we can see that the lock has three important feature words: locked,waiting to lock,waiting on. By understanding these three feature words, we will be able to analyze the lock. Most of them have one-waiting to lock must have a locked, but sometimes you can't find the locked because there is a window between one thread releasing the lock and another thread being awakened, and that's what happens to the dump.
4. About statu
Many types of problems can be analyzed with the help of the stack. CPU consumption analysis is an important part of the stack. First take a look at the thread flow diagram of java (the figure comes from the network).
Let's talk about the thread state in the stack.
RUNABLE from the JVM point of view, this state is that the thread is running, but, in fact, he does not necessarily consume CPU, may be in the network IO, when most threads are suspended, only when the data arrives, the thread will be awakened again, hang occurs in the local code (Native), JVM actually does not know (but not to call wait/sleep).
As an example, the following stack shows that the current program is reading data from the network.
Thread-39 "daemon prio=1 tid=0x08646590 nid=0x666d runnable [5beb7000..5beb88b8] java.lang.Thread.State: RUNNABLEat java.net.SocketInputStream.socketRead0 (Native Method)
The following stack shows that the thread is actually consuming CPU
Thread-444 prio=1 tid=0xa4853568 nid=0x7ade runnable [0xafcf7000..0xafcf8680] java.lang.Thread.State: RUNNABLEat org.apache.commons.collections.ReferenceMap.getEntry (Unknown Source) at org.apache.commons.collections.ReferenceMap.get (Unknown Source)
TIMED_WAITING (on object monitor) indicates that the current thread is suspended for a period of time and is executing the obj.wait (int time) method
"JMX server connection timeout 16" # 16 daemon prio=5 os_prio=0 tid=0x000000001a59f000 nid=0x5a74 in Object.wait () [0x000000001b7df000] java.lang.Thread.State: TIMED_WAITING (on object monitor) at java.lang.Object.wait (Native Method)-waiting on (a [I) at com.sun.jmx.remote.internal.ServerCommunicatorAdmin$Timeout.run (ServerCommunicatorAdmin.java:168)-locked (a [I) at java.lang.Thread.run (Thread.java:748)
TIMED_WAITING (sleeping) indicates that the current thread has been suspended for a period of time, that is, the Thread.sleep (intt ime) method is being executed
"Comm thread" daemon prio=10 tid=0x00002aaad4107400 nid=0x649f waiting on condition [0x000000004133b000.. 0x000000004133ba00] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep (Native Method) at org.apache.hadoop.mapred.Task$1.run (Task.java:282) at java.lang.Thread.run (Thread.java:619)
TIMED_WAITING (parking) the current thread is suspended for a period of time, that is, it is executing the Thread.sleep (int time) method, which is the sleep that executes the local method.
"RMI TCP" daemon prio=6 tid=0x0ae3b800 nid=0x958 waiting on condition [0x17eff000..0x17effa94] java.lang.Thread.State: TIMED_WAITING (parking)
WAINTING (on object monitor) the current thread is suspended, that is, the obj.wait () method (the wait () method with no parameters) is being executed
"IPC Client" daemon prio=10 tid=0x00002aaad4129800 nid=0x649d in Object.wait () [0x039000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait (Native Method)
SO, threads in TIMED_WAITING and WAINTING states must not consume CPU. The thread in RUNNABLE should judge whether it consumes CPU. If it is a pure Java operation code, it consumes CPU. If it is a network IO, it rarely consumes CPU. If it is the native code, combined with the nature of the local code (you can get the local thread stack through pstack/gstack), if it is pure operation code, it consumes CPU, if it is suspended, it does not consume CPU, if it is IO, it does not consume much CPU.
After reading the above, do you have any further understanding of how to analyze ThreadDump problems? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Mobile to-do list, deal with work orders anytime and anywhere
© 2024 shulou.com SLNews company. All rights reserved.