JVM 如何决定对方法进行 JIT 编译(将方法归类为“热")? [英] How does the JVM decided to JIT-compile a method (categorize a method as "hot")?

查看:34
本文介绍了JVM 如何决定对方法进行 JIT 编译(将方法归类为“热")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用过 -XX:+PrintCompilation,并且我知道 JIT 编译器的基本技术以及为什么使用 JIT 编译.

I already worked with -XX:+PrintCompilation, and I know the basic techniques of the JIT-compiler and why JIT-compilation is used.

然而,我仍然没有发现 JVM 如何决定 JIT 编译一个方法,即在合适的时间来 JIT 编译一个方法".

Yet I still have not found out how the JVM decides to JIT-compile a method, i.e. "when the right time has come to JIT-compile a method".

我是否正确地假设每个方法都开始被解释,并且只要它不被归类为热方法"就不会被编译?我脑后有一些东西,我读到一个方法在执行至少 10.000 次时被认为是热的"(在解释该方法 10.000 次后,它将被编译),但我不得不承认我是不确定这个或我在哪里读过这个.

Am I right in the assumption that every method starts being interpreted, and as long as it is not categorized as "hot method" it will not be compiled? I have something in the back of my head that I read that a method is considered "hot" when it was executed at least 10.000 times (after interpreting the method 10.000 times, it will be compiled), but I have to admit that I am not sure about this or where I've read this.

总结一下我的问题:

(1) 每个方法是否被解释,只要它没有被归类为热"方法(因此已经被编译),或者即使方法被编译,是否也有理由被编译不热"?

(1) Is every method interpreted as long as it not has been categorized as "hot" method (and therefore has been compiled) or are there reasons for methods to get compiled even if they are not "hot"?

(2) JVM 如何将方法分为非热"和热"方法?执行次数?还有什么?

(2) How does the JVM categorize methods into "non-hot" and "hot" methods? Number of execution? Anything else?

(3) 如果热"方法有特定的阈值(比如执行次数),是否有 Java 标志(-XX:...)来设置这个阈值?

(3) If there are certain thresholds (like number of executions) for "hot" methods, are there Java flags (-XX:...) to set this thresholds?

推荐答案

HotSpot 编译策略相当复杂,特别是对于分层编译,Java 8 中默认启用它.它既不是执行次数,也不是CompileThreshold 参数.

HotSpot compilation policy is rather complex, especially for Tiered Compilation, which is on by default in Java 8. It's neither a number of executions, nor a matter of CompileThreshold parameter.

最好的解释(显然,唯一合理的解释)可以在 HotSpot 资源中找到,参见 advancedThresholdPolicy.hpp.

The best explanation (apparently, the only reasonable explanation) can be found in HotSpot sources, see advancedThresholdPolicy.hpp.

我将总结这个高级编译策略的要点:

I'll summarize the main points of this advanced compilation policy:

  • 执行从第 0 层(解释器)开始.
  • 编译的主要触发器是
  • Execution starts at tier 0 (interpreter).
  • The main triggers for compilation are
  1. 方法调用计数器i;
  2. 后台计数器b.向后分支通常表示代码中的循环.
  1. method invocation counter i;
  2. backedge counter b. Backward branches typically denote a loop in the code.

  • 每当计数器达到某个频率值(TierXInvokeNotifyFreqLogTierXBackedgeNotifyFreqLog)时,就会调用一个编译策略来决定对当前运行的方法下一步做什么.根据 ib 的值和 C1 和 C2 编译器线程的当前负载,可以决定

  • Every time counters reach certain frequency value (TierXInvokeNotifyFreqLog, TierXBackedgeNotifyFreqLog), a compilation policy is called to decide what to do next with currently running method. Depending on the values of i, b and current load of C1 and C2 compiler threads it can be decided to

    • 在解释器中继续执行;
    • 在解释器中开始分析;
    • 在第 3 层使用 C1 编译方法,并提供进一步重新编译所需的完整配置文件数据;
    • 在第 2 层使用 C1 编译方法,没有配置文件,但有可能重新编译(不太可能);
    • 最终在第 1 层使用 C1 编译方法,没有配置文件或计数器(也不太可能).

    这里的关键参数是TierXInvocationThresholdTierXBackEdgeThreshold.可以根据编译队列的长度为给定方法动态调整阈值.

    Key parameters here are TierXInvocationThreshold and TierXBackEdgeThreshold. Thresholds can be dynamically adjusted for a given method depending on the length of compilation queue.

    编译队列不是先进先出,而是一个优先队列.

    Compilation queue is not FIFO, but rather a priority queue.

    带有配置文件数据(第 3 层)的 C1 编译代码的行为类似,只是切换到下一个级别(C2,第 4 层)的阈值要大得多.例如.一个解释的方法可以在大约 200 次调用后在第 3 层编译,而 C1 编译的方法在 5000 次调用后需要在第 4 层重新编译.

    C1-compiled code with profile data (tier 3) behave similarly, except that thresholds for switching to the next level (C2, tier 4) are much bigger. E.g. an interpreted method can be compiled at tier 3 after about 200 invocations, while C1-compiled method is subject for recompilation at tier 4 after 5000+ invocations.

    这篇关于JVM 如何决定对方法进行 JIT 编译(将方法归类为“热")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆