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/concurrentMarkThread.hpp" 27 #include "gc/g1/g1Allocator.inline.hpp" 28 #include "gc/g1/g1CollectedHeap.hpp" 29 #include "gc/g1/g1CollectedHeap.inline.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 class VerifyRootsClosure: public OopClosure { 47 private: 48 G1CollectedHeap* _g1h; 49 VerifyOption _vo; 50 bool _failures; 51 public: 52 // _vo == UsePrevMarking -> use "prev" marking information, 53 // _vo == UseNextMarking -> use "next" marking information, 54 // _vo == UseFullMarking -> use "next" marking bitmap but no TAMS 55 VerifyRootsClosure(VerifyOption vo) : 56 _g1h(G1CollectedHeap::heap()), 57 _vo(vo), 58 _failures(false) { } 59 60 bool failures() { return _failures; } 61 62 template <class T> void do_oop_nv(T* p) { 63 T heap_oop = RawAccess<>::oop_load(p); 64 if (!CompressedOops::is_null(heap_oop)) { 65 oop obj = CompressedOops::decode_not_null(heap_oop); 66 if (_g1h->is_obj_dead_cond(obj, _vo)) { 67 Log(gc, verify) log; 68 log.error("Root location " PTR_FORMAT " points to dead obj " PTR_FORMAT, p2i(p), p2i(obj)); 69 ResourceMark rm; 70 LogStream ls(log.error()); 71 obj->print_on(&ls); 72 _failures = true; 73 } 74 } 75 } 76 77 void do_oop(oop* p) { do_oop_nv(p); } 78 void do_oop(narrowOop* p) { do_oop_nv(p); } 79 }; 80 81 class G1VerifyCodeRootOopClosure: public OopClosure { 82 G1CollectedHeap* _g1h; 83 OopClosure* _root_cl; 84 nmethod* _nm; 85 VerifyOption _vo; 86 bool _failures; 87 88 template <class T> void do_oop_work(T* p) { 89 // First verify that this root is live 90 _root_cl->do_oop(p); 91 92 if (!G1VerifyHeapRegionCodeRoots) { 93 // We're not verifying the code roots attached to heap region. 94 return; 95 } 96 97 // Don't check the code roots during marking verification in a full GC 98 if (_vo == VerifyOption_G1UseFullMarking) { 99 return; 100 } 101 102 // Now verify that the current nmethod (which contains p) is 103 // in the code root list of the heap region containing the 104 // object referenced by p. 105 106 T heap_oop = RawAccess<>::oop_load(p); 107 if (!CompressedOops::is_null(heap_oop)) { 108 oop obj = CompressedOops::decode_not_null(heap_oop); 109 110 // Now fetch the region containing the object 111 HeapRegion* hr = _g1h->heap_region_containing(obj); 112 HeapRegionRemSet* hrrs = hr->rem_set(); 113 // Verify that the strong code root list for this region 114 // contains the nmethod 115 if (!hrrs->strong_code_roots_list_contains(_nm)) { 116 log_error(gc, verify)("Code root location " PTR_FORMAT " " 117 "from nmethod " PTR_FORMAT " not in strong " 118 "code roots for region [" PTR_FORMAT "," PTR_FORMAT ")", 119 p2i(p), p2i(_nm), p2i(hr->bottom()), p2i(hr->end())); 120 _failures = true; 121 } 122 } 123 } 124 125 public: 126 G1VerifyCodeRootOopClosure(G1CollectedHeap* g1h, OopClosure* root_cl, VerifyOption vo): 127 _g1h(g1h), _root_cl(root_cl), _vo(vo), _nm(NULL), _failures(false) {} 128 129 void do_oop(oop* p) { do_oop_work(p); } 130 void do_oop(narrowOop* p) { do_oop_work(p); } 131 132 void set_nmethod(nmethod* nm) { _nm = nm; } 133 bool failures() { return _failures; } 134 }; 135 136 class G1VerifyCodeRootBlobClosure: public CodeBlobClosure { 137 G1VerifyCodeRootOopClosure* _oop_cl; 138 139 public: 140 G1VerifyCodeRootBlobClosure(G1VerifyCodeRootOopClosure* oop_cl): 141 _oop_cl(oop_cl) {} 142 143 void do_code_blob(CodeBlob* cb) { 144 nmethod* nm = cb->as_nmethod_or_null(); 145 if (nm != NULL) { 146 _oop_cl->set_nmethod(nm); 147 nm->oops_do(_oop_cl); 148 } 149 } 150 }; 151 152 class YoungRefCounterClosure : public OopClosure { 153 G1CollectedHeap* _g1h; 154 int _count; 155 public: 156 YoungRefCounterClosure(G1CollectedHeap* g1h) : _g1h(g1h), _count(0) {} 157 void do_oop(oop* p) { if (_g1h->is_in_young(*p)) { _count++; } } 158 void do_oop(narrowOop* p) { ShouldNotReachHere(); } 159 160 int count() { return _count; } 161 void reset_count() { _count = 0; }; 162 }; 163 164 class VerifyCLDClosure: public CLDClosure { 165 YoungRefCounterClosure _young_ref_counter_closure; 166 OopClosure *_oop_closure; 167 public: 168 VerifyCLDClosure(G1CollectedHeap* g1h, OopClosure* cl) : _young_ref_counter_closure(g1h), _oop_closure(cl) {} 169 void do_cld(ClassLoaderData* cld) { 170 cld->oops_do(_oop_closure, false); 171 172 _young_ref_counter_closure.reset_count(); 173 cld->oops_do(&_young_ref_counter_closure, false); 174 if (_young_ref_counter_closure.count() > 0) { 175 guarantee(cld->has_modified_oops(), "CLD " PTR_FORMAT ", has young %d refs but is not dirty.", p2i(cld), _young_ref_counter_closure.count()); 176 } 177 } 178 }; 179 180 class VerifyLivenessOopClosure: public OopClosure { 181 G1CollectedHeap* _g1h; 182 VerifyOption _vo; 183 public: 184 VerifyLivenessOopClosure(G1CollectedHeap* g1h, VerifyOption vo): 185 _g1h(g1h), _vo(vo) 186 { } 187 void do_oop(narrowOop *p) { do_oop_work(p); } 188 void do_oop( oop *p) { do_oop_work(p); } 189 190 template <class T> void do_oop_work(T *p) { 191 oop obj = RawAccess<>::oop_load(p); 192 guarantee(obj == NULL || !_g1h->is_obj_dead_cond(obj, _vo), 193 "Dead object referenced by a not dead object"); 194 } 195 }; 196 197 class VerifyObjsInRegionClosure: public ObjectClosure { 198 private: 199 G1CollectedHeap* _g1h; 200 size_t _live_bytes; 201 HeapRegion *_hr; 202 VerifyOption _vo; 203 public: 204 // _vo == UsePrevMarking -> use "prev" marking information, 205 // _vo == UseNextMarking -> use "next" marking information, 206 // _vo == UseFullMarking -> use "next" marking bitmap but no TAMS. 207 VerifyObjsInRegionClosure(HeapRegion *hr, VerifyOption vo) 208 : _live_bytes(0), _hr(hr), _vo(vo) { 209 _g1h = G1CollectedHeap::heap(); 210 } 211 void do_object(oop o) { 212 VerifyLivenessOopClosure isLive(_g1h, _vo); 213 assert(o != NULL, "Huh?"); 214 if (!_g1h->is_obj_dead_cond(o, _vo)) { 215 // If the object is alive according to the full gc mark, 216 // then verify that the marking information agrees. 217 // Note we can't verify the contra-positive of the 218 // above: if the object is dead (according to the mark 219 // word), it may not be marked, or may have been marked 220 // but has since became dead, or may have been allocated 221 // since the last marking. 222 if (_vo == VerifyOption_G1UseFullMarking) { 223 guarantee(!_g1h->is_obj_dead(o), "Full GC marking and concurrent mark mismatch"); 224 } 225 226 o->oop_iterate_no_header(&isLive); 227 if (!_hr->obj_allocated_since_prev_marking(o)) { 228 size_t obj_size = o->size(); // Make sure we don't overflow 229 _live_bytes += (obj_size * HeapWordSize); 230 } 231 } 232 } 233 size_t live_bytes() { return _live_bytes; } 234 }; 235 236 class VerifyArchiveOopClosure: public OopClosure { 237 HeapRegion* _hr; 238 public: 239 VerifyArchiveOopClosure(HeapRegion *hr) 240 : _hr(hr) { } 241 void do_oop(narrowOop *p) { do_oop_work(p); } 242 void do_oop( oop *p) { do_oop_work(p); } 243 244 template <class T> void do_oop_work(T *p) { 245 oop obj = RawAccess<>::oop_load(p); 246 247 if (_hr->is_open_archive()) { 248 guarantee(obj == NULL || G1ArchiveAllocator::is_archive_object(obj), 249 "Archive object at " PTR_FORMAT " references a non-archive object at " PTR_FORMAT, 250 p2i(p), p2i(obj)); 251 } else { 252 assert(_hr->is_closed_archive(), "should be closed archive region"); 253 guarantee(obj == NULL || G1ArchiveAllocator::is_closed_archive_object(obj), 254 "Archive object at " PTR_FORMAT " references a non-archive object at " PTR_FORMAT, 255 p2i(p), p2i(obj)); 256 } 257 } 258 }; 259 260 class VerifyObjectInArchiveRegionClosure: public ObjectClosure { 261 HeapRegion* _hr; 262 public: 263 VerifyObjectInArchiveRegionClosure(HeapRegion *hr, bool verbose) 264 : _hr(hr) { } 265 // Verify that all object pointers are to archive regions. 266 void do_object(oop o) { 267 VerifyArchiveOopClosure checkOop(_hr); 268 assert(o != NULL, "Should not be here for NULL oops"); 269 o->oop_iterate_no_header(&checkOop); 270 } 271 }; 272 273 // Should be only used at CDS dump time 274 class VerifyArchivePointerRegionClosure: public HeapRegionClosure { 275 private: 276 G1CollectedHeap* _g1h; 277 public: 278 VerifyArchivePointerRegionClosure(G1CollectedHeap* g1h) { } 279 virtual bool do_heap_region(HeapRegion* r) { 280 if (r->is_archive()) { 281 VerifyObjectInArchiveRegionClosure verify_oop_pointers(r, false); 282 r->object_iterate(&verify_oop_pointers); 283 } 284 return false; 285 } 286 }; 287 288 void G1HeapVerifier::verify_archive_regions() { 289 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 290 VerifyArchivePointerRegionClosure cl(NULL); 291 g1h->heap_region_iterate(&cl); 292 } 293 294 class VerifyRegionClosure: public HeapRegionClosure { 295 private: 296 bool _par; 297 VerifyOption _vo; 298 bool _failures; 299 public: 300 // _vo == UsePrevMarking -> use "prev" marking information, 301 // _vo == UseNextMarking -> use "next" marking information, 302 // _vo == UseFullMarking -> use "next" marking bitmap but no TAMS 303 VerifyRegionClosure(bool par, VerifyOption vo) 304 : _par(par), 305 _vo(vo), 306 _failures(false) {} 307 308 bool failures() { 309 return _failures; 310 } 311 312 bool do_heap_region(HeapRegion* r) { 313 // For archive regions, verify there are no heap pointers to 314 // non-pinned regions. For all others, verify liveness info. 315 if (r->is_closed_archive()) { 316 VerifyObjectInArchiveRegionClosure verify_oop_pointers(r, false); 317 r->object_iterate(&verify_oop_pointers); 318 return true; 319 } else if (r->is_open_archive()) { 320 VerifyObjsInRegionClosure verify_open_archive_oop(r, _vo); 321 r->object_iterate(&verify_open_archive_oop); 322 return true; 323 } else if (!r->is_continues_humongous()) { 324 bool failures = false; 325 r->verify(_vo, &failures); 326 if (failures) { 327 _failures = true; 328 } else if (!r->is_starts_humongous()) { 329 VerifyObjsInRegionClosure not_dead_yet_cl(r, _vo); 330 r->object_iterate(¬_dead_yet_cl); 331 if (_vo != VerifyOption_G1UseNextMarking) { 332 if (r->max_live_bytes() < not_dead_yet_cl.live_bytes()) { 333 log_error(gc, verify)("[" PTR_FORMAT "," PTR_FORMAT "] max_live_bytes " SIZE_FORMAT " < calculated " SIZE_FORMAT, 334 p2i(r->bottom()), p2i(r->end()), r->max_live_bytes(), not_dead_yet_cl.live_bytes()); 335 _failures = true; 336 } 337 } else { 338 // When vo == UseNextMarking we cannot currently do a sanity 339 // check on the live bytes as the calculation has not been 340 // finalized yet. 341 } 342 } 343 } 344 return false; // stop the region iteration if we hit a failure 345 } 346 }; 347 348 // This is the task used for parallel verification of the heap regions 349 350 class G1ParVerifyTask: public AbstractGangTask { 351 private: 352 G1CollectedHeap* _g1h; 353 VerifyOption _vo; 354 bool _failures; 355 HeapRegionClaimer _hrclaimer; 356 357 public: 358 // _vo == UsePrevMarking -> use "prev" marking information, 359 // _vo == UseNextMarking -> use "next" marking information, 360 // _vo == UseFullMarking -> use "next" marking bitmap but no TAMS 361 G1ParVerifyTask(G1CollectedHeap* g1h, VerifyOption vo) : 362 AbstractGangTask("Parallel verify task"), 363 _g1h(g1h), 364 _vo(vo), 365 _failures(false), 366 _hrclaimer(g1h->workers()->active_workers()) {} 367 368 bool failures() { 369 return _failures; 370 } 371 372 void work(uint worker_id) { 373 HandleMark hm; 374 VerifyRegionClosure blk(true, _vo); 375 _g1h->heap_region_par_iterate_from_worker_offset(&blk, &_hrclaimer, worker_id); 376 if (blk.failures()) { 377 _failures = true; 378 } 379 } 380 }; 381 382 void G1HeapVerifier::parse_verification_type(const char* type) { 383 if (strcmp(type, "young-only") == 0) { 384 enable_verification_type(G1VerifyYoungOnly); 385 } else if (strcmp(type, "initial-mark") == 0) { 386 enable_verification_type(G1VerifyInitialMark); 387 } else if (strcmp(type, "mixed") == 0) { 388 enable_verification_type(G1VerifyMixed); 389 } else if (strcmp(type, "remark") == 0) { 390 enable_verification_type(G1VerifyRemark); 391 } else if (strcmp(type, "cleanup") == 0) { 392 enable_verification_type(G1VerifyCleanup); 393 } else if (strcmp(type, "full") == 0) { 394 enable_verification_type(G1VerifyFull); 395 } else { 396 log_warning(gc, verify)("VerifyGCType: '%s' is unknown. Available types are: " 397 "young-only, initial-mark, mixed, remark, cleanup and full", type); 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()->full_collection()) { 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:"); 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 // Given that a concurrent operation might be adding regions to 539 // the secondary free list we have to take the lock before 540 // verifying it. 541 MutexLockerEx x(SecondaryFreeList_lock, Mutex::_no_safepoint_check_flag); 542 _g1h->_secondary_free_list.verify_list(); 543 } 544 545 // If a concurrent region freeing operation is in progress it will 546 // be difficult to correctly attributed any free regions we come 547 // across to the correct free list given that they might belong to 548 // one of several (free_list, secondary_free_list, any local lists, 549 // etc.). So, if that's the case we will skip the rest of the 550 // verification operation. Alternatively, waiting for the concurrent 551 // operation to complete will have a non-trivial effect on the GC's 552 // operation (no concurrent operation will last longer than the 553 // interval between two calls to verification) and it might hide 554 // any issues that we would like to catch during testing. 555 if (_g1h->free_regions_coming()) { 556 return; 557 } 558 559 // Make sure we append the secondary_free_list on the free_list so 560 // that all free regions we will come across can be safely 561 // attributed to the free_list. 562 _g1h->append_secondary_free_list_if_not_empty_with_lock(); 563 564 // Finally, make sure that the region accounting in the lists is 565 // consistent with what we see in the heap. 566 567 VerifyRegionListsClosure cl(&_g1h->_old_set, &_g1h->_humongous_set, &_g1h->_hrm); 568 _g1h->heap_region_iterate(&cl); 569 cl.verify_counts(&_g1h->_old_set, &_g1h->_humongous_set, &_g1h->_hrm); 570 } 571 572 void G1HeapVerifier::prepare_for_verify() { 573 if (SafepointSynchronize::is_at_safepoint() || ! UseTLAB) { 574 _g1h->ensure_parsability(false); 575 } 576 } 577 578 double G1HeapVerifier::verify(G1VerifyType type, VerifyOption vo, const char* msg) { 579 double verify_time_ms = 0.0; 580 581 if (should_verify(type) && _g1h->total_collections() >= VerifyGCStartAt) { 582 double verify_start = os::elapsedTime(); 583 HandleMark hm; // Discard invalid handles created during verification 584 prepare_for_verify(); 585 Universe::verify(vo, msg); 586 verify_time_ms = (os::elapsedTime() - verify_start) * 1000; 587 } 588 589 return verify_time_ms; 590 } 591 592 void G1HeapVerifier::verify_before_gc(G1VerifyType type) { 593 if (VerifyBeforeGC) { 594 double verify_time_ms = verify(type, VerifyOption_G1UsePrevMarking, "Before GC"); 595 _g1h->g1_policy()->phase_times()->record_verify_before_time_ms(verify_time_ms); 596 } 597 } 598 599 void G1HeapVerifier::verify_after_gc(G1VerifyType type) { 600 if (VerifyAfterGC) { 601 double verify_time_ms = verify(type, VerifyOption_G1UsePrevMarking, "After GC"); 602 _g1h->g1_policy()->phase_times()->record_verify_after_time_ms(verify_time_ms); 603 } 604 } 605 606 607 #ifndef PRODUCT 608 class G1VerifyCardTableCleanup: public HeapRegionClosure { 609 G1HeapVerifier* _verifier; 610 public: 611 G1VerifyCardTableCleanup(G1HeapVerifier* verifier) 612 : _verifier(verifier) { } 613 virtual bool do_heap_region(HeapRegion* r) { 614 if (r->is_survivor()) { 615 _verifier->verify_dirty_region(r); 616 } else { 617 _verifier->verify_not_dirty_region(r); 618 } 619 return false; 620 } 621 }; 622 623 void G1HeapVerifier::verify_card_table_cleanup() { 624 if (G1VerifyCTCleanup || VerifyAfterGC) { 625 G1VerifyCardTableCleanup cleanup_verifier(this); 626 _g1h->heap_region_iterate(&cleanup_verifier); 627 } 628 } 629 630 void G1HeapVerifier::verify_not_dirty_region(HeapRegion* hr) { 631 // All of the region should be clean. 632 G1CardTable* ct = _g1h->card_table(); 633 MemRegion mr(hr->bottom(), hr->end()); 634 ct->verify_not_dirty_region(mr); 635 } 636 637 void G1HeapVerifier::verify_dirty_region(HeapRegion* hr) { 638 // We cannot guarantee that [bottom(),end()] is dirty. Threads 639 // dirty allocated blocks as they allocate them. The thread that 640 // retires each region and replaces it with a new one will do a 641 // maximal allocation to fill in [pre_dummy_top(),end()] but will 642 // not dirty that area (one less thing to have to do while holding 643 // a lock). So we can only verify that [bottom(),pre_dummy_top()] 644 // is dirty. 645 G1CardTable* ct = _g1h->card_table(); 646 MemRegion mr(hr->bottom(), hr->pre_dummy_top()); 647 if (hr->is_young()) { 648 ct->verify_g1_young_region(mr); 649 } else { 650 ct->verify_dirty_region(mr); 651 } 652 } 653 654 class G1VerifyDirtyYoungListClosure : public HeapRegionClosure { 655 private: 656 G1HeapVerifier* _verifier; 657 public: 658 G1VerifyDirtyYoungListClosure(G1HeapVerifier* verifier) : HeapRegionClosure(), _verifier(verifier) { } 659 virtual bool do_heap_region(HeapRegion* r) { 660 _verifier->verify_dirty_region(r); 661 return false; 662 } 663 }; 664 665 void G1HeapVerifier::verify_dirty_young_regions() { 666 G1VerifyDirtyYoungListClosure cl(this); 667 _g1h->collection_set()->iterate(&cl); 668 } 669 670 bool G1HeapVerifier::verify_no_bits_over_tams(const char* bitmap_name, const G1CMBitMap* const bitmap, 671 HeapWord* tams, HeapWord* end) { 672 guarantee(tams <= end, 673 "tams: " PTR_FORMAT " end: " PTR_FORMAT, p2i(tams), p2i(end)); 674 HeapWord* result = bitmap->get_next_marked_addr(tams, end); 675 if (result < end) { 676 log_error(gc, verify)("## wrong marked address on %s bitmap: " PTR_FORMAT, bitmap_name, p2i(result)); 677 log_error(gc, verify)("## %s tams: " PTR_FORMAT " end: " PTR_FORMAT, bitmap_name, p2i(tams), p2i(end)); 678 return false; 679 } 680 return true; 681 } 682 683 bool G1HeapVerifier::verify_bitmaps(const char* caller, HeapRegion* hr) { 684 const G1CMBitMap* const prev_bitmap = _g1h->concurrent_mark()->prev_mark_bitmap(); 685 const G1CMBitMap* const next_bitmap = _g1h->concurrent_mark()->next_mark_bitmap(); 686 687 HeapWord* ptams = hr->prev_top_at_mark_start(); 688 HeapWord* ntams = hr->next_top_at_mark_start(); 689 HeapWord* end = hr->end(); 690 691 bool res_p = verify_no_bits_over_tams("prev", prev_bitmap, ptams, end); 692 693 bool res_n = true; 694 // We reset mark_in_progress() before we reset _cmThread->in_progress() and in this window 695 // we do the clearing of the next bitmap concurrently. Thus, we can not verify the bitmap 696 // if we happen to be in that state. 697 if (_g1h->collector_state()->mark_in_progress() || !_g1h->_cmThread->in_progress()) { 698 res_n = verify_no_bits_over_tams("next", next_bitmap, ntams, end); 699 } 700 if (!res_p || !res_n) { 701 log_error(gc, verify)("#### Bitmap verification failed for " HR_FORMAT, HR_FORMAT_PARAMS(hr)); 702 log_error(gc, verify)("#### Caller: %s", caller); 703 return false; 704 } 705 return true; 706 } 707 708 void G1HeapVerifier::check_bitmaps(const char* caller, HeapRegion* hr) { 709 if (!G1VerifyBitmaps) return; 710 711 guarantee(verify_bitmaps(caller, hr), "bitmap verification"); 712 } 713 714 class G1VerifyBitmapClosure : public HeapRegionClosure { 715 private: 716 const char* _caller; 717 G1HeapVerifier* _verifier; 718 bool _failures; 719 720 public: 721 G1VerifyBitmapClosure(const char* caller, G1HeapVerifier* verifier) : 722 _caller(caller), _verifier(verifier), _failures(false) { } 723 724 bool failures() { return _failures; } 725 726 virtual bool do_heap_region(HeapRegion* hr) { 727 bool result = _verifier->verify_bitmaps(_caller, hr); 728 if (!result) { 729 _failures = true; 730 } 731 return false; 732 } 733 }; 734 735 void G1HeapVerifier::check_bitmaps(const char* caller) { 736 if (!G1VerifyBitmaps) return; 737 738 G1VerifyBitmapClosure cl(caller, this); 739 _g1h->heap_region_iterate(&cl); 740 guarantee(!cl.failures(), "bitmap verification"); 741 } 742 743 class G1CheckCSetFastTableClosure : public HeapRegionClosure { 744 private: 745 bool _failures; 746 public: 747 G1CheckCSetFastTableClosure() : HeapRegionClosure(), _failures(false) { } 748 749 virtual bool do_heap_region(HeapRegion* hr) { 750 uint i = hr->hrm_index(); 751 InCSetState cset_state = (InCSetState) G1CollectedHeap::heap()->_in_cset_fast_test.get_by_index(i); 752 if (hr->is_humongous()) { 753 if (hr->in_collection_set()) { 754 log_error(gc, verify)("## humongous region %u in CSet", i); 755 _failures = true; 756 return true; 757 } 758 if (cset_state.is_in_cset()) { 759 log_error(gc, verify)("## inconsistent cset state " CSETSTATE_FORMAT " for humongous region %u", cset_state.value(), i); 760 _failures = true; 761 return true; 762 } 763 if (hr->is_continues_humongous() && cset_state.is_humongous()) { 764 log_error(gc, verify)("## inconsistent cset state " CSETSTATE_FORMAT " for continues humongous region %u", cset_state.value(), i); 765 _failures = true; 766 return true; 767 } 768 } else { 769 if (cset_state.is_humongous()) { 770 log_error(gc, verify)("## inconsistent cset state " CSETSTATE_FORMAT " for non-humongous region %u", cset_state.value(), i); 771 _failures = true; 772 return true; 773 } 774 if (hr->in_collection_set() != cset_state.is_in_cset()) { 775 log_error(gc, verify)("## in CSet %d / cset state " CSETSTATE_FORMAT " inconsistency for region %u", 776 hr->in_collection_set(), cset_state.value(), i); 777 _failures = true; 778 return true; 779 } 780 if (cset_state.is_in_cset()) { 781 if (hr->is_young() != (cset_state.is_young())) { 782 log_error(gc, verify)("## is_young %d / cset state " CSETSTATE_FORMAT " inconsistency for region %u", 783 hr->is_young(), cset_state.value(), i); 784 _failures = true; 785 return true; 786 } 787 if (hr->is_old() != (cset_state.is_old())) { 788 log_error(gc, verify)("## is_old %d / cset state " CSETSTATE_FORMAT " inconsistency for region %u", 789 hr->is_old(), cset_state.value(), i); 790 _failures = true; 791 return true; 792 } 793 } 794 } 795 return false; 796 } 797 798 bool failures() const { return _failures; } 799 }; 800 801 bool G1HeapVerifier::check_cset_fast_test() { 802 G1CheckCSetFastTableClosure cl; 803 _g1h->_hrm.iterate(&cl); 804 return !cl.failures(); 805 } 806 #endif // PRODUCT