1 /*
   2  * Copyright (c) 1997, 2015, 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/shared/collectedHeap.inline.hpp"
  29 #include "gc/shared/gcTimer.hpp"
  30 #include "gc/shared/gcTrace.hpp"
  31 #include "gc/shared/specialized_oop_closures.hpp"
  32 #include "memory/iterator.inline.hpp"
  33 #include "oops/instanceClassLoaderKlass.inline.hpp"
  34 #include "oops/instanceKlass.inline.hpp"
  35 #include "oops/instanceMirrorKlass.inline.hpp"
  36 #include "oops/instanceRefKlass.inline.hpp"
  37 #include "oops/methodData.hpp"
  38 #include "oops/objArrayKlass.inline.hpp"
  39 #include "oops/oop.inline.hpp"
  40 #include "utilities/macros.hpp"
  41 #include "utilities/stack.inline.hpp"
  42 #if INCLUDE_ALL_GCS
  43 #include "gc/g1/g1StringDedup.hpp"
  44 #endif // INCLUDE_ALL_GCS
  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 #if INCLUDE_ALL_GCS
  68   if (G1StringDedup::is_enabled()) {
  69     // We must enqueue the object before it is marked
  70     // as we otherwise can't read the object's age.
  71     G1StringDedup::enqueue_from_mark(obj);
  72   }
  73 #endif
  74   // some marks may contain information we need to preserve so we store them away
  75   // and overwrite the mark.  We'll restore it at the end of markSweep.
  76   markOop mark = obj->mark();
  77   obj->set_mark(markOopDesc::prototype()->set_marked());
  78 
  79   if (mark->must_be_preserved(obj)) {
  80     preserve_mark(obj, mark);
  81   }
  82 }
  83 
  84 template <class T> inline void MarkSweep::mark_and_push(T* p) {
  85   T heap_oop = oopDesc::load_heap_oop(p);
  86   if (!oopDesc::is_null(heap_oop)) {
  87     oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
  88     if (UseShenandoahGC) {
  89       obj = ShenandoahBarrierSet::resolve_and_update_oop_static(p, obj);
  90     }
  91     if (!obj->mark()->is_marked() &&
  92         !is_archive_object(obj)) {
  93       mark_object(obj);
  94       _marking_stack.push(obj);
  95     }
  96   }
  97 }
  98 
  99 inline void MarkSweep::follow_klass(Klass* klass) {
 100   oop op = klass->klass_holder();
 101   MarkSweep::mark_and_push(&op);
 102 }
 103 
 104 inline void MarkSweep::follow_cld(ClassLoaderData* cld) {
 105   MarkSweep::follow_cld_closure.do_cld(cld);
 106 }
 107 
 108 template <typename T>
 109 inline void MarkAndPushClosure::do_oop_nv(T* p)                 { MarkSweep::mark_and_push(p); }
 110 void MarkAndPushClosure::do_oop(oop* p)                         { do_oop_nv(p); }
 111 void MarkAndPushClosure::do_oop(narrowOop* p)                   { do_oop_nv(p); }
 112 inline bool MarkAndPushClosure::do_metadata_nv()                { return true; }
 113 bool MarkAndPushClosure::do_metadata()                          { return do_metadata_nv(); }
 114 inline void MarkAndPushClosure::do_klass_nv(Klass* k)           { MarkSweep::follow_klass(k); }
 115 void MarkAndPushClosure::do_klass(Klass* k)                     { do_klass_nv(k); }
 116 inline void MarkAndPushClosure::do_cld_nv(ClassLoaderData* cld) { MarkSweep::follow_cld(cld); }
 117 void MarkAndPushClosure::do_cld(ClassLoaderData* cld)           { do_cld_nv(cld); }
 118 
 119 template <class T> inline void MarkSweep::KeepAliveClosure::do_oop_work(T* p) {
 120   mark_and_push(p);
 121 }
 122 
 123 void MarkSweep::push_objarray(oop obj, size_t index) {
 124   ObjArrayTask task(obj, index);
 125   assert(task.is_valid(), "bad ObjArrayTask");
 126   _objarray_stack.push(task);
 127 }
 128 
 129 inline void MarkSweep::follow_array(objArrayOop array) {
 130   MarkSweep::follow_klass(array->klass());
 131   // Don't push empty arrays to avoid unnecessary work.
 132   if (array->length() > 0) {
 133     MarkSweep::push_objarray(array, 0);
 134   }
 135 }
 136 
 137 inline void MarkSweep::follow_object(oop obj) {
 138   assert(obj->is_gc_marked(), "should be marked");
 139   if (obj->is_objArray()) {
 140     // Handle object arrays explicitly to allow them to
 141     // be split into chunks if needed.
 142     MarkSweep::follow_array((objArrayOop)obj);
 143   } else {
 144     obj->oop_iterate(&mark_and_push_closure);
 145   }
 146 }
 147 
 148 void MarkSweep::follow_array_chunk(objArrayOop array, int index) {
 149   const int len = array->length();
 150   const int beg_index = index;
 151   assert(beg_index < len || len == 0, "index too large");
 152 
 153   const int stride = MIN2(len - beg_index, (int) ObjArrayMarkingStride);
 154   const int end_index = beg_index + stride;
 155 
 156   array->oop_iterate_range(&mark_and_push_closure, beg_index, end_index);
 157 
 158   if (end_index < len) {
 159     MarkSweep::push_objarray(array, end_index); // Push the continuation.
 160   }
 161 }
 162 
 163 void MarkSweep::follow_stack() {
 164   do {
 165     while (!_marking_stack.is_empty()) {
 166       oop obj = _marking_stack.pop();
 167       assert (obj->is_gc_marked(), "p must be marked");
 168       follow_object(obj);
 169     }
 170     // Process ObjArrays one at a time to avoid marking stack bloat.
 171     if (!_objarray_stack.is_empty()) {
 172       ObjArrayTask task = _objarray_stack.pop();
 173       follow_array_chunk(objArrayOop(task.obj()), task.index());
 174     }
 175   } while (!_marking_stack.is_empty() || !_objarray_stack.is_empty());
 176 }
 177 
 178 MarkSweep::FollowStackClosure MarkSweep::follow_stack_closure;
 179 
 180 void MarkSweep::FollowStackClosure::do_void() { follow_stack(); }
 181 
 182 template <class T> inline void MarkSweep::follow_root(T* p) {
 183   assert(!Universe::heap()->is_in_reserved(p),
 184          "roots shouldn't be things within the heap");
 185   T heap_oop = oopDesc::load_heap_oop(p);
 186   if (!oopDesc::is_null(heap_oop)) {
 187     oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
 188     if (UseShenandoahGC) {
 189       obj = ShenandoahBarrierSet::resolve_and_update_oop_static(p, obj);
 190     }
 191     if (!obj->mark()->is_marked() &&
 192         !is_archive_object(obj)) {
 193       mark_object(obj);
 194       follow_object(obj);
 195     }
 196   }
 197   follow_stack();
 198 }
 199 
 200 void MarkSweep::FollowRootClosure::do_oop(oop* p)       { follow_root(p); }
 201 void MarkSweep::FollowRootClosure::do_oop(narrowOop* p) { follow_root(p); }
 202 
 203 void PreservedMark::adjust_pointer() {
 204   MarkSweep::adjust_pointer(&_obj);
 205 }
 206 
 207 void PreservedMark::restore() {
 208   _obj->set_mark(_mark);
 209 }
 210 
 211 // We preserve the mark which should be replaced at the end and the location
 212 // that it will go.  Note that the object that this markOop belongs to isn't
 213 // currently at that address but it will be after phase4
 214 void MarkSweep::preserve_mark(oop obj, markOop mark) {
 215   // We try to store preserved marks in the to space of the new generation since
 216   // this is storage which should be available.  Most of the time this should be
 217   // sufficient space for the marks we need to preserve but if it isn't we fall
 218   // back to using Stacks to keep track of the overflow.
 219   if (_preserved_count < _preserved_count_max) {
 220     _preserved_marks[_preserved_count++].init(obj, mark);
 221   } else {
 222     _preserved_mark_stack.push(mark);
 223     _preserved_oop_stack.push(obj);
 224   }
 225 }
 226 
 227 void MarkSweep::set_ref_processor(ReferenceProcessor* rp) {
 228   _ref_processor = rp;
 229   mark_and_push_closure.set_ref_processor(_ref_processor);
 230 }
 231 
 232 MarkSweep::AdjustPointerClosure MarkSweep::adjust_pointer_closure;
 233 
 234 template <typename T>
 235 void MarkSweep::AdjustPointerClosure::do_oop_nv(T* p)      { adjust_pointer(p); }
 236 void MarkSweep::AdjustPointerClosure::do_oop(oop* p)       { do_oop_nv(p); }
 237 void MarkSweep::AdjustPointerClosure::do_oop(narrowOop* p) { do_oop_nv(p); }
 238 
 239 void MarkSweep::adjust_marks() {
 240   assert( _preserved_oop_stack.size() == _preserved_mark_stack.size(),
 241          "inconsistent preserved oop stacks");
 242 
 243   // adjust the oops we saved earlier
 244   for (size_t i = 0; i < _preserved_count; i++) {
 245     _preserved_marks[i].adjust_pointer();
 246   }
 247 
 248   // deal with the overflow stack
 249   StackIterator<oop, mtGC> iter(_preserved_oop_stack);
 250   while (!iter.is_empty()) {
 251     oop* p = iter.next_addr();
 252     adjust_pointer(p);
 253   }
 254 }
 255 
 256 void MarkSweep::restore_marks() {
 257   assert(_preserved_oop_stack.size() == _preserved_mark_stack.size(),
 258          "inconsistent preserved oop stacks");
 259   if (PrintGC && Verbose) {
 260     gclog_or_tty->print_cr("Restoring " SIZE_FORMAT " marks",
 261                            _preserved_count + _preserved_oop_stack.size());
 262   }
 263 
 264   // restore the marks we saved earlier
 265   for (size_t i = 0; i < _preserved_count; i++) {
 266     _preserved_marks[i].restore();
 267   }
 268 
 269   // deal with the overflow
 270   while (!_preserved_oop_stack.is_empty()) {
 271     oop obj       = _preserved_oop_stack.pop();
 272     markOop mark  = _preserved_mark_stack.pop();
 273     obj->set_mark(mark);
 274   }
 275 }
 276 
 277 MarkSweep::IsAliveClosure   MarkSweep::is_alive;
 278 
 279 bool MarkSweep::IsAliveClosure::do_object_b(oop p) { return p->is_gc_marked() || is_archive_object(p); }
 280 
 281 MarkSweep::KeepAliveClosure MarkSweep::keep_alive;
 282 
 283 void MarkSweep::KeepAliveClosure::do_oop(oop* p)       { MarkSweep::KeepAliveClosure::do_oop_work(p); }
 284 void MarkSweep::KeepAliveClosure::do_oop(narrowOop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); }
 285 
 286 void marksweep_init() {
 287   MarkSweep::_gc_timer = new (ResourceObj::C_HEAP, mtGC) STWGCTimer();
 288   MarkSweep::_gc_tracer = new (ResourceObj::C_HEAP, mtGC) SerialOldTracer();
 289 }
 290 
 291 int InstanceKlass::oop_ms_adjust_pointers(oop obj) {
 292   int size = size_helper();
 293   oop_oop_iterate_oop_maps<true>(obj, &MarkSweep::adjust_pointer_closure);
 294   return size;
 295 }
 296 
 297 int InstanceMirrorKlass::oop_ms_adjust_pointers(oop obj) {
 298   int size = oop_size(obj);
 299   InstanceKlass::oop_ms_adjust_pointers(obj);
 300 
 301   oop_oop_iterate_statics<true>(obj, &MarkSweep::adjust_pointer_closure);
 302   return size;
 303 }
 304 
 305 int InstanceClassLoaderKlass::oop_ms_adjust_pointers(oop obj) {
 306   return InstanceKlass::oop_ms_adjust_pointers(obj);
 307 }
 308 
 309 #ifdef ASSERT
 310 template <class T> static void trace_reference_gc(const char *s, oop obj,
 311                                                   T* referent_addr,
 312                                                   T* next_addr,
 313                                                   T* discovered_addr) {
 314   if(TraceReferenceGC && PrintGCDetails) {
 315     gclog_or_tty->print_cr("%s obj " PTR_FORMAT, s, p2i(obj));
 316     gclog_or_tty->print_cr("     referent_addr/* " PTR_FORMAT " / "
 317                            PTR_FORMAT, p2i(referent_addr),
 318                            p2i(referent_addr ?
 319                                (address)oopDesc::load_decode_heap_oop(referent_addr) : NULL));
 320     gclog_or_tty->print_cr("     next_addr/* " PTR_FORMAT " / "
 321                            PTR_FORMAT, p2i(next_addr),
 322                            p2i(next_addr ? (address)oopDesc::load_decode_heap_oop(next_addr) : NULL));
 323     gclog_or_tty->print_cr("     discovered_addr/* " PTR_FORMAT " / "
 324                            PTR_FORMAT, p2i(discovered_addr),
 325                            p2i(discovered_addr ?
 326                                (address)oopDesc::load_decode_heap_oop(discovered_addr) : NULL));
 327   }
 328 }
 329 #endif
 330 
 331 template <class T> void static adjust_object_specialized(oop obj) {
 332   T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);
 333   MarkSweep::adjust_pointer(referent_addr);
 334   T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);
 335   MarkSweep::adjust_pointer(next_addr);
 336   T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);
 337   MarkSweep::adjust_pointer(discovered_addr);
 338   debug_only(trace_reference_gc("InstanceRefKlass::oop_ms_adjust_pointers", obj,
 339                                 referent_addr, next_addr, discovered_addr);)
 340 }
 341 
 342 int InstanceRefKlass::oop_ms_adjust_pointers(oop obj) {
 343   int size = size_helper();
 344   InstanceKlass::oop_ms_adjust_pointers(obj);
 345 
 346   if (UseCompressedOops) {
 347     adjust_object_specialized<narrowOop>(obj);
 348   } else {
 349     adjust_object_specialized<oop>(obj);
 350   }
 351   return size;
 352 }
 353 
 354 int ObjArrayKlass::oop_ms_adjust_pointers(oop obj) {
 355   assert(obj->is_objArray(), "obj must be obj array");
 356   objArrayOop a = objArrayOop(obj);
 357   // Get size before changing pointers.
 358   // Don't call size() or oop_size() since that is a virtual call.
 359   int size = a->object_size();
 360   oop_oop_iterate_elements<true>(a, &MarkSweep::adjust_pointer_closure);
 361   return size;
 362 }
 363 
 364 int TypeArrayKlass::oop_ms_adjust_pointers(oop obj) {
 365   assert(obj->is_typeArray(), "must be a type array");
 366   typeArrayOop t = typeArrayOop(obj);
 367   // Performance tweak: We skip iterating over the klass pointer since we
 368   // know that Universe::TypeArrayKlass never moves.
 369   return t->object_size();
 370 }
 371 
 372 // Generate MS specialized oop_oop_iterate functions.
 373 SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_MS(ALL_KLASS_OOP_OOP_ITERATE_DEFN)