我刚刚了解了 JMH 微基准测试框架 (http://openjdk.java.net/projects/code-tools/jmh/)。我只是想知道他们是如何实现@CompileControl注解功能的。他们从源代码中将指令(提示)列表简单地写入单个文本文件中的编译器。
我只是想知道是否可以找到一些有关底层机制的附加文档,它仅适用于 OpenJDK。
JMH 使用 HotSpot 特定的 CompilerOracle,它控制 JIT 编译器。这显然适用于任何基于 HotSpot 的 VM,包括普通 OpenJDK 和 Oracle JDK 版本。使用以下命令运行 OpenJDK:
$ java -XX:CompileCommand=help
CompileCommand and the CompilerOracle allows simple control over
what's allowed to be compiled. The standard supported directives
are exclude and compileonly. The exclude directive stops a method
from being compiled and compileonly excludes all methods except for
the ones mentioned by compileonly directives. The basic form of
all commands is a command name followed by the name of the method
in one of two forms: the standard class file format as in
class/name.methodName or the PrintCompilation format
class.name::methodName. The method name can optionally be followed
by a space then the signature of the method in the class file
format. Otherwise the directive applies to all methods with the
same name and class regardless of signature. Leading and trailing
*'s in the class and/or method name allows a small amount of
wildcarding.
Examples:
exclude java/lang/StringBuffer.append
compileonly java/lang/StringBuffer.toString ()Ljava/lang/String;
exclude java/lang/String*.*
exclude *.toString
您在生成的
CompilerHints
资源文件中看到的基本上是逐字逐句的 CompilerOracle 命令;这些通过 -XX:CompileCommandFile=...
传递到虚拟机。 JMH @CompilerControl
设施只是声明 CompileOracle 命令的便捷方法,而不会弄乱原始命令行选项。
这并不是说 JMH 喜欢禁用内联。 JMH 中的某些方法不应该内联,例如 Blackhole.consumeFull()。