CPU Usage
-
Is there any way in Windows to throttle a process' CPU usage? If so, what's the technique? EDITED: I'm asking if one can programmatically throttle a process' CPU usage. Meaning managing the CPU time that a process is allowed to consume.
The difficult we do right away... ...the impossible takes slightly longer.
Not that I've ever seen. Thinking about it, you might be able to kind of simulate it by limiting the cores the process can run on by setting processor affinity for it.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
Is there any way in Windows to throttle a process' CPU usage? If so, what's the technique? EDITED: I'm asking if one can programmatically throttle a process' CPU usage. Meaning managing the CPU time that a process is allowed to consume.
The difficult we do right away... ...the impossible takes slightly longer.
Supposedly [JOBOBJECT_CPU_RATE_CONTROL_INFORMATION](https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject\_cpu\_rate\_control\_information) (passed into `SetInformationJobObject`) can be used for something like that. I've never used it, so I have no details beyond that.
-
Is there any way in Windows to throttle a process' CPU usage? If so, what's the technique? EDITED: I'm asking if one can programmatically throttle a process' CPU usage. Meaning managing the CPU time that a process is allowed to consume.
The difficult we do right away... ...the impossible takes slightly longer.
You can use these for your own process or another for which you have the required permission: SetPriorityClass function (processthreadsapi.h) - Win32 apps | Microsoft Learn[^] SetProcessAffinityMask function (winbase.h) - Win32 apps | Microsoft Learn[^] Apparently these can also be invoked via PowerShell or the Task Manager.
Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
Is there any way in Windows to throttle a process' CPU usage? If so, what's the technique? EDITED: I'm asking if one can programmatically throttle a process' CPU usage. Meaning managing the CPU time that a process is allowed to consume.
The difficult we do right away... ...the impossible takes slightly longer.
A tiny virtual machine.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
Is there any way in Windows to throttle a process' CPU usage? If so, what's the technique? EDITED: I'm asking if one can programmatically throttle a process' CPU usage. Meaning managing the CPU time that a process is allowed to consume.
The difficult we do right away... ...the impossible takes slightly longer.
Hi, If you mean you want to limit the process to a specific percentage (say 10% for example), then you can do this by attaching the process to a Job object. [Job Objects - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/procthread/job-objects) I think you would want
JOB_OBJECT_CPU_RATE_CONTROL_ENABLE | JOB_OBJECT_CPU_RATE_CONTROL_HARD_CAP
[JOBOBJECT_CPU_RATE_CONTROL_INFORMATION (winnt.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject\_cpu\_rate\_control\_information) -
Hi, If you mean you want to limit the process to a specific percentage (say 10% for example), then you can do this by attaching the process to a Job object. [Job Objects - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/procthread/job-objects) I think you would want
JOB_OBJECT_CPU_RATE_CONTROL_ENABLE | JOB_OBJECT_CPU_RATE_CONTROL_HARD_CAP
[JOBOBJECT_CPU_RATE_CONTROL_INFORMATION (winnt.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject\_cpu\_rate\_control\_information)This looks awesome. Thanks, David.
The difficult we do right away... ...the impossible takes slightly longer.
-
Hi, If you mean you want to limit the process to a specific percentage (say 10% for example), then you can do this by attaching the process to a Job object. [Job Objects - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/procthread/job-objects) I think you would want
JOB_OBJECT_CPU_RATE_CONTROL_ENABLE | JOB_OBJECT_CPU_RATE_CONTROL_HARD_CAP
[JOBOBJECT_CPU_RATE_CONTROL_INFORMATION (winnt.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject\_cpu\_rate\_control\_information)Nice. Learn something new every day. :thumbsup:
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
This looks awesome. Thanks, David.
The difficult we do right away... ...the impossible takes slightly longer.
I know this is the C/C++ forum, but here's an example of how to do this in C#. It was an interesting little research project. The result can be seen in Task Manager quite easily. The CPU is limited to an AVERAGE of 20% in this example. It'll go as low as 8% and as high as 25% on my 13900K.
using System.Diagnostics;
using System.Runtime.InteropServices;namespace CsJobObjectSandbox
{
internal class Program
{
[StructLayout(LayoutKind.Explicit, Size = 8)]
public struct JobObject_CPU_Rate_Control_Information
{
[FieldOffset(0)]
public uint ControlFlags;\[FieldOffset(4)\] public uint CpuRate; \[FieldOffset(4)\] public uint Weight; \[FieldOffset(4)\] public ushort MinRate; \[FieldOffset(6)\] public ushort MaxRate; } \[StructLayout(LayoutKind.Sequential)\] public struct SECURITY\_ATTRIBUTES { public int nLength; public IntPtr lpSecurityDescriptor; public int bInheritHandle; } public enum JobObject\_Info\_Class { BasicLimitInformation = 2, BasicUiRestrictions = 4, SecurityLimitInformation = 5, EndOfJobItmeInformation = 6, AssociateCompletionPortInformation = 7, ExtendedLimitInformation = 9, GroupInformation = 11, NoticiationLimitInformation = 12, GroupInformationEx = 14, CpuRateControlInformation = 15, NetRateControlinformation = 32, NotificationLimitInformation = 33, LimitViolationInformation2 = 34 } private const uint JOBOBJECT\_CPU\_RATE\_CONTROL\_ENABLE = 0x1; private const uint JOBOBJECT\_CPU\_RATE\_CONTROL\_WEIGHT\_BASED = 0x2; private const uint JOBOBJECT\_CPU\_RATE\_CONTROL\_HARD\_CAP = 0x4; private const uint JOBOBJECT\_CPU\_RATE\_CONTROL\_NOTIFY = 0x8; private const uint JOBOBJECT\_CPU\_RATE\_CONTROL\_MIN\_MAX\_RATE = 0x10; \[DllImport("kernel32.dll", CharSet = CharSet.Auto)\] static extern IntPtr CreateJobObject(\[In\] ref SECURITY\_ATTRIBUTES lpJobAttributes, string lpName); \[DllImport("kernel32.dll", CharSet = CharSet.Auto)\] static extern bool SetInformationJobObject(\[In\] IntPtr hJob, \[In\] JobObject\_Info\_Class jobObjectInfoClass, IntPtr lpJobObjectInfo, int cbJobObjectInfoLength); \[D
-
I know this is the C/C++ forum, but here's an example of how to do this in C#. It was an interesting little research project. The result can be seen in Task Manager quite easily. The CPU is limited to an AVERAGE of 20% in this example. It'll go as low as 8% and as high as 25% on my 13900K.
using System.Diagnostics;
using System.Runtime.InteropServices;namespace CsJobObjectSandbox
{
internal class Program
{
[StructLayout(LayoutKind.Explicit, Size = 8)]
public struct JobObject_CPU_Rate_Control_Information
{
[FieldOffset(0)]
public uint ControlFlags;\[FieldOffset(4)\] public uint CpuRate; \[FieldOffset(4)\] public uint Weight; \[FieldOffset(4)\] public ushort MinRate; \[FieldOffset(6)\] public ushort MaxRate; } \[StructLayout(LayoutKind.Sequential)\] public struct SECURITY\_ATTRIBUTES { public int nLength; public IntPtr lpSecurityDescriptor; public int bInheritHandle; } public enum JobObject\_Info\_Class { BasicLimitInformation = 2, BasicUiRestrictions = 4, SecurityLimitInformation = 5, EndOfJobItmeInformation = 6, AssociateCompletionPortInformation = 7, ExtendedLimitInformation = 9, GroupInformation = 11, NoticiationLimitInformation = 12, GroupInformationEx = 14, CpuRateControlInformation = 15, NetRateControlinformation = 32, NotificationLimitInformation = 33, LimitViolationInformation2 = 34 } private const uint JOBOBJECT\_CPU\_RATE\_CONTROL\_ENABLE = 0x1; private const uint JOBOBJECT\_CPU\_RATE\_CONTROL\_WEIGHT\_BASED = 0x2; private const uint JOBOBJECT\_CPU\_RATE\_CONTROL\_HARD\_CAP = 0x4; private const uint JOBOBJECT\_CPU\_RATE\_CONTROL\_NOTIFY = 0x8; private const uint JOBOBJECT\_CPU\_RATE\_CONTROL\_MIN\_MAX\_RATE = 0x10; \[DllImport("kernel32.dll", CharSet = CharSet.Auto)\] static extern IntPtr CreateJobObject(\[In\] ref SECURITY\_ATTRIBUTES lpJobAttributes, string lpName); \[DllImport("kernel32.dll", CharSet = CharSet.Auto)\] static extern bool SetInformationJobObject(\[In\] IntPtr hJob, \[In\] JobObject\_Info\_Class jobObjectInfoClass, IntPtr lpJobObjectInfo, int cbJobObjectInfoLength); \[D
Just out of curiosity, if you set a hard cap of 20%, what causes it to exceed that?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Just out of curiosity, if you set a hard cap of 20%, what causes it to exceed that?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
I don't know for sure, but I suspect thread scheduling on different cores running at different speeds. On a 13900, you have 8 performance cores, which support HT, and 16 efficiency cores, which don't support HT. So I have 24 cores that can run at vastly different speeds, supporting 32 threads.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak