1 /* 2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "interpreter/invocationCounter.hpp" 27 #include "runtime/frame.hpp" 28 #include "runtime/handles.inline.hpp" 29 30 31 // Implementation of InvocationCounter 32 33 void InvocationCounter::init() { 34 _counter = 0; // reset all the bits, including the sticky carry 35 reset(); 36 } 37 38 void InvocationCounter::reset() { 39 // Only reset the state and don't make the method look like it's never 40 // been executed 41 set_state(wait_for_compile); 42 } 43 44 void InvocationCounter::set_carry() { 45 set_carry_flag(); 46 // The carry bit now indicates that this counter had achieved a very 47 // large value. Now reduce the value, so that the method can be 48 // executed many more times before re-entering the VM. 49 int old_count = count(); 50 int new_count = MIN2(old_count, (int) (CompileThreshold / 2)); 51 // prevent from going to zero, to distinguish from never-executed methods 52 if (new_count == 0) new_count = 1; 53 if (old_count != new_count) set(state(), new_count); 54 } 55 56 void InvocationCounter::set_state(State state) { 57 assert(0 <= state && state < number_of_states, "illegal state"); 58 int init = _init[state]; 59 // prevent from going to zero, to distinguish from never-executed methods 60 if (init == 0 && count() > 0) init = 1; 61 int carry = (_counter & carry_mask); // the carry bit is sticky 62 _counter = (init << number_of_noncount_bits) | carry | state; 63 } 64 65 66 void InvocationCounter::print() { 67 tty->print_cr("invocation count: up = %d, limit = %d, carry = %s, state = %s", 68 count(), limit(), 69 carry() ? "true" : "false", 70 state_as_string(state())); 71 } 72 73 void InvocationCounter::print_short() { 74 tty->print(" [%d%s;%s]", count(), carry()?"+carry":"", state_as_short_string(state())); 75 } 76 77 // Initialization 78 79 int InvocationCounter::_init [InvocationCounter::number_of_states]; 80 InvocationCounter::Action InvocationCounter::_action[InvocationCounter::number_of_states]; 81 82 83 const char* InvocationCounter::state_as_string(State state) { 84 switch (state) { 85 case wait_for_nothing : return "wait_for_nothing"; 86 case wait_for_compile : return "wait_for_compile"; 87 default: 88 ShouldNotReachHere(); 89 return NULL; 90 } 91 } 92 93 const char* InvocationCounter::state_as_short_string(State state) { 94 switch (state) { 95 case wait_for_nothing : return "not comp."; 96 case wait_for_compile : return "compileable"; 97 default: 98 ShouldNotReachHere(); 99 return NULL; 100 } 101 } 102 103 104 static address do_nothing(const methodHandle& method, TRAPS) { 105 // dummy action for inactive invocation counters 106 MethodCounters* mcs = method->method_counters(); 107 assert(mcs != NULL, ""); 108 mcs->invocation_counter()->set_carry(); 109 mcs->invocation_counter()->set_state(InvocationCounter::wait_for_nothing); 110 return NULL; 111 } 112 113 114 static address do_decay(const methodHandle& method, TRAPS) { 115 // decay invocation counters so compilation gets delayed 116 MethodCounters* mcs = method->method_counters(); 117 assert(mcs != NULL, ""); 118 mcs->invocation_counter()->decay(); 119 return NULL; 120 } 121 122 123 void InvocationCounter::def(State state, int init, Action action) { 124 assert(0 <= state && state < number_of_states, "illegal state"); 125 assert(0 <= init && init < count_limit, "initial value out of range"); 126 _init [state] = init; 127 _action[state] = action; 128 } 129 130 void InvocationCounter::reinitialize() { 131 // define states 132 guarantee((int)number_of_states <= (int)state_limit, "adjust number_of_state_bits"); 133 def(wait_for_nothing, 0, do_nothing); 134 def(wait_for_compile, 0, do_decay); 135 } 136 137 void invocationCounter_init() { 138 InvocationCounter::reinitialize(); 139 }