是否可以强制现有的Java应用程序使用不超过x个内核?

时间:2021-06-21 17:21:47

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.

我们正在对现有Java程序进行基准测试它们是旨在从多核CPU中受益的线程应用程序。我们想测量内核数量对运行速度的影响,但我们不愿意(也无法)更改这些应用程序的代码。

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.

注意:您可以假设测试平台是Windows,Linux或Mac。理想情况下,我们希望能够在这两个平台上运行测试。

2 个解决方案

#1


33  

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

它被称为设置CPU亲和性,它是进程的操作系统设置,不是特定于Java的。

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

在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/

在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

在Mac上看起来不像你可以设置它:https://superuser.com/questions/149312/how-to-set-processor-affinity-on-os-x

#2


-6  

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.

把问题颠倒过来;而不是弄清楚如何将应用程序限制为仅在m核心机器上使用n个核心,而不是确定如何确保应用程序只有n个核心可用。您可以编写一个非常简单的程序,通过执行类似while(true)的操作来占用一个核心;并启动以最高优先级运行的该程序的m-n个实例,从而确保您的代码只有n个核心可用。

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.

或者,如果您的BIOS或操作系统允许禁用CPU内核,那也可以解决问题。我知道Mac OS X上的CHUD工具曾经这样做过,但是自从G5时代以来我还没有接触到这些工具,所以这可能不再适用了。

#1


33  

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

它被称为设置CPU亲和性,它是进程的操作系统设置,不是特定于Java的。

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

在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/

在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

在Mac上看起来不像你可以设置它:https://superuser.com/questions/149312/how-to-set-processor-affinity-on-os-x

#2


-6  

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.

把问题颠倒过来;而不是弄清楚如何将应用程序限制为仅在m核心机器上使用n个核心,而不是确定如何确保应用程序只有n个核心可用。您可以编写一个非常简单的程序,通过执行类似while(true)的操作来占用一个核心;并启动以最高优先级运行的该程序的m-n个实例,从而确保您的代码只有n个核心可用。

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.

或者,如果您的BIOS或操作系统允许禁用CPU内核,那也可以解决问题。我知道Mac OS X上的CHUD工具曾经这样做过,但是自从G5时代以来我还没有接触到这些工具,所以这可能不再适用了。