1 /*
   2  * Copyright (c) 2001, 2017, 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_G1CONCURRENTMARK_INLINE_HPP
  26 #define SHARE_VM_GC_G1_G1CONCURRENTMARK_INLINE_HPP
  27 
  28 #include "gc/g1/g1CollectedHeap.inline.hpp"
  29 #include "gc/g1/g1ConcurrentMark.hpp"
  30 #include "gc/g1/g1ConcurrentMarkBitMap.inline.hpp"
  31 #include "gc/g1/g1ConcurrentMarkObjArrayProcessor.inline.hpp"
  32 #include "gc/g1/suspendibleThreadSet.hpp"
  33 #include "gc/shared/taskqueue.inline.hpp"
  34 #include "utilities/bitMap.inline.hpp"
  35 
  36 inline bool G1ConcurrentMark::mark_in_next_bitmap(oop const obj) {
  37   HeapRegion* const hr = _g1h->heap_region_containing(obj);
  38   return mark_in_next_bitmap(hr, obj);
  39 }
  40 
  41 inline bool G1ConcurrentMark::mark_in_next_bitmap(HeapRegion* const hr, oop const obj) {
  42   assert(hr != NULL, "just checking");
  43   assert(hr->is_in_reserved(obj),
  44          "Attempting to mark object at " PTR_FORMAT " that is not contained in the given region %u",
  45          p2i(obj), hr->hrm_index());
  46 
  47   if (hr->obj_allocated_since_next_marking(obj)) {
  48     return false;
  49   }
  50 
  51   // Some callers may have stale objects to mark above nTAMS after humongous reclaim.
  52   assert(obj->is_oop(true /* ignore mark word */), "Address " PTR_FORMAT " to mark is not an oop", p2i(obj));
  53   assert(!hr->is_continues_humongous(),
  54          "Should not try to mark object " PTR_FORMAT " in Humongous continues region %u above nTAMS " PTR_FORMAT,
  55          p2i(obj), hr->hrm_index(), p2i(hr->next_top_at_mark_start()));
  56 
  57   HeapWord* const obj_addr = (HeapWord*)obj;
  58   return _nextMarkBitMap->par_mark(obj_addr);
  59 }
  60 
  61 #ifndef PRODUCT
  62 template<typename Fn>
  63 inline void G1CMMarkStack::iterate(Fn fn) const {
  64   assert_at_safepoint(true);
  65 
  66   size_t num_chunks = 0;
  67 
  68   TaskQueueEntryChunk* cur = _chunk_list;
  69   while (cur != NULL) {
  70     guarantee(num_chunks <= _chunks_in_chunk_list, "Found " SIZE_FORMAT " oop chunks which is more than there should be", num_chunks);
  71 
  72     for (size_t i = 0; i < EntriesPerChunk; ++i) {
  73       if (cur->data[i].is_null()) {
  74         break;
  75       }
  76       fn(cur->data[i]);
  77     }
  78     cur = cur->next;
  79     num_chunks++;
  80   }
  81 }
  82 #endif
  83 
  84 // It scans an object and visits its children.
  85 inline void G1CMTask::scan_task_entry(G1TaskQueueEntry task_entry) { process_grey_task_entry<true>(task_entry); }
  86 
  87 inline void G1CMTask::push(G1TaskQueueEntry task_entry) {
  88   assert(task_entry.is_array_slice() || _g1h->is_in_g1_reserved(task_entry.obj()), "invariant");
  89   assert(task_entry.is_array_slice() || !_g1h->is_on_master_free_list(
  90               _g1h->heap_region_containing(task_entry.obj())), "invariant");
  91   assert(task_entry.is_array_slice() || !_g1h->is_obj_ill(task_entry.obj()), "invariant");  // FIXME!!!
  92   assert(task_entry.is_array_slice() || _nextMarkBitMap->is_marked((HeapWord*)task_entry.obj()), "invariant");
  93 
  94   if (!_task_queue->push(task_entry)) {
  95     // The local task queue looks full. We need to push some entries
  96     // to the global stack.
  97     move_entries_to_global_stack();
  98 
  99     // this should succeed since, even if we overflow the global
 100     // stack, we should have definitely removed some entries from the
 101     // local queue. So, there must be space on it.
 102     bool success = _task_queue->push(task_entry);
 103     assert(success, "invariant");
 104   }
 105 }
 106 
 107 inline bool G1CMTask::is_below_finger(oop obj, HeapWord* global_finger) const {
 108   // If obj is above the global finger, then the mark bitmap scan
 109   // will find it later, and no push is needed.  Similarly, if we have
 110   // a current region and obj is between the local finger and the
 111   // end of the current region, then no push is needed.  The tradeoff
 112   // of checking both vs only checking the global finger is that the
 113   // local check will be more accurate and so result in fewer pushes,
 114   // but may also be a little slower.
 115   HeapWord* objAddr = (HeapWord*)obj;
 116   if (_finger != NULL) {
 117     // We have a current region.
 118 
 119     // Finger and region values are all NULL or all non-NULL.  We
 120     // use _finger to check since we immediately use its value.
 121     assert(_curr_region != NULL, "invariant");
 122     assert(_region_limit != NULL, "invariant");
 123     assert(_region_limit <= global_finger, "invariant");
 124 
 125     // True if obj is less than the local finger, or is between
 126     // the region limit and the global finger.
 127     if (objAddr < _finger) {
 128       return true;
 129     } else if (objAddr < _region_limit) {
 130       return false;
 131     } // Else check global finger.
 132   }
 133   // Check global finger.
 134   return objAddr < global_finger;
 135 }
 136 
 137 template<bool scan>
 138 inline void G1CMTask::process_grey_task_entry(G1TaskQueueEntry task_entry) {
 139   assert(scan || (task_entry.is_oop() && task_entry.obj()->is_typeArray()), "Skipping scan of grey non-typeArray");
 140   assert(task_entry.is_array_slice() || _nextMarkBitMap->is_marked((HeapWord*)task_entry.obj()),
 141          "Any stolen object should be a slice or marked");
 142 
 143   if (scan) {
 144     if (task_entry.is_array_slice()) {
 145       _words_scanned += _objArray_processor.process_slice(task_entry.slice());
 146     } else {
 147       oop obj = task_entry.obj();
 148       if (G1CMObjArrayProcessor::should_be_sliced(obj)) {
 149         _words_scanned += _objArray_processor.process_obj(obj);
 150       } else {
 151         _words_scanned += obj->oop_iterate_size(_cm_oop_closure);;
 152       }
 153     }
 154   }
 155   check_limits();
 156 }
 157 
 158 inline size_t G1CMTask::scan_objArray(objArrayOop obj, MemRegion mr) {
 159   obj->oop_iterate(_cm_oop_closure, mr);
 160   return mr.word_size();
 161 }
 162 
 163 inline void G1CMTask::make_reference_grey(oop obj) {
 164   if (!_cm->mark_in_next_bitmap(obj)) {
 165     return;
 166   }
 167 
 168   // No OrderAccess:store_load() is needed. It is implicit in the
 169   // CAS done in G1CMBitMap::parMark() call in the routine above.
 170   HeapWord* global_finger = _cm->finger();
 171 
 172   // We only need to push a newly grey object on the mark
 173   // stack if it is in a section of memory the mark bitmap
 174   // scan has already examined.  Mark bitmap scanning
 175   // maintains progress "fingers" for determining that.
 176   //
 177   // Notice that the global finger might be moving forward
 178   // concurrently. This is not a problem. In the worst case, we
 179   // mark the object while it is above the global finger and, by
 180   // the time we read the global finger, it has moved forward
 181   // past this object. In this case, the object will probably
 182   // be visited when a task is scanning the region and will also
 183   // be pushed on the stack. So, some duplicate work, but no
 184   // correctness problems.
 185   if (is_below_finger(obj, global_finger)) {
 186     G1TaskQueueEntry entry = G1TaskQueueEntry::from_oop(obj);
 187     if (obj->is_typeArray()) {
 188       // Immediately process arrays of primitive types, rather
 189       // than pushing on the mark stack.  This keeps us from
 190       // adding humongous objects to the mark stack that might
 191       // be reclaimed before the entry is processed - see
 192       // selection of candidates for eager reclaim of humongous
 193       // objects.  The cost of the additional type test is
 194       // mitigated by avoiding a trip through the mark stack,
 195       // by only doing a bookkeeping update and avoiding the
 196       // actual scan of the object - a typeArray contains no
 197       // references, and the metadata is built-in.
 198       process_grey_task_entry<false>(entry);
 199     } else {
 200       push(entry);
 201     }
 202   }
 203 }
 204 
 205 inline void G1CMTask::deal_with_reference(oop obj) {
 206   increment_refs_reached();
 207   if (obj == NULL) {
 208     return;
 209   }
 210   make_reference_grey(obj);
 211 }
 212 
 213 inline void G1ConcurrentMark::markPrev(oop p) {
 214   assert(!_prevMarkBitMap->is_marked((HeapWord*) p), "sanity");
 215  _prevMarkBitMap->mark((HeapWord*) p);
 216 }
 217 
 218 bool G1ConcurrentMark::isPrevMarked(oop p) const {
 219   assert(p != NULL && p->is_oop(), "expected an oop");
 220   return _prevMarkBitMap->is_marked((HeapWord*)p);
 221 }
 222 
 223 inline bool G1ConcurrentMark::do_yield_check() {
 224   if (SuspendibleThreadSet::should_yield()) {
 225     SuspendibleThreadSet::yield();
 226     return true;
 227   } else {
 228     return false;
 229   }
 230 }
 231 
 232 #endif // SHARE_VM_GC_G1_G1CONCURRENTMARK_INLINE_HPP