Category Archives: Scripts

Find vCPU to Physical CPU Ratio with PowerCLI

This is an interesting thing that I have been looking at lately.

Before I start though I should probably clarify terms, so we are all speaking the same language.

In this article when I say Physical CPU, or pCPU, what I am referring to is the logical threads available on the ESXi host. For a 2 socket, quad-core host, this would mean 8pCPUs, if said host is hyperthreaded, that number would then be 16.

When I say vCPU I simply mean the number of CPUs assigned to the guest, or VM.

Great, now that is cleared up let’s keep going.

vCPU to pCPU Ratio is an interesting stat to look at for both looking at current performance in an environment and planning upgrades/expansions. I will say though that this definitely is not an end all be all stat by any means, and for troubleshooting any number of other things, there are far better stats to investigate. As I said this is merely an interesting one to keep in mind.

vCPU to pCPU Ratio is just that. It is a ratio of how many virtual CPUs you are using per each physical CPU.

The next question you should probably be asking is “What is a good/acceptable Ratio to have?”, and I am happy you asked.

The answer, as with pretty much any question IT related is the tried and true:

“It Depends”

It depends on VM workloads, number CPUs assigned to VM, among any number of other variables. I think there are a large number of environments that would be find with 3:1 ┬áratio or even 5:1 ratio. For instance if you have a host with 16 pCPUs and this host has 6 VMs each with 8 vCPUs each (a 3:1 Ratio), it is likely you may have CPU Ready Time issues. Conversely, this same host could have 48 VMs on it each with 1 CPU(again 3:1 ratio) and be perfectly fine. Well it may have 99 other issues but CPU wouldn’t be 1(See what I did there ­čśë )

What I am trying to get across is that although this stat might be helpful, it shouldn’t be and no stat should be used to holistically define an environment.

This is however an easier statistic to understand than some others, and it could be helpful explaining CPU issues or non-issues to the non-technical.

Anyway, enough rambling, let’s get to the script.


param($vmhosts="*")

$vmhosts=Get-VMHost $vmhosts
$vms=Get-VM

$Output=@()

ForEach ($vmhost in $vmhosts)
{

$vcpus=0
$ratio=$null
$hostthreads=$vmhost.extensiondata.hardware.cpuinfo.numcputhreads
$vms |Where-Object {$_.vmhost -like $vmhost}|ForEach {$vcpus+=$_.numcpu}
if ($vcpus -ne "0") {$ratio= "$("{0:N2}" -f ($vcpus/$hostthreads))" + ":1"}

$temp= New-Object psobject
$temp| Add-Member -MemberType Noteproperty "Hostname" -value $vmhost.name
$temp| Add-Member -MemberType Noteproperty "PhysicalThreads" -Value $Hostthreads
$temp| Add-Member -MemberType Noteproperty "vCPUs" -Value $vcpus
$temp| Add-Member -MemberType Noteproperty "Ratio" -Value $ratio
$Output+=$temp

}

$output
}

It grabs all the VMs and the Host/s you want, and does the math!

cpuratio
For the first example, I didn’t define a parameter, so it pulled back all the hosts. 3:1 and 1.69:1 seems fine. The last 2 hosts listed didn’t have an VMs running on them.

cpuratio1
For this second screenshot, I defined the host when I ran the function, and it returned the 69 host.

Hope this helps or atleast you find it interesting. Below is the full function

Getcpuratio
Getcpuratio
getcpuratio.ps1
1.4 KiB
1610 Downloads
Details...

Check ESXi Host VAAI with PowerCLI

vStorage APIs for Array Integration, or VAAI, allows the vSphere ESXi hosts to offload some of their storage workloads to the Storage arrays. Some of these workloads might be Storage vMotion a VM or provisioning a vmdk among others. VAAI is generally enabled by default on Hosts and most Storage Arrays support it. There are… Continue Reading