1 /*
   2  * Copyright (c) 2014, 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_G1PARSCANTHREADSTATE_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1PARSCANTHREADSTATE_HPP
  27 
  28 #include "gc_implementation/g1/dirtyCardQueue.hpp"
  29 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
  30 #include "gc_implementation/g1/g1CollectedHeap.hpp"
  31 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
  32 #include "gc_implementation/g1/g1OopClosures.hpp"
  33 #include "gc_implementation/g1/g1RemSet.hpp"
  34 #include "gc_implementation/shared/ageTable.hpp"
  35 #include "memory/allocation.hpp"
  36 #include "oops/oop.hpp"
  37 
  38 class HeapRegion;
  39 class outputStream;
  40 
  41 class G1ParScanThreadState : public CHeapObj<mtGC> {
  42  private:
  43   G1CollectedHeap* _g1h;
  44   RefToScanQueue*  _refs;
  45   DirtyCardQueue   _dcq;
  46   G1SATBCardTableModRefBS* _ct_bs;
  47   G1RemSet* _g1_rem;
  48 
  49   PLABAllocator*  _plab_allocator;
  50 
  51   ageTable          _age_table;
  52   InCSetState       _dest[InCSetState::Num];
  53   // Local tenuring threshold.
  54   uint              _tenuring_threshold;
  55   G1ParScanClosure  _scanner;
  56   G1ParScanHeapEvacFailureClosure _evac_failure_cl;
  57 
  58   int  _hash_seed;
  59   uint _worker_id;
  60 
  61   // Map from young-age-index (0 == not young, 1 is youngest) to
  62   // surviving words. base is what we get back from the malloc call
  63   size_t* _surviving_young_words_base;
  64   // this points into the array, as we use the first few entries for padding
  65   size_t* _surviving_young_words;
  66 
  67 #define PADDING_ELEM_NUM (DEFAULT_CACHE_LINE_SIZE / sizeof(size_t))
  68 
  69   DirtyCardQueue& dirty_card_queue()             { return _dcq;  }
  70   G1SATBCardTableModRefBS* ctbs()                { return _ct_bs; }
  71 
  72   InCSetState dest(InCSetState original) const {
  73     assert(original.is_valid(),
  74            err_msg("Original state invalid: " CSETSTATE_FORMAT, original.value()));
  75     assert(_dest[original.value()].is_valid_gen(),
  76            err_msg("Dest state is invalid: " CSETSTATE_FORMAT, _dest[original.value()].value()));
  77     return _dest[original.value()];
  78   }
  79 
  80  public:
  81   G1ParScanThreadState(G1CollectedHeap* g1h, uint worker_id);
  82   ~G1ParScanThreadState();
  83 
  84   void set_ref_processor(ReferenceProcessor* rp) { _scanner.set_ref_processor(rp); _evac_failure_cl.set_ref_processor(rp); }
  85   
  86   ageTable*         age_table()       { return &_age_table;       }
  87 
  88 #ifdef ASSERT
  89   bool queue_is_empty() const { return _refs->is_empty(); }
  90 
  91   bool verify_ref(narrowOop* ref) const;
  92   bool verify_ref(oop* ref) const;
  93   bool verify_task(StarTask ref) const;
  94 #endif // ASSERT
  95 
  96   template <class T> void push_on_queue(T* ref) {
  97     assert(verify_ref(ref), "sanity");
  98     _refs->push(ref);
  99   }
 100 
 101   template <class T> void update_rs(HeapRegion* from, T* p, uint tid) {
 102     // If the new value of the field points to the same region or
 103     // is the to-space, we don't need to include it in the Rset updates.
 104     if (!from->is_in_reserved(oopDesc::load_decode_heap_oop(p)) && !from->is_survivor()) {
 105       size_t card_index = ctbs()->index_for(p);
 106       // If the card hasn't been added to the buffer, do it.
 107       if (ctbs()->mark_card_deferred(card_index)) {
 108         dirty_card_queue().enqueue((jbyte*)ctbs()->byte_for_index(card_index));
 109       }
 110     }
 111   }
 112 
 113   OopsInHeapRegionClosure* evac_failure_closure() { return &_evac_failure_cl; }
 114 
 115   int* hash_seed() { return &_hash_seed; }
 116   uint worker_queue_id() { return _worker_id; }
 117 
 118   // Returns the current amount of waste due to alignment or not being able to fit
 119   // objects within LABs.
 120   size_t lab_waste() const;
 121   // Returns the current amount of waste due to undo after trying to reserve space
 122   // in the LAB.
 123   size_t lab_undo_waste() const;
 124 
 125   size_t* surviving_young_words() {
 126     // We add on to hide entry 0 which accumulates surviving words for
 127     // age -1 regions (i.e. non-young ones)
 128     return _surviving_young_words;
 129   }
 130 
 131  private:
 132   #define G1_PARTIAL_ARRAY_MASK 0x2
 133 
 134   inline bool has_partial_array_mask(oop* ref) const {
 135     return ((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) == G1_PARTIAL_ARRAY_MASK;
 136   }
 137 
 138   // We never encode partial array oops as narrowOop*, so return false immediately.
 139   // This allows the compiler to create optimized code when popping references from
 140   // the work queue.
 141   inline bool has_partial_array_mask(narrowOop* ref) const {
 142     assert(((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) != G1_PARTIAL_ARRAY_MASK, "Partial array oop reference encoded as narrowOop*");
 143     return false;
 144   }
 145 
 146   // Only implement set_partial_array_mask() for regular oops, not for narrowOops.
 147   // We always encode partial arrays as regular oop, to allow the
 148   // specialization for has_partial_array_mask() for narrowOops above.
 149   // This means that unintentional use of this method with narrowOops are caught
 150   // by the compiler.
 151   inline oop* set_partial_array_mask(oop obj) const {
 152     assert(((uintptr_t)(void *)obj & G1_PARTIAL_ARRAY_MASK) == 0, "Information loss!");
 153     return (oop*) ((uintptr_t)(void *)obj | G1_PARTIAL_ARRAY_MASK);
 154   }
 155 
 156   inline oop clear_partial_array_mask(oop* ref) const {
 157     return cast_to_oop((intptr_t)ref & ~G1_PARTIAL_ARRAY_MASK);
 158   }
 159 
 160   inline void do_oop_partial_array(oop* p);
 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, HeapRegion* from);
 164 
 165   template <class T> inline void deal_with_reference(T* ref_to_scan);
 166 
 167   inline void dispatch_reference(StarTask ref);
 168 
 169   // Tries to allocate word_sz in the PLAB of the next "generation" after trying to
 170   // allocate into dest. State is the original (source) cset state for the object
 171   // that is allocated for.
 172   // Returns a non-NULL pointer if successful, and updates dest if required.
 173   HeapWord* allocate_in_next_plab(InCSetState const state,
 174                                   InCSetState* dest,
 175                                   size_t word_sz,
 176                                   AllocationContext_t const context);
 177 
 178   inline InCSetState next_state(InCSetState const state, markOop const m, uint& age);
 179  public:
 180 
 181   oop copy_to_survivor_space(InCSetState const state, oop const obj, markOop const old_mark);
 182 
 183   void trim_queue();
 184 
 185   inline void steal_and_trim_queue(RefToScanQueueSet *task_queues);
 186 };
 187 
 188 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1PARSCANTHREADSTATE_HPP