src/share/vm/oops/methodData.cpp

changeset 4037
da91efe96a93
parent 2571
a97fd181b813
child 4497
16fb9f942703
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/oops/methodData.cpp	Sat Sep 01 13:25:18 2012 -0400
     1.3 @@ -0,0 +1,874 @@
     1.4 +/*
     1.5 + * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "classfile/systemDictionary.hpp"
    1.30 +#include "interpreter/bytecode.hpp"
    1.31 +#include "interpreter/bytecodeStream.hpp"
    1.32 +#include "interpreter/linkResolver.hpp"
    1.33 +#include "oops/methodData.hpp"
    1.34 +#include "prims/jvmtiRedefineClasses.hpp"
    1.35 +#include "runtime/compilationPolicy.hpp"
    1.36 +#include "runtime/deoptimization.hpp"
    1.37 +#include "runtime/handles.inline.hpp"
    1.38 +
    1.39 +// ==================================================================
    1.40 +// DataLayout
    1.41 +//
    1.42 +// Overlay for generic profiling data.
    1.43 +
    1.44 +// Some types of data layouts need a length field.
    1.45 +bool DataLayout::needs_array_len(u1 tag) {
    1.46 +  return (tag == multi_branch_data_tag) || (tag == arg_info_data_tag);
    1.47 +}
    1.48 +
    1.49 +// Perform generic initialization of the data.  More specific
    1.50 +// initialization occurs in overrides of ProfileData::post_initialize.
    1.51 +void DataLayout::initialize(u1 tag, u2 bci, int cell_count) {
    1.52 +  _header._bits = (intptr_t)0;
    1.53 +  _header._struct._tag = tag;
    1.54 +  _header._struct._bci = bci;
    1.55 +  for (int i = 0; i < cell_count; i++) {
    1.56 +    set_cell_at(i, (intptr_t)0);
    1.57 +  }
    1.58 +  if (needs_array_len(tag)) {
    1.59 +    set_cell_at(ArrayData::array_len_off_set, cell_count - 1); // -1 for header.
    1.60 +  }
    1.61 +}
    1.62 +
    1.63 +void DataLayout::clean_weak_klass_links(BoolObjectClosure* cl) {
    1.64 +  ResourceMark m;
    1.65 +  data_in()->clean_weak_klass_links(cl);
    1.66 +}
    1.67 +
    1.68 +
    1.69 +// ==================================================================
    1.70 +// ProfileData
    1.71 +//
    1.72 +// A ProfileData object is created to refer to a section of profiling
    1.73 +// data in a structured way.
    1.74 +
    1.75 +// Constructor for invalid ProfileData.
    1.76 +ProfileData::ProfileData() {
    1.77 +  _data = NULL;
    1.78 +}
    1.79 +
    1.80 +#ifndef PRODUCT
    1.81 +void ProfileData::print_shared(outputStream* st, const char* name) {
    1.82 +  st->print("bci: %d", bci());
    1.83 +  st->fill_to(tab_width_one);
    1.84 +  st->print("%s", name);
    1.85 +  tab(st);
    1.86 +  int trap = trap_state();
    1.87 +  if (trap != 0) {
    1.88 +    char buf[100];
    1.89 +    st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
    1.90 +  }
    1.91 +  int flags = data()->flags();
    1.92 +  if (flags != 0)
    1.93 +    st->print("flags(%d) ", flags);
    1.94 +}
    1.95 +
    1.96 +void ProfileData::tab(outputStream* st) {
    1.97 +  st->fill_to(tab_width_two);
    1.98 +}
    1.99 +#endif // !PRODUCT
   1.100 +
   1.101 +// ==================================================================
   1.102 +// BitData
   1.103 +//
   1.104 +// A BitData corresponds to a one-bit flag.  This is used to indicate
   1.105 +// whether a checkcast bytecode has seen a null value.
   1.106 +
   1.107 +
   1.108 +#ifndef PRODUCT
   1.109 +void BitData::print_data_on(outputStream* st) {
   1.110 +  print_shared(st, "BitData");
   1.111 +}
   1.112 +#endif // !PRODUCT
   1.113 +
   1.114 +// ==================================================================
   1.115 +// CounterData
   1.116 +//
   1.117 +// A CounterData corresponds to a simple counter.
   1.118 +
   1.119 +#ifndef PRODUCT
   1.120 +void CounterData::print_data_on(outputStream* st) {
   1.121 +  print_shared(st, "CounterData");
   1.122 +  st->print_cr("count(%u)", count());
   1.123 +}
   1.124 +#endif // !PRODUCT
   1.125 +
   1.126 +// ==================================================================
   1.127 +// JumpData
   1.128 +//
   1.129 +// A JumpData is used to access profiling information for a direct
   1.130 +// branch.  It is a counter, used for counting the number of branches,
   1.131 +// plus a data displacement, used for realigning the data pointer to
   1.132 +// the corresponding target bci.
   1.133 +
   1.134 +void JumpData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
   1.135 +  assert(stream->bci() == bci(), "wrong pos");
   1.136 +  int target;
   1.137 +  Bytecodes::Code c = stream->code();
   1.138 +  if (c == Bytecodes::_goto_w || c == Bytecodes::_jsr_w) {
   1.139 +    target = stream->dest_w();
   1.140 +  } else {
   1.141 +    target = stream->dest();
   1.142 +  }
   1.143 +  int my_di = mdo->dp_to_di(dp());
   1.144 +  int target_di = mdo->bci_to_di(target);
   1.145 +  int offset = target_di - my_di;
   1.146 +  set_displacement(offset);
   1.147 +}
   1.148 +
   1.149 +#ifndef PRODUCT
   1.150 +void JumpData::print_data_on(outputStream* st) {
   1.151 +  print_shared(st, "JumpData");
   1.152 +  st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
   1.153 +}
   1.154 +#endif // !PRODUCT
   1.155 +
   1.156 +// ==================================================================
   1.157 +// ReceiverTypeData
   1.158 +//
   1.159 +// A ReceiverTypeData is used to access profiling information about a
   1.160 +// dynamic type check.  It consists of a counter which counts the total times
   1.161 +// that the check is reached, and a series of (Klass*, count) pairs
   1.162 +// which are used to store a type profile for the receiver of the check.
   1.163 +
   1.164 +void ReceiverTypeData::clean_weak_klass_links(BoolObjectClosure* is_alive_cl) {
   1.165 +    for (uint row = 0; row < row_limit(); row++) {
   1.166 +    Klass* p = receiver(row);
   1.167 +    if (p != NULL && !p->is_loader_alive(is_alive_cl)) {
   1.168 +      clear_row(row);
   1.169 +    }
   1.170 +  }
   1.171 +}
   1.172 +
   1.173 +#ifndef PRODUCT
   1.174 +void ReceiverTypeData::print_receiver_data_on(outputStream* st) {
   1.175 +  uint row;
   1.176 +  int entries = 0;
   1.177 +  for (row = 0; row < row_limit(); row++) {
   1.178 +    if (receiver(row) != NULL)  entries++;
   1.179 +  }
   1.180 +  st->print_cr("count(%u) entries(%u)", count(), entries);
   1.181 +  int total = count();
   1.182 +  for (row = 0; row < row_limit(); row++) {
   1.183 +    if (receiver(row) != NULL) {
   1.184 +      total += receiver_count(row);
   1.185 +    }
   1.186 +  }
   1.187 +  for (row = 0; row < row_limit(); row++) {
   1.188 +    if (receiver(row) != NULL) {
   1.189 +      tab(st);
   1.190 +      receiver(row)->print_value_on(st);
   1.191 +      st->print_cr("(%u %4.2f)", receiver_count(row), (float) receiver_count(row) / (float) total);
   1.192 +    }
   1.193 +  }
   1.194 +}
   1.195 +void ReceiverTypeData::print_data_on(outputStream* st) {
   1.196 +  print_shared(st, "ReceiverTypeData");
   1.197 +  print_receiver_data_on(st);
   1.198 +}
   1.199 +void VirtualCallData::print_data_on(outputStream* st) {
   1.200 +  print_shared(st, "VirtualCallData");
   1.201 +  print_receiver_data_on(st);
   1.202 +}
   1.203 +#endif // !PRODUCT
   1.204 +
   1.205 +// ==================================================================
   1.206 +// RetData
   1.207 +//
   1.208 +// A RetData is used to access profiling information for a ret bytecode.
   1.209 +// It is composed of a count of the number of times that the ret has
   1.210 +// been executed, followed by a series of triples of the form
   1.211 +// (bci, count, di) which count the number of times that some bci was the
   1.212 +// target of the ret and cache a corresponding displacement.
   1.213 +
   1.214 +void RetData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
   1.215 +  for (uint row = 0; row < row_limit(); row++) {
   1.216 +    set_bci_displacement(row, -1);
   1.217 +    set_bci(row, no_bci);
   1.218 +  }
   1.219 +  // release so other threads see a consistent state.  bci is used as
   1.220 +  // a valid flag for bci_displacement.
   1.221 +  OrderAccess::release();
   1.222 +}
   1.223 +
   1.224 +// This routine needs to atomically update the RetData structure, so the
   1.225 +// caller needs to hold the RetData_lock before it gets here.  Since taking
   1.226 +// the lock can block (and allow GC) and since RetData is a ProfileData is a
   1.227 +// wrapper around a derived oop, taking the lock in _this_ method will
   1.228 +// basically cause the 'this' pointer's _data field to contain junk after the
   1.229 +// lock.  We require the caller to take the lock before making the ProfileData
   1.230 +// structure.  Currently the only caller is InterpreterRuntime::update_mdp_for_ret
   1.231 +address RetData::fixup_ret(int return_bci, MethodData* h_mdo) {
   1.232 +  // First find the mdp which corresponds to the return bci.
   1.233 +  address mdp = h_mdo->bci_to_dp(return_bci);
   1.234 +
   1.235 +  // Now check to see if any of the cache slots are open.
   1.236 +  for (uint row = 0; row < row_limit(); row++) {
   1.237 +    if (bci(row) == no_bci) {
   1.238 +      set_bci_displacement(row, mdp - dp());
   1.239 +      set_bci_count(row, DataLayout::counter_increment);
   1.240 +      // Barrier to ensure displacement is written before the bci; allows
   1.241 +      // the interpreter to read displacement without fear of race condition.
   1.242 +      release_set_bci(row, return_bci);
   1.243 +      break;
   1.244 +    }
   1.245 +  }
   1.246 +  return mdp;
   1.247 +}
   1.248 +
   1.249 +
   1.250 +#ifndef PRODUCT
   1.251 +void RetData::print_data_on(outputStream* st) {
   1.252 +  print_shared(st, "RetData");
   1.253 +  uint row;
   1.254 +  int entries = 0;
   1.255 +  for (row = 0; row < row_limit(); row++) {
   1.256 +    if (bci(row) != no_bci)  entries++;
   1.257 +  }
   1.258 +  st->print_cr("count(%u) entries(%u)", count(), entries);
   1.259 +  for (row = 0; row < row_limit(); row++) {
   1.260 +    if (bci(row) != no_bci) {
   1.261 +      tab(st);
   1.262 +      st->print_cr("bci(%d: count(%u) displacement(%d))",
   1.263 +                   bci(row), bci_count(row), bci_displacement(row));
   1.264 +    }
   1.265 +  }
   1.266 +}
   1.267 +#endif // !PRODUCT
   1.268 +
   1.269 +// ==================================================================
   1.270 +// BranchData
   1.271 +//
   1.272 +// A BranchData is used to access profiling data for a two-way branch.
   1.273 +// It consists of taken and not_taken counts as well as a data displacement
   1.274 +// for the taken case.
   1.275 +
   1.276 +void BranchData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
   1.277 +  assert(stream->bci() == bci(), "wrong pos");
   1.278 +  int target = stream->dest();
   1.279 +  int my_di = mdo->dp_to_di(dp());
   1.280 +  int target_di = mdo->bci_to_di(target);
   1.281 +  int offset = target_di - my_di;
   1.282 +  set_displacement(offset);
   1.283 +}
   1.284 +
   1.285 +#ifndef PRODUCT
   1.286 +void BranchData::print_data_on(outputStream* st) {
   1.287 +  print_shared(st, "BranchData");
   1.288 +  st->print_cr("taken(%u) displacement(%d)",
   1.289 +               taken(), displacement());
   1.290 +  tab(st);
   1.291 +  st->print_cr("not taken(%u)", not_taken());
   1.292 +}
   1.293 +#endif
   1.294 +
   1.295 +// ==================================================================
   1.296 +// MultiBranchData
   1.297 +//
   1.298 +// A MultiBranchData is used to access profiling information for
   1.299 +// a multi-way branch (*switch bytecodes).  It consists of a series
   1.300 +// of (count, displacement) pairs, which count the number of times each
   1.301 +// case was taken and specify the data displacment for each branch target.
   1.302 +
   1.303 +int MultiBranchData::compute_cell_count(BytecodeStream* stream) {
   1.304 +  int cell_count = 0;
   1.305 +  if (stream->code() == Bytecodes::_tableswitch) {
   1.306 +    Bytecode_tableswitch sw(stream->method()(), stream->bcp());
   1.307 +    cell_count = 1 + per_case_cell_count * (1 + sw.length()); // 1 for default
   1.308 +  } else {
   1.309 +    Bytecode_lookupswitch sw(stream->method()(), stream->bcp());
   1.310 +    cell_count = 1 + per_case_cell_count * (sw.number_of_pairs() + 1); // 1 for default
   1.311 +  }
   1.312 +  return cell_count;
   1.313 +}
   1.314 +
   1.315 +void MultiBranchData::post_initialize(BytecodeStream* stream,
   1.316 +                                      MethodData* mdo) {
   1.317 +  assert(stream->bci() == bci(), "wrong pos");
   1.318 +  int target;
   1.319 +  int my_di;
   1.320 +  int target_di;
   1.321 +  int offset;
   1.322 +  if (stream->code() == Bytecodes::_tableswitch) {
   1.323 +    Bytecode_tableswitch sw(stream->method()(), stream->bcp());
   1.324 +    int len = sw.length();
   1.325 +    assert(array_len() == per_case_cell_count * (len + 1), "wrong len");
   1.326 +    for (int count = 0; count < len; count++) {
   1.327 +      target = sw.dest_offset_at(count) + bci();
   1.328 +      my_di = mdo->dp_to_di(dp());
   1.329 +      target_di = mdo->bci_to_di(target);
   1.330 +      offset = target_di - my_di;
   1.331 +      set_displacement_at(count, offset);
   1.332 +    }
   1.333 +    target = sw.default_offset() + bci();
   1.334 +    my_di = mdo->dp_to_di(dp());
   1.335 +    target_di = mdo->bci_to_di(target);
   1.336 +    offset = target_di - my_di;
   1.337 +    set_default_displacement(offset);
   1.338 +
   1.339 +  } else {
   1.340 +    Bytecode_lookupswitch sw(stream->method()(), stream->bcp());
   1.341 +    int npairs = sw.number_of_pairs();
   1.342 +    assert(array_len() == per_case_cell_count * (npairs + 1), "wrong len");
   1.343 +    for (int count = 0; count < npairs; count++) {
   1.344 +      LookupswitchPair pair = sw.pair_at(count);
   1.345 +      target = pair.offset() + bci();
   1.346 +      my_di = mdo->dp_to_di(dp());
   1.347 +      target_di = mdo->bci_to_di(target);
   1.348 +      offset = target_di - my_di;
   1.349 +      set_displacement_at(count, offset);
   1.350 +    }
   1.351 +    target = sw.default_offset() + bci();
   1.352 +    my_di = mdo->dp_to_di(dp());
   1.353 +    target_di = mdo->bci_to_di(target);
   1.354 +    offset = target_di - my_di;
   1.355 +    set_default_displacement(offset);
   1.356 +  }
   1.357 +}
   1.358 +
   1.359 +#ifndef PRODUCT
   1.360 +void MultiBranchData::print_data_on(outputStream* st) {
   1.361 +  print_shared(st, "MultiBranchData");
   1.362 +  st->print_cr("default_count(%u) displacement(%d)",
   1.363 +               default_count(), default_displacement());
   1.364 +  int cases = number_of_cases();
   1.365 +  for (int i = 0; i < cases; i++) {
   1.366 +    tab(st);
   1.367 +    st->print_cr("count(%u) displacement(%d)",
   1.368 +                 count_at(i), displacement_at(i));
   1.369 +  }
   1.370 +}
   1.371 +#endif
   1.372 +
   1.373 +#ifndef PRODUCT
   1.374 +void ArgInfoData::print_data_on(outputStream* st) {
   1.375 +  print_shared(st, "ArgInfoData");
   1.376 +  int nargs = number_of_args();
   1.377 +  for (int i = 0; i < nargs; i++) {
   1.378 +    st->print("  0x%x", arg_modified(i));
   1.379 +  }
   1.380 +  st->cr();
   1.381 +}
   1.382 +
   1.383 +#endif
   1.384 +// ==================================================================
   1.385 +// MethodData*
   1.386 +//
   1.387 +// A MethodData* holds information which has been collected about
   1.388 +// a method.
   1.389 +
   1.390 +MethodData* MethodData::allocate(ClassLoaderData* loader_data, methodHandle method, TRAPS) {
   1.391 +  int size = MethodData::compute_allocation_size_in_words(method);
   1.392 +
   1.393 +  return new (loader_data, size, false, THREAD) MethodData(method(), size, CHECK_NULL);
   1.394 +}
   1.395 +
   1.396 +int MethodData::bytecode_cell_count(Bytecodes::Code code) {
   1.397 +  switch (code) {
   1.398 +  case Bytecodes::_checkcast:
   1.399 +  case Bytecodes::_instanceof:
   1.400 +  case Bytecodes::_aastore:
   1.401 +    if (TypeProfileCasts) {
   1.402 +      return ReceiverTypeData::static_cell_count();
   1.403 +    } else {
   1.404 +      return BitData::static_cell_count();
   1.405 +    }
   1.406 +  case Bytecodes::_invokespecial:
   1.407 +  case Bytecodes::_invokestatic:
   1.408 +    return CounterData::static_cell_count();
   1.409 +  case Bytecodes::_goto:
   1.410 +  case Bytecodes::_goto_w:
   1.411 +  case Bytecodes::_jsr:
   1.412 +  case Bytecodes::_jsr_w:
   1.413 +    return JumpData::static_cell_count();
   1.414 +  case Bytecodes::_invokevirtual:
   1.415 +  case Bytecodes::_invokeinterface:
   1.416 +    return VirtualCallData::static_cell_count();
   1.417 +  case Bytecodes::_invokedynamic:
   1.418 +    return CounterData::static_cell_count();
   1.419 +  case Bytecodes::_ret:
   1.420 +    return RetData::static_cell_count();
   1.421 +  case Bytecodes::_ifeq:
   1.422 +  case Bytecodes::_ifne:
   1.423 +  case Bytecodes::_iflt:
   1.424 +  case Bytecodes::_ifge:
   1.425 +  case Bytecodes::_ifgt:
   1.426 +  case Bytecodes::_ifle:
   1.427 +  case Bytecodes::_if_icmpeq:
   1.428 +  case Bytecodes::_if_icmpne:
   1.429 +  case Bytecodes::_if_icmplt:
   1.430 +  case Bytecodes::_if_icmpge:
   1.431 +  case Bytecodes::_if_icmpgt:
   1.432 +  case Bytecodes::_if_icmple:
   1.433 +  case Bytecodes::_if_acmpeq:
   1.434 +  case Bytecodes::_if_acmpne:
   1.435 +  case Bytecodes::_ifnull:
   1.436 +  case Bytecodes::_ifnonnull:
   1.437 +    return BranchData::static_cell_count();
   1.438 +  case Bytecodes::_lookupswitch:
   1.439 +  case Bytecodes::_tableswitch:
   1.440 +    return variable_cell_count;
   1.441 +  }
   1.442 +  return no_profile_data;
   1.443 +}
   1.444 +
   1.445 +// Compute the size of the profiling information corresponding to
   1.446 +// the current bytecode.
   1.447 +int MethodData::compute_data_size(BytecodeStream* stream) {
   1.448 +  int cell_count = bytecode_cell_count(stream->code());
   1.449 +  if (cell_count == no_profile_data) {
   1.450 +    return 0;
   1.451 +  }
   1.452 +  if (cell_count == variable_cell_count) {
   1.453 +    cell_count = MultiBranchData::compute_cell_count(stream);
   1.454 +  }
   1.455 +  // Note:  cell_count might be zero, meaning that there is just
   1.456 +  //        a DataLayout header, with no extra cells.
   1.457 +  assert(cell_count >= 0, "sanity");
   1.458 +  return DataLayout::compute_size_in_bytes(cell_count);
   1.459 +}
   1.460 +
   1.461 +int MethodData::compute_extra_data_count(int data_size, int empty_bc_count) {
   1.462 +  if (ProfileTraps) {
   1.463 +    // Assume that up to 3% of BCIs with no MDP will need to allocate one.
   1.464 +    int extra_data_count = (uint)(empty_bc_count * 3) / 128 + 1;
   1.465 +    // If the method is large, let the extra BCIs grow numerous (to ~1%).
   1.466 +    int one_percent_of_data
   1.467 +      = (uint)data_size / (DataLayout::header_size_in_bytes()*128);
   1.468 +    if (extra_data_count < one_percent_of_data)
   1.469 +      extra_data_count = one_percent_of_data;
   1.470 +    if (extra_data_count > empty_bc_count)
   1.471 +      extra_data_count = empty_bc_count;  // no need for more
   1.472 +    return extra_data_count;
   1.473 +  } else {
   1.474 +    return 0;
   1.475 +  }
   1.476 +}
   1.477 +
   1.478 +// Compute the size of the MethodData* necessary to store
   1.479 +// profiling information about a given method.  Size is in bytes.
   1.480 +int MethodData::compute_allocation_size_in_bytes(methodHandle method) {
   1.481 +  int data_size = 0;
   1.482 +  BytecodeStream stream(method);
   1.483 +  Bytecodes::Code c;
   1.484 +  int empty_bc_count = 0;  // number of bytecodes lacking data
   1.485 +  while ((c = stream.next()) >= 0) {
   1.486 +    int size_in_bytes = compute_data_size(&stream);
   1.487 +    data_size += size_in_bytes;
   1.488 +    if (size_in_bytes == 0)  empty_bc_count += 1;
   1.489 +  }
   1.490 +  int object_size = in_bytes(data_offset()) + data_size;
   1.491 +
   1.492 +  // Add some extra DataLayout cells (at least one) to track stray traps.
   1.493 +  int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
   1.494 +  object_size += extra_data_count * DataLayout::compute_size_in_bytes(0);
   1.495 +
   1.496 +  // Add a cell to record information about modified arguments.
   1.497 +  int arg_size = method->size_of_parameters();
   1.498 +  object_size += DataLayout::compute_size_in_bytes(arg_size+1);
   1.499 +  return object_size;
   1.500 +}
   1.501 +
   1.502 +// Compute the size of the MethodData* necessary to store
   1.503 +// profiling information about a given method.  Size is in words
   1.504 +int MethodData::compute_allocation_size_in_words(methodHandle method) {
   1.505 +  int byte_size = compute_allocation_size_in_bytes(method);
   1.506 +  int word_size = align_size_up(byte_size, BytesPerWord) / BytesPerWord;
   1.507 +  return align_object_size(word_size);
   1.508 +}
   1.509 +
   1.510 +// Initialize an individual data segment.  Returns the size of
   1.511 +// the segment in bytes.
   1.512 +int MethodData::initialize_data(BytecodeStream* stream,
   1.513 +                                       int data_index) {
   1.514 +  int cell_count = -1;
   1.515 +  int tag = DataLayout::no_tag;
   1.516 +  DataLayout* data_layout = data_layout_at(data_index);
   1.517 +  Bytecodes::Code c = stream->code();
   1.518 +  switch (c) {
   1.519 +  case Bytecodes::_checkcast:
   1.520 +  case Bytecodes::_instanceof:
   1.521 +  case Bytecodes::_aastore:
   1.522 +    if (TypeProfileCasts) {
   1.523 +      cell_count = ReceiverTypeData::static_cell_count();
   1.524 +      tag = DataLayout::receiver_type_data_tag;
   1.525 +    } else {
   1.526 +      cell_count = BitData::static_cell_count();
   1.527 +      tag = DataLayout::bit_data_tag;
   1.528 +    }
   1.529 +    break;
   1.530 +  case Bytecodes::_invokespecial:
   1.531 +  case Bytecodes::_invokestatic:
   1.532 +    cell_count = CounterData::static_cell_count();
   1.533 +    tag = DataLayout::counter_data_tag;
   1.534 +    break;
   1.535 +  case Bytecodes::_goto:
   1.536 +  case Bytecodes::_goto_w:
   1.537 +  case Bytecodes::_jsr:
   1.538 +  case Bytecodes::_jsr_w:
   1.539 +    cell_count = JumpData::static_cell_count();
   1.540 +    tag = DataLayout::jump_data_tag;
   1.541 +    break;
   1.542 +  case Bytecodes::_invokevirtual:
   1.543 +  case Bytecodes::_invokeinterface:
   1.544 +    cell_count = VirtualCallData::static_cell_count();
   1.545 +    tag = DataLayout::virtual_call_data_tag;
   1.546 +    break;
   1.547 +  case Bytecodes::_invokedynamic:
   1.548 +    // %%% should make a type profile for any invokedynamic that takes a ref argument
   1.549 +    cell_count = CounterData::static_cell_count();
   1.550 +    tag = DataLayout::counter_data_tag;
   1.551 +    break;
   1.552 +  case Bytecodes::_ret:
   1.553 +    cell_count = RetData::static_cell_count();
   1.554 +    tag = DataLayout::ret_data_tag;
   1.555 +    break;
   1.556 +  case Bytecodes::_ifeq:
   1.557 +  case Bytecodes::_ifne:
   1.558 +  case Bytecodes::_iflt:
   1.559 +  case Bytecodes::_ifge:
   1.560 +  case Bytecodes::_ifgt:
   1.561 +  case Bytecodes::_ifle:
   1.562 +  case Bytecodes::_if_icmpeq:
   1.563 +  case Bytecodes::_if_icmpne:
   1.564 +  case Bytecodes::_if_icmplt:
   1.565 +  case Bytecodes::_if_icmpge:
   1.566 +  case Bytecodes::_if_icmpgt:
   1.567 +  case Bytecodes::_if_icmple:
   1.568 +  case Bytecodes::_if_acmpeq:
   1.569 +  case Bytecodes::_if_acmpne:
   1.570 +  case Bytecodes::_ifnull:
   1.571 +  case Bytecodes::_ifnonnull:
   1.572 +    cell_count = BranchData::static_cell_count();
   1.573 +    tag = DataLayout::branch_data_tag;
   1.574 +    break;
   1.575 +  case Bytecodes::_lookupswitch:
   1.576 +  case Bytecodes::_tableswitch:
   1.577 +    cell_count = MultiBranchData::compute_cell_count(stream);
   1.578 +    tag = DataLayout::multi_branch_data_tag;
   1.579 +    break;
   1.580 +  }
   1.581 +  assert(tag == DataLayout::multi_branch_data_tag ||
   1.582 +         cell_count == bytecode_cell_count(c), "cell counts must agree");
   1.583 +  if (cell_count >= 0) {
   1.584 +    assert(tag != DataLayout::no_tag, "bad tag");
   1.585 +    assert(bytecode_has_profile(c), "agree w/ BHP");
   1.586 +    data_layout->initialize(tag, stream->bci(), cell_count);
   1.587 +    return DataLayout::compute_size_in_bytes(cell_count);
   1.588 +  } else {
   1.589 +    assert(!bytecode_has_profile(c), "agree w/ !BHP");
   1.590 +    return 0;
   1.591 +  }
   1.592 +}
   1.593 +
   1.594 +// Get the data at an arbitrary (sort of) data index.
   1.595 +ProfileData* MethodData::data_at(int data_index) const {
   1.596 +  if (out_of_bounds(data_index)) {
   1.597 +    return NULL;
   1.598 +  }
   1.599 +  DataLayout* data_layout = data_layout_at(data_index);
   1.600 +  return data_layout->data_in();
   1.601 +}
   1.602 +
   1.603 +ProfileData* DataLayout::data_in() {
   1.604 +  switch (tag()) {
   1.605 +  case DataLayout::no_tag:
   1.606 +  default:
   1.607 +    ShouldNotReachHere();
   1.608 +    return NULL;
   1.609 +  case DataLayout::bit_data_tag:
   1.610 +    return new BitData(this);
   1.611 +  case DataLayout::counter_data_tag:
   1.612 +    return new CounterData(this);
   1.613 +  case DataLayout::jump_data_tag:
   1.614 +    return new JumpData(this);
   1.615 +  case DataLayout::receiver_type_data_tag:
   1.616 +    return new ReceiverTypeData(this);
   1.617 +  case DataLayout::virtual_call_data_tag:
   1.618 +    return new VirtualCallData(this);
   1.619 +  case DataLayout::ret_data_tag:
   1.620 +    return new RetData(this);
   1.621 +  case DataLayout::branch_data_tag:
   1.622 +    return new BranchData(this);
   1.623 +  case DataLayout::multi_branch_data_tag:
   1.624 +    return new MultiBranchData(this);
   1.625 +  case DataLayout::arg_info_data_tag:
   1.626 +    return new ArgInfoData(this);
   1.627 +  };
   1.628 +}
   1.629 +
   1.630 +// Iteration over data.
   1.631 +ProfileData* MethodData::next_data(ProfileData* current) const {
   1.632 +  int current_index = dp_to_di(current->dp());
   1.633 +  int next_index = current_index + current->size_in_bytes();
   1.634 +  ProfileData* next = data_at(next_index);
   1.635 +  return next;
   1.636 +}
   1.637 +
   1.638 +// Give each of the data entries a chance to perform specific
   1.639 +// data initialization.
   1.640 +void MethodData::post_initialize(BytecodeStream* stream) {
   1.641 +  ResourceMark rm;
   1.642 +  ProfileData* data;
   1.643 +  for (data = first_data(); is_valid(data); data = next_data(data)) {
   1.644 +    stream->set_start(data->bci());
   1.645 +    stream->next();
   1.646 +    data->post_initialize(stream, this);
   1.647 +  }
   1.648 +}
   1.649 +
   1.650 +// Initialize the MethodData* corresponding to a given method.
   1.651 +MethodData::MethodData(methodHandle method, int size, TRAPS) {
   1.652 +  No_Safepoint_Verifier no_safepoint;  // init function atomic wrt GC
   1.653 +  ResourceMark rm;
   1.654 +  // Set the method back-pointer.
   1.655 +  _method = method();
   1.656 +
   1.657 +  if (TieredCompilation) {
   1.658 +    _invocation_counter.init();
   1.659 +    _backedge_counter.init();
   1.660 +    _invocation_counter_start = 0;
   1.661 +    _backedge_counter_start = 0;
   1.662 +    _num_loops = 0;
   1.663 +    _num_blocks = 0;
   1.664 +    _highest_comp_level = 0;
   1.665 +    _highest_osr_comp_level = 0;
   1.666 +    _would_profile = true;
   1.667 +  }
   1.668 +  set_creation_mileage(mileage_of(method()));
   1.669 +
   1.670 +  // Initialize flags and trap history.
   1.671 +  _nof_decompiles = 0;
   1.672 +  _nof_overflow_recompiles = 0;
   1.673 +  _nof_overflow_traps = 0;
   1.674 +  assert(sizeof(_trap_hist) % sizeof(HeapWord) == 0, "align");
   1.675 +  Copy::zero_to_words((HeapWord*) &_trap_hist,
   1.676 +                      sizeof(_trap_hist) / sizeof(HeapWord));
   1.677 +
   1.678 +  // Go through the bytecodes and allocate and initialize the
   1.679 +  // corresponding data cells.
   1.680 +  int data_size = 0;
   1.681 +  int empty_bc_count = 0;  // number of bytecodes lacking data
   1.682 +  BytecodeStream stream(method);
   1.683 +  Bytecodes::Code c;
   1.684 +  while ((c = stream.next()) >= 0) {
   1.685 +    int size_in_bytes = initialize_data(&stream, data_size);
   1.686 +    data_size += size_in_bytes;
   1.687 +    if (size_in_bytes == 0)  empty_bc_count += 1;
   1.688 +  }
   1.689 +  _data_size = data_size;
   1.690 +  int object_size = in_bytes(data_offset()) + data_size;
   1.691 +
   1.692 +  // Add some extra DataLayout cells (at least one) to track stray traps.
   1.693 +  int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
   1.694 +  int extra_size = extra_data_count * DataLayout::compute_size_in_bytes(0);
   1.695 +
   1.696 +  // Add a cell to record information about modified arguments.
   1.697 +  // Set up _args_modified array after traps cells so that
   1.698 +  // the code for traps cells works.
   1.699 +  DataLayout *dp = data_layout_at(data_size + extra_size);
   1.700 +
   1.701 +  int arg_size = method->size_of_parameters();
   1.702 +  dp->initialize(DataLayout::arg_info_data_tag, 0, arg_size+1);
   1.703 +
   1.704 +  object_size += extra_size + DataLayout::compute_size_in_bytes(arg_size+1);
   1.705 +
   1.706 +  // Set an initial hint. Don't use set_hint_di() because
   1.707 +  // first_di() may be out of bounds if data_size is 0.
   1.708 +  // In that situation, _hint_di is never used, but at
   1.709 +  // least well-defined.
   1.710 +  _hint_di = first_di();
   1.711 +
   1.712 +  post_initialize(&stream);
   1.713 +
   1.714 +  set_size(object_size);
   1.715 +}
   1.716 +
   1.717 +// Get a measure of how much mileage the method has on it.
   1.718 +int MethodData::mileage_of(Method* method) {
   1.719 +  int mileage = 0;
   1.720 +  if (TieredCompilation) {
   1.721 +    mileage = MAX2(method->invocation_count(), method->backedge_count());
   1.722 +  } else {
   1.723 +    int iic = method->interpreter_invocation_count();
   1.724 +    if (mileage < iic)  mileage = iic;
   1.725 +    InvocationCounter* ic = method->invocation_counter();
   1.726 +    InvocationCounter* bc = method->backedge_counter();
   1.727 +    int icval = ic->count();
   1.728 +    if (ic->carry()) icval += CompileThreshold;
   1.729 +    if (mileage < icval)  mileage = icval;
   1.730 +    int bcval = bc->count();
   1.731 +    if (bc->carry()) bcval += CompileThreshold;
   1.732 +    if (mileage < bcval)  mileage = bcval;
   1.733 +  }
   1.734 +  return mileage;
   1.735 +}
   1.736 +
   1.737 +bool MethodData::is_mature() const {
   1.738 +  return CompilationPolicy::policy()->is_mature(_method);
   1.739 +}
   1.740 +
   1.741 +// Translate a bci to its corresponding data index (di).
   1.742 +address MethodData::bci_to_dp(int bci) {
   1.743 +  ResourceMark rm;
   1.744 +  ProfileData* data = data_before(bci);
   1.745 +  ProfileData* prev = NULL;
   1.746 +  for ( ; is_valid(data); data = next_data(data)) {
   1.747 +    if (data->bci() >= bci) {
   1.748 +      if (data->bci() == bci)  set_hint_di(dp_to_di(data->dp()));
   1.749 +      else if (prev != NULL)   set_hint_di(dp_to_di(prev->dp()));
   1.750 +      return data->dp();
   1.751 +    }
   1.752 +    prev = data;
   1.753 +  }
   1.754 +  return (address)limit_data_position();
   1.755 +}
   1.756 +
   1.757 +// Translate a bci to its corresponding data, or NULL.
   1.758 +ProfileData* MethodData::bci_to_data(int bci) {
   1.759 +  ProfileData* data = data_before(bci);
   1.760 +  for ( ; is_valid(data); data = next_data(data)) {
   1.761 +    if (data->bci() == bci) {
   1.762 +      set_hint_di(dp_to_di(data->dp()));
   1.763 +      return data;
   1.764 +    } else if (data->bci() > bci) {
   1.765 +      break;
   1.766 +    }
   1.767 +  }
   1.768 +  return bci_to_extra_data(bci, false);
   1.769 +}
   1.770 +
   1.771 +// Translate a bci to its corresponding extra data, or NULL.
   1.772 +ProfileData* MethodData::bci_to_extra_data(int bci, bool create_if_missing) {
   1.773 +  DataLayout* dp    = extra_data_base();
   1.774 +  DataLayout* end   = extra_data_limit();
   1.775 +  DataLayout* avail = NULL;
   1.776 +  for (; dp < end; dp = next_extra(dp)) {
   1.777 +    // No need for "OrderAccess::load_acquire" ops,
   1.778 +    // since the data structure is monotonic.
   1.779 +    if (dp->tag() == DataLayout::no_tag)  break;
   1.780 +    if (dp->tag() == DataLayout::arg_info_data_tag) {
   1.781 +      dp = end; // ArgInfoData is at the end of extra data section.
   1.782 +      break;
   1.783 +    }
   1.784 +    if (dp->bci() == bci) {
   1.785 +      assert(dp->tag() == DataLayout::bit_data_tag, "sane");
   1.786 +      return new BitData(dp);
   1.787 +    }
   1.788 +  }
   1.789 +  if (create_if_missing && dp < end) {
   1.790 +    // Allocate this one.  There is no mutual exclusion,
   1.791 +    // so two threads could allocate different BCIs to the
   1.792 +    // same data layout.  This means these extra data
   1.793 +    // records, like most other MDO contents, must not be
   1.794 +    // trusted too much.
   1.795 +    DataLayout temp;
   1.796 +    temp.initialize(DataLayout::bit_data_tag, bci, 0);
   1.797 +    dp->release_set_header(temp.header());
   1.798 +    assert(dp->tag() == DataLayout::bit_data_tag, "sane");
   1.799 +    //NO: assert(dp->bci() == bci, "no concurrent allocation");
   1.800 +    return new BitData(dp);
   1.801 +  }
   1.802 +  return NULL;
   1.803 +}
   1.804 +
   1.805 +ArgInfoData *MethodData::arg_info() {
   1.806 +  DataLayout* dp    = extra_data_base();
   1.807 +  DataLayout* end   = extra_data_limit();
   1.808 +  for (; dp < end; dp = next_extra(dp)) {
   1.809 +    if (dp->tag() == DataLayout::arg_info_data_tag)
   1.810 +      return new ArgInfoData(dp);
   1.811 +  }
   1.812 +  return NULL;
   1.813 +}
   1.814 +
   1.815 +// Printing
   1.816 +
   1.817 +#ifndef PRODUCT
   1.818 +
   1.819 +void MethodData::print_on(outputStream* st) const {
   1.820 +  assert(is_methodData(), "should be method data");
   1.821 +  st->print("method data for ");
   1.822 +  method()->print_value_on(st);
   1.823 +  st->cr();
   1.824 +  print_data_on(st);
   1.825 +}
   1.826 +
   1.827 +#endif //PRODUCT
   1.828 +
   1.829 +void MethodData::print_value_on(outputStream* st) const {
   1.830 +  assert(is_methodData(), "should be method data");
   1.831 +  st->print("method data for ");
   1.832 +  method()->print_value_on(st);
   1.833 +}
   1.834 +
   1.835 +#ifndef PRODUCT
   1.836 +void MethodData::print_data_on(outputStream* st) const {
   1.837 +  ResourceMark rm;
   1.838 +  ProfileData* data = first_data();
   1.839 +  for ( ; is_valid(data); data = next_data(data)) {
   1.840 +    st->print("%d", dp_to_di(data->dp()));
   1.841 +    st->fill_to(6);
   1.842 +    data->print_data_on(st);
   1.843 +  }
   1.844 +  st->print_cr("--- Extra data:");
   1.845 +  DataLayout* dp    = extra_data_base();
   1.846 +  DataLayout* end   = extra_data_limit();
   1.847 +  for (; dp < end; dp = next_extra(dp)) {
   1.848 +    // No need for "OrderAccess::load_acquire" ops,
   1.849 +    // since the data structure is monotonic.
   1.850 +    if (dp->tag() == DataLayout::no_tag)  continue;
   1.851 +    if (dp->tag() == DataLayout::bit_data_tag) {
   1.852 +      data = new BitData(dp);
   1.853 +    } else {
   1.854 +      assert(dp->tag() == DataLayout::arg_info_data_tag, "must be BitData or ArgInfo");
   1.855 +      data = new ArgInfoData(dp);
   1.856 +      dp = end; // ArgInfoData is at the end of extra data section.
   1.857 +    }
   1.858 +    st->print("%d", dp_to_di(data->dp()));
   1.859 +    st->fill_to(6);
   1.860 +    data->print_data_on(st);
   1.861 +  }
   1.862 +}
   1.863 +#endif
   1.864 +
   1.865 +
   1.866 +// Verification
   1.867 +
   1.868 +void MethodData::verify_on(outputStream* st) {
   1.869 +  guarantee(is_methodData(), "object must be method data");
   1.870 +  // guarantee(m->is_perm(), "should be in permspace");
   1.871 +  this->verify_data_on(st);
   1.872 +}
   1.873 +
   1.874 +void MethodData::verify_data_on(outputStream* st) {
   1.875 +  NEEDS_CLEANUP;
   1.876 +  // not yet implemented.
   1.877 +}

mercurial