Search code examples
performancepowershellwindowwindows-server

setting CPU throttling per user on server 2012


On server 2008 R2 i programatically set the CPU LIMIT to a certain percentage with the following PowerShell

function Set-UserAccountCPUThrottle
{
[CmdletBinding()]
Param(
[ValidateNotNullOrEmpty()]
[parameter(Mandatory = $true)]
$UserNameToRestrict,
[Parameter(Mandatory = $false)]
[int]$CpuPercentage = 5
)

write-host "about to restrict user account $UserNameToRestrict to use ${CpuPercentage}% CPU"

  try
    {     
      $objUser = New-Object System.Security.Principal.NTAccount($UserNameToRestrict)
      $local:ResolvedSID= $objUser.Translate([System.Security.Principal.SecurityIdentifier]).Value.trim()   
    }
  catch
    {
      throw "Cannot resolve the User (or find its SID) for $UserNameToRestrict"
    }    
 $regpath = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Quota System\${local:ResolvedSID}"
 #in creating a new key for this sid, it will remove any old item
mkdir $regpath -Force -ErrorAction stop | out-null
#as the old key if existing was removed by the above code, this will create a new value
 New-ItemProperty -Path $regpath  -Name "CpuRateLimit" -Value $CpuPercentage -PropertyType "DWord" -Force -ErrorAction stop | out-null   

}

However while this does create the registry key per http://technet.microsoft.com/en-us/library/ff384148(v=ws.10).aspx

it has null effect on server 2012 or windows 8. Is this broken or is there a new way to do this in server 2012?


Solution

  • According to this page on TechNet, resource management is not enforced unless the total CPU usage is greater than 70%.

    Is your machine that busy? That page also describes a few other cases where resource management may not occur.

    You didn't mention precisely what you are trying to control. If your workload is RDS (virtual desktops, session-based desktops or RemoteApp programs), the Windows Server 2012 "FairShare" feature might be more useful to you. Relevant snippet:

    "Fairshare of resources in RD Session Host. In Windows Server 2012, RD Session Host server allocates CPU, Disk I/O, and Network I/O such that a single user cannot consume resources that would negatively impact other users on the same host. Each user will get a “fair share”. This is done with minimum overhead so the CPU, disk, and network resources are used to maximum capacity."

    That snippet can be found about midway down this technet page.