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