windows 创建 CPU 利用率 >70% 的线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/456786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create thread with >70% CPU utilization
提问by anand
I am creating a test program to test the functionality of program which calcultes CPU Utilization.
我正在创建一个测试程序来测试计算 CPU 利用率的程序的功能。
Now I want to test that program at different times when CPU utilization is 100%, 50% 0% etc.
现在我想在 CPU 利用率为 100%、50% 0% 等的不同时间测试该程序。
My question how to make CPU to utilize to 100% or may be > 80%.
我的问题是如何使 CPU 利用率达到 100% 或可能 > 80%。
I think creating a while loop like will suffice
我认为创建一个 while 循环就足够了
while(i++< 2000)
{
cout<<" in while "<< endl;
Sleep(10); // sleep for 10 ms.
}
After running this I dont get high CPU utilization. What would be the possible solutions to make high cpu intensive??
运行此程序后,我没有获得高 CPU 利用率。使高 CPU 密集型的可能解决方案是什么?
回答by Jon Skeet
You're right to use a loop, but:
使用循环是正确的,但是:
- You've got IO
- You've got a sleep
- 你有 IO
- 你睡了
Basically nothing in that loop is going to take very much CPU time compared with the time it's sleeping or waiting for IO.
与睡眠或等待 IO 的时间相比,该循环中的任何内容基本上都不会占用大量 CPU 时间。
To kill a CPU you need to give it justCPU stuff. The only tricky bit really is making sure the C++ compiler doesn't optimise away the loop. Something like this should probably be okay:
杀了你需要给它一个CPU只是CPU的东西。唯一棘手的是确保 C++ 编译器不会优化循环。像这样的事情应该没问题:
// A bit like generating a hashcode. Pretty arbitrary choice,
// but simple code which would be hard for the compiler to
// optimise away.
int running_total = 23;
for (int i=0; i < some_large_number; i++)
{
running_total = 37 * running_total + i;
}
return running_total;
Note the fact that I'm returning the value out of the loop. That should stop the C++ compiler from noticing that the loop is useless (if you never used the value anywhere, the loop would have no purpose). You may want to disable inlining too, as otherwise I guess there's a possibility that a smart compiler would notice you calling the function without using the return value, and inline it to nothing. (As Suma points out in the answer, using volatile
when calling the function should disable inlining.)
请注意,我将值从循环中返回。这应该会阻止 C++ 编译器注意到循环是无用的(如果您从未在任何地方使用过该值,则循环将毫无意义)。您可能还想禁用内联,否则我猜有可能智能编译器会注意到您在不使用返回值的情况下调用了该函数,并将其内联为空。(正如 Suma 在答案中指出的那样,volatile
在调用函数时使用应禁用内联。)
回答by Suma
Your loop mostly sleeps, which means it has very light CPU load. Besides of Sleep, be sure to include some loop performing any computations, like this (Factorial implementation is left as an exercise to reader, you may replace it with any other non-trivial function).
您的循环大部分时间处于休眠状态,这意味着它的 CPU 负载非常轻。除了 Sleep 之外,一定要包含一些执行任何计算的循环,像这样(阶乘实现留给读者作为练习,你可以用任何其他非平凡的函数替换它)。
while(i++< 2000)
{
int sleepBalance = 10; // increase this to reduce the CPU load
int computeBalance = 1000; // increase this to increase the CPU load
for (int i=0; i<computeBalance; i++)
{
/* both volatiles are important to prevent compiler */
/* optimizing out the function */
volatile int n = 30;
volatile int pretendWeNeedTheResult = Factorial(n);
}
Sleep(sleepBalance);
}
By adjusting sleepBalance / computeBalance you may adjust how much CPU this program takes. If you want to this as a CPU load simulation, you might want to take a few addtional steps:
通过调整 sleepBalance / computeBalance 你可以调整这个程序需要多少 CPU。如果您想将此作为 CPU 负载模拟,您可能需要采取一些额外的步骤:
- on a multicore system be sure to either spawn the loop like this in multiple threads (one for each CPU), or execute the process multiple times, and to make the scheduling predictable assign thread/process affinity explicitly
- sometimes you may also want to increase the thread/process priority to simulate the environment where CPU is heavily loaded with high priority applications.
- 在多核系统上,确保在多个线程(每个 CPU 一个)中生成这样的循环,或者多次执行进程,并使调度可预测,明确分配线程/进程关联
- 有时您可能还想提高线程/进程优先级以模拟 CPU 负载高且具有高优先级应用程序的环境。
回答by Rob K
Use consume.exe in the Windows SDK.
在Windows SDK 中使用consume.exe。
Don't roll your own when someone else has already done the work and will give it to you for free.
当其他人已经完成工作并将免费提供给您时,请不要推出自己的工作。
回答by Nolte
I know the "yes" command on UNIX systems, when routed to /dev/null will eat up 100% CPU on a single core (it doesn't thread). You can launch multiple instances of it to utilize each core. You could probably compile the "yes" code in your application and call it directly. You don't specify what C++ compiler you are using for Windows, but I am going to assume it has POSIX compatibility of some kind (ala Cygwin). If that's the case, "yes" should work fine.
我知道 UNIX 系统上的“是”命令,当路由到 /dev/null 时会在单核上消耗 100% 的 CPU(它没有线程)。您可以启动它的多个实例以利用每个核心。您可能可以在应用程序中编译“是”代码并直接调用它。您没有指定用于 Windows 的 C++ 编译器,但我假设它具有某种 POSIX 兼容性(ala Cygwin)。如果是这种情况,“是”应该可以正常工作。
回答by Stu Mackellar
If you call Sleep in your loop then most of the the loop's time will be spent doing nothing (sleeping). This is why your CPU utilization is low - because that 10mS sleep is huge compared to the time the CPU will spend executing the rest of the code in each loop iteration. It is a non-trivial task to write code to accurately waste CPU time. Roger's suggestion of using CPU Burn-In is a good one.
如果您在循环中调用 Sleep,则循环的大部分时间将用于无所事事(睡眠)。这就是您的 CPU 利用率低的原因——因为与 CPU 在每次循环迭代中执行其余代码所花费的时间相比,10 毫秒的睡眠时间是巨大的。编写代码以准确地浪费 CPU 时间是一项重要的任务。Roger 建议使用 CPU Burn-In 是一个很好的建议。
回答by Brian Rasmussen
To make a thread use a lot of CPU, make sure it doesn't block/wait. Your Sleep call will suspend the thread and not schedule it for at least the number of ms the Sleep call indicates, during which it will not use the CPU.
要使线程使用大量 CPU,请确保它不会阻塞/等待。您的 Sleep 调用将挂起线程,并且不会在至少 Sleep 调用指示的毫秒数内对其进行调度,在此期间它不会使用 CPU。
回答by Roger Lipscombe
Get hold of a copy of CPU Burn-In.
获取一份CPU Burn-In的副本。