src/share/vm/memory/genMarkSweep.cpp

Mon, 23 Jun 2008 16:49:37 -0700

author
ysr
date
Mon, 23 Jun 2008 16:49:37 -0700
changeset 782
60fb9c4db4e6
parent 548
ba764ed4b6f2
child 631
d1605aabd0a1
child 698
12eea04c8b06
permissions
-rw-r--r--

6718086: CMS assert: _concurrent_iteration_safe_limit update missed
Summary: Initialize the field correctly in ContiguousSpace's constructor and initialize() methods, using the latter for the survivor spaces upon initial construction or a subsequent resizing of the young generation. Add some missing Space sub-class constructors.
Reviewed-by: apetrusenko

duke@435 1 /*
duke@435 2 * Copyright 2001-2007 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/_genMarkSweep.cpp.incl"
duke@435 27
duke@435 28 void GenMarkSweep::invoke_at_safepoint(int level, ReferenceProcessor* rp,
duke@435 29 bool clear_all_softrefs) {
duke@435 30 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
duke@435 31
duke@435 32 // hook up weak ref data so it can be used during Mark-Sweep
duke@435 33 assert(ref_processor() == NULL, "no stomping");
duke@435 34 _ref_processor = rp;
duke@435 35 assert(rp != NULL, "should be non-NULL");
duke@435 36
duke@435 37 TraceTime t1("Full GC", PrintGC && !PrintGCDetails, true, gclog_or_tty);
duke@435 38
duke@435 39 // When collecting the permanent generation methodOops may be moving,
duke@435 40 // so we either have to flush all bcp data or convert it into bci.
duke@435 41 CodeCache::gc_prologue();
duke@435 42 Threads::gc_prologue();
duke@435 43
duke@435 44 // Increment the invocation count for the permanent generation, since it is
duke@435 45 // implicitly collected whenever we do a full mark sweep collection.
duke@435 46 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 47 gch->perm_gen()->stat_record()->invocations++;
duke@435 48
duke@435 49 // Capture heap size before collection for printing.
duke@435 50 size_t gch_prev_used = gch->used();
duke@435 51
duke@435 52 // Some of the card table updates below assume that the perm gen is
duke@435 53 // also being collected.
duke@435 54 assert(level == gch->n_gens() - 1,
duke@435 55 "All generations are being collected, ergo perm gen too.");
duke@435 56
duke@435 57 // Capture used regions for each generation that will be
duke@435 58 // subject to collection, so that card table adjustments can
duke@435 59 // be made intelligently (see clear / invalidate further below).
duke@435 60 gch->save_used_regions(level, true /* perm */);
duke@435 61
duke@435 62 allocate_stacks();
duke@435 63
duke@435 64 mark_sweep_phase1(level, clear_all_softrefs);
duke@435 65
duke@435 66 mark_sweep_phase2();
duke@435 67
duke@435 68 // Don't add any more derived pointers during phase3
duke@435 69 COMPILER2_PRESENT(assert(DerivedPointerTable::is_active(), "Sanity"));
duke@435 70 COMPILER2_PRESENT(DerivedPointerTable::set_active(false));
duke@435 71
duke@435 72 mark_sweep_phase3(level);
duke@435 73
duke@435 74 VALIDATE_MARK_SWEEP_ONLY(
duke@435 75 if (ValidateMarkSweep) {
coleenp@548 76 guarantee(_root_refs_stack->length() == 0, "should be empty by now");
duke@435 77 }
duke@435 78 )
duke@435 79
duke@435 80 mark_sweep_phase4();
duke@435 81
duke@435 82 VALIDATE_MARK_SWEEP_ONLY(
duke@435 83 if (ValidateMarkSweep) {
duke@435 84 guarantee(_live_oops->length() == _live_oops_moved_to->length(),
duke@435 85 "should be the same size");
duke@435 86 }
duke@435 87 )
duke@435 88
duke@435 89 restore_marks();
duke@435 90
duke@435 91 // Set saved marks for allocation profiler (and other things? -- dld)
duke@435 92 // (Should this be in general part?)
duke@435 93 gch->save_marks();
duke@435 94
duke@435 95 deallocate_stacks();
duke@435 96
duke@435 97 // If compaction completely evacuated all generations younger than this
duke@435 98 // one, then we can clear the card table. Otherwise, we must invalidate
duke@435 99 // it (consider all cards dirty). In the future, we might consider doing
duke@435 100 // compaction within generations only, and doing card-table sliding.
duke@435 101 bool all_empty = true;
duke@435 102 for (int i = 0; all_empty && i < level; i++) {
duke@435 103 Generation* g = gch->get_gen(i);
duke@435 104 all_empty = all_empty && gch->get_gen(i)->used() == 0;
duke@435 105 }
duke@435 106 GenRemSet* rs = gch->rem_set();
duke@435 107 // Clear/invalidate below make use of the "prev_used_regions" saved earlier.
duke@435 108 if (all_empty) {
duke@435 109 // We've evacuated all generations below us.
duke@435 110 Generation* g = gch->get_gen(level);
duke@435 111 rs->clear_into_younger(g, true /* perm */);
duke@435 112 } else {
duke@435 113 // Invalidate the cards corresponding to the currently used
duke@435 114 // region and clear those corresponding to the evacuated region
duke@435 115 // of all generations just collected (i.e. level and younger).
duke@435 116 rs->invalidate_or_clear(gch->get_gen(level),
duke@435 117 true /* younger */,
duke@435 118 true /* perm */);
duke@435 119 }
duke@435 120
duke@435 121 Threads::gc_epilogue();
duke@435 122 CodeCache::gc_epilogue();
duke@435 123
duke@435 124 if (PrintGC && !PrintGCDetails) {
duke@435 125 gch->print_heap_change(gch_prev_used);
duke@435 126 }
duke@435 127
duke@435 128 // refs processing: clean slate
duke@435 129 _ref_processor = NULL;
duke@435 130
duke@435 131 // Update heap occupancy information which is used as
duke@435 132 // input to soft ref clearing policy at the next gc.
duke@435 133 Universe::update_heap_info_at_gc();
duke@435 134
duke@435 135 // Update time of last gc for all generations we collected
duke@435 136 // (which curently is all the generations in the heap).
duke@435 137 gch->update_time_of_last_gc(os::javaTimeMillis());
duke@435 138 }
duke@435 139
duke@435 140 void GenMarkSweep::allocate_stacks() {
duke@435 141 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 142 // Scratch request on behalf of oldest generation; will do no
duke@435 143 // allocation.
duke@435 144 ScratchBlock* scratch = gch->gather_scratch(gch->_gens[gch->_n_gens-1], 0);
duke@435 145
duke@435 146 // $$$ To cut a corner, we'll only use the first scratch block, and then
duke@435 147 // revert to malloc.
duke@435 148 if (scratch != NULL) {
duke@435 149 _preserved_count_max =
duke@435 150 scratch->num_words * HeapWordSize / sizeof(PreservedMark);
duke@435 151 } else {
duke@435 152 _preserved_count_max = 0;
duke@435 153 }
duke@435 154
duke@435 155 _preserved_marks = (PreservedMark*)scratch;
duke@435 156 _preserved_count = 0;
duke@435 157 _preserved_mark_stack = NULL;
duke@435 158 _preserved_oop_stack = NULL;
duke@435 159
duke@435 160 _marking_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(4000, true);
duke@435 161
duke@435 162 int size = SystemDictionary::number_of_classes() * 2;
duke@435 163 _revisit_klass_stack = new (ResourceObj::C_HEAP) GrowableArray<Klass*>(size, true);
duke@435 164
duke@435 165 #ifdef VALIDATE_MARK_SWEEP
duke@435 166 if (ValidateMarkSweep) {
coleenp@548 167 _root_refs_stack = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true);
coleenp@548 168 _other_refs_stack = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true);
coleenp@548 169 _adjusted_pointers = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true);
duke@435 170 _live_oops = new (ResourceObj::C_HEAP) GrowableArray<oop>(100, true);
duke@435 171 _live_oops_moved_to = new (ResourceObj::C_HEAP) GrowableArray<oop>(100, true);
duke@435 172 _live_oops_size = new (ResourceObj::C_HEAP) GrowableArray<size_t>(100, true);
duke@435 173 }
duke@435 174 if (RecordMarkSweepCompaction) {
duke@435 175 if (_cur_gc_live_oops == NULL) {
duke@435 176 _cur_gc_live_oops = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
duke@435 177 _cur_gc_live_oops_moved_to = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
duke@435 178 _cur_gc_live_oops_size = new(ResourceObj::C_HEAP) GrowableArray<size_t>(100, true);
duke@435 179 _last_gc_live_oops = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
duke@435 180 _last_gc_live_oops_moved_to = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
duke@435 181 _last_gc_live_oops_size = new(ResourceObj::C_HEAP) GrowableArray<size_t>(100, true);
duke@435 182 } else {
duke@435 183 _cur_gc_live_oops->clear();
duke@435 184 _cur_gc_live_oops_moved_to->clear();
duke@435 185 _cur_gc_live_oops_size->clear();
duke@435 186 }
duke@435 187 }
duke@435 188 #endif
duke@435 189 }
duke@435 190
duke@435 191
duke@435 192 void GenMarkSweep::deallocate_stacks() {
duke@435 193 if (_preserved_oop_stack) {
duke@435 194 delete _preserved_mark_stack;
duke@435 195 _preserved_mark_stack = NULL;
duke@435 196 delete _preserved_oop_stack;
duke@435 197 _preserved_oop_stack = NULL;
duke@435 198 }
duke@435 199
duke@435 200 delete _marking_stack;
duke@435 201 delete _revisit_klass_stack;
duke@435 202
duke@435 203 #ifdef VALIDATE_MARK_SWEEP
duke@435 204 if (ValidateMarkSweep) {
duke@435 205 delete _root_refs_stack;
duke@435 206 delete _other_refs_stack;
duke@435 207 delete _adjusted_pointers;
duke@435 208 delete _live_oops;
duke@435 209 delete _live_oops_size;
duke@435 210 delete _live_oops_moved_to;
duke@435 211 _live_oops_index = 0;
duke@435 212 _live_oops_index_at_perm = 0;
duke@435 213 }
duke@435 214 #endif
duke@435 215 }
duke@435 216
duke@435 217 void GenMarkSweep::mark_sweep_phase1(int level,
duke@435 218 bool clear_all_softrefs) {
duke@435 219 // Recursively traverse all live objects and mark them
duke@435 220 EventMark m("1 mark object");
duke@435 221 TraceTime tm("phase 1", PrintGC && Verbose, true, gclog_or_tty);
duke@435 222 trace(" 1");
duke@435 223
duke@435 224 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
duke@435 225
duke@435 226 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 227
duke@435 228 // Because follow_root_closure is created statically, cannot
duke@435 229 // use OopsInGenClosure constructor which takes a generation,
duke@435 230 // as the Universe has not been created when the static constructors
duke@435 231 // are run.
duke@435 232 follow_root_closure.set_orig_generation(gch->get_gen(level));
duke@435 233
duke@435 234 gch->gen_process_strong_roots(level,
duke@435 235 false, // Younger gens are not roots.
duke@435 236 true, // Collecting permanent generation.
duke@435 237 SharedHeap::SO_SystemClasses,
duke@435 238 &follow_root_closure, &follow_root_closure);
duke@435 239
duke@435 240 // Process reference objects found during marking
duke@435 241 {
duke@435 242 ReferencePolicy *soft_ref_policy;
duke@435 243 if (clear_all_softrefs) {
duke@435 244 soft_ref_policy = new AlwaysClearPolicy();
duke@435 245 } else {
duke@435 246 #ifdef COMPILER2
duke@435 247 soft_ref_policy = new LRUMaxHeapPolicy();
duke@435 248 #else
duke@435 249 soft_ref_policy = new LRUCurrentHeapPolicy();
duke@435 250 #endif // COMPILER2
duke@435 251 }
duke@435 252 assert(soft_ref_policy != NULL,"No soft reference policy");
duke@435 253 ref_processor()->process_discovered_references(
duke@435 254 soft_ref_policy, &is_alive, &keep_alive,
duke@435 255 &follow_stack_closure, NULL);
duke@435 256 }
duke@435 257
duke@435 258 // Follow system dictionary roots and unload classes
duke@435 259 bool purged_class = SystemDictionary::do_unloading(&is_alive);
duke@435 260
duke@435 261 // Follow code cache roots
duke@435 262 CodeCache::do_unloading(&is_alive, &keep_alive, purged_class);
duke@435 263 follow_stack(); // Flush marking stack
duke@435 264
duke@435 265 // Update subklass/sibling/implementor links of live klasses
duke@435 266 follow_weak_klass_links();
duke@435 267 assert(_marking_stack->is_empty(), "just drained");
duke@435 268
duke@435 269 // Visit symbol and interned string tables and delete unmarked oops
duke@435 270 SymbolTable::unlink(&is_alive);
duke@435 271 StringTable::unlink(&is_alive);
duke@435 272
duke@435 273 assert(_marking_stack->is_empty(), "stack should be empty by now");
duke@435 274 }
duke@435 275
duke@435 276
duke@435 277 void GenMarkSweep::mark_sweep_phase2() {
duke@435 278 // Now all live objects are marked, compute the new object addresses.
duke@435 279
duke@435 280 // It is imperative that we traverse perm_gen LAST. If dead space is
duke@435 281 // allowed a range of dead object may get overwritten by a dead int
duke@435 282 // array. If perm_gen is not traversed last a klassOop may get
duke@435 283 // overwritten. This is fine since it is dead, but if the class has dead
duke@435 284 // instances we have to skip them, and in order to find their size we
duke@435 285 // need the klassOop!
duke@435 286 //
duke@435 287 // It is not required that we traverse spaces in the same order in
duke@435 288 // phase2, phase3 and phase4, but the ValidateMarkSweep live oops
duke@435 289 // tracking expects us to do so. See comment under phase4.
duke@435 290
duke@435 291 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 292 Generation* pg = gch->perm_gen();
duke@435 293
duke@435 294 EventMark m("2 compute new addresses");
duke@435 295 TraceTime tm("phase 2", PrintGC && Verbose, true, gclog_or_tty);
duke@435 296 trace("2");
duke@435 297
duke@435 298 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
duke@435 299
duke@435 300 gch->prepare_for_compaction();
duke@435 301
duke@435 302 VALIDATE_MARK_SWEEP_ONLY(_live_oops_index_at_perm = _live_oops_index);
duke@435 303 CompactPoint perm_cp(pg, NULL, NULL);
duke@435 304 pg->prepare_for_compaction(&perm_cp);
duke@435 305 }
duke@435 306
duke@435 307 class GenAdjustPointersClosure: public GenCollectedHeap::GenClosure {
duke@435 308 public:
duke@435 309 void do_generation(Generation* gen) {
duke@435 310 gen->adjust_pointers();
duke@435 311 }
duke@435 312 };
duke@435 313
duke@435 314 void GenMarkSweep::mark_sweep_phase3(int level) {
duke@435 315 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 316 Generation* pg = gch->perm_gen();
duke@435 317
duke@435 318 // Adjust the pointers to reflect the new locations
duke@435 319 EventMark m("3 adjust pointers");
duke@435 320 TraceTime tm("phase 3", PrintGC && Verbose, true, gclog_or_tty);
duke@435 321 trace("3");
duke@435 322
duke@435 323 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
duke@435 324
duke@435 325 // Needs to be done before the system dictionary is adjusted.
duke@435 326 pg->pre_adjust_pointers();
duke@435 327
duke@435 328 // Because the two closures below are created statically, cannot
duke@435 329 // use OopsInGenClosure constructor which takes a generation,
duke@435 330 // as the Universe has not been created when the static constructors
duke@435 331 // are run.
duke@435 332 adjust_root_pointer_closure.set_orig_generation(gch->get_gen(level));
duke@435 333 adjust_pointer_closure.set_orig_generation(gch->get_gen(level));
duke@435 334
duke@435 335 gch->gen_process_strong_roots(level,
duke@435 336 false, // Younger gens are not roots.
duke@435 337 true, // Collecting permanent generation.
duke@435 338 SharedHeap::SO_AllClasses,
duke@435 339 &adjust_root_pointer_closure,
duke@435 340 &adjust_root_pointer_closure);
duke@435 341
duke@435 342 // Now adjust pointers in remaining weak roots. (All of which should
duke@435 343 // have been cleared if they pointed to non-surviving objects.)
duke@435 344 gch->gen_process_weak_roots(&adjust_root_pointer_closure,
duke@435 345 &adjust_pointer_closure);
duke@435 346
duke@435 347 adjust_marks();
duke@435 348 GenAdjustPointersClosure blk;
duke@435 349 gch->generation_iterate(&blk, true);
duke@435 350 pg->adjust_pointers();
duke@435 351 }
duke@435 352
duke@435 353 class GenCompactClosure: public GenCollectedHeap::GenClosure {
duke@435 354 public:
duke@435 355 void do_generation(Generation* gen) {
duke@435 356 gen->compact();
duke@435 357 }
duke@435 358 };
duke@435 359
duke@435 360 void GenMarkSweep::mark_sweep_phase4() {
duke@435 361 // All pointers are now adjusted, move objects accordingly
duke@435 362
duke@435 363 // It is imperative that we traverse perm_gen first in phase4. All
duke@435 364 // classes must be allocated earlier than their instances, and traversing
duke@435 365 // perm_gen first makes sure that all klassOops have moved to their new
duke@435 366 // location before any instance does a dispatch through it's klass!
duke@435 367
duke@435 368 // The ValidateMarkSweep live oops tracking expects us to traverse spaces
duke@435 369 // in the same order in phase2, phase3 and phase4. We don't quite do that
duke@435 370 // here (perm_gen first rather than last), so we tell the validate code
duke@435 371 // to use a higher index (saved from phase2) when verifying perm_gen.
duke@435 372 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 373 Generation* pg = gch->perm_gen();
duke@435 374
duke@435 375 EventMark m("4 compact heap");
duke@435 376 TraceTime tm("phase 4", PrintGC && Verbose, true, gclog_or_tty);
duke@435 377 trace("4");
duke@435 378
duke@435 379 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(true));
duke@435 380
duke@435 381 pg->compact();
duke@435 382
duke@435 383 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
duke@435 384
duke@435 385 GenCompactClosure blk;
duke@435 386 gch->generation_iterate(&blk, true);
duke@435 387
duke@435 388 VALIDATE_MARK_SWEEP_ONLY(compaction_complete());
duke@435 389
duke@435 390 pg->post_compact(); // Shared spaces verification.
duke@435 391 }

mercurial