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

Fri, 25 Sep 2009 12:17:06 -0700

author
trims
date
Fri, 25 Sep 2009 12:17:06 -0700
changeset 1417
7a102acc9f17
parent 1383
89e0543e1737
parent 1415
c3c4a1d3801a
child 1435
a1423fe86a18
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright 1997-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/_markSweep.cpp.incl"
    28 GrowableArray<oop>*     MarkSweep::_marking_stack       = NULL;
    29 GrowableArray<Klass*>*  MarkSweep::_revisit_klass_stack = NULL;
    30 GrowableArray<DataLayout*>*  MarkSweep::_revisit_mdo_stack = NULL;
    32 GrowableArray<oop>*     MarkSweep::_preserved_oop_stack = NULL;
    33 GrowableArray<markOop>* MarkSweep::_preserved_mark_stack= NULL;
    34 size_t                  MarkSweep::_preserved_count = 0;
    35 size_t                  MarkSweep::_preserved_count_max = 0;
    36 PreservedMark*          MarkSweep::_preserved_marks = NULL;
    37 ReferenceProcessor*     MarkSweep::_ref_processor   = NULL;
    39 #ifdef VALIDATE_MARK_SWEEP
    40 GrowableArray<void*>*   MarkSweep::_root_refs_stack = NULL;
    41 GrowableArray<oop> *    MarkSweep::_live_oops = NULL;
    42 GrowableArray<oop> *    MarkSweep::_live_oops_moved_to = NULL;
    43 GrowableArray<size_t>*  MarkSweep::_live_oops_size = NULL;
    44 size_t                  MarkSweep::_live_oops_index = 0;
    45 size_t                  MarkSweep::_live_oops_index_at_perm = 0;
    46 GrowableArray<void*>*   MarkSweep::_other_refs_stack = NULL;
    47 GrowableArray<void*>*   MarkSweep::_adjusted_pointers = NULL;
    48 bool                         MarkSweep::_pointer_tracking = false;
    49 bool                         MarkSweep::_root_tracking = true;
    51 GrowableArray<HeapWord*>* MarkSweep::_cur_gc_live_oops = NULL;
    52 GrowableArray<HeapWord*>* MarkSweep::_cur_gc_live_oops_moved_to = NULL;
    53 GrowableArray<size_t>   * MarkSweep::_cur_gc_live_oops_size = NULL;
    54 GrowableArray<HeapWord*>* MarkSweep::_last_gc_live_oops = NULL;
    55 GrowableArray<HeapWord*>* MarkSweep::_last_gc_live_oops_moved_to = NULL;
    56 GrowableArray<size_t>   * MarkSweep::_last_gc_live_oops_size = NULL;
    57 #endif
    59 void MarkSweep::revisit_weak_klass_link(Klass* k) {
    60   _revisit_klass_stack->push(k);
    61 }
    63 void MarkSweep::follow_weak_klass_links() {
    64   // All klasses on the revisit stack are marked at this point.
    65   // Update and follow all subklass, sibling and implementor links.
    66   if (PrintRevisitStats) {
    67     gclog_or_tty->print_cr("#classes in system dictionary = %d", SystemDictionary::number_of_classes());
    68     gclog_or_tty->print_cr("Revisit klass stack length = %d", _revisit_klass_stack->length());
    69   }
    70   for (int i = 0; i < _revisit_klass_stack->length(); i++) {
    71     _revisit_klass_stack->at(i)->follow_weak_klass_links(&is_alive,&keep_alive);
    72   }
    73   follow_stack();
    74 }
    76 void MarkSweep::revisit_mdo(DataLayout* p) {
    77   _revisit_mdo_stack->push(p);
    78 }
    80 void MarkSweep::follow_mdo_weak_refs() {
    81   // All strongly reachable oops have been marked at this point;
    82   // we can visit and clear any weak references from MDO's which
    83   // we memoized during the strong marking phase.
    84   assert(_marking_stack->is_empty(), "Marking stack should be empty");
    85   if (PrintRevisitStats) {
    86     gclog_or_tty->print_cr("#classes in system dictionary = %d", SystemDictionary::number_of_classes());
    87     gclog_or_tty->print_cr("Revisit MDO stack length = %d", _revisit_mdo_stack->length());
    88   }
    89   for (int i = 0; i < _revisit_mdo_stack->length(); i++) {
    90     _revisit_mdo_stack->at(i)->follow_weak_refs(&is_alive);
    91   }
    92   follow_stack();
    93 }
    95 MarkSweep::FollowRootClosure  MarkSweep::follow_root_closure;
    97 void MarkSweep::FollowRootClosure::do_oop(oop* p)       { follow_root(p); }
    98 void MarkSweep::FollowRootClosure::do_oop(narrowOop* p) { follow_root(p); }
   100 MarkSweep::MarkAndPushClosure MarkSweep::mark_and_push_closure;
   102 void MarkSweep::MarkAndPushClosure::do_oop(oop* p)       { mark_and_push(p); }
   103 void MarkSweep::MarkAndPushClosure::do_oop(narrowOop* p) { mark_and_push(p); }
   105 void MarkSweep::follow_stack() {
   106   while (!_marking_stack->is_empty()) {
   107     oop obj = _marking_stack->pop();
   108     assert (obj->is_gc_marked(), "p must be marked");
   109     obj->follow_contents();
   110   }
   111 }
   113 MarkSweep::FollowStackClosure MarkSweep::follow_stack_closure;
   115 void MarkSweep::FollowStackClosure::do_void() { follow_stack(); }
   117 // We preserve the mark which should be replaced at the end and the location that it
   118 // will go.  Note that the object that this markOop belongs to isn't currently at that
   119 // address but it will be after phase4
   120 void MarkSweep::preserve_mark(oop obj, markOop mark) {
   121   // we try to store preserved marks in the to space of the new generation since this
   122   // is storage which should be available.  Most of the time this should be sufficient
   123   // space for the marks we need to preserve but if it isn't we fall back in using
   124   // GrowableArrays to keep track of the overflow.
   125   if (_preserved_count < _preserved_count_max) {
   126     _preserved_marks[_preserved_count++].init(obj, mark);
   127   } else {
   128     if (_preserved_mark_stack == NULL) {
   129       _preserved_mark_stack = new (ResourceObj::C_HEAP) GrowableArray<markOop>(40, true);
   130       _preserved_oop_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(40, true);
   131     }
   132     _preserved_mark_stack->push(mark);
   133     _preserved_oop_stack->push(obj);
   134   }
   135 }
   137 MarkSweep::AdjustPointerClosure MarkSweep::adjust_root_pointer_closure(true);
   138 MarkSweep::AdjustPointerClosure MarkSweep::adjust_pointer_closure(false);
   140 void MarkSweep::AdjustPointerClosure::do_oop(oop* p)       { adjust_pointer(p, _is_root); }
   141 void MarkSweep::AdjustPointerClosure::do_oop(narrowOop* p) { adjust_pointer(p, _is_root); }
   143 void MarkSweep::adjust_marks() {
   144   assert(_preserved_oop_stack == NULL ||
   145          _preserved_oop_stack->length() == _preserved_mark_stack->length(),
   146          "inconsistent preserved oop stacks");
   148   // adjust the oops we saved earlier
   149   for (size_t i = 0; i < _preserved_count; i++) {
   150     _preserved_marks[i].adjust_pointer();
   151   }
   153   // deal with the overflow stack
   154   if (_preserved_oop_stack) {
   155     for (int i = 0; i < _preserved_oop_stack->length(); i++) {
   156       oop* p = _preserved_oop_stack->adr_at(i);
   157       adjust_pointer(p);
   158     }
   159   }
   160 }
   162 void MarkSweep::restore_marks() {
   163   assert(_preserved_oop_stack == NULL ||
   164          _preserved_oop_stack->length() == _preserved_mark_stack->length(),
   165          "inconsistent preserved oop stacks");
   166   if (PrintGC && Verbose) {
   167     gclog_or_tty->print_cr("Restoring %d marks", _preserved_count +
   168                   (_preserved_oop_stack ? _preserved_oop_stack->length() : 0));
   169   }
   171   // restore the marks we saved earlier
   172   for (size_t i = 0; i < _preserved_count; i++) {
   173     _preserved_marks[i].restore();
   174   }
   176   // deal with the overflow
   177   if (_preserved_oop_stack) {
   178     for (int i = 0; i < _preserved_oop_stack->length(); i++) {
   179       oop obj       = _preserved_oop_stack->at(i);
   180       markOop mark  = _preserved_mark_stack->at(i);
   181       obj->set_mark(mark);
   182     }
   183   }
   184 }
   186 #ifdef VALIDATE_MARK_SWEEP
   188 void MarkSweep::track_adjusted_pointer(void* p, bool isroot) {
   189   if (!ValidateMarkSweep)
   190     return;
   192   if (!isroot) {
   193     if (_pointer_tracking) {
   194       guarantee(_adjusted_pointers->contains(p), "should have seen this pointer");
   195       _adjusted_pointers->remove(p);
   196     }
   197   } else {
   198     ptrdiff_t index = _root_refs_stack->find(p);
   199     if (index != -1) {
   200       int l = _root_refs_stack->length();
   201       if (l > 0 && l - 1 != index) {
   202         void* last = _root_refs_stack->pop();
   203         assert(last != p, "should be different");
   204         _root_refs_stack->at_put(index, last);
   205       } else {
   206         _root_refs_stack->remove(p);
   207       }
   208     }
   209   }
   210 }
   212 void MarkSweep::check_adjust_pointer(void* p) {
   213   _adjusted_pointers->push(p);
   214 }
   216 class AdjusterTracker: public OopClosure {
   217  public:
   218   AdjusterTracker() {}
   219   void do_oop(oop* o)       { MarkSweep::check_adjust_pointer(o); }
   220   void do_oop(narrowOop* o) { MarkSweep::check_adjust_pointer(o); }
   221 };
   223 void MarkSweep::track_interior_pointers(oop obj) {
   224   if (ValidateMarkSweep) {
   225     _adjusted_pointers->clear();
   226     _pointer_tracking = true;
   228     AdjusterTracker checker;
   229     obj->oop_iterate(&checker);
   230   }
   231 }
   233 void MarkSweep::check_interior_pointers() {
   234   if (ValidateMarkSweep) {
   235     _pointer_tracking = false;
   236     guarantee(_adjusted_pointers->length() == 0, "should have processed the same pointers");
   237   }
   238 }
   240 void MarkSweep::reset_live_oop_tracking(bool at_perm) {
   241   if (ValidateMarkSweep) {
   242     guarantee((size_t)_live_oops->length() == _live_oops_index, "should be at end of live oops");
   243     _live_oops_index = at_perm ? _live_oops_index_at_perm : 0;
   244   }
   245 }
   247 void MarkSweep::register_live_oop(oop p, size_t size) {
   248   if (ValidateMarkSweep) {
   249     _live_oops->push(p);
   250     _live_oops_size->push(size);
   251     _live_oops_index++;
   252   }
   253 }
   255 void MarkSweep::validate_live_oop(oop p, size_t size) {
   256   if (ValidateMarkSweep) {
   257     oop obj = _live_oops->at((int)_live_oops_index);
   258     guarantee(obj == p, "should be the same object");
   259     guarantee(_live_oops_size->at((int)_live_oops_index) == size, "should be the same size");
   260     _live_oops_index++;
   261   }
   262 }
   264 void MarkSweep::live_oop_moved_to(HeapWord* q, size_t size,
   265                                   HeapWord* compaction_top) {
   266   assert(oop(q)->forwardee() == NULL || oop(q)->forwardee() == oop(compaction_top),
   267          "should be moved to forwarded location");
   268   if (ValidateMarkSweep) {
   269     MarkSweep::validate_live_oop(oop(q), size);
   270     _live_oops_moved_to->push(oop(compaction_top));
   271   }
   272   if (RecordMarkSweepCompaction) {
   273     _cur_gc_live_oops->push(q);
   274     _cur_gc_live_oops_moved_to->push(compaction_top);
   275     _cur_gc_live_oops_size->push(size);
   276   }
   277 }
   279 void MarkSweep::compaction_complete() {
   280   if (RecordMarkSweepCompaction) {
   281     GrowableArray<HeapWord*>* _tmp_live_oops          = _cur_gc_live_oops;
   282     GrowableArray<HeapWord*>* _tmp_live_oops_moved_to = _cur_gc_live_oops_moved_to;
   283     GrowableArray<size_t>   * _tmp_live_oops_size     = _cur_gc_live_oops_size;
   285     _cur_gc_live_oops           = _last_gc_live_oops;
   286     _cur_gc_live_oops_moved_to  = _last_gc_live_oops_moved_to;
   287     _cur_gc_live_oops_size      = _last_gc_live_oops_size;
   288     _last_gc_live_oops          = _tmp_live_oops;
   289     _last_gc_live_oops_moved_to = _tmp_live_oops_moved_to;
   290     _last_gc_live_oops_size     = _tmp_live_oops_size;
   291   }
   292 }
   294 void MarkSweep::print_new_location_of_heap_address(HeapWord* q) {
   295   if (!RecordMarkSweepCompaction) {
   296     tty->print_cr("Requires RecordMarkSweepCompaction to be enabled");
   297     return;
   298   }
   300   if (_last_gc_live_oops == NULL) {
   301     tty->print_cr("No compaction information gathered yet");
   302     return;
   303   }
   305   for (int i = 0; i < _last_gc_live_oops->length(); i++) {
   306     HeapWord* old_oop = _last_gc_live_oops->at(i);
   307     size_t    sz      = _last_gc_live_oops_size->at(i);
   308     if (old_oop <= q && q < (old_oop + sz)) {
   309       HeapWord* new_oop = _last_gc_live_oops_moved_to->at(i);
   310       size_t offset = (q - old_oop);
   311       tty->print_cr("Address " PTR_FORMAT, q);
   312       tty->print_cr(" Was in oop " PTR_FORMAT ", size " SIZE_FORMAT ", at offset " SIZE_FORMAT, old_oop, sz, offset);
   313       tty->print_cr(" Now in oop " PTR_FORMAT ", actual address " PTR_FORMAT, new_oop, new_oop + offset);
   314       return;
   315     }
   316   }
   318   tty->print_cr("Address " PTR_FORMAT " not found in live oop information from last GC", q);
   319 }
   320 #endif //VALIDATE_MARK_SWEEP
   322 MarkSweep::IsAliveClosure   MarkSweep::is_alive;
   324 void MarkSweep::IsAliveClosure::do_object(oop p)   { ShouldNotReachHere(); }
   325 bool MarkSweep::IsAliveClosure::do_object_b(oop p) { return p->is_gc_marked(); }
   327 MarkSweep::KeepAliveClosure MarkSweep::keep_alive;
   329 void MarkSweep::KeepAliveClosure::do_oop(oop* p)       { MarkSweep::KeepAliveClosure::do_oop_work(p); }
   330 void MarkSweep::KeepAliveClosure::do_oop(narrowOop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); }
   332 void marksweep_init() { /* empty */ }
   334 #ifndef PRODUCT
   336 void MarkSweep::trace(const char* msg) {
   337   if (TraceMarkSweep)
   338     gclog_or_tty->print("%s", msg);
   339 }
   341 #endif

mercurial