simpleThresholdPolicy.cpp revision 2662
1703N/A/*
2455N/A * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
1703N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1703N/A *
1703N/A * This code is free software; you can redistribute it and/or modify it
1703N/A * under the terms of the GNU General Public License version 2 only, as
1703N/A * published by the Free Software Foundation.
1703N/A *
1703N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1703N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1703N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1703N/A * version 2 for more details (a copy is included in the LICENSE file that
1703N/A * accompanied this code).
1703N/A *
1703N/A * You should have received a copy of the GNU General Public License version
1703N/A * 2 along with this work; if not, write to the Free Software Foundation,
1703N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1703N/A *
1703N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1703N/A * or visit www.oracle.com if you need additional information or have any
1703N/A * questions.
1703N/A *
1703N/A */
1703N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "compiler/compileBroker.hpp"
1879N/A#include "memory/resourceArea.hpp"
1879N/A#include "runtime/arguments.hpp"
1879N/A#include "runtime/simpleThresholdPolicy.hpp"
1879N/A#include "runtime/simpleThresholdPolicy.inline.hpp"
2124N/A#include "code/scopeDesc.hpp"
1703N/A
1703N/A// Print an event.
1703N/Avoid SimpleThresholdPolicy::print_event(EventType type, methodHandle mh, methodHandle imh,
1703N/A int bci, CompLevel level) {
1703N/A bool inlinee_event = mh() != imh();
1703N/A
1703N/A ttyLocker tty_lock;
1703N/A tty->print("%lf: [", os::elapsedTime());
1703N/A
1703N/A int invocation_count = mh->invocation_count();
1703N/A int backedge_count = mh->backedge_count();
1703N/A switch(type) {
1703N/A case CALL:
1703N/A tty->print("call");
1703N/A break;
1703N/A case LOOP:
1703N/A tty->print("loop");
1703N/A break;
1703N/A case COMPILE:
1703N/A tty->print("compile");
2124N/A break;
2616N/A case REMOVE_FROM_QUEUE:
2616N/A tty->print("remove-from-queue");
2124N/A break;
2616N/A case UPDATE_IN_QUEUE:
2616N/A tty->print("update-in-queue");
2124N/A break;
2124N/A case REPROFILE:
2124N/A tty->print("reprofile");
2124N/A break;
2616N/A case MAKE_NOT_ENTRANT:
2616N/A tty->print("make-not-entrant");
2616N/A break;
2124N/A default:
2124N/A tty->print("unknown");
1703N/A }
1703N/A
1703N/A tty->print(" level: %d ", level);
1703N/A
1703N/A ResourceMark rm;
1703N/A char *method_name = mh->name_and_sig_as_C_string();
1703N/A tty->print("[%s", method_name);
1703N/A if (inlinee_event) {
1703N/A char *inlinee_name = imh->name_and_sig_as_C_string();
1703N/A tty->print(" [%s]] ", inlinee_name);
1703N/A }
1703N/A else tty->print("] ");
1703N/A tty->print("@%d queues: %d,%d", bci, CompileBroker::queue_size(CompLevel_full_profile),
1703N/A CompileBroker::queue_size(CompLevel_full_optimization));
1703N/A
1703N/A print_specific(type, mh, imh, bci, level);
1703N/A
1703N/A if (type != COMPILE) {
1703N/A methodDataHandle mdh = mh->method_data();
1703N/A int mdo_invocations = 0, mdo_backedges = 0;
2124N/A int mdo_invocations_start = 0, mdo_backedges_start = 0;
1703N/A if (mdh() != NULL) {
1703N/A mdo_invocations = mdh->invocation_count();
1703N/A mdo_backedges = mdh->backedge_count();
2124N/A mdo_invocations_start = mdh->invocation_count_start();
2124N/A mdo_backedges_start = mdh->backedge_count_start();
1703N/A }
2124N/A tty->print(" total: %d,%d mdo: %d(%d),%d(%d)",
1703N/A invocation_count, backedge_count,
2124N/A mdo_invocations, mdo_invocations_start,
2124N/A mdo_backedges, mdo_backedges_start);
1703N/A tty->print(" max levels: %d,%d",
1703N/A mh->highest_comp_level(), mh->highest_osr_comp_level());
1703N/A if (inlinee_event) {
1703N/A tty->print(" inlinee max levels: %d,%d", imh->highest_comp_level(), imh->highest_osr_comp_level());
1703N/A }
1703N/A tty->print(" compilable: ");
1703N/A bool need_comma = false;
1703N/A if (!mh->is_not_compilable(CompLevel_full_profile)) {
1703N/A tty->print("c1");
1703N/A need_comma = true;
1703N/A }
1703N/A if (!mh->is_not_compilable(CompLevel_full_optimization)) {
1703N/A if (need_comma) tty->print(", ");
1703N/A tty->print("c2");
1703N/A need_comma = true;
1703N/A }
1703N/A if (!mh->is_not_osr_compilable()) {
1703N/A if (need_comma) tty->print(", ");
1703N/A tty->print("osr");
1703N/A }
1703N/A tty->print(" status:");
1703N/A if (mh->queued_for_compilation()) {
1703N/A tty->print(" in queue");
1703N/A } else tty->print(" idle");
1703N/A }
1703N/A tty->print_cr("]");
1703N/A}
1703N/A
1703N/Avoid SimpleThresholdPolicy::initialize() {
1703N/A if (FLAG_IS_DEFAULT(CICompilerCount)) {
1703N/A FLAG_SET_DEFAULT(CICompilerCount, 3);
1703N/A }
1703N/A int count = CICompilerCount;
1703N/A if (CICompilerCountPerCPU) {
1703N/A count = MAX2(log2_intptr(os::active_processor_count()), 1) * 3 / 2;
1703N/A }
1703N/A set_c1_count(MAX2(count / 3, 1));
1703N/A set_c2_count(MAX2(count - count / 3, 1));
1703N/A}
1703N/A
1703N/Avoid SimpleThresholdPolicy::set_carry_if_necessary(InvocationCounter *counter) {
1703N/A if (!counter->carry() && counter->count() > InvocationCounter::count_limit / 2) {
1703N/A counter->set_carry_flag();
1703N/A }
1703N/A}
1703N/A
1703N/A// Set carry flags on the counters if necessary
1703N/Avoid SimpleThresholdPolicy::handle_counter_overflow(methodOop method) {
1703N/A set_carry_if_necessary(method->invocation_counter());
1703N/A set_carry_if_necessary(method->backedge_counter());
1703N/A methodDataOop mdo = method->method_data();
1703N/A if (mdo != NULL) {
1703N/A set_carry_if_necessary(mdo->invocation_counter());
1703N/A set_carry_if_necessary(mdo->backedge_counter());
1703N/A }
1703N/A}
1703N/A
1703N/A// Called with the queue locked and with at least one element
1703N/ACompileTask* SimpleThresholdPolicy::select_task(CompileQueue* compile_queue) {
1703N/A return compile_queue->first();
1703N/A}
1703N/A
2124N/Avoid SimpleThresholdPolicy::reprofile(ScopeDesc* trap_scope, bool is_osr) {
2124N/A for (ScopeDesc* sd = trap_scope;; sd = sd->sender()) {
2124N/A if (PrintTieredEvents) {
2124N/A methodHandle mh(sd->method());
2124N/A print_event(REPROFILE, mh, mh, InvocationEntryBci, CompLevel_none);
2124N/A }
2124N/A methodDataOop mdo = sd->method()->method_data();
2124N/A if (mdo != NULL) {
2124N/A mdo->reset_start_counters();
2124N/A }
2124N/A if (sd->is_top()) break;
2124N/A }
2124N/A}
2124N/A
1703N/Anmethod* SimpleThresholdPolicy::event(methodHandle method, methodHandle inlinee,
2616N/A int branch_bci, int bci, CompLevel comp_level, nmethod* nm, TRAPS) {
1703N/A if (comp_level == CompLevel_none &&
1703N/A JvmtiExport::can_post_interpreter_events()) {
1703N/A assert(THREAD->is_Java_thread(), "Should be java thread");
1703N/A if (((JavaThread*)THREAD)->is_interp_only_mode()) {
1703N/A return NULL;
1703N/A }
1703N/A }
1703N/A nmethod *osr_nm = NULL;
1703N/A
1703N/A handle_counter_overflow(method());
1703N/A if (method() != inlinee()) {
1703N/A handle_counter_overflow(inlinee());
1703N/A }
1703N/A
1703N/A if (PrintTieredEvents) {
1703N/A print_event(bci == InvocationEntryBci ? CALL : LOOP, method, inlinee, bci, comp_level);
1703N/A }
1703N/A
1703N/A if (bci == InvocationEntryBci) {
2616N/A method_invocation_event(method, inlinee, comp_level, nm, THREAD);
1703N/A } else {
2616N/A method_back_branch_event(method, inlinee, bci, comp_level, nm, THREAD);
2616N/A // method == inlinee if the event originated in the main method
2616N/A int highest_level = inlinee->highest_osr_comp_level();
1703N/A if (highest_level > comp_level) {
2616N/A osr_nm = inlinee->lookup_osr_nmethod_for(bci, highest_level, false);
1703N/A }
1703N/A }
1703N/A return osr_nm;
1703N/A}
1703N/A
1703N/A// Check if the method can be compiled, change level if necessary
1703N/Avoid SimpleThresholdPolicy::compile(methodHandle mh, int bci, CompLevel level, TRAPS) {
2662N/A assert(level <= TieredStopAtLevel, "Invalid compilation level");
1703N/A if (level == CompLevel_none) {
1703N/A return;
1703N/A }
1871N/A // Check if the method can be compiled. If it cannot be compiled with C1, continue profiling
1871N/A // in the interpreter and then compile with C2 (the transition function will request that,
1871N/A // see common() ). If the method cannot be compiled with C2 but still can with C1, compile it with
1871N/A // pure C1.
1703N/A if (!can_be_compiled(mh, level)) {
1703N/A if (level == CompLevel_full_optimization && can_be_compiled(mh, CompLevel_simple)) {
1703N/A compile(mh, bci, CompLevel_simple, THREAD);
1703N/A }
1703N/A return;
1703N/A }
1703N/A if (bci != InvocationEntryBci && mh->is_not_osr_compilable()) {
1703N/A return;
1703N/A }
1703N/A if (!CompileBroker::compilation_is_in_queue(mh, bci)) {
2662N/A if (PrintTieredEvents) {
2662N/A print_event(COMPILE, mh, mh, bci, level);
2662N/A }
1703N/A submit_compile(mh, bci, level, THREAD);
1703N/A }
1703N/A}
1703N/A
1703N/A// Tell the broker to compile the method
1703N/Avoid SimpleThresholdPolicy::submit_compile(methodHandle mh, int bci, CompLevel level, TRAPS) {
1703N/A int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count();
1703N/A CompileBroker::compile_method(mh, bci, level, mh, hot_count, "tiered", THREAD);
1703N/A}
1703N/A
1703N/A// Call and loop predicates determine whether a transition to a higher
1703N/A// compilation level should be performed (pointers to predicate functions
1703N/A// are passed to common() transition function).
1703N/Abool SimpleThresholdPolicy::loop_predicate(int i, int b, CompLevel cur_level) {
1703N/A switch(cur_level) {
1703N/A case CompLevel_none:
1703N/A case CompLevel_limited_profile: {
1703N/A return loop_predicate_helper<CompLevel_none>(i, b, 1.0);
1703N/A }
1703N/A case CompLevel_full_profile: {
1703N/A return loop_predicate_helper<CompLevel_full_profile>(i, b, 1.0);
1703N/A }
1703N/A default:
1703N/A return true;
1703N/A }
1703N/A}
1703N/A
1703N/Abool SimpleThresholdPolicy::call_predicate(int i, int b, CompLevel cur_level) {
1703N/A switch(cur_level) {
1703N/A case CompLevel_none:
1703N/A case CompLevel_limited_profile: {
1703N/A return call_predicate_helper<CompLevel_none>(i, b, 1.0);
1703N/A }
1703N/A case CompLevel_full_profile: {
1703N/A return call_predicate_helper<CompLevel_full_profile>(i, b, 1.0);
1703N/A }
1703N/A default:
1703N/A return true;
1703N/A }
1703N/A}
1703N/A
1703N/A// Determine is a method is mature.
1703N/Abool SimpleThresholdPolicy::is_mature(methodOop method) {
1703N/A if (is_trivial(method)) return true;
1703N/A methodDataOop mdo = method->method_data();
1703N/A if (mdo != NULL) {
1703N/A int i = mdo->invocation_count();
1703N/A int b = mdo->backedge_count();
1703N/A double k = ProfileMaturityPercentage / 100.0;
1703N/A return call_predicate_helper<CompLevel_full_profile>(i, b, k) ||
1703N/A loop_predicate_helper<CompLevel_full_profile>(i, b, k);
1703N/A }
1703N/A return false;
1703N/A}
1703N/A
1703N/A// Common transition function. Given a predicate determines if a method should transition to another level.
1703N/ACompLevel SimpleThresholdPolicy::common(Predicate p, methodOop method, CompLevel cur_level) {
1703N/A CompLevel next_level = cur_level;
1703N/A int i = method->invocation_count();
1703N/A int b = method->backedge_count();
1703N/A
2662N/A if (is_trivial(method)) {
2662N/A next_level = CompLevel_simple;
2662N/A } else {
2662N/A switch(cur_level) {
2662N/A case CompLevel_none:
2662N/A // If we were at full profile level, would we switch to full opt?
2662N/A if (common(p, method, CompLevel_full_profile) == CompLevel_full_optimization) {
2662N/A next_level = CompLevel_full_optimization;
2662N/A } else if ((this->*p)(i, b, cur_level)) {
2662N/A next_level = CompLevel_full_profile;
2662N/A }
2662N/A break;
2662N/A case CompLevel_limited_profile:
2662N/A case CompLevel_full_profile:
2662N/A {
2662N/A methodDataOop mdo = method->method_data();
2662N/A if (mdo != NULL) {
2662N/A if (mdo->would_profile()) {
2662N/A int mdo_i = mdo->invocation_count_delta();
2662N/A int mdo_b = mdo->backedge_count_delta();
2662N/A if ((this->*p)(mdo_i, mdo_b, cur_level)) {
2662N/A next_level = CompLevel_full_optimization;
2662N/A }
2662N/A } else {
2124N/A next_level = CompLevel_full_optimization;
2124N/A }
1703N/A }
1703N/A }
2662N/A break;
1703N/A }
1703N/A }
2662N/A return MIN2(next_level, (CompLevel)TieredStopAtLevel);
1703N/A}
1703N/A
1703N/A// Determine if a method should be compiled with a normal entry point at a different level.
1703N/ACompLevel SimpleThresholdPolicy::call_event(methodOop method, CompLevel cur_level) {
2615N/A CompLevel osr_level = MIN2((CompLevel) method->highest_osr_comp_level(),
2615N/A common(&SimpleThresholdPolicy::loop_predicate, method, cur_level));
1703N/A CompLevel next_level = common(&SimpleThresholdPolicy::call_predicate, method, cur_level);
1703N/A
1703N/A // If OSR method level is greater than the regular method level, the levels should be
1703N/A // equalized by raising the regular method level in order to avoid OSRs during each
1703N/A // invocation of the method.
1703N/A if (osr_level == CompLevel_full_optimization && cur_level == CompLevel_full_profile) {
1703N/A methodDataOop mdo = method->method_data();
1703N/A guarantee(mdo != NULL, "MDO should not be NULL");
1703N/A if (mdo->invocation_count() >= 1) {
1703N/A next_level = CompLevel_full_optimization;
1703N/A }
1703N/A } else {
1703N/A next_level = MAX2(osr_level, next_level);
1703N/A }
1703N/A
1703N/A return next_level;
1703N/A}
1703N/A
1703N/A// Determine if we should do an OSR compilation of a given method.
1703N/ACompLevel SimpleThresholdPolicy::loop_event(methodOop method, CompLevel cur_level) {
2615N/A CompLevel next_level = common(&SimpleThresholdPolicy::loop_predicate, method, cur_level);
1703N/A if (cur_level == CompLevel_none) {
1703N/A // If there is a live OSR method that means that we deopted to the interpreter
1703N/A // for the transition.
2615N/A CompLevel osr_level = MIN2((CompLevel)method->highest_osr_comp_level(), next_level);
1703N/A if (osr_level > CompLevel_none) {
1703N/A return osr_level;
1703N/A }
1703N/A }
2615N/A return next_level;
1703N/A}
1703N/A
1703N/A
1703N/A// Handle the invocation event.
1703N/Avoid SimpleThresholdPolicy::method_invocation_event(methodHandle mh, methodHandle imh,
2616N/A CompLevel level, nmethod* nm, TRAPS) {
1703N/A if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh, InvocationEntryBci)) {
1703N/A CompLevel next_level = call_event(mh(), level);
1703N/A if (next_level != level) {
1703N/A compile(mh, InvocationEntryBci, next_level, THREAD);
1703N/A }
1703N/A }
1703N/A}
1703N/A
1703N/A// Handle the back branch event. Notice that we can compile the method
1703N/A// with a regular entry from here.
1703N/Avoid SimpleThresholdPolicy::method_back_branch_event(methodHandle mh, methodHandle imh,
2616N/A int bci, CompLevel level, nmethod* nm, TRAPS) {
1703N/A // If the method is already compiling, quickly bail out.
1703N/A if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh, bci)) {
1703N/A // Use loop event as an opportinity to also check there's been
1703N/A // enough calls.
1703N/A CompLevel cur_level = comp_level(mh());
1703N/A CompLevel next_level = call_event(mh(), cur_level);
1703N/A CompLevel next_osr_level = loop_event(mh(), level);
1703N/A
1703N/A next_level = MAX2(next_level,
1703N/A next_osr_level < CompLevel_full_optimization ? next_osr_level : cur_level);
1703N/A bool is_compiling = false;
1703N/A if (next_level != cur_level) {
1703N/A compile(mh, InvocationEntryBci, next_level, THREAD);
1703N/A is_compiling = true;
1703N/A }
1703N/A
1703N/A // Do the OSR version
1703N/A if (!is_compiling && next_osr_level != level) {
1703N/A compile(mh, bci, next_osr_level, THREAD);
1703N/A }
1703N/A }
1703N/A}