Thread Profiling

Use of multiple threads is essential to building scalable and reliable applications. Working with multiple threads brings up other issues such as concurrency and contention over resources. Thread analysis provides information about the threads that are created and used by the application being profiled. The different analytical reports show the state of the threads and monitors which help you to detect race conditions, contentions and deadlocks if any in your application.

The threads views can be accessed from the Navigational Tree by clicking on the Thread Profiling node.

The types of Thread Reports generated by the AppPerfect Java Profiler are explained below.

Threads

Charts

Thread Chart information is useful for checking if the threads created are running as expected or if there is some Contention over the allocation or availability of resources.

The Thread Chart shows a listing of all threads with their names appearing on the left hand side of the view. Time is plotted on the horizontal axis. The colored lines represent live threads in the JVM being profiled. Colors indicate the status of the thread throughout its existence in the test and is one of green, orange and red.

In the Thread Chart profiling it is also possible to sort and view the threads based on certain specified conditions. There is a drop-down box provided where one can select an individual thread which is to be viewed in the frame. All the threads can further be sorted on the basis of the current state of the thread. The various states are: Running, Waiting, Blocked and Stopped. A Refresh Button is also provided which can be used to refresh the current view.

The Count tab shows graphically the number of threads at a given point of time. Time is plotted on the X-axis and number of threads or thread count on the Y-axis. The color-coded lines are indicative of thread states.

Status

Thread Status provides useful information to track the status of the different threads present in the profiled application. The Thread Status table shows the group containing the Thread, Parent Thread, Thread Name, Status of the Thread, Start Time and the End Time of the thread. The thread status report is displayed on selecting Status under the Threads node in the navigation tree.

You can sort the data based on any of the column headings so that data most relevant to your analysis is shown. To filter the data in the thread status table in the Filter on drop down menu select Thread Name, Group or Status for the filter to be applied, and also select the Not equal to or Equal to option from the drop down menu. Once you have applied the filter and entered the details click Filter. To clear the filtered data click Clear.

Details

Click a thread name to view Details of its status in various graphs.

Monitors

Monitor Details shows the details of the monitor classes. A monitor is a resource which two or more threads are trying to access concurrently. The AppPerfect Java Profiler shows three tabs with different details related to monitors:

Monitors

This tab shows various statistics related to the monitor.

Monitor ID

This is the unique ID for the monitor.

Monitor Class

This is the name of the monitor class.

Block Count

This is the number of times this monitor has been blocked by a thread.

Block Duration(ms)

This is the duration in milliseconds for which this monitor has been blocked.

Wait Count

This is the number of threads that are waiting for this monitor.

Wait Duration (ms)

This is the duration in milliseconds for which the monitor has been waited for.

Details

This tab shows the Contention History for monitors.

Start Time

This is the time at which the monitor was acquired by a thread.

End Time

This is the time at which the monitor was released by a thread.

Duration (ms)

This is the difference between end time and start time.

Status

This is the status of the thread - Owner, Waiting or Blocked

Monitor ID

This is the unique ID for the monitor.

Monitor Class

This is the name of the monitor class.

Affected Thread

This is the name of the thread affected by the monitor.

Snapshot

This tab allows you to take monitor snapshots and show details of same here.

Race Conditions

A race condition occurs when two or more threads are reading or writing some shared data simultaneously. In such situations, the resultant state of the shared data depends on the order in which the threads modify the shared data. Thus, race conditions can lead to non-deterministic state and unpredictable results from your program.

Potential race conditions can be diagnosed by analyzing the data provided by the Current monitor usage table. If two different threads are accessing a particular Monitor ID with Status being shown as Running for both the threads is a potential race condition. After identifying part of the code for the race condition you can do a detailed analysis of the code and correct the problem. The Current Monitor Usage table is displayed on selecting Monitor Details under the Threads node of the navigation tree.

The Current Monitors Usage table shows the Start time, Duration, Thread status, Monitor ID, Monitor Class, Affected thread and the Owner thread. You can sort the data based on any of the column headings so that data most relevant to your analysis is shown. To filter the data in the monitors statistics table in the Filter on drop down menu select any of the columns for the filter to be applied, and also select the Not equal to or Equal to option from the drop down menu. Once you have applied the filter and entered the details click on Filter. To clear the filtered data click Clear.

Contentions

Contention is a condition when two or more threads try to access a shared resource simultaneously. In such cases, the first thread that requests the resource gets it, while the other threads have to wait. This wait time can severely degrade performance, especially as the load on the system increases.

Contentions can be diagnosed by analyzing the data provided by the Monitor usage history table. If two different threads are accessing a particular Monitor ID with Status being shown for one thread as blocked and waiting for another, this situation of the threads is a contention After identifying part of the code for the contentions you can do a deeper analysis and correct the problem. The Monitor usage history table is displayed on selecting Monitor Details under the Threads node of the navigation tree.

The Monitors usage history table shows the Start time, Duration, Thread status, Monitor ID, Monitor Class and the Affected thread. You can sort the data based on any of the column headings so that data most relevant to your analysis is shown. To filter the data in the monitors statistics table in the Filter on drop-down menu select any of the columns for the filter to be applied, and also select the Not equal to or Equal to option from the drop-down menu. Once you have applied the filter and entered the details click Filter. To clear the filtered data click Clear.

Deadlocks

A deadlock occurs when in a set of processes or threads, each process is waiting for an event that only another process in the set can cause. Deadlocks are dangerous as they will simply cause your application to hang. The Deadlock occurred is described in a chart and a table giving the details of threads involved in the deadlock. The deadlock report's chart shows a diagrammatic representation of the deadlock found, the threads involved, and the threads affected by it.

The deadlock report's table has the columns Threads in Deadlock and Affected Threads. For example, there are three threads or computer programs running simultaneously. Now consider that each of the thread or program is trying share the same resource say a printer. Now each thread or program starts effectively preventing the other threads or programs from accessing the printer, resulting in all the thread or programs discontinue functioning.