33

We are benchmarking existing Java programs. They are threaded applications designed to benefit from multi-core CPUs. We would like to measure the effect of the number of cores on the running speed, but we are unwilling (and unable) to change the code of these applications.

Of course, we could test the software on different machines, but this is expensive and complicated. We would rather have a software solution.

Note: you can assume that the testing platform is either Windows, Linux or Mac. Ideally, we would like to be able to run the tests on either of these platforms.

Daniel Lemire
  • 3,470
  • 2
  • 25
  • 23
  • I don't think it is controlled in java programs, I guess it is pure OS specific decision. I am curious to see experts comment on this question. – kosa Jan 16 '12 at 16:21
  • If you need some custom software solution / customization that will most probably will be much more expensive than the hardware itself. And I am not sure if you can force a java program to use only one core at any moment anyway. OS/JVM related things – fmucar Jan 16 '12 at 16:21
  • Consider running inside a virtual machine like VirtualBox. You then have a simple GUI to determine the cpu power available to the instance. – Thorbjørn Ravn Andersen Apr 13 '13 at 12:59

2 Answers2

36

It's called setting CPU affinity, and it's an OS setting for processes, not specific to Java.

On Linux: http://www.cyberciti.biz/tips/setting-processor-affinity-certain-task-or-process.html

On Windows: http://www.addictivetips.com/windows-tips/how-to-set-processor-affinity-to-an-application-in-windows/

On Mac it doesn't look like you can set it: https://superuser.com/questions/149312/how-to-set-processor-affinity-on-os-x

Community
  • 1
  • 1
Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • Also, check out https://github.com/peter-lawrey/Java-Thread-Affinity for a java library that may help you with your tests. – philwb Jan 16 '12 at 20:08
  • Additionally (despite this may be implementation-dependent), if you set the CPU affinity *before* invoking `Runtime.getRuntime().availableProcessors()`, this method will reflect the change and return the (reduced) number of cores *available* for the JVM -- in exact accordance with its [javadoc](http://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#availableProcessors--). – Bass Oct 23 '15 at 11:09
-9

Turn the problem upside down; instead of figuring out how to restrict the application to only use n cores on an m core machine, figure out how to make sure there are only n cores available to your application. You could write a very simple program that eats up one core by doing something like while (true); and start m-n instances of that program running at the highest priority, thus ensuring that your code only has n cores available.

Alternatively, if your BIOS or OS allows disabling CPU cores, that works out too. I know CHUD tools on Mac OS X used to do it, but I haven't touched those tools since the G5 era, so that might not work anymore.