本文介绍了JVM / JAVA中的预取指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有Java语言或JVM中的任何软件预取指令,如在GCC中可用

Is there any software prefetching instructions in Java language or JVM, like __builtin_prefetch which is available in GCC

推荐答案

有趣的是Hotspot JVM实际上支持 prefetch!

它处理 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 * 内在函数完全:

这篇关于JVM / JAVA中的预取指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 08:26