src/share/vm/memory/genMarkSweep.cpp

Tue, 30 Oct 2012 10:23:55 -0700

author
jmasa
date
Tue, 30 Oct 2012 10:23:55 -0700
changeset 4234
3fadc0e8cffe
parent 4098
8966c2d65d96
child 4299
f34d701e952e
permissions
-rw-r--r--

8000988: VM deadlock when running btree006 on windows-i586
Reviewed-by: johnc, jcoomes, ysr

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

mercurial