3

I'm trying to limit a process CPU usage by calling NtSetInformationProcess with ProcessQuotaLimits info class. When using NtQueryInformationProcess with the ProcessQuotaLimits class I get the right numbers for the Page limits/working sets etc. but CpuRateQuota is not filled.

The code I'm using

public struct _QUOTA_LIMITS_EX
{
    public uint PagedPoolLimit;
    public uint NonPagedPoolLimit;
    public uint MinimumWorkingSetSize;
    public uint MaximumWorkingSetSize;
    public uint PagefileLimit;
    public ulong TimeLimit;
    public uint WorkingSetLimit;
    public uint Reserved2;
    public uint Reserved3;
    public uint Reserved4;
    public uint Flags;
    public _RATE_QUOTA_LIMIT CpuRate;
}

public struct _RATE_QUOTA_LIMIT
{
    public uint RateData;
    public uint RatePercent;
    public uint Reserved0;
}

[DllImport("ntdll.dll", SetLastError = true)]
private static extern int NtSetInformationProcess(IntPtr hProcess, int processInformationClass, ref _QUOTA_LIMITS_EX processInformation, int processInformationLength);

[DllImport("ntdll.dll", SetLastError = true)]
private static extern int NtQueryInformationProcess( IntPtr processHandle, int processInformationClass, ref _QUOTA_LIMITS_EX processInformation, uint processInformationLength, ref int returnLength);

public void Limit()
{
    Process.EnterDebugMode();                      

    _QUOTA_LIMITS_EX quotaLimits = new _QUOTA_LIMITS_EX();
    int size = 56;

    int returnLength = 0;
    int status = NtQueryInformationProcess(this._process.Handle, 1 /*ProcessQuotaLimits*/, ref quotaLimits, (uint)size, ref returnLength);
}

The call returns NT_STATUS 0, which means succeeded. I'm trying this on Windows server 2016 Standard which should be equivalent to Windows 10. Using JobInformation and suspending/resuming the process aren't viable options in this case.

0 Answers0