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

Mon, 06 Oct 2008 13:16:35 -0400

author
tonyp
date
Mon, 06 Oct 2008 13:16:35 -0400
changeset 825
cc68c8e9b309
parent 790
0edda524b58c
child 905
ad8c8ca4ab0f
child 952
e9be0e04635a
permissions
-rw-r--r--

6752248: G1: introduce parallel heap verification
Summary: Introduce parallel heap verification in G1.
Reviewed-by: jcoomes, apetrusenko

     1 /*
     2  * Copyright 2001-2007 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 HeapRegionList;
    33 class PermanentGenerationSpec;
    34 class GenerationSpec;
    35 class OopsInHeapRegionClosure;
    36 class G1ScanHeapEvacClosure;
    37 class ObjectClosure;
    38 class SpaceClosure;
    39 class CompactibleSpaceClosure;
    40 class Space;
    41 class G1CollectorPolicy;
    42 class GenRemSet;
    43 class G1RemSet;
    44 class HeapRegionRemSetIterator;
    45 class ConcurrentMark;
    46 class ConcurrentMarkThread;
    47 class ConcurrentG1Refine;
    48 class ConcurrentZFThread;
    50 // If want to accumulate detailed statistics on work queues
    51 // turn this on.
    52 #define G1_DETAILED_STATS 0
    54 #if G1_DETAILED_STATS
    55 #  define IF_G1_DETAILED_STATS(code) code
    56 #else
    57 #  define IF_G1_DETAILED_STATS(code)
    58 #endif
    60 typedef GenericTaskQueue<oop*>    RefToScanQueue;
    61 typedef GenericTaskQueueSet<oop*> RefToScanQueueSet;
    63 enum G1GCThreadGroups {
    64   G1CRGroup = 0,
    65   G1ZFGroup = 1,
    66   G1CMGroup = 2,
    67   G1CLGroup = 3
    68 };
    70 enum GCAllocPurpose {
    71   GCAllocForTenured,
    72   GCAllocForSurvived,
    73   GCAllocPurposeCount
    74 };
    76 class YoungList : public CHeapObj {
    77 private:
    78   G1CollectedHeap* _g1h;
    80   HeapRegion* _head;
    82   HeapRegion* _scan_only_head;
    83   HeapRegion* _scan_only_tail;
    84   size_t      _length;
    85   size_t      _scan_only_length;
    87   size_t      _last_sampled_rs_lengths;
    88   size_t      _sampled_rs_lengths;
    89   HeapRegion* _curr;
    90   HeapRegion* _curr_scan_only;
    92   HeapRegion* _survivor_head;
    93   HeapRegion* _survivors_tail;
    94   size_t      _survivor_length;
    96   void          empty_list(HeapRegion* list);
    98 public:
    99   YoungList(G1CollectedHeap* g1h);
   101   void          push_region(HeapRegion* hr);
   102   void          add_survivor_region(HeapRegion* hr);
   103   HeapRegion*   pop_region();
   104   void          empty_list();
   105   bool          is_empty() { return _length == 0; }
   106   size_t        length() { return _length; }
   107   size_t        scan_only_length() { return _scan_only_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* par_get_next_scan_only_region() {
   124     MutexLockerEx x(ParGCRareEvent_lock, Mutex::_no_safepoint_check_flag);
   125     HeapRegion* ret = _curr_scan_only;
   126     if (ret != NULL)
   127       _curr_scan_only = ret->get_next_young_region();
   128     return ret;
   129   }
   131   // debugging
   132   bool          check_list_well_formed();
   133   bool          check_list_empty(bool ignore_scan_only_list,
   134                                  bool check_sample = true);
   135   void          print();
   136 };
   138 class RefineCardTableEntryClosure;
   139 class G1CollectedHeap : public SharedHeap {
   140   friend class VM_G1CollectForAllocation;
   141   friend class VM_GenCollectForPermanentAllocation;
   142   friend class VM_G1CollectFull;
   143   friend class VM_G1IncCollectionPause;
   144   friend class VM_G1PopRegionCollectionPause;
   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   };
   174   // The one and only G1CollectedHeap, so static functions can find it.
   175   static G1CollectedHeap* _g1h;
   177   // Storage for the G1 heap (excludes the permanent generation).
   178   VirtualSpace _g1_storage;
   179   MemRegion    _g1_reserved;
   181   // The part of _g1_storage that is currently committed.
   182   MemRegion _g1_committed;
   184   // The maximum part of _g1_storage that has ever been committed.
   185   MemRegion _g1_max_committed;
   187   // The number of regions that are completely free.
   188   size_t _free_regions;
   190   // The number of regions we could create by expansion.
   191   size_t _expansion_regions;
   193   // Return the number of free regions in the heap (by direct counting.)
   194   size_t count_free_regions();
   195   // Return the number of free regions on the free and unclean lists.
   196   size_t count_free_regions_list();
   198   // The block offset table for the G1 heap.
   199   G1BlockOffsetSharedArray* _bot_shared;
   201   // Move all of the regions off the free lists, then rebuild those free
   202   // lists, before and after full GC.
   203   void tear_down_region_lists();
   204   void rebuild_region_lists();
   205   // This sets all non-empty regions to need zero-fill (which they will if
   206   // they are empty after full collection.)
   207   void set_used_regions_to_need_zero_fill();
   209   // The sequence of all heap regions in the heap.
   210   HeapRegionSeq* _hrs;
   212   // The region from which normal-sized objects are currently being
   213   // allocated.  May be NULL.
   214   HeapRegion* _cur_alloc_region;
   216   // Postcondition: cur_alloc_region == NULL.
   217   void abandon_cur_alloc_region();
   219   // The to-space memory regions into which objects are being copied during
   220   // a GC.
   221   HeapRegion* _gc_alloc_regions[GCAllocPurposeCount];
   222   uint _gc_alloc_region_counts[GCAllocPurposeCount];
   224   // A list of the regions that have been set to be alloc regions in the
   225   // current collection.
   226   HeapRegion* _gc_alloc_region_list;
   228   // When called by par thread, require par_alloc_during_gc_lock() to be held.
   229   void push_gc_alloc_region(HeapRegion* hr);
   231   // This should only be called single-threaded.  Undeclares all GC alloc
   232   // regions.
   233   void forget_alloc_region_list();
   235   // Should be used to set an alloc region, because there's other
   236   // associated bookkeeping.
   237   void set_gc_alloc_region(int purpose, HeapRegion* r);
   239   // Check well-formedness of alloc region list.
   240   bool check_gc_alloc_regions();
   242   // Outside of GC pauses, the number of bytes used in all regions other
   243   // than the current allocation region.
   244   size_t _summary_bytes_used;
   246   // Summary information about popular objects; method to print it.
   247   NumberSeq _pop_obj_rc_at_copy;
   248   void print_popularity_summary_info() const;
   250   volatile unsigned _gc_time_stamp;
   252   size_t* _surviving_young_words;
   254   void setup_surviving_young_words();
   255   void update_surviving_young_words(size_t* surv_young_words);
   256   void cleanup_surviving_young_words();
   258 protected:
   260   // Returns "true" iff none of the gc alloc regions have any allocations
   261   // since the last call to "save_marks".
   262   bool all_alloc_regions_no_allocs_since_save_marks();
   263   // Calls "note_end_of_copying on all gc alloc_regions.
   264   void all_alloc_regions_note_end_of_copying();
   266   // The number of regions allocated to hold humongous objects.
   267   int         _num_humongous_regions;
   268   YoungList*  _young_list;
   270   // The current policy object for the collector.
   271   G1CollectorPolicy* _g1_policy;
   273   // Parallel allocation lock to protect the current allocation region.
   274   Mutex  _par_alloc_during_gc_lock;
   275   Mutex* par_alloc_during_gc_lock() { return &_par_alloc_during_gc_lock; }
   277   // If possible/desirable, allocate a new HeapRegion for normal object
   278   // allocation sufficient for an allocation of the given "word_size".
   279   // If "do_expand" is true, will attempt to expand the heap if necessary
   280   // to to satisfy the request.  If "zero_filled" is true, requires a
   281   // zero-filled region.
   282   // (Returning NULL will trigger a GC.)
   283   virtual HeapRegion* newAllocRegion_work(size_t word_size,
   284                                           bool do_expand,
   285                                           bool zero_filled);
   287   virtual HeapRegion* newAllocRegion(size_t word_size,
   288                                      bool zero_filled = true) {
   289     return newAllocRegion_work(word_size, false, zero_filled);
   290   }
   291   virtual HeapRegion* newAllocRegionWithExpansion(int purpose,
   292                                                   size_t word_size,
   293                                                   bool zero_filled = true);
   295   // Attempt to allocate an object of the given (very large) "word_size".
   296   // Returns "NULL" on failure.
   297   virtual HeapWord* humongousObjAllocate(size_t word_size);
   299   // If possible, allocate a block of the given word_size, else return "NULL".
   300   // Returning NULL will trigger GC or heap expansion.
   301   // These two methods have rather awkward pre- and
   302   // post-conditions. If they are called outside a safepoint, then
   303   // they assume that the caller is holding the heap lock. Upon return
   304   // they release the heap lock, if they are returning a non-NULL
   305   // value. attempt_allocation_slow() also dirties the cards of a
   306   // newly-allocated young region after it releases the heap
   307   // lock. This change in interface was the neatest way to achieve
   308   // this card dirtying without affecting mem_allocate(), which is a
   309   // more frequently called method. We tried two or three different
   310   // approaches, but they were even more hacky.
   311   HeapWord* attempt_allocation(size_t word_size,
   312                                bool permit_collection_pause = true);
   314   HeapWord* attempt_allocation_slow(size_t word_size,
   315                                     bool permit_collection_pause = true);
   317   // Allocate blocks during garbage collection. Will ensure an
   318   // allocation region, either by picking one or expanding the
   319   // heap, and then allocate a block of the given size. The block
   320   // may not be a humongous - it must fit into a single heap region.
   321   HeapWord* allocate_during_gc(GCAllocPurpose purpose, size_t word_size);
   322   HeapWord* par_allocate_during_gc(GCAllocPurpose purpose, size_t word_size);
   324   HeapWord* allocate_during_gc_slow(GCAllocPurpose purpose,
   325                                     HeapRegion*    alloc_region,
   326                                     bool           par,
   327                                     size_t         word_size);
   329   // Ensure that no further allocations can happen in "r", bearing in mind
   330   // that parallel threads might be attempting allocations.
   331   void par_allocate_remaining_space(HeapRegion* r);
   333   // Helper function for two callbacks below.
   334   // "full", if true, indicates that the GC is for a System.gc() request,
   335   // and should collect the entire heap.  If "clear_all_soft_refs" is true,
   336   // all soft references are cleared during the GC.  If "full" is false,
   337   // "word_size" describes the allocation that the GC should
   338   // attempt (at least) to satisfy.
   339   void do_collection(bool full, bool clear_all_soft_refs,
   340                      size_t word_size);
   342   // Callback from VM_G1CollectFull operation.
   343   // Perform a full collection.
   344   void do_full_collection(bool clear_all_soft_refs);
   346   // Resize the heap if necessary after a full collection.  If this is
   347   // after a collect-for allocation, "word_size" is the allocation size,
   348   // and will be considered part of the used portion of the heap.
   349   void resize_if_necessary_after_full_collection(size_t word_size);
   351   // Callback from VM_G1CollectForAllocation operation.
   352   // This function does everything necessary/possible to satisfy a
   353   // failed allocation request (including collection, expansion, etc.)
   354   HeapWord* satisfy_failed_allocation(size_t word_size);
   356   // Attempting to expand the heap sufficiently
   357   // to support an allocation of the given "word_size".  If
   358   // successful, perform the allocation and return the address of the
   359   // allocated block, or else "NULL".
   360   virtual HeapWord* expand_and_allocate(size_t word_size);
   362 public:
   363   // Expand the garbage-first heap by at least the given size (in bytes!).
   364   // (Rounds up to a HeapRegion boundary.)
   365   virtual void expand(size_t expand_bytes);
   367   // Do anything common to GC's.
   368   virtual void gc_prologue(bool full);
   369   virtual void gc_epilogue(bool full);
   371 protected:
   373   // Shrink the garbage-first heap by at most the given size (in bytes!).
   374   // (Rounds down to a HeapRegion boundary.)
   375   virtual void shrink(size_t expand_bytes);
   376   void shrink_helper(size_t expand_bytes);
   378   // Do an incremental collection: identify a collection set, and evacuate
   379   // its live objects elsewhere.
   380   virtual void do_collection_pause();
   382   // The guts of the incremental collection pause, executed by the vm
   383   // thread.  If "popular_region" is non-NULL, this pause should evacuate
   384   // this single region whose remembered set has gotten large, moving
   385   // any popular objects to one of the popular regions.
   386   virtual void do_collection_pause_at_safepoint(HeapRegion* popular_region);
   388   // Actually do the work of evacuating the collection set.
   389   virtual void evacuate_collection_set();
   391   // If this is an appropriate right time, do a collection pause.
   392   // The "word_size" argument, if non-zero, indicates the size of an
   393   // allocation request that is prompting this query.
   394   void do_collection_pause_if_appropriate(size_t word_size);
   396   // The g1 remembered set of the heap.
   397   G1RemSet* _g1_rem_set;
   398   // And it's mod ref barrier set, used to track updates for the above.
   399   ModRefBarrierSet* _mr_bs;
   401   // The Heap Region Rem Set Iterator.
   402   HeapRegionRemSetIterator** _rem_set_iterator;
   404   // The closure used to refine a single card.
   405   RefineCardTableEntryClosure* _refine_cte_cl;
   407   // A function to check the consistency of dirty card logs.
   408   void check_ct_logs_at_safepoint();
   410   // After a collection pause, make the regions in the CS into free
   411   // regions.
   412   void free_collection_set(HeapRegion* cs_head);
   414   // Applies "scan_non_heap_roots" to roots outside the heap,
   415   // "scan_rs" to roots inside the heap (having done "set_region" to
   416   // indicate the region in which the root resides), and does "scan_perm"
   417   // (setting the generation to the perm generation.)  If "scan_rs" is
   418   // NULL, then this step is skipped.  The "worker_i"
   419   // param is for use with parallel roots processing, and should be
   420   // the "i" of the calling parallel worker thread's work(i) function.
   421   // In the sequential case this param will be ignored.
   422   void g1_process_strong_roots(bool collecting_perm_gen,
   423                                SharedHeap::ScanningOption so,
   424                                OopClosure* scan_non_heap_roots,
   425                                OopsInHeapRegionClosure* scan_rs,
   426                                OopsInHeapRegionClosure* scan_so,
   427                                OopsInGenClosure* scan_perm,
   428                                int worker_i);
   430   void scan_scan_only_set(OopsInHeapRegionClosure* oc,
   431                           int worker_i);
   432   void scan_scan_only_region(HeapRegion* hr,
   433                              OopsInHeapRegionClosure* oc,
   434                              int worker_i);
   436   // Apply "blk" to all the weak roots of the system.  These include
   437   // JNI weak roots, the code cache, system dictionary, symbol table,
   438   // string table, and referents of reachable weak refs.
   439   void g1_process_weak_roots(OopClosure* root_closure,
   440                              OopClosure* non_root_closure);
   442   // Invoke "save_marks" on all heap regions.
   443   void save_marks();
   445   // Free a heap region.
   446   void free_region(HeapRegion* hr);
   447   // A component of "free_region", exposed for 'batching'.
   448   // All the params after "hr" are out params: the used bytes of the freed
   449   // region(s), the number of H regions cleared, the number of regions
   450   // freed, and pointers to the head and tail of a list of freed contig
   451   // regions, linked throught the "next_on_unclean_list" field.
   452   void free_region_work(HeapRegion* hr,
   453                         size_t& pre_used,
   454                         size_t& cleared_h,
   455                         size_t& freed_regions,
   456                         UncleanRegionList* list,
   457                         bool par = false);
   460   // The concurrent marker (and the thread it runs in.)
   461   ConcurrentMark* _cm;
   462   ConcurrentMarkThread* _cmThread;
   463   bool _mark_in_progress;
   465   // The concurrent refiner.
   466   ConcurrentG1Refine* _cg1r;
   468   // The concurrent zero-fill thread.
   469   ConcurrentZFThread* _czft;
   471   // The parallel task queues
   472   RefToScanQueueSet *_task_queues;
   474   // True iff a evacuation has failed in the current collection.
   475   bool _evacuation_failed;
   477   // Set the attribute indicating whether evacuation has failed in the
   478   // current collection.
   479   void set_evacuation_failed(bool b) { _evacuation_failed = b; }
   481   // Failed evacuations cause some logical from-space objects to have
   482   // forwarding pointers to themselves.  Reset them.
   483   void remove_self_forwarding_pointers();
   485   // When one is non-null, so is the other.  Together, they each pair is
   486   // an object with a preserved mark, and its mark value.
   487   GrowableArray<oop>*     _objs_with_preserved_marks;
   488   GrowableArray<markOop>* _preserved_marks_of_objs;
   490   // Preserve the mark of "obj", if necessary, in preparation for its mark
   491   // word being overwritten with a self-forwarding-pointer.
   492   void preserve_mark_if_necessary(oop obj, markOop m);
   494   // The stack of evac-failure objects left to be scanned.
   495   GrowableArray<oop>*    _evac_failure_scan_stack;
   496   // The closure to apply to evac-failure objects.
   498   OopsInHeapRegionClosure* _evac_failure_closure;
   499   // Set the field above.
   500   void
   501   set_evac_failure_closure(OopsInHeapRegionClosure* evac_failure_closure) {
   502     _evac_failure_closure = evac_failure_closure;
   503   }
   505   // Push "obj" on the scan stack.
   506   void push_on_evac_failure_scan_stack(oop obj);
   507   // Process scan stack entries until the stack is empty.
   508   void drain_evac_failure_scan_stack();
   509   // True iff an invocation of "drain_scan_stack" is in progress; to
   510   // prevent unnecessary recursion.
   511   bool _drain_in_progress;
   513   // Do any necessary initialization for evacuation-failure handling.
   514   // "cl" is the closure that will be used to process evac-failure
   515   // objects.
   516   void init_for_evac_failure(OopsInHeapRegionClosure* cl);
   517   // Do any necessary cleanup for evacuation-failure handling data
   518   // structures.
   519   void finalize_for_evac_failure();
   521   // An attempt to evacuate "obj" has failed; take necessary steps.
   522   void handle_evacuation_failure(oop obj);
   523   oop handle_evacuation_failure_par(OopsInHeapRegionClosure* cl, oop obj);
   524   void handle_evacuation_failure_common(oop obj, markOop m);
   527   // Ensure that the relevant gc_alloc regions are set.
   528   void get_gc_alloc_regions();
   529   // We're done with GC alloc regions; release them, as appropriate.
   530   void release_gc_alloc_regions();
   532   // ("Weak") Reference processing support
   533   ReferenceProcessor* _ref_processor;
   535   enum G1H_process_strong_roots_tasks {
   536     G1H_PS_mark_stack_oops_do,
   537     G1H_PS_refProcessor_oops_do,
   538     // Leave this one last.
   539     G1H_PS_NumElements
   540   };
   542   SubTasksDone* _process_strong_tasks;
   544   // Allocate space to hold a popular object.  Result is guaranteed below
   545   // "popular_object_boundary()".  Note: CURRENTLY halts the system if we
   546   // run out of space to hold popular objects.
   547   HeapWord* allocate_popular_object(size_t word_size);
   549   // The boundary between popular and non-popular objects.
   550   HeapWord* _popular_object_boundary;
   552   HeapRegionList* _popular_regions_to_be_evacuated;
   554   // Compute which objects in "single_region" are popular.  If any are,
   555   // evacuate them to a popular region, leaving behind forwarding pointers,
   556   // and select "popular_region" as the single collection set region.
   557   // Otherwise, leave the collection set null.
   558   void popularity_pause_preamble(HeapRegion* populer_region);
   560   // Compute which objects in "single_region" are popular, and evacuate
   561   // them to a popular region, leaving behind forwarding pointers.
   562   // Returns "true" if at least one popular object is discovered and
   563   // evacuated.  In any case, "*max_rc" is set to the maximum reference
   564   // count of an object in the region.
   565   bool compute_reference_counts_and_evac_popular(HeapRegion* populer_region,
   566                                                  size_t* max_rc);
   567   // Subroutines used in the above.
   568   bool _rc_region_above;
   569   size_t _rc_region_diff;
   570   jint* obj_rc_addr(oop obj) {
   571     uintptr_t obj_addr = (uintptr_t)obj;
   572     if (_rc_region_above) {
   573       jint* res = (jint*)(obj_addr + _rc_region_diff);
   574       assert((uintptr_t)res > obj_addr, "RC region is above.");
   575       return res;
   576     } else {
   577       jint* res = (jint*)(obj_addr - _rc_region_diff);
   578       assert((uintptr_t)res < obj_addr, "RC region is below.");
   579       return res;
   580     }
   581   }
   582   jint obj_rc(oop obj) {
   583     return *obj_rc_addr(obj);
   584   }
   585   void inc_obj_rc(oop obj) {
   586     (*obj_rc_addr(obj))++;
   587   }
   588   void atomic_inc_obj_rc(oop obj);
   591   // Number of popular objects and bytes (latter is cheaper!).
   592   size_t pop_object_used_objs();
   593   size_t pop_object_used_bytes();
   595   // Index of the popular region in which allocation is currently being
   596   // done.
   597   int _cur_pop_hr_index;
   599   // List of regions which require zero filling.
   600   UncleanRegionList _unclean_region_list;
   601   bool _unclean_regions_coming;
   603   bool check_age_cohort_well_formed_work(int a, HeapRegion* hr);
   605 public:
   606   void set_refine_cte_cl_concurrency(bool concurrent);
   608   RefToScanQueue *task_queue(int i);
   610   // Create a G1CollectedHeap with the specified policy.
   611   // Must call the initialize method afterwards.
   612   // May not return if something goes wrong.
   613   G1CollectedHeap(G1CollectorPolicy* policy);
   615   // Initialize the G1CollectedHeap to have the initial and
   616   // maximum sizes, permanent generation, and remembered and barrier sets
   617   // specified by the policy object.
   618   jint initialize();
   620   void ref_processing_init();
   622   void set_par_threads(int t) {
   623     SharedHeap::set_par_threads(t);
   624     _process_strong_tasks->set_par_threads(t);
   625   }
   627   virtual CollectedHeap::Name kind() const {
   628     return CollectedHeap::G1CollectedHeap;
   629   }
   631   // The current policy object for the collector.
   632   G1CollectorPolicy* g1_policy() const { return _g1_policy; }
   634   // Adaptive size policy.  No such thing for g1.
   635   virtual AdaptiveSizePolicy* size_policy() { return NULL; }
   637   // The rem set and barrier set.
   638   G1RemSet* g1_rem_set() const { return _g1_rem_set; }
   639   ModRefBarrierSet* mr_bs() const { return _mr_bs; }
   641   // The rem set iterator.
   642   HeapRegionRemSetIterator* rem_set_iterator(int i) {
   643     return _rem_set_iterator[i];
   644   }
   646   HeapRegionRemSetIterator* rem_set_iterator() {
   647     return _rem_set_iterator[0];
   648   }
   650   unsigned get_gc_time_stamp() {
   651     return _gc_time_stamp;
   652   }
   654   void reset_gc_time_stamp() {
   655     _gc_time_stamp = 0;
   656     OrderAccess::fence();
   657   }
   659   void increment_gc_time_stamp() {
   660     ++_gc_time_stamp;
   661     OrderAccess::fence();
   662   }
   664   void iterate_dirty_card_closure(bool concurrent, int worker_i);
   666   // The shared block offset table array.
   667   G1BlockOffsetSharedArray* bot_shared() const { return _bot_shared; }
   669   // Reference Processing accessor
   670   ReferenceProcessor* ref_processor() { return _ref_processor; }
   672   // Reserved (g1 only; super method includes perm), capacity and the used
   673   // portion in bytes.
   674   size_t g1_reserved_obj_bytes() { return _g1_reserved.byte_size(); }
   675   virtual size_t capacity() const;
   676   virtual size_t used() const;
   677   size_t recalculate_used() const;
   678 #ifndef PRODUCT
   679   size_t recalculate_used_regions() const;
   680 #endif // PRODUCT
   682   // These virtual functions do the actual allocation.
   683   virtual HeapWord* mem_allocate(size_t word_size,
   684                                  bool   is_noref,
   685                                  bool   is_tlab,
   686                                  bool* gc_overhead_limit_was_exceeded);
   688   // Some heaps may offer a contiguous region for shared non-blocking
   689   // allocation, via inlined code (by exporting the address of the top and
   690   // end fields defining the extent of the contiguous allocation region.)
   691   // But G1CollectedHeap doesn't yet support this.
   693   // Return an estimate of the maximum allocation that could be performed
   694   // without triggering any collection or expansion activity.  In a
   695   // generational collector, for example, this is probably the largest
   696   // allocation that could be supported (without expansion) in the youngest
   697   // generation.  It is "unsafe" because no locks are taken; the result
   698   // should be treated as an approximation, not a guarantee, for use in
   699   // heuristic resizing decisions.
   700   virtual size_t unsafe_max_alloc();
   702   virtual bool is_maximal_no_gc() const {
   703     return _g1_storage.uncommitted_size() == 0;
   704   }
   706   // The total number of regions in the heap.
   707   size_t n_regions();
   709   // The number of regions that are completely free.
   710   size_t max_regions();
   712   // The number of regions that are completely free.
   713   size_t free_regions();
   715   // The number of regions that are not completely free.
   716   size_t used_regions() { return n_regions() - free_regions(); }
   718   // True iff the ZF thread should run.
   719   bool should_zf();
   721   // The number of regions available for "regular" expansion.
   722   size_t expansion_regions() { return _expansion_regions; }
   724 #ifndef PRODUCT
   725   bool regions_accounted_for();
   726   bool print_region_accounting_info();
   727   void print_region_counts();
   728 #endif
   730   HeapRegion* alloc_region_from_unclean_list(bool zero_filled);
   731   HeapRegion* alloc_region_from_unclean_list_locked(bool zero_filled);
   733   void put_region_on_unclean_list(HeapRegion* r);
   734   void put_region_on_unclean_list_locked(HeapRegion* r);
   736   void prepend_region_list_on_unclean_list(UncleanRegionList* list);
   737   void prepend_region_list_on_unclean_list_locked(UncleanRegionList* list);
   739   void set_unclean_regions_coming(bool b);
   740   void set_unclean_regions_coming_locked(bool b);
   741   // Wait for cleanup to be complete.
   742   void wait_for_cleanup_complete();
   743   // Like above, but assumes that the calling thread owns the Heap_lock.
   744   void wait_for_cleanup_complete_locked();
   746   // Return the head of the unclean list.
   747   HeapRegion* peek_unclean_region_list_locked();
   748   // Remove and return the head of the unclean list.
   749   HeapRegion* pop_unclean_region_list_locked();
   751   // List of regions which are zero filled and ready for allocation.
   752   HeapRegion* _free_region_list;
   753   // Number of elements on the free list.
   754   size_t _free_region_list_size;
   756   // If the head of the unclean list is ZeroFilled, move it to the free
   757   // list.
   758   bool move_cleaned_region_to_free_list_locked();
   759   bool move_cleaned_region_to_free_list();
   761   void put_free_region_on_list_locked(HeapRegion* r);
   762   void put_free_region_on_list(HeapRegion* r);
   764   // Remove and return the head element of the free list.
   765   HeapRegion* pop_free_region_list_locked();
   767   // If "zero_filled" is true, we first try the free list, then we try the
   768   // unclean list, zero-filling the result.  If "zero_filled" is false, we
   769   // first try the unclean list, then the zero-filled list.
   770   HeapRegion* alloc_free_region_from_lists(bool zero_filled);
   772   // Verify the integrity of the region lists.
   773   void remove_allocated_regions_from_lists();
   774   bool verify_region_lists();
   775   bool verify_region_lists_locked();
   776   size_t unclean_region_list_length();
   777   size_t free_region_list_length();
   779   // Perform a collection of the heap; intended for use in implementing
   780   // "System.gc".  This probably implies as full a collection as the
   781   // "CollectedHeap" supports.
   782   virtual void collect(GCCause::Cause cause);
   784   // The same as above but assume that the caller holds the Heap_lock.
   785   void collect_locked(GCCause::Cause cause);
   787   // This interface assumes that it's being called by the
   788   // vm thread. It collects the heap assuming that the
   789   // heap lock is already held and that we are executing in
   790   // the context of the vm thread.
   791   virtual void collect_as_vm_thread(GCCause::Cause cause);
   793   // True iff a evacuation has failed in the most-recent collection.
   794   bool evacuation_failed() { return _evacuation_failed; }
   796   // Free a region if it is totally full of garbage.  Returns the number of
   797   // bytes freed (0 ==> didn't free it).
   798   size_t free_region_if_totally_empty(HeapRegion *hr);
   799   void free_region_if_totally_empty_work(HeapRegion *hr,
   800                                          size_t& pre_used,
   801                                          size_t& cleared_h_regions,
   802                                          size_t& freed_regions,
   803                                          UncleanRegionList* list,
   804                                          bool par = false);
   806   // If we've done free region work that yields the given changes, update
   807   // the relevant global variables.
   808   void finish_free_region_work(size_t pre_used,
   809                                size_t cleared_h_regions,
   810                                size_t freed_regions,
   811                                UncleanRegionList* list);
   814   // Returns "TRUE" iff "p" points into the allocated area of the heap.
   815   virtual bool is_in(const void* p) const;
   817   // Return "TRUE" iff the given object address is within the collection
   818   // set.
   819   inline bool obj_in_cs(oop obj);
   821   // Return "TRUE" iff the given object address is in the reserved
   822   // region of g1 (excluding the permanent generation).
   823   bool is_in_g1_reserved(const void* p) const {
   824     return _g1_reserved.contains(p);
   825   }
   827   // Returns a MemRegion that corresponds to the space that  has been
   828   // committed in the heap
   829   MemRegion g1_committed() {
   830     return _g1_committed;
   831   }
   833   NOT_PRODUCT( bool is_in_closed_subset(const void* p) const; )
   835   // Dirty card table entries covering a list of young regions.
   836   void dirtyCardsForYoungRegions(CardTableModRefBS* ct_bs, HeapRegion* list);
   838   // This resets the card table to all zeros.  It is used after
   839   // a collection pause which used the card table to claim cards.
   840   void cleanUpCardTable();
   842   // Iteration functions.
   844   // Iterate over all the ref-containing fields of all objects, calling
   845   // "cl.do_oop" on each.
   846   virtual void oop_iterate(OopClosure* cl);
   848   // Same as above, restricted to a memory region.
   849   virtual void oop_iterate(MemRegion mr, OopClosure* cl);
   851   // Iterate over all objects, calling "cl.do_object" on each.
   852   virtual void object_iterate(ObjectClosure* cl);
   854   // Iterate over all objects allocated since the last collection, calling
   855   // "cl.do_object" on each.  The heap must have been initialized properly
   856   // to support this function, or else this call will fail.
   857   virtual void object_iterate_since_last_GC(ObjectClosure* cl);
   859   // Iterate over all spaces in use in the heap, in ascending address order.
   860   virtual void space_iterate(SpaceClosure* cl);
   862   // Iterate over heap regions, in address order, terminating the
   863   // iteration early if the "doHeapRegion" method returns "true".
   864   void heap_region_iterate(HeapRegionClosure* blk);
   866   // Iterate over heap regions starting with r (or the first region if "r"
   867   // is NULL), in address order, terminating early if the "doHeapRegion"
   868   // method returns "true".
   869   void heap_region_iterate_from(HeapRegion* r, HeapRegionClosure* blk);
   871   // As above but starting from the region at index idx.
   872   void heap_region_iterate_from(int idx, HeapRegionClosure* blk);
   874   HeapRegion* region_at(size_t idx);
   876   // Divide the heap region sequence into "chunks" of some size (the number
   877   // of regions divided by the number of parallel threads times some
   878   // overpartition factor, currently 4).  Assumes that this will be called
   879   // in parallel by ParallelGCThreads worker threads with discinct worker
   880   // ids in the range [0..max(ParallelGCThreads-1, 1)], that all parallel
   881   // calls will use the same "claim_value", and that that claim value is
   882   // different from the claim_value of any heap region before the start of
   883   // the iteration.  Applies "blk->doHeapRegion" to each of the regions, by
   884   // attempting to claim the first region in each chunk, and, if
   885   // successful, applying the closure to each region in the chunk (and
   886   // setting the claim value of the second and subsequent regions of the
   887   // chunk.)  For now requires that "doHeapRegion" always returns "false",
   888   // i.e., that a closure never attempt to abort a traversal.
   889   void heap_region_par_iterate_chunked(HeapRegionClosure* blk,
   890                                        int worker,
   891                                        jint claim_value);
   893   // It resets all the region claim values to the default.
   894   void reset_heap_region_claim_values();
   896 #ifdef ASSERT
   897   bool check_heap_region_claim_values(jint claim_value);
   898 #endif // ASSERT
   900   // Iterate over the regions (if any) in the current collection set.
   901   void collection_set_iterate(HeapRegionClosure* blk);
   903   // As above but starting from region r
   904   void collection_set_iterate_from(HeapRegion* r, HeapRegionClosure *blk);
   906   // Returns the first (lowest address) compactible space in the heap.
   907   virtual CompactibleSpace* first_compactible_space();
   909   // A CollectedHeap will contain some number of spaces.  This finds the
   910   // space containing a given address, or else returns NULL.
   911   virtual Space* space_containing(const void* addr) const;
   913   // A G1CollectedHeap will contain some number of heap regions.  This
   914   // finds the region containing a given address, or else returns NULL.
   915   HeapRegion* heap_region_containing(const void* addr) const;
   917   // Like the above, but requires "addr" to be in the heap (to avoid a
   918   // null-check), and unlike the above, may return an continuing humongous
   919   // region.
   920   HeapRegion* heap_region_containing_raw(const void* addr) const;
   922   // A CollectedHeap is divided into a dense sequence of "blocks"; that is,
   923   // each address in the (reserved) heap is a member of exactly
   924   // one block.  The defining characteristic of a block is that it is
   925   // possible to find its size, and thus to progress forward to the next
   926   // block.  (Blocks may be of different sizes.)  Thus, blocks may
   927   // represent Java objects, or they might be free blocks in a
   928   // free-list-based heap (or subheap), as long as the two kinds are
   929   // distinguishable and the size of each is determinable.
   931   // Returns the address of the start of the "block" that contains the
   932   // address "addr".  We say "blocks" instead of "object" since some heaps
   933   // may not pack objects densely; a chunk may either be an object or a
   934   // non-object.
   935   virtual HeapWord* block_start(const void* addr) const;
   937   // Requires "addr" to be the start of a chunk, and returns its size.
   938   // "addr + size" is required to be the start of a new chunk, or the end
   939   // of the active area of the heap.
   940   virtual size_t block_size(const HeapWord* addr) const;
   942   // Requires "addr" to be the start of a block, and returns "TRUE" iff
   943   // the block is an object.
   944   virtual bool block_is_obj(const HeapWord* addr) const;
   946   // Does this heap support heap inspection? (+PrintClassHistogram)
   947   virtual bool supports_heap_inspection() const { return true; }
   949   // Section on thread-local allocation buffers (TLABs)
   950   // See CollectedHeap for semantics.
   952   virtual bool supports_tlab_allocation() const;
   953   virtual size_t tlab_capacity(Thread* thr) const;
   954   virtual size_t unsafe_max_tlab_alloc(Thread* thr) const;
   955   virtual HeapWord* allocate_new_tlab(size_t size);
   957   // Can a compiler initialize a new object without store barriers?
   958   // This permission only extends from the creation of a new object
   959   // via a TLAB up to the first subsequent safepoint.
   960   virtual bool can_elide_tlab_store_barriers() const {
   961     // Since G1's TLAB's may, on occasion, come from non-young regions
   962     // as well. (Is there a flag controlling that? XXX)
   963     return false;
   964   }
   966   // Can a compiler elide a store barrier when it writes
   967   // a permanent oop into the heap?  Applies when the compiler
   968   // is storing x to the heap, where x->is_perm() is true.
   969   virtual bool can_elide_permanent_oop_store_barriers() const {
   970     // At least until perm gen collection is also G1-ified, at
   971     // which point this should return false.
   972     return true;
   973   }
   975   virtual bool allocs_are_zero_filled();
   977   // The boundary between a "large" and "small" array of primitives, in
   978   // words.
   979   virtual size_t large_typearray_limit();
   981   // All popular objects are guaranteed to have addresses below this
   982   // boundary.
   983   HeapWord* popular_object_boundary() {
   984     return _popular_object_boundary;
   985   }
   987   // Declare the region as one that should be evacuated because its
   988   // remembered set is too large.
   989   void schedule_popular_region_evac(HeapRegion* r);
   990   // If there is a popular region to evacuate it, remove it from the list
   991   // and return it.
   992   HeapRegion* popular_region_to_evac();
   993   // Evacuate the given popular region.
   994   void evac_popular_region(HeapRegion* r);
   996   // Returns "true" iff the given word_size is "very large".
   997   static bool isHumongous(size_t word_size) {
   998     return word_size >= VeryLargeInWords;
   999   }
  1001   // Update mod union table with the set of dirty cards.
  1002   void updateModUnion();
  1004   // Set the mod union bits corresponding to the given memRegion.  Note
  1005   // that this is always a safe operation, since it doesn't clear any
  1006   // bits.
  1007   void markModUnionRange(MemRegion mr);
  1009   // Records the fact that a marking phase is no longer in progress.
  1010   void set_marking_complete() {
  1011     _mark_in_progress = false;
  1013   void set_marking_started() {
  1014     _mark_in_progress = true;
  1016   bool mark_in_progress() {
  1017     return _mark_in_progress;
  1020   // Print the maximum heap capacity.
  1021   virtual size_t max_capacity() const;
  1023   virtual jlong millis_since_last_gc();
  1025   // Perform any cleanup actions necessary before allowing a verification.
  1026   virtual void prepare_for_verify();
  1028   // Perform verification.
  1029   virtual void verify(bool allow_dirty, bool silent);
  1030   virtual void print() const;
  1031   virtual void print_on(outputStream* st) const;
  1033   virtual void print_gc_threads_on(outputStream* st) const;
  1034   virtual void gc_threads_do(ThreadClosure* tc) const;
  1036   // Override
  1037   void print_tracing_info() const;
  1039   // If "addr" is a pointer into the (reserved?) heap, returns a positive
  1040   // number indicating the "arena" within the heap in which "addr" falls.
  1041   // Or else returns 0.
  1042   virtual int addr_to_arena_id(void* addr) const;
  1044   // Convenience function to be used in situations where the heap type can be
  1045   // asserted to be this type.
  1046   static G1CollectedHeap* heap();
  1048   void empty_young_list();
  1049   bool should_set_young_locked();
  1051   void set_region_short_lived_locked(HeapRegion* hr);
  1052   // add appropriate methods for any other surv rate groups
  1054   void young_list_rs_length_sampling_init() {
  1055     _young_list->rs_length_sampling_init();
  1057   bool young_list_rs_length_sampling_more() {
  1058     return _young_list->rs_length_sampling_more();
  1060   void young_list_rs_length_sampling_next() {
  1061     _young_list->rs_length_sampling_next();
  1063   size_t young_list_sampled_rs_lengths() {
  1064     return _young_list->sampled_rs_lengths();
  1067   size_t young_list_length()   { return _young_list->length(); }
  1068   size_t young_list_scan_only_length() {
  1069                                       return _young_list->scan_only_length(); }
  1071   HeapRegion* pop_region_from_young_list() {
  1072     return _young_list->pop_region();
  1075   HeapRegion* young_list_first_region() {
  1076     return _young_list->first_region();
  1079   // debugging
  1080   bool check_young_list_well_formed() {
  1081     return _young_list->check_list_well_formed();
  1083   bool check_young_list_empty(bool ignore_scan_only_list,
  1084                               bool check_sample = true);
  1086   // *** Stuff related to concurrent marking.  It's not clear to me that so
  1087   // many of these need to be public.
  1089   // The functions below are helper functions that a subclass of
  1090   // "CollectedHeap" can use in the implementation of its virtual
  1091   // functions.
  1092   // This performs a concurrent marking of the live objects in a
  1093   // bitmap off to the side.
  1094   void doConcurrentMark();
  1096   // This is called from the marksweep collector which then does
  1097   // a concurrent mark and verifies that the results agree with
  1098   // the stop the world marking.
  1099   void checkConcurrentMark();
  1100   void do_sync_mark();
  1102   bool isMarkedPrev(oop obj) const;
  1103   bool isMarkedNext(oop obj) const;
  1105   // Determine if an object is dead, given the object and also
  1106   // the region to which the object belongs. An object is dead
  1107   // iff a) it was not allocated since the last mark and b) it
  1108   // is not marked.
  1110   bool is_obj_dead(const oop obj, const HeapRegion* hr) const {
  1111     return
  1112       !hr->obj_allocated_since_prev_marking(obj) &&
  1113       !isMarkedPrev(obj);
  1116   // This is used when copying an object to survivor space.
  1117   // If the object is marked live, then we mark the copy live.
  1118   // If the object is allocated since the start of this mark
  1119   // cycle, then we mark the copy live.
  1120   // If the object has been around since the previous mark
  1121   // phase, and hasn't been marked yet during this phase,
  1122   // then we don't mark it, we just wait for the
  1123   // current marking cycle to get to it.
  1125   // This function returns true when an object has been
  1126   // around since the previous marking and hasn't yet
  1127   // been marked during this marking.
  1129   bool is_obj_ill(const oop obj, const HeapRegion* hr) const {
  1130     return
  1131       !hr->obj_allocated_since_next_marking(obj) &&
  1132       !isMarkedNext(obj);
  1135   // Determine if an object is dead, given only the object itself.
  1136   // This will find the region to which the object belongs and
  1137   // then call the region version of the same function.
  1139   // Added if it is in permanent gen it isn't dead.
  1140   // Added if it is NULL it isn't dead.
  1142   bool is_obj_dead(oop obj) {
  1143     HeapRegion* hr = heap_region_containing(obj);
  1144     if (hr == NULL) {
  1145       if (Universe::heap()->is_in_permanent(obj))
  1146         return false;
  1147       else if (obj == NULL) return false;
  1148       else return true;
  1150     else return is_obj_dead(obj, hr);
  1153   bool is_obj_ill(oop obj) {
  1154     HeapRegion* hr = heap_region_containing(obj);
  1155     if (hr == NULL) {
  1156       if (Universe::heap()->is_in_permanent(obj))
  1157         return false;
  1158       else if (obj == NULL) return false;
  1159       else return true;
  1161     else return is_obj_ill(obj, hr);
  1164   // The following is just to alert the verification code
  1165   // that a full collection has occurred and that the
  1166   // remembered sets are no longer up to date.
  1167   bool _full_collection;
  1168   void set_full_collection() { _full_collection = true;}
  1169   void clear_full_collection() {_full_collection = false;}
  1170   bool full_collection() {return _full_collection;}
  1172   ConcurrentMark* concurrent_mark() const { return _cm; }
  1173   ConcurrentG1Refine* concurrent_g1_refine() const { return _cg1r; }
  1175 public:
  1176   void stop_conc_gc_threads();
  1178   // <NEW PREDICTION>
  1180   double predict_region_elapsed_time_ms(HeapRegion* hr, bool young);
  1181   void check_if_region_is_too_expensive(double predicted_time_ms);
  1182   size_t pending_card_num();
  1183   size_t max_pending_card_num();
  1184   size_t cards_scanned();
  1186   // </NEW PREDICTION>
  1188 protected:
  1189   size_t _max_heap_capacity;
  1191 //  debug_only(static void check_for_valid_allocation_state();)
  1193 public:
  1194   // Temporary: call to mark things unimplemented for the G1 heap (e.g.,
  1195   // MemoryService).  In productization, we can make this assert false
  1196   // to catch such places (as well as searching for calls to this...)
  1197   static void g1_unimplemented();
  1199 };
  1201 // Local Variables: ***
  1202 // c-indentation-style: gnu ***
  1203 // End: ***

mercurial