1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)psScavenge.cpp       1.99 07/09/07 09:53:34 JVM"
   3 #endif
   4 /*
   5  * Copyright 2002-2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 
  29 # include "incls/_precompiled.incl"
  30 # include "incls/_psScavenge.cpp.incl"
  31 
  32 HeapWord*                  PSScavenge::_to_space_top_before_gc = NULL;
  33 int                        PSScavenge::_consecutive_skipped_scavenges = 0;
  34 ReferenceProcessor*        PSScavenge::_ref_processor = NULL;
  35 CardTableExtension*        PSScavenge::_card_table = NULL;
  36 bool                       PSScavenge::_survivor_overflow = false;
  37 int                        PSScavenge::_tenuring_threshold = 0;
  38 HeapWord*                  PSScavenge::_young_generation_boundary = NULL;
  39 elapsedTimer               PSScavenge::_accumulated_time;
  40 GrowableArray<markOop>*    PSScavenge::_preserved_mark_stack = NULL;
  41 GrowableArray<oop>*        PSScavenge::_preserved_oop_stack = NULL;
  42 CollectorCounters*         PSScavenge::_counters = NULL;
  43 
  44 // Define before use
  45 class PSIsAliveClosure: public BoolObjectClosure {
  46 public:
  47   void do_object(oop p) {
  48     assert(false, "Do not call.");
  49   }
  50   bool do_object_b(oop p) {
  51     return (!PSScavenge::is_obj_in_young((HeapWord*) p)) || p->is_forwarded();
  52   }
  53 };
  54 
  55 PSIsAliveClosure PSScavenge::_is_alive_closure;
  56 
  57 class PSKeepAliveClosure: public OopClosure {
  58 protected:
  59   MutableSpace* _to_space;
  60   PSPromotionManager* _promotion_manager;
  61 
  62 public:
  63   PSKeepAliveClosure(PSPromotionManager* pm) : _promotion_manager(pm) {
  64     ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
  65     assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
  66     _to_space = heap->young_gen()->to_space();
  67     
  68     assert(_promotion_manager != NULL, "Sanity");
  69   }
  70 
  71   void do_oop(oop* p) {
  72     assert (*p != NULL, "expected non-null ref");
  73     assert ((*p)->is_oop(), "expected an oop while scanning weak refs");
  74   
  75     oop obj = oop(*p);
  76     // Weak refs may be visited more than once.
  77     if (PSScavenge::should_scavenge(obj, _to_space)) {
  78       PSScavenge::copy_and_push_safe_barrier(_promotion_manager, p);
  79     }
  80   }
  81 };
  82 
  83 class PSEvacuateFollowersClosure: public VoidClosure {
  84  private:
  85   PSPromotionManager* _promotion_manager;
  86  public:
  87   PSEvacuateFollowersClosure(PSPromotionManager* pm) : _promotion_manager(pm) {}
  88 
  89   void do_void() {
  90     assert(_promotion_manager != NULL, "Sanity");
  91     _promotion_manager->drain_stacks(true);
  92     guarantee(_promotion_manager->stacks_empty(),
  93               "stacks should be empty at this point");
  94   }
  95 };
  96 
  97 class PSPromotionFailedClosure : public ObjectClosure {
  98   virtual void do_object(oop obj) {
  99     if (obj->is_forwarded()) {
 100       obj->init_mark();
 101     }
 102   }
 103 };
 104 
 105 class PSRefProcTaskProxy: public GCTask {
 106   typedef AbstractRefProcTaskExecutor::ProcessTask ProcessTask;
 107   ProcessTask & _rp_task;
 108   uint          _work_id;
 109 public:
 110   PSRefProcTaskProxy(ProcessTask & rp_task, uint work_id)
 111     : _rp_task(rp_task),
 112       _work_id(work_id)
 113   { }
 114   
 115 private:
 116   virtual char* name() { return (char *)"Process referents by policy in parallel"; }
 117   virtual void do_it(GCTaskManager* manager, uint which);
 118 };
 119 
 120 void PSRefProcTaskProxy::do_it(GCTaskManager* manager, uint which)
 121 {  
 122   PSPromotionManager* promotion_manager = 
 123     PSPromotionManager::gc_thread_promotion_manager(which);
 124   assert(promotion_manager != NULL, "sanity check");
 125   PSKeepAliveClosure keep_alive(promotion_manager);
 126   PSEvacuateFollowersClosure evac_followers(promotion_manager);
 127   PSIsAliveClosure is_alive;
 128   _rp_task.work(_work_id, is_alive, keep_alive, evac_followers);
 129 }
 130 
 131 class PSRefEnqueueTaskProxy: public GCTask {
 132   typedef AbstractRefProcTaskExecutor::EnqueueTask EnqueueTask;
 133   EnqueueTask& _enq_task;
 134   uint         _work_id;
 135 
 136 public:
 137   PSRefEnqueueTaskProxy(EnqueueTask& enq_task, uint work_id)
 138     : _enq_task(enq_task),
 139       _work_id(work_id)
 140   { }
 141 
 142   virtual char* name() { return (char *)"Enqueue reference objects in parallel"; }
 143   virtual void do_it(GCTaskManager* manager, uint which)
 144   {
 145     _enq_task.work(_work_id);
 146   }
 147 };
 148 
 149 class PSRefProcTaskExecutor: public AbstractRefProcTaskExecutor {
 150   virtual void execute(ProcessTask& task);
 151   virtual void execute(EnqueueTask& task);
 152 };
 153 
 154 void PSRefProcTaskExecutor::execute(ProcessTask& task)
 155 {
 156   GCTaskQueue* q = GCTaskQueue::create();
 157   for(uint i=0; i<ParallelGCThreads; i++) {
 158     q->enqueue(new PSRefProcTaskProxy(task, i));
 159   }
 160   ParallelTaskTerminator terminator(
 161     ParallelScavengeHeap::gc_task_manager()->workers(),
 162     UseDepthFirstScavengeOrder ?
 163         (TaskQueueSetSuper*) PSPromotionManager::stack_array_depth()
 164       : (TaskQueueSetSuper*) PSPromotionManager::stack_array_breadth());
 165   if (task.marks_oops_alive() && ParallelGCThreads > 1) {
 166     for (uint j=0; j<ParallelGCThreads; j++) {
 167       q->enqueue(new StealTask(&terminator));
 168     }
 169   }
 170   ParallelScavengeHeap::gc_task_manager()->execute_and_wait(q);
 171 }
 172 
 173 
 174 void PSRefProcTaskExecutor::execute(EnqueueTask& task)
 175 {
 176   GCTaskQueue* q = GCTaskQueue::create();
 177   for(uint i=0; i<ParallelGCThreads; i++) {
 178     q->enqueue(new PSRefEnqueueTaskProxy(task, i));
 179   }
 180   ParallelScavengeHeap::gc_task_manager()->execute_and_wait(q);
 181 }
 182 
 183 // This method contains all heap specific policy for invoking scavenge.
 184 // PSScavenge::invoke_no_policy() will do nothing but attempt to
 185 // scavenge. It will not clean up after failed promotions, bail out if
 186 // we've exceeded policy time limits, or any other special behavior.
 187 // All such policy should be placed here.
 188 //
 189 // Note that this method should only be called from the vm_thread while
 190 // at a safepoint!
 191 void PSScavenge::invoke()
 192 {
 193   assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
 194   assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
 195   assert(!Universe::heap()->is_gc_active(), "not reentrant");
 196 
 197   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 198   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 199 
 200   PSAdaptiveSizePolicy* policy = heap->size_policy();
 201 
 202   // Before each allocation/collection attempt, find out from the
 203   // policy object if GCs are, on the whole, taking too long. If so,
 204   // bail out without attempting a collection.
 205   if (!policy->gc_time_limit_exceeded()) {
 206     IsGCActiveMark mark;
 207 
 208     bool scavenge_was_done = PSScavenge::invoke_no_policy();
 209 
 210     PSGCAdaptivePolicyCounters* counters = heap->gc_policy_counters();
 211     if (UsePerfData)
 212       counters->update_full_follows_scavenge(0);
 213     if (!scavenge_was_done || 
 214         policy->should_full_GC(heap->old_gen()->free_in_bytes())) {
 215       if (UsePerfData)
 216         counters->update_full_follows_scavenge(full_follows_scavenge);
 217 
 218       GCCauseSetter gccs(heap, GCCause::_adaptive_size_policy);
 219       if (UseParallelOldGC) {
 220         PSParallelCompact::invoke_no_policy(false);
 221       } else {
 222         PSMarkSweep::invoke_no_policy(false);
 223       }
 224     }
 225   }
 226 }
 227 
 228 // This method contains no policy. You should probably
 229 // be calling invoke() instead. 
 230 bool PSScavenge::invoke_no_policy() {
 231   assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
 232   assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
 233 
 234   TimeStamp scavenge_entry;
 235   TimeStamp scavenge_midpoint;
 236   TimeStamp scavenge_exit;
 237 
 238   scavenge_entry.update();
 239 
 240   if (GC_locker::check_active_before_gc()) {
 241     return false;
 242   }
 243 
 244   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 245   GCCause::Cause gc_cause = heap->gc_cause();
 246   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 247 
 248   // Check for potential problems.
 249   if (!should_attempt_scavenge()) {
 250     return false;
 251   }
 252 
 253   bool promotion_failure_occurred = false;
 254 
 255   PSYoungGen* young_gen = heap->young_gen();
 256   PSOldGen* old_gen = heap->old_gen();
 257   PSPermGen* perm_gen = heap->perm_gen();
 258   PSAdaptiveSizePolicy* size_policy = heap->size_policy();
 259   heap->increment_total_collections();
 260 
 261   AdaptiveSizePolicyOutput(size_policy, heap->total_collections());
 262 
 263   if ((gc_cause != GCCause::_java_lang_system_gc) ||
 264        UseAdaptiveSizePolicyWithSystemGC) {
 265     // Gather the feedback data for eden occupancy.
 266     young_gen->eden_space()->accumulate_statistics();
 267   }
 268 
 269   if (PrintHeapAtGC) {
 270     Universe::print_heap_before_gc();
 271   }
 272 
 273   assert(!NeverTenure || _tenuring_threshold == markOopDesc::max_age + 1, "Sanity");
 274   assert(!AlwaysTenure || _tenuring_threshold == 0, "Sanity");
 275 
 276   size_t prev_used = heap->used();
 277   assert(promotion_failed() == false, "Sanity");
 278 
 279   // Fill in TLABs
 280   heap->accumulate_statistics_all_tlabs();
 281   heap->ensure_parsability(true);  // retire TLABs
 282 
 283   if (VerifyBeforeGC && heap->total_collections() >= VerifyGCStartAt) {
 284     HandleMark hm;  // Discard invalid handles created during verification
 285     gclog_or_tty->print(" VerifyBeforeGC:");
 286     Universe::verify(true);
 287   }
 288 
 289   {
 290     ResourceMark rm;
 291     HandleMark hm;
 292 
 293     gclog_or_tty->date_stamp(PrintGC && PrintGCDateStamps);
 294     TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);
 295     TraceTime t1("GC", PrintGC, !PrintGCDetails, gclog_or_tty);
 296     TraceCollectorStats tcs(counters());
 297     TraceMemoryManagerStats tms(false /* not full GC */);
 298 
 299     if (TraceGen0Time) accumulated_time()->start();
 300 
 301     // Let the size policy know we're starting
 302     size_policy->minor_collection_begin();
 303     
 304     // Verify the object start arrays.
 305     if (VerifyObjectStartArray &&
 306         VerifyBeforeGC) {
 307       old_gen->verify_object_start_array();
 308       perm_gen->verify_object_start_array();
 309     }
 310 
 311     // Verify no unmarked old->young roots
 312     if (VerifyRememberedSets) {
 313       CardTableExtension::verify_all_young_refs_imprecise();
 314     }
 315     
 316     if (!ScavengeWithObjectsInToSpace) {
 317       assert(young_gen->to_space()->is_empty(),
 318              "Attempt to scavenge with live objects in to_space");
 319       young_gen->to_space()->clear();
 320     } else if (ZapUnusedHeapArea) {
 321       young_gen->to_space()->mangle_unused_area();
 322     }
 323     save_to_space_top_before_gc();
 324 
 325     NOT_PRODUCT(reference_processor()->verify_no_references_recorded());
 326     COMPILER2_PRESENT(DerivedPointerTable::clear());
 327 
 328     reference_processor()->enable_discovery();
 329     
 330     // We track how much was promoted to the next generation for
 331     // the AdaptiveSizePolicy.
 332     size_t old_gen_used_before = old_gen->used_in_bytes();
 333 
 334     // For PrintGCDetails
 335     size_t young_gen_used_before = young_gen->used_in_bytes();
 336 
 337     // Reset our survivor overflow.
 338     set_survivor_overflow(false);
 339     
 340     // We need to save the old/perm top values before
 341     // creating the promotion_manager. We pass the top
 342     // values to the card_table, to prevent it from
 343     // straying into the promotion labs.
 344     HeapWord* old_top = old_gen->object_space()->top();
 345     HeapWord* perm_top = perm_gen->object_space()->top();
 346 
 347     // Release all previously held resources
 348     gc_task_manager()->release_all_resources();
 349 
 350     PSPromotionManager::pre_scavenge();
 351 
 352     // We'll use the promotion manager again later.
 353     PSPromotionManager* promotion_manager = PSPromotionManager::vm_thread_promotion_manager();
 354     {
 355       // TraceTime("Roots");
 356       
 357       GCTaskQueue* q = GCTaskQueue::create();
 358       
 359       for(uint i=0; i<ParallelGCThreads; i++) {
 360         q->enqueue(new OldToYoungRootsTask(old_gen, old_top, i));
 361       }
 362 
 363       q->enqueue(new SerialOldToYoungRootsTask(perm_gen, perm_top));
 364 
 365       q->enqueue(new ScavengeRootsTask(ScavengeRootsTask::universe));
 366       q->enqueue(new ScavengeRootsTask(ScavengeRootsTask::jni_handles));
 367       // We scan the thread roots in parallel
 368       Threads::create_thread_roots_tasks(q);
 369       q->enqueue(new ScavengeRootsTask(ScavengeRootsTask::object_synchronizer));
 370       q->enqueue(new ScavengeRootsTask(ScavengeRootsTask::flat_profiler));
 371       q->enqueue(new ScavengeRootsTask(ScavengeRootsTask::management));
 372       q->enqueue(new ScavengeRootsTask(ScavengeRootsTask::system_dictionary));
 373       q->enqueue(new ScavengeRootsTask(ScavengeRootsTask::jvmti));
 374 
 375       ParallelTaskTerminator terminator(
 376         gc_task_manager()->workers(),
 377         promotion_manager->depth_first() ?
 378             (TaskQueueSetSuper*) promotion_manager->stack_array_depth()
 379           : (TaskQueueSetSuper*) promotion_manager->stack_array_breadth());
 380       if (ParallelGCThreads>1) {
 381         for (uint j=0; j<ParallelGCThreads; j++) {
 382           q->enqueue(new StealTask(&terminator));
 383         }
 384       }
 385 
 386       gc_task_manager()->execute_and_wait(q);
 387     }
 388 
 389     scavenge_midpoint.update();
 390 
 391     // Process reference objects discovered during scavenge
 392     {
 393 #ifdef COMPILER2
 394       ReferencePolicy *soft_ref_policy = new LRUMaxHeapPolicy();
 395 #else
 396       ReferencePolicy *soft_ref_policy = new LRUCurrentHeapPolicy();
 397 #endif // COMPILER2
 398     
 399       PSKeepAliveClosure keep_alive(promotion_manager);
 400       PSEvacuateFollowersClosure evac_followers(promotion_manager);
 401       assert(soft_ref_policy != NULL,"No soft reference policy");
 402       if (reference_processor()->processing_is_mt()) {
 403         PSRefProcTaskExecutor task_executor;
 404         reference_processor()->process_discovered_references(
 405           soft_ref_policy, &_is_alive_closure, &keep_alive, &evac_followers, 
 406           &task_executor);
 407       } else {
 408         reference_processor()->process_discovered_references(
 409           soft_ref_policy, &_is_alive_closure, &keep_alive, &evac_followers,
 410           NULL);
 411       }
 412     }
 413     
 414     // Enqueue reference objects discovered during scavenge.
 415     if (reference_processor()->processing_is_mt()) {
 416       PSRefProcTaskExecutor task_executor;
 417       reference_processor()->enqueue_discovered_references(&task_executor);
 418     } else {
 419       reference_processor()->enqueue_discovered_references(NULL);
 420     }
 421     
 422     // Finally, flush the promotion_manager's labs, and deallocate its stacks.
 423     assert(promotion_manager->claimed_stack_empty(), "Sanity");
 424     PSPromotionManager::post_scavenge();
 425 
 426     promotion_failure_occurred = promotion_failed();
 427     if (promotion_failure_occurred) {
 428       clean_up_failed_promotion();
 429       if (PrintGC) {
 430         gclog_or_tty->print("--");
 431       }
 432     }
 433 
 434     // Let the size policy know we're done.  Note that we count promotion
 435     // failure cleanup time as part of the collection (otherwise, we're
 436     // implicitly saying it's mutator time).
 437     size_policy->minor_collection_end(gc_cause);
 438 
 439     if (!promotion_failure_occurred) {
 440       // Swap the survivor spaces.
 441       young_gen->eden_space()->clear();
 442       young_gen->from_space()->clear();
 443       young_gen->swap_spaces();
 444 
 445       size_t survived = young_gen->from_space()->used_in_bytes();
 446       size_t promoted = old_gen->used_in_bytes() - old_gen_used_before;
 447       size_policy->update_averages(_survivor_overflow, survived, promoted);
 448 
 449       if (UseAdaptiveSizePolicy) {
 450         // Calculate the new survivor size and tenuring threshold
 451 
 452         if (PrintAdaptiveSizePolicy) {
 453           gclog_or_tty->print("AdaptiveSizeStart: ");
 454           gclog_or_tty->stamp();
 455           gclog_or_tty->print_cr(" collection: %d ",
 456                          heap->total_collections());
 457 
 458           if (Verbose) {
 459             gclog_or_tty->print("old_gen_capacity: %d young_gen_capacity: %d"
 460               " perm_gen_capacity: %d ",
 461               old_gen->capacity_in_bytes(), young_gen->capacity_in_bytes(),
 462               perm_gen->capacity_in_bytes());
 463           }
 464         }
 465 
 466 
 467         if (UsePerfData) {
 468           PSGCAdaptivePolicyCounters* counters = heap->gc_policy_counters();
 469           counters->update_old_eden_size(
 470             size_policy->calculated_eden_size_in_bytes());
 471           counters->update_old_promo_size(
 472             size_policy->calculated_promo_size_in_bytes());
 473           counters->update_old_capacity(old_gen->capacity_in_bytes());
 474           counters->update_young_capacity(young_gen->capacity_in_bytes());
 475           counters->update_survived(survived);
 476           counters->update_promoted(promoted);
 477           counters->update_survivor_overflowed(_survivor_overflow);
 478         }
 479 
 480         size_t survivor_limit = 
 481           size_policy->max_survivor_size(young_gen->max_size());
 482         _tenuring_threshold = 
 483           size_policy->compute_survivor_space_size_and_threshold(
 484                                                            _survivor_overflow, 
 485                                                            _tenuring_threshold,
 486                                                            survivor_limit);
 487 
 488        if (PrintTenuringDistribution) {
 489          gclog_or_tty->cr();
 490          gclog_or_tty->print_cr("Desired survivor size %ld bytes, new threshold %d (max %d)",
 491                                 size_policy->calculated_survivor_size_in_bytes(), 
 492                                 _tenuring_threshold, MaxTenuringThreshold);
 493        }
 494     
 495         if (UsePerfData) {
 496           PSGCAdaptivePolicyCounters* counters = heap->gc_policy_counters();
 497           counters->update_tenuring_threshold(_tenuring_threshold);
 498           counters->update_survivor_size_counters();
 499         }
 500 
 501         // Do call at minor collections?
 502         // Don't check if the size_policy is ready at this
 503         // level.  Let the size_policy check that internally.
 504         if (UseAdaptiveSizePolicy &&
 505             UseAdaptiveGenerationSizePolicyAtMinorCollection &&
 506             ((gc_cause != GCCause::_java_lang_system_gc) ||
 507               UseAdaptiveSizePolicyWithSystemGC)) {
 508 
 509           // Calculate optimial free space amounts
 510           assert(young_gen->max_size() > 
 511             young_gen->from_space()->capacity_in_bytes() + 
 512             young_gen->to_space()->capacity_in_bytes(), 
 513             "Sizes of space in young gen are out-of-bounds");
 514           size_t max_eden_size = young_gen->max_size() - 
 515             young_gen->from_space()->capacity_in_bytes() - 
 516             young_gen->to_space()->capacity_in_bytes();
 517           size_policy->compute_generation_free_space(young_gen->used_in_bytes(),
 518                                    young_gen->eden_space()->used_in_bytes(),
 519                                    old_gen->used_in_bytes(),
 520                                    perm_gen->used_in_bytes(),
 521                                    young_gen->eden_space()->capacity_in_bytes(),
 522                                    old_gen->max_gen_size(),
 523                                    max_eden_size,
 524                                    false  /* full gc*/,
 525                                    gc_cause);
 526         
 527         }
 528         // Resize the young generation at every collection
 529         // even if new sizes have not been calculated.  This is
 530         // to allow resizes that may have been inhibited by the
 531         // relative location of the "to" and "from" spaces.
 532         
 533         // Resizing the old gen at minor collects can cause increases
 534         // that don't feed back to the generation sizing policy until
 535         // a major collection.  Don't resize the old gen here.
 536 
 537         heap->resize_young_gen(size_policy->calculated_eden_size_in_bytes(),
 538                         size_policy->calculated_survivor_size_in_bytes());
 539 
 540         if (PrintAdaptiveSizePolicy) {
 541           gclog_or_tty->print_cr("AdaptiveSizeStop: collection: %d ",
 542                          heap->total_collections());
 543         }
 544       }
 545 
 546       // Update the structure of the eden. With NUMA-eden CPU hotplugging or offlining can
 547       // cause the change of the heap layout. Make sure eden is reshaped if that's the case.
 548       // Also update() will case adaptive NUMA chunk resizing.
 549       assert(young_gen->eden_space()->is_empty(), "eden space should be empty now");
 550       young_gen->eden_space()->update();
 551 
 552       heap->gc_policy_counters()->update_counters();
 553 
 554       heap->resize_all_tlabs();
 555 
 556       assert(young_gen->to_space()->is_empty(), "to space should be empty now");
 557     }
 558 
 559     COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
 560 
 561     NOT_PRODUCT(reference_processor()->verify_no_references_recorded());
 562     
 563     // Re-verify object start arrays
 564     if (VerifyObjectStartArray &&
 565         VerifyAfterGC) {
 566       old_gen->verify_object_start_array();
 567       perm_gen->verify_object_start_array();
 568     }
 569 
 570     // Verify all old -> young cards are now precise
 571     if (VerifyRememberedSets) {
 572       // Precise verification will give false positives. Until this is fixed,
 573       // use imprecise verification.
 574       // CardTableExtension::verify_all_young_refs_precise();
 575       CardTableExtension::verify_all_young_refs_imprecise();
 576     }
 577 
 578     if (TraceGen0Time) accumulated_time()->stop();
 579     
 580     if (PrintGC) {
 581       if (PrintGCDetails) {
 582         // Don't print a GC timestamp here.  This is after the GC so
 583         // would be confusing.
 584         young_gen->print_used_change(young_gen_used_before);
 585       }
 586       heap->print_heap_change(prev_used);
 587     }
 588 
 589     // Track memory usage and detect low memory
 590     MemoryService::track_memory_usage();
 591     heap->update_counters();
 592   }
 593 
 594   if (VerifyAfterGC && heap->total_collections() >= VerifyGCStartAt) {
 595     HandleMark hm;  // Discard invalid handles created during verification
 596     gclog_or_tty->print(" VerifyAfterGC:");
 597     Universe::verify(false);
 598   }
 599 
 600   if (PrintHeapAtGC) {
 601     Universe::print_heap_after_gc();
 602   }
 603 
 604   scavenge_exit.update();
 605 
 606   if (PrintGCTaskTimeStamps) {
 607     tty->print_cr("VM-Thread " INT64_FORMAT " " INT64_FORMAT " " INT64_FORMAT,
 608                   scavenge_entry.ticks(), scavenge_midpoint.ticks(),
 609                   scavenge_exit.ticks());
 610     gc_task_manager()->print_task_time_stamps();
 611   }
 612 
 613   return !promotion_failure_occurred;
 614 }
 615 
 616 // This method iterates over all objects in the young generation,
 617 // unforwarding markOops. It then restores any preserved mark oops,
 618 // and clears the _preserved_mark_stack.
 619 void PSScavenge::clean_up_failed_promotion() {
 620   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 621   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 622   assert(promotion_failed(), "Sanity");
 623 
 624   PSYoungGen* young_gen = heap->young_gen();
 625 
 626   {
 627     ResourceMark rm;
 628 
 629     // Unforward all pointers in the young gen.
 630     PSPromotionFailedClosure unforward_closure;
 631     young_gen->object_iterate(&unforward_closure);
 632 
 633     if (PrintGC && Verbose) {
 634       gclog_or_tty->print_cr("Restoring %d marks", 
 635                               _preserved_oop_stack->length());
 636     }
 637 
 638     // Restore any saved marks.
 639     for (int i=0; i < _preserved_oop_stack->length(); i++) {
 640       oop obj       = _preserved_oop_stack->at(i);
 641       markOop mark  = _preserved_mark_stack->at(i);
 642       obj->set_mark(mark);      
 643     }
 644  
 645     // Deallocate the preserved mark and oop stacks.
 646     // The stacks were allocated as CHeap objects, so
 647     // we must call delete to prevent mem leaks.
 648     delete _preserved_mark_stack;
 649     _preserved_mark_stack = NULL;
 650     delete _preserved_oop_stack;
 651     _preserved_oop_stack = NULL;
 652   }
 653 
 654   // Reset the PromotionFailureALot counters.
 655   NOT_PRODUCT(Universe::heap()->reset_promotion_should_fail();)
 656 }
 657 
 658 // This method is called whenever an attempt to promote an object
 659 // fails. Some markOops will need preserving, some will not. Note
 660 // that the entire eden is traversed after a failed promotion, with
 661 // all forwarded headers replaced by the default markOop. This means
 662 // it is not neccessary to preserve most markOops.
 663 void PSScavenge::oop_promotion_failed(oop obj, markOop obj_mark) {
 664   if (_preserved_mark_stack == NULL) {
 665     ThreadCritical tc; // Lock and retest
 666     if (_preserved_mark_stack == NULL) {
 667       assert(_preserved_oop_stack == NULL, "Sanity");
 668       _preserved_mark_stack = new (ResourceObj::C_HEAP) GrowableArray<markOop>(40, true);
 669       _preserved_oop_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(40, true);
 670     }
 671   }
 672 
 673   // Because we must hold the ThreadCritical lock before using
 674   // the stacks, we should be safe from observing partial allocations,
 675   // which are also guarded by the ThreadCritical lock.
 676   if (obj_mark->must_be_preserved_for_promotion_failure(obj)) {
 677     ThreadCritical tc;
 678     _preserved_oop_stack->push(obj);
 679     _preserved_mark_stack->push(obj_mark);
 680   }
 681 }
 682 
 683 bool PSScavenge::should_attempt_scavenge() {
 684   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 685   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 686   PSGCAdaptivePolicyCounters* counters = heap->gc_policy_counters();
 687   
 688   if (UsePerfData) {
 689     counters->update_scavenge_skipped(not_skipped);
 690   }
 691 
 692   PSYoungGen* young_gen = heap->young_gen();
 693   PSOldGen* old_gen = heap->old_gen();
 694 
 695   if (!ScavengeWithObjectsInToSpace) {
 696     // Do not attempt to promote unless to_space is empty
 697     if (!young_gen->to_space()->is_empty()) {
 698       _consecutive_skipped_scavenges++;
 699       if (UsePerfData) {
 700         counters->update_scavenge_skipped(to_space_not_empty);
 701       }
 702       return false;
 703     }
 704   }
 705 
 706   // Test to see if the scavenge will likely fail.
 707   PSAdaptiveSizePolicy* policy = heap->size_policy();
 708 
 709   // A similar test is done in the policy's should_full_GC().  If this is
 710   // changed, decide if that test should also be changed.
 711   size_t avg_promoted = (size_t) policy->padded_average_promoted_in_bytes();
 712   size_t promotion_estimate = MIN2(avg_promoted, young_gen->used_in_bytes());
 713   bool result = promotion_estimate < old_gen->free_in_bytes();
 714 
 715   if (PrintGCDetails && Verbose) {
 716     gclog_or_tty->print(result ? "  do scavenge: " : "  skip scavenge: ");
 717     gclog_or_tty->print_cr(" average_promoted " SIZE_FORMAT
 718       " padded_average_promoted " SIZE_FORMAT
 719       " free in old gen " SIZE_FORMAT,
 720       (size_t) policy->average_promoted_in_bytes(),
 721       (size_t) policy->padded_average_promoted_in_bytes(),
 722       old_gen->free_in_bytes());
 723     if (young_gen->used_in_bytes() < 
 724         (size_t) policy->padded_average_promoted_in_bytes()) {
 725       gclog_or_tty->print_cr(" padded_promoted_average is greater"
 726         " than maximum promotion = " SIZE_FORMAT, young_gen->used_in_bytes());
 727     }
 728   }
 729 
 730   if (result) {
 731     _consecutive_skipped_scavenges = 0;
 732   } else {
 733     _consecutive_skipped_scavenges++;
 734     if (UsePerfData) {
 735       counters->update_scavenge_skipped(promoted_too_large);
 736     }
 737   }
 738   return result;
 739 }
 740 
 741   // Used to add tasks
 742 GCTaskManager* const PSScavenge::gc_task_manager() {
 743   assert(ParallelScavengeHeap::gc_task_manager() != NULL,
 744    "shouldn't return NULL");
 745   return ParallelScavengeHeap::gc_task_manager();
 746 }
 747 
 748 void PSScavenge::initialize() {
 749   // Arguments must have been parsed 
 750 
 751   if (AlwaysTenure) {
 752     _tenuring_threshold = 0;
 753   } else if (NeverTenure) {
 754     _tenuring_threshold = markOopDesc::max_age + 1;
 755   } else {
 756     // We want to smooth out our startup times for the AdaptiveSizePolicy
 757     _tenuring_threshold = (UseAdaptiveSizePolicy) ? InitialTenuringThreshold : 
 758                                                     MaxTenuringThreshold;
 759   }
 760   
 761   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 762   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 763 
 764   PSYoungGen* young_gen = heap->young_gen();
 765   PSOldGen* old_gen = heap->old_gen();
 766   PSPermGen* perm_gen = heap->perm_gen();
 767 
 768   // Set boundary between young_gen and old_gen
 769   assert(perm_gen->reserved().end() <= old_gen->object_space()->bottom(),
 770          "perm above old");
 771   assert(old_gen->reserved().end() <= young_gen->eden_space()->bottom(), 
 772          "old above young");
 773   _young_generation_boundary = young_gen->eden_space()->bottom();
 774 
 775   // Initialize ref handling object for scavenging.
 776   MemRegion mr = young_gen->reserved();
 777   _ref_processor = ReferenceProcessor::create_ref_processor(
 778     mr,                         // span
 779     true,                       // atomic_discovery
 780     true,                       // mt_discovery
 781     NULL,                       // is_alive_non_header
 782     ParallelGCThreads,
 783     ParallelRefProcEnabled);
 784 
 785   // Cache the cardtable
 786   BarrierSet* bs = Universe::heap()->barrier_set();
 787   assert(bs->kind() == BarrierSet::CardTableModRef, "Wrong barrier set kind");
 788   _card_table = (CardTableExtension*)bs;
 789 
 790   _counters = new CollectorCounters("PSScavenge", 0);
 791 }