NetBeans IDE 6.7 with the C/C++ plugin provides new tools for observing your projects while
they run so that you can detect runtime problems in your applications. These types
of problems might not be detectable when debugging your code. The tools show CPU
usage, Memory usage, and Thread usage information in graphs which you can click
for more detailed information about problem areas.
This tutorial demonstrates how to use NetBeans IDE profiling for C/C++ projects.
The C/C++ profiling tools work automatically when
you are running your NetBeans project on Solaris, OpenSolaris, and Linux operating
systems. The profiling tools gather information about the
project's runtime, using external data providers running in the operating system where the project
is running. The data providers necessary for detailed information are not available on Windows platforms.
If you run the project on the Solaris or OpenSolaris OS, the IDE can use the Solaris DTrace utility
or Sun Studio's collector utility as a data provider.
If you run the project on Linux, you can use Sun Studio as the data provider,
as DTrace is not available on Linux currently. Sun Studio tools are free to download.
Platform Support for NetBeans C/C++ Profiling
This table shows the operating systems platforms and underlying data providers
that can be used to gather profiling information.
Windows
Mac OS X
Linux
Solaris
Simple indicators
no
no
yes
yes
Sun Studio
no
no
yes
yes
DTrace
no
no
no
yes
If you are a Windows user and have a system running Solaris, OpenSolaris, or Linux available to you, note that you
can run the IDE locally on your Windows PC and set up for remote development on the
Solaris, OpenSolaris, or Linux system. Remote development would enable you to use the
profiling tools even when you run the IDE in Windows. See the
C/C++ Remote Development Tutorial for more information about how
to set up to build on a remote development host.
This tutorial uses a project running on a Solaris SPARC processor-based system using the DTrace
utility as the data provider, but includes instructions for using Sun Studio as the data provider as well.
If you are on a Linux platform, you should download Sun Studio tools. Sun Studio tools are free to download from
http://developers.sun.com/sunstudio and
can also be installed in OpenSolaris using Package Manager to install the sunstudio package.
Adding Sun Studio Tools to the NetBeans IDE
If you install Sun Studio tools after you install the NetBeans IDE,
or the Sun Studio tools are not on your path,
you may need to tell the IDE where the Sun Studio tools are located, as described in
the following steps. If you are not using Sun Studio tools, you can skip this procedure.
Choose Tools > Options.
In the Options dialog box, click C/C++ near the top of the dialog box.
Click the Build Tools tab. In this tab, the Tool Collection displays the tool collections
found on the current development host.
The default collection is listed in bold type, and is used for any new project you create.
Select the Development Host where you want to add the tool collection. In this example,
we are running a Solaris local host.
Click Add under the Tool Collection list.
In the Add New Tool Collection dialog box, type the base directory path for the Sun Studio
collection in the Base Directory text field, or use the Browse button to navigate to the
path of the Sun Studio bin directory. Most likely, Sun Studio is installed
in /opt/sunstudio12, /opt/sun/sunstudio12,
or /opt/SUNWspro, depending on the version of Sun Studio and the OS.
If the Development Host is not localhost, the Browse feature is not
available for navigation to the tools directory. You must type in the complete path to the base
bin directory of the tools on the remote server. Click Check to have the IDE check
that the path you have entered is valid for a supported tool collection.
The Tool Collection Family and Tool Collection Name are filled in automatically. You can specify a
different name for the collection if you want.
Click OK in the Options dialog box.
Create a C/C++ Project for the Tutorial
To explore the C/C++ profiling features, we'll create a new project from
the ProfilingDemo sample application, which is included in the IDE.
Open the New Project wizard by choosing File > New Project.
In the wizard, select the Samples category, and then the C/C++ subcategory.
Select the Profiling Demo sample as shown in the figure and click Next.
You can choose the name of the project and the location of the project if you want. We
use the default ProfilingDemo_1 in our NetBeansProjects directory.
Click Finish to exit the wizard and create the project.
Set the Project Properties
Right-click the ProfilingDemo_1 project node in the Projects tab, and select Properties.
Select the Build node in the Categories panel.
Select GNU or SunStudio_12 as the Tool Collection and click Apply.
Select the Profile node in the Categories panel. For the Data Provider, select
DTrace from the list. Be sure that Profile on Run is selected, and that
all the indicators are selected: CPU Usage, Memory Usage, and Thread Usage.
Click OK.
Build and Run the ProfilingDemo Project
Right click the ProfilingDemo project node and select Build.
The Output tab shows the results of the build, similar to that shown below.
Notice that the compiler is the GNU C compiler, gcc.
Right-click the ProfilingDemo project node and select Run.
An external terminal window opens to show
output from the program.
The Run Monitor tab opens to display dynamic graphs for CPU Usage, Memory Usage, and
Thread Usage.
Notice in the external terminal window the ProfilingDemo program tells you what it is doing
so you can match it to the data that the IDE is representing graphically in the tools. For example,
the program displays how much memory it is allocating, performs calculations, and
then frees the memory. You can see the graph reflect the program's activity.
Explore CPU Usage
The CPU Usage graph shows the percentage of the total CPU time used by your application
during its run.
Click the CPU Usage graph to display details about the CPU time.
The CPU Time Per Function tab opens to display the functions of the program, along with the CPU time used
by each function. The functions are listed in order of CPU time used, with the functions that use the most time
listed first. The time initially displayed is the amount of time consumed at the moment you clicked the graph.
Click the refresh button, located to the left of the Function Name column, to update the display.
Click the Function Name column head to sort the functions alphabetically.
Click the CPU Time (Exclusive) to sort the functions by the order of time used by the individual functions.
Notice the difference between the two columns of CPU Time. CPU Time (Inclusive) shows the total CPU time
spent from the time the function is entered until the time it is exited, including the time of all other functions
that are called by the listed function. CPU Time (Exclusive) shows the time used only by the specific
function, not including any functions that it calls.
Click the CPU Time (Inclusive) column label to place the most time-consuming function back at the top.
Notice that the main function has 0 CPU Time (Exclusive), which means that all its CPU time
was actually used by other functions that it calls.
Double-click the work_run function. The common.c file opens,
with the cursor resting on the work_run function, line 149.
Explore Memory Usage
The Memory Usage indicator shows how your project's memory heap changes over its runtime. You can use it
to identify memory leaks, which are points in your program where memory that is no longer needed fails to
be released. Memory leaks can lead to increased memory consumption in your program. If the program runs
long enough, it can eventually run out of usable memory.
Click the Memory Usage graph to display details about memory leaks in your program.
Only functions that are producing memory leaks are listed in the table. The leak locations shown are
those that exist at the moment you clicked the graph. More leaks may
occur as time goes on. If no memory leaks are detected by the end of the run, the Memory Leak Details
tab indicates that no memory leaks were found.
Click the refresh button, located to the left of the Leak Locations column,
to update the display to list any more memory leaks while the program runs.
Click the column headings to sort the table rows.
Double-click one of the leak locations in the table, and the source file
will open with the cursor resting
on the line in which the memory leak occurs. You can fix the error here, save the project,
and run the project immediately to see if your fix solved the problem.
Explore Thread Usage
The Thread Usage indicator shows the number of threads in use by your program, and any
moments where a thread has to wait to get a lock in order to proceed with its task. This data
is useful for multithreaded applications, which must perform thread synchronization in order
to avoid expensive wait times.
Click the Thread Usage graph to details about thread locks in your project.
The Thread Synchronization Details tab opens and lists functions that had to
wait to obtain a mutex lock, and the number of milliseconds spent waiting.
Click the refresh button, located to the left of the Function column,
to update the display with the latest thread lock information.
Click the Wait Time column to sort the functions in order of time spent waiting.
Click the Lock Waits column to sort the functions by the number of times a thread
was waiting in the functions.
Double-click the function with the most lock waits to open the relevant source file and go
to the function in that file.
When you find and fix the thread synch error, you can save the project and run it to determine
immediately by looking at the Thread Usage tool if your fix has solved the problem.
See Also
A tutorial written by a NetBeans developer
may be helpful for troubleshooting problems you might run into when using the profiling tools, such as insufficient DTrace privileges.
The tutorial also shows an example of running on Linux platforms.
Note that the tutorial refers to the tools by an earlier name, Observability Tools.
Summary
This tutorial demonstrated the following points about the C/C++ Profiling tools:
Profiling information is collected on Linux and Solaris platforms automatically
Graphical profiling infomation is displayed in the Run Monitor tab
Clicking the graphs causes detailed information about the program's functions to display
Clicking functions in the detail tabs causes the source file containing the function to be opened at the line number where the function is called
Profiling settings are located in project properties, in the Profile category
Profiling can be disabled or indicators can be selectively disabled in the project properties
Data providers are utilities outside of the NetBeans IDE that collect data, and can be selected in the project properties
If you have access to a system running Linux or Solaris platforms on your network, you can use the profiling tools
from your Windows or Mac desktop by setting up the Linux or Solaris system as a remote development host for building and running
your projects