How to get Processor Utilization for Hyper-V via WMI

There are a number of groups building management software (OEMs, Microsoft, …) for Hyper-V which is cool to see. A common ask from these teams has been around reading and computing VM CPU usage.

The following is an example of how to compute Hyper-V guest processors usage. You can use the same formula for “% Total Run Time, “% Hypervisor Time” and “% Idle time”. The counters show up in the Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor WMI object as “PercentGuestRunTime”, “PercentTotalRunTime”, “PercentHypervisorRunTime”, and “PercentIdleTime”.

To make the formula easier to read lets use:

                GN – Percent Guest Run Time (substitute other usage values here)

                PN – Timestamp_PerfTime

                FN – Frequency_PerfTime

                LP = Number of logical processors (Get this from the “Hyper-V Hypervisor” counterset)

 

  F1 * (G2 – G1)

 Utilization = -----------------------------------

                     100000 * LP * (P2-P1)

 

G2 and P2 are the second values read and G1 and P1 are the first values read.

To test the formula lets read the “Hyper-V Hypervisor Logical Process” counterset twice via the Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor WMI object about 10 seconds apart with a single VM running at 100% Guest CPU. Since my test machine has two CPU’s (2 LP) this means we should see about 50% overall utilization.

V:\backup>winrm enum wmi/root/cimv2/* -filter:"select * from Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor where name='_Total'"

Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor

    C1TransitionsPersec = 409197889

    C2TransitionsPersec = 0

    C3TransitionsPersec = 0

    Caption = null

    ContextSwitchesPersec = 889911109

    Description = null

    Frequency_Object = 0

    Frequency_PerfTime = 14318180

    Frequency_Sys100NS = 10000000

    HardwareInterruptsPersec = 92282462

    InterProcessorInterruptsPersec = 8174254

    InterProcessorInterruptsSentPersec = 8174254

    MonitorTransitionCost = 16

    Name = _Total

    PercentC1Time = 4193635539355

    PercentC2Time = 0

    PercentC3Time = 0

    PercentGuestRunTime = 314976793671

    PercentHypervisorRunTime = 53745475789

    PercentIdleTime = 8385447570540

    PercentTotalRunTime = 368722269460

    SchedulerInterruptsPersec = 384836664

    TimerInterruptsPersec = 33425466

    Timestamp_Object = 0

    Timestamp_PerfTime = 6268633722843

    Timestamp_Sys100NS = 4199325031975

    TotalInterruptsPersec = 518718846

 

V:\backup>winrm enum wmi/root/cimv2/* -filter:"select * from Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor where name='_Total'"

Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor

    C1TransitionsPersec = 409201218

    C2TransitionsPersec = 0

    C3TransitionsPersec = 0

    Caption = null

    ContextSwitchesPersec = 889922035

    Description = null

    Frequency_Object = 0

    Frequency_PerfTime = 14318180

    Frequency_Sys100NS = 10000000

    HardwareInterruptsPersec = 92283571

    InterProcessorInterruptsPersec = 8174425

    InterProcessorInterruptsSentPersec = 8174425

    MonitorTransitionCost = 16

    Name = _Total

    PercentC1Time = 4193667417779

    PercentC2Time = 0

    PercentC3Time = 0

    PercentGuestRunTime = 315044817737

    PercentHypervisorRunTime = 53746578312

    PercentIdleTime = 8385511363951

    PercentTotalRunTime = 368791396049

    SchedulerInterruptsPersec = 384840537

    TimerInterruptsPersec = 33426627

    Timestamp_Object = 0

    Timestamp_PerfTime = 6268728855292

    Timestamp_Sys100NS = 4199364353043

    TotalInterruptsPersec = 518725160

 

Based the formula above and the data below we get = 51% which is spot on.

  Enjoy,

    Tony Voellm

Comments


Tony's reply Please post the full query you are using.  Thanks.

  • Anonymous
    January 21, 2009
    Hi Tony, On our Hyper-V machine(Windows 2008 x64 with Hyper-V Role enabled), we have created 5 VMs each with different no of virtual processors. Using the wmi we were able to query all the performance related counters like PercentTotalRuntime, PercentGuestRuntime. But suddenly we are seeing that most of the runtime counters are missing from WMI. Can you please give us tips/help us to find the reason for this: Please note that Perfmon is still showing all the %GuestRuntime and all other counter values properly. c:UsersAdministrator>winrm enum wmi/root/cimv2/* -filter:"select * from Win32_ PerfRawData_HvStats_HyperVHypervisorLogicalProcessor where Name='_Total'" Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor    C1TransitionsPersec = 224586390    C2TransitionsPersec = 0    Caption = null    ContextSwitchesPersec = 465444370    Description = null    Frequency_Object = 0    Frequency_PerfTime = 25000000    Frequency_Sys100NS = 10000000    HardwareInterruptsPersec = 41350707    InterProcessorInterruptsPersec = 58882296    InterProcessorInterruptsSentPersec = 9885971    MonitorTransitionCost = 138    Name = _Total    PercentC1Time = 444477161794    PercentC2Time = 0    SchedulerInterruptsPersec = 206489220    TimerInterruptsPersec = 46487497    Timestamp_Object = 0    Timestamp_PerfTime = 1811834426354    Timestamp_Sys100NS = 702268355257 c:UsersAdministrator> Also, note that WMI Browser we are using is also showing only above properties. Similar properties are missing under all Hyper-V Processor related WMI Objects,,

  • Anonymous
    May 21, 2009
    I am not able to find any information about "Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor" WMI object and objects similar to it on Web or MSDN. I have also checked virtualization namespace on my machine where Hyper-V is installed through CIM studio. Any pointers?

  • Anonymous
    July 27, 2009
    Thank you for this information! It is very useful for me. ^^

  • Anonymous
    September 10, 2009
    When i try to query via WMI, i can't find the WMI Class like Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor. I use the Hyper-V Server (Server 2008 core)

  • Anonymous
    March 06, 2010
    Thanks to your great article. I'm cann't read&write english well but I have a one question. My server(Dell R710) is use that intel E5520 * 2 Physical processor. E5520 is 4core and Hyperthreading Enable. How much my E5520*2cpu have a LogicalProcessor??? Logical Processo 16ea right ??? sorry my low grade english skill...  [Tony's reply] 2 cpus * 4 core * 2 thread = 16LP However keep in mind that threads are not full cores so not all application will run better with them.

  • Anonymous
    March 25, 2012
    Got the following question today from Salar...


Hi Tony, I read your wonderful blog post blogs.msdn.com/.../how-to-get-processor-utilization-for-hyper-v-via-wmi.aspx I am new to HyperV and had some questions. I hope you can help me. As mentioned in the blog post, to get the CPU utilization, we need to read 2 samples of data from Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor. I was wondering if we have WMI object which can provide us the CPU utilization using just 1 sample. Meaning, just read the data for CPU utilization at a given point of time without doing any calculation. I would like to fetch CPU utilization for both the HyperV hosts and VMs. Thanks in advance Regards, Sagar

My reply.... Hey Sagar, While I've moved on to Google but I'm happy to help. You can get what you want from the following WMI class - msdn.microsoft.com/.../cc136898(v=vs.85).aspx  Tony