Thursday, June 23, 2011

Dumping the thread - Websphere Performance Analysis

There are numerous occasions when we land up working in a performance problem be it at a development box or a defect opened from Production or Performance testing environment. While there are good profiling tools to help us get to the root of the issue like JProbe or YourKit, they come with a price tag with them. If there's a constraint on the team budget you wouldn't be able to use these tools. You can still workaround and use the trial version but if you read through the fine prints in the license file, you and your project stakeholders can be in trouble. So, stay out if not officially working on evaluating on these tools.

Though it could be a handicap there are other options that can be of use. In this mail, I'm going to demonstrate how to get the thread dump for your Websphere (WAS) server for performance analysis.

What is a thread dump?
A thread dump is a list of all the Java threads that are currently active in a Java Virtual Machine (JVM). When the jvm receives the signal for the same, it collects all the thread statistics and outputs it to a .txt file.

How do I generate it?
The most reliable way to generate a thread dump in WAS is using wsadmin utility. The steps for the same are as follows:
1.Navigate to the bin directory
cd <was_root>/profiles/<PROFILE_NAME>/bin/

2. Connect to deployment manager using wsadmin script
wsadmin.bat -conntype SOAP -username -password

3. The above command opens a wsadmin prompt. Now set the object variable to be used for generating the dumps
wsadmin> set jvm [$AdminControl completeObjectName type=JVM,process=,node=,*]

4.Run this command:
wsadmin> $AdminControl invoke $jvm dumpThreads

5. If you want to force heap dump, run the following command:
wsadmin> $AdminControl invoke $jvm generateHeapDump

Besides, if you have unix based systems like Linux/Mac, you can generate threaddump by just running the command:
kill -3 <pid>.

use ps -ef | grep java or ps -ef | grep to get the process-id(pid).

If you run WAS in console mode in Windows, Ctrl+Break helps to generate the dump but I have never tried it before.

A sample for generating a thread dump in a machine with node 5184Node01 and hosted locally in port 9443 with user/pass (ADMIN/password) is as follows:
wsadmin.bat localhost 9443 -username ADMIN -password password
The following commands will be run in wsadmin prompt.
set jvm [$AdminControl completeObjectName type=JVM,process=server1,node=5184Node01,*]
$AdminControl invoke $jvm dumpThreads

How to read the logs?
There are several ways to do it. Doing it manually is one of the most painful thing. IBM alphaworks has a cool tool for the same - IBM Thread and Monitor Dump Analyzer for Java shortly called as JCA. The ReadMe html inside the jar and the FAQ section talk a lot about the usage and interpretation of the data. I have used this a lot in the past and it helped us fix a lot of problems.

All said and done, try them in your leisure or when you are dealing with performance problems.

Thursday, June 16, 2011

Effective Enums - 1

I'm planning to start a small series of one of my favorite inclusions in Java 5 - Enums. Joshua Bloch in his book Effective Java has dedicated an entire chapter on the same. This series will also be loosely based on the same.

In short, I want to have a Loan constant that can help me get the interest rate, maximum repay period and to check if one is eligible provided you enter the age and credit score. Here is my implementation in Java 5 for the same using enum.

Now, there are some constraints for a developer on the environment he works. Lets say if you are forced to work on Java 1.4, how do we implement this feature.

Lets create a new interface for Enums

Lets now implement our Loan enum here:

Phew! Its done.

Wednesday, June 8, 2011

Answer:Why is toArray() a generic method in Java Collections

At the outset, a special thanks to Joshua Marotti and Kevin from CinJUG to help me get to the solution. This is the solution to the question of my earlier post: Why is toArray() a generic method in Java Collections.

Consider the following scenario:

class Bar{}

class Foo extends Bar{}

foos = new ArrayList();
foos.add(new Foo());
foos.add(new Foo());

Bar[] bars = new Bar[ foos.size()];
bars = foos.toArray(bars);

This works fine with the current code but will fail at compile if the method use the class level parameterized type as I had mentioned in the previous post. This is the use case why the method is declared generic. However, it doesn't prevent us from writing the below code which would compile just fine but fail at runtime with ArrayStoreException.

String[] strings = new String[foos.size()];
strings = foos.toArray(strings);

Per Joshua Bloch, we should follow PECS while using generics. Producer Extends, Consumer Super. Its probably the reason, the constructor for classes implementing collections has the extends parameter.

public ArrayList(Collection<? extends E> c)

Unfortunately, the language allows only wildcards for super. If it had supported type declaration with super, we could've solved the ArrayStoreException with the following usage.

<T super E> T[] toArray(T[] a)

On the other hand, I see Scala offers something on this line in scala.collection.immutable.List
def copyToArray [B >: A] (xs: Array[B]): Unit

There's a fabulous explanation on codeidol that discusses generics and collections in a very deep sense (suggested by Joshua Marotti). The chapter about "reification" that has some talks on toarrays and why it is the way it is and is a great read: