1 /*
   2  * Copyright (c) 1997, 2010, 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  */
  24 
  25 # include "incls/_precompiled.incl"
  26 # include "incls/_invocationCounter.cpp.incl"
  27 
  28 
  29 // Implementation of InvocationCounter
  30 
  31 void InvocationCounter::init() {
  32   _counter = 0;  // reset all the bits, including the sticky carry
  33   reset();
  34 }
  35 
  36 void InvocationCounter::reset() {
  37   // Only reset the state and don't make the method look like it's never
  38   // been executed
  39   set_state(wait_for_compile);
  40 }
  41 
  42 void InvocationCounter::set_carry() {
  43   set_carry_flag();
  44   // The carry bit now indicates that this counter had achieved a very
  45   // large value.  Now reduce the value, so that the method can be
  46   // executed many more times before re-entering the VM.
  47   int old_count = count();
  48   int new_count = MIN2(old_count, (int) (CompileThreshold / 2));
  49   // prevent from going to zero, to distinguish from never-executed methods
  50   if (new_count == 0)  new_count = 1;
  51   if (old_count != new_count)  set(state(), new_count);
  52 }
  53 
  54 void InvocationCounter::set_state(State state) {
  55   assert(0 <= state && state < number_of_states, "illegal state");
  56   int init = _init[state];
  57   // prevent from going to zero, to distinguish from never-executed methods
  58   if (init == 0 && count() > 0)  init = 1;
  59   int carry = (_counter & carry_mask);    // the carry bit is sticky
  60   _counter = (init << number_of_noncount_bits) | carry | state;
  61 }
  62 
  63 
  64 void InvocationCounter::print() {
  65   tty->print_cr("invocation count: up = %d, limit = %d, carry = %s, state = %s",
  66                                    count(), limit(),
  67                                    carry() ? "true" : "false",
  68                                    state_as_string(state()));
  69 }
  70 
  71 void InvocationCounter::print_short() {
  72   tty->print(" [%d%s;%s]", count(), carry()?"+carry":"", state_as_short_string(state()));
  73 }
  74 
  75 // Initialization
  76 
  77 int                       InvocationCounter::_init  [InvocationCounter::number_of_states];
  78 InvocationCounter::Action InvocationCounter::_action[InvocationCounter::number_of_states];
  79 int                       InvocationCounter::InterpreterInvocationLimit;
  80 int                       InvocationCounter::InterpreterBackwardBranchLimit;
  81 int                       InvocationCounter::InterpreterProfileLimit;
  82 
  83 
  84 const char* InvocationCounter::state_as_string(State state) {
  85   switch (state) {
  86     case wait_for_nothing            : return "wait_for_nothing";
  87     case wait_for_compile            : return "wait_for_compile";
  88   }
  89   ShouldNotReachHere();
  90   return NULL;
  91 }
  92 
  93 const char* InvocationCounter::state_as_short_string(State state) {
  94   switch (state) {
  95     case wait_for_nothing            : return "not comp.";
  96     case wait_for_compile            : return "compileable";
  97   }
  98   ShouldNotReachHere();
  99   return NULL;
 100 }
 101 
 102 
 103 static address do_nothing(methodHandle method, TRAPS) {
 104   // dummy action for inactive invocation counters
 105   method->invocation_counter()->set_carry();
 106   method->invocation_counter()->set_state(InvocationCounter::wait_for_nothing);
 107   return NULL;
 108 }
 109 
 110 
 111 static address do_decay(methodHandle method, TRAPS) {
 112   // decay invocation counters so compilation gets delayed
 113   method->invocation_counter()->decay();
 114   return NULL;
 115 }
 116 
 117 
 118 void InvocationCounter::def(State state, int init, Action action) {
 119   assert(0 <= state && state < number_of_states, "illegal state");
 120   assert(0 <= init  && init  < count_limit, "initial value out of range");
 121   _init  [state] = init;
 122   _action[state] = action;
 123 }
 124 
 125 address dummy_invocation_counter_overflow(methodHandle m, TRAPS) {
 126   ShouldNotReachHere();
 127   return NULL;
 128 }
 129 
 130 void InvocationCounter::reinitialize(bool delay_overflow) {
 131   // define states
 132   guarantee((int)number_of_states <= (int)state_limit, "adjust number_of_state_bits");
 133   def(wait_for_nothing, 0, do_nothing);
 134   if (delay_overflow) {
 135     def(wait_for_compile, 0, do_decay);
 136   } else {
 137     def(wait_for_compile, 0, dummy_invocation_counter_overflow);
 138   }
 139 
 140   InterpreterInvocationLimit = CompileThreshold << number_of_noncount_bits;
 141   InterpreterProfileLimit = ((CompileThreshold * InterpreterProfilePercentage) / 100)<< number_of_noncount_bits;
 142 
 143   // When methodData is collected, the backward branch limit is compared against a
 144   // methodData counter, rather than an InvocationCounter.  In the former case, we
 145   // don't need the shift by number_of_noncount_bits, but we do need to adjust
 146   // the factor by which we scale the threshold.
 147   if (ProfileInterpreter) {
 148     InterpreterBackwardBranchLimit = (CompileThreshold * (OnStackReplacePercentage - InterpreterProfilePercentage)) / 100;
 149   } else {
 150     InterpreterBackwardBranchLimit = ((CompileThreshold * OnStackReplacePercentage) / 100) << number_of_noncount_bits;
 151   }
 152 
 153   assert(0 <= InterpreterBackwardBranchLimit,
 154          "OSR threshold should be non-negative");
 155   assert(0 <= InterpreterProfileLimit &&
 156          InterpreterProfileLimit <= InterpreterInvocationLimit,
 157          "profile threshold should be less than the compilation threshold "
 158          "and non-negative");
 159 }
 160 
 161 void invocationCounter_init() {
 162   InvocationCounter::reinitialize(DelayCompilationDuringStartup);
 163 }