Tue, 12 Oct 2010 09:36:48 -0700
6971296: G1: simplify G1RemSet class hierarchy
Summary: Remove G1RemSet base class and StupidG1RemSet class; rename HRInto_G1RemSet to just G1RemSet.
Reviewed-by: ysr, tonyp
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:
660 SubTasksDone* process_strong_tasks() { return _process_strong_tasks; }
662 void set_refine_cte_cl_concurrency(bool concurrent);
664 RefToScanQueue *task_queue(int i) const;
666 // A set of cards where updates happened during the GC
667 DirtyCardQueueSet& dirty_card_queue_set() { return _dirty_card_queue_set; }
669 // A DirtyCardQueueSet that is used to hold cards that contain
670 // references into the current collection set. This is used to
671 // update the remembered sets of the regions in the collection
672 // set in the event of an evacuation failure.
673 DirtyCardQueueSet& into_cset_dirty_card_queue_set()
674 { return _into_cset_dirty_card_queue_set; }
676 // Create a G1CollectedHeap with the specified policy.
677 // Must call the initialize method afterwards.
678 // May not return if something goes wrong.
679 G1CollectedHeap(G1CollectorPolicy* policy);
681 // Initialize the G1CollectedHeap to have the initial and
682 // maximum sizes, permanent generation, and remembered and barrier sets
683 // specified by the policy object.
684 jint initialize();
686 void ref_processing_init();
688 void set_par_threads(int t) {
689 SharedHeap::set_par_threads(t);
690 _process_strong_tasks->set_n_threads(t);
691 }
693 virtual CollectedHeap::Name kind() const {
694 return CollectedHeap::G1CollectedHeap;
695 }
697 // The current policy object for the collector.
698 G1CollectorPolicy* g1_policy() const { return _g1_policy; }
700 // Adaptive size policy. No such thing for g1.
701 virtual AdaptiveSizePolicy* size_policy() { return NULL; }
703 // The rem set and barrier set.
704 G1RemSet* g1_rem_set() const { return _g1_rem_set; }
705 ModRefBarrierSet* mr_bs() const { return _mr_bs; }
707 // The rem set iterator.
708 HeapRegionRemSetIterator* rem_set_iterator(int i) {
709 return _rem_set_iterator[i];
710 }
712 HeapRegionRemSetIterator* rem_set_iterator() {
713 return _rem_set_iterator[0];
714 }
716 unsigned get_gc_time_stamp() {
717 return _gc_time_stamp;
718 }
720 void reset_gc_time_stamp() {
721 _gc_time_stamp = 0;
722 OrderAccess::fence();
723 }
725 void increment_gc_time_stamp() {
726 ++_gc_time_stamp;
727 OrderAccess::fence();
728 }
730 void iterate_dirty_card_closure(CardTableEntryClosure* cl,
731 DirtyCardQueue* into_cset_dcq,
732 bool concurrent, int worker_i);
734 // The shared block offset table array.
735 G1BlockOffsetSharedArray* bot_shared() const { return _bot_shared; }
737 // Reference Processing accessor
738 ReferenceProcessor* ref_processor() { return _ref_processor; }
740 // Reserved (g1 only; super method includes perm), capacity and the used
741 // portion in bytes.
742 size_t g1_reserved_obj_bytes() const { return _g1_reserved.byte_size(); }
743 virtual size_t capacity() const;
744 virtual size_t used() const;
745 // This should be called when we're not holding the heap lock. The
746 // result might be a bit inaccurate.
747 size_t used_unlocked() const;
748 size_t recalculate_used() const;
749 #ifndef PRODUCT
750 size_t recalculate_used_regions() const;
751 #endif // PRODUCT
753 // These virtual functions do the actual allocation.
754 virtual HeapWord* mem_allocate(size_t word_size,
755 bool is_noref,
756 bool is_tlab,
757 bool* gc_overhead_limit_was_exceeded);
759 // Some heaps may offer a contiguous region for shared non-blocking
760 // allocation, via inlined code (by exporting the address of the top and
761 // end fields defining the extent of the contiguous allocation region.)
762 // But G1CollectedHeap doesn't yet support this.
764 // Return an estimate of the maximum allocation that could be performed
765 // without triggering any collection or expansion activity. In a
766 // generational collector, for example, this is probably the largest
767 // allocation that could be supported (without expansion) in the youngest
768 // generation. It is "unsafe" because no locks are taken; the result
769 // should be treated as an approximation, not a guarantee, for use in
770 // heuristic resizing decisions.
771 virtual size_t unsafe_max_alloc();
773 virtual bool is_maximal_no_gc() const {
774 return _g1_storage.uncommitted_size() == 0;
775 }
777 // The total number of regions in the heap.
778 size_t n_regions();
780 // The number of regions that are completely free.
781 size_t max_regions();
783 // The number of regions that are completely free.
784 size_t free_regions();
786 // The number of regions that are not completely free.
787 size_t used_regions() { return n_regions() - free_regions(); }
789 // True iff the ZF thread should run.
790 bool should_zf();
792 // The number of regions available for "regular" expansion.
793 size_t expansion_regions() { return _expansion_regions; }
795 #ifndef PRODUCT
796 bool regions_accounted_for();
797 bool print_region_accounting_info();
798 void print_region_counts();
799 #endif
801 HeapRegion* alloc_region_from_unclean_list(bool zero_filled);
802 HeapRegion* alloc_region_from_unclean_list_locked(bool zero_filled);
804 void put_region_on_unclean_list(HeapRegion* r);
805 void put_region_on_unclean_list_locked(HeapRegion* r);
807 void prepend_region_list_on_unclean_list(UncleanRegionList* list);
808 void prepend_region_list_on_unclean_list_locked(UncleanRegionList* list);
810 void set_unclean_regions_coming(bool b);
811 void set_unclean_regions_coming_locked(bool b);
812 // Wait for cleanup to be complete.
813 void wait_for_cleanup_complete();
814 // Like above, but assumes that the calling thread owns the Heap_lock.
815 void wait_for_cleanup_complete_locked();
817 // Return the head of the unclean list.
818 HeapRegion* peek_unclean_region_list_locked();
819 // Remove and return the head of the unclean list.
820 HeapRegion* pop_unclean_region_list_locked();
822 // List of regions which are zero filled and ready for allocation.
823 HeapRegion* _free_region_list;
824 // Number of elements on the free list.
825 size_t _free_region_list_size;
827 // If the head of the unclean list is ZeroFilled, move it to the free
828 // list.
829 bool move_cleaned_region_to_free_list_locked();
830 bool move_cleaned_region_to_free_list();
832 void put_free_region_on_list_locked(HeapRegion* r);
833 void put_free_region_on_list(HeapRegion* r);
835 // Remove and return the head element of the free list.
836 HeapRegion* pop_free_region_list_locked();
838 // If "zero_filled" is true, we first try the free list, then we try the
839 // unclean list, zero-filling the result. If "zero_filled" is false, we
840 // first try the unclean list, then the zero-filled list.
841 HeapRegion* alloc_free_region_from_lists(bool zero_filled);
843 // Verify the integrity of the region lists.
844 void remove_allocated_regions_from_lists();
845 bool verify_region_lists();
846 bool verify_region_lists_locked();
847 size_t unclean_region_list_length();
848 size_t free_region_list_length();
850 // Perform a collection of the heap; intended for use in implementing
851 // "System.gc". This probably implies as full a collection as the
852 // "CollectedHeap" supports.
853 virtual void collect(GCCause::Cause cause);
855 // The same as above but assume that the caller holds the Heap_lock.
856 void collect_locked(GCCause::Cause cause);
858 // This interface assumes that it's being called by the
859 // vm thread. It collects the heap assuming that the
860 // heap lock is already held and that we are executing in
861 // the context of the vm thread.
862 virtual void collect_as_vm_thread(GCCause::Cause cause);
864 // True iff a evacuation has failed in the most-recent collection.
865 bool evacuation_failed() { return _evacuation_failed; }
867 // Free a region if it is totally full of garbage. Returns the number of
868 // bytes freed (0 ==> didn't free it).
869 size_t free_region_if_totally_empty(HeapRegion *hr);
870 void free_region_if_totally_empty_work(HeapRegion *hr,
871 size_t& pre_used,
872 size_t& cleared_h_regions,
873 size_t& freed_regions,
874 UncleanRegionList* list,
875 bool par = false);
877 // If we've done free region work that yields the given changes, update
878 // the relevant global variables.
879 void finish_free_region_work(size_t pre_used,
880 size_t cleared_h_regions,
881 size_t freed_regions,
882 UncleanRegionList* list);
885 // Returns "TRUE" iff "p" points into the allocated area of the heap.
886 virtual bool is_in(const void* p) const;
888 // Return "TRUE" iff the given object address is within the collection
889 // set.
890 inline bool obj_in_cs(oop obj);
892 // Return "TRUE" iff the given object address is in the reserved
893 // region of g1 (excluding the permanent generation).
894 bool is_in_g1_reserved(const void* p) const {
895 return _g1_reserved.contains(p);
896 }
898 // Returns a MemRegion that corresponds to the space that has been
899 // committed in the heap
900 MemRegion g1_committed() {
901 return _g1_committed;
902 }
904 NOT_PRODUCT(bool is_in_closed_subset(const void* p) const;)
906 // Dirty card table entries covering a list of young regions.
907 void dirtyCardsForYoungRegions(CardTableModRefBS* ct_bs, HeapRegion* list);
909 // This resets the card table to all zeros. It is used after
910 // a collection pause which used the card table to claim cards.
911 void cleanUpCardTable();
913 // Iteration functions.
915 // Iterate over all the ref-containing fields of all objects, calling
916 // "cl.do_oop" on each.
917 virtual void oop_iterate(OopClosure* cl) {
918 oop_iterate(cl, true);
919 }
920 void oop_iterate(OopClosure* cl, bool do_perm);
922 // Same as above, restricted to a memory region.
923 virtual void oop_iterate(MemRegion mr, OopClosure* cl) {
924 oop_iterate(mr, cl, true);
925 }
926 void oop_iterate(MemRegion mr, OopClosure* cl, bool do_perm);
928 // Iterate over all objects, calling "cl.do_object" on each.
929 virtual void object_iterate(ObjectClosure* cl) {
930 object_iterate(cl, true);
931 }
932 virtual void safe_object_iterate(ObjectClosure* cl) {
933 object_iterate(cl, true);
934 }
935 void object_iterate(ObjectClosure* cl, bool do_perm);
937 // Iterate over all objects allocated since the last collection, calling
938 // "cl.do_object" on each. The heap must have been initialized properly
939 // to support this function, or else this call will fail.
940 virtual void object_iterate_since_last_GC(ObjectClosure* cl);
942 // Iterate over all spaces in use in the heap, in ascending address order.
943 virtual void space_iterate(SpaceClosure* cl);
945 // Iterate over heap regions, in address order, terminating the
946 // iteration early if the "doHeapRegion" method returns "true".
947 void heap_region_iterate(HeapRegionClosure* blk);
949 // Iterate over heap regions starting with r (or the first region if "r"
950 // is NULL), in address order, terminating early if the "doHeapRegion"
951 // method returns "true".
952 void heap_region_iterate_from(HeapRegion* r, HeapRegionClosure* blk);
954 // As above but starting from the region at index idx.
955 void heap_region_iterate_from(int idx, HeapRegionClosure* blk);
957 HeapRegion* region_at(size_t idx);
959 // Divide the heap region sequence into "chunks" of some size (the number
960 // of regions divided by the number of parallel threads times some
961 // overpartition factor, currently 4). Assumes that this will be called
962 // in parallel by ParallelGCThreads worker threads with discinct worker
963 // ids in the range [0..max(ParallelGCThreads-1, 1)], that all parallel
964 // calls will use the same "claim_value", and that that claim value is
965 // different from the claim_value of any heap region before the start of
966 // the iteration. Applies "blk->doHeapRegion" to each of the regions, by
967 // attempting to claim the first region in each chunk, and, if
968 // successful, applying the closure to each region in the chunk (and
969 // setting the claim value of the second and subsequent regions of the
970 // chunk.) For now requires that "doHeapRegion" always returns "false",
971 // i.e., that a closure never attempt to abort a traversal.
972 void heap_region_par_iterate_chunked(HeapRegionClosure* blk,
973 int worker,
974 jint claim_value);
976 // It resets all the region claim values to the default.
977 void reset_heap_region_claim_values();
979 #ifdef ASSERT
980 bool check_heap_region_claim_values(jint claim_value);
981 #endif // ASSERT
983 // Iterate over the regions (if any) in the current collection set.
984 void collection_set_iterate(HeapRegionClosure* blk);
986 // As above but starting from region r
987 void collection_set_iterate_from(HeapRegion* r, HeapRegionClosure *blk);
989 // Returns the first (lowest address) compactible space in the heap.
990 virtual CompactibleSpace* first_compactible_space();
992 // A CollectedHeap will contain some number of spaces. This finds the
993 // space containing a given address, or else returns NULL.
994 virtual Space* space_containing(const void* addr) const;
996 // A G1CollectedHeap will contain some number of heap regions. This
997 // finds the region containing a given address, or else returns NULL.
998 HeapRegion* heap_region_containing(const void* addr) const;
1000 // Like the above, but requires "addr" to be in the heap (to avoid a
1001 // null-check), and unlike the above, may return an continuing humongous
1002 // region.
1003 HeapRegion* heap_region_containing_raw(const void* addr) const;
1005 // A CollectedHeap is divided into a dense sequence of "blocks"; that is,
1006 // each address in the (reserved) heap is a member of exactly
1007 // one block. The defining characteristic of a block is that it is
1008 // possible to find its size, and thus to progress forward to the next
1009 // block. (Blocks may be of different sizes.) Thus, blocks may
1010 // represent Java objects, or they might be free blocks in a
1011 // free-list-based heap (or subheap), as long as the two kinds are
1012 // distinguishable and the size of each is determinable.
1014 // Returns the address of the start of the "block" that contains the
1015 // address "addr". We say "blocks" instead of "object" since some heaps
1016 // may not pack objects densely; a chunk may either be an object or a
1017 // non-object.
1018 virtual HeapWord* block_start(const void* addr) const;
1020 // Requires "addr" to be the start of a chunk, and returns its size.
1021 // "addr + size" is required to be the start of a new chunk, or the end
1022 // of the active area of the heap.
1023 virtual size_t block_size(const HeapWord* addr) const;
1025 // Requires "addr" to be the start of a block, and returns "TRUE" iff
1026 // the block is an object.
1027 virtual bool block_is_obj(const HeapWord* addr) const;
1029 // Does this heap support heap inspection? (+PrintClassHistogram)
1030 virtual bool supports_heap_inspection() const { return true; }
1032 // Section on thread-local allocation buffers (TLABs)
1033 // See CollectedHeap for semantics.
1035 virtual bool supports_tlab_allocation() const;
1036 virtual size_t tlab_capacity(Thread* thr) const;
1037 virtual size_t unsafe_max_tlab_alloc(Thread* thr) const;
1038 virtual HeapWord* allocate_new_tlab(size_t word_size);
1040 // Can a compiler initialize a new object without store barriers?
1041 // This permission only extends from the creation of a new object
1042 // via a TLAB up to the first subsequent safepoint. If such permission
1043 // is granted for this heap type, the compiler promises to call
1044 // defer_store_barrier() below on any slow path allocation of
1045 // a new object for which such initializing store barriers will
1046 // have been elided. G1, like CMS, allows this, but should be
1047 // ready to provide a compensating write barrier as necessary
1048 // if that storage came out of a non-young region. The efficiency
1049 // of this implementation depends crucially on being able to
1050 // answer very efficiently in constant time whether a piece of
1051 // storage in the heap comes from a young region or not.
1052 // See ReduceInitialCardMarks.
1053 virtual bool can_elide_tlab_store_barriers() const {
1054 // 6920090: Temporarily disabled, because of lingering
1055 // instabilities related to RICM with G1. In the
1056 // interim, the option ReduceInitialCardMarksForG1
1057 // below is left solely as a debugging device at least
1058 // until 6920109 fixes the instabilities.
1059 return ReduceInitialCardMarksForG1;
1060 }
1062 virtual bool card_mark_must_follow_store() const {
1063 return true;
1064 }
1066 bool is_in_young(oop obj) {
1067 HeapRegion* hr = heap_region_containing(obj);
1068 return hr != NULL && hr->is_young();
1069 }
1071 // We don't need barriers for initializing stores to objects
1072 // in the young gen: for the SATB pre-barrier, there is no
1073 // pre-value that needs to be remembered; for the remembered-set
1074 // update logging post-barrier, we don't maintain remembered set
1075 // information for young gen objects. Note that non-generational
1076 // G1 does not have any "young" objects, should not elide
1077 // the rs logging barrier and so should always answer false below.
1078 // However, non-generational G1 (-XX:-G1Gen) appears to have
1079 // bit-rotted so was not tested below.
1080 virtual bool can_elide_initializing_store_barrier(oop new_obj) {
1081 // Re 6920090, 6920109 above.
1082 assert(ReduceInitialCardMarksForG1, "Else cannot be here");
1083 assert(G1Gen || !is_in_young(new_obj),
1084 "Non-generational G1 should never return true below");
1085 return is_in_young(new_obj);
1086 }
1088 // Can a compiler elide a store barrier when it writes
1089 // a permanent oop into the heap? Applies when the compiler
1090 // is storing x to the heap, where x->is_perm() is true.
1091 virtual bool can_elide_permanent_oop_store_barriers() const {
1092 // At least until perm gen collection is also G1-ified, at
1093 // which point this should return false.
1094 return true;
1095 }
1097 virtual bool allocs_are_zero_filled();
1099 // The boundary between a "large" and "small" array of primitives, in
1100 // words.
1101 virtual size_t large_typearray_limit();
1103 // Returns "true" iff the given word_size is "very large".
1104 static bool isHumongous(size_t word_size) {
1105 // Note this has to be strictly greater-than as the TLABs
1106 // are capped at the humongous thresold and we want to
1107 // ensure that we don't try to allocate a TLAB as
1108 // humongous and that we don't allocate a humongous
1109 // object in a TLAB.
1110 return word_size > _humongous_object_threshold_in_words;
1111 }
1113 // Update mod union table with the set of dirty cards.
1114 void updateModUnion();
1116 // Set the mod union bits corresponding to the given memRegion. Note
1117 // that this is always a safe operation, since it doesn't clear any
1118 // bits.
1119 void markModUnionRange(MemRegion mr);
1121 // Records the fact that a marking phase is no longer in progress.
1122 void set_marking_complete() {
1123 _mark_in_progress = false;
1124 }
1125 void set_marking_started() {
1126 _mark_in_progress = true;
1127 }
1128 bool mark_in_progress() {
1129 return _mark_in_progress;
1130 }
1132 // Print the maximum heap capacity.
1133 virtual size_t max_capacity() const;
1135 virtual jlong millis_since_last_gc();
1137 // Perform any cleanup actions necessary before allowing a verification.
1138 virtual void prepare_for_verify();
1140 // Perform verification.
1142 // use_prev_marking == true -> use "prev" marking information,
1143 // use_prev_marking == false -> use "next" marking information
1144 // NOTE: Only the "prev" marking information is guaranteed to be
1145 // consistent most of the time, so most calls to this should use
1146 // use_prev_marking == true. Currently, there is only one case where
1147 // this is called with use_prev_marking == false, which is to verify
1148 // the "next" marking information at the end of remark.
1149 void verify(bool allow_dirty, bool silent, bool use_prev_marking);
1151 // Override; it uses the "prev" marking information
1152 virtual void verify(bool allow_dirty, bool silent);
1153 // Default behavior by calling print(tty);
1154 virtual void print() const;
1155 // This calls print_on(st, PrintHeapAtGCExtended).
1156 virtual void print_on(outputStream* st) const;
1157 // If extended is true, it will print out information for all
1158 // regions in the heap by calling print_on_extended(st).
1159 virtual void print_on(outputStream* st, bool extended) const;
1160 virtual void print_on_extended(outputStream* st) const;
1162 virtual void print_gc_threads_on(outputStream* st) const;
1163 virtual void gc_threads_do(ThreadClosure* tc) const;
1165 // Override
1166 void print_tracing_info() const;
1168 // If "addr" is a pointer into the (reserved?) heap, returns a positive
1169 // number indicating the "arena" within the heap in which "addr" falls.
1170 // Or else returns 0.
1171 virtual int addr_to_arena_id(void* addr) const;
1173 // Convenience function to be used in situations where the heap type can be
1174 // asserted to be this type.
1175 static G1CollectedHeap* heap();
1177 void empty_young_list();
1178 bool should_set_young_locked();
1180 void set_region_short_lived_locked(HeapRegion* hr);
1181 // add appropriate methods for any other surv rate groups
1183 YoungList* young_list() { return _young_list; }
1185 // debugging
1186 bool check_young_list_well_formed() {
1187 return _young_list->check_list_well_formed();
1188 }
1190 bool check_young_list_empty(bool check_heap,
1191 bool check_sample = true);
1193 // *** Stuff related to concurrent marking. It's not clear to me that so
1194 // many of these need to be public.
1196 // The functions below are helper functions that a subclass of
1197 // "CollectedHeap" can use in the implementation of its virtual
1198 // functions.
1199 // This performs a concurrent marking of the live objects in a
1200 // bitmap off to the side.
1201 void doConcurrentMark();
1203 // This is called from the marksweep collector which then does
1204 // a concurrent mark and verifies that the results agree with
1205 // the stop the world marking.
1206 void checkConcurrentMark();
1207 void do_sync_mark();
1209 bool isMarkedPrev(oop obj) const;
1210 bool isMarkedNext(oop obj) const;
1212 // use_prev_marking == true -> use "prev" marking information,
1213 // use_prev_marking == false -> use "next" marking information
1214 bool is_obj_dead_cond(const oop obj,
1215 const HeapRegion* hr,
1216 const bool use_prev_marking) const {
1217 if (use_prev_marking) {
1218 return is_obj_dead(obj, hr);
1219 } else {
1220 return is_obj_ill(obj, hr);
1221 }
1222 }
1224 // Determine if an object is dead, given the object and also
1225 // the region to which the object belongs. An object is dead
1226 // iff a) it was not allocated since the last mark and b) it
1227 // is not marked.
1229 bool is_obj_dead(const oop obj, const HeapRegion* hr) const {
1230 return
1231 !hr->obj_allocated_since_prev_marking(obj) &&
1232 !isMarkedPrev(obj);
1233 }
1235 // This is used when copying an object to survivor space.
1236 // If the object is marked live, then we mark the copy live.
1237 // If the object is allocated since the start of this mark
1238 // cycle, then we mark the copy live.
1239 // If the object has been around since the previous mark
1240 // phase, and hasn't been marked yet during this phase,
1241 // then we don't mark it, we just wait for the
1242 // current marking cycle to get to it.
1244 // This function returns true when an object has been
1245 // around since the previous marking and hasn't yet
1246 // been marked during this marking.
1248 bool is_obj_ill(const oop obj, const HeapRegion* hr) const {
1249 return
1250 !hr->obj_allocated_since_next_marking(obj) &&
1251 !isMarkedNext(obj);
1252 }
1254 // Determine if an object is dead, given only the object itself.
1255 // This will find the region to which the object belongs and
1256 // then call the region version of the same function.
1258 // Added if it is in permanent gen it isn't dead.
1259 // Added if it is NULL it isn't dead.
1261 // use_prev_marking == true -> use "prev" marking information,
1262 // use_prev_marking == false -> use "next" marking information
1263 bool is_obj_dead_cond(const oop obj,
1264 const bool use_prev_marking) {
1265 if (use_prev_marking) {
1266 return is_obj_dead(obj);
1267 } else {
1268 return is_obj_ill(obj);
1269 }
1270 }
1272 bool is_obj_dead(const oop obj) {
1273 const HeapRegion* hr = heap_region_containing(obj);
1274 if (hr == NULL) {
1275 if (Universe::heap()->is_in_permanent(obj))
1276 return false;
1277 else if (obj == NULL) return false;
1278 else return true;
1279 }
1280 else return is_obj_dead(obj, hr);
1281 }
1283 bool is_obj_ill(const oop obj) {
1284 const HeapRegion* hr = heap_region_containing(obj);
1285 if (hr == NULL) {
1286 if (Universe::heap()->is_in_permanent(obj))
1287 return false;
1288 else if (obj == NULL) return false;
1289 else return true;
1290 }
1291 else return is_obj_ill(obj, hr);
1292 }
1294 // The following is just to alert the verification code
1295 // that a full collection has occurred and that the
1296 // remembered sets are no longer up to date.
1297 bool _full_collection;
1298 void set_full_collection() { _full_collection = true;}
1299 void clear_full_collection() {_full_collection = false;}
1300 bool full_collection() {return _full_collection;}
1302 ConcurrentMark* concurrent_mark() const { return _cm; }
1303 ConcurrentG1Refine* concurrent_g1_refine() const { return _cg1r; }
1305 // The dirty cards region list is used to record a subset of regions
1306 // whose cards need clearing. The list if populated during the
1307 // remembered set scanning and drained during the card table
1308 // cleanup. Although the methods are reentrant, population/draining
1309 // phases must not overlap. For synchronization purposes the last
1310 // element on the list points to itself.
1311 HeapRegion* _dirty_cards_region_list;
1312 void push_dirty_cards_region(HeapRegion* hr);
1313 HeapRegion* pop_dirty_cards_region();
1315 public:
1316 void stop_conc_gc_threads();
1318 // <NEW PREDICTION>
1320 double predict_region_elapsed_time_ms(HeapRegion* hr, bool young);
1321 void check_if_region_is_too_expensive(double predicted_time_ms);
1322 size_t pending_card_num();
1323 size_t max_pending_card_num();
1324 size_t cards_scanned();
1326 // </NEW PREDICTION>
1328 protected:
1329 size_t _max_heap_capacity;
1331 // debug_only(static void check_for_valid_allocation_state();)
1333 public:
1334 // Temporary: call to mark things unimplemented for the G1 heap (e.g.,
1335 // MemoryService). In productization, we can make this assert false
1336 // to catch such places (as well as searching for calls to this...)
1337 static void g1_unimplemented();
1339 };
1341 #define use_local_bitmaps 1
1342 #define verify_local_bitmaps 0
1343 #define oop_buffer_length 256
1345 #ifndef PRODUCT
1346 class GCLabBitMap;
1347 class GCLabBitMapClosure: public BitMapClosure {
1348 private:
1349 ConcurrentMark* _cm;
1350 GCLabBitMap* _bitmap;
1352 public:
1353 GCLabBitMapClosure(ConcurrentMark* cm,
1354 GCLabBitMap* bitmap) {
1355 _cm = cm;
1356 _bitmap = bitmap;
1357 }
1359 virtual bool do_bit(size_t offset);
1360 };
1361 #endif // !PRODUCT
1363 class GCLabBitMap: public BitMap {
1364 private:
1365 ConcurrentMark* _cm;
1367 int _shifter;
1368 size_t _bitmap_word_covers_words;
1370 // beginning of the heap
1371 HeapWord* _heap_start;
1373 // this is the actual start of the GCLab
1374 HeapWord* _real_start_word;
1376 // this is the actual end of the GCLab
1377 HeapWord* _real_end_word;
1379 // this is the first word, possibly located before the actual start
1380 // of the GCLab, that corresponds to the first bit of the bitmap
1381 HeapWord* _start_word;
1383 // size of a GCLab in words
1384 size_t _gclab_word_size;
1386 static int shifter() {
1387 return MinObjAlignment - 1;
1388 }
1390 // how many heap words does a single bitmap word corresponds to?
1391 static size_t bitmap_word_covers_words() {
1392 return BitsPerWord << shifter();
1393 }
1395 size_t gclab_word_size() const {
1396 return _gclab_word_size;
1397 }
1399 // Calculates actual GCLab size in words
1400 size_t gclab_real_word_size() const {
1401 return bitmap_size_in_bits(pointer_delta(_real_end_word, _start_word))
1402 / BitsPerWord;
1403 }
1405 static size_t bitmap_size_in_bits(size_t gclab_word_size) {
1406 size_t bits_in_bitmap = gclab_word_size >> shifter();
1407 // We are going to ensure that the beginning of a word in this
1408 // bitmap also corresponds to the beginning of a word in the
1409 // global marking bitmap. To handle the case where a GCLab
1410 // starts from the middle of the bitmap, we need to add enough
1411 // space (i.e. up to a bitmap word) to ensure that we have
1412 // enough bits in the bitmap.
1413 return bits_in_bitmap + BitsPerWord - 1;
1414 }
1415 public:
1416 GCLabBitMap(HeapWord* heap_start, size_t gclab_word_size)
1417 : BitMap(bitmap_size_in_bits(gclab_word_size)),
1418 _cm(G1CollectedHeap::heap()->concurrent_mark()),
1419 _shifter(shifter()),
1420 _bitmap_word_covers_words(bitmap_word_covers_words()),
1421 _heap_start(heap_start),
1422 _gclab_word_size(gclab_word_size),
1423 _real_start_word(NULL),
1424 _real_end_word(NULL),
1425 _start_word(NULL)
1426 {
1427 guarantee( size_in_words() >= bitmap_size_in_words(),
1428 "just making sure");
1429 }
1431 inline unsigned heapWordToOffset(HeapWord* addr) {
1432 unsigned offset = (unsigned) pointer_delta(addr, _start_word) >> _shifter;
1433 assert(offset < size(), "offset should be within bounds");
1434 return offset;
1435 }
1437 inline HeapWord* offsetToHeapWord(size_t offset) {
1438 HeapWord* addr = _start_word + (offset << _shifter);
1439 assert(_real_start_word <= addr && addr < _real_end_word, "invariant");
1440 return addr;
1441 }
1443 bool fields_well_formed() {
1444 bool ret1 = (_real_start_word == NULL) &&
1445 (_real_end_word == NULL) &&
1446 (_start_word == NULL);
1447 if (ret1)
1448 return true;
1450 bool ret2 = _real_start_word >= _start_word &&
1451 _start_word < _real_end_word &&
1452 (_real_start_word + _gclab_word_size) == _real_end_word &&
1453 (_start_word + _gclab_word_size + _bitmap_word_covers_words)
1454 > _real_end_word;
1455 return ret2;
1456 }
1458 inline bool mark(HeapWord* addr) {
1459 guarantee(use_local_bitmaps, "invariant");
1460 assert(fields_well_formed(), "invariant");
1462 if (addr >= _real_start_word && addr < _real_end_word) {
1463 assert(!isMarked(addr), "should not have already been marked");
1465 // first mark it on the bitmap
1466 at_put(heapWordToOffset(addr), true);
1468 return true;
1469 } else {
1470 return false;
1471 }
1472 }
1474 inline bool isMarked(HeapWord* addr) {
1475 guarantee(use_local_bitmaps, "invariant");
1476 assert(fields_well_formed(), "invariant");
1478 return at(heapWordToOffset(addr));
1479 }
1481 void set_buffer(HeapWord* start) {
1482 guarantee(use_local_bitmaps, "invariant");
1483 clear();
1485 assert(start != NULL, "invariant");
1486 _real_start_word = start;
1487 _real_end_word = start + _gclab_word_size;
1489 size_t diff =
1490 pointer_delta(start, _heap_start) % _bitmap_word_covers_words;
1491 _start_word = start - diff;
1493 assert(fields_well_formed(), "invariant");
1494 }
1496 #ifndef PRODUCT
1497 void verify() {
1498 // verify that the marks have been propagated
1499 GCLabBitMapClosure cl(_cm, this);
1500 iterate(&cl);
1501 }
1502 #endif // PRODUCT
1504 void retire() {
1505 guarantee(use_local_bitmaps, "invariant");
1506 assert(fields_well_formed(), "invariant");
1508 if (_start_word != NULL) {
1509 CMBitMap* mark_bitmap = _cm->nextMarkBitMap();
1511 // this means that the bitmap was set up for the GCLab
1512 assert(_real_start_word != NULL && _real_end_word != NULL, "invariant");
1514 mark_bitmap->mostly_disjoint_range_union(this,
1515 0, // always start from the start of the bitmap
1516 _start_word,
1517 gclab_real_word_size());
1518 _cm->grayRegionIfNecessary(MemRegion(_real_start_word, _real_end_word));
1520 #ifndef PRODUCT
1521 if (use_local_bitmaps && verify_local_bitmaps)
1522 verify();
1523 #endif // PRODUCT
1524 } else {
1525 assert(_real_start_word == NULL && _real_end_word == NULL, "invariant");
1526 }
1527 }
1529 size_t bitmap_size_in_words() const {
1530 return (bitmap_size_in_bits(gclab_word_size()) + BitsPerWord - 1) / BitsPerWord;
1531 }
1533 };
1535 class G1ParGCAllocBuffer: public ParGCAllocBuffer {
1536 private:
1537 bool _retired;
1538 bool _during_marking;
1539 GCLabBitMap _bitmap;
1541 public:
1542 G1ParGCAllocBuffer(size_t gclab_word_size) :
1543 ParGCAllocBuffer(gclab_word_size),
1544 _during_marking(G1CollectedHeap::heap()->mark_in_progress()),
1545 _bitmap(G1CollectedHeap::heap()->reserved_region().start(), gclab_word_size),
1546 _retired(false)
1547 { }
1549 inline bool mark(HeapWord* addr) {
1550 guarantee(use_local_bitmaps, "invariant");
1551 assert(_during_marking, "invariant");
1552 return _bitmap.mark(addr);
1553 }
1555 inline void set_buf(HeapWord* buf) {
1556 if (use_local_bitmaps && _during_marking)
1557 _bitmap.set_buffer(buf);
1558 ParGCAllocBuffer::set_buf(buf);
1559 _retired = false;
1560 }
1562 inline void retire(bool end_of_gc, bool retain) {
1563 if (_retired)
1564 return;
1565 if (use_local_bitmaps && _during_marking) {
1566 _bitmap.retire();
1567 }
1568 ParGCAllocBuffer::retire(end_of_gc, retain);
1569 _retired = true;
1570 }
1571 };
1573 class G1ParScanThreadState : public StackObj {
1574 protected:
1575 G1CollectedHeap* _g1h;
1576 RefToScanQueue* _refs;
1577 DirtyCardQueue _dcq;
1578 CardTableModRefBS* _ct_bs;
1579 G1RemSet* _g1_rem;
1581 G1ParGCAllocBuffer _surviving_alloc_buffer;
1582 G1ParGCAllocBuffer _tenured_alloc_buffer;
1583 G1ParGCAllocBuffer* _alloc_buffers[GCAllocPurposeCount];
1584 ageTable _age_table;
1586 size_t _alloc_buffer_waste;
1587 size_t _undo_waste;
1589 OopsInHeapRegionClosure* _evac_failure_cl;
1590 G1ParScanHeapEvacClosure* _evac_cl;
1591 G1ParScanPartialArrayClosure* _partial_scan_cl;
1593 int _hash_seed;
1594 int _queue_num;
1596 size_t _term_attempts;
1598 double _start;
1599 double _start_strong_roots;
1600 double _strong_roots_time;
1601 double _start_term;
1602 double _term_time;
1604 // Map from young-age-index (0 == not young, 1 is youngest) to
1605 // surviving words. base is what we get back from the malloc call
1606 size_t* _surviving_young_words_base;
1607 // this points into the array, as we use the first few entries for padding
1608 size_t* _surviving_young_words;
1610 #define PADDING_ELEM_NUM (DEFAULT_CACHE_LINE_SIZE / sizeof(size_t))
1612 void add_to_alloc_buffer_waste(size_t waste) { _alloc_buffer_waste += waste; }
1614 void add_to_undo_waste(size_t waste) { _undo_waste += waste; }
1616 DirtyCardQueue& dirty_card_queue() { return _dcq; }
1617 CardTableModRefBS* ctbs() { return _ct_bs; }
1619 template <class T> void immediate_rs_update(HeapRegion* from, T* p, int tid) {
1620 if (!from->is_survivor()) {
1621 _g1_rem->par_write_ref(from, p, tid);
1622 }
1623 }
1625 template <class T> void deferred_rs_update(HeapRegion* from, T* p, int tid) {
1626 // If the new value of the field points to the same region or
1627 // is the to-space, we don't need to include it in the Rset updates.
1628 if (!from->is_in_reserved(oopDesc::load_decode_heap_oop(p)) && !from->is_survivor()) {
1629 size_t card_index = ctbs()->index_for(p);
1630 // If the card hasn't been added to the buffer, do it.
1631 if (ctbs()->mark_card_deferred(card_index)) {
1632 dirty_card_queue().enqueue((jbyte*)ctbs()->byte_for_index(card_index));
1633 }
1634 }
1635 }
1637 public:
1638 G1ParScanThreadState(G1CollectedHeap* g1h, int queue_num);
1640 ~G1ParScanThreadState() {
1641 FREE_C_HEAP_ARRAY(size_t, _surviving_young_words_base);
1642 }
1644 RefToScanQueue* refs() { return _refs; }
1645 ageTable* age_table() { return &_age_table; }
1647 G1ParGCAllocBuffer* alloc_buffer(GCAllocPurpose purpose) {
1648 return _alloc_buffers[purpose];
1649 }
1651 size_t alloc_buffer_waste() const { return _alloc_buffer_waste; }
1652 size_t undo_waste() const { return _undo_waste; }
1654 template <class T> void push_on_queue(T* ref) {
1655 assert(ref != NULL, "invariant");
1656 assert(has_partial_array_mask(ref) ||
1657 _g1h->is_in_g1_reserved(oopDesc::load_decode_heap_oop(ref)), "invariant");
1658 #ifdef ASSERT
1659 if (has_partial_array_mask(ref)) {
1660 oop p = clear_partial_array_mask(ref);
1661 // Verify that we point into the CS
1662 assert(_g1h->obj_in_cs(p), "Should be in CS");
1663 }
1664 #endif
1665 refs()->push(ref);
1666 }
1668 void pop_from_queue(StarTask& ref) {
1669 if (refs()->pop_local(ref)) {
1670 assert((oop*)ref != NULL, "pop_local() returned true");
1671 assert(UseCompressedOops || !ref.is_narrow(), "Error");
1672 assert(has_partial_array_mask((oop*)ref) ||
1673 _g1h->is_in_g1_reserved(ref.is_narrow() ? oopDesc::load_decode_heap_oop((narrowOop*)ref)
1674 : oopDesc::load_decode_heap_oop((oop*)ref)),
1675 "invariant");
1676 } else {
1677 StarTask null_task;
1678 ref = null_task;
1679 }
1680 }
1682 void pop_from_overflow_queue(StarTask& ref) {
1683 StarTask new_ref;
1684 refs()->pop_overflow(new_ref);
1685 assert((oop*)new_ref != NULL, "pop() from a local non-empty stack");
1686 assert(UseCompressedOops || !new_ref.is_narrow(), "Error");
1687 assert(has_partial_array_mask((oop*)new_ref) ||
1688 _g1h->is_in_g1_reserved(new_ref.is_narrow() ? oopDesc::load_decode_heap_oop((narrowOop*)new_ref)
1689 : oopDesc::load_decode_heap_oop((oop*)new_ref)),
1690 "invariant");
1691 ref = new_ref;
1692 }
1694 int refs_to_scan() { return (int)refs()->size(); }
1695 int overflowed_refs_to_scan() { return (int)refs()->overflow_stack()->size(); }
1697 template <class T> void update_rs(HeapRegion* from, T* p, int tid) {
1698 if (G1DeferredRSUpdate) {
1699 deferred_rs_update(from, p, tid);
1700 } else {
1701 immediate_rs_update(from, p, tid);
1702 }
1703 }
1705 HeapWord* allocate_slow(GCAllocPurpose purpose, size_t word_sz) {
1707 HeapWord* obj = NULL;
1708 size_t gclab_word_size = _g1h->desired_plab_sz(purpose);
1709 if (word_sz * 100 < gclab_word_size * ParallelGCBufferWastePct) {
1710 G1ParGCAllocBuffer* alloc_buf = alloc_buffer(purpose);
1711 assert(gclab_word_size == alloc_buf->word_sz(),
1712 "dynamic resizing is not supported");
1713 add_to_alloc_buffer_waste(alloc_buf->words_remaining());
1714 alloc_buf->retire(false, false);
1716 HeapWord* buf = _g1h->par_allocate_during_gc(purpose, gclab_word_size);
1717 if (buf == NULL) return NULL; // Let caller handle allocation failure.
1718 // Otherwise.
1719 alloc_buf->set_buf(buf);
1721 obj = alloc_buf->allocate(word_sz);
1722 assert(obj != NULL, "buffer was definitely big enough...");
1723 } else {
1724 obj = _g1h->par_allocate_during_gc(purpose, word_sz);
1725 }
1726 return obj;
1727 }
1729 HeapWord* allocate(GCAllocPurpose purpose, size_t word_sz) {
1730 HeapWord* obj = alloc_buffer(purpose)->allocate(word_sz);
1731 if (obj != NULL) return obj;
1732 return allocate_slow(purpose, word_sz);
1733 }
1735 void undo_allocation(GCAllocPurpose purpose, HeapWord* obj, size_t word_sz) {
1736 if (alloc_buffer(purpose)->contains(obj)) {
1737 assert(alloc_buffer(purpose)->contains(obj + word_sz - 1),
1738 "should contain whole object");
1739 alloc_buffer(purpose)->undo_allocation(obj, word_sz);
1740 } else {
1741 CollectedHeap::fill_with_object(obj, word_sz);
1742 add_to_undo_waste(word_sz);
1743 }
1744 }
1746 void set_evac_failure_closure(OopsInHeapRegionClosure* evac_failure_cl) {
1747 _evac_failure_cl = evac_failure_cl;
1748 }
1749 OopsInHeapRegionClosure* evac_failure_closure() {
1750 return _evac_failure_cl;
1751 }
1753 void set_evac_closure(G1ParScanHeapEvacClosure* evac_cl) {
1754 _evac_cl = evac_cl;
1755 }
1757 void set_partial_scan_closure(G1ParScanPartialArrayClosure* partial_scan_cl) {
1758 _partial_scan_cl = partial_scan_cl;
1759 }
1761 int* hash_seed() { return &_hash_seed; }
1762 int queue_num() { return _queue_num; }
1764 size_t term_attempts() const { return _term_attempts; }
1765 void note_term_attempt() { _term_attempts++; }
1767 void start_strong_roots() {
1768 _start_strong_roots = os::elapsedTime();
1769 }
1770 void end_strong_roots() {
1771 _strong_roots_time += (os::elapsedTime() - _start_strong_roots);
1772 }
1773 double strong_roots_time() const { return _strong_roots_time; }
1775 void start_term_time() {
1776 note_term_attempt();
1777 _start_term = os::elapsedTime();
1778 }
1779 void end_term_time() {
1780 _term_time += (os::elapsedTime() - _start_term);
1781 }
1782 double term_time() const { return _term_time; }
1784 double elapsed_time() const {
1785 return os::elapsedTime() - _start;
1786 }
1788 static void
1789 print_termination_stats_hdr(outputStream* const st = gclog_or_tty);
1790 void
1791 print_termination_stats(int i, outputStream* const st = gclog_or_tty) const;
1793 size_t* surviving_young_words() {
1794 // We add on to hide entry 0 which accumulates surviving words for
1795 // age -1 regions (i.e. non-young ones)
1796 return _surviving_young_words;
1797 }
1799 void retire_alloc_buffers() {
1800 for (int ap = 0; ap < GCAllocPurposeCount; ++ap) {
1801 size_t waste = _alloc_buffers[ap]->words_remaining();
1802 add_to_alloc_buffer_waste(waste);
1803 _alloc_buffers[ap]->retire(true, false);
1804 }
1805 }
1807 private:
1808 template <class T> void deal_with_reference(T* ref_to_scan) {
1809 if (has_partial_array_mask(ref_to_scan)) {
1810 _partial_scan_cl->do_oop_nv(ref_to_scan);
1811 } else {
1812 // Note: we can use "raw" versions of "region_containing" because
1813 // "obj_to_scan" is definitely in the heap, and is not in a
1814 // humongous region.
1815 HeapRegion* r = _g1h->heap_region_containing_raw(ref_to_scan);
1816 _evac_cl->set_region(r);
1817 _evac_cl->do_oop_nv(ref_to_scan);
1818 }
1819 }
1821 public:
1822 void trim_queue() {
1823 // I've replicated the loop twice, first to drain the overflow
1824 // queue, second to drain the task queue. This is better than
1825 // having a single loop, which checks both conditions and, inside
1826 // it, either pops the overflow queue or the task queue, as each
1827 // loop is tighter. Also, the decision to drain the overflow queue
1828 // first is not arbitrary, as the overflow queue is not visible
1829 // to the other workers, whereas the task queue is. So, we want to
1830 // drain the "invisible" entries first, while allowing the other
1831 // workers to potentially steal the "visible" entries.
1833 while (refs_to_scan() > 0 || overflowed_refs_to_scan() > 0) {
1834 while (overflowed_refs_to_scan() > 0) {
1835 StarTask ref_to_scan;
1836 assert((oop*)ref_to_scan == NULL, "Constructed above");
1837 pop_from_overflow_queue(ref_to_scan);
1838 // We shouldn't have pushed it on the queue if it was not
1839 // pointing into the CSet.
1840 assert((oop*)ref_to_scan != NULL, "Follows from inner loop invariant");
1841 if (ref_to_scan.is_narrow()) {
1842 assert(UseCompressedOops, "Error");
1843 narrowOop* p = (narrowOop*)ref_to_scan;
1844 assert(!has_partial_array_mask(p) &&
1845 _g1h->is_in_g1_reserved(oopDesc::load_decode_heap_oop(p)), "sanity");
1846 deal_with_reference(p);
1847 } else {
1848 oop* p = (oop*)ref_to_scan;
1849 assert((has_partial_array_mask(p) && _g1h->is_in_g1_reserved(clear_partial_array_mask(p))) ||
1850 _g1h->is_in_g1_reserved(oopDesc::load_decode_heap_oop(p)), "sanity");
1851 deal_with_reference(p);
1852 }
1853 }
1855 while (refs_to_scan() > 0) {
1856 StarTask ref_to_scan;
1857 assert((oop*)ref_to_scan == NULL, "Constructed above");
1858 pop_from_queue(ref_to_scan);
1859 if ((oop*)ref_to_scan != NULL) {
1860 if (ref_to_scan.is_narrow()) {
1861 assert(UseCompressedOops, "Error");
1862 narrowOop* p = (narrowOop*)ref_to_scan;
1863 assert(!has_partial_array_mask(p) &&
1864 _g1h->is_in_g1_reserved(oopDesc::load_decode_heap_oop(p)), "sanity");
1865 deal_with_reference(p);
1866 } else {
1867 oop* p = (oop*)ref_to_scan;
1868 assert((has_partial_array_mask(p) && _g1h->obj_in_cs(clear_partial_array_mask(p))) ||
1869 _g1h->is_in_g1_reserved(oopDesc::load_decode_heap_oop(p)), "sanity");
1870 deal_with_reference(p);
1871 }
1872 }
1873 }
1874 }
1875 }
1876 };