1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/interpreter/invocationCounter.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,169 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "interpreter/invocationCounter.hpp" 1.30 +#include "runtime/frame.hpp" 1.31 +#include "runtime/handles.inline.hpp" 1.32 + 1.33 + 1.34 +// Implementation of InvocationCounter 1.35 + 1.36 +void InvocationCounter::init() { 1.37 + _counter = 0; // reset all the bits, including the sticky carry 1.38 + reset(); 1.39 +} 1.40 + 1.41 +void InvocationCounter::reset() { 1.42 + // Only reset the state and don't make the method look like it's never 1.43 + // been executed 1.44 + set_state(wait_for_compile); 1.45 +} 1.46 + 1.47 +void InvocationCounter::set_carry() { 1.48 + set_carry_flag(); 1.49 + // The carry bit now indicates that this counter had achieved a very 1.50 + // large value. Now reduce the value, so that the method can be 1.51 + // executed many more times before re-entering the VM. 1.52 + int old_count = count(); 1.53 + int new_count = MIN2(old_count, (int) (CompileThreshold / 2)); 1.54 + // prevent from going to zero, to distinguish from never-executed methods 1.55 + if (new_count == 0) new_count = 1; 1.56 + if (old_count != new_count) set(state(), new_count); 1.57 +} 1.58 + 1.59 +void InvocationCounter::set_state(State state) { 1.60 + assert(0 <= state && state < number_of_states, "illegal state"); 1.61 + int init = _init[state]; 1.62 + // prevent from going to zero, to distinguish from never-executed methods 1.63 + if (init == 0 && count() > 0) init = 1; 1.64 + int carry = (_counter & carry_mask); // the carry bit is sticky 1.65 + _counter = (init << number_of_noncount_bits) | carry | state; 1.66 +} 1.67 + 1.68 + 1.69 +void InvocationCounter::print() { 1.70 + tty->print_cr("invocation count: up = %d, limit = %d, carry = %s, state = %s", 1.71 + count(), limit(), 1.72 + carry() ? "true" : "false", 1.73 + state_as_string(state())); 1.74 +} 1.75 + 1.76 +void InvocationCounter::print_short() { 1.77 + tty->print(" [%d%s;%s]", count(), carry()?"+carry":"", state_as_short_string(state())); 1.78 +} 1.79 + 1.80 +// Initialization 1.81 + 1.82 +int InvocationCounter::_init [InvocationCounter::number_of_states]; 1.83 +InvocationCounter::Action InvocationCounter::_action[InvocationCounter::number_of_states]; 1.84 +int InvocationCounter::InterpreterInvocationLimit; 1.85 +int InvocationCounter::InterpreterBackwardBranchLimit; 1.86 +int InvocationCounter::InterpreterProfileLimit; 1.87 + 1.88 + 1.89 +const char* InvocationCounter::state_as_string(State state) { 1.90 + switch (state) { 1.91 + case wait_for_nothing : return "wait_for_nothing"; 1.92 + case wait_for_compile : return "wait_for_compile"; 1.93 + } 1.94 + ShouldNotReachHere(); 1.95 + return NULL; 1.96 +} 1.97 + 1.98 +const char* InvocationCounter::state_as_short_string(State state) { 1.99 + switch (state) { 1.100 + case wait_for_nothing : return "not comp."; 1.101 + case wait_for_compile : return "compileable"; 1.102 + } 1.103 + ShouldNotReachHere(); 1.104 + return NULL; 1.105 +} 1.106 + 1.107 + 1.108 +static address do_nothing(methodHandle method, TRAPS) { 1.109 + // dummy action for inactive invocation counters 1.110 + MethodCounters* mcs = method->method_counters(); 1.111 + assert(mcs != NULL, ""); 1.112 + mcs->invocation_counter()->set_carry(); 1.113 + mcs->invocation_counter()->set_state(InvocationCounter::wait_for_nothing); 1.114 + return NULL; 1.115 +} 1.116 + 1.117 + 1.118 +static address do_decay(methodHandle method, TRAPS) { 1.119 + // decay invocation counters so compilation gets delayed 1.120 + MethodCounters* mcs = method->method_counters(); 1.121 + assert(mcs != NULL, ""); 1.122 + mcs->invocation_counter()->decay(); 1.123 + return NULL; 1.124 +} 1.125 + 1.126 + 1.127 +void InvocationCounter::def(State state, int init, Action action) { 1.128 + assert(0 <= state && state < number_of_states, "illegal state"); 1.129 + assert(0 <= init && init < count_limit, "initial value out of range"); 1.130 + _init [state] = init; 1.131 + _action[state] = action; 1.132 +} 1.133 + 1.134 +address dummy_invocation_counter_overflow(methodHandle m, TRAPS) { 1.135 + ShouldNotReachHere(); 1.136 + return NULL; 1.137 +} 1.138 + 1.139 +void InvocationCounter::reinitialize(bool delay_overflow) { 1.140 + // define states 1.141 + guarantee((int)number_of_states <= (int)state_limit, "adjust number_of_state_bits"); 1.142 + def(wait_for_nothing, 0, do_nothing); 1.143 + if (delay_overflow) { 1.144 + def(wait_for_compile, 0, do_decay); 1.145 + } else { 1.146 + def(wait_for_compile, 0, dummy_invocation_counter_overflow); 1.147 + } 1.148 + 1.149 + InterpreterInvocationLimit = CompileThreshold << number_of_noncount_bits; 1.150 + InterpreterProfileLimit = ((CompileThreshold * InterpreterProfilePercentage) / 100)<< number_of_noncount_bits; 1.151 + 1.152 + // When methodData is collected, the backward branch limit is compared against a 1.153 + // methodData counter, rather than an InvocationCounter. In the former case, we 1.154 + // don't need the shift by number_of_noncount_bits, but we do need to adjust 1.155 + // the factor by which we scale the threshold. 1.156 + if (ProfileInterpreter) { 1.157 + InterpreterBackwardBranchLimit = (CompileThreshold * (OnStackReplacePercentage - InterpreterProfilePercentage)) / 100; 1.158 + } else { 1.159 + InterpreterBackwardBranchLimit = ((CompileThreshold * OnStackReplacePercentage) / 100) << number_of_noncount_bits; 1.160 + } 1.161 + 1.162 + assert(0 <= InterpreterBackwardBranchLimit, 1.163 + "OSR threshold should be non-negative"); 1.164 + assert(0 <= InterpreterProfileLimit && 1.165 + InterpreterProfileLimit <= InterpreterInvocationLimit, 1.166 + "profile threshold should be less than the compilation threshold " 1.167 + "and non-negative"); 1.168 +} 1.169 + 1.170 +void invocationCounter_init() { 1.171 + InvocationCounter::reinitialize(DelayCompilationDuringStartup); 1.172 +}