1 /*
   2  * Copyright (c) 2014, 2018, 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_G1PARSCANTHREADSTATE_HPP
  26 #define SHARE_VM_GC_G1_G1PARSCANTHREADSTATE_HPP
  27 
  28 #include "gc/g1/dirtyCardQueue.hpp"
  29 #include "gc/g1/g1CardTable.hpp"
  30 #include "gc/g1/g1CollectedHeap.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 "memory/allocation.hpp"
  37 #include "oops/oop.hpp"
  38 #include "utilities/ticks.hpp"
  39 
  40 class G1PLABAllocator;
  41 class G1EvacuationRootClosures;
  42 class HeapRegion;
  43 class outputStream;
  44 
  45 class G1ParScanThreadState : public CHeapObj<mtGC> {
  46   G1CollectedHeap* _g1h;
  47   RefToScanQueue*  _refs;
  48   DirtyCardQueue   _dcq;
  49   G1CardTable*     _ct;
  50   G1EvacuationRootClosures* _closures;
  51 
  52   G1PLABAllocator*  _plab_allocator;
  53 
  54   AgeTable          _age_table;
  55   InCSetState       _dest[InCSetState::Num];
  56   // Local tenuring threshold.
  57   uint              _tenuring_threshold;
  58   G1ScanEvacuatedObjClosure  _scanner;
  59 
  60   uint _worker_id;
  61 
  62   // Upper and lower threshold to start and end work queue draining.
  63   uint const _stack_trim_upper_threshold;
  64   uint const _stack_trim_lower_threshold;
  65 
  66   Tickspan _trim_ticks;
  67   // Map from young-age-index (0 == not young, 1 is youngest) to
  68   // surviving words. base is what we get back from the malloc call
  69   size_t* _surviving_young_words_base;
  70   // this points into the array, as we use the first few entries for padding
  71   size_t* _surviving_young_words;
  72 
  73   // Indicates whether in the last generation (old) there is no more space
  74   // available for allocation.
  75   bool _old_gen_is_full;
  76 
  77 #define PADDING_ELEM_NUM (DEFAULT_CACHE_LINE_SIZE / sizeof(size_t))
  78 
  79   DirtyCardQueue& dirty_card_queue()             { return _dcq;  }
  80   G1CardTable* ct()                              { return _ct; }
  81 
  82   InCSetState dest(InCSetState original) const {
  83     assert(original.is_valid(),
  84            "Original state invalid: " CSETSTATE_FORMAT, original.value());
  85     assert(_dest[original.value()].is_valid_gen(),
  86            "Dest state is invalid: " CSETSTATE_FORMAT, _dest[original.value()].value());
  87     return _dest[original.value()];
  88   }
  89 
  90 public:
  91   G1ParScanThreadState(G1CollectedHeap* g1h, uint worker_id, size_t young_cset_length);
  92   virtual ~G1ParScanThreadState();
  93 
  94   void set_ref_discoverer(ReferenceDiscoverer* rd) { _scanner.set_ref_discoverer(rd); }
  95 
  96 #ifdef ASSERT
  97   bool queue_is_empty() const { return _refs->is_empty(); }
  98 
  99   bool verify_ref(narrowOop* ref) const;
 100   bool verify_ref(oop* ref) const;
 101   bool verify_task(StarTask ref) const;
 102 #endif // ASSERT
 103 
 104   template <class T> void do_oop_ext(T* ref);
 105   template <class T> void push_on_queue(T* ref);
 106 
 107   template <class T> void enqueue_card_if_tracked(T* p, oop o) {
 108     assert(!HeapRegion::is_in_same_region(p, o), "Should have filtered out cross-region references already.");
 109     assert(!_g1h->heap_region_containing(p)->is_young(), "Should have filtered out from-young references already.");
 110     if (!_g1h->heap_region_containing((HeapWord*)o)->rem_set()->is_tracked()) {
 111       return;
 112     }
 113     size_t card_index = ct()->index_for(p);
 114     // If the card hasn't been added to the buffer, do it.
 115     if (ct()->mark_card_deferred(card_index)) {
 116       dirty_card_queue().enqueue((jbyte*)ct()->byte_for_index(card_index));
 117     }
 118   }
 119 
 120   G1EvacuationRootClosures* closures() { return _closures; }
 121   uint worker_id() { return _worker_id; }
 122 
 123   // Returns the current amount of waste due to alignment or not being able to fit
 124   // objects within LABs and the undo waste.
 125   virtual void waste(size_t& wasted, size_t& undo_wasted);
 126 
 127   size_t* surviving_young_words() {
 128     // We add one to hide entry 0 which accumulates surviving words for
 129     // age -1 regions (i.e. non-young ones)
 130     return _surviving_young_words + 1;
 131   }
 132 
 133   void flush(size_t* surviving_young_words);
 134 
 135 private:
 136   #define G1_PARTIAL_ARRAY_MASK 0x2
 137 
 138   inline bool has_partial_array_mask(oop* ref) const {
 139     return ((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) == G1_PARTIAL_ARRAY_MASK;
 140   }
 141 
 142   // We never encode partial array oops as narrowOop*, so return false immediately.
 143   // This allows the compiler to create optimized code when popping references from
 144   // the work queue.
 145   inline bool has_partial_array_mask(narrowOop* ref) const {
 146     assert(((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) != G1_PARTIAL_ARRAY_MASK, "Partial array oop reference encoded as narrowOop*");
 147     return false;
 148   }
 149 
 150   // Only implement set_partial_array_mask() for regular oops, not for narrowOops.
 151   // We always encode partial arrays as regular oop, to allow the
 152   // specialization for has_partial_array_mask() for narrowOops above.
 153   // This means that unintentional use of this method with narrowOops are caught
 154   // by the compiler.
 155   inline oop* set_partial_array_mask(oop obj) const {
 156     assert(((uintptr_t)(void *)obj & G1_PARTIAL_ARRAY_MASK) == 0, "Information loss!");
 157     return (oop*) ((uintptr_t)(void *)obj | G1_PARTIAL_ARRAY_MASK);
 158   }
 159 
 160   inline oop clear_partial_array_mask(oop* ref) const {
 161     return cast_to_oop((intptr_t)ref & ~G1_PARTIAL_ARRAY_MASK);
 162   }
 163 
 164   inline void do_oop_partial_array(oop* p);
 165 
 166   // This method is applied to the fields of the objects that have just been copied.
 167   template <class T> inline void do_oop_evac(T* p);
 168 
 169   inline void deal_with_reference(oop* ref_to_scan);
 170   inline void deal_with_reference(narrowOop* ref_to_scan);
 171 
 172   inline void dispatch_reference(StarTask ref);
 173 
 174   // Tries to allocate word_sz in the PLAB of the next "generation" after trying to
 175   // allocate into dest. State is the original (source) cset state for the object
 176   // that is allocated for. Previous_plab_refill_failed indicates whether previously
 177   // a PLAB refill into "state" failed.
 178   // Returns a non-NULL pointer if successful, and updates dest if required.
 179   // Also determines whether we should continue to try to allocate into the various
 180   // generations or just end trying to allocate.
 181   HeapWord* allocate_in_next_plab(InCSetState const state,
 182                                   InCSetState* dest,
 183                                   size_t word_sz,
 184                                   bool previous_plab_refill_failed);
 185 
 186   inline InCSetState next_state(InCSetState const state, markOop const m, uint& age);
 187 
 188   void report_promotion_event(InCSetState const dest_state,
 189                               oop const old, size_t word_sz, uint age,
 190                               HeapWord * const obj_ptr) const;
 191 
 192   inline bool needs_partial_trimming() const;
 193   inline bool is_partially_trimmed() const;
 194 
 195   inline void trim_queue_to_threshold(uint threshold);
 196 public:
 197   oop copy_to_survivor_space(InCSetState const state, oop const obj, markOop const old_mark);
 198 
 199   void trim_queue();
 200   void trim_queue_partially();
 201 
 202   Tickspan trim_ticks() const;
 203   void reset_trim_ticks();
 204 
 205   inline void steal_and_trim_queue(RefToScanQueueSet *task_queues);
 206 
 207   // An attempt to evacuate "obj" has failed; take necessary steps.
 208   oop handle_evacuation_failure_par(oop obj, markOop m);
 209 };
 210 
 211 class G1ParScanThreadStateSet : public StackObj {
 212   G1CollectedHeap* _g1h;
 213   G1ParScanThreadState** _states;
 214   size_t* _surviving_young_words_total;
 215   size_t _young_cset_length;
 216   uint _n_workers;
 217   bool _flushed;
 218 
 219  public:
 220   G1ParScanThreadStateSet(G1CollectedHeap* g1h, uint n_workers, size_t young_cset_length);
 221   ~G1ParScanThreadStateSet();
 222 
 223   void flush();
 224 
 225   G1ParScanThreadState* state_for_worker(uint worker_id);
 226 
 227   const size_t* surviving_young_words() const;
 228 
 229  private:
 230   G1ParScanThreadState* new_par_scan_state(uint worker_id, size_t young_cset_length);
 231 };
 232 
 233 #endif // SHARE_VM_GC_G1_G1PARSCANTHREADSTATE_HPP