simpleThresholdPolicy.cpp 14.3 KB
Newer Older
I
iveresov 已提交
1
/*
2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
I
iveresov 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

25 26 27 28 29 30
#include "precompiled.hpp"
#include "compiler/compileBroker.hpp"
#include "memory/resourceArea.hpp"
#include "runtime/arguments.hpp"
#include "runtime/simpleThresholdPolicy.hpp"
#include "runtime/simpleThresholdPolicy.inline.hpp"
31
#include "code/scopeDesc.hpp"
I
iveresov 已提交
32

33 34 35 36

void SimpleThresholdPolicy::print_counters(const char* prefix, methodHandle mh) {
  int invocation_count = mh->invocation_count();
  int backedge_count = mh->backedge_count();
37
  MethodData* mdh = mh->method_data();
38 39
  int mdo_invocations = 0, mdo_backedges = 0;
  int mdo_invocations_start = 0, mdo_backedges_start = 0;
40
  if (mdh != NULL) {
41 42 43 44 45
    mdo_invocations = mdh->invocation_count();
    mdo_backedges = mdh->backedge_count();
    mdo_invocations_start = mdh->invocation_count_start();
    mdo_backedges_start = mdh->backedge_count_start();
  }
46
  tty->print(" %stotal=%d,%d %smdo=%d(%d),%d(%d)", prefix,
47 48 49
      invocation_count, backedge_count, prefix,
      mdo_invocations, mdo_invocations_start,
      mdo_backedges, mdo_backedges_start);
50
  tty->print(" %smax levels=%d,%d", prefix,
51 52 53
      mh->highest_comp_level(), mh->highest_osr_comp_level());
}

I
iveresov 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
// Print an event.
void SimpleThresholdPolicy::print_event(EventType type, methodHandle mh, methodHandle imh,
                                        int bci, CompLevel level) {
  bool inlinee_event = mh() != imh();

  ttyLocker tty_lock;
  tty->print("%lf: [", os::elapsedTime());

  switch(type) {
  case CALL:
    tty->print("call");
    break;
  case LOOP:
    tty->print("loop");
    break;
  case COMPILE:
    tty->print("compile");
71
    break;
72 73
  case REMOVE_FROM_QUEUE:
    tty->print("remove-from-queue");
74
    break;
75 76
  case UPDATE_IN_QUEUE:
    tty->print("update-in-queue");
77 78 79 80
    break;
  case REPROFILE:
    tty->print("reprofile");
    break;
81 82 83
  case MAKE_NOT_ENTRANT:
    tty->print("make-not-entrant");
    break;
84 85
  default:
    tty->print("unknown");
I
iveresov 已提交
86 87
  }

88
  tty->print(" level=%d ", level);
I
iveresov 已提交
89 90 91 92 93 94 95 96 97

  ResourceMark rm;
  char *method_name = mh->name_and_sig_as_C_string();
  tty->print("[%s", method_name);
  if (inlinee_event) {
    char *inlinee_name = imh->name_and_sig_as_C_string();
    tty->print(" [%s]] ", inlinee_name);
  }
  else tty->print("] ");
98 99
  tty->print("@%d queues=%d,%d", bci, CompileBroker::queue_size(CompLevel_full_profile),
                                      CompileBroker::queue_size(CompLevel_full_optimization));
I
iveresov 已提交
100 101 102 103

  print_specific(type, mh, imh, bci, level);

  if (type != COMPILE) {
104
    print_counters("", mh);
I
iveresov 已提交
105
    if (inlinee_event) {
106
      print_counters("inlinee ", imh);
I
iveresov 已提交
107
    }
108
    tty->print(" compilable=");
I
iveresov 已提交
109 110 111 112 113
    bool need_comma = false;
    if (!mh->is_not_compilable(CompLevel_full_profile)) {
      tty->print("c1");
      need_comma = true;
    }
114 115 116 117 118
    if (!mh->is_not_osr_compilable(CompLevel_full_profile)) {
      if (need_comma) tty->print(",");
      tty->print("c1-osr");
      need_comma = true;
    }
I
iveresov 已提交
119
    if (!mh->is_not_compilable(CompLevel_full_optimization)) {
120
      if (need_comma) tty->print(",");
I
iveresov 已提交
121 122 123
      tty->print("c2");
      need_comma = true;
    }
124 125 126
    if (!mh->is_not_osr_compilable(CompLevel_full_optimization)) {
      if (need_comma) tty->print(",");
      tty->print("c2-osr");
I
iveresov 已提交
127
    }
128
    tty->print(" status=");
I
iveresov 已提交
129
    if (mh->queued_for_compilation()) {
130 131
      tty->print("in-queue");
    } else tty->print("idle");
I
iveresov 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144
  }
  tty->print_cr("]");
}

void SimpleThresholdPolicy::initialize() {
  if (FLAG_IS_DEFAULT(CICompilerCount)) {
    FLAG_SET_DEFAULT(CICompilerCount, 3);
  }
  int count = CICompilerCount;
  if (CICompilerCountPerCPU) {
    count = MAX2(log2_intptr(os::active_processor_count()), 1) * 3 / 2;
  }
  set_c1_count(MAX2(count / 3, 1));
145 146
  set_c2_count(MAX2(count - c1_count(), 1));
  FLAG_SET_ERGO(intx, CICompilerCount, c1_count() + c2_count());
I
iveresov 已提交
147 148 149 150 151 152 153 154 155
}

void SimpleThresholdPolicy::set_carry_if_necessary(InvocationCounter *counter) {
  if (!counter->carry() && counter->count() > InvocationCounter::count_limit / 2) {
    counter->set_carry_flag();
  }
}

// Set carry flags on the counters if necessary
156
void SimpleThresholdPolicy::handle_counter_overflow(Method* method) {
157
  MethodCounters *mcs = method->method_counters();
158 159 160 161
  if (mcs != NULL) {
    set_carry_if_necessary(mcs->invocation_counter());
    set_carry_if_necessary(mcs->backedge_counter());
  }
162
  MethodData* mdo = method->method_data();
I
iveresov 已提交
163 164 165 166 167 168 169 170 171 172 173
  if (mdo != NULL) {
    set_carry_if_necessary(mdo->invocation_counter());
    set_carry_if_necessary(mdo->backedge_counter());
  }
}

// Called with the queue locked and with at least one element
CompileTask* SimpleThresholdPolicy::select_task(CompileQueue* compile_queue) {
  return compile_queue->first();
}

174 175 176 177 178 179
void SimpleThresholdPolicy::reprofile(ScopeDesc* trap_scope, bool is_osr) {
  for (ScopeDesc* sd = trap_scope;; sd = sd->sender()) {
    if (PrintTieredEvents) {
      methodHandle mh(sd->method());
      print_event(REPROFILE, mh, mh, InvocationEntryBci, CompLevel_none);
    }
180
    MethodData* mdo = sd->method()->method_data();
181 182 183 184 185 186 187
    if (mdo != NULL) {
      mdo->reset_start_counters();
    }
    if (sd->is_top()) break;
  }
}

I
iveresov 已提交
188
nmethod* SimpleThresholdPolicy::event(methodHandle method, methodHandle inlinee,
189
                                      int branch_bci, int bci, CompLevel comp_level, nmethod* nm, JavaThread* thread) {
I
iveresov 已提交
190
  if (comp_level == CompLevel_none &&
191 192 193
      JvmtiExport::can_post_interpreter_events() &&
      thread->is_interp_only_mode()) {
    return NULL;
I
iveresov 已提交
194
  }
195 196 197 198
  if (CompileTheWorld || ReplayCompiles) {
    // Don't trigger other compiles in testing mode
    return NULL;
  }
I
iveresov 已提交
199 200 201 202 203 204 205 206 207 208 209 210
  nmethod *osr_nm = NULL;

  handle_counter_overflow(method());
  if (method() != inlinee()) {
    handle_counter_overflow(inlinee());
  }

  if (PrintTieredEvents) {
    print_event(bci == InvocationEntryBci ? CALL : LOOP, method, inlinee, bci, comp_level);
  }

  if (bci == InvocationEntryBci) {
211
    method_invocation_event(method, inlinee, comp_level, nm, thread);
I
iveresov 已提交
212
  } else {
213
    method_back_branch_event(method, inlinee, bci, comp_level, nm, thread);
214 215
    // method == inlinee if the event originated in the main method
    int highest_level = inlinee->highest_osr_comp_level();
I
iveresov 已提交
216
    if (highest_level > comp_level) {
217
      osr_nm = inlinee->lookup_osr_nmethod_for(bci, highest_level, false);
I
iveresov 已提交
218 219 220 221 222 223
    }
  }
  return osr_nm;
}

// Check if the method can be compiled, change level if necessary
224
void SimpleThresholdPolicy::compile(methodHandle mh, int bci, CompLevel level, JavaThread* thread) {
225
  assert(level <= TieredStopAtLevel, "Invalid compilation level");
I
iveresov 已提交
226 227 228
  if (level == CompLevel_none) {
    return;
  }
229 230 231 232
  // Check if the method can be compiled. If it cannot be compiled with C1, continue profiling
  // in the interpreter and then compile with C2 (the transition function will request that,
  // see common() ). If the method cannot be compiled with C2 but still can with C1, compile it with
  // pure C1.
I
iveresov 已提交
233 234
  if (!can_be_compiled(mh, level)) {
    if (level == CompLevel_full_optimization && can_be_compiled(mh, CompLevel_simple)) {
235
        compile(mh, bci, CompLevel_simple, thread);
I
iveresov 已提交
236 237 238
    }
    return;
  }
239
  if (bci != InvocationEntryBci && mh->is_not_osr_compilable(level)) {
I
iveresov 已提交
240 241
    return;
  }
242
  if (!CompileBroker::compilation_is_in_queue(mh)) {
243 244 245
    if (PrintTieredEvents) {
      print_event(COMPILE, mh, mh, bci, level);
    }
246
    submit_compile(mh, bci, level, thread);
I
iveresov 已提交
247 248 249 250
  }
}

// Tell the broker to compile the method
251
void SimpleThresholdPolicy::submit_compile(methodHandle mh, int bci, CompLevel level, JavaThread* thread) {
I
iveresov 已提交
252
  int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count();
253
  CompileBroker::compile_method(mh, bci, level, mh, hot_count, "tiered", thread);
I
iveresov 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
}

// Call and loop predicates determine whether a transition to a higher
// compilation level should be performed (pointers to predicate functions
// are passed to common() transition function).
bool SimpleThresholdPolicy::loop_predicate(int i, int b, CompLevel cur_level) {
  switch(cur_level) {
  case CompLevel_none:
  case CompLevel_limited_profile: {
    return loop_predicate_helper<CompLevel_none>(i, b, 1.0);
  }
  case CompLevel_full_profile: {
    return loop_predicate_helper<CompLevel_full_profile>(i, b, 1.0);
  }
  default:
    return true;
  }
}

bool SimpleThresholdPolicy::call_predicate(int i, int b, CompLevel cur_level) {
  switch(cur_level) {
  case CompLevel_none:
  case CompLevel_limited_profile: {
    return call_predicate_helper<CompLevel_none>(i, b, 1.0);
  }
  case CompLevel_full_profile: {
    return call_predicate_helper<CompLevel_full_profile>(i, b, 1.0);
  }
  default:
    return true;
  }
}

// Determine is a method is mature.
288
bool SimpleThresholdPolicy::is_mature(Method* method) {
I
iveresov 已提交
289
  if (is_trivial(method)) return true;
290
  MethodData* mdo = method->method_data();
I
iveresov 已提交
291 292 293 294 295 296 297 298 299 300 301
  if (mdo != NULL) {
    int i = mdo->invocation_count();
    int b = mdo->backedge_count();
    double k = ProfileMaturityPercentage / 100.0;
    return call_predicate_helper<CompLevel_full_profile>(i, b, k) ||
           loop_predicate_helper<CompLevel_full_profile>(i, b, k);
  }
  return false;
}

// Common transition function. Given a predicate determines if a method should transition to another level.
302
CompLevel SimpleThresholdPolicy::common(Predicate p, Method* method, CompLevel cur_level) {
I
iveresov 已提交
303 304 305 306
  CompLevel next_level = cur_level;
  int i = method->invocation_count();
  int b = method->backedge_count();

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
  if (is_trivial(method)) {
    next_level = CompLevel_simple;
  } else {
    switch(cur_level) {
    case CompLevel_none:
      // If we were at full profile level, would we switch to full opt?
      if (common(p, method, CompLevel_full_profile) == CompLevel_full_optimization) {
        next_level = CompLevel_full_optimization;
      } else if ((this->*p)(i, b, cur_level)) {
        next_level = CompLevel_full_profile;
      }
      break;
    case CompLevel_limited_profile:
    case CompLevel_full_profile:
      {
322
        MethodData* mdo = method->method_data();
323 324 325 326 327 328 329 330
        if (mdo != NULL) {
          if (mdo->would_profile()) {
            int mdo_i = mdo->invocation_count_delta();
            int mdo_b = mdo->backedge_count_delta();
            if ((this->*p)(mdo_i, mdo_b, cur_level)) {
              next_level = CompLevel_full_optimization;
            }
          } else {
331 332
            next_level = CompLevel_full_optimization;
          }
I
iveresov 已提交
333 334
        }
      }
335
      break;
I
iveresov 已提交
336 337
    }
  }
338
  return MIN2(next_level, (CompLevel)TieredStopAtLevel);
I
iveresov 已提交
339 340 341
}

// Determine if a method should be compiled with a normal entry point at a different level.
342
CompLevel SimpleThresholdPolicy::call_event(Method* method,  CompLevel cur_level) {
343 344
  CompLevel osr_level = MIN2((CompLevel) method->highest_osr_comp_level(),
                             common(&SimpleThresholdPolicy::loop_predicate, method, cur_level));
I
iveresov 已提交
345 346 347 348 349 350
  CompLevel next_level = common(&SimpleThresholdPolicy::call_predicate, method, cur_level);

  // If OSR method level is greater than the regular method level, the levels should be
  // equalized by raising the regular method level in order to avoid OSRs during each
  // invocation of the method.
  if (osr_level == CompLevel_full_optimization && cur_level == CompLevel_full_profile) {
351
    MethodData* mdo = method->method_data();
I
iveresov 已提交
352 353 354 355 356 357 358 359 360 361 362 363
    guarantee(mdo != NULL, "MDO should not be NULL");
    if (mdo->invocation_count() >= 1) {
      next_level = CompLevel_full_optimization;
    }
  } else {
    next_level = MAX2(osr_level, next_level);
  }

  return next_level;
}

// Determine if we should do an OSR compilation of a given method.
364
CompLevel SimpleThresholdPolicy::loop_event(Method* method, CompLevel cur_level) {
365
  CompLevel next_level = common(&SimpleThresholdPolicy::loop_predicate, method, cur_level);
I
iveresov 已提交
366 367 368
  if (cur_level == CompLevel_none) {
    // If there is a live OSR method that means that we deopted to the interpreter
    // for the transition.
369
    CompLevel osr_level = MIN2((CompLevel)method->highest_osr_comp_level(), next_level);
I
iveresov 已提交
370 371 372 373
    if (osr_level > CompLevel_none) {
      return osr_level;
    }
  }
374
  return next_level;
I
iveresov 已提交
375 376 377 378 379
}


// Handle the invocation event.
void SimpleThresholdPolicy::method_invocation_event(methodHandle mh, methodHandle imh,
380
                                              CompLevel level, nmethod* nm, JavaThread* thread) {
381
  if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh)) {
I
iveresov 已提交
382 383
    CompLevel next_level = call_event(mh(), level);
    if (next_level != level) {
384
      compile(mh, InvocationEntryBci, next_level, thread);
I
iveresov 已提交
385 386 387 388 389 390 391
    }
  }
}

// Handle the back branch event. Notice that we can compile the method
// with a regular entry from here.
void SimpleThresholdPolicy::method_back_branch_event(methodHandle mh, methodHandle imh,
392
                                                     int bci, CompLevel level, nmethod* nm, JavaThread* thread) {
I
iveresov 已提交
393
  // If the method is already compiling, quickly bail out.
394 395
  if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh)) {
    // Use loop event as an opportunity to also check there's been
I
iveresov 已提交
396 397 398 399 400 401 402 403 404
    // enough calls.
    CompLevel cur_level = comp_level(mh());
    CompLevel next_level = call_event(mh(), cur_level);
    CompLevel next_osr_level = loop_event(mh(), level);

    next_level = MAX2(next_level,
                      next_osr_level < CompLevel_full_optimization ? next_osr_level : cur_level);
    bool is_compiling = false;
    if (next_level != cur_level) {
405
      compile(mh, InvocationEntryBci, next_level, thread);
I
iveresov 已提交
406 407 408 409 410
      is_compiling = true;
    }

    // Do the OSR version
    if (!is_compiling && next_osr_level != level) {
411
      compile(mh, bci, next_osr_level, thread);
I
iveresov 已提交
412 413 414
    }
  }
}