1 /* 2 * Copyright (c) 2001, 2019, 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/g1CollectionSet.hpp" 30 #include "gc/g1/g1HeapRegionTraceType.hpp" 31 #include "gc/g1/g1NUMA.hpp" 32 #include "gc/g1/g1OopClosures.inline.hpp" 33 #include "gc/g1/heapRegion.inline.hpp" 34 #include "gc/g1/heapRegionBounds.inline.hpp" 35 #include "gc/g1/heapRegionManager.inline.hpp" 36 #include "gc/g1/heapRegionRemSet.hpp" 37 #include "gc/g1/heapRegionTracer.hpp" 38 #include "gc/shared/genOopClosures.inline.hpp" 39 #include "logging/log.hpp" 40 #include "logging/logStream.hpp" 41 #include "memory/iterator.inline.hpp" 42 #include "memory/resourceArea.hpp" 43 #include "oops/access.inline.hpp" 44 #include "oops/compressedOops.inline.hpp" 45 #include "oops/oop.inline.hpp" 46 #include "runtime/atomic.hpp" 47 #include "runtime/orderAccess.hpp" 48 49 int HeapRegion::LogOfHRGrainBytes = 0; 50 int HeapRegion::LogOfHRGrainWords = 0; 51 int HeapRegion::LogCardsPerRegion = 0; 52 size_t HeapRegion::GrainBytes = 0; 53 size_t HeapRegion::GrainWords = 0; 54 size_t HeapRegion::CardsPerRegion = 0; 55 56 size_t HeapRegion::max_region_size() { 57 return HeapRegionBounds::max_size(); 58 } 59 60 size_t HeapRegion::min_region_size_in_words() { 61 return HeapRegionBounds::min_size() >> LogHeapWordSize; 62 } 63 64 void HeapRegion::setup_heap_region_size(size_t initial_heap_size, size_t max_heap_size) { 65 size_t region_size = G1HeapRegionSize; 66 if (FLAG_IS_DEFAULT(G1HeapRegionSize)) { 67 size_t average_heap_size = (initial_heap_size + max_heap_size) / 2; 68 region_size = MAX2(average_heap_size / HeapRegionBounds::target_number(), 69 HeapRegionBounds::min_size()); 70 } 71 72 int region_size_log = log2_long((jlong) region_size); 73 // Recalculate the region size to make sure it's a power of 74 // 2. This means that region_size is the largest power of 2 that's 75 // <= what we've calculated so far. 76 region_size = ((size_t)1 << region_size_log); 77 78 // Now make sure that we don't go over or under our limits. 79 if (region_size < HeapRegionBounds::min_size()) { 80 region_size = HeapRegionBounds::min_size(); 81 } else if (region_size > HeapRegionBounds::max_size()) { 82 region_size = HeapRegionBounds::max_size(); 83 } 84 85 // And recalculate the log. 86 region_size_log = log2_long((jlong) region_size); 87 88 // Now, set up the globals. 89 guarantee(LogOfHRGrainBytes == 0, "we should only set it once"); 90 LogOfHRGrainBytes = region_size_log; 91 92 guarantee(LogOfHRGrainWords == 0, "we should only set it once"); 93 LogOfHRGrainWords = LogOfHRGrainBytes - LogHeapWordSize; 94 95 guarantee(GrainBytes == 0, "we should only set it once"); 96 // The cast to int is safe, given that we've bounded region_size by 97 // MIN_REGION_SIZE and MAX_REGION_SIZE. 98 GrainBytes = region_size; 99 log_info(gc, heap)("Heap region size: " SIZE_FORMAT "M", GrainBytes / M); 100 101 guarantee(GrainWords == 0, "we should only set it once"); 102 GrainWords = GrainBytes >> LogHeapWordSize; 103 guarantee((size_t) 1 << LogOfHRGrainWords == GrainWords, "sanity"); 104 105 guarantee(CardsPerRegion == 0, "we should only set it once"); 106 CardsPerRegion = GrainBytes >> G1CardTable::card_shift; 107 108 LogCardsPerRegion = log2_long((jlong) CardsPerRegion); 109 110 if (G1HeapRegionSize != GrainBytes) { 111 FLAG_SET_ERGO(G1HeapRegionSize, GrainBytes); 112 } 113 } 114 115 void HeapRegion::handle_evacuation_failed() { 116 uninstall_surv_rate_group(); 117 clear_young_index_in_cset(); 118 set_evacuation_failed(false); 119 set_old(); 120 } 121 122 void HeapRegion::unlink_from_list() { 123 set_next(NULL); 124 set_prev(NULL); 125 set_containing_set(NULL); 126 } 127 128 void HeapRegion::hr_clear(bool keep_remset, bool clear_space, bool locked) { 129 assert(_humongous_start_region == NULL, 130 "we should have already filtered out humongous regions"); 131 assert(!in_collection_set(), 132 "Should not clear heap region %u in the collection set", hrm_index()); 133 134 clear_young_index_in_cset(); 135 clear_index_in_opt_cset(); 136 uninstall_surv_rate_group(); 137 set_free(); 138 reset_pre_dummy_top(); 139 140 if (!keep_remset) { 141 if (locked) { 142 rem_set()->clear_locked(); 143 } else { 144 rem_set()->clear(); 145 } 146 } 147 148 zero_marked_bytes(); 149 150 init_top_at_mark_start(); 151 if (clear_space) clear(SpaceDecorator::Mangle); 152 153 _evacuation_failed = false; 154 _gc_efficiency = 0.0; 155 _recorded_rs_length = 0; 156 _predicted_elapsed_time_ms = 0.0; 157 } 158 159 void HeapRegion::clear_cardtable() { 160 G1CardTable* ct = G1CollectedHeap::heap()->card_table(); 161 ct->clear(MemRegion(bottom(), end())); 162 } 163 164 void HeapRegion::calc_gc_efficiency() { 165 // GC efficiency is the ratio of how much space would be 166 // reclaimed over how long we predict it would take to reclaim it. 167 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 168 G1Policy* policy = g1h->policy(); 169 170 // Retrieve a prediction of the elapsed time for this region for 171 // a mixed gc because the region will only be evacuated during a 172 // mixed gc. 173 double region_elapsed_time_ms = 174 policy->predict_region_elapsed_time_ms(this, false /* for_young_gc */); 175 _gc_efficiency = (double) reclaimable_bytes() / region_elapsed_time_ms; 176 } 177 178 void HeapRegion::set_free() { 179 report_region_type_change(G1HeapRegionTraceType::Free); 180 _type.set_free(); 181 } 182 183 void HeapRegion::set_eden() { 184 report_region_type_change(G1HeapRegionTraceType::Eden); 185 _type.set_eden(); 186 } 187 188 void HeapRegion::set_eden_pre_gc() { 189 report_region_type_change(G1HeapRegionTraceType::Eden); 190 _type.set_eden_pre_gc(); 191 } 192 193 void HeapRegion::set_survivor() { 194 report_region_type_change(G1HeapRegionTraceType::Survivor); 195 _type.set_survivor(); 196 } 197 198 void HeapRegion::move_to_old() { 199 if (_type.relabel_as_old()) { 200 report_region_type_change(G1HeapRegionTraceType::Old); 201 } 202 } 203 204 void HeapRegion::set_old() { 205 report_region_type_change(G1HeapRegionTraceType::Old); 206 _type.set_old(); 207 } 208 209 void HeapRegion::set_open_archive() { 210 report_region_type_change(G1HeapRegionTraceType::OpenArchive); 211 _type.set_open_archive(); 212 } 213 214 void HeapRegion::set_closed_archive() { 215 report_region_type_change(G1HeapRegionTraceType::ClosedArchive); 216 _type.set_closed_archive(); 217 } 218 219 void HeapRegion::set_starts_humongous(HeapWord* obj_top, size_t fill_size) { 220 assert(!is_humongous(), "sanity / pre-condition"); 221 assert(top() == bottom(), "should be empty"); 222 223 report_region_type_change(G1HeapRegionTraceType::StartsHumongous); 224 _type.set_starts_humongous(); 225 _humongous_start_region = this; 226 227 _bot_part.set_for_starts_humongous(obj_top, fill_size); 228 } 229 230 void HeapRegion::set_continues_humongous(HeapRegion* first_hr) { 231 assert(!is_humongous(), "sanity / pre-condition"); 232 assert(top() == bottom(), "should be empty"); 233 assert(first_hr->is_starts_humongous(), "pre-condition"); 234 235 report_region_type_change(G1HeapRegionTraceType::ContinuesHumongous); 236 _type.set_continues_humongous(); 237 _humongous_start_region = first_hr; 238 239 _bot_part.set_object_can_span(true); 240 } 241 242 void HeapRegion::clear_humongous() { 243 assert(is_humongous(), "pre-condition"); 244 245 assert(capacity() == HeapRegion::GrainBytes, "pre-condition"); 246 _humongous_start_region = NULL; 247 248 _bot_part.set_object_can_span(false); 249 } 250 251 HeapRegion::HeapRegion(uint hrm_index, 252 G1BlockOffsetTable* bot, 253 MemRegion mr) : 254 _bottom(mr.start()), 255 _end(mr.end()), 256 _top(NULL), 257 _compaction_top(NULL), 258 _bot_part(bot, this), 259 _par_alloc_lock(Mutex::leaf, "HeapRegion par alloc lock", true), 260 _pre_dummy_top(NULL), 261 _rem_set(NULL), 262 _hrm_index(hrm_index), 263 _type(), 264 _humongous_start_region(NULL), 265 _evacuation_failed(false), 266 _index_in_opt_cset(InvalidCSetIndex), 267 _next(NULL), _prev(NULL), 268 #ifdef ASSERT 269 _containing_set(NULL), 270 #endif 271 _prev_top_at_mark_start(NULL), _next_top_at_mark_start(NULL), 272 _prev_marked_bytes(0), _next_marked_bytes(0), 273 _young_index_in_cset(-1), 274 _surv_rate_group(NULL), _age_index(-1), _gc_efficiency(0.0), 275 _recorded_rs_length(0), _predicted_elapsed_time_ms(0), 276 _node_index(G1NUMA::UnknownNodeIndex) 277 { 278 assert(Universe::on_page_boundary(mr.start()) && Universe::on_page_boundary(mr.end()), 279 "invalid space boundaries"); 280 281 _rem_set = new HeapRegionRemSet(bot, this); 282 initialize(); 283 } 284 285 void HeapRegion::initialize(bool clear_space, bool mangle_space) { 286 assert(_rem_set->is_empty(), "Remembered set must be empty"); 287 288 if (clear_space) { 289 clear(mangle_space); 290 } 291 292 set_top(bottom()); 293 set_compaction_top(bottom()); 294 reset_bot(); 295 296 hr_clear(false /*par*/, false /*clear_space*/); 297 } 298 299 void HeapRegion::report_region_type_change(G1HeapRegionTraceType::Type to) { 300 HeapRegionTracer::send_region_type_change(_hrm_index, 301 get_trace_type(), 302 to, 303 (uintptr_t)bottom(), 304 used()); 305 } 306 307 void HeapRegion::note_self_forwarding_removal_start(bool during_initial_mark, 308 bool during_conc_mark) { 309 // We always recreate the prev marking info and we'll explicitly 310 // mark all objects we find to be self-forwarded on the prev 311 // bitmap. So all objects need to be below PTAMS. 312 _prev_marked_bytes = 0; 313 314 if (during_initial_mark) { 315 // During initial-mark, we'll also explicitly mark all objects 316 // we find to be self-forwarded on the next bitmap. So all 317 // objects need to be below NTAMS. 318 _next_top_at_mark_start = top(); 319 _next_marked_bytes = 0; 320 } else if (during_conc_mark) { 321 // During concurrent mark, all objects in the CSet (including 322 // the ones we find to be self-forwarded) are implicitly live. 323 // So all objects need to be above NTAMS. 324 _next_top_at_mark_start = bottom(); 325 _next_marked_bytes = 0; 326 } 327 } 328 329 void HeapRegion::note_self_forwarding_removal_end(size_t marked_bytes) { 330 assert(marked_bytes <= used(), 331 "marked: " SIZE_FORMAT " used: " SIZE_FORMAT, marked_bytes, used()); 332 _prev_top_at_mark_start = top(); 333 _prev_marked_bytes = marked_bytes; 334 } 335 336 // Code roots support 337 338 void HeapRegion::add_strong_code_root(nmethod* nm) { 339 HeapRegionRemSet* hrrs = rem_set(); 340 hrrs->add_strong_code_root(nm); 341 } 342 343 void HeapRegion::add_strong_code_root_locked(nmethod* nm) { 344 assert_locked_or_safepoint(CodeCache_lock); 345 HeapRegionRemSet* hrrs = rem_set(); 346 hrrs->add_strong_code_root_locked(nm); 347 } 348 349 void HeapRegion::remove_strong_code_root(nmethod* nm) { 350 HeapRegionRemSet* hrrs = rem_set(); 351 hrrs->remove_strong_code_root(nm); 352 } 353 354 void HeapRegion::strong_code_roots_do(CodeBlobClosure* blk) const { 355 HeapRegionRemSet* hrrs = rem_set(); 356 hrrs->strong_code_roots_do(blk); 357 } 358 359 class VerifyStrongCodeRootOopClosure: public OopClosure { 360 const HeapRegion* _hr; 361 bool _failures; 362 bool _has_oops_in_region; 363 364 template <class T> void do_oop_work(T* p) { 365 T heap_oop = RawAccess<>::oop_load(p); 366 if (!CompressedOops::is_null(heap_oop)) { 367 oop obj = CompressedOops::decode_not_null(heap_oop); 368 369 // Note: not all the oops embedded in the nmethod are in the 370 // current region. We only look at those which are. 371 if (_hr->is_in(obj)) { 372 // Object is in the region. Check that its less than top 373 if (_hr->top() <= (HeapWord*)obj) { 374 // Object is above top 375 log_error(gc, verify)("Object " PTR_FORMAT " in region " HR_FORMAT " is above top ", 376 p2i(obj), HR_FORMAT_PARAMS(_hr)); 377 _failures = true; 378 return; 379 } 380 // Nmethod has at least one oop in the current region 381 _has_oops_in_region = true; 382 } 383 } 384 } 385 386 public: 387 VerifyStrongCodeRootOopClosure(const HeapRegion* hr): 388 _hr(hr), _failures(false), _has_oops_in_region(false) {} 389 390 void do_oop(narrowOop* p) { do_oop_work(p); } 391 void do_oop(oop* p) { do_oop_work(p); } 392 393 bool failures() { return _failures; } 394 bool has_oops_in_region() { return _has_oops_in_region; } 395 }; 396 397 class VerifyStrongCodeRootCodeBlobClosure: public CodeBlobClosure { 398 const HeapRegion* _hr; 399 bool _failures; 400 public: 401 VerifyStrongCodeRootCodeBlobClosure(const HeapRegion* hr) : 402 _hr(hr), _failures(false) {} 403 404 void do_code_blob(CodeBlob* cb) { 405 nmethod* nm = (cb == NULL) ? NULL : cb->as_compiled_method()->as_nmethod_or_null(); 406 if (nm != NULL) { 407 // Verify that the nemthod is live 408 if (!nm->is_alive()) { 409 log_error(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] has dead nmethod " PTR_FORMAT " in its strong code roots", 410 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm)); 411 _failures = true; 412 } else { 413 VerifyStrongCodeRootOopClosure oop_cl(_hr); 414 nm->oops_do(&oop_cl); 415 if (!oop_cl.has_oops_in_region()) { 416 log_error(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] has nmethod " PTR_FORMAT " in its strong code roots with no pointers into region", 417 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm)); 418 _failures = true; 419 } else if (oop_cl.failures()) { 420 log_error(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] has other failures for nmethod " PTR_FORMAT, 421 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm)); 422 _failures = true; 423 } 424 } 425 } 426 } 427 428 bool failures() { return _failures; } 429 }; 430 431 void HeapRegion::verify_strong_code_roots(VerifyOption vo, bool* failures) const { 432 if (!G1VerifyHeapRegionCodeRoots) { 433 // We're not verifying code roots. 434 return; 435 } 436 if (vo == VerifyOption_G1UseFullMarking) { 437 // Marking verification during a full GC is performed after class 438 // unloading, code cache unloading, etc so the strong code roots 439 // attached to each heap region are in an inconsistent state. They won't 440 // be consistent until the strong code roots are rebuilt after the 441 // actual GC. Skip verifying the strong code roots in this particular 442 // time. 443 assert(VerifyDuringGC, "only way to get here"); 444 return; 445 } 446 447 HeapRegionRemSet* hrrs = rem_set(); 448 size_t strong_code_roots_length = hrrs->strong_code_roots_list_length(); 449 450 // if this region is empty then there should be no entries 451 // on its strong code root list 452 if (is_empty()) { 453 if (strong_code_roots_length > 0) { 454 log_error(gc, verify)("region " HR_FORMAT " is empty but has " SIZE_FORMAT " code root entries", 455 HR_FORMAT_PARAMS(this), strong_code_roots_length); 456 *failures = true; 457 } 458 return; 459 } 460 461 if (is_continues_humongous()) { 462 if (strong_code_roots_length > 0) { 463 log_error(gc, verify)("region " HR_FORMAT " is a continuation of a humongous region but has " SIZE_FORMAT " code root entries", 464 HR_FORMAT_PARAMS(this), strong_code_roots_length); 465 *failures = true; 466 } 467 return; 468 } 469 470 VerifyStrongCodeRootCodeBlobClosure cb_cl(this); 471 strong_code_roots_do(&cb_cl); 472 473 if (cb_cl.failures()) { 474 *failures = true; 475 } 476 } 477 478 void HeapRegion::print() const { print_on(tty); } 479 480 void HeapRegion::print_on(outputStream* st) const { 481 st->print("|%4u", this->_hrm_index); 482 st->print("|" PTR_FORMAT ", " PTR_FORMAT ", " PTR_FORMAT, 483 p2i(bottom()), p2i(top()), p2i(end())); 484 st->print("|%3d%%", (int) ((double) used() * 100 / capacity())); 485 st->print("|%2s", get_short_type_str()); 486 if (in_collection_set()) { 487 st->print("|CS"); 488 } else { 489 st->print("| "); 490 } 491 st->print("|TAMS " PTR_FORMAT ", " PTR_FORMAT "| %s ", 492 p2i(prev_top_at_mark_start()), p2i(next_top_at_mark_start()), rem_set()->get_state_str()); 493 if (UseNUMA) { 494 G1NUMA* numa = G1NUMA::numa(); 495 if (node_index() < numa->num_active_nodes()) { 496 st->print("|%d", numa->numa_id(node_index())); 497 } else { 498 st->print("|-"); 499 } 500 } 501 st->print_cr(""); 502 } 503 504 class G1VerificationClosure : public BasicOopIterateClosure { 505 protected: 506 G1CollectedHeap* _g1h; 507 G1CardTable *_ct; 508 oop _containing_obj; 509 bool _failures; 510 int _n_failures; 511 VerifyOption _vo; 512 public: 513 // _vo == UsePrevMarking -> use "prev" marking information, 514 // _vo == UseNextMarking -> use "next" marking information, 515 // _vo == UseFullMarking -> use "next" marking bitmap but no TAMS. 516 G1VerificationClosure(G1CollectedHeap* g1h, VerifyOption vo) : 517 _g1h(g1h), _ct(g1h->card_table()), 518 _containing_obj(NULL), _failures(false), _n_failures(0), _vo(vo) { 519 } 520 521 void set_containing_obj(oop obj) { 522 _containing_obj = obj; 523 } 524 525 bool failures() { return _failures; } 526 int n_failures() { return _n_failures; } 527 528 void print_object(outputStream* out, oop obj) { 529 #ifdef PRODUCT 530 Klass* k = obj->klass(); 531 const char* class_name = k->external_name(); 532 out->print_cr("class name %s", class_name); 533 #else // PRODUCT 534 obj->print_on(out); 535 #endif // PRODUCT 536 } 537 538 // This closure provides its own oop verification code. 539 debug_only(virtual bool should_verify_oops() { return false; }) 540 }; 541 542 class VerifyLiveClosure : public G1VerificationClosure { 543 public: 544 VerifyLiveClosure(G1CollectedHeap* g1h, VerifyOption vo) : G1VerificationClosure(g1h, vo) {} 545 virtual void do_oop(narrowOop* p) { do_oop_work(p); } 546 virtual void do_oop(oop* p) { do_oop_work(p); } 547 548 template <class T> 549 void do_oop_work(T* p) { 550 assert(_containing_obj != NULL, "Precondition"); 551 assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo), 552 "Precondition"); 553 verify_liveness(p); 554 } 555 556 template <class T> 557 void verify_liveness(T* p) { 558 T heap_oop = RawAccess<>::oop_load(p); 559 Log(gc, verify) log; 560 if (!CompressedOops::is_null(heap_oop)) { 561 oop obj = CompressedOops::decode_not_null(heap_oop); 562 bool failed = false; 563 if (!_g1h->is_in(obj) || _g1h->is_obj_dead_cond(obj, _vo)) { 564 MutexLocker x(ParGCRareEvent_lock, 565 Mutex::_no_safepoint_check_flag); 566 567 if (!_failures) { 568 log.error("----------"); 569 } 570 ResourceMark rm; 571 if (!_g1h->is_in(obj)) { 572 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p); 573 log.error("Field " PTR_FORMAT " of live obj " PTR_FORMAT " in region " HR_FORMAT, 574 p2i(p), p2i(_containing_obj), HR_FORMAT_PARAMS(from)); 575 LogStream ls(log.error()); 576 print_object(&ls, _containing_obj); 577 HeapRegion* const to = _g1h->heap_region_containing(obj); 578 log.error("points to obj " PTR_FORMAT " in region " HR_FORMAT " remset %s", 579 p2i(obj), HR_FORMAT_PARAMS(to), to->rem_set()->get_state_str()); 580 } else { 581 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p); 582 HeapRegion* to = _g1h->heap_region_containing((HeapWord*)obj); 583 log.error("Field " PTR_FORMAT " of live obj " PTR_FORMAT " in region " HR_FORMAT, 584 p2i(p), p2i(_containing_obj), HR_FORMAT_PARAMS(from)); 585 LogStream ls(log.error()); 586 print_object(&ls, _containing_obj); 587 log.error("points to dead obj " PTR_FORMAT " in region " HR_FORMAT, 588 p2i(obj), HR_FORMAT_PARAMS(to)); 589 print_object(&ls, obj); 590 } 591 log.error("----------"); 592 _failures = true; 593 failed = true; 594 _n_failures++; 595 } 596 } 597 } 598 }; 599 600 class VerifyRemSetClosure : public G1VerificationClosure { 601 public: 602 VerifyRemSetClosure(G1CollectedHeap* g1h, VerifyOption vo) : G1VerificationClosure(g1h, vo) {} 603 virtual void do_oop(narrowOop* p) { do_oop_work(p); } 604 virtual void do_oop(oop* p) { do_oop_work(p); } 605 606 template <class T> 607 void do_oop_work(T* p) { 608 assert(_containing_obj != NULL, "Precondition"); 609 assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo), 610 "Precondition"); 611 verify_remembered_set(p); 612 } 613 614 template <class T> 615 void verify_remembered_set(T* p) { 616 T heap_oop = RawAccess<>::oop_load(p); 617 Log(gc, verify) log; 618 if (!CompressedOops::is_null(heap_oop)) { 619 oop obj = CompressedOops::decode_not_null(heap_oop); 620 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p); 621 HeapRegion* to = _g1h->heap_region_containing(obj); 622 if (from != NULL && to != NULL && 623 from != to && 624 !to->is_pinned() && 625 to->rem_set()->is_complete()) { 626 jbyte cv_obj = *_ct->byte_for_const(_containing_obj); 627 jbyte cv_field = *_ct->byte_for_const(p); 628 const jbyte dirty = G1CardTable::dirty_card_val(); 629 630 bool is_bad = !(from->is_young() 631 || to->rem_set()->contains_reference(p) 632 || (_containing_obj->is_objArray() ? 633 cv_field == dirty : 634 cv_obj == dirty || cv_field == dirty)); 635 if (is_bad) { 636 MutexLocker x(ParGCRareEvent_lock, 637 Mutex::_no_safepoint_check_flag); 638 639 if (!_failures) { 640 log.error("----------"); 641 } 642 log.error("Missing rem set entry:"); 643 log.error("Field " PTR_FORMAT " of obj " PTR_FORMAT " in region " HR_FORMAT, 644 p2i(p), p2i(_containing_obj), HR_FORMAT_PARAMS(from)); 645 ResourceMark rm; 646 LogStream ls(log.error()); 647 _containing_obj->print_on(&ls); 648 log.error("points to obj " PTR_FORMAT " in region " HR_FORMAT " remset %s", 649 p2i(obj), HR_FORMAT_PARAMS(to), to->rem_set()->get_state_str()); 650 if (oopDesc::is_oop(obj)) { 651 obj->print_on(&ls); 652 } 653 log.error("Obj head CTE = %d, field CTE = %d.", cv_obj, cv_field); 654 log.error("----------"); 655 _failures = true; 656 _n_failures++; 657 } 658 } 659 } 660 } 661 }; 662 663 // Closure that applies the given two closures in sequence. 664 class G1Mux2Closure : public BasicOopIterateClosure { 665 OopClosure* _c1; 666 OopClosure* _c2; 667 public: 668 G1Mux2Closure(OopClosure *c1, OopClosure *c2) { _c1 = c1; _c2 = c2; } 669 template <class T> inline void do_oop_work(T* p) { 670 // Apply first closure; then apply the second. 671 _c1->do_oop(p); 672 _c2->do_oop(p); 673 } 674 virtual inline void do_oop(oop* p) { do_oop_work(p); } 675 virtual inline void do_oop(narrowOop* p) { do_oop_work(p); } 676 677 // This closure provides its own oop verification code. 678 debug_only(virtual bool should_verify_oops() { return false; }) 679 }; 680 681 void HeapRegion::verify(VerifyOption vo, 682 bool* failures) const { 683 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 684 *failures = false; 685 HeapWord* p = bottom(); 686 HeapWord* prev_p = NULL; 687 VerifyLiveClosure vl_cl(g1h, vo); 688 VerifyRemSetClosure vr_cl(g1h, vo); 689 bool is_region_humongous = is_humongous(); 690 size_t object_num = 0; 691 while (p < top()) { 692 oop obj = oop(p); 693 size_t obj_size = block_size(p); 694 object_num += 1; 695 696 if (!g1h->is_obj_dead_cond(obj, this, vo)) { 697 if (oopDesc::is_oop(obj)) { 698 Klass* klass = obj->klass(); 699 bool is_metaspace_object = Metaspace::contains(klass); 700 if (!is_metaspace_object) { 701 log_error(gc, verify)("klass " PTR_FORMAT " of object " PTR_FORMAT " " 702 "not metadata", p2i(klass), p2i(obj)); 703 *failures = true; 704 return; 705 } else if (!klass->is_klass()) { 706 log_error(gc, verify)("klass " PTR_FORMAT " of object " PTR_FORMAT " " 707 "not a klass", p2i(klass), p2i(obj)); 708 *failures = true; 709 return; 710 } else { 711 vl_cl.set_containing_obj(obj); 712 if (!g1h->collector_state()->in_full_gc() || G1VerifyRSetsDuringFullGC) { 713 // verify liveness and rem_set 714 vr_cl.set_containing_obj(obj); 715 G1Mux2Closure mux(&vl_cl, &vr_cl); 716 obj->oop_iterate(&mux); 717 718 if (vr_cl.failures()) { 719 *failures = true; 720 } 721 if (G1MaxVerifyFailures >= 0 && 722 vr_cl.n_failures() >= G1MaxVerifyFailures) { 723 return; 724 } 725 } else { 726 // verify only liveness 727 obj->oop_iterate(&vl_cl); 728 } 729 if (vl_cl.failures()) { 730 *failures = true; 731 } 732 if (G1MaxVerifyFailures >= 0 && 733 vl_cl.n_failures() >= G1MaxVerifyFailures) { 734 return; 735 } 736 } 737 } else { 738 log_error(gc, verify)(PTR_FORMAT " not an oop", p2i(obj)); 739 *failures = true; 740 return; 741 } 742 } 743 prev_p = p; 744 p += obj_size; 745 } 746 747 if (!is_young() && !is_empty()) { 748 _bot_part.verify(); 749 } 750 751 if (is_region_humongous) { 752 oop obj = oop(this->humongous_start_region()->bottom()); 753 if ((HeapWord*)obj > bottom() || (HeapWord*)obj + obj->size() < bottom()) { 754 log_error(gc, verify)("this humongous region is not part of its' humongous object " PTR_FORMAT, p2i(obj)); 755 *failures = true; 756 return; 757 } 758 } 759 760 if (!is_region_humongous && p != top()) { 761 log_error(gc, verify)("end of last object " PTR_FORMAT " " 762 "does not match top " PTR_FORMAT, p2i(p), p2i(top())); 763 *failures = true; 764 return; 765 } 766 767 HeapWord* the_end = end(); 768 // Do some extra BOT consistency checking for addresses in the 769 // range [top, end). BOT look-ups in this range should yield 770 // top. No point in doing that if top == end (there's nothing there). 771 if (p < the_end) { 772 // Look up top 773 HeapWord* addr_1 = p; 774 HeapWord* b_start_1 = block_start_const(addr_1); 775 if (b_start_1 != p) { 776 log_error(gc, verify)("BOT look up for top: " PTR_FORMAT " " 777 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT, 778 p2i(addr_1), p2i(b_start_1), p2i(p)); 779 *failures = true; 780 return; 781 } 782 783 // Look up top + 1 784 HeapWord* addr_2 = p + 1; 785 if (addr_2 < the_end) { 786 HeapWord* b_start_2 = block_start_const(addr_2); 787 if (b_start_2 != p) { 788 log_error(gc, verify)("BOT look up for top + 1: " PTR_FORMAT " " 789 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT, 790 p2i(addr_2), p2i(b_start_2), p2i(p)); 791 *failures = true; 792 return; 793 } 794 } 795 796 // Look up an address between top and end 797 size_t diff = pointer_delta(the_end, p) / 2; 798 HeapWord* addr_3 = p + diff; 799 if (addr_3 < the_end) { 800 HeapWord* b_start_3 = block_start_const(addr_3); 801 if (b_start_3 != p) { 802 log_error(gc, verify)("BOT look up for top + diff: " PTR_FORMAT " " 803 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT, 804 p2i(addr_3), p2i(b_start_3), p2i(p)); 805 *failures = true; 806 return; 807 } 808 } 809 810 // Look up end - 1 811 HeapWord* addr_4 = the_end - 1; 812 HeapWord* b_start_4 = block_start_const(addr_4); 813 if (b_start_4 != p) { 814 log_error(gc, verify)("BOT look up for end - 1: " PTR_FORMAT " " 815 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT, 816 p2i(addr_4), p2i(b_start_4), p2i(p)); 817 *failures = true; 818 return; 819 } 820 } 821 822 verify_strong_code_roots(vo, failures); 823 } 824 825 void HeapRegion::verify() const { 826 bool dummy = false; 827 verify(VerifyOption_G1UsePrevMarking, /* failures */ &dummy); 828 } 829 830 void HeapRegion::verify_rem_set(VerifyOption vo, bool* failures) const { 831 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 832 *failures = false; 833 HeapWord* p = bottom(); 834 HeapWord* prev_p = NULL; 835 VerifyRemSetClosure vr_cl(g1h, vo); 836 while (p < top()) { 837 oop obj = oop(p); 838 size_t obj_size = block_size(p); 839 840 if (!g1h->is_obj_dead_cond(obj, this, vo)) { 841 if (oopDesc::is_oop(obj)) { 842 vr_cl.set_containing_obj(obj); 843 obj->oop_iterate(&vr_cl); 844 845 if (vr_cl.failures()) { 846 *failures = true; 847 } 848 if (G1MaxVerifyFailures >= 0 && 849 vr_cl.n_failures() >= G1MaxVerifyFailures) { 850 return; 851 } 852 } else { 853 log_error(gc, verify)(PTR_FORMAT " not an oop", p2i(obj)); 854 *failures = true; 855 return; 856 } 857 } 858 859 prev_p = p; 860 p += obj_size; 861 } 862 } 863 864 void HeapRegion::verify_rem_set() const { 865 bool failures = false; 866 verify_rem_set(VerifyOption_G1UsePrevMarking, &failures); 867 guarantee(!failures, "HeapRegion RemSet verification failed"); 868 } 869 870 void HeapRegion::clear(bool mangle_space) { 871 set_top(bottom()); 872 set_compaction_top(bottom()); 873 874 if (ZapUnusedHeapArea && mangle_space) { 875 mangle_unused_area(); 876 } 877 reset_bot(); 878 } 879 880 #ifndef PRODUCT 881 void HeapRegion::mangle_unused_area() { 882 SpaceMangler::mangle_region(MemRegion(top(), end())); 883 } 884 #endif 885 886 HeapWord* HeapRegion::initialize_threshold() { 887 return _bot_part.initialize_threshold(); 888 } 889 890 HeapWord* HeapRegion::cross_threshold(HeapWord* start, HeapWord* end) { 891 _bot_part.alloc_block(start, end); 892 return _bot_part.threshold(); 893 } 894 895 void HeapRegion::object_iterate(ObjectClosure* blk) { 896 HeapWord* p = bottom(); 897 while (p < top()) { 898 if (block_is_obj(p)) { 899 blk->do_object(oop(p)); 900 } 901 p += block_size(p); 902 } 903 }