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