1 /* 2 * Copyright (c) 2001, 2019, 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_GC_G1_G1CONCURRENTMARK_HPP 26 #define SHARE_GC_G1_G1CONCURRENTMARK_HPP 27 28 #include "gc/g1/g1ConcurrentMarkBitMap.hpp" 29 #include "gc/g1/g1ConcurrentMarkObjArrayProcessor.hpp" 30 #include "gc/g1/g1HeapVerifier.hpp" 31 #include "gc/g1/g1RegionMarkStatsCache.hpp" 32 #include "gc/g1/heapRegionSet.hpp" 33 #include "gc/shared/taskqueue.hpp" 34 #include "gc/shared/verifyOption.hpp" 35 #include "memory/allocation.hpp" 36 #include "utilities/compilerWarnings.hpp" 37 38 class ConcurrentGCTimer; 39 class G1ConcurrentMarkThread; 40 class G1CollectedHeap; 41 class G1CMOopClosure; 42 class G1CMTask; 43 class G1ConcurrentMark; 44 class G1OldTracer; 45 class G1RegionToSpaceMapper; 46 class G1SurvivorRegions; 47 48 PRAGMA_DIAG_PUSH 49 // warning C4522: multiple assignment operators specified 50 PRAGMA_DISABLE_MSVC_WARNING(4522) 51 52 // This is a container class for either an oop or a continuation address for 53 // mark stack entries. Both are pushed onto the mark stack. 54 class G1TaskQueueEntry { 55 private: 56 void* _holder; 57 58 static const uintptr_t ArraySliceBit = 1; 59 60 G1TaskQueueEntry(oop obj) : _holder(obj) { 61 assert(_holder != NULL, "Not allowed to set NULL task queue element"); 62 } 63 G1TaskQueueEntry(HeapWord* addr) : _holder((void*)((uintptr_t)addr | ArraySliceBit)) { } 64 public: 65 G1TaskQueueEntry(const G1TaskQueueEntry& other) { _holder = other._holder; } 66 G1TaskQueueEntry() : _holder(NULL) { } 67 68 static G1TaskQueueEntry from_slice(HeapWord* what) { return G1TaskQueueEntry(what); } 69 static G1TaskQueueEntry from_oop(oop obj) { return G1TaskQueueEntry(obj); } 70 71 G1TaskQueueEntry& operator=(const G1TaskQueueEntry& t) { 72 _holder = t._holder; 73 return *this; 74 } 75 76 volatile G1TaskQueueEntry& operator=(const volatile G1TaskQueueEntry& t) volatile { 77 _holder = t._holder; 78 return *this; 79 } 80 81 oop obj() const { 82 assert(!is_array_slice(), "Trying to read array slice " PTR_FORMAT " as oop", p2i(_holder)); 83 return (oop)_holder; 84 } 85 86 HeapWord* slice() const { 87 assert(is_array_slice(), "Trying to read oop " PTR_FORMAT " as array slice", p2i(_holder)); 88 return (HeapWord*)((uintptr_t)_holder & ~ArraySliceBit); 89 } 90 91 bool is_oop() const { return !is_array_slice(); } 92 bool is_array_slice() const { return ((uintptr_t)_holder & ArraySliceBit) != 0; } 93 bool is_null() const { return _holder == NULL; } 94 }; 95 96 PRAGMA_DIAG_POP 97 98 typedef GenericTaskQueue<G1TaskQueueEntry, mtGC> G1CMTaskQueue; 99 typedef GenericTaskQueueSet<G1CMTaskQueue, mtGC> G1CMTaskQueueSet; 100 101 // Closure used by CM during concurrent reference discovery 102 // and reference processing (during remarking) to determine 103 // if a particular object is alive. It is primarily used 104 // to determine if referents of discovered reference objects 105 // are alive. An instance is also embedded into the 106 // reference processor as the _is_alive_non_header field 107 class G1CMIsAliveClosure : public BoolObjectClosure { 108 G1CollectedHeap* _g1h; 109 public: 110 G1CMIsAliveClosure(G1CollectedHeap* g1h) : _g1h(g1h) { } 111 bool do_object_b(oop obj); 112 }; 113 114 class G1CMSubjectToDiscoveryClosure : public BoolObjectClosure { 115 G1CollectedHeap* _g1h; 116 public: 117 G1CMSubjectToDiscoveryClosure(G1CollectedHeap* g1h) : _g1h(g1h) { } 118 bool do_object_b(oop obj); 119 }; 120 121 // Represents the overflow mark stack used by concurrent marking. 122 // 123 // Stores oops in a huge buffer in virtual memory that is always fully committed. 124 // Resizing may only happen during a STW pause when the stack is empty. 125 // 126 // Memory is allocated on a "chunk" basis, i.e. a set of oops. For this, the mark 127 // stack memory is split into evenly sized chunks of oops. Users can only 128 // add or remove entries on that basis. 129 // Chunks are filled in increasing address order. Not completely filled chunks 130 // have a NULL element as a terminating element. 131 // 132 // Every chunk has a header containing a single pointer element used for memory 133 // management. This wastes some space, but is negligible (< .1% with current sizing). 134 // 135 // Memory management is done using a mix of tracking a high water-mark indicating 136 // that all chunks at a lower address are valid chunks, and a singly linked free 137 // list connecting all empty chunks. 138 class G1CMMarkStack { 139 public: 140 // Number of TaskQueueEntries that can fit in a single chunk. 141 static const size_t EntriesPerChunk = 1024 - 1 /* One reference for the next pointer */; 142 private: 143 struct TaskQueueEntryChunk { 144 TaskQueueEntryChunk* next; 145 G1TaskQueueEntry data[EntriesPerChunk]; 146 }; 147 148 size_t _max_chunk_capacity; // Maximum number of TaskQueueEntryChunk elements on the stack. 149 150 TaskQueueEntryChunk* _base; // Bottom address of allocated memory area. 151 size_t _chunk_capacity; // Current maximum number of TaskQueueEntryChunk elements. 152 153 char _pad0[DEFAULT_CACHE_LINE_SIZE]; 154 TaskQueueEntryChunk* volatile _free_list; // Linked list of free chunks that can be allocated by users. 155 char _pad1[DEFAULT_CACHE_LINE_SIZE - sizeof(TaskQueueEntryChunk*)]; 156 TaskQueueEntryChunk* volatile _chunk_list; // List of chunks currently containing data. 157 volatile size_t _chunks_in_chunk_list; 158 char _pad2[DEFAULT_CACHE_LINE_SIZE - sizeof(TaskQueueEntryChunk*) - sizeof(size_t)]; 159 160 volatile size_t _hwm; // High water mark within the reserved space. 161 char _pad4[DEFAULT_CACHE_LINE_SIZE - sizeof(size_t)]; 162 163 // Allocate a new chunk from the reserved memory, using the high water mark. Returns 164 // NULL if out of memory. 165 TaskQueueEntryChunk* allocate_new_chunk(); 166 167 // Atomically add the given chunk to the list. 168 void add_chunk_to_list(TaskQueueEntryChunk* volatile* list, TaskQueueEntryChunk* elem); 169 // Atomically remove and return a chunk from the given list. Returns NULL if the 170 // list is empty. 171 TaskQueueEntryChunk* remove_chunk_from_list(TaskQueueEntryChunk* volatile* list); 172 173 void add_chunk_to_chunk_list(TaskQueueEntryChunk* elem); 174 void add_chunk_to_free_list(TaskQueueEntryChunk* elem); 175 176 TaskQueueEntryChunk* remove_chunk_from_chunk_list(); 177 TaskQueueEntryChunk* remove_chunk_from_free_list(); 178 179 // Resizes the mark stack to the given new capacity. Releases any previous 180 // memory if successful. 181 bool resize(size_t new_capacity); 182 183 public: 184 G1CMMarkStack(); 185 ~G1CMMarkStack(); 186 187 // Alignment and minimum capacity of this mark stack in number of oops. 188 static size_t capacity_alignment(); 189 190 // Allocate and initialize the mark stack with the given number of oops. 191 bool initialize(size_t initial_capacity, size_t max_capacity); 192 193 // Pushes the given buffer containing at most EntriesPerChunk elements on the mark 194 // stack. If less than EntriesPerChunk elements are to be pushed, the array must 195 // be terminated with a NULL. 196 // Returns whether the buffer contents were successfully pushed to the global mark 197 // stack. 198 bool par_push_chunk(G1TaskQueueEntry* buffer); 199 200 // Pops a chunk from this mark stack, copying them into the given buffer. This 201 // chunk may contain up to EntriesPerChunk elements. If there are less, the last 202 // element in the array is a NULL pointer. 203 bool par_pop_chunk(G1TaskQueueEntry* buffer); 204 205 // Return whether the chunk list is empty. Racy due to unsynchronized access to 206 // _chunk_list. 207 bool is_empty() const { return _chunk_list == NULL; } 208 209 size_t capacity() const { return _chunk_capacity; } 210 211 // Expand the stack, typically in response to an overflow condition 212 void expand(); 213 214 // Return the approximate number of oops on this mark stack. Racy due to 215 // unsynchronized access to _chunks_in_chunk_list. 216 size_t size() const { return _chunks_in_chunk_list * EntriesPerChunk; } 217 218 void set_empty(); 219 220 // Apply Fn to every oop on the mark stack. The mark stack must not 221 // be modified while iterating. 222 template<typename Fn> void iterate(Fn fn) const PRODUCT_RETURN; 223 }; 224 225 // Root Regions are regions that contain objects from nTAMS to top. These are roots 226 // for marking, i.e. their referenced objects must be kept alive to maintain the 227 // SATB invariant. 228 // We could scan and mark them through during the initial-mark pause, but for 229 // pause time reasons we move this work to the concurrent phase. 230 // We need to complete this procedure before the next GC because it might determine 231 // that some of these "root objects" are dead, potentially dropping some required 232 // references. 233 // Root regions comprise of the complete contents of survivor regions, and any 234 // objects copied into old gen during GC. 235 class G1CMRootRegions { 236 HeapRegion** _root_regions; 237 size_t const _max_regions; 238 239 volatile size_t _num_root_regions; // Actual number of root regions. 240 241 volatile size_t _claimed_root_regions; // Number of root regions currently claimed. 242 243 volatile bool _scan_in_progress; 244 volatile bool _should_abort; 245 246 void notify_scan_done(); 247 248 public: 249 G1CMRootRegions(uint const max_regions); 250 ~G1CMRootRegions(); 251 252 // Reset the data structure to allow addition of new root regions. 253 void reset(); 254 255 void add(HeapRegion* hr); 256 257 // Reset the claiming / scanning of the root regions. 258 void prepare_for_scan(); 259 260 // Forces get_next() to return NULL so that the iteration aborts early. 261 void abort() { _should_abort = true; } 262 263 // Return true if the CM thread are actively scanning root regions, 264 // false otherwise. 265 bool scan_in_progress() { return _scan_in_progress; } 266 267 // Claim the next root region to scan atomically, or return NULL if 268 // all have been claimed. 269 HeapRegion* claim_next(); 270 271 // The number of root regions to scan. 272 uint num_root_regions() const; 273 274 void cancel_scan(); 275 276 // Flag that we're done with root region scanning and notify anyone 277 // who's waiting on it. If aborted is false, assume that all regions 278 // have been claimed. 279 void scan_finished(); 280 281 // If CM threads are still scanning root regions, wait until they 282 // are done. Return true if we had to wait, false otherwise. 283 bool wait_until_scan_finished(); 284 }; 285 286 // This class manages data structures and methods for doing liveness analysis in 287 // G1's concurrent cycle. 288 class G1ConcurrentMark : public CHeapObj<mtGC> { 289 friend class G1ConcurrentMarkThread; 290 friend class G1CMRefProcTaskProxy; 291 friend class G1CMRefProcTaskExecutor; 292 friend class G1CMKeepAliveAndDrainClosure; 293 friend class G1CMDrainMarkingStackClosure; 294 friend class G1CMBitMapClosure; 295 friend class G1CMConcurrentMarkingTask; 296 friend class G1CMRemarkTask; 297 friend class G1CMTask; 298 299 G1ConcurrentMarkThread* _cm_thread; // The thread doing the work 300 G1CollectedHeap* _g1h; // The heap 301 bool _completed_initialization; // Set to true when initialization is complete 302 303 // Concurrent marking support structures 304 G1CMBitMap _mark_bitmap_1; 305 G1CMBitMap _mark_bitmap_2; 306 G1CMBitMap* _prev_mark_bitmap; // Completed mark bitmap 307 G1CMBitMap* _next_mark_bitmap; // Under-construction mark bitmap 308 309 // Heap bounds 310 MemRegion const _heap; 311 312 // Root region tracking and claiming 313 G1CMRootRegions _root_regions; 314 315 // For grey objects 316 G1CMMarkStack _global_mark_stack; // Grey objects behind global finger 317 HeapWord* volatile _finger; // The global finger, region aligned, 318 // always pointing to the end of the 319 // last claimed region 320 321 uint _worker_id_offset; 322 uint _max_num_tasks; // Maximum number of marking tasks 323 uint _num_active_tasks; // Number of tasks currently active 324 G1CMTask** _tasks; // Task queue array (max_worker_id length) 325 326 G1CMTaskQueueSet* _task_queues; // Task queue set 327 TaskTerminator _terminator; // For termination 328 329 // Two sync barriers that are used to synchronize tasks when an 330 // overflow occurs. The algorithm is the following. All tasks enter 331 // the first one to ensure that they have all stopped manipulating 332 // the global data structures. After they exit it, they re-initialize 333 // their data structures and task 0 re-initializes the global data 334 // structures. Then, they enter the second sync barrier. This 335 // ensure, that no task starts doing work before all data 336 // structures (local and global) have been re-initialized. When they 337 // exit it, they are free to start working again. 338 WorkGangBarrierSync _first_overflow_barrier_sync; 339 WorkGangBarrierSync _second_overflow_barrier_sync; 340 341 // This is set by any task, when an overflow on the global data 342 // structures is detected 343 volatile bool _has_overflown; 344 // True: marking is concurrent, false: we're in remark 345 volatile bool _concurrent; 346 // Set at the end of a Full GC so that marking aborts 347 volatile bool _has_aborted; 348 349 // Used when remark aborts due to an overflow to indicate that 350 // another concurrent marking phase should start 351 volatile bool _restart_for_overflow; 352 353 ConcurrentGCTimer* _gc_timer_cm; 354 355 G1OldTracer* _gc_tracer_cm; 356 357 // Timing statistics. All of them are in ms 358 NumberSeq _init_times; 359 NumberSeq _remark_times; 360 NumberSeq _remark_mark_times; 361 NumberSeq _remark_weak_ref_times; 362 NumberSeq _cleanup_times; 363 double _total_cleanup_time; 364 365 double* _accum_task_vtime; // Accumulated task vtime 366 367 WorkGang* _concurrent_workers; 368 uint _num_concurrent_workers; // The number of marking worker threads we're using 369 uint _max_concurrent_workers; // Maximum number of marking worker threads 370 371 void verify_during_pause(G1HeapVerifier::G1VerifyType type, VerifyOption vo, const char* caller); 372 373 void finalize_marking(); 374 375 void weak_refs_work_parallel_part(BoolObjectClosure* is_alive, bool purged_classes); 376 void weak_refs_work(bool clear_all_soft_refs); 377 378 void report_object_count(bool mark_completed); 379 380 void swap_mark_bitmaps(); 381 382 void reclaim_empty_regions(); 383 384 // After reclaiming empty regions, update heap sizes. 385 void compute_new_sizes(); 386 387 // Clear statistics gathered during the concurrent cycle for the given region after 388 // it has been reclaimed. 389 void clear_statistics(HeapRegion* r); 390 391 // Resets the global marking data structures, as well as the 392 // task local ones; should be called during initial mark. 393 void reset(); 394 395 // Resets all the marking data structures. Called when we have to restart 396 // marking or when marking completes (via set_non_marking_state below). 397 void reset_marking_for_restart(); 398 399 // We do this after we're done with marking so that the marking data 400 // structures are initialized to a sensible and predictable state. 401 void reset_at_marking_complete(); 402 403 // Called to indicate how many threads are currently active. 404 void set_concurrency(uint active_tasks); 405 406 // Should be called to indicate which phase we're in (concurrent 407 // mark or remark) and how many threads are currently active. 408 void set_concurrency_and_phase(uint active_tasks, bool concurrent); 409 410 // Prints all gathered CM-related statistics 411 void print_stats(); 412 413 HeapWord* finger() { return _finger; } 414 bool concurrent() { return _concurrent; } 415 uint active_tasks() { return _num_active_tasks; } 416 ParallelTaskTerminator* terminator() const { return _terminator.terminator(); } 417 418 // Claims the next available region to be scanned by a marking 419 // task/thread. It might return NULL if the next region is empty or 420 // we have run out of regions. In the latter case, out_of_regions() 421 // determines whether we've really run out of regions or the task 422 // should call claim_region() again. This might seem a bit 423 // awkward. Originally, the code was written so that claim_region() 424 // either successfully returned with a non-empty region or there 425 // were no more regions to be claimed. The problem with this was 426 // that, in certain circumstances, it iterated over large chunks of 427 // the heap finding only empty regions and, while it was working, it 428 // was preventing the calling task to call its regular clock 429 // method. So, this way, each task will spend very little time in 430 // claim_region() and is allowed to call the regular clock method 431 // frequently. 432 HeapRegion* claim_region(uint worker_id); 433 434 // Determines whether we've run out of regions to scan. Note that 435 // the finger can point past the heap end in case the heap was expanded 436 // to satisfy an allocation without doing a GC. This is fine, because all 437 // objects in those regions will be considered live anyway because of 438 // SATB guarantees (i.e. their TAMS will be equal to bottom). 439 bool out_of_regions() { return _finger >= _heap.end(); } 440 441 // Returns the task with the given id 442 G1CMTask* task(uint id) { 443 // During initial mark we use the parallel gc threads to do some work, so 444 // we can only compare against _max_num_tasks. 445 assert(id < _max_num_tasks, "Task id %u not within bounds up to %u", id, _max_num_tasks); 446 return _tasks[id]; 447 } 448 449 // Access / manipulation of the overflow flag which is set to 450 // indicate that the global stack has overflown 451 bool has_overflown() { return _has_overflown; } 452 void set_has_overflown() { _has_overflown = true; } 453 void clear_has_overflown() { _has_overflown = false; } 454 bool restart_for_overflow() { return _restart_for_overflow; } 455 456 // Methods to enter the two overflow sync barriers 457 void enter_first_sync_barrier(uint worker_id); 458 void enter_second_sync_barrier(uint worker_id); 459 460 // Clear the given bitmap in parallel using the given WorkGang. If may_yield is 461 // true, periodically insert checks to see if this method should exit prematurely. 462 void clear_bitmap(G1CMBitMap* bitmap, WorkGang* workers, bool may_yield); 463 464 // Region statistics gathered during marking. 465 G1RegionMarkStats* _region_mark_stats; 466 // Top pointer for each region at the start of the rebuild remembered set process 467 // for regions which remembered sets need to be rebuilt. A NULL for a given region 468 // means that this region does not be scanned during the rebuilding remembered 469 // set phase at all. 470 HeapWord* volatile* _top_at_rebuild_starts; 471 public: 472 void add_to_liveness(uint worker_id, oop const obj, size_t size); 473 // Liveness of the given region as determined by concurrent marking, i.e. the amount of 474 // live words between bottom and nTAMS. 475 size_t liveness(uint region) const { return _region_mark_stats[region]._live_words; } 476 477 // Sets the internal top_at_region_start for the given region to current top of the region. 478 inline void update_top_at_rebuild_start(HeapRegion* r); 479 // TARS for the given region during remembered set rebuilding. 480 inline HeapWord* top_at_rebuild_start(uint region) const; 481 482 // Clear statistics gathered during the concurrent cycle for the given region after 483 // it has been reclaimed. 484 void clear_statistics_in_region(uint region_idx); 485 // Notification for eagerly reclaimed regions to clean up. 486 void humongous_object_eagerly_reclaimed(HeapRegion* r); 487 // Manipulation of the global mark stack. 488 // The push and pop operations are used by tasks for transfers 489 // between task-local queues and the global mark stack. 490 bool mark_stack_push(G1TaskQueueEntry* arr) { 491 if (!_global_mark_stack.par_push_chunk(arr)) { 492 set_has_overflown(); 493 return false; 494 } 495 return true; 496 } 497 bool mark_stack_pop(G1TaskQueueEntry* arr) { 498 return _global_mark_stack.par_pop_chunk(arr); 499 } 500 size_t mark_stack_size() const { return _global_mark_stack.size(); } 501 size_t partial_mark_stack_size_target() const { return _global_mark_stack.capacity() / 3; } 502 bool mark_stack_empty() const { return _global_mark_stack.is_empty(); } 503 504 G1CMRootRegions* root_regions() { return &_root_regions; } 505 506 void concurrent_cycle_start(); 507 // Abandon current marking iteration due to a Full GC. 508 void concurrent_cycle_abort(); 509 void concurrent_cycle_end(); 510 511 void update_accum_task_vtime(int i, double vtime) { 512 _accum_task_vtime[i] += vtime; 513 } 514 515 double all_task_accum_vtime() { 516 double ret = 0.0; 517 for (uint i = 0; i < _max_num_tasks; ++i) 518 ret += _accum_task_vtime[i]; 519 return ret; 520 } 521 522 // Attempts to steal an object from the task queues of other tasks 523 bool try_stealing(uint worker_id, G1TaskQueueEntry& task_entry); 524 525 G1ConcurrentMark(G1CollectedHeap* g1h, 526 G1RegionToSpaceMapper* prev_bitmap_storage, 527 G1RegionToSpaceMapper* next_bitmap_storage); 528 ~G1ConcurrentMark(); 529 530 G1ConcurrentMarkThread* cm_thread() { return _cm_thread; } 531 532 const G1CMBitMap* const prev_mark_bitmap() const { return _prev_mark_bitmap; } 533 G1CMBitMap* next_mark_bitmap() const { return _next_mark_bitmap; } 534 535 // Calculates the number of concurrent GC threads to be used in the marking phase. 536 uint calc_active_marking_workers(); 537 538 // Moves all per-task cached data into global state. 539 void flush_all_task_caches(); 540 // Prepare internal data structures for the next mark cycle. This includes clearing 541 // the next mark bitmap and some internal data structures. This method is intended 542 // to be called concurrently to the mutator. It will yield to safepoint requests. 543 void cleanup_for_next_mark(); 544 545 // Clear the previous marking bitmap during safepoint. 546 void clear_prev_bitmap(WorkGang* workers); 547 548 // These two methods do the work that needs to be done at the start and end of the 549 // initial mark pause. 550 void pre_initial_mark(); 551 void post_initial_mark(); 552 553 // Scan all the root regions and mark everything reachable from 554 // them. 555 void scan_root_regions(); 556 557 // Scan a single root region from nTAMS to top and mark everything reachable from it. 558 void scan_root_region(HeapRegion* hr, uint worker_id); 559 560 // Do concurrent phase of marking, to a tentative transitive closure. 561 void mark_from_roots(); 562 563 // Do concurrent preclean work. 564 void preclean(); 565 566 void remark(); 567 568 void cleanup(); 569 // Mark in the previous bitmap. Caution: the prev bitmap is usually read-only, so use 570 // this carefully. 571 inline void mark_in_prev_bitmap(oop p); 572 573 // Clears marks for all objects in the given range, for the prev or 574 // next bitmaps. Caution: the previous bitmap is usually 575 // read-only, so use this carefully! 576 void clear_range_in_prev_bitmap(MemRegion mr); 577 578 inline bool is_marked_in_prev_bitmap(oop p) const; 579 580 // Verify that there are no collection set oops on the stacks (taskqueues / 581 // global mark stack) and fingers (global / per-task). 582 // If marking is not in progress, it's a no-op. 583 void verify_no_collection_set_oops() PRODUCT_RETURN; 584 585 inline bool do_yield_check(); 586 587 bool has_aborted() { return _has_aborted; } 588 589 void print_summary_info(); 590 591 void print_worker_threads_on(outputStream* st) const; 592 void threads_do(ThreadClosure* tc) const; 593 594 void print_on_error(outputStream* st) const; 595 596 // Mark the given object on the next bitmap if it is below nTAMS. 597 inline bool mark_in_next_bitmap(uint worker_id, HeapRegion* const hr, oop const obj); 598 inline bool mark_in_next_bitmap(uint worker_id, oop const obj); 599 600 inline bool is_marked_in_next_bitmap(oop p) const; 601 602 // Returns true if initialization was successfully completed. 603 bool completed_initialization() const { 604 return _completed_initialization; 605 } 606 607 ConcurrentGCTimer* gc_timer_cm() const { return _gc_timer_cm; } 608 G1OldTracer* gc_tracer_cm() const { return _gc_tracer_cm; } 609 610 private: 611 // Rebuilds the remembered sets for chosen regions in parallel and concurrently to the application. 612 void rebuild_rem_set_concurrently(); 613 }; 614 615 // A class representing a marking task. 616 class G1CMTask : public TerminatorTerminator { 617 private: 618 enum PrivateConstants { 619 // The regular clock call is called once the scanned words reaches 620 // this limit 621 words_scanned_period = 12*1024, 622 // The regular clock call is called once the number of visited 623 // references reaches this limit 624 refs_reached_period = 1024, 625 // Initial value for the hash seed, used in the work stealing code 626 init_hash_seed = 17 627 }; 628 629 // Number of entries in the per-task stats entry. This seems enough to have a very 630 // low cache miss rate. 631 static const uint RegionMarkStatsCacheSize = 1024; 632 633 G1CMObjArrayProcessor _objArray_processor; 634 635 uint _worker_id; 636 G1CollectedHeap* _g1h; 637 G1ConcurrentMark* _cm; 638 G1CMBitMap* _next_mark_bitmap; 639 // the task queue of this task 640 G1CMTaskQueue* _task_queue; 641 642 G1RegionMarkStatsCache _mark_stats_cache; 643 // Number of calls to this task 644 uint _calls; 645 646 // When the virtual timer reaches this time, the marking step should exit 647 double _time_target_ms; 648 // Start time of the current marking step 649 double _start_time_ms; 650 651 // Oop closure used for iterations over oops 652 G1CMOopClosure* _cm_oop_closure; 653 654 // Region this task is scanning, NULL if we're not scanning any 655 HeapRegion* _curr_region; 656 // Local finger of this task, NULL if we're not scanning a region 657 HeapWord* _finger; 658 // Limit of the region this task is scanning, NULL if we're not scanning one 659 HeapWord* _region_limit; 660 661 // Number of words this task has scanned 662 size_t _words_scanned; 663 // When _words_scanned reaches this limit, the regular clock is 664 // called. Notice that this might be decreased under certain 665 // circumstances (i.e. when we believe that we did an expensive 666 // operation). 667 size_t _words_scanned_limit; 668 // Initial value of _words_scanned_limit (i.e. what it was 669 // before it was decreased). 670 size_t _real_words_scanned_limit; 671 672 // Number of references this task has visited 673 size_t _refs_reached; 674 // When _refs_reached reaches this limit, the regular clock is 675 // called. Notice this this might be decreased under certain 676 // circumstances (i.e. when we believe that we did an expensive 677 // operation). 678 size_t _refs_reached_limit; 679 // Initial value of _refs_reached_limit (i.e. what it was before 680 // it was decreased). 681 size_t _real_refs_reached_limit; 682 683 // If true, then the task has aborted for some reason 684 bool _has_aborted; 685 // Set when the task aborts because it has met its time quota 686 bool _has_timed_out; 687 // True when we're draining SATB buffers; this avoids the task 688 // aborting due to SATB buffers being available (as we're already 689 // dealing with them) 690 bool _draining_satb_buffers; 691 692 // Number sequence of past step times 693 NumberSeq _step_times_ms; 694 // Elapsed time of this task 695 double _elapsed_time_ms; 696 // Termination time of this task 697 double _termination_time_ms; 698 // When this task got into the termination protocol 699 double _termination_start_time_ms; 700 701 TruncatedSeq _marking_step_diffs_ms; 702 703 // Updates the local fields after this task has claimed 704 // a new region to scan 705 void setup_for_region(HeapRegion* hr); 706 // Makes the limit of the region up-to-date 707 void update_region_limit(); 708 709 // Called when either the words scanned or the refs visited limit 710 // has been reached 711 void reached_limit(); 712 // Recalculates the words scanned and refs visited limits 713 void recalculate_limits(); 714 // Decreases the words scanned and refs visited limits when we reach 715 // an expensive operation 716 void decrease_limits(); 717 // Checks whether the words scanned or refs visited reached their 718 // respective limit and calls reached_limit() if they have 719 void check_limits() { 720 if (_words_scanned >= _words_scanned_limit || 721 _refs_reached >= _refs_reached_limit) { 722 reached_limit(); 723 } 724 } 725 // Supposed to be called regularly during a marking step as 726 // it checks a bunch of conditions that might cause the marking step 727 // to abort 728 // Return true if the marking step should continue. Otherwise, return false to abort 729 bool regular_clock_call(); 730 731 // Set abort flag if regular_clock_call() check fails 732 inline void abort_marking_if_regular_check_fail(); 733 734 // Test whether obj might have already been passed over by the 735 // mark bitmap scan, and so needs to be pushed onto the mark stack. 736 bool is_below_finger(oop obj, HeapWord* global_finger) const; 737 738 template<bool scan> void process_grey_task_entry(G1TaskQueueEntry task_entry); 739 public: 740 // Apply the closure on the given area of the objArray. Return the number of words 741 // scanned. 742 inline size_t scan_objArray(objArrayOop obj, MemRegion mr); 743 // Resets the task; should be called right at the beginning of a marking phase. 744 void reset(G1CMBitMap* next_mark_bitmap); 745 // Clears all the fields that correspond to a claimed region. 746 void clear_region_fields(); 747 748 // The main method of this class which performs a marking step 749 // trying not to exceed the given duration. However, it might exit 750 // prematurely, according to some conditions (i.e. SATB buffers are 751 // available for processing). 752 void do_marking_step(double target_ms, 753 bool do_termination, 754 bool is_serial); 755 756 // These two calls start and stop the timer 757 void record_start_time() { 758 _elapsed_time_ms = os::elapsedTime() * 1000.0; 759 } 760 void record_end_time() { 761 _elapsed_time_ms = os::elapsedTime() * 1000.0 - _elapsed_time_ms; 762 } 763 764 // Returns the worker ID associated with this task. 765 uint worker_id() { return _worker_id; } 766 767 // From TerminatorTerminator. It determines whether this task should 768 // exit the termination protocol after it's entered it. 769 virtual bool should_exit_termination(); 770 771 // Resets the local region fields after a task has finished scanning a 772 // region; or when they have become stale as a result of the region 773 // being evacuated. 774 void giveup_current_region(); 775 776 HeapWord* finger() { return _finger; } 777 778 bool has_aborted() { return _has_aborted; } 779 void set_has_aborted() { _has_aborted = true; } 780 void clear_has_aborted() { _has_aborted = false; } 781 782 void set_cm_oop_closure(G1CMOopClosure* cm_oop_closure); 783 784 // Increment the number of references this task has visited. 785 void increment_refs_reached() { ++_refs_reached; } 786 787 // Grey the object by marking it. If not already marked, push it on 788 // the local queue if below the finger. obj is required to be below its region's NTAMS. 789 // Returns whether there has been a mark to the bitmap. 790 inline bool make_reference_grey(oop obj); 791 792 // Grey the object (by calling make_grey_reference) if required, 793 // e.g. obj is below its containing region's NTAMS. 794 // Precondition: obj is a valid heap object. 795 // Returns true if the reference caused a mark to be set in the next bitmap. 796 template <class T> 797 inline bool deal_with_reference(T* p); 798 799 // Scans an object and visits its children. 800 inline void scan_task_entry(G1TaskQueueEntry task_entry); 801 802 // Pushes an object on the local queue. 803 inline void push(G1TaskQueueEntry task_entry); 804 805 // Move entries to the global stack. 806 void move_entries_to_global_stack(); 807 // Move entries from the global stack, return true if we were successful to do so. 808 bool get_entries_from_global_stack(); 809 810 // Pops and scans objects from the local queue. If partially is 811 // true, then it stops when the queue size is of a given limit. If 812 // partially is false, then it stops when the queue is empty. 813 void drain_local_queue(bool partially); 814 // Moves entries from the global stack to the local queue and 815 // drains the local queue. If partially is true, then it stops when 816 // both the global stack and the local queue reach a given size. If 817 // partially if false, it tries to empty them totally. 818 void drain_global_stack(bool partially); 819 // Keeps picking SATB buffers and processing them until no SATB 820 // buffers are available. 821 void drain_satb_buffers(); 822 823 // Moves the local finger to a new location 824 inline void move_finger_to(HeapWord* new_finger) { 825 assert(new_finger >= _finger && new_finger < _region_limit, "invariant"); 826 _finger = new_finger; 827 } 828 829 G1CMTask(uint worker_id, 830 G1ConcurrentMark *cm, 831 G1CMTaskQueue* task_queue, 832 G1RegionMarkStats* mark_stats, 833 uint max_regions); 834 835 inline void update_liveness(oop const obj, size_t const obj_size); 836 837 // Clear (without flushing) the mark cache entry for the given region. 838 void clear_mark_stats_cache(uint region_idx); 839 // Evict the whole statistics cache into the global statistics. Returns the 840 // number of cache hits and misses so far. 841 Pair<size_t, size_t> flush_mark_stats_cache(); 842 // Prints statistics associated with this task 843 void print_stats(); 844 }; 845 846 // Class that's used to to print out per-region liveness 847 // information. It's currently used at the end of marking and also 848 // after we sort the old regions at the end of the cleanup operation. 849 class G1PrintRegionLivenessInfoClosure : public HeapRegionClosure { 850 // Accumulators for these values. 851 size_t _total_used_bytes; 852 size_t _total_capacity_bytes; 853 size_t _total_prev_live_bytes; 854 size_t _total_next_live_bytes; 855 856 // Accumulator for the remembered set size 857 size_t _total_remset_bytes; 858 859 // Accumulator for strong code roots memory size 860 size_t _total_strong_code_roots_bytes; 861 862 static double bytes_to_mb(size_t val) { 863 return (double) val / (double) M; 864 } 865 866 public: 867 // The header and footer are printed in the constructor and 868 // destructor respectively. 869 G1PrintRegionLivenessInfoClosure(const char* phase_name); 870 virtual bool do_heap_region(HeapRegion* r); 871 ~G1PrintRegionLivenessInfoClosure(); 872 }; 873 874 #endif // SHARE_GC_G1_G1CONCURRENTMARK_HPP