1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc/shared/markBitMap.inline.hpp"
  26 #include "gc/shenandoah/shenandoahHeap.hpp"
  27 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  28 #include "gc/shenandoah/shenandoahMarkingContext.hpp"
  29 
  30 ShenandoahMarkingContext::ShenandoahMarkingContext(MemRegion heap_region, MemRegion bitmap_region, size_t num_regions) :
  31   _top_bitmaps(NEW_C_HEAP_ARRAY(HeapWord*, num_regions, mtGC)),
  32   _top_at_mark_starts_base(NEW_C_HEAP_ARRAY(HeapWord*, num_regions, mtGC)),
  33   _top_at_mark_starts(_top_at_mark_starts_base -
  34                       ((uintx) heap_region.start() >> ShenandoahHeapRegion::region_size_bytes_shift())) {
  35   _mark_bit_map.initialize(heap_region, bitmap_region);
  36 }
  37 
  38 bool ShenandoahMarkingContext::is_bitmap_clear() const {
  39   ShenandoahHeap* heap = ShenandoahHeap::heap();
  40   size_t num_regions = heap->num_regions();
  41   for (size_t idx = 0; idx < num_regions; idx++) {
  42     ShenandoahHeapRegion* r = heap->get_region(idx);
  43     if (heap->is_bitmap_slice_committed(r) && !is_bitmap_clear_range(r->bottom(), r->end())) {
  44       return false;
  45     }
  46   }
  47   return true;
  48 }
  49 
  50 bool ShenandoahMarkingContext::is_bitmap_clear_range(HeapWord* start, HeapWord* end) const {
  51   return _mark_bit_map.get_next_marked_addr(start, end) == end;
  52 }
  53 
  54 void ShenandoahMarkingContext::initialize_top_at_mark_start(ShenandoahHeapRegion* r) {
  55   size_t idx = r->region_number();
  56   HeapWord *bottom = r->bottom();
  57   _top_at_mark_starts_base[idx] = bottom;
  58   _top_bitmaps[idx] = bottom;
  59 }
  60 
  61 void ShenandoahMarkingContext::capture_top_at_mark_start(ShenandoahHeapRegion *r) {
  62   size_t region_number = r->region_number();
  63   HeapWord* old_tams = _top_at_mark_starts_base[region_number];
  64   HeapWord* new_tams = r->top();
  65 
  66   assert(new_tams >= old_tams,
  67          "Region " SIZE_FORMAT", TAMS updates should be monotonic: " PTR_FORMAT " -> " PTR_FORMAT,
  68          region_number, p2i(old_tams), p2i(new_tams));
  69   assert(is_bitmap_clear_range(old_tams, new_tams),
  70          "Region " SIZE_FORMAT ", bitmap should be clear while adjusting TAMS: " PTR_FORMAT " -> " PTR_FORMAT,
  71          region_number, p2i(old_tams), p2i(new_tams));
  72 
  73   _top_at_mark_starts_base[region_number] = new_tams;
  74   _top_bitmaps[region_number] = new_tams;
  75 }
  76 
  77 void ShenandoahMarkingContext::reset_top_at_mark_start(ShenandoahHeapRegion* r) {
  78   _top_at_mark_starts_base[r->region_number()] = r->bottom();
  79 }
  80 
  81 HeapWord* ShenandoahMarkingContext::top_at_mark_start(ShenandoahHeapRegion* r) const {
  82   return _top_at_mark_starts_base[r->region_number()];
  83 }
  84 
  85 void ShenandoahMarkingContext::reset_top_bitmap(ShenandoahHeapRegion* r) {
  86   assert(is_bitmap_clear_range(r->bottom(), r->end()),
  87          "Region " SIZE_FORMAT " should have no marks in bitmap", r->region_number());
  88   _top_bitmaps[r->region_number()] = r->bottom();
  89 }
  90 
  91 void ShenandoahMarkingContext::clear_bitmap(ShenandoahHeapRegion* r) {
  92   HeapWord* bottom = r->bottom();
  93   HeapWord* top_bitmap = _top_bitmaps[r->region_number()];
  94   if (top_bitmap > bottom) {
  95     _mark_bit_map.clear_range_large(MemRegion(bottom, top_bitmap));
  96     _top_bitmaps[r->region_number()] = bottom;
  97   }
  98   assert(is_bitmap_clear_range(bottom, r->end()),
  99          "Region " SIZE_FORMAT " should have no marks in bitmap", r->region_number());
 100 }
 101 
 102 bool ShenandoahMarkingContext::is_complete() {
 103   return _is_complete.is_set();
 104 }
 105 
 106 void ShenandoahMarkingContext::mark_complete() {
 107   _is_complete.set();
 108 }
 109 
 110 void ShenandoahMarkingContext::mark_incomplete() {
 111   _is_complete.unset();
 112 }