src/share/vm/runtime/compilationPolicy.cpp

Fri, 03 Sep 2010 17:51:07 -0700

author
iveresov
date
Fri, 03 Sep 2010 17:51:07 -0700
changeset 2138
d5d065957597
parent 2103
3e8fbc61cee8
child 2176
df015ec64052
permissions
-rw-r--r--

6953144: Tiered compilation
Summary: Infrastructure for tiered compilation support (interpreter + c1 + c2) for 32 and 64 bit. Simple tiered policy implementation.
Reviewed-by: kvn, never, phh, twisti

duke@435 1 /*
trims@1907 2 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_compilationPolicy.cpp.incl"
duke@435 27
duke@435 28 CompilationPolicy* CompilationPolicy::_policy;
duke@435 29 elapsedTimer CompilationPolicy::_accumulated_time;
duke@435 30 bool CompilationPolicy::_in_vm_startup;
duke@435 31
duke@435 32 // Determine compilation policy based on command line argument
duke@435 33 void compilationPolicy_init() {
duke@435 34 CompilationPolicy::set_in_vm_startup(DelayCompilationDuringStartup);
duke@435 35
duke@435 36 switch(CompilationPolicyChoice) {
duke@435 37 case 0:
duke@435 38 CompilationPolicy::set_policy(new SimpleCompPolicy());
duke@435 39 break;
duke@435 40
duke@435 41 case 1:
duke@435 42 #ifdef COMPILER2
duke@435 43 CompilationPolicy::set_policy(new StackWalkCompPolicy());
duke@435 44 #else
duke@435 45 Unimplemented();
duke@435 46 #endif
duke@435 47 break;
iveresov@2138 48 case 2:
iveresov@2138 49 #ifdef TIERED
iveresov@2138 50 CompilationPolicy::set_policy(new SimpleThresholdPolicy());
iveresov@2138 51 #else
iveresov@2138 52 Unimplemented();
iveresov@2138 53 #endif
iveresov@2138 54 break;
duke@435 55 default:
iveresov@2138 56 fatal("CompilationPolicyChoice must be in the range: [0-2]");
duke@435 57 }
iveresov@2138 58 CompilationPolicy::policy()->initialize();
duke@435 59 }
duke@435 60
duke@435 61 void CompilationPolicy::completed_vm_startup() {
duke@435 62 if (TraceCompilationPolicy) {
duke@435 63 tty->print("CompilationPolicy: completed vm startup.\n");
duke@435 64 }
duke@435 65 _in_vm_startup = false;
duke@435 66 }
duke@435 67
duke@435 68 // Returns true if m must be compiled before executing it
duke@435 69 // This is intended to force compiles for methods (usually for
duke@435 70 // debugging) that would otherwise be interpreted for some reason.
iveresov@2138 71 bool CompilationPolicy::must_be_compiled(methodHandle m, int comp_level) {
duke@435 72 if (m->has_compiled_code()) return false; // already compiled
iveresov@2138 73 if (!can_be_compiled(m, comp_level)) return false;
duke@435 74
duke@435 75 return !UseInterpreter || // must compile all methods
kvn@1637 76 (UseCompiler && AlwaysCompileLoopMethods && m->has_loops() && CompileBroker::should_compile_new_jobs()); // eagerly compile loop methods
duke@435 77 }
duke@435 78
duke@435 79 // Returns true if m is allowed to be compiled
iveresov@2138 80 bool CompilationPolicy::can_be_compiled(methodHandle m, int comp_level) {
duke@435 81 if (m->is_abstract()) return false;
duke@435 82 if (DontCompileHugeMethods && m->code_size() > HugeMethodLimit) return false;
duke@435 83
never@1609 84 // Math intrinsics should never be compiled as this can lead to
never@1609 85 // monotonicity problems because the interpreter will prefer the
never@1609 86 // compiled code to the intrinsic version. This can't happen in
never@1609 87 // production because the invocation counter can't be incremented
never@1609 88 // but we shouldn't expose the system to this problem in testing
never@1609 89 // modes.
never@1609 90 if (!AbstractInterpreter::can_be_compiled(m)) {
never@1609 91 return false;
never@1609 92 }
iveresov@2138 93 if (comp_level == CompLevel_all) {
iveresov@2138 94 return !m->is_not_compilable(CompLevel_simple) && !m->is_not_compilable(CompLevel_full_optimization);
iveresov@2138 95 } else {
iveresov@2138 96 return !m->is_not_compilable(comp_level);
iveresov@2138 97 }
iveresov@2138 98 }
never@1609 99
iveresov@2138 100 bool CompilationPolicy::is_compilation_enabled() {
iveresov@2138 101 // NOTE: CompileBroker::should_compile_new_jobs() checks for UseCompiler
iveresov@2138 102 return !delay_compilation_during_startup() && CompileBroker::should_compile_new_jobs();
duke@435 103 }
duke@435 104
duke@435 105 #ifndef PRODUCT
duke@435 106 void CompilationPolicy::print_time() {
duke@435 107 tty->print_cr ("Accumulated compilationPolicy times:");
duke@435 108 tty->print_cr ("---------------------------");
duke@435 109 tty->print_cr (" Total: %3.3f sec.", _accumulated_time.seconds());
duke@435 110 }
duke@435 111
iveresov@2138 112 void NonTieredCompPolicy::trace_osr_completion(nmethod* osr_nm) {
duke@435 113 if (TraceOnStackReplacement) {
duke@435 114 if (osr_nm == NULL) tty->print_cr("compilation failed");
duke@435 115 else tty->print_cr("nmethod " INTPTR_FORMAT, osr_nm);
duke@435 116 }
duke@435 117 }
duke@435 118 #endif // !PRODUCT
duke@435 119
iveresov@2138 120 void NonTieredCompPolicy::initialize() {
iveresov@2138 121 // Setup the compiler thread numbers
iveresov@2138 122 if (CICompilerCountPerCPU) {
iveresov@2138 123 // Example: if CICompilerCountPerCPU is true, then we get
iveresov@2138 124 // max(log2(8)-1,1) = 2 compiler threads on an 8-way machine.
iveresov@2138 125 // May help big-app startup time.
iveresov@2138 126 _compiler_count = MAX2(log2_intptr(os::active_processor_count())-1,1);
iveresov@2138 127 } else {
iveresov@2138 128 _compiler_count = CICompilerCount;
iveresov@2138 129 }
iveresov@2138 130 }
iveresov@2138 131
iveresov@2138 132 int NonTieredCompPolicy::compiler_count(CompLevel comp_level) {
iveresov@2138 133 #ifdef COMPILER1
iveresov@2138 134 if (is_c1_compile(comp_level)) {
iveresov@2138 135 return _compiler_count;
iveresov@2138 136 }
iveresov@2138 137 #endif
iveresov@2138 138
iveresov@2138 139 #ifdef COMPILER2
iveresov@2138 140 if (is_c2_compile(comp_level)) {
iveresov@2138 141 return _compiler_count;
iveresov@2138 142 }
iveresov@2138 143 #endif
iveresov@2138 144
iveresov@2138 145 return 0;
iveresov@2138 146 }
iveresov@2138 147
iveresov@2138 148 void NonTieredCompPolicy::reset_counter_for_invocation_event(methodHandle m) {
duke@435 149 // Make sure invocation and backedge counter doesn't overflow again right away
duke@435 150 // as would be the case for native methods.
duke@435 151
duke@435 152 // BUT also make sure the method doesn't look like it was never executed.
duke@435 153 // Set carry bit and reduce counter's value to min(count, CompileThreshold/2).
duke@435 154 m->invocation_counter()->set_carry();
duke@435 155 m->backedge_counter()->set_carry();
duke@435 156
duke@435 157 assert(!m->was_never_executed(), "don't reset to 0 -- could be mistaken for never-executed");
duke@435 158 }
duke@435 159
iveresov@2138 160 void NonTieredCompPolicy::reset_counter_for_back_branch_event(methodHandle m) {
duke@435 161 // Delay next back-branch event but pump up invocation counter to triger
duke@435 162 // whole method compilation.
duke@435 163 InvocationCounter* i = m->invocation_counter();
duke@435 164 InvocationCounter* b = m->backedge_counter();
duke@435 165
duke@435 166 // Don't set invocation_counter's value too low otherwise the method will
duke@435 167 // look like immature (ic < ~5300) which prevents the inlining based on
duke@435 168 // the type profiling.
duke@435 169 i->set(i->state(), CompileThreshold);
duke@435 170 // Don't reset counter too low - it is used to check if OSR method is ready.
duke@435 171 b->set(b->state(), CompileThreshold / 2);
duke@435 172 }
duke@435 173
iveresov@2138 174 //
iveresov@2138 175 // CounterDecay
iveresov@2138 176 //
iveresov@2138 177 // Interates through invocation counters and decrements them. This
iveresov@2138 178 // is done at each safepoint.
iveresov@2138 179 //
iveresov@2138 180 class CounterDecay : public AllStatic {
iveresov@2138 181 static jlong _last_timestamp;
iveresov@2138 182 static void do_method(methodOop m) {
iveresov@2138 183 m->invocation_counter()->decay();
iveresov@2138 184 }
iveresov@2138 185 public:
iveresov@2138 186 static void decay();
iveresov@2138 187 static bool is_decay_needed() {
iveresov@2138 188 return (os::javaTimeMillis() - _last_timestamp) > CounterDecayMinIntervalLength;
iveresov@2138 189 }
iveresov@2138 190 };
iveresov@2138 191
iveresov@2138 192 jlong CounterDecay::_last_timestamp = 0;
iveresov@2138 193
iveresov@2138 194 void CounterDecay::decay() {
iveresov@2138 195 _last_timestamp = os::javaTimeMillis();
iveresov@2138 196
iveresov@2138 197 // This operation is going to be performed only at the end of a safepoint
iveresov@2138 198 // and hence GC's will not be going on, all Java mutators are suspended
iveresov@2138 199 // at this point and hence SystemDictionary_lock is also not needed.
iveresov@2138 200 assert(SafepointSynchronize::is_at_safepoint(), "can only be executed at a safepoint");
iveresov@2138 201 int nclasses = SystemDictionary::number_of_classes();
iveresov@2138 202 double classes_per_tick = nclasses * (CounterDecayMinIntervalLength * 1e-3 /
iveresov@2138 203 CounterHalfLifeTime);
iveresov@2138 204 for (int i = 0; i < classes_per_tick; i++) {
iveresov@2138 205 klassOop k = SystemDictionary::try_get_next_class();
iveresov@2138 206 if (k != NULL && k->klass_part()->oop_is_instance()) {
iveresov@2138 207 instanceKlass::cast(k)->methods_do(do_method);
iveresov@2138 208 }
iveresov@2138 209 }
iveresov@2138 210 }
iveresov@2138 211
iveresov@2138 212 // Called at the end of the safepoint
iveresov@2138 213 void NonTieredCompPolicy::do_safepoint_work() {
iveresov@2138 214 if(UseCounterDecay && CounterDecay::is_decay_needed()) {
iveresov@2138 215 CounterDecay::decay();
iveresov@2138 216 }
iveresov@2138 217 }
iveresov@2138 218
iveresov@2138 219 void NonTieredCompPolicy::reprofile(ScopeDesc* trap_scope, bool is_osr) {
iveresov@2138 220 ScopeDesc* sd = trap_scope;
iveresov@2138 221 for (; !sd->is_top(); sd = sd->sender()) {
iveresov@2138 222 // Reset ICs of inlined methods, since they can trigger compilations also.
iveresov@2138 223 sd->method()->invocation_counter()->reset();
iveresov@2138 224 }
iveresov@2138 225 InvocationCounter* c = sd->method()->invocation_counter();
iveresov@2138 226 if (is_osr) {
iveresov@2138 227 // It was an OSR method, so bump the count higher.
iveresov@2138 228 c->set(c->state(), CompileThreshold);
iveresov@2138 229 } else {
iveresov@2138 230 c->reset();
iveresov@2138 231 }
iveresov@2138 232 sd->method()->backedge_counter()->reset();
iveresov@2138 233 }
iveresov@2138 234
iveresov@2138 235 // This method can be called by any component of the runtime to notify the policy
iveresov@2138 236 // that it's recommended to delay the complation of this method.
iveresov@2138 237 void NonTieredCompPolicy::delay_compilation(methodOop method) {
iveresov@2138 238 method->invocation_counter()->decay();
iveresov@2138 239 method->backedge_counter()->decay();
iveresov@2138 240 }
iveresov@2138 241
iveresov@2138 242 void NonTieredCompPolicy::disable_compilation(methodOop method) {
iveresov@2138 243 method->invocation_counter()->set_state(InvocationCounter::wait_for_nothing);
iveresov@2138 244 method->backedge_counter()->set_state(InvocationCounter::wait_for_nothing);
iveresov@2138 245 }
iveresov@2138 246
iveresov@2138 247 CompileTask* NonTieredCompPolicy::select_task(CompileQueue* compile_queue) {
iveresov@2138 248 return compile_queue->first();
iveresov@2138 249 }
iveresov@2138 250
iveresov@2138 251 bool NonTieredCompPolicy::is_mature(methodOop method) {
iveresov@2138 252 methodDataOop mdo = method->method_data();
iveresov@2138 253 assert(mdo != NULL, "Should be");
iveresov@2138 254 uint current = mdo->mileage_of(method);
iveresov@2138 255 uint initial = mdo->creation_mileage();
iveresov@2138 256 if (current < initial)
iveresov@2138 257 return true; // some sort of overflow
iveresov@2138 258 uint target;
iveresov@2138 259 if (ProfileMaturityPercentage <= 0)
iveresov@2138 260 target = (uint) -ProfileMaturityPercentage; // absolute value
iveresov@2138 261 else
iveresov@2138 262 target = (uint)( (ProfileMaturityPercentage * CompileThreshold) / 100 );
iveresov@2138 263 return (current >= initial + target);
iveresov@2138 264 }
iveresov@2138 265
iveresov@2138 266 nmethod* NonTieredCompPolicy::event(methodHandle method, methodHandle inlinee, int branch_bci, int bci, CompLevel comp_level, TRAPS) {
iveresov@2138 267 assert(comp_level == CompLevel_none, "This should be only called from the interpreter");
iveresov@2138 268 NOT_PRODUCT(trace_frequency_counter_overflow(method, branch_bci, bci));
iveresov@2138 269 if (JvmtiExport::can_post_interpreter_events()) {
iveresov@2138 270 assert(THREAD->is_Java_thread(), "Wrong type of thread");
iveresov@2138 271 if (((JavaThread*)THREAD)->is_interp_only_mode()) {
iveresov@2138 272 // If certain JVMTI events (e.g. frame pop event) are requested then the
iveresov@2138 273 // thread is forced to remain in interpreted code. This is
iveresov@2138 274 // implemented partly by a check in the run_compiled_code
iveresov@2138 275 // section of the interpreter whether we should skip running
iveresov@2138 276 // compiled code, and partly by skipping OSR compiles for
iveresov@2138 277 // interpreted-only threads.
iveresov@2138 278 if (bci != InvocationEntryBci) {
iveresov@2138 279 reset_counter_for_back_branch_event(method);
iveresov@2138 280 return NULL;
iveresov@2138 281 }
iveresov@2138 282 }
iveresov@2138 283 }
iveresov@2138 284 if (bci == InvocationEntryBci) {
iveresov@2138 285 // when code cache is full, compilation gets switched off, UseCompiler
iveresov@2138 286 // is set to false
iveresov@2138 287 if (!method->has_compiled_code() && UseCompiler) {
iveresov@2138 288 method_invocation_event(method, CHECK_NULL);
iveresov@2138 289 } else {
iveresov@2138 290 // Force counter overflow on method entry, even if no compilation
iveresov@2138 291 // happened. (The method_invocation_event call does this also.)
iveresov@2138 292 reset_counter_for_invocation_event(method);
iveresov@2138 293 }
iveresov@2138 294 // compilation at an invocation overflow no longer goes and retries test for
iveresov@2138 295 // compiled method. We always run the loser of the race as interpreted.
iveresov@2138 296 // so return NULL
iveresov@2138 297 return NULL;
iveresov@2138 298 } else {
iveresov@2138 299 // counter overflow in a loop => try to do on-stack-replacement
iveresov@2138 300 nmethod* osr_nm = method->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true);
iveresov@2138 301 NOT_PRODUCT(trace_osr_request(method, osr_nm, bci));
iveresov@2138 302 // when code cache is full, we should not compile any more...
iveresov@2138 303 if (osr_nm == NULL && UseCompiler) {
iveresov@2138 304 method_back_branch_event(method, bci, CHECK_NULL);
iveresov@2138 305 osr_nm = method->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true);
iveresov@2138 306 }
iveresov@2138 307 if (osr_nm == NULL) {
iveresov@2138 308 reset_counter_for_back_branch_event(method);
iveresov@2138 309 return NULL;
iveresov@2138 310 }
iveresov@2138 311 return osr_nm;
iveresov@2138 312 }
iveresov@2138 313 return NULL;
iveresov@2138 314 }
iveresov@2138 315
iveresov@2138 316 #ifndef PRODUCT
iveresov@2138 317 void NonTieredCompPolicy::trace_frequency_counter_overflow(methodHandle m, int branch_bci, int bci) {
iveresov@2138 318 if (TraceInvocationCounterOverflow) {
iveresov@2138 319 InvocationCounter* ic = m->invocation_counter();
iveresov@2138 320 InvocationCounter* bc = m->backedge_counter();
iveresov@2138 321 ResourceMark rm;
iveresov@2138 322 const char* msg =
iveresov@2138 323 bci == InvocationEntryBci
iveresov@2138 324 ? "comp-policy cntr ovfl @ %d in entry of "
iveresov@2138 325 : "comp-policy cntr ovfl @ %d in loop of ";
iveresov@2138 326 tty->print(msg, bci);
iveresov@2138 327 m->print_value();
iveresov@2138 328 tty->cr();
iveresov@2138 329 ic->print();
iveresov@2138 330 bc->print();
iveresov@2138 331 if (ProfileInterpreter) {
iveresov@2138 332 if (bci != InvocationEntryBci) {
iveresov@2138 333 methodDataOop mdo = m->method_data();
iveresov@2138 334 if (mdo != NULL) {
iveresov@2138 335 int count = mdo->bci_to_data(branch_bci)->as_JumpData()->taken();
iveresov@2138 336 tty->print_cr("back branch count = %d", count);
iveresov@2138 337 }
iveresov@2138 338 }
iveresov@2138 339 }
iveresov@2138 340 }
iveresov@2138 341 }
iveresov@2138 342
iveresov@2138 343 void NonTieredCompPolicy::trace_osr_request(methodHandle method, nmethod* osr, int bci) {
iveresov@2138 344 if (TraceOnStackReplacement) {
iveresov@2138 345 ResourceMark rm;
iveresov@2138 346 tty->print(osr != NULL ? "Reused OSR entry for " : "Requesting OSR entry for ");
iveresov@2138 347 method->print_short_name(tty);
iveresov@2138 348 tty->print_cr(" at bci %d", bci);
iveresov@2138 349 }
iveresov@2138 350 }
iveresov@2138 351 #endif // !PRODUCT
iveresov@2138 352
duke@435 353 // SimpleCompPolicy - compile current method
duke@435 354
duke@435 355 void SimpleCompPolicy::method_invocation_event( methodHandle m, TRAPS) {
duke@435 356 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
duke@435 357
duke@435 358 int hot_count = m->invocation_count();
duke@435 359 reset_counter_for_invocation_event(m);
duke@435 360 const char* comment = "count";
duke@435 361
iveresov@2138 362 if (is_compilation_enabled() && can_be_compiled(m)) {
duke@435 363 nmethod* nm = m->code();
duke@435 364 if (nm == NULL ) {
duke@435 365 const char* comment = "count";
iveresov@2138 366 CompileBroker::compile_method(m, InvocationEntryBci, CompLevel_highest_tier,
duke@435 367 m, hot_count, comment, CHECK);
duke@435 368 }
duke@435 369 }
duke@435 370 }
duke@435 371
iveresov@2138 372 void SimpleCompPolicy::method_back_branch_event(methodHandle m, int bci, TRAPS) {
duke@435 373 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
duke@435 374
duke@435 375 int hot_count = m->backedge_count();
duke@435 376 const char* comment = "backedge_count";
duke@435 377
iveresov@2138 378 if (is_compilation_enabled() && !m->is_not_osr_compilable() && can_be_compiled(m)) {
iveresov@2138 379 CompileBroker::compile_method(m, bci, CompLevel_highest_tier,
iveresov@2138 380 m, hot_count, comment, CHECK);
iveresov@2138 381 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true));)
duke@435 382 }
duke@435 383 }
duke@435 384 // StackWalkCompPolicy - walk up stack to find a suitable method to compile
duke@435 385
duke@435 386 #ifdef COMPILER2
duke@435 387 const char* StackWalkCompPolicy::_msg = NULL;
duke@435 388
duke@435 389
duke@435 390 // Consider m for compilation
duke@435 391 void StackWalkCompPolicy::method_invocation_event(methodHandle m, TRAPS) {
duke@435 392 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
duke@435 393
duke@435 394 int hot_count = m->invocation_count();
duke@435 395 reset_counter_for_invocation_event(m);
duke@435 396 const char* comment = "count";
duke@435 397
iveresov@2138 398 if (is_compilation_enabled() && m->code() == NULL && can_be_compiled(m)) {
duke@435 399 ResourceMark rm(THREAD);
duke@435 400 JavaThread *thread = (JavaThread*)THREAD;
duke@435 401 frame fr = thread->last_frame();
duke@435 402 assert(fr.is_interpreted_frame(), "must be interpreted");
duke@435 403 assert(fr.interpreter_frame_method() == m(), "bad method");
duke@435 404
duke@435 405 if (TraceCompilationPolicy) {
duke@435 406 tty->print("method invocation trigger: ");
duke@435 407 m->print_short_name(tty);
duke@435 408 tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", (address)m(), m->code_size());
duke@435 409 }
duke@435 410 RegisterMap reg_map(thread, false);
duke@435 411 javaVFrame* triggerVF = thread->last_java_vframe(&reg_map);
duke@435 412 // triggerVF is the frame that triggered its counter
duke@435 413 RFrame* first = new InterpretedRFrame(triggerVF->fr(), thread, m);
duke@435 414
duke@435 415 if (first->top_method()->code() != NULL) {
duke@435 416 // called obsolete method/nmethod -- no need to recompile
duke@435 417 if (TraceCompilationPolicy) tty->print_cr(" --> " INTPTR_FORMAT, first->top_method()->code());
duke@435 418 } else {
duke@435 419 if (TimeCompilationPolicy) accumulated_time()->start();
duke@435 420 GrowableArray<RFrame*>* stack = new GrowableArray<RFrame*>(50);
duke@435 421 stack->push(first);
duke@435 422 RFrame* top = findTopInlinableFrame(stack);
duke@435 423 if (TimeCompilationPolicy) accumulated_time()->stop();
duke@435 424 assert(top != NULL, "findTopInlinableFrame returned null");
duke@435 425 if (TraceCompilationPolicy) top->print();
iveresov@2138 426 CompileBroker::compile_method(top->top_method(), InvocationEntryBci, CompLevel_highest_tier,
duke@435 427 m, hot_count, comment, CHECK);
duke@435 428 }
duke@435 429 }
duke@435 430 }
duke@435 431
iveresov@2138 432 void StackWalkCompPolicy::method_back_branch_event(methodHandle m, int bci, TRAPS) {
duke@435 433 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
duke@435 434
duke@435 435 int hot_count = m->backedge_count();
duke@435 436 const char* comment = "backedge_count";
duke@435 437
iveresov@2138 438 if (is_compilation_enabled() && !m->is_not_osr_compilable() && can_be_compiled(m)) {
iveresov@2138 439 CompileBroker::compile_method(m, bci, CompLevel_highest_tier, m, hot_count, comment, CHECK);
duke@435 440
iveresov@2138 441 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true));)
duke@435 442 }
duke@435 443 }
duke@435 444
duke@435 445 RFrame* StackWalkCompPolicy::findTopInlinableFrame(GrowableArray<RFrame*>* stack) {
duke@435 446 // go up the stack until finding a frame that (probably) won't be inlined
duke@435 447 // into its caller
duke@435 448 RFrame* current = stack->at(0); // current choice for stopping
duke@435 449 assert( current && !current->is_compiled(), "" );
duke@435 450 const char* msg = NULL;
duke@435 451
duke@435 452 while (1) {
duke@435 453
duke@435 454 // before going up the stack further, check if doing so would get us into
duke@435 455 // compiled code
duke@435 456 RFrame* next = senderOf(current, stack);
duke@435 457 if( !next ) // No next frame up the stack?
duke@435 458 break; // Then compile with current frame
duke@435 459
duke@435 460 methodHandle m = current->top_method();
duke@435 461 methodHandle next_m = next->top_method();
duke@435 462
duke@435 463 if (TraceCompilationPolicy && Verbose) {
duke@435 464 tty->print("[caller: ");
duke@435 465 next_m->print_short_name(tty);
duke@435 466 tty->print("] ");
duke@435 467 }
duke@435 468
duke@435 469 if( !Inline ) { // Inlining turned off
duke@435 470 msg = "Inlining turned off";
duke@435 471 break;
duke@435 472 }
duke@435 473 if (next_m->is_not_compilable()) { // Did fail to compile this before/
duke@435 474 msg = "caller not compilable";
duke@435 475 break;
duke@435 476 }
duke@435 477 if (next->num() > MaxRecompilationSearchLength) {
duke@435 478 // don't go up too high when searching for recompilees
duke@435 479 msg = "don't go up any further: > MaxRecompilationSearchLength";
duke@435 480 break;
duke@435 481 }
duke@435 482 if (next->distance() > MaxInterpretedSearchLength) {
duke@435 483 // don't go up too high when searching for recompilees
duke@435 484 msg = "don't go up any further: next > MaxInterpretedSearchLength";
duke@435 485 break;
duke@435 486 }
duke@435 487 // Compiled frame above already decided not to inline;
duke@435 488 // do not recompile him.
duke@435 489 if (next->is_compiled()) {
duke@435 490 msg = "not going up into optimized code";
duke@435 491 break;
duke@435 492 }
duke@435 493
duke@435 494 // Interpreted frame above us was already compiled. Do not force
duke@435 495 // a recompile, although if the frame above us runs long enough an
duke@435 496 // OSR might still happen.
duke@435 497 if( current->is_interpreted() && next_m->has_compiled_code() ) {
duke@435 498 msg = "not going up -- already compiled caller";
duke@435 499 break;
duke@435 500 }
duke@435 501
duke@435 502 // Compute how frequent this call site is. We have current method 'm'.
duke@435 503 // We know next method 'next_m' is interpreted. Find the call site and
duke@435 504 // check the various invocation counts.
duke@435 505 int invcnt = 0; // Caller counts
duke@435 506 if (ProfileInterpreter) {
duke@435 507 invcnt = next_m->interpreter_invocation_count();
duke@435 508 }
duke@435 509 int cnt = 0; // Call site counts
duke@435 510 if (ProfileInterpreter && next_m->method_data() != NULL) {
duke@435 511 ResourceMark rm;
duke@435 512 int bci = next->top_vframe()->bci();
duke@435 513 ProfileData* data = next_m->method_data()->bci_to_data(bci);
duke@435 514 if (data != NULL && data->is_CounterData())
duke@435 515 cnt = data->as_CounterData()->count();
duke@435 516 }
duke@435 517
duke@435 518 // Caller counts / call-site counts; i.e. is this call site
duke@435 519 // a hot call site for method next_m?
duke@435 520 int freq = (invcnt) ? cnt/invcnt : cnt;
duke@435 521
duke@435 522 // Check size and frequency limits
duke@435 523 if ((msg = shouldInline(m, freq, cnt)) != NULL) {
duke@435 524 break;
duke@435 525 }
duke@435 526 // Check inlining negative tests
duke@435 527 if ((msg = shouldNotInline(m)) != NULL) {
duke@435 528 break;
duke@435 529 }
duke@435 530
duke@435 531
duke@435 532 // If the caller method is too big or something then we do not want to
duke@435 533 // compile it just to inline a method
iveresov@2138 534 if (!can_be_compiled(next_m)) {
duke@435 535 msg = "caller cannot be compiled";
duke@435 536 break;
duke@435 537 }
duke@435 538
duke@435 539 if( next_m->name() == vmSymbols::class_initializer_name() ) {
duke@435 540 msg = "do not compile class initializer (OSR ok)";
duke@435 541 break;
duke@435 542 }
duke@435 543
duke@435 544 if (TraceCompilationPolicy && Verbose) {
duke@435 545 tty->print("\n\t check caller: ");
duke@435 546 next_m->print_short_name(tty);
duke@435 547 tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", (address)next_m(), next_m->code_size());
duke@435 548 }
duke@435 549
duke@435 550 current = next;
duke@435 551 }
duke@435 552
duke@435 553 assert( !current || !current->is_compiled(), "" );
duke@435 554
duke@435 555 if (TraceCompilationPolicy && msg) tty->print("(%s)\n", msg);
duke@435 556
duke@435 557 return current;
duke@435 558 }
duke@435 559
duke@435 560 RFrame* StackWalkCompPolicy::senderOf(RFrame* rf, GrowableArray<RFrame*>* stack) {
duke@435 561 RFrame* sender = rf->caller();
duke@435 562 if (sender && sender->num() == stack->length()) stack->push(sender);
duke@435 563 return sender;
duke@435 564 }
duke@435 565
duke@435 566
duke@435 567 const char* StackWalkCompPolicy::shouldInline(methodHandle m, float freq, int cnt) {
duke@435 568 // Allows targeted inlining
duke@435 569 // positive filter: should send be inlined? returns NULL (--> yes)
duke@435 570 // or rejection msg
duke@435 571 int max_size = MaxInlineSize;
duke@435 572 int cost = m->code_size();
duke@435 573
duke@435 574 // Check for too many throws (and not too huge)
duke@435 575 if (m->interpreter_throwout_count() > InlineThrowCount && cost < InlineThrowMaxSize ) {
duke@435 576 return NULL;
duke@435 577 }
duke@435 578
duke@435 579 // bump the max size if the call is frequent
duke@435 580 if ((freq >= InlineFrequencyRatio) || (cnt >= InlineFrequencyCount)) {
duke@435 581 if (TraceFrequencyInlining) {
duke@435 582 tty->print("(Inlined frequent method)\n");
duke@435 583 m->print();
duke@435 584 }
duke@435 585 max_size = FreqInlineSize;
duke@435 586 }
duke@435 587 if (cost > max_size) {
duke@435 588 return (_msg = "too big");
duke@435 589 }
duke@435 590 return NULL;
duke@435 591 }
duke@435 592
duke@435 593
duke@435 594 const char* StackWalkCompPolicy::shouldNotInline(methodHandle m) {
duke@435 595 // negative filter: should send NOT be inlined? returns NULL (--> inline) or rejection msg
duke@435 596 if (m->is_abstract()) return (_msg = "abstract method");
duke@435 597 // note: we allow ik->is_abstract()
duke@435 598 if (!instanceKlass::cast(m->method_holder())->is_initialized()) return (_msg = "method holder not initialized");
duke@435 599 if (m->is_native()) return (_msg = "native method");
duke@435 600 nmethod* m_code = m->code();
twisti@2103 601 if (m_code != NULL && m_code->code_size() > InlineSmallCode)
duke@435 602 return (_msg = "already compiled into a big method");
duke@435 603
duke@435 604 // use frequency-based objections only for non-trivial methods
duke@435 605 if (m->code_size() <= MaxTrivialSize) return NULL;
duke@435 606 if (UseInterpreter) { // don't use counts with -Xcomp
duke@435 607 if ((m->code() == NULL) && m->was_never_executed()) return (_msg = "never executed");
duke@435 608 if (!m->was_executed_more_than(MIN2(MinInliningThreshold, CompileThreshold >> 1))) return (_msg = "executed < MinInliningThreshold times");
duke@435 609 }
duke@435 610 if (methodOopDesc::has_unloaded_classes_in_signature(m, JavaThread::current())) return (_msg = "unloaded signature classes");
duke@435 611
duke@435 612 return NULL;
duke@435 613 }
duke@435 614
duke@435 615
duke@435 616
duke@435 617 #endif // COMPILER2

mercurial