1 /* 2 * Copyright (c) 2001, 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 "code/nmethod.hpp" 27 #include "gc/g1/g1BlockOffsetTable.inline.hpp" 28 #include "gc/g1/g1CollectedHeap.inline.hpp" 29 #include "gc/g1/g1OopClosures.inline.hpp" 30 #include "gc/g1/heapRegion.inline.hpp" 31 #include "gc/g1/heapRegionBounds.inline.hpp" 32 #include "gc/g1/heapRegionManager.inline.hpp" 33 #include "gc/g1/heapRegionRemSet.hpp" 34 #include "gc/shared/genOopClosures.inline.hpp" 35 #include "gc/shared/liveRange.hpp" 36 #include "gc/shared/space.inline.hpp" 37 #include "memory/iterator.hpp" 38 #include "oops/oop.inline.hpp" 39 #include "runtime/atomic.inline.hpp" 40 #include "runtime/orderAccess.inline.hpp" 41 42 int HeapRegion::LogOfHRGrainBytes = 0; 43 int HeapRegion::LogOfHRGrainWords = 0; 44 size_t HeapRegion::GrainBytes = 0; 45 size_t HeapRegion::GrainWords = 0; 46 size_t HeapRegion::CardsPerRegion = 0; 47 48 HeapRegionDCTOC::HeapRegionDCTOC(G1CollectedHeap* g1, 49 HeapRegion* hr, 50 G1ParPushHeapRSClosure* cl, 51 CardTableModRefBS::PrecisionStyle precision) : 52 DirtyCardToOopClosure(hr, cl, precision, NULL), 53 _hr(hr), _rs_scan(cl), _g1(g1) { } 54 55 FilterOutOfRegionClosure::FilterOutOfRegionClosure(HeapRegion* r, 56 OopClosure* oc) : 57 _r_bottom(r->bottom()), _r_end(r->end()), _oc(oc) { } 58 59 void HeapRegionDCTOC::walk_mem_region(MemRegion mr, 60 HeapWord* bottom, 61 HeapWord* top) { 62 G1CollectedHeap* g1h = _g1; 63 size_t oop_size; 64 HeapWord* cur = bottom; 65 66 // Start filtering what we add to the remembered set. If the object is 67 // not considered dead, either because it is marked (in the mark bitmap) 68 // or it was allocated after marking finished, then we add it. Otherwise 69 // we can safely ignore the object. 70 if (!g1h->is_obj_dead(oop(cur), _hr)) { 71 oop_size = oop(cur)->oop_iterate(_rs_scan, mr); 72 } else { 73 oop_size = _hr->block_size(cur); 74 } 75 76 cur += oop_size; 77 78 if (cur < top) { 79 oop cur_oop = oop(cur); 80 oop_size = _hr->block_size(cur); 81 HeapWord* next_obj = cur + oop_size; 82 while (next_obj < top) { 83 // Keep filtering the remembered set. 84 if (!g1h->is_obj_dead(cur_oop, _hr)) { 85 // Bottom lies entirely below top, so we can call the 86 // non-memRegion version of oop_iterate below. 87 cur_oop->oop_iterate(_rs_scan); 88 } 89 cur = next_obj; 90 cur_oop = oop(cur); 91 oop_size = _hr->block_size(cur); 92 next_obj = cur + oop_size; 93 } 94 95 // Last object. Need to do dead-obj filtering here too. 96 if (!g1h->is_obj_dead(oop(cur), _hr)) { 97 oop(cur)->oop_iterate(_rs_scan, mr); 98 } 99 } 100 } 101 102 size_t HeapRegion::max_region_size() { 103 return HeapRegionBounds::max_size(); 104 } 105 106 size_t HeapRegion::min_region_size_in_words() { 107 return HeapRegionBounds::min_size() >> LogHeapWordSize; 108 } 109 110 void HeapRegion::setup_heap_region_size(size_t initial_heap_size, size_t max_heap_size) { 111 size_t region_size = G1HeapRegionSize; 112 if (FLAG_IS_DEFAULT(G1HeapRegionSize)) { 113 size_t average_heap_size = (initial_heap_size + max_heap_size) / 2; 114 region_size = MAX2(average_heap_size / HeapRegionBounds::target_number(), 115 HeapRegionBounds::min_size()); 116 } 117 118 int region_size_log = log2_long((jlong) region_size); 119 // Recalculate the region size to make sure it's a power of 120 // 2. This means that region_size is the largest power of 2 that's 121 // <= what we've calculated so far. 122 region_size = ((size_t)1 << region_size_log); 123 124 // Now make sure that we don't go over or under our limits. 125 if (region_size < HeapRegionBounds::min_size()) { 126 region_size = HeapRegionBounds::min_size(); 127 } else if (region_size > HeapRegionBounds::max_size()) { 128 region_size = HeapRegionBounds::max_size(); 129 } 130 131 // And recalculate the log. 132 region_size_log = log2_long((jlong) region_size); 133 134 // Now, set up the globals. 135 guarantee(LogOfHRGrainBytes == 0, "we should only set it once"); 136 LogOfHRGrainBytes = region_size_log; 137 138 guarantee(LogOfHRGrainWords == 0, "we should only set it once"); 139 LogOfHRGrainWords = LogOfHRGrainBytes - LogHeapWordSize; 140 141 guarantee(GrainBytes == 0, "we should only set it once"); 142 // The cast to int is safe, given that we've bounded region_size by 143 // MIN_REGION_SIZE and MAX_REGION_SIZE. 144 GrainBytes = region_size; 145 146 guarantee(GrainWords == 0, "we should only set it once"); 147 GrainWords = GrainBytes >> LogHeapWordSize; 148 guarantee((size_t) 1 << LogOfHRGrainWords == GrainWords, "sanity"); 149 150 guarantee(CardsPerRegion == 0, "we should only set it once"); 151 CardsPerRegion = GrainBytes >> CardTableModRefBS::card_shift; 152 } 153 154 void HeapRegion::reset_after_compaction() { 155 G1OffsetTableContigSpace::reset_after_compaction(); 156 // After a compaction the mark bitmap is invalid, so we must 157 // treat all objects as being inside the unmarked area. 158 zero_marked_bytes(); 159 init_top_at_mark_start(); 160 } 161 162 void HeapRegion::hr_clear(bool par, bool clear_space, bool locked) { 163 assert(_humongous_start_region == NULL, 164 "we should have already filtered out humongous regions"); 165 assert(_end == orig_end(), 166 "we should have already filtered out humongous regions"); 167 assert(!in_collection_set(), 168 err_msg("Should not clear heap region %u in the collection set", hrm_index())); 169 170 set_allocation_context(AllocationContext::system()); 171 set_young_index_in_cset(-1); 172 uninstall_surv_rate_group(); 173 set_free(); 174 reset_pre_dummy_top(); 175 176 if (!par) { 177 // If this is parallel, this will be done later. 178 HeapRegionRemSet* hrrs = rem_set(); 179 if (locked) { 180 hrrs->clear_locked(); 181 } else { 182 hrrs->clear(); 183 } 184 } 185 zero_marked_bytes(); 186 187 _offsets.resize(HeapRegion::GrainWords); 188 init_top_at_mark_start(); 189 if (clear_space) clear(SpaceDecorator::Mangle); 190 } 191 192 void HeapRegion::par_clear() { 193 assert(used() == 0, "the region should have been already cleared"); 194 assert(capacity() == HeapRegion::GrainBytes, "should be back to normal"); 195 HeapRegionRemSet* hrrs = rem_set(); 196 hrrs->clear(); 197 CardTableModRefBS* ct_bs = 198 barrier_set_cast<CardTableModRefBS>(G1CollectedHeap::heap()->barrier_set()); 199 ct_bs->clear(MemRegion(bottom(), end())); 200 } 201 202 void HeapRegion::calc_gc_efficiency() { 203 // GC efficiency is the ratio of how much space would be 204 // reclaimed over how long we predict it would take to reclaim it. 205 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 206 G1CollectorPolicy* g1p = g1h->g1_policy(); 207 208 // Retrieve a prediction of the elapsed time for this region for 209 // a mixed gc because the region will only be evacuated during a 210 // mixed gc. 211 double region_elapsed_time_ms = 212 g1p->predict_region_elapsed_time_ms(this, false /* for_young_gc */); 213 _gc_efficiency = (double) reclaimable_bytes() / region_elapsed_time_ms; 214 } 215 216 void HeapRegion::set_starts_humongous(HeapWord* new_top, HeapWord* new_end) { 217 assert(!is_humongous(), "sanity / pre-condition"); 218 assert(end() == orig_end(), 219 "Should be normal before the humongous object allocation"); 220 assert(top() == bottom(), "should be empty"); 221 assert(bottom() <= new_top && new_top <= new_end, "pre-condition"); 222 223 _type.set_starts_humongous(); 224 _humongous_start_region = this; 225 226 set_end(new_end); 227 _offsets.set_for_starts_humongous(new_top); 228 } 229 230 void HeapRegion::set_continues_humongous(HeapRegion* first_hr) { 231 assert(!is_humongous(), "sanity / pre-condition"); 232 assert(end() == orig_end(), 233 "Should be normal before the humongous object allocation"); 234 assert(top() == bottom(), "should be empty"); 235 assert(first_hr->is_starts_humongous(), "pre-condition"); 236 237 _type.set_continues_humongous(); 238 _humongous_start_region = first_hr; 239 } 240 241 void HeapRegion::clear_humongous() { 242 assert(is_humongous(), "pre-condition"); 243 244 if (is_starts_humongous()) { 245 assert(top() <= end(), "pre-condition"); 246 set_end(orig_end()); 247 if (top() > end()) { 248 // at least one "continues humongous" region after it 249 set_top(end()); 250 } 251 } else { 252 // continues humongous 253 assert(end() == orig_end(), "sanity"); 254 } 255 256 assert(capacity() == HeapRegion::GrainBytes, "pre-condition"); 257 _humongous_start_region = NULL; 258 } 259 260 HeapRegion::HeapRegion(uint hrm_index, 261 G1BlockOffsetSharedArray* sharedOffsetArray, 262 MemRegion mr) : 263 G1OffsetTableContigSpace(sharedOffsetArray, mr), 264 _hrm_index(hrm_index), 265 _allocation_context(AllocationContext::system()), 266 _humongous_start_region(NULL), 267 _next_in_special_set(NULL), 268 _evacuation_failed(false), 269 _prev_marked_bytes(0), _next_marked_bytes(0), _gc_efficiency(0.0), 270 _next_young_region(NULL), 271 _next_dirty_cards_region(NULL), _next(NULL), _prev(NULL), 272 #ifdef ASSERT 273 _containing_set(NULL), 274 #endif // ASSERT 275 _young_index_in_cset(-1), _surv_rate_group(NULL), _age_index(-1), 276 _rem_set(NULL), _recorded_rs_length(0), _predicted_elapsed_time_ms(0), 277 _predicted_bytes_to_copy(0) 278 { 279 _rem_set = new HeapRegionRemSet(sharedOffsetArray, this); 280 assert(HeapRegionRemSet::num_par_rem_sets() > 0, "Invariant."); 281 282 initialize(mr); 283 } 284 285 void HeapRegion::initialize(MemRegion mr, bool clear_space, bool mangle_space) { 286 assert(_rem_set->is_empty(), "Remembered set must be empty"); 287 288 G1OffsetTableContigSpace::initialize(mr, clear_space, mangle_space); 289 290 hr_clear(false /*par*/, false /*clear_space*/); 291 set_top(bottom()); 292 record_timestamp(); 293 294 assert(mr.end() == orig_end(), 295 err_msg("Given region end address " PTR_FORMAT " should match exactly " 296 "bottom plus one region size, i.e. " PTR_FORMAT, 297 p2i(mr.end()), p2i(orig_end()))); 298 } 299 300 CompactibleSpace* HeapRegion::next_compaction_space() const { 301 return G1CollectedHeap::heap()->next_compaction_region(this); 302 } 303 304 void HeapRegion::note_self_forwarding_removal_start(bool during_initial_mark, 305 bool during_conc_mark) { 306 // We always recreate the prev marking info and we'll explicitly 307 // mark all objects we find to be self-forwarded on the prev 308 // bitmap. So all objects need to be below PTAMS. 309 _prev_marked_bytes = 0; 310 311 if (during_initial_mark) { 312 // During initial-mark, we'll also explicitly mark all objects 313 // we find to be self-forwarded on the next bitmap. So all 314 // objects need to be below NTAMS. 315 _next_top_at_mark_start = top(); 316 _next_marked_bytes = 0; 317 } else if (during_conc_mark) { 318 // During concurrent mark, all objects in the CSet (including 319 // the ones we find to be self-forwarded) are implicitly live. 320 // So all objects need to be above NTAMS. 321 _next_top_at_mark_start = bottom(); 322 _next_marked_bytes = 0; 323 } 324 } 325 326 void HeapRegion::note_self_forwarding_removal_end(bool during_initial_mark, 327 bool during_conc_mark, 328 size_t marked_bytes) { 329 assert(marked_bytes <= used(), 330 err_msg("marked: " SIZE_FORMAT " used: " SIZE_FORMAT, marked_bytes, used())); 331 _prev_top_at_mark_start = top(); 332 _prev_marked_bytes = marked_bytes; 333 } 334 335 HeapWord* 336 HeapRegion::object_iterate_mem_careful(MemRegion mr, 337 ObjectClosure* cl) { 338 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 339 // We used to use "block_start_careful" here. But we're actually happy 340 // to update the BOT while we do this... 341 HeapWord* cur = block_start(mr.start()); 342 mr = mr.intersection(used_region()); 343 if (mr.is_empty()) return NULL; 344 // Otherwise, find the obj that extends onto mr.start(). 345 346 assert(cur <= mr.start() 347 && (oop(cur)->klass_or_null() == NULL || 348 cur + oop(cur)->size() > mr.start()), 349 "postcondition of block_start"); 350 oop obj; 351 while (cur < mr.end()) { 352 obj = oop(cur); 353 if (obj->klass_or_null() == NULL) { 354 // Ran into an unparseable point. 355 return cur; 356 } else if (!g1h->is_obj_dead(obj)) { 357 cl->do_object(obj); 358 } 359 cur += block_size(cur); 360 } 361 return NULL; 362 } 363 364 365 bool HeapRegion::clean_card(MemRegion& mr, 366 bool filter_young, 367 jbyte* &card_ptr) { 368 // Currently, we should only have to clean the card if filter_young 369 // is true and vice versa. 370 if (filter_young) { 371 assert(card_ptr != NULL, "pre-condition"); 372 } else { 373 assert(card_ptr == NULL, "pre-condition"); 374 } 375 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 376 377 // If we're within a stop-world GC, then we might look at a card in a 378 // GC alloc region that extends onto a GC LAB, which may not be 379 // parseable. Stop such at the "scan_top" of the region. 380 if (g1h->is_gc_active()) { 381 mr = mr.intersection(MemRegion(bottom(), scan_top())); 382 } else { 383 mr = mr.intersection(used_region()); 384 } 385 if (mr.is_empty()) return false; 386 // Otherwise, find the obj that extends onto mr.start(). 387 388 // The intersection of the incoming mr (for the card) and the 389 // allocated part of the region is non-empty. This implies that 390 // we have actually allocated into this region. The code in 391 // G1CollectedHeap.cpp that allocates a new region sets the 392 // is_young tag on the region before allocating. Thus we 393 // safely know if this region is young. 394 if (is_young() && filter_young) { 395 return false; 396 } 397 398 assert(!is_young(), "check value of filter_young"); 399 400 // We can only clean the card here, after we make the decision that 401 // the card is not young. And we only clean the card if we have been 402 // asked to (i.e., card_ptr != NULL). 403 if (card_ptr != NULL) { 404 *card_ptr = CardTableModRefBS::clean_card_val(); 405 } 406 407 return true; 408 } 409 410 HeapWord* HeapRegion::process_oops_on_card(MemRegion mr, 411 FilterOutOfRegionClosure *cl, 412 jbyte *card_ptr) { 413 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 414 G1SATBCardTableLoggingModRefBS* bs = g1h->g1_barrier_set(); 415 // Cache the boundaries of the memory region in some const locals 416 HeapWord* const start = mr.start(); 417 HeapWord* const end = mr.end(); 418 419 HeapWord* cur; 420 421 // We used to use "block_start_careful" here. But we're actually happy 422 // to update the BOT while we do this... 423 cur = block_start(start); 424 assert(cur <= start, "Postcondition"); 425 426 oop obj; 427 428 HeapWord* next = cur; 429 do { 430 cur = next; 431 obj = oop(cur); 432 if (obj->klass_or_null() == NULL) { 433 // Ran into an unparseable point. 434 return cur; 435 } 436 // Otherwise... 437 next = cur + block_size(cur); 438 } while (next <= start); 439 440 // If we finish the above loop...We have a parseable object that 441 // begins on or before the start of the memory region, and ends 442 // inside or spans the entire region. 443 assert(cur <= start, "Loop postcondition"); 444 assert(obj->klass_or_null() != NULL, "Loop postcondition"); 445 446 do { 447 obj = oop(cur); 448 assert((cur + block_size(cur)) > (HeapWord*)obj, "Loop invariant"); 449 if (obj->klass_or_null() == NULL) { 450 // Ran into an unparseable point. 451 return cur; 452 } 453 454 // Advance the current pointer. "obj" still points to the object to iterate. 455 cur = cur + block_size(cur); 456 457 if (!g1h->is_obj_dead(obj)) { 458 // Non-objArrays are sometimes marked imprecise at the object start. We 459 // always need to iterate over them in full. 460 // We only iterate over object arrays in full if they are completely contained 461 // in the memory region. 462 if (!obj->is_objArray() || (((HeapWord*)obj) >= start && cur <= end)) { 463 obj->oop_iterate(cl); 464 } else { 465 obj->oop_iterate(cl, mr); 466 } 467 } 468 } while (cur < end); 469 470 return NULL; 471 } 472 473 HeapWord* 474 HeapRegion:: 475 oops_on_card_seq_iterate_careful(MemRegion mr, 476 FilterOutOfRegionClosure* cl, 477 bool filter_young, 478 jbyte* card_ptr) { 479 if (!clean_card(mr, filter_young, card_ptr)) return NULL; 480 if (card_ptr != NULL) OrderAccess::storeload(); // serialize card cleaning 481 return process_oops_on_card(mr, cl, card_ptr); 482 } 483 484 // Code roots support 485 486 void HeapRegion::add_strong_code_root(nmethod* nm) { 487 HeapRegionRemSet* hrrs = rem_set(); 488 hrrs->add_strong_code_root(nm); 489 } 490 491 void HeapRegion::add_strong_code_root_locked(nmethod* nm) { 492 assert_locked_or_safepoint(CodeCache_lock); 493 HeapRegionRemSet* hrrs = rem_set(); 494 hrrs->add_strong_code_root_locked(nm); 495 } 496 497 void HeapRegion::remove_strong_code_root(nmethod* nm) { 498 HeapRegionRemSet* hrrs = rem_set(); 499 hrrs->remove_strong_code_root(nm); 500 } 501 502 void HeapRegion::strong_code_roots_do(CodeBlobClosure* blk) const { 503 HeapRegionRemSet* hrrs = rem_set(); 504 hrrs->strong_code_roots_do(blk); 505 } 506 507 class VerifyStrongCodeRootOopClosure: public OopClosure { 508 const HeapRegion* _hr; 509 nmethod* _nm; 510 bool _failures; 511 bool _has_oops_in_region; 512 513 template <class T> void do_oop_work(T* p) { 514 T heap_oop = oopDesc::load_heap_oop(p); 515 if (!oopDesc::is_null(heap_oop)) { 516 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop); 517 518 // Note: not all the oops embedded in the nmethod are in the 519 // current region. We only look at those which are. 520 if (_hr->is_in(obj)) { 521 // Object is in the region. Check that its less than top 522 if (_hr->top() <= (HeapWord*)obj) { 523 // Object is above top 524 gclog_or_tty->print_cr("Object " PTR_FORMAT " in region " 525 "[" PTR_FORMAT ", " PTR_FORMAT ") is above " 526 "top " PTR_FORMAT, 527 p2i(obj), p2i(_hr->bottom()), p2i(_hr->end()), p2i(_hr->top())); 528 _failures = true; 529 return; 530 } 531 // Nmethod has at least one oop in the current region 532 _has_oops_in_region = true; 533 } 534 } 535 } 536 537 public: 538 VerifyStrongCodeRootOopClosure(const HeapRegion* hr, nmethod* nm): 539 _hr(hr), _failures(false), _has_oops_in_region(false) {} 540 541 void do_oop(narrowOop* p) { do_oop_work(p); } 542 void do_oop(oop* p) { do_oop_work(p); } 543 544 bool failures() { return _failures; } 545 bool has_oops_in_region() { return _has_oops_in_region; } 546 }; 547 548 class VerifyStrongCodeRootCodeBlobClosure: public CodeBlobClosure { 549 const HeapRegion* _hr; 550 bool _failures; 551 public: 552 VerifyStrongCodeRootCodeBlobClosure(const HeapRegion* hr) : 553 _hr(hr), _failures(false) {} 554 555 void do_code_blob(CodeBlob* cb) { 556 nmethod* nm = (cb == NULL) ? NULL : cb->as_nmethod_or_null(); 557 if (nm != NULL) { 558 // Verify that the nemthod is live 559 if (!nm->is_alive()) { 560 gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] has dead nmethod " 561 PTR_FORMAT " in its strong code roots", 562 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm)); 563 _failures = true; 564 } else { 565 VerifyStrongCodeRootOopClosure oop_cl(_hr, nm); 566 nm->oops_do(&oop_cl); 567 if (!oop_cl.has_oops_in_region()) { 568 gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] has nmethod " 569 PTR_FORMAT " in its strong code roots " 570 "with no pointers into region", 571 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm)); 572 _failures = true; 573 } else if (oop_cl.failures()) { 574 gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] has other " 575 "failures for nmethod " PTR_FORMAT, 576 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm)); 577 _failures = true; 578 } 579 } 580 } 581 } 582 583 bool failures() { return _failures; } 584 }; 585 586 void HeapRegion::verify_strong_code_roots(VerifyOption vo, bool* failures) const { 587 if (!G1VerifyHeapRegionCodeRoots) { 588 // We're not verifying code roots. 589 return; 590 } 591 if (vo == VerifyOption_G1UseMarkWord) { 592 // Marking verification during a full GC is performed after class 593 // unloading, code cache unloading, etc so the strong code roots 594 // attached to each heap region are in an inconsistent state. They won't 595 // be consistent until the strong code roots are rebuilt after the 596 // actual GC. Skip verifying the strong code roots in this particular 597 // time. 598 assert(VerifyDuringGC, "only way to get here"); 599 return; 600 } 601 602 HeapRegionRemSet* hrrs = rem_set(); 603 size_t strong_code_roots_length = hrrs->strong_code_roots_list_length(); 604 605 // if this region is empty then there should be no entries 606 // on its strong code root list 607 if (is_empty()) { 608 if (strong_code_roots_length > 0) { 609 gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] is empty " 610 "but has " SIZE_FORMAT " code root entries", 611 p2i(bottom()), p2i(end()), strong_code_roots_length); 612 *failures = true; 613 } 614 return; 615 } 616 617 if (is_continues_humongous()) { 618 if (strong_code_roots_length > 0) { 619 gclog_or_tty->print_cr("region " HR_FORMAT " is a continuation of a humongous " 620 "region but has " SIZE_FORMAT " code root entries", 621 HR_FORMAT_PARAMS(this), strong_code_roots_length); 622 *failures = true; 623 } 624 return; 625 } 626 627 VerifyStrongCodeRootCodeBlobClosure cb_cl(this); 628 strong_code_roots_do(&cb_cl); 629 630 if (cb_cl.failures()) { 631 *failures = true; 632 } 633 } 634 635 void HeapRegion::print() const { print_on(gclog_or_tty); } 636 void HeapRegion::print_on(outputStream* st) const { 637 st->print("AC%4u", allocation_context()); 638 639 st->print(" %2s", get_short_type_str()); 640 if (in_collection_set()) 641 st->print(" CS"); 642 else 643 st->print(" "); 644 st->print(" TS %5d", _gc_time_stamp); 645 st->print(" PTAMS " PTR_FORMAT " NTAMS " PTR_FORMAT, 646 p2i(prev_top_at_mark_start()), p2i(next_top_at_mark_start())); 647 G1OffsetTableContigSpace::print_on(st); 648 } 649 650 class VerifyLiveClosure: public OopClosure { 651 private: 652 G1CollectedHeap* _g1h; 653 CardTableModRefBS* _bs; 654 oop _containing_obj; 655 bool _failures; 656 int _n_failures; 657 VerifyOption _vo; 658 public: 659 // _vo == UsePrevMarking -> use "prev" marking information, 660 // _vo == UseNextMarking -> use "next" marking information, 661 // _vo == UseMarkWord -> use mark word from object header. 662 VerifyLiveClosure(G1CollectedHeap* g1h, VerifyOption vo) : 663 _g1h(g1h), _bs(barrier_set_cast<CardTableModRefBS>(g1h->barrier_set())), 664 _containing_obj(NULL), _failures(false), _n_failures(0), _vo(vo) 665 { } 666 667 void set_containing_obj(oop obj) { 668 _containing_obj = obj; 669 } 670 671 bool failures() { return _failures; } 672 int n_failures() { return _n_failures; } 673 674 virtual void do_oop(narrowOop* p) { do_oop_work(p); } 675 virtual void do_oop( oop* p) { do_oop_work(p); } 676 677 void print_object(outputStream* out, oop obj) { 678 #ifdef PRODUCT 679 Klass* k = obj->klass(); 680 const char* class_name = InstanceKlass::cast(k)->external_name(); 681 out->print_cr("class name %s", class_name); 682 #else // PRODUCT 683 obj->print_on(out); 684 #endif // PRODUCT 685 } 686 687 template <class T> 688 void do_oop_work(T* p) { 689 assert(_containing_obj != NULL, "Precondition"); 690 assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo), 691 "Precondition"); 692 T heap_oop = oopDesc::load_heap_oop(p); 693 if (!oopDesc::is_null(heap_oop)) { 694 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop); 695 bool failed = false; 696 if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead_cond(obj, _vo)) { 697 MutexLockerEx x(ParGCRareEvent_lock, 698 Mutex::_no_safepoint_check_flag); 699 700 if (!_failures) { 701 gclog_or_tty->cr(); 702 gclog_or_tty->print_cr("----------"); 703 } 704 if (!_g1h->is_in_closed_subset(obj)) { 705 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p); 706 gclog_or_tty->print_cr("Field " PTR_FORMAT 707 " of live obj " PTR_FORMAT " in region " 708 "[" PTR_FORMAT ", " PTR_FORMAT ")", 709 p2i(p), p2i(_containing_obj), 710 p2i(from->bottom()), p2i(from->end())); 711 print_object(gclog_or_tty, _containing_obj); 712 gclog_or_tty->print_cr("points to obj " PTR_FORMAT " not in the heap", 713 p2i(obj)); 714 } else { 715 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p); 716 HeapRegion* to = _g1h->heap_region_containing((HeapWord*)obj); 717 gclog_or_tty->print_cr("Field " PTR_FORMAT 718 " of live obj " PTR_FORMAT " in region " 719 "[" PTR_FORMAT ", " PTR_FORMAT ")", 720 p2i(p), p2i(_containing_obj), 721 p2i(from->bottom()), p2i(from->end())); 722 print_object(gclog_or_tty, _containing_obj); 723 gclog_or_tty->print_cr("points to dead obj " PTR_FORMAT " in region " 724 "[" PTR_FORMAT ", " PTR_FORMAT ")", 725 p2i(obj), p2i(to->bottom()), p2i(to->end())); 726 print_object(gclog_or_tty, obj); 727 } 728 gclog_or_tty->print_cr("----------"); 729 gclog_or_tty->flush(); 730 _failures = true; 731 failed = true; 732 _n_failures++; 733 } 734 735 if (!_g1h->collector_state()->full_collection() || G1VerifyRSetsDuringFullGC) { 736 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p); 737 HeapRegion* to = _g1h->heap_region_containing(obj); 738 if (from != NULL && to != NULL && 739 from != to && 740 !to->is_pinned()) { 741 jbyte cv_obj = *_bs->byte_for_const(_containing_obj); 742 jbyte cv_field = *_bs->byte_for_const(p); 743 const jbyte dirty = CardTableModRefBS::dirty_card_val(); 744 745 bool is_bad = !(from->is_young() 746 || to->rem_set()->contains_reference(p) 747 || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed 748 (_containing_obj->is_objArray() ? 749 cv_field == dirty 750 : cv_obj == dirty || cv_field == dirty)); 751 if (is_bad) { 752 MutexLockerEx x(ParGCRareEvent_lock, 753 Mutex::_no_safepoint_check_flag); 754 755 if (!_failures) { 756 gclog_or_tty->cr(); 757 gclog_or_tty->print_cr("----------"); 758 } 759 gclog_or_tty->print_cr("Missing rem set entry:"); 760 gclog_or_tty->print_cr("Field " PTR_FORMAT " " 761 "of obj " PTR_FORMAT ", " 762 "in region " HR_FORMAT, 763 p2i(p), p2i(_containing_obj), 764 HR_FORMAT_PARAMS(from)); 765 _containing_obj->print_on(gclog_or_tty); 766 gclog_or_tty->print_cr("points to obj " PTR_FORMAT " " 767 "in region " HR_FORMAT, 768 p2i(obj), 769 HR_FORMAT_PARAMS(to)); 770 obj->print_on(gclog_or_tty); 771 gclog_or_tty->print_cr("Obj head CTE = %d, field CTE = %d.", 772 cv_obj, cv_field); 773 gclog_or_tty->print_cr("----------"); 774 gclog_or_tty->flush(); 775 _failures = true; 776 if (!failed) _n_failures++; 777 } 778 } 779 } 780 } 781 } 782 }; 783 784 // This really ought to be commoned up into OffsetTableContigSpace somehow. 785 // We would need a mechanism to make that code skip dead objects. 786 787 void HeapRegion::verify(VerifyOption vo, 788 bool* failures) const { 789 G1CollectedHeap* g1 = G1CollectedHeap::heap(); 790 *failures = false; 791 HeapWord* p = bottom(); 792 HeapWord* prev_p = NULL; 793 VerifyLiveClosure vl_cl(g1, vo); 794 bool is_region_humongous = is_humongous(); 795 size_t object_num = 0; 796 while (p < top()) { 797 oop obj = oop(p); 798 size_t obj_size = block_size(p); 799 object_num += 1; 800 801 if (is_region_humongous != g1->is_humongous(obj_size) && 802 !g1->is_obj_dead(obj, this)) { // Dead objects may have bigger block_size since they span several objects. 803 gclog_or_tty->print_cr("obj " PTR_FORMAT " is of %shumongous size (" 804 SIZE_FORMAT " words) in a %shumongous region", 805 p2i(p), g1->is_humongous(obj_size) ? "" : "non-", 806 obj_size, is_region_humongous ? "" : "non-"); 807 *failures = true; 808 return; 809 } 810 811 if (!g1->is_obj_dead_cond(obj, this, vo)) { 812 if (obj->is_oop()) { 813 Klass* klass = obj->klass(); 814 bool is_metaspace_object = Metaspace::contains(klass) || 815 (vo == VerifyOption_G1UsePrevMarking && 816 ClassLoaderDataGraph::unload_list_contains(klass)); 817 if (!is_metaspace_object) { 818 gclog_or_tty->print_cr("klass " PTR_FORMAT " of object " PTR_FORMAT " " 819 "not metadata", p2i(klass), p2i(obj)); 820 *failures = true; 821 return; 822 } else if (!klass->is_klass()) { 823 gclog_or_tty->print_cr("klass " PTR_FORMAT " of object " PTR_FORMAT " " 824 "not a klass", p2i(klass), p2i(obj)); 825 *failures = true; 826 return; 827 } else { 828 vl_cl.set_containing_obj(obj); 829 obj->oop_iterate_no_header(&vl_cl); 830 if (vl_cl.failures()) { 831 *failures = true; 832 } 833 if (G1MaxVerifyFailures >= 0 && 834 vl_cl.n_failures() >= G1MaxVerifyFailures) { 835 return; 836 } 837 } 838 } else { 839 gclog_or_tty->print_cr(PTR_FORMAT " no an oop", p2i(obj)); 840 *failures = true; 841 return; 842 } 843 } 844 prev_p = p; 845 p += obj_size; 846 } 847 848 if (!is_young() && !is_empty()) { 849 _offsets.verify(); 850 } 851 852 if (p != top()) { 853 gclog_or_tty->print_cr("end of last object " PTR_FORMAT " " 854 "does not match top " PTR_FORMAT, p2i(p), p2i(top())); 855 *failures = true; 856 return; 857 } 858 859 HeapWord* the_end = end(); 860 assert(p == top(), "it should still hold"); 861 // Do some extra BOT consistency checking for addresses in the 862 // range [top, end). BOT look-ups in this range should yield 863 // top. No point in doing that if top == end (there's nothing there). 864 if (p < the_end) { 865 // Look up top 866 HeapWord* addr_1 = p; 867 HeapWord* b_start_1 = _offsets.block_start_const(addr_1); 868 if (b_start_1 != p) { 869 gclog_or_tty->print_cr("BOT look up for top: " PTR_FORMAT " " 870 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT, 871 p2i(addr_1), p2i(b_start_1), p2i(p)); 872 *failures = true; 873 return; 874 } 875 876 // Look up top + 1 877 HeapWord* addr_2 = p + 1; 878 if (addr_2 < the_end) { 879 HeapWord* b_start_2 = _offsets.block_start_const(addr_2); 880 if (b_start_2 != p) { 881 gclog_or_tty->print_cr("BOT look up for top + 1: " PTR_FORMAT " " 882 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT, 883 p2i(addr_2), p2i(b_start_2), p2i(p)); 884 *failures = true; 885 return; 886 } 887 } 888 889 // Look up an address between top and end 890 size_t diff = pointer_delta(the_end, p) / 2; 891 HeapWord* addr_3 = p + diff; 892 if (addr_3 < the_end) { 893 HeapWord* b_start_3 = _offsets.block_start_const(addr_3); 894 if (b_start_3 != p) { 895 gclog_or_tty->print_cr("BOT look up for top + diff: " PTR_FORMAT " " 896 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT, 897 p2i(addr_3), p2i(b_start_3), p2i(p)); 898 *failures = true; 899 return; 900 } 901 } 902 903 // Look up end - 1 904 HeapWord* addr_4 = the_end - 1; 905 HeapWord* b_start_4 = _offsets.block_start_const(addr_4); 906 if (b_start_4 != p) { 907 gclog_or_tty->print_cr("BOT look up for end - 1: " PTR_FORMAT " " 908 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT, 909 p2i(addr_4), p2i(b_start_4), p2i(p)); 910 *failures = true; 911 return; 912 } 913 } 914 915 if (is_region_humongous && object_num > 1) { 916 gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] is humongous " 917 "but has " SIZE_FORMAT ", objects", 918 p2i(bottom()), p2i(end()), object_num); 919 *failures = true; 920 return; 921 } 922 923 verify_strong_code_roots(vo, failures); 924 } 925 926 void HeapRegion::verify() const { 927 bool dummy = false; 928 verify(VerifyOption_G1UsePrevMarking, /* failures */ &dummy); 929 } 930 931 void HeapRegion::prepare_for_compaction(CompactPoint* cp) { 932 scan_and_forward(this, cp); 933 } 934 935 // G1OffsetTableContigSpace code; copied from space.cpp. Hope this can go 936 // away eventually. 937 938 void G1OffsetTableContigSpace::clear(bool mangle_space) { 939 set_top(bottom()); 940 _scan_top = bottom(); 941 CompactibleSpace::clear(mangle_space); 942 reset_bot(); 943 } 944 945 void G1OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) { 946 Space::set_bottom(new_bottom); 947 _offsets.set_bottom(new_bottom); 948 } 949 950 void G1OffsetTableContigSpace::set_end(HeapWord* new_end) { 951 Space::set_end(new_end); 952 _offsets.resize(new_end - bottom()); 953 } 954 955 #ifndef PRODUCT 956 void G1OffsetTableContigSpace::mangle_unused_area() { 957 mangle_unused_area_complete(); 958 } 959 960 void G1OffsetTableContigSpace::mangle_unused_area_complete() { 961 SpaceMangler::mangle_region(MemRegion(top(), end())); 962 } 963 #endif 964 965 void G1OffsetTableContigSpace::print() const { 966 print_short(); 967 gclog_or_tty->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " 968 INTPTR_FORMAT ", " INTPTR_FORMAT ")", 969 p2i(bottom()), p2i(top()), p2i(_offsets.threshold()), p2i(end())); 970 } 971 972 HeapWord* G1OffsetTableContigSpace::initialize_threshold() { 973 return _offsets.initialize_threshold(); 974 } 975 976 HeapWord* G1OffsetTableContigSpace::cross_threshold(HeapWord* start, 977 HeapWord* end) { 978 _offsets.alloc_block(start, end); 979 return _offsets.threshold(); 980 } 981 982 HeapWord* G1OffsetTableContigSpace::scan_top() const { 983 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 984 HeapWord* local_top = top(); 985 OrderAccess::loadload(); 986 const unsigned local_time_stamp = _gc_time_stamp; 987 assert(local_time_stamp <= g1h->get_gc_time_stamp(), "invariant"); 988 if (local_time_stamp < g1h->get_gc_time_stamp()) { 989 return local_top; 990 } else { 991 return _scan_top; 992 } 993 } 994 995 void G1OffsetTableContigSpace::record_timestamp() { 996 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 997 unsigned curr_gc_time_stamp = g1h->get_gc_time_stamp(); 998 999 if (_gc_time_stamp < curr_gc_time_stamp) { 1000 // Setting the time stamp here tells concurrent readers to look at 1001 // scan_top to know the maximum allowed address to look at. 1002 1003 // scan_top should be bottom for all regions except for the 1004 // retained old alloc region which should have scan_top == top 1005 HeapWord* st = _scan_top; 1006 guarantee(st == _bottom || st == _top, "invariant"); 1007 1008 _gc_time_stamp = curr_gc_time_stamp; 1009 } 1010 } 1011 1012 void G1OffsetTableContigSpace::record_retained_region() { 1013 // scan_top is the maximum address where it's safe for the next gc to 1014 // scan this region. 1015 _scan_top = top(); 1016 } 1017 1018 void G1OffsetTableContigSpace::safe_object_iterate(ObjectClosure* blk) { 1019 object_iterate(blk); 1020 } 1021 1022 void G1OffsetTableContigSpace::object_iterate(ObjectClosure* blk) { 1023 HeapWord* p = bottom(); 1024 while (p < top()) { 1025 if (block_is_obj(p)) { 1026 blk->do_object(oop(p)); 1027 } 1028 p += block_size(p); 1029 } 1030 } 1031 1032 G1OffsetTableContigSpace:: 1033 G1OffsetTableContigSpace(G1BlockOffsetSharedArray* sharedOffsetArray, 1034 MemRegion mr) : 1035 _offsets(sharedOffsetArray, mr), 1036 _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true), 1037 _gc_time_stamp(0) 1038 { 1039 _offsets.set_space(this); 1040 } 1041 1042 void G1OffsetTableContigSpace::initialize(MemRegion mr, bool clear_space, bool mangle_space) { 1043 CompactibleSpace::initialize(mr, clear_space, mangle_space); 1044 _top = bottom(); 1045 _scan_top = bottom(); 1046 set_saved_mark_word(NULL); 1047 reset_bot(); 1048 }