Share via


CPU Performance Counters in Visual Studio 2010 Profiler

 

While using Visual Studio Profiler either from Visual Studio UI or through command line tools, you may wish to collect more/different information using specific CPU Performance Counters. The collected data depends on the profiling mode. As usage of CPU Performance Counters considered an advanced technique, let me explain what is the proper way to use them and how to interpret the results.

First of all, CPU Performance Counters are not operating system performance counters, as they are implemented by CPU vendors inside of a CPU die.

If you run inside of a Virtual Machine or on a Hyper-V enabled machine – you have no access to CPU Performance Counters (as I mentioned in my previous post). This is the current limitation of virtualization technologies.

Let’s review 2 basic modes of VS Profiler – Sampling and Instrumentation – and discuss how the counters are used in each one of them.

Sampling

In Sampling mode, the profiler periodically interrupts an execution of your application in order to take a sample of it. The regularity of interrupts is achieved through usage of one of the CPU Performance Counters. By default – it is 10,000,000 Non Halted CPU Cycles. You can change the predefined sampling interval or the counter itself. When you change the counter, that means that you switch to a different event as a trigger of your samples. If the default Non Halted Cycles event causes your samples being collected when your application is actively using CPU, then switching to Last Level Cache Misses event, for example,  will cause samples being generated on cache misses (so, if there are no cache misses – no data is collected).

VS Profiler supports sampling on only one counter at a time. Therefore, you can use only one counter when you configure your sampling session.

When you look through results of sampling, you will not see separate data columns with the counter name. Remember, that usage of specific counter in this scenario means that your samples were triggered by the counter event. So interpret your samples (inclusive, exclusive, function samples, module samples, call trees etc) appropriately.

To select a specific counter for your Sampling session in Visual Studio, go to Properties of your Performance Session (right click –> Properties), and select the Sampling tab. In Sample event drop down list switch default Clock cycles to Performance counter.

 image

Now you can select one of the provided counters and set its Sampling interval.

image 

The full set of counters provided by CPU vendors is quite big. VS Profiler suggests a sub-set of them, with an ability to add more counters to the selection if necessary. You will see all currently supported counters in VS UI. If you use command line tools for your data collection, you can see the counters through the following option:

C:\MyProjects>VSPerfCmd.exe /querycounters
Microsoft (R) VSPerf Command Version 10.0.30319 x86
Copyright (C) Microsoft Corp. All rights reserved.

Portable counters:
InstructionsRetired
NonHaltedCycles
L2Misses
L2References
ITLBMisses
BranchesRetired
MispredictedBranches
Platform counters:
DCULinesIn
DCUModifiedLinesIn
. . .

To use one of them, you need to provide the counter name in /COUNTER option of VSPerfCmd.exe when you start your data collection. VSPerfCmd.exe /? will give you an exact syntax. For example:

VSPerfCmd.exe /attach:MyApplication.exe /COUNTER:L2Misses,1000

Instrumentation

In instrumentation mode, VS Profiler records timestamps at function entries and exits. If you add CPU Performance Counters to the session, their values will be collected at the same time, and you will have separate columns of data for each counter. Unlike the sampling mode, you theoretically can add to instrumentation profiling session as many CPU counters as you wish. Practically, however, the number of counters you can use simultaneously is limited by a number of registers that are implemented in your CPU for that purpose. Sometimes it could be as few as two registers only, and VS Profiler always tries to acquire one of them for internal usage.

To add some Performance Counters to your Performance Session, go to the session Properties and select CPU Counters tab. There you can enable and select counters you want to add to your instrumentation data collection.

image

When you look at collected results, you will see new columns of data:

image

If number of counters that you select exceeds current CPU limitation, some of them will be dropped from the collection and you will get a WARNING message in the Output window:

Warning VSP2318: The monitor was unable to initialize counter 'Mispredicted Branches'.

While using command line tools, you can have multiple /COUNTER options to add several counters to your instrumentation session:

VSPerfCmd.exe /start:trace /output:MyFile.vsp /COUNTER:c1 /COUNTER:c2

Enjoy!

Comments

  • Anonymous
    September 13, 2010
    Thanks Max - it was very helpful. Although unfortunately even in Instrumentation mode I was able to use only one CPU counter - I guess it means I only had 1 register available :(

  • Anonymous
    August 06, 2013
    It is very informative and helpful post. thanks for sharing max..