1 /*
   2  * Copyright (c) 2001, 2015, 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_CONCURRENTMARK_INLINE_HPP
  26 #define SHARE_VM_GC_G1_CONCURRENTMARK_INLINE_HPP
  27 
  28 #include "gc/g1/concurrentMark.hpp"
  29 #include "gc/g1/g1CollectedHeap.inline.hpp"
  30 #include "gc/shared/taskqueue.inline.hpp"
  31 
  32 // Utility routine to set an exclusive range of cards on the given
  33 // card liveness bitmap
  34 inline void ConcurrentMark::set_card_bitmap_range(BitMap* card_bm,
  35                                                   BitMap::idx_t start_idx,
  36                                                   BitMap::idx_t end_idx,
  37                                                   bool is_par) {
  38 
  39   // Set the exclusive bit range [start_idx, end_idx).
  40   assert((end_idx - start_idx) > 0, "at least one card");
  41   assert(end_idx <= card_bm->size(), "sanity");
  42 
  43   // Silently clip the end index
  44   end_idx = MIN2(end_idx, card_bm->size());
  45 
  46   // For small ranges use a simple loop; otherwise use set_range or
  47   // use par_at_put_range (if parallel). The range is made up of the
  48   // cards that are spanned by an object/mem region so 8 cards will
  49   // allow up to object sizes up to 4K to be handled using the loop.
  50   if ((end_idx - start_idx) <= 8) {
  51     for (BitMap::idx_t i = start_idx; i < end_idx; i += 1) {
  52       if (is_par) {
  53         card_bm->par_set_bit(i);
  54       } else {
  55         card_bm->set_bit(i);
  56       }
  57     }
  58   } else {
  59     // Note BitMap::par_at_put_range() and BitMap::set_range() are exclusive.
  60     if (is_par) {
  61       card_bm->par_at_put_range(start_idx, end_idx, true);
  62     } else {
  63       card_bm->set_range(start_idx, end_idx);
  64     }
  65   }
  66 }
  67 
  68 // Returns the index in the liveness accounting card bitmap
  69 // for the given address
  70 inline BitMap::idx_t ConcurrentMark::card_bitmap_index_for(HeapWord* addr) {
  71   // Below, the term "card num" means the result of shifting an address
  72   // by the card shift -- address 0 corresponds to card number 0.  One
  73   // must subtract the card num of the bottom of the heap to obtain a
  74   // card table index.
  75   intptr_t card_num = intptr_t(uintptr_t(addr) >> CardTableModRefBS::card_shift);
  76   return card_num - heap_bottom_card_num();
  77 }
  78 
  79 // Counts the given memory region in the given task/worker
  80 // counting data structures.
  81 inline void ConcurrentMark::count_region(MemRegion mr, HeapRegion* hr,
  82                                          size_t* marked_bytes_array,
  83                                          BitMap* task_card_bm) {
  84   G1CollectedHeap* g1h = _g1h;
  85   CardTableModRefBS* ct_bs = g1h->g1_barrier_set();
  86 
  87   HeapWord* start = mr.start();
  88   HeapWord* end = mr.end();
  89   size_t region_size_bytes = mr.byte_size();
  90   uint index = hr->hrm_index();
  91 
  92   assert(hr == g1h->heap_region_containing(start), "sanity");
  93   assert(marked_bytes_array != NULL, "pre-condition");
  94   assert(task_card_bm != NULL, "pre-condition");
  95 
  96   // Add to the task local marked bytes for this region.
  97   marked_bytes_array[index] += region_size_bytes;
  98 
  99   BitMap::idx_t start_idx = card_bitmap_index_for(start);
 100   BitMap::idx_t end_idx = card_bitmap_index_for(end);
 101 
 102   // Note: if we're looking at the last region in heap - end
 103   // could be actually just beyond the end of the heap; end_idx
 104   // will then correspond to a (non-existent) card that is also
 105   // just beyond the heap.
 106   if (g1h->is_in_g1_reserved(end) && !ct_bs->is_card_aligned(end)) {
 107     // end of region is not card aligned - increment to cover
 108     // all the cards spanned by the region.
 109     end_idx += 1;
 110   }
 111   // The card bitmap is task/worker specific => no need to use
 112   // the 'par' BitMap routines.
 113   // Set bits in the exclusive bit range [start_idx, end_idx).
 114   set_card_bitmap_range(task_card_bm, start_idx, end_idx, false /* is_par */);
 115 }
 116 
 117 // Counts the given object in the given task/worker counting data structures.
 118 inline void ConcurrentMark::count_object(oop obj,
 119                                          HeapRegion* hr,
 120                                          size_t* marked_bytes_array,
 121                                          BitMap* task_card_bm,
 122                                          size_t word_size) {
 123   assert(!hr->is_continues_humongous(), "Cannot enter count_object with continues humongous");
 124   if (!hr->is_starts_humongous()) {
 125     MemRegion mr((HeapWord*)obj, word_size);
 126     count_region(mr, hr, marked_bytes_array, task_card_bm);
 127   } else {
 128     do {
 129       MemRegion mr(hr->bottom(), hr->top());
 130       count_region(mr, hr, marked_bytes_array, task_card_bm);
 131       hr = _g1h->next_region_in_humongous(hr);
 132     } while (hr != NULL);
 133   }
 134 }
 135 
 136 // Attempts to mark the given object and, if successful, counts
 137 // the object in the given task/worker counting structures.
 138 inline bool ConcurrentMark::par_mark_and_count(oop obj,
 139                                                HeapRegion* hr,
 140                                                size_t* marked_bytes_array,
 141                                                BitMap* task_card_bm) {
 142   if (_nextMarkBitMap->parMark((HeapWord*)obj)) {
 143     // Update the task specific count data for the object.
 144     count_object(obj, hr, marked_bytes_array, task_card_bm, obj->size());
 145     return true;
 146   }
 147   return false;
 148 }
 149 
 150 // Attempts to mark the given object and, if successful, counts
 151 // the object in the task/worker counting structures for the
 152 // given worker id.
 153 inline bool ConcurrentMark::par_mark_and_count(oop obj,
 154                                                size_t word_size,
 155                                                HeapRegion* hr,
 156                                                uint worker_id) {
 157   if (_nextMarkBitMap->parMark((HeapWord*)obj)) {
 158     size_t* marked_bytes_array = count_marked_bytes_array_for(worker_id);
 159     BitMap* task_card_bm = count_card_bitmap_for(worker_id);
 160     count_object(obj, hr, marked_bytes_array, task_card_bm, word_size);
 161     return true;
 162   }
 163   return false;
 164 }
 165 
 166 inline bool CMBitMapRO::iterate(BitMapClosure* cl, MemRegion mr) {
 167   HeapWord* start_addr = MAX2(startWord(), mr.start());
 168   HeapWord* end_addr = MIN2(endWord(), mr.end());
 169 
 170   if (end_addr > start_addr) {
 171     // Right-open interval [start-offset, end-offset).
 172     BitMap::idx_t start_offset = heapWordToOffset(start_addr);
 173     BitMap::idx_t end_offset = heapWordToOffset(end_addr);
 174 
 175     start_offset = _bm.get_next_one_offset(start_offset, end_offset);
 176     while (start_offset < end_offset) {
 177       if (!cl->do_bit(start_offset)) {
 178         return false;
 179       }
 180       HeapWord* next_addr = MIN2(nextObject(offsetToHeapWord(start_offset)), end_addr);
 181       BitMap::idx_t next_offset = heapWordToOffset(next_addr);
 182       start_offset = _bm.get_next_one_offset(next_offset, end_offset);
 183     }
 184   }
 185   return true;
 186 }
 187 
 188 inline bool CMBitMapRO::iterate(BitMapClosure* cl) {
 189   MemRegion mr(startWord(), sizeInWords());
 190   return iterate(cl, mr);
 191 }
 192 
 193 #define check_mark(addr)                                                       \
 194   assert(_bmStartWord <= (addr) && (addr) < (_bmStartWord + _bmWordSize),      \
 195          "outside underlying space?");                                         \
 196   assert(G1CollectedHeap::heap()->is_in_exact(addr),                           \
 197          "Trying to access not available bitmap " PTR_FORMAT                   \
 198          " corresponding to " PTR_FORMAT " (%u)",                              \
 199          p2i(this), p2i(addr), G1CollectedHeap::heap()->addr_to_region(addr));
 200 
 201 inline void CMBitMap::mark(HeapWord* addr) {
 202   check_mark(addr);
 203   _bm.set_bit(heapWordToOffset(addr));
 204 }
 205 
 206 inline void CMBitMap::clear(HeapWord* addr) {
 207   check_mark(addr);
 208   _bm.clear_bit(heapWordToOffset(addr));
 209 }
 210 
 211 inline bool CMBitMap::parMark(HeapWord* addr) {
 212   check_mark(addr);
 213   return _bm.par_set_bit(heapWordToOffset(addr));
 214 }
 215 
 216 inline bool CMBitMap::parClear(HeapWord* addr) {
 217   check_mark(addr);
 218   return _bm.par_clear_bit(heapWordToOffset(addr));
 219 }
 220 
 221 #undef check_mark
 222 
 223 template<typename Fn>
 224 inline void CMMarkStack::iterate(Fn fn) {
 225   assert(_saved_index == _index, "saved index: %d index: %d", _saved_index, _index);
 226   for (int i = 0; i < _index; ++i) {
 227     fn(_base[i]);
 228   }
 229 }
 230 
 231 // It scans an object and visits its children.
 232 inline void CMTask::scan_object(oop obj) { process_grey_object<true>(obj); }
 233 
 234 inline void CMTask::push(oop obj) {
 235   HeapWord* objAddr = (HeapWord*) obj;
 236   assert(_g1h->is_in_g1_reserved(objAddr), "invariant");
 237   assert(!_g1h->is_on_master_free_list(
 238               _g1h->heap_region_containing((HeapWord*) objAddr)), "invariant");
 239   assert(!_g1h->is_obj_ill(obj), "invariant");
 240   assert(_nextMarkBitMap->isMarked(objAddr), "invariant");
 241 
 242   if (!_task_queue->push(obj)) {
 243     // The local task queue looks full. We need to push some entries
 244     // to the global stack.
 245     move_entries_to_global_stack();
 246 
 247     // this should succeed since, even if we overflow the global
 248     // stack, we should have definitely removed some entries from the
 249     // local queue. So, there must be space on it.
 250     bool success = _task_queue->push(obj);
 251     assert(success, "invariant");
 252   }
 253 }
 254 
 255 inline bool CMTask::is_below_finger(oop obj, HeapWord* global_finger) const {
 256   // If obj is above the global finger, then the mark bitmap scan
 257   // will find it later, and no push is needed.  Similarly, if we have
 258   // a current region and obj is between the local finger and the
 259   // end of the current region, then no push is needed.  The tradeoff
 260   // of checking both vs only checking the global finger is that the
 261   // local check will be more accurate and so result in fewer pushes,
 262   // but may also be a little slower.
 263   HeapWord* objAddr = (HeapWord*)obj;
 264   if (_finger != NULL) {
 265     // We have a current region.
 266 
 267     // Finger and region values are all NULL or all non-NULL.  We
 268     // use _finger to check since we immediately use its value.
 269     assert(_curr_region != NULL, "invariant");
 270     assert(_region_limit != NULL, "invariant");
 271     assert(_region_limit <= global_finger, "invariant");
 272 
 273     // True if obj is less than the local finger, or is between
 274     // the region limit and the global finger.
 275     if (objAddr < _finger) {
 276       return true;
 277     } else if (objAddr < _region_limit) {
 278       return false;
 279     } // Else check global finger.
 280   }
 281   // Check global finger.
 282   return objAddr < global_finger;
 283 }
 284 
 285 template<bool scan>
 286 inline void CMTask::process_grey_object(oop obj) {
 287   assert(scan || obj->is_typeArray(), "Skipping scan of grey non-typeArray");
 288   assert(_nextMarkBitMap->isMarked((HeapWord*) obj), "invariant");
 289 
 290   size_t obj_size = obj->size();
 291   _words_scanned += obj_size;
 292 
 293   if (scan) {
 294     obj->oop_iterate(_cm_oop_closure);
 295   }
 296   check_limits();
 297 }
 298 
 299 
 300 
 301 inline void CMTask::make_reference_grey(oop obj, HeapRegion* hr) {
 302   if (_cm->par_mark_and_count(obj, hr, _marked_bytes_array, _card_bm)) {
 303     // No OrderAccess:store_load() is needed. It is implicit in the
 304     // CAS done in CMBitMap::parMark() call in the routine above.
 305     HeapWord* global_finger = _cm->finger();
 306 
 307     // We only need to push a newly grey object on the mark
 308     // stack if it is in a section of memory the mark bitmap
 309     // scan has already examined.  Mark bitmap scanning
 310     // maintains progress "fingers" for determining that.
 311     //
 312     // Notice that the global finger might be moving forward
 313     // concurrently. This is not a problem. In the worst case, we
 314     // mark the object while it is above the global finger and, by
 315     // the time we read the global finger, it has moved forward
 316     // past this object. In this case, the object will probably
 317     // be visited when a task is scanning the region and will also
 318     // be pushed on the stack. So, some duplicate work, but no
 319     // correctness problems.
 320     if (is_below_finger(obj, global_finger)) {
 321       if (obj->is_typeArray()) {
 322         // Immediately process arrays of primitive types, rather
 323         // than pushing on the mark stack.  This keeps us from
 324         // adding humongous objects to the mark stack that might
 325         // be reclaimed before the entry is processed - see
 326         // selection of candidates for eager reclaim of humongous
 327         // objects.  The cost of the additional type test is
 328         // mitigated by avoiding a trip through the mark stack,
 329         // by only doing a bookkeeping update and avoiding the
 330         // actual scan of the object - a typeArray contains no
 331         // references, and the metadata is built-in.
 332         process_grey_object<false>(obj);
 333       } else {
 334         push(obj);
 335       }
 336     }
 337   }
 338 }
 339 
 340 inline void CMTask::deal_with_reference(oop obj) {
 341   increment_refs_reached();
 342 
 343   HeapWord* objAddr = (HeapWord*) obj;
 344   assert(obj->is_oop_or_null(true /* ignore mark word */), "Expected an oop or NULL at " PTR_FORMAT, p2i(obj));
 345   if (_g1h->is_in_g1_reserved(objAddr)) {
 346     assert(obj != NULL, "null check is implicit");
 347     if (!_nextMarkBitMap->isMarked(objAddr)) {
 348       // Only get the containing region if the object is not marked on the
 349       // bitmap (otherwise, it's a waste of time since we won't do
 350       // anything with it).
 351       HeapRegion* hr = _g1h->heap_region_containing(obj);
 352       if (!hr->obj_allocated_since_next_marking(obj)) {
 353         make_reference_grey(obj, hr);
 354       }
 355     }
 356   }
 357 }
 358 
 359 inline void ConcurrentMark::markPrev(oop p) {
 360   assert(!_prevMarkBitMap->isMarked((HeapWord*) p), "sanity");
 361   // Note we are overriding the read-only view of the prev map here, via
 362   // the cast.
 363   ((CMBitMap*)_prevMarkBitMap)->mark((HeapWord*) p);
 364 }
 365 
 366 inline void ConcurrentMark::grayRoot(oop obj, size_t word_size,
 367                                      uint worker_id, HeapRegion* hr) {
 368   assert(obj != NULL, "pre-condition");
 369   HeapWord* addr = (HeapWord*) obj;
 370   if (hr == NULL) {
 371     hr = _g1h->heap_region_containing(addr);
 372   } else {
 373     assert(hr->is_in(addr), "pre-condition");
 374   }
 375   assert(hr != NULL, "sanity");
 376   // Given that we're looking for a region that contains an object
 377   // header it's impossible to get back a HC region.
 378   assert(!hr->is_continues_humongous(), "sanity");
 379 
 380   if (addr < hr->next_top_at_mark_start()) {
 381     if (!_nextMarkBitMap->isMarked(addr)) {
 382       par_mark_and_count(obj, word_size, hr, worker_id);
 383     }
 384   }
 385 }
 386 
 387 #endif // SHARE_VM_GC_G1_CONCURRENTMARK_INLINE_HPP