1 /*
   2  * Copyright (c) 2001, 2010, 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 "compiler/compileBroker.hpp"
  27 #include "memory/resourceArea.hpp"
  28 #include "runtime/arguments.hpp"
  29 #include "runtime/simpleThresholdPolicy.hpp"
  30 #include "runtime/simpleThresholdPolicy.inline.hpp"
  31 
  32 // Print an event.
  33 void SimpleThresholdPolicy::print_event(EventType type, methodHandle mh, methodHandle imh,
  34                                         int bci, CompLevel level) {
  35   bool inlinee_event = mh() != imh();
  36 
  37   ttyLocker tty_lock;
  38   tty->print("%lf: [", os::elapsedTime());
  39 
  40   int invocation_count = mh->invocation_count();
  41   int backedge_count = mh->backedge_count();
  42   switch(type) {
  43   case CALL:
  44     tty->print("call");
  45     break;
  46   case LOOP:
  47     tty->print("loop");
  48     break;
  49   case COMPILE:
  50     tty->print("compile");
  51   }
  52 
  53   tty->print(" level: %d ", level);
  54 
  55   ResourceMark rm;
  56   char *method_name = mh->name_and_sig_as_C_string();
  57   tty->print("[%s", method_name);
  58   // We can have an inlinee, although currently we don't generate any notifications for the inlined methods.
  59   if (inlinee_event) {
  60     char *inlinee_name = imh->name_and_sig_as_C_string();
  61     tty->print(" [%s]] ", inlinee_name);
  62   }
  63   else tty->print("] ");
  64   tty->print("@%d queues: %d,%d", bci, CompileBroker::queue_size(CompLevel_full_profile),
  65                                        CompileBroker::queue_size(CompLevel_full_optimization));
  66 
  67   print_specific(type, mh, imh, bci, level);
  68 
  69   if (type != COMPILE) {
  70     methodDataHandle mdh = mh->method_data();
  71     int mdo_invocations = 0, mdo_backedges = 0;
  72     if (mdh() != NULL) {
  73       mdo_invocations = mdh->invocation_count();
  74       mdo_backedges = mdh->backedge_count();
  75     }
  76     tty->print(" total: %d,%d mdo: %d,%d",
  77                invocation_count, backedge_count,
  78                mdo_invocations, mdo_backedges);
  79     tty->print(" max levels: %d,%d",
  80                 mh->highest_comp_level(), mh->highest_osr_comp_level());
  81     if (inlinee_event) {
  82       tty->print(" inlinee max levels: %d,%d", imh->highest_comp_level(), imh->highest_osr_comp_level());
  83     }
  84     tty->print(" compilable: ");
  85     bool need_comma = false;
  86     if (!mh->is_not_compilable(CompLevel_full_profile)) {
  87       tty->print("c1");
  88       need_comma = true;
  89     }
  90     if (!mh->is_not_compilable(CompLevel_full_optimization)) {
  91       if (need_comma) tty->print(", ");
  92       tty->print("c2");
  93       need_comma = true;
  94     }
  95     if (!mh->is_not_osr_compilable()) {
  96       if (need_comma) tty->print(", ");
  97       tty->print("osr");
  98     }
  99     tty->print(" status:");
 100     if (mh->queued_for_compilation()) {
 101       tty->print(" in queue");
 102     } else tty->print(" idle");
 103   }
 104   tty->print_cr("]");
 105 }
 106 
 107 void SimpleThresholdPolicy::initialize() {
 108   if (FLAG_IS_DEFAULT(CICompilerCount)) {
 109     FLAG_SET_DEFAULT(CICompilerCount, 3);
 110   }
 111   int count = CICompilerCount;
 112   if (CICompilerCountPerCPU) {
 113     count = MAX2(log2_intptr(os::active_processor_count()), 1) * 3 / 2;
 114   }
 115   set_c1_count(MAX2(count / 3, 1));
 116   set_c2_count(MAX2(count - count / 3, 1));
 117 }
 118 
 119 void SimpleThresholdPolicy::set_carry_if_necessary(InvocationCounter *counter) {
 120   if (!counter->carry() && counter->count() > InvocationCounter::count_limit / 2) {
 121     counter->set_carry_flag();
 122   }
 123 }
 124 
 125 // Set carry flags on the counters if necessary
 126 void SimpleThresholdPolicy::handle_counter_overflow(methodOop method) {
 127   set_carry_if_necessary(method->invocation_counter());
 128   set_carry_if_necessary(method->backedge_counter());
 129   methodDataOop mdo = method->method_data();
 130   if (mdo != NULL) {
 131     set_carry_if_necessary(mdo->invocation_counter());
 132     set_carry_if_necessary(mdo->backedge_counter());
 133   }
 134 }
 135 
 136 // Called with the queue locked and with at least one element
 137 CompileTask* SimpleThresholdPolicy::select_task(CompileQueue* compile_queue) {
 138   return compile_queue->first();
 139 }
 140 
 141 nmethod* SimpleThresholdPolicy::event(methodHandle method, methodHandle inlinee,
 142                                       int branch_bci, int bci, CompLevel comp_level, TRAPS) {
 143   if (comp_level == CompLevel_none &&
 144       JvmtiExport::can_post_interpreter_events()) {
 145     assert(THREAD->is_Java_thread(), "Should be java thread");
 146     if (((JavaThread*)THREAD)->is_interp_only_mode()) {
 147       return NULL;
 148     }
 149   }
 150   nmethod *osr_nm = NULL;
 151 
 152   handle_counter_overflow(method());
 153   if (method() != inlinee()) {
 154     handle_counter_overflow(inlinee());
 155   }
 156 
 157   if (PrintTieredEvents) {
 158     print_event(bci == InvocationEntryBci ? CALL : LOOP, method, inlinee, bci, comp_level);
 159   }
 160 
 161   if (bci == InvocationEntryBci) {
 162     method_invocation_event(method, inlinee, comp_level, THREAD);
 163   } else {
 164     method_back_branch_event(method, inlinee, bci, comp_level, THREAD);
 165     int highest_level = method->highest_osr_comp_level();
 166     if (highest_level > comp_level) {
 167       osr_nm = method->lookup_osr_nmethod_for(bci, highest_level, false);
 168     }
 169   }
 170   return osr_nm;
 171 }
 172 
 173 // Check if the method can be compiled, change level if necessary
 174 void SimpleThresholdPolicy::compile(methodHandle mh, int bci, CompLevel level, TRAPS) {
 175   // Take the given ceiling into the account.
 176   // NOTE: You can set it to 1 to get a pure C1 version.
 177   if ((CompLevel)TieredStopAtLevel < level) {
 178     level = (CompLevel)TieredStopAtLevel;
 179   }
 180   if (level == CompLevel_none) {
 181     return;
 182   }
 183   // Check if the method can be compiled, if not - try different levels.
 184   if (!can_be_compiled(mh, level)) {
 185     if (level < CompLevel_full_optimization && can_be_compiled(mh, CompLevel_full_optimization)) {
 186       compile(mh, bci, CompLevel_full_optimization, THREAD);
 187     }
 188     if (level == CompLevel_full_optimization && can_be_compiled(mh, CompLevel_simple)) {
 189         compile(mh, bci, CompLevel_simple, THREAD);
 190     }
 191     return;
 192   }
 193   if (bci != InvocationEntryBci && mh->is_not_osr_compilable()) {
 194     return;
 195   }
 196   if (PrintTieredEvents) {
 197     print_event(COMPILE, mh, mh, bci, level);
 198   }
 199   if (!CompileBroker::compilation_is_in_queue(mh, bci)) {
 200     submit_compile(mh, bci, level, THREAD);
 201   }
 202 }
 203 
 204 // Tell the broker to compile the method
 205 void SimpleThresholdPolicy::submit_compile(methodHandle mh, int bci, CompLevel level, TRAPS) {
 206   int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count();
 207   CompileBroker::compile_method(mh, bci, level, mh, hot_count, "tiered", THREAD);
 208 }
 209 
 210 // Call and loop predicates determine whether a transition to a higher
 211 // compilation level should be performed (pointers to predicate functions
 212 // are passed to common() transition function).
 213 bool SimpleThresholdPolicy::loop_predicate(int i, int b, CompLevel cur_level) {
 214   switch(cur_level) {
 215   case CompLevel_none:
 216   case CompLevel_limited_profile: {
 217     return loop_predicate_helper<CompLevel_none>(i, b, 1.0);
 218   }
 219   case CompLevel_full_profile: {
 220     return loop_predicate_helper<CompLevel_full_profile>(i, b, 1.0);
 221   }
 222   default:
 223     return true;
 224   }
 225 }
 226 
 227 bool SimpleThresholdPolicy::call_predicate(int i, int b, CompLevel cur_level) {
 228   switch(cur_level) {
 229   case CompLevel_none:
 230   case CompLevel_limited_profile: {
 231     return call_predicate_helper<CompLevel_none>(i, b, 1.0);
 232   }
 233   case CompLevel_full_profile: {
 234     return call_predicate_helper<CompLevel_full_profile>(i, b, 1.0);
 235   }
 236   default:
 237     return true;
 238   }
 239 }
 240 
 241 // Determine is a method is mature.
 242 bool SimpleThresholdPolicy::is_mature(methodOop method) {
 243   if (is_trivial(method)) return true;
 244   methodDataOop mdo = method->method_data();
 245   if (mdo != NULL) {
 246     int i = mdo->invocation_count();
 247     int b = mdo->backedge_count();
 248     double k = ProfileMaturityPercentage / 100.0;
 249     return call_predicate_helper<CompLevel_full_profile>(i, b, k) ||
 250            loop_predicate_helper<CompLevel_full_profile>(i, b, k);
 251   }
 252   return false;
 253 }
 254 
 255 // Common transition function. Given a predicate determines if a method should transition to another level.
 256 CompLevel SimpleThresholdPolicy::common(Predicate p, methodOop method, CompLevel cur_level) {
 257   CompLevel next_level = cur_level;
 258   int i = method->invocation_count();
 259   int b = method->backedge_count();
 260 
 261   switch(cur_level) {
 262   case CompLevel_none:
 263     {
 264       methodDataOop mdo = method->method_data();
 265       if (mdo != NULL) {
 266         int mdo_i = mdo->invocation_count();
 267         int mdo_b = mdo->backedge_count();
 268         // If we were at full profile level, would we switch to full opt?
 269         if ((this->*p)(mdo_i, mdo_b, CompLevel_full_profile)) {
 270           next_level = CompLevel_full_optimization;
 271         }
 272       }
 273     }
 274     if (next_level == cur_level && (this->*p)(i, b, cur_level)) {
 275       if (is_trivial(method)) {
 276         next_level = CompLevel_simple;
 277       } else {
 278         next_level = CompLevel_full_profile;
 279       }
 280     }
 281     break;
 282   case CompLevel_limited_profile:
 283   case CompLevel_full_profile:
 284     if (is_trivial(method)) {
 285       next_level = CompLevel_simple;
 286     } else {
 287       methodDataOop mdo = method->method_data();
 288       guarantee(mdo != NULL, "MDO should always exist");
 289       if (mdo->would_profile()) {
 290         int mdo_i = mdo->invocation_count();
 291         int mdo_b = mdo->backedge_count();
 292         if ((this->*p)(mdo_i, mdo_b, cur_level)) {
 293           next_level = CompLevel_full_optimization;
 294         }
 295       } else {
 296         next_level = CompLevel_full_optimization;
 297       }
 298     }
 299     break;
 300   }
 301   return next_level;
 302 }
 303 
 304 // Determine if a method should be compiled with a normal entry point at a different level.
 305 CompLevel SimpleThresholdPolicy::call_event(methodOop method,  CompLevel cur_level) {
 306   CompLevel highest_level = (CompLevel)method->highest_comp_level();
 307   if (cur_level == CompLevel_none && highest_level > cur_level) {
 308     // TODO: We may want to try to do more extensive reprofiling in this case.
 309     return highest_level;
 310   }
 311 
 312   CompLevel osr_level = (CompLevel) method->highest_osr_comp_level();
 313   CompLevel next_level = common(&SimpleThresholdPolicy::call_predicate, method, cur_level);
 314 
 315   // If OSR method level is greater than the regular method level, the levels should be
 316   // equalized by raising the regular method level in order to avoid OSRs during each
 317   // invocation of the method.
 318   if (osr_level == CompLevel_full_optimization && cur_level == CompLevel_full_profile) {
 319     methodDataOop mdo = method->method_data();
 320     guarantee(mdo != NULL, "MDO should not be NULL");
 321     if (mdo->invocation_count() >= 1) {
 322       next_level = CompLevel_full_optimization;
 323     }
 324   } else {
 325     next_level = MAX2(osr_level, next_level);
 326   }
 327 
 328   return next_level;
 329 }
 330 
 331 // Determine if we should do an OSR compilation of a given method.
 332 CompLevel SimpleThresholdPolicy::loop_event(methodOop method, CompLevel cur_level) {
 333   if (cur_level == CompLevel_none) {
 334     // If there is a live OSR method that means that we deopted to the interpreter
 335     // for the transition.
 336     CompLevel osr_level = (CompLevel)method->highest_osr_comp_level();
 337     if (osr_level > CompLevel_none) {
 338       return osr_level;
 339     }
 340   }
 341   return common(&SimpleThresholdPolicy::loop_predicate, method, cur_level);
 342 }
 343 
 344 
 345 // Handle the invocation event.
 346 void SimpleThresholdPolicy::method_invocation_event(methodHandle mh, methodHandle imh,
 347                                               CompLevel level, TRAPS) {
 348   if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh, InvocationEntryBci)) {
 349     CompLevel next_level = call_event(mh(), level);
 350     if (next_level != level) {
 351       compile(mh, InvocationEntryBci, next_level, THREAD);
 352     }
 353   }
 354 }
 355 
 356 // Handle the back branch event. Notice that we can compile the method
 357 // with a regular entry from here.
 358 void SimpleThresholdPolicy::method_back_branch_event(methodHandle mh, methodHandle imh,
 359                                                int bci, CompLevel level, TRAPS) {
 360   // If the method is already compiling, quickly bail out.
 361   if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh, bci)) {
 362     // Use loop event as an opportinity to also check there's been
 363     // enough calls.
 364     CompLevel cur_level = comp_level(mh());
 365     CompLevel next_level = call_event(mh(), cur_level);
 366     CompLevel next_osr_level = loop_event(mh(), level);
 367 
 368     next_level = MAX2(next_level,
 369                       next_osr_level < CompLevel_full_optimization ? next_osr_level : cur_level);
 370     bool is_compiling = false;
 371     if (next_level != cur_level) {
 372       compile(mh, InvocationEntryBci, next_level, THREAD);
 373       is_compiling = true;
 374     }
 375 
 376     // Do the OSR version
 377     if (!is_compiling && next_osr_level != level) {
 378       compile(mh, bci, next_osr_level, THREAD);
 379     }
 380   }
 381 }