Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CPU Usage

CPU Usage

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 5 Posters 22 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    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.

    D L Greg UtasG 5 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      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.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        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.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          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.

          Greg UtasG Offline
          Greg UtasG Offline
          Greg Utas
          wrote on last edited by
          #4

          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.

          <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
          <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

          1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            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.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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)

              Richard Andrew x64R D 2 Replies Last reply
              0
              • L Lost User

                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)

                Richard Andrew x64R Offline
                Richard Andrew x64R Offline
                Richard Andrew x64
                wrote on last edited by
                #7

                This looks awesome. Thanks, David.

                The difficult we do right away... ...the impossible takes slightly longer.

                D 1 Reply Last reply
                0
                • L Lost User

                  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)

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • Richard Andrew x64R Richard Andrew x64

                    This looks awesome. Thanks, David.

                    The difficult we do right away... ...the impossible takes slightly longer.

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    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
                    
                    D 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      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
                      
                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      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

                      D 1 Reply Last reply
                      0
                      • D David Crow

                        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

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups