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