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