Using PowerShell to find the virtual processor to logical processor ratio of Hyper-V

Using PowerShell to find the virtual processor to logical processor ratio of Hyper-V

  • Comments 13


For as long as I can remember – I have been talking to people about supported / optimal virtual processor to logical processor ratios with Hyper-V.  Or – said with less technical jargon – how many virtual machines, with how many virtual processors, should you run on any given physical computer?

Today our documented guidance is that we support up to 8 virtual processors for each logical processor in the physical computer – and that for best performance under most reasonable circumstances you should aim for a ratio of 4 virtual processors per logical processor in the physical computer.

The problem with all of this?  We do not provide any simple way to find out what your current ratio is.

When I realized this – I simultaneously realized that I did not know what the virtual processor to logical processor ratio was on any of my computers.  As such – I decided that I needed to write a script to do this.

Now – in the past some of my readers have accused me of not being a “real PowerShell scripter”, and that I was “writing VBscript style PowerShell”.  Today I would like to try and set the record straight on this accusation.  Because as I started looking into this – I realized that this did not need to be a script at all.  No! It can be done with one (nearly incomprehensible) line of PowerShell code.  Which goes as follows:

write-host (@(gwmi -ns root\virtualization MSVM_Processor).count / (@(gwmi Win32_Processor) | measure -p NumberOfLogicalProcessors -sum).Sum) "virtual processor(s) per logical processor" -f yellow

When I ran this on my main Hyper-V server at home – I got the following result:

image

Neat! I am practically right on the sweet spot!

But what does this line of code do – and how does it work?  Well – there are a couple of things to know:

  • For each virtual processor for each currently running virtual machine there is an instance of the MSVM_Processor WMI object on the system.  So to figure out how many virtual processors are currently running – you just have to count how many instances of this WMI object are currently present on the system.
  • Summing up the NumberOfLogicalProcessors property of the Win32_Processor object will given you the other side of the calculation – namely how many logical processors are present in the system.
  • In order to keep the one line of code as short as possible – I have used every PowerShell abbreviation that I know of (gwmi for Get-WMIObject, –ns for –namespace, measure for measure-object, –p for –property, –f for –foregroundColor)

Cheers,
Ben

Leave a Comment
  • Please add 3 and 4 and type the answer here:
  • Post
  • write-host (@((get-counter "\Hyper-V Hypervisor\Logical Processors").CounterSamples[0].CookedValue /(get-counter "\Hyper-V Hypervisor\Virtual Processors").CounterSamples[0].CookedValue - 1)) "virtual processor(s) per logical processor" -f yellow

  • I think you forgot "echo" as a shorthand for "write-host". :)

  • Hi, Ben:

    I have tried the script on my server. But the result confused me.

    my result is: -0.238.95238.95238 vitual processor(s) per logical processor.

  • Very nice Ben! I ran this on my Hyper-V cluster hosts and I'm below 4 on all of them. In fact, I'm below 3.

    It may be a hard to understand one liner, but it's better than anything from Schwarzenegger. :-)

  • Add the -computername paramater and save yourself from logging onto all of your Virtual Machines.

    write-host (@(gwmi -computername YOURSERVERNAME -ns root\virtualization MSVM_Processor).count / (@(gwmi Win32_Processor) | measure -p NumberOfLogicalProcessors -sum).Sum) "virtual processor(s) per logical processor" -f yellow

  • Hi Ben,

    in my server the following message has returned: 0 virtual processor(s) per logical processor

    Is the above iformation correct?

    I have a quad-core server runnig two VMs using 1 virtual processor each.

    Regards,

    Paulo Oliveira.

  • "Today our documented guidance is that we support up to 8 virtual processors for each logical processor in the physical computer – and that for best performance under most reasonable circumstances you should aim for a ratio of 4 virtual processors per logical processor in the physical computer."

    Ben,

    What about HT cores in regard to the best performance ratio? Any recent Intel server CPU has twice as many logical processors than cores because of HT. Your script counts all logical processors whether or not they are actual "complete" cores or HT cores. To count actual "complete" cores instead of HT cores would be an easy change of "NumberOfLogicalProcessors" to "NumberOfCores".

    Regards,

    Andreas Erson

  • Thanks for posting this -- it is a handy way to check the ratio on the fly.

    I have been unable to find any supporting documentation that also recommends the 'sweet spot' ratio of 4:1.  The other recommendations I've read say to aim for a 1:1 pairing.  While it makes sense that is only ideal if you're using the processors under heavy load, is there an additional write-up that supports the 4:1 ratio?

    Thanks again.

  • msdn.microsoft.com/.../cc768529(v=bts.10).aspx

    It's not a best practice

  • Ben, would you have the version for hyper-v 2012?

  • Windows Server 2012 Version:

    Write-Host (@(Get-WmiObject -Namespace "root\virtualization" -Class MSVM_Processor).count / (@(Get-WmiObject -Class Win32_Processor) | measure -Property NumberOfLogicalProcessors -Sum).Sum) "Virtual Processor (s) per logical processor" -ForegroundColor yellow

  • Can't run this on a Windows 8.1 client.

    Write-Host (@(Get-WmiObject -Namespace "root\virtualization" -Class MSVM_Processor).count / (@(Get-WmiObject -Class Win32_Processor) | measure -Property NumberOfLogicalProcessors -Sum).Sum) "Virtual Processor (s) per logical processor" -ForegroundColor yellow

    Error: Invalid class "MSVM_Processor"--------0 Virtual Processor (s) per logical processor

    Is there a separate class for Client machines' visualization ?

  • For Win 8.1 and Server 2012 R2 it would be:

    Write-Host (@(Get-WmiObject -Namespace "root\virtualization\v2" -Class MSVM_Processor).count / (@(Get-WmiObject -Class Win32_Processor) | measure -Property NumberOfLogicalProcessors -Sum).Sum) "Virtual Processor (s) per logical processor" -ForegroundColor yellow

    The WMI v1 namespace was deprecated in Windows Server 2012, and then removed in Windows Server 2012 R2.   In Windows Server 2012 and above it is root\virtualization\v2.

Page 1 of 1 (13 items)