src/share/vm/gc_implementation/g1/g1CollectedHeap.hpp

Wed, 25 Mar 2009 13:10:54 -0700

author
apetrusenko
date
Wed, 25 Mar 2009 13:10:54 -0700
changeset 1112
96b229c54d1e
parent 1075
ba50942c8138
child 1113
4ac7d97e6101
permissions
-rw-r--r--

6543938: G1: remove the concept of popularity
Reviewed-by: iveresov, tonyp

     1 /*
     2  * Copyright 2001-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 // 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 // If want to accumulate detailed statistics on work queues
    50 // turn this on.
    51 #define G1_DETAILED_STATS 0
    53 #if G1_DETAILED_STATS
    54 #  define IF_G1_DETAILED_STATS(code) code
    55 #else
    56 #  define IF_G1_DETAILED_STATS(code)
    57 #endif
    59 typedef GenericTaskQueue<oop*>    RefToScanQueue;
    60 typedef GenericTaskQueueSet<oop*> RefToScanQueueSet;
    62 enum G1GCThreadGroups {
    63   G1CRGroup = 0,
    64   G1ZFGroup = 1,
    65   G1CMGroup = 2,
    66   G1CLGroup = 3
    67 };
    69 enum GCAllocPurpose {
    70   GCAllocForTenured,
    71   GCAllocForSurvived,
    72   GCAllocPurposeCount
    73 };
    75 class YoungList : public CHeapObj {
    76 private:
    77   G1CollectedHeap* _g1h;
    79   HeapRegion* _head;
    81   HeapRegion* _scan_only_head;
    82   HeapRegion* _scan_only_tail;
    83   size_t      _length;
    84   size_t      _scan_only_length;
    86   size_t      _last_sampled_rs_lengths;
    87   size_t      _sampled_rs_lengths;
    88   HeapRegion* _curr;
    89   HeapRegion* _curr_scan_only;
    91   HeapRegion* _survivor_head;
    92   HeapRegion* _survivor_tail;
    93   size_t      _survivor_length;
    95   void          empty_list(HeapRegion* list);
    97 public:
    98   YoungList(G1CollectedHeap* g1h);
   100   void          push_region(HeapRegion* hr);
   101   void          add_survivor_region(HeapRegion* hr);
   102   HeapRegion*   pop_region();
   103   void          empty_list();
   104   bool          is_empty() { return _length == 0; }
   105   size_t        length() { return _length; }
   106   size_t        scan_only_length() { return _scan_only_length; }
   107   size_t        survivor_length() { return _survivor_length; }
   109   void rs_length_sampling_init();
   110   bool rs_length_sampling_more();
   111   void rs_length_sampling_next();
   113   void reset_sampled_info() {
   114     _last_sampled_rs_lengths =   0;
   115   }
   116   size_t sampled_rs_lengths() { return _last_sampled_rs_lengths; }
   118   // for development purposes
   119   void reset_auxilary_lists();
   120   HeapRegion* first_region() { return _head; }
   121   HeapRegion* first_scan_only_region() { return _scan_only_head; }
   122   HeapRegion* first_survivor_region() { return _survivor_head; }
   123   HeapRegion* last_survivor_region() { return _survivor_tail; }
   124   HeapRegion* par_get_next_scan_only_region() {
   125     MutexLockerEx x(ParGCRareEvent_lock, Mutex::_no_safepoint_check_flag);
   126     HeapRegion* ret = _curr_scan_only;
   127     if (ret != NULL)
   128       _curr_scan_only = ret->get_next_young_region();
   129     return ret;
   130   }
   132   // debugging
   133   bool          check_list_well_formed();
   134   bool          check_list_empty(bool ignore_scan_only_list,
   135                                  bool check_sample = true);
   136   void          print();
   137 };
   139 class RefineCardTableEntryClosure;
   140 class G1CollectedHeap : public SharedHeap {
   141   friend class VM_G1CollectForAllocation;
   142   friend class VM_GenCollectForPermanentAllocation;
   143   friend class VM_G1CollectFull;
   144   friend class VM_G1IncCollectionPause;
   145   friend class VMStructs;
   147   // Closures used in implementation.
   148   friend class G1ParCopyHelper;
   149   friend class G1IsAliveClosure;
   150   friend class G1EvacuateFollowersClosure;
   151   friend class G1ParScanThreadState;
   152   friend class G1ParScanClosureSuper;
   153   friend class G1ParEvacuateFollowersClosure;
   154   friend class G1ParTask;
   155   friend class G1FreeGarbageRegionClosure;
   156   friend class RefineCardTableEntryClosure;
   157   friend class G1PrepareCompactClosure;
   158   friend class RegionSorter;
   159   friend class CountRCClosure;
   160   friend class EvacPopObjClosure;
   162   // Other related classes.
   163   friend class G1MarkSweep;
   165 private:
   166   enum SomePrivateConstants {
   167     VeryLargeInBytes = HeapRegion::GrainBytes/2,
   168     VeryLargeInWords = VeryLargeInBytes/HeapWordSize,
   169     MinHeapDeltaBytes = 10 * HeapRegion::GrainBytes,      // FIXME
   170     NumAPIs = HeapRegion::MaxAge
   171   };
   173   // The one and only G1CollectedHeap, so static functions can find it.
   174   static G1CollectedHeap* _g1h;
   176   // Storage for the G1 heap (excludes the permanent generation).
   177   VirtualSpace _g1_storage;
   178   MemRegion    _g1_reserved;
   180   // The part of _g1_storage that is currently committed.
   181   MemRegion _g1_committed;
   183   // The maximum part of _g1_storage that has ever been committed.
   184   MemRegion _g1_max_committed;
   186   // The number of regions that are completely free.
   187   size_t _free_regions;
   189   // The number of regions we could create by expansion.
   190   size_t _expansion_regions;
   192   // Return the number of free regions in the heap (by direct counting.)
   193   size_t count_free_regions();
   194   // Return the number of free regions on the free and unclean lists.
   195   size_t count_free_regions_list();
   197   // The block offset table for the G1 heap.
   198   G1BlockOffsetSharedArray* _bot_shared;
   200   // Move all of the regions off the free lists, then rebuild those free
   201   // lists, before and after full GC.
   202   void tear_down_region_lists();
   203   void rebuild_region_lists();
   204   // This sets all non-empty regions to need zero-fill (which they will if
   205   // they are empty after full collection.)
   206   void set_used_regions_to_need_zero_fill();
   208   // The sequence of all heap regions in the heap.
   209   HeapRegionSeq* _hrs;
   211   // The region from which normal-sized objects are currently being
   212   // allocated.  May be NULL.
   213   HeapRegion* _cur_alloc_region;
   215   // Postcondition: cur_alloc_region == NULL.
   216   void abandon_cur_alloc_region();
   217   void abandon_gc_alloc_regions();
   219   // The to-space memory regions into which objects are being copied during
   220   // a GC.
   221   HeapRegion* _gc_alloc_regions[GCAllocPurposeCount];
   222   size_t _gc_alloc_region_counts[GCAllocPurposeCount];
   223   // These are the regions, one per GCAllocPurpose, that are half-full
   224   // at the end of a collection and that we want to reuse during the
   225   // next collection.
   226   HeapRegion* _retained_gc_alloc_regions[GCAllocPurposeCount];
   227   // This specifies whether we will keep the last half-full region at
   228   // the end of a collection so that it can be reused during the next
   229   // collection (this is specified per GCAllocPurpose)
   230   bool _retain_gc_alloc_region[GCAllocPurposeCount];
   232   // A list of the regions that have been set to be alloc regions in the
   233   // current collection.
   234   HeapRegion* _gc_alloc_region_list;
   236   // When called by par thread, require par_alloc_during_gc_lock() to be held.
   237   void push_gc_alloc_region(HeapRegion* hr);
   239   // This should only be called single-threaded.  Undeclares all GC alloc
   240   // regions.
   241   void forget_alloc_region_list();
   243   // Should be used to set an alloc region, because there's other
   244   // associated bookkeeping.
   245   void set_gc_alloc_region(int purpose, HeapRegion* r);
   247   // Check well-formedness of alloc region list.
   248   bool check_gc_alloc_regions();
   250   // Outside of GC pauses, the number of bytes used in all regions other
   251   // than the current allocation region.
   252   size_t _summary_bytes_used;
   254   // This is used for a quick test on whether a reference points into
   255   // the collection set or not. Basically, we have an array, with one
   256   // byte per region, and that byte denotes whether the corresponding
   257   // region is in the collection set or not. The entry corresponding
   258   // the bottom of the heap, i.e., region 0, is pointed to by
   259   // _in_cset_fast_test_base.  The _in_cset_fast_test field has been
   260   // biased so that it actually points to address 0 of the address
   261   // space, to make the test as fast as possible (we can simply shift
   262   // the address to address into it, instead of having to subtract the
   263   // bottom of the heap from the address before shifting it; basically
   264   // it works in the same way the card table works).
   265   bool* _in_cset_fast_test;
   267   // The allocated array used for the fast test on whether a reference
   268   // points into the collection set or not. This field is also used to
   269   // free the array.
   270   bool* _in_cset_fast_test_base;
   272   // The length of the _in_cset_fast_test_base array.
   273   size_t _in_cset_fast_test_length;
   275   volatile unsigned _gc_time_stamp;
   277   size_t* _surviving_young_words;
   279   void setup_surviving_young_words();
   280   void update_surviving_young_words(size_t* surv_young_words);
   281   void cleanup_surviving_young_words();
   283 protected:
   285   // Returns "true" iff none of the gc alloc regions have any allocations
   286   // since the last call to "save_marks".
   287   bool all_alloc_regions_no_allocs_since_save_marks();
   288   // Perform finalization stuff on all allocation regions.
   289   void retire_all_alloc_regions();
   291   // The number of regions allocated to hold humongous objects.
   292   int         _num_humongous_regions;
   293   YoungList*  _young_list;
   295   // The current policy object for the collector.
   296   G1CollectorPolicy* _g1_policy;
   298   // Parallel allocation lock to protect the current allocation region.
   299   Mutex  _par_alloc_during_gc_lock;
   300   Mutex* par_alloc_during_gc_lock() { return &_par_alloc_during_gc_lock; }
   302   // If possible/desirable, allocate a new HeapRegion for normal object
   303   // allocation sufficient for an allocation of the given "word_size".
   304   // If "do_expand" is true, will attempt to expand the heap if necessary
   305   // to to satisfy the request.  If "zero_filled" is true, requires a
   306   // zero-filled region.
   307   // (Returning NULL will trigger a GC.)
   308   virtual HeapRegion* newAllocRegion_work(size_t word_size,
   309                                           bool do_expand,
   310                                           bool zero_filled);
   312   virtual HeapRegion* newAllocRegion(size_t word_size,
   313                                      bool zero_filled = true) {
   314     return newAllocRegion_work(word_size, false, zero_filled);
   315   }
   316   virtual HeapRegion* newAllocRegionWithExpansion(int purpose,
   317                                                   size_t word_size,
   318                                                   bool zero_filled = true);
   320   // Attempt to allocate an object of the given (very large) "word_size".
   321   // Returns "NULL" on failure.
   322   virtual HeapWord* humongousObjAllocate(size_t word_size);
   324   // If possible, allocate a block of the given word_size, else return "NULL".
   325   // Returning NULL will trigger GC or heap expansion.
   326   // These two methods have rather awkward pre- and
   327   // post-conditions. If they are called outside a safepoint, then
   328   // they assume that the caller is holding the heap lock. Upon return
   329   // they release the heap lock, if they are returning a non-NULL
   330   // value. attempt_allocation_slow() also dirties the cards of a
   331   // newly-allocated young region after it releases the heap
   332   // lock. This change in interface was the neatest way to achieve
   333   // this card dirtying without affecting mem_allocate(), which is a
   334   // more frequently called method. We tried two or three different
   335   // approaches, but they were even more hacky.
   336   HeapWord* attempt_allocation(size_t word_size,
   337                                bool permit_collection_pause = true);
   339   HeapWord* attempt_allocation_slow(size_t word_size,
   340                                     bool permit_collection_pause = true);
   342   // Allocate blocks during garbage collection. Will ensure an
   343   // allocation region, either by picking one or expanding the
   344   // heap, and then allocate a block of the given size. The block
   345   // may not be a humongous - it must fit into a single heap region.
   346   HeapWord* allocate_during_gc(GCAllocPurpose purpose, size_t word_size);
   347   HeapWord* par_allocate_during_gc(GCAllocPurpose purpose, size_t word_size);
   349   HeapWord* allocate_during_gc_slow(GCAllocPurpose purpose,
   350                                     HeapRegion*    alloc_region,
   351                                     bool           par,
   352                                     size_t         word_size);
   354   // Ensure that no further allocations can happen in "r", bearing in mind
   355   // that parallel threads might be attempting allocations.
   356   void par_allocate_remaining_space(HeapRegion* r);
   358   // Retires an allocation region when it is full or at the end of a
   359   // GC pause.
   360   void  retire_alloc_region(HeapRegion* alloc_region, bool par);
   362   // Helper function for two callbacks below.
   363   // "full", if true, indicates that the GC is for a System.gc() request,
   364   // and should collect the entire heap.  If "clear_all_soft_refs" is true,
   365   // all soft references are cleared during the GC.  If "full" is false,
   366   // "word_size" describes the allocation that the GC should
   367   // attempt (at least) to satisfy.
   368   void do_collection(bool full, 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 <= (size_t) index && (size_t) index < _in_cset_fast_test_length,
   407            "invariant");
   408     assert(!_in_cset_fast_test_base[index], "invariant");
   409     _in_cset_fast_test_base[index] = true;
   410   }
   412   // This is a fast test on whether a reference points into the
   413   // collection set or not. It does not assume that the reference
   414   // points into the heap; if it doesn't, it will return false.
   415   bool in_cset_fast_test(oop obj) {
   416     assert(_in_cset_fast_test != NULL, "sanity");
   417     if (_g1_committed.contains((HeapWord*) obj)) {
   418       // no need to subtract the bottom of the heap from obj,
   419       // _in_cset_fast_test is biased
   420       size_t index = ((size_t) obj) >> HeapRegion::LogOfHRGrainBytes;
   421       bool ret = _in_cset_fast_test[index];
   422       // let's make sure the result is consistent with what the slower
   423       // test returns
   424       assert( ret || !obj_in_cs(obj), "sanity");
   425       assert(!ret ||  obj_in_cs(obj), "sanity");
   426       return ret;
   427     } else {
   428       return false;
   429     }
   430   }
   432 protected:
   434   // Shrink the garbage-first heap by at most the given size (in bytes!).
   435   // (Rounds down to a HeapRegion boundary.)
   436   virtual void shrink(size_t expand_bytes);
   437   void shrink_helper(size_t expand_bytes);
   439   // Do an incremental collection: identify a collection set, and evacuate
   440   // its live objects elsewhere.
   441   virtual void do_collection_pause();
   443   // The guts of the incremental collection pause, executed by the vm
   444   // thread.
   445   virtual void do_collection_pause_at_safepoint();
   447   // Actually do the work of evacuating the collection set.
   448   virtual void evacuate_collection_set();
   450   // If this is an appropriate right time, do a collection pause.
   451   // The "word_size" argument, if non-zero, indicates the size of an
   452   // allocation request that is prompting this query.
   453   void do_collection_pause_if_appropriate(size_t word_size);
   455   // The g1 remembered set of the heap.
   456   G1RemSet* _g1_rem_set;
   457   // And it's mod ref barrier set, used to track updates for the above.
   458   ModRefBarrierSet* _mr_bs;
   460   // A set of cards that cover the objects for which the Rsets should be updated
   461   // concurrently after the collection.
   462   DirtyCardQueueSet _dirty_card_queue_set;
   464   // The Heap Region Rem Set Iterator.
   465   HeapRegionRemSetIterator** _rem_set_iterator;
   467   // The closure used to refine a single card.
   468   RefineCardTableEntryClosure* _refine_cte_cl;
   470   // A function to check the consistency of dirty card logs.
   471   void check_ct_logs_at_safepoint();
   473   // After a collection pause, make the regions in the CS into free
   474   // regions.
   475   void free_collection_set(HeapRegion* cs_head);
   477   // Applies "scan_non_heap_roots" to roots outside the heap,
   478   // "scan_rs" to roots inside the heap (having done "set_region" to
   479   // indicate the region in which the root resides), and does "scan_perm"
   480   // (setting the generation to the perm generation.)  If "scan_rs" is
   481   // NULL, then this step is skipped.  The "worker_i"
   482   // param is for use with parallel roots processing, and should be
   483   // the "i" of the calling parallel worker thread's work(i) function.
   484   // In the sequential case this param will be ignored.
   485   void g1_process_strong_roots(bool collecting_perm_gen,
   486                                SharedHeap::ScanningOption so,
   487                                OopClosure* scan_non_heap_roots,
   488                                OopsInHeapRegionClosure* scan_rs,
   489                                OopsInHeapRegionClosure* scan_so,
   490                                OopsInGenClosure* scan_perm,
   491                                int worker_i);
   493   void scan_scan_only_set(OopsInHeapRegionClosure* oc,
   494                           int worker_i);
   495   void scan_scan_only_region(HeapRegion* hr,
   496                              OopsInHeapRegionClosure* oc,
   497                              int worker_i);
   499   // Apply "blk" to all the weak roots of the system.  These include
   500   // JNI weak roots, the code cache, system dictionary, symbol table,
   501   // string table, and referents of reachable weak refs.
   502   void g1_process_weak_roots(OopClosure* root_closure,
   503                              OopClosure* non_root_closure);
   505   // Invoke "save_marks" on all heap regions.
   506   void save_marks();
   508   // Free a heap region.
   509   void free_region(HeapRegion* hr);
   510   // A component of "free_region", exposed for 'batching'.
   511   // All the params after "hr" are out params: the used bytes of the freed
   512   // region(s), the number of H regions cleared, the number of regions
   513   // freed, and pointers to the head and tail of a list of freed contig
   514   // regions, linked throught the "next_on_unclean_list" field.
   515   void free_region_work(HeapRegion* hr,
   516                         size_t& pre_used,
   517                         size_t& cleared_h,
   518                         size_t& freed_regions,
   519                         UncleanRegionList* list,
   520                         bool par = false);
   523   // The concurrent marker (and the thread it runs in.)
   524   ConcurrentMark* _cm;
   525   ConcurrentMarkThread* _cmThread;
   526   bool _mark_in_progress;
   528   // The concurrent refiner.
   529   ConcurrentG1Refine* _cg1r;
   531   // The concurrent zero-fill thread.
   532   ConcurrentZFThread* _czft;
   534   // The parallel task queues
   535   RefToScanQueueSet *_task_queues;
   537   // True iff a evacuation has failed in the current collection.
   538   bool _evacuation_failed;
   540   // Set the attribute indicating whether evacuation has failed in the
   541   // current collection.
   542   void set_evacuation_failed(bool b) { _evacuation_failed = b; }
   544   // Failed evacuations cause some logical from-space objects to have
   545   // forwarding pointers to themselves.  Reset them.
   546   void remove_self_forwarding_pointers();
   548   // When one is non-null, so is the other.  Together, they each pair is
   549   // an object with a preserved mark, and its mark value.
   550   GrowableArray<oop>*     _objs_with_preserved_marks;
   551   GrowableArray<markOop>* _preserved_marks_of_objs;
   553   // Preserve the mark of "obj", if necessary, in preparation for its mark
   554   // word being overwritten with a self-forwarding-pointer.
   555   void preserve_mark_if_necessary(oop obj, markOop m);
   557   // The stack of evac-failure objects left to be scanned.
   558   GrowableArray<oop>*    _evac_failure_scan_stack;
   559   // The closure to apply to evac-failure objects.
   561   OopsInHeapRegionClosure* _evac_failure_closure;
   562   // Set the field above.
   563   void
   564   set_evac_failure_closure(OopsInHeapRegionClosure* evac_failure_closure) {
   565     _evac_failure_closure = evac_failure_closure;
   566   }
   568   // Push "obj" on the scan stack.
   569   void push_on_evac_failure_scan_stack(oop obj);
   570   // Process scan stack entries until the stack is empty.
   571   void drain_evac_failure_scan_stack();
   572   // True iff an invocation of "drain_scan_stack" is in progress; to
   573   // prevent unnecessary recursion.
   574   bool _drain_in_progress;
   576   // Do any necessary initialization for evacuation-failure handling.
   577   // "cl" is the closure that will be used to process evac-failure
   578   // objects.
   579   void init_for_evac_failure(OopsInHeapRegionClosure* cl);
   580   // Do any necessary cleanup for evacuation-failure handling data
   581   // structures.
   582   void finalize_for_evac_failure();
   584   // An attempt to evacuate "obj" has failed; take necessary steps.
   585   void handle_evacuation_failure(oop obj);
   586   oop handle_evacuation_failure_par(OopsInHeapRegionClosure* cl, oop obj);
   587   void handle_evacuation_failure_common(oop obj, markOop m);
   590   // Ensure that the relevant gc_alloc regions are set.
   591   void get_gc_alloc_regions();
   592   // We're done with GC alloc regions. We are going to tear down the
   593   // gc alloc list and remove the gc alloc tag from all the regions on
   594   // that list. However, we will also retain the last (i.e., the one
   595   // that is half-full) GC alloc region, per GCAllocPurpose, for
   596   // possible reuse during the next collection, provided
   597   // _retain_gc_alloc_region[] indicates that it should be the
   598   // case. Said regions are kept in the _retained_gc_alloc_regions[]
   599   // array. If the parameter totally is set, we will not retain any
   600   // regions, irrespective of what _retain_gc_alloc_region[]
   601   // indicates.
   602   void release_gc_alloc_regions(bool totally);
   603 #ifndef PRODUCT
   604   // Useful for debugging.
   605   void print_gc_alloc_regions();
   606 #endif // !PRODUCT
   608   // ("Weak") Reference processing support
   609   ReferenceProcessor* _ref_processor;
   611   enum G1H_process_strong_roots_tasks {
   612     G1H_PS_mark_stack_oops_do,
   613     G1H_PS_refProcessor_oops_do,
   614     // Leave this one last.
   615     G1H_PS_NumElements
   616   };
   618   SubTasksDone* _process_strong_tasks;
   620   // List of regions which require zero filling.
   621   UncleanRegionList _unclean_region_list;
   622   bool _unclean_regions_coming;
   624 public:
   625   void set_refine_cte_cl_concurrency(bool concurrent);
   627   RefToScanQueue *task_queue(int i);
   629   // A set of cards where updates happened during the GC
   630   DirtyCardQueueSet& dirty_card_queue_set() { return _dirty_card_queue_set; }
   632   // Create a G1CollectedHeap with the specified policy.
   633   // Must call the initialize method afterwards.
   634   // May not return if something goes wrong.
   635   G1CollectedHeap(G1CollectorPolicy* policy);
   637   // Initialize the G1CollectedHeap to have the initial and
   638   // maximum sizes, permanent generation, and remembered and barrier sets
   639   // specified by the policy object.
   640   jint initialize();
   642   void ref_processing_init();
   644   void set_par_threads(int t) {
   645     SharedHeap::set_par_threads(t);
   646     _process_strong_tasks->set_par_threads(t);
   647   }
   649   virtual CollectedHeap::Name kind() const {
   650     return CollectedHeap::G1CollectedHeap;
   651   }
   653   // The current policy object for the collector.
   654   G1CollectorPolicy* g1_policy() const { return _g1_policy; }
   656   // Adaptive size policy.  No such thing for g1.
   657   virtual AdaptiveSizePolicy* size_policy() { return NULL; }
   659   // The rem set and barrier set.
   660   G1RemSet* g1_rem_set() const { return _g1_rem_set; }
   661   ModRefBarrierSet* mr_bs() const { return _mr_bs; }
   663   // The rem set iterator.
   664   HeapRegionRemSetIterator* rem_set_iterator(int i) {
   665     return _rem_set_iterator[i];
   666   }
   668   HeapRegionRemSetIterator* rem_set_iterator() {
   669     return _rem_set_iterator[0];
   670   }
   672   unsigned get_gc_time_stamp() {
   673     return _gc_time_stamp;
   674   }
   676   void reset_gc_time_stamp() {
   677     _gc_time_stamp = 0;
   678     OrderAccess::fence();
   679   }
   681   void increment_gc_time_stamp() {
   682     ++_gc_time_stamp;
   683     OrderAccess::fence();
   684   }
   686   void iterate_dirty_card_closure(bool concurrent, int worker_i);
   688   // The shared block offset table array.
   689   G1BlockOffsetSharedArray* bot_shared() const { return _bot_shared; }
   691   // Reference Processing accessor
   692   ReferenceProcessor* ref_processor() { return _ref_processor; }
   694   // Reserved (g1 only; super method includes perm), capacity and the used
   695   // portion in bytes.
   696   size_t g1_reserved_obj_bytes() { return _g1_reserved.byte_size(); }
   697   virtual size_t capacity() const;
   698   virtual size_t used() const;
   699   size_t recalculate_used() const;
   700 #ifndef PRODUCT
   701   size_t recalculate_used_regions() const;
   702 #endif // PRODUCT
   704   // These virtual functions do the actual allocation.
   705   virtual HeapWord* mem_allocate(size_t word_size,
   706                                  bool   is_noref,
   707                                  bool   is_tlab,
   708                                  bool* gc_overhead_limit_was_exceeded);
   710   // Some heaps may offer a contiguous region for shared non-blocking
   711   // allocation, via inlined code (by exporting the address of the top and
   712   // end fields defining the extent of the contiguous allocation region.)
   713   // But G1CollectedHeap doesn't yet support this.
   715   // Return an estimate of the maximum allocation that could be performed
   716   // without triggering any collection or expansion activity.  In a
   717   // generational collector, for example, this is probably the largest
   718   // allocation that could be supported (without expansion) in the youngest
   719   // generation.  It is "unsafe" because no locks are taken; the result
   720   // should be treated as an approximation, not a guarantee, for use in
   721   // heuristic resizing decisions.
   722   virtual size_t unsafe_max_alloc();
   724   virtual bool is_maximal_no_gc() const {
   725     return _g1_storage.uncommitted_size() == 0;
   726   }
   728   // The total number of regions in the heap.
   729   size_t n_regions();
   731   // The number of regions that are completely free.
   732   size_t max_regions();
   734   // The number of regions that are completely free.
   735   size_t free_regions();
   737   // The number of regions that are not completely free.
   738   size_t used_regions() { return n_regions() - free_regions(); }
   740   // True iff the ZF thread should run.
   741   bool should_zf();
   743   // The number of regions available for "regular" expansion.
   744   size_t expansion_regions() { return _expansion_regions; }
   746 #ifndef PRODUCT
   747   bool regions_accounted_for();
   748   bool print_region_accounting_info();
   749   void print_region_counts();
   750 #endif
   752   HeapRegion* alloc_region_from_unclean_list(bool zero_filled);
   753   HeapRegion* alloc_region_from_unclean_list_locked(bool zero_filled);
   755   void put_region_on_unclean_list(HeapRegion* r);
   756   void put_region_on_unclean_list_locked(HeapRegion* r);
   758   void prepend_region_list_on_unclean_list(UncleanRegionList* list);
   759   void prepend_region_list_on_unclean_list_locked(UncleanRegionList* list);
   761   void set_unclean_regions_coming(bool b);
   762   void set_unclean_regions_coming_locked(bool b);
   763   // Wait for cleanup to be complete.
   764   void wait_for_cleanup_complete();
   765   // Like above, but assumes that the calling thread owns the Heap_lock.
   766   void wait_for_cleanup_complete_locked();
   768   // Return the head of the unclean list.
   769   HeapRegion* peek_unclean_region_list_locked();
   770   // Remove and return the head of the unclean list.
   771   HeapRegion* pop_unclean_region_list_locked();
   773   // List of regions which are zero filled and ready for allocation.
   774   HeapRegion* _free_region_list;
   775   // Number of elements on the free list.
   776   size_t _free_region_list_size;
   778   // If the head of the unclean list is ZeroFilled, move it to the free
   779   // list.
   780   bool move_cleaned_region_to_free_list_locked();
   781   bool move_cleaned_region_to_free_list();
   783   void put_free_region_on_list_locked(HeapRegion* r);
   784   void put_free_region_on_list(HeapRegion* r);
   786   // Remove and return the head element of the free list.
   787   HeapRegion* pop_free_region_list_locked();
   789   // If "zero_filled" is true, we first try the free list, then we try the
   790   // unclean list, zero-filling the result.  If "zero_filled" is false, we
   791   // first try the unclean list, then the zero-filled list.
   792   HeapRegion* alloc_free_region_from_lists(bool zero_filled);
   794   // Verify the integrity of the region lists.
   795   void remove_allocated_regions_from_lists();
   796   bool verify_region_lists();
   797   bool verify_region_lists_locked();
   798   size_t unclean_region_list_length();
   799   size_t free_region_list_length();
   801   // Perform a collection of the heap; intended for use in implementing
   802   // "System.gc".  This probably implies as full a collection as the
   803   // "CollectedHeap" supports.
   804   virtual void collect(GCCause::Cause cause);
   806   // The same as above but assume that the caller holds the Heap_lock.
   807   void collect_locked(GCCause::Cause cause);
   809   // This interface assumes that it's being called by the
   810   // vm thread. It collects the heap assuming that the
   811   // heap lock is already held and that we are executing in
   812   // the context of the vm thread.
   813   virtual void collect_as_vm_thread(GCCause::Cause cause);
   815   // True iff a evacuation has failed in the most-recent collection.
   816   bool evacuation_failed() { return _evacuation_failed; }
   818   // Free a region if it is totally full of garbage.  Returns the number of
   819   // bytes freed (0 ==> didn't free it).
   820   size_t free_region_if_totally_empty(HeapRegion *hr);
   821   void free_region_if_totally_empty_work(HeapRegion *hr,
   822                                          size_t& pre_used,
   823                                          size_t& cleared_h_regions,
   824                                          size_t& freed_regions,
   825                                          UncleanRegionList* list,
   826                                          bool par = false);
   828   // If we've done free region work that yields the given changes, update
   829   // the relevant global variables.
   830   void finish_free_region_work(size_t pre_used,
   831                                size_t cleared_h_regions,
   832                                size_t freed_regions,
   833                                UncleanRegionList* list);
   836   // Returns "TRUE" iff "p" points into the allocated area of the heap.
   837   virtual bool is_in(const void* p) const;
   839   // Return "TRUE" iff the given object address is within the collection
   840   // set.
   841   inline bool obj_in_cs(oop obj);
   843   // Return "TRUE" iff the given object address is in the reserved
   844   // region of g1 (excluding the permanent generation).
   845   bool is_in_g1_reserved(const void* p) const {
   846     return _g1_reserved.contains(p);
   847   }
   849   // Returns a MemRegion that corresponds to the space that  has been
   850   // committed in the heap
   851   MemRegion g1_committed() {
   852     return _g1_committed;
   853   }
   855   NOT_PRODUCT( bool is_in_closed_subset(const void* p) const; )
   857   // Dirty card table entries covering a list of young regions.
   858   void dirtyCardsForYoungRegions(CardTableModRefBS* ct_bs, HeapRegion* list);
   860   // This resets the card table to all zeros.  It is used after
   861   // a collection pause which used the card table to claim cards.
   862   void cleanUpCardTable();
   864   // Iteration functions.
   866   // Iterate over all the ref-containing fields of all objects, calling
   867   // "cl.do_oop" on each.
   868   virtual void oop_iterate(OopClosure* cl);
   870   // Same as above, restricted to a memory region.
   871   virtual void oop_iterate(MemRegion mr, OopClosure* cl);
   873   // Iterate over all objects, calling "cl.do_object" on each.
   874   virtual void object_iterate(ObjectClosure* cl);
   875   virtual void safe_object_iterate(ObjectClosure* cl) { object_iterate(cl); }
   877   // Iterate over all objects allocated since the last collection, calling
   878   // "cl.do_object" on each.  The heap must have been initialized properly
   879   // to support this function, or else this call will fail.
   880   virtual void object_iterate_since_last_GC(ObjectClosure* cl);
   882   // Iterate over all spaces in use in the heap, in ascending address order.
   883   virtual void space_iterate(SpaceClosure* cl);
   885   // Iterate over heap regions, in address order, terminating the
   886   // iteration early if the "doHeapRegion" method returns "true".
   887   void heap_region_iterate(HeapRegionClosure* blk);
   889   // Iterate over heap regions starting with r (or the first region if "r"
   890   // is NULL), in address order, terminating early if the "doHeapRegion"
   891   // method returns "true".
   892   void heap_region_iterate_from(HeapRegion* r, HeapRegionClosure* blk);
   894   // As above but starting from the region at index idx.
   895   void heap_region_iterate_from(int idx, HeapRegionClosure* blk);
   897   HeapRegion* region_at(size_t idx);
   899   // Divide the heap region sequence into "chunks" of some size (the number
   900   // of regions divided by the number of parallel threads times some
   901   // overpartition factor, currently 4).  Assumes that this will be called
   902   // in parallel by ParallelGCThreads worker threads with discinct worker
   903   // ids in the range [0..max(ParallelGCThreads-1, 1)], that all parallel
   904   // calls will use the same "claim_value", and that that claim value is
   905   // different from the claim_value of any heap region before the start of
   906   // the iteration.  Applies "blk->doHeapRegion" to each of the regions, by
   907   // attempting to claim the first region in each chunk, and, if
   908   // successful, applying the closure to each region in the chunk (and
   909   // setting the claim value of the second and subsequent regions of the
   910   // chunk.)  For now requires that "doHeapRegion" always returns "false",
   911   // i.e., that a closure never attempt to abort a traversal.
   912   void heap_region_par_iterate_chunked(HeapRegionClosure* blk,
   913                                        int worker,
   914                                        jint claim_value);
   916   // It resets all the region claim values to the default.
   917   void reset_heap_region_claim_values();
   919 #ifdef ASSERT
   920   bool check_heap_region_claim_values(jint claim_value);
   921 #endif // ASSERT
   923   // Iterate over the regions (if any) in the current collection set.
   924   void collection_set_iterate(HeapRegionClosure* blk);
   926   // As above but starting from region r
   927   void collection_set_iterate_from(HeapRegion* r, HeapRegionClosure *blk);
   929   // Returns the first (lowest address) compactible space in the heap.
   930   virtual CompactibleSpace* first_compactible_space();
   932   // A CollectedHeap will contain some number of spaces.  This finds the
   933   // space containing a given address, or else returns NULL.
   934   virtual Space* space_containing(const void* addr) const;
   936   // A G1CollectedHeap will contain some number of heap regions.  This
   937   // finds the region containing a given address, or else returns NULL.
   938   HeapRegion* heap_region_containing(const void* addr) const;
   940   // Like the above, but requires "addr" to be in the heap (to avoid a
   941   // null-check), and unlike the above, may return an continuing humongous
   942   // region.
   943   HeapRegion* heap_region_containing_raw(const void* addr) const;
   945   // A CollectedHeap is divided into a dense sequence of "blocks"; that is,
   946   // each address in the (reserved) heap is a member of exactly
   947   // one block.  The defining characteristic of a block is that it is
   948   // possible to find its size, and thus to progress forward to the next
   949   // block.  (Blocks may be of different sizes.)  Thus, blocks may
   950   // represent Java objects, or they might be free blocks in a
   951   // free-list-based heap (or subheap), as long as the two kinds are
   952   // distinguishable and the size of each is determinable.
   954   // Returns the address of the start of the "block" that contains the
   955   // address "addr".  We say "blocks" instead of "object" since some heaps
   956   // may not pack objects densely; a chunk may either be an object or a
   957   // non-object.
   958   virtual HeapWord* block_start(const void* addr) const;
   960   // Requires "addr" to be the start of a chunk, and returns its size.
   961   // "addr + size" is required to be the start of a new chunk, or the end
   962   // of the active area of the heap.
   963   virtual size_t block_size(const HeapWord* addr) const;
   965   // Requires "addr" to be the start of a block, and returns "TRUE" iff
   966   // the block is an object.
   967   virtual bool block_is_obj(const HeapWord* addr) const;
   969   // Does this heap support heap inspection? (+PrintClassHistogram)
   970   virtual bool supports_heap_inspection() const { return true; }
   972   // Section on thread-local allocation buffers (TLABs)
   973   // See CollectedHeap for semantics.
   975   virtual bool supports_tlab_allocation() const;
   976   virtual size_t tlab_capacity(Thread* thr) const;
   977   virtual size_t unsafe_max_tlab_alloc(Thread* thr) const;
   978   virtual HeapWord* allocate_new_tlab(size_t size);
   980   // Can a compiler initialize a new object without store barriers?
   981   // This permission only extends from the creation of a new object
   982   // via a TLAB up to the first subsequent safepoint.
   983   virtual bool can_elide_tlab_store_barriers() const {
   984     // Since G1's TLAB's may, on occasion, come from non-young regions
   985     // as well. (Is there a flag controlling that? XXX)
   986     return false;
   987   }
   989   // Can a compiler elide a store barrier when it writes
   990   // a permanent oop into the heap?  Applies when the compiler
   991   // is storing x to the heap, where x->is_perm() is true.
   992   virtual bool can_elide_permanent_oop_store_barriers() const {
   993     // At least until perm gen collection is also G1-ified, at
   994     // which point this should return false.
   995     return true;
   996   }
   998   virtual bool allocs_are_zero_filled();
  1000   // The boundary between a "large" and "small" array of primitives, in
  1001   // words.
  1002   virtual size_t large_typearray_limit();
  1004   // Returns "true" iff the given word_size is "very large".
  1005   static bool isHumongous(size_t word_size) {
  1006     return word_size >= VeryLargeInWords;
  1009   // Update mod union table with the set of dirty cards.
  1010   void updateModUnion();
  1012   // Set the mod union bits corresponding to the given memRegion.  Note
  1013   // that this is always a safe operation, since it doesn't clear any
  1014   // bits.
  1015   void markModUnionRange(MemRegion mr);
  1017   // Records the fact that a marking phase is no longer in progress.
  1018   void set_marking_complete() {
  1019     _mark_in_progress = false;
  1021   void set_marking_started() {
  1022     _mark_in_progress = true;
  1024   bool mark_in_progress() {
  1025     return _mark_in_progress;
  1028   // Print the maximum heap capacity.
  1029   virtual size_t max_capacity() const;
  1031   virtual jlong millis_since_last_gc();
  1033   // Perform any cleanup actions necessary before allowing a verification.
  1034   virtual void prepare_for_verify();
  1036   // Perform verification.
  1037   virtual void verify(bool allow_dirty, bool silent);
  1038   virtual void print() const;
  1039   virtual void print_on(outputStream* st) const;
  1041   virtual void print_gc_threads_on(outputStream* st) const;
  1042   virtual void gc_threads_do(ThreadClosure* tc) const;
  1044   // Override
  1045   void print_tracing_info() const;
  1047   // If "addr" is a pointer into the (reserved?) heap, returns a positive
  1048   // number indicating the "arena" within the heap in which "addr" falls.
  1049   // Or else returns 0.
  1050   virtual int addr_to_arena_id(void* addr) const;
  1052   // Convenience function to be used in situations where the heap type can be
  1053   // asserted to be this type.
  1054   static G1CollectedHeap* heap();
  1056   void empty_young_list();
  1057   bool should_set_young_locked();
  1059   void set_region_short_lived_locked(HeapRegion* hr);
  1060   // add appropriate methods for any other surv rate groups
  1062   void young_list_rs_length_sampling_init() {
  1063     _young_list->rs_length_sampling_init();
  1065   bool young_list_rs_length_sampling_more() {
  1066     return _young_list->rs_length_sampling_more();
  1068   void young_list_rs_length_sampling_next() {
  1069     _young_list->rs_length_sampling_next();
  1071   size_t young_list_sampled_rs_lengths() {
  1072     return _young_list->sampled_rs_lengths();
  1075   size_t young_list_length()   { return _young_list->length(); }
  1076   size_t young_list_scan_only_length() {
  1077                                       return _young_list->scan_only_length(); }
  1079   HeapRegion* pop_region_from_young_list() {
  1080     return _young_list->pop_region();
  1083   HeapRegion* young_list_first_region() {
  1084     return _young_list->first_region();
  1087   // debugging
  1088   bool check_young_list_well_formed() {
  1089     return _young_list->check_list_well_formed();
  1091   bool check_young_list_empty(bool ignore_scan_only_list,
  1092                               bool check_sample = true);
  1094   // *** Stuff related to concurrent marking.  It's not clear to me that so
  1095   // many of these need to be public.
  1097   // The functions below are helper functions that a subclass of
  1098   // "CollectedHeap" can use in the implementation of its virtual
  1099   // functions.
  1100   // This performs a concurrent marking of the live objects in a
  1101   // bitmap off to the side.
  1102   void doConcurrentMark();
  1104   // This is called from the marksweep collector which then does
  1105   // a concurrent mark and verifies that the results agree with
  1106   // the stop the world marking.
  1107   void checkConcurrentMark();
  1108   void do_sync_mark();
  1110   bool isMarkedPrev(oop obj) const;
  1111   bool isMarkedNext(oop obj) const;
  1113   // Determine if an object is dead, given the object and also
  1114   // the region to which the object belongs. An object is dead
  1115   // iff a) it was not allocated since the last mark and b) it
  1116   // is not marked.
  1118   bool is_obj_dead(const oop obj, const HeapRegion* hr) const {
  1119     return
  1120       !hr->obj_allocated_since_prev_marking(obj) &&
  1121       !isMarkedPrev(obj);
  1124   // This is used when copying an object to survivor space.
  1125   // If the object is marked live, then we mark the copy live.
  1126   // If the object is allocated since the start of this mark
  1127   // cycle, then we mark the copy live.
  1128   // If the object has been around since the previous mark
  1129   // phase, and hasn't been marked yet during this phase,
  1130   // then we don't mark it, we just wait for the
  1131   // current marking cycle to get to it.
  1133   // This function returns true when an object has been
  1134   // around since the previous marking and hasn't yet
  1135   // been marked during this marking.
  1137   bool is_obj_ill(const oop obj, const HeapRegion* hr) const {
  1138     return
  1139       !hr->obj_allocated_since_next_marking(obj) &&
  1140       !isMarkedNext(obj);
  1143   // Determine if an object is dead, given only the object itself.
  1144   // This will find the region to which the object belongs and
  1145   // then call the region version of the same function.
  1147   // Added if it is in permanent gen it isn't dead.
  1148   // Added if it is NULL it isn't dead.
  1150   bool is_obj_dead(oop obj) {
  1151     HeapRegion* hr = heap_region_containing(obj);
  1152     if (hr == NULL) {
  1153       if (Universe::heap()->is_in_permanent(obj))
  1154         return false;
  1155       else if (obj == NULL) return false;
  1156       else return true;
  1158     else return is_obj_dead(obj, hr);
  1161   bool is_obj_ill(oop obj) {
  1162     HeapRegion* hr = heap_region_containing(obj);
  1163     if (hr == NULL) {
  1164       if (Universe::heap()->is_in_permanent(obj))
  1165         return false;
  1166       else if (obj == NULL) return false;
  1167       else return true;
  1169     else return is_obj_ill(obj, hr);
  1172   // The following is just to alert the verification code
  1173   // that a full collection has occurred and that the
  1174   // remembered sets are no longer up to date.
  1175   bool _full_collection;
  1176   void set_full_collection() { _full_collection = true;}
  1177   void clear_full_collection() {_full_collection = false;}
  1178   bool full_collection() {return _full_collection;}
  1180   ConcurrentMark* concurrent_mark() const { return _cm; }
  1181   ConcurrentG1Refine* concurrent_g1_refine() const { return _cg1r; }
  1183 public:
  1184   void stop_conc_gc_threads();
  1186   // <NEW PREDICTION>
  1188   double predict_region_elapsed_time_ms(HeapRegion* hr, bool young);
  1189   void check_if_region_is_too_expensive(double predicted_time_ms);
  1190   size_t pending_card_num();
  1191   size_t max_pending_card_num();
  1192   size_t cards_scanned();
  1194   // </NEW PREDICTION>
  1196 protected:
  1197   size_t _max_heap_capacity;
  1199 //  debug_only(static void check_for_valid_allocation_state();)
  1201 public:
  1202   // Temporary: call to mark things unimplemented for the G1 heap (e.g.,
  1203   // MemoryService).  In productization, we can make this assert false
  1204   // to catch such places (as well as searching for calls to this...)
  1205   static void g1_unimplemented();
  1207 };
  1209 // Local Variables: ***
  1210 // c-indentation-style: gnu ***
  1211 // End: ***

mercurial