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