src/share/vm/runtime/compilationPolicy.cpp

Thu, 24 May 2018 20:03:11 +0800

author
aoqi
date
Thu, 24 May 2018 20:03:11 +0800
changeset 8868
91ddc23482a4
parent 6876
710a3c8b516e
child 9637
eef07cd490d4
permissions
-rw-r--r--

Increase MaxHeapSize for better performance on MIPS

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

mercurial