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   // Set to true, if another GC thread already had a promotion failure.
1121   bool _has_promotion_failed;
1122 
1123   // Support for CMSFastPromotionFailure
1124   bool has_promotion_failed()   { return _has_promotion_failed; }
1125   void set_promotion_failed()   { _has_promotion_failed = true; }
1126   void reset_promotion_failed() { _has_promotion_failed = false; }
1127 
1128  protected:
1129   // Shrink generation by specified size (returns false if unable to shrink)
1130   void shrink_free_list_by(size_t bytes);
1131 
1132   // Update statistics for GC
1133   virtual void update_gc_stats(int level, bool full);
1134 
1135   // Maximum available space in the generation (including uncommitted)
1136   // space.
1137   size_t max_available() const;
1138 
1139   // getter and initializer for _initiating_occupancy field.
1140   double initiating_occupancy() const { return _initiating_occupancy; }
1141   void   init_initiating_occupancy(intx io, uintx tr);
1142 
1143  public:
1144   ConcurrentMarkSweepGeneration(ReservedSpace rs, size_t initial_byte_size,
1145                                 int level, CardTableRS* ct,
1146                                 bool use_adaptive_freelists,
1147                                 FreeBlockDictionary<FreeChunk>::DictionaryChoice);
1148 
1149   // Accessors
1150   CMSCollector* collector() const { return _collector; }
1151   static void set_collector(CMSCollector* collector) {
1152     assert(_collector == NULL, "already set");
1153     _collector = collector;
1154   }
1155   CompactibleFreeListSpace*  cmsSpace() const { return _cmsSpace;  }
1156 
1157   Mutex* freelistLock() const;
1158 
1159   virtual Generation::Name kind() { return Generation::ConcurrentMarkSweep; }
1160 
1161   void set_did_compact(bool v) { _did_compact = v; }
1162 
1163   bool refs_discovery_is_atomic() const { return false; }
1164   bool refs_discovery_is_mt()     const {
1165     // Note: CMS does MT-discovery during the parallel-remark
1166     // phases. Use ReferenceProcessorMTMutator to make refs
1167     // discovery MT-safe during such phases or other parallel
1168     // discovery phases in the future. This may all go away
1169     // if/when we decide that refs discovery is sufficiently
1170     // rare that the cost of the CAS's involved is in the
1171     // noise. That's a measurement that should be done, and
1172     // the code simplified if that turns out to be the case.
1173     return ConcGCThreads > 1;
1174   }
1175 
1176   // Override
1177   virtual void ref_processor_init();
1178 
1179   // Grow generation by specified size (returns false if unable to grow)
1180   bool grow_by(size_t bytes);
1181   // Grow generation to reserved size.
1182   bool grow_to_reserved();
1183 
1184   void clear_expansion_cause() { _expansion_cause = CMSExpansionCause::_no_expansion; }
1185 
1186   // Space enquiries
1187   size_t capacity() const;
1188   size_t used() const;
1189   size_t free() const;
1190   double occupancy() const { return ((double)used())/((double)capacity()); }
1191   size_t contiguous_available() const;
1192   size_t unsafe_max_alloc_nogc() const;
1193 
1194   // over-rides
1195   MemRegion used_region() const;
1196   MemRegion used_region_at_save_marks() const;
1197 
1198   // Does a "full" (forced) collection invoked on this generation collect
1199   // all younger generations as well? Note that the second conjunct is a
1200   // hack to allow the collection of the younger gen first if the flag is
1201   // set.
1202   virtual bool full_collects_younger_generations() const {
1203     return UseCMSCompactAtFullCollection && !ScavengeBeforeFullGC;
1204   }
1205 
1206   void space_iterate(SpaceClosure* blk, bool usedOnly = false);
1207 
1208   // Support for compaction
1209   CompactibleSpace* first_compaction_space() const;
1210   // Adjust quantities in the generation affected by
1211   // the compaction.
1212   void reset_after_compaction();
1213 
1214   // Allocation support
1215   HeapWord* allocate(size_t size, bool tlab);
1216   HeapWord* have_lock_and_allocate(size_t size, bool tlab);
1217   oop       promote(oop obj, size_t obj_size);
1218   HeapWord* par_allocate(size_t size, bool tlab) {
1219     return allocate(size, tlab);
1220   }
1221 
1222   // Incremental mode triggering.
1223   HeapWord* allocation_limit_reached(Space* space, HeapWord* top,
1224                                      size_t word_size);
1225 
1226   // Used by CMSStats to track direct allocation.  The value is sampled and
1227   // reset after each young gen collection.
1228   size_t direct_allocated_words() const { return _direct_allocated_words; }
1229   void reset_direct_allocated_words()   { _direct_allocated_words = 0; }
1230 
1231   // Overrides for parallel promotion.
1232   virtual oop par_promote(int thread_num,
1233                           oop obj, markOop m, size_t word_sz);
1234   // This one should not be called for CMS.
1235   virtual void par_promote_alloc_undo(int thread_num,
1236                                       HeapWord* obj, size_t word_sz);
1237   virtual void par_promote_alloc_done(int thread_num);
1238   virtual void par_oop_since_save_marks_iterate_done(int thread_num);
1239 
1240   virtual bool promotion_attempt_is_safe(size_t promotion_in_bytes) const;
1241 
1242   // Inform this (non-young) generation that a promotion failure was
1243   // encountered during a collection of a younger generation that
1244   // promotes into this generation.
1245   virtual void promotion_failure_occurred();
1246 
1247   bool should_collect(bool full, size_t size, bool tlab);
1248   virtual bool should_concurrent_collect() const;
1249   virtual bool is_too_full() const;
1250   void collect(bool   full,
1251                bool   clear_all_soft_refs,
1252                size_t size,
1253                bool   tlab);
1254 
1255   HeapWord* expand_and_allocate(size_t word_size,
1256                                 bool tlab,
1257                                 bool parallel = false);
1258 
1259   // GC prologue and epilogue
1260   void gc_prologue(bool full);
1261   void gc_prologue_work(bool full, bool registerClosure,
1262                         ModUnionClosure* modUnionClosure);
1263   void gc_epilogue(bool full);
1264   void gc_epilogue_work(bool full);
1265 
1266   // Time since last GC of this generation
1267   jlong time_of_last_gc(jlong now) {
1268     return collector()->time_of_last_gc(now);
1269   }
1270   void update_time_of_last_gc(jlong now) {
1271     collector()-> update_time_of_last_gc(now);
1272   }
1273 
1274   // Allocation failure
1275   void expand(size_t bytes, size_t expand_bytes,
1276     CMSExpansionCause::Cause cause);
1277   virtual bool expand(size_t bytes, size_t expand_bytes);
1278   void shrink(size_t bytes);
1279   void shrink_by(size_t bytes);
1280   HeapWord* expand_and_par_lab_allocate(CMSParGCThreadState* ps, size_t word_sz);
1281   bool expand_and_ensure_spooling_space(PromotionInfo* promo);
1282 
1283   // Iteration support and related enquiries
1284   void save_marks();
1285   bool no_allocs_since_save_marks();
1286   void younger_refs_iterate(OopsInGenClosure* cl);
1287 
1288   // Iteration support specific to CMS generations
1289   void save_sweep_limit();
1290 
1291   // More iteration support
1292   virtual void oop_iterate(ExtendedOopClosure* cl);
1293   virtual void safe_object_iterate(ObjectClosure* cl);
1294   virtual void object_iterate(ObjectClosure* cl);
1295 
1296   // Need to declare the full complement of closures, whether we'll
1297   // override them or not, or get message from the compiler:
1298   //   oop_since_save_marks_iterate_nv hides virtual function...
1299   #define CMS_SINCE_SAVE_MARKS_DECL(OopClosureType, nv_suffix) \
1300     void oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl);
1301   ALL_SINCE_SAVE_MARKS_CLOSURES(CMS_SINCE_SAVE_MARKS_DECL)
1302 
1303   // Smart allocation  XXX -- move to CFLSpace?
1304   void setNearLargestChunk();
1305   bool isNearLargestChunk(HeapWord* addr);
1306 
1307   // Get the chunk at the end of the space.  Delegates to
1308   // the space.
1309   FreeChunk* find_chunk_at_end();
1310 
1311   void post_compact();
1312 
1313   // Debugging
1314   void prepare_for_verify();
1315   void verify();
1316   void print_statistics()               PRODUCT_RETURN;
1317 
1318   // Performance Counters support
1319   virtual void update_counters();
1320   virtual void update_counters(size_t used);
1321   void initialize_performance_counters();
1322   CollectorCounters* counters()  { return collector()->counters(); }
1323 
1324   // Support for parallel remark of survivor space
1325   void* get_data_recorder(int thr_num) {
1326     //Delegate to collector
1327     return collector()->get_data_recorder(thr_num);
1328   }
1329   void sample_eden_chunk() {
1330     //Delegate to collector
1331     return collector()->sample_eden_chunk();
1332   }
1333 
1334   // Printing
1335   const char* name() const;
1336   virtual const char* short_name() const { return "CMS"; }
1337   void        print() const;
1338   void printOccupancy(const char* s);
1339   bool must_be_youngest() const { return false; }
1340   bool must_be_oldest()   const { return true; }
1341 
1342   // Resize the generation after a compacting GC.  The
1343   // generation can be treated as a contiguous space
1344   // after the compaction.
1345   virtual void compute_new_size();
1346   // Resize the generation after a non-compacting
1347   // collection.
1348   void compute_new_size_free_list();
1349 
1350   CollectionTypes debug_collection_type() { return _debug_collection_type; }
1351   void rotate_debug_collection_type();
1352 };
1353 
1354 //
1355 // Closures of various sorts used by CMS to accomplish its work
1356 //
1357 
1358 // This closure is used to do concurrent marking from the roots
1359 // following the first checkpoint.
1360 class MarkFromRootsClosure: public BitMapClosure {
1361   CMSCollector*  _collector;
1362   MemRegion      _span;
1363   CMSBitMap*     _bitMap;
1364   CMSBitMap*     _mut;
1365   CMSMarkStack*  _markStack;
1366   bool           _yield;
1367   int            _skipBits;
1368   HeapWord*      _finger;
1369   HeapWord*      _threshold;
1370   DEBUG_ONLY(bool _verifying;)
1371 
1372  public:
1373   MarkFromRootsClosure(CMSCollector* collector, MemRegion span,
1374                        CMSBitMap* bitMap,
1375                        CMSMarkStack*  markStack,
1376                        bool should_yield, bool verifying = false);
1377   bool do_bit(size_t offset);
1378   void reset(HeapWord* addr);
1379   inline void do_yield_check();
1380 
1381  private:
1382   void scanOopsInOop(HeapWord* ptr);
1383   void do_yield_work();
1384 };
1385 
1386 // This closure is used to do concurrent multi-threaded
1387 // marking from the roots following the first checkpoint.
1388 // XXX This should really be a subclass of The serial version
1389 // above, but i have not had the time to refactor things cleanly.
1390 class Par_MarkFromRootsClosure: public BitMapClosure {
1391   CMSCollector*  _collector;
1392   MemRegion      _whole_span;
1393   MemRegion      _span;
1394   CMSBitMap*     _bit_map;
1395   CMSBitMap*     _mut;
1396   OopTaskQueue*  _work_queue;
1397   CMSMarkStack*  _overflow_stack;
1398   bool           _yield;
1399   int            _skip_bits;
1400   HeapWord*      _finger;
1401   HeapWord*      _threshold;
1402   CMSConcMarkingTask* _task;
1403  public:
1404   Par_MarkFromRootsClosure(CMSConcMarkingTask* task, CMSCollector* collector,
1405                        MemRegion span,
1406                        CMSBitMap* bit_map,
1407                        OopTaskQueue* work_queue,
1408                        CMSMarkStack*  overflow_stack,
1409                        bool should_yield);
1410   bool do_bit(size_t offset);
1411   inline void do_yield_check();
1412 
1413  private:
1414   void scan_oops_in_oop(HeapWord* ptr);
1415   void do_yield_work();
1416   bool get_work_from_overflow_stack();
1417 };
1418 
1419 // The following closures are used to do certain kinds of verification of
1420 // CMS marking.
1421 class PushAndMarkVerifyClosure: public MetadataAwareOopClosure {
1422   CMSCollector*    _collector;
1423   MemRegion        _span;
1424   CMSBitMap*       _verification_bm;
1425   CMSBitMap*       _cms_bm;
1426   CMSMarkStack*    _mark_stack;
1427  protected:
1428   void do_oop(oop p);
1429   template <class T> inline void do_oop_work(T *p) {
1430     oop obj = oopDesc::load_decode_heap_oop(p);
1431     do_oop(obj);
1432   }
1433  public:
1434   PushAndMarkVerifyClosure(CMSCollector* cms_collector,
1435                            MemRegion span,
1436                            CMSBitMap* verification_bm,
1437                            CMSBitMap* cms_bm,
1438                            CMSMarkStack*  mark_stack);
1439   void do_oop(oop* p);
1440   void do_oop(narrowOop* p);
1441 
1442   // Deal with a stack overflow condition
1443   void handle_stack_overflow(HeapWord* lost);
1444 };
1445 
1446 class MarkFromRootsVerifyClosure: public BitMapClosure {
1447   CMSCollector*  _collector;
1448   MemRegion      _span;
1449   CMSBitMap*     _verification_bm;
1450   CMSBitMap*     _cms_bm;
1451   CMSMarkStack*  _mark_stack;
1452   HeapWord*      _finger;
1453   PushAndMarkVerifyClosure _pam_verify_closure;
1454  public:
1455   MarkFromRootsVerifyClosure(CMSCollector* collector, MemRegion span,
1456                              CMSBitMap* verification_bm,
1457                              CMSBitMap* cms_bm,
1458                              CMSMarkStack*  mark_stack);
1459   bool do_bit(size_t offset);
1460   void reset(HeapWord* addr);
1461 };
1462 
1463 
1464 // This closure is used to check that a certain set of bits is
1465 // "empty" (i.e. the bit vector doesn't have any 1-bits).
1466 class FalseBitMapClosure: public BitMapClosure {
1467  public:
1468   bool do_bit(size_t offset) {
1469     guarantee(false, "Should not have a 1 bit");
1470     return true;
1471   }
1472 };
1473 
1474 // A version of ObjectClosure with "memory" (see _previous_address below)
1475 class UpwardsObjectClosure: public BoolObjectClosure {
1476   HeapWord* _previous_address;
1477  public:
1478   UpwardsObjectClosure() : _previous_address(NULL) { }
1479   void set_previous(HeapWord* addr) { _previous_address = addr; }
1480   HeapWord* previous()              { return _previous_address; }
1481   // A return value of "true" can be used by the caller to decide
1482   // if this object's end should *NOT* be recorded in
1483   // _previous_address above.
1484   virtual bool do_object_bm(oop obj, MemRegion mr) = 0;
1485 };
1486 
1487 // This closure is used during the second checkpointing phase
1488 // to rescan the marked objects on the dirty cards in the mod
1489 // union table and the card table proper. It's invoked via
1490 // MarkFromDirtyCardsClosure below. It uses either
1491 // [Par_]MarkRefsIntoAndScanClosure (Par_ in the parallel case)
1492 // declared in genOopClosures.hpp to accomplish some of its work.
1493 // In the parallel case the bitMap is shared, so access to
1494 // it needs to be suitably synchronized for updates by embedded
1495 // closures that update it; however, this closure itself only
1496 // reads the bit_map and because it is idempotent, is immune to
1497 // reading stale values.
1498 class ScanMarkedObjectsAgainClosure: public UpwardsObjectClosure {
1499   #ifdef ASSERT
1500     CMSCollector*          _collector;
1501     MemRegion              _span;
1502     union {
1503       CMSMarkStack*        _mark_stack;
1504       OopTaskQueue*        _work_queue;
1505     };
1506   #endif // ASSERT
1507   bool                       _parallel;
1508   CMSBitMap*                 _bit_map;
1509   union {
1510     MarkRefsIntoAndScanClosure*     _scan_closure;
1511     Par_MarkRefsIntoAndScanClosure* _par_scan_closure;
1512   };
1513 
1514  public:
1515   ScanMarkedObjectsAgainClosure(CMSCollector* collector,
1516                                 MemRegion span,
1517                                 ReferenceProcessor* rp,
1518                                 CMSBitMap* bit_map,
1519                                 CMSMarkStack*  mark_stack,
1520                                 MarkRefsIntoAndScanClosure* cl):
1521     #ifdef ASSERT
1522       _collector(collector),
1523       _span(span),
1524       _mark_stack(mark_stack),
1525     #endif // ASSERT
1526     _parallel(false),
1527     _bit_map(bit_map),
1528     _scan_closure(cl) { }
1529 
1530   ScanMarkedObjectsAgainClosure(CMSCollector* collector,
1531                                 MemRegion span,
1532                                 ReferenceProcessor* rp,
1533                                 CMSBitMap* bit_map,
1534                                 OopTaskQueue* work_queue,
1535                                 Par_MarkRefsIntoAndScanClosure* cl):
1536     #ifdef ASSERT
1537       _collector(collector),
1538       _span(span),
1539       _work_queue(work_queue),
1540     #endif // ASSERT
1541     _parallel(true),
1542     _bit_map(bit_map),
1543     _par_scan_closure(cl) { }
1544 
1545   bool do_object_b(oop obj) {
1546     guarantee(false, "Call do_object_b(oop, MemRegion) form instead");
1547     return false;
1548   }
1549   bool do_object_bm(oop p, MemRegion mr);
1550 };
1551 
1552 // This closure is used during the second checkpointing phase
1553 // to rescan the marked objects on the dirty cards in the mod
1554 // union table and the card table proper. It invokes
1555 // ScanMarkedObjectsAgainClosure above to accomplish much of its work.
1556 // In the parallel case, the bit map is shared and requires
1557 // synchronized access.
1558 class MarkFromDirtyCardsClosure: public MemRegionClosure {
1559   CompactibleFreeListSpace*      _space;
1560   ScanMarkedObjectsAgainClosure  _scan_cl;
1561   size_t                         _num_dirty_cards;
1562 
1563  public:
1564   MarkFromDirtyCardsClosure(CMSCollector* collector,
1565                             MemRegion span,
1566                             CompactibleFreeListSpace* space,
1567                             CMSBitMap* bit_map,
1568                             CMSMarkStack* mark_stack,
1569                             MarkRefsIntoAndScanClosure* cl):
1570     _space(space),
1571     _num_dirty_cards(0),
1572     _scan_cl(collector, span, collector->ref_processor(), bit_map,
1573                  mark_stack, cl) { }
1574 
1575   MarkFromDirtyCardsClosure(CMSCollector* collector,
1576                             MemRegion span,
1577                             CompactibleFreeListSpace* space,
1578                             CMSBitMap* bit_map,
1579                             OopTaskQueue* work_queue,
1580                             Par_MarkRefsIntoAndScanClosure* cl):
1581     _space(space),
1582     _num_dirty_cards(0),
1583     _scan_cl(collector, span, collector->ref_processor(), bit_map,
1584              work_queue, cl) { }
1585 
1586   void do_MemRegion(MemRegion mr);
1587   void set_space(CompactibleFreeListSpace* space) { _space = space; }
1588   size_t num_dirty_cards() { return _num_dirty_cards; }
1589 };
1590 
1591 // This closure is used in the non-product build to check
1592 // that there are no MemRegions with a certain property.
1593 class FalseMemRegionClosure: public MemRegionClosure {
1594   void do_MemRegion(MemRegion mr) {
1595     guarantee(!mr.is_empty(), "Shouldn't be empty");
1596     guarantee(false, "Should never be here");
1597   }
1598 };
1599 
1600 // This closure is used during the precleaning phase
1601 // to "carefully" rescan marked objects on dirty cards.
1602 // It uses MarkRefsIntoAndScanClosure declared in genOopClosures.hpp
1603 // to accomplish some of its work.
1604 class ScanMarkedObjectsAgainCarefullyClosure: public ObjectClosureCareful {
1605   CMSCollector*                  _collector;
1606   MemRegion                      _span;
1607   bool                           _yield;
1608   Mutex*                         _freelistLock;
1609   CMSBitMap*                     _bitMap;
1610   CMSMarkStack*                  _markStack;
1611   MarkRefsIntoAndScanClosure*    _scanningClosure;
1612 
1613  public:
1614   ScanMarkedObjectsAgainCarefullyClosure(CMSCollector* collector,
1615                                          MemRegion     span,
1616                                          CMSBitMap* bitMap,
1617                                          CMSMarkStack*  markStack,
1618                                          MarkRefsIntoAndScanClosure* cl,
1619                                          bool should_yield):
1620     _collector(collector),
1621     _span(span),
1622     _yield(should_yield),
1623     _bitMap(bitMap),
1624     _markStack(markStack),
1625     _scanningClosure(cl) {
1626   }
1627 
1628   void do_object(oop p) {
1629     guarantee(false, "call do_object_careful instead");
1630   }
1631 
1632   size_t      do_object_careful(oop p) {
1633     guarantee(false, "Unexpected caller");
1634     return 0;
1635   }
1636 
1637   size_t      do_object_careful_m(oop p, MemRegion mr);
1638 
1639   void setFreelistLock(Mutex* m) {
1640     _freelistLock = m;
1641     _scanningClosure->set_freelistLock(m);
1642   }
1643 
1644  private:
1645   inline bool do_yield_check();
1646 
1647   void do_yield_work();
1648 };
1649 
1650 class SurvivorSpacePrecleanClosure: public ObjectClosureCareful {
1651   CMSCollector*                  _collector;
1652   MemRegion                      _span;
1653   bool                           _yield;
1654   CMSBitMap*                     _bit_map;
1655   CMSMarkStack*                  _mark_stack;
1656   PushAndMarkClosure*            _scanning_closure;
1657   unsigned int                   _before_count;
1658 
1659  public:
1660   SurvivorSpacePrecleanClosure(CMSCollector* collector,
1661                                MemRegion     span,
1662                                CMSBitMap*    bit_map,
1663                                CMSMarkStack* mark_stack,
1664                                PushAndMarkClosure* cl,
1665                                unsigned int  before_count,
1666                                bool          should_yield):
1667     _collector(collector),
1668     _span(span),
1669     _yield(should_yield),
1670     _bit_map(bit_map),
1671     _mark_stack(mark_stack),
1672     _scanning_closure(cl),
1673     _before_count(before_count)
1674   { }
1675 
1676   void do_object(oop p) {
1677     guarantee(false, "call do_object_careful instead");
1678   }
1679 
1680   size_t      do_object_careful(oop p);
1681 
1682   size_t      do_object_careful_m(oop p, MemRegion mr) {
1683     guarantee(false, "Unexpected caller");
1684     return 0;
1685   }
1686 
1687  private:
1688   inline void do_yield_check();
1689   void do_yield_work();
1690 };
1691 
1692 // This closure is used to accomplish the sweeping work
1693 // after the second checkpoint but before the concurrent reset
1694 // phase.
1695 //
1696 // Terminology
1697 //   left hand chunk (LHC) - block of one or more chunks currently being
1698 //     coalesced.  The LHC is available for coalescing with a new chunk.
1699 //   right hand chunk (RHC) - block that is currently being swept that is
1700 //     free or garbage that can be coalesced with the LHC.
1701 // _inFreeRange is true if there is currently a LHC
1702 // _lastFreeRangeCoalesced is true if the LHC consists of more than one chunk.
1703 // _freeRangeInFreeLists is true if the LHC is in the free lists.
1704 // _freeFinger is the address of the current LHC
1705 class SweepClosure: public BlkClosureCareful {
1706   CMSCollector*                  _collector;  // collector doing the work
1707   ConcurrentMarkSweepGeneration* _g;    // Generation being swept
1708   CompactibleFreeListSpace*      _sp;   // Space being swept
1709   HeapWord*                      _limit;// the address at or above which the sweep should stop
1710                                         // because we do not expect newly garbage blocks
1711                                         // eligible for sweeping past that address.
1712   Mutex*                         _freelistLock; // Free list lock (in space)
1713   CMSBitMap*                     _bitMap;       // Marking bit map (in
1714                                                 // generation)
1715   bool                           _inFreeRange;  // Indicates if we are in the
1716                                                 // midst of a free run
1717   bool                           _freeRangeInFreeLists;
1718                                         // Often, we have just found
1719                                         // a free chunk and started
1720                                         // a new free range; we do not
1721                                         // eagerly remove this chunk from
1722                                         // the free lists unless there is
1723                                         // a possibility of coalescing.
1724                                         // When true, this flag indicates
1725                                         // that the _freeFinger below
1726                                         // points to a potentially free chunk
1727                                         // that may still be in the free lists
1728   bool                           _lastFreeRangeCoalesced;
1729                                         // free range contains chunks
1730                                         // coalesced
1731   bool                           _yield;
1732                                         // Whether sweeping should be
1733                                         // done with yields. For instance
1734                                         // when done by the foreground
1735                                         // collector we shouldn't yield.
1736   HeapWord*                      _freeFinger;   // When _inFreeRange is set, the
1737                                                 // pointer to the "left hand
1738                                                 // chunk"
1739   size_t                         _freeRangeSize;
1740                                         // When _inFreeRange is set, this
1741                                         // indicates the accumulated size
1742                                         // of the "left hand chunk"
1743   NOT_PRODUCT(
1744     size_t                       _numObjectsFreed;
1745     size_t                       _numWordsFreed;
1746     size_t                       _numObjectsLive;
1747     size_t                       _numWordsLive;
1748     size_t                       _numObjectsAlreadyFree;
1749     size_t                       _numWordsAlreadyFree;
1750     FreeChunk*                   _last_fc;
1751   )
1752  private:
1753   // Code that is common to a free chunk or garbage when
1754   // encountered during sweeping.
1755   void do_post_free_or_garbage_chunk(FreeChunk *fc, size_t chunkSize);
1756   // Process a free chunk during sweeping.
1757   void do_already_free_chunk(FreeChunk *fc);
1758   // Work method called when processing an already free or a
1759   // freshly garbage chunk to do a lookahead and possibly a
1760   // preemptive flush if crossing over _limit.
1761   void lookahead_and_flush(FreeChunk* fc, size_t chunkSize);
1762   // Process a garbage chunk during sweeping.
1763   size_t do_garbage_chunk(FreeChunk *fc);
1764   // Process a live chunk during sweeping.
1765   size_t do_live_chunk(FreeChunk* fc);
1766 
1767   // Accessors.
1768   HeapWord* freeFinger() const          { return _freeFinger; }
1769   void set_freeFinger(HeapWord* v)      { _freeFinger = v; }
1770   bool inFreeRange()    const           { return _inFreeRange; }
1771   void set_inFreeRange(bool v)          { _inFreeRange = v; }
1772   bool lastFreeRangeCoalesced() const    { return _lastFreeRangeCoalesced; }
1773   void set_lastFreeRangeCoalesced(bool v) { _lastFreeRangeCoalesced = v; }
1774   bool freeRangeInFreeLists() const     { return _freeRangeInFreeLists; }
1775   void set_freeRangeInFreeLists(bool v) { _freeRangeInFreeLists = v; }
1776 
1777   // Initialize a free range.
1778   void initialize_free_range(HeapWord* freeFinger, bool freeRangeInFreeLists);
1779   // Return this chunk to the free lists.
1780   void flush_cur_free_chunk(HeapWord* chunk, size_t size);
1781 
1782   // Check if we should yield and do so when necessary.
1783   inline void do_yield_check(HeapWord* addr);
1784 
1785   // Yield
1786   void do_yield_work(HeapWord* addr);
1787 
1788   // Debugging/Printing
1789   void print_free_block_coalesced(FreeChunk* fc) const;
1790 
1791  public:
1792   SweepClosure(CMSCollector* collector, ConcurrentMarkSweepGeneration* g,
1793                CMSBitMap* bitMap, bool should_yield);
1794   ~SweepClosure() PRODUCT_RETURN;
1795 
1796   size_t       do_blk_careful(HeapWord* addr);
1797   void         print() const { print_on(tty); }
1798   void         print_on(outputStream *st) const;
1799 };
1800 
1801 // Closures related to weak references processing
1802 
1803 // During CMS' weak reference processing, this is a
1804 // work-routine/closure used to complete transitive
1805 // marking of objects as live after a certain point
1806 // in which an initial set has been completely accumulated.
1807 // This closure is currently used both during the final
1808 // remark stop-world phase, as well as during the concurrent
1809 // precleaning of the discovered reference lists.
1810 class CMSDrainMarkingStackClosure: public VoidClosure {
1811   CMSCollector*        _collector;
1812   MemRegion            _span;
1813   CMSMarkStack*        _mark_stack;
1814   CMSBitMap*           _bit_map;
1815   CMSKeepAliveClosure* _keep_alive;
1816   bool                 _concurrent_precleaning;
1817  public:
1818   CMSDrainMarkingStackClosure(CMSCollector* collector, MemRegion span,
1819                       CMSBitMap* bit_map, CMSMarkStack* mark_stack,
1820                       CMSKeepAliveClosure* keep_alive,
1821                       bool cpc):
1822     _collector(collector),
1823     _span(span),
1824     _bit_map(bit_map),
1825     _mark_stack(mark_stack),
1826     _keep_alive(keep_alive),
1827     _concurrent_precleaning(cpc) {
1828     assert(_concurrent_precleaning == _keep_alive->concurrent_precleaning(),
1829            "Mismatch");
1830   }
1831 
1832   void do_void();
1833 };
1834 
1835 // A parallel version of CMSDrainMarkingStackClosure above.
1836 class CMSParDrainMarkingStackClosure: public VoidClosure {
1837   CMSCollector*           _collector;
1838   MemRegion               _span;
1839   OopTaskQueue*           _work_queue;
1840   CMSBitMap*              _bit_map;
1841   CMSInnerParMarkAndPushClosure _mark_and_push;
1842 
1843  public:
1844   CMSParDrainMarkingStackClosure(CMSCollector* collector,
1845                                  MemRegion span, CMSBitMap* bit_map,
1846                                  OopTaskQueue* work_queue):
1847     _collector(collector),
1848     _span(span),
1849     _bit_map(bit_map),
1850     _work_queue(work_queue),
1851     _mark_and_push(collector, span, bit_map, work_queue) { }
1852 
1853  public:
1854   void trim_queue(uint max);
1855   void do_void();
1856 };
1857 
1858 // Allow yielding or short-circuiting of reference list
1859 // precleaning work.
1860 class CMSPrecleanRefsYieldClosure: public YieldClosure {
1861   CMSCollector* _collector;
1862   void do_yield_work();
1863  public:
1864   CMSPrecleanRefsYieldClosure(CMSCollector* collector):
1865     _collector(collector) {}
1866   virtual bool should_return();
1867 };
1868 
1869 
1870 // Convenience class that locks free list locks for given CMS collector
1871 class FreelistLocker: public StackObj {
1872  private:
1873   CMSCollector* _collector;
1874  public:
1875   FreelistLocker(CMSCollector* collector):
1876     _collector(collector) {
1877     _collector->getFreelistLocks();
1878   }
1879 
1880   ~FreelistLocker() {
1881     _collector->releaseFreelistLocks();
1882   }
1883 };
1884 
1885 // Mark all dead objects in a given space.
1886 class MarkDeadObjectsClosure: public BlkClosure {
1887   const CMSCollector*             _collector;
1888   const CompactibleFreeListSpace* _sp;
1889   CMSBitMap*                      _live_bit_map;
1890   CMSBitMap*                      _dead_bit_map;
1891 public:
1892   MarkDeadObjectsClosure(const CMSCollector* collector,
1893                          const CompactibleFreeListSpace* sp,
1894                          CMSBitMap *live_bit_map,
1895                          CMSBitMap *dead_bit_map) :
1896     _collector(collector),
1897     _sp(sp),
1898     _live_bit_map(live_bit_map),
1899     _dead_bit_map(dead_bit_map) {}
1900   size_t do_blk(HeapWord* addr);
1901 };
1902 
1903 class TraceCMSMemoryManagerStats : public TraceMemoryManagerStats {
1904 
1905  public:
1906   TraceCMSMemoryManagerStats(CMSCollector::CollectorState phase, GCCause::Cause cause);
1907 };
1908 
1909 
1910 #endif // SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPGENERATION_HPP
--- EOF ---