1 /*
   2  * Copyright (c) 2014, 2020, 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_GC_G1_G1PARSCANTHREADSTATE_HPP
  26 #define SHARE_GC_G1_G1PARSCANTHREADSTATE_HPP
  27 
  28 #include "gc/g1/g1CardTable.hpp"
  29 #include "gc/g1/g1CollectedHeap.hpp"
  30 #include "gc/g1/g1RedirtyCardsQueue.hpp"
  31 #include "gc/g1/g1OopClosures.hpp"
  32 #include "gc/g1/g1Policy.hpp"
  33 #include "gc/g1/g1RemSet.hpp"
  34 #include "gc/g1/heapRegionRemSet.hpp"
  35 #include "gc/shared/ageTable.hpp"
  36 #include "gc/shared/taskqueue.hpp"
  37 #include "memory/allocation.hpp"
  38 #include "oops/oop.hpp"
  39 #include "utilities/ticks.hpp"
  40 
  41 class G1OopStarChunkedList;
  42 class G1PLABAllocator;
  43 class G1EvacuationRootClosures;
  44 class HeapRegion;
  45 class outputStream;
  46 
  47 class G1ParScanThreadState : public CHeapObj<mtGC> {
  48   G1CollectedHeap* _g1h;
  49   G1ScannerTasksQueue* _task_queue;
  50   G1RedirtyCardsQueue _rdcq;
  51   G1CardTable* _ct;
  52   G1EvacuationRootClosures* _closures;
  53 
  54   G1PLABAllocator* _plab_allocator;
  55 
  56   AgeTable _age_table;
  57   G1HeapRegionAttr _dest[G1HeapRegionAttr::Num];
  58   // Local tenuring threshold.
  59   uint _tenuring_threshold;
  60   G1ScanEvacuatedObjClosure  _scanner;
  61 
  62   uint _worker_id;
  63 
  64   // Remember the last enqueued card to avoid enqueuing the same card over and over;
  65   // since we only ever scan a card once, this is sufficient.
  66   size_t _last_enqueued_card;
  67 
  68   // Upper and lower threshold to start and end work queue draining.
  69   uint const _stack_trim_upper_threshold;
  70   uint const _stack_trim_lower_threshold;
  71 
  72   Tickspan _trim_ticks;
  73   // Map from young-age-index (0 == not young, 1 is youngest) to
  74   // surviving words. base is what we get back from the malloc call
  75   size_t* _surviving_young_words_base;
  76   // this points into the array, as we use the first few entries for padding
  77   size_t* _surviving_young_words;
  78   // Number of elements in the array above.
  79   size_t _surviving_words_length;
  80   // Indicates whether in the last generation (old) there is no more space
  81   // available for allocation.
  82   bool _old_gen_is_full;
  83 
  84   G1RedirtyCardsQueue& redirty_cards_queue()     { return _rdcq; }
  85   G1CardTable* ct()                              { return _ct; }
  86 
  87   G1HeapRegionAttr dest(G1HeapRegionAttr original) const {
  88     assert(original.is_valid(),
  89            "Original region attr invalid: %s", original.get_type_str());
  90     assert(_dest[original.type()].is_valid_gen(),
  91            "Dest region attr is invalid: %s", _dest[original.type()].get_type_str());
  92     return _dest[original.type()];
  93   }
  94 
  95   size_t _num_optional_regions;
  96   G1OopStarChunkedList* _oops_into_optional_regions;
  97 
  98   G1NUMA* _numa;
  99 
 100   // Records how many object allocations happened at each node during copy to survivor.
 101   // Only starts recording when log of gc+heap+numa is enabled and its data is
 102   // transferred when flushed.
 103   size_t* _obj_alloc_stat;
 104 
 105 public:
 106   G1ParScanThreadState(G1CollectedHeap* g1h,
 107                        G1RedirtyCardsQueueSet* rdcqs,
 108                        uint worker_id,
 109                        size_t young_cset_length,
 110                        size_t optional_cset_length);
 111   virtual ~G1ParScanThreadState();
 112 
 113   void set_ref_discoverer(ReferenceDiscoverer* rd) { _scanner.set_ref_discoverer(rd); }
 114 
 115 #ifdef ASSERT
 116   bool queue_is_empty() const { return _task_queue->is_empty(); }
 117 #endif
 118 
 119   void verify_task(narrowOop* task) const NOT_DEBUG_RETURN;
 120   void verify_task(oop* task) const NOT_DEBUG_RETURN;
 121   void verify_task(PartialArrayScanTask task) const NOT_DEBUG_RETURN;
 122   void verify_task(ScannerTask task) const NOT_DEBUG_RETURN;
 123 
 124   void push_on_queue(ScannerTask task);
 125 
 126   template <class T> void enqueue_card_if_tracked(G1HeapRegionAttr region_attr, T* p, oop o) {
 127     assert(!HeapRegion::is_in_same_region(p, o), "Should have filtered out cross-region references already.");
 128     assert(!_g1h->heap_region_containing(p)->is_young(), "Should have filtered out from-young references already.");
 129 
 130 #ifdef ASSERT
 131     HeapRegion* const hr_obj = _g1h->heap_region_containing(o);
 132     assert(region_attr.needs_remset_update() == hr_obj->rem_set()->is_tracked(),
 133            "State flag indicating remset tracking disagrees (%s) with actual remembered set (%s) for region %u",
 134            BOOL_TO_STR(region_attr.needs_remset_update()),
 135            BOOL_TO_STR(hr_obj->rem_set()->is_tracked()),
 136            hr_obj->hrm_index());
 137 #endif
 138     if (!region_attr.needs_remset_update()) {
 139       return;
 140     }
 141     size_t card_index = ct()->index_for(p);
 142     // If the card hasn't been added to the buffer, do it.
 143     if (_last_enqueued_card != card_index) {
 144       redirty_cards_queue().enqueue(ct()->byte_for_index(card_index));
 145       _last_enqueued_card = card_index;
 146     }
 147   }
 148 
 149   G1EvacuationRootClosures* closures() { return _closures; }
 150   uint worker_id() { return _worker_id; }
 151 
 152   size_t lab_waste_words() const;
 153   size_t lab_undo_waste_words() const;
 154 
 155   // Pass locally gathered statistics to global state. Returns the total number of
 156   // HeapWords copied.
 157   size_t flush(size_t* surviving_young_words);
 158 
 159 private:
 160   inline void do_partial_array(PartialArrayScanTask task);
 161 
 162   // This method is applied to the fields of the objects that have just been copied.
 163   template <class T> inline void do_oop_evac(T* p);
 164 
 165   inline void dispatch_task(ScannerTask task);
 166 
 167   // Tries to allocate word_sz in the PLAB of the next "generation" after trying to
 168   // allocate into dest. Previous_plab_refill_failed indicates whether previous
 169   // PLAB refill for the original (source) object failed.
 170   // Returns a non-NULL pointer if successful, and updates dest if required.
 171   // Also determines whether we should continue to try to allocate into the various
 172   // generations or just end trying to allocate.
 173   HeapWord* allocate_in_next_plab(G1HeapRegionAttr* dest,
 174                                   size_t word_sz,
 175                                   bool previous_plab_refill_failed,
 176                                   uint node_index);
 177 
 178   inline G1HeapRegionAttr next_region_attr(G1HeapRegionAttr const region_attr, markWord const m, uint& age);
 179 
 180   void report_promotion_event(G1HeapRegionAttr const dest_attr,
 181                               oop const old, size_t word_sz, uint age,
 182                               HeapWord * const obj_ptr, uint node_index) const;
 183 
 184   inline bool needs_partial_trimming() const;
 185   inline bool is_partially_trimmed() const;
 186 
 187   inline void trim_queue_to_threshold(uint threshold);
 188 
 189   // NUMA statistics related methods.
 190   inline void initialize_numa_stats();
 191   inline void flush_numa_stats();
 192   inline void update_numa_stats(uint node_index);
 193 
 194 public:
 195   oop copy_to_survivor_space(G1HeapRegionAttr const region_attr, oop const obj, markWord const old_mark);
 196 
 197   void trim_queue();
 198   void trim_queue_partially();
 199 
 200   Tickspan trim_ticks() const;
 201   void reset_trim_ticks();
 202 
 203   inline void steal_and_trim_queue(G1ScannerTasksQueueSet *task_queues);
 204 
 205   // An attempt to evacuate "obj" has failed; take necessary steps.
 206   oop handle_evacuation_failure_par(oop obj, markWord m);
 207 
 208   template <typename T>
 209   inline void remember_root_into_optional_region(T* p);
 210   template <typename T>
 211   inline void remember_reference_into_optional_region(T* p);
 212 
 213   inline G1OopStarChunkedList* oops_into_optional_region(const HeapRegion* hr);
 214 };
 215 
 216 class G1ParScanThreadStateSet : public StackObj {
 217   G1CollectedHeap* _g1h;
 218   G1RedirtyCardsQueueSet* _rdcqs;
 219   G1ParScanThreadState** _states;
 220   size_t* _surviving_young_words_total;
 221   size_t _young_cset_length;
 222   size_t _optional_cset_length;
 223   uint _n_workers;
 224   bool _flushed;
 225 
 226  public:
 227   G1ParScanThreadStateSet(G1CollectedHeap* g1h,
 228                           G1RedirtyCardsQueueSet* rdcqs,
 229                           uint n_workers,
 230                           size_t young_cset_length,
 231                           size_t optional_cset_length);
 232   ~G1ParScanThreadStateSet();
 233 
 234   void flush();
 235   void record_unused_optional_region(HeapRegion* hr);
 236 
 237   G1ParScanThreadState* state_for_worker(uint worker_id);
 238 
 239   const size_t* surviving_young_words() const;
 240 
 241  private:
 242   G1ParScanThreadState* new_par_scan_state(uint worker_id, size_t young_cset_length);
 243 };
 244 
 245 #endif // SHARE_GC_G1_G1PARSCANTHREADSTATE_HPP