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