问题描述
出于某种目的,有必要让JVM
考虑它在板上装有N
个内核而不是实际内核数(例如4
内核而不是16
)的机器上运行.
In some purpose it is needed to make JVM
think about it runs on machine with N
cores on board instead of real number of cores (e.g. 4
cores instead of 16
).
JVM
在基于Mandriva/Red Hat Linux内核的某些Linux版本下运行.
JVM
runs under some Linux build, based on Mandriva/Red Hat Linux core.
这个问题是临界情况,因为我希望这个问题有各种解决方案.这不是纯粹的linux-administration问题,也不是纯粹的程序员问题.
This question is borderline case because I expect various solutions of this problem. This is not pure linux-administration question, and it isn't pure programmer's question.
那么...有什么想法吗?
So... any ideas?
推荐答案
以下Java程序显示Java VM看到的处理器数量:
The following Java program prints the number of processors as seen by the Java VM:
public class AvailableProcessors {
public static void main(String... args) {
System.out.println(Runtime.getRuntime().availableProcessors());
}
}
如果我在家用计算机上执行此程序,它将显示4
,这是实际的内核数(包括超线程).现在,让我们欺骗Java VM使其相信只有两个处理器:
If I execute this program on my home computer, it prints 4
, which is the actual number of cores (including hyper threading). Now let's trick the Java VM into believing there are only two processors:
$ echo '0-1' > /tmp/online
$ mount --bind /tmp/online /sys/devices/system/cpu/online
如果我再次运行上述程序,它将打印2
而不是4
.
If I run the above program again, it prints 2
instead of 4
.
此技巧会影响系统上的所有进程.但是,可以将效果仅限制在某些过程中. Linux上的每个进程都可以拥有自己的安装点名称空间.例如,请参见以下部分中的预处理名称空间部分. mount(2)的手册页.例如,您可以使用 lxc 以其自己的 mount名称空间.
This trick affects all processes on your system. However, it's possible to restrict the effect only to certain processes. Each process on Linux can have its own namespace of mount points. See for example the section Pre-process namespaces in the man page of mount(2). You can for example use lxc to start new processes with their own mount namespace.
这篇关于欺骗JVM有关可用内核数的信息(在Linux上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!