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