0N/A/*
1472N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "interpreter/invocationCounter.hpp"
1879N/A#include "runtime/frame.hpp"
1879N/A#include "runtime/handles.inline.hpp"
1879N/A
1879N/A
1879N/A// Implementation of InvocationCounter
1879N/A
1879N/Avoid InvocationCounter::init() {
1879N/A _counter = 0; // reset all the bits, including the sticky carry
1879N/A reset();
1879N/A}
1879N/A
1879N/Avoid InvocationCounter::reset() {
1879N/A // Only reset the state and don't make the method look like it's never
1879N/A // been executed
1879N/A set_state(wait_for_compile);
1879N/A}
1879N/A
1879N/Avoid InvocationCounter::set_carry() {
1879N/A set_carry_flag();
1879N/A // The carry bit now indicates that this counter had achieved a very
1879N/A // large value. Now reduce the value, so that the method can be
1879N/A // executed many more times before re-entering the VM.
1879N/A int old_count = count();
1879N/A int new_count = MIN2(old_count, (int) (CompileThreshold / 2));
1879N/A // prevent from going to zero, to distinguish from never-executed methods
1879N/A if (new_count == 0) new_count = 1;
1879N/A if (old_count != new_count) set(state(), new_count);
1879N/A}
1879N/A
0N/Avoid InvocationCounter::set_state(State state) {
0N/A assert(0 <= state && state < number_of_states, "illegal state");
0N/A int init = _init[state];
0N/A // prevent from going to zero, to distinguish from never-executed methods
0N/A if (init == 0 && count() > 0) init = 1;
0N/A int carry = (_counter & carry_mask); // the carry bit is sticky
0N/A _counter = (init << number_of_noncount_bits) | carry | state;
0N/A}
1798N/A
0N/A
0N/Avoid InvocationCounter::print() {
0N/A tty->print_cr("invocation count: up = %d, limit = %d, carry = %s, state = %s",
0N/A count(), limit(),
0N/A carry() ? "true" : "false",
0N/A state_as_string(state()));
0N/A}
0N/A
0N/Avoid InvocationCounter::print_short() {
0N/A tty->print(" [%d%s;%s]", count(), carry()?"+carry":"", state_as_short_string(state()));
0N/A}
0N/A
0N/A// Initialization
0N/A
0N/Aint InvocationCounter::_init [InvocationCounter::number_of_states];
0N/AInvocationCounter::Action InvocationCounter::_action[InvocationCounter::number_of_states];
1881N/Aint InvocationCounter::InterpreterInvocationLimit;
1881N/Aint InvocationCounter::InterpreterBackwardBranchLimit;
0N/Aint InvocationCounter::InterpreterProfileLimit;
0N/A
1119N/A
1119N/Aconst char* InvocationCounter::state_as_string(State state) {
1119N/A switch (state) {
1119N/A case wait_for_nothing : return "wait_for_nothing";
1119N/A case wait_for_compile : return "wait_for_compile";
1119N/A }
0N/A ShouldNotReachHere();
0N/A return NULL;
0N/A}
0N/A
0N/Aconst char* InvocationCounter::state_as_short_string(State state) {
0N/A switch (state) {
0N/A case wait_for_nothing : return "not comp.";
0N/A case wait_for_compile : return "compileable";
0N/A }
0N/A ShouldNotReachHere();
0N/A return NULL;
0N/A}
0N/A
0N/A
0N/Astatic address do_nothing(methodHandle method, TRAPS) {
0N/A // dummy action for inactive invocation counters
0N/A method->invocation_counter()->set_carry();
0N/A method->invocation_counter()->set_state(InvocationCounter::wait_for_nothing);
0N/A return NULL;
0N/A}
0N/A
0N/A
0N/Astatic address do_decay(methodHandle method, TRAPS) {
0N/A // decay invocation counters so compilation gets delayed
0N/A method->invocation_counter()->decay();
0N/A return NULL;
0N/A}
0N/A
0N/A
0N/Avoid InvocationCounter::def(State state, int init, Action action) {
0N/A assert(0 <= state && state < number_of_states, "illegal state");
0N/A assert(0 <= init && init < count_limit, "initial value out of range");
0N/A _init [state] = init;
0N/A _action[state] = action;
0N/A}
0N/A
0N/Aaddress dummy_invocation_counter_overflow(methodHandle m, TRAPS) {
0N/A ShouldNotReachHere();
0N/A return NULL;
0N/A}
0N/A
0N/Avoid InvocationCounter::reinitialize(bool delay_overflow) {
0N/A // define states
0N/A guarantee((int)number_of_states <= (int)state_limit, "adjust number_of_state_bits");
0N/A def(wait_for_nothing, 0, do_nothing);
0N/A if (delay_overflow) {
0N/A def(wait_for_compile, 0, do_decay);
0N/A } else {
0N/A def(wait_for_compile, 0, dummy_invocation_counter_overflow);
0N/A }
0N/A
0N/A InterpreterInvocationLimit = CompileThreshold << number_of_noncount_bits;
0N/A InterpreterProfileLimit = ((CompileThreshold * InterpreterProfilePercentage) / 100)<< number_of_noncount_bits;
0N/A
0N/A // When methodData is collected, the backward branch limit is compared against a
0N/A // methodData counter, rather than an InvocationCounter. In the former case, we
0N/A // don't need the shift by number_of_noncount_bits, but we do need to adjust
0N/A // the factor by which we scale the threshold.
0N/A if (ProfileInterpreter) {
0N/A InterpreterBackwardBranchLimit = (CompileThreshold * (OnStackReplacePercentage - InterpreterProfilePercentage)) / 100;
0N/A } else {
0N/A InterpreterBackwardBranchLimit = ((CompileThreshold * OnStackReplacePercentage) / 100) << number_of_noncount_bits;
0N/A }
0N/A
0N/A assert(0 <= InterpreterBackwardBranchLimit,
0N/A "OSR threshold should be non-negative");
0N/A assert(0 <= InterpreterProfileLimit &&
0N/A InterpreterProfileLimit <= InterpreterInvocationLimit,
0N/A "profile threshold should be less than the compilation threshold "
0N/A "and non-negative");
0N/A}
0N/A
0N/Avoid invocationCounter_init() {
0N/A InvocationCounter::reinitialize(DelayCompilationDuringStartup);
0N/A}
0N/A