corner imagecorner image
IDEPlatformPluginsDocs & SupportCommunityPartners

C/C++ Profiling - NetBeans IDE 6.8/6.9 Tutorial

Contents

Content on this page applies to NetBeans IDE 6.8
Requirements
Introduction
Adding Sun Studio Tools to the NetBeans IDE
Create a C/C++ Project for the Tutorial
Set the Project Properties
Build and Run the ProfilingDemo Project
Use the Indicator Controls
Explore CPU Usage
Explore Memory Usage
Explore Thread Usage
Manage Profile Configurations
See Also
Summary

Requirements

To follow this tutorial, you need the following software.

Software Version Required
Solaris or Linux Operating Systems OpenSolaris, Solaris OS version 10, recent Linux versions
NetBeans IDE (including C/C++ support) Version 6.8 or 6.9
Java Development Kit (JDK) Version 6
C and C++ compilers, make, gdb C/C++ Tool Collections Tested with NetBeans IDE

See the NetBeans IDE 6.9 Installation Instructions and Configuring the NetBeans IDE for C/C++/Fortran
for information about downloading and installing the required software.

Introduction

NetBeans IDE 6.9 with the C/C++ plugin provides 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 C/C++ profiling tools are:

  • CPU Usage
  • Memory Usage
  • Thread Usage
  • I/O Usage, which is only available on Solaris platforms
  • Thread Microstates, which is only available on Solaris platforms

The tools show usage information in graphs, and include buttons that you click for more detailed information about problem areas in your application.

This tutorial demonstrates how to use NetBeans IDE profiling for C/C++ projects, with an emphasis on the CPU Usage, Memory Usage, and Thread Usage tools.

The I/O Usage tool is covered in I/O Profiling for C/C++ Projects on Solaris Operating Systems.

The Thread Microstates tool is covered in Using the Thread Microstates Tool on Solaris Operating Systems.

Platform Support for NetBeans C/C++ Profiling

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 facilities running in the operating system where the project is running. The facilities necessary for detailed information are not available on Windows or Mac platforms.

This tutorial uses a project running on a Solaris SPARC processor-based system with Sun Studio tools. However, the Sun Studio tool collection is not required on Solaris platforms because the profiling tools can also use the Solaris DTrace utility to obtain information about running programs. If you are using the OpenSolaris OS, you can easily install the Sun Studio software through the Package Manager or by using the command pkg install sunstudio in a terminal window.

If you are using a Linux platform, you must download Sun Studio software to use the C/C+ profiling tools in the NetBeans IDE. Sun Studio tools are free to download from http://developers.sun.com/sunstudio.

This table shows the operating systems and tool collections that support the C/C++ profiling tools.

Solaris Linux Notes
CPU Usage checkmark checkmark On Linux, requires Sun Studio tools to be available.
Memory Usage checkmark checkmark On Linux, requires Sun Studio tools to be available.
Thread Usage checkmark checkmark On Linux, requires Sun Studio tools to be available.
I/O Usage checkmark x mark Uses Solaris DTrace, not Sun Studio tools.
Thread Microstates checkmark x mark Uses Solaris DTrace, not Sun Studio tools.

If you are a Windows or Mac 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 or Mac 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.

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.

  1. Choose Tools > Options.
  2. In the Options dialog box, click C/C++ near the top of the dialog box.
  3. 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.
  4. Select the Development Host where you want to add the tool collection. In this example, we are running the IDE on a Solaris local host.
  5. Click Add under the Tool Collection list.
  6. 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.

  7. The Tool Collection Family and Tool Collection Name are filled in automatically. You can specify a different name for the collection if you want.


  8. 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.

  1. Open the New Project wizard by choosing File > New Project.
  2. In the wizard, select the Samples category, and then the C/C++ subcategory.
  3. Select the Profiling Demo sample as shown in the figure and click Next.

  4. Screenshot for creating
      new project from Profiling Demo sample

  5. 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.
  6. Click Finish to exit the wizard and create the project.

Set the Project Properties

  1. Right-click the ProfilingDemo_1 project node in the Projects tab, and select Properties.
  2. Select the Build node in the Categories panel.
  3. Select SunStudio_12 as the Tool Collection and click Apply.
  4. Screenshot for
            selecting project's tool collection


  5. Select the Run node in the Categories panel. For the Console Type, select Output Window and click Apply. This enables you to see the program output in the IDE's Output Window instead of an external terminal window, as shown in this tutorial.
    Screenshot for
      selecting project's output window
  6. Select the Profile node in the Categories panel. Select Show profiling indicators during run.
  7. For the Profile Configuration, select C/C++ SunStudio Standard.

  8. Screenshot for
      selecting project's Profile options

  9. Click the ... button next to the Profile Configuration list to open the Profile Tools Manager dialog box. Notice the tools selected for the C/C++ SunStudio Standard configuration are Thread Usage, Memory Usage, and CPU Usage. If you select other Profile Configurations, you can see that different combinations of tools are selected for each one. For this tutorial, we are using the C/C++ SunStudio Standard configuration.

  10. Screenshot for
      Profile Tools Manager dialog box

  11. Click Cancel in the Profile Tools Manager to exit without changing anything.
  12. Click OK in the Project Properties dialog box.

Build and Run the ProfilingDemo Project

  1. Right click the ProfilingDemo project node and select Build.
  2. The Output tab shows the results of the build, similar to that shown below.



  3. Notice that the compiler is the Sun Studio C compiler, cc. The argument mismatch compiler warnings can be safely ignored.
  4. Right-click the ProfilingDemo project node and select Run.
  5. The Run Monitor tab opens to display indicators with dynamic graphs for CPU Usage, Memory Usage, and Thread Usage.


  6. Notice in the output 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.
  7. Press Enter each time you are requested, until the program is finished.
  8. Place your mouse cursor over the indicators to see tooltips explaining what each graph represents.
  9. Each indicator includes a button for more detailed information, which we will explore later in this tutorial. First we should look at the indicator controls.

Using the Indicator Controls

  1. At the bottom of the Run Monitor window, you can see sliders for controlling your view of the graphs: View slider, Details slider, and Time slider. The sliders are labeled in the screenshot below. Place your mouse cursor over the end points of the sliders for information about the sliders.
    Closeup of timeline controls
  2. Click and hold the mouse button on the Time slider, the horizontal scroll bar located at the bottom, and drag the slider to the left to see the beginning of the run. All the graphs slide in unison so you can see what happened in each area (CPU, memory, threads) at any given time, and see the relationships between them.
  3. Drag the time slider from left to right to see the complete run.
  4. Move your mouse pointer to the view slider, the controls that overlay the time units. The view slider controls let you select the part of the run time that's displayed in the indicator.
  5. Click and drag the handles of the view slider on the left, the start point, all the way to the beginning of the run. The indicators now show the entire run at once. The effect is similar to zooming out as far as possible. Notice that the horizontal scroll bar is not functional when you select the complete run time. You are already looking at all the data so there is nothing left to scroll.
  6. View slider shows complete run
  7. Now let's focus our attention closer. Drag the view slider's start point to about 40 seconds, or the point where the PTHREAD_MUTEX_DEMO starts as shown in the output window. As you drag the handle, the indicators zoom in to focus on the area from 0:40 to the end. Notice that the scroll bar can again be used to scroll back and forth in the run time.
  8. Place your mouse cursor over the end points of the orange colored Details slider for a description of how to use the slider. The Details slider controls enable you to select a portion of the runtime for examining the detailed information.
  9. Drag the Details slider's start point handle to about 40 seconds, and notice that the indicators are grayed out in the area in front of the start point. This gives a highlight effect to the graph between the start and end points.
    View slider shows zoom in

    When you click on any of the indicators' detail buttons (Hot Spots, Memory Leaks, or Sync Problems), the data for the area highlighted is shown in the details tab. So, these controls are for filtering data.
  10. Drag the start point back to the beginning so we can see all the data.

Explore CPU Usage

The CPU Usage graph shows the percentage of the total CPU time used by your application during its run.

  1. Click the CPU Usage Hot Spots button 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. If the program is still running, the time initially displayed is the amount of time consumed at the moment you clicked the graph.
  2. Click the Function Name column head to sort the functions alphabetically.
  3. Click the CPU Time (Exclusive) to sort the functions by the order of time used by the individual functions.
  4. 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.
  5. Click the CPU Time (Inclusive) column label to place the most time-consuming function back at the top. Notice that the work_run_usrcpu function has 5.994 CPU Time (Exclusive) and 6.034 CPU Time (Inclusive), which means that a small amount of its CPU time was actually used by other functions that it calls, but the work_run_usrcpu function itself used most of the time.
  6. Some of the functions are shown in bold text. You can go to the source file that calls those functions. Double-click the work_run_usrcpu function.
  7. The common.c file opens, with the cursor resting on the work_run_usrcpu function, line 59. Some numbers are displayed in the left margin for this line.
  8. Place your mouse cursor over the numbers in the left margin. The numbers are the same metrics for exclusive and inclusive CPU time for the function that are displayed in the CPU Time Per Function tab. The metrics are rounded to use less space, but the unrounded value is shown when you mouse over them. Metrics for CPU-consuming lines such as the for loop that does calculations within the work_run_usrcpu function are also shown in the common.c source file.
  9. Change the Time Filter start time to 0:40 by either typing the time and pressing Enter, or using the arrows to scroll through the seconds. The graphic indicators change just as they did when you moved the handles on the data filtering control. If you drag the handles, the Time Filter settings in the CPU Time Per Function tab are updated to match. More importantly, the data shown for the functions in the table is updated to reflect the filter so only the CPU time used during that time period is shown.
  10. You can also filter for data that meets a certain metric. Right-click on the CPU Time (Exclusive) metrics for work_run_usrcpu (5.994 in our example). Select Show only rows where > CPU Time (Exclusive) == 5.994. All the rows are filtered away, and only the row whose exclusive CPU time is equal to 5.994 is displayed.

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 a program with a memory leak runs long enough, it can eventually run out of usable memory.

  1. Slide the time slider in the Run Monitor to the left and right to see how the memory heap increases and decreases over time. There are four spikes in usage in our run of the project. The first two occur during the Sequential Demo, the third occurs during the Parallel Demo, and the last occurs during the Pthread Mutex Demo.
  2. Click the Memory Leaks button to display the Memory Leak Details window, which shows details about which functions exhibit memory leaks.
    Only functions that are producing memory leaks are listed in the table. If your program is running when you click the button, the leak locations shown are those that exist at the moment you clicked the button. More leaks may occur as time goes on, so you should click the Refresh button. If no memory leaks are detected by the end of the run, the Memory Leak Details tab indicates that no memory leaks were found.
  3. You can change the Start and End times to filter the data, or use the orange colored details slider in the Run Monitor window, just as you did earlier in this tutorial in the CPU Time per Function window.
  4. In this run, the ProfilingDemo program shows a memory leak associated with a work_run_getmem function.
  5. Double-click the work_run_getmem function and the common.c file opens at the line where the memory leak occurs in the function.
  6. The memory leak metrics are displayed in the left margin in the source editor. Mouse over them to display the details as we did with the CPU Usage metrics.
  7. Right-click the metrics in the table to filter the data in the table. This data filtering in the table is possible with all the profiling tools.
  8. Filtering data in the table

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.
  1. The Thread Usage indicator shows the number of threads running during a project's run time. Slide the time slider back to the beginning and notice that the number of threads is 1 until about 0:40 seconds when it increases to 3 threads.
  2. Move the view slider endpoint handle to about 0:40 seconds so that you can see 0:00 through 0:40 at one time in the indicator.

  3. Look at the CPU Usage and Memory Usage indicators for the same time period of 0:00 to 0:40 seconds and see that the single thread is performing some activity that uses CPU time and memory. This period corresponds to the Sequential Demo portion, in which the main thread writes to a file and then performs some calculations, sequentially. CPU and memory usage both decrease when the program waits for the user to press Enter, and the number of threads remains at 1.
  4. Slide the time slider to the right so that you can see two points where the threads increase to 3.
  5. The increase in threads around 40 seconds corresponds to the Parallel Demo portion of the project run, where the main thread starts two additional threads to do the work of writing to a file and performing calculations, in parallel. This completes shortly after the 50 second mark. Notice the memory usage and CPU usage are a bit higher during this portion, but the two tasks are completed in much less time.
  6. Notice the number of threads returns to 1 after the Parallel Demo threads finish, and the main thread waits for the user to press Enter.
  7. The thread count increases to 3 as the Pthread Mutex Demo portion of the program runs.
  8. Notice that shortly after the thread count increases to 3, a lock wait appears, shown in orange. The Pthread Mutex Demo uses mutual exclusion locks to prevent overlapping access to certain functions by multiple threads. This is what causes the threads to wait to obtain a lock.
  9. Click the Sync Problems button to display 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. Also displayed are metrics for the number of milliseconds that the function spent waiting and the number of times the function had to wait for a lock.
  10. If you click the Sync Problems button while the program is running, you might need to click the refresh button, located to the left of the Function column, to update the display with the latest thread lock information.
  11. Click the Wait Time column to sort the functions in order of time spent waiting.
  12. Click the Lock Waits column to sort the functions by the number of times a thread was waiting in the functions.
  13. Double-click the mutex_threadfunc function, which has the most lock waits. The mutex.c source file opens in the editor at the line where the pthread_mutex_lock function is called. This function is responsible for locking a memory location before the location is read or written, and must wait until no other thread has a lock on that memory.


  14. The metrics for Wait Time and Lock Waits are displayed in the left column in the source file. Place your mouse cursor over the metrics to see the details, which matches what is shown in the Thread Synchronization Details tab.
  15. Right-click on the metrics and deselect Show Profiler Metrics. The metrics are no longer displayed in the source editor for any of the C/C++ profiling tools.
  16. Choose View in the IDE menu bar and select Show Profiler Metrics to display the metrics again.
  17. Setting to view metrics in source files

As you can see the CPU Usage, Memory Usage, and Thread Usage monitors can be used together to see what is happening in your program as it runs. However, if you don't want to see any of these indicators as your project runs, you can turn them off. In the project properties, select the Profile category and deselect the option Show profiling indicators during run.

Manage Profile Configurations

You can select which of the profiling tools you want to display when you run your projects by managing profile configurations.

Let's set up our own profile configuration so that only CPU Usage and Memory Usage are displayed when we run C/C++ projects.

  1. Select Tools > Profiler Tools to open the Profile Tools Manager dialog box.
  2. Profile Tools Manager Dialog Box

  3. From the Profiler Configurations list, select Manage Configurations...
  4. In the Profiler Configurations Manager dialog box, select C/C++ SunStudio Standard.
  5. Click Duplicate.
  6. Click Rename and type SunStudio CPU/Mem, then click OK.
  7. Creating a Profile Configuration

  8. Click OK in the Profile Configuration Manager dialog box.
  9. Select the new SunStudio CPU/Mem configuration in Profile Tools Manager dialog box.
  10. Deselect the Thread Usage tool in the list of tools and click OK.
  11. To use the configuration, right click the project node and select Properties.
  12. In the Project Properties dialog box, select the Profile category.
  13. For the Profile Configuration, select SunSutdio CPU/Mem and click OK.
  14. Selecting a New Profile Configuration

  15. Run the project and the CPU Usage and Memory Usage tools are displayed in the Run Monitor window.

See Also

The I/O Usage tool is covered in I/O Profiling for C/C++ Projects on Solaris Operating Systems.

The Thread Microstates tool is covered in Using the Thread Microstates Tool on Solaris Operating Systems.

Summary

This tutorial demonstrated the following points about the C/C++ Profiling tools:

  • Profiling information is collected on Linux with Sun Studio software, and Solaris platforms automatically with your choice of Sun Studio software or the Solaris DTrace utility.
  • Graphical profiling indicators are displayed in the Run Monitor tab
  • Clicking a button in an indicator 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 in the project properties
  • Profiling tools can be selected in Profile Configurations
  • If you have access to a system running Linux or Solaris operating systems on your network, you can use the profiling tools from your Windows or Mac machine by setting up the Linux or Solaris system as a remote development host for building and running your projects