Is it possible to force an existing Java application to use no more than x cores?

吃可爱长大的小学妹 提交于 2019-12-17 09:29:09

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/8882885/is-it-possible-to-force-an-existing-java-application-to-use-no-more-than-x-cores

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!