advancedThresholdPolicy.hpp revision 3602:da91efe96a93
141118Sjdp/* 241118Sjdp * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. 341118Sjdp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 441118Sjdp * 541118Sjdp * This code is free software; you can redistribute it and/or modify it 641118Sjdp * under the terms of the GNU General Public License version 2 only, as 741118Sjdp * published by the Free Software Foundation. 841118Sjdp * 941118Sjdp * This code is distributed in the hope that it will be useful, but WITHOUT 1041118Sjdp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1141118Sjdp * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1241118Sjdp * version 2 for more details (a copy is included in the LICENSE file that 1341118Sjdp * accompanied this code). 1441118Sjdp * 1541118Sjdp * You should have received a copy of the GNU General Public License version 1641118Sjdp * 2 along with this work; if not, write to the Free Software Foundation, 1741118Sjdp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1841118Sjdp * 1941118Sjdp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2041118Sjdp * or visit www.oracle.com if you need additional information or have any 2141118Sjdp * questions. 2241118Sjdp * 2341118Sjdp */ 2441118Sjdp 2541118Sjdp#ifndef SHARE_VM_RUNTIME_ADVANCEDTHRESHOLDPOLICY_HPP 2641118Sjdp#define SHARE_VM_RUNTIME_ADVANCEDTHRESHOLDPOLICY_HPP 2784219Sdillon 2884219Sdillon#include "runtime/simpleThresholdPolicy.hpp" 2984219Sdillon 3041118Sjdp#ifdef TIERED 3141118Sjdpclass CompileTask; 3241118Sjdpclass CompileQueue; 3341118Sjdp 3441118Sjdp/* 35128684Sru * The system supports 5 execution levels: 36128684Sru * * level 0 - interpreter 37128684Sru * * level 1 - C1 with full optimization (no profiling) 38128684Sru * * level 2 - C1 with invocation and backedge counters 39128684Sru * * level 3 - C1 with full profiling (level 2 + MDO) 40128684Sru * * level 4 - C2 41128684Sru * 42128684Sru * Levels 0, 2 and 3 periodically notify the runtime about the current value of the counters 43128684Sru * (invocation counters and backedge counters). The frequency of these notifications is 44128684Sru * different at each level. These notifications are used by the policy to decide what transition 4541118Sjdp * to make. 46243956Ssem * 47243956Ssem * Execution starts at level 0 (interpreter), then the policy can decide either to compile the 48128684Sru * method at level 3 or level 2. The decision is based on the following factors: 49128684Sru * 1. The length of the C2 queue determines the next level. The observation is that level 2 50128684Sru * is generally faster than level 3 by about 30%, therefore we would want to minimize the time 5141118Sjdp * a method spends at level 3. We should only spend the time at level 3 that is necessary to get 5241118Sjdp * adequate profiling. So, if the C2 queue is long enough it is more beneficial to go first to 5341118Sjdp * level 2, because if we transitioned to level 3 we would be stuck there until our C2 compile 5441118Sjdp * request makes its way through the long queue. When the load on C2 recedes we are going to 5541118Sjdp * recompile at level 3 and start gathering profiling information. 5641118Sjdp * 2. The length of C1 queue is used to dynamically adjust the thresholds, so as to introduce 5741118Sjdp * additional filtering if the compiler is overloaded. The rationale is that by the time a 5841118Sjdp * method gets compiled it can become unused, so it doesn't make sense to put too much onto the 5941118Sjdp * queue. 6041118Sjdp * 6141118Sjdp * After profiling is completed at level 3 the transition is made to level 4. Again, the length 6241118Sjdp * of the C2 queue is used as a feedback to adjust the thresholds. 6341118Sjdp * 6441118Sjdp * After the first C1 compile some basic information is determined about the code like the number 6541118Sjdp * of the blocks and the number of the loops. Based on that it can be decided that a method 6652709Sjdp * is trivial and compiling it with C1 will yield the same code. In this case the method is 67128684Sru * compiled at level 1 instead of 4. 6841118Sjdp * 6941118Sjdp * We also support profiling at level 0. If C1 is slow enough to produce the level 3 version of 7041118Sjdp * the code and the C2 queue is sufficiently small we can decide to start profiling in the 7141118Sjdp * interpreter (and continue profiling in the compiled code once the level 3 version arrives). 7241118Sjdp * If the profiling at level 0 is fully completed before level 3 version is produced, a level 2 7341118Sjdp * version is compiled instead in order to run faster waiting for a level 4 version. 7441118Sjdp * 7541118Sjdp * Compile queues are implemented as priority queues - for each method in the queue we compute 7641118Sjdp * the event rate (the number of invocation and backedge counter increments per unit of time). 7741118Sjdp * When getting an element off the queue we pick the one with the largest rate. Maintaining the 7841118Sjdp * rate also allows us to remove stale methods (the ones that got on the queue but stopped 7941118Sjdp * being used shortly after that). 8041118Sjdp*/ 8141118Sjdp 8241118Sjdp/* Command line options: 8368499Seivind * - Tier?InvokeNotifyFreqLog and Tier?BackedgeNotifyFreqLog control the frequency of method 8441118Sjdp * invocation and backedge notifications. Basically every n-th invocation or backedge a mutator thread 8541118Sjdp * makes a call into the runtime. 8641118Sjdp * 8741118Sjdp * - Tier?CompileThreshold, Tier?BackEdgeThreshold, Tier?MinInvocationThreshold control 8841118Sjdp * compilation thresholds. 8941118Sjdp * Level 2 thresholds are not used and are provided for option-compatibility and potential future use. 9041118Sjdp * Other thresholds work as follows: 9141118Sjdp * 9241118Sjdp * Transition from interpreter (level 0) to C1 with full profiling (level 3) happens when 9341118Sjdp * the following predicate is true (X is the level): 9441118Sjdp * 9541118Sjdp * i > TierXInvocationThreshold * s || (i > TierXMinInvocationThreshold * s && i + b > TierXCompileThreshold * s), 9641118Sjdp * 9741118Sjdp * where $i$ is the number of method invocations, $b$ number of backedges and $s$ is the scaling 9841118Sjdp * coefficient that will be discussed further. 9941118Sjdp * The intuition is to equalize the time that is spend profiling each method. 100128684Sru * The same predicate is used to control the transition from level 3 to level 4 (C2). It should be 10141118Sjdp * noted though that the thresholds are relative. Moreover i and b for the 0->3 transition come 10241118Sjdp * from Method* and for 3->4 transition they come from MDO (since profiled invocations are 10341118Sjdp * counted separately). 10441118Sjdp * 10541118Sjdp * OSR transitions are controlled simply with b > TierXBackEdgeThreshold * s predicates. 10641118Sjdp * 10741118Sjdp * - Tier?LoadFeedback options are used to automatically scale the predicates described above depending 108197086Smav * on the compiler load. The scaling coefficients are computed as follows: 10941118Sjdp * 11041118Sjdp * s = queue_size_X / (TierXLoadFeedback * compiler_count_X) + 1, 11141118Sjdp * 11241118Sjdp * where queue_size_X is the current size of the compiler queue of level X, and compiler_count_X 11341118Sjdp * is the number of level X compiler threads. 11441118Sjdp * 11541118Sjdp * Basically these parameters describe how many methods should be in the compile queue 11641118Sjdp * per compiler thread before the scaling coefficient increases by one. 11741118Sjdp * 11841118Sjdp * This feedback provides the mechanism to automatically control the flow of compilation requests 11941118Sjdp * depending on the machine speed, mutator load and other external factors. 12041118Sjdp * 12141118Sjdp * - Tier3DelayOn and Tier3DelayOff parameters control another important feedback loop. 12241118Sjdp * Consider the following observation: a method compiled with full profiling (level 3) 12341118Sjdp * is about 30% slower than a method at level 2 (just invocation and backedge counters, no MDO). 12441118Sjdp * Normally, the following transitions will occur: 0->3->4. The problem arises when the C2 queue 125197086Smav * gets congested and the 3->4 transition is delayed. While the method is the C2 queue it continues 12641118Sjdp * executing at level 3 for much longer time than is required by the predicate and at suboptimal speed. 12741118Sjdp * The idea is to dynamically change the behavior of the system in such a way that if a substantial 12841118Sjdp * load on C2 is detected we would first do the 0->2 transition allowing a method to run faster. 12941118Sjdp * And then when the load decreases to allow 2->3 transitions. 13052709Sjdp * 131197086Smav * Tier3Delay* parameters control this switching mechanism. 13252709Sjdp * Tier3DelayOn is the number of methods in the C2 queue per compiler thread after which the policy 13352709Sjdp * no longer does 0->3 transitions but does 0->2 transitions instead. 13452709Sjdp * Tier3DelayOff switches the original behavior back when the number of methods in the C2 queue 13552709Sjdp * per compiler thread falls below the specified amount. 136197086Smav * The hysteresis is necessary to avoid jitter. 13752709Sjdp * 13852709Sjdp * - TieredCompileTaskTimeout is the amount of time an idle method can spend in the compile queue. 13952709Sjdp * Basically, since we use the event rate d(i + b)/dt as a value of priority when selecting a method to 140197086Smav * compile from the compile queue, we also can detect stale methods for which the rate has been 141197086Smav * 0 for some time in the same iteration. Stale methods can appear in the queue when an application 142197086Smav * abruptly changes its behavior. 143197086Smav * 144197086Smav * - TieredStopAtLevel, is used mostly for testing. It allows to bypass the policy logic and stick 145197086Smav * to a given level. For example it's useful to set TieredStopAtLevel = 1 in order to compile everything 14652709Sjdp * with pure c1. 147197086Smav * 14852709Sjdp * - Tier0ProfilingStartPercentage allows the interpreter to start profiling when the inequalities in the 14952709Sjdp * 0->3 predicate are already exceeded by the given percentage but the level 3 version of the 150128684Sru * method is still not ready. We can even go directly from level 0 to 4 if c1 doesn't produce a compiled 151197086Smav * version in time. This reduces the overall transition to level 4 and decreases the startup time. 152128684Sru * Note that this behavior is also guarded by the Tier3Delay mechanism: when the c2 queue is too long 153128684Sru * these is not reason to start profiling prematurely. 154128684Sru * 155128684Sru * - TieredRateUpdateMinTime and TieredRateUpdateMaxTime are parameters of the rate computation. 156128684Sru * Basically, the rate is not computed more frequently than TieredRateUpdateMinTime and is considered 157128684Sru * to be zero if no events occurred in TieredRateUpdateMaxTime. 158197086Smav */ 159128684Sru 160128684Sru 161128684Sruclass AdvancedThresholdPolicy : public SimpleThresholdPolicy { 162128684Sru jlong _start_time; 163197086Smav 164197086Smav // Call and loop predicates determine whether a transition to a higher compilation 165197086Smav // level should be performed (pointers to predicate functions are passed to common(). 166197086Smav // Predicates also take compiler load into account. 167197086Smav typedef bool (AdvancedThresholdPolicy::*Predicate)(int i, int b, CompLevel cur_level); 168197086Smav bool call_predicate(int i, int b, CompLevel cur_level); 169197086Smav bool loop_predicate(int i, int b, CompLevel cur_level); 170128684Sru // Common transition function. Given a predicate determines if a method should transition to another level. 171128684Sru CompLevel common(Predicate p, Method* method, CompLevel cur_level, bool disable_feedback = false); 172128684Sru // Transition functions. 173197086Smav // call_event determines if a method should be compiled at a different 174128684Sru // level with a regular invocation entry. 175128684Sru CompLevel call_event(Method* method, CompLevel cur_level); 176128684Sru // loop_event checks if a method should be OSR compiled at a different 177128684Sru // level. 17841118Sjdp CompLevel loop_event(Method* method, CompLevel cur_level); 17941118Sjdp // Has a method been long around? 18041118Sjdp // We don't remove old methods from the compile queue even if they have 18141118Sjdp // very low activity (see select_task()). 18241118Sjdp inline bool is_old(Method* method); 18341118Sjdp // Was a given method inactive for a given number of milliseconds. 18441118Sjdp // If it is, we would remove it from the queue (see select_task()). 18541118Sjdp inline bool is_stale(jlong t, jlong timeout, Method* m); 18641118Sjdp // Compute the weight of the method for the compilation scheduling 187128684Sru inline double weight(Method* method); 18841118Sjdp // Apply heuristics and return true if x should be compiled before y 18941118Sjdp inline bool compare_methods(Method* x, Method* y); 190128684Sru // Compute event rate for a given method. The rate is the number of event (invocations + backedges) 191128684Sru // per millisecond. 192128684Sru inline void update_rate(jlong t, Method* m); 193168341Skan // Compute threshold scaling coefficient 194168341Skan inline double threshold_scale(CompLevel level, int feedback_k); 195128684Sru // If a method is old enough and is still in the interpreter we would want to 19641118Sjdp // start profiling without waiting for the compiled method to arrive. This function 19741118Sjdp // determines whether we should do that. 19841118Sjdp inline bool should_create_mdo(Method* method, CompLevel cur_level); 19941118Sjdp // Create MDO if necessary. 20041118Sjdp void create_mdo(methodHandle mh, JavaThread* thread); 20141118Sjdp // Is method profiled enough? 20241118Sjdp bool is_method_profiled(Method* method); 20341118Sjdp 20441118Sjdpprotected: 20541118Sjdp void print_specific(EventType type, methodHandle mh, methodHandle imh, int bci, CompLevel level); 206197086Smav 20741118Sjdp void set_start_time(jlong t) { _start_time = t; } 208197086Smav jlong start_time() const { return _start_time; } 209197086Smav 21041118Sjdp // Submit a given method for compilation (and update the rate). 21141118Sjdp virtual void submit_compile(methodHandle mh, int bci, CompLevel level, JavaThread* thread); 21241118Sjdp // event() from SimpleThresholdPolicy would call these. 21341118Sjdp virtual void method_invocation_event(methodHandle method, methodHandle inlinee, 214197086Smav CompLevel level, nmethod* nm, JavaThread* thread); 215197086Smav virtual void method_back_branch_event(methodHandle method, methodHandle inlinee, 216197086Smav int bci, CompLevel level, nmethod* nm, JavaThread* thread); 21741118Sjdppublic: 21841118Sjdp AdvancedThresholdPolicy() : _start_time(0) { } 219197086Smav // Select task is called by CompileBroker. We should return a task or NULL. 22041118Sjdp virtual CompileTask* select_task(CompileQueue* compile_queue); 22141118Sjdp virtual void initialize(); 222128684Sru virtual bool should_not_inline(ciEnv* env, ciMethod* callee); 223128684Sru 224128684Sru}; 225128684Sru 226128684Sru#endif // TIERED 227197086Smav 228128684Sru#endif // SHARE_VM_RUNTIME_ADVANCEDTHRESHOLDPOLICY_HPP 229197086Smav