1 /*
  2  * Copyright (c) 2018, 2021, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "gc/shared/markBitMap.inline.hpp"
 28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 29 #include "gc/shenandoah/shenandoahMarkingContext.hpp"
 30 
 31 ShenandoahMarkingContext::ShenandoahMarkingContext(MemRegion heap_region, MemRegion bitmap_region, size_t num_regions) :
 32   _mark_bit_map(heap_region, bitmap_region),
 33   _top_bitmaps(NEW_C_HEAP_ARRAY(HeapWord*, num_regions, mtGC)),
 34   _top_at_mark_starts_base(NEW_C_HEAP_ARRAY(HeapWord*, num_regions, mtGC)),
 35   _top_at_mark_starts(_top_at_mark_starts_base -
 36                       ((uintx) heap_region.start() >> ShenandoahHeapRegion::region_size_bytes_shift())) {
 37 }
 38 
 39 bool ShenandoahMarkingContext::is_bitmap_clear() const {
 40   ShenandoahHeap* heap = ShenandoahHeap::heap();
 41   size_t num_regions = heap->num_regions();
 42   for (size_t idx = 0; idx < num_regions; idx++) {
 43     ShenandoahHeapRegion* r = heap->get_region(idx);
 44     if (r->is_affiliated() && heap->is_bitmap_slice_committed(r)
 45         && !is_bitmap_range_within_region_clear(r->bottom(), r->end())) {
 46       return false;
 47     }
 48   }
 49   return true;
 50 }
 51 
 52 bool ShenandoahMarkingContext::is_bitmap_range_within_region_clear(const HeapWord* start, const HeapWord* end) const {
 53   assert(start <= end, "Invalid start " PTR_FORMAT " and end " PTR_FORMAT, p2i(start), p2i(end));
 54   if (start < end) {
 55     ShenandoahHeap* heap = ShenandoahHeap::heap();
 56     size_t start_idx = heap->heap_region_index_containing(start);
 57 #ifdef ASSERT
 58     size_t end_idx = heap->heap_region_index_containing(end - 1);
 59     assert(start_idx == end_idx, "Expected range to be within same region (" SIZE_FORMAT ", " SIZE_FORMAT ")", start_idx, end_idx);
 60 #endif
 61     ShenandoahHeapRegion* r = heap->get_region(start_idx);
 62     if (!heap->is_bitmap_slice_committed(r)) {
 63       return true;
 64     }
 65   }
 66   return _mark_bit_map.is_bitmap_clear_range(start, end);
 67 }
 68 
 69 void ShenandoahMarkingContext::initialize_top_at_mark_start(ShenandoahHeapRegion* r) {
 70   size_t idx = r->index();
 71   HeapWord *bottom = r->bottom();
 72 
 73   _top_at_mark_starts_base[idx] = bottom;
 74   _top_bitmaps[idx] = bottom;
 75 
 76   log_debug(gc)("SMC:initialize_top_at_mark_start for Region " SIZE_FORMAT ", TAMS: " PTR_FORMAT ", TopOfBitMap: " PTR_FORMAT,
 77                 r->index(), p2i(bottom), p2i(r->end()));
 78 }
 79 
 80 HeapWord* ShenandoahMarkingContext::top_bitmap(ShenandoahHeapRegion* r) {
 81   return _top_bitmaps[r->index()];
 82 }
 83 
 84 void ShenandoahMarkingContext::clear_bitmap(ShenandoahHeapRegion* r) {
 85   HeapWord* bottom = r->bottom();
 86   HeapWord* top_bitmap = _top_bitmaps[r->index()];
 87 
 88   log_debug(gc)("SMC:clear_bitmap for %s Region " SIZE_FORMAT ", top_bitmap: " PTR_FORMAT,
 89                 r->affiliation_name(), r->index(), p2i(top_bitmap));
 90 
 91   if (top_bitmap > bottom) {
 92     _mark_bit_map.clear_range_large(MemRegion(bottom, top_bitmap));
 93     _top_bitmaps[r->index()] = bottom;
 94   }
 95 
 96   assert(is_bitmap_range_within_region_clear(bottom, r->end()),
 97          "Region " SIZE_FORMAT " should have no marks in bitmap", r->index());
 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 }