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 StackObj {
  42 protected:
  43   G1CollectedHeap* _g1h;
  44   RefToScanQueue*  _refs;
  45   DirtyCardQueue   _dcq;
  46   G1SATBCardTableModRefBS* _ct_bs;
  47   G1RemSet* _g1_rem;
  48 
  49   G1ParGCAllocBuffer  _surviving_alloc_buffer;
  50   G1ParGCAllocBuffer  _tenured_alloc_buffer;
  51   G1ParGCAllocBuffer* _alloc_buffers[GCAllocPurposeCount];
  52   ageTable            _age_table;
  53 
  54   G1ParScanClosure    _scanner;
  55 
  56   size_t           _alloc_buffer_waste;
  57   size_t           _undo_waste;
  58 
  59   OopsInHeapRegionClosure*      _evac_failure_cl;
  60 
  61   int  _hash_seed;
  62   uint _queue_num;
  63 
  64   size_t _term_attempts;
  65 
  66   double _start;
  67   double _start_strong_roots;
  68   double _strong_roots_time;
  69   double _start_term;
  70   double _term_time;
  71 
  72   // Map from young-age-index (0 == not young, 1 is youngest) to
  73   // surviving words. base is what we get back from the malloc call
  74   size_t* _surviving_young_words_base;
  75   // this points into the array, as we use the first few entries for padding
  76   size_t* _surviving_young_words;
  77 
  78 #define PADDING_ELEM_NUM (DEFAULT_CACHE_LINE_SIZE / sizeof(size_t))
  79 
  80   void   add_to_alloc_buffer_waste(size_t waste) { _alloc_buffer_waste += waste; }
  81 
  82   void   add_to_undo_waste(size_t waste)         { _undo_waste += waste; }
  83 
  84   DirtyCardQueue& dirty_card_queue()             { return _dcq;  }
  85   G1SATBCardTableModRefBS* ctbs()                { return _ct_bs; }
  86 
  87   template <class T> inline void immediate_rs_update(HeapRegion* from, T* p, int tid);
  88 
  89   template <class T> void deferred_rs_update(HeapRegion* from, T* p, int tid) {
  90     // If the new value of the field points to the same region or
  91     // is the to-space, we don't need to include it in the Rset updates.
  92     if (!from->is_in_reserved(oopDesc::load_decode_heap_oop(p)) && !from->is_survivor()) {
  93       size_t card_index = ctbs()->index_for(p);
  94       // If the card hasn't been added to the buffer, do it.
  95       if (ctbs()->mark_card_deferred(card_index)) {
  96         dirty_card_queue().enqueue((jbyte*)ctbs()->byte_for_index(card_index));
  97       }
  98     }
  99   }
 100 
 101 public:
 102   G1ParScanThreadState(G1CollectedHeap* g1h, uint queue_num, ReferenceProcessor* rp);
 103   ~G1ParScanThreadState();
 104 
 105   RefToScanQueue*   refs()            { return _refs;             }
 106   ageTable*         age_table()       { return &_age_table;       }
 107 
 108   G1ParGCAllocBuffer* alloc_buffer(GCAllocPurpose purpose) {
 109     return _alloc_buffers[purpose];
 110   }
 111 
 112   size_t alloc_buffer_waste() const              { return _alloc_buffer_waste; }
 113   size_t undo_waste() const                      { return _undo_waste; }
 114 
 115 #ifdef ASSERT
 116   bool verify_ref(narrowOop* ref) const;
 117   bool verify_ref(oop* ref) const;
 118   bool verify_task(StarTask ref) const;
 119 #endif // ASSERT
 120 
 121   template <class T> void push_on_queue(T* ref) {
 122     assert(verify_ref(ref), "sanity");
 123     refs()->push(ref);
 124   }
 125 
 126   template <class T> inline void update_rs(HeapRegion* from, T* p, int tid);
 127 
 128   HeapWord* allocate_slow(GCAllocPurpose purpose, size_t word_sz) {
 129     HeapWord* obj = NULL;
 130     size_t gclab_word_size = _g1h->desired_plab_sz(purpose);
 131     if (word_sz * 100 < gclab_word_size * ParallelGCBufferWastePct) {
 132       G1ParGCAllocBuffer* alloc_buf = alloc_buffer(purpose);
 133       add_to_alloc_buffer_waste(alloc_buf->words_remaining());
 134       alloc_buf->retire(false /* end_of_gc */, false /* retain */);
 135 
 136       HeapWord* buf = _g1h->par_allocate_during_gc(purpose, gclab_word_size);
 137       if (buf == NULL) return NULL; // Let caller handle allocation failure.
 138       // Otherwise.
 139       alloc_buf->set_word_size(gclab_word_size);
 140       alloc_buf->set_buf(buf);
 141 
 142       obj = alloc_buf->allocate(word_sz);
 143       assert(obj != NULL, "buffer was definitely big enough...");
 144     } else {
 145       obj = _g1h->par_allocate_during_gc(purpose, word_sz);
 146     }
 147     return obj;
 148   }
 149 
 150   HeapWord* allocate(GCAllocPurpose purpose, size_t word_sz) {
 151     HeapWord* obj = alloc_buffer(purpose)->allocate(word_sz);
 152     if (obj != NULL) return obj;
 153     return allocate_slow(purpose, word_sz);
 154   }
 155 
 156   void undo_allocation(GCAllocPurpose purpose, HeapWord* obj, size_t word_sz) {
 157     if (alloc_buffer(purpose)->contains(obj)) {
 158       assert(alloc_buffer(purpose)->contains(obj + word_sz - 1),
 159              "should contain whole object");
 160       alloc_buffer(purpose)->undo_allocation(obj, word_sz);
 161     } else {
 162       CollectedHeap::fill_with_object(obj, word_sz);
 163       add_to_undo_waste(word_sz);
 164     }
 165   }
 166 
 167   void set_evac_failure_closure(OopsInHeapRegionClosure* evac_failure_cl) {
 168     _evac_failure_cl = evac_failure_cl;
 169   }
 170   OopsInHeapRegionClosure* evac_failure_closure() {
 171     return _evac_failure_cl;
 172   }
 173 
 174   int* hash_seed() { return &_hash_seed; }
 175   uint queue_num() { return _queue_num; }
 176 
 177   size_t term_attempts() const  { return _term_attempts; }
 178   void note_term_attempt() { _term_attempts++; }
 179 
 180   void start_strong_roots() {
 181     _start_strong_roots = os::elapsedTime();
 182   }
 183   void end_strong_roots() {
 184     _strong_roots_time += (os::elapsedTime() - _start_strong_roots);
 185   }
 186   double strong_roots_time() const { return _strong_roots_time; }
 187 
 188   void start_term_time() {
 189     note_term_attempt();
 190     _start_term = os::elapsedTime();
 191   }
 192   void end_term_time() {
 193     _term_time += (os::elapsedTime() - _start_term);
 194   }
 195   double term_time() const { return _term_time; }
 196 
 197   double elapsed_time() const {
 198     return os::elapsedTime() - _start;
 199   }
 200 
 201   static void
 202     print_termination_stats_hdr(outputStream* const st = gclog_or_tty);
 203   void
 204     print_termination_stats(int i, outputStream* const st = gclog_or_tty) const;
 205 
 206   size_t* surviving_young_words() {
 207     // We add on to hide entry 0 which accumulates surviving words for
 208     // age -1 regions (i.e. non-young ones)
 209     return _surviving_young_words;
 210   }
 211 
 212  private:
 213   void retire_alloc_buffers() {
 214     for (int ap = 0; ap < GCAllocPurposeCount; ++ap) {
 215       size_t waste = _alloc_buffers[ap]->words_remaining();
 216       add_to_alloc_buffer_waste(waste);
 217       _alloc_buffers[ap]->flush_stats_and_retire(_g1h->stats_for_purpose((GCAllocPurpose)ap),
 218                                                  true /* end_of_gc */,
 219                                                  false /* retain */);
 220     }
 221   }
 222 
 223   #define G1_PARTIAL_ARRAY_MASK 0x2
 224 
 225   inline bool has_partial_array_mask(oop* ref) const {
 226     return ((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) == G1_PARTIAL_ARRAY_MASK;
 227   }
 228 
 229   // We never encode partial array oops as narrowOop*, so return false immediately.
 230   // This allows the compiler to create optimized code when popping references from
 231   // the work queue.
 232   inline bool has_partial_array_mask(narrowOop* ref) const {
 233     assert(((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) != G1_PARTIAL_ARRAY_MASK, "Partial array oop reference encoded as narrowOop*");
 234     return false;
 235   }
 236 
 237   // Only implement set_partial_array_mask() for regular oops, not for narrowOops.
 238   // We always encode partial arrays as regular oop, to allow the
 239   // specialization for has_partial_array_mask() for narrowOops above.
 240   // This means that unintentional use of this method with narrowOops are caught
 241   // by the compiler.
 242   inline oop* set_partial_array_mask(oop obj) const {
 243     assert(((uintptr_t)(void *)obj & G1_PARTIAL_ARRAY_MASK) == 0, "Information loss!");
 244     return (oop*) ((uintptr_t)(void *)obj | G1_PARTIAL_ARRAY_MASK);
 245   }
 246 
 247   inline oop clear_partial_array_mask(oop* ref) const {
 248     return cast_to_oop((intptr_t)ref & ~G1_PARTIAL_ARRAY_MASK);
 249   }
 250 
 251   inline void do_oop_partial_array(oop* p);
 252 
 253   // This method is applied to the fields of the objects that have just been copied.
 254   template <class T> void do_oop_evac(T* p, HeapRegion* from) {
 255     assert(!oopDesc::is_null(oopDesc::load_decode_heap_oop(p)),
 256            "Reference should not be NULL here as such are never pushed to the task queue.");
 257     oop obj = oopDesc::load_decode_heap_oop_not_null(p);
 258 
 259     // Although we never intentionally push references outside of the collection
 260     // set, due to (benign) races in the claim mechanism during RSet scanning more
 261     // than one thread might claim the same card. So the same card may be
 262     // processed multiple times. So redo this check.
 263     if (_g1h->in_cset_fast_test(obj)) {
 264       oop forwardee;
 265       if (obj->is_forwarded()) {
 266         forwardee = obj->forwardee();
 267       } else {
 268         forwardee = copy_to_survivor_space(obj);
 269       }
 270       assert(forwardee != NULL, "forwardee should not be NULL");
 271       oopDesc::encode_store_heap_oop(p, forwardee);
 272     }
 273 
 274     assert(obj != NULL, "Must be");
 275     update_rs(from, p, queue_num());
 276   }
 277 public:
 278 
 279   oop copy_to_survivor_space(oop const obj);
 280 
 281   template <class T> inline void deal_with_reference(T* ref_to_scan);
 282 
 283   inline void deal_with_reference(StarTask ref);
 284 
 285 public:
 286   void trim_queue();
 287 };
 288 
 289 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1PARSCANTHREADSTATE_HPP