1 /*
   2  * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPGENERATION_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPGENERATION_HPP
  27 
  28 #include "gc_implementation/shared/gcHeapSummary.hpp"
  29 #include "gc_implementation/shared/gSpaceCounters.hpp"
  30 #include "gc_implementation/shared/gcStats.hpp"
  31 #include "gc_implementation/shared/gcWhen.hpp"
  32 #include "gc_implementation/shared/generationCounters.hpp"
  33 #include "memory/freeBlockDictionary.hpp"
  34 #include "memory/generation.hpp"
  35 #include "memory/iterator.hpp"
  36 #include "runtime/mutexLocker.hpp"
  37 #include "runtime/virtualspace.hpp"
  38 #include "services/memoryService.hpp"
  39 #include "utilities/bitMap.inline.hpp"
  40 #include "utilities/stack.inline.hpp"
  41 #include "utilities/taskqueue.hpp"
  42 #include "utilities/yieldingWorkgroup.hpp"
  43 
  44 // ConcurrentMarkSweepGeneration is in support of a concurrent
  45 // mark-sweep old generation in the Detlefs-Printezis--Boehm-Demers-Schenker
  46 // style. We assume, for now, that this generation is always the
  47 // seniormost generation and for simplicity
  48 // in the first implementation, that this generation is a single compactible
  49 // space. Neither of these restrictions appears essential, and will be
  50 // relaxed in the future when more time is available to implement the
  51 // greater generality (and there's a need for it).
  52 //
  53 // Concurrent mode failures are currently handled by
  54 // means of a sliding mark-compact.
  55 
  56 class AdaptiveSizePolicy;
  57 class CMSConcMarkingTask;
  58 class CMSGCAdaptivePolicyCounters;
  59 class CMSTracer;
  60 class ConcurrentGCTimer;
  61 class ConcurrentMarkSweepGeneration;
  62 class ConcurrentMarkSweepPolicy;
  63 class ConcurrentMarkSweepThread;
  64 class CompactibleFreeListSpace;
  65 class FreeChunk;
  66 class PromotionInfo;
  67 class ScanMarkedObjectsAgainCarefullyClosure;
  68 class TenuredGeneration;
  69 class SerialOldTracer;
  70 
  71 // A generic CMS bit map. It's the basis for both the CMS marking bit map
  72 // as well as for the mod union table (in each case only a subset of the
  73 // methods are used). This is essentially a wrapper around the BitMap class,
  74 // with one bit per (1<<_shifter) HeapWords. (i.e. for the marking bit map,
  75 // we have _shifter == 0. and for the mod union table we have
  76 // shifter == CardTableModRefBS::card_shift - LogHeapWordSize.)
  77 // XXX 64-bit issues in BitMap?
  78 class CMSBitMap VALUE_OBJ_CLASS_SPEC {
  79   friend class VMStructs;
  80 
  81   HeapWord* _bmStartWord;   // base address of range covered by map
  82   size_t    _bmWordSize;    // map size (in #HeapWords covered)
  83   const int _shifter;       // shifts to convert HeapWord to bit position
  84   VirtualSpace _virtual_space; // underlying the bit map
  85   BitMap    _bm;            // the bit map itself
  86  public:
  87   Mutex* const _lock;       // mutex protecting _bm;
  88 
  89  public:
  90   // constructor
  91   CMSBitMap(int shifter, int mutex_rank, const char* mutex_name);
  92 
  93   // allocates the actual storage for the map
  94   bool allocate(MemRegion mr);
  95   // field getter
  96   Mutex* lock() const { return _lock; }
  97   // locking verifier convenience function
  98   void assert_locked() const PRODUCT_RETURN;
  99 
 100   // inquiries
 101   HeapWord* startWord()   const { return _bmStartWord; }
 102   size_t    sizeInWords() const { return _bmWordSize;  }
 103   size_t    sizeInBits()  const { return _bm.size();   }
 104   // the following is one past the last word in space
 105   HeapWord* endWord()     const { return _bmStartWord + _bmWordSize; }
 106 
 107   // reading marks
 108   bool isMarked(HeapWord* addr) const;
 109   bool par_isMarked(HeapWord* addr) const; // do not lock checks
 110   bool isUnmarked(HeapWord* addr) const;
 111   bool isAllClear() const;
 112 
 113   // writing marks
 114   void mark(HeapWord* addr);
 115   // For marking by parallel GC threads;
 116   // returns true if we did, false if another thread did
 117   bool par_mark(HeapWord* addr);
 118 
 119   void mark_range(MemRegion mr);
 120   void par_mark_range(MemRegion mr);
 121   void mark_large_range(MemRegion mr);
 122   void par_mark_large_range(MemRegion mr);
 123   void par_clear(HeapWord* addr); // For unmarking by parallel GC threads.
 124   void clear_range(MemRegion mr);
 125   void par_clear_range(MemRegion mr);
 126   void clear_large_range(MemRegion mr);
 127   void par_clear_large_range(MemRegion mr);
 128   void clear_all();
 129   void clear_all_incrementally();  // Not yet implemented!!
 130 
 131   NOT_PRODUCT(
 132     // checks the memory region for validity
 133     void region_invariant(MemRegion mr);
 134   )
 135 
 136   // iteration
 137   void iterate(BitMapClosure* cl) {
 138     _bm.iterate(cl);
 139   }
 140   void iterate(BitMapClosure* cl, HeapWord* left, HeapWord* right);
 141   void dirty_range_iterate_clear(MemRegionClosure* cl);
 142   void dirty_range_iterate_clear(MemRegion mr, MemRegionClosure* cl);
 143 
 144   // auxiliary support for iteration
 145   HeapWord* getNextMarkedWordAddress(HeapWord* addr) const;
 146   HeapWord* getNextMarkedWordAddress(HeapWord* start_addr,
 147                                             HeapWord* end_addr) const;
 148   HeapWord* getNextUnmarkedWordAddress(HeapWord* addr) const;
 149   HeapWord* getNextUnmarkedWordAddress(HeapWord* start_addr,
 150                                               HeapWord* end_addr) const;
 151   MemRegion getAndClearMarkedRegion(HeapWord* addr);
 152   MemRegion getAndClearMarkedRegion(HeapWord* start_addr,
 153                                            HeapWord* end_addr);
 154 
 155   // conversion utilities
 156   HeapWord* offsetToHeapWord(size_t offset) const;
 157   size_t    heapWordToOffset(HeapWord* addr) const;
 158   size_t    heapWordDiffToOffsetDiff(size_t diff) const;
 159 
 160   void print_on_error(outputStream* st, const char* prefix) const;
 161 
 162   // debugging
 163   // is this address range covered by the bit-map?
 164   NOT_PRODUCT(
 165     bool covers(MemRegion mr) const;
 166     bool covers(HeapWord* start, size_t size = 0) const;
 167   )
 168   void verifyNoOneBitsInRange(HeapWord* left, HeapWord* right) PRODUCT_RETURN;
 169 };
 170 
 171 // Represents a marking stack used by the CMS collector.
 172 // Ideally this should be GrowableArray<> just like MSC's marking stack(s).
 173 class CMSMarkStack: public CHeapObj<mtGC>  {
 174   //
 175   friend class CMSCollector;   // To get at expansion stats further below.
 176   //
 177 
 178   VirtualSpace _virtual_space;  // Space for the stack
 179   oop*   _base;      // Bottom of stack
 180   size_t _index;     // One more than last occupied index
 181   size_t _capacity;  // Max #elements
 182   Mutex  _par_lock;  // An advisory lock used in case of parallel access
 183   NOT_PRODUCT(size_t _max_depth;)  // Max depth plumbed during run
 184 
 185  protected:
 186   size_t _hit_limit;      // We hit max stack size limit
 187   size_t _failed_double;  // We failed expansion before hitting limit
 188 
 189  public:
 190   CMSMarkStack():
 191     _par_lock(Mutex::event, "CMSMarkStack._par_lock", true),
 192     _hit_limit(0),
 193     _failed_double(0) {}
 194 
 195   bool allocate(size_t size);
 196 
 197   size_t capacity() const { return _capacity; }
 198 
 199   oop pop() {
 200     if (!isEmpty()) {
 201       return _base[--_index] ;
 202     }
 203     return NULL;
 204   }
 205 
 206   bool push(oop ptr) {
 207     if (isFull()) {
 208       return false;
 209     } else {
 210       _base[_index++] = ptr;
 211       NOT_PRODUCT(_max_depth = MAX2(_max_depth, _index));
 212       return true;
 213     }
 214   }
 215 
 216   bool isEmpty() const { return _index == 0; }
 217   bool isFull()  const {
 218     assert(_index <= _capacity, "buffer overflow");
 219     return _index == _capacity;
 220   }
 221 
 222   size_t length() { return _index; }
 223 
 224   // "Parallel versions" of some of the above
 225   oop par_pop() {
 226     // lock and pop
 227     MutexLockerEx x(&_par_lock, Mutex::_no_safepoint_check_flag);
 228     return pop();
 229   }
 230 
 231   bool par_push(oop ptr) {
 232     // lock and push
 233     MutexLockerEx x(&_par_lock, Mutex::_no_safepoint_check_flag);
 234     return push(ptr);
 235   }
 236 
 237   // Forcibly reset the stack, losing all of its contents.
 238   void reset() {
 239     _index = 0;
 240   }
 241 
 242   // Expand the stack, typically in response to an overflow condition.
 243   void expand();
 244 
 245   // Compute the least valued stack element.
 246   oop least_value(HeapWord* low) {
 247      oop least = (oop)low;
 248      for (size_t i = 0; i < _index; i++) {
 249        least = MIN2(least, _base[i]);
 250      }
 251      return least;
 252   }
 253 
 254   // Exposed here to allow stack expansion in || case.
 255   Mutex* par_lock() { return &_par_lock; }
 256 };
 257 
 258 class CardTableRS;
 259 class CMSParGCThreadState;
 260 
 261 class ModUnionClosure: public MemRegionClosure {
 262  protected:
 263   CMSBitMap* _t;
 264  public:
 265   ModUnionClosure(CMSBitMap* t): _t(t) { }
 266   void do_MemRegion(MemRegion mr);
 267 };
 268 
 269 class ModUnionClosurePar: public ModUnionClosure {
 270  public:
 271   ModUnionClosurePar(CMSBitMap* t): ModUnionClosure(t) { }
 272   void do_MemRegion(MemRegion mr);
 273 };
 274 
 275 // Survivor Chunk Array in support of parallelization of
 276 // Survivor Space rescan.
 277 class ChunkArray: public CHeapObj<mtGC> {
 278   size_t _index;
 279   size_t _capacity;
 280   size_t _overflows;
 281   HeapWord** _array;   // storage for array
 282 
 283  public:
 284   ChunkArray() : _index(0), _capacity(0), _overflows(0), _array(NULL) {}
 285   ChunkArray(HeapWord** a, size_t c):
 286     _index(0), _capacity(c), _overflows(0), _array(a) {}
 287 
 288   HeapWord** array() { return _array; }
 289   void set_array(HeapWord** a) { _array = a; }
 290 
 291   size_t capacity() { return _capacity; }
 292   void set_capacity(size_t c) { _capacity = c; }
 293 
 294   size_t end() {
 295     assert(_index <= capacity(),
 296            err_msg("_index (" SIZE_FORMAT ") > _capacity (" SIZE_FORMAT "): out of bounds",
 297                    _index, _capacity));
 298     return _index;
 299   }  // exclusive
 300 
 301   HeapWord* nth(size_t n) {
 302     assert(n < end(), "Out of bounds access");
 303     return _array[n];
 304   }
 305 
 306   void reset() {
 307     _index = 0;
 308     if (_overflows > 0 && PrintCMSStatistics > 1) {
 309       warning("CMS: ChunkArray[" SIZE_FORMAT "] overflowed " SIZE_FORMAT " times",
 310               _capacity, _overflows);
 311     }
 312     _overflows = 0;
 313   }
 314 
 315   void record_sample(HeapWord* p, size_t sz) {
 316     // For now we do not do anything with the size
 317     if (_index < _capacity) {
 318       _array[_index++] = p;
 319     } else {
 320       ++_overflows;
 321       assert(_index == _capacity,
 322              err_msg("_index (" SIZE_FORMAT ") > _capacity (" SIZE_FORMAT
 323                      "): out of bounds at overflow#" SIZE_FORMAT,
 324                      _index, _capacity, _overflows));
 325     }
 326   }
 327 };
 328 
 329 //
 330 // Timing, allocation and promotion statistics for gc scheduling and incremental
 331 // mode pacing.  Most statistics are exponential averages.
 332 //
 333 class CMSStats VALUE_OBJ_CLASS_SPEC {
 334  private:
 335   ConcurrentMarkSweepGeneration* const _cms_gen;   // The cms (old) gen.
 336 
 337   // The following are exponential averages with factor alpha:
 338   //   avg = (100 - alpha) * avg + alpha * cur_sample
 339   //
 340   //   The durations measure:  end_time[n] - start_time[n]
 341   //   The periods measure:    start_time[n] - start_time[n-1]
 342   //
 343   // The cms period and duration include only concurrent collections; time spent
 344   // in foreground cms collections due to System.gc() or because of a failure to
 345   // keep up are not included.
 346   //
 347   // There are 3 alphas to "bootstrap" the statistics.  The _saved_alpha is the
 348   // real value, but is used only after the first period.  A value of 100 is
 349   // used for the first sample so it gets the entire weight.
 350   unsigned int _saved_alpha; // 0-100
 351   unsigned int _gc0_alpha;
 352   unsigned int _cms_alpha;
 353 
 354   double _gc0_duration;
 355   double _gc0_period;
 356   size_t _gc0_promoted;         // bytes promoted per gc0
 357   double _cms_duration;
 358   double _cms_duration_pre_sweep; // time from initiation to start of sweep
 359   double _cms_duration_per_mb;
 360   double _cms_period;
 361   size_t _cms_allocated;        // bytes of direct allocation per gc0 period
 362 
 363   // Timers.
 364   elapsedTimer _cms_timer;
 365   TimeStamp    _gc0_begin_time;
 366   TimeStamp    _cms_begin_time;
 367   TimeStamp    _cms_end_time;
 368 
 369   // Snapshots of the amount used in the CMS generation.
 370   size_t _cms_used_at_gc0_begin;
 371   size_t _cms_used_at_gc0_end;
 372   size_t _cms_used_at_cms_begin;
 373 
 374   // Used to prevent the duty cycle from being reduced in the middle of a cms
 375   // cycle.
 376   bool _allow_duty_cycle_reduction;
 377 
 378   enum {
 379     _GC0_VALID = 0x1,
 380     _CMS_VALID = 0x2,
 381     _ALL_VALID = _GC0_VALID | _CMS_VALID
 382   };
 383 
 384   unsigned int _valid_bits;
 385 
 386   unsigned int _icms_duty_cycle;        // icms duty cycle (0-100).
 387 
 388  protected:
 389 
 390   // Return a duty cycle that avoids wild oscillations, by limiting the amount
 391   // of change between old_duty_cycle and new_duty_cycle (the latter is treated
 392   // as a recommended value).
 393   static unsigned int icms_damped_duty_cycle(unsigned int old_duty_cycle,
 394                                              unsigned int new_duty_cycle);
 395   unsigned int icms_update_duty_cycle_impl();
 396 
 397   // In support of adjusting of cms trigger ratios based on history
 398   // of concurrent mode failure.
 399   double cms_free_adjustment_factor(size_t free) const;
 400   void   adjust_cms_free_adjustment_factor(bool fail, size_t free);
 401 
 402  public:
 403   CMSStats(ConcurrentMarkSweepGeneration* cms_gen,
 404            unsigned int alpha = CMSExpAvgFactor);
 405 
 406   // Whether or not the statistics contain valid data; higher level statistics
 407   // cannot be called until this returns true (they require at least one young
 408   // gen and one cms cycle to have completed).
 409   bool valid() const;
 410 
 411   // Record statistics.
 412   void record_gc0_begin();
 413   void record_gc0_end(size_t cms_gen_bytes_used);
 414   void record_cms_begin();
 415   void record_cms_end();
 416 
 417   // Allow management of the cms timer, which must be stopped/started around
 418   // yield points.
 419   elapsedTimer& cms_timer()     { return _cms_timer; }
 420   void start_cms_timer()        { _cms_timer.start(); }
 421   void stop_cms_timer()         { _cms_timer.stop(); }
 422 
 423   // Basic statistics; units are seconds or bytes.
 424   double gc0_period() const     { return _gc0_period; }
 425   double gc0_duration() const   { return _gc0_duration; }
 426   size_t gc0_promoted() const   { return _gc0_promoted; }
 427   double cms_period() const          { return _cms_period; }
 428   double cms_duration() const        { return _cms_duration; }
 429   double cms_duration_per_mb() const { return _cms_duration_per_mb; }
 430   size_t cms_allocated() const       { return _cms_allocated; }
 431 
 432   size_t cms_used_at_gc0_end() const { return _cms_used_at_gc0_end;}
 433 
 434   // Seconds since the last background cms cycle began or ended.
 435   double cms_time_since_begin() const;
 436   double cms_time_since_end() const;
 437 
 438   // Higher level statistics--caller must check that valid() returns true before
 439   // calling.
 440 
 441   // Returns bytes promoted per second of wall clock time.
 442   double promotion_rate() const;
 443 
 444   // Returns bytes directly allocated per second of wall clock time.
 445   double cms_allocation_rate() const;
 446 
 447   // Rate at which space in the cms generation is being consumed (sum of the
 448   // above two).
 449   double cms_consumption_rate() const;
 450 
 451   // Returns an estimate of the number of seconds until the cms generation will
 452   // fill up, assuming no collection work is done.
 453   double time_until_cms_gen_full() const;
 454 
 455   // Returns an estimate of the number of seconds remaining until
 456   // the cms generation collection should start.
 457   double time_until_cms_start() const;
 458 
 459   // End of higher level statistics.
 460 
 461   // Returns the cms incremental mode duty cycle, as a percentage (0-100).
 462   unsigned int icms_duty_cycle() const { return _icms_duty_cycle; }
 463 
 464   // Update the duty cycle and return the new value.
 465   unsigned int icms_update_duty_cycle();
 466 
 467   // Debugging.
 468   void print_on(outputStream* st) const PRODUCT_RETURN;
 469   void print() const { print_on(gclog_or_tty); }
 470 };
 471 
 472 // A closure related to weak references processing which
 473 // we embed in the CMSCollector, since we need to pass
 474 // it to the reference processor for secondary filtering
 475 // of references based on reachability of referent;
 476 // see role of _is_alive_non_header closure in the
 477 // ReferenceProcessor class.
 478 // For objects in the CMS generation, this closure checks
 479 // if the object is "live" (reachable). Used in weak
 480 // reference processing.
 481 class CMSIsAliveClosure: public BoolObjectClosure {
 482   const MemRegion  _span;
 483   const CMSBitMap* _bit_map;
 484 
 485   friend class CMSCollector;
 486  public:
 487   CMSIsAliveClosure(MemRegion span,
 488                     CMSBitMap* bit_map):
 489     _span(span),
 490     _bit_map(bit_map) {
 491     assert(!span.is_empty(), "Empty span could spell trouble");
 492   }
 493 
 494   bool do_object_b(oop obj);
 495 };
 496 
 497 
 498 // Implements AbstractRefProcTaskExecutor for CMS.
 499 class CMSRefProcTaskExecutor: public AbstractRefProcTaskExecutor {
 500 public:
 501 
 502   CMSRefProcTaskExecutor(CMSCollector& collector)
 503     : _collector(collector)
 504   { }
 505 
 506   // Executes a task using worker threads.
 507   virtual void execute(ProcessTask& task);
 508   virtual void execute(EnqueueTask& task);
 509 private:
 510   CMSCollector& _collector;
 511 };
 512 
 513 
 514 class CMSCollector: public CHeapObj<mtGC> {
 515   friend class VMStructs;
 516   friend class ConcurrentMarkSweepThread;
 517   friend class ConcurrentMarkSweepGeneration;
 518   friend class CompactibleFreeListSpace;
 519   friend class CMSParMarkTask;
 520   friend class CMSParInitialMarkTask;
 521   friend class CMSParRemarkTask;
 522   friend class CMSConcMarkingTask;
 523   friend class CMSRefProcTaskProxy;
 524   friend class CMSRefProcTaskExecutor;
 525   friend class ScanMarkedObjectsAgainCarefullyClosure;  // for sampling eden
 526   friend class SurvivorSpacePrecleanClosure;            // --- ditto -------
 527   friend class PushOrMarkClosure;             // to access _restart_addr
 528   friend class Par_PushOrMarkClosure;             // to access _restart_addr
 529   friend class MarkFromRootsClosure;          //  -- ditto --
 530                                               // ... and for clearing cards
 531   friend class Par_MarkFromRootsClosure;      //  to access _restart_addr
 532                                               // ... and for clearing cards
 533   friend class Par_ConcMarkingClosure;        //  to access _restart_addr etc.
 534   friend class MarkFromRootsVerifyClosure;    // to access _restart_addr
 535   friend class PushAndMarkVerifyClosure;      //  -- ditto --
 536   friend class MarkRefsIntoAndScanClosure;    // to access _overflow_list
 537   friend class PushAndMarkClosure;            //  -- ditto --
 538   friend class Par_PushAndMarkClosure;        //  -- ditto --
 539   friend class CMSKeepAliveClosure;           //  -- ditto --
 540   friend class CMSDrainMarkingStackClosure;   //  -- ditto --
 541   friend class CMSInnerParMarkAndPushClosure; //  -- ditto --
 542   NOT_PRODUCT(friend class ScanMarkedObjectsAgainClosure;) //  assertion on _overflow_list
 543   friend class ReleaseForegroundGC;  // to access _foregroundGCShouldWait
 544   friend class VM_CMS_Operation;
 545   friend class VM_CMS_Initial_Mark;
 546   friend class VM_CMS_Final_Remark;
 547   friend class TraceCMSMemoryManagerStats;
 548 
 549  private:
 550   jlong _time_of_last_gc;
 551   void update_time_of_last_gc(jlong now) {
 552     _time_of_last_gc = now;
 553   }
 554 
 555   OopTaskQueueSet* _task_queues;
 556 
 557   // Overflow list of grey objects, threaded through mark-word
 558   // Manipulated with CAS in the parallel/multi-threaded case.
 559   oop _overflow_list;
 560   // The following array-pair keeps track of mark words
 561   // displaced for accommodating overflow list above.
 562   // This code will likely be revisited under RFE#4922830.
 563   Stack<oop, mtGC>     _preserved_oop_stack;
 564   Stack<markOop, mtGC> _preserved_mark_stack;
 565 
 566   int*             _hash_seed;
 567 
 568   // In support of multi-threaded concurrent phases
 569   YieldingFlexibleWorkGang* _conc_workers;
 570 
 571   // Performance Counters
 572   CollectorCounters* _gc_counters;
 573 
 574   // Initialization Errors
 575   bool _completed_initialization;
 576 
 577   // In support of ExplicitGCInvokesConcurrent
 578   static bool _full_gc_requested;
 579   static GCCause::Cause _full_gc_cause;
 580   unsigned int _collection_count_start;
 581 
 582   // Should we unload classes this concurrent cycle?
 583   bool _should_unload_classes;
 584   unsigned int  _concurrent_cycles_since_last_unload;
 585   unsigned int concurrent_cycles_since_last_unload() const {
 586     return _concurrent_cycles_since_last_unload;
 587   }
 588   // Did we (allow) unload classes in the previous concurrent cycle?
 589   bool unloaded_classes_last_cycle() const {
 590     return concurrent_cycles_since_last_unload() == 0;
 591   }
 592   // Root scanning options for perm gen
 593   int _roots_scanning_options;
 594   int roots_scanning_options() const      { return _roots_scanning_options; }
 595   void add_root_scanning_option(int o)    { _roots_scanning_options |= o;   }
 596   void remove_root_scanning_option(int o) { _roots_scanning_options &= ~o;  }
 597 
 598   // Verification support
 599   CMSBitMap     _verification_mark_bm;
 600   void verify_after_remark_work_1();
 601   void verify_after_remark_work_2();
 602 
 603   // True if any verification flag is on.
 604   bool _verifying;
 605   bool verifying() const { return _verifying; }
 606   void set_verifying(bool v) { _verifying = v; }
 607 
 608   // Collector policy
 609   ConcurrentMarkSweepPolicy* _collector_policy;
 610   ConcurrentMarkSweepPolicy* collector_policy() { return _collector_policy; }
 611 
 612   void set_did_compact(bool v);
 613 
 614   // XXX Move these to CMSStats ??? FIX ME !!!
 615   elapsedTimer _inter_sweep_timer;   // Time between sweeps
 616   elapsedTimer _intra_sweep_timer;   // Time _in_ sweeps
 617   // Padded decaying average estimates of the above
 618   AdaptivePaddedAverage _inter_sweep_estimate;
 619   AdaptivePaddedAverage _intra_sweep_estimate;
 620 
 621   CMSTracer* _gc_tracer_cm;
 622   ConcurrentGCTimer* _gc_timer_cm;
 623 
 624   bool _cms_start_registered;
 625 
 626   GCHeapSummary _last_heap_summary;
 627   MetaspaceSummary _last_metaspace_summary;
 628 
 629   void register_foreground_gc_start(GCCause::Cause cause);
 630   void register_gc_start(GCCause::Cause cause);
 631   void register_gc_end();
 632   void save_heap_summary();
 633   void report_heap_summary(GCWhen::Type when);
 634 
 635  protected:
 636   ConcurrentMarkSweepGeneration* _cmsGen;  // Old gen (CMS)
 637   MemRegion                      _span;    // Span covering above two
 638   CardTableRS*                   _ct;      // Card table
 639 
 640   // CMS marking support structures
 641   CMSBitMap     _markBitMap;
 642   CMSBitMap     _modUnionTable;
 643   CMSMarkStack  _markStack;
 644 
 645   HeapWord*     _restart_addr; // In support of marking stack overflow
 646   void          lower_restart_addr(HeapWord* low);
 647 
 648   // Counters in support of marking stack / work queue overflow handling:
 649   // a non-zero value indicates certain types of overflow events during
 650   // the current CMS cycle and could lead to stack resizing efforts at
 651   // an opportune future time.
 652   size_t        _ser_pmc_preclean_ovflw;
 653   size_t        _ser_pmc_remark_ovflw;
 654   size_t        _par_pmc_remark_ovflw;
 655   size_t        _ser_kac_preclean_ovflw;
 656   size_t        _ser_kac_ovflw;
 657   size_t        _par_kac_ovflw;
 658   NOT_PRODUCT(ssize_t _num_par_pushes;)
 659 
 660   // ("Weak") Reference processing support.
 661   ReferenceProcessor*            _ref_processor;
 662   CMSIsAliveClosure              _is_alive_closure;
 663   // Keep this textually after _markBitMap and _span; c'tor dependency.
 664 
 665   ConcurrentMarkSweepThread*     _cmsThread;   // The thread doing the work
 666   ModUnionClosure    _modUnionClosure;
 667   ModUnionClosurePar _modUnionClosurePar;
 668 
 669   // CMS abstract state machine
 670   // initial_state: Idling
 671   // next_state(Idling)            = {Marking}
 672   // next_state(Marking)           = {Precleaning, Sweeping}
 673   // next_state(Precleaning)       = {AbortablePreclean, FinalMarking}
 674   // next_state(AbortablePreclean) = {FinalMarking}
 675   // next_state(FinalMarking)      = {Sweeping}
 676   // next_state(Sweeping)          = {Resizing}
 677   // next_state(Resizing)          = {Resetting}
 678   // next_state(Resetting)         = {Idling}
 679   // The numeric values below are chosen so that:
 680   // . _collectorState <= Idling ==  post-sweep && pre-mark
 681   // . _collectorState in (Idling, Sweeping) == {initial,final}marking ||
 682   //                                            precleaning || abortablePrecleanb
 683  public:
 684   enum CollectorState {
 685     Resizing            = 0,
 686     Resetting           = 1,
 687     Idling              = 2,
 688     InitialMarking      = 3,
 689     Marking             = 4,
 690     Precleaning         = 5,
 691     AbortablePreclean   = 6,
 692     FinalMarking        = 7,
 693     Sweeping            = 8
 694   };
 695  protected:
 696   static CollectorState _collectorState;
 697 
 698   // State related to prologue/epilogue invocation for my generations
 699   bool _between_prologue_and_epilogue;
 700 
 701   // Signaling/State related to coordination between fore- and background GC
 702   // Note: When the baton has been passed from background GC to foreground GC,
 703   // _foregroundGCIsActive is true and _foregroundGCShouldWait is false.
 704   static bool _foregroundGCIsActive;    // true iff foreground collector is active or
 705                                  // wants to go active
 706   static bool _foregroundGCShouldWait;  // true iff background GC is active and has not
 707                                  // yet passed the baton to the foreground GC
 708 
 709   // Support for CMSScheduleRemark (abortable preclean)
 710   bool _abort_preclean;
 711   bool _start_sampling;
 712 
 713   int    _numYields;
 714   size_t _numDirtyCards;
 715   size_t _sweep_count;
 716   // Number of full gc's since the last concurrent gc.
 717   uint   _full_gcs_since_conc_gc;
 718 
 719   // Occupancy used for bootstrapping stats
 720   double _bootstrap_occupancy;
 721 
 722   // Timer
 723   elapsedTimer _timer;
 724 
 725   // Timing, allocation and promotion statistics, used for scheduling.
 726   CMSStats      _stats;
 727 
 728   // Allocation limits installed in the young gen, used only in
 729   // CMSIncrementalMode.  When an allocation in the young gen would cross one of
 730   // these limits, the cms generation is notified and the cms thread is started
 731   // or stopped, respectively.
 732   HeapWord*     _icms_start_limit;
 733   HeapWord*     _icms_stop_limit;
 734 
 735   enum CMS_op_type {
 736     CMS_op_checkpointRootsInitial,
 737     CMS_op_checkpointRootsFinal
 738   };
 739 
 740   void do_CMS_operation(CMS_op_type op, GCCause::Cause gc_cause);
 741   bool stop_world_and_do(CMS_op_type op);
 742 
 743   OopTaskQueueSet* task_queues() { return _task_queues; }
 744   int*             hash_seed(int i) { return &_hash_seed[i]; }
 745   YieldingFlexibleWorkGang* conc_workers() { return _conc_workers; }
 746 
 747   // Support for parallelizing Eden rescan in CMS remark phase
 748   void sample_eden(); // ... sample Eden space top
 749 
 750  private:
 751   // Support for parallelizing young gen rescan in CMS remark phase
 752   Generation* _young_gen;  // the younger gen
 753   HeapWord** _top_addr;    // ... Top of Eden
 754   HeapWord** _end_addr;    // ... End of Eden
 755   Mutex*     _eden_chunk_lock;
 756   HeapWord** _eden_chunk_array; // ... Eden partitioning array
 757   size_t     _eden_chunk_index; // ... top (exclusive) of array
 758   size_t     _eden_chunk_capacity;  // ... max entries in array
 759 
 760   // Support for parallelizing survivor space rescan
 761   HeapWord** _survivor_chunk_array;
 762   size_t     _survivor_chunk_index;
 763   size_t     _survivor_chunk_capacity;
 764   size_t*    _cursor;
 765   ChunkArray* _survivor_plab_array;
 766 
 767   // Support for marking stack overflow handling
 768   bool take_from_overflow_list(size_t num, CMSMarkStack* to_stack);
 769   bool par_take_from_overflow_list(size_t num,
 770                                    OopTaskQueue* to_work_q,
 771                                    int no_of_gc_threads);
 772   void push_on_overflow_list(oop p);
 773   void par_push_on_overflow_list(oop p);
 774   // The following is, obviously, not, in general, "MT-stable"
 775   bool overflow_list_is_empty() const;
 776 
 777   void preserve_mark_if_necessary(oop p);
 778   void par_preserve_mark_if_necessary(oop p);
 779   void preserve_mark_work(oop p, markOop m);
 780   void restore_preserved_marks_if_any();
 781   NOT_PRODUCT(bool no_preserved_marks() const;)
 782   // In support of testing overflow code
 783   NOT_PRODUCT(int _overflow_counter;)
 784   NOT_PRODUCT(bool simulate_overflow();)       // Sequential
 785   NOT_PRODUCT(bool par_simulate_overflow();)   // MT version
 786 
 787   // CMS work methods
 788   void checkpointRootsInitialWork(bool asynch); // Initial checkpoint work
 789 
 790   // A return value of false indicates failure due to stack overflow
 791   bool markFromRootsWork(bool asynch);  // Concurrent marking work
 792 
 793  public:   // FIX ME!!! only for testing
 794   bool do_marking_st(bool asynch);      // Single-threaded marking
 795   bool do_marking_mt(bool asynch);      // Multi-threaded  marking
 796 
 797  private:
 798 
 799   // Concurrent precleaning work
 800   size_t preclean_mod_union_table(ConcurrentMarkSweepGeneration* gen,
 801                                   ScanMarkedObjectsAgainCarefullyClosure* cl);
 802   size_t preclean_card_table(ConcurrentMarkSweepGeneration* gen,
 803                              ScanMarkedObjectsAgainCarefullyClosure* cl);
 804   // Does precleaning work, returning a quantity indicative of
 805   // the amount of "useful work" done.
 806   size_t preclean_work(bool clean_refs, bool clean_survivors);
 807   void preclean_klasses(MarkRefsIntoAndScanClosure* cl, Mutex* freelistLock);
 808   void abortable_preclean(); // Preclean while looking for possible abort
 809   void initialize_sequential_subtasks_for_young_gen_rescan(int i);
 810   // Helper function for above; merge-sorts the per-thread plab samples
 811   void merge_survivor_plab_arrays(ContiguousSpace* surv, int no_of_gc_threads);
 812   // Resets (i.e. clears) the per-thread plab sample vectors
 813   void reset_survivor_plab_arrays();
 814 
 815   // Final (second) checkpoint work
 816   void checkpointRootsFinalWork(bool asynch, bool clear_all_soft_refs,
 817                                 bool init_mark_was_synchronous);
 818   // Work routine for parallel version of remark
 819   void do_remark_parallel();
 820   // Work routine for non-parallel version of remark
 821   void do_remark_non_parallel();
 822   // Reference processing work routine (during second checkpoint)
 823   void refProcessingWork(bool asynch, bool clear_all_soft_refs);
 824 
 825   // Concurrent sweeping work
 826   void sweepWork(ConcurrentMarkSweepGeneration* gen, bool asynch);
 827 
 828   // (Concurrent) resetting of support data structures
 829   void reset(bool asynch);
 830 
 831   // Clear _expansion_cause fields of constituent generations
 832   void clear_expansion_cause();
 833 
 834   // An auxiliary method used to record the ends of
 835   // used regions of each generation to limit the extent of sweep
 836   void save_sweep_limits();
 837 
 838   // A work method used by foreground collection to determine
 839   // what type of collection (compacting or not, continuing or fresh)
 840   // it should do.
 841   void decide_foreground_collection_type(bool clear_all_soft_refs,
 842     bool* should_compact, bool* should_start_over);
 843 
 844   // A work method used by the foreground collector to do
 845   // a mark-sweep-compact.
 846   void do_compaction_work(bool clear_all_soft_refs);
 847 
 848   // A work method used by the foreground collector to do
 849   // a mark-sweep, after taking over from a possibly on-going
 850   // concurrent mark-sweep collection.
 851   void do_mark_sweep_work(bool clear_all_soft_refs,
 852     CollectorState first_state, bool should_start_over);
 853 
 854   // Work methods for reporting concurrent mode interruption or failure
 855   bool is_external_interruption();
 856   void report_concurrent_mode_interruption();
 857 
 858   // If the background GC is active, acquire control from the background
 859   // GC and do the collection.
 860   void acquire_control_and_collect(bool   full, bool clear_all_soft_refs);
 861 
 862   // For synchronizing passing of control from background to foreground
 863   // GC.  waitForForegroundGC() is called by the background
 864   // collector.  It if had to wait for a foreground collection,
 865   // it returns true and the background collection should assume
 866   // that the collection was finished by the foreground
 867   // collector.
 868   bool waitForForegroundGC();
 869 
 870   // Incremental mode triggering:  recompute the icms duty cycle and set the
 871   // allocation limits in the young gen.
 872   void icms_update_allocation_limits();
 873 
 874   size_t block_size_using_printezis_bits(HeapWord* addr) const;
 875   size_t block_size_if_printezis_bits(HeapWord* addr) const;
 876   HeapWord* next_card_start_after_block(HeapWord* addr) const;
 877 
 878   void setup_cms_unloading_and_verification_state();
 879  public:
 880   CMSCollector(ConcurrentMarkSweepGeneration* cmsGen,
 881                CardTableRS*                   ct,
 882                ConcurrentMarkSweepPolicy*     cp);
 883   ConcurrentMarkSweepThread* cmsThread() { return _cmsThread; }
 884 
 885   ReferenceProcessor* ref_processor() { return _ref_processor; }
 886   void ref_processor_init();
 887 
 888   Mutex* bitMapLock()        const { return _markBitMap.lock();    }
 889   static CollectorState abstract_state() { return _collectorState;  }
 890 
 891   bool should_abort_preclean() const; // Whether preclean should be aborted.
 892   size_t get_eden_used() const;
 893   size_t get_eden_capacity() const;
 894 
 895   ConcurrentMarkSweepGeneration* cmsGen() { return _cmsGen; }
 896 
 897   // Locking checks
 898   NOT_PRODUCT(static bool have_cms_token();)
 899 
 900   // XXXPERM bool should_collect(bool full, size_t size, bool tlab);
 901   bool shouldConcurrentCollect();
 902 
 903   void collect(bool   full,
 904                bool   clear_all_soft_refs,
 905                size_t size,
 906                bool   tlab);
 907   void collect_in_background(bool clear_all_soft_refs, GCCause::Cause cause);
 908   void collect_in_foreground(bool clear_all_soft_refs, GCCause::Cause cause);
 909 
 910   // In support of ExplicitGCInvokesConcurrent
 911   static void request_full_gc(unsigned int full_gc_count, GCCause::Cause cause);
 912   // Should we unload classes in a particular concurrent cycle?
 913   bool should_unload_classes() const {
 914     return _should_unload_classes;
 915   }
 916   void update_should_unload_classes();
 917 
 918   void direct_allocated(HeapWord* start, size_t size);
 919 
 920   // Object is dead if not marked and current phase is sweeping.
 921   bool is_dead_obj(oop obj) const;
 922 
 923   // After a promotion (of "start"), do any necessary marking.
 924   // If "par", then it's being done by a parallel GC thread.
 925   // The last two args indicate if we need precise marking
 926   // and if so the size of the object so it can be dirtied
 927   // in its entirety.
 928   void promoted(bool par, HeapWord* start,
 929                 bool is_obj_array, size_t obj_size);
 930 
 931   HeapWord* allocation_limit_reached(Space* space, HeapWord* top,
 932                                      size_t word_size);
 933 
 934   void getFreelistLocks() const;
 935   void releaseFreelistLocks() const;
 936   bool haveFreelistLocks() const;
 937 
 938   // Adjust size of underlying generation
 939   void compute_new_size();
 940 
 941   // GC prologue and epilogue
 942   void gc_prologue(bool full);
 943   void gc_epilogue(bool full);
 944 
 945   jlong time_of_last_gc(jlong now) {
 946     if (_collectorState <= Idling) {
 947       // gc not in progress
 948       return _time_of_last_gc;
 949     } else {
 950       // collection in progress
 951       return now;
 952     }
 953   }
 954 
 955   // Support for parallel remark of survivor space
 956   void* get_data_recorder(int thr_num);
 957   void sample_eden_chunk();
 958 
 959   CMSBitMap* markBitMap()  { return &_markBitMap; }
 960   void directAllocated(HeapWord* start, size_t size);
 961 
 962   // Main CMS steps and related support
 963   void checkpointRootsInitial(bool asynch);
 964   bool markFromRoots(bool asynch);  // a return value of false indicates failure
 965                                     // due to stack overflow
 966   void preclean();
 967   void checkpointRootsFinal(bool asynch, bool clear_all_soft_refs,
 968                             bool init_mark_was_synchronous);
 969   void sweep(bool asynch);
 970 
 971   // Check that the currently executing thread is the expected
 972   // one (foreground collector or background collector).
 973   static void check_correct_thread_executing() PRODUCT_RETURN;
 974   // XXXPERM void print_statistics()           PRODUCT_RETURN;
 975 
 976   bool is_cms_reachable(HeapWord* addr);
 977 
 978   // Performance Counter Support
 979   CollectorCounters* counters()    { return _gc_counters; }
 980 
 981   // Timer stuff
 982   void    startTimer() { assert(!_timer.is_active(), "Error"); _timer.start();   }
 983   void    stopTimer()  { assert( _timer.is_active(), "Error"); _timer.stop();    }
 984   void    resetTimer() { assert(!_timer.is_active(), "Error"); _timer.reset();   }
 985   double  timerValue() { assert(!_timer.is_active(), "Error"); return _timer.seconds(); }
 986 
 987   int  yields()          { return _numYields; }
 988   void resetYields()     { _numYields = 0;    }
 989   void incrementYields() { _numYields++;      }
 990   void resetNumDirtyCards()               { _numDirtyCards = 0; }
 991   void incrementNumDirtyCards(size_t num) { _numDirtyCards += num; }
 992   size_t  numDirtyCards()                 { return _numDirtyCards; }
 993 
 994   static bool foregroundGCShouldWait() { return _foregroundGCShouldWait; }
 995   static void set_foregroundGCShouldWait(bool v) { _foregroundGCShouldWait = v; }
 996   static bool foregroundGCIsActive() { return _foregroundGCIsActive; }
 997   static void set_foregroundGCIsActive(bool v) { _foregroundGCIsActive = v; }
 998   size_t sweep_count() const             { return _sweep_count; }
 999   void   increment_sweep_count()         { _sweep_count++; }
1000 
1001   // Timers/stats for gc scheduling and incremental mode pacing.
1002   CMSStats& stats() { return _stats; }
1003 
1004   // Convenience methods that check whether CMSIncrementalMode is enabled and
1005   // forward to the corresponding methods in ConcurrentMarkSweepThread.
1006   static void start_icms();
1007   static void stop_icms();    // Called at the end of the cms cycle.
1008   static void disable_icms(); // Called before a foreground collection.
1009   static void enable_icms();  // Called after a foreground collection.
1010   void icms_wait();          // Called at yield points.
1011 
1012   // Adaptive size policy
1013   AdaptiveSizePolicy* size_policy();
1014 
1015   static void print_on_error(outputStream* st);
1016 
1017   // Debugging
1018   void verify();
1019   bool verify_after_remark(bool silent = VerifySilently);
1020   void verify_ok_to_terminate() const PRODUCT_RETURN;
1021   void verify_work_stacks_empty() const PRODUCT_RETURN;
1022   void verify_overflow_empty() const PRODUCT_RETURN;
1023 
1024   // Convenience methods in support of debugging
1025   static const size_t skip_header_HeapWords() PRODUCT_RETURN0;
1026   HeapWord* block_start(const void* p) const PRODUCT_RETURN0;
1027 
1028   // Accessors
1029   CMSMarkStack* verification_mark_stack() { return &_markStack; }
1030   CMSBitMap*    verification_mark_bm()    { return &_verification_mark_bm; }
1031 
1032   // Initialization errors
1033   bool completed_initialization() { return _completed_initialization; }
1034 
1035   void print_eden_and_survivor_chunk_arrays();
1036 };
1037 
1038 class CMSExpansionCause : public AllStatic  {
1039  public:
1040   enum Cause {
1041     _no_expansion,
1042     _satisfy_free_ratio,
1043     _satisfy_promotion,
1044     _satisfy_allocation,
1045     _allocate_par_lab,
1046     _allocate_par_spooling_space,
1047     _adaptive_size_policy
1048   };
1049   // Return a string describing the cause of the expansion.
1050   static const char* to_string(CMSExpansionCause::Cause cause);
1051 };
1052 
1053 class ConcurrentMarkSweepGeneration: public CardGeneration {
1054   friend class VMStructs;
1055   friend class ConcurrentMarkSweepThread;
1056   friend class ConcurrentMarkSweep;
1057   friend class CMSCollector;
1058  protected:
1059   static CMSCollector*       _collector; // the collector that collects us
1060   CompactibleFreeListSpace*  _cmsSpace;  // underlying space (only one for now)
1061 
1062   // Performance Counters
1063   GenerationCounters*      _gen_counters;
1064   GSpaceCounters*          _space_counters;
1065 
1066   // Words directly allocated, used by CMSStats.
1067   size_t _direct_allocated_words;
1068 
1069   // Non-product stat counters
1070   NOT_PRODUCT(
1071     size_t _numObjectsPromoted;
1072     size_t _numWordsPromoted;
1073     size_t _numObjectsAllocated;
1074     size_t _numWordsAllocated;
1075   )
1076 
1077   // Used for sizing decisions
1078   bool _incremental_collection_failed;
1079   bool incremental_collection_failed() {
1080     return _incremental_collection_failed;
1081   }
1082   void set_incremental_collection_failed() {
1083     _incremental_collection_failed = true;
1084   }
1085   void clear_incremental_collection_failed() {
1086     _incremental_collection_failed = false;
1087   }
1088 
1089   // accessors
1090   void set_expansion_cause(CMSExpansionCause::Cause v) { _expansion_cause = v;}
1091   CMSExpansionCause::Cause expansion_cause() const { return _expansion_cause; }
1092 
1093  private:
1094   // For parallel young-gen GC support.
1095   CMSParGCThreadState** _par_gc_thread_states;
1096 
1097   // Reason generation was expanded
1098   CMSExpansionCause::Cause _expansion_cause;
1099 
1100   // In support of MinChunkSize being larger than min object size
1101   const double _dilatation_factor;
1102 
1103   enum CollectionTypes {
1104     Concurrent_collection_type          = 0,
1105     MS_foreground_collection_type       = 1,
1106     MSC_foreground_collection_type      = 2,
1107     Unknown_collection_type             = 3
1108   };
1109 
1110   CollectionTypes _debug_collection_type;
1111 
1112   // True if a compacting collection was done.
1113   bool _did_compact;
1114   bool did_compact() { return _did_compact; }
1115 
1116   // Fraction of current occupancy at which to start a CMS collection which
1117   // will collect this generation (at least).
1118   double _initiating_occupancy;
1119 
1120  protected:
1121   // Shrink generation by specified size (returns false if unable to shrink)
1122   void shrink_free_list_by(size_t bytes);
1123 
1124   // Update statistics for GC
1125   virtual void update_gc_stats(Generation* current_generation, bool full);
1126 
1127   // Maximum available space in the generation (including uncommitted)
1128   // space.
1129   size_t max_available() const;
1130 
1131   // getter and initializer for _initiating_occupancy field.
1132   double initiating_occupancy() const { return _initiating_occupancy; }
1133   void   init_initiating_occupancy(intx io, uintx tr);
1134 
1135  public:
1136   ConcurrentMarkSweepGeneration(ReservedSpace rs, size_t initial_byte_size,
1137                                 CardTableRS* ct,
1138                                 bool use_adaptive_freelists,
1139                                 FreeBlockDictionary<FreeChunk>::DictionaryChoice);
1140 
1141   // Accessors
1142   CMSCollector* collector() const { return _collector; }
1143   static void set_collector(CMSCollector* collector) {
1144     assert(_collector == NULL, "already set");
1145     _collector = collector;
1146   }
1147   CompactibleFreeListSpace*  cmsSpace() const { return _cmsSpace;  }
1148 
1149   Mutex* freelistLock() const;
1150 
1151   virtual Generation::Name kind() { return Generation::ConcurrentMarkSweep; }
1152 
1153   void set_did_compact(bool v) { _did_compact = v; }
1154 
1155   bool refs_discovery_is_atomic() const { return false; }
1156   bool refs_discovery_is_mt()     const {
1157     // Note: CMS does MT-discovery during the parallel-remark
1158     // phases. Use ReferenceProcessorMTMutator to make refs
1159     // discovery MT-safe during such phases or other parallel
1160     // discovery phases in the future. This may all go away
1161     // if/when we decide that refs discovery is sufficiently
1162     // rare that the cost of the CAS's involved is in the
1163     // noise. That's a measurement that should be done, and
1164     // the code simplified if that turns out to be the case.
1165     return ConcGCThreads > 1;
1166   }
1167 
1168   // Override
1169   virtual void ref_processor_init();
1170 
1171   // Grow generation by specified size (returns false if unable to grow)
1172   bool grow_by(size_t bytes);
1173   // Grow generation to reserved size.
1174   bool grow_to_reserved();
1175 
1176   void clear_expansion_cause() { _expansion_cause = CMSExpansionCause::_no_expansion; }
1177 
1178   // Space enquiries
1179   size_t capacity() const;
1180   size_t used() const;
1181   size_t free() const;
1182   double occupancy() const { return ((double)used())/((double)capacity()); }
1183   size_t contiguous_available() const;
1184   size_t unsafe_max_alloc_nogc() const;
1185 
1186   // over-rides
1187   MemRegion used_region() const;
1188   MemRegion used_region_at_save_marks() const;
1189 
1190   // Does a "full" (forced) collection invoked on this generation collect
1191   // all younger generations as well? Note that the second conjunct is a
1192   // hack to allow the collection of the younger gen first if the flag is
1193   // set.
1194   virtual bool full_collects_younger_generations() const {
1195     return UseCMSCompactAtFullCollection && !ScavengeBeforeFullGC;
1196   }
1197 
1198   void space_iterate(SpaceClosure* blk, bool usedOnly = false);
1199 
1200   // Support for compaction
1201   CompactibleSpace* first_compaction_space() const;
1202   // Adjust quantities in the generation affected by
1203   // the compaction.
1204   void reset_after_compaction();
1205 
1206   // Allocation support
1207   HeapWord* allocate(size_t size, bool tlab);
1208   HeapWord* have_lock_and_allocate(size_t size, bool tlab);
1209   oop       promote(oop obj, size_t obj_size);
1210   HeapWord* par_allocate(size_t size, bool tlab) {
1211     return allocate(size, tlab);
1212   }
1213 
1214   // Incremental mode triggering.
1215   HeapWord* allocation_limit_reached(Space* space, HeapWord* top,
1216                                      size_t word_size);
1217 
1218   // Used by CMSStats to track direct allocation.  The value is sampled and
1219   // reset after each young gen collection.
1220   size_t direct_allocated_words() const { return _direct_allocated_words; }
1221   void reset_direct_allocated_words()   { _direct_allocated_words = 0; }
1222 
1223   // Overrides for parallel promotion.
1224   virtual oop par_promote(int thread_num,
1225                           oop obj, markOop m, size_t word_sz);
1226   // This one should not be called for CMS.
1227   virtual void par_promote_alloc_undo(int thread_num,
1228                                       HeapWord* obj, size_t word_sz);
1229   virtual void par_promote_alloc_done(int thread_num);
1230   virtual void par_oop_since_save_marks_iterate_done(int thread_num);
1231 
1232   virtual bool promotion_attempt_is_safe(size_t promotion_in_bytes) const;
1233 
1234   // Inform this (non-young) generation that a promotion failure was
1235   // encountered during a collection of a younger generation that
1236   // promotes into this generation.
1237   virtual void promotion_failure_occurred();
1238 
1239   bool should_collect(bool full, size_t size, bool tlab);
1240   virtual bool should_concurrent_collect() const;
1241   virtual bool is_too_full() const;
1242   void collect(bool   full,
1243                bool   clear_all_soft_refs,
1244                size_t size,
1245                bool   tlab);
1246 
1247   HeapWord* expand_and_allocate(size_t word_size,
1248                                 bool tlab,
1249                                 bool parallel = false);
1250 
1251   // GC prologue and epilogue
1252   void gc_prologue(bool full);
1253   void gc_prologue_work(bool full, bool registerClosure,
1254                         ModUnionClosure* modUnionClosure);
1255   void gc_epilogue(bool full);
1256   void gc_epilogue_work(bool full);
1257 
1258   // Time since last GC of this generation
1259   jlong time_of_last_gc(jlong now) {
1260     return collector()->time_of_last_gc(now);
1261   }
1262   void update_time_of_last_gc(jlong now) {
1263     collector()-> update_time_of_last_gc(now);
1264   }
1265 
1266   // Allocation failure
1267   void expand(size_t bytes, size_t expand_bytes,
1268     CMSExpansionCause::Cause cause);
1269   virtual bool expand(size_t bytes, size_t expand_bytes);
1270   void shrink(size_t bytes);
1271   void shrink_by(size_t bytes);
1272   HeapWord* expand_and_par_lab_allocate(CMSParGCThreadState* ps, size_t word_sz);
1273   bool expand_and_ensure_spooling_space(PromotionInfo* promo);
1274 
1275   // Iteration support and related enquiries
1276   void save_marks();
1277   bool no_allocs_since_save_marks();
1278   void younger_refs_iterate(OopsInGenClosure* cl);
1279 
1280   // Iteration support specific to CMS generations
1281   void save_sweep_limit();
1282 
1283   // More iteration support
1284   virtual void oop_iterate(ExtendedOopClosure* cl);
1285   virtual void safe_object_iterate(ObjectClosure* cl);
1286   virtual void object_iterate(ObjectClosure* cl);
1287 
1288   // Need to declare the full complement of closures, whether we'll
1289   // override them or not, or get message from the compiler:
1290   //   oop_since_save_marks_iterate_nv hides virtual function...
1291   #define CMS_SINCE_SAVE_MARKS_DECL(OopClosureType, nv_suffix) \
1292     void oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl);
1293   ALL_SINCE_SAVE_MARKS_CLOSURES(CMS_SINCE_SAVE_MARKS_DECL)
1294 
1295   // Smart allocation  XXX -- move to CFLSpace?
1296   void setNearLargestChunk();
1297   bool isNearLargestChunk(HeapWord* addr);
1298 
1299   // Get the chunk at the end of the space.  Delegates to
1300   // the space.
1301   FreeChunk* find_chunk_at_end();
1302 
1303   void post_compact();
1304 
1305   // Debugging
1306   void prepare_for_verify();
1307   void verify();
1308   void print_statistics()               PRODUCT_RETURN;
1309 
1310   // Performance Counters support
1311   virtual void update_counters();
1312   virtual void update_counters(size_t used);
1313   void initialize_performance_counters();
1314   CollectorCounters* counters()  { return collector()->counters(); }
1315 
1316   // Support for parallel remark of survivor space
1317   void* get_data_recorder(int thr_num) {
1318     //Delegate to collector
1319     return collector()->get_data_recorder(thr_num);
1320   }
1321   void sample_eden_chunk() {
1322     //Delegate to collector
1323     return collector()->sample_eden_chunk();
1324   }
1325 
1326   // Printing
1327   const char* name() const;
1328   virtual const char* short_name() const { return "CMS"; }
1329   void        print() const;
1330   void printOccupancy(const char* s);
1331   bool must_be_youngest() const { return false; }
1332   bool must_be_oldest()   const { return true; }
1333 
1334   // Resize the generation after a compacting GC.  The
1335   // generation can be treated as a contiguous space
1336   // after the compaction.
1337   virtual void compute_new_size();
1338   // Resize the generation after a non-compacting
1339   // collection.
1340   void compute_new_size_free_list();
1341 
1342   CollectionTypes debug_collection_type() { return _debug_collection_type; }
1343   void rotate_debug_collection_type();
1344 };
1345 
1346 //
1347 // Closures of various sorts used by CMS to accomplish its work
1348 //
1349 
1350 // This closure is used to do concurrent marking from the roots
1351 // following the first checkpoint.
1352 class MarkFromRootsClosure: public BitMapClosure {
1353   CMSCollector*  _collector;
1354   MemRegion      _span;
1355   CMSBitMap*     _bitMap;
1356   CMSBitMap*     _mut;
1357   CMSMarkStack*  _markStack;
1358   bool           _yield;
1359   int            _skipBits;
1360   HeapWord*      _finger;
1361   HeapWord*      _threshold;
1362   DEBUG_ONLY(bool _verifying;)
1363 
1364  public:
1365   MarkFromRootsClosure(CMSCollector* collector, MemRegion span,
1366                        CMSBitMap* bitMap,
1367                        CMSMarkStack*  markStack,
1368                        bool should_yield, bool verifying = false);
1369   bool do_bit(size_t offset);
1370   void reset(HeapWord* addr);
1371   inline void do_yield_check();
1372 
1373  private:
1374   void scanOopsInOop(HeapWord* ptr);
1375   void do_yield_work();
1376 };
1377 
1378 // This closure is used to do concurrent multi-threaded
1379 // marking from the roots following the first checkpoint.
1380 // XXX This should really be a subclass of The serial version
1381 // above, but i have not had the time to refactor things cleanly.
1382 class Par_MarkFromRootsClosure: public BitMapClosure {
1383   CMSCollector*  _collector;
1384   MemRegion      _whole_span;
1385   MemRegion      _span;
1386   CMSBitMap*     _bit_map;
1387   CMSBitMap*     _mut;
1388   OopTaskQueue*  _work_queue;
1389   CMSMarkStack*  _overflow_stack;
1390   bool           _yield;
1391   int            _skip_bits;
1392   HeapWord*      _finger;
1393   HeapWord*      _threshold;
1394   CMSConcMarkingTask* _task;
1395  public:
1396   Par_MarkFromRootsClosure(CMSConcMarkingTask* task, CMSCollector* collector,
1397                        MemRegion span,
1398                        CMSBitMap* bit_map,
1399                        OopTaskQueue* work_queue,
1400                        CMSMarkStack*  overflow_stack,
1401                        bool should_yield);
1402   bool do_bit(size_t offset);
1403   inline void do_yield_check();
1404 
1405  private:
1406   void scan_oops_in_oop(HeapWord* ptr);
1407   void do_yield_work();
1408   bool get_work_from_overflow_stack();
1409 };
1410 
1411 // The following closures are used to do certain kinds of verification of
1412 // CMS marking.
1413 class PushAndMarkVerifyClosure: public MetadataAwareOopClosure {
1414   CMSCollector*    _collector;
1415   MemRegion        _span;
1416   CMSBitMap*       _verification_bm;
1417   CMSBitMap*       _cms_bm;
1418   CMSMarkStack*    _mark_stack;
1419  protected:
1420   void do_oop(oop p);
1421   template <class T> inline void do_oop_work(T *p) {
1422     oop obj = oopDesc::load_decode_heap_oop(p);
1423     do_oop(obj);
1424   }
1425  public:
1426   PushAndMarkVerifyClosure(CMSCollector* cms_collector,
1427                            MemRegion span,
1428                            CMSBitMap* verification_bm,
1429                            CMSBitMap* cms_bm,
1430                            CMSMarkStack*  mark_stack);
1431   void do_oop(oop* p);
1432   void do_oop(narrowOop* p);
1433 
1434   // Deal with a stack overflow condition
1435   void handle_stack_overflow(HeapWord* lost);
1436 };
1437 
1438 class MarkFromRootsVerifyClosure: public BitMapClosure {
1439   CMSCollector*  _collector;
1440   MemRegion      _span;
1441   CMSBitMap*     _verification_bm;
1442   CMSBitMap*     _cms_bm;
1443   CMSMarkStack*  _mark_stack;
1444   HeapWord*      _finger;
1445   PushAndMarkVerifyClosure _pam_verify_closure;
1446  public:
1447   MarkFromRootsVerifyClosure(CMSCollector* collector, MemRegion span,
1448                              CMSBitMap* verification_bm,
1449                              CMSBitMap* cms_bm,
1450                              CMSMarkStack*  mark_stack);
1451   bool do_bit(size_t offset);
1452   void reset(HeapWord* addr);
1453 };
1454 
1455 
1456 // This closure is used to check that a certain set of bits is
1457 // "empty" (i.e. the bit vector doesn't have any 1-bits).
1458 class FalseBitMapClosure: public BitMapClosure {
1459  public:
1460   bool do_bit(size_t offset) {
1461     guarantee(false, "Should not have a 1 bit");
1462     return true;
1463   }
1464 };
1465 
1466 // A version of ObjectClosure with "memory" (see _previous_address below)
1467 class UpwardsObjectClosure: public BoolObjectClosure {
1468   HeapWord* _previous_address;
1469  public:
1470   UpwardsObjectClosure() : _previous_address(NULL) { }
1471   void set_previous(HeapWord* addr) { _previous_address = addr; }
1472   HeapWord* previous()              { return _previous_address; }
1473   // A return value of "true" can be used by the caller to decide
1474   // if this object's end should *NOT* be recorded in
1475   // _previous_address above.
1476   virtual bool do_object_bm(oop obj, MemRegion mr) = 0;
1477 };
1478 
1479 // This closure is used during the second checkpointing phase
1480 // to rescan the marked objects on the dirty cards in the mod
1481 // union table and the card table proper. It's invoked via
1482 // MarkFromDirtyCardsClosure below. It uses either
1483 // [Par_]MarkRefsIntoAndScanClosure (Par_ in the parallel case)
1484 // declared in genOopClosures.hpp to accomplish some of its work.
1485 // In the parallel case the bitMap is shared, so access to
1486 // it needs to be suitably synchronized for updates by embedded
1487 // closures that update it; however, this closure itself only
1488 // reads the bit_map and because it is idempotent, is immune to
1489 // reading stale values.
1490 class ScanMarkedObjectsAgainClosure: public UpwardsObjectClosure {
1491   #ifdef ASSERT
1492     CMSCollector*          _collector;
1493     MemRegion              _span;
1494     union {
1495       CMSMarkStack*        _mark_stack;
1496       OopTaskQueue*        _work_queue;
1497     };
1498   #endif // ASSERT
1499   bool                       _parallel;
1500   CMSBitMap*                 _bit_map;
1501   union {
1502     MarkRefsIntoAndScanClosure*     _scan_closure;
1503     Par_MarkRefsIntoAndScanClosure* _par_scan_closure;
1504   };
1505 
1506  public:
1507   ScanMarkedObjectsAgainClosure(CMSCollector* collector,
1508                                 MemRegion span,
1509                                 ReferenceProcessor* rp,
1510                                 CMSBitMap* bit_map,
1511                                 CMSMarkStack*  mark_stack,
1512                                 MarkRefsIntoAndScanClosure* cl):
1513     #ifdef ASSERT
1514       _collector(collector),
1515       _span(span),
1516       _mark_stack(mark_stack),
1517     #endif // ASSERT
1518     _parallel(false),
1519     _bit_map(bit_map),
1520     _scan_closure(cl) { }
1521 
1522   ScanMarkedObjectsAgainClosure(CMSCollector* collector,
1523                                 MemRegion span,
1524                                 ReferenceProcessor* rp,
1525                                 CMSBitMap* bit_map,
1526                                 OopTaskQueue* work_queue,
1527                                 Par_MarkRefsIntoAndScanClosure* cl):
1528     #ifdef ASSERT
1529       _collector(collector),
1530       _span(span),
1531       _work_queue(work_queue),
1532     #endif // ASSERT
1533     _parallel(true),
1534     _bit_map(bit_map),
1535     _par_scan_closure(cl) { }
1536 
1537   bool do_object_b(oop obj) {
1538     guarantee(false, "Call do_object_b(oop, MemRegion) form instead");
1539     return false;
1540   }
1541   bool do_object_bm(oop p, MemRegion mr);
1542 };
1543 
1544 // This closure is used during the second checkpointing phase
1545 // to rescan the marked objects on the dirty cards in the mod
1546 // union table and the card table proper. It invokes
1547 // ScanMarkedObjectsAgainClosure above to accomplish much of its work.
1548 // In the parallel case, the bit map is shared and requires
1549 // synchronized access.
1550 class MarkFromDirtyCardsClosure: public MemRegionClosure {
1551   CompactibleFreeListSpace*      _space;
1552   ScanMarkedObjectsAgainClosure  _scan_cl;
1553   size_t                         _num_dirty_cards;
1554 
1555  public:
1556   MarkFromDirtyCardsClosure(CMSCollector* collector,
1557                             MemRegion span,
1558                             CompactibleFreeListSpace* space,
1559                             CMSBitMap* bit_map,
1560                             CMSMarkStack* mark_stack,
1561                             MarkRefsIntoAndScanClosure* cl):
1562     _space(space),
1563     _num_dirty_cards(0),
1564     _scan_cl(collector, span, collector->ref_processor(), bit_map,
1565                  mark_stack, cl) { }
1566 
1567   MarkFromDirtyCardsClosure(CMSCollector* collector,
1568                             MemRegion span,
1569                             CompactibleFreeListSpace* space,
1570                             CMSBitMap* bit_map,
1571                             OopTaskQueue* work_queue,
1572                             Par_MarkRefsIntoAndScanClosure* cl):
1573     _space(space),
1574     _num_dirty_cards(0),
1575     _scan_cl(collector, span, collector->ref_processor(), bit_map,
1576              work_queue, cl) { }
1577 
1578   void do_MemRegion(MemRegion mr);
1579   void set_space(CompactibleFreeListSpace* space) { _space = space; }
1580   size_t num_dirty_cards() { return _num_dirty_cards; }
1581 };
1582 
1583 // This closure is used in the non-product build to check
1584 // that there are no MemRegions with a certain property.
1585 class FalseMemRegionClosure: public MemRegionClosure {
1586   void do_MemRegion(MemRegion mr) {
1587     guarantee(!mr.is_empty(), "Shouldn't be empty");
1588     guarantee(false, "Should never be here");
1589   }
1590 };
1591 
1592 // This closure is used during the precleaning phase
1593 // to "carefully" rescan marked objects on dirty cards.
1594 // It uses MarkRefsIntoAndScanClosure declared in genOopClosures.hpp
1595 // to accomplish some of its work.
1596 class ScanMarkedObjectsAgainCarefullyClosure: public ObjectClosureCareful {
1597   CMSCollector*                  _collector;
1598   MemRegion                      _span;
1599   bool                           _yield;
1600   Mutex*                         _freelistLock;
1601   CMSBitMap*                     _bitMap;
1602   CMSMarkStack*                  _markStack;
1603   MarkRefsIntoAndScanClosure*    _scanningClosure;
1604 
1605  public:
1606   ScanMarkedObjectsAgainCarefullyClosure(CMSCollector* collector,
1607                                          MemRegion     span,
1608                                          CMSBitMap* bitMap,
1609                                          CMSMarkStack*  markStack,
1610                                          MarkRefsIntoAndScanClosure* cl,
1611                                          bool should_yield):
1612     _collector(collector),
1613     _span(span),
1614     _yield(should_yield),
1615     _bitMap(bitMap),
1616     _markStack(markStack),
1617     _scanningClosure(cl) {
1618   }
1619 
1620   void do_object(oop p) {
1621     guarantee(false, "call do_object_careful instead");
1622   }
1623 
1624   size_t      do_object_careful(oop p) {
1625     guarantee(false, "Unexpected caller");
1626     return 0;
1627   }
1628 
1629   size_t      do_object_careful_m(oop p, MemRegion mr);
1630 
1631   void setFreelistLock(Mutex* m) {
1632     _freelistLock = m;
1633     _scanningClosure->set_freelistLock(m);
1634   }
1635 
1636  private:
1637   inline bool do_yield_check();
1638 
1639   void do_yield_work();
1640 };
1641 
1642 class SurvivorSpacePrecleanClosure: public ObjectClosureCareful {
1643   CMSCollector*                  _collector;
1644   MemRegion                      _span;
1645   bool                           _yield;
1646   CMSBitMap*                     _bit_map;
1647   CMSMarkStack*                  _mark_stack;
1648   PushAndMarkClosure*            _scanning_closure;
1649   unsigned int                   _before_count;
1650 
1651  public:
1652   SurvivorSpacePrecleanClosure(CMSCollector* collector,
1653                                MemRegion     span,
1654                                CMSBitMap*    bit_map,
1655                                CMSMarkStack* mark_stack,
1656                                PushAndMarkClosure* cl,
1657                                unsigned int  before_count,
1658                                bool          should_yield):
1659     _collector(collector),
1660     _span(span),
1661     _yield(should_yield),
1662     _bit_map(bit_map),
1663     _mark_stack(mark_stack),
1664     _scanning_closure(cl),
1665     _before_count(before_count)
1666   { }
1667 
1668   void do_object(oop p) {
1669     guarantee(false, "call do_object_careful instead");
1670   }
1671 
1672   size_t      do_object_careful(oop p);
1673 
1674   size_t      do_object_careful_m(oop p, MemRegion mr) {
1675     guarantee(false, "Unexpected caller");
1676     return 0;
1677   }
1678 
1679  private:
1680   inline void do_yield_check();
1681   void do_yield_work();
1682 };
1683 
1684 // This closure is used to accomplish the sweeping work
1685 // after the second checkpoint but before the concurrent reset
1686 // phase.
1687 //
1688 // Terminology
1689 //   left hand chunk (LHC) - block of one or more chunks currently being
1690 //     coalesced.  The LHC is available for coalescing with a new chunk.
1691 //   right hand chunk (RHC) - block that is currently being swept that is
1692 //     free or garbage that can be coalesced with the LHC.
1693 // _inFreeRange is true if there is currently a LHC
1694 // _lastFreeRangeCoalesced is true if the LHC consists of more than one chunk.
1695 // _freeRangeInFreeLists is true if the LHC is in the free lists.
1696 // _freeFinger is the address of the current LHC
1697 class SweepClosure: public BlkClosureCareful {
1698   CMSCollector*                  _collector;  // collector doing the work
1699   ConcurrentMarkSweepGeneration* _g;    // Generation being swept
1700   CompactibleFreeListSpace*      _sp;   // Space being swept
1701   HeapWord*                      _limit;// the address at or above which the sweep should stop
1702                                         // because we do not expect newly garbage blocks
1703                                         // eligible for sweeping past that address.
1704   Mutex*                         _freelistLock; // Free list lock (in space)
1705   CMSBitMap*                     _bitMap;       // Marking bit map (in
1706                                                 // generation)
1707   bool                           _inFreeRange;  // Indicates if we are in the
1708                                                 // midst of a free run
1709   bool                           _freeRangeInFreeLists;
1710                                         // Often, we have just found
1711                                         // a free chunk and started
1712                                         // a new free range; we do not
1713                                         // eagerly remove this chunk from
1714                                         // the free lists unless there is
1715                                         // a possibility of coalescing.
1716                                         // When true, this flag indicates
1717                                         // that the _freeFinger below
1718                                         // points to a potentially free chunk
1719                                         // that may still be in the free lists
1720   bool                           _lastFreeRangeCoalesced;
1721                                         // free range contains chunks
1722                                         // coalesced
1723   bool                           _yield;
1724                                         // Whether sweeping should be
1725                                         // done with yields. For instance
1726                                         // when done by the foreground
1727                                         // collector we shouldn't yield.
1728   HeapWord*                      _freeFinger;   // When _inFreeRange is set, the
1729                                                 // pointer to the "left hand
1730                                                 // chunk"
1731   size_t                         _freeRangeSize;
1732                                         // When _inFreeRange is set, this
1733                                         // indicates the accumulated size
1734                                         // of the "left hand chunk"
1735   NOT_PRODUCT(
1736     size_t                       _numObjectsFreed;
1737     size_t                       _numWordsFreed;
1738     size_t                       _numObjectsLive;
1739     size_t                       _numWordsLive;
1740     size_t                       _numObjectsAlreadyFree;
1741     size_t                       _numWordsAlreadyFree;
1742     FreeChunk*                   _last_fc;
1743   )
1744  private:
1745   // Code that is common to a free chunk or garbage when
1746   // encountered during sweeping.
1747   void do_post_free_or_garbage_chunk(FreeChunk *fc, size_t chunkSize);
1748   // Process a free chunk during sweeping.
1749   void do_already_free_chunk(FreeChunk *fc);
1750   // Work method called when processing an already free or a
1751   // freshly garbage chunk to do a lookahead and possibly a
1752   // preemptive flush if crossing over _limit.
1753   void lookahead_and_flush(FreeChunk* fc, size_t chunkSize);
1754   // Process a garbage chunk during sweeping.
1755   size_t do_garbage_chunk(FreeChunk *fc);
1756   // Process a live chunk during sweeping.
1757   size_t do_live_chunk(FreeChunk* fc);
1758 
1759   // Accessors.
1760   HeapWord* freeFinger() const          { return _freeFinger; }
1761   void set_freeFinger(HeapWord* v)      { _freeFinger = v; }
1762   bool inFreeRange()    const           { return _inFreeRange; }
1763   void set_inFreeRange(bool v)          { _inFreeRange = v; }
1764   bool lastFreeRangeCoalesced() const    { return _lastFreeRangeCoalesced; }
1765   void set_lastFreeRangeCoalesced(bool v) { _lastFreeRangeCoalesced = v; }
1766   bool freeRangeInFreeLists() const     { return _freeRangeInFreeLists; }
1767   void set_freeRangeInFreeLists(bool v) { _freeRangeInFreeLists = v; }
1768 
1769   // Initialize a free range.
1770   void initialize_free_range(HeapWord* freeFinger, bool freeRangeInFreeLists);
1771   // Return this chunk to the free lists.
1772   void flush_cur_free_chunk(HeapWord* chunk, size_t size);
1773 
1774   // Check if we should yield and do so when necessary.
1775   inline void do_yield_check(HeapWord* addr);
1776 
1777   // Yield
1778   void do_yield_work(HeapWord* addr);
1779 
1780   // Debugging/Printing
1781   void print_free_block_coalesced(FreeChunk* fc) const;
1782 
1783  public:
1784   SweepClosure(CMSCollector* collector, ConcurrentMarkSweepGeneration* g,
1785                CMSBitMap* bitMap, bool should_yield);
1786   ~SweepClosure() PRODUCT_RETURN;
1787 
1788   size_t       do_blk_careful(HeapWord* addr);
1789   void         print() const { print_on(tty); }
1790   void         print_on(outputStream *st) const;
1791 };
1792 
1793 // Closures related to weak references processing
1794 
1795 // During CMS' weak reference processing, this is a
1796 // work-routine/closure used to complete transitive
1797 // marking of objects as live after a certain point
1798 // in which an initial set has been completely accumulated.
1799 // This closure is currently used both during the final
1800 // remark stop-world phase, as well as during the concurrent
1801 // precleaning of the discovered reference lists.
1802 class CMSDrainMarkingStackClosure: public VoidClosure {
1803   CMSCollector*        _collector;
1804   MemRegion            _span;
1805   CMSMarkStack*        _mark_stack;
1806   CMSBitMap*           _bit_map;
1807   CMSKeepAliveClosure* _keep_alive;
1808   bool                 _concurrent_precleaning;
1809  public:
1810   CMSDrainMarkingStackClosure(CMSCollector* collector, MemRegion span,
1811                       CMSBitMap* bit_map, CMSMarkStack* mark_stack,
1812                       CMSKeepAliveClosure* keep_alive,
1813                       bool cpc):
1814     _collector(collector),
1815     _span(span),
1816     _bit_map(bit_map),
1817     _mark_stack(mark_stack),
1818     _keep_alive(keep_alive),
1819     _concurrent_precleaning(cpc) {
1820     assert(_concurrent_precleaning == _keep_alive->concurrent_precleaning(),
1821            "Mismatch");
1822   }
1823 
1824   void do_void();
1825 };
1826 
1827 // A parallel version of CMSDrainMarkingStackClosure above.
1828 class CMSParDrainMarkingStackClosure: public VoidClosure {
1829   CMSCollector*           _collector;
1830   MemRegion               _span;
1831   OopTaskQueue*           _work_queue;
1832   CMSBitMap*              _bit_map;
1833   CMSInnerParMarkAndPushClosure _mark_and_push;
1834 
1835  public:
1836   CMSParDrainMarkingStackClosure(CMSCollector* collector,
1837                                  MemRegion span, CMSBitMap* bit_map,
1838                                  OopTaskQueue* work_queue):
1839     _collector(collector),
1840     _span(span),
1841     _bit_map(bit_map),
1842     _work_queue(work_queue),
1843     _mark_and_push(collector, span, bit_map, work_queue) { }
1844 
1845  public:
1846   void trim_queue(uint max);
1847   void do_void();
1848 };
1849 
1850 // Allow yielding or short-circuiting of reference list
1851 // precleaning work.
1852 class CMSPrecleanRefsYieldClosure: public YieldClosure {
1853   CMSCollector* _collector;
1854   void do_yield_work();
1855  public:
1856   CMSPrecleanRefsYieldClosure(CMSCollector* collector):
1857     _collector(collector) {}
1858   virtual bool should_return();
1859 };
1860 
1861 
1862 // Convenience class that locks free list locks for given CMS collector
1863 class FreelistLocker: public StackObj {
1864  private:
1865   CMSCollector* _collector;
1866  public:
1867   FreelistLocker(CMSCollector* collector):
1868     _collector(collector) {
1869     _collector->getFreelistLocks();
1870   }
1871 
1872   ~FreelistLocker() {
1873     _collector->releaseFreelistLocks();
1874   }
1875 };
1876 
1877 // Mark all dead objects in a given space.
1878 class MarkDeadObjectsClosure: public BlkClosure {
1879   const CMSCollector*             _collector;
1880   const CompactibleFreeListSpace* _sp;
1881   CMSBitMap*                      _live_bit_map;
1882   CMSBitMap*                      _dead_bit_map;
1883 public:
1884   MarkDeadObjectsClosure(const CMSCollector* collector,
1885                          const CompactibleFreeListSpace* sp,
1886                          CMSBitMap *live_bit_map,
1887                          CMSBitMap *dead_bit_map) :
1888     _collector(collector),
1889     _sp(sp),
1890     _live_bit_map(live_bit_map),
1891     _dead_bit_map(dead_bit_map) {}
1892   size_t do_blk(HeapWord* addr);
1893 };
1894 
1895 class TraceCMSMemoryManagerStats : public TraceMemoryManagerStats {
1896 
1897  public:
1898   TraceCMSMemoryManagerStats(CMSCollector::CollectorState phase, GCCause::Cause cause);
1899 };
1900 
1901 
1902 #endif // SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPGENERATION_HPP