简体   繁体   English

减少线程或进程的CPU使用率

[英]Reducing the CPU usage of a thread or process

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. 还有很多像这样的问题,但我看到的唯一实质性答案就是你使用SetPriorityClass优先考虑其他进程。 This is not what I want. 这不是我想要的。 I want to explicitly limit the CPU usage of my thread/process. 我想明确限制我的线程/进程的CPU使用率。

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. 我正在将我的代码注入到游戏中,我希望在后台“自动化”。

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. 限制进程或线程的cpu使用的最佳解决方案是确保线程或进程使用较少的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. 找出实际占用所有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. 通过等待文件/套接字/ fifos / mutexes /信号量/消息队列等。
  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. 如果你的进程在无限循环中做了很多工作,那么在每N个循环之后插入一个sched_yield()或sleep()。 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. 如果没有其他进程等待CPU使用,那么您的进程几乎会立即重新调度,但会允许系统的其余部分在必要时使用cpu时间。
  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. 但请注意,正如@Mooing Duck所说,通过这样做,您可以将CPU使用率从一个地方转移到另一个地方而不会看到整体改进。

如何定期发出睡眠命令?

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. 您当然可以跟踪线程的I / O并强制它在传递某个阈值后放弃cpu。

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%). 我最终枚举了一个线程列表,然后有一个100ms的计时器,它在每五次迭代中有两次暂停线程列表(理论上这会减少40%的CPU使用率)。

Thanks for all the answers. 感谢所有的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM