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