Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

prefetch instruction in JVM/JAVA

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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);

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

Usage example:

long[] array = new long[100*1024*1024];
// ...
sun.misc.Unsafe.prefetchReadStatic(array, 50*1024*1024);

UPDATE

Unsafe.prefetch* intrinsics are completely removed in JDK 9:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...