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

Print this page
rev 4773 : 8005849: JEP 167: Event-Based JVM Tracing
Reviewed-by: acorn, coleenp, sla
Contributed-by: Karen Kinnear <karen.kinnear@oracle.com>, Bengt Rutisson <bengt.rutisson@oracle.com>, Calvin Cheung <calvin.cheung@oracle.com>, Erik Gahlin <erik.gahlin@oracle.com>, Erik Helin <erik.helin@oracle.com>, Jesper Wilhelmsson <jesper.wilhelmsson@oracle.com>, Keith McGuigan <keith.mcguigan@oracle.com>, Mattias Tobiasson <mattias.tobiasson@oracle.com>, Markus Gronlund <markus.gronlund@oracle.com>, Mikael Auno <mikael.auno@oracle.com>, Nils Eliasson <nils.eliasson@oracle.com>, Nils Loodin <nils.loodin@oracle.com>, Rickard Backman <rickard.backman@oracle.com>, Staffan Larsen <staffan.larsen@oracle.com>, Stefan Karlsson <stefan.karlsson@oracle.com>, Yekaterina Kantserova <yekaterina.kantserova@oracle.com>
   1 /*
   2  * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1COLLECTEDHEAP_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1COLLECTEDHEAP_HPP
  27 
  28 #include "gc_implementation/g1/concurrentMark.hpp"

  29 #include "gc_implementation/g1/g1AllocRegion.hpp"
  30 #include "gc_implementation/g1/g1HRPrinter.hpp"
  31 #include "gc_implementation/g1/g1RemSet.hpp"
  32 #include "gc_implementation/g1/g1MonitoringSupport.hpp"


  33 #include "gc_implementation/g1/heapRegionSeq.hpp"
  34 #include "gc_implementation/g1/heapRegionSets.hpp"
  35 #include "gc_implementation/shared/hSpaceCounters.hpp"
  36 #include "gc_implementation/shared/parGCAllocBuffer.hpp"
  37 #include "memory/barrierSet.hpp"
  38 #include "memory/memRegion.hpp"
  39 #include "memory/sharedHeap.hpp"
  40 #include "utilities/stack.hpp"
  41 
  42 // A "G1CollectedHeap" is an implementation of a java heap for HotSpot.
  43 // It uses the "Garbage First" heap organization and algorithm, which
  44 // may combine concurrent marking with parallel, incremental compaction of
  45 // heap subsets that will yield large amounts of garbage.
  46 
  47 class HeapRegion;
  48 class HRRSCleanupTask;
  49 class GenerationSpec;
  50 class OopsInHeapRegionClosure;
  51 class G1KlassScanClosure;
  52 class G1ScanHeapEvacClosure;
  53 class ObjectClosure;
  54 class SpaceClosure;
  55 class CompactibleSpaceClosure;
  56 class Space;
  57 class G1CollectorPolicy;
  58 class GenRemSet;
  59 class G1RemSet;
  60 class HeapRegionRemSetIterator;
  61 class ConcurrentMark;
  62 class ConcurrentMarkThread;
  63 class ConcurrentG1Refine;

  64 class GenerationCounters;




  65 
  66 typedef OverflowTaskQueue<StarTask, mtGC>         RefToScanQueue;
  67 typedef GenericTaskQueueSet<RefToScanQueue, mtGC> RefToScanQueueSet;
  68 
  69 typedef int RegionIdx_t;   // needs to hold [ 0..max_regions() )
  70 typedef int CardIdx_t;     // needs to hold [ 0..CardsPerRegion )
  71 
  72 enum GCAllocPurpose {
  73   GCAllocForTenured,
  74   GCAllocForSurvived,
  75   GCAllocPurposeCount
  76 };
  77 
  78 class YoungList : public CHeapObj<mtGC> {
  79 private:
  80   G1CollectedHeap* _g1h;
  81 
  82   HeapRegion* _head;
  83 
  84   HeapRegion* _survivor_head;


 143 
 144   // debugging
 145   bool          check_list_well_formed();
 146   bool          check_list_empty(bool check_sample = true);
 147   void          print();
 148 };
 149 
 150 class MutatorAllocRegion : public G1AllocRegion {
 151 protected:
 152   virtual HeapRegion* allocate_new_region(size_t word_size, bool force);
 153   virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);
 154 public:
 155   MutatorAllocRegion()
 156     : G1AllocRegion("Mutator Alloc Region", false /* bot_updates */) { }
 157 };
 158 
 159 // The G1 STW is alive closure.
 160 // An instance is embedded into the G1CH and used as the
 161 // (optional) _is_alive_non_header closure in the STW
 162 // reference processor. It is also extensively used during
 163 // refence processing during STW evacuation pauses.
 164 class G1STWIsAliveClosure: public BoolObjectClosure {
 165   G1CollectedHeap* _g1;
 166 public:
 167   G1STWIsAliveClosure(G1CollectedHeap* g1) : _g1(g1) {}
 168   bool do_object_b(oop p);
 169 };
 170 
 171 class SurvivorGCAllocRegion : public G1AllocRegion {
 172 protected:
 173   virtual HeapRegion* allocate_new_region(size_t word_size, bool force);
 174   virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);
 175 public:
 176   SurvivorGCAllocRegion()
 177   : G1AllocRegion("Survivor GC Alloc Region", false /* bot_updates */) { }
 178 };
 179 
 180 class OldGCAllocRegion : public G1AllocRegion {
 181 protected:
 182   virtual HeapRegion* allocate_new_region(size_t word_size, bool force);
 183   virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);


 306 
 307   // The last old region we allocated to during the last GC.
 308   // Typically, it is not full so we should re-use it during the next GC.
 309   HeapRegion* _retained_old_gc_alloc_region;
 310 
 311   // It specifies whether we should attempt to expand the heap after a
 312   // region allocation failure. If heap expansion fails we set this to
 313   // false so that we don't re-attempt the heap expansion (it's likely
 314   // that subsequent expansion attempts will also fail if one fails).
 315   // Currently, it is only consulted during GC and it's reset at the
 316   // start of each GC.
 317   bool _expand_heap_after_alloc_failure;
 318 
 319   // It resets the mutator alloc region before new allocations can take place.
 320   void init_mutator_alloc_region();
 321 
 322   // It releases the mutator alloc region.
 323   void release_mutator_alloc_region();
 324 
 325   // It initializes the GC alloc regions at the start of a GC.
 326   void init_gc_alloc_regions();
 327 
 328   // It releases the GC alloc regions at the end of a GC.
 329   void release_gc_alloc_regions(uint no_of_gc_workers);
 330 
 331   // It does any cleanup that needs to be done on the GC alloc regions
 332   // before a Full GC.
 333   void abandon_gc_alloc_regions();
 334 
 335   // Helper for monitoring and management support.
 336   G1MonitoringSupport* _g1mm;
 337 
 338   // Determines PLAB size for a particular allocation purpose.
 339   size_t desired_plab_sz(GCAllocPurpose purpose);
 340 
 341   // Outside of GC pauses, the number of bytes used in all regions other
 342   // than the current allocation region.
 343   size_t _summary_bytes_used;
 344 
 345   // This is used for a quick test on whether a reference points into
 346   // the collection set or not. Basically, we have an array, with one
 347   // byte per region, and that byte denotes whether the corresponding
 348   // region is in the collection set or not. The entry corresponding
 349   // the bottom of the heap, i.e., region 0, is pointed to by


 372   void setup_surviving_young_words();
 373   void update_surviving_young_words(size_t* surv_young_words);
 374   void cleanup_surviving_young_words();
 375 
 376   // It decides whether an explicit GC should start a concurrent cycle
 377   // instead of doing a STW GC. Currently, a concurrent cycle is
 378   // explicitly started if:
 379   // (a) cause == _gc_locker and +GCLockerInvokesConcurrent, or
 380   // (b) cause == _java_lang_system_gc and +ExplicitGCInvokesConcurrent.
 381   // (c) cause == _g1_humongous_allocation
 382   bool should_do_concurrent_full_gc(GCCause::Cause cause);
 383 
 384   // Keeps track of how many "old marking cycles" (i.e., Full GCs or
 385   // concurrent cycles) we have started.
 386   volatile unsigned int _old_marking_cycles_started;
 387 
 388   // Keeps track of how many "old marking cycles" (i.e., Full GCs or
 389   // concurrent cycles) we have completed.
 390   volatile unsigned int _old_marking_cycles_completed;
 391 


 392   // This is a non-product method that is helpful for testing. It is
 393   // called at the end of a GC and artificially expands the heap by
 394   // allocating a number of dead regions. This way we can induce very
 395   // frequent marking cycles and stress the cleanup / concurrent
 396   // cleanup code more (as all the regions that will be allocated by
 397   // this method will be found dead by the marking cycle).
 398   void allocate_dummy_regions() PRODUCT_RETURN;
 399 
 400   // Clear RSets after a compaction. It also resets the GC time stamps.
 401   void clear_rsets_post_compaction();
 402 
 403   // If the HR printer is active, dump the state of the regions in the
 404   // heap after a compaction.
 405   void print_hrs_post_compaction();
 406 
 407   double verify(bool guard, const char* msg);
 408   void verify_before_gc();
 409   void verify_after_gc();
 410 
 411   void log_gc_header();


 722   // This is called at the end of either a concurrent cycle or a Full
 723   // GC to update the number of old marking cycles completed. Those two
 724   // can happen in a nested fashion, i.e., we start a concurrent
 725   // cycle, a Full GC happens half-way through it which ends first,
 726   // and then the cycle notices that a Full GC happened and ends
 727   // too. The concurrent parameter is a boolean to help us do a bit
 728   // tighter consistency checking in the method. If concurrent is
 729   // false, the caller is the inner caller in the nesting (i.e., the
 730   // Full GC). If concurrent is true, the caller is the outer caller
 731   // in this nesting (i.e., the concurrent cycle). Further nesting is
 732   // not currently supported. The end of this call also notifies
 733   // the FullGCCount_lock in case a Java thread is waiting for a full
 734   // GC to happen (e.g., it called System.gc() with
 735   // +ExplicitGCInvokesConcurrent).
 736   void increment_old_marking_cycles_completed(bool concurrent);
 737 
 738   unsigned int old_marking_cycles_completed() {
 739     return _old_marking_cycles_completed;
 740   }
 741 






 742   G1HRPrinter* hr_printer() { return &_hr_printer; }
 743 
 744 protected:
 745 
 746   // Shrink the garbage-first heap by at most the given size (in bytes!).
 747   // (Rounds down to a HeapRegion boundary.)
 748   virtual void shrink(size_t expand_bytes);
 749   void shrink_helper(size_t expand_bytes);
 750 
 751   #if TASKQUEUE_STATS
 752   static void print_taskqueue_stats_hdr(outputStream* const st = gclog_or_tty);
 753   void print_taskqueue_stats(outputStream* const st = gclog_or_tty) const;
 754   void reset_taskqueue_stats();
 755   #endif // TASKQUEUE_STATS
 756 
 757   // Schedule the VM operation that will do an evacuation pause to
 758   // satisfy an allocation request of word_size. *succeeded will
 759   // return whether the VM operation was successful (it did do an
 760   // evacuation pause) or not (another thread beat us to it or the GC
 761   // locker was active). Given that we should not be holding the
 762   // Heap_lock when we enter this method, we will pass the
 763   // gc_count_before (i.e., total_collections()) as a parameter since
 764   // it has to be read while holding the Heap_lock. Currently, both
 765   // methods that call do_collection_pause() release the Heap_lock
 766   // before the call, so it's easy to read gc_count_before just before.
 767   HeapWord* do_collection_pause(size_t       word_size,
 768                                 unsigned int gc_count_before,
 769                                 bool*        succeeded);
 770 
 771   // The guts of the incremental collection pause, executed by the vm
 772   // thread. It returns false if it is unable to do the collection due
 773   // to the GC locker being active, true otherwise
 774   bool do_collection_pause_at_safepoint(double target_pause_time_ms);
 775 
 776   // Actually do the work of evacuating the collection set.
 777   void evacuate_collection_set();
 778 
 779   // The g1 remembered set of the heap.
 780   G1RemSet* _g1_rem_set;
 781   // And it's mod ref barrier set, used to track updates for the above.
 782   ModRefBarrierSet* _mr_bs;
 783 
 784   // A set of cards that cover the objects for which the Rsets should be updated
 785   // concurrently after the collection.
 786   DirtyCardQueueSet _dirty_card_queue_set;
 787 
 788   // The closure used to refine a single card.
 789   RefineCardTableEntryClosure* _refine_cte_cl;
 790 
 791   // A function to check the consistency of dirty card logs.
 792   void check_ct_logs_at_safepoint();
 793 
 794   // A DirtyCardQueueSet that is used to hold cards that contain
 795   // references into the current collection set. This is used to
 796   // update the remembered sets of the regions in the collection
 797   // set in the event of an evacuation failure.
 798   DirtyCardQueueSet _into_cset_dirty_card_queue_set;
 799 
 800   // After a collection pause, make the regions in the CS into free
 801   // regions.
 802   void free_collection_set(HeapRegion* cs_head);
 803 
 804   // Abandon the current collection set without recording policy
 805   // statistics or updating free lists.
 806   void abandon_collection_set(HeapRegion* cs_head);
 807 
 808   // Applies "scan_non_heap_roots" to roots outside the heap,
 809   // "scan_rs" to roots inside the heap (having done "set_region" to
 810   // indicate the region in which the root resides),
 811   // and does "scan_metadata" If "scan_rs" is
 812   // NULL, then this step is skipped.  The "worker_i"
 813   // param is for use with parallel roots processing, and should be
 814   // the "i" of the calling parallel worker thread's work(i) function.
 815   // In the sequential case this param will be ignored.
 816   void g1_process_strong_roots(bool is_scavenging,
 817                                ScanningOption so,
 818                                OopClosure* scan_non_heap_roots,
 819                                OopsInHeapRegionClosure* scan_rs,
 820                                G1KlassScanClosure* scan_klasses,
 821                                int worker_i);
 822 


 851 
 852   // Notifies all the necessary spaces that the committed space has
 853   // been updated (either expanded or shrunk). It should be called
 854   // after _g1_storage is updated.
 855   void update_committed_space(HeapWord* old_end, HeapWord* new_end);
 856 
 857   // The concurrent marker (and the thread it runs in.)
 858   ConcurrentMark* _cm;
 859   ConcurrentMarkThread* _cmThread;
 860   bool _mark_in_progress;
 861 
 862   // The concurrent refiner.
 863   ConcurrentG1Refine* _cg1r;
 864 
 865   // The parallel task queues
 866   RefToScanQueueSet *_task_queues;
 867 
 868   // True iff a evacuation has failed in the current collection.
 869   bool _evacuation_failed;
 870 
 871   // Set the attribute indicating whether evacuation has failed in the
 872   // current collection.
 873   void set_evacuation_failed(bool b) { _evacuation_failed = b; }
 874 
 875   // Failed evacuations cause some logical from-space objects to have
 876   // forwarding pointers to themselves.  Reset them.
 877   void remove_self_forwarding_pointers();
 878 
 879   // Together, these store an object with a preserved mark, and its mark value.
 880   Stack<oop, mtGC>     _objs_with_preserved_marks;
 881   Stack<markOop, mtGC> _preserved_marks_of_objs;
 882 
 883   // Preserve the mark of "obj", if necessary, in preparation for its mark
 884   // word being overwritten with a self-forwarding-pointer.
 885   void preserve_mark_if_necessary(oop obj, markOop m);
 886 
 887   // The stack of evac-failure objects left to be scanned.
 888   GrowableArray<oop>*    _evac_failure_scan_stack;
 889   // The closure to apply to evac-failure objects.
 890 
 891   OopsInHeapRegionClosure* _evac_failure_closure;
 892   // Set the field above.
 893   void


 895     _evac_failure_closure = evac_failure_closure;
 896   }
 897 
 898   // Push "obj" on the scan stack.
 899   void push_on_evac_failure_scan_stack(oop obj);
 900   // Process scan stack entries until the stack is empty.
 901   void drain_evac_failure_scan_stack();
 902   // True iff an invocation of "drain_scan_stack" is in progress; to
 903   // prevent unnecessary recursion.
 904   bool _drain_in_progress;
 905 
 906   // Do any necessary initialization for evacuation-failure handling.
 907   // "cl" is the closure that will be used to process evac-failure
 908   // objects.
 909   void init_for_evac_failure(OopsInHeapRegionClosure* cl);
 910   // Do any necessary cleanup for evacuation-failure handling data
 911   // structures.
 912   void finalize_for_evac_failure();
 913 
 914   // An attempt to evacuate "obj" has failed; take necessary steps.
 915   oop handle_evacuation_failure_par(OopsInHeapRegionClosure* cl, oop obj);
 916   void handle_evacuation_failure_common(oop obj, markOop m);
 917 
 918 #ifndef PRODUCT
 919   // Support for forcing evacuation failures. Analogous to
 920   // PromotionFailureALot for the other collectors.
 921 
 922   // Records whether G1EvacuationFailureALot should be in effect
 923   // for the current GC
 924   bool _evacuation_failure_alot_for_current_gc;
 925 
 926   // Used to record the GC number for interval checking when
 927   // determining whether G1EvaucationFailureALot is in effect
 928   // for the current GC.
 929   size_t _evacuation_failure_alot_gc_number;
 930 
 931   // Count of the number of evacuations between failures.
 932   volatile size_t _evacuation_failure_alot_count;
 933 
 934   // Set whether G1EvacuationFailureALot should be in effect
 935   // for the current GC (based upon the type of GC and which
 936   // command line flags are set);
 937   inline bool evacuation_failure_alot_for_gc_type(bool gcs_are_young,
 938                                                   bool during_initial_mark,
 939                                                   bool during_marking);
 940 
 941   inline void set_evacuation_failure_alot_for_current_gc();
 942 
 943   // Return true if it's time to cause an evacuation failure.
 944   inline bool evacuation_should_fail();
 945 
 946   // Reset the G1EvacuationFailureALot counters.  Should be called at
 947   // the end of an evacuation pause in which an evacuation failure ocurred.
 948   inline void reset_evacuation_should_fail();
 949 #endif // !PRODUCT
 950 
 951   // ("Weak") Reference processing support.
 952   //
 953   // G1 has 2 instances of the referece processor class. One
 954   // (_ref_processor_cm) handles reference object discovery
 955   // and subsequent processing during concurrent marking cycles.
 956   //
 957   // The other (_ref_processor_stw) handles reference object
 958   // discovery and processing during full GCs and incremental
 959   // evacuation pauses.
 960   //
 961   // During an incremental pause, reference discovery will be
 962   // temporarily disabled for _ref_processor_cm and will be
 963   // enabled for _ref_processor_stw. At the end of the evacuation
 964   // pause references discovered by _ref_processor_stw will be
 965   // processed and discovery will be disabled. The previous
 966   // setting for reference object discovery for _ref_processor_cm
 967   // will be re-instated.
 968   //
 969   // At the start of marking:
 970   //  * Discovery by the CM ref processor is verified to be inactive
 971   //    and it's discovered lists are empty.
 972   //  * Discovery by the CM ref processor is then enabled.
 973   //


 983   //    discovered lists are empty.
 984   //  * Temporarily set STW ref processor discovery as single threaded.
 985   //  * Temporarily clear the STW ref processor's _is_alive_non_header
 986   //    field.
 987   //  * Finally enable discovery by the STW ref processor.
 988   //
 989   // The STW ref processor is used to record any discovered
 990   // references during the full GC.
 991   //
 992   // At the end of a full GC we:
 993   //  * Enqueue any reference objects discovered by the STW ref processor
 994   //    that have non-live referents. This has the side-effect of
 995   //    making the STW ref processor inactive by disabling discovery.
 996   //  * Verify that the CM ref processor is still inactive
 997   //    and no references have been placed on it's discovered
 998   //    lists (also checked as a precondition during initial marking).
 999 
1000   // The (stw) reference processor...
1001   ReferenceProcessor* _ref_processor_stw;
1002 






1003   // During reference object discovery, the _is_alive_non_header
1004   // closure (if non-null) is applied to the referent object to
1005   // determine whether the referent is live. If so then the
1006   // reference object does not need to be 'discovered' and can
1007   // be treated as a regular oop. This has the benefit of reducing
1008   // the number of 'discovered' reference objects that need to
1009   // be processed.
1010   //
1011   // Instance of the is_alive closure for embedding into the
1012   // STW reference processor as the _is_alive_non_header field.
1013   // Supplying a value for the _is_alive_non_header field is
1014   // optional but doing so prevents unnecessary additions to
1015   // the discovered lists during reference discovery.
1016   G1STWIsAliveClosure _is_alive_closure_stw;
1017 
1018   // The (concurrent marking) reference processor...
1019   ReferenceProcessor* _ref_processor_cm;
1020 
1021   // Instance of the concurrent mark is_alive closure for embedding
1022   // into the Concurrent Marking reference processor as the


1128     OrderAccess::fence();
1129   }
1130 
1131   // Reset the given region's GC timestamp. If it's starts humongous,
1132   // also reset the GC timestamp of its corresponding
1133   // continues humongous regions too.
1134   void reset_gc_time_stamps(HeapRegion* hr);
1135 
1136   void iterate_dirty_card_closure(CardTableEntryClosure* cl,
1137                                   DirtyCardQueue* into_cset_dcq,
1138                                   bool concurrent, int worker_i);
1139 
1140   // The shared block offset table array.
1141   G1BlockOffsetSharedArray* bot_shared() const { return _bot_shared; }
1142 
1143   // Reference Processing accessors
1144 
1145   // The STW reference processor....
1146   ReferenceProcessor* ref_processor_stw() const { return _ref_processor_stw; }
1147 
1148   // The Concurent Marking reference processor...
1149   ReferenceProcessor* ref_processor_cm() const { return _ref_processor_cm; }
1150 



1151   virtual size_t capacity() const;
1152   virtual size_t used() const;
1153   // This should be called when we're not holding the heap lock. The
1154   // result might be a bit inaccurate.
1155   size_t used_unlocked() const;
1156   size_t recalculate_used() const;
1157 
1158   // These virtual functions do the actual allocation.
1159   // Some heaps may offer a contiguous region for shared non-blocking
1160   // allocation, via inlined code (by exporting the address of the top and
1161   // end fields defining the extent of the contiguous allocation region.)
1162   // But G1CollectedHeap doesn't yet support this.
1163 
1164   // Return an estimate of the maximum allocation that could be performed
1165   // without triggering any collection or expansion activity.  In a
1166   // generational collector, for example, this is probably the largest
1167   // allocation that could be supported (without expansion) in the youngest
1168   // generation.  It is "unsafe" because no locks are taken; the result
1169   // should be treated as an approximation, not a guarantee, for use in
1170   // heuristic resizing decisions.


1188 
1189   // The number of regions available for "regular" expansion.
1190   uint expansion_regions() { return _expansion_regions; }
1191 
1192   // Factory method for HeapRegion instances. It will return NULL if
1193   // the allocation fails.
1194   HeapRegion* new_heap_region(uint hrs_index, HeapWord* bottom);
1195 
1196   void verify_not_dirty_region(HeapRegion* hr) PRODUCT_RETURN;
1197   void verify_dirty_region(HeapRegion* hr) PRODUCT_RETURN;
1198   void verify_dirty_young_list(HeapRegion* head) PRODUCT_RETURN;
1199   void verify_dirty_young_regions() PRODUCT_RETURN;
1200 
1201   // verify_region_sets() performs verification over the region
1202   // lists. It will be compiled in the product code to be used when
1203   // necessary (i.e., during heap verification).
1204   void verify_region_sets();
1205 
1206   // verify_region_sets_optional() is planted in the code for
1207   // list verification in non-product builds (and it can be enabled in
1208   // product builds by definning HEAP_REGION_SET_FORCE_VERIFY to be 1).
1209 #if HEAP_REGION_SET_FORCE_VERIFY
1210   void verify_region_sets_optional() {
1211     verify_region_sets();
1212   }
1213 #else // HEAP_REGION_SET_FORCE_VERIFY
1214   void verify_region_sets_optional() { }
1215 #endif // HEAP_REGION_SET_FORCE_VERIFY
1216 
1217 #ifdef ASSERT
1218   bool is_on_master_free_list(HeapRegion* hr) {
1219     return hr->containing_set() == &_free_list;
1220   }
1221 
1222   bool is_in_humongous_set(HeapRegion* hr) {
1223     return hr->containing_set() == &_humongous_set;
1224   }
1225 #endif // ASSERT
1226 
1227   // Wrapper for the region list operations that can be called from
1228   // methods outside this class.


1254 
1255   void set_free_regions_coming();
1256   void reset_free_regions_coming();
1257   bool free_regions_coming() { return _free_regions_coming; }
1258   void wait_while_free_regions_coming();
1259 
1260   // Determine whether the given region is one that we are using as an
1261   // old GC alloc region.
1262   bool is_old_gc_alloc_region(HeapRegion* hr) {
1263     return hr == _retained_old_gc_alloc_region;
1264   }
1265 
1266   // Perform a collection of the heap; intended for use in implementing
1267   // "System.gc".  This probably implies as full a collection as the
1268   // "CollectedHeap" supports.
1269   virtual void collect(GCCause::Cause cause);
1270 
1271   // The same as above but assume that the caller holds the Heap_lock.
1272   void collect_locked(GCCause::Cause cause);
1273 
1274   // True iff a evacuation has failed in the most-recent collection.
1275   bool evacuation_failed() { return _evacuation_failed; }
1276 
1277   // It will free a region if it has allocated objects in it that are
1278   // all dead. It calls either free_region() or
1279   // free_humongous_region() depending on the type of the region that
1280   // is passed to it.
1281   void free_region_if_empty(HeapRegion* hr,
1282                             size_t* pre_used,
1283                             FreeRegionList* free_list,
1284                             OldRegionSet* old_proxy_set,
1285                             HumongousRegionSet* humongous_proxy_set,
1286                             HRRSCleanupTask* hrrs_cleanup_task,
1287                             bool par);
1288 
1289   // It appends the free list to the master free list and updates the
1290   // master humongous list according to the contents of the proxy
1291   // list. It also adjusts the total used bytes according to pre_used
1292   // (if par is true, it will do so by taking the ParGCRareEvent_lock).
1293   void update_sets_after_freeing_regions(size_t pre_used,
1294                                        FreeRegionList* free_list,


1542 
1543   // Perform verification.
1544 
1545   // vo == UsePrevMarking  -> use "prev" marking information,
1546   // vo == UseNextMarking -> use "next" marking information
1547   // vo == UseMarkWord    -> use the mark word in the object header
1548   //
1549   // NOTE: Only the "prev" marking information is guaranteed to be
1550   // consistent most of the time, so most calls to this should use
1551   // vo == UsePrevMarking.
1552   // Currently, there is only one case where this is called with
1553   // vo == UseNextMarking, which is to verify the "next" marking
1554   // information at the end of remark.
1555   // Currently there is only one place where this is called with
1556   // vo == UseMarkWord, which is to verify the marking during a
1557   // full GC.
1558   void verify(bool silent, VerifyOption vo);
1559 
1560   // Override; it uses the "prev" marking information
1561   virtual void verify(bool silent);

1562   virtual void print_on(outputStream* st) const;
1563   virtual void print_extended_on(outputStream* st) const;
1564   virtual void print_on_error(outputStream* st) const;
1565 
1566   virtual void print_gc_threads_on(outputStream* st) const;
1567   virtual void gc_threads_do(ThreadClosure* tc) const;
1568 
1569   // Override
1570   void print_tracing_info() const;
1571 
1572   // The following two methods are helpful for debugging RSet issues.
1573   void print_cset_rsets() PRODUCT_RETURN;
1574   void print_all_rsets() PRODUCT_RETURN;
1575 
1576   // Convenience function to be used in situations where the heap type can be
1577   // asserted to be this type.
1578   static G1CollectedHeap* heap();
1579 
1580   void set_region_short_lived_locked(HeapRegion* hr);
1581   // add appropriate methods for any other surv rate groups


1942       _partial_scan_cl->do_oop_nv(ref_to_scan);
1943     } else {
1944       // Note: we can use "raw" versions of "region_containing" because
1945       // "obj_to_scan" is definitely in the heap, and is not in a
1946       // humongous region.
1947       HeapRegion* r = _g1h->heap_region_containing_raw(ref_to_scan);
1948       _evac_cl->set_region(r);
1949       _evac_cl->do_oop_nv(ref_to_scan);
1950     }
1951   }
1952 
1953   void deal_with_reference(StarTask ref) {
1954     assert(verify_task(ref), "sanity");
1955     if (ref.is_narrow()) {
1956       deal_with_reference((narrowOop*)ref);
1957     } else {
1958       deal_with_reference((oop*)ref);
1959     }
1960   }
1961 
1962 public:
1963   void trim_queue();
1964 };
1965 
1966 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1COLLECTEDHEAP_HPP
   1 /*
   2  * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1COLLECTEDHEAP_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1COLLECTEDHEAP_HPP
  27 
  28 #include "gc_implementation/g1/concurrentMark.hpp"
  29 #include "gc_implementation/g1/evacuationInfo.hpp"
  30 #include "gc_implementation/g1/g1AllocRegion.hpp"
  31 #include "gc_implementation/g1/g1HRPrinter.hpp"

  32 #include "gc_implementation/g1/g1MonitoringSupport.hpp"
  33 #include "gc_implementation/g1/g1RemSet.hpp"
  34 #include "gc_implementation/g1/g1YCTypes.hpp"
  35 #include "gc_implementation/g1/heapRegionSeq.hpp"
  36 #include "gc_implementation/g1/heapRegionSets.hpp"
  37 #include "gc_implementation/shared/hSpaceCounters.hpp"
  38 #include "gc_implementation/shared/parGCAllocBuffer.hpp"
  39 #include "memory/barrierSet.hpp"
  40 #include "memory/memRegion.hpp"
  41 #include "memory/sharedHeap.hpp"
  42 #include "utilities/stack.hpp"
  43 
  44 // A "G1CollectedHeap" is an implementation of a java heap for HotSpot.
  45 // It uses the "Garbage First" heap organization and algorithm, which
  46 // may combine concurrent marking with parallel, incremental compaction of
  47 // heap subsets that will yield large amounts of garbage.
  48 
  49 class HeapRegion;
  50 class HRRSCleanupTask;
  51 class GenerationSpec;
  52 class OopsInHeapRegionClosure;
  53 class G1KlassScanClosure;
  54 class G1ScanHeapEvacClosure;
  55 class ObjectClosure;
  56 class SpaceClosure;
  57 class CompactibleSpaceClosure;
  58 class Space;
  59 class G1CollectorPolicy;
  60 class GenRemSet;
  61 class G1RemSet;
  62 class HeapRegionRemSetIterator;
  63 class ConcurrentMark;
  64 class ConcurrentMarkThread;
  65 class ConcurrentG1Refine;
  66 class ConcurrentGCTimer;
  67 class GenerationCounters;
  68 class STWGCTimer;
  69 class G1NewTracer;
  70 class G1OldTracer;
  71 class EvacuationFailedInfo;
  72 
  73 typedef OverflowTaskQueue<StarTask, mtGC>         RefToScanQueue;
  74 typedef GenericTaskQueueSet<RefToScanQueue, mtGC> RefToScanQueueSet;
  75 
  76 typedef int RegionIdx_t;   // needs to hold [ 0..max_regions() )
  77 typedef int CardIdx_t;     // needs to hold [ 0..CardsPerRegion )
  78 
  79 enum GCAllocPurpose {
  80   GCAllocForTenured,
  81   GCAllocForSurvived,
  82   GCAllocPurposeCount
  83 };
  84 
  85 class YoungList : public CHeapObj<mtGC> {
  86 private:
  87   G1CollectedHeap* _g1h;
  88 
  89   HeapRegion* _head;
  90 
  91   HeapRegion* _survivor_head;


 150 
 151   // debugging
 152   bool          check_list_well_formed();
 153   bool          check_list_empty(bool check_sample = true);
 154   void          print();
 155 };
 156 
 157 class MutatorAllocRegion : public G1AllocRegion {
 158 protected:
 159   virtual HeapRegion* allocate_new_region(size_t word_size, bool force);
 160   virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);
 161 public:
 162   MutatorAllocRegion()
 163     : G1AllocRegion("Mutator Alloc Region", false /* bot_updates */) { }
 164 };
 165 
 166 // The G1 STW is alive closure.
 167 // An instance is embedded into the G1CH and used as the
 168 // (optional) _is_alive_non_header closure in the STW
 169 // reference processor. It is also extensively used during
 170 // reference processing during STW evacuation pauses.
 171 class G1STWIsAliveClosure: public BoolObjectClosure {
 172   G1CollectedHeap* _g1;
 173 public:
 174   G1STWIsAliveClosure(G1CollectedHeap* g1) : _g1(g1) {}
 175   bool do_object_b(oop p);
 176 };
 177 
 178 class SurvivorGCAllocRegion : public G1AllocRegion {
 179 protected:
 180   virtual HeapRegion* allocate_new_region(size_t word_size, bool force);
 181   virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);
 182 public:
 183   SurvivorGCAllocRegion()
 184   : G1AllocRegion("Survivor GC Alloc Region", false /* bot_updates */) { }
 185 };
 186 
 187 class OldGCAllocRegion : public G1AllocRegion {
 188 protected:
 189   virtual HeapRegion* allocate_new_region(size_t word_size, bool force);
 190   virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);


 313 
 314   // The last old region we allocated to during the last GC.
 315   // Typically, it is not full so we should re-use it during the next GC.
 316   HeapRegion* _retained_old_gc_alloc_region;
 317 
 318   // It specifies whether we should attempt to expand the heap after a
 319   // region allocation failure. If heap expansion fails we set this to
 320   // false so that we don't re-attempt the heap expansion (it's likely
 321   // that subsequent expansion attempts will also fail if one fails).
 322   // Currently, it is only consulted during GC and it's reset at the
 323   // start of each GC.
 324   bool _expand_heap_after_alloc_failure;
 325 
 326   // It resets the mutator alloc region before new allocations can take place.
 327   void init_mutator_alloc_region();
 328 
 329   // It releases the mutator alloc region.
 330   void release_mutator_alloc_region();
 331 
 332   // It initializes the GC alloc regions at the start of a GC.
 333   void init_gc_alloc_regions(EvacuationInfo& evacuation_info);
 334 
 335   // It releases the GC alloc regions at the end of a GC.
 336   void release_gc_alloc_regions(uint no_of_gc_workers, EvacuationInfo& evacuation_info);
 337 
 338   // It does any cleanup that needs to be done on the GC alloc regions
 339   // before a Full GC.
 340   void abandon_gc_alloc_regions();
 341 
 342   // Helper for monitoring and management support.
 343   G1MonitoringSupport* _g1mm;
 344 
 345   // Determines PLAB size for a particular allocation purpose.
 346   size_t desired_plab_sz(GCAllocPurpose purpose);
 347 
 348   // Outside of GC pauses, the number of bytes used in all regions other
 349   // than the current allocation region.
 350   size_t _summary_bytes_used;
 351 
 352   // This is used for a quick test on whether a reference points into
 353   // the collection set or not. Basically, we have an array, with one
 354   // byte per region, and that byte denotes whether the corresponding
 355   // region is in the collection set or not. The entry corresponding
 356   // the bottom of the heap, i.e., region 0, is pointed to by


 379   void setup_surviving_young_words();
 380   void update_surviving_young_words(size_t* surv_young_words);
 381   void cleanup_surviving_young_words();
 382 
 383   // It decides whether an explicit GC should start a concurrent cycle
 384   // instead of doing a STW GC. Currently, a concurrent cycle is
 385   // explicitly started if:
 386   // (a) cause == _gc_locker and +GCLockerInvokesConcurrent, or
 387   // (b) cause == _java_lang_system_gc and +ExplicitGCInvokesConcurrent.
 388   // (c) cause == _g1_humongous_allocation
 389   bool should_do_concurrent_full_gc(GCCause::Cause cause);
 390 
 391   // Keeps track of how many "old marking cycles" (i.e., Full GCs or
 392   // concurrent cycles) we have started.
 393   volatile unsigned int _old_marking_cycles_started;
 394 
 395   // Keeps track of how many "old marking cycles" (i.e., Full GCs or
 396   // concurrent cycles) we have completed.
 397   volatile unsigned int _old_marking_cycles_completed;
 398 
 399   bool _concurrent_cycle_started;
 400 
 401   // This is a non-product method that is helpful for testing. It is
 402   // called at the end of a GC and artificially expands the heap by
 403   // allocating a number of dead regions. This way we can induce very
 404   // frequent marking cycles and stress the cleanup / concurrent
 405   // cleanup code more (as all the regions that will be allocated by
 406   // this method will be found dead by the marking cycle).
 407   void allocate_dummy_regions() PRODUCT_RETURN;
 408 
 409   // Clear RSets after a compaction. It also resets the GC time stamps.
 410   void clear_rsets_post_compaction();
 411 
 412   // If the HR printer is active, dump the state of the regions in the
 413   // heap after a compaction.
 414   void print_hrs_post_compaction();
 415 
 416   double verify(bool guard, const char* msg);
 417   void verify_before_gc();
 418   void verify_after_gc();
 419 
 420   void log_gc_header();


 731   // This is called at the end of either a concurrent cycle or a Full
 732   // GC to update the number of old marking cycles completed. Those two
 733   // can happen in a nested fashion, i.e., we start a concurrent
 734   // cycle, a Full GC happens half-way through it which ends first,
 735   // and then the cycle notices that a Full GC happened and ends
 736   // too. The concurrent parameter is a boolean to help us do a bit
 737   // tighter consistency checking in the method. If concurrent is
 738   // false, the caller is the inner caller in the nesting (i.e., the
 739   // Full GC). If concurrent is true, the caller is the outer caller
 740   // in this nesting (i.e., the concurrent cycle). Further nesting is
 741   // not currently supported. The end of this call also notifies
 742   // the FullGCCount_lock in case a Java thread is waiting for a full
 743   // GC to happen (e.g., it called System.gc() with
 744   // +ExplicitGCInvokesConcurrent).
 745   void increment_old_marking_cycles_completed(bool concurrent);
 746 
 747   unsigned int old_marking_cycles_completed() {
 748     return _old_marking_cycles_completed;
 749   }
 750 
 751   void register_concurrent_cycle_start(jlong start_time);
 752   void register_concurrent_cycle_end();
 753   void trace_heap_after_concurrent_cycle();
 754 
 755   G1YCType yc_type();
 756 
 757   G1HRPrinter* hr_printer() { return &_hr_printer; }
 758 
 759 protected:
 760 
 761   // Shrink the garbage-first heap by at most the given size (in bytes!).
 762   // (Rounds down to a HeapRegion boundary.)
 763   virtual void shrink(size_t expand_bytes);
 764   void shrink_helper(size_t expand_bytes);
 765 
 766   #if TASKQUEUE_STATS
 767   static void print_taskqueue_stats_hdr(outputStream* const st = gclog_or_tty);
 768   void print_taskqueue_stats(outputStream* const st = gclog_or_tty) const;
 769   void reset_taskqueue_stats();
 770   #endif // TASKQUEUE_STATS
 771 
 772   // Schedule the VM operation that will do an evacuation pause to
 773   // satisfy an allocation request of word_size. *succeeded will
 774   // return whether the VM operation was successful (it did do an
 775   // evacuation pause) or not (another thread beat us to it or the GC
 776   // locker was active). Given that we should not be holding the
 777   // Heap_lock when we enter this method, we will pass the
 778   // gc_count_before (i.e., total_collections()) as a parameter since
 779   // it has to be read while holding the Heap_lock. Currently, both
 780   // methods that call do_collection_pause() release the Heap_lock
 781   // before the call, so it's easy to read gc_count_before just before.
 782   HeapWord* do_collection_pause(size_t       word_size,
 783                                 unsigned int gc_count_before,
 784                                 bool*        succeeded);
 785 
 786   // The guts of the incremental collection pause, executed by the vm
 787   // thread. It returns false if it is unable to do the collection due
 788   // to the GC locker being active, true otherwise
 789   bool do_collection_pause_at_safepoint(double target_pause_time_ms);
 790 
 791   // Actually do the work of evacuating the collection set.
 792   void evacuate_collection_set(EvacuationInfo& evacuation_info);
 793 
 794   // The g1 remembered set of the heap.
 795   G1RemSet* _g1_rem_set;
 796   // And it's mod ref barrier set, used to track updates for the above.
 797   ModRefBarrierSet* _mr_bs;
 798 
 799   // A set of cards that cover the objects for which the Rsets should be updated
 800   // concurrently after the collection.
 801   DirtyCardQueueSet _dirty_card_queue_set;
 802 
 803   // The closure used to refine a single card.
 804   RefineCardTableEntryClosure* _refine_cte_cl;
 805 
 806   // A function to check the consistency of dirty card logs.
 807   void check_ct_logs_at_safepoint();
 808 
 809   // A DirtyCardQueueSet that is used to hold cards that contain
 810   // references into the current collection set. This is used to
 811   // update the remembered sets of the regions in the collection
 812   // set in the event of an evacuation failure.
 813   DirtyCardQueueSet _into_cset_dirty_card_queue_set;
 814 
 815   // After a collection pause, make the regions in the CS into free
 816   // regions.
 817   void free_collection_set(HeapRegion* cs_head, EvacuationInfo& evacuation_info);
 818 
 819   // Abandon the current collection set without recording policy
 820   // statistics or updating free lists.
 821   void abandon_collection_set(HeapRegion* cs_head);
 822 
 823   // Applies "scan_non_heap_roots" to roots outside the heap,
 824   // "scan_rs" to roots inside the heap (having done "set_region" to
 825   // indicate the region in which the root resides),
 826   // and does "scan_metadata" If "scan_rs" is
 827   // NULL, then this step is skipped.  The "worker_i"
 828   // param is for use with parallel roots processing, and should be
 829   // the "i" of the calling parallel worker thread's work(i) function.
 830   // In the sequential case this param will be ignored.
 831   void g1_process_strong_roots(bool is_scavenging,
 832                                ScanningOption so,
 833                                OopClosure* scan_non_heap_roots,
 834                                OopsInHeapRegionClosure* scan_rs,
 835                                G1KlassScanClosure* scan_klasses,
 836                                int worker_i);
 837 


 866 
 867   // Notifies all the necessary spaces that the committed space has
 868   // been updated (either expanded or shrunk). It should be called
 869   // after _g1_storage is updated.
 870   void update_committed_space(HeapWord* old_end, HeapWord* new_end);
 871 
 872   // The concurrent marker (and the thread it runs in.)
 873   ConcurrentMark* _cm;
 874   ConcurrentMarkThread* _cmThread;
 875   bool _mark_in_progress;
 876 
 877   // The concurrent refiner.
 878   ConcurrentG1Refine* _cg1r;
 879 
 880   // The parallel task queues
 881   RefToScanQueueSet *_task_queues;
 882 
 883   // True iff a evacuation has failed in the current collection.
 884   bool _evacuation_failed;
 885 
 886   EvacuationFailedInfo* _evacuation_failed_info_array;


 887 
 888   // Failed evacuations cause some logical from-space objects to have
 889   // forwarding pointers to themselves.  Reset them.
 890   void remove_self_forwarding_pointers();
 891 
 892   // Together, these store an object with a preserved mark, and its mark value.
 893   Stack<oop, mtGC>     _objs_with_preserved_marks;
 894   Stack<markOop, mtGC> _preserved_marks_of_objs;
 895 
 896   // Preserve the mark of "obj", if necessary, in preparation for its mark
 897   // word being overwritten with a self-forwarding-pointer.
 898   void preserve_mark_if_necessary(oop obj, markOop m);
 899 
 900   // The stack of evac-failure objects left to be scanned.
 901   GrowableArray<oop>*    _evac_failure_scan_stack;
 902   // The closure to apply to evac-failure objects.
 903 
 904   OopsInHeapRegionClosure* _evac_failure_closure;
 905   // Set the field above.
 906   void


 908     _evac_failure_closure = evac_failure_closure;
 909   }
 910 
 911   // Push "obj" on the scan stack.
 912   void push_on_evac_failure_scan_stack(oop obj);
 913   // Process scan stack entries until the stack is empty.
 914   void drain_evac_failure_scan_stack();
 915   // True iff an invocation of "drain_scan_stack" is in progress; to
 916   // prevent unnecessary recursion.
 917   bool _drain_in_progress;
 918 
 919   // Do any necessary initialization for evacuation-failure handling.
 920   // "cl" is the closure that will be used to process evac-failure
 921   // objects.
 922   void init_for_evac_failure(OopsInHeapRegionClosure* cl);
 923   // Do any necessary cleanup for evacuation-failure handling data
 924   // structures.
 925   void finalize_for_evac_failure();
 926 
 927   // An attempt to evacuate "obj" has failed; take necessary steps.
 928   oop handle_evacuation_failure_par(G1ParScanThreadState* _par_scan_state, oop obj);
 929   void handle_evacuation_failure_common(oop obj, markOop m);
 930 
 931 #ifndef PRODUCT
 932   // Support for forcing evacuation failures. Analogous to
 933   // PromotionFailureALot for the other collectors.
 934 
 935   // Records whether G1EvacuationFailureALot should be in effect
 936   // for the current GC
 937   bool _evacuation_failure_alot_for_current_gc;
 938 
 939   // Used to record the GC number for interval checking when
 940   // determining whether G1EvaucationFailureALot is in effect
 941   // for the current GC.
 942   size_t _evacuation_failure_alot_gc_number;
 943 
 944   // Count of the number of evacuations between failures.
 945   volatile size_t _evacuation_failure_alot_count;
 946 
 947   // Set whether G1EvacuationFailureALot should be in effect
 948   // for the current GC (based upon the type of GC and which
 949   // command line flags are set);
 950   inline bool evacuation_failure_alot_for_gc_type(bool gcs_are_young,
 951                                                   bool during_initial_mark,
 952                                                   bool during_marking);
 953 
 954   inline void set_evacuation_failure_alot_for_current_gc();
 955 
 956   // Return true if it's time to cause an evacuation failure.
 957   inline bool evacuation_should_fail();
 958 
 959   // Reset the G1EvacuationFailureALot counters.  Should be called at
 960   // the end of an evacuation pause in which an evacuation failure occurred.
 961   inline void reset_evacuation_should_fail();
 962 #endif // !PRODUCT
 963 
 964   // ("Weak") Reference processing support.
 965   //
 966   // G1 has 2 instances of the reference processor class. One
 967   // (_ref_processor_cm) handles reference object discovery
 968   // and subsequent processing during concurrent marking cycles.
 969   //
 970   // The other (_ref_processor_stw) handles reference object
 971   // discovery and processing during full GCs and incremental
 972   // evacuation pauses.
 973   //
 974   // During an incremental pause, reference discovery will be
 975   // temporarily disabled for _ref_processor_cm and will be
 976   // enabled for _ref_processor_stw. At the end of the evacuation
 977   // pause references discovered by _ref_processor_stw will be
 978   // processed and discovery will be disabled. The previous
 979   // setting for reference object discovery for _ref_processor_cm
 980   // will be re-instated.
 981   //
 982   // At the start of marking:
 983   //  * Discovery by the CM ref processor is verified to be inactive
 984   //    and it's discovered lists are empty.
 985   //  * Discovery by the CM ref processor is then enabled.
 986   //


 996   //    discovered lists are empty.
 997   //  * Temporarily set STW ref processor discovery as single threaded.
 998   //  * Temporarily clear the STW ref processor's _is_alive_non_header
 999   //    field.
1000   //  * Finally enable discovery by the STW ref processor.
1001   //
1002   // The STW ref processor is used to record any discovered
1003   // references during the full GC.
1004   //
1005   // At the end of a full GC we:
1006   //  * Enqueue any reference objects discovered by the STW ref processor
1007   //    that have non-live referents. This has the side-effect of
1008   //    making the STW ref processor inactive by disabling discovery.
1009   //  * Verify that the CM ref processor is still inactive
1010   //    and no references have been placed on it's discovered
1011   //    lists (also checked as a precondition during initial marking).
1012 
1013   // The (stw) reference processor...
1014   ReferenceProcessor* _ref_processor_stw;
1015 
1016   STWGCTimer* _gc_timer_stw;
1017   ConcurrentGCTimer* _gc_timer_cm;
1018 
1019   G1OldTracer* _gc_tracer_cm;
1020   G1NewTracer* _gc_tracer_stw;
1021 
1022   // During reference object discovery, the _is_alive_non_header
1023   // closure (if non-null) is applied to the referent object to
1024   // determine whether the referent is live. If so then the
1025   // reference object does not need to be 'discovered' and can
1026   // be treated as a regular oop. This has the benefit of reducing
1027   // the number of 'discovered' reference objects that need to
1028   // be processed.
1029   //
1030   // Instance of the is_alive closure for embedding into the
1031   // STW reference processor as the _is_alive_non_header field.
1032   // Supplying a value for the _is_alive_non_header field is
1033   // optional but doing so prevents unnecessary additions to
1034   // the discovered lists during reference discovery.
1035   G1STWIsAliveClosure _is_alive_closure_stw;
1036 
1037   // The (concurrent marking) reference processor...
1038   ReferenceProcessor* _ref_processor_cm;
1039 
1040   // Instance of the concurrent mark is_alive closure for embedding
1041   // into the Concurrent Marking reference processor as the


1147     OrderAccess::fence();
1148   }
1149 
1150   // Reset the given region's GC timestamp. If it's starts humongous,
1151   // also reset the GC timestamp of its corresponding
1152   // continues humongous regions too.
1153   void reset_gc_time_stamps(HeapRegion* hr);
1154 
1155   void iterate_dirty_card_closure(CardTableEntryClosure* cl,
1156                                   DirtyCardQueue* into_cset_dcq,
1157                                   bool concurrent, int worker_i);
1158 
1159   // The shared block offset table array.
1160   G1BlockOffsetSharedArray* bot_shared() const { return _bot_shared; }
1161 
1162   // Reference Processing accessors
1163 
1164   // The STW reference processor....
1165   ReferenceProcessor* ref_processor_stw() const { return _ref_processor_stw; }
1166 
1167   // The Concurrent Marking reference processor...
1168   ReferenceProcessor* ref_processor_cm() const { return _ref_processor_cm; }
1169 
1170   ConcurrentGCTimer* gc_timer_cm() const { return _gc_timer_cm; }
1171   G1OldTracer* gc_tracer_cm() const { return _gc_tracer_cm; }
1172 
1173   virtual size_t capacity() const;
1174   virtual size_t used() const;
1175   // This should be called when we're not holding the heap lock. The
1176   // result might be a bit inaccurate.
1177   size_t used_unlocked() const;
1178   size_t recalculate_used() const;
1179 
1180   // These virtual functions do the actual allocation.
1181   // Some heaps may offer a contiguous region for shared non-blocking
1182   // allocation, via inlined code (by exporting the address of the top and
1183   // end fields defining the extent of the contiguous allocation region.)
1184   // But G1CollectedHeap doesn't yet support this.
1185 
1186   // Return an estimate of the maximum allocation that could be performed
1187   // without triggering any collection or expansion activity.  In a
1188   // generational collector, for example, this is probably the largest
1189   // allocation that could be supported (without expansion) in the youngest
1190   // generation.  It is "unsafe" because no locks are taken; the result
1191   // should be treated as an approximation, not a guarantee, for use in
1192   // heuristic resizing decisions.


1210 
1211   // The number of regions available for "regular" expansion.
1212   uint expansion_regions() { return _expansion_regions; }
1213 
1214   // Factory method for HeapRegion instances. It will return NULL if
1215   // the allocation fails.
1216   HeapRegion* new_heap_region(uint hrs_index, HeapWord* bottom);
1217 
1218   void verify_not_dirty_region(HeapRegion* hr) PRODUCT_RETURN;
1219   void verify_dirty_region(HeapRegion* hr) PRODUCT_RETURN;
1220   void verify_dirty_young_list(HeapRegion* head) PRODUCT_RETURN;
1221   void verify_dirty_young_regions() PRODUCT_RETURN;
1222 
1223   // verify_region_sets() performs verification over the region
1224   // lists. It will be compiled in the product code to be used when
1225   // necessary (i.e., during heap verification).
1226   void verify_region_sets();
1227 
1228   // verify_region_sets_optional() is planted in the code for
1229   // list verification in non-product builds (and it can be enabled in
1230   // product builds by defining HEAP_REGION_SET_FORCE_VERIFY to be 1).
1231 #if HEAP_REGION_SET_FORCE_VERIFY
1232   void verify_region_sets_optional() {
1233     verify_region_sets();
1234   }
1235 #else // HEAP_REGION_SET_FORCE_VERIFY
1236   void verify_region_sets_optional() { }
1237 #endif // HEAP_REGION_SET_FORCE_VERIFY
1238 
1239 #ifdef ASSERT
1240   bool is_on_master_free_list(HeapRegion* hr) {
1241     return hr->containing_set() == &_free_list;
1242   }
1243 
1244   bool is_in_humongous_set(HeapRegion* hr) {
1245     return hr->containing_set() == &_humongous_set;
1246   }
1247 #endif // ASSERT
1248 
1249   // Wrapper for the region list operations that can be called from
1250   // methods outside this class.


1276 
1277   void set_free_regions_coming();
1278   void reset_free_regions_coming();
1279   bool free_regions_coming() { return _free_regions_coming; }
1280   void wait_while_free_regions_coming();
1281 
1282   // Determine whether the given region is one that we are using as an
1283   // old GC alloc region.
1284   bool is_old_gc_alloc_region(HeapRegion* hr) {
1285     return hr == _retained_old_gc_alloc_region;
1286   }
1287 
1288   // Perform a collection of the heap; intended for use in implementing
1289   // "System.gc".  This probably implies as full a collection as the
1290   // "CollectedHeap" supports.
1291   virtual void collect(GCCause::Cause cause);
1292 
1293   // The same as above but assume that the caller holds the Heap_lock.
1294   void collect_locked(GCCause::Cause cause);
1295 
1296   // True iff an evacuation has failed in the most-recent collection.
1297   bool evacuation_failed() { return _evacuation_failed; }
1298 
1299   // It will free a region if it has allocated objects in it that are
1300   // all dead. It calls either free_region() or
1301   // free_humongous_region() depending on the type of the region that
1302   // is passed to it.
1303   void free_region_if_empty(HeapRegion* hr,
1304                             size_t* pre_used,
1305                             FreeRegionList* free_list,
1306                             OldRegionSet* old_proxy_set,
1307                             HumongousRegionSet* humongous_proxy_set,
1308                             HRRSCleanupTask* hrrs_cleanup_task,
1309                             bool par);
1310 
1311   // It appends the free list to the master free list and updates the
1312   // master humongous list according to the contents of the proxy
1313   // list. It also adjusts the total used bytes according to pre_used
1314   // (if par is true, it will do so by taking the ParGCRareEvent_lock).
1315   void update_sets_after_freeing_regions(size_t pre_used,
1316                                        FreeRegionList* free_list,


1564 
1565   // Perform verification.
1566 
1567   // vo == UsePrevMarking  -> use "prev" marking information,
1568   // vo == UseNextMarking -> use "next" marking information
1569   // vo == UseMarkWord    -> use the mark word in the object header
1570   //
1571   // NOTE: Only the "prev" marking information is guaranteed to be
1572   // consistent most of the time, so most calls to this should use
1573   // vo == UsePrevMarking.
1574   // Currently, there is only one case where this is called with
1575   // vo == UseNextMarking, which is to verify the "next" marking
1576   // information at the end of remark.
1577   // Currently there is only one place where this is called with
1578   // vo == UseMarkWord, which is to verify the marking during a
1579   // full GC.
1580   void verify(bool silent, VerifyOption vo);
1581 
1582   // Override; it uses the "prev" marking information
1583   virtual void verify(bool silent);
1584 
1585   virtual void print_on(outputStream* st) const;
1586   virtual void print_extended_on(outputStream* st) const;
1587   virtual void print_on_error(outputStream* st) const;
1588 
1589   virtual void print_gc_threads_on(outputStream* st) const;
1590   virtual void gc_threads_do(ThreadClosure* tc) const;
1591 
1592   // Override
1593   void print_tracing_info() const;
1594 
1595   // The following two methods are helpful for debugging RSet issues.
1596   void print_cset_rsets() PRODUCT_RETURN;
1597   void print_all_rsets() PRODUCT_RETURN;
1598 
1599   // Convenience function to be used in situations where the heap type can be
1600   // asserted to be this type.
1601   static G1CollectedHeap* heap();
1602 
1603   void set_region_short_lived_locked(HeapRegion* hr);
1604   // add appropriate methods for any other surv rate groups


1965       _partial_scan_cl->do_oop_nv(ref_to_scan);
1966     } else {
1967       // Note: we can use "raw" versions of "region_containing" because
1968       // "obj_to_scan" is definitely in the heap, and is not in a
1969       // humongous region.
1970       HeapRegion* r = _g1h->heap_region_containing_raw(ref_to_scan);
1971       _evac_cl->set_region(r);
1972       _evac_cl->do_oop_nv(ref_to_scan);
1973     }
1974   }
1975 
1976   void deal_with_reference(StarTask ref) {
1977     assert(verify_task(ref), "sanity");
1978     if (ref.is_narrow()) {
1979       deal_with_reference((narrowOop*)ref);
1980     } else {
1981       deal_with_reference((oop*)ref);
1982     }
1983   }
1984 

1985   void trim_queue();
1986 };
1987 
1988 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1COLLECTEDHEAP_HPP