[vbox-dev] vbox-dev: Implementing Performance Counter Support

Klaus Espenlaub klaus.espenlaub at oracle.com
Wed Nov 22 19:48:34 GMT 2023


This is a non-trivial project involving kernel-level code. The fact that 
the CPU performance counters are updated all the time (and there is no 
virtualization support for them I assume besides the usual MSR updating) 
makes them hard to virtualize.

The concept itself is simple (excuse a little over-simplification): the 
initial state for the VM's perf counters is all 0. When starting the VM 
execution the host OS's perf counter values need to be saved and the 
ones for the guest need to be loaded. Similarly when ending the VM 
execution the guest's perf counter values need to be saved and the host 
OS's need to be loaded.

In the end this is an extension to the MSR handling which is already 
present. Still, you need to understand how the additional MSRs can be 
handled safely.

Note that the result won't ever be perfect, because the VM switching 
machinery will never be counted quite right and therefore it will 
introduce potentially a lot of inaccuracies to the perf counters.

Additionally it costs some clock cycles for every VM execution start and 
VM execution end, because MSR loading and saving isn't for free. Adding 
possibly another condition to the hot code path...

I wouldn't mind having such functionality, therefore please don't see 
the above as an attempt to talk you out of it. However, it needs dealing 
with low level code and it will pass through very careful code review to 
make sure the code doesn't have unintentional side effects.

Klaus

On 2023-11-16 18:00, Valdis Klētnieks via vbox-dev wrote:
> On Tue, 07 Nov 2023 21:38:48 +0100, David via vbox-dev said:
> 
>> I am considering contributing to VBox and add support for CPU performance
>> counters. You might have encountered the lack of such support while using
>> Perf, which will dispay *<not supported>* in place of, for example, the
>> branch-misses that were requested.
> 
>   Hw should you access the hardware specific registers? You're basically going to
> have to include much of the infrastructure from the Linux 'perf' command, or
> whatever the equivalent is on other host operating systems.
> 
> As to how to make them available inside the virtual machine?  As virtualized
> MSRs or whatever the real hardare presents - because 'perf' running inside the
> VM will use the same system calls to get info from the (virtualized) hardware, and the
> virtualized OS will attempt to use the same MSR/etc access it would if it as
> running on bare iron.


More information about the vbox-dev mailing list