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