1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)methodDataOop.cpp    1.51 07/05/29 09:44:22 JVM"
   3 #endif
   4 /*
   5  * Copyright 2000-2008 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 # include "incls/_precompiled.incl"
  29 # include "incls/_methodDataOop.cpp.incl"
  30 
  31 // ==================================================================
  32 // DataLayout
  33 //
  34 // Overlay for generic profiling data.
  35 
  36 // Some types of data layouts need a length field.
  37 bool DataLayout::needs_array_len(u1 tag) {
  38   return (tag == multi_branch_data_tag) || (tag == arg_info_data_tag);
  39 }
  40 
  41 // Perform generic initialization of the data.  More specific
  42 // initialization occurs in overrides of ProfileData::post_initialize.
  43 void DataLayout::initialize(u1 tag, u2 bci, int cell_count) {
  44   _header._bits = (intptr_t)0;
  45   _header._struct._tag = tag;
  46   _header._struct._bci = bci;
  47   for (int i = 0; i < cell_count; i++) {
  48     set_cell_at(i, (intptr_t)0);
  49   }
  50   if (needs_array_len(tag)) {
  51     set_cell_at(ArrayData::array_len_off_set, cell_count - 1); // -1 for header.
  52   }
  53 }
  54 
  55 // ==================================================================
  56 // ProfileData
  57 //
  58 // A ProfileData object is created to refer to a section of profiling
  59 // data in a structured way.
  60 
  61 // Constructor for invalid ProfileData.
  62 ProfileData::ProfileData() {
  63   _data = NULL;
  64 }
  65 
  66 #ifndef PRODUCT
  67 void ProfileData::print_shared(outputStream* st, const char* name) {
  68   st->print("bci: %d", bci());
  69   st->fill_to(tab_width_one);
  70   st->print("%s", name);
  71   tab(st);
  72   int trap = trap_state();
  73   if (trap != 0) {
  74     char buf[100];
  75     st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
  76   }
  77   int flags = data()->flags();
  78   if (flags != 0)
  79     st->print("flags(%d) ", flags);
  80 }
  81 
  82 void ProfileData::tab(outputStream* st) {
  83   st->fill_to(tab_width_two);
  84 }
  85 #endif // !PRODUCT
  86 
  87 // ==================================================================
  88 // BitData
  89 //
  90 // A BitData corresponds to a one-bit flag.  This is used to indicate
  91 // whether a checkcast bytecode has seen a null value.
  92 
  93 
  94 #ifndef PRODUCT
  95 void BitData::print_data_on(outputStream* st) {
  96   print_shared(st, "BitData");
  97 }
  98 #endif // !PRODUCT
  99 
 100 // ==================================================================
 101 // CounterData
 102 //
 103 // A CounterData corresponds to a simple counter.
 104 
 105 #ifndef PRODUCT
 106 void CounterData::print_data_on(outputStream* st) {
 107   print_shared(st, "CounterData");
 108   st->print_cr("count(%u)", count());
 109 }
 110 #endif // !PRODUCT
 111 
 112 // ==================================================================
 113 // JumpData
 114 //
 115 // A JumpData is used to access profiling information for a direct
 116 // branch.  It is a counter, used for counting the number of branches,
 117 // plus a data displacement, used for realigning the data pointer to
 118 // the corresponding target bci.
 119 
 120 void JumpData::post_initialize(BytecodeStream* stream, methodDataOop mdo) {
 121   assert(stream->bci() == bci(), "wrong pos");
 122   int target;
 123   Bytecodes::Code c = stream->code();
 124   if (c == Bytecodes::_goto_w || c == Bytecodes::_jsr_w) {
 125     target = stream->dest_w();
 126   } else {
 127     target = stream->dest();
 128   }
 129   int my_di = mdo->dp_to_di(dp());
 130   int target_di = mdo->bci_to_di(target);
 131   int offset = target_di - my_di;
 132   set_displacement(offset);
 133 }
 134 
 135 #ifndef PRODUCT
 136 void JumpData::print_data_on(outputStream* st) {
 137   print_shared(st, "JumpData");
 138   st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
 139 }
 140 #endif // !PRODUCT
 141 
 142 // ==================================================================
 143 // ReceiverTypeData
 144 //
 145 // A ReceiverTypeData is used to access profiling information about a
 146 // dynamic type check.  It consists of a counter which counts the total times
 147 // that the check is reached, and a series of (klassOop, count) pairs
 148 // which are used to store a type profile for the receiver of the check.
 149 
 150 void ReceiverTypeData::follow_contents() {
 151   for (uint row = 0; row < row_limit(); row++) {
 152     if (receiver(row) != NULL) {
 153       MarkSweep::mark_and_push(adr_receiver(row));
 154     }
 155   }
 156 }
 157 
 158 #ifndef SERIALGC
 159 void ReceiverTypeData::follow_contents(ParCompactionManager* cm) {
 160   for (uint row = 0; row < row_limit(); row++) {
 161     if (receiver(row) != NULL) {
 162       PSParallelCompact::mark_and_push(cm, adr_receiver(row));
 163     }
 164   }
 165 }
 166 #endif // SERIALGC
 167 
 168 void ReceiverTypeData::oop_iterate(OopClosure* blk) {
 169   for (uint row = 0; row < row_limit(); row++) {
 170     if (receiver(row) != NULL) {
 171       blk->do_oop(adr_receiver(row));
 172     }
 173   }
 174 }  
 175 
 176 void ReceiverTypeData::oop_iterate_m(OopClosure* blk, MemRegion mr) {
 177   for (uint row = 0; row < row_limit(); row++) {
 178     if (receiver(row) != NULL) {
 179       oop* adr = adr_receiver(row);
 180       if (mr.contains(adr)) {
 181         blk->do_oop(adr);
 182       }
 183     }
 184   }
 185 }  
 186 
 187 void ReceiverTypeData::adjust_pointers() {
 188   for (uint row = 0; row < row_limit(); row++) {
 189     if (receiver(row) != NULL) {
 190       MarkSweep::adjust_pointer(adr_receiver(row));
 191     }
 192   }
 193 }
 194 
 195 #ifndef SERIALGC
 196 void ReceiverTypeData::update_pointers() {
 197   for (uint row = 0; row < row_limit(); row++) {
 198     if (receiver_unchecked(row) != NULL) {
 199       PSParallelCompact::adjust_pointer(adr_receiver(row));
 200     }
 201   }
 202 }
 203 
 204 void ReceiverTypeData::update_pointers(HeapWord* beg_addr, HeapWord* end_addr) {
 205   // The loop bounds could be computed based on beg_addr/end_addr and the
 206   // boundary test hoisted outside the loop (see klassVTable for an example);
 207   // however, row_limit() is small enough (2) to make that less efficient.
 208   for (uint row = 0; row < row_limit(); row++) {
 209     if (receiver_unchecked(row) != NULL) {
 210       PSParallelCompact::adjust_pointer(adr_receiver(row), beg_addr, end_addr);
 211     }
 212   }
 213 }
 214 #endif // SERIALGC
 215 
 216 #ifndef PRODUCT
 217 void ReceiverTypeData::print_receiver_data_on(outputStream* st) {
 218   uint row;
 219   int entries = 0;
 220   for (row = 0; row < row_limit(); row++) {
 221     if (receiver(row) != NULL)  entries++;
 222   }
 223   st->print_cr("count(%u) entries(%u)", count(), entries);
 224   for (row = 0; row < row_limit(); row++) {
 225     if (receiver(row) != NULL) {
 226       tab(st);
 227       receiver(row)->print_value_on(st);
 228       st->print_cr("(%u)", receiver_count(row));
 229     }
 230   }
 231 }
 232 void ReceiverTypeData::print_data_on(outputStream* st) {
 233   print_shared(st, "ReceiverTypeData");
 234   print_receiver_data_on(st);
 235 }
 236 void VirtualCallData::print_data_on(outputStream* st) {
 237   print_shared(st, "VirtualCallData");
 238   print_receiver_data_on(st);
 239 }
 240 #endif // !PRODUCT
 241 
 242 // ==================================================================
 243 // RetData
 244 //
 245 // A RetData is used to access profiling information for a ret bytecode.
 246 // It is composed of a count of the number of times that the ret has
 247 // been executed, followed by a series of triples of the form
 248 // (bci, count, di) which count the number of times that some bci was the
 249 // target of the ret and cache a corresponding displacement.
 250   
 251 void RetData::post_initialize(BytecodeStream* stream, methodDataOop mdo) {
 252   for (uint row = 0; row < row_limit(); row++) {
 253     set_bci_displacement(row, -1);
 254     set_bci(row, no_bci);
 255   }
 256   // release so other threads see a consistent state.  bci is used as
 257   // a valid flag for bci_displacement.
 258   OrderAccess::release();
 259 }
 260 
 261 // This routine needs to atomically update the RetData structure, so the
 262 // caller needs to hold the RetData_lock before it gets here.  Since taking
 263 // the lock can block (and allow GC) and since RetData is a ProfileData is a
 264 // wrapper around a derived oop, taking the lock in _this_ method will
 265 // basically cause the 'this' pointer's _data field to contain junk after the
 266 // lock.  We require the caller to take the lock before making the ProfileData
 267 // structure.  Currently the only caller is InterpreterRuntime::update_mdp_for_ret
 268 address RetData::fixup_ret(int return_bci, methodDataHandle h_mdo) {
 269   // First find the mdp which corresponds to the return bci.
 270   address mdp = h_mdo->bci_to_dp(return_bci);
 271 
 272   // Now check to see if any of the cache slots are open.
 273   for (uint row = 0; row < row_limit(); row++) {
 274     if (bci(row) == no_bci) {
 275       set_bci_displacement(row, mdp - dp());
 276       set_bci_count(row, DataLayout::counter_increment);
 277       // Barrier to ensure displacement is written before the bci; allows
 278       // the interpreter to read displacement without fear of race condition.
 279       release_set_bci(row, return_bci);
 280       break;
 281     }
 282   }
 283   return mdp;
 284 }
 285 
 286 
 287 #ifndef PRODUCT
 288 void RetData::print_data_on(outputStream* st) {
 289   print_shared(st, "RetData");
 290   uint row;
 291   int entries = 0;
 292   for (row = 0; row < row_limit(); row++) {
 293     if (bci(row) != no_bci)  entries++;
 294   }
 295   st->print_cr("count(%u) entries(%u)", count(), entries);
 296   for (row = 0; row < row_limit(); row++) {
 297     if (bci(row) != no_bci) {
 298       tab(st);
 299       st->print_cr("bci(%d: count(%u) displacement(%d))",
 300                    bci(row), bci_count(row), bci_displacement(row));
 301     }
 302   }
 303 }
 304 #endif // !PRODUCT
 305 
 306 // ==================================================================
 307 // BranchData
 308 //
 309 // A BranchData is used to access profiling data for a two-way branch.
 310 // It consists of taken and not_taken counts as well as a data displacement
 311 // for the taken case.
 312 
 313 void BranchData::post_initialize(BytecodeStream* stream, methodDataOop mdo) {
 314   assert(stream->bci() == bci(), "wrong pos");
 315   int target = stream->dest();
 316   int my_di = mdo->dp_to_di(dp());
 317   int target_di = mdo->bci_to_di(target);
 318   int offset = target_di - my_di;
 319   set_displacement(offset);
 320 }
 321 
 322 #ifndef PRODUCT
 323 void BranchData::print_data_on(outputStream* st) {
 324   print_shared(st, "BranchData");
 325   st->print_cr("taken(%u) displacement(%d)",
 326                taken(), displacement());
 327   tab(st);
 328   st->print_cr("not taken(%u)", not_taken());
 329 }
 330 #endif
 331 
 332 // ==================================================================
 333 // MultiBranchData
 334 //
 335 // A MultiBranchData is used to access profiling information for
 336 // a multi-way branch (*switch bytecodes).  It consists of a series
 337 // of (count, displacement) pairs, which count the number of times each
 338 // case was taken and specify the data displacment for each branch target.
 339 
 340 int MultiBranchData::compute_cell_count(BytecodeStream* stream) {
 341   int cell_count = 0;
 342   if (stream->code() == Bytecodes::_tableswitch) {
 343     Bytecode_tableswitch* sw = Bytecode_tableswitch_at(stream->bcp());
 344     cell_count = 1 + per_case_cell_count * (1 + sw->length()); // 1 for default
 345   } else {
 346     Bytecode_lookupswitch* sw = Bytecode_lookupswitch_at(stream->bcp());
 347     cell_count = 1 + per_case_cell_count * (sw->number_of_pairs() + 1); // 1 for default
 348   }
 349   return cell_count;
 350 }
 351 
 352 void MultiBranchData::post_initialize(BytecodeStream* stream,
 353                                       methodDataOop mdo) {
 354   assert(stream->bci() == bci(), "wrong pos");
 355   int target;
 356   int my_di;
 357   int target_di;
 358   int offset;
 359   if (stream->code() == Bytecodes::_tableswitch) {
 360     Bytecode_tableswitch* sw = Bytecode_tableswitch_at(stream->bcp());
 361     int len = sw->length();
 362     assert(array_len() == per_case_cell_count * (len + 1), "wrong len");
 363     for (int count = 0; count < len; count++) {
 364       target = sw->dest_offset_at(count) + bci();
 365       my_di = mdo->dp_to_di(dp());
 366       target_di = mdo->bci_to_di(target);
 367       offset = target_di - my_di;
 368       set_displacement_at(count, offset);
 369     }         
 370     target = sw->default_offset() + bci();
 371     my_di = mdo->dp_to_di(dp());
 372     target_di = mdo->bci_to_di(target);
 373     offset = target_di - my_di;
 374     set_default_displacement(offset);
 375 
 376   } else {
 377     Bytecode_lookupswitch* sw = Bytecode_lookupswitch_at(stream->bcp());
 378     int npairs = sw->number_of_pairs();
 379     assert(array_len() == per_case_cell_count * (npairs + 1), "wrong len");
 380     for (int count = 0; count < npairs; count++) {
 381       LookupswitchPair *pair = sw->pair_at(count);
 382       target = pair->offset() + bci();
 383       my_di = mdo->dp_to_di(dp());
 384       target_di = mdo->bci_to_di(target);
 385       offset = target_di - my_di;
 386       set_displacement_at(count, offset);
 387     }
 388     target = sw->default_offset() + bci();
 389     my_di = mdo->dp_to_di(dp());
 390     target_di = mdo->bci_to_di(target);
 391     offset = target_di - my_di;
 392     set_default_displacement(offset);
 393   }
 394 }
 395 
 396 #ifndef PRODUCT
 397 void MultiBranchData::print_data_on(outputStream* st) {
 398   print_shared(st, "MultiBranchData");
 399   st->print_cr("default_count(%u) displacement(%d)",
 400                default_count(), default_displacement());
 401   int cases = number_of_cases();
 402   for (int i = 0; i < cases; i++) {
 403     tab(st);
 404     st->print_cr("count(%u) displacement(%d)",
 405                  count_at(i), displacement_at(i));
 406   }
 407 }
 408 #endif
 409 
 410 #ifndef PRODUCT
 411 void ArgInfoData::print_data_on(outputStream* st) {
 412   print_shared(st, "ArgInfoData");
 413   int nargs = number_of_args();
 414   for (int i = 0; i < nargs; i++) {
 415     st->print("  0x%x", arg_modified(i));
 416   }
 417   st->cr();
 418 }
 419 
 420 #endif
 421 // ==================================================================
 422 // methodDataOop
 423 //
 424 // A methodDataOop holds information which has been collected about
 425 // a method.
 426 
 427 int methodDataOopDesc::bytecode_cell_count(Bytecodes::Code code) {
 428   switch (code) {
 429   case Bytecodes::_checkcast:
 430   case Bytecodes::_instanceof:
 431   case Bytecodes::_aastore:
 432     if (TypeProfileCasts) {
 433       return ReceiverTypeData::static_cell_count();
 434     } else {
 435       return BitData::static_cell_count();
 436     }
 437   case Bytecodes::_invokespecial: 
 438   case Bytecodes::_invokestatic:
 439     return CounterData::static_cell_count();
 440   case Bytecodes::_goto: 
 441   case Bytecodes::_goto_w: 
 442   case Bytecodes::_jsr:
 443   case Bytecodes::_jsr_w:
 444     return JumpData::static_cell_count();
 445   case Bytecodes::_invokevirtual: 
 446   case Bytecodes::_invokeinterface:
 447     return VirtualCallData::static_cell_count();
 448   case Bytecodes::_ret:
 449     return RetData::static_cell_count();
 450   case Bytecodes::_ifeq:
 451   case Bytecodes::_ifne:
 452   case Bytecodes::_iflt:
 453   case Bytecodes::_ifge:
 454   case Bytecodes::_ifgt:
 455   case Bytecodes::_ifle:
 456   case Bytecodes::_if_icmpeq:
 457   case Bytecodes::_if_icmpne:
 458   case Bytecodes::_if_icmplt:
 459   case Bytecodes::_if_icmpge:
 460   case Bytecodes::_if_icmpgt:
 461   case Bytecodes::_if_icmple:
 462   case Bytecodes::_if_acmpeq:
 463   case Bytecodes::_if_acmpne:
 464   case Bytecodes::_ifnull:
 465   case Bytecodes::_ifnonnull:
 466     return BranchData::static_cell_count();
 467   case Bytecodes::_lookupswitch:
 468   case Bytecodes::_tableswitch:
 469     return variable_cell_count;
 470   }
 471   return no_profile_data;
 472 }
 473 
 474 // Compute the size of the profiling information corresponding to
 475 // the current bytecode.
 476 int methodDataOopDesc::compute_data_size(BytecodeStream* stream) {
 477   int cell_count = bytecode_cell_count(stream->code());
 478   if (cell_count == no_profile_data) {
 479     return 0;
 480   }
 481   if (cell_count == variable_cell_count) {
 482     cell_count = MultiBranchData::compute_cell_count(stream);
 483   }
 484   // Note:  cell_count might be zero, meaning that there is just
 485   //        a DataLayout header, with no extra cells.
 486   assert(cell_count >= 0, "sanity");
 487   return DataLayout::compute_size_in_bytes(cell_count);
 488 }
 489 
 490 int methodDataOopDesc::compute_extra_data_count(int data_size, int empty_bc_count) {
 491   if (ProfileTraps) {
 492     // Assume that up to 3% of BCIs with no MDP will need to allocate one.
 493     int extra_data_count = (uint)(empty_bc_count * 3) / 128 + 1;
 494     // If the method is large, let the extra BCIs grow numerous (to ~1%).
 495     int one_percent_of_data
 496       = (uint)data_size / (DataLayout::header_size_in_bytes()*128);
 497     if (extra_data_count < one_percent_of_data)
 498       extra_data_count = one_percent_of_data;
 499     if (extra_data_count > empty_bc_count)
 500       extra_data_count = empty_bc_count;  // no need for more
 501     return extra_data_count;
 502   } else {
 503     return 0;
 504   }
 505 }
 506 
 507 // Compute the size of the methodDataOop necessary to store
 508 // profiling information about a given method.  Size is in bytes.
 509 int methodDataOopDesc::compute_allocation_size_in_bytes(methodHandle method) {
 510   int data_size = 0;
 511   BytecodeStream stream(method);
 512   Bytecodes::Code c;
 513   int empty_bc_count = 0;  // number of bytecodes lacking data
 514   while ((c = stream.next()) >= 0) {
 515     int size_in_bytes = compute_data_size(&stream);
 516     data_size += size_in_bytes;
 517     if (size_in_bytes == 0)  empty_bc_count += 1;
 518   }
 519   int object_size = in_bytes(data_offset()) + data_size;
 520 
 521   // Add some extra DataLayout cells (at least one) to track stray traps.
 522   int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
 523   object_size += extra_data_count * DataLayout::compute_size_in_bytes(0);
 524 
 525   // Add a cell to record information about modified arguments.
 526   int arg_size = method->size_of_parameters();
 527   object_size += DataLayout::compute_size_in_bytes(arg_size+1);
 528   return object_size;
 529 }
 530 
 531 // Compute the size of the methodDataOop necessary to store
 532 // profiling information about a given method.  Size is in words
 533 int methodDataOopDesc::compute_allocation_size_in_words(methodHandle method) {
 534   int byte_size = compute_allocation_size_in_bytes(method);
 535   int word_size = align_size_up(byte_size, BytesPerWord) / BytesPerWord;
 536   return align_object_size(word_size);
 537 }
 538 
 539 // Initialize an individual data segment.  Returns the size of
 540 // the segment in bytes.
 541 int methodDataOopDesc::initialize_data(BytecodeStream* stream,
 542                                        int data_index) {
 543   int cell_count = -1;
 544   int tag = DataLayout::no_tag;
 545   DataLayout* data_layout = data_layout_at(data_index);
 546   Bytecodes::Code c = stream->code();
 547   switch (c) {
 548   case Bytecodes::_checkcast:
 549   case Bytecodes::_instanceof:
 550   case Bytecodes::_aastore:
 551     if (TypeProfileCasts) {
 552       cell_count = ReceiverTypeData::static_cell_count();
 553       tag = DataLayout::receiver_type_data_tag;
 554     } else {
 555       cell_count = BitData::static_cell_count();
 556       tag = DataLayout::bit_data_tag;
 557     }
 558     break;
 559   case Bytecodes::_invokespecial:
 560   case Bytecodes::_invokestatic:
 561     cell_count = CounterData::static_cell_count();
 562     tag = DataLayout::counter_data_tag;
 563     break;
 564   case Bytecodes::_goto:
 565   case Bytecodes::_goto_w:
 566   case Bytecodes::_jsr:
 567   case Bytecodes::_jsr_w:
 568     cell_count = JumpData::static_cell_count();
 569     tag = DataLayout::jump_data_tag;
 570     break;
 571   case Bytecodes::_invokevirtual:
 572   case Bytecodes::_invokeinterface:
 573     cell_count = VirtualCallData::static_cell_count();
 574     tag = DataLayout::virtual_call_data_tag;
 575     break;
 576   case Bytecodes::_ret:
 577     cell_count = RetData::static_cell_count();
 578     tag = DataLayout::ret_data_tag;
 579     break;
 580   case Bytecodes::_ifeq:
 581   case Bytecodes::_ifne:
 582   case Bytecodes::_iflt:
 583   case Bytecodes::_ifge:
 584   case Bytecodes::_ifgt:
 585   case Bytecodes::_ifle:
 586   case Bytecodes::_if_icmpeq:
 587   case Bytecodes::_if_icmpne:
 588   case Bytecodes::_if_icmplt:
 589   case Bytecodes::_if_icmpge:
 590   case Bytecodes::_if_icmpgt:
 591   case Bytecodes::_if_icmple:
 592   case Bytecodes::_if_acmpeq:
 593   case Bytecodes::_if_acmpne:
 594   case Bytecodes::_ifnull:
 595   case Bytecodes::_ifnonnull:
 596     cell_count = BranchData::static_cell_count();
 597     tag = DataLayout::branch_data_tag;
 598     break;
 599   case Bytecodes::_lookupswitch:
 600   case Bytecodes::_tableswitch:
 601     cell_count = MultiBranchData::compute_cell_count(stream);
 602     tag = DataLayout::multi_branch_data_tag;
 603     break;
 604   }
 605   assert(tag == DataLayout::multi_branch_data_tag ||
 606          cell_count == bytecode_cell_count(c), "cell counts must agree");
 607   if (cell_count >= 0) {
 608     assert(tag != DataLayout::no_tag, "bad tag");
 609     assert(bytecode_has_profile(c), "agree w/ BHP");
 610     data_layout->initialize(tag, stream->bci(), cell_count);
 611     return DataLayout::compute_size_in_bytes(cell_count);
 612   } else {
 613     assert(!bytecode_has_profile(c), "agree w/ !BHP");
 614     return 0;
 615   }
 616 }
 617 
 618 // Get the data at an arbitrary (sort of) data index.
 619 ProfileData* methodDataOopDesc::data_at(int data_index) {
 620   if (out_of_bounds(data_index)) {
 621     return NULL;
 622   }
 623   DataLayout* data_layout = data_layout_at(data_index);
 624   
 625   switch (data_layout->tag()) {
 626   case DataLayout::no_tag:
 627   default:
 628     ShouldNotReachHere();
 629     return NULL;
 630   case DataLayout::bit_data_tag:
 631     return new BitData(data_layout);
 632   case DataLayout::counter_data_tag:
 633     return new CounterData(data_layout);
 634   case DataLayout::jump_data_tag:
 635     return new JumpData(data_layout);
 636   case DataLayout::receiver_type_data_tag:
 637     return new ReceiverTypeData(data_layout);
 638   case DataLayout::virtual_call_data_tag:
 639     return new VirtualCallData(data_layout);
 640   case DataLayout::ret_data_tag:
 641     return new RetData(data_layout);
 642   case DataLayout::branch_data_tag:
 643     return new BranchData(data_layout);
 644   case DataLayout::multi_branch_data_tag:
 645     return new MultiBranchData(data_layout);
 646   case DataLayout::arg_info_data_tag:
 647     return new ArgInfoData(data_layout);
 648   };
 649 }
 650 
 651 // Iteration over data.
 652 ProfileData* methodDataOopDesc::next_data(ProfileData* current) {
 653   int current_index = dp_to_di(current->dp());
 654   int next_index = current_index + current->size_in_bytes();
 655   ProfileData* next = data_at(next_index);
 656   return next;
 657 }
 658 
 659 // Give each of the data entries a chance to perform specific
 660 // data initialization.
 661 void methodDataOopDesc::post_initialize(BytecodeStream* stream) {
 662   ResourceMark rm;
 663   ProfileData* data;
 664   for (data = first_data(); is_valid(data); data = next_data(data)) {
 665     stream->set_start(data->bci());
 666     stream->next();
 667     data->post_initialize(stream, this);
 668   }
 669 }
 670 
 671 // Initialize the methodDataOop corresponding to a given method.
 672 void methodDataOopDesc::initialize(methodHandle method) {
 673   ResourceMark rm;
 674 
 675   // Set the method back-pointer.
 676   _method = method();
 677   set_creation_mileage(mileage_of(method()));
 678 
 679   // Initialize flags and trap history.
 680   _nof_decompiles = 0;
 681   _nof_overflow_recompiles = 0;
 682   _nof_overflow_traps = 0;
 683   assert(sizeof(_trap_hist) % sizeof(HeapWord) == 0, "align");
 684   Copy::zero_to_words((HeapWord*) &_trap_hist,
 685                       sizeof(_trap_hist) / sizeof(HeapWord));
 686 
 687   // Go through the bytecodes and allocate and initialize the
 688   // corresponding data cells.
 689   int data_size = 0;
 690   int empty_bc_count = 0;  // number of bytecodes lacking data
 691   BytecodeStream stream(method);
 692   Bytecodes::Code c;
 693   while ((c = stream.next()) >= 0) {
 694     int size_in_bytes = initialize_data(&stream, data_size);
 695     data_size += size_in_bytes;
 696     if (size_in_bytes == 0)  empty_bc_count += 1;
 697   }
 698   _data_size = data_size;
 699   int object_size = in_bytes(data_offset()) + data_size;
 700 
 701   // Add some extra DataLayout cells (at least one) to track stray traps.
 702   int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
 703   int extra_size = extra_data_count * DataLayout::compute_size_in_bytes(0);
 704 
 705   // Add a cell to record information about modified arguments.
 706   // Set up _args_modified array after traps cells so that
 707   // the code for traps cells works.
 708   DataLayout *dp = data_layout_at(data_size + extra_size);
 709 
 710   int arg_size = method->size_of_parameters();
 711   dp->initialize(DataLayout::arg_info_data_tag, 0, arg_size+1);
 712 
 713   object_size += extra_size + DataLayout::compute_size_in_bytes(arg_size+1);
 714 
 715   // Set an initial hint. Don't use set_hint_di() because
 716   // first_di() may be out of bounds if data_size is 0.
 717   // In that situation, _hint_di is never used, but at
 718   // least well-defined.
 719   _hint_di = first_di();
 720 
 721   post_initialize(&stream);
 722 
 723   set_object_is_parsable(object_size);
 724 }
 725 
 726 // Get a measure of how much mileage the method has on it.
 727 int methodDataOopDesc::mileage_of(methodOop method) {
 728   int mileage = 0;
 729   int iic = method->interpreter_invocation_count();
 730   if (mileage < iic)  mileage = iic; 
 731 
 732   InvocationCounter* ic = method->invocation_counter();
 733   InvocationCounter* bc = method->backedge_counter();
 734 
 735   int icval = ic->count();
 736   if (ic->carry()) icval += CompileThreshold;
 737   if (mileage < icval)  mileage = icval; 
 738   int bcval = bc->count();
 739   if (bc->carry()) bcval += CompileThreshold;
 740   if (mileage < bcval)  mileage = bcval;
 741   return mileage;
 742 }
 743 
 744 bool methodDataOopDesc::is_mature() const {
 745   uint current = mileage_of(_method);
 746   uint initial = creation_mileage();
 747   if (current < initial)
 748     return true;  // some sort of overflow
 749   uint target;
 750   if (ProfileMaturityPercentage <= 0)
 751     target = (uint) -ProfileMaturityPercentage;  // absolute value
 752   else
 753     target = (uint)( (ProfileMaturityPercentage * CompileThreshold) / 100 );
 754   return (current >= initial + target);
 755 }
 756 
 757 // Translate a bci to its corresponding data index (di).
 758 address methodDataOopDesc::bci_to_dp(int bci) {
 759   ResourceMark rm;
 760   ProfileData* data = data_before(bci);
 761   ProfileData* prev = NULL;
 762   for ( ; is_valid(data); data = next_data(data)) {
 763     if (data->bci() >= bci) {
 764       if (data->bci() == bci)  set_hint_di(dp_to_di(data->dp()));
 765       else if (prev != NULL)   set_hint_di(dp_to_di(prev->dp()));
 766       return data->dp();
 767     }
 768     prev = data;
 769   }
 770   return (address)limit_data_position();
 771 }
 772 
 773 // Translate a bci to its corresponding data, or NULL.
 774 ProfileData* methodDataOopDesc::bci_to_data(int bci) {
 775   ProfileData* data = data_before(bci);
 776   for ( ; is_valid(data); data = next_data(data)) {
 777     if (data->bci() == bci) {
 778       set_hint_di(dp_to_di(data->dp()));
 779       return data;
 780     } else if (data->bci() > bci) {
 781       break;
 782     }
 783   }
 784   return bci_to_extra_data(bci, false);
 785 }
 786 
 787 // Translate a bci to its corresponding extra data, or NULL.
 788 ProfileData* methodDataOopDesc::bci_to_extra_data(int bci, bool create_if_missing) {
 789   DataLayout* dp    = extra_data_base();
 790   DataLayout* end   = extra_data_limit();
 791   DataLayout* avail = NULL;
 792   for (; dp < end; dp = next_extra(dp)) {
 793     // No need for "OrderAccess::load_acquire" ops,
 794     // since the data structure is monotonic.
 795     if (dp->tag() == DataLayout::no_tag)  break;
 796     if (dp->tag() == DataLayout::arg_info_data_tag) {
 797       dp = end; // ArgInfoData is at the end of extra data section.
 798       break;
 799     }
 800     if (dp->bci() == bci) {
 801       assert(dp->tag() == DataLayout::bit_data_tag, "sane");
 802       return new BitData(dp);
 803     }
 804   }
 805   if (create_if_missing && dp < end) {
 806     // Allocate this one.  There is no mutual exclusion,
 807     // so two threads could allocate different BCIs to the
 808     // same data layout.  This means these extra data
 809     // records, like most other MDO contents, must not be
 810     // trusted too much.
 811     DataLayout temp;
 812     temp.initialize(DataLayout::bit_data_tag, bci, 0);
 813     dp->release_set_header(temp.header());
 814     assert(dp->tag() == DataLayout::bit_data_tag, "sane");
 815     //NO: assert(dp->bci() == bci, "no concurrent allocation");
 816     return new BitData(dp);
 817   }
 818   return NULL;
 819 }
 820 
 821 ArgInfoData *methodDataOopDesc::arg_info() {
 822   DataLayout* dp    = extra_data_base();
 823   DataLayout* end   = extra_data_limit();
 824   for (; dp < end; dp = next_extra(dp)) {
 825     if (dp->tag() == DataLayout::arg_info_data_tag)
 826       return new ArgInfoData(dp);
 827   }
 828   return NULL;
 829 }
 830 
 831 #ifndef PRODUCT
 832 void methodDataOopDesc::print_data_on(outputStream* st) {
 833   ResourceMark rm;
 834   ProfileData* data = first_data();
 835   for ( ; is_valid(data); data = next_data(data)) {
 836     st->print("%d", dp_to_di(data->dp()));
 837     st->fill_to(6);
 838     data->print_data_on(st);
 839   }
 840   st->print_cr("--- Extra data:");
 841   DataLayout* dp    = extra_data_base();
 842   DataLayout* end   = extra_data_limit();
 843   for (; dp < end; dp = next_extra(dp)) {
 844     // No need for "OrderAccess::load_acquire" ops,
 845     // since the data structure is monotonic.
 846     if (dp->tag() == DataLayout::no_tag)  continue;
 847     if (dp->tag() == DataLayout::bit_data_tag) {
 848       data = new BitData(dp);
 849     } else {
 850       assert(dp->tag() == DataLayout::arg_info_data_tag, "must be BitData or ArgInfo");
 851       data = new ArgInfoData(dp);
 852       dp = end; // ArgInfoData is at the end of extra data section.
 853     }
 854     st->print("%d", dp_to_di(data->dp()));
 855     st->fill_to(6);
 856     data->print_data_on(st);
 857   }
 858 }
 859 #endif
 860 
 861 void methodDataOopDesc::verify_data_on(outputStream* st) {
 862   NEEDS_CLEANUP;
 863   // not yet implemented.
 864 }