Sat, 01 Dec 2007 00:00:00 +0000
Initial load
duke@435 | 1 | /* |
duke@435 | 2 | * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | #include "incls/_precompiled.incl" |
duke@435 | 26 | #include "incls/_markSweep.cpp.incl" |
duke@435 | 27 | |
duke@435 | 28 | GrowableArray<oop>* MarkSweep::_marking_stack = NULL; |
duke@435 | 29 | GrowableArray<Klass*>* MarkSweep::_revisit_klass_stack = NULL; |
duke@435 | 30 | |
duke@435 | 31 | GrowableArray<oop>* MarkSweep::_preserved_oop_stack = NULL; |
duke@435 | 32 | GrowableArray<markOop>* MarkSweep::_preserved_mark_stack= NULL; |
duke@435 | 33 | size_t MarkSweep::_preserved_count = 0; |
duke@435 | 34 | size_t MarkSweep::_preserved_count_max = 0; |
duke@435 | 35 | PreservedMark* MarkSweep::_preserved_marks = NULL; |
duke@435 | 36 | ReferenceProcessor* MarkSweep::_ref_processor = NULL; |
duke@435 | 37 | |
duke@435 | 38 | #ifdef VALIDATE_MARK_SWEEP |
duke@435 | 39 | GrowableArray<oop*>* MarkSweep::_root_refs_stack = NULL; |
duke@435 | 40 | GrowableArray<oop> * MarkSweep::_live_oops = NULL; |
duke@435 | 41 | GrowableArray<oop> * MarkSweep::_live_oops_moved_to = NULL; |
duke@435 | 42 | GrowableArray<size_t>* MarkSweep::_live_oops_size = NULL; |
duke@435 | 43 | size_t MarkSweep::_live_oops_index = 0; |
duke@435 | 44 | size_t MarkSweep::_live_oops_index_at_perm = 0; |
duke@435 | 45 | GrowableArray<oop*>* MarkSweep::_other_refs_stack = NULL; |
duke@435 | 46 | GrowableArray<oop*>* MarkSweep::_adjusted_pointers = NULL; |
duke@435 | 47 | bool MarkSweep::_pointer_tracking = false; |
duke@435 | 48 | bool MarkSweep::_root_tracking = true; |
duke@435 | 49 | |
duke@435 | 50 | GrowableArray<HeapWord*>* MarkSweep::_cur_gc_live_oops = NULL; |
duke@435 | 51 | GrowableArray<HeapWord*>* MarkSweep::_cur_gc_live_oops_moved_to = NULL; |
duke@435 | 52 | GrowableArray<size_t> * MarkSweep::_cur_gc_live_oops_size = NULL; |
duke@435 | 53 | GrowableArray<HeapWord*>* MarkSweep::_last_gc_live_oops = NULL; |
duke@435 | 54 | GrowableArray<HeapWord*>* MarkSweep::_last_gc_live_oops_moved_to = NULL; |
duke@435 | 55 | GrowableArray<size_t> * MarkSweep::_last_gc_live_oops_size = NULL; |
duke@435 | 56 | #endif |
duke@435 | 57 | |
duke@435 | 58 | void MarkSweep::revisit_weak_klass_link(Klass* k) { |
duke@435 | 59 | _revisit_klass_stack->push(k); |
duke@435 | 60 | } |
duke@435 | 61 | |
duke@435 | 62 | |
duke@435 | 63 | void MarkSweep::follow_weak_klass_links() { |
duke@435 | 64 | // All klasses on the revisit stack are marked at this point. |
duke@435 | 65 | // Update and follow all subklass, sibling and implementor links. |
duke@435 | 66 | for (int i = 0; i < _revisit_klass_stack->length(); i++) { |
duke@435 | 67 | _revisit_klass_stack->at(i)->follow_weak_klass_links(&is_alive,&keep_alive); |
duke@435 | 68 | } |
duke@435 | 69 | follow_stack(); |
duke@435 | 70 | } |
duke@435 | 71 | |
duke@435 | 72 | |
duke@435 | 73 | void MarkSweep::mark_and_follow(oop* p) { |
duke@435 | 74 | assert(Universe::heap()->is_in_reserved(p), |
duke@435 | 75 | "we should only be traversing objects here"); |
duke@435 | 76 | oop m = *p; |
duke@435 | 77 | if (m != NULL && !m->mark()->is_marked()) { |
duke@435 | 78 | mark_object(m); |
duke@435 | 79 | m->follow_contents(); // Follow contents of the marked object |
duke@435 | 80 | } |
duke@435 | 81 | } |
duke@435 | 82 | |
duke@435 | 83 | void MarkSweep::_mark_and_push(oop* p) { |
duke@435 | 84 | // Push marked object, contents will be followed later |
duke@435 | 85 | oop m = *p; |
duke@435 | 86 | mark_object(m); |
duke@435 | 87 | _marking_stack->push(m); |
duke@435 | 88 | } |
duke@435 | 89 | |
duke@435 | 90 | MarkSweep::MarkAndPushClosure MarkSweep::mark_and_push_closure; |
duke@435 | 91 | |
duke@435 | 92 | void MarkSweep::follow_root(oop* p) { |
duke@435 | 93 | assert(!Universe::heap()->is_in_reserved(p), |
duke@435 | 94 | "roots shouldn't be things within the heap"); |
duke@435 | 95 | #ifdef VALIDATE_MARK_SWEEP |
duke@435 | 96 | if (ValidateMarkSweep) { |
duke@435 | 97 | guarantee(!_root_refs_stack->contains(p), "should only be in here once"); |
duke@435 | 98 | _root_refs_stack->push(p); |
duke@435 | 99 | } |
duke@435 | 100 | #endif |
duke@435 | 101 | oop m = *p; |
duke@435 | 102 | if (m != NULL && !m->mark()->is_marked()) { |
duke@435 | 103 | mark_object(m); |
duke@435 | 104 | m->follow_contents(); // Follow contents of the marked object |
duke@435 | 105 | } |
duke@435 | 106 | follow_stack(); |
duke@435 | 107 | } |
duke@435 | 108 | |
duke@435 | 109 | MarkSweep::FollowRootClosure MarkSweep::follow_root_closure; |
duke@435 | 110 | |
duke@435 | 111 | void MarkSweep::follow_stack() { |
duke@435 | 112 | while (!_marking_stack->is_empty()) { |
duke@435 | 113 | oop obj = _marking_stack->pop(); |
duke@435 | 114 | assert (obj->is_gc_marked(), "p must be marked"); |
duke@435 | 115 | obj->follow_contents(); |
duke@435 | 116 | } |
duke@435 | 117 | } |
duke@435 | 118 | |
duke@435 | 119 | MarkSweep::FollowStackClosure MarkSweep::follow_stack_closure; |
duke@435 | 120 | |
duke@435 | 121 | |
duke@435 | 122 | // We preserve the mark which should be replaced at the end and the location that it |
duke@435 | 123 | // will go. Note that the object that this markOop belongs to isn't currently at that |
duke@435 | 124 | // address but it will be after phase4 |
duke@435 | 125 | void MarkSweep::preserve_mark(oop obj, markOop mark) { |
duke@435 | 126 | // we try to store preserved marks in the to space of the new generation since this |
duke@435 | 127 | // is storage which should be available. Most of the time this should be sufficient |
duke@435 | 128 | // space for the marks we need to preserve but if it isn't we fall back in using |
duke@435 | 129 | // GrowableArrays to keep track of the overflow. |
duke@435 | 130 | if (_preserved_count < _preserved_count_max) { |
duke@435 | 131 | _preserved_marks[_preserved_count++].init(obj, mark); |
duke@435 | 132 | } else { |
duke@435 | 133 | if (_preserved_mark_stack == NULL) { |
duke@435 | 134 | _preserved_mark_stack = new (ResourceObj::C_HEAP) GrowableArray<markOop>(40, true); |
duke@435 | 135 | _preserved_oop_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(40, true); |
duke@435 | 136 | } |
duke@435 | 137 | _preserved_mark_stack->push(mark); |
duke@435 | 138 | _preserved_oop_stack->push(obj); |
duke@435 | 139 | } |
duke@435 | 140 | } |
duke@435 | 141 | |
duke@435 | 142 | MarkSweep::AdjustPointerClosure MarkSweep::adjust_root_pointer_closure(true); |
duke@435 | 143 | MarkSweep::AdjustPointerClosure MarkSweep::adjust_pointer_closure(false); |
duke@435 | 144 | |
duke@435 | 145 | void MarkSweep::adjust_marks() { |
duke@435 | 146 | assert(_preserved_oop_stack == NULL || |
duke@435 | 147 | _preserved_oop_stack->length() == _preserved_mark_stack->length(), |
duke@435 | 148 | "inconsistent preserved oop stacks"); |
duke@435 | 149 | |
duke@435 | 150 | // adjust the oops we saved earlier |
duke@435 | 151 | for (size_t i = 0; i < _preserved_count; i++) { |
duke@435 | 152 | _preserved_marks[i].adjust_pointer(); |
duke@435 | 153 | } |
duke@435 | 154 | |
duke@435 | 155 | // deal with the overflow stack |
duke@435 | 156 | if (_preserved_oop_stack) { |
duke@435 | 157 | for (int i = 0; i < _preserved_oop_stack->length(); i++) { |
duke@435 | 158 | oop* p = _preserved_oop_stack->adr_at(i); |
duke@435 | 159 | adjust_pointer(p); |
duke@435 | 160 | } |
duke@435 | 161 | } |
duke@435 | 162 | } |
duke@435 | 163 | |
duke@435 | 164 | void MarkSweep::restore_marks() { |
duke@435 | 165 | assert(_preserved_oop_stack == NULL || |
duke@435 | 166 | _preserved_oop_stack->length() == _preserved_mark_stack->length(), |
duke@435 | 167 | "inconsistent preserved oop stacks"); |
duke@435 | 168 | if (PrintGC && Verbose) { |
duke@435 | 169 | gclog_or_tty->print_cr("Restoring %d marks", _preserved_count + |
duke@435 | 170 | (_preserved_oop_stack ? _preserved_oop_stack->length() : 0)); |
duke@435 | 171 | } |
duke@435 | 172 | |
duke@435 | 173 | // restore the marks we saved earlier |
duke@435 | 174 | for (size_t i = 0; i < _preserved_count; i++) { |
duke@435 | 175 | _preserved_marks[i].restore(); |
duke@435 | 176 | } |
duke@435 | 177 | |
duke@435 | 178 | // deal with the overflow |
duke@435 | 179 | if (_preserved_oop_stack) { |
duke@435 | 180 | for (int i = 0; i < _preserved_oop_stack->length(); i++) { |
duke@435 | 181 | oop obj = _preserved_oop_stack->at(i); |
duke@435 | 182 | markOop mark = _preserved_mark_stack->at(i); |
duke@435 | 183 | obj->set_mark(mark); |
duke@435 | 184 | } |
duke@435 | 185 | } |
duke@435 | 186 | } |
duke@435 | 187 | |
duke@435 | 188 | #ifdef VALIDATE_MARK_SWEEP |
duke@435 | 189 | |
duke@435 | 190 | void MarkSweep::track_adjusted_pointer(oop* p, oop newobj, bool isroot) { |
duke@435 | 191 | if (!ValidateMarkSweep) |
duke@435 | 192 | return; |
duke@435 | 193 | |
duke@435 | 194 | if (!isroot) { |
duke@435 | 195 | if (_pointer_tracking) { |
duke@435 | 196 | guarantee(_adjusted_pointers->contains(p), "should have seen this pointer"); |
duke@435 | 197 | _adjusted_pointers->remove(p); |
duke@435 | 198 | } |
duke@435 | 199 | } else { |
duke@435 | 200 | ptrdiff_t index = _root_refs_stack->find(p); |
duke@435 | 201 | if (index != -1) { |
duke@435 | 202 | int l = _root_refs_stack->length(); |
duke@435 | 203 | if (l > 0 && l - 1 != index) { |
duke@435 | 204 | oop* last = _root_refs_stack->pop(); |
duke@435 | 205 | assert(last != p, "should be different"); |
duke@435 | 206 | _root_refs_stack->at_put(index, last); |
duke@435 | 207 | } else { |
duke@435 | 208 | _root_refs_stack->remove(p); |
duke@435 | 209 | } |
duke@435 | 210 | } |
duke@435 | 211 | } |
duke@435 | 212 | } |
duke@435 | 213 | |
duke@435 | 214 | |
duke@435 | 215 | void MarkSweep::check_adjust_pointer(oop* p) { |
duke@435 | 216 | _adjusted_pointers->push(p); |
duke@435 | 217 | } |
duke@435 | 218 | |
duke@435 | 219 | |
duke@435 | 220 | class AdjusterTracker: public OopClosure { |
duke@435 | 221 | public: |
duke@435 | 222 | AdjusterTracker() {}; |
duke@435 | 223 | void do_oop(oop* o) { MarkSweep::check_adjust_pointer(o); } |
duke@435 | 224 | }; |
duke@435 | 225 | |
duke@435 | 226 | |
duke@435 | 227 | void MarkSweep::track_interior_pointers(oop obj) { |
duke@435 | 228 | if (ValidateMarkSweep) { |
duke@435 | 229 | _adjusted_pointers->clear(); |
duke@435 | 230 | _pointer_tracking = true; |
duke@435 | 231 | |
duke@435 | 232 | AdjusterTracker checker; |
duke@435 | 233 | obj->oop_iterate(&checker); |
duke@435 | 234 | } |
duke@435 | 235 | } |
duke@435 | 236 | |
duke@435 | 237 | |
duke@435 | 238 | void MarkSweep::check_interior_pointers() { |
duke@435 | 239 | if (ValidateMarkSweep) { |
duke@435 | 240 | _pointer_tracking = false; |
duke@435 | 241 | guarantee(_adjusted_pointers->length() == 0, "should have processed the same pointers"); |
duke@435 | 242 | } |
duke@435 | 243 | } |
duke@435 | 244 | |
duke@435 | 245 | |
duke@435 | 246 | void MarkSweep::reset_live_oop_tracking(bool at_perm) { |
duke@435 | 247 | if (ValidateMarkSweep) { |
duke@435 | 248 | guarantee((size_t)_live_oops->length() == _live_oops_index, "should be at end of live oops"); |
duke@435 | 249 | _live_oops_index = at_perm ? _live_oops_index_at_perm : 0; |
duke@435 | 250 | } |
duke@435 | 251 | } |
duke@435 | 252 | |
duke@435 | 253 | |
duke@435 | 254 | void MarkSweep::register_live_oop(oop p, size_t size) { |
duke@435 | 255 | if (ValidateMarkSweep) { |
duke@435 | 256 | _live_oops->push(p); |
duke@435 | 257 | _live_oops_size->push(size); |
duke@435 | 258 | _live_oops_index++; |
duke@435 | 259 | } |
duke@435 | 260 | } |
duke@435 | 261 | |
duke@435 | 262 | void MarkSweep::validate_live_oop(oop p, size_t size) { |
duke@435 | 263 | if (ValidateMarkSweep) { |
duke@435 | 264 | oop obj = _live_oops->at((int)_live_oops_index); |
duke@435 | 265 | guarantee(obj == p, "should be the same object"); |
duke@435 | 266 | guarantee(_live_oops_size->at((int)_live_oops_index) == size, "should be the same size"); |
duke@435 | 267 | _live_oops_index++; |
duke@435 | 268 | } |
duke@435 | 269 | } |
duke@435 | 270 | |
duke@435 | 271 | void MarkSweep::live_oop_moved_to(HeapWord* q, size_t size, |
duke@435 | 272 | HeapWord* compaction_top) { |
duke@435 | 273 | assert(oop(q)->forwardee() == NULL || oop(q)->forwardee() == oop(compaction_top), |
duke@435 | 274 | "should be moved to forwarded location"); |
duke@435 | 275 | if (ValidateMarkSweep) { |
duke@435 | 276 | MarkSweep::validate_live_oop(oop(q), size); |
duke@435 | 277 | _live_oops_moved_to->push(oop(compaction_top)); |
duke@435 | 278 | } |
duke@435 | 279 | if (RecordMarkSweepCompaction) { |
duke@435 | 280 | _cur_gc_live_oops->push(q); |
duke@435 | 281 | _cur_gc_live_oops_moved_to->push(compaction_top); |
duke@435 | 282 | _cur_gc_live_oops_size->push(size); |
duke@435 | 283 | } |
duke@435 | 284 | } |
duke@435 | 285 | |
duke@435 | 286 | |
duke@435 | 287 | void MarkSweep::compaction_complete() { |
duke@435 | 288 | if (RecordMarkSweepCompaction) { |
duke@435 | 289 | GrowableArray<HeapWord*>* _tmp_live_oops = _cur_gc_live_oops; |
duke@435 | 290 | GrowableArray<HeapWord*>* _tmp_live_oops_moved_to = _cur_gc_live_oops_moved_to; |
duke@435 | 291 | GrowableArray<size_t> * _tmp_live_oops_size = _cur_gc_live_oops_size; |
duke@435 | 292 | |
duke@435 | 293 | _cur_gc_live_oops = _last_gc_live_oops; |
duke@435 | 294 | _cur_gc_live_oops_moved_to = _last_gc_live_oops_moved_to; |
duke@435 | 295 | _cur_gc_live_oops_size = _last_gc_live_oops_size; |
duke@435 | 296 | _last_gc_live_oops = _tmp_live_oops; |
duke@435 | 297 | _last_gc_live_oops_moved_to = _tmp_live_oops_moved_to; |
duke@435 | 298 | _last_gc_live_oops_size = _tmp_live_oops_size; |
duke@435 | 299 | } |
duke@435 | 300 | } |
duke@435 | 301 | |
duke@435 | 302 | |
duke@435 | 303 | void MarkSweep::print_new_location_of_heap_address(HeapWord* q) { |
duke@435 | 304 | if (!RecordMarkSweepCompaction) { |
duke@435 | 305 | tty->print_cr("Requires RecordMarkSweepCompaction to be enabled"); |
duke@435 | 306 | return; |
duke@435 | 307 | } |
duke@435 | 308 | |
duke@435 | 309 | if (_last_gc_live_oops == NULL) { |
duke@435 | 310 | tty->print_cr("No compaction information gathered yet"); |
duke@435 | 311 | return; |
duke@435 | 312 | } |
duke@435 | 313 | |
duke@435 | 314 | for (int i = 0; i < _last_gc_live_oops->length(); i++) { |
duke@435 | 315 | HeapWord* old_oop = _last_gc_live_oops->at(i); |
duke@435 | 316 | size_t sz = _last_gc_live_oops_size->at(i); |
duke@435 | 317 | if (old_oop <= q && q < (old_oop + sz)) { |
duke@435 | 318 | HeapWord* new_oop = _last_gc_live_oops_moved_to->at(i); |
duke@435 | 319 | size_t offset = (q - old_oop); |
duke@435 | 320 | tty->print_cr("Address " PTR_FORMAT, q); |
duke@435 | 321 | tty->print_cr(" Was in oop " PTR_FORMAT ", size %d, at offset %d", old_oop, sz, offset); |
duke@435 | 322 | tty->print_cr(" Now in oop " PTR_FORMAT ", actual address " PTR_FORMAT, new_oop, new_oop + offset); |
duke@435 | 323 | return; |
duke@435 | 324 | } |
duke@435 | 325 | } |
duke@435 | 326 | |
duke@435 | 327 | tty->print_cr("Address " PTR_FORMAT " not found in live oop information from last GC", q); |
duke@435 | 328 | } |
duke@435 | 329 | #endif //VALIDATE_MARK_SWEEP |
duke@435 | 330 | |
duke@435 | 331 | MarkSweep::IsAliveClosure MarkSweep::is_alive; |
duke@435 | 332 | |
duke@435 | 333 | void MarkSweep::KeepAliveClosure::do_oop(oop* p) { |
duke@435 | 334 | #ifdef VALIDATE_MARK_SWEEP |
duke@435 | 335 | if (ValidateMarkSweep) { |
duke@435 | 336 | if (!Universe::heap()->is_in_reserved(p)) { |
duke@435 | 337 | _root_refs_stack->push(p); |
duke@435 | 338 | } else { |
duke@435 | 339 | _other_refs_stack->push(p); |
duke@435 | 340 | } |
duke@435 | 341 | } |
duke@435 | 342 | #endif |
duke@435 | 343 | mark_and_push(p); |
duke@435 | 344 | } |
duke@435 | 345 | |
duke@435 | 346 | MarkSweep::KeepAliveClosure MarkSweep::keep_alive; |
duke@435 | 347 | |
duke@435 | 348 | void marksweep_init() { /* empty */ } |
duke@435 | 349 | |
duke@435 | 350 | #ifndef PRODUCT |
duke@435 | 351 | |
duke@435 | 352 | void MarkSweep::trace(const char* msg) { |
duke@435 | 353 | if (TraceMarkSweep) |
duke@435 | 354 | gclog_or_tty->print("%s", msg); |
duke@435 | 355 | } |
duke@435 | 356 | |
duke@435 | 357 | #endif |