C ++中的CPU节流 [英] CPU throttling in C++

查看:146
本文介绍了C ++中的CPU节流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否有一种优雅的方法来为执行密集计算的特定线程设置最大CPU负载。


现在,我在线程中找到了最耗时的循环(它仅压缩),并使用 GetTickCount() Sleep()具有硬编码的值。它可以确保循环持续一定时间,然后休眠一定的最短时间。它或多或少地完成了这项工作,即保证线程使用的CPU不超过50%。
但是,行为取决于CPU内核的数量(不利之处很大),而又丑陋(不利之处:))。
有什么想法吗?

I was just wondering if there is an elegant way to set the maximum CPU load for a particular thread doing intensive calculations.

Right now I have located the most time consuming loop in the thread (it does only compression) and use GetTickCount() and Sleep() with hardcoded values. It makes sure that the loop continues for a certain period and then sleeps for a certain minimum time. It more or less does the job, i.e. guarantees that the thread will not use more than 50% of CPU.
However, behavior is dependent on the number of CPU cores (huge disadvantage) and simply ugly (smaller disadvantage :)).
Any ideas?

推荐答案

我不知道有任何API可以使操作系统的调度程序执行您想要的操作(即使您的线程是空闲优先级的,如果没有更高优先级的就绪线程,您的线程也会运行)。但是,我认为您可以根据已经在做的事情即兴发挥一个相当优雅的调节功能。本质上(我没有Windows开发机):

I am not aware of any API to do get the OS's scheduler to do what you want (even if your thread is idle-priority, if there are no higher-priority ready threads, yours will run). However, I think you can improvise a fairly elegant throttling function based on what you are already doing. Essentially (I don't have a Windows dev machine handy):

选择默认时间,线程将在每次迭代中休眠。然后,在每次迭代(或每第n次迭代,这样节流功能本身就不会成为很大的CPU负载)

Pick a default amount of time the thread will sleep each iteration. Then, on each iteration (or on every nth iteration, such that the throttling function doesn't itself become a significant CPU load),


  1. 计算自上次调用限制功能以来,线程使用的CPU时间(我将其称为dCPU)。您可以使用 GetThreadTimes() API来获取您的时间量

  2. 计算自上次调用节流函数(我将其称为dClock)以来经过的实时时间。

  3. dCPU / dClock是(一个CPU的)CPU使用率百分比。如果它比您想要的高,请增加您的睡眠时间;如果它比您想要的要低,请减少睡眠时间。

  4. 让线程在计算出的时间内睡眠。

  1. Compute the amount of CPU time your thread used since the last time your throttling function was called (I'll call this dCPU). You can use the GetThreadTimes() API to get the amount of time your thread has been executing.
  2. Compute the amount of real time elapsed since the last time your throttling function was called (I'll call this dClock).
  3. dCPU / dClock is the percent CPU usage (of one CPU). If it is higher than you want, increase your sleep time, if lower, decrease the sleep time.
  4. Have your thread sleep for the computed time.

根据看门狗计算CPU使用率的方式,您可能需要使用 GetProcessAffinityMask()来找出系统有多少个CPU。 dCPU /(dClock * CPUs)是可用的总CPU时间的百分比。

Depending on how your watchdog computes CPU usage, you might want to use GetProcessAffinityMask() to find out how many CPUs the system has. dCPU / (dClock * CPUs) is the percentage of total CPU time available.

您仍然必须为初始睡眠时间和增量/减量选择一些魔术数字。数量,但我认为可以对该算法进行调整,以使线程在相当接近确定的CPU百分比的情况下运行。

You will still have to pick some magic numbers for the initial sleep time and the increment/decrement amount, but I think this algorithm could be tuned to keep a thread running at fairly close to a determined percent of CPU.

这篇关于C ++中的CPU节流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆