1 /* 2 * Copyright (c) 1997, 2010, 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/markSweep.inline.hpp" 28 #include "gc_interface/collectedHeap.inline.hpp" 29 #include "oops/methodDataOop.hpp" 30 #include "oops/objArrayKlass.inline.hpp" 31 #include "oops/oop.inline.hpp" 32 33 GrowableArray<oop>* MarkSweep::_marking_stack = NULL; 34 GrowableArray<ObjArrayTask>* MarkSweep::_objarray_stack = NULL; 35 GrowableArray<Klass*>* MarkSweep::_revisit_klass_stack = NULL; 36 GrowableArray<DataLayout*>* MarkSweep::_revisit_mdo_stack = NULL; 37 38 GrowableArray<oop>* MarkSweep::_preserved_oop_stack = NULL; 39 GrowableArray<markOop>* MarkSweep::_preserved_mark_stack= NULL; 40 size_t MarkSweep::_preserved_count = 0; 41 size_t MarkSweep::_preserved_count_max = 0; 42 PreservedMark* MarkSweep::_preserved_marks = NULL; 43 ReferenceProcessor* MarkSweep::_ref_processor = NULL; 44 45 #ifdef VALIDATE_MARK_SWEEP 46 GrowableArray<void*>* MarkSweep::_root_refs_stack = NULL; 47 GrowableArray<oop> * MarkSweep::_live_oops = NULL; 48 GrowableArray<oop> * MarkSweep::_live_oops_moved_to = NULL; 49 GrowableArray<size_t>* MarkSweep::_live_oops_size = NULL; 50 size_t MarkSweep::_live_oops_index = 0; 51 size_t MarkSweep::_live_oops_index_at_perm = 0; 52 GrowableArray<void*>* MarkSweep::_other_refs_stack = NULL; 53 GrowableArray<void*>* MarkSweep::_adjusted_pointers = NULL; 54 bool MarkSweep::_pointer_tracking = false; 55 bool MarkSweep::_root_tracking = true; 56 57 GrowableArray<HeapWord*>* MarkSweep::_cur_gc_live_oops = NULL; 58 GrowableArray<HeapWord*>* MarkSweep::_cur_gc_live_oops_moved_to = NULL; 59 GrowableArray<size_t> * MarkSweep::_cur_gc_live_oops_size = NULL; 60 GrowableArray<HeapWord*>* MarkSweep::_last_gc_live_oops = NULL; 61 GrowableArray<HeapWord*>* MarkSweep::_last_gc_live_oops_moved_to = NULL; 62 GrowableArray<size_t> * MarkSweep::_last_gc_live_oops_size = NULL; 63 #endif 64 65 void MarkSweep::revisit_weak_klass_link(Klass* k) { 66 _revisit_klass_stack->push(k); 67 } 68 69 void MarkSweep::follow_weak_klass_links() { 70 // All klasses on the revisit stack are marked at this point. 71 // Update and follow all subklass, sibling and implementor links. 72 if (PrintRevisitStats) { 73 gclog_or_tty->print_cr("#classes in system dictionary = %d", SystemDictionary::number_of_classes()); 74 gclog_or_tty->print_cr("Revisit klass stack length = %d", _revisit_klass_stack->length()); 75 } 76 for (int i = 0; i < _revisit_klass_stack->length(); i++) { 77 _revisit_klass_stack->at(i)->follow_weak_klass_links(&is_alive,&keep_alive); 78 } 79 follow_stack(); 80 } 81 82 void MarkSweep::revisit_mdo(DataLayout* p) { 83 _revisit_mdo_stack->push(p); 84 } 85 86 void MarkSweep::follow_mdo_weak_refs() { 87 // All strongly reachable oops have been marked at this point; 88 // we can visit and clear any weak references from MDO's which 89 // we memoized during the strong marking phase. 90 assert(_marking_stack->is_empty(), "Marking stack should be empty"); 91 if (PrintRevisitStats) { 92 gclog_or_tty->print_cr("#classes in system dictionary = %d", SystemDictionary::number_of_classes()); 93 gclog_or_tty->print_cr("Revisit MDO stack length = %d", _revisit_mdo_stack->length()); 94 } 95 for (int i = 0; i < _revisit_mdo_stack->length(); i++) { 96 _revisit_mdo_stack->at(i)->follow_weak_refs(&is_alive); 97 } 98 follow_stack(); 99 } 100 101 MarkSweep::FollowRootClosure MarkSweep::follow_root_closure; 102 CodeBlobToOopClosure MarkSweep::follow_code_root_closure(&MarkSweep::follow_root_closure, /*do_marking=*/ true); 103 104 void MarkSweep::FollowRootClosure::do_oop(oop* p) { follow_root(p); } 105 void MarkSweep::FollowRootClosure::do_oop(narrowOop* p) { follow_root(p); } 106 107 MarkSweep::MarkAndPushClosure MarkSweep::mark_and_push_closure; 108 109 void MarkSweep::MarkAndPushClosure::do_oop(oop* p) { mark_and_push(p); } 110 void MarkSweep::MarkAndPushClosure::do_oop(narrowOop* p) { mark_and_push(p); } 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 obj->follow_contents(); 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 objArrayKlass* const k = (objArrayKlass*)task.obj()->blueprint(); 123 k->oop_follow_contents(task.obj(), task.index()); 124 } 125 } while (!_marking_stack->is_empty() || !_objarray_stack->is_empty()); 126 } 127 128 MarkSweep::FollowStackClosure MarkSweep::follow_stack_closure; 129 130 void MarkSweep::FollowStackClosure::do_void() { follow_stack(); } 131 132 // We preserve the mark which should be replaced at the end and the location that it 133 // will go. Note that the object that this markOop belongs to isn't currently at that 134 // address but it will be after phase4 135 void MarkSweep::preserve_mark(oop obj, markOop mark) { 136 // we try to store preserved marks in the to space of the new generation since this 137 // is storage which should be available. Most of the time this should be sufficient 138 // space for the marks we need to preserve but if it isn't we fall back in using 139 // GrowableArrays to keep track of the overflow. 140 if (_preserved_count < _preserved_count_max) { 141 _preserved_marks[_preserved_count++].init(obj, mark); 142 } else { 143 if (_preserved_mark_stack == NULL) { 144 _preserved_mark_stack = new (ResourceObj::C_HEAP) GrowableArray<markOop>(40, true); 145 _preserved_oop_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(40, true); 146 } 147 _preserved_mark_stack->push(mark); 148 _preserved_oop_stack->push(obj); 149 } 150 } 151 152 MarkSweep::AdjustPointerClosure MarkSweep::adjust_root_pointer_closure(true); 153 MarkSweep::AdjustPointerClosure MarkSweep::adjust_pointer_closure(false); 154 155 void MarkSweep::AdjustPointerClosure::do_oop(oop* p) { adjust_pointer(p, _is_root); } 156 void MarkSweep::AdjustPointerClosure::do_oop(narrowOop* p) { adjust_pointer(p, _is_root); } 157 158 void MarkSweep::adjust_marks() { 159 assert(_preserved_oop_stack == NULL || 160 _preserved_oop_stack->length() == _preserved_mark_stack->length(), 161 "inconsistent preserved oop stacks"); 162 163 // adjust the oops we saved earlier 164 for (size_t i = 0; i < _preserved_count; i++) { 165 _preserved_marks[i].adjust_pointer(); 166 } 167 168 // deal with the overflow stack 169 if (_preserved_oop_stack) { 170 for (int i = 0; i < _preserved_oop_stack->length(); i++) { 171 oop* p = _preserved_oop_stack->adr_at(i); 172 adjust_pointer(p); 173 } 174 } 175 } 176 177 void MarkSweep::restore_marks() { 178 assert(_preserved_oop_stack == NULL || 179 _preserved_oop_stack->length() == _preserved_mark_stack->length(), 180 "inconsistent preserved oop stacks"); 181 if (PrintGC && Verbose) { 182 gclog_or_tty->print_cr("Restoring %d marks", _preserved_count + 183 (_preserved_oop_stack ? _preserved_oop_stack->length() : 0)); 184 } 185 186 // restore the marks we saved earlier 187 for (size_t i = 0; i < _preserved_count; i++) { 188 _preserved_marks[i].restore(); 189 } 190 191 // deal with the overflow 192 if (_preserved_oop_stack) { 193 for (int i = 0; i < _preserved_oop_stack->length(); i++) { 194 oop obj = _preserved_oop_stack->at(i); 195 markOop mark = _preserved_mark_stack->at(i); 196 obj->set_mark(mark); 197 } 198 } 199 } 200 201 #ifdef VALIDATE_MARK_SWEEP 202 203 void MarkSweep::track_adjusted_pointer(void* p, bool isroot) { 204 if (!ValidateMarkSweep) 205 return; 206 207 if (!isroot) { 208 if (_pointer_tracking) { 209 guarantee(_adjusted_pointers->contains(p), "should have seen this pointer"); 210 _adjusted_pointers->remove(p); 211 } 212 } else { 213 ptrdiff_t index = _root_refs_stack->find(p); 214 if (index != -1) { 215 int l = _root_refs_stack->length(); 216 if (l > 0 && l - 1 != index) { 217 void* last = _root_refs_stack->pop(); 218 assert(last != p, "should be different"); 219 _root_refs_stack->at_put(index, last); 220 } else { 221 _root_refs_stack->remove(p); 222 } 223 } 224 } 225 } 226 227 void MarkSweep::check_adjust_pointer(void* p) { 228 _adjusted_pointers->push(p); 229 } 230 231 class AdjusterTracker: public OopClosure { 232 public: 233 AdjusterTracker() {} 234 void do_oop(oop* o) { MarkSweep::check_adjust_pointer(o); } 235 void do_oop(narrowOop* o) { MarkSweep::check_adjust_pointer(o); } 236 }; 237 238 void MarkSweep::track_interior_pointers(oop obj) { 239 if (ValidateMarkSweep) { 240 _adjusted_pointers->clear(); 241 _pointer_tracking = true; 242 243 AdjusterTracker checker; 244 obj->oop_iterate(&checker); 245 } 246 } 247 248 void MarkSweep::check_interior_pointers() { 249 if (ValidateMarkSweep) { 250 _pointer_tracking = false; 251 guarantee(_adjusted_pointers->length() == 0, "should have processed the same pointers"); 252 } 253 } 254 255 void MarkSweep::reset_live_oop_tracking(bool at_perm) { 256 if (ValidateMarkSweep) { 257 guarantee((size_t)_live_oops->length() == _live_oops_index, "should be at end of live oops"); 258 _live_oops_index = at_perm ? _live_oops_index_at_perm : 0; 259 } 260 } 261 262 void MarkSweep::register_live_oop(oop p, size_t size) { 263 if (ValidateMarkSweep) { 264 _live_oops->push(p); 265 _live_oops_size->push(size); 266 _live_oops_index++; 267 } 268 } 269 270 void MarkSweep::validate_live_oop(oop p, size_t size) { 271 if (ValidateMarkSweep) { 272 oop obj = _live_oops->at((int)_live_oops_index); 273 guarantee(obj == p, "should be the same object"); 274 guarantee(_live_oops_size->at((int)_live_oops_index) == size, "should be the same size"); 275 _live_oops_index++; 276 } 277 } 278 279 void MarkSweep::live_oop_moved_to(HeapWord* q, size_t size, 280 HeapWord* compaction_top) { 281 assert(oop(q)->forwardee() == NULL || oop(q)->forwardee() == oop(compaction_top), 282 "should be moved to forwarded location"); 283 if (ValidateMarkSweep) { 284 MarkSweep::validate_live_oop(oop(q), size); 285 _live_oops_moved_to->push(oop(compaction_top)); 286 } 287 if (RecordMarkSweepCompaction) { 288 _cur_gc_live_oops->push(q); 289 _cur_gc_live_oops_moved_to->push(compaction_top); 290 _cur_gc_live_oops_size->push(size); 291 } 292 } 293 294 void MarkSweep::compaction_complete() { 295 if (RecordMarkSweepCompaction) { 296 GrowableArray<HeapWord*>* _tmp_live_oops = _cur_gc_live_oops; 297 GrowableArray<HeapWord*>* _tmp_live_oops_moved_to = _cur_gc_live_oops_moved_to; 298 GrowableArray<size_t> * _tmp_live_oops_size = _cur_gc_live_oops_size; 299 300 _cur_gc_live_oops = _last_gc_live_oops; 301 _cur_gc_live_oops_moved_to = _last_gc_live_oops_moved_to; 302 _cur_gc_live_oops_size = _last_gc_live_oops_size; 303 _last_gc_live_oops = _tmp_live_oops; 304 _last_gc_live_oops_moved_to = _tmp_live_oops_moved_to; 305 _last_gc_live_oops_size = _tmp_live_oops_size; 306 } 307 } 308 309 void MarkSweep::print_new_location_of_heap_address(HeapWord* q) { 310 if (!RecordMarkSweepCompaction) { 311 tty->print_cr("Requires RecordMarkSweepCompaction to be enabled"); 312 return; 313 } 314 315 if (_last_gc_live_oops == NULL) { 316 tty->print_cr("No compaction information gathered yet"); 317 return; 318 } 319 320 for (int i = 0; i < _last_gc_live_oops->length(); i++) { 321 HeapWord* old_oop = _last_gc_live_oops->at(i); 322 size_t sz = _last_gc_live_oops_size->at(i); 323 if (old_oop <= q && q < (old_oop + sz)) { 324 HeapWord* new_oop = _last_gc_live_oops_moved_to->at(i); 325 size_t offset = (q - old_oop); 326 tty->print_cr("Address " PTR_FORMAT, q); 327 tty->print_cr(" Was in oop " PTR_FORMAT ", size " SIZE_FORMAT ", at offset " SIZE_FORMAT, old_oop, sz, offset); 328 tty->print_cr(" Now in oop " PTR_FORMAT ", actual address " PTR_FORMAT, new_oop, new_oop + offset); 329 return; 330 } 331 } 332 333 tty->print_cr("Address " PTR_FORMAT " not found in live oop information from last GC", q); 334 } 335 #endif //VALIDATE_MARK_SWEEP 336 337 MarkSweep::IsAliveClosure MarkSweep::is_alive; 338 339 void MarkSweep::IsAliveClosure::do_object(oop p) { ShouldNotReachHere(); } 340 bool MarkSweep::IsAliveClosure::do_object_b(oop p) { return p->is_gc_marked(); } 341 342 MarkSweep::KeepAliveClosure MarkSweep::keep_alive; 343 344 void MarkSweep::KeepAliveClosure::do_oop(oop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); } 345 void MarkSweep::KeepAliveClosure::do_oop(narrowOop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); } 346 347 void marksweep_init() { /* empty */ } 348 349 #ifndef PRODUCT 350 351 void MarkSweep::trace(const char* msg) { 352 if (TraceMarkSweep) 353 gclog_or_tty->print("%s", msg); 354 } 355 356 #endif