src/share/vm/oops/methodDataOop.cpp

Wed, 02 Sep 2009 00:04:29 -0700

author
ysr
date
Wed, 02 Sep 2009 00:04:29 -0700
changeset 1376
8b46c4d82093
parent 1279
bd02caa94611
child 1907
c18cbe5936b8
permissions
-rw-r--r--

4957990: Perm heap bloat in JVM
Summary: Treat ProfileData in MDO's as a source of weak, not strong, roots. Fixes the bug for stop-world collection -- the case of concurrent collection will be fixed separately.
Reviewed-by: jcoomes, jmasa, kvn, never

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

mercurial