1 /*
   2  * Copyright (c) 1997, 2018, 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/serial/markSweep.inline.hpp"
  28 #include "gc/serial/serial_specialized_oop_closures.hpp"
  29 #include "gc/shared/collectedHeap.inline.hpp"
  30 #include "gc/shared/gcTimer.hpp"
  31 #include "gc/shared/gcTrace.hpp"
  32 #include "memory/iterator.inline.hpp"
  33 #include "oops/access.inline.hpp"
  34 #include "oops/compressedOops.inline.hpp"
  35 #include "oops/instanceClassLoaderKlass.inline.hpp"
  36 #include "oops/instanceKlass.inline.hpp"
  37 #include "oops/instanceMirrorKlass.inline.hpp"
  38 #include "oops/instanceRefKlass.inline.hpp"
  39 #include "oops/methodData.hpp"
  40 #include "oops/objArrayKlass.inline.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "oops/typeArrayOop.inline.hpp"
  43 #include "utilities/macros.hpp"
  44 #include "utilities/stack.inline.hpp"
  45 
  46 uint                    MarkSweep::_total_invocations = 0;
  47 
  48 Stack<oop, mtGC>              MarkSweep::_marking_stack;
  49 Stack<ObjArrayTask, mtGC>     MarkSweep::_objarray_stack;
  50 
  51 Stack<oop, mtGC>              MarkSweep::_preserved_oop_stack;
  52 Stack<markOop, mtGC>          MarkSweep::_preserved_mark_stack;
  53 size_t                  MarkSweep::_preserved_count = 0;
  54 size_t                  MarkSweep::_preserved_count_max = 0;
  55 PreservedMark*          MarkSweep::_preserved_marks = NULL;
  56 ReferenceProcessor*     MarkSweep::_ref_processor   = NULL;
  57 STWGCTimer*             MarkSweep::_gc_timer        = NULL;
  58 SerialOldTracer*        MarkSweep::_gc_tracer       = NULL;
  59 
  60 MarkSweep::FollowRootClosure  MarkSweep::follow_root_closure;
  61 
  62 MarkAndPushClosure            MarkSweep::mark_and_push_closure;
  63 CLDToOopClosure               MarkSweep::follow_cld_closure(&mark_and_push_closure);
  64 CLDToOopClosure               MarkSweep::adjust_cld_closure(&adjust_pointer_closure);
  65 
  66 inline void MarkSweep::mark_object(oop obj) {
  67   // some marks may contain information we need to preserve so we store them away
  68   // and overwrite the mark.  We'll restore it at the end of markSweep.
  69   markOop mark = obj->mark_raw();
  70   obj->set_mark_raw(markOopDesc::prototype()->set_marked());
  71 
  72   if (mark->must_be_preserved(obj)) {
  73     preserve_mark(obj, mark);
  74   }
  75 }
  76 
  77 template <class T> inline void MarkSweep::mark_and_push(T* p) {
  78   T heap_oop = RawAccess<>::oop_load(p);
  79   if (!CompressedOops::is_null(heap_oop)) {
  80     oop obj = CompressedOops::decode_not_null(heap_oop);
  81     if (!obj->mark_raw()->is_marked()) {
  82       mark_object(obj);
  83       _marking_stack.push(obj);
  84     }
  85   }
  86 }
  87 
  88 inline void MarkSweep::follow_klass(Klass* klass) {
  89   oop op = klass->klass_holder();
  90   MarkSweep::mark_and_push(&op);
  91 }
  92 
  93 inline void MarkSweep::follow_cld(ClassLoaderData* cld) {
  94   MarkSweep::follow_cld_closure.do_cld(cld);
  95 }
  96 
  97 template <typename T>
  98 inline void MarkAndPushClosure::do_oop_nv(T* p)                 { MarkSweep::mark_and_push(p); }
  99 void MarkAndPushClosure::do_oop(oop* p)                         { do_oop_nv(p); }
 100 void MarkAndPushClosure::do_oop(narrowOop* p)                   { do_oop_nv(p); }
 101 inline bool MarkAndPushClosure::do_metadata_nv()                { return true; }
 102 bool MarkAndPushClosure::do_metadata()                          { return do_metadata_nv(); }
 103 inline void MarkAndPushClosure::do_klass_nv(Klass* k)           { MarkSweep::follow_klass(k); }
 104 void MarkAndPushClosure::do_klass(Klass* k)                     { do_klass_nv(k); }
 105 inline void MarkAndPushClosure::do_cld_nv(ClassLoaderData* cld) { MarkSweep::follow_cld(cld); }
 106 void MarkAndPushClosure::do_cld(ClassLoaderData* cld)           { do_cld_nv(cld); }
 107 
 108 template <class T> inline void MarkSweep::KeepAliveClosure::do_oop_work(T* p) {
 109   mark_and_push(p);
 110 }
 111 
 112 void MarkSweep::push_objarray(oop obj, size_t index) {
 113   ObjArrayTask task(obj, index);
 114   assert(task.is_valid(), "bad ObjArrayTask");
 115   _objarray_stack.push(task);
 116 }
 117 
 118 inline void MarkSweep::follow_array(objArrayOop array) {
 119   MarkSweep::follow_klass(array->klass());
 120   // Don't push empty arrays to avoid unnecessary work.
 121   if (array->length() > 0) {
 122     MarkSweep::push_objarray(array, 0);
 123   }
 124 }
 125 
 126 inline void MarkSweep::follow_object(oop obj) {
 127   assert(obj->is_gc_marked(), "should be marked");
 128   if (obj->is_objArray()) {
 129     // Handle object arrays explicitly to allow them to
 130     // be split into chunks if needed.
 131     MarkSweep::follow_array((objArrayOop)obj);
 132   } else {
 133     obj->oop_iterate(&mark_and_push_closure);
 134   }
 135 }
 136 
 137 void MarkSweep::follow_array_chunk(objArrayOop array, int index) {
 138   const int len = array->length();
 139   const int beg_index = index;
 140   assert(beg_index < len || len == 0, "index too large");
 141 
 142   const int stride = MIN2(len - beg_index, (int) ObjArrayMarkingStride);
 143   const int end_index = beg_index + stride;
 144 
 145   array->oop_iterate_range(&mark_and_push_closure, beg_index, end_index);
 146 
 147   if (end_index < len) {
 148     MarkSweep::push_objarray(array, end_index); // Push the continuation.
 149   }
 150 }
 151 
 152 void MarkSweep::follow_stack() {
 153   do {
 154     while (!_marking_stack.is_empty()) {
 155       oop obj = _marking_stack.pop();
 156       assert (obj->is_gc_marked(), "p must be marked");
 157       follow_object(obj);
 158     }
 159     // Process ObjArrays one at a time to avoid marking stack bloat.
 160     if (!_objarray_stack.is_empty()) {
 161       ObjArrayTask task = _objarray_stack.pop();
 162       follow_array_chunk(objArrayOop(task.obj()), task.index());
 163     }
 164   } while (!_marking_stack.is_empty() || !_objarray_stack.is_empty());
 165 }
 166 
 167 MarkSweep::FollowStackClosure MarkSweep::follow_stack_closure;
 168 
 169 void MarkSweep::FollowStackClosure::do_void() { follow_stack(); }
 170 
 171 template <class T> inline void MarkSweep::follow_root(T* p) {
 172   assert(!Universe::heap()->is_in_reserved(p),
 173          "roots shouldn't be things within the heap");
 174   T heap_oop = RawAccess<>::oop_load(p);
 175   if (!CompressedOops::is_null(heap_oop)) {
 176     oop obj = CompressedOops::decode_not_null(heap_oop);
 177     if (!obj->mark_raw()->is_marked()) {
 178       mark_object(obj);
 179       follow_object(obj);
 180     }
 181   }
 182   follow_stack();
 183 }
 184 
 185 void MarkSweep::FollowRootClosure::do_oop(oop* p)       { follow_root(p); }
 186 void MarkSweep::FollowRootClosure::do_oop(narrowOop* p) { follow_root(p); }
 187 
 188 void PreservedMark::adjust_pointer() {
 189   MarkSweep::adjust_pointer(&_obj);
 190 }
 191 
 192 void PreservedMark::restore() {
 193   _obj->set_mark_raw(_mark);
 194 }
 195 
 196 // We preserve the mark which should be replaced at the end and the location
 197 // that it will go.  Note that the object that this markOop belongs to isn't
 198 // currently at that address but it will be after phase4
 199 void MarkSweep::preserve_mark(oop obj, markOop mark) {
 200   // We try to store preserved marks in the to space of the new generation since
 201   // this is storage which should be available.  Most of the time this should be
 202   // sufficient space for the marks we need to preserve but if it isn't we fall
 203   // back to using Stacks to keep track of the overflow.
 204   if (_preserved_count < _preserved_count_max) {
 205     _preserved_marks[_preserved_count++].init(obj, mark);
 206   } else {
 207     _preserved_mark_stack.push(mark);
 208     _preserved_oop_stack.push(obj);
 209   }
 210 }
 211 
 212 void MarkSweep::set_ref_processor(ReferenceProcessor* rp) {
 213   _ref_processor = rp;
 214   mark_and_push_closure.set_ref_discoverer(_ref_processor);
 215 }
 216 
 217 AdjustPointerClosure MarkSweep::adjust_pointer_closure;
 218 
 219 template <typename T>
 220 void AdjustPointerClosure::do_oop_nv(T* p)      { MarkSweep::adjust_pointer(p); }
 221 void AdjustPointerClosure::do_oop(oop* p)       { do_oop_nv(p); }
 222 void AdjustPointerClosure::do_oop(narrowOop* p) { do_oop_nv(p); }
 223 
 224 void MarkSweep::adjust_marks() {
 225   assert( _preserved_oop_stack.size() == _preserved_mark_stack.size(),
 226          "inconsistent preserved oop stacks");
 227 
 228   // adjust the oops we saved earlier
 229   for (size_t i = 0; i < _preserved_count; i++) {
 230     _preserved_marks[i].adjust_pointer();
 231   }
 232 
 233   // deal with the overflow stack
 234   StackIterator<oop, mtGC> iter(_preserved_oop_stack);
 235   while (!iter.is_empty()) {
 236     oop* p = iter.next_addr();
 237     adjust_pointer(p);
 238   }
 239 }
 240 
 241 void MarkSweep::restore_marks() {
 242   assert(_preserved_oop_stack.size() == _preserved_mark_stack.size(),
 243          "inconsistent preserved oop stacks");
 244   log_trace(gc)("Restoring " SIZE_FORMAT " marks", _preserved_count + _preserved_oop_stack.size());
 245 
 246   // restore the marks we saved earlier
 247   for (size_t i = 0; i < _preserved_count; i++) {
 248     _preserved_marks[i].restore();
 249   }
 250 
 251   // deal with the overflow
 252   while (!_preserved_oop_stack.is_empty()) {
 253     oop obj       = _preserved_oop_stack.pop();
 254     markOop mark  = _preserved_mark_stack.pop();
 255     obj->set_mark_raw(mark);
 256   }
 257 }
 258 
 259 MarkSweep::IsAliveClosure   MarkSweep::is_alive;
 260 
 261 bool MarkSweep::IsAliveClosure::do_object_b(oop p) { return p->is_gc_marked(); }
 262 
 263 MarkSweep::KeepAliveClosure MarkSweep::keep_alive;
 264 
 265 void MarkSweep::KeepAliveClosure::do_oop(oop* p)       { MarkSweep::KeepAliveClosure::do_oop_work(p); }
 266 void MarkSweep::KeepAliveClosure::do_oop(narrowOop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); }
 267 
 268 void MarkSweep::initialize() {
 269   MarkSweep::_gc_timer = new (ResourceObj::C_HEAP, mtGC) STWGCTimer();
 270   MarkSweep::_gc_tracer = new (ResourceObj::C_HEAP, mtGC) SerialOldTracer();
 271 }
 272 
 273 // Generate MS specialized oop_oop_iterate functions.
 274 SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_MS(ALL_KLASS_OOP_OOP_ITERATE_DEFN)