JMH 喜欢禁用方法内联 - 它是如何完成的?

JMH like disabling method inlining - How is it done?

我刚刚了解了 JMH 微型基准测试框架 (http://openjdk.java.net/projects/code-tools/jmh/)。我只是想知道他们是如何实现@CompileControl注解功能的。他们从源代码中简单地将指令列表(提示)写入单个文本文件中的编译器。

我只是想知道我是否可以找到一些底层机制的额外文档,它仅适用于 OpenJDK。

JMH 使用 HotSpot 特定的 CompilerOracle,它控制 JIT 编译器。这显然适用于任何基于 HotSpot 的 VM,包括 vanilla OpenJDK 和 Oracle JDK 构建。 运行 打开JDK 用:

$ 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 命令的便捷方式,而不会弄乱原始命令行选项。