1 /*
   2  * Copyright (c) 2001, 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_G1OOPCLOSURES_INLINE_HPP
  26 #define SHARE_VM_GC_G1_G1OOPCLOSURES_INLINE_HPP
  27 
  28 #include "gc/g1/g1CollectedHeap.hpp"
  29 #include "gc/g1/g1ConcurrentMark.inline.hpp"
  30 #include "gc/g1/g1OopClosures.hpp"
  31 #include "gc/g1/g1ParScanThreadState.inline.hpp"
  32 #include "gc/g1/g1RemSet.hpp"
  33 #include "gc/g1/heapRegion.inline.hpp"
  34 #include "gc/g1/heapRegionRemSet.hpp"
  35 #include "memory/iterator.inline.hpp"
  36 #include "oops/access.inline.hpp"
  37 #include "oops/compressedOops.inline.hpp"
  38 #include "oops/oopsHierarchy.hpp"
  39 #include "oops/oop.inline.hpp"
  40 #include "runtime/prefetch.inline.hpp"
  41 
  42 template <class T>
  43 inline void G1ScanClosureBase::prefetch_and_push(T* p, const oop obj) {
  44   // We're not going to even bother checking whether the object is
  45   // already forwarded or not, as this usually causes an immediate
  46   // stall. We'll try to prefetch the object (for write, given that
  47   // we might need to install the forwarding reference) and we'll
  48   // get back to it when pop it from the queue
  49   Prefetch::write(obj->mark_addr_raw(), 0);
  50   Prefetch::read(obj->mark_addr_raw(), (HeapWordSize*2));
  51 
  52   // slightly paranoid test; I'm trying to catch potential
  53   // problems before we go into push_on_queue to know where the
  54   // problem is coming from
  55   assert((obj == RawAccess<>::oop_load(p)) ||
  56          (obj->is_forwarded() &&
  57          obj->forwardee() == RawAccess<>::oop_load(p)),
  58          "p should still be pointing to obj or to its forwardee");
  59 
  60   _par_scan_state->push_on_queue(p);
  61 }
  62 
  63 template <class T>
  64 inline void G1ScanClosureBase::handle_non_cset_obj_common(InCSetState const state, T* p, oop const obj) {
  65   if (state.is_humongous()) {
  66     _g1h->set_humongous_is_live(obj);
  67   }
  68 }
  69 
  70 inline void G1ScanClosureBase::trim_queue_partially() {
  71   _par_scan_state->trim_queue_partially();
  72 }
  73 
  74 template <class T>
  75 inline void G1ScanEvacuatedObjClosure::do_oop_work(T* p) {
  76   T heap_oop = RawAccess<>::oop_load(p);
  77 
  78   if (CompressedOops::is_null(heap_oop)) {
  79     return;
  80   }
  81   oop obj = CompressedOops::decode_not_null(heap_oop);
  82   const InCSetState state = _g1h->in_cset_state(obj);
  83   if (state.is_in_cset()) {
  84     prefetch_and_push(p, obj);
  85   } else if (!HeapRegion::is_in_same_region(p, obj)) {
  86     handle_non_cset_obj_common(state, p, obj);
  87     if (_scanning_in_young) {
  88       return;
  89     }
  90     _par_scan_state->enqueue_card_if_tracked(p, obj);
  91   }
  92 }
  93 
  94 template <class T>
  95 inline void G1CMOopClosure::do_oop_work(T* p) {
  96   _task->deal_with_reference(p);
  97 }
  98 
  99 template <class T>
 100 inline void G1RootRegionScanClosure::do_oop_work(T* p) {
 101   T heap_oop = RawAccess<MO_VOLATILE>::oop_load(p);
 102   if (CompressedOops::is_null(heap_oop)) {
 103     return;
 104   }
 105   oop obj = CompressedOops::decode_not_null(heap_oop);
 106   _cm->mark_in_next_bitmap(_worker_id, obj);
 107 }
 108 
 109 template <class T>
 110 inline static void check_obj_during_refinement(T* p, oop const obj) {
 111 #ifdef ASSERT
 112   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 113   // can't do because of races
 114   // assert(oopDesc::is_oop_or_null(obj), "expected an oop");
 115   assert(check_obj_alignment(obj), "not oop aligned");
 116   assert(g1h->is_in_reserved(obj), "must be in heap");
 117 
 118   HeapRegion* from = g1h->heap_region_containing(p);
 119 
 120   assert(from != NULL, "from region must be non-NULL");
 121   assert(from->is_in_reserved(p) ||
 122          (from->is_humongous() &&
 123           g1h->heap_region_containing(p)->is_humongous() &&
 124           from->humongous_start_region() == g1h->heap_region_containing(p)->humongous_start_region()),
 125          "p " PTR_FORMAT " is not in the same region %u or part of the correct humongous object starting at region %u.",
 126          p2i(p), from->hrm_index(), from->humongous_start_region()->hrm_index());
 127 #endif // ASSERT
 128 }
 129 
 130 template <class T>
 131 inline void G1ConcurrentRefineOopClosure::do_oop_work(T* p) {
 132   T o = RawAccess<MO_VOLATILE>::oop_load(p);
 133   if (CompressedOops::is_null(o)) {
 134     return;
 135   }
 136   oop obj = CompressedOops::decode_not_null(o);
 137 
 138   check_obj_during_refinement(p, obj);
 139 
 140   if (HeapRegion::is_in_same_region(p, obj)) {
 141     // Normally this closure should only be called with cross-region references.
 142     // But since Java threads are manipulating the references concurrently and we
 143     // reload the values things may have changed.
 144     // Also this check lets slip through references from a humongous continues region
 145     // to its humongous start region, as they are in different regions, and adds a
 146     // remembered set entry. This is benign (apart from memory usage), as we never
 147     // try to either evacuate or eager reclaim humonguous arrays of j.l.O.
 148     return;
 149   }
 150 
 151   HeapRegionRemSet* to_rem_set = _g1h->heap_region_containing(obj)->rem_set();
 152 
 153   assert(to_rem_set != NULL, "Need per-region 'into' remsets.");
 154   if (to_rem_set->is_tracked()) {
 155     to_rem_set->add_reference(p, _worker_i);
 156   }
 157 }
 158 
 159 template <class T>
 160 inline void G1ScanObjsDuringUpdateRSClosure::do_oop_work(T* p) {
 161   T o = RawAccess<>::oop_load(p);
 162   if (CompressedOops::is_null(o)) {
 163     return;
 164   }
 165   oop obj = CompressedOops::decode_not_null(o);
 166 
 167   check_obj_during_refinement(p, obj);
 168 
 169   assert(!_g1h->is_in_cset((HeapWord*)p), "Oop originates from " PTR_FORMAT " (region: %u) which is in the collection set.", p2i(p), _g1h->addr_to_region((HeapWord*)p));
 170   const InCSetState state = _g1h->in_cset_state(obj);
 171   if (state.is_in_cset()) {
 172     // Since the source is always from outside the collection set, here we implicitly know
 173     // that this is a cross-region reference too.
 174     prefetch_and_push(p, obj);
 175   } else if (!HeapRegion::is_in_same_region(p, obj)) {
 176     handle_non_cset_obj_common(state, p, obj);
 177     _par_scan_state->enqueue_card_if_tracked(p, obj);
 178   }
 179 }
 180 
 181 template <class T>
 182 inline void G1ScanObjsDuringScanRSClosure::do_oop_work(T* p) {
 183   T heap_oop = RawAccess<>::oop_load(p);
 184   if (CompressedOops::is_null(heap_oop)) {
 185     return;
 186   }
 187   oop obj = CompressedOops::decode_not_null(heap_oop);
 188 
 189   const InCSetState state = _g1h->in_cset_state(obj);
 190   if (state.is_in_cset()) {
 191     prefetch_and_push(p, obj);
 192   } else if (!HeapRegion::is_in_same_region(p, obj)) {
 193     handle_non_cset_obj_common(state, p, obj);
 194   }
 195 }
 196 
 197 void G1ParCopyHelper::do_cld_barrier(oop new_obj) {
 198   if (_g1h->heap_region_containing(new_obj)->is_young()) {
 199     _scanned_cld->record_modified_oops();
 200   }
 201 }
 202 
 203 void G1ParCopyHelper::mark_object(oop obj) {
 204   assert(!_g1h->heap_region_containing(obj)->in_collection_set(), "should not mark objects in the CSet");
 205 
 206   // We know that the object is not moving so it's safe to read its size.
 207   _cm->mark_in_next_bitmap(_worker_id, obj);
 208 }
 209 
 210 void G1ParCopyHelper::mark_forwarded_object(oop from_obj, oop to_obj) {
 211   assert(from_obj->is_forwarded(), "from obj should be forwarded");
 212   assert(from_obj->forwardee() == to_obj, "to obj should be the forwardee");
 213   assert(from_obj != to_obj, "should not be self-forwarded");
 214 
 215   assert(_g1h->heap_region_containing(from_obj)->in_collection_set(), "from obj should be in the CSet");
 216   assert(!_g1h->heap_region_containing(to_obj)->in_collection_set(), "should not mark objects in the CSet");
 217 
 218   // The object might be in the process of being copied by another
 219   // worker so we cannot trust that its to-space image is
 220   // well-formed. So we have to read its size from its from-space
 221   // image which we know should not be changing.
 222   _cm->mark_in_next_bitmap(_worker_id, to_obj, from_obj->size());
 223 }
 224 
 225 void G1ParCopyHelper::trim_queue_partially() {
 226   _par_scan_state->trim_queue_partially();
 227 }
 228 
 229 template <G1Barrier barrier, G1Mark do_mark_object>
 230 template <class T>
 231 void G1ParCopyClosure<barrier, do_mark_object>::do_oop_work(T* p) {
 232   T heap_oop = RawAccess<>::oop_load(p);
 233 
 234   if (CompressedOops::is_null(heap_oop)) {
 235     return;
 236   }
 237 
 238   oop obj = CompressedOops::decode_not_null(heap_oop);
 239 
 240   assert(_worker_id == _par_scan_state->worker_id(), "sanity");
 241 
 242   const InCSetState state = _g1h->in_cset_state(obj);
 243   if (state.is_in_cset()) {
 244     oop forwardee;
 245     markOop m = obj->mark_raw();
 246     if (m->is_marked()) {
 247       forwardee = (oop) m->decode_pointer();
 248     } else {
 249       forwardee = _par_scan_state->copy_to_survivor_space(state, obj, m);
 250     }
 251     assert(forwardee != NULL, "forwardee should not be NULL");
 252     RawAccess<IS_NOT_NULL>::oop_store(p, forwardee);
 253     if (do_mark_object != G1MarkNone && forwardee != obj) {
 254       // If the object is self-forwarded we don't need to explicitly
 255       // mark it, the evacuation failure protocol will do so.
 256       mark_forwarded_object(obj, forwardee);
 257     }
 258 
 259     if (barrier == G1BarrierCLD) {
 260       do_cld_barrier(forwardee);
 261     }
 262   } else {
 263     if (state.is_humongous()) {
 264       _g1h->set_humongous_is_live(obj);
 265     }
 266 
 267     // The object is not in collection set. If we're a root scanning
 268     // closure during an initial mark pause then attempt to mark the object.
 269     if (do_mark_object == G1MarkFromRoot) {
 270       mark_object(obj);
 271     }
 272   }
 273   trim_queue_partially();
 274 }
 275 
 276 template <class T> void G1RebuildRemSetClosure::do_oop_work(T* p) {
 277   oop const obj = RawAccess<MO_VOLATILE>::oop_load(p);
 278   if (obj == NULL) {
 279     return;
 280   }
 281 
 282   if (HeapRegion::is_in_same_region(p, obj)) {
 283     return;
 284   }
 285 
 286   HeapRegion* to = _g1h->heap_region_containing(obj);
 287   HeapRegionRemSet* rem_set = to->rem_set();
 288   rem_set->add_reference(p, _worker_id);
 289 }
 290 
 291 #endif // SHARE_VM_GC_G1_G1OOPCLOSURES_INLINE_HPP