src/share/vm/oops/cpCacheOop.cpp

Thu, 27 May 2010 19:08:38 -0700

author
trims
date
Thu, 27 May 2010 19:08:38 -0700
changeset 1907
c18cbe5936b8
parent 1862
cd5dbf694d45
child 2015
083fde3b838e
permissions
-rw-r--r--

6941466: Oracle rebranding changes for Hotspot repositories
Summary: Change all the Sun copyrights to Oracle copyright
Reviewed-by: ohair

     1 /*
     2  * Copyright (c) 1998, 2009, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #include "incls/_precompiled.incl"
    26 #include "incls/_cpCacheOop.cpp.incl"
    29 // Implememtation of ConstantPoolCacheEntry
    31 void ConstantPoolCacheEntry::initialize_entry(int index) {
    32   assert(0 < index && index < 0x10000, "sanity check");
    33   _indices = index;
    34   assert(constant_pool_index() == index, "");
    35 }
    37 void ConstantPoolCacheEntry::initialize_secondary_entry(int main_index) {
    38   assert(0 <= main_index && main_index < 0x10000, "sanity check");
    39   _indices = (main_index << 16);
    40   assert(main_entry_index() == main_index, "");
    41 }
    43 int ConstantPoolCacheEntry::as_flags(TosState state, bool is_final,
    44                     bool is_vfinal, bool is_volatile,
    45                     bool is_method_interface, bool is_method) {
    46   int f = state;
    48   assert( state < number_of_states, "Invalid state in as_flags");
    50   f <<= 1;
    51   if (is_final) f |= 1;
    52   f <<= 1;
    53   if (is_vfinal) f |= 1;
    54   f <<= 1;
    55   if (is_volatile) f |= 1;
    56   f <<= 1;
    57   if (is_method_interface) f |= 1;
    58   f <<= 1;
    59   if (is_method) f |= 1;
    60   f <<= ConstantPoolCacheEntry::hotSwapBit;
    61   // Preserve existing flag bit values
    62 #ifdef ASSERT
    63   int old_state = ((_flags >> tosBits) & 0x0F);
    64   assert(old_state == 0 || old_state == state,
    65          "inconsistent cpCache flags state");
    66 #endif
    67   return (_flags | f) ;
    68 }
    70 void ConstantPoolCacheEntry::set_bytecode_1(Bytecodes::Code code) {
    71 #ifdef ASSERT
    72   // Read once.
    73   volatile Bytecodes::Code c = bytecode_1();
    74   assert(c == 0 || c == code || code == 0, "update must be consistent");
    75 #endif
    76   // Need to flush pending stores here before bytecode is written.
    77   OrderAccess::release_store_ptr(&_indices, _indices | ((u_char)code << 16));
    78 }
    80 void ConstantPoolCacheEntry::set_bytecode_2(Bytecodes::Code code) {
    81 #ifdef ASSERT
    82   // Read once.
    83   volatile Bytecodes::Code c = bytecode_2();
    84   assert(c == 0 || c == code || code == 0, "update must be consistent");
    85 #endif
    86   // Need to flush pending stores here before bytecode is written.
    87   OrderAccess::release_store_ptr(&_indices, _indices | ((u_char)code << 24));
    88 }
    90 #ifdef ASSERT
    91 // It is possible to have two different dummy methodOops created
    92 // when the resolve code for invoke interface executes concurrently
    93 // Hence the assertion below is weakened a bit for the invokeinterface
    94 // case.
    95 bool ConstantPoolCacheEntry::same_methodOop(oop cur_f1, oop f1) {
    96   return (cur_f1 == f1 || ((methodOop)cur_f1)->name() ==
    97          ((methodOop)f1)->name() || ((methodOop)cur_f1)->signature() ==
    98          ((methodOop)f1)->signature());
    99 }
   100 #endif
   102 // Note that concurrent update of both bytecodes can leave one of them
   103 // reset to zero.  This is harmless; the interpreter will simply re-resolve
   104 // the damaged entry.  More seriously, the memory synchronization is needed
   105 // to flush other fields (f1, f2) completely to memory before the bytecodes
   106 // are updated, lest other processors see a non-zero bytecode but zero f1/f2.
   107 void ConstantPoolCacheEntry::set_field(Bytecodes::Code get_code,
   108                                        Bytecodes::Code put_code,
   109                                        KlassHandle field_holder,
   110                                        int orig_field_index,
   111                                        int field_offset,
   112                                        TosState field_type,
   113                                        bool is_final,
   114                                        bool is_volatile) {
   115   set_f1(field_holder());
   116   set_f2(field_offset);
   117   // The field index is used by jvm/ti and is the index into fields() array
   118   // in holder instanceKlass.  This is scaled by instanceKlass::next_offset.
   119   assert((orig_field_index % instanceKlass::next_offset) == 0, "wierd index");
   120   const int field_index = orig_field_index / instanceKlass::next_offset;
   121   assert(field_index <= field_index_mask,
   122          "field index does not fit in low flag bits");
   123   set_flags(as_flags(field_type, is_final, false, is_volatile, false, false) |
   124             (field_index & field_index_mask));
   125   set_bytecode_1(get_code);
   126   set_bytecode_2(put_code);
   127   NOT_PRODUCT(verify(tty));
   128 }
   130 int  ConstantPoolCacheEntry::field_index() const {
   131   return (_flags & field_index_mask) * instanceKlass::next_offset;
   132 }
   134 void ConstantPoolCacheEntry::set_method(Bytecodes::Code invoke_code,
   135                                         methodHandle method,
   136                                         int vtable_index) {
   138   assert(method->interpreter_entry() != NULL, "should have been set at this point");
   139   assert(!method->is_obsolete(),  "attempt to write obsolete method to cpCache");
   140   bool change_to_virtual = (invoke_code == Bytecodes::_invokeinterface);
   142   int byte_no = -1;
   143   bool needs_vfinal_flag = false;
   144   switch (invoke_code) {
   145     case Bytecodes::_invokedynamic:
   146     case Bytecodes::_invokevirtual:
   147     case Bytecodes::_invokeinterface: {
   148         if (method->can_be_statically_bound()) {
   149           set_f2((intptr_t)method());
   150           needs_vfinal_flag = true;
   151         } else {
   152           assert(vtable_index >= 0, "valid index");
   153           set_f2(vtable_index);
   154         }
   155         byte_no = 2;
   156         break;
   157     }
   158     case Bytecodes::_invokespecial:
   159       // Preserve the value of the vfinal flag on invokevirtual bytecode
   160       // which may be shared with this constant pool cache entry.
   161       needs_vfinal_flag = is_resolved(Bytecodes::_invokevirtual) && is_vfinal();
   162       // fall through
   163     case Bytecodes::_invokestatic:
   164       set_f1(method());
   165       byte_no = 1;
   166       break;
   167     default:
   168       ShouldNotReachHere();
   169       break;
   170   }
   172   set_flags(as_flags(as_TosState(method->result_type()),
   173                      method->is_final_method(),
   174                      needs_vfinal_flag,
   175                      false,
   176                      change_to_virtual,
   177                      true)|
   178             method()->size_of_parameters());
   180   // Note:  byte_no also appears in TemplateTable::resolve.
   181   if (byte_no == 1) {
   182     set_bytecode_1(invoke_code);
   183   } else if (byte_no == 2)  {
   184     if (change_to_virtual) {
   185       // NOTE: THIS IS A HACK - BE VERY CAREFUL!!!
   186       //
   187       // Workaround for the case where we encounter an invokeinterface, but we
   188       // should really have an _invokevirtual since the resolved method is a
   189       // virtual method in java.lang.Object. This is a corner case in the spec
   190       // but is presumably legal. javac does not generate this code.
   191       //
   192       // We set bytecode_1() to _invokeinterface, because that is the
   193       // bytecode # used by the interpreter to see if it is resolved.
   194       // We set bytecode_2() to _invokevirtual.
   195       // See also interpreterRuntime.cpp. (8/25/2000)
   196       // Only set resolved for the invokeinterface case if method is public.
   197       // Otherwise, the method needs to be reresolved with caller for each
   198       // interface call.
   199       if (method->is_public()) set_bytecode_1(invoke_code);
   200       set_bytecode_2(Bytecodes::_invokevirtual);
   201     } else {
   202       set_bytecode_2(invoke_code);
   203     }
   204   } else {
   205     ShouldNotReachHere();
   206   }
   207   NOT_PRODUCT(verify(tty));
   208 }
   211 void ConstantPoolCacheEntry::set_interface_call(methodHandle method, int index) {
   212   klassOop interf = method->method_holder();
   213   assert(instanceKlass::cast(interf)->is_interface(), "must be an interface");
   214   set_f1(interf);
   215   set_f2(index);
   216   set_flags(as_flags(as_TosState(method->result_type()), method->is_final_method(), false, false, false, true) | method()->size_of_parameters());
   217   set_bytecode_1(Bytecodes::_invokeinterface);
   218 }
   221 void ConstantPoolCacheEntry::set_dynamic_call(Handle call_site,
   222                                               methodHandle signature_invoker) {
   223   int param_size = signature_invoker->size_of_parameters();
   224   assert(param_size >= 1, "method argument size must include MH.this");
   225   param_size -= 1;              // do not count MH.this; it is not stacked for invokedynamic
   226   if (Atomic::cmpxchg_ptr(call_site(), &_f1, NULL) == NULL) {
   227     // racing threads might be trying to install their own favorites
   228     set_f1(call_site());
   229   }
   230   //set_f2(0);
   231   bool is_final = true;
   232   assert(signature_invoker->is_final_method(), "is_final");
   233   set_flags(as_flags(as_TosState(signature_invoker->result_type()), is_final, false, false, false, true) | param_size);
   234   // do not do set_bytecode on a secondary CP cache entry
   235   //set_bytecode_1(Bytecodes::_invokedynamic);
   236 }
   239 class LocalOopClosure: public OopClosure {
   240  private:
   241   void (*_f)(oop*);
   243  public:
   244   LocalOopClosure(void f(oop*))        { _f = f; }
   245   virtual void do_oop(oop* o)          { _f(o); }
   246   virtual void do_oop(narrowOop *o)    { ShouldNotReachHere(); }
   247 };
   250 void ConstantPoolCacheEntry::oops_do(void f(oop*)) {
   251   LocalOopClosure blk(f);
   252   oop_iterate(&blk);
   253 }
   256 void ConstantPoolCacheEntry::oop_iterate(OopClosure* blk) {
   257   assert(in_words(size()) == 4, "check code below - may need adjustment");
   258   // field[1] is always oop or NULL
   259   blk->do_oop((oop*)&_f1);
   260   if (is_vfinal()) {
   261     blk->do_oop((oop*)&_f2);
   262   }
   263 }
   266 void ConstantPoolCacheEntry::oop_iterate_m(OopClosure* blk, MemRegion mr) {
   267   assert(in_words(size()) == 4, "check code below - may need adjustment");
   268   // field[1] is always oop or NULL
   269   if (mr.contains((oop *)&_f1)) blk->do_oop((oop*)&_f1);
   270   if (is_vfinal()) {
   271     if (mr.contains((oop *)&_f2)) blk->do_oop((oop*)&_f2);
   272   }
   273 }
   276 void ConstantPoolCacheEntry::follow_contents() {
   277   assert(in_words(size()) == 4, "check code below - may need adjustment");
   278   // field[1] is always oop or NULL
   279   MarkSweep::mark_and_push((oop*)&_f1);
   280   if (is_vfinal()) {
   281     MarkSweep::mark_and_push((oop*)&_f2);
   282   }
   283 }
   285 #ifndef SERIALGC
   286 void ConstantPoolCacheEntry::follow_contents(ParCompactionManager* cm) {
   287   assert(in_words(size()) == 4, "check code below - may need adjustment");
   288   // field[1] is always oop or NULL
   289   PSParallelCompact::mark_and_push(cm, (oop*)&_f1);
   290   if (is_vfinal()) {
   291     PSParallelCompact::mark_and_push(cm, (oop*)&_f2);
   292   }
   293 }
   294 #endif // SERIALGC
   296 void ConstantPoolCacheEntry::adjust_pointers() {
   297   assert(in_words(size()) == 4, "check code below - may need adjustment");
   298   // field[1] is always oop or NULL
   299   MarkSweep::adjust_pointer((oop*)&_f1);
   300   if (is_vfinal()) {
   301     MarkSweep::adjust_pointer((oop*)&_f2);
   302   }
   303 }
   305 #ifndef SERIALGC
   306 void ConstantPoolCacheEntry::update_pointers() {
   307   assert(in_words(size()) == 4, "check code below - may need adjustment");
   308   // field[1] is always oop or NULL
   309   PSParallelCompact::adjust_pointer((oop*)&_f1);
   310   if (is_vfinal()) {
   311     PSParallelCompact::adjust_pointer((oop*)&_f2);
   312   }
   313 }
   315 void ConstantPoolCacheEntry::update_pointers(HeapWord* beg_addr,
   316                                              HeapWord* end_addr) {
   317   assert(in_words(size()) == 4, "check code below - may need adjustment");
   318   // field[1] is always oop or NULL
   319   PSParallelCompact::adjust_pointer((oop*)&_f1, beg_addr, end_addr);
   320   if (is_vfinal()) {
   321     PSParallelCompact::adjust_pointer((oop*)&_f2, beg_addr, end_addr);
   322   }
   323 }
   324 #endif // SERIALGC
   326 // RedefineClasses() API support:
   327 // If this constantPoolCacheEntry refers to old_method then update it
   328 // to refer to new_method.
   329 bool ConstantPoolCacheEntry::adjust_method_entry(methodOop old_method,
   330        methodOop new_method, bool * trace_name_printed) {
   332   if (is_vfinal()) {
   333     // virtual and final so f2() contains method ptr instead of vtable index
   334     if (f2() == (intptr_t)old_method) {
   335       // match old_method so need an update
   336       _f2 = (intptr_t)new_method;
   337       if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
   338         if (!(*trace_name_printed)) {
   339           // RC_TRACE_MESG macro has an embedded ResourceMark
   340           RC_TRACE_MESG(("adjust: name=%s",
   341             Klass::cast(old_method->method_holder())->external_name()));
   342           *trace_name_printed = true;
   343         }
   344         // RC_TRACE macro has an embedded ResourceMark
   345         RC_TRACE(0x00400000, ("cpc vf-entry update: %s(%s)",
   346           new_method->name()->as_C_string(),
   347           new_method->signature()->as_C_string()));
   348       }
   350       return true;
   351     }
   353     // f1() is not used with virtual entries so bail out
   354     return false;
   355   }
   357   if ((oop)_f1 == NULL) {
   358     // NULL f1() means this is a virtual entry so bail out
   359     // We are assuming that the vtable index does not need change.
   360     return false;
   361   }
   363   if ((oop)_f1 == old_method) {
   364     _f1 = new_method;
   365     if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
   366       if (!(*trace_name_printed)) {
   367         // RC_TRACE_MESG macro has an embedded ResourceMark
   368         RC_TRACE_MESG(("adjust: name=%s",
   369           Klass::cast(old_method->method_holder())->external_name()));
   370         *trace_name_printed = true;
   371       }
   372       // RC_TRACE macro has an embedded ResourceMark
   373       RC_TRACE(0x00400000, ("cpc entry update: %s(%s)",
   374         new_method->name()->as_C_string(),
   375         new_method->signature()->as_C_string()));
   376     }
   378     return true;
   379   }
   381   return false;
   382 }
   384 bool ConstantPoolCacheEntry::is_interesting_method_entry(klassOop k) {
   385   if (!is_method_entry()) {
   386     // not a method entry so not interesting by default
   387     return false;
   388   }
   390   methodOop m = NULL;
   391   if (is_vfinal()) {
   392     // virtual and final so _f2 contains method ptr instead of vtable index
   393     m = (methodOop)_f2;
   394   } else if ((oop)_f1 == NULL) {
   395     // NULL _f1 means this is a virtual entry so also not interesting
   396     return false;
   397   } else {
   398     if (!((oop)_f1)->is_method()) {
   399       // _f1 can also contain a klassOop for an interface
   400       return false;
   401     }
   402     m = (methodOop)_f1;
   403   }
   405   assert(m != NULL && m->is_method(), "sanity check");
   406   if (m == NULL || !m->is_method() || m->method_holder() != k) {
   407     // robustness for above sanity checks or method is not in
   408     // the interesting class
   409     return false;
   410   }
   412   // the method is in the interesting class so the entry is interesting
   413   return true;
   414 }
   416 void ConstantPoolCacheEntry::print(outputStream* st, int index) const {
   417   // print separator
   418   if (index == 0) tty->print_cr("                 -------------");
   419   // print entry
   420   tty->print_cr("%3d  (%08x)  ", index, this);
   421   if (is_secondary_entry())
   422     tty->print_cr("[%5d|secondary]", main_entry_index());
   423   else
   424     tty->print_cr("[%02x|%02x|%5d]", bytecode_2(), bytecode_1(), constant_pool_index());
   425   tty->print_cr("                 [   %08x]", (address)(oop)_f1);
   426   tty->print_cr("                 [   %08x]", _f2);
   427   tty->print_cr("                 [   %08x]", _flags);
   428   tty->print_cr("                 -------------");
   429 }
   431 void ConstantPoolCacheEntry::verify(outputStream* st) const {
   432   // not implemented yet
   433 }
   435 // Implementation of ConstantPoolCache
   437 void constantPoolCacheOopDesc::initialize(intArray& inverse_index_map) {
   438   assert(inverse_index_map.length() == length(), "inverse index map must have same length as cache");
   439   for (int i = 0; i < length(); i++) {
   440     ConstantPoolCacheEntry* e = entry_at(i);
   441     int original_index = inverse_index_map[i];
   442     if ((original_index & Rewriter::_secondary_entry_tag) != 0) {
   443       int main_index = (original_index - Rewriter::_secondary_entry_tag);
   444       assert(!entry_at(main_index)->is_secondary_entry(), "valid main index");
   445       e->initialize_secondary_entry(main_index);
   446     } else {
   447       e->initialize_entry(original_index);
   448     }
   449     assert(entry_at(i) == e, "sanity");
   450   }
   451 }
   453 // RedefineClasses() API support:
   454 // If any entry of this constantPoolCache points to any of
   455 // old_methods, replace it with the corresponding new_method.
   456 void constantPoolCacheOopDesc::adjust_method_entries(methodOop* old_methods, methodOop* new_methods,
   457                                                      int methods_length, bool * trace_name_printed) {
   459   if (methods_length == 0) {
   460     // nothing to do if there are no methods
   461     return;
   462   }
   464   // get shorthand for the interesting class
   465   klassOop old_holder = old_methods[0]->method_holder();
   467   for (int i = 0; i < length(); i++) {
   468     if (!entry_at(i)->is_interesting_method_entry(old_holder)) {
   469       // skip uninteresting methods
   470       continue;
   471     }
   473     // The constantPoolCache contains entries for several different
   474     // things, but we only care about methods. In fact, we only care
   475     // about methods in the same class as the one that contains the
   476     // old_methods. At this point, we have an interesting entry.
   478     for (int j = 0; j < methods_length; j++) {
   479       methodOop old_method = old_methods[j];
   480       methodOop new_method = new_methods[j];
   482       if (entry_at(i)->adjust_method_entry(old_method, new_method,
   483           trace_name_printed)) {
   484         // current old_method matched this entry and we updated it so
   485         // break out and get to the next interesting entry if there one
   486         break;
   487       }
   488     }
   489   }
   490 }

mercurial