src/share/vm/gc_implementation/shared/markSweep.cpp

Tue, 15 Sep 2009 21:53:47 -0700

author
jrose
date
Tue, 15 Sep 2009 21:53:47 -0700
changeset 1424
148e5441d916
parent 631
d1605aabd0a1
child 1428
54b3b351d6f9
permissions
-rw-r--r--

6863023: need non-perm oops in code cache for JSR 292
Summary: Make a special root-list for those few nmethods which might contain non-perm oops.
Reviewed-by: twisti, kvn, never, jmasa, ysr

     1 /*
     2  * Copyright 1997-2008 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/_markSweep.cpp.incl"
    28 GrowableArray<oop>*     MarkSweep::_marking_stack       = NULL;
    29 GrowableArray<Klass*>*  MarkSweep::_revisit_klass_stack = NULL;
    31 GrowableArray<oop>*     MarkSweep::_preserved_oop_stack = NULL;
    32 GrowableArray<markOop>* MarkSweep::_preserved_mark_stack= NULL;
    33 size_t                  MarkSweep::_preserved_count = 0;
    34 size_t                  MarkSweep::_preserved_count_max = 0;
    35 PreservedMark*          MarkSweep::_preserved_marks = NULL;
    36 ReferenceProcessor*     MarkSweep::_ref_processor   = NULL;
    38 #ifdef VALIDATE_MARK_SWEEP
    39 GrowableArray<void*>*   MarkSweep::_root_refs_stack = NULL;
    40 GrowableArray<oop> *    MarkSweep::_live_oops = NULL;
    41 GrowableArray<oop> *    MarkSweep::_live_oops_moved_to = NULL;
    42 GrowableArray<size_t>*  MarkSweep::_live_oops_size = NULL;
    43 size_t                  MarkSweep::_live_oops_index = 0;
    44 size_t                  MarkSweep::_live_oops_index_at_perm = 0;
    45 GrowableArray<void*>*   MarkSweep::_other_refs_stack = NULL;
    46 GrowableArray<void*>*   MarkSweep::_adjusted_pointers = NULL;
    47 bool                         MarkSweep::_pointer_tracking = false;
    48 bool                         MarkSweep::_root_tracking = true;
    50 GrowableArray<HeapWord*>* MarkSweep::_cur_gc_live_oops = NULL;
    51 GrowableArray<HeapWord*>* MarkSweep::_cur_gc_live_oops_moved_to = NULL;
    52 GrowableArray<size_t>   * MarkSweep::_cur_gc_live_oops_size = NULL;
    53 GrowableArray<HeapWord*>* MarkSweep::_last_gc_live_oops = NULL;
    54 GrowableArray<HeapWord*>* MarkSweep::_last_gc_live_oops_moved_to = NULL;
    55 GrowableArray<size_t>   * MarkSweep::_last_gc_live_oops_size = NULL;
    56 #endif
    58 void MarkSweep::revisit_weak_klass_link(Klass* k) {
    59   _revisit_klass_stack->push(k);
    60 }
    62 void MarkSweep::follow_weak_klass_links() {
    63   // All klasses on the revisit stack are marked at this point.
    64   // Update and follow all subklass, sibling and implementor links.
    65   for (int i = 0; i < _revisit_klass_stack->length(); i++) {
    66     _revisit_klass_stack->at(i)->follow_weak_klass_links(&is_alive,&keep_alive);
    67   }
    68   follow_stack();
    69 }
    71 MarkSweep::FollowRootClosure  MarkSweep::follow_root_closure;
    72 CodeBlobToOopClosure MarkSweep::follow_code_root_closure(&MarkSweep::follow_root_closure, /*do_marking=*/ true);
    74 void MarkSweep::FollowRootClosure::do_oop(oop* p)       { follow_root(p); }
    75 void MarkSweep::FollowRootClosure::do_oop(narrowOop* p) { follow_root(p); }
    77 MarkSweep::MarkAndPushClosure MarkSweep::mark_and_push_closure;
    79 void MarkSweep::MarkAndPushClosure::do_oop(oop* p)       { mark_and_push(p); }
    80 void MarkSweep::MarkAndPushClosure::do_oop(narrowOop* p) { mark_and_push(p); }
    82 void MarkSweep::follow_stack() {
    83   while (!_marking_stack->is_empty()) {
    84     oop obj = _marking_stack->pop();
    85     assert (obj->is_gc_marked(), "p must be marked");
    86     obj->follow_contents();
    87   }
    88 }
    90 MarkSweep::FollowStackClosure MarkSweep::follow_stack_closure;
    92 void MarkSweep::FollowStackClosure::do_void() { follow_stack(); }
    94 // We preserve the mark which should be replaced at the end and the location that it
    95 // will go.  Note that the object that this markOop belongs to isn't currently at that
    96 // address but it will be after phase4
    97 void MarkSweep::preserve_mark(oop obj, markOop mark) {
    98   // we try to store preserved marks in the to space of the new generation since this
    99   // is storage which should be available.  Most of the time this should be sufficient
   100   // space for the marks we need to preserve but if it isn't we fall back in using
   101   // GrowableArrays to keep track of the overflow.
   102   if (_preserved_count < _preserved_count_max) {
   103     _preserved_marks[_preserved_count++].init(obj, mark);
   104   } else {
   105     if (_preserved_mark_stack == NULL) {
   106       _preserved_mark_stack = new (ResourceObj::C_HEAP) GrowableArray<markOop>(40, true);
   107       _preserved_oop_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(40, true);
   108     }
   109     _preserved_mark_stack->push(mark);
   110     _preserved_oop_stack->push(obj);
   111   }
   112 }
   114 MarkSweep::AdjustPointerClosure MarkSweep::adjust_root_pointer_closure(true);
   115 MarkSweep::AdjustPointerClosure MarkSweep::adjust_pointer_closure(false);
   117 void MarkSweep::AdjustPointerClosure::do_oop(oop* p)       { adjust_pointer(p, _is_root); }
   118 void MarkSweep::AdjustPointerClosure::do_oop(narrowOop* p) { adjust_pointer(p, _is_root); }
   120 void MarkSweep::adjust_marks() {
   121   assert(_preserved_oop_stack == NULL ||
   122          _preserved_oop_stack->length() == _preserved_mark_stack->length(),
   123          "inconsistent preserved oop stacks");
   125   // adjust the oops we saved earlier
   126   for (size_t i = 0; i < _preserved_count; i++) {
   127     _preserved_marks[i].adjust_pointer();
   128   }
   130   // deal with the overflow stack
   131   if (_preserved_oop_stack) {
   132     for (int i = 0; i < _preserved_oop_stack->length(); i++) {
   133       oop* p = _preserved_oop_stack->adr_at(i);
   134       adjust_pointer(p);
   135     }
   136   }
   137 }
   139 void MarkSweep::restore_marks() {
   140   assert(_preserved_oop_stack == NULL ||
   141          _preserved_oop_stack->length() == _preserved_mark_stack->length(),
   142          "inconsistent preserved oop stacks");
   143   if (PrintGC && Verbose) {
   144     gclog_or_tty->print_cr("Restoring %d marks", _preserved_count +
   145                   (_preserved_oop_stack ? _preserved_oop_stack->length() : 0));
   146   }
   148   // restore the marks we saved earlier
   149   for (size_t i = 0; i < _preserved_count; i++) {
   150     _preserved_marks[i].restore();
   151   }
   153   // deal with the overflow
   154   if (_preserved_oop_stack) {
   155     for (int i = 0; i < _preserved_oop_stack->length(); i++) {
   156       oop obj       = _preserved_oop_stack->at(i);
   157       markOop mark  = _preserved_mark_stack->at(i);
   158       obj->set_mark(mark);
   159     }
   160   }
   161 }
   163 #ifdef VALIDATE_MARK_SWEEP
   165 void MarkSweep::track_adjusted_pointer(void* p, bool isroot) {
   166   if (!ValidateMarkSweep)
   167     return;
   169   if (!isroot) {
   170     if (_pointer_tracking) {
   171       guarantee(_adjusted_pointers->contains(p), "should have seen this pointer");
   172       _adjusted_pointers->remove(p);
   173     }
   174   } else {
   175     ptrdiff_t index = _root_refs_stack->find(p);
   176     if (index != -1) {
   177       int l = _root_refs_stack->length();
   178       if (l > 0 && l - 1 != index) {
   179         void* last = _root_refs_stack->pop();
   180         assert(last != p, "should be different");
   181         _root_refs_stack->at_put(index, last);
   182       } else {
   183         _root_refs_stack->remove(p);
   184       }
   185     }
   186   }
   187 }
   189 void MarkSweep::check_adjust_pointer(void* p) {
   190   _adjusted_pointers->push(p);
   191 }
   193 class AdjusterTracker: public OopClosure {
   194  public:
   195   AdjusterTracker() {}
   196   void do_oop(oop* o)       { MarkSweep::check_adjust_pointer(o); }
   197   void do_oop(narrowOop* o) { MarkSweep::check_adjust_pointer(o); }
   198 };
   200 void MarkSweep::track_interior_pointers(oop obj) {
   201   if (ValidateMarkSweep) {
   202     _adjusted_pointers->clear();
   203     _pointer_tracking = true;
   205     AdjusterTracker checker;
   206     obj->oop_iterate(&checker);
   207   }
   208 }
   210 void MarkSweep::check_interior_pointers() {
   211   if (ValidateMarkSweep) {
   212     _pointer_tracking = false;
   213     guarantee(_adjusted_pointers->length() == 0, "should have processed the same pointers");
   214   }
   215 }
   217 void MarkSweep::reset_live_oop_tracking(bool at_perm) {
   218   if (ValidateMarkSweep) {
   219     guarantee((size_t)_live_oops->length() == _live_oops_index, "should be at end of live oops");
   220     _live_oops_index = at_perm ? _live_oops_index_at_perm : 0;
   221   }
   222 }
   224 void MarkSweep::register_live_oop(oop p, size_t size) {
   225   if (ValidateMarkSweep) {
   226     _live_oops->push(p);
   227     _live_oops_size->push(size);
   228     _live_oops_index++;
   229   }
   230 }
   232 void MarkSweep::validate_live_oop(oop p, size_t size) {
   233   if (ValidateMarkSweep) {
   234     oop obj = _live_oops->at((int)_live_oops_index);
   235     guarantee(obj == p, "should be the same object");
   236     guarantee(_live_oops_size->at((int)_live_oops_index) == size, "should be the same size");
   237     _live_oops_index++;
   238   }
   239 }
   241 void MarkSweep::live_oop_moved_to(HeapWord* q, size_t size,
   242                                   HeapWord* compaction_top) {
   243   assert(oop(q)->forwardee() == NULL || oop(q)->forwardee() == oop(compaction_top),
   244          "should be moved to forwarded location");
   245   if (ValidateMarkSweep) {
   246     MarkSweep::validate_live_oop(oop(q), size);
   247     _live_oops_moved_to->push(oop(compaction_top));
   248   }
   249   if (RecordMarkSweepCompaction) {
   250     _cur_gc_live_oops->push(q);
   251     _cur_gc_live_oops_moved_to->push(compaction_top);
   252     _cur_gc_live_oops_size->push(size);
   253   }
   254 }
   256 void MarkSweep::compaction_complete() {
   257   if (RecordMarkSweepCompaction) {
   258     GrowableArray<HeapWord*>* _tmp_live_oops          = _cur_gc_live_oops;
   259     GrowableArray<HeapWord*>* _tmp_live_oops_moved_to = _cur_gc_live_oops_moved_to;
   260     GrowableArray<size_t>   * _tmp_live_oops_size     = _cur_gc_live_oops_size;
   262     _cur_gc_live_oops           = _last_gc_live_oops;
   263     _cur_gc_live_oops_moved_to  = _last_gc_live_oops_moved_to;
   264     _cur_gc_live_oops_size      = _last_gc_live_oops_size;
   265     _last_gc_live_oops          = _tmp_live_oops;
   266     _last_gc_live_oops_moved_to = _tmp_live_oops_moved_to;
   267     _last_gc_live_oops_size     = _tmp_live_oops_size;
   268   }
   269 }
   271 void MarkSweep::print_new_location_of_heap_address(HeapWord* q) {
   272   if (!RecordMarkSweepCompaction) {
   273     tty->print_cr("Requires RecordMarkSweepCompaction to be enabled");
   274     return;
   275   }
   277   if (_last_gc_live_oops == NULL) {
   278     tty->print_cr("No compaction information gathered yet");
   279     return;
   280   }
   282   for (int i = 0; i < _last_gc_live_oops->length(); i++) {
   283     HeapWord* old_oop = _last_gc_live_oops->at(i);
   284     size_t    sz      = _last_gc_live_oops_size->at(i);
   285     if (old_oop <= q && q < (old_oop + sz)) {
   286       HeapWord* new_oop = _last_gc_live_oops_moved_to->at(i);
   287       size_t offset = (q - old_oop);
   288       tty->print_cr("Address " PTR_FORMAT, q);
   289       tty->print_cr(" Was in oop " PTR_FORMAT ", size " SIZE_FORMAT ", at offset " SIZE_FORMAT, old_oop, sz, offset);
   290       tty->print_cr(" Now in oop " PTR_FORMAT ", actual address " PTR_FORMAT, new_oop, new_oop + offset);
   291       return;
   292     }
   293   }
   295   tty->print_cr("Address " PTR_FORMAT " not found in live oop information from last GC", q);
   296 }
   297 #endif //VALIDATE_MARK_SWEEP
   299 MarkSweep::IsAliveClosure   MarkSweep::is_alive;
   301 void MarkSweep::IsAliveClosure::do_object(oop p)   { ShouldNotReachHere(); }
   302 bool MarkSweep::IsAliveClosure::do_object_b(oop p) { return p->is_gc_marked(); }
   304 MarkSweep::KeepAliveClosure MarkSweep::keep_alive;
   306 void MarkSweep::KeepAliveClosure::do_oop(oop* p)       { MarkSweep::KeepAliveClosure::do_oop_work(p); }
   307 void MarkSweep::KeepAliveClosure::do_oop(narrowOop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); }
   309 void marksweep_init() { /* empty */ }
   311 #ifndef PRODUCT
   313 void MarkSweep::trace(const char* msg) {
   314   if (TraceMarkSweep)
   315     gclog_or_tty->print("%s", msg);
   316 }
   318 #endif

mercurial