hotspot/src/share/vm/runtime/compilationPolicy.cpp
author phh
Wed, 28 Oct 2009 16:25:51 -0400
changeset 4434 4b41e5b42f81
parent 1 489c9b5090e2
child 4645 0c5f5b94e93a
permissions -rw-r--r--
6887571: Increase default heap config sizes Summary: Apply modification of existing server heap size ergo to all collectors except CMS. Reviewed-by: jmasa, ysr, xlu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_compilationPolicy.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
CompilationPolicy* CompilationPolicy::_policy;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
elapsedTimer       CompilationPolicy::_accumulated_time;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
bool               CompilationPolicy::_in_vm_startup;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// Determine compilation policy based on command line argument
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
void compilationPolicy_init() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  CompilationPolicy::set_in_vm_startup(DelayCompilationDuringStartup);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  switch(CompilationPolicyChoice) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  case 0:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    CompilationPolicy::set_policy(new SimpleCompPolicy());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  case 1:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
#ifdef COMPILER2
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
    CompilationPolicy::set_policy(new StackWalkCompPolicy());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    Unimplemented();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
    fatal("CompilationPolicyChoice must be in the range: [0-1]");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
void CompilationPolicy::completed_vm_startup() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  if (TraceCompilationPolicy) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    tty->print("CompilationPolicy: completed vm startup.\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  _in_vm_startup = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
// Returns true if m must be compiled before executing it
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
// This is intended to force compiles for methods (usually for
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
// debugging) that would otherwise be interpreted for some reason.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
bool CompilationPolicy::mustBeCompiled(methodHandle m) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  if (m->has_compiled_code()) return false;       // already compiled
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  if (!canBeCompiled(m))      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  return !UseInterpreter ||                                              // must compile all methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
         (UseCompiler && AlwaysCompileLoopMethods && m->has_loops()); // eagerly compile loop methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
// Returns true if m is allowed to be compiled
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
bool CompilationPolicy::canBeCompiled(methodHandle m) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  if (m->is_abstract()) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  if (DontCompileHugeMethods && m->code_size() > HugeMethodLimit) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  return !m->is_not_compilable();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
void CompilationPolicy::print_time() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  tty->print_cr ("Accumulated compilationPolicy times:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  tty->print_cr ("---------------------------");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  tty->print_cr ("  Total: %3.3f sec.", _accumulated_time.seconds());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
static void trace_osr_completion(nmethod* osr_nm) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  if (TraceOnStackReplacement) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    if (osr_nm == NULL) tty->print_cr("compilation failed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    else tty->print_cr("nmethod " INTPTR_FORMAT, osr_nm);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
#endif // !PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
void CompilationPolicy::reset_counter_for_invocation_event(methodHandle m) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  // Make sure invocation and backedge counter doesn't overflow again right away
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  // as would be the case for native methods.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  // BUT also make sure the method doesn't look like it was never executed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  // Set carry bit and reduce counter's value to min(count, CompileThreshold/2).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  m->invocation_counter()->set_carry();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  m->backedge_counter()->set_carry();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  assert(!m->was_never_executed(), "don't reset to 0 -- could be mistaken for never-executed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
void CompilationPolicy::reset_counter_for_back_branch_event(methodHandle m) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  // Delay next back-branch event but pump up invocation counter to triger
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  // whole method compilation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  InvocationCounter* i = m->invocation_counter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  InvocationCounter* b = m->backedge_counter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  // Don't set invocation_counter's value too low otherwise the method will
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  // look like immature (ic < ~5300) which prevents the inlining based on
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  // the type profiling.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  i->set(i->state(), CompileThreshold);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  // Don't reset counter too low - it is used to check if OSR method is ready.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  b->set(b->state(), CompileThreshold / 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
// SimpleCompPolicy - compile current method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
void SimpleCompPolicy::method_invocation_event( methodHandle m, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  int hot_count = m->invocation_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  reset_counter_for_invocation_event(m);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  const char* comment = "count";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  if (!delayCompilationDuringStartup() && canBeCompiled(m) && UseCompiler) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    nmethod* nm = m->code();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    if (nm == NULL ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
      const char* comment = "count";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
      CompileBroker::compile_method(m, InvocationEntryBci,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
                                    m, hot_count, comment, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
#ifdef TIERED
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
      if (nm->is_compiled_by_c1()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
        const char* comment = "tier1 overflow";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
        CompileBroker::compile_method(m, InvocationEntryBci,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
                                      m, hot_count, comment, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
#endif // TIERED
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
void SimpleCompPolicy::method_back_branch_event(methodHandle m, int branch_bci, int loop_top_bci, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  int hot_count = m->backedge_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  const char* comment = "backedge_count";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  if (!m->is_not_osr_compilable() && !delayCompilationDuringStartup() && canBeCompiled(m)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    CompileBroker::compile_method(m, loop_top_bci, m, hot_count, comment, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(loop_top_bci));)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
int SimpleCompPolicy::compilation_level(methodHandle m, int branch_bci)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
#ifdef TIERED
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  if (!TieredCompilation) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    return CompLevel_highest_tier;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  if (/* m()->tier1_compile_done() && */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
     // QQQ HACK FIX ME set tier1_compile_done!!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
      !m()->is_native()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    // Grab the nmethod so it doesn't go away while it's being queried
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    nmethod* code = m()->code();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
    if (code != NULL && code->is_compiled_by_c1()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
      return CompLevel_highest_tier;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  return CompLevel_fast_compile;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  return CompLevel_highest_tier;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
#endif // TIERED
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
// StackWalkCompPolicy - walk up stack to find a suitable method to compile
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
#ifdef COMPILER2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
const char* StackWalkCompPolicy::_msg = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
// Consider m for compilation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
void StackWalkCompPolicy::method_invocation_event(methodHandle m, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  int hot_count = m->invocation_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  reset_counter_for_invocation_event(m);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  const char* comment = "count";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  if (m->code() == NULL && !delayCompilationDuringStartup() && canBeCompiled(m) && UseCompiler) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    ResourceMark rm(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    JavaThread *thread = (JavaThread*)THREAD;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    frame       fr     = thread->last_frame();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    assert(fr.is_interpreted_frame(), "must be interpreted");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
    assert(fr.interpreter_frame_method() == m(), "bad method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    if (TraceCompilationPolicy) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
      tty->print("method invocation trigger: ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
      m->print_short_name(tty);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
      tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", (address)m(), m->code_size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    RegisterMap reg_map(thread, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
    javaVFrame* triggerVF = thread->last_java_vframe(&reg_map);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
    // triggerVF is the frame that triggered its counter
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
    RFrame* first = new InterpretedRFrame(triggerVF->fr(), thread, m);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
    if (first->top_method()->code() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
      // called obsolete method/nmethod -- no need to recompile
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
      if (TraceCompilationPolicy) tty->print_cr(" --> " INTPTR_FORMAT, first->top_method()->code());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
    } else if (compilation_level(m, InvocationEntryBci) == CompLevel_fast_compile) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
      // Tier1 compilation policy avaoids stack walking.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
      CompileBroker::compile_method(m, InvocationEntryBci,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
                                    m, hot_count, comment, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
      if (TimeCompilationPolicy) accumulated_time()->start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
      GrowableArray<RFrame*>* stack = new GrowableArray<RFrame*>(50);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
      stack->push(first);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
      RFrame* top = findTopInlinableFrame(stack);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
      if (TimeCompilationPolicy) accumulated_time()->stop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
      assert(top != NULL, "findTopInlinableFrame returned null");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
      if (TraceCompilationPolicy) top->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
      CompileBroker::compile_method(top->top_method(), InvocationEntryBci,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
                                    m, hot_count, comment, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
void StackWalkCompPolicy::method_back_branch_event(methodHandle m, int branch_bci, int loop_top_bci, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  int hot_count = m->backedge_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
  const char* comment = "backedge_count";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
  if (!m->is_not_osr_compilable() && !delayCompilationDuringStartup() && canBeCompiled(m)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
    CompileBroker::compile_method(m, loop_top_bci, m, hot_count, comment, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
    NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(loop_top_bci));)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
int StackWalkCompPolicy::compilation_level(methodHandle m, int osr_bci)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  int comp_level = CompLevel_full_optimization;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  if (TieredCompilation && osr_bci == InvocationEntryBci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
    if (CompileTheWorld) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
      // Under CTW, the first compile is tier1, the second tier2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
      if (m->highest_tier_compile() == CompLevel_none) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
        comp_level = CompLevel_fast_compile;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
    } else if (!m->has_osr_nmethod()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
      // Before tier1 is done, use invocation_count + backedge_count to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
      // compare against the threshold.  After that, the counters may/will
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
      // be reset, so rely on the straight interpreter_invocation_count.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
      if (m->highest_tier_compile() == CompLevel_initial_compile) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
        if (m->interpreter_invocation_count() < Tier2CompileThreshold) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
          comp_level = CompLevel_fast_compile;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
      } else if (m->invocation_count() + m->backedge_count() <
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
                 Tier2CompileThreshold) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
        comp_level = CompLevel_fast_compile;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  return comp_level;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
RFrame* StackWalkCompPolicy::findTopInlinableFrame(GrowableArray<RFrame*>* stack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  // go up the stack until finding a frame that (probably) won't be inlined
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  // into its caller
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  RFrame* current = stack->at(0); // current choice for stopping
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  assert( current && !current->is_compiled(), "" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  const char* msg = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  while (1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
    // before going up the stack further, check if doing so would get us into
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
    // compiled code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
    RFrame* next = senderOf(current, stack);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
    if( !next )               // No next frame up the stack?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
      break;                  // Then compile with current frame
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
    methodHandle m = current->top_method();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
    methodHandle next_m = next->top_method();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    if (TraceCompilationPolicy && Verbose) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
      tty->print("[caller: ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
      next_m->print_short_name(tty);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
      tty->print("] ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    if( !Inline ) {           // Inlining turned off
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
      msg = "Inlining turned off";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    if (next_m->is_not_compilable()) { // Did fail to compile this before/
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
      msg = "caller not compilable";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    if (next->num() > MaxRecompilationSearchLength) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
      // don't go up too high when searching for recompilees
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
      msg = "don't go up any further: > MaxRecompilationSearchLength";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
    if (next->distance() > MaxInterpretedSearchLength) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
      // don't go up too high when searching for recompilees
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
      msg = "don't go up any further: next > MaxInterpretedSearchLength";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
    // Compiled frame above already decided not to inline;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
    // do not recompile him.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
    if (next->is_compiled()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
      msg = "not going up into optimized code";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
    // Interpreted frame above us was already compiled.  Do not force
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
    // a recompile, although if the frame above us runs long enough an
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    // OSR might still happen.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
    if( current->is_interpreted() && next_m->has_compiled_code() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
      msg = "not going up -- already compiled caller";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    // Compute how frequent this call site is.  We have current method 'm'.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
    // We know next method 'next_m' is interpreted.  Find the call site and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
    // check the various invocation counts.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
    int invcnt = 0;             // Caller counts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
    if (ProfileInterpreter) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
      invcnt = next_m->interpreter_invocation_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
    int cnt = 0;                // Call site counts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
    if (ProfileInterpreter && next_m->method_data() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
      ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
      int bci = next->top_vframe()->bci();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
      ProfileData* data = next_m->method_data()->bci_to_data(bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
      if (data != NULL && data->is_CounterData())
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
        cnt = data->as_CounterData()->count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
    // Caller counts / call-site counts; i.e. is this call site
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
    // a hot call site for method next_m?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
    int freq = (invcnt) ? cnt/invcnt : cnt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
    // Check size and frequency limits
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
    if ((msg = shouldInline(m, freq, cnt)) != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    // Check inlining negative tests
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
    if ((msg = shouldNotInline(m)) != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
    // If the caller method is too big or something then we do not want to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
    // compile it just to inline a method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
    if (!canBeCompiled(next_m)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
      msg = "caller cannot be compiled";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
    if( next_m->name() == vmSymbols::class_initializer_name() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
      msg = "do not compile class initializer (OSR ok)";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    if (TraceCompilationPolicy && Verbose) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
      tty->print("\n\t     check caller: ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
      next_m->print_short_name(tty);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
      tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", (address)next_m(), next_m->code_size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
    current = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
  assert( !current || !current->is_compiled(), "" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
  if (TraceCompilationPolicy && msg) tty->print("(%s)\n", msg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
  return current;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
RFrame* StackWalkCompPolicy::senderOf(RFrame* rf, GrowableArray<RFrame*>* stack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  RFrame* sender = rf->caller();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  if (sender && sender->num() == stack->length()) stack->push(sender);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
  return sender;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
const char* StackWalkCompPolicy::shouldInline(methodHandle m, float freq, int cnt) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
  // Allows targeted inlining
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
  // positive filter: should send be inlined?  returns NULL (--> yes)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
  // or rejection msg
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
  int max_size = MaxInlineSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  int cost = m->code_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
  // Check for too many throws (and not too huge)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
  if (m->interpreter_throwout_count() > InlineThrowCount && cost < InlineThrowMaxSize ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  // bump the max size if the call is frequent
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
  if ((freq >= InlineFrequencyRatio) || (cnt >= InlineFrequencyCount)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
    if (TraceFrequencyInlining) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
      tty->print("(Inlined frequent method)\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
      m->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
    max_size = FreqInlineSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  if (cost > max_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
    return (_msg = "too big");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
const char* StackWalkCompPolicy::shouldNotInline(methodHandle m) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  // negative filter: should send NOT be inlined?  returns NULL (--> inline) or rejection msg
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  if (m->is_abstract()) return (_msg = "abstract method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
  // note: we allow ik->is_abstract()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
  if (!instanceKlass::cast(m->method_holder())->is_initialized()) return (_msg = "method holder not initialized");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  if (m->is_native()) return (_msg = "native method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  nmethod* m_code = m->code();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  if( m_code != NULL && m_code->instructions_size() > InlineSmallCode )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
    return (_msg = "already compiled into a big method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
  // use frequency-based objections only for non-trivial methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  if (m->code_size() <= MaxTrivialSize) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
  if (UseInterpreter) {     // don't use counts with -Xcomp
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
    if ((m->code() == NULL) && m->was_never_executed()) return (_msg = "never executed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
    if (!m->was_executed_more_than(MIN2(MinInliningThreshold, CompileThreshold >> 1))) return (_msg = "executed < MinInliningThreshold times");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
  if (methodOopDesc::has_unloaded_classes_in_signature(m, JavaThread::current())) return (_msg = "unloaded signature classes");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
#endif // COMPILER2