src/share/vm/runtime/compilationPolicy.cpp

Mon, 15 Apr 2013 21:25:23 -0400

author
jiangli
date
Mon, 15 Apr 2013 21:25:23 -0400
changeset 4938
8df6ddda8090
parent 4908
b84fd7d73702
parent 4937
42a42da29fd7
child 5032
d1c9384eecb4
permissions
-rw-r--r--

Merge

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

mercurial