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/g1OopClosures.inline.hpp"
30 #include "gc/g1/heapRegion.inline.hpp"
31 #include "gc/g1/heapRegionBounds.inline.hpp"
32 #include "gc/g1/heapRegionManager.inline.hpp"
33 #include "gc/g1/heapRegionRemSet.hpp"
34 #include "gc/shared/genOopClosures.inline.hpp"
35 #include "gc/shared/liveRange.hpp"
36 #include "gc/shared/space.inline.hpp"
37 #include "memory/iterator.hpp"
38 #include "oops/oop.inline.hpp"
39 #include "runtime/atomic.inline.hpp"
40 #include "runtime/orderAccess.inline.hpp"
41
42 int HeapRegion::LogOfHRGrainBytes = 0;
43 int HeapRegion::LogOfHRGrainWords = 0;
44 size_t HeapRegion::GrainBytes = 0;
45 size_t HeapRegion::GrainWords = 0;
46 size_t HeapRegion::CardsPerRegion = 0;
47
48 HeapRegionDCTOC::HeapRegionDCTOC(G1CollectedHeap* g1,
49 HeapRegion* hr,
50 G1ParPushHeapRSClosure* cl,
51 CardTableModRefBS::PrecisionStyle precision) :
52 DirtyCardToOopClosure(hr, cl, precision, NULL),
53 _hr(hr), _rs_scan(cl), _g1(g1) { }
54
55 FilterOutOfRegionClosure::FilterOutOfRegionClosure(HeapRegion* r,
56 OopClosure* oc) :
462 hrrs->strong_code_roots_do(blk);
463 }
464
465 class VerifyStrongCodeRootOopClosure: public OopClosure {
466 const HeapRegion* _hr;
467 nmethod* _nm;
468 bool _failures;
469 bool _has_oops_in_region;
470
471 template <class T> void do_oop_work(T* p) {
472 T heap_oop = oopDesc::load_heap_oop(p);
473 if (!oopDesc::is_null(heap_oop)) {
474 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
475
476 // Note: not all the oops embedded in the nmethod are in the
477 // current region. We only look at those which are.
478 if (_hr->is_in(obj)) {
479 // Object is in the region. Check that its less than top
480 if (_hr->top() <= (HeapWord*)obj) {
481 // Object is above top
482 gclog_or_tty->print_cr("Object " PTR_FORMAT " in region "
483 "[" PTR_FORMAT ", " PTR_FORMAT ") is above "
484 "top " PTR_FORMAT,
485 p2i(obj), p2i(_hr->bottom()), p2i(_hr->end()), p2i(_hr->top()));
486 _failures = true;
487 return;
488 }
489 // Nmethod has at least one oop in the current region
490 _has_oops_in_region = true;
491 }
492 }
493 }
494
495 public:
496 VerifyStrongCodeRootOopClosure(const HeapRegion* hr, nmethod* nm):
497 _hr(hr), _failures(false), _has_oops_in_region(false) {}
498
499 void do_oop(narrowOop* p) { do_oop_work(p); }
500 void do_oop(oop* p) { do_oop_work(p); }
501
502 bool failures() { return _failures; }
503 bool has_oops_in_region() { return _has_oops_in_region; }
504 };
505
506 class VerifyStrongCodeRootCodeBlobClosure: public CodeBlobClosure {
507 const HeapRegion* _hr;
508 bool _failures;
509 public:
510 VerifyStrongCodeRootCodeBlobClosure(const HeapRegion* hr) :
511 _hr(hr), _failures(false) {}
512
513 void do_code_blob(CodeBlob* cb) {
514 nmethod* nm = (cb == NULL) ? NULL : cb->as_nmethod_or_null();
515 if (nm != NULL) {
516 // Verify that the nemthod is live
517 if (!nm->is_alive()) {
518 gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] has dead nmethod "
519 PTR_FORMAT " in its strong code roots",
520 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
521 _failures = true;
522 } else {
523 VerifyStrongCodeRootOopClosure oop_cl(_hr, nm);
524 nm->oops_do(&oop_cl);
525 if (!oop_cl.has_oops_in_region()) {
526 gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] has nmethod "
527 PTR_FORMAT " in its strong code roots "
528 "with no pointers into region",
529 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
530 _failures = true;
531 } else if (oop_cl.failures()) {
532 gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] has other "
533 "failures for nmethod " PTR_FORMAT,
534 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
535 _failures = true;
536 }
537 }
538 }
539 }
540
541 bool failures() { return _failures; }
542 };
543
544 void HeapRegion::verify_strong_code_roots(VerifyOption vo, bool* failures) const {
545 if (!G1VerifyHeapRegionCodeRoots) {
546 // We're not verifying code roots.
547 return;
548 }
549 if (vo == VerifyOption_G1UseMarkWord) {
550 // Marking verification during a full GC is performed after class
551 // unloading, code cache unloading, etc so the strong code roots
552 // attached to each heap region are in an inconsistent state. They won't
553 // be consistent until the strong code roots are rebuilt after the
554 // actual GC. Skip verifying the strong code roots in this particular
555 // time.
556 assert(VerifyDuringGC, "only way to get here");
557 return;
558 }
559
560 HeapRegionRemSet* hrrs = rem_set();
561 size_t strong_code_roots_length = hrrs->strong_code_roots_list_length();
562
563 // if this region is empty then there should be no entries
564 // on its strong code root list
565 if (is_empty()) {
566 if (strong_code_roots_length > 0) {
567 gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] is empty "
568 "but has " SIZE_FORMAT " code root entries",
569 p2i(bottom()), p2i(end()), strong_code_roots_length);
570 *failures = true;
571 }
572 return;
573 }
574
575 if (is_continues_humongous()) {
576 if (strong_code_roots_length > 0) {
577 gclog_or_tty->print_cr("region " HR_FORMAT " is a continuation of a humongous "
578 "region but has " SIZE_FORMAT " code root entries",
579 HR_FORMAT_PARAMS(this), strong_code_roots_length);
580 *failures = true;
581 }
582 return;
583 }
584
585 VerifyStrongCodeRootCodeBlobClosure cb_cl(this);
586 strong_code_roots_do(&cb_cl);
587
588 if (cb_cl.failures()) {
589 *failures = true;
590 }
591 }
592
593 void HeapRegion::print() const { print_on(gclog_or_tty); }
594 void HeapRegion::print_on(outputStream* st) const {
595 st->print("AC%4u", allocation_context());
596
597 st->print(" %2s", get_short_type_str());
598 if (in_collection_set())
599 st->print(" CS");
600 else
601 st->print(" ");
602 st->print(" TS %5d", _gc_time_stamp);
603 st->print(" PTAMS " PTR_FORMAT " NTAMS " PTR_FORMAT,
604 p2i(prev_top_at_mark_start()), p2i(next_top_at_mark_start()));
605 G1OffsetTableContigSpace::print_on(st);
606 }
607
608 class VerifyLiveClosure: public OopClosure {
609 private:
610 G1CollectedHeap* _g1h;
611 CardTableModRefBS* _bs;
612 oop _containing_obj;
613 bool _failures;
631
632 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
633 virtual void do_oop( oop* p) { do_oop_work(p); }
634
635 void print_object(outputStream* out, oop obj) {
636 #ifdef PRODUCT
637 Klass* k = obj->klass();
638 const char* class_name = k->external_name();
639 out->print_cr("class name %s", class_name);
640 #else // PRODUCT
641 obj->print_on(out);
642 #endif // PRODUCT
643 }
644
645 template <class T>
646 void do_oop_work(T* p) {
647 assert(_containing_obj != NULL, "Precondition");
648 assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo),
649 "Precondition");
650 T heap_oop = oopDesc::load_heap_oop(p);
651 if (!oopDesc::is_null(heap_oop)) {
652 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
653 bool failed = false;
654 if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead_cond(obj, _vo)) {
655 MutexLockerEx x(ParGCRareEvent_lock,
656 Mutex::_no_safepoint_check_flag);
657
658 if (!_failures) {
659 gclog_or_tty->cr();
660 gclog_or_tty->print_cr("----------");
661 }
662 if (!_g1h->is_in_closed_subset(obj)) {
663 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
664 gclog_or_tty->print_cr("Field " PTR_FORMAT
665 " of live obj " PTR_FORMAT " in region "
666 "[" PTR_FORMAT ", " PTR_FORMAT ")",
667 p2i(p), p2i(_containing_obj),
668 p2i(from->bottom()), p2i(from->end()));
669 print_object(gclog_or_tty, _containing_obj);
670 gclog_or_tty->print_cr("points to obj " PTR_FORMAT " not in the heap",
671 p2i(obj));
672 } else {
673 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
674 HeapRegion* to = _g1h->heap_region_containing((HeapWord*)obj);
675 gclog_or_tty->print_cr("Field " PTR_FORMAT
676 " of live obj " PTR_FORMAT " in region "
677 "[" PTR_FORMAT ", " PTR_FORMAT ")",
678 p2i(p), p2i(_containing_obj),
679 p2i(from->bottom()), p2i(from->end()));
680 print_object(gclog_or_tty, _containing_obj);
681 gclog_or_tty->print_cr("points to dead obj " PTR_FORMAT " in region "
682 "[" PTR_FORMAT ", " PTR_FORMAT ")",
683 p2i(obj), p2i(to->bottom()), p2i(to->end()));
684 print_object(gclog_or_tty, obj);
685 }
686 gclog_or_tty->print_cr("----------");
687 gclog_or_tty->flush();
688 _failures = true;
689 failed = true;
690 _n_failures++;
691 }
692
693 if (!_g1h->collector_state()->full_collection() || G1VerifyRSetsDuringFullGC) {
694 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
695 HeapRegion* to = _g1h->heap_region_containing(obj);
696 if (from != NULL && to != NULL &&
697 from != to &&
698 !to->is_pinned()) {
699 jbyte cv_obj = *_bs->byte_for_const(_containing_obj);
700 jbyte cv_field = *_bs->byte_for_const(p);
701 const jbyte dirty = CardTableModRefBS::dirty_card_val();
702
703 bool is_bad = !(from->is_young()
704 || to->rem_set()->contains_reference(p)
705 || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed
706 (_containing_obj->is_objArray() ?
707 cv_field == dirty
708 : cv_obj == dirty || cv_field == dirty));
709 if (is_bad) {
710 MutexLockerEx x(ParGCRareEvent_lock,
711 Mutex::_no_safepoint_check_flag);
712
713 if (!_failures) {
714 gclog_or_tty->cr();
715 gclog_or_tty->print_cr("----------");
716 }
717 gclog_or_tty->print_cr("Missing rem set entry:");
718 gclog_or_tty->print_cr("Field " PTR_FORMAT " "
719 "of obj " PTR_FORMAT ", "
720 "in region " HR_FORMAT,
721 p2i(p), p2i(_containing_obj),
722 HR_FORMAT_PARAMS(from));
723 _containing_obj->print_on(gclog_or_tty);
724 gclog_or_tty->print_cr("points to obj " PTR_FORMAT " "
725 "in region " HR_FORMAT,
726 p2i(obj),
727 HR_FORMAT_PARAMS(to));
728 obj->print_on(gclog_or_tty);
729 gclog_or_tty->print_cr("Obj head CTE = %d, field CTE = %d.",
730 cv_obj, cv_field);
731 gclog_or_tty->print_cr("----------");
732 gclog_or_tty->flush();
733 _failures = true;
734 if (!failed) _n_failures++;
735 }
736 }
737 }
738 }
739 }
740 };
741
742 // This really ought to be commoned up into OffsetTableContigSpace somehow.
743 // We would need a mechanism to make that code skip dead objects.
744
745 void HeapRegion::verify(VerifyOption vo,
746 bool* failures) const {
747 G1CollectedHeap* g1 = G1CollectedHeap::heap();
748 *failures = false;
749 HeapWord* p = bottom();
750 HeapWord* prev_p = NULL;
751 VerifyLiveClosure vl_cl(g1, vo);
752 bool is_region_humongous = is_humongous();
753 size_t object_num = 0;
754 while (p < top()) {
755 oop obj = oop(p);
756 size_t obj_size = block_size(p);
757 object_num += 1;
758
759 if (is_region_humongous != g1->is_humongous(obj_size) &&
760 !g1->is_obj_dead(obj, this)) { // Dead objects may have bigger block_size since they span several objects.
761 gclog_or_tty->print_cr("obj " PTR_FORMAT " is of %shumongous size ("
762 SIZE_FORMAT " words) in a %shumongous region",
763 p2i(p), g1->is_humongous(obj_size) ? "" : "non-",
764 obj_size, is_region_humongous ? "" : "non-");
765 *failures = true;
766 return;
767 }
768
769 if (!g1->is_obj_dead_cond(obj, this, vo)) {
770 if (obj->is_oop()) {
771 Klass* klass = obj->klass();
772 bool is_metaspace_object = Metaspace::contains(klass) ||
773 (vo == VerifyOption_G1UsePrevMarking &&
774 ClassLoaderDataGraph::unload_list_contains(klass));
775 if (!is_metaspace_object) {
776 gclog_or_tty->print_cr("klass " PTR_FORMAT " of object " PTR_FORMAT " "
777 "not metadata", p2i(klass), p2i(obj));
778 *failures = true;
779 return;
780 } else if (!klass->is_klass()) {
781 gclog_or_tty->print_cr("klass " PTR_FORMAT " of object " PTR_FORMAT " "
782 "not a klass", p2i(klass), p2i(obj));
783 *failures = true;
784 return;
785 } else {
786 vl_cl.set_containing_obj(obj);
787 obj->oop_iterate_no_header(&vl_cl);
788 if (vl_cl.failures()) {
789 *failures = true;
790 }
791 if (G1MaxVerifyFailures >= 0 &&
792 vl_cl.n_failures() >= G1MaxVerifyFailures) {
793 return;
794 }
795 }
796 } else {
797 gclog_or_tty->print_cr(PTR_FORMAT " no an oop", p2i(obj));
798 *failures = true;
799 return;
800 }
801 }
802 prev_p = p;
803 p += obj_size;
804 }
805
806 if (!is_young() && !is_empty()) {
807 _offsets.verify();
808 }
809
810 if (is_region_humongous) {
811 oop obj = oop(this->humongous_start_region()->bottom());
812 if ((HeapWord*)obj > bottom() || (HeapWord*)obj + obj->size() < bottom()) {
813 gclog_or_tty->print_cr("this humongous region is not part of its' humongous object " PTR_FORMAT, p2i(obj));
814 }
815 }
816
817 if (!is_region_humongous && p != top()) {
818 gclog_or_tty->print_cr("end of last object " PTR_FORMAT " "
819 "does not match top " PTR_FORMAT, p2i(p), p2i(top()));
820 *failures = true;
821 return;
822 }
823
824 HeapWord* the_end = end();
825 // Do some extra BOT consistency checking for addresses in the
826 // range [top, end). BOT look-ups in this range should yield
827 // top. No point in doing that if top == end (there's nothing there).
828 if (p < the_end) {
829 // Look up top
830 HeapWord* addr_1 = p;
831 HeapWord* b_start_1 = _offsets.block_start_const(addr_1);
832 if (b_start_1 != p) {
833 gclog_or_tty->print_cr("BOT look up for top: " PTR_FORMAT " "
834 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
835 p2i(addr_1), p2i(b_start_1), p2i(p));
836 *failures = true;
837 return;
838 }
839
840 // Look up top + 1
841 HeapWord* addr_2 = p + 1;
842 if (addr_2 < the_end) {
843 HeapWord* b_start_2 = _offsets.block_start_const(addr_2);
844 if (b_start_2 != p) {
845 gclog_or_tty->print_cr("BOT look up for top + 1: " PTR_FORMAT " "
846 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
847 p2i(addr_2), p2i(b_start_2), p2i(p));
848 *failures = true;
849 return;
850 }
851 }
852
853 // Look up an address between top and end
854 size_t diff = pointer_delta(the_end, p) / 2;
855 HeapWord* addr_3 = p + diff;
856 if (addr_3 < the_end) {
857 HeapWord* b_start_3 = _offsets.block_start_const(addr_3);
858 if (b_start_3 != p) {
859 gclog_or_tty->print_cr("BOT look up for top + diff: " PTR_FORMAT " "
860 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
861 p2i(addr_3), p2i(b_start_3), p2i(p));
862 *failures = true;
863 return;
864 }
865 }
866
867 // Look up end - 1
868 HeapWord* addr_4 = the_end - 1;
869 HeapWord* b_start_4 = _offsets.block_start_const(addr_4);
870 if (b_start_4 != p) {
871 gclog_or_tty->print_cr("BOT look up for end - 1: " PTR_FORMAT " "
872 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
873 p2i(addr_4), p2i(b_start_4), p2i(p));
874 *failures = true;
875 return;
876 }
877 }
878
879 if (is_region_humongous && object_num > 1) {
880 gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] is humongous "
881 "but has " SIZE_FORMAT ", objects",
882 p2i(bottom()), p2i(end()), object_num);
883 *failures = true;
884 return;
885 }
886
887 verify_strong_code_roots(vo, failures);
888 }
889
890 void HeapRegion::verify() const {
891 bool dummy = false;
892 verify(VerifyOption_G1UsePrevMarking, /* failures */ &dummy);
893 }
894
895 void HeapRegion::prepare_for_compaction(CompactPoint* cp) {
896 scan_and_forward(this, cp);
897 }
898
899 // G1OffsetTableContigSpace code; copied from space.cpp. Hope this can go
900 // away eventually.
912 }
913
914 void G1OffsetTableContigSpace::set_end(HeapWord* new_end) {
915 assert(new_end == _bottom + HeapRegion::GrainWords, "set_end should only ever be set to _bottom + HeapRegion::GrainWords");
916 Space::set_end(new_end);
917 _offsets.resize(new_end - bottom());
918 }
919
920 #ifndef PRODUCT
921 void G1OffsetTableContigSpace::mangle_unused_area() {
922 mangle_unused_area_complete();
923 }
924
925 void G1OffsetTableContigSpace::mangle_unused_area_complete() {
926 SpaceMangler::mangle_region(MemRegion(top(), end()));
927 }
928 #endif
929
930 void G1OffsetTableContigSpace::print() const {
931 print_short();
932 gclog_or_tty->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", "
933 INTPTR_FORMAT ", " INTPTR_FORMAT ")",
934 p2i(bottom()), p2i(top()), p2i(_offsets.threshold()), p2i(end()));
935 }
936
937 HeapWord* G1OffsetTableContigSpace::initialize_threshold() {
938 return _offsets.initialize_threshold();
939 }
940
941 HeapWord* G1OffsetTableContigSpace::cross_threshold(HeapWord* start,
942 HeapWord* end) {
943 _offsets.alloc_block(start, end);
944 return _offsets.threshold();
945 }
946
947 HeapWord* G1OffsetTableContigSpace::scan_top() const {
948 G1CollectedHeap* g1h = G1CollectedHeap::heap();
949 HeapWord* local_top = top();
950 OrderAccess::loadload();
951 const unsigned local_time_stamp = _gc_time_stamp;
952 assert(local_time_stamp <= g1h->get_gc_time_stamp(), "invariant");
|
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/g1OopClosures.inline.hpp"
30 #include "gc/g1/heapRegion.inline.hpp"
31 #include "gc/g1/heapRegionBounds.inline.hpp"
32 #include "gc/g1/heapRegionManager.inline.hpp"
33 #include "gc/g1/heapRegionRemSet.hpp"
34 #include "gc/shared/genOopClosures.inline.hpp"
35 #include "gc/shared/liveRange.hpp"
36 #include "gc/shared/space.inline.hpp"
37 #include "logging/log.hpp"
38 #include "memory/iterator.hpp"
39 #include "oops/oop.inline.hpp"
40 #include "runtime/atomic.inline.hpp"
41 #include "runtime/orderAccess.inline.hpp"
42
43 int HeapRegion::LogOfHRGrainBytes = 0;
44 int HeapRegion::LogOfHRGrainWords = 0;
45 size_t HeapRegion::GrainBytes = 0;
46 size_t HeapRegion::GrainWords = 0;
47 size_t HeapRegion::CardsPerRegion = 0;
48
49 HeapRegionDCTOC::HeapRegionDCTOC(G1CollectedHeap* g1,
50 HeapRegion* hr,
51 G1ParPushHeapRSClosure* cl,
52 CardTableModRefBS::PrecisionStyle precision) :
53 DirtyCardToOopClosure(hr, cl, precision, NULL),
54 _hr(hr), _rs_scan(cl), _g1(g1) { }
55
56 FilterOutOfRegionClosure::FilterOutOfRegionClosure(HeapRegion* r,
57 OopClosure* oc) :
463 hrrs->strong_code_roots_do(blk);
464 }
465
466 class VerifyStrongCodeRootOopClosure: public OopClosure {
467 const HeapRegion* _hr;
468 nmethod* _nm;
469 bool _failures;
470 bool _has_oops_in_region;
471
472 template <class T> void do_oop_work(T* p) {
473 T heap_oop = oopDesc::load_heap_oop(p);
474 if (!oopDesc::is_null(heap_oop)) {
475 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
476
477 // Note: not all the oops embedded in the nmethod are in the
478 // current region. We only look at those which are.
479 if (_hr->is_in(obj)) {
480 // Object is in the region. Check that its less than top
481 if (_hr->top() <= (HeapWord*)obj) {
482 // Object is above top
483 log_info(gc, verify)("Object " PTR_FORMAT " in region [" PTR_FORMAT ", " PTR_FORMAT ") is above top " PTR_FORMAT,
484 p2i(obj), p2i(_hr->bottom()), p2i(_hr->end()), p2i(_hr->top()));
485 _failures = true;
486 return;
487 }
488 // Nmethod has at least one oop in the current region
489 _has_oops_in_region = true;
490 }
491 }
492 }
493
494 public:
495 VerifyStrongCodeRootOopClosure(const HeapRegion* hr, nmethod* nm):
496 _hr(hr), _failures(false), _has_oops_in_region(false) {}
497
498 void do_oop(narrowOop* p) { do_oop_work(p); }
499 void do_oop(oop* p) { do_oop_work(p); }
500
501 bool failures() { return _failures; }
502 bool has_oops_in_region() { return _has_oops_in_region; }
503 };
504
505 class VerifyStrongCodeRootCodeBlobClosure: public CodeBlobClosure {
506 const HeapRegion* _hr;
507 bool _failures;
508 public:
509 VerifyStrongCodeRootCodeBlobClosure(const HeapRegion* hr) :
510 _hr(hr), _failures(false) {}
511
512 void do_code_blob(CodeBlob* cb) {
513 nmethod* nm = (cb == NULL) ? NULL : cb->as_nmethod_or_null();
514 if (nm != NULL) {
515 // Verify that the nemthod is live
516 if (!nm->is_alive()) {
517 log_info(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] has dead nmethod " PTR_FORMAT " in its strong code roots",
518 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
519 _failures = true;
520 } else {
521 VerifyStrongCodeRootOopClosure oop_cl(_hr, nm);
522 nm->oops_do(&oop_cl);
523 if (!oop_cl.has_oops_in_region()) {
524 log_info(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] has nmethod " PTR_FORMAT " in its strong code roots with no pointers into region",
525 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
526 _failures = true;
527 } else if (oop_cl.failures()) {
528 log_info(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] has other failures for nmethod " PTR_FORMAT,
529 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
530 _failures = true;
531 }
532 }
533 }
534 }
535
536 bool failures() { return _failures; }
537 };
538
539 void HeapRegion::verify_strong_code_roots(VerifyOption vo, bool* failures) const {
540 if (!G1VerifyHeapRegionCodeRoots) {
541 // We're not verifying code roots.
542 return;
543 }
544 if (vo == VerifyOption_G1UseMarkWord) {
545 // Marking verification during a full GC is performed after class
546 // unloading, code cache unloading, etc so the strong code roots
547 // attached to each heap region are in an inconsistent state. They won't
548 // be consistent until the strong code roots are rebuilt after the
549 // actual GC. Skip verifying the strong code roots in this particular
550 // time.
551 assert(VerifyDuringGC, "only way to get here");
552 return;
553 }
554
555 HeapRegionRemSet* hrrs = rem_set();
556 size_t strong_code_roots_length = hrrs->strong_code_roots_list_length();
557
558 // if this region is empty then there should be no entries
559 // on its strong code root list
560 if (is_empty()) {
561 if (strong_code_roots_length > 0) {
562 log_info(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] is empty but has " SIZE_FORMAT " code root entries",
563 p2i(bottom()), p2i(end()), strong_code_roots_length);
564 *failures = true;
565 }
566 return;
567 }
568
569 if (is_continues_humongous()) {
570 if (strong_code_roots_length > 0) {
571 log_info(gc, verify)("region " HR_FORMAT " is a continuation of a humongous region but has " SIZE_FORMAT " code root entries",
572 HR_FORMAT_PARAMS(this), strong_code_roots_length);
573 *failures = true;
574 }
575 return;
576 }
577
578 VerifyStrongCodeRootCodeBlobClosure cb_cl(this);
579 strong_code_roots_do(&cb_cl);
580
581 if (cb_cl.failures()) {
582 *failures = true;
583 }
584 }
585
586 void HeapRegion::print() const { print_on(tty); }
587 void HeapRegion::print_on(outputStream* st) const {
588 st->print("AC%4u", allocation_context());
589
590 st->print(" %2s", get_short_type_str());
591 if (in_collection_set())
592 st->print(" CS");
593 else
594 st->print(" ");
595 st->print(" TS %5d", _gc_time_stamp);
596 st->print(" PTAMS " PTR_FORMAT " NTAMS " PTR_FORMAT,
597 p2i(prev_top_at_mark_start()), p2i(next_top_at_mark_start()));
598 G1OffsetTableContigSpace::print_on(st);
599 }
600
601 class VerifyLiveClosure: public OopClosure {
602 private:
603 G1CollectedHeap* _g1h;
604 CardTableModRefBS* _bs;
605 oop _containing_obj;
606 bool _failures;
624
625 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
626 virtual void do_oop( oop* p) { do_oop_work(p); }
627
628 void print_object(outputStream* out, oop obj) {
629 #ifdef PRODUCT
630 Klass* k = obj->klass();
631 const char* class_name = k->external_name();
632 out->print_cr("class name %s", class_name);
633 #else // PRODUCT
634 obj->print_on(out);
635 #endif // PRODUCT
636 }
637
638 template <class T>
639 void do_oop_work(T* p) {
640 assert(_containing_obj != NULL, "Precondition");
641 assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo),
642 "Precondition");
643 T heap_oop = oopDesc::load_heap_oop(p);
644 LogHandle(gc, verify) log;
645 if (!oopDesc::is_null(heap_oop)) {
646 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
647 bool failed = false;
648 if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead_cond(obj, _vo)) {
649 MutexLockerEx x(ParGCRareEvent_lock,
650 Mutex::_no_safepoint_check_flag);
651
652 if (!_failures) {
653 log.info("----------");
654 }
655 ResourceMark rm;
656 if (!_g1h->is_in_closed_subset(obj)) {
657 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
658 log.info("Field " PTR_FORMAT " of live obj " PTR_FORMAT " in region [" PTR_FORMAT ", " PTR_FORMAT ")",
659 p2i(p), p2i(_containing_obj), p2i(from->bottom()), p2i(from->end()));
660 print_object(log.info_stream(), _containing_obj);
661 log.info("points to obj " PTR_FORMAT " not in the heap", p2i(obj));
662 } else {
663 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
664 HeapRegion* to = _g1h->heap_region_containing((HeapWord*)obj);
665 log.info("Field " PTR_FORMAT " of live obj " PTR_FORMAT " in region [" PTR_FORMAT ", " PTR_FORMAT ")",
666 p2i(p), p2i(_containing_obj), p2i(from->bottom()), p2i(from->end()));
667 print_object(log.info_stream(), _containing_obj);
668 log.info("points to dead obj " PTR_FORMAT " in region [" PTR_FORMAT ", " PTR_FORMAT ")",
669 p2i(obj), p2i(to->bottom()), p2i(to->end()));
670 print_object(log.info_stream(), obj);
671 }
672 log.info("----------");
673 _failures = true;
674 failed = true;
675 _n_failures++;
676 }
677
678 if (!_g1h->collector_state()->full_collection() || G1VerifyRSetsDuringFullGC) {
679 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
680 HeapRegion* to = _g1h->heap_region_containing(obj);
681 if (from != NULL && to != NULL &&
682 from != to &&
683 !to->is_pinned()) {
684 jbyte cv_obj = *_bs->byte_for_const(_containing_obj);
685 jbyte cv_field = *_bs->byte_for_const(p);
686 const jbyte dirty = CardTableModRefBS::dirty_card_val();
687
688 bool is_bad = !(from->is_young()
689 || to->rem_set()->contains_reference(p)
690 || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed
691 (_containing_obj->is_objArray() ?
692 cv_field == dirty
693 : cv_obj == dirty || cv_field == dirty));
694 if (is_bad) {
695 MutexLockerEx x(ParGCRareEvent_lock,
696 Mutex::_no_safepoint_check_flag);
697
698 if (!_failures) {
699 log.info("----------");
700 }
701 log.info("Missing rem set entry:");
702 log.info("Field " PTR_FORMAT " of obj " PTR_FORMAT ", in region " HR_FORMAT,
703 p2i(p), p2i(_containing_obj), HR_FORMAT_PARAMS(from));
704 ResourceMark rm;
705 _containing_obj->print_on(log.info_stream());
706 log.info("points to obj " PTR_FORMAT " in region " HR_FORMAT, p2i(obj), HR_FORMAT_PARAMS(to));
707 obj->print_on(log.info_stream());
708 log.info("Obj head CTE = %d, field CTE = %d.", cv_obj, cv_field);
709 log.info("----------");
710 _failures = true;
711 if (!failed) _n_failures++;
712 }
713 }
714 }
715 }
716 }
717 };
718
719 // This really ought to be commoned up into OffsetTableContigSpace somehow.
720 // We would need a mechanism to make that code skip dead objects.
721
722 void HeapRegion::verify(VerifyOption vo,
723 bool* failures) const {
724 G1CollectedHeap* g1 = G1CollectedHeap::heap();
725 *failures = false;
726 HeapWord* p = bottom();
727 HeapWord* prev_p = NULL;
728 VerifyLiveClosure vl_cl(g1, vo);
729 bool is_region_humongous = is_humongous();
730 size_t object_num = 0;
731 while (p < top()) {
732 oop obj = oop(p);
733 size_t obj_size = block_size(p);
734 object_num += 1;
735
736 if (is_region_humongous != g1->is_humongous(obj_size) &&
737 !g1->is_obj_dead(obj, this)) { // Dead objects may have bigger block_size since they span several objects.
738 log_info(gc, verify)("obj " PTR_FORMAT " is of %shumongous size ("
739 SIZE_FORMAT " words) in a %shumongous region",
740 p2i(p), g1->is_humongous(obj_size) ? "" : "non-",
741 obj_size, is_region_humongous ? "" : "non-");
742 *failures = true;
743 return;
744 }
745
746 if (!g1->is_obj_dead_cond(obj, this, vo)) {
747 if (obj->is_oop()) {
748 Klass* klass = obj->klass();
749 bool is_metaspace_object = Metaspace::contains(klass) ||
750 (vo == VerifyOption_G1UsePrevMarking &&
751 ClassLoaderDataGraph::unload_list_contains(klass));
752 if (!is_metaspace_object) {
753 log_info(gc, verify)("klass " PTR_FORMAT " of object " PTR_FORMAT " "
754 "not metadata", p2i(klass), p2i(obj));
755 *failures = true;
756 return;
757 } else if (!klass->is_klass()) {
758 log_info(gc, verify)("klass " PTR_FORMAT " of object " PTR_FORMAT " "
759 "not a klass", p2i(klass), p2i(obj));
760 *failures = true;
761 return;
762 } else {
763 vl_cl.set_containing_obj(obj);
764 obj->oop_iterate_no_header(&vl_cl);
765 if (vl_cl.failures()) {
766 *failures = true;
767 }
768 if (G1MaxVerifyFailures >= 0 &&
769 vl_cl.n_failures() >= G1MaxVerifyFailures) {
770 return;
771 }
772 }
773 } else {
774 log_info(gc, verify)(PTR_FORMAT " no an oop", p2i(obj));
775 *failures = true;
776 return;
777 }
778 }
779 prev_p = p;
780 p += obj_size;
781 }
782
783 if (!is_young() && !is_empty()) {
784 _offsets.verify();
785 }
786
787 if (is_region_humongous) {
788 oop obj = oop(this->humongous_start_region()->bottom());
789 if ((HeapWord*)obj > bottom() || (HeapWord*)obj + obj->size() < bottom()) {
790 log_info(gc, verify)("this humongous region is not part of its' humongous object " PTR_FORMAT, p2i(obj));
791 }
792 }
793
794 if (!is_region_humongous && p != top()) {
795 log_info(gc, verify)("end of last object " PTR_FORMAT " "
796 "does not match top " PTR_FORMAT, p2i(p), p2i(top()));
797 *failures = true;
798 return;
799 }
800
801 HeapWord* the_end = end();
802 // Do some extra BOT consistency checking for addresses in the
803 // range [top, end). BOT look-ups in this range should yield
804 // top. No point in doing that if top == end (there's nothing there).
805 if (p < the_end) {
806 // Look up top
807 HeapWord* addr_1 = p;
808 HeapWord* b_start_1 = _offsets.block_start_const(addr_1);
809 if (b_start_1 != p) {
810 log_info(gc, verify)("BOT look up for top: " PTR_FORMAT " "
811 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
812 p2i(addr_1), p2i(b_start_1), p2i(p));
813 *failures = true;
814 return;
815 }
816
817 // Look up top + 1
818 HeapWord* addr_2 = p + 1;
819 if (addr_2 < the_end) {
820 HeapWord* b_start_2 = _offsets.block_start_const(addr_2);
821 if (b_start_2 != p) {
822 log_info(gc, verify)("BOT look up for top + 1: " PTR_FORMAT " "
823 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
824 p2i(addr_2), p2i(b_start_2), p2i(p));
825 *failures = true;
826 return;
827 }
828 }
829
830 // Look up an address between top and end
831 size_t diff = pointer_delta(the_end, p) / 2;
832 HeapWord* addr_3 = p + diff;
833 if (addr_3 < the_end) {
834 HeapWord* b_start_3 = _offsets.block_start_const(addr_3);
835 if (b_start_3 != p) {
836 log_info(gc, verify)("BOT look up for top + diff: " PTR_FORMAT " "
837 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
838 p2i(addr_3), p2i(b_start_3), p2i(p));
839 *failures = true;
840 return;
841 }
842 }
843
844 // Look up end - 1
845 HeapWord* addr_4 = the_end - 1;
846 HeapWord* b_start_4 = _offsets.block_start_const(addr_4);
847 if (b_start_4 != p) {
848 log_info(gc, verify)("BOT look up for end - 1: " PTR_FORMAT " "
849 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
850 p2i(addr_4), p2i(b_start_4), p2i(p));
851 *failures = true;
852 return;
853 }
854 }
855
856 if (is_region_humongous && object_num > 1) {
857 log_info(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] is humongous "
858 "but has " SIZE_FORMAT ", objects",
859 p2i(bottom()), p2i(end()), object_num);
860 *failures = true;
861 return;
862 }
863
864 verify_strong_code_roots(vo, failures);
865 }
866
867 void HeapRegion::verify() const {
868 bool dummy = false;
869 verify(VerifyOption_G1UsePrevMarking, /* failures */ &dummy);
870 }
871
872 void HeapRegion::prepare_for_compaction(CompactPoint* cp) {
873 scan_and_forward(this, cp);
874 }
875
876 // G1OffsetTableContigSpace code; copied from space.cpp. Hope this can go
877 // away eventually.
889 }
890
891 void G1OffsetTableContigSpace::set_end(HeapWord* new_end) {
892 assert(new_end == _bottom + HeapRegion::GrainWords, "set_end should only ever be set to _bottom + HeapRegion::GrainWords");
893 Space::set_end(new_end);
894 _offsets.resize(new_end - bottom());
895 }
896
897 #ifndef PRODUCT
898 void G1OffsetTableContigSpace::mangle_unused_area() {
899 mangle_unused_area_complete();
900 }
901
902 void G1OffsetTableContigSpace::mangle_unused_area_complete() {
903 SpaceMangler::mangle_region(MemRegion(top(), end()));
904 }
905 #endif
906
907 void G1OffsetTableContigSpace::print() const {
908 print_short();
909 tty->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", "
910 INTPTR_FORMAT ", " INTPTR_FORMAT ")",
911 p2i(bottom()), p2i(top()), p2i(_offsets.threshold()), p2i(end()));
912 }
913
914 HeapWord* G1OffsetTableContigSpace::initialize_threshold() {
915 return _offsets.initialize_threshold();
916 }
917
918 HeapWord* G1OffsetTableContigSpace::cross_threshold(HeapWord* start,
919 HeapWord* end) {
920 _offsets.alloc_block(start, end);
921 return _offsets.threshold();
922 }
923
924 HeapWord* G1OffsetTableContigSpace::scan_top() const {
925 G1CollectedHeap* g1h = G1CollectedHeap::heap();
926 HeapWord* local_top = top();
927 OrderAccess::loadload();
928 const unsigned local_time_stamp = _gc_time_stamp;
929 assert(local_time_stamp <= g1h->get_gc_time_stamp(), "invariant");
|