JVM / JAVA中的预取指令 [英] prefetch instruction in JVM/JAVA

查看:292
本文介绍了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实际上支持 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 * 内在函数完全删除


注意读取/写入预取支持是作为
的实验实现的如果JDK库代码可以使用它来提高性能。
然而,实验结果并未表明这是
值得的。因此,sun.misc.Unsafe中没有相应的prefetch
本机方法声明。

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.

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

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