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