Visual Studio Profiler in Virtual Environment

Visual Studio profiler usage on virtual machines was limited so far, as it didn’t support Sampling mode in such environment. I’m glad to note that in Visual Studio 2010 you can sample your applications inside of virtual machines. VMs could be created using either VPC, VMWare of Hyper-V technologies.

Some hardware is not virtualized, though, and CPU performance counters (that are used by profiler) are, unfortunately, in the list. To overcome the issue, we used a different approach that targets each VM-vendor specifically, and timers that are used in virtual world are not the same as on a raw hardware. Some functionality, however, suffers from lack of CPU performance counters virtualization. Thus, you obviously cannot explicitly sample with those counters (instead of default one), and an attempt to do so will result in an error message:

Error VSP1478: Performance counters are not supported on virtual machines.

So, you cannot sample with Instructions Retired, L2 Cache Misses, Branch Mispredictions and other fancy counters provided by HW vendors.

Instrumentation mode of profiler inside of VM has some limitation as well. Explicit usage of CPU performance counters (through /COUNTER option of VSPerfMon.exe or VSPerfCmd.exe command line tools, or through Visual Studio UI) results in the same error. Default usage of Instrumentation will warn you that a difference between Elapsed (user mode + kernel mode) and Application (user mode) times for each function will not be detected:

Warning VSP2317: The monitor was unable to acquire a hardware performance counter for detection of kernel mode execution. Elapsed and Application time will be the same.

But apart from this difference, Instrumentation mode works just fine.

One word of caution. You may be confused to see those errors/warning about non supported performance counters on a real, non-virtual machine. It could happen if your machine has CPU virtual technology enabled in BIOS, and a hypervisor role is added to operating system (Hyper-V, for example). In this case, your operating system runs on top of hypervisor and, in fact, becomes virtual (even when you logged in to a real machine, and didn’t create any virtual machine there). So, if that happens and you want to use CPU performance counters – all you need to do is to remove hypervisor role in your OS and disable virtual technology in BIOS. After that you can use profiler as usual.