0

I am trying to design and then write a java program that can increase/decrease CPU usage. Here is my basic idea: write a multi-thread program. And each thread does float point calculation. Increase/Decrease cpu usage through adding/reducing threads.

I am not sure what kinds of float point operations are best in this test case. Especially, I am gonna test VMWare virtual machine.

SecureFish
  • 2,391
  • 7
  • 32
  • 43
  • Why would you increase the CPU usage? – Eng.Fouad Jul 09 '11 at 21:39
  • And how are you going to decrease it? – Tomasz Nurkiewicz Jul 09 '11 at 21:40
  • We want to increase cpu usage and thus observe the behavior of the virtual machine,such as memory management. And decrease it through command line. – SecureFish Jul 09 '11 at 21:50
  • If you want to test memory mamagement, you need some programs that require memory. Just using the FPU won't be enough in this case. – Roland Illig Jul 09 '11 at 22:04
  • To be more specifically, we want to observe how virtual machine behave when cpu usage increases/decrease. The behavior we are interested in includes memory, cpu, self-protection, ... – SecureFish Jul 09 '11 at 22:12
  • You are far better of running a real program. Increasing CPU is not likely to increase memory demands and it is the interaction with the OS which tends to suffer for virtual machines. i.e. you could easily write a test which runs very well even though no real application does. – Peter Lawrey Jul 10 '11 at 06:39

2 Answers2

0

You can just sum up the reciprocals of the natural numbers. Since this sum doesn't converge, the compiler will not dare to optimize it away. Just make sure that the result is somehow used in the end.

1/1 + 1/2 + 1/3 + 1/4 + 1/5 ...

This will of course occupy the floating point unit, but not necessarily the central processing unit. So if this approach is good or not is the main question you should pose.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
0

Just simple busy loops will increase the CPU usage -- I am not aware if doing FP calculations will change this significantly or otherwise be able to achieve a more consistent load factor, even though it does exercise the FPU and not just the ALU.

While creating a similar proof-of-concept in C# I used a fixed number of threads and changed the sleep/word durations of each thread. Bear in mind that this process isn't exact and is subject to both CPU and process throttling as well as other factors of modern preemptive operating systems. Adding VMware to the mix may also compound the observed behaviors. In degenerate cases harmonics can form between the code designed to adjust the load and the load reported by the system.

If lower-level constructs were used (generally requiring "kernel mode" access) then a more consistent throttle could be implemented -- partially because of the ability to avoid certain [thread] preemptions :-)

Another alternative that may be looked into, with the appropriate hardware, is setting the CPU clock and then running at 100%. The current Intel Core-i line of chips is very flexible this way (the CPU multiplier can be set discreetly through the entire range), although access from Java may be problematic. This would be done in the host, of course, not VMware.

Happy coding.

Community
  • 1
  • 1