Taking my last post on page sharing a little further and inspired by @VBrianGraf & @vmMarkA’s Intra TPS Host Memory Assessment tool, I wanted to create a script that would calculate the memory savings gained from inter-VM page sharing directly from the statistics available in vCenter.

In my last post I showed the values that need to be obtained from the performance graphs and the calculation that needs to be performed. With the magic of PowerCLI, I’ve ended up with the function below.

Usage

Get-MemorySharingInterVM -VMHost hostname.local.domain "hostname.local.domain" | Get-MemorySharingInterVM $myCluster = "Production" Get-Cluster $myCluster | Get-VMHost | Get-MemorySharingInterVM 1 2 3 4 5 6 Get-MemorySharingInterVM -VMHost hostname . local . domain "hostname.local.domain" | Get-MemorySharingInterVM $myCluster = "Production" Get-Cluster $myCluster | Get-VMHost | Get-MemorySharingInterVM

PowerCLI function Get-MemorySharingInterVM

Function Get-MemorySharingInterVM { <# .Synopsis Calculating the Page Sharing values .DESCRIPTION Calculates the memory shared between VMs on the host. InterVMGB property is the amount of memory in GB shared between VMs on the host minus the zero page memory value. .EXAMPLE Get-MemorySharingInterVM -VMHost HOSTNAME .OUTPUTS Shared : 289222366 Zero : 154171146 InterVM : 135051220 InterVMGB : 129 HostName : hostname .NOTES Connect to vCenter beforehand #> [CmdletBinding()] Param ( [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] [ValidateNotNullOrEmpty()] [string[]]$VMHost ) Begin{ $Results = @() } Process{ Foreach ($Hst in $VMHost) { $Stats = Get-Stat -Entity $Hst -Stat "mem.shared.average", "mem.zero.average" -MaxSamples 1 -Realtime $Zero = $Stats | ? {$_.MetricId -eq "mem.zero.average"} $Shared = $Stats | ? {$_.MetricId -eq "mem.shared.average"} $InterVM = ($Shared.Value - $Zero.Value ) $InterVMGB = [math]::Round(( ($InterVM / (1024 * 1024))),0) $Results += New-Object PSObject -Property @{HostName=$Hst; Shared=$Shared.Value; Zero=$Zero.Value; InterVM=$InterVM; InterVMGB=$InterVMGB} } } End { $Results } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 Function Get-MemorySharingInterVM { <# .Synopsis Calculating the Page Sharing values .DESCRIPTION Calculates the memory shared between VMs on the host. InterVMGB property is the amount of memory in GB shared between VMs on the host minus the zero page memory value. .EXAMPLE Get-MemorySharingInterVM -VMHost HOSTNAME .OUTPUTS Shared : 289222366 Zero : 154171146 InterVM : 135051220 InterVMGB : 129 HostName : hostname .NOTES Connect to vCenter beforehand #> [ CmdletBinding ( ) ] Param ( [ Parameter ( Mandatory = $true , ValueFromPipeline = $true , Position = 0 ) ] [ ValidateNotNullOrEmpty ( ) ] [ string [ ] ] $VMHost ) Begin { $Results = @ ( ) } Process { Foreach ( $Hst in $VMHost ) { $Stats = Get-Stat -Entity $Hst -Stat "mem.shared.average" , "mem.zero.average" -MaxSamples 1 -Realtime $Zero = $Stats | ? { $_ . MetricId -eq "mem.zero.average" } $Shared = $Stats | ? { $_ . MetricId -eq "mem.shared.average" } $InterVM = ( $Shared . Value - $Zero . Value ) $InterVMGB = [ math ] :: Round ( ( ( $InterVM / ( 1024 * 1024 ) ) ) , 0 ) $Results += New-Object PSObject -Property @ { HostName = $Hst ; Shared = $Shared . Value ; Zero = $Zero . Value ; InterVM = $InterVM ; InterVMGB = $InterVMGB } } } End { $Results } }

Output – an array of custom objects. InterVMGB is the memory (GB) shared between VMs on the host minus zero pages

Shared : 289222366 Zero : 154171146 InterVM : 135051220 InterVMGB : 129 HostName : hostname 1 2 3 4 5 Shared : 289222366 Zero : 154171146 InterVM : 135051220 InterVMGB : 129 HostName : hostname

Feel free to use and drop me any feedback.