Mon, 09 Aug 2010 05:41:05 -0700
6966222: G1: simplify TaskQueue overflow handling
Reviewed-by: tonyp, ysr
1 /*
2 * Copyright (c) 2001, 2010, 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 */
25 // A "G1CollectedHeap" is an implementation of a java heap for HotSpot.
26 // It uses the "Garbage First" heap organization and algorithm, which
27 // may combine concurrent marking with parallel, incremental compaction of
28 // heap subsets that will yield large amounts of garbage.
30 class HeapRegion;
31 class HeapRegionSeq;
32 class PermanentGenerationSpec;
33 class GenerationSpec;
34 class OopsInHeapRegionClosure;
35 class G1ScanHeapEvacClosure;
36 class ObjectClosure;
37 class SpaceClosure;
38 class CompactibleSpaceClosure;
39 class Space;
40 class G1CollectorPolicy;
41 class GenRemSet;
42 class G1RemSet;
43 class HeapRegionRemSetIterator;
44 class ConcurrentMark;
45 class ConcurrentMarkThread;
46 class ConcurrentG1Refine;
47 class ConcurrentZFThread;
49 typedef OverflowTaskQueue<StarTask> RefToScanQueue;
50 typedef GenericTaskQueueSet<RefToScanQueue> RefToScanQueueSet;
52 typedef int RegionIdx_t; // needs to hold [ 0..max_regions() )
53 typedef int CardIdx_t; // needs to hold [ 0..CardsPerRegion )
55 enum G1GCThreadGroups {
56 G1CRGroup = 0,
57 G1ZFGroup = 1,
58 G1CMGroup = 2,
59 G1CLGroup = 3
60 };
62 enum GCAllocPurpose {
63 GCAllocForTenured,
64 GCAllocForSurvived,
65 GCAllocPurposeCount
66 };
68 class YoungList : public CHeapObj {
69 private:
70 G1CollectedHeap* _g1h;
72 HeapRegion* _head;
74 HeapRegion* _survivor_head;
75 HeapRegion* _survivor_tail;
77 HeapRegion* _curr;
79 size_t _length;
80 size_t _survivor_length;
82 size_t _last_sampled_rs_lengths;
83 size_t _sampled_rs_lengths;
85 void empty_list(HeapRegion* list);
87 public:
88 YoungList(G1CollectedHeap* g1h);
90 void push_region(HeapRegion* hr);
91 void add_survivor_region(HeapRegion* hr);
93 void empty_list();
94 bool is_empty() { return _length == 0; }
95 size_t length() { return _length; }
96 size_t survivor_length() { return _survivor_length; }
98 void rs_length_sampling_init();
99 bool rs_length_sampling_more();
100 void rs_length_sampling_next();
102 void reset_sampled_info() {
103 _last_sampled_rs_lengths = 0;
104 }
105 size_t sampled_rs_lengths() { return _last_sampled_rs_lengths; }
107 // for development purposes
108 void reset_auxilary_lists();
109 void clear() { _head = NULL; _length = 0; }
111 void clear_survivors() {
112 _survivor_head = NULL;
113 _survivor_tail = NULL;
114 _survivor_length = 0;
115 }
117 HeapRegion* first_region() { return _head; }
118 HeapRegion* first_survivor_region() { return _survivor_head; }
119 HeapRegion* last_survivor_region() { return _survivor_tail; }
121 // debugging
122 bool check_list_well_formed();
123 bool check_list_empty(bool check_sample = true);
124 void print();
125 };
127 class RefineCardTableEntryClosure;
128 class G1CollectedHeap : public SharedHeap {
129 friend class VM_G1CollectForAllocation;
130 friend class VM_GenCollectForPermanentAllocation;
131 friend class VM_G1CollectFull;
132 friend class VM_G1IncCollectionPause;
133 friend class VMStructs;
135 // Closures used in implementation.
136 friend class G1ParCopyHelper;
137 friend class G1IsAliveClosure;
138 friend class G1EvacuateFollowersClosure;
139 friend class G1ParScanThreadState;
140 friend class G1ParScanClosureSuper;
141 friend class G1ParEvacuateFollowersClosure;
142 friend class G1ParTask;
143 friend class G1FreeGarbageRegionClosure;
144 friend class RefineCardTableEntryClosure;
145 friend class G1PrepareCompactClosure;
146 friend class RegionSorter;
147 friend class CountRCClosure;
148 friend class EvacPopObjClosure;
149 friend class G1ParCleanupCTTask;
151 // Other related classes.
152 friend class G1MarkSweep;
154 private:
155 // The one and only G1CollectedHeap, so static functions can find it.
156 static G1CollectedHeap* _g1h;
158 static size_t _humongous_object_threshold_in_words;
160 // Storage for the G1 heap (excludes the permanent generation).
161 VirtualSpace _g1_storage;
162 MemRegion _g1_reserved;
164 // The part of _g1_storage that is currently committed.
165 MemRegion _g1_committed;
167 // The maximum part of _g1_storage that has ever been committed.
168 MemRegion _g1_max_committed;
170 // The number of regions that are completely free.
171 size_t _free_regions;
173 // The number of regions we could create by expansion.
174 size_t _expansion_regions;
176 // Return the number of free regions in the heap (by direct counting.)
177 size_t count_free_regions();
178 // Return the number of free regions on the free and unclean lists.
179 size_t count_free_regions_list();
181 // The block offset table for the G1 heap.
182 G1BlockOffsetSharedArray* _bot_shared;
184 // Move all of the regions off the free lists, then rebuild those free
185 // lists, before and after full GC.
186 void tear_down_region_lists();
187 void rebuild_region_lists();
188 // This sets all non-empty regions to need zero-fill (which they will if
189 // they are empty after full collection.)
190 void set_used_regions_to_need_zero_fill();
192 // The sequence of all heap regions in the heap.
193 HeapRegionSeq* _hrs;
195 // The region from which normal-sized objects are currently being
196 // allocated. May be NULL.
197 HeapRegion* _cur_alloc_region;
199 // Postcondition: cur_alloc_region == NULL.
200 void abandon_cur_alloc_region();
201 void abandon_gc_alloc_regions();
203 // The to-space memory regions into which objects are being copied during
204 // a GC.
205 HeapRegion* _gc_alloc_regions[GCAllocPurposeCount];
206 size_t _gc_alloc_region_counts[GCAllocPurposeCount];
207 // These are the regions, one per GCAllocPurpose, that are half-full
208 // at the end of a collection and that we want to reuse during the
209 // next collection.
210 HeapRegion* _retained_gc_alloc_regions[GCAllocPurposeCount];
211 // This specifies whether we will keep the last half-full region at
212 // the end of a collection so that it can be reused during the next
213 // collection (this is specified per GCAllocPurpose)
214 bool _retain_gc_alloc_region[GCAllocPurposeCount];
216 // A list of the regions that have been set to be alloc regions in the
217 // current collection.
218 HeapRegion* _gc_alloc_region_list;
220 // Determines PLAB size for a particular allocation purpose.
221 static size_t desired_plab_sz(GCAllocPurpose purpose);
223 // When called by par thread, require par_alloc_during_gc_lock() to be held.
224 void push_gc_alloc_region(HeapRegion* hr);
226 // This should only be called single-threaded. Undeclares all GC alloc
227 // regions.
228 void forget_alloc_region_list();
230 // Should be used to set an alloc region, because there's other
231 // associated bookkeeping.
232 void set_gc_alloc_region(int purpose, HeapRegion* r);
234 // Check well-formedness of alloc region list.
235 bool check_gc_alloc_regions();
237 // Outside of GC pauses, the number of bytes used in all regions other
238 // than the current allocation region.
239 size_t _summary_bytes_used;
241 // This is used for a quick test on whether a reference points into
242 // the collection set or not. Basically, we have an array, with one
243 // byte per region, and that byte denotes whether the corresponding
244 // region is in the collection set or not. The entry corresponding
245 // the bottom of the heap, i.e., region 0, is pointed to by
246 // _in_cset_fast_test_base. The _in_cset_fast_test field has been
247 // biased so that it actually points to address 0 of the address
248 // space, to make the test as fast as possible (we can simply shift
249 // the address to address into it, instead of having to subtract the
250 // bottom of the heap from the address before shifting it; basically
251 // it works in the same way the card table works).
252 bool* _in_cset_fast_test;
254 // The allocated array used for the fast test on whether a reference
255 // points into the collection set or not. This field is also used to
256 // free the array.
257 bool* _in_cset_fast_test_base;
259 // The length of the _in_cset_fast_test_base array.
260 size_t _in_cset_fast_test_length;
262 volatile unsigned _gc_time_stamp;
264 size_t* _surviving_young_words;
266 void setup_surviving_young_words();
267 void update_surviving_young_words(size_t* surv_young_words);
268 void cleanup_surviving_young_words();
270 // It decides whether an explicit GC should start a concurrent cycle
271 // instead of doing a STW GC. Currently, a concurrent cycle is
272 // explicitly started if:
273 // (a) cause == _gc_locker and +GCLockerInvokesConcurrent, or
274 // (b) cause == _java_lang_system_gc and +ExplicitGCInvokesConcurrent.
275 bool should_do_concurrent_full_gc(GCCause::Cause cause);
277 // Keeps track of how many "full collections" (i.e., Full GCs or
278 // concurrent cycles) we have completed. The number of them we have
279 // started is maintained in _total_full_collections in CollectedHeap.
280 volatile unsigned int _full_collections_completed;
282 protected:
284 // Returns "true" iff none of the gc alloc regions have any allocations
285 // since the last call to "save_marks".
286 bool all_alloc_regions_no_allocs_since_save_marks();
287 // Perform finalization stuff on all allocation regions.
288 void retire_all_alloc_regions();
290 // The number of regions allocated to hold humongous objects.
291 int _num_humongous_regions;
292 YoungList* _young_list;
294 // The current policy object for the collector.
295 G1CollectorPolicy* _g1_policy;
297 // Parallel allocation lock to protect the current allocation region.
298 Mutex _par_alloc_during_gc_lock;
299 Mutex* par_alloc_during_gc_lock() { return &_par_alloc_during_gc_lock; }
301 // If possible/desirable, allocate a new HeapRegion for normal object
302 // allocation sufficient for an allocation of the given "word_size".
303 // If "do_expand" is true, will attempt to expand the heap if necessary
304 // to to satisfy the request. If "zero_filled" is true, requires a
305 // zero-filled region.
306 // (Returning NULL will trigger a GC.)
307 virtual HeapRegion* newAllocRegion_work(size_t word_size,
308 bool do_expand,
309 bool zero_filled);
311 virtual HeapRegion* newAllocRegion(size_t word_size,
312 bool zero_filled = true) {
313 return newAllocRegion_work(word_size, false, zero_filled);
314 }
315 virtual HeapRegion* newAllocRegionWithExpansion(int purpose,
316 size_t word_size,
317 bool zero_filled = true);
319 // Attempt to allocate an object of the given (very large) "word_size".
320 // Returns "NULL" on failure.
321 virtual HeapWord* humongousObjAllocate(size_t word_size);
323 // If possible, allocate a block of the given word_size, else return "NULL".
324 // Returning NULL will trigger GC or heap expansion.
325 // These two methods have rather awkward pre- and
326 // post-conditions. If they are called outside a safepoint, then
327 // they assume that the caller is holding the heap lock. Upon return
328 // they release the heap lock, if they are returning a non-NULL
329 // value. attempt_allocation_slow() also dirties the cards of a
330 // newly-allocated young region after it releases the heap
331 // lock. This change in interface was the neatest way to achieve
332 // this card dirtying without affecting mem_allocate(), which is a
333 // more frequently called method. We tried two or three different
334 // approaches, but they were even more hacky.
335 HeapWord* attempt_allocation(size_t word_size,
336 bool permit_collection_pause = true);
338 HeapWord* attempt_allocation_slow(size_t word_size,
339 bool permit_collection_pause = true);
341 // Allocate blocks during garbage collection. Will ensure an
342 // allocation region, either by picking one or expanding the
343 // heap, and then allocate a block of the given size. The block
344 // may not be a humongous - it must fit into a single heap region.
345 HeapWord* allocate_during_gc(GCAllocPurpose purpose, size_t word_size);
346 HeapWord* par_allocate_during_gc(GCAllocPurpose purpose, size_t word_size);
348 HeapWord* allocate_during_gc_slow(GCAllocPurpose purpose,
349 HeapRegion* alloc_region,
350 bool par,
351 size_t word_size);
353 // Ensure that no further allocations can happen in "r", bearing in mind
354 // that parallel threads might be attempting allocations.
355 void par_allocate_remaining_space(HeapRegion* r);
357 // Retires an allocation region when it is full or at the end of a
358 // GC pause.
359 void retire_alloc_region(HeapRegion* alloc_region, bool par);
361 // - if explicit_gc is true, the GC is for a System.gc() or a heap
362 // inspection request and should collect the entire heap
363 // - if clear_all_soft_refs is true, all soft references are cleared
364 // during the GC
365 // - if explicit_gc is false, word_size describes the allocation that
366 // the GC should attempt (at least) to satisfy
367 void do_collection(bool explicit_gc,
368 bool clear_all_soft_refs,
369 size_t word_size);
371 // Callback from VM_G1CollectFull operation.
372 // Perform a full collection.
373 void do_full_collection(bool clear_all_soft_refs);
375 // Resize the heap if necessary after a full collection. If this is
376 // after a collect-for allocation, "word_size" is the allocation size,
377 // and will be considered part of the used portion of the heap.
378 void resize_if_necessary_after_full_collection(size_t word_size);
380 // Callback from VM_G1CollectForAllocation operation.
381 // This function does everything necessary/possible to satisfy a
382 // failed allocation request (including collection, expansion, etc.)
383 HeapWord* satisfy_failed_allocation(size_t word_size);
385 // Attempting to expand the heap sufficiently
386 // to support an allocation of the given "word_size". If
387 // successful, perform the allocation and return the address of the
388 // allocated block, or else "NULL".
389 virtual HeapWord* expand_and_allocate(size_t word_size);
391 public:
392 // Expand the garbage-first heap by at least the given size (in bytes!).
393 // (Rounds up to a HeapRegion boundary.)
394 virtual void expand(size_t expand_bytes);
396 // Do anything common to GC's.
397 virtual void gc_prologue(bool full);
398 virtual void gc_epilogue(bool full);
400 // We register a region with the fast "in collection set" test. We
401 // simply set to true the array slot corresponding to this region.
402 void register_region_with_in_cset_fast_test(HeapRegion* r) {
403 assert(_in_cset_fast_test_base != NULL, "sanity");
404 assert(r->in_collection_set(), "invariant");
405 int index = r->hrs_index();
406 assert(0 <= index && (size_t) index < _in_cset_fast_test_length, "invariant");
407 assert(!_in_cset_fast_test_base[index], "invariant");
408 _in_cset_fast_test_base[index] = true;
409 }
411 // This is a fast test on whether a reference points into the
412 // collection set or not. It does not assume that the reference
413 // points into the heap; if it doesn't, it will return false.
414 bool in_cset_fast_test(oop obj) {
415 assert(_in_cset_fast_test != NULL, "sanity");
416 if (_g1_committed.contains((HeapWord*) obj)) {
417 // no need to subtract the bottom of the heap from obj,
418 // _in_cset_fast_test is biased
419 size_t index = ((size_t) obj) >> HeapRegion::LogOfHRGrainBytes;
420 bool ret = _in_cset_fast_test[index];
421 // let's make sure the result is consistent with what the slower
422 // test returns
423 assert( ret || !obj_in_cs(obj), "sanity");
424 assert(!ret || obj_in_cs(obj), "sanity");
425 return ret;
426 } else {
427 return false;
428 }
429 }
431 void clear_cset_fast_test() {
432 assert(_in_cset_fast_test_base != NULL, "sanity");
433 memset(_in_cset_fast_test_base, false,
434 _in_cset_fast_test_length * sizeof(bool));
435 }
437 // This is called at the end of either a concurrent cycle or a Full
438 // GC to update the number of full collections completed. Those two
439 // can happen in a nested fashion, i.e., we start a concurrent
440 // cycle, a Full GC happens half-way through it which ends first,
441 // and then the cycle notices that a Full GC happened and ends
442 // too. The outer parameter is a boolean to help us do a bit tighter
443 // consistency checking in the method. If outer is false, the caller
444 // is the inner caller in the nesting (i.e., the Full GC). If outer
445 // is true, the caller is the outer caller in this nesting (i.e.,
446 // the concurrent cycle). Further nesting is not currently
447 // supported. The end of the this call also notifies the
448 // FullGCCount_lock in case a Java thread is waiting for a full GC
449 // to happen (e.g., it called System.gc() with
450 // +ExplicitGCInvokesConcurrent).
451 void increment_full_collections_completed(bool outer);
453 unsigned int full_collections_completed() {
454 return _full_collections_completed;
455 }
457 protected:
459 // Shrink the garbage-first heap by at most the given size (in bytes!).
460 // (Rounds down to a HeapRegion boundary.)
461 virtual void shrink(size_t expand_bytes);
462 void shrink_helper(size_t expand_bytes);
464 #if TASKQUEUE_STATS
465 static void print_taskqueue_stats_hdr(outputStream* const st = gclog_or_tty);
466 void print_taskqueue_stats(outputStream* const st = gclog_or_tty) const;
467 void reset_taskqueue_stats();
468 #endif // TASKQUEUE_STATS
470 // Do an incremental collection: identify a collection set, and evacuate
471 // its live objects elsewhere.
472 virtual void do_collection_pause();
474 // The guts of the incremental collection pause, executed by the vm
475 // thread.
476 virtual void do_collection_pause_at_safepoint(double target_pause_time_ms);
478 // Actually do the work of evacuating the collection set.
479 virtual void evacuate_collection_set();
481 // If this is an appropriate right time, do a collection pause.
482 // The "word_size" argument, if non-zero, indicates the size of an
483 // allocation request that is prompting this query.
484 void do_collection_pause_if_appropriate(size_t word_size);
486 // The g1 remembered set of the heap.
487 G1RemSet* _g1_rem_set;
488 // And it's mod ref barrier set, used to track updates for the above.
489 ModRefBarrierSet* _mr_bs;
491 // A set of cards that cover the objects for which the Rsets should be updated
492 // concurrently after the collection.
493 DirtyCardQueueSet _dirty_card_queue_set;
495 // The Heap Region Rem Set Iterator.
496 HeapRegionRemSetIterator** _rem_set_iterator;
498 // The closure used to refine a single card.
499 RefineCardTableEntryClosure* _refine_cte_cl;
501 // A function to check the consistency of dirty card logs.
502 void check_ct_logs_at_safepoint();
504 // A DirtyCardQueueSet that is used to hold cards that contain
505 // references into the current collection set. This is used to
506 // update the remembered sets of the regions in the collection
507 // set in the event of an evacuation failure.
508 DirtyCardQueueSet _into_cset_dirty_card_queue_set;
510 // After a collection pause, make the regions in the CS into free
511 // regions.
512 void free_collection_set(HeapRegion* cs_head);
514 // Abandon the current collection set without recording policy
515 // statistics or updating free lists.
516 void abandon_collection_set(HeapRegion* cs_head);
518 // Applies "scan_non_heap_roots" to roots outside the heap,
519 // "scan_rs" to roots inside the heap (having done "set_region" to
520 // indicate the region in which the root resides), and does "scan_perm"
521 // (setting the generation to the perm generation.) If "scan_rs" is
522 // NULL, then this step is skipped. The "worker_i"
523 // param is for use with parallel roots processing, and should be
524 // the "i" of the calling parallel worker thread's work(i) function.
525 // In the sequential case this param will be ignored.
526 void g1_process_strong_roots(bool collecting_perm_gen,
527 SharedHeap::ScanningOption so,
528 OopClosure* scan_non_heap_roots,
529 OopsInHeapRegionClosure* scan_rs,
530 OopsInGenClosure* scan_perm,
531 int worker_i);
533 // Apply "blk" to all the weak roots of the system. These include
534 // JNI weak roots, the code cache, system dictionary, symbol table,
535 // string table, and referents of reachable weak refs.
536 void g1_process_weak_roots(OopClosure* root_closure,
537 OopClosure* non_root_closure);
539 // Invoke "save_marks" on all heap regions.
540 void save_marks();
542 // Free a heap region.
543 void free_region(HeapRegion* hr);
544 // A component of "free_region", exposed for 'batching'.
545 // All the params after "hr" are out params: the used bytes of the freed
546 // region(s), the number of H regions cleared, the number of regions
547 // freed, and pointers to the head and tail of a list of freed contig
548 // regions, linked throught the "next_on_unclean_list" field.
549 void free_region_work(HeapRegion* hr,
550 size_t& pre_used,
551 size_t& cleared_h,
552 size_t& freed_regions,
553 UncleanRegionList* list,
554 bool par = false);
557 // The concurrent marker (and the thread it runs in.)
558 ConcurrentMark* _cm;
559 ConcurrentMarkThread* _cmThread;
560 bool _mark_in_progress;
562 // The concurrent refiner.
563 ConcurrentG1Refine* _cg1r;
565 // The concurrent zero-fill thread.
566 ConcurrentZFThread* _czft;
568 // The parallel task queues
569 RefToScanQueueSet *_task_queues;
571 // True iff a evacuation has failed in the current collection.
572 bool _evacuation_failed;
574 // Set the attribute indicating whether evacuation has failed in the
575 // current collection.
576 void set_evacuation_failed(bool b) { _evacuation_failed = b; }
578 // Failed evacuations cause some logical from-space objects to have
579 // forwarding pointers to themselves. Reset them.
580 void remove_self_forwarding_pointers();
582 // When one is non-null, so is the other. Together, they each pair is
583 // an object with a preserved mark, and its mark value.
584 GrowableArray<oop>* _objs_with_preserved_marks;
585 GrowableArray<markOop>* _preserved_marks_of_objs;
587 // Preserve the mark of "obj", if necessary, in preparation for its mark
588 // word being overwritten with a self-forwarding-pointer.
589 void preserve_mark_if_necessary(oop obj, markOop m);
591 // The stack of evac-failure objects left to be scanned.
592 GrowableArray<oop>* _evac_failure_scan_stack;
593 // The closure to apply to evac-failure objects.
595 OopsInHeapRegionClosure* _evac_failure_closure;
596 // Set the field above.
597 void
598 set_evac_failure_closure(OopsInHeapRegionClosure* evac_failure_closure) {
599 _evac_failure_closure = evac_failure_closure;
600 }
602 // Push "obj" on the scan stack.
603 void push_on_evac_failure_scan_stack(oop obj);
604 // Process scan stack entries until the stack is empty.
605 void drain_evac_failure_scan_stack();
606 // True iff an invocation of "drain_scan_stack" is in progress; to
607 // prevent unnecessary recursion.
608 bool _drain_in_progress;
610 // Do any necessary initialization for evacuation-failure handling.
611 // "cl" is the closure that will be used to process evac-failure
612 // objects.
613 void init_for_evac_failure(OopsInHeapRegionClosure* cl);
614 // Do any necessary cleanup for evacuation-failure handling data
615 // structures.
616 void finalize_for_evac_failure();
618 // An attempt to evacuate "obj" has failed; take necessary steps.
619 void handle_evacuation_failure(oop obj);
620 oop handle_evacuation_failure_par(OopsInHeapRegionClosure* cl, oop obj);
621 void handle_evacuation_failure_common(oop obj, markOop m);
624 // Ensure that the relevant gc_alloc regions are set.
625 void get_gc_alloc_regions();
626 // We're done with GC alloc regions. We are going to tear down the
627 // gc alloc list and remove the gc alloc tag from all the regions on
628 // that list. However, we will also retain the last (i.e., the one
629 // that is half-full) GC alloc region, per GCAllocPurpose, for
630 // possible reuse during the next collection, provided
631 // _retain_gc_alloc_region[] indicates that it should be the
632 // case. Said regions are kept in the _retained_gc_alloc_regions[]
633 // array. If the parameter totally is set, we will not retain any
634 // regions, irrespective of what _retain_gc_alloc_region[]
635 // indicates.
636 void release_gc_alloc_regions(bool totally);
637 #ifndef PRODUCT
638 // Useful for debugging.
639 void print_gc_alloc_regions();
640 #endif // !PRODUCT
642 // ("Weak") Reference processing support
643 ReferenceProcessor* _ref_processor;
645 enum G1H_process_strong_roots_tasks {
646 G1H_PS_mark_stack_oops_do,
647 G1H_PS_refProcessor_oops_do,
648 // Leave this one last.
649 G1H_PS_NumElements
650 };
652 SubTasksDone* _process_strong_tasks;
654 // List of regions which require zero filling.
655 UncleanRegionList _unclean_region_list;
656 bool _unclean_regions_coming;
658 public:
659 void set_refine_cte_cl_concurrency(bool concurrent);
661 RefToScanQueue *task_queue(int i) const;
663 // A set of cards where updates happened during the GC
664 DirtyCardQueueSet& dirty_card_queue_set() { return _dirty_card_queue_set; }
666 // A DirtyCardQueueSet that is used to hold cards that contain
667 // references into the current collection set. This is used to
668 // update the remembered sets of the regions in the collection
669 // set in the event of an evacuation failure.
670 DirtyCardQueueSet& into_cset_dirty_card_queue_set()
671 { return _into_cset_dirty_card_queue_set; }
673 // Create a G1CollectedHeap with the specified policy.
674 // Must call the initialize method afterwards.
675 // May not return if something goes wrong.
676 G1CollectedHeap(G1CollectorPolicy* policy);
678 // Initialize the G1CollectedHeap to have the initial and
679 // maximum sizes, permanent generation, and remembered and barrier sets
680 // specified by the policy object.
681 jint initialize();
683 void ref_processing_init();
685 void set_par_threads(int t) {
686 SharedHeap::set_par_threads(t);
687 _process_strong_tasks->set_par_threads(t);
688 }
690 virtual CollectedHeap::Name kind() const {
691 return CollectedHeap::G1CollectedHeap;
692 }
694 // The current policy object for the collector.
695 G1CollectorPolicy* g1_policy() const { return _g1_policy; }
697 // Adaptive size policy. No such thing for g1.
698 virtual AdaptiveSizePolicy* size_policy() { return NULL; }
700 // The rem set and barrier set.
701 G1RemSet* g1_rem_set() const { return _g1_rem_set; }
702 ModRefBarrierSet* mr_bs() const { return _mr_bs; }
704 // The rem set iterator.
705 HeapRegionRemSetIterator* rem_set_iterator(int i) {
706 return _rem_set_iterator[i];
707 }
709 HeapRegionRemSetIterator* rem_set_iterator() {
710 return _rem_set_iterator[0];
711 }
713 unsigned get_gc_time_stamp() {
714 return _gc_time_stamp;
715 }
717 void reset_gc_time_stamp() {
718 _gc_time_stamp = 0;
719 OrderAccess::fence();
720 }
722 void increment_gc_time_stamp() {
723 ++_gc_time_stamp;
724 OrderAccess::fence();
725 }
727 void iterate_dirty_card_closure(CardTableEntryClosure* cl,
728 DirtyCardQueue* into_cset_dcq,
729 bool concurrent, int worker_i);
731 // The shared block offset table array.
732 G1BlockOffsetSharedArray* bot_shared() const { return _bot_shared; }
734 // Reference Processing accessor
735 ReferenceProcessor* ref_processor() { return _ref_processor; }
737 // Reserved (g1 only; super method includes perm), capacity and the used
738 // portion in bytes.
739 size_t g1_reserved_obj_bytes() const { return _g1_reserved.byte_size(); }
740 virtual size_t capacity() const;
741 virtual size_t used() const;
742 // This should be called when we're not holding the heap lock. The
743 // result might be a bit inaccurate.
744 size_t used_unlocked() const;
745 size_t recalculate_used() const;
746 #ifndef PRODUCT
747 size_t recalculate_used_regions() const;
748 #endif // PRODUCT
750 // These virtual functions do the actual allocation.
751 virtual HeapWord* mem_allocate(size_t word_size,
752 bool is_noref,
753 bool is_tlab,
754 bool* gc_overhead_limit_was_exceeded);
756 // Some heaps may offer a contiguous region for shared non-blocking
757 // allocation, via inlined code (by exporting the address of the top and
758 // end fields defining the extent of the contiguous allocation region.)
759 // But G1CollectedHeap doesn't yet support this.
761 // Return an estimate of the maximum allocation that could be performed
762 // without triggering any collection or expansion activity. In a
763 // generational collector, for example, this is probably the largest
764 // allocation that could be supported (without expansion) in the youngest
765 // generation. It is "unsafe" because no locks are taken; the result
766 // should be treated as an approximation, not a guarantee, for use in
767 // heuristic resizing decisions.
768 virtual size_t unsafe_max_alloc();
770 virtual bool is_maximal_no_gc() const {
771 return _g1_storage.uncommitted_size() == 0;
772 }
774 // The total number of regions in the heap.
775 size_t n_regions();
777 // The number of regions that are completely free.
778 size_t max_regions();
780 // The number of regions that are completely free.
781 size_t free_regions();
783 // The number of regions that are not completely free.
784 size_t used_regions() { return n_regions() - free_regions(); }
786 // True iff the ZF thread should run.
787 bool should_zf();
789 // The number of regions available for "regular" expansion.
790 size_t expansion_regions() { return _expansion_regions; }
792 #ifndef PRODUCT
793 bool regions_accounted_for();
794 bool print_region_accounting_info();
795 void print_region_counts();
796 #endif
798 HeapRegion* alloc_region_from_unclean_list(bool zero_filled);
799 HeapRegion* alloc_region_from_unclean_list_locked(bool zero_filled);
801 void put_region_on_unclean_list(HeapRegion* r);
802 void put_region_on_unclean_list_locked(HeapRegion* r);
804 void prepend_region_list_on_unclean_list(UncleanRegionList* list);
805 void prepend_region_list_on_unclean_list_locked(UncleanRegionList* list);
807 void set_unclean_regions_coming(bool b);
808 void set_unclean_regions_coming_locked(bool b);
809 // Wait for cleanup to be complete.
810 void wait_for_cleanup_complete();
811 // Like above, but assumes that the calling thread owns the Heap_lock.
812 void wait_for_cleanup_complete_locked();
814 // Return the head of the unclean list.
815 HeapRegion* peek_unclean_region_list_locked();
816 // Remove and return the head of the unclean list.
817 HeapRegion* pop_unclean_region_list_locked();
819 // List of regions which are zero filled and ready for allocation.
820 HeapRegion* _free_region_list;
821 // Number of elements on the free list.
822 size_t _free_region_list_size;
824 // If the head of the unclean list is ZeroFilled, move it to the free
825 // list.
826 bool move_cleaned_region_to_free_list_locked();
827 bool move_cleaned_region_to_free_list();
829 void put_free_region_on_list_locked(HeapRegion* r);
830 void put_free_region_on_list(HeapRegion* r);
832 // Remove and return the head element of the free list.
833 HeapRegion* pop_free_region_list_locked();
835 // If "zero_filled" is true, we first try the free list, then we try the
836 // unclean list, zero-filling the result. If "zero_filled" is false, we
837 // first try the unclean list, then the zero-filled list.
838 HeapRegion* alloc_free_region_from_lists(bool zero_filled);
840 // Verify the integrity of the region lists.
841 void remove_allocated_regions_from_lists();
842 bool verify_region_lists();
843 bool verify_region_lists_locked();
844 size_t unclean_region_list_length();
845 size_t free_region_list_length();
847 // Perform a collection of the heap; intended for use in implementing
848 // "System.gc". This probably implies as full a collection as the
849 // "CollectedHeap" supports.
850 virtual void collect(GCCause::Cause cause);
852 // The same as above but assume that the caller holds the Heap_lock.
853 void collect_locked(GCCause::Cause cause);
855 // This interface assumes that it's being called by the
856 // vm thread. It collects the heap assuming that the
857 // heap lock is already held and that we are executing in
858 // the context of the vm thread.
859 virtual void collect_as_vm_thread(GCCause::Cause cause);
861 // True iff a evacuation has failed in the most-recent collection.
862 bool evacuation_failed() { return _evacuation_failed; }
864 // Free a region if it is totally full of garbage. Returns the number of
865 // bytes freed (0 ==> didn't free it).
866 size_t free_region_if_totally_empty(HeapRegion *hr);
867 void free_region_if_totally_empty_work(HeapRegion *hr,
868 size_t& pre_used,
869 size_t& cleared_h_regions,
870 size_t& freed_regions,
871 UncleanRegionList* list,
872 bool par = false);
874 // If we've done free region work that yields the given changes, update
875 // the relevant global variables.
876 void finish_free_region_work(size_t pre_used,
877 size_t cleared_h_regions,
878 size_t freed_regions,
879 UncleanRegionList* list);
882 // Returns "TRUE" iff "p" points into the allocated area of the heap.
883 virtual bool is_in(const void* p) const;
885 // Return "TRUE" iff the given object address is within the collection
886 // set.
887 inline bool obj_in_cs(oop obj);
889 // Return "TRUE" iff the given object address is in the reserved
890 // region of g1 (excluding the permanent generation).
891 bool is_in_g1_reserved(const void* p) const {
892 return _g1_reserved.contains(p);
893 }
895 // Returns a MemRegion that corresponds to the space that has been
896 // committed in the heap
897 MemRegion g1_committed() {
898 return _g1_committed;
899 }
901 NOT_PRODUCT(bool is_in_closed_subset(const void* p) const;)
903 // Dirty card table entries covering a list of young regions.
904 void dirtyCardsForYoungRegions(CardTableModRefBS* ct_bs, HeapRegion* list);
906 // This resets the card table to all zeros. It is used after
907 // a collection pause which used the card table to claim cards.
908 void cleanUpCardTable();
910 // Iteration functions.
912 // Iterate over all the ref-containing fields of all objects, calling
913 // "cl.do_oop" on each.
914 virtual void oop_iterate(OopClosure* cl) {
915 oop_iterate(cl, true);
916 }
917 void oop_iterate(OopClosure* cl, bool do_perm);
919 // Same as above, restricted to a memory region.
920 virtual void oop_iterate(MemRegion mr, OopClosure* cl) {
921 oop_iterate(mr, cl, true);
922 }
923 void oop_iterate(MemRegion mr, OopClosure* cl, bool do_perm);
925 // Iterate over all objects, calling "cl.do_object" on each.
926 virtual void object_iterate(ObjectClosure* cl) {
927 object_iterate(cl, true);
928 }
929 virtual void safe_object_iterate(ObjectClosure* cl) {
930 object_iterate(cl, true);
931 }
932 void object_iterate(ObjectClosure* cl, bool do_perm);
934 // Iterate over all objects allocated since the last collection, calling
935 // "cl.do_object" on each. The heap must have been initialized properly
936 // to support this function, or else this call will fail.
937 virtual void object_iterate_since_last_GC(ObjectClosure* cl);
939 // Iterate over all spaces in use in the heap, in ascending address order.
940 virtual void space_iterate(SpaceClosure* cl);
942 // Iterate over heap regions, in address order, terminating the
943 // iteration early if the "doHeapRegion" method returns "true".
944 void heap_region_iterate(HeapRegionClosure* blk);
946 // Iterate over heap regions starting with r (or the first region if "r"
947 // is NULL), in address order, terminating early if the "doHeapRegion"
948 // method returns "true".
949 void heap_region_iterate_from(HeapRegion* r, HeapRegionClosure* blk);
951 // As above but starting from the region at index idx.
952 void heap_region_iterate_from(int idx, HeapRegionClosure* blk);
954 HeapRegion* region_at(size_t idx);
956 // Divide the heap region sequence into "chunks" of some size (the number
957 // of regions divided by the number of parallel threads times some
958 // overpartition factor, currently 4). Assumes that this will be called
959 // in parallel by ParallelGCThreads worker threads with discinct worker
960 // ids in the range [0..max(ParallelGCThreads-1, 1)], that all parallel
961 // calls will use the same "claim_value", and that that claim value is
962 // different from the claim_value of any heap region before the start of
963 // the iteration. Applies "blk->doHeapRegion" to each of the regions, by
964 // attempting to claim the first region in each chunk, and, if
965 // successful, applying the closure to each region in the chunk (and
966 // setting the claim value of the second and subsequent regions of the
967 // chunk.) For now requires that "doHeapRegion" always returns "false",
968 // i.e., that a closure never attempt to abort a traversal.
969 void heap_region_par_iterate_chunked(HeapRegionClosure* blk,
970 int worker,
971 jint claim_value);
973 // It resets all the region claim values to the default.
974 void reset_heap_region_claim_values();
976 #ifdef ASSERT
977 bool check_heap_region_claim_values(jint claim_value);
978 #endif // ASSERT
980 // Iterate over the regions (if any) in the current collection set.
981 void collection_set_iterate(HeapRegionClosure* blk);
983 // As above but starting from region r
984 void collection_set_iterate_from(HeapRegion* r, HeapRegionClosure *blk);
986 // Returns the first (lowest address) compactible space in the heap.
987 virtual CompactibleSpace* first_compactible_space();
989 // A CollectedHeap will contain some number of spaces. This finds the
990 // space containing a given address, or else returns NULL.
991 virtual Space* space_containing(const void* addr) const;
993 // A G1CollectedHeap will contain some number of heap regions. This
994 // finds the region containing a given address, or else returns NULL.
995 HeapRegion* heap_region_containing(const void* addr) const;
997 // Like the above, but requires "addr" to be in the heap (to avoid a
998 // null-check), and unlike the above, may return an continuing humongous
999 // region.
1000 HeapRegion* heap_region_containing_raw(const void* addr) const;
1002 // A CollectedHeap is divided into a dense sequence of "blocks"; that is,
1003 // each address in the (reserved) heap is a member of exactly
1004 // one block. The defining characteristic of a block is that it is
1005 // possible to find its size, and thus to progress forward to the next
1006 // block. (Blocks may be of different sizes.) Thus, blocks may
1007 // represent Java objects, or they might be free blocks in a
1008 // free-list-based heap (or subheap), as long as the two kinds are
1009 // distinguishable and the size of each is determinable.
1011 // Returns the address of the start of the "block" that contains the
1012 // address "addr". We say "blocks" instead of "object" since some heaps
1013 // may not pack objects densely; a chunk may either be an object or a
1014 // non-object.
1015 virtual HeapWord* block_start(const void* addr) const;
1017 // Requires "addr" to be the start of a chunk, and returns its size.
1018 // "addr + size" is required to be the start of a new chunk, or the end
1019 // of the active area of the heap.
1020 virtual size_t block_size(const HeapWord* addr) const;
1022 // Requires "addr" to be the start of a block, and returns "TRUE" iff
1023 // the block is an object.
1024 virtual bool block_is_obj(const HeapWord* addr) const;
1026 // Does this heap support heap inspection? (+PrintClassHistogram)
1027 virtual bool supports_heap_inspection() const { return true; }
1029 // Section on thread-local allocation buffers (TLABs)
1030 // See CollectedHeap for semantics.
1032 virtual bool supports_tlab_allocation() const;
1033 virtual size_t tlab_capacity(Thread* thr) const;
1034 virtual size_t unsafe_max_tlab_alloc(Thread* thr) const;
1035 virtual HeapWord* allocate_new_tlab(size_t size);
1037 // Can a compiler initialize a new object without store barriers?
1038 // This permission only extends from the creation of a new object
1039 // via a TLAB up to the first subsequent safepoint. If such permission
1040 // is granted for this heap type, the compiler promises to call
1041 // defer_store_barrier() below on any slow path allocation of
1042 // a new object for which such initializing store barriers will
1043 // have been elided. G1, like CMS, allows this, but should be
1044 // ready to provide a compensating write barrier as necessary
1045 // if that storage came out of a non-young region. The efficiency
1046 // of this implementation depends crucially on being able to
1047 // answer very efficiently in constant time whether a piece of
1048 // storage in the heap comes from a young region or not.
1049 // See ReduceInitialCardMarks.
1050 virtual bool can_elide_tlab_store_barriers() const {
1051 // 6920090: Temporarily disabled, because of lingering
1052 // instabilities related to RICM with G1. In the
1053 // interim, the option ReduceInitialCardMarksForG1
1054 // below is left solely as a debugging device at least
1055 // until 6920109 fixes the instabilities.
1056 return ReduceInitialCardMarksForG1;
1057 }
1059 virtual bool card_mark_must_follow_store() const {
1060 return true;
1061 }
1063 bool is_in_young(oop obj) {
1064 HeapRegion* hr = heap_region_containing(obj);
1065 return hr != NULL && hr->is_young();
1066 }
1068 // We don't need barriers for initializing stores to objects
1069 // in the young gen: for the SATB pre-barrier, there is no
1070 // pre-value that needs to be remembered; for the remembered-set
1071 // update logging post-barrier, we don't maintain remembered set
1072 // information for young gen objects. Note that non-generational
1073 // G1 does not have any "young" objects, should not elide
1074 // the rs logging barrier and so should always answer false below.
1075 // However, non-generational G1 (-XX:-G1Gen) appears to have
1076 // bit-rotted so was not tested below.
1077 virtual bool can_elide_initializing_store_barrier(oop new_obj) {
1078 // Re 6920090, 6920109 above.
1079 assert(ReduceInitialCardMarksForG1, "Else cannot be here");
1080 assert(G1Gen || !is_in_young(new_obj),
1081 "Non-generational G1 should never return true below");
1082 return is_in_young(new_obj);
1083 }
1085 // Can a compiler elide a store barrier when it writes
1086 // a permanent oop into the heap? Applies when the compiler
1087 // is storing x to the heap, where x->is_perm() is true.
1088 virtual bool can_elide_permanent_oop_store_barriers() const {
1089 // At least until perm gen collection is also G1-ified, at
1090 // which point this should return false.
1091 return true;
1092 }
1094 virtual bool allocs_are_zero_filled();
1096 // The boundary between a "large" and "small" array of primitives, in
1097 // words.
1098 virtual size_t large_typearray_limit();
1100 // Returns "true" iff the given word_size is "very large".
1101 static bool isHumongous(size_t word_size) {
1102 // Note this has to be strictly greater-than as the TLABs
1103 // are capped at the humongous thresold and we want to
1104 // ensure that we don't try to allocate a TLAB as
1105 // humongous and that we don't allocate a humongous
1106 // object in a TLAB.
1107 return word_size > _humongous_object_threshold_in_words;
1108 }
1110 // Update mod union table with the set of dirty cards.
1111 void updateModUnion();
1113 // Set the mod union bits corresponding to the given memRegion. Note
1114 // that this is always a safe operation, since it doesn't clear any
1115 // bits.
1116 void markModUnionRange(MemRegion mr);
1118 // Records the fact that a marking phase is no longer in progress.
1119 void set_marking_complete() {
1120 _mark_in_progress = false;
1121 }
1122 void set_marking_started() {
1123 _mark_in_progress = true;
1124 }
1125 bool mark_in_progress() {
1126 return _mark_in_progress;
1127 }
1129 // Print the maximum heap capacity.
1130 virtual size_t max_capacity() const;
1132 virtual jlong millis_since_last_gc();
1134 // Perform any cleanup actions necessary before allowing a verification.
1135 virtual void prepare_for_verify();
1137 // Perform verification.
1139 // use_prev_marking == true -> use "prev" marking information,
1140 // use_prev_marking == false -> use "next" marking information
1141 // NOTE: Only the "prev" marking information is guaranteed to be
1142 // consistent most of the time, so most calls to this should use
1143 // use_prev_marking == true. Currently, there is only one case where
1144 // this is called with use_prev_marking == false, which is to verify
1145 // the "next" marking information at the end of remark.
1146 void verify(bool allow_dirty, bool silent, bool use_prev_marking);
1148 // Override; it uses the "prev" marking information
1149 virtual void verify(bool allow_dirty, bool silent);
1150 // Default behavior by calling print(tty);
1151 virtual void print() const;
1152 // This calls print_on(st, PrintHeapAtGCExtended).
1153 virtual void print_on(outputStream* st) const;
1154 // If extended is true, it will print out information for all
1155 // regions in the heap by calling print_on_extended(st).
1156 virtual void print_on(outputStream* st, bool extended) const;
1157 virtual void print_on_extended(outputStream* st) const;
1159 virtual void print_gc_threads_on(outputStream* st) const;
1160 virtual void gc_threads_do(ThreadClosure* tc) const;
1162 // Override
1163 void print_tracing_info() const;
1165 // If "addr" is a pointer into the (reserved?) heap, returns a positive
1166 // number indicating the "arena" within the heap in which "addr" falls.
1167 // Or else returns 0.
1168 virtual int addr_to_arena_id(void* addr) const;
1170 // Convenience function to be used in situations where the heap type can be
1171 // asserted to be this type.
1172 static G1CollectedHeap* heap();
1174 void empty_young_list();
1175 bool should_set_young_locked();
1177 void set_region_short_lived_locked(HeapRegion* hr);
1178 // add appropriate methods for any other surv rate groups
1180 YoungList* young_list() { return _young_list; }
1182 // debugging
1183 bool check_young_list_well_formed() {
1184 return _young_list->check_list_well_formed();
1185 }
1187 bool check_young_list_empty(bool check_heap,
1188 bool check_sample = true);
1190 // *** Stuff related to concurrent marking. It's not clear to me that so
1191 // many of these need to be public.
1193 // The functions below are helper functions that a subclass of
1194 // "CollectedHeap" can use in the implementation of its virtual
1195 // functions.
1196 // This performs a concurrent marking of the live objects in a
1197 // bitmap off to the side.
1198 void doConcurrentMark();
1200 // This is called from the marksweep collector which then does
1201 // a concurrent mark and verifies that the results agree with
1202 // the stop the world marking.
1203 void checkConcurrentMark();
1204 void do_sync_mark();
1206 bool isMarkedPrev(oop obj) const;
1207 bool isMarkedNext(oop obj) const;
1209 // use_prev_marking == true -> use "prev" marking information,
1210 // use_prev_marking == false -> use "next" marking information
1211 bool is_obj_dead_cond(const oop obj,
1212 const HeapRegion* hr,
1213 const bool use_prev_marking) const {
1214 if (use_prev_marking) {
1215 return is_obj_dead(obj, hr);
1216 } else {
1217 return is_obj_ill(obj, hr);
1218 }
1219 }
1221 // Determine if an object is dead, given the object and also
1222 // the region to which the object belongs. An object is dead
1223 // iff a) it was not allocated since the last mark and b) it
1224 // is not marked.
1226 bool is_obj_dead(const oop obj, const HeapRegion* hr) const {
1227 return
1228 !hr->obj_allocated_since_prev_marking(obj) &&
1229 !isMarkedPrev(obj);
1230 }
1232 // This is used when copying an object to survivor space.
1233 // If the object is marked live, then we mark the copy live.
1234 // If the object is allocated since the start of this mark
1235 // cycle, then we mark the copy live.
1236 // If the object has been around since the previous mark
1237 // phase, and hasn't been marked yet during this phase,
1238 // then we don't mark it, we just wait for the
1239 // current marking cycle to get to it.
1241 // This function returns true when an object has been
1242 // around since the previous marking and hasn't yet
1243 // been marked during this marking.
1245 bool is_obj_ill(const oop obj, const HeapRegion* hr) const {
1246 return
1247 !hr->obj_allocated_since_next_marking(obj) &&
1248 !isMarkedNext(obj);
1249 }
1251 // Determine if an object is dead, given only the object itself.
1252 // This will find the region to which the object belongs and
1253 // then call the region version of the same function.
1255 // Added if it is in permanent gen it isn't dead.
1256 // Added if it is NULL it isn't dead.
1258 // use_prev_marking == true -> use "prev" marking information,
1259 // use_prev_marking == false -> use "next" marking information
1260 bool is_obj_dead_cond(const oop obj,
1261 const bool use_prev_marking) {
1262 if (use_prev_marking) {
1263 return is_obj_dead(obj);
1264 } else {
1265 return is_obj_ill(obj);
1266 }
1267 }
1269 bool is_obj_dead(const oop obj) {
1270 const HeapRegion* hr = heap_region_containing(obj);
1271 if (hr == NULL) {
1272 if (Universe::heap()->is_in_permanent(obj))
1273 return false;
1274 else if (obj == NULL) return false;
1275 else return true;
1276 }
1277 else return is_obj_dead(obj, hr);
1278 }
1280 bool is_obj_ill(const oop obj) {
1281 const HeapRegion* hr = heap_region_containing(obj);
1282 if (hr == NULL) {
1283 if (Universe::heap()->is_in_permanent(obj))
1284 return false;
1285 else if (obj == NULL) return false;
1286 else return true;
1287 }
1288 else return is_obj_ill(obj, hr);
1289 }
1291 // The following is just to alert the verification code
1292 // that a full collection has occurred and that the
1293 // remembered sets are no longer up to date.
1294 bool _full_collection;
1295 void set_full_collection() { _full_collection = true;}
1296 void clear_full_collection() {_full_collection = false;}
1297 bool full_collection() {return _full_collection;}
1299 ConcurrentMark* concurrent_mark() const { return _cm; }
1300 ConcurrentG1Refine* concurrent_g1_refine() const { return _cg1r; }
1302 // The dirty cards region list is used to record a subset of regions
1303 // whose cards need clearing. The list if populated during the
1304 // remembered set scanning and drained during the card table
1305 // cleanup. Although the methods are reentrant, population/draining
1306 // phases must not overlap. For synchronization purposes the last
1307 // element on the list points to itself.
1308 HeapRegion* _dirty_cards_region_list;
1309 void push_dirty_cards_region(HeapRegion* hr);
1310 HeapRegion* pop_dirty_cards_region();
1312 public:
1313 void stop_conc_gc_threads();
1315 // <NEW PREDICTION>
1317 double predict_region_elapsed_time_ms(HeapRegion* hr, bool young);
1318 void check_if_region_is_too_expensive(double predicted_time_ms);
1319 size_t pending_card_num();
1320 size_t max_pending_card_num();
1321 size_t cards_scanned();
1323 // </NEW PREDICTION>
1325 protected:
1326 size_t _max_heap_capacity;
1328 // debug_only(static void check_for_valid_allocation_state();)
1330 public:
1331 // Temporary: call to mark things unimplemented for the G1 heap (e.g.,
1332 // MemoryService). In productization, we can make this assert false
1333 // to catch such places (as well as searching for calls to this...)
1334 static void g1_unimplemented();
1336 };
1338 #define use_local_bitmaps 1
1339 #define verify_local_bitmaps 0
1340 #define oop_buffer_length 256
1342 #ifndef PRODUCT
1343 class GCLabBitMap;
1344 class GCLabBitMapClosure: public BitMapClosure {
1345 private:
1346 ConcurrentMark* _cm;
1347 GCLabBitMap* _bitmap;
1349 public:
1350 GCLabBitMapClosure(ConcurrentMark* cm,
1351 GCLabBitMap* bitmap) {
1352 _cm = cm;
1353 _bitmap = bitmap;
1354 }
1356 virtual bool do_bit(size_t offset);
1357 };
1358 #endif // !PRODUCT
1360 class GCLabBitMap: public BitMap {
1361 private:
1362 ConcurrentMark* _cm;
1364 int _shifter;
1365 size_t _bitmap_word_covers_words;
1367 // beginning of the heap
1368 HeapWord* _heap_start;
1370 // this is the actual start of the GCLab
1371 HeapWord* _real_start_word;
1373 // this is the actual end of the GCLab
1374 HeapWord* _real_end_word;
1376 // this is the first word, possibly located before the actual start
1377 // of the GCLab, that corresponds to the first bit of the bitmap
1378 HeapWord* _start_word;
1380 // size of a GCLab in words
1381 size_t _gclab_word_size;
1383 static int shifter() {
1384 return MinObjAlignment - 1;
1385 }
1387 // how many heap words does a single bitmap word corresponds to?
1388 static size_t bitmap_word_covers_words() {
1389 return BitsPerWord << shifter();
1390 }
1392 size_t gclab_word_size() const {
1393 return _gclab_word_size;
1394 }
1396 // Calculates actual GCLab size in words
1397 size_t gclab_real_word_size() const {
1398 return bitmap_size_in_bits(pointer_delta(_real_end_word, _start_word))
1399 / BitsPerWord;
1400 }
1402 static size_t bitmap_size_in_bits(size_t gclab_word_size) {
1403 size_t bits_in_bitmap = gclab_word_size >> shifter();
1404 // We are going to ensure that the beginning of a word in this
1405 // bitmap also corresponds to the beginning of a word in the
1406 // global marking bitmap. To handle the case where a GCLab
1407 // starts from the middle of the bitmap, we need to add enough
1408 // space (i.e. up to a bitmap word) to ensure that we have
1409 // enough bits in the bitmap.
1410 return bits_in_bitmap + BitsPerWord - 1;
1411 }
1412 public:
1413 GCLabBitMap(HeapWord* heap_start, size_t gclab_word_size)
1414 : BitMap(bitmap_size_in_bits(gclab_word_size)),
1415 _cm(G1CollectedHeap::heap()->concurrent_mark()),
1416 _shifter(shifter()),
1417 _bitmap_word_covers_words(bitmap_word_covers_words()),
1418 _heap_start(heap_start),
1419 _gclab_word_size(gclab_word_size),
1420 _real_start_word(NULL),
1421 _real_end_word(NULL),
1422 _start_word(NULL)
1423 {
1424 guarantee( size_in_words() >= bitmap_size_in_words(),
1425 "just making sure");
1426 }
1428 inline unsigned heapWordToOffset(HeapWord* addr) {
1429 unsigned offset = (unsigned) pointer_delta(addr, _start_word) >> _shifter;
1430 assert(offset < size(), "offset should be within bounds");
1431 return offset;
1432 }
1434 inline HeapWord* offsetToHeapWord(size_t offset) {
1435 HeapWord* addr = _start_word + (offset << _shifter);
1436 assert(_real_start_word <= addr && addr < _real_end_word, "invariant");
1437 return addr;
1438 }
1440 bool fields_well_formed() {
1441 bool ret1 = (_real_start_word == NULL) &&
1442 (_real_end_word == NULL) &&
1443 (_start_word == NULL);
1444 if (ret1)
1445 return true;
1447 bool ret2 = _real_start_word >= _start_word &&
1448 _start_word < _real_end_word &&
1449 (_real_start_word + _gclab_word_size) == _real_end_word &&
1450 (_start_word + _gclab_word_size + _bitmap_word_covers_words)
1451 > _real_end_word;
1452 return ret2;
1453 }
1455 inline bool mark(HeapWord* addr) {
1456 guarantee(use_local_bitmaps, "invariant");
1457 assert(fields_well_formed(), "invariant");
1459 if (addr >= _real_start_word && addr < _real_end_word) {
1460 assert(!isMarked(addr), "should not have already been marked");
1462 // first mark it on the bitmap
1463 at_put(heapWordToOffset(addr), true);
1465 return true;
1466 } else {
1467 return false;
1468 }
1469 }
1471 inline bool isMarked(HeapWord* addr) {
1472 guarantee(use_local_bitmaps, "invariant");
1473 assert(fields_well_formed(), "invariant");
1475 return at(heapWordToOffset(addr));
1476 }
1478 void set_buffer(HeapWord* start) {
1479 guarantee(use_local_bitmaps, "invariant");
1480 clear();
1482 assert(start != NULL, "invariant");
1483 _real_start_word = start;
1484 _real_end_word = start + _gclab_word_size;
1486 size_t diff =
1487 pointer_delta(start, _heap_start) % _bitmap_word_covers_words;
1488 _start_word = start - diff;
1490 assert(fields_well_formed(), "invariant");
1491 }
1493 #ifndef PRODUCT
1494 void verify() {
1495 // verify that the marks have been propagated
1496 GCLabBitMapClosure cl(_cm, this);
1497 iterate(&cl);
1498 }
1499 #endif // PRODUCT
1501 void retire() {
1502 guarantee(use_local_bitmaps, "invariant");
1503 assert(fields_well_formed(), "invariant");
1505 if (_start_word != NULL) {
1506 CMBitMap* mark_bitmap = _cm->nextMarkBitMap();
1508 // this means that the bitmap was set up for the GCLab
1509 assert(_real_start_word != NULL && _real_end_word != NULL, "invariant");
1511 mark_bitmap->mostly_disjoint_range_union(this,
1512 0, // always start from the start of the bitmap
1513 _start_word,
1514 gclab_real_word_size());
1515 _cm->grayRegionIfNecessary(MemRegion(_real_start_word, _real_end_word));
1517 #ifndef PRODUCT
1518 if (use_local_bitmaps && verify_local_bitmaps)
1519 verify();
1520 #endif // PRODUCT
1521 } else {
1522 assert(_real_start_word == NULL && _real_end_word == NULL, "invariant");
1523 }
1524 }
1526 size_t bitmap_size_in_words() const {
1527 return (bitmap_size_in_bits(gclab_word_size()) + BitsPerWord - 1) / BitsPerWord;
1528 }
1530 };
1532 class G1ParGCAllocBuffer: public ParGCAllocBuffer {
1533 private:
1534 bool _retired;
1535 bool _during_marking;
1536 GCLabBitMap _bitmap;
1538 public:
1539 G1ParGCAllocBuffer(size_t gclab_word_size) :
1540 ParGCAllocBuffer(gclab_word_size),
1541 _during_marking(G1CollectedHeap::heap()->mark_in_progress()),
1542 _bitmap(G1CollectedHeap::heap()->reserved_region().start(), gclab_word_size),
1543 _retired(false)
1544 { }
1546 inline bool mark(HeapWord* addr) {
1547 guarantee(use_local_bitmaps, "invariant");
1548 assert(_during_marking, "invariant");
1549 return _bitmap.mark(addr);
1550 }
1552 inline void set_buf(HeapWord* buf) {
1553 if (use_local_bitmaps && _during_marking)
1554 _bitmap.set_buffer(buf);
1555 ParGCAllocBuffer::set_buf(buf);
1556 _retired = false;
1557 }
1559 inline void retire(bool end_of_gc, bool retain) {
1560 if (_retired)
1561 return;
1562 if (use_local_bitmaps && _during_marking) {
1563 _bitmap.retire();
1564 }
1565 ParGCAllocBuffer::retire(end_of_gc, retain);
1566 _retired = true;
1567 }
1568 };
1570 class G1ParScanThreadState : public StackObj {
1571 protected:
1572 G1CollectedHeap* _g1h;
1573 RefToScanQueue* _refs;
1574 DirtyCardQueue _dcq;
1575 CardTableModRefBS* _ct_bs;
1576 G1RemSet* _g1_rem;
1578 G1ParGCAllocBuffer _surviving_alloc_buffer;
1579 G1ParGCAllocBuffer _tenured_alloc_buffer;
1580 G1ParGCAllocBuffer* _alloc_buffers[GCAllocPurposeCount];
1581 ageTable _age_table;
1583 size_t _alloc_buffer_waste;
1584 size_t _undo_waste;
1586 OopsInHeapRegionClosure* _evac_failure_cl;
1587 G1ParScanHeapEvacClosure* _evac_cl;
1588 G1ParScanPartialArrayClosure* _partial_scan_cl;
1590 int _hash_seed;
1591 int _queue_num;
1593 size_t _term_attempts;
1595 double _start;
1596 double _start_strong_roots;
1597 double _strong_roots_time;
1598 double _start_term;
1599 double _term_time;
1601 // Map from young-age-index (0 == not young, 1 is youngest) to
1602 // surviving words. base is what we get back from the malloc call
1603 size_t* _surviving_young_words_base;
1604 // this points into the array, as we use the first few entries for padding
1605 size_t* _surviving_young_words;
1607 #define PADDING_ELEM_NUM (DEFAULT_CACHE_LINE_SIZE / sizeof(size_t))
1609 void add_to_alloc_buffer_waste(size_t waste) { _alloc_buffer_waste += waste; }
1611 void add_to_undo_waste(size_t waste) { _undo_waste += waste; }
1613 DirtyCardQueue& dirty_card_queue() { return _dcq; }
1614 CardTableModRefBS* ctbs() { return _ct_bs; }
1616 template <class T> void immediate_rs_update(HeapRegion* from, T* p, int tid) {
1617 if (!from->is_survivor()) {
1618 _g1_rem->par_write_ref(from, p, tid);
1619 }
1620 }
1622 template <class T> void deferred_rs_update(HeapRegion* from, T* p, int tid) {
1623 // If the new value of the field points to the same region or
1624 // is the to-space, we don't need to include it in the Rset updates.
1625 if (!from->is_in_reserved(oopDesc::load_decode_heap_oop(p)) && !from->is_survivor()) {
1626 size_t card_index = ctbs()->index_for(p);
1627 // If the card hasn't been added to the buffer, do it.
1628 if (ctbs()->mark_card_deferred(card_index)) {
1629 dirty_card_queue().enqueue((jbyte*)ctbs()->byte_for_index(card_index));
1630 }
1631 }
1632 }
1634 public:
1635 G1ParScanThreadState(G1CollectedHeap* g1h, int queue_num);
1637 ~G1ParScanThreadState() {
1638 FREE_C_HEAP_ARRAY(size_t, _surviving_young_words_base);
1639 }
1641 RefToScanQueue* refs() { return _refs; }
1642 ageTable* age_table() { return &_age_table; }
1644 G1ParGCAllocBuffer* alloc_buffer(GCAllocPurpose purpose) {
1645 return _alloc_buffers[purpose];
1646 }
1648 size_t alloc_buffer_waste() const { return _alloc_buffer_waste; }
1649 size_t undo_waste() const { return _undo_waste; }
1651 template <class T> void push_on_queue(T* ref) {
1652 assert(ref != NULL, "invariant");
1653 assert(has_partial_array_mask(ref) ||
1654 _g1h->is_in_g1_reserved(oopDesc::load_decode_heap_oop(ref)), "invariant");
1655 #ifdef ASSERT
1656 if (has_partial_array_mask(ref)) {
1657 oop p = clear_partial_array_mask(ref);
1658 // Verify that we point into the CS
1659 assert(_g1h->obj_in_cs(p), "Should be in CS");
1660 }
1661 #endif
1662 refs()->push(ref);
1663 }
1665 void pop_from_queue(StarTask& ref) {
1666 if (refs()->pop_local(ref)) {
1667 assert((oop*)ref != NULL, "pop_local() returned true");
1668 assert(UseCompressedOops || !ref.is_narrow(), "Error");
1669 assert(has_partial_array_mask((oop*)ref) ||
1670 _g1h->is_in_g1_reserved(ref.is_narrow() ? oopDesc::load_decode_heap_oop((narrowOop*)ref)
1671 : oopDesc::load_decode_heap_oop((oop*)ref)),
1672 "invariant");
1673 } else {
1674 StarTask null_task;
1675 ref = null_task;
1676 }
1677 }
1679 void pop_from_overflow_queue(StarTask& ref) {
1680 StarTask new_ref;
1681 refs()->pop_overflow(new_ref);
1682 assert((oop*)new_ref != NULL, "pop() from a local non-empty stack");
1683 assert(UseCompressedOops || !new_ref.is_narrow(), "Error");
1684 assert(has_partial_array_mask((oop*)new_ref) ||
1685 _g1h->is_in_g1_reserved(new_ref.is_narrow() ? oopDesc::load_decode_heap_oop((narrowOop*)new_ref)
1686 : oopDesc::load_decode_heap_oop((oop*)new_ref)),
1687 "invariant");
1688 ref = new_ref;
1689 }
1691 int refs_to_scan() { return refs()->size(); }
1692 int overflowed_refs_to_scan() { return refs()->overflow_stack()->length(); }
1694 template <class T> void update_rs(HeapRegion* from, T* p, int tid) {
1695 if (G1DeferredRSUpdate) {
1696 deferred_rs_update(from, p, tid);
1697 } else {
1698 immediate_rs_update(from, p, tid);
1699 }
1700 }
1702 HeapWord* allocate_slow(GCAllocPurpose purpose, size_t word_sz) {
1704 HeapWord* obj = NULL;
1705 size_t gclab_word_size = _g1h->desired_plab_sz(purpose);
1706 if (word_sz * 100 < gclab_word_size * ParallelGCBufferWastePct) {
1707 G1ParGCAllocBuffer* alloc_buf = alloc_buffer(purpose);
1708 assert(gclab_word_size == alloc_buf->word_sz(),
1709 "dynamic resizing is not supported");
1710 add_to_alloc_buffer_waste(alloc_buf->words_remaining());
1711 alloc_buf->retire(false, false);
1713 HeapWord* buf = _g1h->par_allocate_during_gc(purpose, gclab_word_size);
1714 if (buf == NULL) return NULL; // Let caller handle allocation failure.
1715 // Otherwise.
1716 alloc_buf->set_buf(buf);
1718 obj = alloc_buf->allocate(word_sz);
1719 assert(obj != NULL, "buffer was definitely big enough...");
1720 } else {
1721 obj = _g1h->par_allocate_during_gc(purpose, word_sz);
1722 }
1723 return obj;
1724 }
1726 HeapWord* allocate(GCAllocPurpose purpose, size_t word_sz) {
1727 HeapWord* obj = alloc_buffer(purpose)->allocate(word_sz);
1728 if (obj != NULL) return obj;
1729 return allocate_slow(purpose, word_sz);
1730 }
1732 void undo_allocation(GCAllocPurpose purpose, HeapWord* obj, size_t word_sz) {
1733 if (alloc_buffer(purpose)->contains(obj)) {
1734 assert(alloc_buffer(purpose)->contains(obj + word_sz - 1),
1735 "should contain whole object");
1736 alloc_buffer(purpose)->undo_allocation(obj, word_sz);
1737 } else {
1738 CollectedHeap::fill_with_object(obj, word_sz);
1739 add_to_undo_waste(word_sz);
1740 }
1741 }
1743 void set_evac_failure_closure(OopsInHeapRegionClosure* evac_failure_cl) {
1744 _evac_failure_cl = evac_failure_cl;
1745 }
1746 OopsInHeapRegionClosure* evac_failure_closure() {
1747 return _evac_failure_cl;
1748 }
1750 void set_evac_closure(G1ParScanHeapEvacClosure* evac_cl) {
1751 _evac_cl = evac_cl;
1752 }
1754 void set_partial_scan_closure(G1ParScanPartialArrayClosure* partial_scan_cl) {
1755 _partial_scan_cl = partial_scan_cl;
1756 }
1758 int* hash_seed() { return &_hash_seed; }
1759 int queue_num() { return _queue_num; }
1761 size_t term_attempts() const { return _term_attempts; }
1762 void note_term_attempt() { _term_attempts++; }
1764 void start_strong_roots() {
1765 _start_strong_roots = os::elapsedTime();
1766 }
1767 void end_strong_roots() {
1768 _strong_roots_time += (os::elapsedTime() - _start_strong_roots);
1769 }
1770 double strong_roots_time() const { return _strong_roots_time; }
1772 void start_term_time() {
1773 note_term_attempt();
1774 _start_term = os::elapsedTime();
1775 }
1776 void end_term_time() {
1777 _term_time += (os::elapsedTime() - _start_term);
1778 }
1779 double term_time() const { return _term_time; }
1781 double elapsed_time() const {
1782 return os::elapsedTime() - _start;
1783 }
1785 static void
1786 print_termination_stats_hdr(outputStream* const st = gclog_or_tty);
1787 void
1788 print_termination_stats(int i, outputStream* const st = gclog_or_tty) const;
1790 size_t* surviving_young_words() {
1791 // We add on to hide entry 0 which accumulates surviving words for
1792 // age -1 regions (i.e. non-young ones)
1793 return _surviving_young_words;
1794 }
1796 void retire_alloc_buffers() {
1797 for (int ap = 0; ap < GCAllocPurposeCount; ++ap) {
1798 size_t waste = _alloc_buffers[ap]->words_remaining();
1799 add_to_alloc_buffer_waste(waste);
1800 _alloc_buffers[ap]->retire(true, false);
1801 }
1802 }
1804 private:
1805 template <class T> void deal_with_reference(T* ref_to_scan) {
1806 if (has_partial_array_mask(ref_to_scan)) {
1807 _partial_scan_cl->do_oop_nv(ref_to_scan);
1808 } else {
1809 // Note: we can use "raw" versions of "region_containing" because
1810 // "obj_to_scan" is definitely in the heap, and is not in a
1811 // humongous region.
1812 HeapRegion* r = _g1h->heap_region_containing_raw(ref_to_scan);
1813 _evac_cl->set_region(r);
1814 _evac_cl->do_oop_nv(ref_to_scan);
1815 }
1816 }
1818 public:
1819 void trim_queue() {
1820 // I've replicated the loop twice, first to drain the overflow
1821 // queue, second to drain the task queue. This is better than
1822 // having a single loop, which checks both conditions and, inside
1823 // it, either pops the overflow queue or the task queue, as each
1824 // loop is tighter. Also, the decision to drain the overflow queue
1825 // first is not arbitrary, as the overflow queue is not visible
1826 // to the other workers, whereas the task queue is. So, we want to
1827 // drain the "invisible" entries first, while allowing the other
1828 // workers to potentially steal the "visible" entries.
1830 while (refs_to_scan() > 0 || overflowed_refs_to_scan() > 0) {
1831 while (overflowed_refs_to_scan() > 0) {
1832 StarTask ref_to_scan;
1833 assert((oop*)ref_to_scan == NULL, "Constructed above");
1834 pop_from_overflow_queue(ref_to_scan);
1835 // We shouldn't have pushed it on the queue if it was not
1836 // pointing into the CSet.
1837 assert((oop*)ref_to_scan != NULL, "Follows from inner loop invariant");
1838 if (ref_to_scan.is_narrow()) {
1839 assert(UseCompressedOops, "Error");
1840 narrowOop* p = (narrowOop*)ref_to_scan;
1841 assert(!has_partial_array_mask(p) &&
1842 _g1h->is_in_g1_reserved(oopDesc::load_decode_heap_oop(p)), "sanity");
1843 deal_with_reference(p);
1844 } else {
1845 oop* p = (oop*)ref_to_scan;
1846 assert((has_partial_array_mask(p) && _g1h->is_in_g1_reserved(clear_partial_array_mask(p))) ||
1847 _g1h->is_in_g1_reserved(oopDesc::load_decode_heap_oop(p)), "sanity");
1848 deal_with_reference(p);
1849 }
1850 }
1852 while (refs_to_scan() > 0) {
1853 StarTask ref_to_scan;
1854 assert((oop*)ref_to_scan == NULL, "Constructed above");
1855 pop_from_queue(ref_to_scan);
1856 if ((oop*)ref_to_scan != NULL) {
1857 if (ref_to_scan.is_narrow()) {
1858 assert(UseCompressedOops, "Error");
1859 narrowOop* p = (narrowOop*)ref_to_scan;
1860 assert(!has_partial_array_mask(p) &&
1861 _g1h->is_in_g1_reserved(oopDesc::load_decode_heap_oop(p)), "sanity");
1862 deal_with_reference(p);
1863 } else {
1864 oop* p = (oop*)ref_to_scan;
1865 assert((has_partial_array_mask(p) && _g1h->obj_in_cs(clear_partial_array_mask(p))) ||
1866 _g1h->is_in_g1_reserved(oopDesc::load_decode_heap_oop(p)), "sanity");
1867 deal_with_reference(p);
1868 }
1869 }
1870 }
1871 }
1872 }
1873 };