1 /*
   2  * Copyright (c) 2014, 2015, 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/g1CollectedHeap.hpp"
  30 #include "gc/g1/g1CollectorPolicy.hpp"
  31 #include "gc/g1/g1OopClosures.hpp"
  32 #include "gc/g1/g1RemSet.hpp"
  33 #include "gc/g1/g1SATBCardTableModRefBS.hpp"
  34 #include "gc/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 StackObj {
  42  private:
  43   G1CollectedHeap* _g1h;
  44   RefToScanQueue*  _refs;
  45   DirtyCardQueue   _dcq;
  46   G1SATBCardTableModRefBS* _ct_bs;
  47   G1RemSet* _g1_rem;
  48 
  49   G1PLABAllocator*  _plab_allocator;
  50 
  51   ageTable          _age_table;
  52   InCSetState       _dest[InCSetState::Num];
  53   // Local tenuring threshold.
  54   uint              _tenuring_threshold;
  55   G1ParScanClosure  _scanner;
  56 
  57   int  _hash_seed;
  58   uint _queue_num;
  59 
  60   size_t _term_attempts;
  61 
  62   double _start;
  63   double _start_strong_roots;
  64   double _strong_roots_time;
  65   double _start_term;
  66   double _term_time;
  67 
  68   // Map from young-age-index (0 == not young, 1 is youngest) to
  69   // surviving words. base is what we get back from the malloc call
  70   size_t* _surviving_young_words_base;
  71   // this points into the array, as we use the first few entries for padding
  72   size_t* _surviving_young_words;
  73 
  74   // Indicates whether in the last generation (old) there is no more space
  75   // available for allocation.
  76   bool _last_gen_is_full;
  77 
  78 #define PADDING_ELEM_NUM (DEFAULT_CACHE_LINE_SIZE / sizeof(size_t))
  79 
  80   DirtyCardQueue& dirty_card_queue()             { return _dcq;  }
  81   G1SATBCardTableModRefBS* ctbs()                { return _ct_bs; }
  82 
  83   InCSetState dest(InCSetState original) const {
  84     assert(original.is_valid(),
  85            err_msg("Original state invalid: " CSETSTATE_FORMAT, original.value()));
  86     assert(_dest[original.value()].is_valid_gen(),
  87            err_msg("Dest state is invalid: " CSETSTATE_FORMAT, _dest[original.value()].value()));
  88     return _dest[original.value()];
  89   }
  90 
  91  public:
  92   G1ParScanThreadState(G1CollectedHeap* g1h, uint queue_num, ReferenceProcessor* rp);
  93   ~G1ParScanThreadState();
  94 
  95   ageTable*         age_table()       { return &_age_table;       }
  96 
  97 #ifdef ASSERT
  98   bool queue_is_empty() const { return _refs->is_empty(); }
  99 
 100   bool verify_ref(narrowOop* ref) const;
 101   bool verify_ref(oop* ref) const;
 102   bool verify_task(StarTask ref) const;
 103 #endif // ASSERT
 104 
 105   template <class T> void push_on_queue(T* ref);
 106 
 107   template <class T> void update_rs(HeapRegion* from, T* p, uint tid) {
 108     // If the new value of the field points to the same region or
 109     // is the to-space, we don't need to include it in the Rset updates.
 110     if (!from->is_in_reserved(oopDesc::load_decode_heap_oop(p)) && !from->is_survivor()) {
 111       size_t card_index = ctbs()->index_for(p);
 112       // If the card hasn't been added to the buffer, do it.
 113       if (ctbs()->mark_card_deferred(card_index)) {
 114         dirty_card_queue().enqueue((jbyte*)ctbs()->byte_for_index(card_index));
 115       }
 116     }
 117   }
 118 
 119   int* hash_seed() { return &_hash_seed; }
 120   uint queue_num() { return _queue_num; }
 121 
 122   size_t term_attempts() const  { return _term_attempts; }
 123   void note_term_attempt() { _term_attempts++; }
 124 
 125   void start_strong_roots() {
 126     _start_strong_roots = os::elapsedTime();
 127   }
 128   void end_strong_roots() {
 129     _strong_roots_time += (os::elapsedTime() - _start_strong_roots);
 130   }
 131   double strong_roots_time() const { return _strong_roots_time; }
 132 
 133   void start_term_time() {
 134     note_term_attempt();
 135     _start_term = os::elapsedTime();
 136   }
 137   void end_term_time() {
 138     _term_time += (os::elapsedTime() - _start_term);
 139   }
 140   double term_time() const { return _term_time; }
 141 
 142   double elapsed_time() const {
 143     return os::elapsedTime() - _start;
 144   }
 145 
 146   static void print_termination_stats_hdr(outputStream* const st = gclog_or_tty);
 147   void print_termination_stats(int i, outputStream* const st = gclog_or_tty) const;
 148 
 149   size_t* surviving_young_words() {
 150     // We add on to hide entry 0 which accumulates surviving words for
 151     // age -1 regions (i.e. non-young ones)
 152     return _surviving_young_words;
 153   }
 154 
 155  private:
 156   #define G1_PARTIAL_ARRAY_MASK 0x2
 157 
 158   inline bool has_partial_array_mask(oop* ref) const {
 159     return ((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) == G1_PARTIAL_ARRAY_MASK;
 160   }
 161 
 162   // We never encode partial array oops as narrowOop*, so return false immediately.
 163   // This allows the compiler to create optimized code when popping references from
 164   // the work queue.
 165   inline bool has_partial_array_mask(narrowOop* ref) const {
 166     assert(((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) != G1_PARTIAL_ARRAY_MASK, "Partial array oop reference encoded as narrowOop*");
 167     return false;
 168   }
 169 
 170   // Only implement set_partial_array_mask() for regular oops, not for narrowOops.
 171   // We always encode partial arrays as regular oop, to allow the
 172   // specialization for has_partial_array_mask() for narrowOops above.
 173   // This means that unintentional use of this method with narrowOops are caught
 174   // by the compiler.
 175   inline oop* set_partial_array_mask(oop obj) const {
 176     assert(((uintptr_t)(void *)obj & G1_PARTIAL_ARRAY_MASK) == 0, "Information loss!");
 177     return (oop*) ((uintptr_t)(void *)obj | G1_PARTIAL_ARRAY_MASK);
 178   }
 179 
 180   inline oop clear_partial_array_mask(oop* ref) const {
 181     return cast_to_oop((intptr_t)ref & ~G1_PARTIAL_ARRAY_MASK);
 182   }
 183 
 184   inline void do_oop_partial_array(oop* p);
 185 
 186   // This method is applied to the fields of the objects that have just been copied.
 187   template <class T> inline void do_oop_evac(T* p, HeapRegion* from);
 188 
 189   template <class T> inline void deal_with_reference(T* ref_to_scan);
 190 
 191   inline void dispatch_reference(StarTask ref);
 192 
 193   // Tries to allocate word_sz in the PLAB of the next "generation" after trying to
 194   // allocate into dest. State is the original (source) cset state for the object
 195   // that is allocated for. Previous_plab_refill_failed indicates whether previously
 196   // a PLAB refill into "state" failed.
 197   // Returns a non-NULL pointer if successful, and updates dest if required.
 198   // Also determines whether we should continue to try to allocate into the various
 199   // generations or just end trying to allocate.
 200   HeapWord* allocate_in_next_plab(InCSetState const state,
 201                                   InCSetState* dest,
 202                                   size_t word_sz,
 203                                   AllocationContext_t const context,
 204                                   bool previous_plab_refill_failed);
 205 
 206   inline InCSetState next_state(InCSetState const state, markOop const m, uint& age);
 207  public:
 208 
 209   oop copy_to_survivor_space(InCSetState const state, oop const obj, markOop const old_mark);
 210 
 211   void trim_queue();
 212 
 213   inline void steal_and_trim_queue(RefToScanQueueSet *task_queues);
 214 
 215   // An attempt to evacuate "obj" has failed; take necessary steps.
 216   oop handle_evacuation_failure_par(oop obj, markOop m);
 217 };
 218 
 219 #endif // SHARE_VM_GC_G1_G1PARSCANTHREADSTATE_HPP