1 /* 2 * Copyright (c) 2000, 2018, Oracle 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 #ifndef SHARE_VM_GC_SHARED_CARDTABLE_HPP 26 #define SHARE_VM_GC_SHARED_CARDTABLE_HPP 27 28 #include "memory/allocation.hpp" 29 #include "memory/memRegion.hpp" 30 #include "oops/oopsHierarchy.hpp" 31 #include "utilities/align.hpp" 32 33 class CardTable: public CHeapObj<mtGC> { 34 friend class VMStructs; 35 protected: 36 // The declaration order of these const fields is important; see the 37 // constructor before changing. 38 const bool _scanned_concurrently; 39 const MemRegion _whole_heap; // the region covered by the card table 40 size_t _guard_index; // index of very last element in the card 41 // table; it is set to a guard value 42 // (last_card) and should never be modified 43 size_t _last_valid_index; // index of the last valid element 44 const size_t _page_size; // page size used when mapping _byte_map 45 size_t _byte_map_size; // in bytes 46 jbyte* _byte_map; // the card marking array 47 jbyte* _byte_map_base; 48 49 int _cur_covered_regions; 50 51 // The covered regions should be in address order. 52 MemRegion* _covered; 53 // The committed regions correspond one-to-one to the covered regions. 54 // They represent the card-table memory that has been committed to service 55 // the corresponding covered region. It may be that committed region for 56 // one covered region corresponds to a larger region because of page-size 57 // roundings. Thus, a committed region for one covered region may 58 // actually extend onto the card-table space for the next covered region. 59 MemRegion* _committed; 60 61 // The last card is a guard card, and we commit the page for it so 62 // we can use the card for verification purposes. We make sure we never 63 // uncommit the MemRegion for that page. 64 MemRegion _guard_region; 65 66 inline size_t compute_byte_map_size(); 67 68 // Finds and return the index of the region, if any, to which the given 69 // region would be contiguous. If none exists, assign a new region and 70 // returns its index. Requires that no more than the maximum number of 71 // covered regions defined in the constructor are ever in use. 72 int find_covering_region_by_base(HeapWord* base); 73 74 // Same as above, but finds the region containing the given address 75 // instead of starting at a given base address. 76 int find_covering_region_containing(HeapWord* addr); 77 78 // Returns the leftmost end of a committed region corresponding to a 79 // covered region before covered region "ind", or else "NULL" if "ind" is 80 // the first covered region. 81 HeapWord* largest_prev_committed_end(int ind) const; 82 83 // Returns the part of the region mr that doesn't intersect with 84 // any committed region other than self. Used to prevent uncommitting 85 // regions that are also committed by other regions. Also protects 86 // against uncommitting the guard region. 87 MemRegion committed_unique_to_self(int self, MemRegion mr) const; 88 89 // Some barrier sets create tables whose elements correspond to parts of 90 // the heap; the CardTableBarrierSet is an example. Such barrier sets will 91 // normally reserve space for such tables, and commit parts of the table 92 // "covering" parts of the heap that are committed. At most one covered 93 // region per generation is needed. 94 static const int _max_covered_regions = 2; 95 96 enum CardValues { 97 clean_card = -1, 98 // The mask contains zeros in places for all other values. 99 clean_card_mask = clean_card - 31, 100 101 dirty_card = 0, 102 precleaned_card = 1, 103 claimed_card = 2, 104 deferred_card = 4, 105 last_card = 8, 106 CT_MR_BS_last_reserved = 16 107 }; 108 109 // a word's worth (row) of clean card values 110 static const intptr_t clean_card_row = (intptr_t)(-1); 111 112 public: 113 CardTable(MemRegion whole_heap, bool conc_scan); 114 virtual ~CardTable(); 115 virtual void initialize(); 116 117 // The kinds of precision a CardTable may offer. 118 enum PrecisionStyle { 119 Precise, 120 ObjHeadPreciseArray 121 }; 122 123 // Tells what style of precision this card table offers. precision()124 PrecisionStyle precision() { 125 return ObjHeadPreciseArray; // Only one supported for now. 126 } 127 128 // *** Barrier set functions. 129 130 // Initialization utilities; covered_words is the size of the covered region 131 // in, um, words. cards_required(size_t covered_words)132 inline size_t cards_required(size_t covered_words) { 133 // Add one for a guard card, used to detect errors. 134 const size_t words = align_up(covered_words, card_size_in_words); 135 return words / card_size_in_words + 1; 136 } 137 138 // Dirty the bytes corresponding to "mr" (not all of which must be 139 // covered.) 140 void dirty_MemRegion(MemRegion mr); 141 142 // Clear (to clean_card) the bytes entirely contained within "mr" (not 143 // all of which must be covered.) 144 void clear_MemRegion(MemRegion mr); 145 146 // Return true if "p" is at the start of a card. is_card_aligned(HeapWord * p)147 bool is_card_aligned(HeapWord* p) { 148 jbyte* pcard = byte_for(p); 149 return (addr_for(pcard) == p); 150 } 151 152 // Mapping from address to card marking array entry byte_for(const void * p) const153 jbyte* byte_for(const void* p) const { 154 assert(_whole_heap.contains(p), 155 "Attempt to access p = " PTR_FORMAT " out of bounds of " 156 " card marking array's _whole_heap = [" PTR_FORMAT "," PTR_FORMAT ")", 157 p2i(p), p2i(_whole_heap.start()), p2i(_whole_heap.end())); 158 jbyte* result = &_byte_map_base[uintptr_t(p) >> card_shift]; 159 assert(result >= _byte_map && result < _byte_map + _byte_map_size, 160 "out of bounds accessor for card marking array"); 161 return result; 162 } 163 164 // The card table byte one after the card marking array 165 // entry for argument address. Typically used for higher bounds 166 // for loops iterating through the card table. byte_after(const void * p) const167 jbyte* byte_after(const void* p) const { 168 return byte_for(p) + 1; 169 } 170 171 virtual void invalidate(MemRegion mr); 172 void clear(MemRegion mr); 173 void dirty(MemRegion mr); 174 175 // Provide read-only access to the card table array. byte_for_const(const void * p) const176 const jbyte* byte_for_const(const void* p) const { 177 return byte_for(p); 178 } byte_after_const(const void * p) const179 const jbyte* byte_after_const(const void* p) const { 180 return byte_after(p); 181 } 182 183 // Mapping from card marking array entry to address of first word addr_for(const jbyte * p) const184 HeapWord* addr_for(const jbyte* p) const { 185 assert(p >= _byte_map && p < _byte_map + _byte_map_size, 186 "out of bounds access to card marking array. p: " PTR_FORMAT 187 " _byte_map: " PTR_FORMAT " _byte_map + _byte_map_size: " PTR_FORMAT, 188 p2i(p), p2i(_byte_map), p2i(_byte_map + _byte_map_size)); 189 size_t delta = pointer_delta(p, _byte_map_base, sizeof(jbyte)); 190 HeapWord* result = (HeapWord*) (delta << card_shift); 191 assert(_whole_heap.contains(result), 192 "Returning result = " PTR_FORMAT " out of bounds of " 193 " card marking array's _whole_heap = [" PTR_FORMAT "," PTR_FORMAT ")", 194 p2i(result), p2i(_whole_heap.start()), p2i(_whole_heap.end())); 195 return result; 196 } 197 198 // Mapping from address to card marking array index. index_for(void * p)199 size_t index_for(void* p) { 200 assert(_whole_heap.contains(p), 201 "Attempt to access p = " PTR_FORMAT " out of bounds of " 202 " card marking array's _whole_heap = [" PTR_FORMAT "," PTR_FORMAT ")", 203 p2i(p), p2i(_whole_heap.start()), p2i(_whole_heap.end())); 204 return byte_for(p) - _byte_map; 205 } 206 byte_for_index(const size_t card_index) const207 const jbyte* byte_for_index(const size_t card_index) const { 208 return _byte_map + card_index; 209 } 210 211 // Resize one of the regions covered by the remembered set. 212 virtual void resize_covered_region(MemRegion new_region); 213 214 // *** Card-table-RemSet-specific things. 215 216 static uintx ct_max_alignment_constraint(); 217 218 // Apply closure "cl" to the dirty cards containing some part of 219 // MemRegion "mr". 220 void dirty_card_iterate(MemRegion mr, MemRegionClosure* cl); 221 222 // Return the MemRegion corresponding to the first maximal run 223 // of dirty cards lying completely within MemRegion mr. 224 // If reset is "true", then sets those card table entries to the given 225 // value. 226 MemRegion dirty_card_range_after_reset(MemRegion mr, bool reset, 227 int reset_val); 228 229 // Constants 230 enum SomePublicConstants { 231 card_shift = 9, 232 card_size = 1 << card_shift, 233 card_size_in_words = card_size / sizeof(HeapWord) 234 }; 235 clean_card_val()236 static jbyte clean_card_val() { return clean_card; } clean_card_mask_val()237 static jbyte clean_card_mask_val() { return clean_card_mask; } dirty_card_val()238 static jbyte dirty_card_val() { return dirty_card; } claimed_card_val()239 static jbyte claimed_card_val() { return claimed_card; } precleaned_card_val()240 static jbyte precleaned_card_val() { return precleaned_card; } deferred_card_val()241 static jbyte deferred_card_val() { return deferred_card; } clean_card_row_val()242 static intptr_t clean_card_row_val() { return clean_card_row; } 243 244 // Card marking array base (adjusted for heap low boundary) 245 // This would be the 0th element of _byte_map, if the heap started at 0x0. 246 // But since the heap starts at some higher address, this points to somewhere 247 // before the beginning of the actual _byte_map. byte_map_base() const248 jbyte* byte_map_base() const { return _byte_map_base; } scanned_concurrently() const249 bool scanned_concurrently() const { return _scanned_concurrently; } 250 251 virtual bool is_in_young(oop obj) const = 0; 252 253 // Print a description of the memory for the card table 254 virtual void print_on(outputStream* st) const; 255 256 void verify(); 257 void verify_guard(); 258 259 // val_equals -> it will check that all cards covered by mr equal val 260 // !val_equals -> it will check that all cards covered by mr do not equal val 261 void verify_region(MemRegion mr, jbyte val, bool val_equals) PRODUCT_RETURN; 262 void verify_not_dirty_region(MemRegion mr) PRODUCT_RETURN; 263 void verify_dirty_region(MemRegion mr) PRODUCT_RETURN; 264 }; 265 266 #endif // SHARE_VM_GC_SHARED_CARDTABLE_HPP 267