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