JVM/JAVA 中的预取指令
是否有 Java 语言或 JVM 中的软件预取指令,例如 __builtin_prefetch 在 GCC 中可用
Is there any software prefetching instructions in Java language or JVM, like __builtin_prefetch which is available in GCC
推荐答案
Hotspot JVM 实际上确实支持预取!
它将 Unsafe.prefetchRead()
和 Unsafe.prefetchWrite()
方法视为内在函数,并将它们编译为相应的 CPU 指令.
One interesting thing is that Hotspot JVM actually does support prefetch!
It treats Unsafe.prefetchRead()
and Unsafe.prefetchWrite()
methods as intrinsics and compiles them into corresponding CPU instructions.
很遗憾,sun.misc.Unsafe
没有声明此类方法.但是,如果您将以下方法添加到 Unsafe.java,重新编译它并替换 rt.jar 中的 Unsafe.class(或仅添加 -Xbootclasspath/p
JVM 参数),您将能够使用预取内在函数在您的应用程序中.
Unfortunately, sun.misc.Unsafe
does not declare such methods. But, if you add the following methods to Unsafe.java, recompile it and replace Unsafe.class inside rt.jar (or just add -Xbootclasspath/p
JVM argument) you would be able to use prefetch intrinsics in your application.
public native void prefetchRead(Object o, long offset);
public native void prefetchWrite(Object o, long offset);
public static native void prefetchReadStatic(Object o, long offset);
public static native void prefetchWriteStatic(Object o, long offset);
我怀疑这对实际应用程序有多大帮助,但如果您想使用它,我可以提供更多详细信息.
这是 JDK 8 的已编译补丁,可启用预取方法:下载
I doubt this could help much in real applications, but if you'd like to play with it, I can provide more details.
Here is a compiled patch to JDK 8 that enables prefetch methods: download
使用示例:
long[] array = new long[100*1024*1024];
// ...
sun.misc.Unsafe.prefetchReadStatic(array, 50*1024*1024);
更新
Unsafe.prefetch*
内部函数已完全删除 在 JDK 9 中:
Unsafe.prefetch*
intrinsics are completely removed in JDK 9:
注意读/写预取支持是作为实验实现的看看 JDK 库代码是否可以使用它来获得性能优势.但是,实验结果并不表明这是值得.因此没有相应的预取sun.misc.Unsafe 中的本机方法声明.
Note read/write prefetch support was implemented as an experiment to see if JDK library code could use it for performance advantages. However, the results of the experiment did not indicate this was worthwhile. As a consequence there are no corresponding prefetch native method declarations in sun.misc.Unsafe.
相关文章