methodData.cpp 35.9 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * 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.
 *
19 20 21
 * 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.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29
#include "precompiled.hpp"
#include "classfile/systemDictionary.hpp"
#include "interpreter/bytecode.hpp"
#include "interpreter/bytecodeStream.hpp"
#include "interpreter/linkResolver.hpp"
30
#include "memory/heapInspection.hpp"
31 32
#include "oops/methodData.hpp"
#include "prims/jvmtiRedefineClasses.hpp"
33 34 35
#include "runtime/compilationPolicy.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/handles.inline.hpp"
D
duke 已提交
36 37 38 39 40 41 42 43

// ==================================================================
// DataLayout
//
// Overlay for generic profiling data.

// Some types of data layouts need a length field.
bool DataLayout::needs_array_len(u1 tag) {
44
  return (tag == multi_branch_data_tag) || (tag == arg_info_data_tag);
D
duke 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58
}

// Perform generic initialization of the data.  More specific
// initialization occurs in overrides of ProfileData::post_initialize.
void DataLayout::initialize(u1 tag, u2 bci, int cell_count) {
  _header._bits = (intptr_t)0;
  _header._struct._tag = tag;
  _header._struct._bci = bci;
  for (int i = 0; i < cell_count; i++) {
    set_cell_at(i, (intptr_t)0);
  }
  if (needs_array_len(tag)) {
    set_cell_at(ArrayData::array_len_off_set, cell_count - 1); // -1 for header.
  }
59 60 61 62 63
  if (tag == call_type_data_tag) {
    CallTypeData::initialize(this, cell_count);
  } else if (tag == virtual_call_type_data_tag) {
    VirtualCallTypeData::initialize(this, cell_count);
  }
D
duke 已提交
64 65
}

66
void DataLayout::clean_weak_klass_links(BoolObjectClosure* cl) {
Y
ysr 已提交
67
  ResourceMark m;
68
  data_in()->clean_weak_klass_links(cl);
Y
ysr 已提交
69 70 71
}


D
duke 已提交
72 73 74 75 76 77 78 79 80 81 82 83
// ==================================================================
// ProfileData
//
// A ProfileData object is created to refer to a section of profiling
// data in a structured way.

// Constructor for invalid ProfileData.
ProfileData::ProfileData() {
  _data = NULL;
}

#ifndef PRODUCT
84
void ProfileData::print_shared(outputStream* st, const char* name) const {
D
duke 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98
  st->print("bci: %d", bci());
  st->fill_to(tab_width_one);
  st->print("%s", name);
  tab(st);
  int trap = trap_state();
  if (trap != 0) {
    char buf[100];
    st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
  }
  int flags = data()->flags();
  if (flags != 0)
    st->print("flags(%d) ", flags);
}

99 100
void ProfileData::tab(outputStream* st, bool first) const {
  st->fill_to(first ? tab_width_one : tab_width_two);
D
duke 已提交
101 102 103 104 105 106 107 108 109 110 111
}
#endif // !PRODUCT

// ==================================================================
// BitData
//
// A BitData corresponds to a one-bit flag.  This is used to indicate
// whether a checkcast bytecode has seen a null value.


#ifndef PRODUCT
112
void BitData::print_data_on(outputStream* st) const {
D
duke 已提交
113 114 115 116 117 118 119 120 121 122
  print_shared(st, "BitData");
}
#endif // !PRODUCT

// ==================================================================
// CounterData
//
// A CounterData corresponds to a simple counter.

#ifndef PRODUCT
123
void CounterData::print_data_on(outputStream* st) const {
D
duke 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136
  print_shared(st, "CounterData");
  st->print_cr("count(%u)", count());
}
#endif // !PRODUCT

// ==================================================================
// JumpData
//
// A JumpData is used to access profiling information for a direct
// branch.  It is a counter, used for counting the number of branches,
// plus a data displacement, used for realigning the data pointer to
// the corresponding target bci.

137
void JumpData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
D
duke 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  assert(stream->bci() == bci(), "wrong pos");
  int target;
  Bytecodes::Code c = stream->code();
  if (c == Bytecodes::_goto_w || c == Bytecodes::_jsr_w) {
    target = stream->dest_w();
  } else {
    target = stream->dest();
  }
  int my_di = mdo->dp_to_di(dp());
  int target_di = mdo->bci_to_di(target);
  int offset = target_di - my_di;
  set_displacement(offset);
}

#ifndef PRODUCT
153
void JumpData::print_data_on(outputStream* st) const {
D
duke 已提交
154 155 156 157 158
  print_shared(st, "JumpData");
  st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
}
#endif // !PRODUCT

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
int TypeStackSlotEntries::compute_cell_count(BytecodeStream* stream) {
  int max = TypeProfileArgsLimit;
  assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
  Bytecode_invoke inv(stream->method(), stream->bci());

  ResourceMark rm;
  SignatureStream ss(inv.signature());
  int args_count = MIN2(ss.reference_parameter_count(), max);

  return args_count * per_arg_cell_count + (args_count > 0 ? header_cell_count() : 0);
}

class ArgumentOffsetComputer : public SignatureInfo {
private:
  int _max;
  GrowableArray<int> _offsets;

  void set(int size, BasicType type) { _size += size; }
  void do_object(int begin, int end) {
    if (_offsets.length() < _max) {
      _offsets.push(_size);
    }
    SignatureInfo::do_object(begin, end);
  }
  void do_array (int begin, int end) {
    if (_offsets.length() < _max) {
      _offsets.push(_size);
    }
    SignatureInfo::do_array(begin, end);
  }

public:
  ArgumentOffsetComputer(Symbol* signature, int max)
    : SignatureInfo(signature), _max(max), _offsets(Thread::current(), max) {
  }

  int total() { lazy_iterate_parameters(); return _size; }

  int off_at(int i) const { return _offsets.at(i); }
};

void TypeStackSlotEntries::post_initialize(BytecodeStream* stream) {
  ResourceMark rm;

  assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
  Bytecode_invoke inv(stream->method(), stream->bci());

#ifdef ASSERT
  SignatureStream ss(inv.signature());
  int count = MIN2(ss.reference_parameter_count(), (int)TypeProfileArgsLimit);
  assert(count > 0, "room for args type but none found?");
  check_number_of_arguments(count);
#endif

  int start = 0;
  ArgumentOffsetComputer aos(inv.signature(), number_of_arguments()-start);
  aos.total();
  bool has_receiver = inv.has_receiver();
  for (int i = start; i < number_of_arguments(); i++) {
    set_stack_slot(i, aos.off_at(i-start) + (has_receiver ? 1 : 0));
    set_type(i, type_none());
  }
}

bool TypeEntries::is_loader_alive(BoolObjectClosure* is_alive_cl, intptr_t p) {
  return !is_type_none(p) &&
    !((Klass*)klass_part(p))->is_loader_alive(is_alive_cl);
}

void TypeStackSlotEntries::clean_weak_klass_links(BoolObjectClosure* is_alive_cl) {
  for (int i = 0; i < number_of_arguments(); i++) {
    intptr_t p = type(i);
    if (is_loader_alive(is_alive_cl, p)) {
      set_type(i, type_none());
    }
  }
}

bool TypeStackSlotEntries::arguments_profiling_enabled() {
  return MethodData::profile_arguments();
}

#ifndef PRODUCT
void TypeEntries::print_klass(outputStream* st, intptr_t k) {
  if (is_type_none(k)) {
    st->print("none");
  } else if (is_type_unknown(k)) {
    st->print("unknown");
  } else {
    valid_klass(k)->print_value_on(st);
  }
  if (was_null_seen(k)) {
    st->print(" (null seen)");
  }
}

void TypeStackSlotEntries::print_data_on(outputStream* st) const {
  _pd->tab(st, true);
  st->print("argument types");
  for (int i = 0; i < number_of_arguments(); i++) {
    _pd->tab(st);
    st->print("%d: stack(%u) ", i, stack_slot(i));
    print_klass(st, type(i));
    st->cr();
  }
}

void CallTypeData::print_data_on(outputStream* st) const {
  CounterData::print_data_on(st);
  _args.print_data_on(st);
}

void VirtualCallTypeData::print_data_on(outputStream* st) const {
  VirtualCallData::print_data_on(st);
  _args.print_data_on(st);
}
#endif

D
duke 已提交
277 278 279 280 281
// ==================================================================
// ReceiverTypeData
//
// A ReceiverTypeData is used to access profiling information about a
// dynamic type check.  It consists of a counter which counts the total times
282
// that the check is reached, and a series of (Klass*, count) pairs
D
duke 已提交
283 284
// which are used to store a type profile for the receiver of the check.

285
void ReceiverTypeData::clean_weak_klass_links(BoolObjectClosure* is_alive_cl) {
Y
ysr 已提交
286
    for (uint row = 0; row < row_limit(); row++) {
287 288
    Klass* p = receiver(row);
    if (p != NULL && !p->is_loader_alive(is_alive_cl)) {
Y
ysr 已提交
289 290 291 292 293
      clear_row(row);
    }
  }
}

D
duke 已提交
294
#ifndef PRODUCT
295
void ReceiverTypeData::print_receiver_data_on(outputStream* st) const {
D
duke 已提交
296 297 298 299 300 301
  uint row;
  int entries = 0;
  for (row = 0; row < row_limit(); row++) {
    if (receiver(row) != NULL)  entries++;
  }
  st->print_cr("count(%u) entries(%u)", count(), entries);
I
iveresov 已提交
302 303 304 305 306 307
  int total = count();
  for (row = 0; row < row_limit(); row++) {
    if (receiver(row) != NULL) {
      total += receiver_count(row);
    }
  }
D
duke 已提交
308 309 310 311
  for (row = 0; row < row_limit(); row++) {
    if (receiver(row) != NULL) {
      tab(st);
      receiver(row)->print_value_on(st);
I
iveresov 已提交
312
      st->print_cr("(%u %4.2f)", receiver_count(row), (float) receiver_count(row) / (float) total);
D
duke 已提交
313 314 315
    }
  }
}
316
void ReceiverTypeData::print_data_on(outputStream* st) const {
D
duke 已提交
317 318 319
  print_shared(st, "ReceiverTypeData");
  print_receiver_data_on(st);
}
320
void VirtualCallData::print_data_on(outputStream* st) const {
D
duke 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334
  print_shared(st, "VirtualCallData");
  print_receiver_data_on(st);
}
#endif // !PRODUCT

// ==================================================================
// RetData
//
// A RetData is used to access profiling information for a ret bytecode.
// It is composed of a count of the number of times that the ret has
// been executed, followed by a series of triples of the form
// (bci, count, di) which count the number of times that some bci was the
// target of the ret and cache a corresponding displacement.

335
void RetData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
D
duke 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  for (uint row = 0; row < row_limit(); row++) {
    set_bci_displacement(row, -1);
    set_bci(row, no_bci);
  }
  // release so other threads see a consistent state.  bci is used as
  // a valid flag for bci_displacement.
  OrderAccess::release();
}

// This routine needs to atomically update the RetData structure, so the
// caller needs to hold the RetData_lock before it gets here.  Since taking
// the lock can block (and allow GC) and since RetData is a ProfileData is a
// wrapper around a derived oop, taking the lock in _this_ method will
// basically cause the 'this' pointer's _data field to contain junk after the
// lock.  We require the caller to take the lock before making the ProfileData
// structure.  Currently the only caller is InterpreterRuntime::update_mdp_for_ret
352
address RetData::fixup_ret(int return_bci, MethodData* h_mdo) {
D
duke 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
  // First find the mdp which corresponds to the return bci.
  address mdp = h_mdo->bci_to_dp(return_bci);

  // Now check to see if any of the cache slots are open.
  for (uint row = 0; row < row_limit(); row++) {
    if (bci(row) == no_bci) {
      set_bci_displacement(row, mdp - dp());
      set_bci_count(row, DataLayout::counter_increment);
      // Barrier to ensure displacement is written before the bci; allows
      // the interpreter to read displacement without fear of race condition.
      release_set_bci(row, return_bci);
      break;
    }
  }
  return mdp;
}


#ifndef PRODUCT
372
void RetData::print_data_on(outputStream* st) const {
D
duke 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  print_shared(st, "RetData");
  uint row;
  int entries = 0;
  for (row = 0; row < row_limit(); row++) {
    if (bci(row) != no_bci)  entries++;
  }
  st->print_cr("count(%u) entries(%u)", count(), entries);
  for (row = 0; row < row_limit(); row++) {
    if (bci(row) != no_bci) {
      tab(st);
      st->print_cr("bci(%d: count(%u) displacement(%d))",
                   bci(row), bci_count(row), bci_displacement(row));
    }
  }
}
#endif // !PRODUCT

// ==================================================================
// BranchData
//
// A BranchData is used to access profiling data for a two-way branch.
// It consists of taken and not_taken counts as well as a data displacement
// for the taken case.

397
void BranchData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
D
duke 已提交
398 399 400 401 402 403 404 405 406
  assert(stream->bci() == bci(), "wrong pos");
  int target = stream->dest();
  int my_di = mdo->dp_to_di(dp());
  int target_di = mdo->bci_to_di(target);
  int offset = target_di - my_di;
  set_displacement(offset);
}

#ifndef PRODUCT
407
void BranchData::print_data_on(outputStream* st) const {
D
duke 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
  print_shared(st, "BranchData");
  st->print_cr("taken(%u) displacement(%d)",
               taken(), displacement());
  tab(st);
  st->print_cr("not taken(%u)", not_taken());
}
#endif

// ==================================================================
// MultiBranchData
//
// A MultiBranchData is used to access profiling information for
// a multi-way branch (*switch bytecodes).  It consists of a series
// of (count, displacement) pairs, which count the number of times each
// case was taken and specify the data displacment for each branch target.

int MultiBranchData::compute_cell_count(BytecodeStream* stream) {
  int cell_count = 0;
  if (stream->code() == Bytecodes::_tableswitch) {
427 428
    Bytecode_tableswitch sw(stream->method()(), stream->bcp());
    cell_count = 1 + per_case_cell_count * (1 + sw.length()); // 1 for default
D
duke 已提交
429
  } else {
430 431
    Bytecode_lookupswitch sw(stream->method()(), stream->bcp());
    cell_count = 1 + per_case_cell_count * (sw.number_of_pairs() + 1); // 1 for default
D
duke 已提交
432 433 434 435 436
  }
  return cell_count;
}

void MultiBranchData::post_initialize(BytecodeStream* stream,
437
                                      MethodData* mdo) {
D
duke 已提交
438 439 440 441 442 443
  assert(stream->bci() == bci(), "wrong pos");
  int target;
  int my_di;
  int target_di;
  int offset;
  if (stream->code() == Bytecodes::_tableswitch) {
444 445
    Bytecode_tableswitch sw(stream->method()(), stream->bcp());
    int len = sw.length();
D
duke 已提交
446 447
    assert(array_len() == per_case_cell_count * (len + 1), "wrong len");
    for (int count = 0; count < len; count++) {
448
      target = sw.dest_offset_at(count) + bci();
D
duke 已提交
449 450 451 452 453
      my_di = mdo->dp_to_di(dp());
      target_di = mdo->bci_to_di(target);
      offset = target_di - my_di;
      set_displacement_at(count, offset);
    }
454
    target = sw.default_offset() + bci();
D
duke 已提交
455 456 457 458 459 460
    my_di = mdo->dp_to_di(dp());
    target_di = mdo->bci_to_di(target);
    offset = target_di - my_di;
    set_default_displacement(offset);

  } else {
461 462
    Bytecode_lookupswitch sw(stream->method()(), stream->bcp());
    int npairs = sw.number_of_pairs();
D
duke 已提交
463 464
    assert(array_len() == per_case_cell_count * (npairs + 1), "wrong len");
    for (int count = 0; count < npairs; count++) {
465 466
      LookupswitchPair pair = sw.pair_at(count);
      target = pair.offset() + bci();
D
duke 已提交
467 468 469 470 471
      my_di = mdo->dp_to_di(dp());
      target_di = mdo->bci_to_di(target);
      offset = target_di - my_di;
      set_displacement_at(count, offset);
    }
472
    target = sw.default_offset() + bci();
D
duke 已提交
473 474 475 476 477 478 479 480
    my_di = mdo->dp_to_di(dp());
    target_di = mdo->bci_to_di(target);
    offset = target_di - my_di;
    set_default_displacement(offset);
  }
}

#ifndef PRODUCT
481
void MultiBranchData::print_data_on(outputStream* st) const {
D
duke 已提交
482 483 484 485 486 487 488 489 490 491 492 493
  print_shared(st, "MultiBranchData");
  st->print_cr("default_count(%u) displacement(%d)",
               default_count(), default_displacement());
  int cases = number_of_cases();
  for (int i = 0; i < cases; i++) {
    tab(st);
    st->print_cr("count(%u) displacement(%d)",
                 count_at(i), displacement_at(i));
  }
}
#endif

494
#ifndef PRODUCT
495
void ArgInfoData::print_data_on(outputStream* st) const {
496 497 498 499 500 501 502 503 504
  print_shared(st, "ArgInfoData");
  int nargs = number_of_args();
  for (int i = 0; i < nargs; i++) {
    st->print("  0x%x", arg_modified(i));
  }
  st->cr();
}

#endif
D
duke 已提交
505
// ==================================================================
506
// MethodData*
D
duke 已提交
507
//
508
// A MethodData* holds information which has been collected about
D
duke 已提交
509 510
// a method.

511 512 513
MethodData* MethodData::allocate(ClassLoaderData* loader_data, methodHandle method, TRAPS) {
  int size = MethodData::compute_allocation_size_in_words(method);

514 515
  return new (loader_data, size, false, MetaspaceObj::MethodDataType, THREAD)
    MethodData(method(), size, CHECK_NULL);
516 517 518
}

int MethodData::bytecode_cell_count(Bytecodes::Code code) {
519 520 521
#if defined(COMPILER1) && !defined(COMPILER2)
  return no_profile_data;
#else
D
duke 已提交
522 523 524 525 526 527 528 529 530 531 532
  switch (code) {
  case Bytecodes::_checkcast:
  case Bytecodes::_instanceof:
  case Bytecodes::_aastore:
    if (TypeProfileCasts) {
      return ReceiverTypeData::static_cell_count();
    } else {
      return BitData::static_cell_count();
    }
  case Bytecodes::_invokespecial:
  case Bytecodes::_invokestatic:
533 534 535 536 537
    if (MethodData::profile_arguments()) {
      return variable_cell_count;
    } else {
      return CounterData::static_cell_count();
    }
D
duke 已提交
538 539 540 541 542 543 544
  case Bytecodes::_goto:
  case Bytecodes::_goto_w:
  case Bytecodes::_jsr:
  case Bytecodes::_jsr_w:
    return JumpData::static_cell_count();
  case Bytecodes::_invokevirtual:
  case Bytecodes::_invokeinterface:
545 546 547 548 549
    if (MethodData::profile_arguments()) {
      return variable_cell_count;
    } else {
      return VirtualCallData::static_cell_count();
    }
550
  case Bytecodes::_invokedynamic:
551 552 553 554 555
    if (MethodData::profile_arguments()) {
      return variable_cell_count;
    } else {
      return CounterData::static_cell_count();
    }
D
duke 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
  case Bytecodes::_ret:
    return RetData::static_cell_count();
  case Bytecodes::_ifeq:
  case Bytecodes::_ifne:
  case Bytecodes::_iflt:
  case Bytecodes::_ifge:
  case Bytecodes::_ifgt:
  case Bytecodes::_ifle:
  case Bytecodes::_if_icmpeq:
  case Bytecodes::_if_icmpne:
  case Bytecodes::_if_icmplt:
  case Bytecodes::_if_icmpge:
  case Bytecodes::_if_icmpgt:
  case Bytecodes::_if_icmple:
  case Bytecodes::_if_acmpeq:
  case Bytecodes::_if_acmpne:
  case Bytecodes::_ifnull:
  case Bytecodes::_ifnonnull:
    return BranchData::static_cell_count();
  case Bytecodes::_lookupswitch:
  case Bytecodes::_tableswitch:
    return variable_cell_count;
  }
  return no_profile_data;
580
#endif
D
duke 已提交
581 582 583 584
}

// Compute the size of the profiling information corresponding to
// the current bytecode.
585
int MethodData::compute_data_size(BytecodeStream* stream) {
D
duke 已提交
586 587 588 589 590
  int cell_count = bytecode_cell_count(stream->code());
  if (cell_count == no_profile_data) {
    return 0;
  }
  if (cell_count == variable_cell_count) {
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
    switch (stream->code()) {
    case Bytecodes::_lookupswitch:
    case Bytecodes::_tableswitch:
      cell_count = MultiBranchData::compute_cell_count(stream);
      break;
    case Bytecodes::_invokespecial:
    case Bytecodes::_invokestatic:
    case Bytecodes::_invokedynamic:
      assert(MethodData::profile_arguments(), "should be collecting args profile");
      if (profile_arguments_for_invoke(stream->method(), stream->bci())) {
        cell_count = CallTypeData::compute_cell_count(stream);
      } else {
        cell_count = CounterData::static_cell_count();
      }
      break;
    case Bytecodes::_invokevirtual:
    case Bytecodes::_invokeinterface: {
      assert(MethodData::profile_arguments(), "should be collecting args profile");
      if (profile_arguments_for_invoke(stream->method(), stream->bci())) {
        cell_count = VirtualCallTypeData::compute_cell_count(stream);
      } else {
        cell_count = VirtualCallData::static_cell_count();
      }
      break;
    }
    default:
      fatal("unexpected bytecode for var length profile data");
    }
D
duke 已提交
619 620 621 622 623 624 625
  }
  // Note:  cell_count might be zero, meaning that there is just
  //        a DataLayout header, with no extra cells.
  assert(cell_count >= 0, "sanity");
  return DataLayout::compute_size_in_bytes(cell_count);
}

626
int MethodData::compute_extra_data_count(int data_size, int empty_bc_count) {
D
duke 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
  if (ProfileTraps) {
    // Assume that up to 3% of BCIs with no MDP will need to allocate one.
    int extra_data_count = (uint)(empty_bc_count * 3) / 128 + 1;
    // If the method is large, let the extra BCIs grow numerous (to ~1%).
    int one_percent_of_data
      = (uint)data_size / (DataLayout::header_size_in_bytes()*128);
    if (extra_data_count < one_percent_of_data)
      extra_data_count = one_percent_of_data;
    if (extra_data_count > empty_bc_count)
      extra_data_count = empty_bc_count;  // no need for more
    return extra_data_count;
  } else {
    return 0;
  }
}

643
// Compute the size of the MethodData* necessary to store
D
duke 已提交
644
// profiling information about a given method.  Size is in bytes.
645
int MethodData::compute_allocation_size_in_bytes(methodHandle method) {
D
duke 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
  int data_size = 0;
  BytecodeStream stream(method);
  Bytecodes::Code c;
  int empty_bc_count = 0;  // number of bytecodes lacking data
  while ((c = stream.next()) >= 0) {
    int size_in_bytes = compute_data_size(&stream);
    data_size += size_in_bytes;
    if (size_in_bytes == 0)  empty_bc_count += 1;
  }
  int object_size = in_bytes(data_offset()) + data_size;

  // Add some extra DataLayout cells (at least one) to track stray traps.
  int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
  object_size += extra_data_count * DataLayout::compute_size_in_bytes(0);

661 662 663
  // Add a cell to record information about modified arguments.
  int arg_size = method->size_of_parameters();
  object_size += DataLayout::compute_size_in_bytes(arg_size+1);
664

D
duke 已提交
665 666 667
  return object_size;
}

668
// Compute the size of the MethodData* necessary to store
D
duke 已提交
669
// profiling information about a given method.  Size is in words
670
int MethodData::compute_allocation_size_in_words(methodHandle method) {
D
duke 已提交
671 672 673 674 675 676 677
  int byte_size = compute_allocation_size_in_bytes(method);
  int word_size = align_size_up(byte_size, BytesPerWord) / BytesPerWord;
  return align_object_size(word_size);
}

// Initialize an individual data segment.  Returns the size of
// the segment in bytes.
678
int MethodData::initialize_data(BytecodeStream* stream,
D
duke 已提交
679
                                       int data_index) {
680 681 682
#if defined(COMPILER1) && !defined(COMPILER2)
  return 0;
#else
D
duke 已提交
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
  int cell_count = -1;
  int tag = DataLayout::no_tag;
  DataLayout* data_layout = data_layout_at(data_index);
  Bytecodes::Code c = stream->code();
  switch (c) {
  case Bytecodes::_checkcast:
  case Bytecodes::_instanceof:
  case Bytecodes::_aastore:
    if (TypeProfileCasts) {
      cell_count = ReceiverTypeData::static_cell_count();
      tag = DataLayout::receiver_type_data_tag;
    } else {
      cell_count = BitData::static_cell_count();
      tag = DataLayout::bit_data_tag;
    }
    break;
  case Bytecodes::_invokespecial:
700 701 702 703 704 705 706 707 708 709 710 711
  case Bytecodes::_invokestatic: {
    int counter_data_cell_count = CounterData::static_cell_count();
    if (profile_arguments_for_invoke(stream->method(), stream->bci())) {
      cell_count = CallTypeData::compute_cell_count(stream);
    } else {
      cell_count = counter_data_cell_count;
    }
    if (cell_count > counter_data_cell_count) {
      tag = DataLayout::call_type_data_tag;
    } else {
      tag = DataLayout::counter_data_tag;
    }
D
duke 已提交
712
    break;
713
  }
D
duke 已提交
714 715 716 717 718 719 720 721
  case Bytecodes::_goto:
  case Bytecodes::_goto_w:
  case Bytecodes::_jsr:
  case Bytecodes::_jsr_w:
    cell_count = JumpData::static_cell_count();
    tag = DataLayout::jump_data_tag;
    break;
  case Bytecodes::_invokevirtual:
722 723 724 725 726 727 728 729 730 731 732 733
  case Bytecodes::_invokeinterface: {
    int virtual_call_data_cell_count = VirtualCallData::static_cell_count();
    if (profile_arguments_for_invoke(stream->method(), stream->bci())) {
      cell_count = VirtualCallTypeData::compute_cell_count(stream);
    } else {
      cell_count = virtual_call_data_cell_count;
    }
    if (cell_count > virtual_call_data_cell_count) {
      tag = DataLayout::virtual_call_type_data_tag;
    } else {
      tag = DataLayout::virtual_call_data_tag;
    }
D
duke 已提交
734
    break;
735 736
  }
  case Bytecodes::_invokedynamic: {
737
    // %%% should make a type profile for any invokedynamic that takes a ref argument
738 739 740 741 742 743 744 745 746 747 748
    int counter_data_cell_count = CounterData::static_cell_count();
    if (profile_arguments_for_invoke(stream->method(), stream->bci())) {
      cell_count = CallTypeData::compute_cell_count(stream);
    } else {
      cell_count = counter_data_cell_count;
    }
    if (cell_count > counter_data_cell_count) {
      tag = DataLayout::call_type_data_tag;
    } else {
      tag = DataLayout::counter_data_tag;
    }
749
    break;
750
  }
D
duke 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
  case Bytecodes::_ret:
    cell_count = RetData::static_cell_count();
    tag = DataLayout::ret_data_tag;
    break;
  case Bytecodes::_ifeq:
  case Bytecodes::_ifne:
  case Bytecodes::_iflt:
  case Bytecodes::_ifge:
  case Bytecodes::_ifgt:
  case Bytecodes::_ifle:
  case Bytecodes::_if_icmpeq:
  case Bytecodes::_if_icmpne:
  case Bytecodes::_if_icmplt:
  case Bytecodes::_if_icmpge:
  case Bytecodes::_if_icmpgt:
  case Bytecodes::_if_icmple:
  case Bytecodes::_if_acmpeq:
  case Bytecodes::_if_acmpne:
  case Bytecodes::_ifnull:
  case Bytecodes::_ifnonnull:
    cell_count = BranchData::static_cell_count();
    tag = DataLayout::branch_data_tag;
    break;
  case Bytecodes::_lookupswitch:
  case Bytecodes::_tableswitch:
    cell_count = MultiBranchData::compute_cell_count(stream);
    tag = DataLayout::multi_branch_data_tag;
    break;
  }
  assert(tag == DataLayout::multi_branch_data_tag ||
781 782 783 784 785
         (MethodData::profile_arguments() &&
          (tag == DataLayout::call_type_data_tag ||
           tag == DataLayout::counter_data_tag ||
           tag == DataLayout::virtual_call_type_data_tag ||
           tag == DataLayout::virtual_call_data_tag)) ||
D
duke 已提交
786 787 788 789 790 791 792 793 794 795
         cell_count == bytecode_cell_count(c), "cell counts must agree");
  if (cell_count >= 0) {
    assert(tag != DataLayout::no_tag, "bad tag");
    assert(bytecode_has_profile(c), "agree w/ BHP");
    data_layout->initialize(tag, stream->bci(), cell_count);
    return DataLayout::compute_size_in_bytes(cell_count);
  } else {
    assert(!bytecode_has_profile(c), "agree w/ !BHP");
    return 0;
  }
796
#endif
D
duke 已提交
797 798 799
}

// Get the data at an arbitrary (sort of) data index.
800
ProfileData* MethodData::data_at(int data_index) const {
D
duke 已提交
801 802 803 804
  if (out_of_bounds(data_index)) {
    return NULL;
  }
  DataLayout* data_layout = data_layout_at(data_index);
Y
ysr 已提交
805 806
  return data_layout->data_in();
}
D
duke 已提交
807

Y
ysr 已提交
808 809
ProfileData* DataLayout::data_in() {
  switch (tag()) {
D
duke 已提交
810 811 812 813 814
  case DataLayout::no_tag:
  default:
    ShouldNotReachHere();
    return NULL;
  case DataLayout::bit_data_tag:
Y
ysr 已提交
815
    return new BitData(this);
D
duke 已提交
816
  case DataLayout::counter_data_tag:
Y
ysr 已提交
817
    return new CounterData(this);
D
duke 已提交
818
  case DataLayout::jump_data_tag:
Y
ysr 已提交
819
    return new JumpData(this);
D
duke 已提交
820
  case DataLayout::receiver_type_data_tag:
Y
ysr 已提交
821
    return new ReceiverTypeData(this);
D
duke 已提交
822
  case DataLayout::virtual_call_data_tag:
Y
ysr 已提交
823
    return new VirtualCallData(this);
D
duke 已提交
824
  case DataLayout::ret_data_tag:
Y
ysr 已提交
825
    return new RetData(this);
D
duke 已提交
826
  case DataLayout::branch_data_tag:
Y
ysr 已提交
827
    return new BranchData(this);
D
duke 已提交
828
  case DataLayout::multi_branch_data_tag:
Y
ysr 已提交
829
    return new MultiBranchData(this);
830
  case DataLayout::arg_info_data_tag:
Y
ysr 已提交
831
    return new ArgInfoData(this);
832 833 834 835
  case DataLayout::call_type_data_tag:
    return new CallTypeData(this);
  case DataLayout::virtual_call_type_data_tag:
    return new VirtualCallTypeData(this);
D
duke 已提交
836 837 838 839
  };
}

// Iteration over data.
840
ProfileData* MethodData::next_data(ProfileData* current) const {
D
duke 已提交
841 842 843 844 845 846 847 848
  int current_index = dp_to_di(current->dp());
  int next_index = current_index + current->size_in_bytes();
  ProfileData* next = data_at(next_index);
  return next;
}

// Give each of the data entries a chance to perform specific
// data initialization.
849
void MethodData::post_initialize(BytecodeStream* stream) {
D
duke 已提交
850 851 852 853 854 855 856 857 858
  ResourceMark rm;
  ProfileData* data;
  for (data = first_data(); is_valid(data); data = next_data(data)) {
    stream->set_start(data->bci());
    stream->next();
    data->post_initialize(stream, this);
  }
}

859 860 861
// Initialize the MethodData* corresponding to a given method.
MethodData::MethodData(methodHandle method, int size, TRAPS) {
  No_Safepoint_Verifier no_safepoint;  // init function atomic wrt GC
D
duke 已提交
862 863 864
  ResourceMark rm;
  // Set the method back-pointer.
  _method = method();
I
iveresov 已提交
865

866
  init();
D
duke 已提交
867 868 869 870 871 872
  set_creation_mileage(mileage_of(method()));

  // Go through the bytecodes and allocate and initialize the
  // corresponding data cells.
  int data_size = 0;
  int empty_bc_count = 0;  // number of bytecodes lacking data
873
  _data[0] = 0;  // apparently not set below.
D
duke 已提交
874 875 876 877 878 879 880 881 882 883 884 885
  BytecodeStream stream(method);
  Bytecodes::Code c;
  while ((c = stream.next()) >= 0) {
    int size_in_bytes = initialize_data(&stream, data_size);
    data_size += size_in_bytes;
    if (size_in_bytes == 0)  empty_bc_count += 1;
  }
  _data_size = data_size;
  int object_size = in_bytes(data_offset()) + data_size;

  // Add some extra DataLayout cells (at least one) to track stray traps.
  int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
886 887 888 889 890 891 892 893 894 895 896
  int extra_size = extra_data_count * DataLayout::compute_size_in_bytes(0);

  // Add a cell to record information about modified arguments.
  // Set up _args_modified array after traps cells so that
  // the code for traps cells works.
  DataLayout *dp = data_layout_at(data_size + extra_size);

  int arg_size = method->size_of_parameters();
  dp->initialize(DataLayout::arg_info_data_tag, 0, arg_size+1);

  object_size += extra_size + DataLayout::compute_size_in_bytes(arg_size+1);
D
duke 已提交
897 898 899 900 901 902 903 904 905

  // Set an initial hint. Don't use set_hint_di() because
  // first_di() may be out of bounds if data_size is 0.
  // In that situation, _hint_di is never used, but at
  // least well-defined.
  _hint_di = first_di();

  post_initialize(&stream);

906
  set_size(object_size);
907
}
908

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
void MethodData::init() {
  _invocation_counter.init();
  _backedge_counter.init();
  _invocation_counter_start = 0;
  _backedge_counter_start = 0;
  _num_loops = 0;
  _num_blocks = 0;
  _highest_comp_level = 0;
  _highest_osr_comp_level = 0;
  _would_profile = true;

  // Initialize flags and trap history.
  _nof_decompiles = 0;
  _nof_overflow_recompiles = 0;
  _nof_overflow_traps = 0;
  clear_escape_info();
  assert(sizeof(_trap_hist) % sizeof(HeapWord) == 0, "align");
  Copy::zero_to_words((HeapWord*) &_trap_hist,
                      sizeof(_trap_hist) / sizeof(HeapWord));
D
duke 已提交
928 929 930
}

// Get a measure of how much mileage the method has on it.
931
int MethodData::mileage_of(Method* method) {
D
duke 已提交
932
  int mileage = 0;
I
iveresov 已提交
933 934 935 936 937
  if (TieredCompilation) {
    mileage = MAX2(method->invocation_count(), method->backedge_count());
  } else {
    int iic = method->interpreter_invocation_count();
    if (mileage < iic)  mileage = iic;
938 939 940 941 942 943 944 945 946 947 948
    MethodCounters* mcs = method->method_counters();
    if (mcs != NULL) {
      InvocationCounter* ic = mcs->invocation_counter();
      InvocationCounter* bc = mcs->backedge_counter();
      int icval = ic->count();
      if (ic->carry()) icval += CompileThreshold;
      if (mileage < icval)  mileage = icval;
      int bcval = bc->count();
      if (bc->carry()) bcval += CompileThreshold;
      if (mileage < bcval)  mileage = bcval;
    }
I
iveresov 已提交
949
  }
D
duke 已提交
950 951 952
  return mileage;
}

953
bool MethodData::is_mature() const {
I
iveresov 已提交
954
  return CompilationPolicy::policy()->is_mature(_method);
D
duke 已提交
955 956 957
}

// Translate a bci to its corresponding data index (di).
958
address MethodData::bci_to_dp(int bci) {
D
duke 已提交
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
  ResourceMark rm;
  ProfileData* data = data_before(bci);
  ProfileData* prev = NULL;
  for ( ; is_valid(data); data = next_data(data)) {
    if (data->bci() >= bci) {
      if (data->bci() == bci)  set_hint_di(dp_to_di(data->dp()));
      else if (prev != NULL)   set_hint_di(dp_to_di(prev->dp()));
      return data->dp();
    }
    prev = data;
  }
  return (address)limit_data_position();
}

// Translate a bci to its corresponding data, or NULL.
974
ProfileData* MethodData::bci_to_data(int bci) {
D
duke 已提交
975 976 977 978 979 980 981 982 983 984 985 986 987
  ProfileData* data = data_before(bci);
  for ( ; is_valid(data); data = next_data(data)) {
    if (data->bci() == bci) {
      set_hint_di(dp_to_di(data->dp()));
      return data;
    } else if (data->bci() > bci) {
      break;
    }
  }
  return bci_to_extra_data(bci, false);
}

// Translate a bci to its corresponding extra data, or NULL.
988
ProfileData* MethodData::bci_to_extra_data(int bci, bool create_if_missing) {
D
duke 已提交
989 990 991 992 993 994 995
  DataLayout* dp    = extra_data_base();
  DataLayout* end   = extra_data_limit();
  DataLayout* avail = NULL;
  for (; dp < end; dp = next_extra(dp)) {
    // No need for "OrderAccess::load_acquire" ops,
    // since the data structure is monotonic.
    if (dp->tag() == DataLayout::no_tag)  break;
996 997 998 999
    if (dp->tag() == DataLayout::arg_info_data_tag) {
      dp = end; // ArgInfoData is at the end of extra data section.
      break;
    }
D
duke 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
    if (dp->bci() == bci) {
      assert(dp->tag() == DataLayout::bit_data_tag, "sane");
      return new BitData(dp);
    }
  }
  if (create_if_missing && dp < end) {
    // Allocate this one.  There is no mutual exclusion,
    // so two threads could allocate different BCIs to the
    // same data layout.  This means these extra data
    // records, like most other MDO contents, must not be
    // trusted too much.
    DataLayout temp;
    temp.initialize(DataLayout::bit_data_tag, bci, 0);
    dp->release_set_header(temp.header());
    assert(dp->tag() == DataLayout::bit_data_tag, "sane");
    //NO: assert(dp->bci() == bci, "no concurrent allocation");
    return new BitData(dp);
  }
  return NULL;
}

1021
ArgInfoData *MethodData::arg_info() {
1022 1023 1024 1025 1026 1027 1028 1029 1030
  DataLayout* dp    = extra_data_base();
  DataLayout* end   = extra_data_limit();
  for (; dp < end; dp = next_extra(dp)) {
    if (dp->tag() == DataLayout::arg_info_data_tag)
      return new ArgInfoData(dp);
  }
  return NULL;
}

1031 1032
// Printing

D
duke 已提交
1033
#ifndef PRODUCT
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052

void MethodData::print_on(outputStream* st) const {
  assert(is_methodData(), "should be method data");
  st->print("method data for ");
  method()->print_value_on(st);
  st->cr();
  print_data_on(st);
}

#endif //PRODUCT

void MethodData::print_value_on(outputStream* st) const {
  assert(is_methodData(), "should be method data");
  st->print("method data for ");
  method()->print_value_on(st);
}

#ifndef PRODUCT
void MethodData::print_data_on(outputStream* st) const {
D
duke 已提交
1053 1054 1055 1056 1057 1058 1059
  ResourceMark rm;
  ProfileData* data = first_data();
  for ( ; is_valid(data); data = next_data(data)) {
    st->print("%d", dp_to_di(data->dp()));
    st->fill_to(6);
    data->print_data_on(st);
  }
1060
  st->print_cr("--- Extra data:");
D
duke 已提交
1061 1062 1063 1064 1065
  DataLayout* dp    = extra_data_base();
  DataLayout* end   = extra_data_limit();
  for (; dp < end; dp = next_extra(dp)) {
    // No need for "OrderAccess::load_acquire" ops,
    // since the data structure is monotonic.
1066 1067 1068 1069 1070 1071 1072 1073
    if (dp->tag() == DataLayout::no_tag)  continue;
    if (dp->tag() == DataLayout::bit_data_tag) {
      data = new BitData(dp);
    } else {
      assert(dp->tag() == DataLayout::arg_info_data_tag, "must be BitData or ArgInfo");
      data = new ArgInfoData(dp);
      dp = end; // ArgInfoData is at the end of extra data section.
    }
D
duke 已提交
1074 1075 1076 1077 1078 1079 1080
    st->print("%d", dp_to_di(data->dp()));
    st->fill_to(6);
    data->print_data_on(st);
  }
}
#endif

1081 1082 1083 1084 1085 1086 1087 1088 1089
#if INCLUDE_SERVICES
// Size Statistics
void MethodData::collect_statistics(KlassSizeStats *sz) const {
  int n = sz->count(this);
  sz->_method_data_bytes += n;
  sz->_method_all_bytes += n;
  sz->_rw_bytes += n;
}
#endif // INCLUDE_SERVICES
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099

// Verification

void MethodData::verify_on(outputStream* st) {
  guarantee(is_methodData(), "object must be method data");
  // guarantee(m->is_perm(), "should be in permspace");
  this->verify_data_on(st);
}

void MethodData::verify_data_on(outputStream* st) {
D
duke 已提交
1100 1101 1102
  NEEDS_CLEANUP;
  // not yet implemented.
}
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141

bool MethodData::profile_jsr292(methodHandle m, int bci) {
  if (m->is_compiled_lambda_form()) {
    return true;
  }

  Bytecode_invoke inv(m , bci);
  return inv.is_invokedynamic() || inv.is_invokehandle();
}

int MethodData::profile_arguments_flag() {
  return TypeProfileLevel;
}

bool MethodData::profile_arguments() {
  return profile_arguments_flag() > no_type_profile && profile_arguments_flag() <= type_profile_all;
}

bool MethodData::profile_arguments_jsr292_only() {
  return profile_arguments_flag() == type_profile_jsr292;
}

bool MethodData::profile_all_arguments() {
  return profile_arguments_flag() == type_profile_all;
}

bool MethodData::profile_arguments_for_invoke(methodHandle m, int bci) {
  if (!profile_arguments()) {
    return false;
  }

  if (profile_all_arguments()) {
    return true;
  }

  assert(profile_arguments_jsr292_only(), "inconsistent");
  return profile_jsr292(m, bci);
}