开发者

Controlling calculation speed in a program

开发者 https://www.devze.com 2023-01-08 12:35 出处:网络
One of our comrades, Peter Mortensen, asked a question on Super User about an application which could consume arbitrarily percentage of CPU time. It piqued my interest.

One of our comrades, Peter Mortensen, asked a question on Super User about an application which could consume arbitrarily percentage of CPU time. It piqued my interest.

My question is how could such application be made:

Here are some of my general ideas and why I think that they are bad:

  1. A calculation program could be made which will use algorithms which will be inefficient on purpose. The problem with that option is that application will basically have one gear. To solve that we would need several algorithms of various inefficiencies in order to provide more that one gear. Still, user wouldn't be able to select say 50% utilization. He'd have to choose one of the algorithms and see how it performs on his computer.
  2. The application could be made using a worker thread which will be set to sleep from time to time. This way user could control how long thread sleeps between each execution and control CPU utilization. However, while mean utilization would be controllable, at one moment the thread would either be sleeping or consuming resources, so it's not very good solution.
  3. This would be a variant of number one. Application might be made in such way as to make use of cache misses or speculative compiling in Java. For example, a loop could be made which would control calculation and have it to make a cache miss at a user set interval. This would slow down computation, but it would be difficult to implement. Or in Java JIT optimizations could be used. A loop with a condition would execute in such way that JIT optimizes away the condition. Once the condition is hit, JIT would have to deoptimize the code and execute it. But if it stores both optimized and unoptimized versions of code, it will only work once. Also, something like that would require great knowledge of Java optimizations in or开发者_如何学运维der for it to work.

Any other ideas on how to make a program deliberately slow?

I was primarily thinking about implementing something like this in C, C++, C# or Java, but I fear that C# or Java may be too high level for this kind of problem.


Assuming one thread and one core, at any nanosecond, the thread is either doing something (100%) or it is waiting (0%). Partial CPU utilization is only possible as a time-average between running and not-running. So, basically, to get 50%, just let it sleep half the time. The lengths of the sleeps is up to you.


And I do not know if this would work, but what I would try do as a first attempt is to write an application that burned the CPU, and in that burn loop monitored its CPU utilzation yielded its timeslice to reduce CPU utilization to compensate when the average utilization exceeded the target percentage.

In psuedocode, something like this:

while (true)
{
    double cpuUtilization = GetCpuUtilization();
    if (cpuUtilization > 0.5)
    {
        Thread.Sleep(0);
    }
}

There might be some caveats that I have not considered here, but that is off the top of my head. One thing, you will probably need to run a loop in a seperate thread for each CPU core in the system, so a four core system should probably have four threads each running the above loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消