欺骗 JVM 关于可用内核的数量(在 linux 上) [英] Deceive the JVM about the number of available cores (on linux)

查看:32
本文介绍了欺骗 JVM 关于可用内核的数量(在 linux 上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种目的,需要让 JVM 考虑它运行在带有 N 核心而不是实际核心数量的机器上(例如 4 核心而不是 16).

In some purpose it is needed to make JVM think about it runs on machine with Ncores 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 管理问题,也不是纯粹的程序员问题.

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 上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆