1 /* 2 * Copyright (c) 2016, 2018, 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 "gc/g1/g1Allocator.inline.hpp" 27 #include "gc/g1/g1CollectedHeap.hpp" 28 #include "gc/g1/g1CollectedHeap.inline.hpp" 29 #include "gc/g1/g1ConcurrentMarkThread.hpp" 30 #include "gc/g1/g1HeapVerifier.hpp" 31 #include "gc/g1/g1Policy.hpp" 32 #include "gc/g1/g1RemSet.hpp" 33 #include "gc/g1/g1RootProcessor.hpp" 34 #include "gc/g1/heapRegion.hpp" 35 #include "gc/g1/heapRegion.inline.hpp" 36 #include "gc/g1/heapRegionRemSet.hpp" 37 #include "gc/g1/g1StringDedup.hpp" 38 #include "logging/log.hpp" 39 #include "logging/logStream.hpp" 40 #include "memory/resourceArea.hpp" 41 #include "oops/access.inline.hpp" 42 #include "oops/compressedOops.inline.hpp" 43 #include "oops/oop.inline.hpp" 44 #include "runtime/handles.inline.hpp" 45 46 int G1HeapVerifier::_enabled_verification_types = G1HeapVerifier::G1VerifyAll; 47 48 class VerifyRootsClosure: public OopClosure { 49 private: 50 G1CollectedHeap* _g1h; 51 VerifyOption _vo; 52 bool _failures; 53 public: 54 // _vo == UsePrevMarking -> use "prev" marking information, 55 // _vo == UseNextMarking -> use "next" marking information, 56 // _vo == UseFullMarking -> use "next" marking bitmap but no TAMS 57 VerifyRootsClosure(VerifyOption vo) : 58 _g1h(G1CollectedHeap::heap()), 59 _vo(vo), 60 _failures(false) { } 61 62 bool failures() { return _failures; } 63 64 template <class T> void do_oop_nv(T* p) { 65 T heap_oop = RawAccess<>::oop_load(p); 66 if (!CompressedOops::is_null(heap_oop)) { 67 oop obj = CompressedOops::decode_not_null(heap_oop); 68 if (_g1h->is_obj_dead_cond(obj, _vo)) { 69 Log(gc, verify) log; 70 log.error("Root location " PTR_FORMAT " points to dead obj " PTR_FORMAT, p2i(p), p2i(obj)); 71 ResourceMark rm; 72 LogStream ls(log.error()); 73 obj->print_on(&ls); 74 _failures = true; 75 } 76 } 77 } 78 79 void do_oop(oop* p) { do_oop_nv(p); } 80 void do_oop(narrowOop* p) { do_oop_nv(p); } 81 }; 82 83 class G1VerifyCodeRootOopClosure: public OopClosure { 84 G1CollectedHeap* _g1h; 85 OopClosure* _root_cl; 86 nmethod* _nm; 87 VerifyOption _vo; 88 bool _failures; 89 90 template <class T> void do_oop_work(T* p) { 91 // First verify that this root is live 92 _root_cl->do_oop(p); 93 94 if (!G1VerifyHeapRegionCodeRoots) { 95 // We're not verifying the code roots attached to heap region. 96 return; 97 } 98 99 // Don't check the code roots during marking verification in a full GC 100 if (_vo == VerifyOption_G1UseFullMarking) { 101 return; 102 } 103 104 // Now verify that the current nmethod (which contains p) is 105 // in the code root list of the heap region containing the 106 // object referenced by p. 107 108 T heap_oop = RawAccess<>::oop_load(p); 109 if (!CompressedOops::is_null(heap_oop)) { 110 oop obj = CompressedOops::decode_not_null(heap_oop); 111 112 // Now fetch the region containing the object 113 HeapRegion* hr = _g1h->heap_region_containing(obj); 114 HeapRegionRemSet* hrrs = hr->rem_set(); 115 // Verify that the strong code root list for this region 116 // contains the nmethod 117 if (!hrrs->strong_code_roots_list_contains(_nm)) { 118 log_error(gc, verify)("Code root location " PTR_FORMAT " " 119 "from nmethod " PTR_FORMAT " not in strong " 120 "code roots for region [" PTR_FORMAT "," PTR_FORMAT ")", 121 p2i(p), p2i(_nm), p2i(hr->bottom()), p2i(hr->end())); 122 _failures = true; 123 } 124 } 125 } 126 127 public: 128 G1VerifyCodeRootOopClosure(G1CollectedHeap* g1h, OopClosure* root_cl, VerifyOption vo): 129 _g1h(g1h), _root_cl(root_cl), _vo(vo), _nm(NULL), _failures(false) {} 130 131 void do_oop(oop* p) { do_oop_work(p); } 132 void do_oop(narrowOop* p) { do_oop_work(p); } 133 134 void set_nmethod(nmethod* nm) { _nm = nm; } 135 bool failures() { return _failures; } 136 }; 137 138 class G1VerifyCodeRootBlobClosure: public CodeBlobClosure { 139 G1VerifyCodeRootOopClosure* _oop_cl; 140 141 public: 142 G1VerifyCodeRootBlobClosure(G1VerifyCodeRootOopClosure* oop_cl): 143 _oop_cl(oop_cl) {} 144 145 void do_code_blob(CodeBlob* cb) { 146 nmethod* nm = cb->as_nmethod_or_null(); 147 if (nm != NULL) { 148 _oop_cl->set_nmethod(nm); 149 nm->oops_do(_oop_cl); 150 } 151 } 152 }; 153 154 class YoungRefCounterClosure : public OopClosure { 155 G1CollectedHeap* _g1h; 156 int _count; 157 public: 158 YoungRefCounterClosure(G1CollectedHeap* g1h) : _g1h(g1h), _count(0) {} 159 void do_oop(oop* p) { if (_g1h->is_in_young(*p)) { _count++; } } 160 void do_oop(narrowOop* p) { ShouldNotReachHere(); } 161 162 int count() { return _count; } 163 void reset_count() { _count = 0; }; 164 }; 165 166 class VerifyCLDClosure: public CLDClosure { 167 YoungRefCounterClosure _young_ref_counter_closure; 168 OopClosure *_oop_closure; 169 public: 170 VerifyCLDClosure(G1CollectedHeap* g1h, OopClosure* cl) : _young_ref_counter_closure(g1h), _oop_closure(cl) {} 171 void do_cld(ClassLoaderData* cld) { 172 cld->oops_do(_oop_closure, false); 173 174 _young_ref_counter_closure.reset_count(); 175 cld->oops_do(&_young_ref_counter_closure, false); 176 if (_young_ref_counter_closure.count() > 0) { 177 guarantee(cld->has_modified_oops(), "CLD " PTR_FORMAT ", has young %d refs but is not dirty.", p2i(cld), _young_ref_counter_closure.count()); 178 } 179 } 180 }; 181 182 class VerifyLivenessOopClosure: public OopClosure { 183 G1CollectedHeap* _g1h; 184 VerifyOption _vo; 185 public: 186 VerifyLivenessOopClosure(G1CollectedHeap* g1h, VerifyOption vo): 187 _g1h(g1h), _vo(vo) 188 { } 189 void do_oop(narrowOop *p) { do_oop_work(p); } 190 void do_oop( oop *p) { do_oop_work(p); } 191 192 template <class T> void do_oop_work(T *p) { 193 oop obj = RawAccess<>::oop_load(p); 194 guarantee(obj == NULL || !_g1h->is_obj_dead_cond(obj, _vo), 195 "Dead object referenced by a not dead object"); 196 } 197 }; 198 199 class VerifyObjsInRegionClosure: public ObjectClosure { 200 private: 201 G1CollectedHeap* _g1h; 202 size_t _live_bytes; 203 HeapRegion *_hr; 204 VerifyOption _vo; 205 public: 206 // _vo == UsePrevMarking -> use "prev" marking information, 207 // _vo == UseNextMarking -> use "next" marking information, 208 // _vo == UseFullMarking -> use "next" marking bitmap but no TAMS. 209 VerifyObjsInRegionClosure(HeapRegion *hr, VerifyOption vo) 210 : _live_bytes(0), _hr(hr), _vo(vo) { 211 _g1h = G1CollectedHeap::heap(); 212 } 213 void do_object(oop o) { 214 VerifyLivenessOopClosure isLive(_g1h, _vo); 215 assert(o != NULL, "Huh?"); 216 if (!_g1h->is_obj_dead_cond(o, _vo)) { 217 // If the object is alive according to the full gc mark, 218 // then verify that the marking information agrees. 219 // Note we can't verify the contra-positive of the 220 // above: if the object is dead (according to the mark 221 // word), it may not be marked, or may have been marked 222 // but has since became dead, or may have been allocated 223 // since the last marking. 224 if (_vo == VerifyOption_G1UseFullMarking) { 225 guarantee(!_g1h->is_obj_dead(o), "Full GC marking and concurrent mark mismatch"); 226 } 227 228 o->oop_iterate_no_header(&isLive); 229 if (!_hr->obj_allocated_since_prev_marking(o)) { 230 size_t obj_size = o->size(); // Make sure we don't overflow 231 _live_bytes += (obj_size * HeapWordSize); 232 } 233 } 234 } 235 size_t live_bytes() { return _live_bytes; } 236 }; 237 238 class VerifyArchiveOopClosure: public OopClosure { 239 HeapRegion* _hr; 240 public: 241 VerifyArchiveOopClosure(HeapRegion *hr) 242 : _hr(hr) { } 243 void do_oop(narrowOop *p) { do_oop_work(p); } 244 void do_oop( oop *p) { do_oop_work(p); } 245 246 template <class T> void do_oop_work(T *p) { 247 oop obj = RawAccess<>::oop_load(p); 248 249 if (_hr->is_open_archive()) { 250 guarantee(obj == NULL || G1ArchiveAllocator::is_archive_object(obj), 251 "Archive object at " PTR_FORMAT " references a non-archive object at " PTR_FORMAT, 252 p2i(p), p2i(obj)); 253 } else { 254 assert(_hr->is_closed_archive(), "should be closed archive region"); 255 guarantee(obj == NULL || G1ArchiveAllocator::is_closed_archive_object(obj), 256 "Archive object at " PTR_FORMAT " references a non-archive object at " PTR_FORMAT, 257 p2i(p), p2i(obj)); 258 } 259 } 260 }; 261 262 class VerifyObjectInArchiveRegionClosure: public ObjectClosure { 263 HeapRegion* _hr; 264 public: 265 VerifyObjectInArchiveRegionClosure(HeapRegion *hr, bool verbose) 266 : _hr(hr) { } 267 // Verify that all object pointers are to archive regions. 268 void do_object(oop o) { 269 VerifyArchiveOopClosure checkOop(_hr); 270 assert(o != NULL, "Should not be here for NULL oops"); 271 o->oop_iterate_no_header(&checkOop); 272 } 273 }; 274 275 // Should be only used at CDS dump time 276 class VerifyArchivePointerRegionClosure: public HeapRegionClosure { 277 private: 278 G1CollectedHeap* _g1h; 279 public: 280 VerifyArchivePointerRegionClosure(G1CollectedHeap* g1h) { } 281 virtual bool do_heap_region(HeapRegion* r) { 282 if (r->is_archive()) { 283 VerifyObjectInArchiveRegionClosure verify_oop_pointers(r, false); 284 r->object_iterate(&verify_oop_pointers); 285 } 286 return false; 287 } 288 }; 289 290 void G1HeapVerifier::verify_archive_regions() { 291 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 292 VerifyArchivePointerRegionClosure cl(NULL); 293 g1h->heap_region_iterate(&cl); 294 } 295 296 class VerifyRegionClosure: public HeapRegionClosure { 297 private: 298 bool _par; 299 VerifyOption _vo; 300 bool _failures; 301 public: 302 // _vo == UsePrevMarking -> use "prev" marking information, 303 // _vo == UseNextMarking -> use "next" marking information, 304 // _vo == UseFullMarking -> use "next" marking bitmap but no TAMS 305 VerifyRegionClosure(bool par, VerifyOption vo) 306 : _par(par), 307 _vo(vo), 308 _failures(false) {} 309 310 bool failures() { 311 return _failures; 312 } 313 314 bool do_heap_region(HeapRegion* r) { 315 guarantee(!r->is_young() || r->rem_set()->is_complete(), "Remembered set for Young region %u must be complete, is %s", r->hrm_index(), r->rem_set()->get_state_str()); 316 // Humongous and old regions regions might be of any state, so can't check here. 317 guarantee(!r->is_free() || !r->rem_set()->is_tracked(), "Remembered set for free region %u must be untracked, is %s", r->hrm_index(), r->rem_set()->get_state_str()); 318 // Verify that the continues humongous regions' remembered set state matches the 319 // one from the starts humongous region. 320 if (r->is_continues_humongous()) { 321 if (r->rem_set()->get_state_str() != r->humongous_start_region()->rem_set()->get_state_str()) { 322 log_error(gc, verify)("Remset states differ: Region %u (%s) remset %s with starts region %u (%s) remset %s", 323 r->hrm_index(), 324 r->get_short_type_str(), 325 r->rem_set()->get_state_str(), 326 r->humongous_start_region()->hrm_index(), 327 r->humongous_start_region()->get_short_type_str(), 328 r->humongous_start_region()->rem_set()->get_state_str()); 329 _failures = true; 330 } 331 } 332 // For archive regions, verify there are no heap pointers to 333 // non-pinned regions. For all others, verify liveness info. 334 if (r->is_closed_archive()) { 335 VerifyObjectInArchiveRegionClosure verify_oop_pointers(r, false); 336 r->object_iterate(&verify_oop_pointers); 337 return true; 338 } else if (r->is_open_archive()) { 339 VerifyObjsInRegionClosure verify_open_archive_oop(r, _vo); 340 r->object_iterate(&verify_open_archive_oop); 341 return true; 342 } else if (!r->is_continues_humongous()) { 343 bool failures = false; 344 r->verify(_vo, &failures); 345 if (failures) { 346 _failures = true; 347 } else if (!r->is_starts_humongous()) { 348 VerifyObjsInRegionClosure not_dead_yet_cl(r, _vo); 349 r->object_iterate(¬_dead_yet_cl); 350 if (_vo != VerifyOption_G1UseNextMarking) { 351 if (r->max_live_bytes() < not_dead_yet_cl.live_bytes()) { 352 log_error(gc, verify)("[" PTR_FORMAT "," PTR_FORMAT "] max_live_bytes " SIZE_FORMAT " < calculated " SIZE_FORMAT, 353 p2i(r->bottom()), p2i(r->end()), r->max_live_bytes(), not_dead_yet_cl.live_bytes()); 354 _failures = true; 355 } 356 } else { 357 // When vo == UseNextMarking we cannot currently do a sanity 358 // check on the live bytes as the calculation has not been 359 // finalized yet. 360 } 361 } 362 } 363 return false; // stop the region iteration if we hit a failure 364 } 365 }; 366 367 // This is the task used for parallel verification of the heap regions 368 369 class G1ParVerifyTask: public AbstractGangTask { 370 private: 371 G1CollectedHeap* _g1h; 372 VerifyOption _vo; 373 bool _failures; 374 HeapRegionClaimer _hrclaimer; 375 376 public: 377 // _vo == UsePrevMarking -> use "prev" marking information, 378 // _vo == UseNextMarking -> use "next" marking information, 379 // _vo == UseFullMarking -> use "next" marking bitmap but no TAMS 380 G1ParVerifyTask(G1CollectedHeap* g1h, VerifyOption vo) : 381 AbstractGangTask("Parallel verify task"), 382 _g1h(g1h), 383 _vo(vo), 384 _failures(false), 385 _hrclaimer(g1h->workers()->active_workers()) {} 386 387 bool failures() { 388 return _failures; 389 } 390 391 void work(uint worker_id) { 392 HandleMark hm; 393 VerifyRegionClosure blk(true, _vo); 394 _g1h->heap_region_par_iterate_from_worker_offset(&blk, &_hrclaimer, worker_id); 395 if (blk.failures()) { 396 _failures = true; 397 } 398 } 399 }; 400 401 void G1HeapVerifier::enable_verification_type(G1VerifyType type) { 402 // First enable will clear _enabled_verification_types. 403 if (_enabled_verification_types == G1VerifyAll) { 404 _enabled_verification_types = type; 405 } else { 406 _enabled_verification_types |= type; 407 } 408 } 409 410 bool G1HeapVerifier::should_verify(G1VerifyType type) { 411 return (_enabled_verification_types & type) == type; 412 } 413 414 void G1HeapVerifier::verify(VerifyOption vo) { 415 if (!SafepointSynchronize::is_at_safepoint()) { 416 log_info(gc, verify)("Skipping verification. Not at safepoint."); 417 } 418 419 assert(Thread::current()->is_VM_thread(), 420 "Expected to be executed serially by the VM thread at this point"); 421 422 log_debug(gc, verify)("Roots"); 423 VerifyRootsClosure rootsCl(vo); 424 VerifyCLDClosure cldCl(_g1h, &rootsCl); 425 426 // We apply the relevant closures to all the oops in the 427 // system dictionary, class loader data graph, the string table 428 // and the nmethods in the code cache. 429 G1VerifyCodeRootOopClosure codeRootsCl(_g1h, &rootsCl, vo); 430 G1VerifyCodeRootBlobClosure blobsCl(&codeRootsCl); 431 432 { 433 G1RootProcessor root_processor(_g1h, 1); 434 root_processor.process_all_roots(&rootsCl, 435 &cldCl, 436 &blobsCl); 437 } 438 439 bool failures = rootsCl.failures() || codeRootsCl.failures(); 440 441 if (!_g1h->g1_policy()->collector_state()->in_full_gc()) { 442 // If we're verifying during a full GC then the region sets 443 // will have been torn down at the start of the GC. Therefore 444 // verifying the region sets will fail. So we only verify 445 // the region sets when not in a full GC. 446 log_debug(gc, verify)("HeapRegionSets"); 447 verify_region_sets(); 448 } 449 450 log_debug(gc, verify)("HeapRegions"); 451 if (GCParallelVerificationEnabled && ParallelGCThreads > 1) { 452 453 G1ParVerifyTask task(_g1h, vo); 454 _g1h->workers()->run_task(&task); 455 if (task.failures()) { 456 failures = true; 457 } 458 459 } else { 460 VerifyRegionClosure blk(false, vo); 461 _g1h->heap_region_iterate(&blk); 462 if (blk.failures()) { 463 failures = true; 464 } 465 } 466 467 if (G1StringDedup::is_enabled()) { 468 log_debug(gc, verify)("StrDedup"); 469 G1StringDedup::verify(); 470 } 471 472 if (failures) { 473 log_error(gc, verify)("Heap after failed verification (kind %d):", vo); 474 // It helps to have the per-region information in the output to 475 // help us track down what went wrong. This is why we call 476 // print_extended_on() instead of print_on(). 477 Log(gc, verify) log; 478 ResourceMark rm; 479 LogStream ls(log.error()); 480 _g1h->print_extended_on(&ls); 481 } 482 guarantee(!failures, "there should not have been any failures"); 483 } 484 485 // Heap region set verification 486 487 class VerifyRegionListsClosure : public HeapRegionClosure { 488 private: 489 HeapRegionSet* _old_set; 490 HeapRegionSet* _humongous_set; 491 HeapRegionManager* _hrm; 492 493 public: 494 uint _old_count; 495 uint _humongous_count; 496 uint _free_count; 497 498 VerifyRegionListsClosure(HeapRegionSet* old_set, 499 HeapRegionSet* humongous_set, 500 HeapRegionManager* hrm) : 501 _old_set(old_set), _humongous_set(humongous_set), _hrm(hrm), 502 _old_count(), _humongous_count(), _free_count(){ } 503 504 bool do_heap_region(HeapRegion* hr) { 505 if (hr->is_young()) { 506 // TODO 507 } else if (hr->is_humongous()) { 508 assert(hr->containing_set() == _humongous_set, "Heap region %u is humongous but not in humongous set.", hr->hrm_index()); 509 _humongous_count++; 510 } else if (hr->is_empty()) { 511 assert(_hrm->is_free(hr), "Heap region %u is empty but not on the free list.", hr->hrm_index()); 512 _free_count++; 513 } else if (hr->is_old()) { 514 assert(hr->containing_set() == _old_set, "Heap region %u is old but not in the old set.", hr->hrm_index()); 515 _old_count++; 516 } else { 517 // There are no other valid region types. Check for one invalid 518 // one we can identify: pinned without old or humongous set. 519 assert(!hr->is_pinned(), "Heap region %u is pinned but not old (archive) or humongous.", hr->hrm_index()); 520 ShouldNotReachHere(); 521 } 522 return false; 523 } 524 525 void verify_counts(HeapRegionSet* old_set, HeapRegionSet* humongous_set, HeapRegionManager* free_list) { 526 guarantee(old_set->length() == _old_count, "Old set count mismatch. Expected %u, actual %u.", old_set->length(), _old_count); 527 guarantee(humongous_set->length() == _humongous_count, "Hum set count mismatch. Expected %u, actual %u.", humongous_set->length(), _humongous_count); 528 guarantee(free_list->num_free_regions() == _free_count, "Free list count mismatch. Expected %u, actual %u.", free_list->num_free_regions(), _free_count); 529 } 530 }; 531 532 void G1HeapVerifier::verify_region_sets() { 533 assert_heap_locked_or_at_safepoint(true /* should_be_vm_thread */); 534 535 // First, check the explicit lists. 536 _g1h->_hrm.verify(); 537 538 // Finally, make sure that the region accounting in the lists is 539 // consistent with what we see in the heap. 540 541 VerifyRegionListsClosure cl(&_g1h->_old_set, &_g1h->_humongous_set, &_g1h->_hrm); 542 _g1h->heap_region_iterate(&cl); 543 cl.verify_counts(&_g1h->_old_set, &_g1h->_humongous_set, &_g1h->_hrm); 544 } 545 546 void G1HeapVerifier::prepare_for_verify() { 547 if (SafepointSynchronize::is_at_safepoint() || ! UseTLAB) { 548 _g1h->ensure_parsability(false); 549 } 550 } 551 552 double G1HeapVerifier::verify(G1VerifyType type, VerifyOption vo, const char* msg) { 553 double verify_time_ms = 0.0; 554 555 if (should_verify(type) && _g1h->total_collections() >= VerifyGCStartAt) { 556 double verify_start = os::elapsedTime(); 557 HandleMark hm; // Discard invalid handles created during verification 558 prepare_for_verify(); 559 Universe::verify(vo, msg); 560 verify_time_ms = (os::elapsedTime() - verify_start) * 1000; 561 } 562 563 return verify_time_ms; 564 } 565 566 void G1HeapVerifier::verify_before_gc(G1VerifyType type) { 567 if (VerifyBeforeGC) { 568 double verify_time_ms = verify(type, VerifyOption_G1UsePrevMarking, "Before GC"); 569 _g1h->g1_policy()->phase_times()->record_verify_before_time_ms(verify_time_ms); 570 } 571 } 572 573 void G1HeapVerifier::verify_after_gc(G1VerifyType type) { 574 if (VerifyAfterGC) { 575 double verify_time_ms = verify(type, VerifyOption_G1UsePrevMarking, "After GC"); 576 _g1h->g1_policy()->phase_times()->record_verify_after_time_ms(verify_time_ms); 577 } 578 } 579 580 581 #ifndef PRODUCT 582 class G1VerifyCardTableCleanup: public HeapRegionClosure { 583 G1HeapVerifier* _verifier; 584 public: 585 G1VerifyCardTableCleanup(G1HeapVerifier* verifier) 586 : _verifier(verifier) { } 587 virtual bool do_heap_region(HeapRegion* r) { 588 if (r->is_survivor()) { 589 _verifier->verify_dirty_region(r); 590 } else { 591 _verifier->verify_not_dirty_region(r); 592 } 593 return false; 594 } 595 }; 596 597 void G1HeapVerifier::verify_card_table_cleanup() { 598 if (G1VerifyCTCleanup || VerifyAfterGC) { 599 G1VerifyCardTableCleanup cleanup_verifier(this); 600 _g1h->heap_region_iterate(&cleanup_verifier); 601 } 602 } 603 604 void G1HeapVerifier::verify_not_dirty_region(HeapRegion* hr) { 605 // All of the region should be clean. 606 G1CardTable* ct = _g1h->card_table(); 607 MemRegion mr(hr->bottom(), hr->end()); 608 ct->verify_not_dirty_region(mr); 609 } 610 611 void G1HeapVerifier::verify_dirty_region(HeapRegion* hr) { 612 // We cannot guarantee that [bottom(),end()] is dirty. Threads 613 // dirty allocated blocks as they allocate them. The thread that 614 // retires each region and replaces it with a new one will do a 615 // maximal allocation to fill in [pre_dummy_top(),end()] but will 616 // not dirty that area (one less thing to have to do while holding 617 // a lock). So we can only verify that [bottom(),pre_dummy_top()] 618 // is dirty. 619 G1CardTable* ct = _g1h->card_table(); 620 MemRegion mr(hr->bottom(), hr->pre_dummy_top()); 621 if (hr->is_young()) { 622 ct->verify_g1_young_region(mr); 623 } else { 624 ct->verify_dirty_region(mr); 625 } 626 } 627 628 class G1VerifyDirtyYoungListClosure : public HeapRegionClosure { 629 private: 630 G1HeapVerifier* _verifier; 631 public: 632 G1VerifyDirtyYoungListClosure(G1HeapVerifier* verifier) : HeapRegionClosure(), _verifier(verifier) { } 633 virtual bool do_heap_region(HeapRegion* r) { 634 _verifier->verify_dirty_region(r); 635 return false; 636 } 637 }; 638 639 void G1HeapVerifier::verify_dirty_young_regions() { 640 G1VerifyDirtyYoungListClosure cl(this); 641 _g1h->collection_set()->iterate(&cl); 642 } 643 644 bool G1HeapVerifier::verify_no_bits_over_tams(const char* bitmap_name, const G1CMBitMap* const bitmap, 645 HeapWord* tams, HeapWord* end) { 646 guarantee(tams <= end, 647 "tams: " PTR_FORMAT " end: " PTR_FORMAT, p2i(tams), p2i(end)); 648 HeapWord* result = bitmap->get_next_marked_addr(tams, end); 649 if (result < end) { 650 log_error(gc, verify)("## wrong marked address on %s bitmap: " PTR_FORMAT, bitmap_name, p2i(result)); 651 log_error(gc, verify)("## %s tams: " PTR_FORMAT " end: " PTR_FORMAT, bitmap_name, p2i(tams), p2i(end)); 652 return false; 653 } 654 return true; 655 } 656 657 bool G1HeapVerifier::verify_bitmaps(const char* caller, HeapRegion* hr) { 658 const G1CMBitMap* const prev_bitmap = _g1h->concurrent_mark()->prev_mark_bitmap(); 659 const G1CMBitMap* const next_bitmap = _g1h->concurrent_mark()->next_mark_bitmap(); 660 661 HeapWord* ptams = hr->prev_top_at_mark_start(); 662 HeapWord* ntams = hr->next_top_at_mark_start(); 663 HeapWord* end = hr->end(); 664 665 bool res_p = verify_no_bits_over_tams("prev", prev_bitmap, ptams, end); 666 667 bool res_n = true; 668 // We cannot verify the next bitmap while we are about to clear it. 669 if (!_g1h->collector_state()->clearing_next_bitmap()) { 670 res_n = verify_no_bits_over_tams("next", next_bitmap, ntams, end); 671 } 672 if (!res_p || !res_n) { 673 log_error(gc, verify)("#### Bitmap verification failed for " HR_FORMAT, HR_FORMAT_PARAMS(hr)); 674 log_error(gc, verify)("#### Caller: %s", caller); 675 return false; 676 } 677 return true; 678 } 679 680 void G1HeapVerifier::check_bitmaps(const char* caller, HeapRegion* hr) { 681 if (!G1VerifyBitmaps) { 682 return; 683 } 684 685 guarantee(verify_bitmaps(caller, hr), "bitmap verification"); 686 } 687 688 class G1VerifyBitmapClosure : public HeapRegionClosure { 689 private: 690 const char* _caller; 691 G1HeapVerifier* _verifier; 692 bool _failures; 693 694 public: 695 G1VerifyBitmapClosure(const char* caller, G1HeapVerifier* verifier) : 696 _caller(caller), _verifier(verifier), _failures(false) { } 697 698 bool failures() { return _failures; } 699 700 virtual bool do_heap_region(HeapRegion* hr) { 701 bool result = _verifier->verify_bitmaps(_caller, hr); 702 if (!result) { 703 _failures = true; 704 } 705 return false; 706 } 707 }; 708 709 void G1HeapVerifier::check_bitmaps(const char* caller) { 710 if (!G1VerifyBitmaps) { 711 return; 712 } 713 714 G1VerifyBitmapClosure cl(caller, this); 715 _g1h->heap_region_iterate(&cl); 716 guarantee(!cl.failures(), "bitmap verification"); 717 } 718 719 class G1CheckCSetFastTableClosure : public HeapRegionClosure { 720 private: 721 bool _failures; 722 public: 723 G1CheckCSetFastTableClosure() : HeapRegionClosure(), _failures(false) { } 724 725 virtual bool do_heap_region(HeapRegion* hr) { 726 uint i = hr->hrm_index(); 727 InCSetState cset_state = (InCSetState) G1CollectedHeap::heap()->_in_cset_fast_test.get_by_index(i); 728 if (hr->is_humongous()) { 729 if (hr->in_collection_set()) { 730 log_error(gc, verify)("## humongous region %u in CSet", i); 731 _failures = true; 732 return true; 733 } 734 if (cset_state.is_in_cset()) { 735 log_error(gc, verify)("## inconsistent cset state " CSETSTATE_FORMAT " for humongous region %u", cset_state.value(), i); 736 _failures = true; 737 return true; 738 } 739 if (hr->is_continues_humongous() && cset_state.is_humongous()) { 740 log_error(gc, verify)("## inconsistent cset state " CSETSTATE_FORMAT " for continues humongous region %u", cset_state.value(), i); 741 _failures = true; 742 return true; 743 } 744 } else { 745 if (cset_state.is_humongous()) { 746 log_error(gc, verify)("## inconsistent cset state " CSETSTATE_FORMAT " for non-humongous region %u", cset_state.value(), i); 747 _failures = true; 748 return true; 749 } 750 if (hr->in_collection_set() != cset_state.is_in_cset()) { 751 log_error(gc, verify)("## in CSet %d / cset state " CSETSTATE_FORMAT " inconsistency for region %u", 752 hr->in_collection_set(), cset_state.value(), i); 753 _failures = true; 754 return true; 755 } 756 if (cset_state.is_in_cset()) { 757 if (hr->is_young() != (cset_state.is_young())) { 758 log_error(gc, verify)("## is_young %d / cset state " CSETSTATE_FORMAT " inconsistency for region %u", 759 hr->is_young(), cset_state.value(), i); 760 _failures = true; 761 return true; 762 } 763 if (hr->is_old() != (cset_state.is_old())) { 764 log_error(gc, verify)("## is_old %d / cset state " CSETSTATE_FORMAT " inconsistency for region %u", 765 hr->is_old(), cset_state.value(), i); 766 _failures = true; 767 return true; 768 } 769 } 770 } 771 return false; 772 } 773 774 bool failures() const { return _failures; } 775 }; 776 777 bool G1HeapVerifier::check_cset_fast_test() { 778 G1CheckCSetFastTableClosure cl; 779 _g1h->_hrm.iterate(&cl); 780 return !cl.failures(); 781 } 782 #endif // PRODUCT