1 /* 2 * Copyright (c) 2001, 2012, 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_implementation/g1/g1BlockOffsetTable.inline.hpp" 27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" 28 #include "gc_implementation/g1/g1OopClosures.inline.hpp" 29 #include "gc_implementation/g1/heapRegion.inline.hpp" 30 #include "gc_implementation/g1/heapRegionRemSet.hpp" 31 #include "gc_implementation/g1/heapRegionSeq.inline.hpp" 32 #include "memory/genOopClosures.inline.hpp" 33 #include "memory/iterator.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "utilities/growableArray.hpp" 36 37 int HeapRegion::LogOfHRGrainBytes = 0; 38 int HeapRegion::LogOfHRGrainWords = 0; 39 size_t HeapRegion::GrainBytes = 0; 40 size_t HeapRegion::GrainWords = 0; 41 size_t HeapRegion::CardsPerRegion = 0; 42 43 HeapRegionDCTOC::HeapRegionDCTOC(G1CollectedHeap* g1, 44 HeapRegion* hr, ExtendedOopClosure* cl, 45 CardTableModRefBS::PrecisionStyle precision, 46 FilterKind fk) : 47 ContiguousSpaceDCTOC(hr, cl, precision, NULL), 48 _hr(hr), _fk(fk), _g1(g1) { } 49 50 FilterOutOfRegionClosure::FilterOutOfRegionClosure(HeapRegion* r, 51 OopClosure* oc) : 52 _r_bottom(r->bottom()), _r_end(r->end()), _oc(oc) { } 53 54 template<class ClosureType> 55 HeapWord* walk_mem_region_loop(ClosureType* cl, G1CollectedHeap* g1h, 56 HeapRegion* hr, 57 HeapWord* cur, HeapWord* top) { 58 oop cur_oop = oop(cur); 59 int oop_size = cur_oop->size(); 60 HeapWord* next_obj = cur + oop_size; 61 while (next_obj < top) { 62 // Keep filtering the remembered set. 63 if (!g1h->is_obj_dead(cur_oop, hr)) { 64 // Bottom lies entirely below top, so we can call the 65 // non-memRegion version of oop_iterate below. 66 cur_oop->oop_iterate(cl); 67 } 68 cur = next_obj; 69 cur_oop = oop(cur); 70 oop_size = cur_oop->size(); 71 next_obj = cur + oop_size; 72 } 73 return cur; 74 } 75 76 void HeapRegionDCTOC::walk_mem_region_with_cl(MemRegion mr, 77 HeapWord* bottom, 78 HeapWord* top, 79 ExtendedOopClosure* cl) { 80 G1CollectedHeap* g1h = _g1; 81 int oop_size; 82 ExtendedOopClosure* cl2 = NULL; 83 84 FilterIntoCSClosure intoCSFilt(this, g1h, cl); 85 FilterOutOfRegionClosure outOfRegionFilt(_hr, cl); 86 87 switch (_fk) { 88 case NoFilterKind: cl2 = cl; break; 89 case IntoCSFilterKind: cl2 = &intoCSFilt; break; 90 case OutOfRegionFilterKind: cl2 = &outOfRegionFilt; break; 91 default: ShouldNotReachHere(); 92 } 93 94 // Start filtering what we add to the remembered set. If the object is 95 // not considered dead, either because it is marked (in the mark bitmap) 96 // or it was allocated after marking finished, then we add it. Otherwise 97 // we can safely ignore the object. 98 if (!g1h->is_obj_dead(oop(bottom), _hr)) { 99 oop_size = oop(bottom)->oop_iterate(cl2, mr); 100 } else { 101 oop_size = oop(bottom)->size(); 102 } 103 104 bottom += oop_size; 105 106 if (bottom < top) { 107 // We replicate the loop below for several kinds of possible filters. 108 switch (_fk) { 109 case NoFilterKind: 110 bottom = walk_mem_region_loop(cl, g1h, _hr, bottom, top); 111 break; 112 113 case IntoCSFilterKind: { 114 FilterIntoCSClosure filt(this, g1h, cl); 115 bottom = walk_mem_region_loop(&filt, g1h, _hr, bottom, top); 116 break; 117 } 118 119 case OutOfRegionFilterKind: { 120 FilterOutOfRegionClosure filt(_hr, cl); 121 bottom = walk_mem_region_loop(&filt, g1h, _hr, bottom, top); 122 break; 123 } 124 125 default: 126 ShouldNotReachHere(); 127 } 128 129 // Last object. Need to do dead-obj filtering here too. 130 if (!g1h->is_obj_dead(oop(bottom), _hr)) { 131 oop(bottom)->oop_iterate(cl2, mr); 132 } 133 } 134 } 135 136 // Minimum region size; we won't go lower than that. 137 // We might want to decrease this in the future, to deal with small 138 // heaps a bit more efficiently. 139 #define MIN_REGION_SIZE ( 1024 * 1024 ) 140 141 // Maximum region size; we don't go higher than that. There's a good 142 // reason for having an upper bound. We don't want regions to get too 143 // large, otherwise cleanup's effectiveness would decrease as there 144 // will be fewer opportunities to find totally empty regions after 145 // marking. 146 #define MAX_REGION_SIZE ( 32 * 1024 * 1024 ) 147 148 // The automatic region size calculation will try to have around this 149 // many regions in the heap (based on the min heap size). 150 #define TARGET_REGION_NUMBER 2048 151 152 void HeapRegion::setup_heap_region_size(uintx min_heap_size) { 153 // region_size in bytes 154 uintx region_size = G1HeapRegionSize; 155 if (FLAG_IS_DEFAULT(G1HeapRegionSize)) { 156 // We base the automatic calculation on the min heap size. This 157 // can be problematic if the spread between min and max is quite 158 // wide, imagine -Xms128m -Xmx32g. But, if we decided it based on 159 // the max size, the region size might be way too large for the 160 // min size. Either way, some users might have to set the region 161 // size manually for some -Xms / -Xmx combos. 162 163 region_size = MAX2(min_heap_size / TARGET_REGION_NUMBER, 164 (uintx) MIN_REGION_SIZE); 165 } 166 167 int region_size_log = log2_long((jlong) region_size); 168 // Recalculate the region size to make sure it's a power of 169 // 2. This means that region_size is the largest power of 2 that's 170 // <= what we've calculated so far. 171 region_size = ((uintx)1 << region_size_log); 172 173 // Now make sure that we don't go over or under our limits. 174 if (region_size < MIN_REGION_SIZE) { 175 region_size = MIN_REGION_SIZE; 176 } else if (region_size > MAX_REGION_SIZE) { 177 region_size = MAX_REGION_SIZE; 178 } 179 180 // And recalculate the log. 181 region_size_log = log2_long((jlong) region_size); 182 183 // Now, set up the globals. 184 guarantee(LogOfHRGrainBytes == 0, "we should only set it once"); 185 LogOfHRGrainBytes = region_size_log; 186 187 guarantee(LogOfHRGrainWords == 0, "we should only set it once"); 188 LogOfHRGrainWords = LogOfHRGrainBytes - LogHeapWordSize; 189 190 guarantee(GrainBytes == 0, "we should only set it once"); 191 // The cast to int is safe, given that we've bounded region_size by 192 // MIN_REGION_SIZE and MAX_REGION_SIZE. 193 GrainBytes = (size_t)region_size; 194 195 guarantee(GrainWords == 0, "we should only set it once"); 196 GrainWords = GrainBytes >> LogHeapWordSize; 197 guarantee((size_t) 1 << LogOfHRGrainWords == GrainWords, "sanity"); 198 199 guarantee(CardsPerRegion == 0, "we should only set it once"); 200 CardsPerRegion = GrainBytes >> CardTableModRefBS::card_shift; 201 } 202 203 void HeapRegion::reset_after_compaction() { 204 G1OffsetTableContigSpace::reset_after_compaction(); 205 // After a compaction the mark bitmap is invalid, so we must 206 // treat all objects as being inside the unmarked area. 207 zero_marked_bytes(); 208 init_top_at_mark_start(); 209 } 210 211 void HeapRegion::hr_clear(bool par, bool clear_space) { 212 assert(_humongous_type == NotHumongous, 213 "we should have already filtered out humongous regions"); 214 assert(_humongous_start_region == NULL, 215 "we should have already filtered out humongous regions"); 216 assert(_end == _orig_end, 217 "we should have already filtered out humongous regions"); 218 219 _in_collection_set = false; 220 221 set_young_index_in_cset(-1); 222 uninstall_surv_rate_group(); 223 set_young_type(NotYoung); 224 reset_pre_dummy_top(); 225 226 if (!par) { 227 // If this is parallel, this will be done later. 228 HeapRegionRemSet* hrrs = rem_set(); 229 if (hrrs != NULL) hrrs->clear(); 230 _claimed = InitialClaimValue; 231 } 232 zero_marked_bytes(); 233 234 _offsets.resize(HeapRegion::GrainWords); 235 init_top_at_mark_start(); 236 237 if (_strong_code_root_list != NULL) { 238 delete _strong_code_root_list; 239 } 240 _strong_code_root_list = new (ResourceObj::C_HEAP, mtGC) 241 GrowableArray<nmethod*>(10, 0, NULL, true); 242 243 if (clear_space) clear(SpaceDecorator::Mangle); 244 } 245 246 void HeapRegion::par_clear() { 247 assert(used() == 0, "the region should have been already cleared"); 248 assert(capacity() == HeapRegion::GrainBytes, "should be back to normal"); 249 HeapRegionRemSet* hrrs = rem_set(); 250 hrrs->clear(); 251 CardTableModRefBS* ct_bs = 252 (CardTableModRefBS*)G1CollectedHeap::heap()->barrier_set(); 253 ct_bs->clear(MemRegion(bottom(), end())); 254 } 255 256 void HeapRegion::calc_gc_efficiency() { 257 // GC efficiency is the ratio of how much space would be 258 // reclaimed over how long we predict it would take to reclaim it. 259 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 260 G1CollectorPolicy* g1p = g1h->g1_policy(); 261 262 // Retrieve a prediction of the elapsed time for this region for 263 // a mixed gc because the region will only be evacuated during a 264 // mixed gc. 265 double region_elapsed_time_ms = 266 g1p->predict_region_elapsed_time_ms(this, false /* for_young_gc */); 267 _gc_efficiency = (double) reclaimable_bytes() / region_elapsed_time_ms; 268 } 269 270 void HeapRegion::set_startsHumongous(HeapWord* new_top, HeapWord* new_end) { 271 assert(!isHumongous(), "sanity / pre-condition"); 272 assert(end() == _orig_end, 273 "Should be normal before the humongous object allocation"); 274 assert(top() == bottom(), "should be empty"); 275 assert(bottom() <= new_top && new_top <= new_end, "pre-condition"); 276 277 _humongous_type = StartsHumongous; 278 _humongous_start_region = this; 279 280 set_end(new_end); 281 _offsets.set_for_starts_humongous(new_top); 282 } 283 284 void HeapRegion::set_continuesHumongous(HeapRegion* first_hr) { 285 assert(!isHumongous(), "sanity / pre-condition"); 286 assert(end() == _orig_end, 287 "Should be normal before the humongous object allocation"); 288 assert(top() == bottom(), "should be empty"); 289 assert(first_hr->startsHumongous(), "pre-condition"); 290 291 _humongous_type = ContinuesHumongous; 292 _humongous_start_region = first_hr; 293 } 294 295 void HeapRegion::set_notHumongous() { 296 assert(isHumongous(), "pre-condition"); 297 298 if (startsHumongous()) { 299 assert(top() <= end(), "pre-condition"); 300 set_end(_orig_end); 301 if (top() > end()) { 302 // at least one "continues humongous" region after it 303 set_top(end()); 304 } 305 } else { 306 // continues humongous 307 assert(end() == _orig_end, "sanity"); 308 } 309 310 assert(capacity() == HeapRegion::GrainBytes, "pre-condition"); 311 _humongous_type = NotHumongous; 312 _humongous_start_region = NULL; 313 } 314 315 bool HeapRegion::claimHeapRegion(jint claimValue) { 316 jint current = _claimed; 317 if (current != claimValue) { 318 jint res = Atomic::cmpxchg(claimValue, &_claimed, current); 319 if (res == current) { 320 return true; 321 } 322 } 323 return false; 324 } 325 326 HeapWord* HeapRegion::next_block_start_careful(HeapWord* addr) { 327 HeapWord* low = addr; 328 HeapWord* high = end(); 329 while (low < high) { 330 size_t diff = pointer_delta(high, low); 331 // Must add one below to bias toward the high amount. Otherwise, if 332 // "high" were at the desired value, and "low" were one less, we 333 // would not converge on "high". This is not symmetric, because 334 // we set "high" to a block start, which might be the right one, 335 // which we don't do for "low". 336 HeapWord* middle = low + (diff+1)/2; 337 if (middle == high) return high; 338 HeapWord* mid_bs = block_start_careful(middle); 339 if (mid_bs < addr) { 340 low = middle; 341 } else { 342 high = mid_bs; 343 } 344 } 345 assert(low == high && low >= addr, "Didn't work."); 346 return low; 347 } 348 349 #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away 350 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list 351 #endif // _MSC_VER 352 353 354 HeapRegion::HeapRegion(uint hrs_index, 355 G1BlockOffsetSharedArray* sharedOffsetArray, 356 MemRegion mr) : 357 G1OffsetTableContigSpace(sharedOffsetArray, mr), 358 _hrs_index(hrs_index), 359 _humongous_type(NotHumongous), _humongous_start_region(NULL), 360 _in_collection_set(false), 361 _next_in_special_set(NULL), _orig_end(NULL), 362 _claimed(InitialClaimValue), _evacuation_failed(false), 363 _prev_marked_bytes(0), _next_marked_bytes(0), _gc_efficiency(0.0), 364 _young_type(NotYoung), _next_young_region(NULL), 365 _next_dirty_cards_region(NULL), _next(NULL), _pending_removal(false), 366 #ifdef ASSERT 367 _containing_set(NULL), 368 #endif // ASSERT 369 _young_index_in_cset(-1), _surv_rate_group(NULL), _age_index(-1), 370 _rem_set(NULL), _recorded_rs_length(0), _predicted_elapsed_time_ms(0), 371 _predicted_bytes_to_copy(0), _strong_code_root_list(NULL) 372 { 373 _orig_end = mr.end(); 374 // Note that initialize() will set the start of the unmarked area of the 375 // region. 376 hr_clear(false /*par*/, false /*clear_space*/); 377 set_top(bottom()); 378 set_saved_mark(); 379 380 _rem_set = new HeapRegionRemSet(sharedOffsetArray, this); 381 382 assert(HeapRegionRemSet::num_par_rem_sets() > 0, "Invariant."); 383 } 384 385 CompactibleSpace* HeapRegion::next_compaction_space() const { 386 // We're not using an iterator given that it will wrap around when 387 // it reaches the last region and this is not what we want here. 388 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 389 uint index = hrs_index() + 1; 390 while (index < g1h->n_regions()) { 391 HeapRegion* hr = g1h->region_at(index); 392 if (!hr->isHumongous()) { 393 return hr; 394 } 395 index += 1; 396 } 397 return NULL; 398 } 399 400 void HeapRegion::save_marks() { 401 set_saved_mark(); 402 } 403 404 void HeapRegion::oops_in_mr_iterate(MemRegion mr, ExtendedOopClosure* cl) { 405 HeapWord* p = mr.start(); 406 HeapWord* e = mr.end(); 407 oop obj; 408 while (p < e) { 409 obj = oop(p); 410 p += obj->oop_iterate(cl); 411 } 412 assert(p == e, "bad memregion: doesn't end on obj boundary"); 413 } 414 415 #define HeapRegion_OOP_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \ 416 void HeapRegion::oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl) { \ 417 ContiguousSpace::oop_since_save_marks_iterate##nv_suffix(cl); \ 418 } 419 SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(HeapRegion_OOP_SINCE_SAVE_MARKS_DEFN) 420 421 422 void HeapRegion::oop_before_save_marks_iterate(ExtendedOopClosure* cl) { 423 oops_in_mr_iterate(MemRegion(bottom(), saved_mark_word()), cl); 424 } 425 426 void HeapRegion::note_self_forwarding_removal_start(bool during_initial_mark, 427 bool during_conc_mark) { 428 // We always recreate the prev marking info and we'll explicitly 429 // mark all objects we find to be self-forwarded on the prev 430 // bitmap. So all objects need to be below PTAMS. 431 _prev_top_at_mark_start = top(); 432 _prev_marked_bytes = 0; 433 434 if (during_initial_mark) { 435 // During initial-mark, we'll also explicitly mark all objects 436 // we find to be self-forwarded on the next bitmap. So all 437 // objects need to be below NTAMS. 438 _next_top_at_mark_start = top(); 439 _next_marked_bytes = 0; 440 } else if (during_conc_mark) { 441 // During concurrent mark, all objects in the CSet (including 442 // the ones we find to be self-forwarded) are implicitly live. 443 // So all objects need to be above NTAMS. 444 _next_top_at_mark_start = bottom(); 445 _next_marked_bytes = 0; 446 } 447 } 448 449 void HeapRegion::note_self_forwarding_removal_end(bool during_initial_mark, 450 bool during_conc_mark, 451 size_t marked_bytes) { 452 assert(0 <= marked_bytes && marked_bytes <= used(), 453 err_msg("marked: "SIZE_FORMAT" used: "SIZE_FORMAT, 454 marked_bytes, used())); 455 _prev_marked_bytes = marked_bytes; 456 } 457 458 HeapWord* 459 HeapRegion::object_iterate_mem_careful(MemRegion mr, 460 ObjectClosure* cl) { 461 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 462 // We used to use "block_start_careful" here. But we're actually happy 463 // to update the BOT while we do this... 464 HeapWord* cur = block_start(mr.start()); 465 mr = mr.intersection(used_region()); 466 if (mr.is_empty()) return NULL; 467 // Otherwise, find the obj that extends onto mr.start(). 468 469 assert(cur <= mr.start() 470 && (oop(cur)->klass_or_null() == NULL || 471 cur + oop(cur)->size() > mr.start()), 472 "postcondition of block_start"); 473 oop obj; 474 while (cur < mr.end()) { 475 obj = oop(cur); 476 if (obj->klass_or_null() == NULL) { 477 // Ran into an unparseable point. 478 return cur; 479 } else if (!g1h->is_obj_dead(obj)) { 480 cl->do_object(obj); 481 } 482 if (cl->abort()) return cur; 483 // The check above must occur before the operation below, since an 484 // abort might invalidate the "size" operation. 485 cur += obj->size(); 486 } 487 return NULL; 488 } 489 490 HeapWord* 491 HeapRegion:: 492 oops_on_card_seq_iterate_careful(MemRegion mr, 493 FilterOutOfRegionClosure* cl, 494 bool filter_young, 495 jbyte* card_ptr) { 496 // Currently, we should only have to clean the card if filter_young 497 // is true and vice versa. 498 if (filter_young) { 499 assert(card_ptr != NULL, "pre-condition"); 500 } else { 501 assert(card_ptr == NULL, "pre-condition"); 502 } 503 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 504 505 // If we're within a stop-world GC, then we might look at a card in a 506 // GC alloc region that extends onto a GC LAB, which may not be 507 // parseable. Stop such at the "saved_mark" of the region. 508 if (g1h->is_gc_active()) { 509 mr = mr.intersection(used_region_at_save_marks()); 510 } else { 511 mr = mr.intersection(used_region()); 512 } 513 if (mr.is_empty()) return NULL; 514 // Otherwise, find the obj that extends onto mr.start(). 515 516 // The intersection of the incoming mr (for the card) and the 517 // allocated part of the region is non-empty. This implies that 518 // we have actually allocated into this region. The code in 519 // G1CollectedHeap.cpp that allocates a new region sets the 520 // is_young tag on the region before allocating. Thus we 521 // safely know if this region is young. 522 if (is_young() && filter_young) { 523 return NULL; 524 } 525 526 assert(!is_young(), "check value of filter_young"); 527 528 // We can only clean the card here, after we make the decision that 529 // the card is not young. And we only clean the card if we have been 530 // asked to (i.e., card_ptr != NULL). 531 if (card_ptr != NULL) { 532 *card_ptr = CardTableModRefBS::clean_card_val(); 533 // We must complete this write before we do any of the reads below. 534 OrderAccess::storeload(); 535 } 536 537 // Cache the boundaries of the memory region in some const locals 538 HeapWord* const start = mr.start(); 539 HeapWord* const end = mr.end(); 540 541 // We used to use "block_start_careful" here. But we're actually happy 542 // to update the BOT while we do this... 543 HeapWord* cur = block_start(start); 544 assert(cur <= start, "Postcondition"); 545 546 oop obj; 547 548 HeapWord* next = cur; 549 while (next <= start) { 550 cur = next; 551 obj = oop(cur); 552 if (obj->klass_or_null() == NULL) { 553 // Ran into an unparseable point. 554 return cur; 555 } 556 // Otherwise... 557 next = (cur + obj->size()); 558 } 559 560 // If we finish the above loop...We have a parseable object that 561 // begins on or before the start of the memory region, and ends 562 // inside or spans the entire region. 563 564 assert(obj == oop(cur), "sanity"); 565 assert(cur <= start && 566 obj->klass_or_null() != NULL && 567 (cur + obj->size()) > start, 568 "Loop postcondition"); 569 570 if (!g1h->is_obj_dead(obj)) { 571 obj->oop_iterate(cl, mr); 572 } 573 574 while (cur < end) { 575 obj = oop(cur); 576 if (obj->klass_or_null() == NULL) { 577 // Ran into an unparseable point. 578 return cur; 579 }; 580 581 // Otherwise: 582 next = (cur + obj->size()); 583 584 if (!g1h->is_obj_dead(obj)) { 585 if (next < end || !obj->is_objArray()) { 586 // This object either does not span the MemRegion 587 // boundary, or if it does it's not an array. 588 // Apply closure to whole object. 589 obj->oop_iterate(cl); 590 } else { 591 // This obj is an array that spans the boundary. 592 // Stop at the boundary. 593 obj->oop_iterate(cl, mr); 594 } 595 } 596 cur = next; 597 } 598 return NULL; 599 } 600 601 // Code roots support 602 603 void HeapRegion::add_strong_code_root(nmethod* nm) { 604 assert(nm != NULL, "sanity"); 605 // Search for the code blob from the RHS to avoid 606 // duplicate entries as much as possible 607 if (_strong_code_root_list->find_from_end(nm) < 0) { 608 // Code blob isn't already in the list 609 _strong_code_root_list->push(nm); 610 } 611 } 612 613 void HeapRegion::remove_strong_code_root(nmethod* nm) { 614 assert(nm != NULL, "sanity"); 615 int idx = _strong_code_root_list->find(nm); 616 while (idx >= 0) { 617 _strong_code_root_list->remove_at(idx); 618 idx = _strong_code_root_list->find(nm); 619 } 620 } 621 622 class NMethodMigrationOopClosure : public OopClosure { 623 G1CollectedHeap* _g1h; 624 HeapRegion* _from; 625 nmethod* _nm; 626 627 uint _num_self_forwarded; 628 629 template <class T> void do_oop_work(T* p) { 630 T heap_oop = oopDesc::load_heap_oop(p); 631 if (!oopDesc::is_null(heap_oop)) { 632 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop); 633 if (_from->is_in(obj)) { 634 // Reference still points into the source region. 635 // Since roots are immediately evacuated this means that 636 // we must have self forwarded the object 637 assert(obj->is_forwarded(), 638 err_msg("code roots should be immediately evacuated. " 639 "Ref: "PTR_FORMAT", " 640 "Obj: "PTR_FORMAT", " 641 "Region: "HR_FORMAT, 642 p, (void*) obj, HR_FORMAT_PARAMS(_from))); 643 assert(obj->forwardee() == obj, 644 err_msg("not self forwarded? obj = "PTR_FORMAT, (void*)obj)); 645 646 // The object has been self forwarded. 647 // Note, if we're during an initial mark pause, there is 648 // no need to explicitly mark object. It will be marked 649 // during the regular evacuation failure handling code. 650 _num_self_forwarded++; 651 } else { 652 // The reference points into a promotion or to-space region 653 HeapRegion* to = _g1h->heap_region_containing(obj); 654 to->add_strong_code_root(_nm); 655 } 656 } 657 } 658 659 public: 660 NMethodMigrationOopClosure(G1CollectedHeap* g1h, HeapRegion* from, nmethod* nm): 661 _g1h(g1h), _from(from), _nm(nm), _num_self_forwarded(0) {} 662 663 void do_oop(narrowOop* p) { do_oop_work(p); } 664 void do_oop(oop* p) { do_oop_work(p); } 665 666 uint retain() { return _num_self_forwarded > 0; } 667 }; 668 669 void HeapRegion::migrate_strong_code_roots() { 670 assert(in_collection_set(), "only collection set regions"); 671 assert(!isHumongous(), "not humongous regions"); 672 673 ResourceMark rm; 674 675 // List of code blobs to retain for this region 676 GrowableArray<nmethod*> to_be_retained(10); 677 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 678 679 while (strong_code_root_list()->is_nonempty()) { 680 nmethod *nm = strong_code_root_list()->pop(); 681 if (nm != NULL) { 682 NMethodMigrationOopClosure oop_cl(g1h, this, nm); 683 nm->oops_do(&oop_cl); 684 if (oop_cl.retain()) { 685 to_be_retained.push(nm); 686 } 687 } 688 } 689 690 // Now push any code roots we need to retain 691 // FIXME: assert that region got an evacuation failure if non-empty 692 while (to_be_retained.is_nonempty()) { 693 nmethod* nm = to_be_retained.pop(); 694 assert(nm != NULL, "sanity"); 695 add_strong_code_root(nm); 696 } 697 } 698 699 void HeapRegion::strong_code_roots_do(CodeBlobClosure* blk) const { 700 for (int i = 0; i < _strong_code_root_list->length(); i += 1) { 701 nmethod* nm = _strong_code_root_list->at(i); 702 blk->do_code_blob(nm); 703 } 704 } 705 706 size_t HeapRegion::strong_code_root_mem_size() { 707 return sizeof(GrowableArray<nmethod*>) + 708 _strong_code_root_list->max_length() * sizeof(nmethod*); 709 } 710 711 class VerifyStrongCodeRootOopClosure: public OopClosure { 712 const HeapRegion* _hr; 713 nmethod* _nm; 714 bool _failures; 715 bool _has_oops_in_region; 716 717 template <class T> void do_oop_work(T* p) { 718 T heap_oop = oopDesc::load_heap_oop(p); 719 if (!oopDesc::is_null(heap_oop)) { 720 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop); 721 722 // Note: not all the oops embedded in the nmethod are in the 723 // current region. We only look at those which are. 724 if (_hr->is_in(obj)) { 725 // Object is in the region. Check that its less than top 726 if (_hr->top() <= (HeapWord*)obj) { 727 // Object is above top 728 gclog_or_tty->print_cr("Object "PTR_FORMAT" in region " 729 "["PTR_FORMAT", "PTR_FORMAT") is above " 730 "top "PTR_FORMAT, 731 obj, _hr->bottom(), _hr->end(), _hr->top()); 732 _failures = true; 733 return; 734 } 735 // Nmethod has at least one oop in the current region 736 _has_oops_in_region = true; 737 } 738 } 739 } 740 741 public: 742 VerifyStrongCodeRootOopClosure(const HeapRegion* hr, nmethod* nm): 743 _hr(hr), _failures(false), _has_oops_in_region(false) {} 744 745 void do_oop(narrowOop* p) { do_oop_work(p); } 746 void do_oop(oop* p) { do_oop_work(p); } 747 748 bool failures() { return _failures; } 749 bool has_oops_in_region() { return _has_oops_in_region; } 750 }; 751 752 class VerifyStrongCodeRootCodeBlobClosure: public CodeBlobClosure { 753 const HeapRegion* _hr; 754 bool _failures; 755 public: 756 VerifyStrongCodeRootCodeBlobClosure(const HeapRegion* hr) : 757 _hr(hr), _failures(false) {} 758 759 void do_code_blob(CodeBlob* cb) { 760 nmethod* nm = (cb == NULL) ? NULL : cb->as_nmethod_or_null(); 761 if (nm != NULL) { 762 // Verify that the nemthod is live 763 if (!nm->is_alive()) { 764 gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] has dead nmethod " 765 PTR_FORMAT" in its strong code roots", 766 _hr->bottom(), _hr->end(), nm); 767 _failures = true; 768 } else { 769 VerifyStrongCodeRootOopClosure oop_cl(_hr, nm); 770 nm->oops_do(&oop_cl); 771 if (!oop_cl.has_oops_in_region()) { 772 gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] has nmethod " 773 PTR_FORMAT" in its strong code roots " 774 "with no pointers into region", 775 _hr->bottom(), _hr->end(), nm); 776 _failures = true; 777 } else if (oop_cl.failures()) { 778 gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] has other " 779 "failures for nmethod "PTR_FORMAT, 780 _hr->bottom(), _hr->end(), nm); 781 _failures = true; 782 } 783 } 784 } 785 } 786 787 bool failures() { return _failures; } 788 }; 789 790 void HeapRegion::verify_strong_code_roots(VerifyOption vo, bool* failures) const { 791 if (!G1VerifyHeapRegionCodeRoots) { 792 // We're not verifying code roots. 793 return; 794 } 795 if (vo == VerifyOption_G1UseMarkWord) { 796 // Marking verification during a full GC is performed after class 797 // unloading, code cache unloading, etc so the strong code roots 798 // attached to each heap region are in an inconsistent state. They won't 799 // be consistent until the strong code roots are rebuilt after the 800 // actual GC. Skip verifying the strong code roots in this particular 801 // time. 802 assert(VerifyDuringGC, "only way to get here"); 803 return; 804 } 805 806 // if this region is empty then there should be no entries 807 // on its strong code root list 808 if (is_empty()) { 809 if (!_strong_code_root_list->is_empty()) { 810 gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] is empty " 811 "but has "INT32_FORMAT" code root entries", 812 bottom(), end(), _strong_code_root_list->length()); 813 *failures = true; 814 } 815 return; 816 } 817 818 // An H-region should have an empty strong code root list 819 if (isHumongous()) { 820 if (!_strong_code_root_list->is_empty()) { 821 gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] is humongous " 822 "but has "INT32_FORMAT" code root entries", 823 bottom(), end(), _strong_code_root_list->length()); 824 *failures = true; 825 } 826 return; 827 } 828 829 VerifyStrongCodeRootCodeBlobClosure cb_cl(this); 830 strong_code_roots_do(&cb_cl); 831 832 if (cb_cl.failures()) { 833 *failures = true; 834 } 835 } 836 837 void HeapRegion::print() const { print_on(gclog_or_tty); } 838 void HeapRegion::print_on(outputStream* st) const { 839 if (isHumongous()) { 840 if (startsHumongous()) 841 st->print(" HS"); 842 else 843 st->print(" HC"); 844 } else { 845 st->print(" "); 846 } 847 if (in_collection_set()) 848 st->print(" CS"); 849 else 850 st->print(" "); 851 if (is_young()) 852 st->print(is_survivor() ? " SU" : " Y "); 853 else 854 st->print(" "); 855 if (is_empty()) 856 st->print(" F"); 857 else 858 st->print(" "); 859 st->print(" TS %5d", _gc_time_stamp); 860 st->print(" PTAMS "PTR_FORMAT" NTAMS "PTR_FORMAT, 861 prev_top_at_mark_start(), next_top_at_mark_start()); 862 G1OffsetTableContigSpace::print_on(st); 863 } 864 865 class VerifyLiveClosure: public OopClosure { 866 private: 867 G1CollectedHeap* _g1h; 868 CardTableModRefBS* _bs; 869 oop _containing_obj; 870 bool _failures; 871 int _n_failures; 872 VerifyOption _vo; 873 public: 874 // _vo == UsePrevMarking -> use "prev" marking information, 875 // _vo == UseNextMarking -> use "next" marking information, 876 // _vo == UseMarkWord -> use mark word from object header. 877 VerifyLiveClosure(G1CollectedHeap* g1h, VerifyOption vo) : 878 _g1h(g1h), _bs(NULL), _containing_obj(NULL), 879 _failures(false), _n_failures(0), _vo(vo) 880 { 881 BarrierSet* bs = _g1h->barrier_set(); 882 if (bs->is_a(BarrierSet::CardTableModRef)) 883 _bs = (CardTableModRefBS*)bs; 884 } 885 886 void set_containing_obj(oop obj) { 887 _containing_obj = obj; 888 } 889 890 bool failures() { return _failures; } 891 int n_failures() { return _n_failures; } 892 893 virtual void do_oop(narrowOop* p) { do_oop_work(p); } 894 virtual void do_oop( oop* p) { do_oop_work(p); } 895 896 void print_object(outputStream* out, oop obj) { 897 #ifdef PRODUCT 898 Klass* k = obj->klass(); 899 const char* class_name = InstanceKlass::cast(k)->external_name(); 900 out->print_cr("class name %s", class_name); 901 #else // PRODUCT 902 obj->print_on(out); 903 #endif // PRODUCT 904 } 905 906 template <class T> 907 void do_oop_work(T* p) { 908 assert(_containing_obj != NULL, "Precondition"); 909 assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo), 910 "Precondition"); 911 T heap_oop = oopDesc::load_heap_oop(p); 912 if (!oopDesc::is_null(heap_oop)) { 913 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop); 914 bool failed = false; 915 if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead_cond(obj, _vo)) { 916 MutexLockerEx x(ParGCRareEvent_lock, 917 Mutex::_no_safepoint_check_flag); 918 919 if (!_failures) { 920 gclog_or_tty->print_cr(""); 921 gclog_or_tty->print_cr("----------"); 922 } 923 if (!_g1h->is_in_closed_subset(obj)) { 924 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p); 925 gclog_or_tty->print_cr("Field "PTR_FORMAT 926 " of live obj "PTR_FORMAT" in region " 927 "["PTR_FORMAT", "PTR_FORMAT")", 928 p, (void*) _containing_obj, 929 from->bottom(), from->end()); 930 print_object(gclog_or_tty, _containing_obj); 931 gclog_or_tty->print_cr("points to obj "PTR_FORMAT" not in the heap", 932 (void*) obj); 933 } else { 934 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p); 935 HeapRegion* to = _g1h->heap_region_containing((HeapWord*)obj); 936 gclog_or_tty->print_cr("Field "PTR_FORMAT 937 " of live obj "PTR_FORMAT" in region " 938 "["PTR_FORMAT", "PTR_FORMAT")", 939 p, (void*) _containing_obj, 940 from->bottom(), from->end()); 941 print_object(gclog_or_tty, _containing_obj); 942 gclog_or_tty->print_cr("points to dead obj "PTR_FORMAT" in region " 943 "["PTR_FORMAT", "PTR_FORMAT")", 944 (void*) obj, to->bottom(), to->end()); 945 print_object(gclog_or_tty, obj); 946 } 947 gclog_or_tty->print_cr("----------"); 948 gclog_or_tty->flush(); 949 _failures = true; 950 failed = true; 951 _n_failures++; 952 } 953 954 if (!_g1h->full_collection() || G1VerifyRSetsDuringFullGC) { 955 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p); 956 HeapRegion* to = _g1h->heap_region_containing(obj); 957 if (from != NULL && to != NULL && 958 from != to && 959 !to->isHumongous()) { 960 jbyte cv_obj = *_bs->byte_for_const(_containing_obj); 961 jbyte cv_field = *_bs->byte_for_const(p); 962 const jbyte dirty = CardTableModRefBS::dirty_card_val(); 963 964 bool is_bad = !(from->is_young() 965 || to->rem_set()->contains_reference(p) 966 || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed 967 (_containing_obj->is_objArray() ? 968 cv_field == dirty 969 : cv_obj == dirty || cv_field == dirty)); 970 if (is_bad) { 971 MutexLockerEx x(ParGCRareEvent_lock, 972 Mutex::_no_safepoint_check_flag); 973 974 if (!_failures) { 975 gclog_or_tty->print_cr(""); 976 gclog_or_tty->print_cr("----------"); 977 } 978 gclog_or_tty->print_cr("Missing rem set entry:"); 979 gclog_or_tty->print_cr("Field "PTR_FORMAT" " 980 "of obj "PTR_FORMAT", " 981 "in region "HR_FORMAT, 982 p, (void*) _containing_obj, 983 HR_FORMAT_PARAMS(from)); 984 _containing_obj->print_on(gclog_or_tty); 985 gclog_or_tty->print_cr("points to obj "PTR_FORMAT" " 986 "in region "HR_FORMAT, 987 (void*) obj, 988 HR_FORMAT_PARAMS(to)); 989 obj->print_on(gclog_or_tty); 990 gclog_or_tty->print_cr("Obj head CTE = %d, field CTE = %d.", 991 cv_obj, cv_field); 992 gclog_or_tty->print_cr("----------"); 993 gclog_or_tty->flush(); 994 _failures = true; 995 if (!failed) _n_failures++; 996 } 997 } 998 } 999 } 1000 } 1001 }; 1002 1003 // This really ought to be commoned up into OffsetTableContigSpace somehow. 1004 // We would need a mechanism to make that code skip dead objects. 1005 1006 void HeapRegion::verify(VerifyOption vo, 1007 bool* failures) const { 1008 G1CollectedHeap* g1 = G1CollectedHeap::heap(); 1009 *failures = false; 1010 HeapWord* p = bottom(); 1011 HeapWord* prev_p = NULL; 1012 VerifyLiveClosure vl_cl(g1, vo); 1013 bool is_humongous = isHumongous(); 1014 bool do_bot_verify = !is_young(); 1015 size_t object_num = 0; 1016 while (p < top()) { 1017 oop obj = oop(p); 1018 size_t obj_size = obj->size(); 1019 object_num += 1; 1020 1021 if (is_humongous != g1->isHumongous(obj_size)) { 1022 gclog_or_tty->print_cr("obj "PTR_FORMAT" is of %shumongous size (" 1023 SIZE_FORMAT" words) in a %shumongous region", 1024 p, g1->isHumongous(obj_size) ? "" : "non-", 1025 obj_size, is_humongous ? "" : "non-"); 1026 *failures = true; 1027 return; 1028 } 1029 1030 // If it returns false, verify_for_object() will output the 1031 // appropriate messasge. 1032 if (do_bot_verify && !_offsets.verify_for_object(p, obj_size)) { 1033 *failures = true; 1034 return; 1035 } 1036 1037 if (!g1->is_obj_dead_cond(obj, this, vo)) { 1038 if (obj->is_oop()) { 1039 Klass* klass = obj->klass(); 1040 if (!klass->is_metadata()) { 1041 gclog_or_tty->print_cr("klass "PTR_FORMAT" of object "PTR_FORMAT" " 1042 "not metadata", klass, obj); 1043 *failures = true; 1044 return; 1045 } else if (!klass->is_klass()) { 1046 gclog_or_tty->print_cr("klass "PTR_FORMAT" of object "PTR_FORMAT" " 1047 "not a klass", klass, obj); 1048 *failures = true; 1049 return; 1050 } else { 1051 vl_cl.set_containing_obj(obj); 1052 obj->oop_iterate_no_header(&vl_cl); 1053 if (vl_cl.failures()) { 1054 *failures = true; 1055 } 1056 if (G1MaxVerifyFailures >= 0 && 1057 vl_cl.n_failures() >= G1MaxVerifyFailures) { 1058 return; 1059 } 1060 } 1061 } else { 1062 gclog_or_tty->print_cr(PTR_FORMAT" no an oop", obj); 1063 *failures = true; 1064 return; 1065 } 1066 } 1067 prev_p = p; 1068 p += obj_size; 1069 } 1070 1071 if (p != top()) { 1072 gclog_or_tty->print_cr("end of last object "PTR_FORMAT" " 1073 "does not match top "PTR_FORMAT, p, top()); 1074 *failures = true; 1075 return; 1076 } 1077 1078 HeapWord* the_end = end(); 1079 assert(p == top(), "it should still hold"); 1080 // Do some extra BOT consistency checking for addresses in the 1081 // range [top, end). BOT look-ups in this range should yield 1082 // top. No point in doing that if top == end (there's nothing there). 1083 if (p < the_end) { 1084 // Look up top 1085 HeapWord* addr_1 = p; 1086 HeapWord* b_start_1 = _offsets.block_start_const(addr_1); 1087 if (b_start_1 != p) { 1088 gclog_or_tty->print_cr("BOT look up for top: "PTR_FORMAT" " 1089 " yielded "PTR_FORMAT", expecting "PTR_FORMAT, 1090 addr_1, b_start_1, p); 1091 *failures = true; 1092 return; 1093 } 1094 1095 // Look up top + 1 1096 HeapWord* addr_2 = p + 1; 1097 if (addr_2 < the_end) { 1098 HeapWord* b_start_2 = _offsets.block_start_const(addr_2); 1099 if (b_start_2 != p) { 1100 gclog_or_tty->print_cr("BOT look up for top + 1: "PTR_FORMAT" " 1101 " yielded "PTR_FORMAT", expecting "PTR_FORMAT, 1102 addr_2, b_start_2, p); 1103 *failures = true; 1104 return; 1105 } 1106 } 1107 1108 // Look up an address between top and end 1109 size_t diff = pointer_delta(the_end, p) / 2; 1110 HeapWord* addr_3 = p + diff; 1111 if (addr_3 < the_end) { 1112 HeapWord* b_start_3 = _offsets.block_start_const(addr_3); 1113 if (b_start_3 != p) { 1114 gclog_or_tty->print_cr("BOT look up for top + diff: "PTR_FORMAT" " 1115 " yielded "PTR_FORMAT", expecting "PTR_FORMAT, 1116 addr_3, b_start_3, p); 1117 *failures = true; 1118 return; 1119 } 1120 } 1121 1122 // Loook up end - 1 1123 HeapWord* addr_4 = the_end - 1; 1124 HeapWord* b_start_4 = _offsets.block_start_const(addr_4); 1125 if (b_start_4 != p) { 1126 gclog_or_tty->print_cr("BOT look up for end - 1: "PTR_FORMAT" " 1127 " yielded "PTR_FORMAT", expecting "PTR_FORMAT, 1128 addr_4, b_start_4, p); 1129 *failures = true; 1130 return; 1131 } 1132 } 1133 1134 if (is_humongous && object_num > 1) { 1135 gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] is humongous " 1136 "but has "SIZE_FORMAT", objects", 1137 bottom(), end(), object_num); 1138 *failures = true; 1139 return; 1140 } 1141 1142 verify_strong_code_roots(vo, failures); 1143 } 1144 1145 void HeapRegion::verify() const { 1146 bool dummy = false; 1147 verify(VerifyOption_G1UsePrevMarking, /* failures */ &dummy); 1148 } 1149 1150 // G1OffsetTableContigSpace code; copied from space.cpp. Hope this can go 1151 // away eventually. 1152 1153 void G1OffsetTableContigSpace::clear(bool mangle_space) { 1154 ContiguousSpace::clear(mangle_space); 1155 _offsets.zero_bottom_entry(); 1156 _offsets.initialize_threshold(); 1157 } 1158 1159 void G1OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) { 1160 Space::set_bottom(new_bottom); 1161 _offsets.set_bottom(new_bottom); 1162 } 1163 1164 void G1OffsetTableContigSpace::set_end(HeapWord* new_end) { 1165 Space::set_end(new_end); 1166 _offsets.resize(new_end - bottom()); 1167 } 1168 1169 void G1OffsetTableContigSpace::print() const { 1170 print_short(); 1171 gclog_or_tty->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " 1172 INTPTR_FORMAT ", " INTPTR_FORMAT ")", 1173 bottom(), top(), _offsets.threshold(), end()); 1174 } 1175 1176 HeapWord* G1OffsetTableContigSpace::initialize_threshold() { 1177 return _offsets.initialize_threshold(); 1178 } 1179 1180 HeapWord* G1OffsetTableContigSpace::cross_threshold(HeapWord* start, 1181 HeapWord* end) { 1182 _offsets.alloc_block(start, end); 1183 return _offsets.threshold(); 1184 } 1185 1186 HeapWord* G1OffsetTableContigSpace::saved_mark_word() const { 1187 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 1188 assert( _gc_time_stamp <= g1h->get_gc_time_stamp(), "invariant" ); 1189 if (_gc_time_stamp < g1h->get_gc_time_stamp()) 1190 return top(); 1191 else 1192 return ContiguousSpace::saved_mark_word(); 1193 } 1194 1195 void G1OffsetTableContigSpace::set_saved_mark() { 1196 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 1197 unsigned curr_gc_time_stamp = g1h->get_gc_time_stamp(); 1198 1199 if (_gc_time_stamp < curr_gc_time_stamp) { 1200 // The order of these is important, as another thread might be 1201 // about to start scanning this region. If it does so after 1202 // set_saved_mark and before _gc_time_stamp = ..., then the latter 1203 // will be false, and it will pick up top() as the high water mark 1204 // of region. If it does so after _gc_time_stamp = ..., then it 1205 // will pick up the right saved_mark_word() as the high water mark 1206 // of the region. Either way, the behaviour will be correct. 1207 ContiguousSpace::set_saved_mark(); 1208 OrderAccess::storestore(); 1209 _gc_time_stamp = curr_gc_time_stamp; 1210 // No need to do another barrier to flush the writes above. If 1211 // this is called in parallel with other threads trying to 1212 // allocate into the region, the caller should call this while 1213 // holding a lock and when the lock is released the writes will be 1214 // flushed. 1215 } 1216 } 1217 1218 G1OffsetTableContigSpace:: 1219 G1OffsetTableContigSpace(G1BlockOffsetSharedArray* sharedOffsetArray, 1220 MemRegion mr) : 1221 _offsets(sharedOffsetArray, mr), 1222 _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true), 1223 _gc_time_stamp(0) 1224 { 1225 _offsets.set_space(this); 1226 // false ==> we'll do the clearing if there's clearing to be done. 1227 ContiguousSpace::initialize(mr, false, SpaceDecorator::Mangle); 1228 _offsets.zero_bottom_entry(); 1229 _offsets.initialize_threshold(); 1230 }