Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/runtime/advancedThresholdPolicy.hpp
32285 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_RUNTIME_ADVANCEDTHRESHOLDPOLICY_HPP25#define SHARE_VM_RUNTIME_ADVANCEDTHRESHOLDPOLICY_HPP2627#include "runtime/simpleThresholdPolicy.hpp"2829#ifdef TIERED30class CompileTask;31class CompileQueue;3233/*34* The system supports 5 execution levels:35* * level 0 - interpreter36* * level 1 - C1 with full optimization (no profiling)37* * level 2 - C1 with invocation and backedge counters38* * level 3 - C1 with full profiling (level 2 + MDO)39* * level 4 - C240*41* Levels 0, 2 and 3 periodically notify the runtime about the current value of the counters42* (invocation counters and backedge counters). The frequency of these notifications is43* different at each level. These notifications are used by the policy to decide what transition44* to make.45*46* Execution starts at level 0 (interpreter), then the policy can decide either to compile the47* method at level 3 or level 2. The decision is based on the following factors:48* 1. The length of the C2 queue determines the next level. The observation is that level 249* is generally faster than level 3 by about 30%, therefore we would want to minimize the time50* a method spends at level 3. We should only spend the time at level 3 that is necessary to get51* adequate profiling. So, if the C2 queue is long enough it is more beneficial to go first to52* level 2, because if we transitioned to level 3 we would be stuck there until our C2 compile53* request makes its way through the long queue. When the load on C2 recedes we are going to54* recompile at level 3 and start gathering profiling information.55* 2. The length of C1 queue is used to dynamically adjust the thresholds, so as to introduce56* additional filtering if the compiler is overloaded. The rationale is that by the time a57* method gets compiled it can become unused, so it doesn't make sense to put too much onto the58* queue.59*60* After profiling is completed at level 3 the transition is made to level 4. Again, the length61* of the C2 queue is used as a feedback to adjust the thresholds.62*63* After the first C1 compile some basic information is determined about the code like the number64* of the blocks and the number of the loops. Based on that it can be decided that a method65* is trivial and compiling it with C1 will yield the same code. In this case the method is66* compiled at level 1 instead of 4.67*68* We also support profiling at level 0. If C1 is slow enough to produce the level 3 version of69* the code and the C2 queue is sufficiently small we can decide to start profiling in the70* interpreter (and continue profiling in the compiled code once the level 3 version arrives).71* If the profiling at level 0 is fully completed before level 3 version is produced, a level 272* version is compiled instead in order to run faster waiting for a level 4 version.73*74* Compile queues are implemented as priority queues - for each method in the queue we compute75* the event rate (the number of invocation and backedge counter increments per unit of time).76* When getting an element off the queue we pick the one with the largest rate. Maintaining the77* rate also allows us to remove stale methods (the ones that got on the queue but stopped78* being used shortly after that).79*/8081/* Command line options:82* - Tier?InvokeNotifyFreqLog and Tier?BackedgeNotifyFreqLog control the frequency of method83* invocation and backedge notifications. Basically every n-th invocation or backedge a mutator thread84* makes a call into the runtime.85*86* - Tier?CompileThreshold, Tier?BackEdgeThreshold, Tier?MinInvocationThreshold control87* compilation thresholds.88* Level 2 thresholds are not used and are provided for option-compatibility and potential future use.89* Other thresholds work as follows:90*91* Transition from interpreter (level 0) to C1 with full profiling (level 3) happens when92* the following predicate is true (X is the level):93*94* i > TierXInvocationThreshold * s || (i > TierXMinInvocationThreshold * s && i + b > TierXCompileThreshold * s),95*96* where $i$ is the number of method invocations, $b$ number of backedges and $s$ is the scaling97* coefficient that will be discussed further.98* The intuition is to equalize the time that is spend profiling each method.99* The same predicate is used to control the transition from level 3 to level 4 (C2). It should be100* noted though that the thresholds are relative. Moreover i and b for the 0->3 transition come101* from Method* and for 3->4 transition they come from MDO (since profiled invocations are102* counted separately).103*104* OSR transitions are controlled simply with b > TierXBackEdgeThreshold * s predicates.105*106* - Tier?LoadFeedback options are used to automatically scale the predicates described above depending107* on the compiler load. The scaling coefficients are computed as follows:108*109* s = queue_size_X / (TierXLoadFeedback * compiler_count_X) + 1,110*111* where queue_size_X is the current size of the compiler queue of level X, and compiler_count_X112* is the number of level X compiler threads.113*114* Basically these parameters describe how many methods should be in the compile queue115* per compiler thread before the scaling coefficient increases by one.116*117* This feedback provides the mechanism to automatically control the flow of compilation requests118* depending on the machine speed, mutator load and other external factors.119*120* - Tier3DelayOn and Tier3DelayOff parameters control another important feedback loop.121* Consider the following observation: a method compiled with full profiling (level 3)122* is about 30% slower than a method at level 2 (just invocation and backedge counters, no MDO).123* Normally, the following transitions will occur: 0->3->4. The problem arises when the C2 queue124* gets congested and the 3->4 transition is delayed. While the method is the C2 queue it continues125* executing at level 3 for much longer time than is required by the predicate and at suboptimal speed.126* The idea is to dynamically change the behavior of the system in such a way that if a substantial127* load on C2 is detected we would first do the 0->2 transition allowing a method to run faster.128* And then when the load decreases to allow 2->3 transitions.129*130* Tier3Delay* parameters control this switching mechanism.131* Tier3DelayOn is the number of methods in the C2 queue per compiler thread after which the policy132* no longer does 0->3 transitions but does 0->2 transitions instead.133* Tier3DelayOff switches the original behavior back when the number of methods in the C2 queue134* per compiler thread falls below the specified amount.135* The hysteresis is necessary to avoid jitter.136*137* - TieredCompileTaskTimeout is the amount of time an idle method can spend in the compile queue.138* Basically, since we use the event rate d(i + b)/dt as a value of priority when selecting a method to139* compile from the compile queue, we also can detect stale methods for which the rate has been140* 0 for some time in the same iteration. Stale methods can appear in the queue when an application141* abruptly changes its behavior.142*143* - TieredStopAtLevel, is used mostly for testing. It allows to bypass the policy logic and stick144* to a given level. For example it's useful to set TieredStopAtLevel = 1 in order to compile everything145* with pure c1.146*147* - Tier0ProfilingStartPercentage allows the interpreter to start profiling when the inequalities in the148* 0->3 predicate are already exceeded by the given percentage but the level 3 version of the149* method is still not ready. We can even go directly from level 0 to 4 if c1 doesn't produce a compiled150* version in time. This reduces the overall transition to level 4 and decreases the startup time.151* Note that this behavior is also guarded by the Tier3Delay mechanism: when the c2 queue is too long152* these is not reason to start profiling prematurely.153*154* - TieredRateUpdateMinTime and TieredRateUpdateMaxTime are parameters of the rate computation.155* Basically, the rate is not computed more frequently than TieredRateUpdateMinTime and is considered156* to be zero if no events occurred in TieredRateUpdateMaxTime.157*/158159160class AdvancedThresholdPolicy : public SimpleThresholdPolicy {161jlong _start_time;162163// Call and loop predicates determine whether a transition to a higher compilation164// level should be performed (pointers to predicate functions are passed to common().165// Predicates also take compiler load into account.166typedef bool (AdvancedThresholdPolicy::*Predicate)(int i, int b, CompLevel cur_level);167bool call_predicate(int i, int b, CompLevel cur_level);168bool loop_predicate(int i, int b, CompLevel cur_level);169// Common transition function. Given a predicate determines if a method should transition to another level.170CompLevel common(Predicate p, Method* method, CompLevel cur_level, bool disable_feedback = false);171// Transition functions.172// call_event determines if a method should be compiled at a different173// level with a regular invocation entry.174CompLevel call_event(Method* method, CompLevel cur_level);175// loop_event checks if a method should be OSR compiled at a different176// level.177CompLevel loop_event(Method* method, CompLevel cur_level);178// Has a method been long around?179// We don't remove old methods from the compile queue even if they have180// very low activity (see select_task()).181inline bool is_old(Method* method);182// Was a given method inactive for a given number of milliseconds.183// If it is, we would remove it from the queue (see select_task()).184inline bool is_stale(jlong t, jlong timeout, Method* m);185// Compute the weight of the method for the compilation scheduling186inline double weight(Method* method);187// Apply heuristics and return true if x should be compiled before y188inline bool compare_methods(Method* x, Method* y);189// Compute event rate for a given method. The rate is the number of event (invocations + backedges)190// per millisecond.191inline void update_rate(jlong t, Method* m);192// Compute threshold scaling coefficient193inline double threshold_scale(CompLevel level, int feedback_k);194// If a method is old enough and is still in the interpreter we would want to195// start profiling without waiting for the compiled method to arrive. This function196// determines whether we should do that.197inline bool should_create_mdo(Method* method, CompLevel cur_level);198// Create MDO if necessary.199void create_mdo(methodHandle mh, JavaThread* thread);200// Is method profiled enough?201bool is_method_profiled(Method* method);202203double _increase_threshold_at_ratio;204205protected:206void print_specific(EventType type, methodHandle mh, methodHandle imh, int bci, CompLevel level);207208void set_increase_threshold_at_ratio() { _increase_threshold_at_ratio = 100 / (100 - (double)IncreaseFirstTierCompileThresholdAt); }209void set_start_time(jlong t) { _start_time = t; }210jlong start_time() const { return _start_time; }211212// Submit a given method for compilation (and update the rate).213virtual void submit_compile(methodHandle mh, int bci, CompLevel level, JavaThread* thread);214// event() from SimpleThresholdPolicy would call these.215virtual void method_invocation_event(methodHandle method, methodHandle inlinee,216CompLevel level, nmethod* nm, JavaThread* thread);217virtual void method_back_branch_event(methodHandle method, methodHandle inlinee,218int bci, CompLevel level, nmethod* nm, JavaThread* thread);219public:220AdvancedThresholdPolicy() : _start_time(0) { }221// Select task is called by CompileBroker. We should return a task or NULL.222virtual CompileTask* select_task(CompileQueue* compile_queue);223virtual void initialize();224virtual bool should_not_inline(ciEnv* env, ciMethod* callee);225226};227228#endif // TIERED229230#endif // SHARE_VM_RUNTIME_ADVANCEDTHRESHOLDPOLICY_HPP231232233