1 /* 2 * Copyright (c) 1997, 2021, 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/genCollectedHeap.hpp" 30 #include "gc/shared/gcTimer.hpp" 31 #include "gc/shared/gcTrace.hpp" 32 #include "gc/shared/gc_globals.hpp" 33 #include "memory/iterator.inline.hpp" 34 #include "memory/universe.hpp" 35 #include "oops/access.inline.hpp" 36 #include "oops/compressedOops.inline.hpp" 37 #include "oops/instanceClassLoaderKlass.inline.hpp" 38 #include "oops/instanceKlass.inline.hpp" 39 #include "oops/instanceMirrorKlass.inline.hpp" 40 #include "oops/instanceRefKlass.inline.hpp" 41 #include "oops/methodData.hpp" 42 #include "oops/objArrayKlass.inline.hpp" 43 #include "oops/oop.inline.hpp" 44 #include "oops/typeArrayOop.inline.hpp" 45 #include "utilities/macros.hpp" 46 #include "utilities/stack.inline.hpp" 47 48 uint MarkSweep::_total_invocations = 0; 49 50 Stack<oop, mtGC> MarkSweep::_marking_stack; 51 Stack<ObjArrayTask, mtGC> MarkSweep::_objarray_stack; 52 53 Stack<PreservedMark, mtGC> MarkSweep::_preserved_overflow_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 StringDedup::Requests* MarkSweep::_string_dedup_requests = NULL; 62 63 MarkSweep::FollowRootClosure MarkSweep::follow_root_closure; 64 65 MarkAndPushClosure MarkSweep::mark_and_push_closure; 66 CLDToOopClosure MarkSweep::follow_cld_closure(&mark_and_push_closure, ClassLoaderData::_claim_strong); 67 68 template <class T> inline void MarkSweep::KeepAliveClosure::do_oop_work(T* p) { 69 mark_and_push(p); 70 } 71 72 void MarkSweep::push_objarray(oop obj, size_t index) { 73 ObjArrayTask task(obj, index); 74 assert(task.is_valid(), "bad ObjArrayTask"); 75 _objarray_stack.push(task); 76 } 77 78 inline void MarkSweep::follow_array(objArrayOop array) { 79 MarkSweep::follow_klass(array->klass()); 80 // Don't push empty arrays to avoid unnecessary work. 81 if (array->length() > 0) { 82 MarkSweep::push_objarray(array, 0); 83 } 84 } 85 86 inline void MarkSweep::follow_object(oop obj) { 87 assert(obj->is_gc_marked(), "should be marked"); 88 if (obj->is_objArray()) { 89 // Handle object arrays explicitly to allow them to 90 // be split into chunks if needed. 91 MarkSweep::follow_array((objArrayOop)obj); 92 } else { 93 obj->oop_iterate(&mark_and_push_closure); 94 } 95 } 96 97 void MarkSweep::follow_array_chunk(objArrayOop array, int index) { 98 const int len = array->length(); 99 const int beg_index = index; 100 assert(beg_index < len || len == 0, "index too large"); 101 102 const int stride = MIN2(len - beg_index, (int) ObjArrayMarkingStride); 103 const int end_index = beg_index + stride; 104 105 array->oop_iterate_range(&mark_and_push_closure, beg_index, end_index); 106 107 if (end_index < len) { 108 MarkSweep::push_objarray(array, end_index); // Push the continuation. 109 } 110 } 111 112 void MarkSweep::follow_stack() { 113 do { 114 while (!_marking_stack.is_empty()) { 115 oop obj = _marking_stack.pop(); 116 assert (obj->is_gc_marked(), "p must be marked"); 117 follow_object(obj); 118 } 119 // Process ObjArrays one at a time to avoid marking stack bloat. 120 if (!_objarray_stack.is_empty()) { 121 ObjArrayTask task = _objarray_stack.pop(); 122 follow_array_chunk(objArrayOop(task.obj()), task.index()); 123 } 124 } while (!_marking_stack.is_empty() || !_objarray_stack.is_empty()); 125 } 126 127 MarkSweep::FollowStackClosure MarkSweep::follow_stack_closure; 128 129 void MarkSweep::FollowStackClosure::do_void() { follow_stack(); } 130 131 template <class T> inline void MarkSweep::follow_root(T* p) { 132 assert(!Universe::heap()->is_in(p), 133 "roots shouldn't be things within the heap"); 134 T heap_oop = RawAccess<>::oop_load(p); 135 if (!CompressedOops::is_null(heap_oop)) { 136 oop obj = CompressedOops::decode_not_null(heap_oop); 137 if (!obj->mark().is_marked()) { 138 mark_object(obj); 139 follow_object(obj); 140 } 141 } 142 follow_stack(); 143 } 144 145 void MarkSweep::FollowRootClosure::do_oop(oop* p) { follow_root(p); } 146 void MarkSweep::FollowRootClosure::do_oop(narrowOop* p) { follow_root(p); } 147 148 void PreservedMark::adjust_pointer(const SlidingForwarding* const forwarding) { 149 MarkSweep::adjust_pointer(forwarding, &_obj); 150 } 151 152 void PreservedMark::restore() { 153 _obj->set_mark(_mark); 154 } 155 156 // We preserve the mark which should be replaced at the end and the location 157 // that it will go. Note that the object that this markWord belongs to isn't 158 // currently at that address but it will be after phase4 159 void MarkSweep::preserve_mark(oop obj, markWord mark) { 160 // We try to store preserved marks in the to space of the new generation since 161 // this is storage which should be available. Most of the time this should be 162 // sufficient space for the marks we need to preserve but if it isn't we fall 163 // back to using Stacks to keep track of the overflow. 164 if (_preserved_count < _preserved_count_max) { 165 _preserved_marks[_preserved_count++] = PreservedMark(obj, mark); 166 } else { 167 _preserved_overflow_stack.push(PreservedMark(obj, mark)); 168 } 169 } 170 171 void MarkSweep::set_ref_processor(ReferenceProcessor* rp) { 172 _ref_processor = rp; 173 mark_and_push_closure.set_ref_discoverer(_ref_processor); 174 } 175 176 void MarkSweep::adjust_marks() { 177 const SlidingForwarding* const forwarding = GenCollectedHeap::heap()->forwarding(); 178 179 // adjust the oops we saved earlier 180 for (size_t i = 0; i < _preserved_count; i++) { 181 _preserved_marks[i].adjust_pointer(forwarding); 182 } 183 184 // deal with the overflow stack 185 StackIterator<PreservedMark, mtGC> iter(_preserved_overflow_stack); 186 while (!iter.is_empty()) { 187 PreservedMark* p = iter.next_addr(); 188 p->adjust_pointer(forwarding); 189 } 190 } 191 192 void MarkSweep::restore_marks() { 193 log_trace(gc)("Restoring " SIZE_FORMAT " marks", _preserved_count + _preserved_overflow_stack.size()); 194 195 // restore the marks we saved earlier 196 for (size_t i = 0; i < _preserved_count; i++) { 197 _preserved_marks[i].restore(); 198 } 199 200 // deal with the overflow 201 while (!_preserved_overflow_stack.is_empty()) { 202 PreservedMark p = _preserved_overflow_stack.pop(); 203 p.restore(); 204 } 205 } 206 207 MarkSweep::IsAliveClosure MarkSweep::is_alive; 208 209 bool MarkSweep::IsAliveClosure::do_object_b(oop p) { return p->is_gc_marked(); } 210 211 MarkSweep::KeepAliveClosure MarkSweep::keep_alive; 212 213 void MarkSweep::KeepAliveClosure::do_oop(oop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); } 214 void MarkSweep::KeepAliveClosure::do_oop(narrowOop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); } 215 216 void MarkSweep::initialize() { 217 MarkSweep::_gc_timer = new (ResourceObj::C_HEAP, mtGC) STWGCTimer(); 218 MarkSweep::_gc_tracer = new (ResourceObj::C_HEAP, mtGC) SerialOldTracer(); 219 MarkSweep::_string_dedup_requests = new StringDedup::Requests(); 220 } --- EOF ---