1 /*
  2  * Copyright (c) 2018, 2021, Red Hat, Inc. 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 #include "precompiled.hpp"
 26 #include "gc/shared/markBitMap.inline.hpp"
 27 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 28 #include "gc/shenandoah/shenandoahMarkingContext.hpp"
 29 
 30 ShenandoahMarkingContext::ShenandoahMarkingContext(MemRegion heap_region, MemRegion bitmap_region, size_t num_regions) :
 31   _mark_bit_map(heap_region, bitmap_region),
 32   _top_bitmaps(NEW_C_HEAP_ARRAY(HeapWord*, num_regions, mtGC)),
 33   _top_at_mark_starts_base(NEW_C_HEAP_ARRAY(HeapWord*, num_regions, mtGC)),
 34   _top_at_mark_starts(_top_at_mark_starts_base -
 35                       ((uintx) heap_region.start() >> ShenandoahHeapRegion::region_size_bytes_shift())) {
 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 ((r->affiliation() != FREE) && 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(const HeapWord* start, const HeapWord* end) const {
 51   if (start < end) {
 52     ShenandoahHeap* heap = ShenandoahHeap::heap();
 53     size_t start_idx = heap->heap_region_index_containing(start);
 54     size_t end_idx = heap->heap_region_index_containing(end - 1);
 55     while (start_idx <= end_idx) {
 56       ShenandoahHeapRegion* r = heap->get_region(start_idx);
 57       if (!heap->is_bitmap_slice_committed(r))
 58         return true;
 59       start_idx++;
 60     }
 61   }
 62   return _mark_bit_map.is_bitmap_clear_range(start, end);
 63 }
 64 
 65 void ShenandoahMarkingContext::initialize_top_at_mark_start(ShenandoahHeapRegion* r) {
 66   size_t idx = r->index();
 67   HeapWord *bottom = r->bottom();
 68 
 69   _top_at_mark_starts_base[idx] = bottom;
 70   _top_bitmaps[idx] = bottom;
 71 
 72   log_debug(gc)("SMC:initialize_top_at_mark_start for Region " SIZE_FORMAT ", TAMS: " PTR_FORMAT ", TopOfBitMap: " PTR_FORMAT,
 73                 r->index(), p2i(bottom), p2i(r->end()));
 74 }
 75 
 76 HeapWord* ShenandoahMarkingContext::top_bitmap(ShenandoahHeapRegion* r) {
 77   return _top_bitmaps[r->index()];
 78 }
 79 
 80 void ShenandoahMarkingContext::clear_bitmap(ShenandoahHeapRegion* r) {
 81   HeapWord* bottom = r->bottom();
 82   HeapWord* top_bitmap = _top_bitmaps[r->index()];
 83 
 84   log_debug(gc)("SMC:clear_bitmap for %s Region " SIZE_FORMAT ", top_bitmap: " PTR_FORMAT,
 85                 affiliation_name(r->affiliation()), r->index(), p2i(top_bitmap));
 86 
 87   if (r->affiliation() != FREE) {
 88     if (top_bitmap > bottom) {
 89       _mark_bit_map.clear_range_large(MemRegion(bottom, top_bitmap));
 90       _top_bitmaps[r->index()] = bottom;
 91     }
 92     r->clear_live_data();
 93     assert(is_bitmap_clear_range(bottom, r->end()),
 94            "Region " SIZE_FORMAT " should have no marks in bitmap", r->index());
 95   }
 96   // heap iterators include FREE regions, which don't need to be cleared.
 97   // TODO: would be better for certain iterators to not include FREE regions.
 98 }
 99 
100 bool ShenandoahMarkingContext::is_complete() {
101   return _is_complete.is_set();
102 }
103 
104 void ShenandoahMarkingContext::mark_complete() {
105   _is_complete.set();
106 }
107 
108 void ShenandoahMarkingContext::mark_incomplete() {
109   _is_complete.unset();
110 }