1 /*
   2  * Copyright (c) 2001, 2016, 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/shared/taskqueue.inline.hpp"
  31 
  32 inline BitMap::idx_t G1ConcurrentMark::card_live_bitmap_index_for(HeapWord* addr) {
  33   // Below, the term "card num" means the result of shifting an address
  34   // by the card shift -- address 0 corresponds to card number 0.  One
  35   // must subtract the card num of the bottom of the heap to obtain a
  36   // card table index.
  37   intptr_t card_num = intptr_t(uintptr_t(addr) >> CardTableModRefBS::card_shift);
  38   return card_num - heap_bottom_card_num();
  39 }
  40 
  41 inline bool G1ConcurrentMark::par_mark(oop obj) {
  42   return _nextMarkBitMap->parMark((HeapWord*)obj);
  43 }
  44 
  45 inline bool G1CMBitMapRO::iterate(BitMapClosure* cl, MemRegion mr) {
  46   HeapWord* start_addr = MAX2(startWord(), mr.start());
  47   HeapWord* end_addr = MIN2(endWord(), mr.end());
  48 
  49   if (end_addr > start_addr) {
  50     // Right-open interval [start-offset, end-offset).
  51     BitMap::idx_t start_offset = heapWordToOffset(start_addr);
  52     BitMap::idx_t end_offset = heapWordToOffset(end_addr);
  53 
  54     start_offset = _bm.get_next_one_offset(start_offset, end_offset);
  55     while (start_offset < end_offset) {
  56       if (!cl->do_bit(start_offset)) {
  57         return false;
  58       }
  59       HeapWord* next_addr = MIN2(nextObject(offsetToHeapWord(start_offset)), end_addr);
  60       BitMap::idx_t next_offset = heapWordToOffset(next_addr);
  61       start_offset = _bm.get_next_one_offset(next_offset, end_offset);
  62     }
  63   }
  64   return true;
  65 }
  66 
  67 // The argument addr should be the start address of a valid object
  68 HeapWord* G1CMBitMapRO::nextObject(HeapWord* addr) {
  69   oop obj = (oop) addr;
  70   HeapWord* res =  addr + obj->size();
  71   assert(offsetToHeapWord(heapWordToOffset(res)) == res, "sanity");
  72   return res;
  73 }
  74 
  75 #define check_mark(addr)                                                       \
  76   assert(_bmStartWord <= (addr) && (addr) < (_bmStartWord + _bmWordSize),      \
  77          "outside underlying space?");                                         \
  78   assert(G1CollectedHeap::heap()->is_in_exact(addr),                           \
  79          "Trying to access not available bitmap " PTR_FORMAT                   \
  80          " corresponding to " PTR_FORMAT " (%u)",                              \
  81          p2i(this), p2i(addr), G1CollectedHeap::heap()->addr_to_region(addr));
  82 
  83 inline void G1CMBitMap::mark(HeapWord* addr) {
  84   check_mark(addr);
  85   _bm.set_bit(heapWordToOffset(addr));
  86 }
  87 
  88 inline void G1CMBitMap::clear(HeapWord* addr) {
  89   check_mark(addr);
  90   _bm.clear_bit(heapWordToOffset(addr));
  91 }
  92 
  93 inline bool G1CMBitMap::parMark(HeapWord* addr) {
  94   check_mark(addr);
  95   return _bm.par_set_bit(heapWordToOffset(addr));
  96 }
  97 
  98 #undef check_mark
  99 
 100 template<typename Fn>
 101 inline void G1CMMarkStack::iterate(Fn fn) {
 102   assert(_saved_index == _index, "saved index: %d index: %d", _saved_index, _index);
 103   for (int i = 0; i < _index; ++i) {
 104     fn(_base[i]);
 105   }
 106 }
 107 
 108 // It scans an object and visits its children.
 109 inline void G1CMTask::scan_object(oop obj) { process_grey_object<true>(obj); }
 110 
 111 inline void G1CMTask::push(oop obj) {
 112   HeapWord* objAddr = (HeapWord*) obj;
 113   assert(_g1h->is_in_g1_reserved(objAddr), "invariant");
 114   assert(!_g1h->is_on_master_free_list(
 115               _g1h->heap_region_containing((HeapWord*) objAddr)), "invariant");
 116   assert(!_g1h->is_obj_ill(obj), "invariant");
 117   assert(_nextMarkBitMap->isMarked(objAddr), "invariant");
 118 
 119   if (!_task_queue->push(obj)) {
 120     // The local task queue looks full. We need to push some entries
 121     // to the global stack.
 122     move_entries_to_global_stack();
 123 
 124     // this should succeed since, even if we overflow the global
 125     // stack, we should have definitely removed some entries from the
 126     // local queue. So, there must be space on it.
 127     bool success = _task_queue->push(obj);
 128     assert(success, "invariant");
 129   }
 130 }
 131 
 132 inline bool G1CMTask::is_below_finger(oop obj, HeapWord* global_finger) const {
 133   // If obj is above the global finger, then the mark bitmap scan
 134   // will find it later, and no push is needed.  Similarly, if we have
 135   // a current region and obj is between the local finger and the
 136   // end of the current region, then no push is needed.  The tradeoff
 137   // of checking both vs only checking the global finger is that the
 138   // local check will be more accurate and so result in fewer pushes,
 139   // but may also be a little slower.
 140   HeapWord* objAddr = (HeapWord*)obj;
 141   if (_finger != NULL) {
 142     // We have a current region.
 143 
 144     // Finger and region values are all NULL or all non-NULL.  We
 145     // use _finger to check since we immediately use its value.
 146     assert(_curr_region != NULL, "invariant");
 147     assert(_region_limit != NULL, "invariant");
 148     assert(_region_limit <= global_finger, "invariant");
 149 
 150     // True if obj is less than the local finger, or is between
 151     // the region limit and the global finger.
 152     if (objAddr < _finger) {
 153       return true;
 154     } else if (objAddr < _region_limit) {
 155       return false;
 156     } // Else check global finger.
 157   }
 158   // Check global finger.
 159   return objAddr < global_finger;
 160 }
 161 
 162 template<bool scan>
 163 inline void G1CMTask::process_grey_object(oop obj) {
 164   assert(scan || obj->is_typeArray(), "Skipping scan of grey non-typeArray");
 165   assert(_nextMarkBitMap->isMarked((HeapWord*) obj), "invariant");
 166 
 167   size_t obj_size = obj->size();
 168   _words_scanned += obj_size;
 169 
 170   if (scan) {
 171     obj->oop_iterate(_cm_oop_closure);
 172   }
 173   check_limits();
 174 }
 175 
 176 inline void G1CMTask::make_reference_grey(oop obj) {
 177   if (_cm->par_mark(obj)) {
 178     // No OrderAccess:store_load() is needed. It is implicit in the
 179     // CAS done in G1CMBitMap::parMark() call in the routine above.
 180     HeapWord* global_finger = _cm->finger();
 181 
 182     // We only need to push a newly grey object on the mark
 183     // stack if it is in a section of memory the mark bitmap
 184     // scan has already examined.  Mark bitmap scanning
 185     // maintains progress "fingers" for determining that.
 186     //
 187     // Notice that the global finger might be moving forward
 188     // concurrently. This is not a problem. In the worst case, we
 189     // mark the object while it is above the global finger and, by
 190     // the time we read the global finger, it has moved forward
 191     // past this object. In this case, the object will probably
 192     // be visited when a task is scanning the region and will also
 193     // be pushed on the stack. So, some duplicate work, but no
 194     // correctness problems.
 195     if (is_below_finger(obj, global_finger)) {
 196       if (obj->is_typeArray()) {
 197         // Immediately process arrays of primitive types, rather
 198         // than pushing on the mark stack.  This keeps us from
 199         // adding humongous objects to the mark stack that might
 200         // be reclaimed before the entry is processed - see
 201         // selection of candidates for eager reclaim of humongous
 202         // objects.  The cost of the additional type test is
 203         // mitigated by avoiding a trip through the mark stack,
 204         // by only doing a bookkeeping update and avoiding the
 205         // actual scan of the object - a typeArray contains no
 206         // references, and the metadata is built-in.
 207         process_grey_object<false>(obj);
 208       } else {
 209         push(obj);
 210       }
 211     }
 212   }
 213 }
 214 
 215 inline void G1CMTask::deal_with_reference(oop obj) {
 216   increment_refs_reached();
 217 
 218   HeapWord* objAddr = (HeapWord*) obj;
 219   assert(obj->is_oop_or_null(true /* ignore mark word */), "Expected an oop or NULL at " PTR_FORMAT, p2i(obj));
 220   if (_g1h->is_in_g1_reserved(objAddr)) {
 221     assert(obj != NULL, "null check is implicit");
 222     if (!_nextMarkBitMap->isMarked(objAddr)) {
 223       // Only get the containing region if the object is not marked on the
 224       // bitmap (otherwise, it's a waste of time since we won't do
 225       // anything with it).
 226       HeapRegion* hr = _g1h->heap_region_containing(obj);
 227       if (!hr->obj_allocated_since_next_marking(obj)) {
 228         make_reference_grey(obj);
 229       }
 230     }
 231   }
 232 }
 233 
 234 inline void G1ConcurrentMark::markPrev(oop p) {
 235   assert(!_prevMarkBitMap->isMarked((HeapWord*) p), "sanity");
 236   // Note we are overriding the read-only view of the prev map here, via
 237   // the cast.
 238   ((G1CMBitMap*)_prevMarkBitMap)->mark((HeapWord*) p);
 239 }
 240 
 241 bool G1ConcurrentMark::isPrevMarked(oop p) const {
 242   assert(p != NULL && p->is_oop(), "expected an oop");
 243   HeapWord* addr = (HeapWord*)p;
 244   assert(addr >= _prevMarkBitMap->startWord() ||
 245          addr < _prevMarkBitMap->endWord(), "in a region");
 246 
 247   return _prevMarkBitMap->isMarked(addr);
 248 }
 249 
 250 inline void G1ConcurrentMark::grayRoot(oop obj, HeapRegion* hr) {
 251   assert(obj != NULL, "pre-condition");
 252   HeapWord* addr = (HeapWord*) obj;
 253   if (hr == NULL) {
 254     hr = _g1h->heap_region_containing(addr);
 255   } else {
 256     assert(hr->is_in(addr), "pre-condition");
 257   }
 258   assert(hr != NULL, "sanity");
 259   // Given that we're looking for a region that contains an object
 260   // header it's impossible to get back a HC region.
 261   assert(!hr->is_continues_humongous(), "sanity");
 262 
 263   if (addr < hr->next_top_at_mark_start()) {
 264     if (!_nextMarkBitMap->isMarked(addr)) {
 265       par_mark(obj);
 266     }
 267   }
 268 }
 269 
 270 #endif // SHARE_VM_GC_G1_G1CONCURRENTMARK_INLINE_HPP