3

There a bunch of other questions like this, but the only substantial answer I've seen is the one where you use SetPriorityClass to give priority to other processes. This is not what I want. I want to explicitly limit the CPU usage of my thread/process.

How can I do this?

Edit: I can't improve the efficiency of the process itself, because I'm not controlling it. I'm injecting my code into a game which I'd like to 'automate' in the background.

  • 2
    possible duplicate of [How do you limit a process' CPU usage on Windows? (need code, not an app)](http://stackoverflow.com/questions/9353119/how-do-you-limit-a-process-cpu-usage-on-windows-need-code-not-an-app) – Jerry Coffin Mar 19 '12 at 23:33
  • There are not substantial answers because not many developers need this. Most just want their work to run on as fast as as is reasonable and use the priority system to allow foreground tasks and I/O-bound threads to be responsive. One snag, for example, is that the act of managing CPU use uses CPU. – Martin James Mar 20 '12 at 11:18
  • 1
    To clarify, not many developers on desktop/laptop single-user systems need this. CPU/disk quotas are very useful on large, expensive mainframes where many different users/departments need to share resources and be billed for data processing. – Martin James Mar 20 '12 at 11:30
  • @Martin James Thanks for the notice. To clarify on my part, I want to do this because I actually have a somewhat slow computer and I'd like to have a video game I'm playing use up less of the CPU. –  Mar 20 '12 at 14:10

4 Answers4

2

The best solution to limiting the cpu usage for a process or thread is to make sure that the thread or process uses less cpu.

That can best be done by improving the efficiency of the code, or by calling it less often. The aim is to make sure that the process doesn't continually consume all of its available time slice.

Things to try:

  1. Work out what is actually taking up all of the CPU. Optimize heavy processing areas - ideally with a change of algorithm.
  2. Minimise polling wherever possible.
  3. Try to rely on the operating system's ability to wake your process when necessary. eg. By waiting on files/sockets/fifos/mutexes/semaphores/message queue etc.
  4. Have processes self regulate their processor usage. If your process is doing a lot of work in an endless loop insert a sched_yield() or sleep() after every N loops. If there are no other processes waiting for CPU usage then your process will get rescheduled almost immediately, but will allow the rest of the system to use cpu time when necessary.
  5. Rearrange your processing to allow lower priority activities to be run when your process is at idle.
  6. Carefully adjust thread or process priorities. But be aware, as @Mooing Duck has said, that by doing this you may just shift the CPU usage from one place to a different place without seeing an overall improvement.
Andrew Edgecombe
  • 39,594
  • 3
  • 35
  • 61
  • Unfortunately, I don't actually have access to how the process works. I'm injecting my code into a game, which I want to 'automate' in the background while I do other things. Sorry, I should have clarified. –  Mar 21 '12 at 23:37
0

How about issuing a sleep command at regular intervals?

mrk
  • 3,061
  • 1
  • 29
  • 34
0

Your question is broad -- I don't know what it's doing. You can certainly track the thread's I/O and force it to give up the cpu after a certain threshold is passed.

kasavbere
  • 5,873
  • 14
  • 49
  • 72
0

I ended up enumerating a list of threads, then having a 100ms timer that suspended the list of threads two out of every five iterations (which in theory reduces CPU usage by 40%).

Thanks for all the answers.

  • If none of the threads are self-limiting, then this does not reduce CPU usage whatsoever. As I don't know how you programmed the threads, this solution may or may not belp. – Mooing Duck Mar 21 '12 at 21:14
  • What do you mean self-limiting? If the entire process is suspended for 200 ms out of every 500 ms, doesn't that essentially reduce its usage of the CPU? –  Mar 21 '12 at 21:36
  • 1
    @JohnathanLingle: If 5 theads each use 20% of the CPU, then suspending 2 might leave 3 threads that each use 33% of the CPU. Even if you pause 4, that might leave 1 thread using 100% of the CPU. Unless the threads limit how much the do (via IO or sleep), or they complete a task and stop altogether. – Mooing Duck Mar 21 '12 at 21:53
  • 1
    if you stop _all_ the threads, then it reduces the average CPU usage by 40% on average, that is correct. – Mooing Duck Mar 21 '12 at 23:49