Wed, 19 Aug 2009 12:53:25 -0400
6871111: G1: remove the concurrent overhead tracker
Summary: Removing the concurrent overhead tracker from G1, along with the GC overhead reporter and the G1AccountConcurrentOverhead (both of which rely on the the concurrent overhead tracker).
Reviewed-by: iveresov, johnc
1 /*
2 * Copyright 2001-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 class G1CollectedHeap;
26 class CMTask;
27 typedef GenericTaskQueue<oop> CMTaskQueue;
28 typedef GenericTaskQueueSet<oop> CMTaskQueueSet;
30 // A generic CM bit map. This is essentially a wrapper around the BitMap
31 // class, with one bit per (1<<_shifter) HeapWords.
33 class CMBitMapRO VALUE_OBJ_CLASS_SPEC {
34 protected:
35 HeapWord* _bmStartWord; // base address of range covered by map
36 size_t _bmWordSize; // map size (in #HeapWords covered)
37 const int _shifter; // map to char or bit
38 VirtualSpace _virtual_space; // underlying the bit map
39 BitMap _bm; // the bit map itself
41 public:
42 // constructor
43 CMBitMapRO(ReservedSpace rs, int shifter);
45 enum { do_yield = true };
47 // inquiries
48 HeapWord* startWord() const { return _bmStartWord; }
49 size_t sizeInWords() const { return _bmWordSize; }
50 // the following is one past the last word in space
51 HeapWord* endWord() const { return _bmStartWord + _bmWordSize; }
53 // read marks
55 bool isMarked(HeapWord* addr) const {
56 assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize),
57 "outside underlying space?");
58 return _bm.at(heapWordToOffset(addr));
59 }
61 // iteration
62 bool iterate(BitMapClosure* cl) { return _bm.iterate(cl); }
63 bool iterate(BitMapClosure* cl, MemRegion mr);
65 // Return the address corresponding to the next marked bit at or after
66 // "addr", and before "limit", if "limit" is non-NULL. If there is no
67 // such bit, returns "limit" if that is non-NULL, or else "endWord()".
68 HeapWord* getNextMarkedWordAddress(HeapWord* addr,
69 HeapWord* limit = NULL) const;
70 // Return the address corresponding to the next unmarked bit at or after
71 // "addr", and before "limit", if "limit" is non-NULL. If there is no
72 // such bit, returns "limit" if that is non-NULL, or else "endWord()".
73 HeapWord* getNextUnmarkedWordAddress(HeapWord* addr,
74 HeapWord* limit = NULL) const;
76 // conversion utilities
77 // XXX Fix these so that offsets are size_t's...
78 HeapWord* offsetToHeapWord(size_t offset) const {
79 return _bmStartWord + (offset << _shifter);
80 }
81 size_t heapWordToOffset(HeapWord* addr) const {
82 return pointer_delta(addr, _bmStartWord) >> _shifter;
83 }
84 int heapWordDiffToOffsetDiff(size_t diff) const;
85 HeapWord* nextWord(HeapWord* addr) {
86 return offsetToHeapWord(heapWordToOffset(addr) + 1);
87 }
89 void mostly_disjoint_range_union(BitMap* from_bitmap,
90 size_t from_start_index,
91 HeapWord* to_start_word,
92 size_t word_num);
94 // debugging
95 NOT_PRODUCT(bool covers(ReservedSpace rs) const;)
96 };
98 class CMBitMap : public CMBitMapRO {
100 public:
101 // constructor
102 CMBitMap(ReservedSpace rs, int shifter) :
103 CMBitMapRO(rs, shifter) {}
105 // write marks
106 void mark(HeapWord* addr) {
107 assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize),
108 "outside underlying space?");
109 _bm.at_put(heapWordToOffset(addr), true);
110 }
111 void clear(HeapWord* addr) {
112 assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize),
113 "outside underlying space?");
114 _bm.at_put(heapWordToOffset(addr), false);
115 }
116 bool parMark(HeapWord* addr) {
117 assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize),
118 "outside underlying space?");
119 return _bm.par_at_put(heapWordToOffset(addr), true);
120 }
121 bool parClear(HeapWord* addr) {
122 assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize),
123 "outside underlying space?");
124 return _bm.par_at_put(heapWordToOffset(addr), false);
125 }
126 void markRange(MemRegion mr);
127 void clearAll();
128 void clearRange(MemRegion mr);
130 // Starting at the bit corresponding to "addr" (inclusive), find the next
131 // "1" bit, if any. This bit starts some run of consecutive "1"'s; find
132 // the end of this run (stopping at "end_addr"). Return the MemRegion
133 // covering from the start of the region corresponding to the first bit
134 // of the run to the end of the region corresponding to the last bit of
135 // the run. If there is no "1" bit at or after "addr", return an empty
136 // MemRegion.
137 MemRegion getAndClearMarkedRegion(HeapWord* addr, HeapWord* end_addr);
138 };
140 // Represents a marking stack used by the CM collector.
141 // Ideally this should be GrowableArray<> just like MSC's marking stack(s).
142 class CMMarkStack VALUE_OBJ_CLASS_SPEC {
143 ConcurrentMark* _cm;
144 oop* _base; // bottom of stack
145 jint _index; // one more than last occupied index
146 jint _capacity; // max #elements
147 jint _oops_do_bound; // Number of elements to include in next iteration.
148 NOT_PRODUCT(jint _max_depth;) // max depth plumbed during run
150 bool _overflow;
151 DEBUG_ONLY(bool _drain_in_progress;)
152 DEBUG_ONLY(bool _drain_in_progress_yields;)
154 public:
155 CMMarkStack(ConcurrentMark* cm);
156 ~CMMarkStack();
158 void allocate(size_t size);
160 oop pop() {
161 if (!isEmpty()) {
162 return _base[--_index] ;
163 }
164 return NULL;
165 }
167 // If overflow happens, don't do the push, and record the overflow.
168 // *Requires* that "ptr" is already marked.
169 void push(oop ptr) {
170 if (isFull()) {
171 // Record overflow.
172 _overflow = true;
173 return;
174 } else {
175 _base[_index++] = ptr;
176 NOT_PRODUCT(_max_depth = MAX2(_max_depth, _index));
177 }
178 }
179 // Non-block impl. Note: concurrency is allowed only with other
180 // "par_push" operations, not with "pop" or "drain". We would need
181 // parallel versions of them if such concurrency was desired.
182 void par_push(oop ptr);
184 // Pushes the first "n" elements of "ptr_arr" on the stack.
185 // Non-block impl. Note: concurrency is allowed only with other
186 // "par_adjoin_arr" or "push" operations, not with "pop" or "drain".
187 void par_adjoin_arr(oop* ptr_arr, int n);
189 // Pushes the first "n" elements of "ptr_arr" on the stack.
190 // Locking impl: concurrency is allowed only with
191 // "par_push_arr" and/or "par_pop_arr" operations, which use the same
192 // locking strategy.
193 void par_push_arr(oop* ptr_arr, int n);
195 // If returns false, the array was empty. Otherwise, removes up to "max"
196 // elements from the stack, and transfers them to "ptr_arr" in an
197 // unspecified order. The actual number transferred is given in "n" ("n
198 // == 0" is deliberately redundant with the return value.) Locking impl:
199 // concurrency is allowed only with "par_push_arr" and/or "par_pop_arr"
200 // operations, which use the same locking strategy.
201 bool par_pop_arr(oop* ptr_arr, int max, int* n);
203 // Drain the mark stack, applying the given closure to all fields of
204 // objects on the stack. (That is, continue until the stack is empty,
205 // even if closure applications add entries to the stack.) The "bm"
206 // argument, if non-null, may be used to verify that only marked objects
207 // are on the mark stack. If "yield_after" is "true", then the
208 // concurrent marker performing the drain offers to yield after
209 // processing each object. If a yield occurs, stops the drain operation
210 // and returns false. Otherwise, returns true.
211 template<class OopClosureClass>
212 bool drain(OopClosureClass* cl, CMBitMap* bm, bool yield_after = false);
214 bool isEmpty() { return _index == 0; }
215 bool isFull() { return _index == _capacity; }
216 int maxElems() { return _capacity; }
218 bool overflow() { return _overflow; }
219 void clear_overflow() { _overflow = false; }
221 int size() { return _index; }
223 void setEmpty() { _index = 0; clear_overflow(); }
225 // Record the current size; a subsequent "oops_do" will iterate only over
226 // indices valid at the time of this call.
227 void set_oops_do_bound(jint bound = -1) {
228 if (bound == -1) {
229 _oops_do_bound = _index;
230 } else {
231 _oops_do_bound = bound;
232 }
233 }
234 jint oops_do_bound() { return _oops_do_bound; }
235 // iterate over the oops in the mark stack, up to the bound recorded via
236 // the call above.
237 void oops_do(OopClosure* f);
238 };
240 class CMRegionStack VALUE_OBJ_CLASS_SPEC {
241 MemRegion* _base;
242 jint _capacity;
243 jint _index;
244 jint _oops_do_bound;
245 bool _overflow;
246 public:
247 CMRegionStack();
248 ~CMRegionStack();
249 void allocate(size_t size);
251 // This is lock-free; assumes that it will only be called in parallel
252 // with other "push" operations (no pops).
253 void push(MemRegion mr);
255 // Lock-free; assumes that it will only be called in parallel
256 // with other "pop" operations (no pushes).
257 MemRegion pop();
259 bool isEmpty() { return _index == 0; }
260 bool isFull() { return _index == _capacity; }
262 bool overflow() { return _overflow; }
263 void clear_overflow() { _overflow = false; }
265 int size() { return _index; }
267 // It iterates over the entries in the region stack and it
268 // invalidates (i.e. assigns MemRegion()) the ones that point to
269 // regions in the collection set.
270 bool invalidate_entries_into_cset();
272 // This gives an upper bound up to which the iteration in
273 // invalidate_entries_into_cset() will reach. This prevents
274 // newly-added entries to be unnecessarily scanned.
275 void set_oops_do_bound() {
276 _oops_do_bound = _index;
277 }
279 void setEmpty() { _index = 0; clear_overflow(); }
280 };
282 // this will enable a variety of different statistics per GC task
283 #define _MARKING_STATS_ 0
284 // this will enable the higher verbose levels
285 #define _MARKING_VERBOSE_ 0
287 #if _MARKING_STATS_
288 #define statsOnly(statement) \
289 do { \
290 statement ; \
291 } while (0)
292 #else // _MARKING_STATS_
293 #define statsOnly(statement) \
294 do { \
295 } while (0)
296 #endif // _MARKING_STATS_
298 // Some extra guarantees that I like to also enable in optimised mode
299 // when debugging. If you want to enable them, comment out the assert
300 // macro and uncomment out the guaratee macro
301 // #define tmp_guarantee_CM(expr, str) guarantee(expr, str)
302 #define tmp_guarantee_CM(expr, str) assert(expr, str)
304 typedef enum {
305 no_verbose = 0, // verbose turned off
306 stats_verbose, // only prints stats at the end of marking
307 low_verbose, // low verbose, mostly per region and per major event
308 medium_verbose, // a bit more detailed than low
309 high_verbose // per object verbose
310 } CMVerboseLevel;
313 class ConcurrentMarkThread;
315 class ConcurrentMark: public CHeapObj {
316 friend class ConcurrentMarkThread;
317 friend class CMTask;
318 friend class CMBitMapClosure;
319 friend class CSMarkOopClosure;
320 friend class CMGlobalObjectClosure;
321 friend class CMRemarkTask;
322 friend class CMConcurrentMarkingTask;
323 friend class G1ParNoteEndTask;
324 friend class CalcLiveObjectsClosure;
326 protected:
327 ConcurrentMarkThread* _cmThread; // the thread doing the work
328 G1CollectedHeap* _g1h; // the heap.
329 size_t _parallel_marking_threads; // the number of marking
330 // threads we'll use
331 double _sleep_factor; // how much we have to sleep, with
332 // respect to the work we just did, to
333 // meet the marking overhead goal
334 double _marking_task_overhead; // marking target overhead for
335 // a single task
337 // same as the two above, but for the cleanup task
338 double _cleanup_sleep_factor;
339 double _cleanup_task_overhead;
341 // Stuff related to age cohort processing.
342 struct ParCleanupThreadState {
343 char _pre[64];
344 UncleanRegionList list;
345 char _post[64];
346 };
347 ParCleanupThreadState** _par_cleanup_thread_state;
349 // CMS marking support structures
350 CMBitMap _markBitMap1;
351 CMBitMap _markBitMap2;
352 CMBitMapRO* _prevMarkBitMap; // completed mark bitmap
353 CMBitMap* _nextMarkBitMap; // under-construction mark bitmap
354 bool _at_least_one_mark_complete;
356 BitMap _region_bm;
357 BitMap _card_bm;
359 // Heap bounds
360 HeapWord* _heap_start;
361 HeapWord* _heap_end;
363 // For gray objects
364 CMMarkStack _markStack; // Grey objects behind global finger.
365 CMRegionStack _regionStack; // Grey regions behind global finger.
366 HeapWord* volatile _finger; // the global finger, region aligned,
367 // always points to the end of the
368 // last claimed region
370 // marking tasks
371 size_t _max_task_num; // maximum task number
372 size_t _active_tasks; // task num currently active
373 CMTask** _tasks; // task queue array (max_task_num len)
374 CMTaskQueueSet* _task_queues; // task queue set
375 ParallelTaskTerminator _terminator; // for termination
377 // Two sync barriers that are used to synchronise tasks when an
378 // overflow occurs. The algorithm is the following. All tasks enter
379 // the first one to ensure that they have all stopped manipulating
380 // the global data structures. After they exit it, they re-initialise
381 // their data structures and task 0 re-initialises the global data
382 // structures. Then, they enter the second sync barrier. This
383 // ensure, that no task starts doing work before all data
384 // structures (local and global) have been re-initialised. When they
385 // exit it, they are free to start working again.
386 WorkGangBarrierSync _first_overflow_barrier_sync;
387 WorkGangBarrierSync _second_overflow_barrier_sync;
390 // this is set by any task, when an overflow on the global data
391 // structures is detected.
392 volatile bool _has_overflown;
393 // true: marking is concurrent, false: we're in remark
394 volatile bool _concurrent;
395 // set at the end of a Full GC so that marking aborts
396 volatile bool _has_aborted;
397 // used when remark aborts due to an overflow to indicate that
398 // another concurrent marking phase should start
399 volatile bool _restart_for_overflow;
401 // This is true from the very start of concurrent marking until the
402 // point when all the tasks complete their work. It is really used
403 // to determine the points between the end of concurrent marking and
404 // time of remark.
405 volatile bool _concurrent_marking_in_progress;
407 // verbose level
408 CMVerboseLevel _verbose_level;
410 // These two fields are used to implement the optimisation that
411 // avoids pushing objects on the global/region stack if there are
412 // no collection set regions above the lowest finger.
414 // This is the lowest finger (among the global and local fingers),
415 // which is calculated before a new collection set is chosen.
416 HeapWord* _min_finger;
417 // If this flag is true, objects/regions that are marked below the
418 // finger should be pushed on the stack(s). If this is flag is
419 // false, it is safe not to push them on the stack(s).
420 bool _should_gray_objects;
422 // All of these times are in ms.
423 NumberSeq _init_times;
424 NumberSeq _remark_times;
425 NumberSeq _remark_mark_times;
426 NumberSeq _remark_weak_ref_times;
427 NumberSeq _cleanup_times;
428 double _total_counting_time;
429 double _total_rs_scrub_time;
431 double* _accum_task_vtime; // accumulated task vtime
433 WorkGang* _parallel_workers;
435 void weakRefsWork(bool clear_all_soft_refs);
437 void swapMarkBitMaps();
439 // It resets the global marking data structures, as well as the
440 // task local ones; should be called during initial mark.
441 void reset();
442 // It resets all the marking data structures.
443 void clear_marking_state();
445 // It should be called to indicate which phase we're in (concurrent
446 // mark or remark) and how many threads are currently active.
447 void set_phase(size_t active_tasks, bool concurrent);
448 // We do this after we're done with marking so that the marking data
449 // structures are initialised to a sensible and predictable state.
450 void set_non_marking_state();
452 // prints all gathered CM-related statistics
453 void print_stats();
455 // accessor methods
456 size_t parallel_marking_threads() { return _parallel_marking_threads; }
457 double sleep_factor() { return _sleep_factor; }
458 double marking_task_overhead() { return _marking_task_overhead;}
459 double cleanup_sleep_factor() { return _cleanup_sleep_factor; }
460 double cleanup_task_overhead() { return _cleanup_task_overhead;}
462 HeapWord* finger() { return _finger; }
463 bool concurrent() { return _concurrent; }
464 size_t active_tasks() { return _active_tasks; }
465 ParallelTaskTerminator* terminator() { return &_terminator; }
467 // It claims the next available region to be scanned by a marking
468 // task. It might return NULL if the next region is empty or we have
469 // run out of regions. In the latter case, out_of_regions()
470 // determines whether we've really run out of regions or the task
471 // should call claim_region() again. This might seem a bit
472 // awkward. Originally, the code was written so that claim_region()
473 // either successfully returned with a non-empty region or there
474 // were no more regions to be claimed. The problem with this was
475 // that, in certain circumstances, it iterated over large chunks of
476 // the heap finding only empty regions and, while it was working, it
477 // was preventing the calling task to call its regular clock
478 // method. So, this way, each task will spend very little time in
479 // claim_region() and is allowed to call the regular clock method
480 // frequently.
481 HeapRegion* claim_region(int task);
483 // It determines whether we've run out of regions to scan.
484 bool out_of_regions() { return _finger == _heap_end; }
486 // Returns the task with the given id
487 CMTask* task(int id) {
488 guarantee( 0 <= id && id < (int) _active_tasks, "task id not within "
489 "active bounds" );
490 return _tasks[id];
491 }
493 // Returns the task queue with the given id
494 CMTaskQueue* task_queue(int id) {
495 guarantee( 0 <= id && id < (int) _active_tasks, "task queue id not within "
496 "active bounds" );
497 return (CMTaskQueue*) _task_queues->queue(id);
498 }
500 // Returns the task queue set
501 CMTaskQueueSet* task_queues() { return _task_queues; }
503 // Access / manipulation of the overflow flag which is set to
504 // indicate that the global stack or region stack has overflown
505 bool has_overflown() { return _has_overflown; }
506 void set_has_overflown() { _has_overflown = true; }
507 void clear_has_overflown() { _has_overflown = false; }
509 bool has_aborted() { return _has_aborted; }
510 bool restart_for_overflow() { return _restart_for_overflow; }
512 // Methods to enter the two overflow sync barriers
513 void enter_first_sync_barrier(int task_num);
514 void enter_second_sync_barrier(int task_num);
516 public:
517 // Manipulation of the global mark stack.
518 // Notice that the first mark_stack_push is CAS-based, whereas the
519 // two below are Mutex-based. This is OK since the first one is only
520 // called during evacuation pauses and doesn't compete with the
521 // other two (which are called by the marking tasks during
522 // concurrent marking or remark).
523 bool mark_stack_push(oop p) {
524 _markStack.par_push(p);
525 if (_markStack.overflow()) {
526 set_has_overflown();
527 return false;
528 }
529 return true;
530 }
531 bool mark_stack_push(oop* arr, int n) {
532 _markStack.par_push_arr(arr, n);
533 if (_markStack.overflow()) {
534 set_has_overflown();
535 return false;
536 }
537 return true;
538 }
539 void mark_stack_pop(oop* arr, int max, int* n) {
540 _markStack.par_pop_arr(arr, max, n);
541 }
542 size_t mark_stack_size() { return _markStack.size(); }
543 size_t partial_mark_stack_size_target() { return _markStack.maxElems()/3; }
544 bool mark_stack_overflow() { return _markStack.overflow(); }
545 bool mark_stack_empty() { return _markStack.isEmpty(); }
547 // Manipulation of the region stack
548 bool region_stack_push(MemRegion mr) {
549 _regionStack.push(mr);
550 if (_regionStack.overflow()) {
551 set_has_overflown();
552 return false;
553 }
554 return true;
555 }
556 MemRegion region_stack_pop() { return _regionStack.pop(); }
557 int region_stack_size() { return _regionStack.size(); }
558 bool region_stack_overflow() { return _regionStack.overflow(); }
559 bool region_stack_empty() { return _regionStack.isEmpty(); }
561 bool concurrent_marking_in_progress() {
562 return _concurrent_marking_in_progress;
563 }
564 void set_concurrent_marking_in_progress() {
565 _concurrent_marking_in_progress = true;
566 }
567 void clear_concurrent_marking_in_progress() {
568 _concurrent_marking_in_progress = false;
569 }
571 void update_accum_task_vtime(int i, double vtime) {
572 _accum_task_vtime[i] += vtime;
573 }
575 double all_task_accum_vtime() {
576 double ret = 0.0;
577 for (int i = 0; i < (int)_max_task_num; ++i)
578 ret += _accum_task_vtime[i];
579 return ret;
580 }
582 // Attempts to steal an object from the task queues of other tasks
583 bool try_stealing(int task_num, int* hash_seed, oop& obj) {
584 return _task_queues->steal(task_num, hash_seed, obj);
585 }
587 // It grays an object by first marking it. Then, if it's behind the
588 // global finger, it also pushes it on the global stack.
589 void deal_with_reference(oop obj);
591 ConcurrentMark(ReservedSpace rs, int max_regions);
592 ~ConcurrentMark();
593 ConcurrentMarkThread* cmThread() { return _cmThread; }
595 CMBitMapRO* prevMarkBitMap() const { return _prevMarkBitMap; }
596 CMBitMap* nextMarkBitMap() const { return _nextMarkBitMap; }
598 // The following three are interaction between CM and
599 // G1CollectedHeap
601 // This notifies CM that a root during initial-mark needs to be
602 // grayed and it's MT-safe. Currently, we just mark it. But, in the
603 // future, we can experiment with pushing it on the stack and we can
604 // do this without changing G1CollectedHeap.
605 void grayRoot(oop p);
606 // It's used during evacuation pauses to gray a region, if
607 // necessary, and it's MT-safe. It assumes that the caller has
608 // marked any objects on that region. If _should_gray_objects is
609 // true and we're still doing concurrent marking, the region is
610 // pushed on the region stack, if it is located below the global
611 // finger, otherwise we do nothing.
612 void grayRegionIfNecessary(MemRegion mr);
613 // It's used during evacuation pauses to mark and, if necessary,
614 // gray a single object and it's MT-safe. It assumes the caller did
615 // not mark the object. If _should_gray_objects is true and we're
616 // still doing concurrent marking, the objects is pushed on the
617 // global stack, if it is located below the global finger, otherwise
618 // we do nothing.
619 void markAndGrayObjectIfNecessary(oop p);
621 // This iterates over the bitmap of the previous marking and prints
622 // out all objects that are marked on the bitmap and indicates
623 // whether what they point to is also marked or not.
624 void print_prev_bitmap_reachable();
626 // Clear the next marking bitmap (will be called concurrently).
627 void clearNextBitmap();
629 // main CMS steps and related support
630 void checkpointRootsInitial();
632 // These two do the work that needs to be done before and after the
633 // initial root checkpoint. Since this checkpoint can be done at two
634 // different points (i.e. an explicit pause or piggy-backed on a
635 // young collection), then it's nice to be able to easily share the
636 // pre/post code. It might be the case that we can put everything in
637 // the post method. TP
638 void checkpointRootsInitialPre();
639 void checkpointRootsInitialPost();
641 // Do concurrent phase of marking, to a tentative transitive closure.
642 void markFromRoots();
644 // Process all unprocessed SATB buffers. It is called at the
645 // beginning of an evacuation pause.
646 void drainAllSATBBuffers();
648 void checkpointRootsFinal(bool clear_all_soft_refs);
649 void checkpointRootsFinalWork();
650 void calcDesiredRegions();
651 void cleanup();
652 void completeCleanup();
654 // Mark in the previous bitmap. NB: this is usually read-only, so use
655 // this carefully!
656 void markPrev(oop p);
657 void clear(oop p);
658 // Clears marks for all objects in the given range, for both prev and
659 // next bitmaps. NB: the previous bitmap is usually read-only, so use
660 // this carefully!
661 void clearRangeBothMaps(MemRegion mr);
663 // Record the current top of the mark and region stacks; a
664 // subsequent oops_do() on the mark stack and
665 // invalidate_entries_into_cset() on the region stack will iterate
666 // only over indices valid at the time of this call.
667 void set_oops_do_bound() {
668 _markStack.set_oops_do_bound();
669 _regionStack.set_oops_do_bound();
670 }
671 // Iterate over the oops in the mark stack and all local queues. It
672 // also calls invalidate_entries_into_cset() on the region stack.
673 void oops_do(OopClosure* f);
674 // It is called at the end of an evacuation pause during marking so
675 // that CM is notified of where the new end of the heap is. It
676 // doesn't do anything if concurrent_marking_in_progress() is false,
677 // unless the force parameter is true.
678 void update_g1_committed(bool force = false);
680 void complete_marking_in_collection_set();
682 // It indicates that a new collection set is being chosen.
683 void newCSet();
684 // It registers a collection set heap region with CM. This is used
685 // to determine whether any heap regions are located above the finger.
686 void registerCSetRegion(HeapRegion* hr);
688 // Returns "true" if at least one mark has been completed.
689 bool at_least_one_mark_complete() { return _at_least_one_mark_complete; }
691 bool isMarked(oop p) const {
692 assert(p != NULL && p->is_oop(), "expected an oop");
693 HeapWord* addr = (HeapWord*)p;
694 assert(addr >= _nextMarkBitMap->startWord() ||
695 addr < _nextMarkBitMap->endWord(), "in a region");
697 return _nextMarkBitMap->isMarked(addr);
698 }
700 inline bool not_yet_marked(oop p) const;
702 // XXX Debug code
703 bool containing_card_is_marked(void* p);
704 bool containing_cards_are_marked(void* start, void* last);
706 bool isPrevMarked(oop p) const {
707 assert(p != NULL && p->is_oop(), "expected an oop");
708 HeapWord* addr = (HeapWord*)p;
709 assert(addr >= _prevMarkBitMap->startWord() ||
710 addr < _prevMarkBitMap->endWord(), "in a region");
712 return _prevMarkBitMap->isMarked(addr);
713 }
715 inline bool do_yield_check(int worker_i = 0);
716 inline bool should_yield();
718 // Called to abort the marking cycle after a Full GC takes palce.
719 void abort();
721 // This prints the global/local fingers. It is used for debugging.
722 NOT_PRODUCT(void print_finger();)
724 void print_summary_info();
726 // The following indicate whether a given verbose level has been
727 // set. Notice that anything above stats is conditional to
728 // _MARKING_VERBOSE_ having been set to 1
729 bool verbose_stats()
730 { return _verbose_level >= stats_verbose; }
731 bool verbose_low()
732 { return _MARKING_VERBOSE_ && _verbose_level >= low_verbose; }
733 bool verbose_medium()
734 { return _MARKING_VERBOSE_ && _verbose_level >= medium_verbose; }
735 bool verbose_high()
736 { return _MARKING_VERBOSE_ && _verbose_level >= high_verbose; }
737 };
739 // A class representing a marking task.
740 class CMTask : public TerminatorTerminator {
741 private:
742 enum PrivateConstants {
743 // the regular clock call is called once the scanned words reaches
744 // this limit
745 words_scanned_period = 12*1024,
746 // the regular clock call is called once the number of visited
747 // references reaches this limit
748 refs_reached_period = 384,
749 // initial value for the hash seed, used in the work stealing code
750 init_hash_seed = 17,
751 // how many entries will be transferred between global stack and
752 // local queues
753 global_stack_transfer_size = 16
754 };
756 int _task_id;
757 G1CollectedHeap* _g1h;
758 ConcurrentMark* _cm;
759 CMBitMap* _nextMarkBitMap;
760 // the task queue of this task
761 CMTaskQueue* _task_queue;
762 private:
763 // the task queue set---needed for stealing
764 CMTaskQueueSet* _task_queues;
765 // indicates whether the task has been claimed---this is only for
766 // debugging purposes
767 bool _claimed;
769 // number of calls to this task
770 int _calls;
772 // when the virtual timer reaches this time, the marking step should
773 // exit
774 double _time_target_ms;
775 // the start time of the current marking step
776 double _start_time_ms;
778 // the oop closure used for iterations over oops
779 OopClosure* _oop_closure;
781 // the region this task is scanning, NULL if we're not scanning any
782 HeapRegion* _curr_region;
783 // the local finger of this task, NULL if we're not scanning a region
784 HeapWord* _finger;
785 // limit of the region this task is scanning, NULL if we're not scanning one
786 HeapWord* _region_limit;
788 // This is used only when we scan regions popped from the region
789 // stack. It records what the last object on such a region we
790 // scanned was. It is used to ensure that, if we abort region
791 // iteration, we do not rescan the first part of the region. This
792 // should be NULL when we're not scanning a region from the region
793 // stack.
794 HeapWord* _region_finger;
796 // the number of words this task has scanned
797 size_t _words_scanned;
798 // When _words_scanned reaches this limit, the regular clock is
799 // called. Notice that this might be decreased under certain
800 // circumstances (i.e. when we believe that we did an expensive
801 // operation).
802 size_t _words_scanned_limit;
803 // the initial value of _words_scanned_limit (i.e. what it was
804 // before it was decreased).
805 size_t _real_words_scanned_limit;
807 // the number of references this task has visited
808 size_t _refs_reached;
809 // When _refs_reached reaches this limit, the regular clock is
810 // called. Notice this this might be decreased under certain
811 // circumstances (i.e. when we believe that we did an expensive
812 // operation).
813 size_t _refs_reached_limit;
814 // the initial value of _refs_reached_limit (i.e. what it was before
815 // it was decreased).
816 size_t _real_refs_reached_limit;
818 // used by the work stealing stuff
819 int _hash_seed;
820 // if this is true, then the task has aborted for some reason
821 bool _has_aborted;
822 // set when the task aborts because it has met its time quota
823 bool _has_aborted_timed_out;
824 // true when we're draining SATB buffers; this avoids the task
825 // aborting due to SATB buffers being available (as we're already
826 // dealing with them)
827 bool _draining_satb_buffers;
829 // number sequence of past step times
830 NumberSeq _step_times_ms;
831 // elapsed time of this task
832 double _elapsed_time_ms;
833 // termination time of this task
834 double _termination_time_ms;
835 // when this task got into the termination protocol
836 double _termination_start_time_ms;
838 // true when the task is during a concurrent phase, false when it is
839 // in the remark phase (so, in the latter case, we do not have to
840 // check all the things that we have to check during the concurrent
841 // phase, i.e. SATB buffer availability...)
842 bool _concurrent;
844 TruncatedSeq _marking_step_diffs_ms;
846 // LOTS of statistics related with this task
847 #if _MARKING_STATS_
848 NumberSeq _all_clock_intervals_ms;
849 double _interval_start_time_ms;
851 int _aborted;
852 int _aborted_overflow;
853 int _aborted_cm_aborted;
854 int _aborted_yield;
855 int _aborted_timed_out;
856 int _aborted_satb;
857 int _aborted_termination;
859 int _steal_attempts;
860 int _steals;
862 int _clock_due_to_marking;
863 int _clock_due_to_scanning;
865 int _local_pushes;
866 int _local_pops;
867 int _local_max_size;
868 int _objs_scanned;
870 int _global_pushes;
871 int _global_pops;
872 int _global_max_size;
874 int _global_transfers_to;
875 int _global_transfers_from;
877 int _region_stack_pops;
879 int _regions_claimed;
880 int _objs_found_on_bitmap;
882 int _satb_buffers_processed;
883 #endif // _MARKING_STATS_
885 // it updates the local fields after this task has claimed
886 // a new region to scan
887 void setup_for_region(HeapRegion* hr);
888 // it brings up-to-date the limit of the region
889 void update_region_limit();
890 // it resets the local fields after a task has finished scanning a
891 // region
892 void giveup_current_region();
894 // called when either the words scanned or the refs visited limit
895 // has been reached
896 void reached_limit();
897 // recalculates the words scanned and refs visited limits
898 void recalculate_limits();
899 // decreases the words scanned and refs visited limits when we reach
900 // an expensive operation
901 void decrease_limits();
902 // it checks whether the words scanned or refs visited reached their
903 // respective limit and calls reached_limit() if they have
904 void check_limits() {
905 if (_words_scanned >= _words_scanned_limit ||
906 _refs_reached >= _refs_reached_limit)
907 reached_limit();
908 }
909 // this is supposed to be called regularly during a marking step as
910 // it checks a bunch of conditions that might cause the marking step
911 // to abort
912 void regular_clock_call();
913 bool concurrent() { return _concurrent; }
915 public:
916 // It resets the task; it should be called right at the beginning of
917 // a marking phase.
918 void reset(CMBitMap* _nextMarkBitMap);
919 // it clears all the fields that correspond to a claimed region.
920 void clear_region_fields();
922 void set_concurrent(bool concurrent) { _concurrent = concurrent; }
924 // The main method of this class which performs a marking step
925 // trying not to exceed the given duration. However, it might exit
926 // prematurely, according to some conditions (i.e. SATB buffers are
927 // available for processing).
928 void do_marking_step(double target_ms);
930 // These two calls start and stop the timer
931 void record_start_time() {
932 _elapsed_time_ms = os::elapsedTime() * 1000.0;
933 }
934 void record_end_time() {
935 _elapsed_time_ms = os::elapsedTime() * 1000.0 - _elapsed_time_ms;
936 }
938 // returns the task ID
939 int task_id() { return _task_id; }
941 // From TerminatorTerminator. It determines whether this task should
942 // exit the termination protocol after it's entered it.
943 virtual bool should_exit_termination();
945 HeapWord* finger() { return _finger; }
947 bool has_aborted() { return _has_aborted; }
948 void set_has_aborted() { _has_aborted = true; }
949 void clear_has_aborted() { _has_aborted = false; }
950 bool claimed() { return _claimed; }
952 void set_oop_closure(OopClosure* oop_closure) {
953 _oop_closure = oop_closure;
954 }
956 // It grays the object by marking it and, if necessary, pushing it
957 // on the local queue
958 void deal_with_reference(oop obj);
960 // It scans an object and visits its children.
961 void scan_object(oop obj) {
962 tmp_guarantee_CM( _nextMarkBitMap->isMarked((HeapWord*) obj),
963 "invariant" );
965 if (_cm->verbose_high())
966 gclog_or_tty->print_cr("[%d] we're scanning object "PTR_FORMAT,
967 _task_id, (void*) obj);
969 size_t obj_size = obj->size();
970 _words_scanned += obj_size;
972 obj->oop_iterate(_oop_closure);
973 statsOnly( ++_objs_scanned );
974 check_limits();
975 }
977 // It pushes an object on the local queue.
978 void push(oop obj);
980 // These two move entries to/from the global stack.
981 void move_entries_to_global_stack();
982 void get_entries_from_global_stack();
984 // It pops and scans objects from the local queue. If partially is
985 // true, then it stops when the queue size is of a given limit. If
986 // partially is false, then it stops when the queue is empty.
987 void drain_local_queue(bool partially);
988 // It moves entries from the global stack to the local queue and
989 // drains the local queue. If partially is true, then it stops when
990 // both the global stack and the local queue reach a given size. If
991 // partially if false, it tries to empty them totally.
992 void drain_global_stack(bool partially);
993 // It keeps picking SATB buffers and processing them until no SATB
994 // buffers are available.
995 void drain_satb_buffers();
996 // It keeps popping regions from the region stack and processing
997 // them until the region stack is empty.
998 void drain_region_stack(BitMapClosure* closure);
1000 // moves the local finger to a new location
1001 inline void move_finger_to(HeapWord* new_finger) {
1002 tmp_guarantee_CM( new_finger >= _finger && new_finger < _region_limit,
1003 "invariant" );
1004 _finger = new_finger;
1005 }
1007 // moves the region finger to a new location
1008 inline void move_region_finger_to(HeapWord* new_finger) {
1009 tmp_guarantee_CM( new_finger < _cm->finger(), "invariant" );
1010 _region_finger = new_finger;
1011 }
1013 CMTask(int task_num, ConcurrentMark *cm,
1014 CMTaskQueue* task_queue, CMTaskQueueSet* task_queues);
1016 // it prints statistics associated with this task
1017 void print_stats();
1019 #if _MARKING_STATS_
1020 void increase_objs_found_on_bitmap() { ++_objs_found_on_bitmap; }
1021 #endif // _MARKING_STATS_
1022 };