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