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