src/share/vm/gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.cpp

Print this page




 602   // Support for multi-threaded concurrent phases
 603   if (ParallelGCThreads > 0 && CMSConcurrentMTEnabled) {
 604     if (FLAG_IS_DEFAULT(ParallelCMSThreads)) {
 605       // just for now
 606       FLAG_SET_DEFAULT(ParallelCMSThreads, (ParallelGCThreads + 3)/4);
 607     }
 608     if (ParallelCMSThreads > 1) {
 609       _conc_workers = new YieldingFlexibleWorkGang("Parallel CMS Threads",
 610                                  ParallelCMSThreads, true);
 611       if (_conc_workers == NULL) {
 612         warning("GC/CMS: _conc_workers allocation failure: "
 613               "forcing -CMSConcurrentMTEnabled");
 614         CMSConcurrentMTEnabled = false;
 615       }
 616     } else {
 617       CMSConcurrentMTEnabled = false;
 618     }
 619   }
 620   if (!CMSConcurrentMTEnabled) {
 621     ParallelCMSThreads = 0;
 622   } else {
 623     // Turn off CMSCleanOnEnter optimization temporarily for
 624     // the MT case where it's not fixed yet; see 6178663.
 625     CMSCleanOnEnter = false;
 626   }
 627   assert((_conc_workers != NULL) == (ParallelCMSThreads > 1),
 628          "Inconsistency");
 629 
 630   // Parallel task queues; these are shared for the
 631   // concurrent and stop-world phases of CMS, but
 632   // are not shared with parallel scavenge (ParNew).
 633   {
 634     uint i;
 635     uint num_queues = (uint) MAX2(ParallelGCThreads, ParallelCMSThreads);
 636 
 637     if ((CMSParallelRemarkEnabled || CMSConcurrentMTEnabled
 638          || ParallelRefProcEnabled)
 639         && num_queues > 0) {
 640       _task_queues = new OopTaskQueueSet(num_queues);
 641       if (_task_queues == NULL) {
 642         warning("task_queues allocation failure.");
 643         return;
 644       }
 645       _hash_seed = NEW_C_HEAP_ARRAY(int, num_queues);


6877 }
6878 
6879 // Should revisit to see if this should be restructured for
6880 // greater efficiency.
6881 bool MarkFromRootsClosure::do_bit(size_t offset) {
6882   if (_skipBits > 0) {
6883     _skipBits--;
6884     return true;
6885   }
6886   // convert offset into a HeapWord*
6887   HeapWord* addr = _bitMap->startWord() + offset;
6888   assert(_bitMap->endWord() && addr < _bitMap->endWord(),
6889          "address out of range");
6890   assert(_bitMap->isMarked(addr), "tautology");
6891   if (_bitMap->isMarked(addr+1)) {
6892     // this is an allocated but not yet initialized object
6893     assert(_skipBits == 0, "tautology");
6894     _skipBits = 2;  // skip next two marked bits ("Printezis-marks")
6895     oop p = oop(addr);
6896     if (p->klass_or_null() == NULL || !p->is_parsable()) {
6897       DEBUG_ONLY(if (!_verifying) {)
6898         // We re-dirty the cards on which this object lies and increase
6899         // the _threshold so that we'll come back to scan this object
6900         // during the preclean or remark phase. (CMSCleanOnEnter)
6901         if (CMSCleanOnEnter) {
6902           size_t sz = _collector->block_size_using_printezis_bits(addr);
6903           HeapWord* end_card_addr   = (HeapWord*)round_to(
6904                                          (intptr_t)(addr+sz), CardTableModRefBS::card_size);
6905           MemRegion redirty_range = MemRegion(addr, end_card_addr);
6906           assert(!redirty_range.is_empty(), "Arithmetical tautology");
6907           // Bump _threshold to end_card_addr; note that
6908           // _threshold cannot possibly exceed end_card_addr, anyhow.
6909           // This prevents future clearing of the card as the scan proceeds
6910           // to the right.
6911           assert(_threshold <= end_card_addr,
6912                  "Because we are just scanning into this object");
6913           if (_threshold < end_card_addr) {
6914             _threshold = end_card_addr;
6915           }
6916           if (p->klass_or_null() != NULL) {
6917             // Redirty the range of cards...
6918             _mut->mark_range(redirty_range);
6919           } // ...else the setting of klass will dirty the card anyway.
6920         }
6921       DEBUG_ONLY(})
6922       return true;
6923     }
6924   }
6925   scanOopsInOop(addr);
6926   return true;
6927 }
6928 
6929 // We take a break if we've been at this for a while,
6930 // so as to avoid monopolizing the locks involved.
6931 void MarkFromRootsClosure::do_yield_work() {
6932   // First give up the locks, then yield, then re-lock
6933   // We should probably use a constructor/destructor idiom to
6934   // do this unlock/lock or modify the MutexUnlocker class to
6935   // serve our purpose. XXX
6936   assert(ConcurrentMarkSweepThread::cms_thread_has_cms_token(),
6937          "CMS thread should hold CMS token");
6938   assert_lock_strong(_bitMap->lock());
6939   _bitMap->lock()->unlock();
6940   ConcurrentMarkSweepThread::desynchronize(true);
6941   ConcurrentMarkSweepThread::acknowledge_yield_request();


6967   oop obj = oop(ptr);
6968   // Ignore mark word in verification below, since we
6969   // may be running concurrent with mutators.
6970   assert(obj->is_oop(true), "should be an oop");
6971   assert(_finger <= ptr, "_finger runneth ahead");
6972   // advance the finger to right end of this object
6973   _finger = ptr + obj->size();
6974   assert(_finger > ptr, "we just incremented it above");
6975   // On large heaps, it may take us some time to get through
6976   // the marking phase (especially if running iCMS). During
6977   // this time it's possible that a lot of mutations have
6978   // accumulated in the card table and the mod union table --
6979   // these mutation records are redundant until we have
6980   // actually traced into the corresponding card.
6981   // Here, we check whether advancing the finger would make
6982   // us cross into a new card, and if so clear corresponding
6983   // cards in the MUT (preclean them in the card-table in the
6984   // future).
6985 
6986   DEBUG_ONLY(if (!_verifying) {)
6987     // The clean-on-enter optimization is disabled by default,
6988     // until we fix 6178663.
6989     if (CMSCleanOnEnter && (_finger > _threshold)) {
6990       // [_threshold, _finger) represents the interval
6991       // of cards to be cleared  in MUT (or precleaned in card table).
6992       // The set of cards to be cleared is all those that overlap
6993       // with the interval [_threshold, _finger); note that
6994       // _threshold is always kept card-aligned but _finger isn't
6995       // always card-aligned.
6996       HeapWord* old_threshold = _threshold;
6997       assert(old_threshold == (HeapWord*)round_to(
6998               (intptr_t)old_threshold, CardTableModRefBS::card_size),
6999              "_threshold should always be card-aligned");
7000       _threshold = (HeapWord*)round_to(
7001                      (intptr_t)_finger, CardTableModRefBS::card_size);
7002       MemRegion mr(old_threshold, _threshold);
7003       assert(!mr.is_empty(), "Control point invariant");


7004       assert(_span.contains(mr), "Should clear within span");
7005       // XXX When _finger crosses from old gen into perm gen
7006       // we may be doing unnecessary cleaning; do better in the
7007       // future by detecting that condition and clearing fewer
7008       // MUT/CT entries.
7009       _mut->clear_range(mr);
7010     }

7011   DEBUG_ONLY(})
7012 
7013   // Note: the finger doesn't advance while we drain
7014   // the stack below.
7015   PushOrMarkClosure pushOrMarkClosure(_collector,
7016                                       _span, _bitMap, _markStack,
7017                                       _revisitStack,
7018                                       _finger, this);
7019   bool res = _markStack->push(obj);
7020   assert(res, "Empty non-zero size stack should have space for single push");
7021   while (!_markStack->isEmpty()) {
7022     oop new_oop = _markStack->pop();
7023     // Skip verifying header mark word below because we are
7024     // running concurrent with mutators.
7025     assert(new_oop->is_oop(true), "Oops! expected to pop an oop");
7026     // now scan this oop's oops
7027     new_oop->oop_iterate(&pushOrMarkClosure);
7028     do_yield_check();
7029   }
7030   assert(_markStack->isEmpty(), "tautology, emphasizing post-condition");


7080   }
7081   scan_oops_in_oop(addr);
7082   return true;
7083 }
7084 
7085 void Par_MarkFromRootsClosure::scan_oops_in_oop(HeapWord* ptr) {
7086   assert(_bit_map->isMarked(ptr), "expected bit to be set");
7087   // Should we assert that our work queue is empty or
7088   // below some drain limit?
7089   assert(_work_queue->size() == 0,
7090          "should drain stack to limit stack usage");
7091   // convert ptr to an oop preparatory to scanning
7092   oop obj = oop(ptr);
7093   // Ignore mark word in verification below, since we
7094   // may be running concurrent with mutators.
7095   assert(obj->is_oop(true), "should be an oop");
7096   assert(_finger <= ptr, "_finger runneth ahead");
7097   // advance the finger to right end of this object
7098   _finger = ptr + obj->size();
7099   assert(_finger > ptr, "we just incremented it above");
7100   // On large heaps, it may take us some time to get through
7101   // the marking phase (especially if running iCMS). During
7102   // this time it's possible that a lot of mutations have
7103   // accumulated in the card table and the mod union table --
7104   // these mutation records are redundant until we have
7105   // actually traced into the corresponding card.
7106   // Here, we check whether advancing the finger would make
7107   // us cross into a new card, and if so clear corresponding
7108   // cards in the MUT (preclean them in the card-table in the
7109   // future).
7110 
7111   // The clean-on-enter optimization is disabled by default,
7112   // until we fix 6178663.
7113   if (CMSCleanOnEnter && (_finger > _threshold)) {
7114     // [_threshold, _finger) represents the interval
7115     // of cards to be cleared  in MUT (or precleaned in card table).
7116     // The set of cards to be cleared is all those that overlap
7117     // with the interval [_threshold, _finger); note that
7118     // _threshold is always kept card-aligned but _finger isn't
7119     // always card-aligned.
7120     HeapWord* old_threshold = _threshold;
7121     assert(old_threshold == (HeapWord*)round_to(
7122             (intptr_t)old_threshold, CardTableModRefBS::card_size),
7123            "_threshold should always be card-aligned");
7124     _threshold = (HeapWord*)round_to(
7125                    (intptr_t)_finger, CardTableModRefBS::card_size);
7126     MemRegion mr(old_threshold, _threshold);
7127     assert(!mr.is_empty(), "Control point invariant");
7128     assert(_span.contains(mr), "Should clear within span"); // _whole_span ??
7129     // XXX When _finger crosses from old gen into perm gen
7130     // we may be doing unnecessary cleaning; do better in the
7131     // future by detecting that condition and clearing fewer
7132     // MUT/CT entries.
7133     _mut->clear_range(mr);
7134   }
7135 
7136   // Note: the local finger doesn't advance while we drain
7137   // the stack below, but the global finger sure can and will.
7138   HeapWord** gfa = _task->global_finger_addr();
7139   Par_PushOrMarkClosure pushOrMarkClosure(_collector,
7140                                       _span, _bit_map,
7141                                       _work_queue,
7142                                       _overflow_stack,
7143                                       _revisit_stack,
7144                                       _finger,
7145                                       gfa, this);
7146   bool res = _work_queue->push(obj);   // overflow could occur here
7147   assert(res, "Will hold once we use workqueues");
7148   while (true) {
7149     oop new_oop;
7150     if (!_work_queue->pop_local(new_oop)) {
7151       // We emptied our work_queue; check if there's stuff that can
7152       // be gotten from the overflow stack.
7153       if (CMSConcMarkingTask::get_work_from_overflow_stack(
7154             _overflow_stack, _work_queue)) {




 602   // Support for multi-threaded concurrent phases
 603   if (ParallelGCThreads > 0 && CMSConcurrentMTEnabled) {
 604     if (FLAG_IS_DEFAULT(ParallelCMSThreads)) {
 605       // just for now
 606       FLAG_SET_DEFAULT(ParallelCMSThreads, (ParallelGCThreads + 3)/4);
 607     }
 608     if (ParallelCMSThreads > 1) {
 609       _conc_workers = new YieldingFlexibleWorkGang("Parallel CMS Threads",
 610                                  ParallelCMSThreads, true);
 611       if (_conc_workers == NULL) {
 612         warning("GC/CMS: _conc_workers allocation failure: "
 613               "forcing -CMSConcurrentMTEnabled");
 614         CMSConcurrentMTEnabled = false;
 615       }
 616     } else {
 617       CMSConcurrentMTEnabled = false;
 618     }
 619   }
 620   if (!CMSConcurrentMTEnabled) {
 621     ParallelCMSThreads = 0;




 622   }
 623   assert((_conc_workers != NULL) == (ParallelCMSThreads > 1),
 624          "Inconsistency");
 625 
 626   // Parallel task queues; these are shared for the
 627   // concurrent and stop-world phases of CMS, but
 628   // are not shared with parallel scavenge (ParNew).
 629   {
 630     uint i;
 631     uint num_queues = (uint) MAX2(ParallelGCThreads, ParallelCMSThreads);
 632 
 633     if ((CMSParallelRemarkEnabled || CMSConcurrentMTEnabled
 634          || ParallelRefProcEnabled)
 635         && num_queues > 0) {
 636       _task_queues = new OopTaskQueueSet(num_queues);
 637       if (_task_queues == NULL) {
 638         warning("task_queues allocation failure.");
 639         return;
 640       }
 641       _hash_seed = NEW_C_HEAP_ARRAY(int, num_queues);


6873 }
6874 
6875 // Should revisit to see if this should be restructured for
6876 // greater efficiency.
6877 bool MarkFromRootsClosure::do_bit(size_t offset) {
6878   if (_skipBits > 0) {
6879     _skipBits--;
6880     return true;
6881   }
6882   // convert offset into a HeapWord*
6883   HeapWord* addr = _bitMap->startWord() + offset;
6884   assert(_bitMap->endWord() && addr < _bitMap->endWord(),
6885          "address out of range");
6886   assert(_bitMap->isMarked(addr), "tautology");
6887   if (_bitMap->isMarked(addr+1)) {
6888     // this is an allocated but not yet initialized object
6889     assert(_skipBits == 0, "tautology");
6890     _skipBits = 2;  // skip next two marked bits ("Printezis-marks")
6891     oop p = oop(addr);
6892     if (p->klass_or_null() == NULL || !p->is_parsable()) {

























6893       return true;
6894     }
6895   }
6896   scanOopsInOop(addr);
6897   return true;
6898 }
6899 
6900 // We take a break if we've been at this for a while,
6901 // so as to avoid monopolizing the locks involved.
6902 void MarkFromRootsClosure::do_yield_work() {
6903   // First give up the locks, then yield, then re-lock
6904   // We should probably use a constructor/destructor idiom to
6905   // do this unlock/lock or modify the MutexUnlocker class to
6906   // serve our purpose. XXX
6907   assert(ConcurrentMarkSweepThread::cms_thread_has_cms_token(),
6908          "CMS thread should hold CMS token");
6909   assert_lock_strong(_bitMap->lock());
6910   _bitMap->lock()->unlock();
6911   ConcurrentMarkSweepThread::desynchronize(true);
6912   ConcurrentMarkSweepThread::acknowledge_yield_request();


6938   oop obj = oop(ptr);
6939   // Ignore mark word in verification below, since we
6940   // may be running concurrent with mutators.
6941   assert(obj->is_oop(true), "should be an oop");
6942   assert(_finger <= ptr, "_finger runneth ahead");
6943   // advance the finger to right end of this object
6944   _finger = ptr + obj->size();
6945   assert(_finger > ptr, "we just incremented it above");
6946   // On large heaps, it may take us some time to get through
6947   // the marking phase (especially if running iCMS). During
6948   // this time it's possible that a lot of mutations have
6949   // accumulated in the card table and the mod union table --
6950   // these mutation records are redundant until we have
6951   // actually traced into the corresponding card.
6952   // Here, we check whether advancing the finger would make
6953   // us cross into a new card, and if so clear corresponding
6954   // cards in the MUT (preclean them in the card-table in the
6955   // future).
6956 
6957   DEBUG_ONLY(if (!_verifying) {)


6958     if (CMSCleanOnEnter && (_finger > _threshold)) {
6959       // When marking enters a card for the first time,
6960       // we can clear the card just entered, because we will
6961       // be scanning it. The field _threshold keeps track of
6962       // the end of the last card we may have cleared in
6963       // this manner.
6964       assert(_threshold == (HeapWord*)round_to(
6965               (intptr_t)_threshold, CardTableModRefBS::card_size),


6966              "_threshold should always be card-aligned");
6967       _threshold = (HeapWord*)round_to(
6968                      (intptr_t)_finger, CardTableModRefBS::card_size);
6969       HeapWord* bot_addr = (HeapWord*)round_to(
6970                              (intptr_t)ptr, CardTableModRefBS::card_size);
6971       MemRegion mr(bot_addr, _threshold);
6972       if (!mr.is_empty()) {
6973         assert(_span.contains(mr), "Should clear within span");




6974         _mut->clear_range(mr);
6975       }
6976     }
6977   DEBUG_ONLY(})
6978 
6979   // Note: the finger doesn't advance while we drain
6980   // the stack below.
6981   PushOrMarkClosure pushOrMarkClosure(_collector,
6982                                       _span, _bitMap, _markStack,
6983                                       _revisitStack,
6984                                       _finger, this);
6985   bool res = _markStack->push(obj);
6986   assert(res, "Empty non-zero size stack should have space for single push");
6987   while (!_markStack->isEmpty()) {
6988     oop new_oop = _markStack->pop();
6989     // Skip verifying header mark word below because we are
6990     // running concurrent with mutators.
6991     assert(new_oop->is_oop(true), "Oops! expected to pop an oop");
6992     // now scan this oop's oops
6993     new_oop->oop_iterate(&pushOrMarkClosure);
6994     do_yield_check();
6995   }
6996   assert(_markStack->isEmpty(), "tautology, emphasizing post-condition");


7046   }
7047   scan_oops_in_oop(addr);
7048   return true;
7049 }
7050 
7051 void Par_MarkFromRootsClosure::scan_oops_in_oop(HeapWord* ptr) {
7052   assert(_bit_map->isMarked(ptr), "expected bit to be set");
7053   // Should we assert that our work queue is empty or
7054   // below some drain limit?
7055   assert(_work_queue->size() == 0,
7056          "should drain stack to limit stack usage");
7057   // convert ptr to an oop preparatory to scanning
7058   oop obj = oop(ptr);
7059   // Ignore mark word in verification below, since we
7060   // may be running concurrent with mutators.
7061   assert(obj->is_oop(true), "should be an oop");
7062   assert(_finger <= ptr, "_finger runneth ahead");
7063   // advance the finger to right end of this object
7064   _finger = ptr + obj->size();
7065   assert(_finger > ptr, "we just incremented it above");










7066 
7067   // Turn off CMSCleanOnEnter optimization temporarily for
7068   // the MT case where it's not fixed yet; see 6178663.






















7069 
7070   // Note: the local finger doesn't advance while we drain
7071   // the stack below, but the global finger sure can and will.
7072   HeapWord** gfa = _task->global_finger_addr();
7073   Par_PushOrMarkClosure pushOrMarkClosure(_collector,
7074                                       _span, _bit_map,
7075                                       _work_queue,
7076                                       _overflow_stack,
7077                                       _revisit_stack,
7078                                       _finger,
7079                                       gfa, this);
7080   bool res = _work_queue->push(obj);   // overflow could occur here
7081   assert(res, "Will hold once we use workqueues");
7082   while (true) {
7083     oop new_oop;
7084     if (!_work_queue->pop_local(new_oop)) {
7085       // We emptied our work_queue; check if there's stuff that can
7086       // be gotten from the overflow stack.
7087       if (CMSConcMarkingTask::get_work_from_overflow_stack(
7088             _overflow_stack, _work_queue)) {