1 /*
   2  * Copyright (c) 2001, 2017, 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_G1_G1REMSET_HPP
  26 #define SHARE_VM_GC_G1_G1REMSET_HPP
  27 
  28 #include "gc/g1/dirtyCardQueue.hpp"
  29 #include "gc/g1/g1CardLiveData.hpp"
  30 #include "gc/g1/g1RemSetSummary.hpp"
  31 #include "gc/g1/heapRegion.hpp"
  32 #include "memory/allocation.hpp"
  33 #include "memory/iterator.hpp"
  34 
  35 // A G1RemSet provides ways of iterating over pointers into a selected
  36 // collection set.
  37 
  38 class BitMap;
  39 class CardTableModRefBS;
  40 class G1BlockOffsetTable;
  41 class CodeBlobClosure;
  42 class G1CollectedHeap;
  43 class G1HotCardCache;
  44 class G1RemSetScanState;
  45 class G1ParScanThreadState;
  46 class G1Policy;
  47 class G1SATBCardTableModRefBS;
  48 class G1ScanObjsDuringScanRSClosure;
  49 class G1ScanObjsDuringUpdateRSClosure;
  50 class HeapRegionClaimer;
  51 
  52 // Closure used for updating remembered sets and recording references that
  53 // point into the collection set while the mutator is running.
  54 // Assumed to be only executed concurrently with the mutator. Yields via
  55 // SuspendibleThreadSet after every card.
  56 class G1RefineCardConcurrentlyClosure: public CardTableEntryClosure {
  57 public:
  58   bool do_card_ptr(jbyte* card_ptr, uint worker_i);
  59 };
  60 
  61 // A G1RemSet in which each heap region has a rem set that records the
  62 // external heap references into it.  Uses a mod ref bs to track updates,
  63 // so that they can be used to update the individual region remsets.
  64 class G1RemSet: public CHeapObj<mtGC> {
  65 private:
  66   G1RefineCardConcurrentlyClosure _refine_card_concurrently_cl;
  67 
  68   G1RemSetScanState* _scan_state;
  69   G1CardLiveData _card_live_data;
  70 
  71   G1RemSetSummary _prev_period_summary;
  72 
  73   // A DirtyCardQueueSet that is used to hold cards that contain
  74   // references into the current collection set. This is used to
  75   // update the remembered sets of the regions in the collection
  76   // set in the event of an evacuation failure.
  77   DirtyCardQueueSet _into_cset_dirty_card_queue_set;
  78 
  79   // Scan all remembered sets of the collection set for references into the collection
  80   // set.
  81   void scan_rem_set(G1ParScanThreadState* pss,
  82                     CodeBlobClosure* heap_region_codeblobs,
  83                     uint worker_i);
  84 
  85   // Flush remaining refinement buffers for cross-region references to either evacuate references
  86   // into the collection set or update the remembered set.
  87   void update_rem_set(DirtyCardQueue* into_cset_dcq, G1ParScanThreadState* pss, uint worker_i);
  88 
  89   G1CollectedHeap* _g1;
  90   size_t _num_conc_refined_cards; // Number of cards refined concurrently to the mutator.
  91 
  92   CardTableModRefBS*     _ct_bs;
  93   G1Policy*              _g1p;
  94   G1HotCardCache*        _hot_card_cache;
  95 
  96 public:
  97   // Gives an approximation on how many threads can be expected to add records to
  98   // a remembered set in parallel. This can be used for sizing data structures to
  99   // decrease performance losses due to data structure sharing.
 100   // Examples for quantities that influence this value are the maximum number of
 101   // mutator threads, maximum number of concurrent refinement or GC threads.
 102   static uint num_par_rem_sets();
 103 
 104   // Initialize data that depends on the heap size being known.
 105   void initialize(size_t capacity, uint max_regions);
 106 
 107   // This is called to reset dual hash tables after the gc pause
 108   // is finished and the initial hash table is no longer being
 109   // scanned.
 110   void cleanupHRRS();
 111 
 112   G1RemSet(G1CollectedHeap* g1,
 113            CardTableModRefBS* ct_bs,
 114            G1HotCardCache* hot_card_cache);
 115   ~G1RemSet();
 116 
 117   // Process all oops in the collection set from the cards in the refinement buffers and
 118   // remembered sets using pss.
 119   //
 120   // Further applies heap_region_codeblobs on the oops of the unmarked nmethods on the strong code
 121   // roots list for each region in the collection set.
 122   void oops_into_collection_set_do(G1ParScanThreadState* pss,
 123                                    CodeBlobClosure* heap_region_codeblobs,
 124                                    uint worker_i);
 125 
 126   // Prepare for and cleanup after an oops_into_collection_set_do
 127   // call.  Must call each of these once before and after (in sequential
 128   // code) any thread calls oops_into_collection_set_do.
 129   void prepare_for_oops_into_collection_set_do();
 130   void cleanup_after_oops_into_collection_set_do();
 131 
 132   G1RemSetScanState* scan_state() const { return _scan_state; }
 133 
 134   // Record, if necessary, the fact that *p (where "p" is in region "from",
 135   // which is required to be non-NULL) has changed to a new non-NULL value.
 136   template <class T> void par_write_ref(HeapRegion* from, T* p, uint tid);
 137 
 138   // Eliminates any remembered set entries that correspond to dead heap ranges.
 139   void scrub(uint worker_num, HeapRegionClaimer* hrclaimer);
 140 
 141   G1RefineCardConcurrentlyClosure* refine_card_concurrently_closure() { return &_refine_card_concurrently_cl; }
 142 
 143   // Refine the card corresponding to "card_ptr". Safe to be called concurrently
 144   // to the mutator.
 145   void refine_card_concurrently(jbyte* card_ptr,
 146                                 uint worker_i);
 147 
 148   // Refine the card corresponding to "card_ptr", applying the given closure to
 149   // all references found. Returns "true" if the given card contains
 150   // oops that have references into the current collection set. Must only be
 151   // called during gc.
 152   bool refine_card_during_gc(jbyte* card_ptr,
 153                              G1ScanObjsDuringUpdateRSClosure* update_rs_cl);
 154 
 155   // Print accumulated summary info from the start of the VM.
 156   void print_summary_info();
 157 
 158   // Print accumulated summary info from the last time called.
 159   void print_periodic_summary_info(const char* header, uint period_count);
 160 
 161   size_t num_conc_refined_cards() const { return _num_conc_refined_cards; }
 162 
 163   void create_card_live_data(WorkGang* workers, G1CMBitMap* mark_bitmap);
 164   void finalize_card_live_data(WorkGang* workers, G1CMBitMap* mark_bitmap);
 165 
 166   // Verify that the liveness count data created concurrently matches one created
 167   // during this safepoint.
 168   void verify_card_live_data(WorkGang* workers, G1CMBitMap* actual_bitmap);
 169 
 170   void clear_card_live_data(WorkGang* workers);
 171 
 172 #ifdef ASSERT
 173   void verify_card_live_data_is_clear();
 174 #endif
 175 };
 176 
 177 class G1ScanRSForRegionClosure : public HeapRegionClosure {
 178   G1RemSetScanState* _scan_state;
 179 
 180   size_t _cards_scanned;
 181   size_t _cards_claimed;
 182   size_t _cards_skipped;
 183 
 184   G1CollectedHeap* _g1h;
 185 
 186   G1ScanObjsDuringScanRSClosure* _scan_objs_on_card_cl;
 187   CodeBlobClosure* _code_root_cl;
 188 
 189   G1BlockOffsetTable* _bot;
 190   G1SATBCardTableModRefBS *_ct_bs;
 191 
 192   double _strong_code_root_scan_time_sec;
 193   uint   _worker_i;
 194 
 195   void claim_card(size_t card_index, const uint region_idx_for_card);
 196   void scan_card(MemRegion mr, uint region_idx_for_card);
 197   void scan_strong_code_roots(HeapRegion* r);
 198 public:
 199   G1ScanRSForRegionClosure(G1RemSetScanState* scan_state,
 200                            G1ScanObjsDuringScanRSClosure* scan_obj_on_card,
 201                            CodeBlobClosure* code_root_cl,
 202                            uint worker_i);
 203 
 204   bool doHeapRegion(HeapRegion* r);
 205 
 206   double strong_code_root_scan_time_sec() {
 207     return _strong_code_root_scan_time_sec;
 208   }
 209 
 210   size_t cards_scanned() const { return _cards_scanned; }
 211   size_t cards_claimed() const { return _cards_claimed; }
 212   size_t cards_skipped() const { return _cards_skipped; }
 213 };
 214 
 215 class RebuildRSOopClosure: public ExtendedOopClosure {
 216   HeapRegion* _from;
 217   G1RemSet* _rs;
 218   uint _worker_i;
 219 
 220   template <class T> void do_oop_work(T* p);
 221 
 222 public:
 223   RebuildRSOopClosure(G1RemSet* rs, uint worker_i = 0) :
 224     _from(NULL), _rs(rs), _worker_i(worker_i)
 225   {}
 226 
 227   void set_from(HeapRegion* from) {
 228     assert(from != NULL, "from region must be non-NULL");
 229     _from = from;
 230   }
 231 
 232   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 233   virtual void do_oop(oop* p)       { do_oop_work(p); }
 234 };
 235 
 236 #endif // SHARE_VM_GC_G1_G1REMSET_HPP