tags:

views:

78

answers:

2

One of our comrades, Peter Mortensen , asked question on SuperUser about 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. 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 at user set interval make a cache miss. 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 order 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.

+1  A: 

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 it's CPU utilzation yielded it's timeslice to reduce CPU utilization to compensate when the avg. utilization exceeded the target %.

In psuedo code, 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 4 core system should probably have 4 threads each running the above loop.

Chris Taylor
That's a good idea for start, but it's still stop-start. Of course the utilization meter makes it usable solution,especially if it's finely tuned; but I'd rather like to see if there's some other way to do it.
AndrejaKo
@AndrejaKo: Within a single core, stop-start is the *only* way to do it.
Mike Dunlavey
+2  A: 

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.

Mike Dunlavey