Udostępnij za pośrednictwem


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

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

Comments

  • Anonymous
    August 13, 2010
    write-host (@((get-counter "Hyper-V HypervisorLogical Processors").CounterSamples[0].CookedValue /(get-counter "Hyper-V HypervisorVirtual Processors").CounterSamples[0].CookedValue - 1)) "virtual processor(s) per logical processor" -f yellow

  • Anonymous
    August 16, 2010
    I think you forgot "echo" as a shorthand for "write-host". :)

  • Anonymous
    August 16, 2010
    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.

  • Anonymous
    August 21, 2010
    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. :-)

  • Anonymous
    September 03, 2010
    Add the -computername paramater and save yourself from logging onto all of your Virtual Machines. write-host (@(gwmi -computername YOURSERVERNAME -ns rootvirtualization MSVM_Processor).count / (@(gwmi Win32_Processor) | measure -p NumberOfLogicalProcessors -sum).Sum) "virtual processor(s) per logical processor" -f yellow

  • Anonymous
    October 22, 2010
    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.

  • Anonymous
    November 22, 2010
    "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

  • Anonymous
    March 25, 2011
    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.

  • Anonymous
    September 07, 2011
    msdn.microsoft.com/.../cc768529(v=bts.10).aspx It's not a best practice

  • Anonymous
    January 28, 2013
    Ben, would you have the version for hyper-v 2012?

  • Anonymous
    January 30, 2013
    Windows Server 2012 Version: Write-Host (@(Get-WmiObject -Namespace "rootvirtualization" -Class MSVM_Processor).count / (@(Get-WmiObject -Class Win32_Processor) | measure -Property NumberOfLogicalProcessors -Sum).Sum) "Virtual Processor (s) per logical processor" -ForegroundColor yellow

  • Anonymous
    November 26, 2013
    The comment has been removed

  • Anonymous
    December 11, 2013
    For Win 8.1 and Server 2012 R2 it would be: Write-Host (@(Get-WmiObject -Namespace "rootvirtualizationv2" -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 rootvirtualizationv2.

  • Anonymous
    January 28, 2015
    The comment has been removed