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