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