1 /* 2 * Copyright (c) 2001, 2016, 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_G1_G1CONCURRENTMARK_HPP 26 #define SHARE_VM_GC_G1_G1CONCURRENTMARK_HPP 27 28 #include "classfile/javaClasses.hpp" 29 #include "gc/g1/g1RegionToSpaceMapper.hpp" 30 #include "gc/g1/heapRegionSet.hpp" 31 #include "gc/shared/taskqueue.hpp" 32 33 class G1CollectedHeap; 34 class G1CMBitMap; 35 class G1CMTask; 36 class G1ConcurrentMark; 37 class ConcurrentGCTimer; 38 class G1OldTracer; 39 class G1SurvivorRegions; 40 typedef GenericTaskQueue<oop, mtGC> G1CMTaskQueue; 41 typedef GenericTaskQueueSet<G1CMTaskQueue, mtGC> G1CMTaskQueueSet; 42 43 // Closure used by CM during concurrent reference discovery 44 // and reference processing (during remarking) to determine 45 // if a particular object is alive. It is primarily used 46 // to determine if referents of discovered reference objects 47 // are alive. An instance is also embedded into the 48 // reference processor as the _is_alive_non_header field 49 class G1CMIsAliveClosure: public BoolObjectClosure { 50 G1CollectedHeap* _g1; 51 public: 52 G1CMIsAliveClosure(G1CollectedHeap* g1) : _g1(g1) { } 53 54 bool do_object_b(oop obj); 55 }; 56 57 // A generic CM bit map. This is essentially a wrapper around the BitMap 58 // class, with one bit per (1<<_shifter) HeapWords. 59 60 class G1CMBitMapRO VALUE_OBJ_CLASS_SPEC { 61 protected: 62 HeapWord* _bmStartWord; // base address of range covered by map 63 size_t _bmWordSize; // map size (in #HeapWords covered) 64 const int _shifter; // map to char or bit 65 BitMapView _bm; // the bit map itself 66 67 public: 68 // constructor 69 G1CMBitMapRO(int shifter); 70 71 // inquiries 72 HeapWord* startWord() const { return _bmStartWord; } 73 // the following is one past the last word in space 74 HeapWord* endWord() const { return _bmStartWord + _bmWordSize; } 75 76 // read marks 77 78 bool isMarked(HeapWord* addr) const { 79 assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize), 80 "outside underlying space?"); 81 return _bm.at(heapWordToOffset(addr)); 82 } 83 84 // iteration 85 inline bool iterate(BitMapClosure* cl, MemRegion mr); 86 87 // Return the address corresponding to the next marked bit at or after 88 // "addr", and before "limit", if "limit" is non-NULL. If there is no 89 // such bit, returns "limit" if that is non-NULL, or else "endWord()". 90 HeapWord* getNextMarkedWordAddress(const HeapWord* addr, 91 const HeapWord* limit = NULL) const; 92 93 // conversion utilities 94 HeapWord* offsetToHeapWord(size_t offset) const { 95 return _bmStartWord + (offset << _shifter); 96 } 97 size_t heapWordToOffset(const HeapWord* addr) const { 98 return pointer_delta(addr, _bmStartWord) >> _shifter; 99 } 100 101 // The argument addr should be the start address of a valid object 102 inline HeapWord* nextObject(HeapWord* addr); 103 104 void print_on_error(outputStream* st, const char* prefix) const; 105 106 // debugging 107 NOT_PRODUCT(bool covers(MemRegion rs) const;) 108 }; 109 110 class G1CMBitMapMappingChangedListener : public G1MappingChangedListener { 111 private: 112 G1CMBitMap* _bm; 113 public: 114 G1CMBitMapMappingChangedListener() : _bm(NULL) {} 115 116 void set_bitmap(G1CMBitMap* bm) { _bm = bm; } 117 118 virtual void on_commit(uint start_idx, size_t num_regions, bool zero_filled); 119 }; 120 121 class G1CMBitMap : public G1CMBitMapRO { 122 private: 123 G1CMBitMapMappingChangedListener _listener; 124 125 public: 126 static size_t compute_size(size_t heap_size); 127 // Returns the amount of bytes on the heap between two marks in the bitmap. 128 static size_t mark_distance(); 129 // Returns how many bytes (or bits) of the heap a single byte (or bit) of the 130 // mark bitmap corresponds to. This is the same as the mark distance above. 131 static size_t heap_map_factor() { 132 return mark_distance(); 133 } 134 135 G1CMBitMap() : G1CMBitMapRO(LogMinObjAlignment), _listener() { _listener.set_bitmap(this); } 136 137 // Initializes the underlying BitMap to cover the given area. 138 void initialize(MemRegion heap, G1RegionToSpaceMapper* storage); 139 140 // Write marks. 141 inline void mark(HeapWord* addr); 142 inline void clear(HeapWord* addr); 143 inline bool parMark(HeapWord* addr); 144 145 void clear_range(MemRegion mr); 146 }; 147 148 // Represents the overflow mark stack used by concurrent marking. 149 // 150 // Stores oops in a huge buffer in virtual memory that is always fully committed. 151 // Resizing may only happen during a STW pause when the stack is empty. 152 // 153 // Memory is allocated on a "chunk" basis, i.e. a set of oops. For this, the mark 154 // stack memory is split into evenly sized chunks of oops. Users can only 155 // add or remove entries on that basis. 156 // Chunks are filled in increasing address order. Not completely filled chunks 157 // have a NULL element as a terminating element. 158 // 159 // Every chunk has a header containing a single pointer element used for memory 160 // management. This wastes some space, but is negligible (< .1% with current sizing). 161 // 162 // Memory management is done using a mix of tracking a high water-mark indicating 163 // that all chunks at a lower address are valid chunks, and a singly linked free 164 // list connecting all empty chunks. 165 class G1CMMarkStack VALUE_OBJ_CLASS_SPEC { 166 public: 167 // Number of oops that can fit in a single chunk. 168 static const size_t OopsPerChunk = 1024 - 1 /* One reference for the next pointer */; 169 private: 170 struct OopChunk { 171 OopChunk* next; 172 oop data[OopsPerChunk]; 173 }; 174 175 size_t _max_chunk_capacity; // Maximum number of OopChunk elements on the stack. 176 177 OopChunk* _base; // Bottom address of allocated memory area. 178 size_t _chunk_capacity; // Current maximum number of OopChunk elements. 179 180 char _pad0[DEFAULT_CACHE_LINE_SIZE]; 181 OopChunk* volatile _free_list; // Linked list of free chunks that can be allocated by users. 182 char _pad1[DEFAULT_CACHE_LINE_SIZE - sizeof(OopChunk*)]; 183 OopChunk* volatile _chunk_list; // List of chunks currently containing data. 184 char _pad2[DEFAULT_CACHE_LINE_SIZE - sizeof(OopChunk*)]; 185 186 volatile size_t _hwm; // High water mark within the reserved space. 187 char _pad4[DEFAULT_CACHE_LINE_SIZE - sizeof(size_t)]; 188 189 // Allocate a new chunk from the reserved memory, using the high water mark. Returns 190 // NULL if out of memory. 191 OopChunk* allocate_new_chunk(); 192 193 size_t volatile _chunks_in_chunk_list; 194 195 volatile bool _out_of_memory; 196 197 // Atomically add the given chunk to the list. 198 void add_chunk_to_list(OopChunk* volatile* list, OopChunk* elem); 199 // Atomically remove and return a chunk from the given list. Returns NULL if the 200 // list is empty. 201 OopChunk* remove_chunk_from_list(OopChunk* volatile* list); 202 203 bool _should_expand; 204 205 // Resizes the mark stack to the given new capacity. Releases any previous 206 // memory if successful. 207 bool resize(size_t new_capacity); 208 209 public: 210 G1CMMarkStack(); 211 ~G1CMMarkStack(); 212 213 // Alignment and minimum capacity of this mark stack in number of oops. 214 static size_t capacity_alignment(); 215 216 // Allocate and initialize the mark stack with the given number of oops. 217 bool initialize(size_t initial_capacity, size_t max_capacity); 218 219 // Pushes the given buffer containing at most OopsPerChunk elements on the mark 220 // stack. If less than OopsPerChunk elements are to be pushed, the array must 221 // be terminated with a NULL. 222 // Returns whether the buffer contents were successfully pushed to the global mark 223 // stack. 224 bool par_push_chunk(oop* buffer); 225 226 // Pops a chunk from this mark stack, copying them into the given buffer. This 227 // chunk may contain up to OopsPerChunk elements. If there are less, the last 228 // element in the array is a NULL pointer. 229 bool par_pop_chunk(oop* buffer); 230 231 // Return whether the chunk list is empty. Racy due to unsynchronized access to 232 // _chunks_in_chunk_list. 233 bool is_empty() const { return _chunks_in_chunk_list == 0; } 234 235 size_t capacity() const { return _chunk_capacity; } 236 237 bool is_out_of_memory() const { return _out_of_memory; } 238 void clear_out_of_memory() { _out_of_memory = false; } 239 240 bool should_expand() const { return _should_expand; } 241 void set_should_expand(bool value) { _should_expand = value; } 242 243 // Expand the stack, typically in response to an overflow condition 244 void expand(); 245 246 // Return the approximate number of oops on this mark stack. Racy due to 247 // unsynchronized access to _chunks_in_chunk_list. 248 size_t size() const { return _chunks_in_chunk_list * OopsPerChunk; } 249 250 void set_empty(); 251 252 // Apply Fn to every oop on the mark stack. The mark stack must not 253 // be modified while iterating. 254 template<typename Fn> void iterate(Fn fn) const PRODUCT_RETURN; 255 }; 256 257 // Root Regions are regions that are not empty at the beginning of a 258 // marking cycle and which we might collect during an evacuation pause 259 // while the cycle is active. Given that, during evacuation pauses, we 260 // do not copy objects that are explicitly marked, what we have to do 261 // for the root regions is to scan them and mark all objects reachable 262 // from them. According to the SATB assumptions, we only need to visit 263 // each object once during marking. So, as long as we finish this scan 264 // before the next evacuation pause, we can copy the objects from the 265 // root regions without having to mark them or do anything else to them. 266 // 267 // Currently, we only support root region scanning once (at the start 268 // of the marking cycle) and the root regions are all the survivor 269 // regions populated during the initial-mark pause. 270 class G1CMRootRegions VALUE_OBJ_CLASS_SPEC { 271 private: 272 const G1SurvivorRegions* _survivors; 273 G1ConcurrentMark* _cm; 274 275 volatile bool _scan_in_progress; 276 volatile bool _should_abort; 277 volatile int _claimed_survivor_index; 278 279 void notify_scan_done(); 280 281 public: 282 G1CMRootRegions(); 283 // We actually do most of the initialization in this method. 284 void init(const G1SurvivorRegions* survivors, G1ConcurrentMark* cm); 285 286 // Reset the claiming / scanning of the root regions. 287 void prepare_for_scan(); 288 289 // Forces get_next() to return NULL so that the iteration aborts early. 290 void abort() { _should_abort = true; } 291 292 // Return true if the CM thread are actively scanning root regions, 293 // false otherwise. 294 bool scan_in_progress() { return _scan_in_progress; } 295 296 // Claim the next root region to scan atomically, or return NULL if 297 // all have been claimed. 298 HeapRegion* claim_next(); 299 300 // The number of root regions to scan. 301 uint num_root_regions() const; 302 303 void cancel_scan(); 304 305 // Flag that we're done with root region scanning and notify anyone 306 // who's waiting on it. If aborted is false, assume that all regions 307 // have been claimed. 308 void scan_finished(); 309 310 // If CM threads are still scanning root regions, wait until they 311 // are done. Return true if we had to wait, false otherwise. 312 bool wait_until_scan_finished(); 313 }; 314 315 class ConcurrentMarkThread; 316 317 class G1ConcurrentMark: public CHeapObj<mtGC> { 318 friend class ConcurrentMarkThread; 319 friend class G1ParNoteEndTask; 320 friend class G1VerifyLiveDataClosure; 321 friend class G1CMRefProcTaskProxy; 322 friend class G1CMRefProcTaskExecutor; 323 friend class G1CMKeepAliveAndDrainClosure; 324 friend class G1CMDrainMarkingStackClosure; 325 friend class G1CMBitMapClosure; 326 friend class G1CMConcurrentMarkingTask; 327 friend class G1CMRemarkTask; 328 friend class G1CMTask; 329 330 protected: 331 ConcurrentMarkThread* _cmThread; // The thread doing the work 332 G1CollectedHeap* _g1h; // The heap 333 uint _parallel_marking_threads; // The number of marking 334 // threads we're using 335 uint _max_parallel_marking_threads; // Max number of marking 336 // threads we'll ever use 337 double _sleep_factor; // How much we have to sleep, with 338 // respect to the work we just did, to 339 // meet the marking overhead goal 340 double _marking_task_overhead; // Marking target overhead for 341 // a single task 342 343 FreeRegionList _cleanup_list; 344 345 // Concurrent marking support structures 346 G1CMBitMap _markBitMap1; 347 G1CMBitMap _markBitMap2; 348 G1CMBitMapRO* _prevMarkBitMap; // Completed mark bitmap 349 G1CMBitMap* _nextMarkBitMap; // Under-construction mark bitmap 350 351 // Heap bounds 352 HeapWord* _heap_start; 353 HeapWord* _heap_end; 354 355 // Root region tracking and claiming 356 G1CMRootRegions _root_regions; 357 358 // For gray objects 359 G1CMMarkStack _global_mark_stack; // Grey objects behind global finger 360 HeapWord* volatile _finger; // The global finger, region aligned, 361 // always points to the end of the 362 // last claimed region 363 364 // Marking tasks 365 uint _max_worker_id;// Maximum worker id 366 uint _active_tasks; // Task num currently active 367 G1CMTask** _tasks; // Task queue array (max_worker_id len) 368 G1CMTaskQueueSet* _task_queues; // Task queue set 369 ParallelTaskTerminator _terminator; // For termination 370 371 // Two sync barriers that are used to synchronize tasks when an 372 // overflow occurs. The algorithm is the following. All tasks enter 373 // the first one to ensure that they have all stopped manipulating 374 // the global data structures. After they exit it, they re-initialize 375 // their data structures and task 0 re-initializes the global data 376 // structures. Then, they enter the second sync barrier. This 377 // ensure, that no task starts doing work before all data 378 // structures (local and global) have been re-initialized. When they 379 // exit it, they are free to start working again. 380 WorkGangBarrierSync _first_overflow_barrier_sync; 381 WorkGangBarrierSync _second_overflow_barrier_sync; 382 383 // This is set by any task, when an overflow on the global data 384 // structures is detected 385 volatile bool _has_overflown; 386 // True: marking is concurrent, false: we're in remark 387 volatile bool _concurrent; 388 // Set at the end of a Full GC so that marking aborts 389 volatile bool _has_aborted; 390 391 // Used when remark aborts due to an overflow to indicate that 392 // another concurrent marking phase should start 393 volatile bool _restart_for_overflow; 394 395 // This is true from the very start of concurrent marking until the 396 // point when all the tasks complete their work. It is really used 397 // to determine the points between the end of concurrent marking and 398 // time of remark. 399 volatile bool _concurrent_marking_in_progress; 400 401 ConcurrentGCTimer* _gc_timer_cm; 402 403 G1OldTracer* _gc_tracer_cm; 404 405 // All of these times are in ms 406 NumberSeq _init_times; 407 NumberSeq _remark_times; 408 NumberSeq _remark_mark_times; 409 NumberSeq _remark_weak_ref_times; 410 NumberSeq _cleanup_times; 411 double _total_counting_time; 412 double _total_rs_scrub_time; 413 414 double* _accum_task_vtime; // Accumulated task vtime 415 416 WorkGang* _parallel_workers; 417 418 void weakRefsWorkParallelPart(BoolObjectClosure* is_alive, bool purged_classes); 419 void weakRefsWork(bool clear_all_soft_refs); 420 421 void swapMarkBitMaps(); 422 423 // It resets the global marking data structures, as well as the 424 // task local ones; should be called during initial mark. 425 void reset(); 426 427 // Resets all the marking data structures. Called when we have to restart 428 // marking or when marking completes (via set_non_marking_state below). 429 void reset_marking_state(bool clear_overflow = true); 430 431 // We do this after we're done with marking so that the marking data 432 // structures are initialized to a sensible and predictable state. 433 void set_non_marking_state(); 434 435 // Called to indicate how many threads are currently active. 436 void set_concurrency(uint active_tasks); 437 438 // It should be called to indicate which phase we're in (concurrent 439 // mark or remark) and how many threads are currently active. 440 void set_concurrency_and_phase(uint active_tasks, bool concurrent); 441 442 // Prints all gathered CM-related statistics 443 void print_stats(); 444 445 bool cleanup_list_is_empty() { 446 return _cleanup_list.is_empty(); 447 } 448 449 // Accessor methods 450 uint parallel_marking_threads() const { return _parallel_marking_threads; } 451 uint max_parallel_marking_threads() const { return _max_parallel_marking_threads;} 452 double sleep_factor() { return _sleep_factor; } 453 double marking_task_overhead() { return _marking_task_overhead;} 454 455 HeapWord* finger() { return _finger; } 456 bool concurrent() { return _concurrent; } 457 uint active_tasks() { return _active_tasks; } 458 ParallelTaskTerminator* terminator() { return &_terminator; } 459 460 // It claims the next available region to be scanned by a marking 461 // task/thread. It might return NULL if the next region is empty or 462 // we have run out of regions. In the latter case, out_of_regions() 463 // determines whether we've really run out of regions or the task 464 // should call claim_region() again. This might seem a bit 465 // awkward. Originally, the code was written so that claim_region() 466 // either successfully returned with a non-empty region or there 467 // were no more regions to be claimed. The problem with this was 468 // that, in certain circumstances, it iterated over large chunks of 469 // the heap finding only empty regions and, while it was working, it 470 // was preventing the calling task to call its regular clock 471 // method. So, this way, each task will spend very little time in 472 // claim_region() and is allowed to call the regular clock method 473 // frequently. 474 HeapRegion* claim_region(uint worker_id); 475 476 // It determines whether we've run out of regions to scan. Note that 477 // the finger can point past the heap end in case the heap was expanded 478 // to satisfy an allocation without doing a GC. This is fine, because all 479 // objects in those regions will be considered live anyway because of 480 // SATB guarantees (i.e. their TAMS will be equal to bottom). 481 bool out_of_regions() { return _finger >= _heap_end; } 482 483 // Returns the task with the given id 484 G1CMTask* task(int id) { 485 assert(0 <= id && id < (int) _active_tasks, 486 "task id not within active bounds"); 487 return _tasks[id]; 488 } 489 490 // Returns the task queue with the given id 491 G1CMTaskQueue* task_queue(int id) { 492 assert(0 <= id && id < (int) _active_tasks, 493 "task queue id not within active bounds"); 494 return (G1CMTaskQueue*) _task_queues->queue(id); 495 } 496 497 // Returns the task queue set 498 G1CMTaskQueueSet* task_queues() { return _task_queues; } 499 500 // Access / manipulation of the overflow flag which is set to 501 // indicate that the global stack has overflown 502 bool has_overflown() { return _has_overflown; } 503 void set_has_overflown() { _has_overflown = true; } 504 void clear_has_overflown() { _has_overflown = false; } 505 bool restart_for_overflow() { return _restart_for_overflow; } 506 507 // Methods to enter the two overflow sync barriers 508 void enter_first_sync_barrier(uint worker_id); 509 void enter_second_sync_barrier(uint worker_id); 510 511 // Card index of the bottom of the G1 heap. Used for biasing indices into 512 // the card bitmaps. 513 intptr_t _heap_bottom_card_num; 514 515 // Set to true when initialization is complete 516 bool _completed_initialization; 517 518 // end_timer, true to end gc timer after ending concurrent phase. 519 void register_concurrent_phase_end_common(bool end_timer); 520 521 // Clear the given bitmap in parallel using the given WorkGang. If may_yield is 522 // true, periodically insert checks to see if this method should exit prematurely. 523 void clear_bitmap(G1CMBitMap* bitmap, WorkGang* workers, bool may_yield); 524 public: 525 // Manipulation of the global mark stack. 526 // The push and pop operations are used by tasks for transfers 527 // between task-local queues and the global mark stack. 528 bool mark_stack_push(oop* arr) { 529 if (!_global_mark_stack.par_push_chunk(arr)) { 530 set_has_overflown(); 531 return false; 532 } 533 return true; 534 } 535 bool mark_stack_pop(oop* arr) { 536 return _global_mark_stack.par_pop_chunk(arr); 537 } 538 size_t mark_stack_size() { return _global_mark_stack.size(); } 539 size_t partial_mark_stack_size_target() { return _global_mark_stack.capacity()/3; } 540 bool mark_stack_overflow() { return _global_mark_stack.is_out_of_memory(); } 541 bool mark_stack_empty() { return _global_mark_stack.is_empty(); } 542 543 G1CMRootRegions* root_regions() { return &_root_regions; } 544 545 bool concurrent_marking_in_progress() { 546 return _concurrent_marking_in_progress; 547 } 548 void set_concurrent_marking_in_progress() { 549 _concurrent_marking_in_progress = true; 550 } 551 void clear_concurrent_marking_in_progress() { 552 _concurrent_marking_in_progress = false; 553 } 554 555 void concurrent_cycle_start(); 556 void concurrent_cycle_end(); 557 558 void update_accum_task_vtime(int i, double vtime) { 559 _accum_task_vtime[i] += vtime; 560 } 561 562 double all_task_accum_vtime() { 563 double ret = 0.0; 564 for (uint i = 0; i < _max_worker_id; ++i) 565 ret += _accum_task_vtime[i]; 566 return ret; 567 } 568 569 // Attempts to steal an object from the task queues of other tasks 570 bool try_stealing(uint worker_id, int* hash_seed, oop& obj); 571 572 G1ConcurrentMark(G1CollectedHeap* g1h, 573 G1RegionToSpaceMapper* prev_bitmap_storage, 574 G1RegionToSpaceMapper* next_bitmap_storage); 575 ~G1ConcurrentMark(); 576 577 ConcurrentMarkThread* cmThread() { return _cmThread; } 578 579 G1CMBitMapRO* prevMarkBitMap() const { return _prevMarkBitMap; } 580 G1CMBitMap* nextMarkBitMap() const { return _nextMarkBitMap; } 581 582 // Returns the number of GC threads to be used in a concurrent 583 // phase based on the number of GC threads being used in a STW 584 // phase. 585 uint scale_parallel_threads(uint n_par_threads); 586 587 // Calculates the number of GC threads to be used in a concurrent phase. 588 uint calc_parallel_marking_threads(); 589 590 // The following three are interaction between CM and 591 // G1CollectedHeap 592 593 // This notifies CM that a root during initial-mark needs to be 594 // grayed. It is MT-safe. hr is the region that 595 // contains the object and it's passed optionally from callers who 596 // might already have it (no point in recalculating it). 597 inline void grayRoot(oop obj, 598 HeapRegion* hr = NULL); 599 600 // Prepare internal data structures for the next mark cycle. This includes clearing 601 // the next mark bitmap and some internal data structures. This method is intended 602 // to be called concurrently to the mutator. It will yield to safepoint requests. 603 void cleanup_for_next_mark(); 604 605 // Clear the previous marking bitmap during safepoint. 606 void clear_prev_bitmap(WorkGang* workers); 607 608 // Return whether the next mark bitmap has no marks set. To be used for assertions 609 // only. Will not yield to pause requests. 610 bool nextMarkBitmapIsClear(); 611 612 // These two do the work that needs to be done before and after the 613 // initial root checkpoint. Since this checkpoint can be done at two 614 // different points (i.e. an explicit pause or piggy-backed on a 615 // young collection), then it's nice to be able to easily share the 616 // pre/post code. It might be the case that we can put everything in 617 // the post method. TP 618 void checkpointRootsInitialPre(); 619 void checkpointRootsInitialPost(); 620 621 // Scan all the root regions and mark everything reachable from 622 // them. 623 void scan_root_regions(); 624 625 // Scan a single root region and mark everything reachable from it. 626 void scanRootRegion(HeapRegion* hr); 627 628 // Do concurrent phase of marking, to a tentative transitive closure. 629 void mark_from_roots(); 630 631 void checkpointRootsFinal(bool clear_all_soft_refs); 632 void checkpointRootsFinalWork(); 633 void cleanup(); 634 void complete_cleanup(); 635 636 // Mark in the previous bitmap. NB: this is usually read-only, so use 637 // this carefully! 638 inline void markPrev(oop p); 639 640 // Clears marks for all objects in the given range, for the prev or 641 // next bitmaps. NB: the previous bitmap is usually 642 // read-only, so use this carefully! 643 void clearRangePrevBitmap(MemRegion mr); 644 645 // Verify that there are no CSet oops on the stacks (taskqueues / 646 // global mark stack) and fingers (global / per-task). 647 // If marking is not in progress, it's a no-op. 648 void verify_no_cset_oops() PRODUCT_RETURN; 649 650 inline bool isPrevMarked(oop p) const; 651 652 inline bool do_yield_check(); 653 654 // Abandon current marking iteration due to a Full GC. 655 void abort(); 656 657 bool has_aborted() { return _has_aborted; } 658 659 void print_summary_info(); 660 661 void print_worker_threads_on(outputStream* st) const; 662 void threads_do(ThreadClosure* tc) const; 663 664 void print_on_error(outputStream* st) const; 665 666 // Attempts to mark the given object on the next mark bitmap. 667 inline bool par_mark(oop obj); 668 669 // Returns true if initialization was successfully completed. 670 bool completed_initialization() const { 671 return _completed_initialization; 672 } 673 674 ConcurrentGCTimer* gc_timer_cm() const { return _gc_timer_cm; } 675 G1OldTracer* gc_tracer_cm() const { return _gc_tracer_cm; } 676 677 private: 678 // Clear (Reset) all liveness count data. 679 void clear_live_data(WorkGang* workers); 680 681 #ifdef ASSERT 682 // Verify all of the above data structures that they are in initial state. 683 void verify_live_data_clear(); 684 #endif 685 686 // Aggregates the per-card liveness data based on the current marking. Also sets 687 // the amount of marked bytes for each region. 688 void create_live_data(); 689 690 void finalize_live_data(); 691 692 void verify_live_data(); 693 }; 694 695 // A class representing a marking task. 696 class G1CMTask : public TerminatorTerminator { 697 private: 698 enum PrivateConstants { 699 // The regular clock call is called once the scanned words reaches 700 // this limit 701 words_scanned_period = 12*1024, 702 // The regular clock call is called once the number of visited 703 // references reaches this limit 704 refs_reached_period = 384, 705 // Initial value for the hash seed, used in the work stealing code 706 init_hash_seed = 17 707 }; 708 709 uint _worker_id; 710 G1CollectedHeap* _g1h; 711 G1ConcurrentMark* _cm; 712 G1CMBitMap* _nextMarkBitMap; 713 // the task queue of this task 714 G1CMTaskQueue* _task_queue; 715 private: 716 // the task queue set---needed for stealing 717 G1CMTaskQueueSet* _task_queues; 718 // indicates whether the task has been claimed---this is only for 719 // debugging purposes 720 bool _claimed; 721 722 // number of calls to this task 723 int _calls; 724 725 // when the virtual timer reaches this time, the marking step should 726 // exit 727 double _time_target_ms; 728 // the start time of the current marking step 729 double _start_time_ms; 730 731 // the oop closure used for iterations over oops 732 G1CMOopClosure* _cm_oop_closure; 733 734 // the region this task is scanning, NULL if we're not scanning any 735 HeapRegion* _curr_region; 736 // the local finger of this task, NULL if we're not scanning a region 737 HeapWord* _finger; 738 // limit of the region this task is scanning, NULL if we're not scanning one 739 HeapWord* _region_limit; 740 741 // the number of words this task has scanned 742 size_t _words_scanned; 743 // When _words_scanned reaches this limit, the regular clock is 744 // called. Notice that this might be decreased under certain 745 // circumstances (i.e. when we believe that we did an expensive 746 // operation). 747 size_t _words_scanned_limit; 748 // the initial value of _words_scanned_limit (i.e. what it was 749 // before it was decreased). 750 size_t _real_words_scanned_limit; 751 752 // the number of references this task has visited 753 size_t _refs_reached; 754 // When _refs_reached reaches this limit, the regular clock is 755 // called. Notice this this might be decreased under certain 756 // circumstances (i.e. when we believe that we did an expensive 757 // operation). 758 size_t _refs_reached_limit; 759 // the initial value of _refs_reached_limit (i.e. what it was before 760 // it was decreased). 761 size_t _real_refs_reached_limit; 762 763 // used by the work stealing stuff 764 int _hash_seed; 765 // if this is true, then the task has aborted for some reason 766 bool _has_aborted; 767 // set when the task aborts because it has met its time quota 768 bool _has_timed_out; 769 // true when we're draining SATB buffers; this avoids the task 770 // aborting due to SATB buffers being available (as we're already 771 // dealing with them) 772 bool _draining_satb_buffers; 773 774 // number sequence of past step times 775 NumberSeq _step_times_ms; 776 // elapsed time of this task 777 double _elapsed_time_ms; 778 // termination time of this task 779 double _termination_time_ms; 780 // when this task got into the termination protocol 781 double _termination_start_time_ms; 782 783 // true when the task is during a concurrent phase, false when it is 784 // in the remark phase (so, in the latter case, we do not have to 785 // check all the things that we have to check during the concurrent 786 // phase, i.e. SATB buffer availability...) 787 bool _concurrent; 788 789 TruncatedSeq _marking_step_diffs_ms; 790 791 // it updates the local fields after this task has claimed 792 // a new region to scan 793 void setup_for_region(HeapRegion* hr); 794 // it brings up-to-date the limit of the region 795 void update_region_limit(); 796 797 // called when either the words scanned or the refs visited limit 798 // has been reached 799 void reached_limit(); 800 // recalculates the words scanned and refs visited limits 801 void recalculate_limits(); 802 // decreases the words scanned and refs visited limits when we reach 803 // an expensive operation 804 void decrease_limits(); 805 // it checks whether the words scanned or refs visited reached their 806 // respective limit and calls reached_limit() if they have 807 void check_limits() { 808 if (_words_scanned >= _words_scanned_limit || 809 _refs_reached >= _refs_reached_limit) { 810 reached_limit(); 811 } 812 } 813 // this is supposed to be called regularly during a marking step as 814 // it checks a bunch of conditions that might cause the marking step 815 // to abort 816 void regular_clock_call(); 817 bool concurrent() { return _concurrent; } 818 819 // Test whether obj might have already been passed over by the 820 // mark bitmap scan, and so needs to be pushed onto the mark stack. 821 bool is_below_finger(oop obj, HeapWord* global_finger) const; 822 823 template<bool scan> void process_grey_object(oop obj); 824 825 public: 826 // It resets the task; it should be called right at the beginning of 827 // a marking phase. 828 void reset(G1CMBitMap* _nextMarkBitMap); 829 // it clears all the fields that correspond to a claimed region. 830 void clear_region_fields(); 831 832 void set_concurrent(bool concurrent) { _concurrent = concurrent; } 833 834 // The main method of this class which performs a marking step 835 // trying not to exceed the given duration. However, it might exit 836 // prematurely, according to some conditions (i.e. SATB buffers are 837 // available for processing). 838 void do_marking_step(double target_ms, 839 bool do_termination, 840 bool is_serial); 841 842 // These two calls start and stop the timer 843 void record_start_time() { 844 _elapsed_time_ms = os::elapsedTime() * 1000.0; 845 } 846 void record_end_time() { 847 _elapsed_time_ms = os::elapsedTime() * 1000.0 - _elapsed_time_ms; 848 } 849 850 // returns the worker ID associated with this task. 851 uint worker_id() { return _worker_id; } 852 853 // From TerminatorTerminator. It determines whether this task should 854 // exit the termination protocol after it's entered it. 855 virtual bool should_exit_termination(); 856 857 // Resets the local region fields after a task has finished scanning a 858 // region; or when they have become stale as a result of the region 859 // being evacuated. 860 void giveup_current_region(); 861 862 HeapWord* finger() { return _finger; } 863 864 bool has_aborted() { return _has_aborted; } 865 void set_has_aborted() { _has_aborted = true; } 866 void clear_has_aborted() { _has_aborted = false; } 867 bool has_timed_out() { return _has_timed_out; } 868 bool claimed() { return _claimed; } 869 870 void set_cm_oop_closure(G1CMOopClosure* cm_oop_closure); 871 872 // Increment the number of references this task has visited. 873 void increment_refs_reached() { ++_refs_reached; } 874 875 // Grey the object by marking it. If not already marked, push it on 876 // the local queue if below the finger. 877 // obj is below its region's NTAMS. 878 inline void make_reference_grey(oop obj); 879 880 // Grey the object (by calling make_grey_reference) if required, 881 // e.g. obj is below its containing region's NTAMS. 882 // Precondition: obj is a valid heap object. 883 inline void deal_with_reference(oop obj); 884 885 // It scans an object and visits its children. 886 inline void scan_object(oop obj); 887 888 // It pushes an object on the local queue. 889 inline void push(oop obj); 890 891 // Move entries to the global stack. 892 void move_entries_to_global_stack(); 893 // Move entries from the global stack, return true if we were successful to do so. 894 bool get_entries_from_global_stack(); 895 896 // It pops and scans objects from the local queue. If partially is 897 // true, then it stops when the queue size is of a given limit. If 898 // partially is false, then it stops when the queue is empty. 899 void drain_local_queue(bool partially); 900 // It moves entries from the global stack to the local queue and 901 // drains the local queue. If partially is true, then it stops when 902 // both the global stack and the local queue reach a given size. If 903 // partially if false, it tries to empty them totally. 904 void drain_global_stack(bool partially); 905 // It keeps picking SATB buffers and processing them until no SATB 906 // buffers are available. 907 void drain_satb_buffers(); 908 909 // moves the local finger to a new location 910 inline void move_finger_to(HeapWord* new_finger) { 911 assert(new_finger >= _finger && new_finger < _region_limit, "invariant"); 912 _finger = new_finger; 913 } 914 915 G1CMTask(uint worker_id, 916 G1ConcurrentMark *cm, 917 G1CMTaskQueue* task_queue, 918 G1CMTaskQueueSet* task_queues); 919 920 // it prints statistics associated with this task 921 void print_stats(); 922 }; 923 924 // Class that's used to to print out per-region liveness 925 // information. It's currently used at the end of marking and also 926 // after we sort the old regions at the end of the cleanup operation. 927 class G1PrintRegionLivenessInfoClosure: public HeapRegionClosure { 928 private: 929 // Accumulators for these values. 930 size_t _total_used_bytes; 931 size_t _total_capacity_bytes; 932 size_t _total_prev_live_bytes; 933 size_t _total_next_live_bytes; 934 935 // Accumulator for the remembered set size 936 size_t _total_remset_bytes; 937 938 // Accumulator for strong code roots memory size 939 size_t _total_strong_code_roots_bytes; 940 941 static double perc(size_t val, size_t total) { 942 if (total == 0) { 943 return 0.0; 944 } else { 945 return 100.0 * ((double) val / (double) total); 946 } 947 } 948 949 static double bytes_to_mb(size_t val) { 950 return (double) val / (double) M; 951 } 952 953 public: 954 // The header and footer are printed in the constructor and 955 // destructor respectively. 956 G1PrintRegionLivenessInfoClosure(const char* phase_name); 957 virtual bool doHeapRegion(HeapRegion* r); 958 ~G1PrintRegionLivenessInfoClosure(); 959 }; 960 961 #endif // SHARE_VM_GC_G1_G1CONCURRENTMARK_HPP