1 #ifdef USE_PRAGMA_IDENT_HDR 2 #pragma ident "@(#)cardTableModRefBS.hpp 1.53 07/10/04 10:49:32 JVM" 3 #endif 4 /* 5 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved. 6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7 * 8 * This code is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License version 2 only, as 10 * published by the Free Software Foundation. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 23 * CA 95054 USA or visit www.sun.com if you need additional information or 24 * have any questions. 25 * 26 */ 27 28 // This kind of "BarrierSet" allows a "CollectedHeap" to detect and 29 // enumerate ref fields that have been modified (since the last 30 // enumeration.) 31 32 // As it currently stands, this barrier is *imprecise*: when a ref field in 33 // an object "o" is modified, the card table entry for the card containing 34 // the head of "o" is dirtied, not necessarily the card containing the 35 // modified field itself. For object arrays, however, the barrier *is* 36 // precise; only the card containing the modified element is dirtied. 37 // Any MemRegionClosures used to scan dirty cards should take these 38 // considerations into account. 39 40 class Generation; 41 class OopsInGenClosure; 42 class DirtyCardToOopClosure; 43 44 class CardTableModRefBS: public ModRefBarrierSet { 45 // Some classes get to look at some private stuff. 46 friend class BytecodeInterpreter; 47 friend class VMStructs; 48 friend class CardTableRS; 49 friend class CheckForUnmarkedOops; // Needs access to raw card bytes. 50 #ifndef PRODUCT 51 // For debugging. 52 friend class GuaranteeNotModClosure; 53 #endif 54 protected: 55 56 enum CardValues { 57 clean_card = -1, 58 dirty_card = 0, 59 precleaned_card = 1, 60 last_card = 4, 61 CT_MR_BS_last_reserved = 10 62 }; 63 64 // dirty and precleaned are equivalent wrt younger_refs_iter. 65 static bool card_is_dirty_wrt_gen_iter(jbyte cv) { 66 return cv == dirty_card || cv == precleaned_card; 67 } 68 69 // Returns "true" iff the value "cv" will cause the card containing it 70 // to be scanned in the current traversal. May be overridden by 71 // subtypes. 72 virtual bool card_will_be_scanned(jbyte cv) { 73 return CardTableModRefBS::card_is_dirty_wrt_gen_iter(cv); 74 } 75 76 // Returns "true" iff the value "cv" may have represented a dirty card at 77 // some point. 78 virtual bool card_may_have_been_dirty(jbyte cv) { 79 return card_is_dirty_wrt_gen_iter(cv); 80 } 81 82 // The declaration order of these const fields is important; see the 83 // constructor before changing. 84 const MemRegion _whole_heap; // the region covered by the card table 85 const size_t _guard_index; // index of very last element in the card 86 // table; it is set to a guard value 87 // (last_card) and should never be modified 88 const size_t _last_valid_index; // index of the last valid element 89 const size_t _page_size; // page size used when mapping _byte_map 90 const size_t _byte_map_size; // in bytes 91 jbyte* _byte_map; // the card marking array 92 93 int _cur_covered_regions; 94 // The covered regions should be in address order. 95 MemRegion* _covered; 96 // The committed regions correspond one-to-one to the covered regions. 97 // They represent the card-table memory that has been committed to service 98 // the corresponding covered region. It may be that committed region for 99 // one covered region corresponds to a larger region because of page-size 100 // roundings. Thus, a committed region for one covered region may 101 // actually extend onto the card-table space for the next covered region. 102 MemRegion* _committed; 103 104 // The last card is a guard card, and we commit the page for it so 105 // we can use the card for verification purposes. We make sure we never 106 // uncommit the MemRegion for that page. 107 MemRegion _guard_region; 108 109 protected: 110 // Initialization utilities; covered_words is the size of the covered region 111 // in, um, words. 112 inline size_t cards_required(size_t covered_words); 113 inline size_t compute_byte_map_size(); 114 115 // Finds and return the index of the region, if any, to which the given 116 // region would be contiguous. If none exists, assign a new region and 117 // returns its index. Requires that no more than the maximum number of 118 // covered regions defined in the constructor are ever in use. 119 int find_covering_region_by_base(HeapWord* base); 120 121 // Same as above, but finds the region containing the given address 122 // instead of starting at a given base address. 123 int find_covering_region_containing(HeapWord* addr); 124 125 // Resize one of the regions covered by the remembered set. 126 void resize_covered_region(MemRegion new_region); 127 128 // Returns the leftmost end of a committed region corresponding to a 129 // covered region before covered region "ind", or else "NULL" if "ind" is 130 // the first covered region. 131 HeapWord* largest_prev_committed_end(int ind) const; 132 133 // Returns the part of the region mr that doesn't intersect with 134 // any committed region other than self. Used to prevent uncommitting 135 // regions that are also committed by other regions. Also protects 136 // against uncommitting the guard region. 137 MemRegion committed_unique_to_self(int self, MemRegion mr) const; 138 139 // Mapping from address to card marking array entry 140 jbyte* byte_for(const void* p) const { 141 assert(_whole_heap.contains(p), 142 "out of bounds access to card marking array"); 143 jbyte* result = &byte_map_base[uintptr_t(p) >> card_shift]; 144 assert(result >= _byte_map && result < _byte_map + _byte_map_size, 145 "out of bounds accessor for card marking array"); 146 return result; 147 } 148 149 // The card table byte one after the card marking array 150 // entry for argument address. Typically used for higher bounds 151 // for loops iterating through the card table. 152 jbyte* byte_after(const void* p) const { 153 return byte_for(p) + 1; 154 } 155 156 // Mapping from card marking array entry to address of first word 157 HeapWord* addr_for(const jbyte* p) const { 158 assert(p >= _byte_map && p < _byte_map + _byte_map_size, 159 "out of bounds access to card marking array"); 160 size_t delta = pointer_delta(p, byte_map_base, sizeof(jbyte)); 161 HeapWord* result = (HeapWord*) (delta << card_shift); 162 assert(_whole_heap.contains(result), 163 "out of bounds accessor from card marking array"); 164 return result; 165 } 166 167 // Iterate over the portion of the card-table which covers the given 168 // region mr in the given space and apply cl to any dirty sub-regions 169 // of mr. cl and dcto_cl must either be the same closure or cl must 170 // wrap dcto_cl. Both are required - neither may be NULL. Also, dcto_cl 171 // may be modified. Note that this function will operate in a parallel 172 // mode if worker threads are available. 173 void non_clean_card_iterate(Space* sp, MemRegion mr, 174 DirtyCardToOopClosure* dcto_cl, 175 MemRegionClosure* cl, 176 bool clear); 177 178 // Utility function used to implement the other versions below. 179 void non_clean_card_iterate_work(MemRegion mr, MemRegionClosure* cl, 180 bool clear); 181 182 void par_non_clean_card_iterate_work(Space* sp, MemRegion mr, 183 DirtyCardToOopClosure* dcto_cl, 184 MemRegionClosure* cl, 185 bool clear, 186 int n_threads); 187 188 // Dirty the bytes corresponding to "mr" (not all of which must be 189 // covered.) 190 void dirty_MemRegion(MemRegion mr); 191 192 // Clear (to clean_card) the bytes entirely contained within "mr" (not 193 // all of which must be covered.) 194 void clear_MemRegion(MemRegion mr); 195 196 // *** Support for parallel card scanning. 197 198 enum SomeConstantsForParallelism { 199 StridesPerThread = 2, 200 CardsPerStrideChunk = 256 201 }; 202 203 // This is an array, one element per covered region of the card table. 204 // Each entry is itself an array, with one element per chunk in the 205 // covered region. Each entry of these arrays is the lowest non-clean 206 // card of the corresponding chunk containing part of an object from the 207 // previous chunk, or else NULL. 208 typedef jbyte* CardPtr; 209 typedef CardPtr* CardArr; 210 CardArr* _lowest_non_clean; 211 size_t* _lowest_non_clean_chunk_size; 212 uintptr_t* _lowest_non_clean_base_chunk_index; 213 int* _last_LNC_resizing_collection; 214 215 // Initializes "lowest_non_clean" to point to the array for the region 216 // covering "sp", and "lowest_non_clean_base_chunk_index" to the chunk 217 // index of the corresponding to the first element of that array. 218 // Ensures that these arrays are of sufficient size, allocating if necessary. 219 // May be called by several threads concurrently. 220 void get_LNC_array_for_space(Space* sp, 221 jbyte**& lowest_non_clean, 222 uintptr_t& lowest_non_clean_base_chunk_index, 223 size_t& lowest_non_clean_chunk_size); 224 225 // Returns the number of chunks necessary to cover "mr". 226 size_t chunks_to_cover(MemRegion mr) { 227 return (size_t)(addr_to_chunk_index(mr.last()) - 228 addr_to_chunk_index(mr.start()) + 1); 229 } 230 231 // Returns the index of the chunk in a stride which 232 // covers the given address. 233 uintptr_t addr_to_chunk_index(const void* addr) { 234 uintptr_t card = (uintptr_t) byte_for(addr); 235 return card / CardsPerStrideChunk; 236 } 237 238 // Apply cl, which must either itself apply dcto_cl or be dcto_cl, 239 // to the cards in the stride (of n_strides) within the given space. 240 void process_stride(Space* sp, 241 MemRegion used, 242 jint stride, int n_strides, 243 DirtyCardToOopClosure* dcto_cl, 244 MemRegionClosure* cl, 245 bool clear, 246 jbyte** lowest_non_clean, 247 uintptr_t lowest_non_clean_base_chunk_index, 248 size_t lowest_non_clean_chunk_size); 249 250 // Makes sure that chunk boundaries are handled appropriately, by 251 // adjusting the min_done of dcto_cl, and by using a special card-table 252 // value to indicate how min_done should be set. 253 void process_chunk_boundaries(Space* sp, 254 DirtyCardToOopClosure* dcto_cl, 255 MemRegion chunk_mr, 256 MemRegion used, 257 jbyte** lowest_non_clean, 258 uintptr_t lowest_non_clean_base_chunk_index, 259 size_t lowest_non_clean_chunk_size); 260 261 public: 262 // Constants 263 enum SomePublicConstants { 264 card_shift = 9, 265 card_size = 1 << card_shift, 266 card_size_in_words = card_size / sizeof(HeapWord) 267 }; 268 269 // For RTTI simulation. 270 BarrierSet::Name kind() { return BarrierSet::CardTableModRef; } 271 bool is_a(BarrierSet::Name bsn) { 272 return bsn == BarrierSet::CardTableModRef || bsn == BarrierSet::ModRef; 273 } 274 275 CardTableModRefBS(MemRegion whole_heap, int max_covered_regions); 276 277 // *** Barrier set functions. 278 279 inline bool write_ref_needs_barrier(oop* field, oop new_val) { 280 // Note that this assumes the perm gen is the highest generation 281 // in the address space 282 return new_val != NULL && !new_val->is_perm(); 283 } 284 285 // Record a reference update. Note that these versions are precise! 286 // The scanning code has to handle the fact that the write barrier may be 287 // either precise or imprecise. We make non-virtual inline variants of 288 // these functions here for performance. 289 protected: 290 void write_ref_field_work(oop obj, size_t offset, oop newVal); 291 void write_ref_field_work(oop* field, oop newVal); 292 public: 293 294 bool has_write_ref_array_opt() { return true; } 295 bool has_write_region_opt() { return true; } 296 297 inline void inline_write_region(MemRegion mr) { 298 dirty_MemRegion(mr); 299 } 300 protected: 301 void write_region_work(MemRegion mr) { 302 inline_write_region(mr); 303 } 304 public: 305 306 inline void inline_write_ref_array(MemRegion mr) { 307 dirty_MemRegion(mr); 308 } 309 protected: 310 void write_ref_array_work(MemRegion mr) { 311 inline_write_ref_array(mr); 312 } 313 public: 314 315 bool is_aligned(HeapWord* addr) { 316 return is_card_aligned(addr); 317 } 318 319 // *** Card-table-barrier-specific things. 320 321 inline void inline_write_ref_field(oop* field, oop newVal) { 322 jbyte* byte = byte_for(field); 323 *byte = dirty_card; 324 } 325 326 // Card marking array base (adjusted for heap low boundary) 327 // This would be the 0th element of _byte_map, if the heap started at 0x0. 328 // But since the heap starts at some higher address, this points to somewhere 329 // before the beginning of the actual _byte_map. 330 jbyte* byte_map_base; 331 332 // Return true if "p" is at the start of a card. 333 bool is_card_aligned(HeapWord* p) { 334 jbyte* pcard = byte_for(p); 335 return (addr_for(pcard) == p); 336 } 337 338 // The kinds of precision a CardTableModRefBS may offer. 339 enum PrecisionStyle { 340 Precise, 341 ObjHeadPreciseArray 342 }; 343 344 // Tells what style of precision this card table offers. 345 PrecisionStyle precision() { 346 return ObjHeadPreciseArray; // Only one supported for now. 347 } 348 349 // ModRefBS functions. 350 void invalidate(MemRegion mr); 351 void clear(MemRegion mr); 352 void mod_oop_in_space_iterate(Space* sp, OopClosure* cl, 353 bool clear = false, 354 bool before_save_marks = false); 355 356 // *** Card-table-RemSet-specific things. 357 358 // Invoke "cl.do_MemRegion" on a set of MemRegions that collectively 359 // includes all the modified cards (expressing each card as a 360 // MemRegion). Thus, several modified cards may be lumped into one 361 // region. The regions are non-overlapping, and are visited in 362 // *decreasing* address order. (This order aids with imprecise card 363 // marking, where a dirty card may cause scanning, and summarization 364 // marking, of objects that extend onto subsequent cards.) 365 // If "clear" is true, the card is (conceptually) marked unmodified before 366 // applying the closure. 367 void mod_card_iterate(MemRegionClosure* cl, bool clear = false) { 368 non_clean_card_iterate_work(_whole_heap, cl, clear); 369 } 370 371 // Like the "mod_cards_iterate" above, except only invokes the closure 372 // for cards within the MemRegion "mr" (which is required to be 373 // card-aligned and sized.) 374 void mod_card_iterate(MemRegion mr, MemRegionClosure* cl, 375 bool clear = false) { 376 non_clean_card_iterate_work(mr, cl, clear); 377 } 378 379 static uintx ct_max_alignment_constraint(); 380 381 // Apply closure cl to the dirty cards lying completely 382 // within MemRegion mr, setting the cards to precleaned. 383 void dirty_card_iterate(MemRegion mr, MemRegionClosure* cl); 384 385 // Return the MemRegion corresponding to the first maximal run 386 // of dirty cards lying completely within MemRegion mr, after 387 // marking those cards precleaned. 388 MemRegion dirty_card_range_after_preclean(MemRegion mr); 389 390 // Set all the dirty cards in the given region to precleaned state. 391 void preclean_dirty_cards(MemRegion mr); 392 393 // Mapping from address to card marking array index. 394 int index_for(void* p) { 395 assert(_whole_heap.contains(p), 396 "out of bounds access to card marking array"); 397 return byte_for(p) - _byte_map; 398 } 399 400 void verify(); 401 void verify_guard(); 402 403 void verify_clean_region(MemRegion mr) PRODUCT_RETURN; 404 405 static size_t par_chunk_heapword_alignment() { 406 return CardsPerStrideChunk * card_size_in_words; 407 } 408 }; 409 410 class CardTableRS; 411 412 // A specialization for the CardTableRS gen rem set. 413 class CardTableModRefBSForCTRS: public CardTableModRefBS { 414 CardTableRS* _rs; 415 protected: 416 bool card_will_be_scanned(jbyte cv); 417 bool card_may_have_been_dirty(jbyte cv); 418 public: 419 CardTableModRefBSForCTRS(MemRegion whole_heap, 420 int max_covered_regions) : 421 CardTableModRefBS(whole_heap, max_covered_regions) {} 422 423 void set_CTRS(CardTableRS* rs) { _rs = rs; } 424 };