1 /*
  2  * Copyright (c) 2020, 2021, Amazon.com, Inc. 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 #include "precompiled.hpp"
 26 #include "gc/shenandoah/shenandoahCardTable.hpp"
 27 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 28 #include "gc/shenandoah/shenandoahUtils.hpp"
 29 
 30 void ShenandoahCardTable::initialize() {
 31   CardTable::initialize();
 32   _write_byte_map = _byte_map;
 33   _write_byte_map_base = _byte_map_base;
 34   const size_t rs_align = _page_size == (size_t) os::vm_page_size() ? 0 :
 35     MAX2(_page_size, (size_t) os::vm_allocation_granularity());
 36 
 37   ReservedSpace heap_rs(_byte_map_size, rs_align, _page_size);
 38   if (!heap_rs.is_reserved()) {
 39     vm_exit_during_initialization("Could not reserve enough space for second copy of card marking array");
 40   }
 41   os::commit_memory_or_exit(heap_rs.base(), _byte_map_size, rs_align, false, "Cannot commit memory for second copy of card table");
 42 
 43   HeapWord* low_bound  = _whole_heap.start();
 44   _read_byte_map = (CardValue*) heap_rs.base();
 45   _read_byte_map_base = _read_byte_map - (uintptr_t(low_bound) >> card_shift);
 46 
 47   log_trace(gc, barrier)("ShenandoahCardTable::ShenandoahCardTable: ");
 48   log_trace(gc, barrier)("    &_read_byte_map[0]: " INTPTR_FORMAT "  &_read_byte_map[_last_valid_index]: " INTPTR_FORMAT,
 49                   p2i(&_read_byte_map[0]), p2i(&_read_byte_map[_last_valid_index]));
 50   log_trace(gc, barrier)("    _read_byte_map_base: " INTPTR_FORMAT, p2i(_read_byte_map_base));
 51 
 52   // TODO: As currently implemented, we do not swap pointers between _read_byte_map and _write_byte_map
 53   // because the mutator write barrier hard codes the address of the _write_byte_map_base.  Instead,
 54   // the current implementation simply copies contents of _write_byte_map onto _read_byte_map and cleans
 55   // the entirety of _write_byte_map at the init_mark safepoint.
 56   //
 57   // If we choose to modify the mutator write barrier so that we can swap _read_byte_map_base and
 58   // _write_byte_map_base pointers, we may also have to figure out certain details about how the
 59   // _guard_region is implemented so that we can replicate the read and write versions of this region.
 60   //
 61   // Alternatively, we may switch to a SATB-based write barrier and replace the direct card-marking
 62   // remembered set with something entirely different.
 63 
 64   resize_covered_region(_whole_heap);
 65 }
 66 
 67 bool ShenandoahCardTable::is_in_young(oop obj) const {
 68   return ShenandoahHeap::heap()->is_in_young(obj);
 69 }
 70 
 71 bool ShenandoahCardTable::is_dirty(MemRegion mr) {
 72   for (size_t i = index_for(mr.start()); i <= index_for(mr.end() - 1); i++) {
 73     CardValue* byte = byte_for_index(i);
 74     if (*byte == CardTable::dirty_card_val()) {
 75       return true;
 76     }
 77   }
 78   return false;
 79 }
 80 
 81 void ShenandoahCardTable::clear() {
 82   CardTable::clear(_whole_heap);
 83 }
 84 
 85 // TODO: This service is not currently used because we are not able to swap _read_byte_map_base and
 86 // _write_byte_map_base pointers.  If we were able to do so, we would invoke clear_read_table "immediately"
 87 // following the end of concurrent remembered set scanning so that this read card table would be ready
 88 // to serve as the new write card table at the time these pointer values were next swapped.
 89 //
 90 // In the current implementation, the write-table is cleared immediately after its contents is copied to
 91 // the read table, obviating the need for this service.
 92 void ShenandoahCardTable::clear_read_table() {
 93   for (size_t i = 0; i < _byte_map_size; i++) {
 94     _read_byte_map[i] = clean_card;
 95   }
 96 }
 97 
 98 // TODO: This service is not currently used because the mutator write barrier implementation hard codes the
 99 // location of the _write_byte_may_base.  If we change the mutator's write barrier implementation, then we
100 // may use this service to exchange the roles of the read-card-table and write-card-table.
101 void ShenandoahCardTable::swap_card_tables() {
102   shenandoah_assert_safepoint();
103 
104   CardValue* save_value = _read_byte_map;
105   _read_byte_map = _write_byte_map;
106   _write_byte_map = save_value;
107 
108   save_value = _read_byte_map_base;
109   _read_byte_map_base = _write_byte_map_base;
110   _write_byte_map_base = save_value;
111 
112   // update the superclass instance variables
113   _byte_map = _write_byte_map;
114   _byte_map_base = _write_byte_map_base;
115 }