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_INLINE_HPP
  26 #define SHARE_VM_GC_G1_G1PARSCANTHREADSTATE_INLINE_HPP
  27 
  28 #include "gc/g1/g1CollectedHeap.inline.hpp"
  29 #include "gc/g1/g1ParScanThreadState.hpp"
  30 #include "gc/g1/g1RemSet.hpp"
  31 #include "oops/access.inline.hpp"
  32 #include "oops/oop.inline.hpp"
  33 
  34 template <class T> void G1ParScanThreadState::do_oop_evac(T* p) {
  35   // Reference should not be NULL here as such are never pushed to the task queue.
  36   oop obj = RawAccess<IS_NOT_NULL>::oop_load(p);
  37 
  38   // Although we never intentionally push references outside of the collection
  39   // set, due to (benign) races in the claim mechanism during RSet scanning more
  40   // than one thread might claim the same card. So the same card may be
  41   // processed multiple times, and so we might get references into old gen here.
  42   // So we need to redo this check.
  43   const InCSetState in_cset_state = _g1h->in_cset_state(obj);
  44   // References pushed onto the work stack should never point to a humongous region
  45   // as they are not added to the collection set due to above precondition.
  46   assert(!in_cset_state.is_humongous(),
  47          "Obj " PTR_FORMAT " should not refer to humongous region %u from " PTR_FORMAT,
  48          p2i(obj), _g1h->addr_to_region((HeapWord*)obj), p2i(p));
  49 
  50   if (!in_cset_state.is_in_cset()) {
  51     // In this case somebody else already did all the work.
  52     return;
  53   }
  54 
  55   markOop m = obj->mark_raw();
  56   if (m->is_marked()) {
  57     obj = (oop) m->decode_pointer();
  58   } else {
  59     obj = copy_to_survivor_space(in_cset_state, obj, m);
  60   }
  61   RawAccess<IS_NOT_NULL>::oop_store(p, obj);
  62 
  63   assert(obj != NULL, "Must be");
  64   if (!HeapRegion::is_in_same_region(p, obj)) {
  65     HeapRegion* from = _g1h->heap_region_containing(p);
  66     update_rs(from, p, obj);
  67   }
  68 }
  69 
  70 template <class T> inline void G1ParScanThreadState::push_on_queue(T* ref) {
  71   assert(verify_ref(ref), "sanity");
  72   _refs->push(ref);
  73 }
  74 
  75 inline void G1ParScanThreadState::do_oop_partial_array(oop* p) {
  76   assert(has_partial_array_mask(p), "invariant");
  77   oop from_obj = clear_partial_array_mask(p);
  78 
  79   assert(_g1h->is_in_reserved(from_obj), "must be in heap.");
  80   assert(from_obj->is_objArray(), "must be obj array");
  81   objArrayOop from_obj_array = objArrayOop(from_obj);
  82   // The from-space object contains the real length.
  83   int length                 = from_obj_array->length();
  84 
  85   assert(from_obj->is_forwarded(), "must be forwarded");
  86   oop to_obj                 = from_obj->forwardee();
  87   assert(from_obj != to_obj, "should not be chunking self-forwarded objects");
  88   objArrayOop to_obj_array   = objArrayOop(to_obj);
  89   // We keep track of the next start index in the length field of the
  90   // to-space object.
  91   int next_index             = to_obj_array->length();
  92   assert(0 <= next_index && next_index < length,
  93          "invariant, next index: %d, length: %d", next_index, length);
  94 
  95   int start                  = next_index;
  96   int end                    = length;
  97   int remainder              = end - start;
  98   // We'll try not to push a range that's smaller than ParGCArrayScanChunk.
  99   if (remainder > 2 * ParGCArrayScanChunk) {
 100     end = start + ParGCArrayScanChunk;
 101     to_obj_array->set_length(end);
 102     // Push the remainder before we process the range in case another
 103     // worker has run out of things to do and can steal it.
 104     oop* from_obj_p = set_partial_array_mask(from_obj);
 105     push_on_queue(from_obj_p);
 106   } else {
 107     assert(length == end, "sanity");
 108     // We'll process the final range for this object. Restore the length
 109     // so that the heap remains parsable in case of evacuation failure.
 110     to_obj_array->set_length(end);
 111   }
 112   _scanner.set_region(_g1h->heap_region_containing(to_obj));
 113   // Process indexes [start,end). It will also process the header
 114   // along with the first chunk (i.e., the chunk with start == 0).
 115   // Note that at this point the length field of to_obj_array is not
 116   // correct given that we are using it to keep track of the next
 117   // start index. oop_iterate_range() (thankfully!) ignores the length
 118   // field and only relies on the start / end parameters.  It does
 119   // however return the size of the object which will be incorrect. So
 120   // we have to ignore it even if we wanted to use it.
 121   to_obj_array->oop_iterate_range(&_scanner, start, end);
 122 }
 123 
 124 inline void G1ParScanThreadState::deal_with_reference(oop* ref_to_scan) {
 125   if (!has_partial_array_mask(ref_to_scan)) {
 126     do_oop_evac(ref_to_scan);
 127   } else {
 128     do_oop_partial_array(ref_to_scan);
 129   }
 130 }
 131 
 132 inline void G1ParScanThreadState::deal_with_reference(narrowOop* ref_to_scan) {
 133   assert(!has_partial_array_mask(ref_to_scan), "NarrowOop* elements should never be partial arrays.");
 134   do_oop_evac(ref_to_scan);
 135 }
 136 
 137 inline void G1ParScanThreadState::dispatch_reference(StarTask ref) {
 138   assert(verify_task(ref), "sanity");
 139   if (ref.is_narrow()) {
 140     deal_with_reference((narrowOop*)ref);
 141   } else {
 142     deal_with_reference((oop*)ref);
 143   }
 144 }
 145 
 146 void G1ParScanThreadState::steal_and_trim_queue(RefToScanQueueSet *task_queues) {
 147   StarTask stolen_task;
 148   while (task_queues->steal(_worker_id, stolen_task)) {
 149     assert(verify_task(stolen_task), "sanity");
 150     dispatch_reference(stolen_task);
 151 
 152     // We've just processed a reference and we might have made
 153     // available new entries on the queues. So we have to make sure
 154     // we drain the queues as necessary.
 155     trim_queue();
 156   }
 157 }
 158 
 159 inline bool G1ParScanThreadState::needs_partial_trimming() const {
 160   return !_refs->overflow_empty() || _refs->size() > _stack_trim_upper_threshold;
 161 }
 162 
 163 inline bool G1ParScanThreadState::is_partially_trimmed() const {
 164   return _refs->overflow_empty() && _refs->size() <= _stack_trim_lower_threshold;
 165 }
 166 
 167 inline void G1ParScanThreadState::trim_queue_to_threshold(uint threshold) {
 168   StarTask ref;
 169   // Drain the overflow stack first, so other threads can potentially steal.
 170   while (_refs->pop_overflow(ref)) {
 171     if (!_refs->try_push_to_taskqueue(ref)) {
 172       dispatch_reference(ref);
 173     }
 174   }
 175 
 176   while (_refs->pop_local(ref, threshold)) {
 177     dispatch_reference(ref);
 178   }
 179 }
 180 
 181 inline void G1ParScanThreadState::trim_queue_partially() {
 182   if (!needs_partial_trimming()) {
 183     return;
 184   }
 185 
 186   const Ticks start = Ticks::now();
 187   do {
 188     trim_queue_to_threshold(_stack_trim_lower_threshold);
 189   } while (!is_partially_trimmed());
 190   _trim_ticks += Ticks::now() - start;
 191 }
 192 
 193 inline Tickspan G1ParScanThreadState::trim_ticks() const {
 194   return _trim_ticks;
 195 }
 196 
 197 inline void G1ParScanThreadState::reset_trim_ticks() {
 198   _trim_ticks = Tickspan();
 199 }
 200 
 201 #endif // SHARE_VM_GC_G1_G1PARSCANTHREADSTATE_INLINE_HPP