Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Profiling Your Application

Profiling your application enables you to determine which areas of your code are working efficiently. To accomplish this, you must use the profiler. The profiler is an analysis tool used to examine the runtime behavior of your programs. This analysis tool produces feedback exposing sections of code that are taking a long time to execute or are not being executed at all.

Remember that profiling is a tuning process, not a one-shot deal. The intention of this tool is to make your programs run better. It is not meant to find bugs or to be used for debugging purposes.

The main reasons for using the profiler follow:

The first reason represents a timing issue. The second represents a counting issue, and the third represents a coverage issue.

You can use two types of profiling to analyze code:

Building the Project for Function Profiling

Before you use the profiler, you must build the project with profiling enabled. To enable profiling, do the following:

  1. From the menu bar, choose Project, Settings.
  2. Select the Link tab.
  3. From the Category drop-down list box, select General.
  4. Click the check mark in the Enable Profiling check box to enable the option.
  5. Click OK.
  6. Build the project.

Note that incremental linking is turned off when enabling the Enable Profiling check box. Clear the Enable Profiling option to reenable incremental linking.

Line Profiling

There are two types of line profiling:

Line profiling uses the debugging information from the .EXE file of the application to trigger the profiler.

To implement line profiling when building the project, follow these steps:

  1. From the menu bar, choose Project, Settings.
  2. Select the Link tab.
  3. From the Category drop-down list box, select General.
  4. Enable the Enable Profiling check box.
  5. Click OK.
  6. Build the project.
  7. From the menu bar, choose Project, Settings.
  8. Select the Link tab.
  9. From the Category drop-down list box, select General.
  10. Enable the Enable Profiling check box.
  11. Enable the Generate Debug Info check box.
  12. Select the C/C++ tab.
  13. In the Category drop-down list box, select General.
  14. In the Debug Info drop-down list box, select Program Database or Line Numbers Only.
  15. Click OK.
  16. Build the project.

After the build is complete, the project is ready to be profiled.

Profiling with PREP, PROFILE, and PLIST

Profiling consists of three separate programs:

The IDE runs all these programs for you after you choose the standard option from the Profile dialog box. The arguments are passed automatically from the PREP program.

Using batch files to run these programs enables you to get the most out of profiling. These files give you more options and control over profiling the applications. You can run the batch files from the command prompt or the Profile dialog box. PLIST output is routed to the output window when using the Profile dialog box. You can redirect the command-line output, such as to a file or to an output device.

The PREP program always is called twice, because there are two phases that are performed when profiling. The first phase is done before the actual profiling is done, and the second phase is done after the profiling.

To transfer information between profiling steps, three files are implemented: .PBI, .PBO, and .PBT.

.PBI is the file extension for the profiler batch input file. This file provides condensed information to the profiler. The first time the profiler is run on a program, the .PBI file is generated by the PREP program.

.PBO is the file extension for the profiler batch output file. This file is used as an intermediate file used by the profiler. Its function is to transfer information between profiling steps.

The .PBT (profiler) file is generated by the PREP program and is used as input to the PLIST program. With this file, the PLIST program generates a profile of the source code.

All these files are intermediate files used by the profiler to transfer information and data between profiling steps.

A profiler batch file might look like the following:

PREP /OM /FT /EXC test.lib %1
if errorlevel == 1 goto done
PROFILE %1 %2 %3 %4 %5 %6 %7 %8 %9
if errorlevel == 1 goto done
PREP /M %1
if errorlevel == 1 goto done
PLIST /SC %1 >%1.lst
:done

The batch file options will make more sense after you examine each of the programs.

PREP

The PREP program has two phases during a normal profiling operation. In phase 1, it reads an executable file and creates .PBT and .PBI files.

Phase 2 consists of reading .PBT and .PBO files, and then writing a new .PBT file for PLIST.

The syntax for PREP follows:

PREP [options][programname1][programname2...programnameN]

The command line is read from left to right. Table 15.2 provides a list of PREP options. Note that since the command is read from left to right, the rightmost options override the contradictory options to the left. None of the options are case-sensitive, and all must be prefixed by a forward slash or a dash. Options are separated by spaces.

Table 15.2. PREP Options

Option Phase Description
/? & 2 Provides information on PREP options.
/AT 1 Retrieves attribution data for function timing and counting. Function attribution reports which function called another function.
/CB 1 Used with function timing, enables you to set the calibrated overhead of profiler calls in case the function timing calls have varied because of calibrated overhead values. The calibrated overhead is displayed in default PLIST output.
/EXC 1 Excludes a specified module from the profile.
/EXCALL 1 Excludes all modules from the profile.
/FC 1 Designates function count profiling.
/FT 1 Designates function timing profiling.
/FV 1 Designates function coverage profiling.
/H 1 & 2 Provides information on PREP options
/INC 1 Includes the profile. Used in conjunction with /EXCALL.
/IO filename 2 Merges an existing PROFILE output file (.PBO). Up to eight .PBO files can be merged at one time.
/IT filename 2 Merges an existing PREP phase 1 output file (.PBT). Up to eight .PBT files can be merged at one time. Files from different profiling methods cannot be merged.
/LC 1 Selects line count profiling.
/LV 1 Selects line coverage profiling.
/M filename 2 Substitutes for the /IT, /IO, and /OT options.
/NOLOGO 1 & 2 Suppresses the PREP copyright message.
/OI filename 1 Creates a .PBI file. If the filename is not specified, the output file is programname1 .PBI.
/OM 1 Creates a self-profiling file with _LL or _XE function coverage, counting, and timing. This option makes profiling faster.
/OT filename 1 & 2 Specifies the output .PBT file. If the filename is not specified, the output file is programname1 .PBT.
/SF function 1 Starts profiling with function, where the function name must correspond to an entry in the .MAP file.
/STACK dpt   Sets the stack depth ( dpt ).

To specify individual library, object, and application source files, use the /INC and /EXC options.

To specify line numbers with source files, you would use syntax such as the following:

/EXCALL /INC example.cpp(12-40,50-80)

The following syntax specifies all line numbers from 80 on:

/EXCALL /INC example.cpp(80-0).

Only lines 12 to 40 and 50 to 80 are included from the file example.cpp.

If you need to specify all the lines in a module, you can specify the .OBJ file as the following:

/EXCALL /INC example.obj
/EXCALL /INC test.cpp(50-0)

PROFILE

The PROFILE program profiles an application and stores the results in a .PBO file. PROFILE is used after a .PBI file is created with PREP.

The syntax for PREP follows:

PROFILE [options] programname [programargs]

The command line is read from left to right. Table 15.3 provides a list of PROFILE options. Note that because the command is read from left to right, the rightmost options override the contradictory options to the left. None of the options are case sensitive, and all must be prefixed by a forward slash or a dash. Options are separated by spaces.

If a .PBO filename is not specified on the command line, the base name of the .PBI file with a .PBO extension is used. If neither a .PBI nor a .PBO file is specified, a program name with .PBI and .PBO extensions is used.

Table 15.3. PROFILE Options

Option Description
/? information on PREP options.
/A Appends any redirect error messages to an existing file. If the /E option is used without the /A option, the file gets overwritten. This option is valid only with the /E option.
/A Appends any redirect error messages to an existing file. If the /E option is used without the /A option, the file gets overwritten. This option is valid only with the /E option.
/E profiler error messages to filename.
/H Provides information on PREP options.
/I filename Specifies a .PBI file to be read. This file is created by PREP.
/IT filename Merges an existing PREP phase 1 output.
/NOLOGO the PROFILE copyright message.
/OI filename Creates a .PBI file. If the filename is not specified, the output file is programname1 .PBI.
/O Specifies a .PBO file to be created. The PREP utility is used to merge with other .PBO files or to create a .PBT file to use with PLIST.
/X Returns the exit code of the program being profiled.

On the PROFILE command line, the filename of the program that is to be profiled must be designated. If no extension is given, the PROFILE assumes the .EXE extension. The program filename can be followed with command-line arguments.

PLIST

The PLIST program converts the results from a .PBT file into a formatted text file.

The syntax for PLIST follows:

PLIST [options] inputfile

The command line is read from left to right. Table 15.4 provides a list of PLIST options. Note that because the command is read from left to right, the rightmost options override the contradictory options to the left. None of the options are case-sensitive, and all must be prefixed by a forward slash or a dash. Options are separated by spaces.

The results of PLIST are sent to STDOUT by default. You can use the greater-than (>) character to redirect the output to a file or device.

Table 15.4. PLIST Options

Option Description
/? Provides information on PLIST options.
/C count Designates the minimum hit count to appear in the listing.
/D directory Specifies an additional directory for PLIST to search for source files. To specify more than one directory, use multiple /D options on the command line. You should use this option when PLIST cannot find a source file.
/F Lists full paths in a tab-delimited file.
/FLAT Displays function attribution with no indentation when using function attribution.
/H Provides information on PLIST options.
/INDENT Displays function attribution information in indented form when using function attribution. If /FLAT or /TAB is not specified, this is the default option.
/L List mangled function names.
/NOLOGO Suppresses the PROFILE copyright message.
/PL length Sets the length of the page for output, where length is equal to the number of lines. If length is zero, page breaks are suppressed. Other than zero, length can be from 15 to 255. The default of length is 0 (zero).
/PW width Sets the width of the page for output. The width is equal to the number of characters. Width must range between 1 and 511, inclusive. The width default is 511.
/SC Sorts output by counts in descending order.
/SL Sorts output in the order in which lines appear in the file. This option is available only for line profiling. This option is the default setting.
/SLS Causes line count profile output to be printed in coverage format.
/SN Sorts output in alphabetical order by the function name. /SNS. Sorts output in alphabetical order by the function name and displays function timing and function counting information in function coverage format.
/ST Sorts output by time in descending order.
/STC Sorts output function+child time./T. Creates a tab- delimited database from the .PBT file to be exported into other applications. All other options, including sort options, are overridden when this option is used.
/TAB indent the tab width for indentation when using function attribution.
/TRACE execution trace if available.

You must run PLIST in the same directory in which the profiled program was compiled.

If a PLIST variable is not specified, default values for PLIST are supplied. The default used depends on the profile type.

Table 15.5 shows the default values used when the PLIST environment variable is not specified.

Table 15.5. Default Values Used with PLIST

Profile Type Sort Option Hit Count Option
Function timing /ST /C 1
Function counting /SC /C 1
Function coverage /SN /C 0
Line counting /SL /C 0
Line coverage /SLS /C 0

Share ThisShare This

Informit Network