invocationCounter.cpp revision 1879:f95d63e2154a
1254721Semaste/*
2254721Semaste * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3254721Semaste * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4254721Semaste *
5254721Semaste * This code is free software; you can redistribute it and/or modify it
6254721Semaste * under the terms of the GNU General Public License version 2 only, as
7254721Semaste * published by the Free Software Foundation.
8254721Semaste *
9254721Semaste * This code is distributed in the hope that it will be useful, but WITHOUT
10254721Semaste * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11254721Semaste * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12254721Semaste * version 2 for more details (a copy is included in the LICENSE file that
13254721Semaste * accompanied this code).
14254721Semaste *
15254721Semaste * You should have received a copy of the GNU General Public License version
16254721Semaste * 2 along with this work; if not, write to the Free Software Foundation,
17254721Semaste * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18254721Semaste *
19254721Semaste * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20254721Semaste * or visit www.oracle.com if you need additional information or have any
21254721Semaste * questions.
22254721Semaste *
23254721Semaste */
24254721Semaste
25254721Semaste#include "precompiled.hpp"
26254721Semaste#include "interpreter/invocationCounter.hpp"
27254721Semaste#include "runtime/frame.hpp"
28254721Semaste#include "runtime/handles.inline.hpp"
29254721Semaste
30254721Semaste
31254721Semaste// Implementation of InvocationCounter
32254721Semaste
33254721Semastevoid InvocationCounter::init() {
34254721Semaste  _counter = 0;  // reset all the bits, including the sticky carry
35254721Semaste  reset();
36254721Semaste}
37254721Semaste
38254721Semastevoid InvocationCounter::reset() {
39254721Semaste  // Only reset the state and don't make the method look like it's never
40254721Semaste  // been executed
41254721Semaste  set_state(wait_for_compile);
42254721Semaste}
43254721Semaste
44254721Semastevoid InvocationCounter::set_carry() {
45254721Semaste  set_carry_flag();
46254721Semaste  // The carry bit now indicates that this counter had achieved a very
47254721Semaste  // large value.  Now reduce the value, so that the method can be
48254721Semaste  // executed many more times before re-entering the VM.
49254721Semaste  int old_count = count();
50254721Semaste  int new_count = MIN2(old_count, (int) (CompileThreshold / 2));
51254721Semaste  // prevent from going to zero, to distinguish from never-executed methods
52254721Semaste  if (new_count == 0)  new_count = 1;
53254721Semaste  if (old_count != new_count)  set(state(), new_count);
54254721Semaste}
55254721Semaste
56254721Semastevoid InvocationCounter::set_state(State state) {
57254721Semaste  assert(0 <= state && state < number_of_states, "illegal state");
58254721Semaste  int init = _init[state];
59254721Semaste  // prevent from going to zero, to distinguish from never-executed methods
60254721Semaste  if (init == 0 && count() > 0)  init = 1;
61254721Semaste  int carry = (_counter & carry_mask);    // the carry bit is sticky
62254721Semaste  _counter = (init << number_of_noncount_bits) | carry | state;
63254721Semaste}
64254721Semaste
65254721Semaste
66254721Semastevoid InvocationCounter::print() {
67254721Semaste  tty->print_cr("invocation count: up = %d, limit = %d, carry = %s, state = %s",
68254721Semaste                                   count(), limit(),
69254721Semaste                                   carry() ? "true" : "false",
70254721Semaste                                   state_as_string(state()));
71254721Semaste}
72254721Semaste
73254721Semastevoid InvocationCounter::print_short() {
74254721Semaste  tty->print(" [%d%s;%s]", count(), carry()?"+carry":"", state_as_short_string(state()));
75254721Semaste}
76254721Semaste
77254721Semaste// Initialization
78254721Semaste
79254721Semasteint                       InvocationCounter::_init  [InvocationCounter::number_of_states];
80254721SemasteInvocationCounter::Action InvocationCounter::_action[InvocationCounter::number_of_states];
81254721Semasteint                       InvocationCounter::InterpreterInvocationLimit;
82254721Semasteint                       InvocationCounter::InterpreterBackwardBranchLimit;
83254721Semasteint                       InvocationCounter::InterpreterProfileLimit;
84254721Semaste
85254721Semaste
86254721Semasteconst char* InvocationCounter::state_as_string(State state) {
87254721Semaste  switch (state) {
88254721Semaste    case wait_for_nothing            : return "wait_for_nothing";
89254721Semaste    case wait_for_compile            : return "wait_for_compile";
90254721Semaste  }
91254721Semaste  ShouldNotReachHere();
92254721Semaste  return NULL;
93254721Semaste}
94254721Semaste
95254721Semasteconst char* InvocationCounter::state_as_short_string(State state) {
96254721Semaste  switch (state) {
97254721Semaste    case wait_for_nothing            : return "not comp.";
98254721Semaste    case wait_for_compile            : return "compileable";
99254721Semaste  }
100254721Semaste  ShouldNotReachHere();
101254721Semaste  return NULL;
102254721Semaste}
103254721Semaste
104254721Semaste
105254721Semastestatic address do_nothing(methodHandle method, TRAPS) {
106254721Semaste  // dummy action for inactive invocation counters
107254721Semaste  method->invocation_counter()->set_carry();
108254721Semaste  method->invocation_counter()->set_state(InvocationCounter::wait_for_nothing);
109254721Semaste  return NULL;
110254721Semaste}
111254721Semaste
112254721Semaste
113254721Semastestatic address do_decay(methodHandle method, TRAPS) {
114254721Semaste  // decay invocation counters so compilation gets delayed
115254721Semaste  method->invocation_counter()->decay();
116254721Semaste  return NULL;
117254721Semaste}
118254721Semaste
119254721Semaste
120254721Semastevoid InvocationCounter::def(State state, int init, Action action) {
121254721Semaste  assert(0 <= state && state < number_of_states, "illegal state");
122254721Semaste  assert(0 <= init  && init  < count_limit, "initial value out of range");
123254721Semaste  _init  [state] = init;
124254721Semaste  _action[state] = action;
125254721Semaste}
126254721Semaste
127254721Semasteaddress dummy_invocation_counter_overflow(methodHandle m, TRAPS) {
128254721Semaste  ShouldNotReachHere();
129254721Semaste  return NULL;
130254721Semaste}
131254721Semaste
132254721Semastevoid InvocationCounter::reinitialize(bool delay_overflow) {
133254721Semaste  // define states
134254721Semaste  guarantee((int)number_of_states <= (int)state_limit, "adjust number_of_state_bits");
135254721Semaste  def(wait_for_nothing, 0, do_nothing);
136254721Semaste  if (delay_overflow) {
137254721Semaste    def(wait_for_compile, 0, do_decay);
138254721Semaste  } else {
139254721Semaste    def(wait_for_compile, 0, dummy_invocation_counter_overflow);
140254721Semaste  }
141254721Semaste
142254721Semaste  InterpreterInvocationLimit = CompileThreshold << number_of_noncount_bits;
143254721Semaste  InterpreterProfileLimit = ((CompileThreshold * InterpreterProfilePercentage) / 100)<< number_of_noncount_bits;
144254721Semaste
145254721Semaste  // When methodData is collected, the backward branch limit is compared against a
146254721Semaste  // methodData counter, rather than an InvocationCounter.  In the former case, we
147254721Semaste  // don't need the shift by number_of_noncount_bits, but we do need to adjust
148254721Semaste  // the factor by which we scale the threshold.
149254721Semaste  if (ProfileInterpreter) {
150254721Semaste    InterpreterBackwardBranchLimit = (CompileThreshold * (OnStackReplacePercentage - InterpreterProfilePercentage)) / 100;
151254721Semaste  } else {
152254721Semaste    InterpreterBackwardBranchLimit = ((CompileThreshold * OnStackReplacePercentage) / 100) << number_of_noncount_bits;
153254721Semaste  }
154254721Semaste
155254721Semaste  assert(0 <= InterpreterBackwardBranchLimit,
156254721Semaste         "OSR threshold should be non-negative");
157254721Semaste  assert(0 <= InterpreterProfileLimit &&
158254721Semaste         InterpreterProfileLimit <= InterpreterInvocationLimit,
159254721Semaste         "profile threshold should be less than the compilation threshold "
160254721Semaste         "and non-negative");
161254721Semaste}
162254721Semaste
163254721Semastevoid invocationCounter_init() {
164254721Semaste  InvocationCounter::reinitialize(DelayCompilationDuringStartup);
165254721Semaste}
166254721Semaste