我刚刚了解了JMH微基准框架(http://openjdk.java.net/projects/code-tools/jmh/)。我只是想知道他们是如何实现@CompileControl注释函数的。从源代码中,他们在单个文本文件中简单地将指令(提示)列表写入编译器。
我只是想知道是否可以找到一些基础机制的附加文档,它只适用于OpenJDK。
答案 0 :(得分:3)
JMH使用特定于HotSpot的CompilerOracle来控制JIT编译器。这显然适用于任何基于HotSpot的VM,包括vanilla 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=...
传递给VM。 JMH @CompilerControl
工具只是在不弄乱原始命令行选项的情况下声明CompileOracle命令的便捷方式。