Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/runtime/advancedThresholdPolicy.cpp
32285 views
/*1* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "runtime/advancedThresholdPolicy.hpp"26#include "runtime/simpleThresholdPolicy.inline.hpp"2728#ifdef TIERED29// Print an event.30void AdvancedThresholdPolicy::print_specific(EventType type, methodHandle mh, methodHandle imh,31int bci, CompLevel level) {32tty->print(" rate=");33if (mh->prev_time() == 0) tty->print("n/a");34else tty->print("%f", mh->rate());3536tty->print(" k=%.2lf,%.2lf", threshold_scale(CompLevel_full_profile, Tier3LoadFeedback),37threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback));3839}4041void AdvancedThresholdPolicy::initialize() {42// Turn on ergonomic compiler count selection43if (FLAG_IS_DEFAULT(CICompilerCountPerCPU) && FLAG_IS_DEFAULT(CICompilerCount)) {44FLAG_SET_DEFAULT(CICompilerCountPerCPU, true);45}46int count = CICompilerCount;47if (CICompilerCountPerCPU) {48// Simple log n seems to grow too slowly for tiered, try something faster: log n * log log n49int log_cpu = log2_int(os::active_processor_count());50int loglog_cpu = log2_int(MAX2(log_cpu, 1));51count = MAX2(log_cpu * loglog_cpu, 1) * 3 / 2;52}5354set_c1_count(MAX2(count / 3, 1));55set_c2_count(MAX2(count - c1_count(), 1));56FLAG_SET_ERGO(intx, CICompilerCount, c1_count() + c2_count());5758// Some inlining tuning59#if defined(X86) || defined(AARCH64)60if (FLAG_IS_DEFAULT(InlineSmallCode)) {61FLAG_SET_DEFAULT(InlineSmallCode, 2000);62}63#endif6465#ifdef SPARC66if (FLAG_IS_DEFAULT(InlineSmallCode)) {67FLAG_SET_DEFAULT(InlineSmallCode, 2500);68}69#endif7071set_increase_threshold_at_ratio();72set_start_time(os::javaTimeMillis());73}7475// update_rate() is called from select_task() while holding a compile queue lock.76void AdvancedThresholdPolicy::update_rate(jlong t, Method* m) {77// Skip update if counters are absent.78// Can't allocate them since we are holding compile queue lock.79if (m->method_counters() == NULL) return;8081if (is_old(m)) {82// We don't remove old methods from the queue,83// so we can just zero the rate.84m->set_rate(0);85return;86}8788// We don't update the rate if we've just came out of a safepoint.89// delta_s is the time since last safepoint in milliseconds.90jlong delta_s = t - SafepointSynchronize::end_of_last_safepoint();91jlong delta_t = t - (m->prev_time() != 0 ? m->prev_time() : start_time()); // milliseconds since the last measurement92// How many events were there since the last time?93int event_count = m->invocation_count() + m->backedge_count();94int delta_e = event_count - m->prev_event_count();9596// We should be running for at least 1ms.97if (delta_s >= TieredRateUpdateMinTime) {98// And we must've taken the previous point at least 1ms before.99if (delta_t >= TieredRateUpdateMinTime && delta_e > 0) {100m->set_prev_time(t);101m->set_prev_event_count(event_count);102m->set_rate((float)delta_e / (float)delta_t); // Rate is events per millisecond103} else {104if (delta_t > TieredRateUpdateMaxTime && delta_e == 0) {105// If nothing happened for 25ms, zero the rate. Don't modify prev values.106m->set_rate(0);107}108}109}110}111112// Check if this method has been stale from a given number of milliseconds.113// See select_task().114bool AdvancedThresholdPolicy::is_stale(jlong t, jlong timeout, Method* m) {115jlong delta_s = t - SafepointSynchronize::end_of_last_safepoint();116jlong delta_t = t - m->prev_time();117if (delta_t > timeout && delta_s > timeout) {118int event_count = m->invocation_count() + m->backedge_count();119int delta_e = event_count - m->prev_event_count();120// Return true if there were no events.121return delta_e == 0;122}123return false;124}125126// We don't remove old methods from the compile queue even if they have127// very low activity. See select_task().128bool AdvancedThresholdPolicy::is_old(Method* method) {129return method->invocation_count() > 50000 || method->backedge_count() > 500000;130}131132double AdvancedThresholdPolicy::weight(Method* method) {133return (double)(method->rate() + 1) *134(method->invocation_count() + 1) * (method->backedge_count() + 1);135}136137// Apply heuristics and return true if x should be compiled before y138bool AdvancedThresholdPolicy::compare_methods(Method* x, Method* y) {139if (x->highest_comp_level() > y->highest_comp_level()) {140// recompilation after deopt141return true;142} else143if (x->highest_comp_level() == y->highest_comp_level()) {144if (weight(x) > weight(y)) {145return true;146}147}148return false;149}150151// Is method profiled enough?152bool AdvancedThresholdPolicy::is_method_profiled(Method* method) {153MethodData* mdo = method->method_data();154if (mdo != NULL) {155int i = mdo->invocation_count_delta();156int b = mdo->backedge_count_delta();157return call_predicate_helper<CompLevel_full_profile>(i, b, 1);158}159return false;160}161162// Called with the queue locked and with at least one element163CompileTask* AdvancedThresholdPolicy::select_task(CompileQueue* compile_queue) {164CompileTask *max_task = NULL;165Method* max_method = NULL;166jlong t = os::javaTimeMillis();167// Iterate through the queue and find a method with a maximum rate.168for (CompileTask* task = compile_queue->first(); task != NULL;) {169CompileTask* next_task = task->next();170Method* method = task->method();171update_rate(t, method);172if (max_task == NULL) {173max_task = task;174max_method = method;175} else {176// If a method has been stale for some time, remove it from the queue.177if (is_stale(t, TieredCompileTaskTimeout, method) && !is_old(method)) {178if (PrintTieredEvents) {179print_event(REMOVE_FROM_QUEUE, method, method, task->osr_bci(), (CompLevel)task->comp_level());180}181compile_queue->remove_and_mark_stale(task);182method->clear_queued_for_compilation();183task = next_task;184continue;185}186187// Select a method with a higher rate188if (compare_methods(method, max_method)) {189max_task = task;190max_method = method;191}192}193task = next_task;194}195196if (max_task->comp_level() == CompLevel_full_profile && TieredStopAtLevel > CompLevel_full_profile197&& is_method_profiled(max_method)) {198199if (CompileBroker::compilation_is_complete(max_method, max_task->osr_bci(), CompLevel_limited_profile)) {200if (PrintTieredEvents) {201print_event(REMOVE_FROM_QUEUE, max_method, max_method, max_task->osr_bci(), (CompLevel)max_task->comp_level());202}203compile_queue->remove_and_mark_stale(max_task);204max_method->clear_queued_for_compilation();205return NULL;206}207208max_task->set_comp_level(CompLevel_limited_profile);209if (PrintTieredEvents) {210print_event(UPDATE_IN_QUEUE, max_method, max_method, max_task->osr_bci(), (CompLevel)max_task->comp_level());211}212}213214return max_task;215}216217double AdvancedThresholdPolicy::threshold_scale(CompLevel level, int feedback_k) {218double queue_size = CompileBroker::queue_size(level);219int comp_count = compiler_count(level);220double k = queue_size / (feedback_k * comp_count) + 1;221222// Increase C1 compile threshold when the code cache is filled more223// than specified by IncreaseFirstTierCompileThresholdAt percentage.224// The main intention is to keep enough free space for C2 compiled code225// to achieve peak performance if the code cache is under stress.226if ((TieredStopAtLevel == CompLevel_full_optimization) && (level != CompLevel_full_optimization)) {227double current_reverse_free_ratio = CodeCache::reverse_free_ratio();228if (current_reverse_free_ratio > _increase_threshold_at_ratio) {229k *= exp(current_reverse_free_ratio - _increase_threshold_at_ratio);230}231}232return k;233}234235// Call and loop predicates determine whether a transition to a higher236// compilation level should be performed (pointers to predicate functions237// are passed to common()).238// Tier?LoadFeedback is basically a coefficient that determines of239// how many methods per compiler thread can be in the queue before240// the threshold values double.241bool AdvancedThresholdPolicy::loop_predicate(int i, int b, CompLevel cur_level) {242switch(cur_level) {243case CompLevel_none:244case CompLevel_limited_profile: {245double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);246return loop_predicate_helper<CompLevel_none>(i, b, k);247}248case CompLevel_full_profile: {249double k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);250return loop_predicate_helper<CompLevel_full_profile>(i, b, k);251}252default:253return true;254}255}256257bool AdvancedThresholdPolicy::call_predicate(int i, int b, CompLevel cur_level) {258switch(cur_level) {259case CompLevel_none:260case CompLevel_limited_profile: {261double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);262return call_predicate_helper<CompLevel_none>(i, b, k);263}264case CompLevel_full_profile: {265double k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);266return call_predicate_helper<CompLevel_full_profile>(i, b, k);267}268default:269return true;270}271}272273// If a method is old enough and is still in the interpreter we would want to274// start profiling without waiting for the compiled method to arrive.275// We also take the load on compilers into the account.276bool AdvancedThresholdPolicy::should_create_mdo(Method* method, CompLevel cur_level) {277if (cur_level == CompLevel_none &&278CompileBroker::queue_size(CompLevel_full_optimization) <=279Tier3DelayOn * compiler_count(CompLevel_full_optimization)) {280int i = method->invocation_count();281int b = method->backedge_count();282double k = Tier0ProfilingStartPercentage / 100.0;283return call_predicate_helper<CompLevel_none>(i, b, k) || loop_predicate_helper<CompLevel_none>(i, b, k);284}285return false;286}287288// Inlining control: if we're compiling a profiled method with C1 and the callee289// is known to have OSRed in a C2 version, don't inline it.290bool AdvancedThresholdPolicy::should_not_inline(ciEnv* env, ciMethod* callee) {291CompLevel comp_level = (CompLevel)env->comp_level();292if (comp_level == CompLevel_full_profile ||293comp_level == CompLevel_limited_profile) {294return callee->highest_osr_comp_level() == CompLevel_full_optimization;295}296return false;297}298299// Create MDO if necessary.300void AdvancedThresholdPolicy::create_mdo(methodHandle mh, JavaThread* THREAD) {301if (mh->is_native() || mh->is_abstract() || mh->is_accessor()) return;302if (mh->method_data() == NULL) {303Method::build_interpreter_method_data(mh, CHECK_AND_CLEAR);304}305}306307308/*309* Method states:310* 0 - interpreter (CompLevel_none)311* 1 - pure C1 (CompLevel_simple)312* 2 - C1 with invocation and backedge counting (CompLevel_limited_profile)313* 3 - C1 with full profiling (CompLevel_full_profile)314* 4 - C2 (CompLevel_full_optimization)315*316* Common state transition patterns:317* a. 0 -> 3 -> 4.318* The most common path. But note that even in this straightforward case319* profiling can start at level 0 and finish at level 3.320*321* b. 0 -> 2 -> 3 -> 4.322* This case occures when the load on C2 is deemed too high. So, instead of transitioning323* into state 3 directly and over-profiling while a method is in the C2 queue we transition to324* level 2 and wait until the load on C2 decreases. This path is disabled for OSRs.325*326* c. 0 -> (3->2) -> 4.327* In this case we enqueue a method for compilation at level 3, but the C1 queue is long enough328* to enable the profiling to fully occur at level 0. In this case we change the compilation level329* of the method to 2 while the request is still in-queue, because it'll allow it to run much faster330* without full profiling while c2 is compiling.331*332* d. 0 -> 3 -> 1 or 0 -> 2 -> 1.333* After a method was once compiled with C1 it can be identified as trivial and be compiled to334* level 1. These transition can also occur if a method can't be compiled with C2 but can with C1.335*336* e. 0 -> 4.337* This can happen if a method fails C1 compilation (it will still be profiled in the interpreter)338* or because of a deopt that didn't require reprofiling (compilation won't happen in this case because339* the compiled version already exists).340*341* Note that since state 0 can be reached from any other state via deoptimization different loops342* are possible.343*344*/345346// Common transition function. Given a predicate determines if a method should transition to another level.347CompLevel AdvancedThresholdPolicy::common(Predicate p, Method* method, CompLevel cur_level, bool disable_feedback) {348CompLevel next_level = cur_level;349int i = method->invocation_count();350int b = method->backedge_count();351352if (is_trivial(method)) {353next_level = CompLevel_simple;354} else {355switch(cur_level) {356case CompLevel_none:357// If we were at full profile level, would we switch to full opt?358if (common(p, method, CompLevel_full_profile, disable_feedback) == CompLevel_full_optimization) {359next_level = CompLevel_full_optimization;360} else if ((this->*p)(i, b, cur_level)) {361// C1-generated fully profiled code is about 30% slower than the limited profile362// code that has only invocation and backedge counters. The observation is that363// if C2 queue is large enough we can spend too much time in the fully profiled code364// while waiting for C2 to pick the method from the queue. To alleviate this problem365// we introduce a feedback on the C2 queue size. If the C2 queue is sufficiently long366// we choose to compile a limited profiled version and then recompile with full profiling367// when the load on C2 goes down.368if (!disable_feedback && CompileBroker::queue_size(CompLevel_full_optimization) >369Tier3DelayOn * compiler_count(CompLevel_full_optimization)) {370next_level = CompLevel_limited_profile;371} else {372next_level = CompLevel_full_profile;373}374}375break;376case CompLevel_limited_profile:377if (is_method_profiled(method)) {378// Special case: we got here because this method was fully profiled in the interpreter.379next_level = CompLevel_full_optimization;380} else {381MethodData* mdo = method->method_data();382if (mdo != NULL) {383if (mdo->would_profile()) {384if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <=385Tier3DelayOff * compiler_count(CompLevel_full_optimization) &&386(this->*p)(i, b, cur_level))) {387next_level = CompLevel_full_profile;388}389} else {390next_level = CompLevel_full_optimization;391}392}393}394break;395case CompLevel_full_profile:396{397MethodData* mdo = method->method_data();398if (mdo != NULL) {399if (mdo->would_profile()) {400int mdo_i = mdo->invocation_count_delta();401int mdo_b = mdo->backedge_count_delta();402if ((this->*p)(mdo_i, mdo_b, cur_level)) {403next_level = CompLevel_full_optimization;404}405} else {406next_level = CompLevel_full_optimization;407}408}409}410break;411}412}413return MIN2(next_level, (CompLevel)TieredStopAtLevel);414}415416// Determine if a method should be compiled with a normal entry point at a different level.417CompLevel AdvancedThresholdPolicy::call_event(Method* method, CompLevel cur_level) {418CompLevel osr_level = MIN2((CompLevel) method->highest_osr_comp_level(),419common(&AdvancedThresholdPolicy::loop_predicate, method, cur_level, true));420CompLevel next_level = common(&AdvancedThresholdPolicy::call_predicate, method, cur_level);421422// If OSR method level is greater than the regular method level, the levels should be423// equalized by raising the regular method level in order to avoid OSRs during each424// invocation of the method.425if (osr_level == CompLevel_full_optimization && cur_level == CompLevel_full_profile) {426MethodData* mdo = method->method_data();427guarantee(mdo != NULL, "MDO should not be NULL");428if (mdo->invocation_count() >= 1) {429next_level = CompLevel_full_optimization;430}431} else {432next_level = MAX2(osr_level, next_level);433}434return next_level;435}436437// Determine if we should do an OSR compilation of a given method.438CompLevel AdvancedThresholdPolicy::loop_event(Method* method, CompLevel cur_level) {439CompLevel next_level = common(&AdvancedThresholdPolicy::loop_predicate, method, cur_level, true);440if (cur_level == CompLevel_none) {441// If there is a live OSR method that means that we deopted to the interpreter442// for the transition.443CompLevel osr_level = MIN2((CompLevel)method->highest_osr_comp_level(), next_level);444if (osr_level > CompLevel_none) {445return osr_level;446}447}448return next_level;449}450451// Update the rate and submit compile452void AdvancedThresholdPolicy::submit_compile(methodHandle mh, int bci, CompLevel level, JavaThread* thread) {453int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count();454update_rate(os::javaTimeMillis(), mh());455CompileBroker::compile_method(mh, bci, level, mh, hot_count, "tiered", thread);456}457458// Handle the invocation event.459void AdvancedThresholdPolicy::method_invocation_event(methodHandle mh, methodHandle imh,460CompLevel level, nmethod* nm, JavaThread* thread) {461if (should_create_mdo(mh(), level)) {462create_mdo(mh, thread);463}464if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh)) {465CompLevel next_level = call_event(mh(), level);466if (next_level != level) {467compile(mh, InvocationEntryBci, next_level, thread);468}469}470}471472// Handle the back branch event. Notice that we can compile the method473// with a regular entry from here.474void AdvancedThresholdPolicy::method_back_branch_event(methodHandle mh, methodHandle imh,475int bci, CompLevel level, nmethod* nm, JavaThread* thread) {476if (should_create_mdo(mh(), level)) {477create_mdo(mh, thread);478}479// Check if MDO should be created for the inlined method480if (should_create_mdo(imh(), level)) {481create_mdo(imh, thread);482}483484if (is_compilation_enabled()) {485CompLevel next_osr_level = loop_event(imh(), level);486CompLevel max_osr_level = (CompLevel)imh->highest_osr_comp_level();487// At the very least compile the OSR version488if (!CompileBroker::compilation_is_in_queue(imh) && (next_osr_level != level)) {489compile(imh, bci, next_osr_level, thread);490}491492// Use loop event as an opportunity to also check if there's been493// enough calls.494CompLevel cur_level, next_level;495if (mh() != imh()) { // If there is an enclosing method496guarantee(nm != NULL, "Should have nmethod here");497cur_level = comp_level(mh());498next_level = call_event(mh(), cur_level);499500if (max_osr_level == CompLevel_full_optimization) {501// The inlinee OSRed to full opt, we need to modify the enclosing method to avoid deopts502bool make_not_entrant = false;503if (nm->is_osr_method()) {504// This is an osr method, just make it not entrant and recompile later if needed505make_not_entrant = true;506} else {507if (next_level != CompLevel_full_optimization) {508// next_level is not full opt, so we need to recompile the509// enclosing method without the inlinee510cur_level = CompLevel_none;511make_not_entrant = true;512}513}514if (make_not_entrant) {515if (PrintTieredEvents) {516int osr_bci = nm->is_osr_method() ? nm->osr_entry_bci() : InvocationEntryBci;517print_event(MAKE_NOT_ENTRANT, mh(), mh(), osr_bci, level);518}519nm->make_not_entrant();520}521}522if (!CompileBroker::compilation_is_in_queue(mh)) {523// Fix up next_level if necessary to avoid deopts524if (next_level == CompLevel_limited_profile && max_osr_level == CompLevel_full_profile) {525next_level = CompLevel_full_profile;526}527if (cur_level != next_level) {528compile(mh, InvocationEntryBci, next_level, thread);529}530}531} else {532cur_level = comp_level(imh());533next_level = call_event(imh(), cur_level);534if (!CompileBroker::compilation_is_in_queue(imh) && (next_level != cur_level)) {535compile(imh, InvocationEntryBci, next_level, thread);536}537}538}539}540541#endif // TIERED542543544