Analyzing Threads |
4 |
![]() |
This chapter describes what you need to do to prepare your program for analysis, how to navigate the Thread Analyzer glyph hierarchy, and how to use Thread Analyzer's menu-driven interface.
Warning - If you are using C++ to create shared libraries, then you need to be careful not to use -Ztha on source files containing static constructors. These constructors will end up trying to record data before the data collection code has been initialized, causing your program to seg fault. Unfortunately, the most common thing that creates static constructors is including <stream.h>.
Basic Concepts
Thread Analyzer displays standard profiling information for each thread in your program, as well as metrics specific to a particular thread such as Mutex wait time and semaphore wait time.
Table 4-1 briefly describes each metric collected by Thread Analyzer.
Note - Thread Analyzer does not support metrics for forked programs.
Compiling and Instrumenting the Source
Compile your program with the -Ztha option. This option instruments your program; that is, it inserts instrumentation points at the beginning and end of each function.
The -Ztha option instruments C++ and Fortran programs as well as C programs. The C syntax looks like this:
Note - If you execute an instrumented program that calls fork, the forked process does not contribute trace data until it calls exec.
% cc -Ztha -o prog1 prog1.c |
Note - Do not use -Ztha on files containing definitions of the C++ generic "new" operator, signal handlers, malloc, or free.
Running an Experiment
Run the executable file to write trace data to files in a tha.pid directory, where pid is the unique process id of the particular invocation of the object.
Caution - Do not use Control-C to stop a running instrumented program. Control-C can leave the trace directory in an inconsistent state. A subsequent invocation of Thread Analyzer on that trace directory might not process its trace data.
Starting Thread Analyzer
To analyze the trace data collected for your program, start Thread Analyzer from the command line:
% tha & |
Optionally, you can direct Thread Analyzer to load a tha.pid trace data directory from the command line.
You can use Thread Analyzer's -exec option, in conjunction with the SunOS ls command, to specify a relative path name or full path name to the executable to be used in conjunction with a particular trace data directory.
Note - Thread Analyzer expects to find the executable in the same directory as the trace directory. There are known problems related to correctly finding the executable associated with a trace directory. If you encounter problems in this area, use the -exec command-line option to specify the path name to the executable.
% tha -exec /bin/ls tha.1234 & |
% tha tha.1234 -exec /bin/ls & |
The following example shows how to specify a relative path name:
% tha -exec ls tha.1234 & |
Figure 4-1 shows Thread Analyzer's main window:
Figure 4-1 Thread Analyzer Main Window
The Load button loads a trace directory. The View menu allows you to choose the metric you are interested in and whether to display the information as a table or graph. For more information see "Load Button" and "View Button" on page 49.
Exiting Thread Analyzer
From the Window Manager, select Quit to exit the Thread Analyzer session.
Loading a Trace Directory
If you did not specify the tha.pid trace data file in the command line, use the Load button to display a window, shown in Figure 4-2, where you can specify the path to your trace data directory.
Figure 4-2 Thread Analyzer Load File Window
Type the path name of the trace directory generated by the instrumented program, or click the icon of the desired trace directory in the displayed list. Then click Open. If Thread Analyzer does not recognize the path name you type, check for typographic errors and retype the path name.
Note - If you load a trace file with the command line, Thread Analyzer makes the Load button unavailable; that is, Thread Analyzer loads only one trace directory per session.
Navigating the Thread Analyzer Glyph Hierarchy
Thread Analyzer glyphs are ordered as shown in Figure 4-3.
Figure 4-3 The Thread Analyzer Glyph Hierarchy
The function level is the bottom of the tree. Thread Analyzer displays the function (thread children) glyphs in alphabetical order, from left to right. The children of a given thread are the procedures called within that thread. Use the scrollbar to scan function glyphs hidden from view (see Figure 4-4).
Figure 4-4 Scrollbar and Footer Help
The thread that contains main() is named with the program name; all other threads are named numerically in order of creation.
Thread Analyzer Menus
Thread Analyzer uses the following conventions for menus:
View Button
Use the View menu to specify the particular metric you are interested in and whether you wish to see it as a table or graph. You can apply a CPU time filter to the glyph display to show only those threads or functions that exceed or equal a particular threshold.
Figure 4-5 Thread Analyzer View Menu
Metric Graph Property Sheet
Graphs plot the value of a metric against wall-clock time.
Graph from the View menu.
See "Scenario 2: Identifying Initial Bottlenecks From Graphical Data" on page 60 for an example of how to display CPU time versus wall-clock time.
Note - Thread Analyzer allows you to display a maximum of ten graphs at a time and emits an audible beep if you attempt to simultaneously display more than the ten-graph maximum.
gprof Table
gprof tables display call-graph profile data in the form of a list of functions called by threads. Gprof Table from the View menu.
prof Table
prof tables display profile data for the program, threads, and functions. The program-level prof table shows the total time for the entire program. The thread-level prof table has an entry for each thread (with a total of all function calls made by the thread). The function-level prof table has an entry for each thread-function combination. Prof Table from the View menu.
Sorted Metric Profile Table Property Sheet
A sorted metric profile table shows a designated metric for all nodes for a particular glyph level. Sorted Metric Profile Table from the View menu.
Figure 4-7 Thread Analyzer Sorted Metric Profile Table Property Sheet
Metric Table
A metric table shows multiple metrics for a particular thread or function. For a function, the metrics are shown for calls of that function made by the thread that is that function's parent. Metric Table from the View menu.
Figure 4-8 Thread Analyzer Metric Table Property Sheet
Filter Threads by CPU
The filter shown in Figure 4-9 displays the thread glyphs whose percent of CPU time is equal to or greater than a designated threshold.
Figure 4-9 Thread Analyzer: Filter Threads by CPU Time
Filter Functions by CPU
The filter shown in Figure 4-10 displays the function glyphs whose percent of CPU time is equal to or greater than a designated threshold.
Figure 4-10 Thread Analyzer: Filter Functions by CPU Time
Thread Analyzer Usage Scenarios
This section contains scenarios demonstrating the use of Thread Analyzer.
Looking at CPU time is a good starting point to discover where your program is spending its time.
Note - called+self shows direct recursion; <cycle> shows indirect recursion.