1 /* 2 * Copyright (c) 2007, 2015, 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 #include "precompiled.hpp" 26 #include "gc/shared/cardTableModRefBS.hpp" 27 #include "gc/shared/cardTableRS.hpp" 28 #include "gc/shared/collectedHeap.hpp" 29 #include "gc/shared/genCollectedHeap.hpp" 30 #include "gc/shared/space.inline.hpp" 31 #include "memory/allocation.inline.hpp" 32 #include "memory/virtualspace.hpp" 33 #include "oops/oop.inline.hpp" 34 #include "runtime/java.hpp" 35 #include "runtime/mutexLocker.hpp" 36 #include "runtime/orderAccess.inline.hpp" 37 #include "runtime/vmThread.hpp" 38 39 // TODO: Split out CMS parts of CardTableRS to a CMS-owned card table class 40 41 void CardTableRS:: 42 non_clean_card_iterate_parallel_work(Space* sp, MemRegion mr, 43 OopsInGenClosure* cl, 44 CardTableRS* ct, 45 uint n_threads) { 46 assert(n_threads > 0, "expected n_threads > 0"); 47 assert(n_threads <= ParallelGCThreads, 48 "n_threads: %u > ParallelGCThreads: %u", n_threads, ParallelGCThreads); 49 50 // Make sure the LNC array is valid for the space. 51 jbyte** lowest_non_clean; 52 uintptr_t lowest_non_clean_base_chunk_index; 53 size_t lowest_non_clean_chunk_size; 54 get_LNC_array_for_space(sp, lowest_non_clean, 55 lowest_non_clean_base_chunk_index, 56 lowest_non_clean_chunk_size); 57 58 uint n_strides = n_threads * ParGCStridesPerThread; 59 SequentialSubTasksDone* pst = sp->par_seq_tasks(); 60 // Sets the condition for completion of the subtask (how many threads 61 // need to finish in order to be done). 62 pst->set_n_threads(n_threads); 63 pst->set_n_tasks(n_strides); 64 65 uint stride = 0; 66 while (!pst->is_task_claimed(/* reference */ stride)) { 67 process_stride(sp, mr, stride, n_strides, 68 cl, ct, 69 lowest_non_clean, 70 lowest_non_clean_base_chunk_index, 71 lowest_non_clean_chunk_size); 72 } 73 if (pst->all_tasks_completed()) { 74 // Clear lowest_non_clean array for next time. 75 intptr_t first_chunk_index = addr_to_chunk_index(mr.start()); 76 uintptr_t last_chunk_index = addr_to_chunk_index(mr.last()); 77 for (uintptr_t ch = first_chunk_index; ch <= last_chunk_index; ch++) { 78 intptr_t ind = ch - lowest_non_clean_base_chunk_index; 79 assert(0 <= ind && ind < (intptr_t)lowest_non_clean_chunk_size, 80 "Bounds error"); 81 lowest_non_clean[ind] = NULL; 82 } 83 } 84 } 85 86 void 87 CardTableRS:: 88 process_stride(Space* sp, 89 MemRegion used, 90 jint stride, int n_strides, 91 OopsInGenClosure* cl, 92 CardTableRS* ct, 93 jbyte** lowest_non_clean, 94 uintptr_t lowest_non_clean_base_chunk_index, 95 size_t lowest_non_clean_chunk_size) { 96 // We go from higher to lower addresses here; it wouldn't help that much 97 // because of the strided parallelism pattern used here. 98 99 // Find the first card address of the first chunk in the stride that is 100 // at least "bottom" of the used region. 101 jbyte* start_card = byte_for(used.start()); 102 jbyte* end_card = byte_after(used.last()); 103 uintptr_t start_chunk = addr_to_chunk_index(used.start()); 104 uintptr_t start_chunk_stride_num = start_chunk % n_strides; 105 jbyte* chunk_card_start; 106 107 if ((uintptr_t)stride >= start_chunk_stride_num) { 108 chunk_card_start = (jbyte*)(start_card + 109 (stride - start_chunk_stride_num) * 110 ParGCCardsPerStrideChunk); 111 } else { 112 // Go ahead to the next chunk group boundary, then to the requested stride. 113 chunk_card_start = (jbyte*)(start_card + 114 (n_strides - start_chunk_stride_num + stride) * 115 ParGCCardsPerStrideChunk); 116 } 117 118 while (chunk_card_start < end_card) { 119 // Even though we go from lower to higher addresses below, the 120 // strided parallelism can interleave the actual processing of the 121 // dirty pages in various ways. For a specific chunk within this 122 // stride, we take care to avoid double scanning or missing a card 123 // by suitably initializing the "min_done" field in process_chunk_boundaries() 124 // below, together with the dirty region extension accomplished in 125 // DirtyCardToOopClosure::do_MemRegion(). 126 jbyte* chunk_card_end = chunk_card_start + ParGCCardsPerStrideChunk; 127 // Invariant: chunk_mr should be fully contained within the "used" region. 128 MemRegion chunk_mr = MemRegion(addr_for(chunk_card_start), 129 chunk_card_end >= end_card ? 130 used.end() : addr_for(chunk_card_end)); 131 assert(chunk_mr.word_size() > 0, "[chunk_card_start > used_end)"); 132 assert(used.contains(chunk_mr), "chunk_mr should be subset of used"); 133 134 // This function is used by the parallel card table iteration. 135 const bool parallel = true; 136 137 DirtyCardToOopClosure* dcto_cl = sp->new_dcto_cl(cl, precision(), 138 cl->gen_boundary(), 139 parallel); 140 ClearNoncleanCardWrapper clear_cl(dcto_cl, ct, parallel); 141 142 143 // Process the chunk. 144 process_chunk_boundaries(sp, 145 dcto_cl, 146 chunk_mr, 147 used, 148 lowest_non_clean, 149 lowest_non_clean_base_chunk_index, 150 lowest_non_clean_chunk_size); 151 152 // We want the LNC array updates above in process_chunk_boundaries 153 // to be visible before any of the card table value changes as a 154 // result of the dirty card iteration below. 155 OrderAccess::storestore(); 156 157 // We want to clear the cards: clear_cl here does the work of finding 158 // contiguous dirty ranges of cards to process and clear. 159 clear_cl.do_MemRegion(chunk_mr); 160 161 // Find the next chunk of the stride. 162 chunk_card_start += ParGCCardsPerStrideChunk * n_strides; 163 } 164 } 165 166 void 167 CardTableRS:: 168 process_chunk_boundaries(Space* sp, 169 DirtyCardToOopClosure* dcto_cl, 170 MemRegion chunk_mr, 171 MemRegion used, 172 jbyte** lowest_non_clean, 173 uintptr_t lowest_non_clean_base_chunk_index, 174 size_t lowest_non_clean_chunk_size) 175 { 176 // We must worry about non-array objects that cross chunk boundaries, 177 // because such objects are both precisely and imprecisely marked: 178 // .. if the head of such an object is dirty, the entire object 179 // needs to be scanned, under the interpretation that this 180 // was an imprecise mark 181 // .. if the head of such an object is not dirty, we can assume 182 // precise marking and it's efficient to scan just the dirty 183 // cards. 184 // In either case, each scanned reference must be scanned precisely 185 // once so as to avoid cloning of a young referent. For efficiency, 186 // our closures depend on this property and do not protect against 187 // double scans. 188 189 uintptr_t start_chunk_index = addr_to_chunk_index(chunk_mr.start()); 190 assert(start_chunk_index >= lowest_non_clean_base_chunk_index, "Bounds error."); 191 uintptr_t cur_chunk_index = start_chunk_index - lowest_non_clean_base_chunk_index; 192 193 // First, set "our" lowest_non_clean entry, which would be 194 // used by the thread scanning an adjoining left chunk with 195 // a non-array object straddling the mutual boundary. 196 // Find the object that spans our boundary, if one exists. 197 // first_block is the block possibly straddling our left boundary. 198 HeapWord* first_block = sp->block_start(chunk_mr.start()); 199 assert((chunk_mr.start() != used.start()) || (first_block == chunk_mr.start()), 200 "First chunk should always have a co-initial block"); 201 // Does the block straddle the chunk's left boundary, and is it 202 // a non-array object? 203 if (first_block < chunk_mr.start() // first block straddles left bdry 204 && sp->block_is_obj(first_block) // first block is an object 205 && !(oop(first_block)->is_objArray() // first block is not an array (arrays are precisely dirtied) 206 || oop(first_block)->is_typeArray())) { 207 // Find our least non-clean card, so that a left neighbor 208 // does not scan an object straddling the mutual boundary 209 // too far to the right, and attempt to scan a portion of 210 // that object twice. 211 jbyte* first_dirty_card = NULL; 212 jbyte* last_card_of_first_obj = 213 byte_for(first_block + sp->block_size(first_block) - 1); 214 jbyte* first_card_of_cur_chunk = byte_for(chunk_mr.start()); 215 jbyte* last_card_of_cur_chunk = byte_for(chunk_mr.last()); 216 jbyte* last_card_to_check = 217 (jbyte*) MIN2((intptr_t) last_card_of_cur_chunk, 218 (intptr_t) last_card_of_first_obj); 219 // Note that this does not need to go beyond our last card 220 // if our first object completely straddles this chunk. 221 for (jbyte* cur = first_card_of_cur_chunk; 222 cur <= last_card_to_check; cur++) { 223 jbyte val = *cur; 224 if (card_will_be_scanned(val)) { 225 first_dirty_card = cur; break; 226 } else { 227 assert(!card_may_have_been_dirty(val), "Error"); 228 } 229 } 230 if (first_dirty_card != NULL) { 231 assert(cur_chunk_index < lowest_non_clean_chunk_size, "Bounds error."); 232 assert(lowest_non_clean[cur_chunk_index] == NULL, 233 "Write exactly once : value should be stable hereafter for this round"); 234 lowest_non_clean[cur_chunk_index] = first_dirty_card; 235 } 236 } else { 237 // In this case we can help our neighbor by just asking them 238 // to stop at our first card (even though it may not be dirty). 239 assert(lowest_non_clean[cur_chunk_index] == NULL, "Write once : value should be stable hereafter"); 240 jbyte* first_card_of_cur_chunk = byte_for(chunk_mr.start()); 241 lowest_non_clean[cur_chunk_index] = first_card_of_cur_chunk; 242 } 243 244 // Next, set our own max_to_do, which will strictly/exclusively bound 245 // the highest address that we will scan past the right end of our chunk. 246 HeapWord* max_to_do = NULL; 247 if (chunk_mr.end() < used.end()) { 248 // This is not the last chunk in the used region. 249 // What is our last block? We check the first block of 250 // the next (right) chunk rather than strictly check our last block 251 // because it's potentially more efficient to do so. 252 HeapWord* const last_block = sp->block_start(chunk_mr.end()); 253 assert(last_block <= chunk_mr.end(), "In case this property changes."); 254 if ((last_block == chunk_mr.end()) // our last block does not straddle boundary 255 || !sp->block_is_obj(last_block) // last_block isn't an object 256 || oop(last_block)->is_objArray() // last_block is an array (precisely marked) 257 || oop(last_block)->is_typeArray()) { 258 max_to_do = chunk_mr.end(); 259 } else { 260 assert(last_block < chunk_mr.end(), "Tautology"); 261 // It is a non-array object that straddles the right boundary of this chunk. 262 // last_obj_card is the card corresponding to the start of the last object 263 // in the chunk. Note that the last object may not start in 264 // the chunk. 265 jbyte* const last_obj_card = byte_for(last_block); 266 const jbyte val = *last_obj_card; 267 if (!card_will_be_scanned(val)) { 268 assert(!card_may_have_been_dirty(val), "Error"); 269 // The card containing the head is not dirty. Any marks on 270 // subsequent cards still in this chunk must have been made 271 // precisely; we can cap processing at the end of our chunk. 272 max_to_do = chunk_mr.end(); 273 } else { 274 // The last object must be considered dirty, and extends onto the 275 // following chunk. Look for a dirty card in that chunk that will 276 // bound our processing. 277 jbyte* limit_card = NULL; 278 const size_t last_block_size = sp->block_size(last_block); 279 jbyte* const last_card_of_last_obj = 280 byte_for(last_block + last_block_size - 1); 281 jbyte* const first_card_of_next_chunk = byte_for(chunk_mr.end()); 282 // This search potentially goes a long distance looking 283 // for the next card that will be scanned, terminating 284 // at the end of the last_block, if no earlier dirty card 285 // is found. 286 assert(byte_for(chunk_mr.end()) - byte_for(chunk_mr.start()) == ParGCCardsPerStrideChunk, 287 "last card of next chunk may be wrong"); 288 for (jbyte* cur = first_card_of_next_chunk; 289 cur <= last_card_of_last_obj; cur++) { 290 const jbyte val = *cur; 291 if (card_will_be_scanned(val)) { 292 limit_card = cur; break; 293 } else { 294 assert(!card_may_have_been_dirty(val), "Error: card can't be skipped"); 295 } 296 } 297 if (limit_card != NULL) { 298 max_to_do = addr_for(limit_card); 299 assert(limit_card != NULL && max_to_do != NULL, "Error"); 300 } else { 301 // The following is a pessimistic value, because it's possible 302 // that a dirty card on a subsequent chunk has been cleared by 303 // the time we get to look at it; we'll correct for that further below, 304 // using the LNC array which records the least non-clean card 305 // before cards were cleared in a particular chunk. 306 limit_card = last_card_of_last_obj; 307 max_to_do = last_block + last_block_size; 308 assert(limit_card != NULL && max_to_do != NULL, "Error"); 309 } 310 assert(0 < cur_chunk_index+1 && cur_chunk_index+1 < lowest_non_clean_chunk_size, 311 "Bounds error."); 312 // It is possible that a dirty card for the last object may have been 313 // cleared before we had a chance to examine it. In that case, the value 314 // will have been logged in the LNC for that chunk. 315 // We need to examine as many chunks to the right as this object 316 // covers. However, we need to bound this checking to the largest 317 // entry in the LNC array: this is because the heap may expand 318 // after the LNC array has been created but before we reach this point, 319 // and the last block in our chunk may have been expanded to include 320 // the expansion delta (and possibly subsequently allocated from, so 321 // it wouldn't be sufficient to check whether that last block was 322 // or was not an object at this point). 323 uintptr_t last_chunk_index_to_check = addr_to_chunk_index(last_block + last_block_size - 1) 324 - lowest_non_clean_base_chunk_index; 325 const uintptr_t last_chunk_index = addr_to_chunk_index(used.last()) 326 - lowest_non_clean_base_chunk_index; 327 if (last_chunk_index_to_check > last_chunk_index) { 328 assert(last_block + last_block_size > used.end(), 329 "Inconsistency detected: last_block [" PTR_FORMAT "," PTR_FORMAT "]" 330 " does not exceed used.end() = " PTR_FORMAT "," 331 " yet last_chunk_index_to_check " INTPTR_FORMAT 332 " exceeds last_chunk_index " INTPTR_FORMAT, 333 p2i(last_block), p2i(last_block + last_block_size), 334 p2i(used.end()), 335 last_chunk_index_to_check, last_chunk_index); 336 assert(sp->used_region().end() > used.end(), 337 "Expansion did not happen: " 338 "[" PTR_FORMAT "," PTR_FORMAT ") -> [" PTR_FORMAT "," PTR_FORMAT ")", 339 p2i(sp->used_region().start()), p2i(sp->used_region().end()), 340 p2i(used.start()), p2i(used.end())); 341 last_chunk_index_to_check = last_chunk_index; 342 } 343 for (uintptr_t lnc_index = cur_chunk_index + 1; 344 lnc_index <= last_chunk_index_to_check; 345 lnc_index++) { 346 jbyte* lnc_card = lowest_non_clean[lnc_index]; 347 if (lnc_card != NULL) { 348 // we can stop at the first non-NULL entry we find 349 if (lnc_card <= limit_card) { 350 limit_card = lnc_card; 351 max_to_do = addr_for(limit_card); 352 assert(limit_card != NULL && max_to_do != NULL, "Error"); 353 } 354 // In any case, we break now 355 break; 356 } // else continue to look for a non-NULL entry if any 357 } 358 assert(limit_card != NULL && max_to_do != NULL, "Error"); 359 } 360 assert(max_to_do != NULL, "OOPS 1 !"); 361 } 362 assert(max_to_do != NULL, "OOPS 2!"); 363 } else { 364 max_to_do = used.end(); 365 } 366 assert(max_to_do != NULL, "OOPS 3!"); 367 // Now we can set the closure we're using so it doesn't to beyond 368 // max_to_do. 369 dcto_cl->set_min_done(max_to_do); 370 #ifndef PRODUCT 371 dcto_cl->set_last_bottom(max_to_do); 372 #endif 373 } 374 375 void 376 CardTableRS:: 377 get_LNC_array_for_space(Space* sp, 378 jbyte**& lowest_non_clean, 379 uintptr_t& lowest_non_clean_base_chunk_index, 380 size_t& lowest_non_clean_chunk_size) { 381 382 int i = find_covering_region_containing(sp->bottom()); 383 MemRegion covered = _covered[i]; 384 size_t n_chunks = chunks_to_cover(covered); 385 386 // Only the first thread to obtain the lock will resize the 387 // LNC array for the covered region. Any later expansion can't affect 388 // the used_at_save_marks region. 389 // (I observed a bug in which the first thread to execute this would 390 // resize, and then it would cause "expand_and_allocate" that would 391 // increase the number of chunks in the covered region. Then a second 392 // thread would come and execute this, see that the size didn't match, 393 // and free and allocate again. So the first thread would be using a 394 // freed "_lowest_non_clean" array.) 395 396 // Do a dirty read here. If we pass the conditional then take the rare 397 // event lock and do the read again in case some other thread had already 398 // succeeded and done the resize. 399 int cur_collection = GenCollectedHeap::heap()->total_collections(); 400 // Updated _last_LNC_resizing_collection[i] must not be visible before 401 // _lowest_non_clean and friends are visible. Therefore use acquire/release 402 // to guarantee this on non TSO architecures. 403 if (OrderAccess::load_acquire(&_last_LNC_resizing_collection[i]) != cur_collection) { 404 MutexLocker x(ParGCRareEvent_lock); 405 // This load_acquire is here for clarity only. The MutexLocker already fences. 406 if (OrderAccess::load_acquire(&_last_LNC_resizing_collection[i]) != cur_collection) { 407 if (_lowest_non_clean[i] == NULL || 408 n_chunks != _lowest_non_clean_chunk_size[i]) { 409 410 // Should we delete the old? 411 if (_lowest_non_clean[i] != NULL) { 412 assert(n_chunks != _lowest_non_clean_chunk_size[i], 413 "logical consequence"); 414 FREE_C_HEAP_ARRAY(CardPtr, _lowest_non_clean[i]); 415 _lowest_non_clean[i] = NULL; 416 } 417 // Now allocate a new one if necessary. 418 if (_lowest_non_clean[i] == NULL) { 419 _lowest_non_clean[i] = NEW_C_HEAP_ARRAY(CardPtr, n_chunks, mtGC); 420 _lowest_non_clean_chunk_size[i] = n_chunks; 421 _lowest_non_clean_base_chunk_index[i] = addr_to_chunk_index(covered.start()); 422 for (int j = 0; j < (int)n_chunks; j++) 423 _lowest_non_clean[i][j] = NULL; 424 } 425 } 426 // Make sure this gets visible only after _lowest_non_clean* was initialized 427 OrderAccess::release_store(&_last_LNC_resizing_collection[i], cur_collection); 428 } 429 } 430 // In any case, now do the initialization. 431 lowest_non_clean = _lowest_non_clean[i]; 432 lowest_non_clean_base_chunk_index = _lowest_non_clean_base_chunk_index[i]; 433 lowest_non_clean_chunk_size = _lowest_non_clean_chunk_size[i]; 434 }