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