Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / C++

Profiling of C++-Applications in Visual Studio for Free

Rate me:
Please Sign up or sign in to vote.
4.97/5 (48 votes)
11 Jul 2012CPOL4 min read 279.7K   6.5K   162   57
A method for profiling C++-applications with freely available tools (vsperfcmd, vsperfreport, and a C#-application for viewing the results)

Introduction

While the Team- or Premium-editions of Visual Studio contain a profiler, users of e.g. the professional edition are lacking such a feature. In this article, I present a method for profiling C++-applications with freely available tools and a simple application for presenting the results in an informative view.

profileWithVsPerfCmd/Screenshot2.png

Of course, the resulting solution will not be as comfortable as an integrated profiler, but if you use profiling only when you find bottlenecks, this method is certainly sufficient - and €5.000 cheaper.

The following method was tested with Visual Studio 2008 Professional and Visual C++ 2010 Express.

The Method

The method consists of four steps:

  • Generate a special "/PROFILE" build using Visual Studio (all editions can do this).
  • Profile the freshly compiled application with the freely available Microsoft tool "VSPerfCmd".
  • Convert the resulting .vsp-file to .csv-files using "VSPerfReport".
  • Analyze the resulting - huge - csv-files with the attached Profile Result Viewer.

Generate a /PROFILE-build

Note: The following steps should of course be done with a release build and with debugging symbols enabled.

In Visual Studio 2008, go to the solution explorer, select the project and open the properties page. In the section, "Configuration Properties", choose "Linker" - "Advanced".
Set "Profile" to "Enable Profiling information (/PROFILE)" (the exact steps for other versions of Visual Studio might differ).

profileWithVsPerfCmd/screenshot1.png

Compile the project.

Generate a Profile Report (vsp)

First of all, install the Visual Studio Performance Tools. The Visual Studio 2008 Service Pack 1 Stand-Alone Profiler can be found at this link.

The generation of the report requires to enter a handful of commands in the command line.

Preparations in the Commandline

Open the command prompt. Define a shortcut to the performance tools and switch to the folder, your application is located in:

set pt="C:\Program Files\Microsoft Visual Studio 9.0\Team Tools\Performance Tools"
cd [my_app_folder]

Profiling a Full Application Run

The simplest method is to profile the full uptime of the application.

:: Start the profiler
%pt%\vsperfcmd /start:sample /output:my_sampled_data.vsp

:: Launch the application via the profiler
%pt%\vsperfcmd /launch:my_app.exe

:: Shut down the profiler (this command waits, until the application is terminated)
%pt%\vsperfcmd /shutdown

Profiling only part of the application Uptime

It is also possible, to profile only part of the application uptime.

Here the handling becomes a bit tedious, so I suppose it would be best to implement some simple app to automize the following steps. Anyways, I never really had the demand for profiling only part of the uptime, since I could (maybe by modifying the application's startup code) always come up with code, that spent 90% of its time in the relevant part. Nevertheless, if you cannot modify your code adequately, here are the additional steps.

Start vsperfcmd as above and attach it to your application afterwards:

:: Start the profiler
%pt%\vsperfcmd /start:sample /output:my_sampled_data.vsp

:: Send the profiler to a waiting state
%pt%\vsperfcmd /globaloff

:: Attach to your application by specifying the pid
%pt%\vsperfcmd /attach:[pid]

After attaching to your application, perform whatever preparation is necessary. Now initiate the function that you want to profile and activate the profiler.

:: Activate the profiler
%pt%\vsperfcmd /globalon 

When the function is finished (or you think enough time has passed), stop profiling:

:: Detach the profiler from your application
%pt%\vsperfcmd /detach

:: Shut down the profiler
%pt%\vsperfcmd /shutdown 

Convert the Profile Report to a csv-file

The tool vsperfreport loads symbol-files (.pdb) and merges them with the generated report (.vsp) to create .csv-Files.

:: generate the report files (.csv)
%pt%\vsperfreport /summary:all my_sampled_data.vsp 

If you are also interested in calls within system files, you can also specify a path to a symbol server or a local cache.

%pt%\vsperfreport /summary:all my_sampled_data.vsp 
/symbolpath:"srv*C:\MySymbolCache*http://msdl.microsoft.com/download/symbols" 

View the Report

The csv-files above can in principle be opened with any spreadsheet application, nonetheless, they are typically huge, and not immediately helpful.

Therefore, I wrote a small application that parses one of the generated files (the one named "...CallerCalleeSummary.csv") and presents it as a tree view, ordered by total percentage. Starting from this view, it should be easy to find the cause for performance problems. For my part, I could uncover several bottlenecks in C++-legacy code, which I would never have found, just by looking at the code.

Using the Code

Attached is also a small C++-application for which I generated a profile. The generated .vsp and .csv-files are contained in the "Release"-subfolder. Opening "my_sampled_data_CallerCalleeSummary.csv" with ProfileResultViewer should resemble the screenshot I have given above.

Points of Interest

  • The profile report will show statistics only for functions, not for individual lines.
  • Functions that are inlined will not be contained in the profile, since they also never appear in the native call stack.
  • The presented treeview is not strictly hierarchic: If A calls B, then in the presented view B might seem to require more time than A. This appears, because total percentages are shown, i.e., it is not shown how much time B took, when it was called from A, but how much time B took in total. I am not sure if the csv-files contain enough information to recover the more detailed figures.

Of course, some of the steps above could be easily integrated into some GUI-Application, but for the moment the procedure I described above is sufficiently simple for me.

History

  • 11.01.2011: Initial version
  • 11.07.2012: Improved speed of ProfileResultViewer for large CSV-files (thanks to SteelixB for giving me the hint)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionVSPerf.exe requires Windows 8 & above OS version. Pin
Dattatraya Mengudale25-Mar-19 0:16
professionalDattatraya Mengudale25-Mar-19 0:16 
AnswerRe: VSPerf.exe requires Windows 8 & above OS version. Pin
akovar20-May-20 20:55
akovar20-May-20 20:55 
QuestionError VSP1335 :: Pin
vikasneedu25-Aug-15 20:07
vikasneedu25-Aug-15 20:07 
QuestionThank you Pin
jschimpf4-May-15 4:40
jschimpf4-May-15 4:40 
QuestionRetrieving function arguments Pin
Member 942148510-Apr-15 16:31
Member 942148510-Apr-15 16:31 
Questionthanks for the sharing habits , but youtube will be amazing thing Pin
anandssunku24-Sep-14 22:19
anandssunku24-Sep-14 22:19 
QuestionDOES IT WORK for 64 bit machine, or there will be some differences? Pin
Member 78975156-Oct-13 8:45
Member 78975156-Oct-13 8:45 
AnswerRe: DOES IT WORK for 64 bit machine, or there will be some differences? Pin
Peter_Smithson4-Jun-14 4:06
Peter_Smithson4-Jun-14 4:06 
QuestionWhat about C#? Pin
w2rf3-Jun-13 5:18
w2rf3-Jun-13 5:18 
AnswerRe: What about C#? Pin
hofingerandi7-Jun-13 9:40
hofingerandi7-Jun-13 9:40 
QuestionGreat! Pin
COFF3-Apr-13 16:32
COFF3-Apr-13 16:32 
GeneralYou've done a great job! Pin
ecquadrat6-Feb-13 0:58
ecquadrat6-Feb-13 0:58 
QuestionA follow-up article Pin
Mattias Högström17-Jan-13 7:12
Mattias Högström17-Jan-13 7:12 
GeneralMy vote of 5 Pin
Mattias Högström29-Nov-12 23:24
Mattias Högström29-Nov-12 23:24 
GeneralMy vote of 5 Pin
Michael Haephrati21-Oct-12 8:23
professionalMichael Haephrati21-Oct-12 8:23 
GeneralMy vote of 5 Pin
Sergey Podobry14-Oct-12 22:35
professionalSergey Podobry14-Oct-12 22:35 
QuestionAlternative Pin
geoyar16-Jul-12 8:14
professionalgeoyar16-Jul-12 8:14 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA14-Jul-12 20:18
professionalȘtefan-Mihai MOGA14-Jul-12 20:18 
SuggestionWorking with huge CSV files Pin
SteelIXB11-Jun-12 1:11
SteelIXB11-Jun-12 1:11 
In a program I was profiling, I generated a huge CSV file. Opening this file basically causes the profile result viewer to process the tree view forever and become unresponsive. This has a knock on effect of causing all other running processes to run sluggish.

The solution is to only fill each node before it is expanded - Fill all top level nodes with a dummy node, and handle the BeforeExpand event on the tree view. In the event handler, remove the dummy node and fill with actual nodes.

The difference in time to display the tree is from 10 minutes to 5 seconds.
AnswerRe: Working with huge CSV files Pin
hofingerandi11-Jul-12 2:09
hofingerandi11-Jul-12 2:09 
SuggestionCall tre XSL Pin
Trigve10-Jun-12 23:08
Trigve10-Jun-12 23:08 
GeneralMy vote of 5 Pin
Member 766644014-Mar-12 6:00
Member 766644014-Mar-12 6:00 
QuestionFiltering by pattern in file name using wildcard Pin
Sergey Chepurin8-Feb-12 23:28
Sergey Chepurin8-Feb-12 23:28 
AnswerRe: Filtering by pattern in file name using wildcard Pin
hofingerandi10-Feb-12 8:44
hofingerandi10-Feb-12 8:44 
GeneralRe: Filtering by pattern in file name using wildcard Pin
Sergey Chepurin10-Feb-12 22:12
Sergey Chepurin10-Feb-12 22:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.