< prev index next >

src/share/vm/gc/g1/heapRegion.cpp

Print this page




  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) :


 487   hrrs->strong_code_roots_do(blk);
 488 }
 489 
 490 class VerifyStrongCodeRootOopClosure: public OopClosure {
 491   const HeapRegion* _hr;
 492   nmethod* _nm;
 493   bool _failures;
 494   bool _has_oops_in_region;
 495 
 496   template <class T> void do_oop_work(T* p) {
 497     T heap_oop = oopDesc::load_heap_oop(p);
 498     if (!oopDesc::is_null(heap_oop)) {
 499       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
 500 
 501       // Note: not all the oops embedded in the nmethod are in the
 502       // current region. We only look at those which are.
 503       if (_hr->is_in(obj)) {
 504         // Object is in the region. Check that its less than top
 505         if (_hr->top() <= (HeapWord*)obj) {
 506           // Object is above top
 507           gclog_or_tty->print_cr("Object " PTR_FORMAT " in region "
 508                                  "[" PTR_FORMAT ", " PTR_FORMAT ") is above "
 509                                  "top " PTR_FORMAT,
 510                                  p2i(obj), p2i(_hr->bottom()), p2i(_hr->end()), p2i(_hr->top()));
 511           _failures = true;
 512           return;
 513         }
 514         // Nmethod has at least one oop in the current region
 515         _has_oops_in_region = true;
 516       }
 517     }
 518   }
 519 
 520 public:
 521   VerifyStrongCodeRootOopClosure(const HeapRegion* hr, nmethod* nm):
 522     _hr(hr), _failures(false), _has_oops_in_region(false) {}
 523 
 524   void do_oop(narrowOop* p) { do_oop_work(p); }
 525   void do_oop(oop* p)       { do_oop_work(p); }
 526 
 527   bool failures()           { return _failures; }
 528   bool has_oops_in_region() { return _has_oops_in_region; }
 529 };
 530 
 531 class VerifyStrongCodeRootCodeBlobClosure: public CodeBlobClosure {
 532   const HeapRegion* _hr;
 533   bool _failures;
 534 public:
 535   VerifyStrongCodeRootCodeBlobClosure(const HeapRegion* hr) :
 536     _hr(hr), _failures(false) {}
 537 
 538   void do_code_blob(CodeBlob* cb) {
 539     nmethod* nm = (cb == NULL) ? NULL : cb->as_nmethod_or_null();
 540     if (nm != NULL) {
 541       // Verify that the nemthod is live
 542       if (!nm->is_alive()) {
 543         gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] has dead nmethod "
 544                                PTR_FORMAT " in its strong code roots",
 545                                p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
 546         _failures = true;
 547       } else {
 548         VerifyStrongCodeRootOopClosure oop_cl(_hr, nm);
 549         nm->oops_do(&oop_cl);
 550         if (!oop_cl.has_oops_in_region()) {
 551           gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] has nmethod "
 552                                  PTR_FORMAT " in its strong code roots "
 553                                  "with no pointers into region",
 554                                  p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
 555           _failures = true;
 556         } else if (oop_cl.failures()) {
 557           gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] has other "
 558                                  "failures for nmethod " PTR_FORMAT,
 559                                  p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
 560           _failures = true;
 561         }
 562       }
 563     }
 564   }
 565 
 566   bool failures()       { return _failures; }
 567 };
 568 
 569 void HeapRegion::verify_strong_code_roots(VerifyOption vo, bool* failures) const {
 570   if (!G1VerifyHeapRegionCodeRoots) {
 571     // We're not verifying code roots.
 572     return;
 573   }
 574   if (vo == VerifyOption_G1UseMarkWord) {
 575     // Marking verification during a full GC is performed after class
 576     // unloading, code cache unloading, etc so the strong code roots
 577     // attached to each heap region are in an inconsistent state. They won't
 578     // be consistent until the strong code roots are rebuilt after the
 579     // actual GC. Skip verifying the strong code roots in this particular
 580     // time.
 581     assert(VerifyDuringGC, "only way to get here");
 582     return;
 583   }
 584 
 585   HeapRegionRemSet* hrrs = rem_set();
 586   size_t strong_code_roots_length = hrrs->strong_code_roots_list_length();
 587 
 588   // if this region is empty then there should be no entries
 589   // on its strong code root list
 590   if (is_empty()) {
 591     if (strong_code_roots_length > 0) {
 592       gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] is empty "
 593                              "but has " SIZE_FORMAT " code root entries",
 594                              p2i(bottom()), p2i(end()), strong_code_roots_length);
 595       *failures = true;
 596     }
 597     return;
 598   }
 599 
 600   if (is_continues_humongous()) {
 601     if (strong_code_roots_length > 0) {
 602       gclog_or_tty->print_cr("region " HR_FORMAT " is a continuation of a humongous "
 603                              "region but has " SIZE_FORMAT " code root entries",
 604                              HR_FORMAT_PARAMS(this), strong_code_roots_length);
 605       *failures = true;
 606     }
 607     return;
 608   }
 609 
 610   VerifyStrongCodeRootCodeBlobClosure cb_cl(this);
 611   strong_code_roots_do(&cb_cl);
 612 
 613   if (cb_cl.failures()) {
 614     *failures = true;
 615   }
 616 }
 617 
 618 void HeapRegion::print() const { print_on(gclog_or_tty); }
 619 void HeapRegion::print_on(outputStream* st) const {
 620   st->print("AC%4u", allocation_context());
 621 
 622   st->print(" %2s", get_short_type_str());
 623   if (in_collection_set())
 624     st->print(" CS");
 625   else
 626     st->print("   ");
 627   st->print(" TS %5d", _gc_time_stamp);
 628   st->print(" PTAMS " PTR_FORMAT " NTAMS " PTR_FORMAT,
 629             p2i(prev_top_at_mark_start()), p2i(next_top_at_mark_start()));
 630   G1OffsetTableContigSpace::print_on(st);
 631 }
 632 
 633 class VerifyLiveClosure: public OopClosure {
 634 private:
 635   G1CollectedHeap* _g1h;
 636   CardTableModRefBS* _bs;
 637   oop _containing_obj;
 638   bool _failures;


 656 
 657   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 658   virtual void do_oop(      oop* p) { do_oop_work(p); }
 659 
 660   void print_object(outputStream* out, oop obj) {
 661 #ifdef PRODUCT
 662     Klass* k = obj->klass();
 663     const char* class_name = k->external_name();
 664     out->print_cr("class name %s", class_name);
 665 #else // PRODUCT
 666     obj->print_on(out);
 667 #endif // PRODUCT
 668   }
 669 
 670   template <class T>
 671   void do_oop_work(T* p) {
 672     assert(_containing_obj != NULL, "Precondition");
 673     assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo),
 674            "Precondition");
 675     T heap_oop = oopDesc::load_heap_oop(p);

 676     if (!oopDesc::is_null(heap_oop)) {
 677       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
 678       bool failed = false;
 679       if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead_cond(obj, _vo)) {
 680         MutexLockerEx x(ParGCRareEvent_lock,
 681                         Mutex::_no_safepoint_check_flag);
 682 
 683         if (!_failures) {
 684           gclog_or_tty->cr();
 685           gclog_or_tty->print_cr("----------");
 686         }

 687         if (!_g1h->is_in_closed_subset(obj)) {
 688           HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 689           gclog_or_tty->print_cr("Field " PTR_FORMAT
 690                                  " of live obj " PTR_FORMAT " in region "
 691                                  "[" PTR_FORMAT ", " PTR_FORMAT ")",
 692                                  p2i(p), p2i(_containing_obj),
 693                                  p2i(from->bottom()), p2i(from->end()));
 694           print_object(gclog_or_tty, _containing_obj);
 695           gclog_or_tty->print_cr("points to obj " PTR_FORMAT " not in the heap",
 696                                  p2i(obj));
 697         } else {
 698           HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 699           HeapRegion* to   = _g1h->heap_region_containing((HeapWord*)obj);
 700           gclog_or_tty->print_cr("Field " PTR_FORMAT
 701                                  " of live obj " PTR_FORMAT " in region "
 702                                  "[" PTR_FORMAT ", " PTR_FORMAT ")",
 703                                  p2i(p), p2i(_containing_obj),
 704                                  p2i(from->bottom()), p2i(from->end()));
 705           print_object(gclog_or_tty, _containing_obj);
 706           gclog_or_tty->print_cr("points to dead obj " PTR_FORMAT " in region "
 707                                  "[" PTR_FORMAT ", " PTR_FORMAT ")",
 708                                  p2i(obj), p2i(to->bottom()), p2i(to->end()));
 709           print_object(gclog_or_tty, obj);
 710         }
 711         gclog_or_tty->print_cr("----------");
 712         gclog_or_tty->flush();
 713         _failures = true;
 714         failed = true;
 715         _n_failures++;
 716       }
 717 
 718       if (!_g1h->collector_state()->full_collection() || G1VerifyRSetsDuringFullGC) {
 719         HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 720         HeapRegion* to   = _g1h->heap_region_containing(obj);
 721         if (from != NULL && to != NULL &&
 722             from != to &&
 723             !to->is_pinned()) {
 724           jbyte cv_obj = *_bs->byte_for_const(_containing_obj);
 725           jbyte cv_field = *_bs->byte_for_const(p);
 726           const jbyte dirty = CardTableModRefBS::dirty_card_val();
 727 
 728           bool is_bad = !(from->is_young()
 729                           || to->rem_set()->contains_reference(p)
 730                           || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed
 731                               (_containing_obj->is_objArray() ?
 732                                   cv_field == dirty
 733                                : cv_obj == dirty || cv_field == dirty));
 734           if (is_bad) {
 735             MutexLockerEx x(ParGCRareEvent_lock,
 736                             Mutex::_no_safepoint_check_flag);
 737 
 738             if (!_failures) {
 739               gclog_or_tty->cr();
 740               gclog_or_tty->print_cr("----------");
 741             }
 742             gclog_or_tty->print_cr("Missing rem set entry:");
 743             gclog_or_tty->print_cr("Field " PTR_FORMAT " "
 744                                    "of obj " PTR_FORMAT ", "
 745                                    "in region " HR_FORMAT,
 746                                    p2i(p), p2i(_containing_obj),
 747                                    HR_FORMAT_PARAMS(from));
 748             _containing_obj->print_on(gclog_or_tty);
 749             gclog_or_tty->print_cr("points to obj " PTR_FORMAT " "
 750                                    "in region " HR_FORMAT,
 751                                    p2i(obj),
 752                                    HR_FORMAT_PARAMS(to));
 753             obj->print_on(gclog_or_tty);
 754             gclog_or_tty->print_cr("Obj head CTE = %d, field CTE = %d.",
 755                           cv_obj, cv_field);
 756             gclog_or_tty->print_cr("----------");
 757             gclog_or_tty->flush();
 758             _failures = true;
 759             if (!failed) _n_failures++;
 760           }
 761         }
 762       }
 763     }
 764   }
 765 };
 766 
 767 // This really ought to be commoned up into OffsetTableContigSpace somehow.
 768 // We would need a mechanism to make that code skip dead objects.
 769 
 770 void HeapRegion::verify(VerifyOption vo,
 771                         bool* failures) const {
 772   G1CollectedHeap* g1 = G1CollectedHeap::heap();
 773   *failures = false;
 774   HeapWord* p = bottom();
 775   HeapWord* prev_p = NULL;
 776   VerifyLiveClosure vl_cl(g1, vo);
 777   bool is_region_humongous = is_humongous();
 778   size_t object_num = 0;
 779   while (p < top()) {
 780     oop obj = oop(p);
 781     size_t obj_size = block_size(p);
 782     object_num += 1;
 783 
 784     if (is_region_humongous != g1->is_humongous(obj_size) &&
 785         !g1->is_obj_dead(obj, this)) { // Dead objects may have bigger block_size since they span several objects.
 786       gclog_or_tty->print_cr("obj " PTR_FORMAT " is of %shumongous size ("
 787                              SIZE_FORMAT " words) in a %shumongous region",
 788                              p2i(p), g1->is_humongous(obj_size) ? "" : "non-",
 789                              obj_size, is_region_humongous ? "" : "non-");
 790        *failures = true;
 791        return;
 792     }
 793 
 794     if (!g1->is_obj_dead_cond(obj, this, vo)) {
 795       if (obj->is_oop()) {
 796         Klass* klass = obj->klass();
 797         bool is_metaspace_object = Metaspace::contains(klass) ||
 798                                    (vo == VerifyOption_G1UsePrevMarking &&
 799                                    ClassLoaderDataGraph::unload_list_contains(klass));
 800         if (!is_metaspace_object) {
 801           gclog_or_tty->print_cr("klass " PTR_FORMAT " of object " PTR_FORMAT " "
 802                                  "not metadata", p2i(klass), p2i(obj));
 803           *failures = true;
 804           return;
 805         } else if (!klass->is_klass()) {
 806           gclog_or_tty->print_cr("klass " PTR_FORMAT " of object " PTR_FORMAT " "
 807                                  "not a klass", p2i(klass), p2i(obj));
 808           *failures = true;
 809           return;
 810         } else {
 811           vl_cl.set_containing_obj(obj);
 812           obj->oop_iterate_no_header(&vl_cl);
 813           if (vl_cl.failures()) {
 814             *failures = true;
 815           }
 816           if (G1MaxVerifyFailures >= 0 &&
 817               vl_cl.n_failures() >= G1MaxVerifyFailures) {
 818             return;
 819           }
 820         }
 821       } else {
 822         gclog_or_tty->print_cr(PTR_FORMAT " no an oop", p2i(obj));
 823         *failures = true;
 824         return;
 825       }
 826     }
 827     prev_p = p;
 828     p += obj_size;
 829   }
 830 
 831   if (!is_young() && !is_empty()) {
 832     _offsets.verify();
 833   }
 834 
 835   if (p != top()) {
 836     gclog_or_tty->print_cr("end of last object " PTR_FORMAT " "
 837                            "does not match top " PTR_FORMAT, p2i(p), p2i(top()));
 838     *failures = true;
 839     return;
 840   }
 841 
 842   HeapWord* the_end = end();
 843   assert(p == top(), "it should still hold");
 844   // Do some extra BOT consistency checking for addresses in the
 845   // range [top, end). BOT look-ups in this range should yield
 846   // top. No point in doing that if top == end (there's nothing there).
 847   if (p < the_end) {
 848     // Look up top
 849     HeapWord* addr_1 = p;
 850     HeapWord* b_start_1 = _offsets.block_start_const(addr_1);
 851     if (b_start_1 != p) {
 852       gclog_or_tty->print_cr("BOT look up for top: " PTR_FORMAT " "
 853                              " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
 854                              p2i(addr_1), p2i(b_start_1), p2i(p));
 855       *failures = true;
 856       return;
 857     }
 858 
 859     // Look up top + 1
 860     HeapWord* addr_2 = p + 1;
 861     if (addr_2 < the_end) {
 862       HeapWord* b_start_2 = _offsets.block_start_const(addr_2);
 863       if (b_start_2 != p) {
 864         gclog_or_tty->print_cr("BOT look up for top + 1: " PTR_FORMAT " "
 865                                " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
 866                                p2i(addr_2), p2i(b_start_2), p2i(p));
 867         *failures = true;
 868         return;
 869       }
 870     }
 871 
 872     // Look up an address between top and end
 873     size_t diff = pointer_delta(the_end, p) / 2;
 874     HeapWord* addr_3 = p + diff;
 875     if (addr_3 < the_end) {
 876       HeapWord* b_start_3 = _offsets.block_start_const(addr_3);
 877       if (b_start_3 != p) {
 878         gclog_or_tty->print_cr("BOT look up for top + diff: " PTR_FORMAT " "
 879                                " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
 880                                p2i(addr_3), p2i(b_start_3), p2i(p));
 881         *failures = true;
 882         return;
 883       }
 884     }
 885 
 886     // Look up end - 1
 887     HeapWord* addr_4 = the_end - 1;
 888     HeapWord* b_start_4 = _offsets.block_start_const(addr_4);
 889     if (b_start_4 != p) {
 890       gclog_or_tty->print_cr("BOT look up for end - 1: " PTR_FORMAT " "
 891                              " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
 892                              p2i(addr_4), p2i(b_start_4), p2i(p));
 893       *failures = true;
 894       return;
 895     }
 896   }
 897 
 898   if (is_region_humongous && object_num > 1) {
 899     gclog_or_tty->print_cr("region [" PTR_FORMAT "," PTR_FORMAT "] is humongous "
 900                            "but has " SIZE_FORMAT ", objects",
 901                            p2i(bottom()), p2i(end()), object_num);
 902     *failures = true;
 903     return;
 904   }
 905 
 906   verify_strong_code_roots(vo, failures);
 907 }
 908 
 909 void HeapRegion::verify() const {
 910   bool dummy = false;
 911   verify(VerifyOption_G1UsePrevMarking, /* failures */ &dummy);
 912 }
 913 
 914 void HeapRegion::prepare_for_compaction(CompactPoint* cp) {
 915   scan_and_forward(this, cp);
 916 }
 917 
 918 // G1OffsetTableContigSpace code; copied from space.cpp.  Hope this can go
 919 // away eventually.


 930   _offsets.set_bottom(new_bottom);
 931 }
 932 
 933 void G1OffsetTableContigSpace::set_end(HeapWord* new_end) {
 934   Space::set_end(new_end);
 935   _offsets.resize(new_end - bottom());
 936 }
 937 
 938 #ifndef PRODUCT
 939 void G1OffsetTableContigSpace::mangle_unused_area() {
 940   mangle_unused_area_complete();
 941 }
 942 
 943 void G1OffsetTableContigSpace::mangle_unused_area_complete() {
 944   SpaceMangler::mangle_region(MemRegion(top(), end()));
 945 }
 946 #endif
 947 
 948 void G1OffsetTableContigSpace::print() const {
 949   print_short();
 950   gclog_or_tty->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", "
 951                 INTPTR_FORMAT ", " INTPTR_FORMAT ")",
 952                 p2i(bottom()), p2i(top()), p2i(_offsets.threshold()), p2i(end()));
 953 }
 954 
 955 HeapWord* G1OffsetTableContigSpace::initialize_threshold() {
 956   return _offsets.initialize_threshold();
 957 }
 958 
 959 HeapWord* G1OffsetTableContigSpace::cross_threshold(HeapWord* start,
 960                                                     HeapWord* end) {
 961   _offsets.alloc_block(start, end);
 962   return _offsets.threshold();
 963 }
 964 
 965 HeapWord* G1OffsetTableContigSpace::scan_top() const {
 966   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 967   HeapWord* local_top = top();
 968   OrderAccess::loadload();
 969   const unsigned local_time_stamp = _gc_time_stamp;
 970   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) :


 488   hrrs->strong_code_roots_do(blk);
 489 }
 490 
 491 class VerifyStrongCodeRootOopClosure: public OopClosure {
 492   const HeapRegion* _hr;
 493   nmethod* _nm;
 494   bool _failures;
 495   bool _has_oops_in_region;
 496 
 497   template <class T> void do_oop_work(T* p) {
 498     T heap_oop = oopDesc::load_heap_oop(p);
 499     if (!oopDesc::is_null(heap_oop)) {
 500       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
 501 
 502       // Note: not all the oops embedded in the nmethod are in the
 503       // current region. We only look at those which are.
 504       if (_hr->is_in(obj)) {
 505         // Object is in the region. Check that its less than top
 506         if (_hr->top() <= (HeapWord*)obj) {
 507           // Object is above top
 508           log_info(gc, verify)("Object " PTR_FORMAT " in region [" PTR_FORMAT ", " PTR_FORMAT ") is above top " PTR_FORMAT,


 509                                p2i(obj), p2i(_hr->bottom()), p2i(_hr->end()), p2i(_hr->top()));
 510           _failures = true;
 511           return;
 512         }
 513         // Nmethod has at least one oop in the current region
 514         _has_oops_in_region = true;
 515       }
 516     }
 517   }
 518 
 519 public:
 520   VerifyStrongCodeRootOopClosure(const HeapRegion* hr, nmethod* nm):
 521     _hr(hr), _failures(false), _has_oops_in_region(false) {}
 522 
 523   void do_oop(narrowOop* p) { do_oop_work(p); }
 524   void do_oop(oop* p)       { do_oop_work(p); }
 525 
 526   bool failures()           { return _failures; }
 527   bool has_oops_in_region() { return _has_oops_in_region; }
 528 };
 529 
 530 class VerifyStrongCodeRootCodeBlobClosure: public CodeBlobClosure {
 531   const HeapRegion* _hr;
 532   bool _failures;
 533 public:
 534   VerifyStrongCodeRootCodeBlobClosure(const HeapRegion* hr) :
 535     _hr(hr), _failures(false) {}
 536 
 537   void do_code_blob(CodeBlob* cb) {
 538     nmethod* nm = (cb == NULL) ? NULL : cb->as_nmethod_or_null();
 539     if (nm != NULL) {
 540       // Verify that the nemthod is live
 541       if (!nm->is_alive()) {
 542         log_info(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] has dead nmethod " PTR_FORMAT " in its strong code roots",

 543                              p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
 544         _failures = true;
 545       } else {
 546         VerifyStrongCodeRootOopClosure oop_cl(_hr, nm);
 547         nm->oops_do(&oop_cl);
 548         if (!oop_cl.has_oops_in_region()) {
 549           log_info(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] has nmethod " PTR_FORMAT " in its strong code roots with no pointers into region",


 550                                p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
 551           _failures = true;
 552         } else if (oop_cl.failures()) {
 553           log_info(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] has other failures for nmethod " PTR_FORMAT,

 554                                p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
 555           _failures = true;
 556         }
 557       }
 558     }
 559   }
 560 
 561   bool failures()       { return _failures; }
 562 };
 563 
 564 void HeapRegion::verify_strong_code_roots(VerifyOption vo, bool* failures) const {
 565   if (!G1VerifyHeapRegionCodeRoots) {
 566     // We're not verifying code roots.
 567     return;
 568   }
 569   if (vo == VerifyOption_G1UseMarkWord) {
 570     // Marking verification during a full GC is performed after class
 571     // unloading, code cache unloading, etc so the strong code roots
 572     // attached to each heap region are in an inconsistent state. They won't
 573     // be consistent until the strong code roots are rebuilt after the
 574     // actual GC. Skip verifying the strong code roots in this particular
 575     // time.
 576     assert(VerifyDuringGC, "only way to get here");
 577     return;
 578   }
 579 
 580   HeapRegionRemSet* hrrs = rem_set();
 581   size_t strong_code_roots_length = hrrs->strong_code_roots_list_length();
 582 
 583   // if this region is empty then there should be no entries
 584   // on its strong code root list
 585   if (is_empty()) {
 586     if (strong_code_roots_length > 0) {
 587       log_info(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] is empty but has " SIZE_FORMAT " code root entries",

 588                            p2i(bottom()), p2i(end()), strong_code_roots_length);
 589       *failures = true;
 590     }
 591     return;
 592   }
 593 
 594   if (is_continues_humongous()) {
 595     if (strong_code_roots_length > 0) {
 596       log_info(gc, verify)("region " HR_FORMAT " is a continuation of a humongous region but has " SIZE_FORMAT " code root entries",

 597                            HR_FORMAT_PARAMS(this), strong_code_roots_length);
 598       *failures = true;
 599     }
 600     return;
 601   }
 602 
 603   VerifyStrongCodeRootCodeBlobClosure cb_cl(this);
 604   strong_code_roots_do(&cb_cl);
 605 
 606   if (cb_cl.failures()) {
 607     *failures = true;
 608   }
 609 }
 610 
 611 void HeapRegion::print() const { print_on(tty); }
 612 void HeapRegion::print_on(outputStream* st) const {
 613   st->print("AC%4u", allocation_context());
 614 
 615   st->print(" %2s", get_short_type_str());
 616   if (in_collection_set())
 617     st->print(" CS");
 618   else
 619     st->print("   ");
 620   st->print(" TS %5d", _gc_time_stamp);
 621   st->print(" PTAMS " PTR_FORMAT " NTAMS " PTR_FORMAT,
 622             p2i(prev_top_at_mark_start()), p2i(next_top_at_mark_start()));
 623   G1OffsetTableContigSpace::print_on(st);
 624 }
 625 
 626 class VerifyLiveClosure: public OopClosure {
 627 private:
 628   G1CollectedHeap* _g1h;
 629   CardTableModRefBS* _bs;
 630   oop _containing_obj;
 631   bool _failures;


 649 
 650   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 651   virtual void do_oop(      oop* p) { do_oop_work(p); }
 652 
 653   void print_object(outputStream* out, oop obj) {
 654 #ifdef PRODUCT
 655     Klass* k = obj->klass();
 656     const char* class_name = k->external_name();
 657     out->print_cr("class name %s", class_name);
 658 #else // PRODUCT
 659     obj->print_on(out);
 660 #endif // PRODUCT
 661   }
 662 
 663   template <class T>
 664   void do_oop_work(T* p) {
 665     assert(_containing_obj != NULL, "Precondition");
 666     assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo),
 667            "Precondition");
 668     T heap_oop = oopDesc::load_heap_oop(p);
 669     LogHandle(gc, verify) log;
 670     if (!oopDesc::is_null(heap_oop)) {
 671       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
 672       bool failed = false;
 673       if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead_cond(obj, _vo)) {
 674         MutexLockerEx x(ParGCRareEvent_lock,
 675                         Mutex::_no_safepoint_check_flag);
 676 
 677         if (!_failures) {
 678           log.info("----------");

 679         }
 680         ResourceMark rm;
 681         if (!_g1h->is_in_closed_subset(obj)) {
 682           HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 683           log.info("Field " PTR_FORMAT " of live obj " PTR_FORMAT " in region [" PTR_FORMAT ", " PTR_FORMAT ")",
 684                    p2i(p), p2i(_containing_obj), p2i(from->bottom()), p2i(from->end()));
 685           print_object(log.info_stream(), _containing_obj);
 686           log.info("points to obj " PTR_FORMAT " not in the heap", p2i(obj));




 687         } else {
 688           HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 689           HeapRegion* to   = _g1h->heap_region_containing((HeapWord*)obj);
 690           log.info("Field " PTR_FORMAT " of live obj " PTR_FORMAT " in region [" PTR_FORMAT ", " PTR_FORMAT ")",
 691                    p2i(p), p2i(_containing_obj), p2i(from->bottom()), p2i(from->end()));
 692           print_object(log.info_stream(), _containing_obj);
 693           log.info("points to dead obj " PTR_FORMAT " in region [" PTR_FORMAT ", " PTR_FORMAT ")",




 694                    p2i(obj), p2i(to->bottom()), p2i(to->end()));
 695           print_object(log.info_stream(), obj);
 696         }
 697         log.info("----------");

 698         _failures = true;
 699         failed = true;
 700         _n_failures++;
 701       }
 702 
 703       if (!_g1h->collector_state()->full_collection() || G1VerifyRSetsDuringFullGC) {
 704         HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 705         HeapRegion* to   = _g1h->heap_region_containing(obj);
 706         if (from != NULL && to != NULL &&
 707             from != to &&
 708             !to->is_pinned()) {
 709           jbyte cv_obj = *_bs->byte_for_const(_containing_obj);
 710           jbyte cv_field = *_bs->byte_for_const(p);
 711           const jbyte dirty = CardTableModRefBS::dirty_card_val();
 712 
 713           bool is_bad = !(from->is_young()
 714                           || to->rem_set()->contains_reference(p)
 715                           || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed
 716                               (_containing_obj->is_objArray() ?
 717                                   cv_field == dirty
 718                                : cv_obj == dirty || cv_field == dirty));
 719           if (is_bad) {
 720             MutexLockerEx x(ParGCRareEvent_lock,
 721                             Mutex::_no_safepoint_check_flag);
 722 
 723             if (!_failures) {
 724               log.info("----------");

 725             }
 726             log.info("Missing rem set entry:");
 727             log.info("Field " PTR_FORMAT " of obj " PTR_FORMAT ", in region " HR_FORMAT,
 728                      p2i(p), p2i(_containing_obj), HR_FORMAT_PARAMS(from));
 729             ResourceMark rm;
 730             _containing_obj->print_on(log.info_stream());
 731             log.info("points to obj " PTR_FORMAT " in region " HR_FORMAT, p2i(obj), HR_FORMAT_PARAMS(to));
 732             obj->print_on(log.info_stream());
 733             log.info("Obj head CTE = %d, field CTE = %d.", cv_obj, cv_field);
 734             log.info("----------");







 735             _failures = true;
 736             if (!failed) _n_failures++;
 737           }
 738         }
 739       }
 740     }
 741   }
 742 };
 743 
 744 // This really ought to be commoned up into OffsetTableContigSpace somehow.
 745 // We would need a mechanism to make that code skip dead objects.
 746 
 747 void HeapRegion::verify(VerifyOption vo,
 748                         bool* failures) const {
 749   G1CollectedHeap* g1 = G1CollectedHeap::heap();
 750   *failures = false;
 751   HeapWord* p = bottom();
 752   HeapWord* prev_p = NULL;
 753   VerifyLiveClosure vl_cl(g1, vo);
 754   bool is_region_humongous = is_humongous();
 755   size_t object_num = 0;
 756   while (p < top()) {
 757     oop obj = oop(p);
 758     size_t obj_size = block_size(p);
 759     object_num += 1;
 760 
 761     if (is_region_humongous != g1->is_humongous(obj_size) &&
 762         !g1->is_obj_dead(obj, this)) { // Dead objects may have bigger block_size since they span several objects.
 763       log_info(gc, verify)("obj " PTR_FORMAT " is of %shumongous size ("
 764                            SIZE_FORMAT " words) in a %shumongous region",
 765                            p2i(p), g1->is_humongous(obj_size) ? "" : "non-",
 766                            obj_size, is_region_humongous ? "" : "non-");
 767        *failures = true;
 768        return;
 769     }
 770 
 771     if (!g1->is_obj_dead_cond(obj, this, vo)) {
 772       if (obj->is_oop()) {
 773         Klass* klass = obj->klass();
 774         bool is_metaspace_object = Metaspace::contains(klass) ||
 775                                    (vo == VerifyOption_G1UsePrevMarking &&
 776                                    ClassLoaderDataGraph::unload_list_contains(klass));
 777         if (!is_metaspace_object) {
 778           log_info(gc, verify)("klass " PTR_FORMAT " of object " PTR_FORMAT " "
 779                                "not metadata", p2i(klass), p2i(obj));
 780           *failures = true;
 781           return;
 782         } else if (!klass->is_klass()) {
 783           log_info(gc, verify)("klass " PTR_FORMAT " of object " PTR_FORMAT " "
 784                                "not a klass", p2i(klass), p2i(obj));
 785           *failures = true;
 786           return;
 787         } else {
 788           vl_cl.set_containing_obj(obj);
 789           obj->oop_iterate_no_header(&vl_cl);
 790           if (vl_cl.failures()) {
 791             *failures = true;
 792           }
 793           if (G1MaxVerifyFailures >= 0 &&
 794               vl_cl.n_failures() >= G1MaxVerifyFailures) {
 795             return;
 796           }
 797         }
 798       } else {
 799         log_info(gc, verify)(PTR_FORMAT " no an oop", p2i(obj));
 800         *failures = true;
 801         return;
 802       }
 803     }
 804     prev_p = p;
 805     p += obj_size;
 806   }
 807 
 808   if (!is_young() && !is_empty()) {
 809     _offsets.verify();
 810   }
 811 
 812   if (p != top()) {
 813     log_info(gc, verify)("end of last object " PTR_FORMAT " "
 814                          "does not match top " PTR_FORMAT, p2i(p), p2i(top()));
 815     *failures = true;
 816     return;
 817   }
 818 
 819   HeapWord* the_end = end();
 820   assert(p == top(), "it should still hold");
 821   // Do some extra BOT consistency checking for addresses in the
 822   // range [top, end). BOT look-ups in this range should yield
 823   // top. No point in doing that if top == end (there's nothing there).
 824   if (p < the_end) {
 825     // Look up top
 826     HeapWord* addr_1 = p;
 827     HeapWord* b_start_1 = _offsets.block_start_const(addr_1);
 828     if (b_start_1 != p) {
 829       log_info(gc, verify)("BOT look up for top: " PTR_FORMAT " "
 830                            " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
 831                            p2i(addr_1), p2i(b_start_1), p2i(p));
 832       *failures = true;
 833       return;
 834     }
 835 
 836     // Look up top + 1
 837     HeapWord* addr_2 = p + 1;
 838     if (addr_2 < the_end) {
 839       HeapWord* b_start_2 = _offsets.block_start_const(addr_2);
 840       if (b_start_2 != p) {
 841         log_info(gc, verify)("BOT look up for top + 1: " PTR_FORMAT " "
 842                              " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
 843                              p2i(addr_2), p2i(b_start_2), p2i(p));
 844         *failures = true;
 845         return;
 846       }
 847     }
 848 
 849     // Look up an address between top and end
 850     size_t diff = pointer_delta(the_end, p) / 2;
 851     HeapWord* addr_3 = p + diff;
 852     if (addr_3 < the_end) {
 853       HeapWord* b_start_3 = _offsets.block_start_const(addr_3);
 854       if (b_start_3 != p) {
 855         log_info(gc, verify)("BOT look up for top + diff: " PTR_FORMAT " "
 856                              " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
 857                              p2i(addr_3), p2i(b_start_3), p2i(p));
 858         *failures = true;
 859         return;
 860       }
 861     }
 862 
 863     // Look up end - 1
 864     HeapWord* addr_4 = the_end - 1;
 865     HeapWord* b_start_4 = _offsets.block_start_const(addr_4);
 866     if (b_start_4 != p) {
 867       log_info(gc, verify)("BOT look up for end - 1: " PTR_FORMAT " "
 868                            " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
 869                            p2i(addr_4), p2i(b_start_4), p2i(p));
 870       *failures = true;
 871       return;
 872     }
 873   }
 874 
 875   if (is_region_humongous && object_num > 1) {
 876     log_info(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] is humongous "
 877                          "but has " SIZE_FORMAT ", objects",
 878                          p2i(bottom()), p2i(end()), object_num);
 879     *failures = true;
 880     return;
 881   }
 882 
 883   verify_strong_code_roots(vo, failures);
 884 }
 885 
 886 void HeapRegion::verify() const {
 887   bool dummy = false;
 888   verify(VerifyOption_G1UsePrevMarking, /* failures */ &dummy);
 889 }
 890 
 891 void HeapRegion::prepare_for_compaction(CompactPoint* cp) {
 892   scan_and_forward(this, cp);
 893 }
 894 
 895 // G1OffsetTableContigSpace code; copied from space.cpp.  Hope this can go
 896 // away eventually.


 907   _offsets.set_bottom(new_bottom);
 908 }
 909 
 910 void G1OffsetTableContigSpace::set_end(HeapWord* new_end) {
 911   Space::set_end(new_end);
 912   _offsets.resize(new_end - bottom());
 913 }
 914 
 915 #ifndef PRODUCT
 916 void G1OffsetTableContigSpace::mangle_unused_area() {
 917   mangle_unused_area_complete();
 918 }
 919 
 920 void G1OffsetTableContigSpace::mangle_unused_area_complete() {
 921   SpaceMangler::mangle_region(MemRegion(top(), end()));
 922 }
 923 #endif
 924 
 925 void G1OffsetTableContigSpace::print() const {
 926   print_short();
 927   tty->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", "
 928                 INTPTR_FORMAT ", " INTPTR_FORMAT ")",
 929                 p2i(bottom()), p2i(top()), p2i(_offsets.threshold()), p2i(end()));
 930 }
 931 
 932 HeapWord* G1OffsetTableContigSpace::initialize_threshold() {
 933   return _offsets.initialize_threshold();
 934 }
 935 
 936 HeapWord* G1OffsetTableContigSpace::cross_threshold(HeapWord* start,
 937                                                     HeapWord* end) {
 938   _offsets.alloc_block(start, end);
 939   return _offsets.threshold();
 940 }
 941 
 942 HeapWord* G1OffsetTableContigSpace::scan_top() const {
 943   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 944   HeapWord* local_top = top();
 945   OrderAccess::loadload();
 946   const unsigned local_time_stamp = _gc_time_stamp;
 947   assert(local_time_stamp <= g1h->get_gc_time_stamp(), "invariant");


< prev index next >