1 /*
   2  * Copyright (c) 1997, 2013, 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 "precompiled.hpp"
  26 #include "compiler/compileBroker.hpp"
  27 #include "gc_implementation/shared/markSweep.inline.hpp"
  28 #include "gc_interface/collectedHeap.inline.hpp"
  29 #include "oops/methodData.hpp"
  30 #include "oops/objArrayKlass.inline.hpp"
  31 #include "oops/oop.inline.hpp"
  32 
  33 uint                    MarkSweep::_total_invocations = 0;
  34 
  35 Stack<oop, mtGC>              MarkSweep::_marking_stack;
  36 Stack<ObjArrayTask, mtGC>     MarkSweep::_objarray_stack;
  37 
  38 Stack<oop, mtGC>              MarkSweep::_preserved_oop_stack;
  39 Stack<markOop, mtGC>          MarkSweep::_preserved_mark_stack;
  40 size_t                  MarkSweep::_preserved_count = 0;
  41 size_t                  MarkSweep::_preserved_count_max = 0;
  42 PreservedMark*          MarkSweep::_preserved_marks = NULL;
  43 ReferenceProcessor*     MarkSweep::_ref_processor   = NULL;
  44 
  45 MarkSweep::FollowRootClosure  MarkSweep::follow_root_closure;
  46 CodeBlobToOopClosure MarkSweep::follow_code_root_closure(&MarkSweep::follow_root_closure, /*do_marking=*/ true);
  47 
  48 void MarkSweep::FollowRootClosure::do_oop(oop* p)       { follow_root(p); }
  49 void MarkSweep::FollowRootClosure::do_oop(narrowOop* p) { follow_root(p); }
  50 
  51 MarkSweep::MarkAndPushClosure MarkSweep::mark_and_push_closure;
  52 MarkSweep::FollowKlassClosure MarkSweep::follow_klass_closure;
  53 MarkSweep::AdjustKlassClosure MarkSweep::adjust_klass_closure;
  54 
  55 void MarkSweep::MarkAndPushClosure::do_oop(oop* p)       { mark_and_push(p); }
  56 void MarkSweep::MarkAndPushClosure::do_oop(narrowOop* p) { mark_and_push(p); }
  57 
  58 void MarkSweep::FollowKlassClosure::do_klass(Klass* klass) {
  59   klass->oops_do(&MarkSweep::mark_and_push_closure);
  60 }
  61 void MarkSweep::AdjustKlassClosure::do_klass(Klass* klass) {
  62   klass->oops_do(&MarkSweep::adjust_pointer_closure);
  63 }
  64 
  65 void MarkSweep::follow_klass(Klass* klass) {
  66   ClassLoaderData* cld = klass->class_loader_data();
  67   // The actual processing of the klass is done when we
  68   // traverse the list of Klasses in the class loader data.
  69   MarkSweep::follow_class_loader(cld);
  70 }
  71 
  72 void MarkSweep::adjust_klass(Klass* klass) {
  73   ClassLoaderData* cld = klass->class_loader_data();
  74   // The actual processing of the klass is done when we
  75   // traverse the list of Klasses in the class loader data.
  76   MarkSweep::adjust_class_loader(cld);
  77 }
  78 
  79 void MarkSweep::follow_class_loader(ClassLoaderData* cld) {
  80   cld->oops_do(&MarkSweep::mark_and_push_closure, &MarkSweep::follow_klass_closure, true);
  81 }
  82 
  83 void MarkSweep::adjust_class_loader(ClassLoaderData* cld) {
  84   cld->oops_do(&MarkSweep::adjust_pointer_closure, &MarkSweep::adjust_klass_closure, true);
  85 }
  86 
  87 
  88 void MarkSweep::follow_stack() {
  89   do {
  90     while (!_marking_stack.is_empty()) {
  91       oop obj = _marking_stack.pop();
  92       assert (obj->is_gc_marked(), "p must be marked");
  93       obj->follow_contents();
  94     }
  95     // Process ObjArrays one at a time to avoid marking stack bloat.
  96     if (!_objarray_stack.is_empty()) {
  97       ObjArrayTask task = _objarray_stack.pop();
  98       ObjArrayKlass* k = (ObjArrayKlass*)task.obj()->klass();
  99       k->oop_follow_contents(task.obj(), task.index());
 100     }
 101   } while (!_marking_stack.is_empty() || !_objarray_stack.is_empty());
 102 }
 103 
 104 MarkSweep::FollowStackClosure MarkSweep::follow_stack_closure;
 105 
 106 void MarkSweep::FollowStackClosure::do_void() { follow_stack(); }
 107 
 108 // We preserve the mark which should be replaced at the end and the location
 109 // that it will go.  Note that the object that this markOop belongs to isn't
 110 // currently at that address but it will be after phase4
 111 void MarkSweep::preserve_mark(oop obj, markOop mark) {
 112   // We try to store preserved marks in the to space of the new generation since
 113   // this is storage which should be available.  Most of the time this should be
 114   // sufficient space for the marks we need to preserve but if it isn't we fall
 115   // back to using Stacks to keep track of the overflow.
 116   if (_preserved_count < _preserved_count_max) {
 117     _preserved_marks[_preserved_count++].init(obj, mark);
 118   } else {
 119     _preserved_mark_stack.push(mark);
 120     _preserved_oop_stack.push(obj);
 121   }
 122 }
 123 
 124 MarkSweep::AdjustPointerClosure MarkSweep::adjust_pointer_closure;
 125 
 126 void MarkSweep::AdjustPointerClosure::do_oop(oop* p)       { adjust_pointer(p); }
 127 void MarkSweep::AdjustPointerClosure::do_oop(narrowOop* p) { adjust_pointer(p); }
 128 
 129 void MarkSweep::adjust_marks() {
 130   assert( _preserved_oop_stack.size() == _preserved_mark_stack.size(),
 131          "inconsistent preserved oop stacks");
 132 
 133   // adjust the oops we saved earlier
 134   for (size_t i = 0; i < _preserved_count; i++) {
 135     _preserved_marks[i].adjust_pointer();
 136   }
 137 
 138   // deal with the overflow stack
 139   StackIterator<oop, mtGC> iter(_preserved_oop_stack);
 140   while (!iter.is_empty()) {
 141     oop* p = iter.next_addr();
 142     adjust_pointer(p);
 143   }
 144 }
 145 
 146 void MarkSweep::restore_marks() {
 147   assert(_preserved_oop_stack.size() == _preserved_mark_stack.size(),
 148          "inconsistent preserved oop stacks");
 149   if (PrintGC && Verbose) {
 150     gclog_or_tty->print_cr("Restoring %d marks",
 151                            _preserved_count + _preserved_oop_stack.size());
 152   }
 153 
 154   // restore the marks we saved earlier
 155   for (size_t i = 0; i < _preserved_count; i++) {
 156     _preserved_marks[i].restore();
 157   }
 158 
 159   // deal with the overflow
 160   while (!_preserved_oop_stack.is_empty()) {
 161     oop obj       = _preserved_oop_stack.pop();
 162     markOop mark  = _preserved_mark_stack.pop();
 163     obj->set_mark(mark);
 164   }
 165 }
 166 
 167 MarkSweep::IsAliveClosure   MarkSweep::is_alive;
 168 
 169 bool MarkSweep::IsAliveClosure::do_object_b(oop p) { return p->is_gc_marked(); }
 170 
 171 MarkSweep::KeepAliveClosure MarkSweep::keep_alive;
 172 
 173 void MarkSweep::KeepAliveClosure::do_oop(oop* p)       { MarkSweep::KeepAliveClosure::do_oop_work(p); }
 174 void MarkSweep::KeepAliveClosure::do_oop(narrowOop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); }
 175 
 176 void marksweep_init() { /* empty */ }
 177 
 178 #ifndef PRODUCT
 179 
 180 void MarkSweep::trace(const char* msg) {
 181   if (TraceMarkSweep)
 182     gclog_or_tty->print("%s", msg);
 183 }
 184 
 185 #endif