- Some Basic Jargons.
- What is Thread dump and use of it?
- How can I get Thread dump?
- How to Analyse it?
Jargons:
Some of the Jorgan's before going to the details..
Thread: It is very important to know what thread means before going to this topic. According to text book, thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently of everything else that might be happening. Just leave it if you don't understand :)
Multi threaded application/process: An process may run as single thread or with multiple threads. For example Oracle DB runs as different processes. Each process can contain single or many threads. If a process run with many threads then we call it as multi threaded process. Generally using multi threads will help us to use the processor resource effectively. For example if your system has 4 processors and if your app is single threaded then at any point of time only one CPU(processor) will be used, rest all 3 will be in idle. So we are wasting the resources. If we redesign the app to make work with 4 threads then you will be using all the CPUs effectively.
Many of the applications that we are using will give an option to the user itself to tell how many threads that the process can use. The best example will be the application servers. For almost all the application servers, we can decide how many threads should it run with.
What is Thread dump?
Thread dump contains the complete list of threads details. These details includes...
- The Thread number or name.
- Thread state - running,waiting,blocked,idle etc
- Locks that it is holding and method under which this lock is holding.
- Method name which the thread is currently executing.
The structure of this thread dump differs from OS to OS and also application to application. As said above this dump is used to find mainly the lock contention and dead locks.
Lock Contention: In Simple words, if a thread is holding a lock for long time and another thread is waiting for this lock to get released then we call it as lock contention.
Deadlock: This is some thing father of Lock contention :) For example consider t1 is holding a lock for which t2 is waiting. And t2 is holding a lock for which t3 is waiting, and t3 is holding a lock for which t1 is waiting. Can you observe a chain over there. Now all 3 threads are waiting for a lock which is held by other thread and hence this state remains as it is for ever. Once the system comes to this state, everything will get stuck and application comes to an deadend. This is deadlock.
How can I get Thread dump?
I always work for Java/j2ee applications and hence let us see how thread dump is captured for JRocket JVM and Sun JVM.
NOTE: Important thing to be noted is, if you want to take a thread dump for a java process then make sure you don't use "-Xrs" in your JVM options.
BEA JRocket JVM:
JRocket JVM comes with an inbuild management console where an option is provided to get a thread dump. Even it gives all the thread locks and deadlocks in that dump at the end of display. Below is the exact process...
For detailed explaination of BEA Jrocket management console goto:
http://edocs.bea.com/jrockit/tools/usingjmc/start.html
Steps:- Start your JVM with -Xmanagement -Djrockit.managementserver.port=un-used port
- Start the JRocket Console using below command. This will open a console as displayed below
/console/ManagementConsole.jar- Click on connect and give necessary details and get connected to the JVM first.
- Now you get the thread dump by clicking View --> "View Thread stack dump".
As said earlier the beauty of this tools is, it will analyze the thread dump and gives the current lock contentions and deadlocks automatically with out breaking your head. The contention details that it gives is the max you can analyze even if you analyze manually.
Sun JVM:
If you are using jdk1.6 and above, then you are lucky enough to get dumps in faster way as below
- Set the jdk's bin path to PATH variable.
- Get the PID using command "jps".
- And run " jstack -l PID"
- Evan you can forward the dump to a file as "jstack -l PID > FileName"
- Windows OS and process is running in console mode in command prompt.
- Windows OS and process is running as service
- Download Sendsignal.exe from http://www.latenighthacking.com/projects/2003/sendSignal
- Find the process ID of your process. You can get this from task manager.
- Goto command prompt and execute below command...
This will print the thread dump in your application related logs. For example if you are using Oracle application server then the log will be printed in opmn.log. Just check all the logs where all the general errors are printed.
- Unix Operating system.
If you are using Unix or Linux then the process is very simple. Just find the process id by using "ps -ef" command and execute below command
In this case also the thread dump is printed under the standard logging files.
Analysing thread dump is a simple yet power full in fidning many performance bottlenecks. Please find my another posting on thread dump anlaysis...
No comments:
Post a Comment