Troubleshoot CPU Utilization Using Profiler (Windows Software Development)

This applies to Windows platform.

To determine bottlenecks, you need a profiler.


Visual Studio Profiler is only available from Visual Studio® Team System Development Edition or Visual Studio Team Suite

It is not available under Visual Studio Professional Edition.

However, there is a free one provided by Microsoft itself and is called 'Visual Studio 2008 Service Pack 1 Stand-Alone Profiler'. It is not integrated with your Visual Studio and hence it will only profile exe instead of the source code, you just need to compile the program into exe and then run with profiler.

Since it is not integrated with Visual Studio, it will not be able to open the generated output file (.vsp), you need to generate vsp file into csv files which you can then open it using Microsoft Excel.

Download here.

To install it, just run the exe file in order to install it using GUI.

And installation is successful.

The profiler and associated files will be installed at the following location (with assumption that you have installed Visual Studio 9.0 on C:\Program Files\Microsoft Visual Studio 9.0\)

C:\Program Files\Microsoft Visual Studio 9.0\Team Tools\Performance Tools

After installation is successful, let's try to do profiling on the this sample code (VB.NET Visual Studio 2008 SP1).

Also refer to 'How to reduce CPU Utilization (Windows Software Development) ?'

Before proceeding to using profiler, please make sure you run CPU-Sample-2.exe and click 'Generate Data' first (so that data is generated to the database). Then close the program. And before that, please make sure that appropriate database and tables are created.

Running Profiler

You have to run it using command prompt.

If you are using Windows Vista, make sure 'Run as administrator'.

1. Set path

set path=%path%;C:\Program Files\Microsoft Visual Studio 9.0\Team Tools\Performance Tools

2. set the appropriate environment variables

'VSPerfCLREnv /sampleon'

3. Go to the application exe directory and start sampling. Save the output file as 'CPU-Sample-2-1.vsp'

Start 'Non Optimized' and run 'Run using custom threadpool'.

4. Make sure the process is finished running.

Then close 'CPU-Sample-2.exe'

5. Shutdown 'VsPerfCmd'

'VsPerfCmd /shutdown'

6. Sample off

'VsPerfCLREnv /sampleoff'

7. set path and create symbols repository (Assuming that you have created a location in the D drive D:\Workspace\Symbols).

'set _NT_SYMBOL_PATH=srv*D:\Workspace\Symbols*'

If you have run it for the first time, it will be downloaded to D:\Workspace\Symbols automatically.

8. Generate the report based on the '.vsp' file.

'VSPerfReport /U CPU-Sample-2-1.vsp /summary:all'

A set of cvs files will be generated.

9. Open generated report file (cvs) using Microsoft Excel.

Repeat the same process by running the 'Optimize Code', save the report file as 'CPU-Sample-2-2.vsp' and then generate another set of cvs files.

Analyzing Reports

Open CPU-Sample-2-1_FunctionSummary.cvs (Non-optimized)

Sort the record according to 'Inclusive Samples %' and 'Exclusive Samples %'

You will notice that what you can control (the codes for CPU-Sample-2) are having high CPU utilization.
  • CPU_Sample_2.Form2.RetrieveTransDB(class ADODB.Connection,class CPU_Sample_2.clsCustomer)
  • CPU_Sample_2.Form2.ReadData(class ADODB.Connection)
  • CPU_Sample_2.Form2._Lambda$__2(object)
  • CPU_Sample_2.Form2.TestRoutine2()

Now, compare with CPU-Sample-2-2_FunctionSummary.cvs (Optimized)

You will notice that those in the percentile of 40th to 50th will no longer be those which we have coded in CPU-Sample-2.exe.

While those four bottlenecks (listed above) discovered in CPU-Sample-2-1_FunctionSummary.cvs now have had the CPU utilization reduced.

More Reference

Refer to this URL for more reference.