1 /*
2 * Copyright Amazon.com Inc. 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 #include "runtime/init.hpp"
30 #include "services/memTracker.hpp"
31
32 void ShenandoahCardTable::initialize() {
33 size_t num_cards = cards_required(_whole_heap.word_size());
34
35 // each card takes 1 byte; + 1 for the guard card
36 size_t num_bytes = num_cards + 1;
37 const size_t granularity = os::vm_allocation_granularity();
38 _byte_map_size = align_up(num_bytes, MAX2(_page_size, granularity));
39
40 HeapWord* low_bound = _whole_heap.start();
41 HeapWord* high_bound = _whole_heap.end();
42
43 // ReservedSpace constructor would assert rs_align >= os::vm_page_size().
44 const size_t rs_align = _page_size == os::vm_page_size() ? 0 : MAX2(_page_size, granularity);
45
46 ReservedSpace write_space(_byte_map_size, rs_align, _page_size);
47 initialize(write_space);
48
49 // The assembler store_check code will do an unsigned shift of the oop,
50 // then add it to _byte_map_base, i.e.
51 //
52 // _byte_map = _byte_map_base + (uintptr_t(low_bound) >> card_shift)
53 _byte_map = (CardValue*) write_space.base();
54 _byte_map_base = _byte_map - (uintptr_t(low_bound) >> _card_shift);
55 assert(byte_for(low_bound) == &_byte_map[0], "Checking start of map");
56 assert(byte_for(high_bound-1) <= &_byte_map[last_valid_index()], "Checking end of map");
57
58 _write_byte_map = _byte_map;
59 _write_byte_map_base = _byte_map_base;
60
61 ReservedSpace read_space(_byte_map_size, rs_align, _page_size);
62 initialize(read_space);
63
64 _read_byte_map = (CardValue*) read_space.base();
65 _read_byte_map_base = _read_byte_map - (uintptr_t(low_bound) >> card_shift());
66 assert(read_byte_for(low_bound) == &_read_byte_map[0], "Checking start of map");
67 assert(read_byte_for(high_bound-1) <= &_read_byte_map[last_valid_index()], "Checking end of map");
68
69 _covered[0] = _whole_heap;
70
71 log_trace(gc, barrier)("ShenandoahCardTable::ShenandoahCardTable:");
72 log_trace(gc, barrier)(" &_write_byte_map[0]: " INTPTR_FORMAT " &_write_byte_map[_last_valid_index]: " INTPTR_FORMAT,
73 p2i(&_write_byte_map[0]), p2i(&_write_byte_map[last_valid_index()]));
74 log_trace(gc, barrier)(" _write_byte_map_base: " INTPTR_FORMAT, p2i(_write_byte_map_base));
75 log_trace(gc, barrier)(" &_read_byte_map[0]: " INTPTR_FORMAT " &_read_byte_map[_last_valid_index]: " INTPTR_FORMAT,
76 p2i(&_read_byte_map[0]), p2i(&_read_byte_map[last_valid_index()]));
77 log_trace(gc, barrier)(" _read_byte_map_base: " INTPTR_FORMAT, p2i(_read_byte_map_base));
78 }
79
80 void ShenandoahCardTable::initialize(const ReservedSpace& card_table) {
81 MemTracker::record_virtual_memory_type((address)card_table.base(), mtGC);
82
83 os::trace_page_sizes("Card Table", _byte_map_size, _byte_map_size,
84 _page_size, card_table.base(), card_table.size());
85 if (!card_table.is_reserved()) {
86 vm_exit_during_initialization("Could not reserve enough space for the card marking array");
87 }
88 os::commit_memory_or_exit(card_table.base(), _byte_map_size, card_table.alignment(), false,
89 "Cannot commit memory for card table");
90 }
91
92 bool ShenandoahCardTable::is_in_young(const void* obj) const {
93 return ShenandoahHeap::heap()->is_in_young(obj);
94 }
95
96 CardValue* ShenandoahCardTable::read_byte_for(const void* p) {
97 CardValue* result = &_read_byte_map_base[uintptr_t(p) >> _card_shift];
98 assert(result >= _read_byte_map && result < _read_byte_map + _byte_map_size,
99 "out of bounds accessor for card marking array");
100 return result;
101 }
102
103 size_t ShenandoahCardTable::last_valid_index() {
104 return CardTable::last_valid_index();
105 }