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