src/hotspot/share/gc/serial/markSweep.cpp
changeset 47216 71c04702a3d5
parent 46810 7dad333205cd
child 47885 5caa1d5f74c1
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 1997, 2017, 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 "oops/typeArrayOop.inline.hpp"
       
    41 #include "utilities/macros.hpp"
       
    42 #include "utilities/stack.inline.hpp"
       
    43 #if INCLUDE_ALL_GCS
       
    44 #include "gc/g1/g1StringDedup.hpp"
       
    45 #endif // INCLUDE_ALL_GCS
       
    46 
       
    47 uint                    MarkSweep::_total_invocations = 0;
       
    48 
       
    49 Stack<oop, mtGC>              MarkSweep::_marking_stack;
       
    50 Stack<ObjArrayTask, mtGC>     MarkSweep::_objarray_stack;
       
    51 
       
    52 Stack<oop, mtGC>              MarkSweep::_preserved_oop_stack;
       
    53 Stack<markOop, mtGC>          MarkSweep::_preserved_mark_stack;
       
    54 size_t                  MarkSweep::_preserved_count = 0;
       
    55 size_t                  MarkSweep::_preserved_count_max = 0;
       
    56 PreservedMark*          MarkSweep::_preserved_marks = NULL;
       
    57 ReferenceProcessor*     MarkSweep::_ref_processor   = NULL;
       
    58 STWGCTimer*             MarkSweep::_gc_timer        = NULL;
       
    59 SerialOldTracer*        MarkSweep::_gc_tracer       = NULL;
       
    60 
       
    61 MarkSweep::FollowRootClosure  MarkSweep::follow_root_closure;
       
    62 
       
    63 MarkAndPushClosure            MarkSweep::mark_and_push_closure;
       
    64 CLDToOopClosure               MarkSweep::follow_cld_closure(&mark_and_push_closure);
       
    65 CLDToOopClosure               MarkSweep::adjust_cld_closure(&adjust_pointer_closure);
       
    66 
       
    67 inline void MarkSweep::mark_object(oop obj) {
       
    68 #if INCLUDE_ALL_GCS
       
    69   if (G1StringDedup::is_enabled()) {
       
    70     // We must enqueue the object before it is marked
       
    71     // as we otherwise can't read the object's age.
       
    72     G1StringDedup::enqueue_from_mark(obj);
       
    73   }
       
    74 #endif
       
    75   // some marks may contain information we need to preserve so we store them away
       
    76   // and overwrite the mark.  We'll restore it at the end of markSweep.
       
    77   markOop mark = obj->mark();
       
    78   obj->set_mark(markOopDesc::prototype()->set_marked());
       
    79 
       
    80   if (mark->must_be_preserved(obj)) {
       
    81     preserve_mark(obj, mark);
       
    82   }
       
    83 }
       
    84 
       
    85 template <class T> inline void MarkSweep::mark_and_push(T* p) {
       
    86   T heap_oop = oopDesc::load_heap_oop(p);
       
    87   if (!oopDesc::is_null(heap_oop)) {
       
    88     oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
       
    89     if (!obj->mark()->is_marked() &&
       
    90         !is_closed_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     if (!obj->mark()->is_marked() &&
       
   187         !is_closed_archive_object(obj)) {
       
   188       mark_object(obj);
       
   189       follow_object(obj);
       
   190     }
       
   191   }
       
   192   follow_stack();
       
   193 }
       
   194 
       
   195 void MarkSweep::FollowRootClosure::do_oop(oop* p)       { follow_root(p); }
       
   196 void MarkSweep::FollowRootClosure::do_oop(narrowOop* p) { follow_root(p); }
       
   197 
       
   198 void PreservedMark::adjust_pointer() {
       
   199   MarkSweep::adjust_pointer(&_obj);
       
   200 }
       
   201 
       
   202 void PreservedMark::restore() {
       
   203   _obj->set_mark(_mark);
       
   204 }
       
   205 
       
   206 // We preserve the mark which should be replaced at the end and the location
       
   207 // that it will go.  Note that the object that this markOop belongs to isn't
       
   208 // currently at that address but it will be after phase4
       
   209 void MarkSweep::preserve_mark(oop obj, markOop mark) {
       
   210   // We try to store preserved marks in the to space of the new generation since
       
   211   // this is storage which should be available.  Most of the time this should be
       
   212   // sufficient space for the marks we need to preserve but if it isn't we fall
       
   213   // back to using Stacks to keep track of the overflow.
       
   214   if (_preserved_count < _preserved_count_max) {
       
   215     _preserved_marks[_preserved_count++].init(obj, mark);
       
   216   } else {
       
   217     _preserved_mark_stack.push(mark);
       
   218     _preserved_oop_stack.push(obj);
       
   219   }
       
   220 }
       
   221 
       
   222 void MarkSweep::set_ref_processor(ReferenceProcessor* rp) {
       
   223   _ref_processor = rp;
       
   224   mark_and_push_closure.set_ref_processor(_ref_processor);
       
   225 }
       
   226 
       
   227 AdjustPointerClosure MarkSweep::adjust_pointer_closure;
       
   228 
       
   229 template <typename T>
       
   230 void AdjustPointerClosure::do_oop_nv(T* p)      { MarkSweep::adjust_pointer(p); }
       
   231 void AdjustPointerClosure::do_oop(oop* p)       { do_oop_nv(p); }
       
   232 void AdjustPointerClosure::do_oop(narrowOop* p) { do_oop_nv(p); }
       
   233 
       
   234 void MarkSweep::adjust_marks() {
       
   235   assert( _preserved_oop_stack.size() == _preserved_mark_stack.size(),
       
   236          "inconsistent preserved oop stacks");
       
   237 
       
   238   // adjust the oops we saved earlier
       
   239   for (size_t i = 0; i < _preserved_count; i++) {
       
   240     _preserved_marks[i].adjust_pointer();
       
   241   }
       
   242 
       
   243   // deal with the overflow stack
       
   244   StackIterator<oop, mtGC> iter(_preserved_oop_stack);
       
   245   while (!iter.is_empty()) {
       
   246     oop* p = iter.next_addr();
       
   247     adjust_pointer(p);
       
   248   }
       
   249 }
       
   250 
       
   251 void MarkSweep::restore_marks() {
       
   252   assert(_preserved_oop_stack.size() == _preserved_mark_stack.size(),
       
   253          "inconsistent preserved oop stacks");
       
   254   log_trace(gc)("Restoring " SIZE_FORMAT " marks", _preserved_count + _preserved_oop_stack.size());
       
   255 
       
   256   // restore the marks we saved earlier
       
   257   for (size_t i = 0; i < _preserved_count; i++) {
       
   258     _preserved_marks[i].restore();
       
   259   }
       
   260 
       
   261   // deal with the overflow
       
   262   while (!_preserved_oop_stack.is_empty()) {
       
   263     oop obj       = _preserved_oop_stack.pop();
       
   264     markOop mark  = _preserved_mark_stack.pop();
       
   265     obj->set_mark(mark);
       
   266   }
       
   267 }
       
   268 
       
   269 MarkSweep::IsAliveClosure   MarkSweep::is_alive;
       
   270 
       
   271 bool MarkSweep::IsAliveClosure::do_object_b(oop p) { return p->is_gc_marked() || is_closed_archive_object(p); }
       
   272 
       
   273 MarkSweep::KeepAliveClosure MarkSweep::keep_alive;
       
   274 
       
   275 void MarkSweep::KeepAliveClosure::do_oop(oop* p)       { MarkSweep::KeepAliveClosure::do_oop_work(p); }
       
   276 void MarkSweep::KeepAliveClosure::do_oop(narrowOop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); }
       
   277 
       
   278 void marksweep_init() {
       
   279   MarkSweep::_gc_timer = new (ResourceObj::C_HEAP, mtGC) STWGCTimer();
       
   280   MarkSweep::_gc_tracer = new (ResourceObj::C_HEAP, mtGC) SerialOldTracer();
       
   281 }
       
   282 
       
   283 // Generate MS specialized oop_oop_iterate functions.
       
   284 SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_MS(ALL_KLASS_OOP_OOP_ITERATE_DEFN)