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