1 /*
   2  * Copyright (c) 2001, 2019, 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_GC_G1_G1OOPCLOSURES_INLINE_HPP
  26 #define SHARE_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(G1HeapRegionAttr const region_attr, T* p, oop const obj) {
  65   if (region_attr.is_humongous()) {
  66     _g1h->set_humongous_is_live(obj);
  67   } else if (region_attr.is_optional()) {
  68     _par_scan_state->remember_reference_into_optional_region(p);
  69   }
  70 }
  71 
  72 inline void G1ScanClosureBase::trim_queue_partially() {
  73   _par_scan_state->trim_queue_partially();
  74 }
  75 
  76 template <class T>
  77 inline void G1ScanEvacuatedObjClosure::do_oop_work(T* p) {
  78   T heap_oop = RawAccess<>::oop_load(p);
  79 
  80   if (CompressedOops::is_null(heap_oop)) {
  81     return;
  82   }
  83   oop obj = CompressedOops::decode_not_null(heap_oop);
  84   const G1HeapRegionAttr region_attr = _g1h->region_attr(obj);
  85   if (region_attr.is_in_cset()) {
  86     prefetch_and_push(p, obj);
  87   } else if (!HeapRegion::is_in_same_region(p, obj)) {
  88     handle_non_cset_obj_common(region_attr, p, obj);
  89     assert(_scanning_in_young != Uninitialized, "Scan location has not been initialized.");
  90     if (_scanning_in_young == True) {
  91       return;
  92     }
  93     _par_scan_state->enqueue_card_if_tracked(region_attr, p, obj);
  94   }
  95 }
  96 
  97 template <class T>
  98 inline void G1CMOopClosure::do_oop_work(T* p) {
  99   _task->deal_with_reference(p);
 100 }
 101 
 102 template <class T>
 103 inline void G1RootRegionScanClosure::do_oop_work(T* p) {
 104   T heap_oop = RawAccess<MO_VOLATILE>::oop_load(p);
 105   if (CompressedOops::is_null(heap_oop)) {
 106     return;
 107   }
 108   oop obj = CompressedOops::decode_not_null(heap_oop);
 109   _cm->mark_in_next_bitmap(_worker_id, obj);
 110 }
 111 
 112 template <class T>
 113 inline static void check_obj_during_refinement(T* p, oop const obj) {
 114 #ifdef ASSERT
 115   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 116   // can't do because of races
 117   // assert(oopDesc::is_oop_or_null(obj), "expected an oop");
 118   g1h->check_oop_location(obj);
 119 
 120   HeapRegion* from = g1h->heap_region_containing(p);
 121 
 122   assert(from != NULL, "from region must be non-NULL");
 123   assert(from->is_in_reserved(p) ||
 124          (from->is_humongous() &&
 125           g1h->heap_region_containing(p)->is_humongous() &&
 126           from->humongous_start_region() == g1h->heap_region_containing(p)->humongous_start_region()),
 127          "p " PTR_FORMAT " is not in the same region %u or part of the correct humongous object starting at region %u.",
 128          p2i(p), from->hrm_index(), from->humongous_start_region()->hrm_index());
 129 #endif // ASSERT
 130 }
 131 
 132 template <class T>
 133 inline void G1ConcurrentRefineOopClosure::do_oop_work(T* p) {
 134   T o = RawAccess<MO_VOLATILE>::oop_load(p);
 135   if (CompressedOops::is_null(o)) {
 136     return;
 137   }
 138   oop obj = CompressedOops::decode_not_null(o);
 139 
 140   check_obj_during_refinement(p, obj);
 141 
 142   if (HeapRegion::is_in_same_region(p, obj)) {
 143     // Normally this closure should only be called with cross-region references.
 144     // But since Java threads are manipulating the references concurrently and we
 145     // reload the values things may have changed.
 146     // Also this check lets slip through references from a humongous continues region
 147     // to its humongous start region, as they are in different regions, and adds a
 148     // remembered set entry. This is benign (apart from memory usage), as we never
 149     // try to either evacuate or eager reclaim humonguous arrays of j.l.O.
 150     return;
 151   }
 152 
 153   HeapRegionRemSet* to_rem_set = _g1h->heap_region_containing(obj)->rem_set();
 154 
 155   assert(to_rem_set != NULL, "Need per-region 'into' remsets.");
 156   if (to_rem_set->is_tracked()) {
 157     to_rem_set->add_reference(p, _worker_id);
 158   }
 159 }
 160 
 161 template <class T>
 162 inline void G1ScanCardClosure::do_oop_work(T* p) {
 163   T o = RawAccess<>::oop_load(p);
 164   if (CompressedOops::is_null(o)) {
 165     return;
 166   }
 167   oop obj = CompressedOops::decode_not_null(o);
 168 
 169   check_obj_during_refinement(p, obj);
 170 
 171   assert(!_g1h->is_in_cset((HeapWord*)p),
 172          "Oop originates from " PTR_FORMAT " (region: %u) which is in the collection set.",
 173          p2i(p), _g1h->addr_to_region((HeapWord*)p));
 174 
 175   const G1HeapRegionAttr region_attr = _g1h->region_attr(obj);
 176   if (region_attr.is_in_cset()) {
 177     // Since the source is always from outside the collection set, here we implicitly know
 178     // that this is a cross-region reference too.
 179     prefetch_and_push(p, obj);
 180   } else if (!HeapRegion::is_in_same_region(p, obj)) {
 181     handle_non_cset_obj_common(region_attr, p, obj);
 182     _par_scan_state->enqueue_card_if_tracked(region_attr, p, obj);
 183   }
 184 }
 185 
 186 template <class T>
 187 inline void G1ScanRSForOptionalClosure::do_oop_work(T* p) {
 188   const G1HeapRegionAttr region_attr = _g1h->region_attr(p);
 189   // Entries in the optional collection set may start to originate from the collection
 190   // set after one or more increments. In this case, previously optional regions
 191   // became actual collection set regions. Filter them out here.
 192   if (region_attr.is_in_cset()) {
 193     return;
 194   }
 195   _scan_cl->do_oop_work(p);
 196   _scan_cl->trim_queue_partially();
 197 }
 198 
 199 void G1ParCopyHelper::do_cld_barrier(oop new_obj) {
 200   if (_g1h->heap_region_containing(new_obj)->is_young()) {
 201     _scanned_cld->record_modified_oops();
 202   }
 203 }
 204 
 205 void G1ParCopyHelper::mark_object(oop obj) {
 206   assert(!_g1h->heap_region_containing(obj)->in_collection_set(), "should not mark objects in the CSet");
 207 
 208   // We know that the object is not moving so it's safe to read its size.
 209   _cm->mark_in_next_bitmap(_worker_id, obj);
 210 }
 211 
 212 void G1ParCopyHelper::trim_queue_partially() {
 213   _par_scan_state->trim_queue_partially();
 214 }
 215 
 216 template <G1Barrier barrier, G1Mark do_mark_object>
 217 template <class T>
 218 void G1ParCopyClosure<barrier, do_mark_object>::do_oop_work(T* p) {
 219   T heap_oop = RawAccess<>::oop_load(p);
 220 
 221   if (CompressedOops::is_null(heap_oop)) {
 222     return;
 223   }
 224 
 225   oop obj = CompressedOops::decode_not_null(heap_oop);
 226 
 227   assert(_worker_id == _par_scan_state->worker_id(), "sanity");
 228 
 229   const G1HeapRegionAttr state = _g1h->region_attr(obj);
 230   if (state.is_in_cset()) {
 231     oop forwardee;
 232     markWord m = obj->mark_raw();
 233     if (m.is_marked()) {
 234       forwardee = (oop) m.decode_pointer();
 235     } else {
 236       forwardee = _par_scan_state->copy_to_survivor_space(state, obj, m);
 237     }
 238     assert(forwardee != NULL, "forwardee should not be NULL");
 239     RawAccess<IS_NOT_NULL>::oop_store(p, forwardee);
 240 
 241     if (barrier == G1BarrierCLD) {
 242       do_cld_barrier(forwardee);
 243     }
 244   } else {
 245     if (state.is_humongous()) {
 246       _g1h->set_humongous_is_live(obj);
 247     } else if (state.is_optional()) {
 248       _par_scan_state->remember_root_into_optional_region(p);
 249     }
 250 
 251     // The object is not in collection set. If we're a root scanning
 252     // closure during an initial mark pause then attempt to mark the object.
 253     if (do_mark_object == G1MarkFromRoot) {
 254       mark_object(obj);
 255     }
 256   }
 257   trim_queue_partially();
 258 }
 259 
 260 template <class T> void G1RebuildRemSetClosure::do_oop_work(T* p) {
 261   oop const obj = RawAccess<MO_VOLATILE>::oop_load(p);
 262   if (obj == NULL) {
 263     return;
 264   }
 265 
 266   if (HeapRegion::is_in_same_region(p, obj)) {
 267     return;
 268   }
 269 
 270   HeapRegion* to = _g1h->heap_region_containing(obj);
 271   HeapRegionRemSet* rem_set = to->rem_set();
 272   rem_set->add_reference(p, _worker_id);
 273 }
 274 
 275 #endif // SHARE_GC_G1_G1OOPCLOSURES_INLINE_HPP