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

Mar*_*ten 1 java benchmarking inline

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

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

Ale*_*lev 5

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
Run Code Online (Sandbox Code Playgroud)

您在生成的CompilerHints资源文件中看到的基本上是 CompilerOracle 命令,逐字逐句;这些通过-XX:CompileCommandFile=.... JMH@CompilerControl工具只是声明 CompileOracle 命令的便捷方式,而不会弄乱原始命令行选项。