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

Print this page
rev 4801 : imported patch code-movement
rev 4802 : imported patch optimize-nmethod-scanning
rev 4803 : imported patch thomas-comments-2


   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_implementation/g1/g1BlockOffsetTable.inline.hpp"
  27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc_implementation/g1/g1OopClosures.inline.hpp"
  29 #include "gc_implementation/g1/heapRegion.inline.hpp"
  30 #include "gc_implementation/g1/heapRegionRemSet.hpp"
  31 #include "gc_implementation/g1/heapRegionSeq.inline.hpp"
  32 #include "memory/genOopClosures.inline.hpp"
  33 #include "memory/iterator.hpp"
  34 #include "oops/oop.inline.hpp"
  35 
  36 int    HeapRegion::LogOfHRGrainBytes = 0;
  37 int    HeapRegion::LogOfHRGrainWords = 0;
  38 size_t HeapRegion::GrainBytes        = 0;
  39 size_t HeapRegion::GrainWords        = 0;
  40 size_t HeapRegion::CardsPerRegion    = 0;
  41 
  42 HeapRegionDCTOC::HeapRegionDCTOC(G1CollectedHeap* g1,
  43                                  HeapRegion* hr, ExtendedOopClosure* cl,
  44                                  CardTableModRefBS::PrecisionStyle precision,
  45                                  FilterKind fk) :
  46   ContiguousSpaceDCTOC(hr, cl, precision, NULL),
  47   _hr(hr), _fk(fk), _g1(g1) { }
  48 
  49 FilterOutOfRegionClosure::FilterOutOfRegionClosure(HeapRegion* r,
  50                                                    OopClosure* oc) :
  51   _r_bottom(r->bottom()), _r_end(r->end()), _oc(oc) { }
  52 
  53 class VerifyLiveClosure: public OopClosure {
  54 private:
  55   G1CollectedHeap* _g1h;
  56   CardTableModRefBS* _bs;
  57   oop _containing_obj;
  58   bool _failures;
  59   int _n_failures;
  60   VerifyOption _vo;
  61 public:
  62   // _vo == UsePrevMarking -> use "prev" marking information,
  63   // _vo == UseNextMarking -> use "next" marking information,
  64   // _vo == UseMarkWord    -> use mark word from object header.
  65   VerifyLiveClosure(G1CollectedHeap* g1h, VerifyOption vo) :
  66     _g1h(g1h), _bs(NULL), _containing_obj(NULL),
  67     _failures(false), _n_failures(0), _vo(vo)
  68   {
  69     BarrierSet* bs = _g1h->barrier_set();
  70     if (bs->is_a(BarrierSet::CardTableModRef))
  71       _bs = (CardTableModRefBS*)bs;
  72   }
  73 
  74   void set_containing_obj(oop obj) {
  75     _containing_obj = obj;
  76   }
  77 
  78   bool failures() { return _failures; }
  79   int n_failures() { return _n_failures; }
  80 
  81   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
  82   virtual void do_oop(      oop* p) { do_oop_work(p); }
  83 
  84   void print_object(outputStream* out, oop obj) {
  85 #ifdef PRODUCT
  86     Klass* k = obj->klass();
  87     const char* class_name = InstanceKlass::cast(k)->external_name();
  88     out->print_cr("class name %s", class_name);
  89 #else // PRODUCT
  90     obj->print_on(out);
  91 #endif // PRODUCT
  92   }
  93 
  94   template <class T>
  95   void do_oop_work(T* p) {
  96     assert(_containing_obj != NULL, "Precondition");
  97     assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo),
  98            "Precondition");
  99     T heap_oop = oopDesc::load_heap_oop(p);
 100     if (!oopDesc::is_null(heap_oop)) {
 101       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
 102       bool failed = false;
 103       if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead_cond(obj, _vo)) {
 104         MutexLockerEx x(ParGCRareEvent_lock,
 105                         Mutex::_no_safepoint_check_flag);
 106 
 107         if (!_failures) {
 108           gclog_or_tty->print_cr("");
 109           gclog_or_tty->print_cr("----------");
 110         }
 111         if (!_g1h->is_in_closed_subset(obj)) {
 112           HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 113           gclog_or_tty->print_cr("Field "PTR_FORMAT
 114                                  " of live obj "PTR_FORMAT" in region "
 115                                  "["PTR_FORMAT", "PTR_FORMAT")",
 116                                  p, (void*) _containing_obj,
 117                                  from->bottom(), from->end());
 118           print_object(gclog_or_tty, _containing_obj);
 119           gclog_or_tty->print_cr("points to obj "PTR_FORMAT" not in the heap",
 120                                  (void*) obj);
 121         } else {
 122           HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 123           HeapRegion* to   = _g1h->heap_region_containing((HeapWord*)obj);
 124           gclog_or_tty->print_cr("Field "PTR_FORMAT
 125                                  " of live obj "PTR_FORMAT" in region "
 126                                  "["PTR_FORMAT", "PTR_FORMAT")",
 127                                  p, (void*) _containing_obj,
 128                                  from->bottom(), from->end());
 129           print_object(gclog_or_tty, _containing_obj);
 130           gclog_or_tty->print_cr("points to dead obj "PTR_FORMAT" in region "
 131                                  "["PTR_FORMAT", "PTR_FORMAT")",
 132                                  (void*) obj, to->bottom(), to->end());
 133           print_object(gclog_or_tty, obj);
 134         }
 135         gclog_or_tty->print_cr("----------");
 136         gclog_or_tty->flush();
 137         _failures = true;
 138         failed = true;
 139         _n_failures++;
 140       }
 141 
 142       if (!_g1h->full_collection() || G1VerifyRSetsDuringFullGC) {
 143         HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 144         HeapRegion* to   = _g1h->heap_region_containing(obj);
 145         if (from != NULL && to != NULL &&
 146             from != to &&
 147             !to->isHumongous()) {
 148           jbyte cv_obj = *_bs->byte_for_const(_containing_obj);
 149           jbyte cv_field = *_bs->byte_for_const(p);
 150           const jbyte dirty = CardTableModRefBS::dirty_card_val();
 151 
 152           bool is_bad = !(from->is_young()
 153                           || to->rem_set()->contains_reference(p)
 154                           || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed
 155                               (_containing_obj->is_objArray() ?
 156                                   cv_field == dirty
 157                                : cv_obj == dirty || cv_field == dirty));
 158           if (is_bad) {
 159             MutexLockerEx x(ParGCRareEvent_lock,
 160                             Mutex::_no_safepoint_check_flag);
 161 
 162             if (!_failures) {
 163               gclog_or_tty->print_cr("");
 164               gclog_or_tty->print_cr("----------");
 165             }
 166             gclog_or_tty->print_cr("Missing rem set entry:");
 167             gclog_or_tty->print_cr("Field "PTR_FORMAT" "
 168                                    "of obj "PTR_FORMAT", "
 169                                    "in region "HR_FORMAT,
 170                                    p, (void*) _containing_obj,
 171                                    HR_FORMAT_PARAMS(from));
 172             _containing_obj->print_on(gclog_or_tty);
 173             gclog_or_tty->print_cr("points to obj "PTR_FORMAT" "
 174                                    "in region "HR_FORMAT,
 175                                    (void*) obj,
 176                                    HR_FORMAT_PARAMS(to));
 177             obj->print_on(gclog_or_tty);
 178             gclog_or_tty->print_cr("Obj head CTE = %d, field CTE = %d.",
 179                           cv_obj, cv_field);
 180             gclog_or_tty->print_cr("----------");
 181             gclog_or_tty->flush();
 182             _failures = true;
 183             if (!failed) _n_failures++;
 184           }
 185         }
 186       }
 187     }
 188   }
 189 };
 190 
 191 template<class ClosureType>
 192 HeapWord* walk_mem_region_loop(ClosureType* cl, G1CollectedHeap* g1h,
 193                                HeapRegion* hr,
 194                                HeapWord* cur, HeapWord* top) {
 195   oop cur_oop = oop(cur);
 196   int oop_size = cur_oop->size();
 197   HeapWord* next_obj = cur + oop_size;
 198   while (next_obj < top) {
 199     // Keep filtering the remembered set.
 200     if (!g1h->is_obj_dead(cur_oop, hr)) {
 201       // Bottom lies entirely below top, so we can call the
 202       // non-memRegion version of oop_iterate below.
 203       cur_oop->oop_iterate(cl);
 204     }
 205     cur = next_obj;
 206     cur_oop = oop(cur);
 207     oop_size = cur_oop->size();
 208     next_obj = cur + oop_size;
 209   }
 210   return cur;


 346 }
 347 
 348 void HeapRegion::hr_clear(bool par, bool clear_space) {
 349   assert(_humongous_type == NotHumongous,
 350          "we should have already filtered out humongous regions");
 351   assert(_humongous_start_region == NULL,
 352          "we should have already filtered out humongous regions");
 353   assert(_end == _orig_end,
 354          "we should have already filtered out humongous regions");
 355 
 356   _in_collection_set = false;
 357 
 358   set_young_index_in_cset(-1);
 359   uninstall_surv_rate_group();
 360   set_young_type(NotYoung);
 361   reset_pre_dummy_top();
 362 
 363   if (!par) {
 364     // If this is parallel, this will be done later.
 365     HeapRegionRemSet* hrrs = rem_set();
 366     if (hrrs != NULL) hrrs->clear();
 367     _claimed = InitialClaimValue;
 368   }
 369   zero_marked_bytes();
 370 
 371   _offsets.resize(HeapRegion::GrainWords);
 372   init_top_at_mark_start();
 373   if (clear_space) clear(SpaceDecorator::Mangle);
 374 }
 375 
 376 void HeapRegion::par_clear() {
 377   assert(used() == 0, "the region should have been already cleared");
 378   assert(capacity() == HeapRegion::GrainBytes, "should be back to normal");
 379   HeapRegionRemSet* hrrs = rem_set();
 380   hrrs->clear();
 381   CardTableModRefBS* ct_bs =
 382                    (CardTableModRefBS*)G1CollectedHeap::heap()->barrier_set();
 383   ct_bs->clear(MemRegion(bottom(), end()));
 384 }
 385 
 386 void HeapRegion::calc_gc_efficiency() {


 483 
 484 HeapRegion::HeapRegion(uint hrs_index,
 485                        G1BlockOffsetSharedArray* sharedOffsetArray,
 486                        MemRegion mr) :
 487     G1OffsetTableContigSpace(sharedOffsetArray, mr),
 488     _hrs_index(hrs_index),
 489     _humongous_type(NotHumongous), _humongous_start_region(NULL),
 490     _in_collection_set(false),
 491     _next_in_special_set(NULL), _orig_end(NULL),
 492     _claimed(InitialClaimValue), _evacuation_failed(false),
 493     _prev_marked_bytes(0), _next_marked_bytes(0), _gc_efficiency(0.0),
 494     _young_type(NotYoung), _next_young_region(NULL),
 495     _next_dirty_cards_region(NULL), _next(NULL), _pending_removal(false),
 496 #ifdef ASSERT
 497     _containing_set(NULL),
 498 #endif // ASSERT
 499      _young_index_in_cset(-1), _surv_rate_group(NULL), _age_index(-1),
 500     _rem_set(NULL), _recorded_rs_length(0), _predicted_elapsed_time_ms(0),
 501     _predicted_bytes_to_copy(0)
 502 {

 503   _orig_end = mr.end();
 504   // Note that initialize() will set the start of the unmarked area of the
 505   // region.
 506   hr_clear(false /*par*/, false /*clear_space*/);
 507   set_top(bottom());
 508   set_saved_mark();
 509 
 510   _rem_set =  new HeapRegionRemSet(sharedOffsetArray, this);
 511 
 512   assert(HeapRegionRemSet::num_par_rem_sets() > 0, "Invariant.");
 513 }
 514 
 515 CompactibleSpace* HeapRegion::next_compaction_space() const {
 516   // We're not using an iterator given that it will wrap around when
 517   // it reaches the last region and this is not what we want here.
 518   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 519   uint index = hrs_index() + 1;
 520   while (index < g1h->n_regions()) {
 521     HeapRegion* hr = g1h->region_at(index);
 522     if (!hr->isHumongous()) {
 523       return hr;
 524     }
 525     index += 1;
 526   }
 527   return NULL;
 528 }
 529 
 530 void HeapRegion::save_marks() {
 531   set_saved_mark();


 711     // Otherwise:
 712     next = (cur + obj->size());
 713 
 714     if (!g1h->is_obj_dead(obj)) {
 715       if (next < end || !obj->is_objArray()) {
 716         // This object either does not span the MemRegion
 717         // boundary, or if it does it's not an array.
 718         // Apply closure to whole object.
 719         obj->oop_iterate(cl);
 720       } else {
 721         // This obj is an array that spans the boundary.
 722         // Stop at the boundary.
 723         obj->oop_iterate(cl, mr);
 724       }
 725     }
 726     cur = next;
 727   }
 728   return NULL;
 729 }
 730 


























































































































































 731 void HeapRegion::print() const { print_on(gclog_or_tty); }
 732 void HeapRegion::print_on(outputStream* st) const {
 733   if (isHumongous()) {
 734     if (startsHumongous())
 735       st->print(" HS");
 736     else
 737       st->print(" HC");
 738   } else {
 739     st->print("   ");
 740   }
 741   if (in_collection_set())
 742     st->print(" CS");
 743   else
 744     st->print("   ");
 745   if (is_young())
 746     st->print(is_survivor() ? " SU" : " Y ");
 747   else
 748     st->print("   ");
 749   if (is_empty())
 750     st->print(" F");
 751   else
 752     st->print("  ");
 753   st->print(" TS %5d", _gc_time_stamp);
 754   st->print(" PTAMS "PTR_FORMAT" NTAMS "PTR_FORMAT,
 755             prev_top_at_mark_start(), next_top_at_mark_start());
 756   G1OffsetTableContigSpace::print_on(st);
 757 }
 758 
 759 void HeapRegion::verify() const {
 760   bool dummy = false;
 761   verify(VerifyOption_G1UsePrevMarking, /* failures */ &dummy);
 762 }





































































































































 763 
 764 // This really ought to be commoned up into OffsetTableContigSpace somehow.
 765 // We would need a mechanism to make that code skip dead objects.
 766 
 767 void HeapRegion::verify(VerifyOption vo,
 768                         bool* failures) const {
 769   G1CollectedHeap* g1 = G1CollectedHeap::heap();
 770   *failures = false;
 771   HeapWord* p = bottom();
 772   HeapWord* prev_p = NULL;
 773   VerifyLiveClosure vl_cl(g1, vo);
 774   bool is_humongous = isHumongous();
 775   bool do_bot_verify = !is_young();
 776   size_t object_num = 0;
 777   while (p < top()) {
 778     oop obj = oop(p);
 779     size_t obj_size = obj->size();
 780     object_num += 1;
 781 
 782     if (is_humongous != g1->isHumongous(obj_size)) {


 882 
 883     // Loook up end - 1
 884     HeapWord* addr_4 = the_end - 1;
 885     HeapWord* b_start_4 = _offsets.block_start_const(addr_4);
 886     if (b_start_4 != p) {
 887       gclog_or_tty->print_cr("BOT look up for end - 1: "PTR_FORMAT" "
 888                              " yielded "PTR_FORMAT", expecting "PTR_FORMAT,
 889                              addr_4, b_start_4, p);
 890       *failures = true;
 891       return;
 892     }
 893   }
 894 
 895   if (is_humongous && object_num > 1) {
 896     gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] is humongous "
 897                            "but has "SIZE_FORMAT", objects",
 898                            bottom(), end(), object_num);
 899     *failures = true;
 900     return;
 901   }







 902 }
 903 
 904 // G1OffsetTableContigSpace code; copied from space.cpp.  Hope this can go
 905 // away eventually.
 906 
 907 void G1OffsetTableContigSpace::clear(bool mangle_space) {
 908   ContiguousSpace::clear(mangle_space);
 909   _offsets.zero_bottom_entry();
 910   _offsets.initialize_threshold();
 911 }
 912 
 913 void G1OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) {
 914   Space::set_bottom(new_bottom);
 915   _offsets.set_bottom(new_bottom);
 916 }
 917 
 918 void G1OffsetTableContigSpace::set_end(HeapWord* new_end) {
 919   Space::set_end(new_end);
 920   _offsets.resize(new_end - bottom());
 921 }




   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 "code/nmethod.hpp"
  27 #include "gc_implementation/g1/g1BlockOffsetTable.inline.hpp"
  28 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  29 #include "gc_implementation/g1/g1OopClosures.inline.hpp"
  30 #include "gc_implementation/g1/heapRegion.inline.hpp"
  31 #include "gc_implementation/g1/heapRegionRemSet.hpp"
  32 #include "gc_implementation/g1/heapRegionSeq.inline.hpp"
  33 #include "memory/genOopClosures.inline.hpp"
  34 #include "memory/iterator.hpp"
  35 #include "oops/oop.inline.hpp"
  36 
  37 int    HeapRegion::LogOfHRGrainBytes = 0;
  38 int    HeapRegion::LogOfHRGrainWords = 0;
  39 size_t HeapRegion::GrainBytes        = 0;
  40 size_t HeapRegion::GrainWords        = 0;
  41 size_t HeapRegion::CardsPerRegion    = 0;
  42 
  43 HeapRegionDCTOC::HeapRegionDCTOC(G1CollectedHeap* g1,
  44                                  HeapRegion* hr, ExtendedOopClosure* cl,
  45                                  CardTableModRefBS::PrecisionStyle precision,
  46                                  FilterKind fk) :
  47   ContiguousSpaceDCTOC(hr, cl, precision, NULL),
  48   _hr(hr), _fk(fk), _g1(g1) { }
  49 
  50 FilterOutOfRegionClosure::FilterOutOfRegionClosure(HeapRegion* r,
  51                                                    OopClosure* oc) :
  52   _r_bottom(r->bottom()), _r_end(r->end()), _oc(oc) { }
  53 










































































































































  54 template<class ClosureType>
  55 HeapWord* walk_mem_region_loop(ClosureType* cl, G1CollectedHeap* g1h,
  56                                HeapRegion* hr,
  57                                HeapWord* cur, HeapWord* top) {
  58   oop cur_oop = oop(cur);
  59   int oop_size = cur_oop->size();
  60   HeapWord* next_obj = cur + oop_size;
  61   while (next_obj < top) {
  62     // Keep filtering the remembered set.
  63     if (!g1h->is_obj_dead(cur_oop, hr)) {
  64       // Bottom lies entirely below top, so we can call the
  65       // non-memRegion version of oop_iterate below.
  66       cur_oop->oop_iterate(cl);
  67     }
  68     cur = next_obj;
  69     cur_oop = oop(cur);
  70     oop_size = cur_oop->size();
  71     next_obj = cur + oop_size;
  72   }
  73   return cur;


 209 }
 210 
 211 void HeapRegion::hr_clear(bool par, bool clear_space) {
 212   assert(_humongous_type == NotHumongous,
 213          "we should have already filtered out humongous regions");
 214   assert(_humongous_start_region == NULL,
 215          "we should have already filtered out humongous regions");
 216   assert(_end == _orig_end,
 217          "we should have already filtered out humongous regions");
 218 
 219   _in_collection_set = false;
 220 
 221   set_young_index_in_cset(-1);
 222   uninstall_surv_rate_group();
 223   set_young_type(NotYoung);
 224   reset_pre_dummy_top();
 225 
 226   if (!par) {
 227     // If this is parallel, this will be done later.
 228     HeapRegionRemSet* hrrs = rem_set();
 229     hrrs->clear();
 230     _claimed = InitialClaimValue;
 231   }
 232   zero_marked_bytes();
 233 
 234   _offsets.resize(HeapRegion::GrainWords);
 235   init_top_at_mark_start();
 236   if (clear_space) clear(SpaceDecorator::Mangle);
 237 }
 238 
 239 void HeapRegion::par_clear() {
 240   assert(used() == 0, "the region should have been already cleared");
 241   assert(capacity() == HeapRegion::GrainBytes, "should be back to normal");
 242   HeapRegionRemSet* hrrs = rem_set();
 243   hrrs->clear();
 244   CardTableModRefBS* ct_bs =
 245                    (CardTableModRefBS*)G1CollectedHeap::heap()->barrier_set();
 246   ct_bs->clear(MemRegion(bottom(), end()));
 247 }
 248 
 249 void HeapRegion::calc_gc_efficiency() {


 346 
 347 HeapRegion::HeapRegion(uint hrs_index,
 348                        G1BlockOffsetSharedArray* sharedOffsetArray,
 349                        MemRegion mr) :
 350     G1OffsetTableContigSpace(sharedOffsetArray, mr),
 351     _hrs_index(hrs_index),
 352     _humongous_type(NotHumongous), _humongous_start_region(NULL),
 353     _in_collection_set(false),
 354     _next_in_special_set(NULL), _orig_end(NULL),
 355     _claimed(InitialClaimValue), _evacuation_failed(false),
 356     _prev_marked_bytes(0), _next_marked_bytes(0), _gc_efficiency(0.0),
 357     _young_type(NotYoung), _next_young_region(NULL),
 358     _next_dirty_cards_region(NULL), _next(NULL), _pending_removal(false),
 359 #ifdef ASSERT
 360     _containing_set(NULL),
 361 #endif // ASSERT
 362      _young_index_in_cset(-1), _surv_rate_group(NULL), _age_index(-1),
 363     _rem_set(NULL), _recorded_rs_length(0), _predicted_elapsed_time_ms(0),
 364     _predicted_bytes_to_copy(0)
 365 {
 366   _rem_set = new HeapRegionRemSet(sharedOffsetArray, this);
 367   _orig_end = mr.end();
 368   // Note that initialize() will set the start of the unmarked area of the
 369   // region.
 370   hr_clear(false /*par*/, false /*clear_space*/);
 371   set_top(bottom());
 372   set_saved_mark();
 373 


 374   assert(HeapRegionRemSet::num_par_rem_sets() > 0, "Invariant.");
 375 }
 376 
 377 CompactibleSpace* HeapRegion::next_compaction_space() const {
 378   // We're not using an iterator given that it will wrap around when
 379   // it reaches the last region and this is not what we want here.
 380   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 381   uint index = hrs_index() + 1;
 382   while (index < g1h->n_regions()) {
 383     HeapRegion* hr = g1h->region_at(index);
 384     if (!hr->isHumongous()) {
 385       return hr;
 386     }
 387     index += 1;
 388   }
 389   return NULL;
 390 }
 391 
 392 void HeapRegion::save_marks() {
 393   set_saved_mark();


 573     // Otherwise:
 574     next = (cur + obj->size());
 575 
 576     if (!g1h->is_obj_dead(obj)) {
 577       if (next < end || !obj->is_objArray()) {
 578         // This object either does not span the MemRegion
 579         // boundary, or if it does it's not an array.
 580         // Apply closure to whole object.
 581         obj->oop_iterate(cl);
 582       } else {
 583         // This obj is an array that spans the boundary.
 584         // Stop at the boundary.
 585         obj->oop_iterate(cl, mr);
 586       }
 587     }
 588     cur = next;
 589   }
 590   return NULL;
 591 }
 592 
 593 // Code roots support
 594 
 595 void HeapRegion::add_strong_code_root(nmethod* nm) {
 596   HeapRegionRemSet* hrrs = rem_set();
 597   hrrs->add_strong_code_root(nm);
 598 }
 599 
 600 void HeapRegion::remove_strong_code_root(nmethod* nm) {
 601   HeapRegionRemSet* hrrs = rem_set();
 602   hrrs->remove_strong_code_root(nm);
 603 }
 604 
 605 void HeapRegion::migrate_strong_code_roots() {
 606   assert(in_collection_set(), "only collection set regions");
 607   assert(!isHumongous(), "not humongous regions");
 608 
 609   HeapRegionRemSet* hrrs = rem_set();
 610   hrrs->migrate_strong_code_roots();
 611 }
 612 
 613 void HeapRegion::strong_code_roots_do(CodeBlobClosure* blk) const {
 614   HeapRegionRemSet* hrrs = rem_set();
 615   hrrs->strong_code_roots_do(blk);
 616 }
 617 
 618 class VerifyStrongCodeRootOopClosure: public OopClosure {
 619   const HeapRegion* _hr;
 620   nmethod* _nm;
 621   bool _failures;
 622   bool _has_oops_in_region;
 623 
 624   template <class T> void do_oop_work(T* p) {
 625     T heap_oop = oopDesc::load_heap_oop(p);
 626     if (!oopDesc::is_null(heap_oop)) {
 627       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
 628 
 629       // Note: not all the oops embedded in the nmethod are in the
 630       // current region. We only look at those which are.
 631       if (_hr->is_in(obj)) {
 632         // Object is in the region. Check that its less than top
 633         if (_hr->top() <= (HeapWord*)obj) {
 634           // Object is above top
 635           gclog_or_tty->print_cr("Object "PTR_FORMAT" in region "
 636                                  "["PTR_FORMAT", "PTR_FORMAT") is above "
 637                                  "top "PTR_FORMAT,
 638                                  obj, _hr->bottom(), _hr->end(), _hr->top());
 639           _failures = true;
 640           return;
 641         }
 642         // Nmethod has at least one oop in the current region
 643         _has_oops_in_region = true;
 644       }
 645     }
 646   }
 647 
 648 public:
 649   VerifyStrongCodeRootOopClosure(const HeapRegion* hr, nmethod* nm):
 650     _hr(hr), _failures(false), _has_oops_in_region(false) {}
 651 
 652   void do_oop(narrowOop* p) { do_oop_work(p); }
 653   void do_oop(oop* p)       { do_oop_work(p); }
 654 
 655   bool failures()           { return _failures; }
 656   bool has_oops_in_region() { return _has_oops_in_region; }
 657 };
 658 
 659 class VerifyStrongCodeRootCodeBlobClosure: public CodeBlobClosure {
 660   const HeapRegion* _hr;
 661   bool _failures;
 662 public:
 663   VerifyStrongCodeRootCodeBlobClosure(const HeapRegion* hr) :
 664     _hr(hr), _failures(false) {}
 665 
 666   void do_code_blob(CodeBlob* cb) {
 667     nmethod* nm = (cb == NULL) ? NULL : cb->as_nmethod_or_null();
 668     if (nm != NULL) {
 669       // Verify that the nemthod is live
 670       if (!nm->is_alive()) {
 671         gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] has dead nmethod "
 672                                PTR_FORMAT" in its strong code roots",
 673                                _hr->bottom(), _hr->end(), nm);
 674         _failures = true;
 675       } else {
 676         VerifyStrongCodeRootOopClosure oop_cl(_hr, nm);
 677         nm->oops_do(&oop_cl);
 678         if (!oop_cl.has_oops_in_region()) {
 679           gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] has nmethod "
 680                                  PTR_FORMAT" in its strong code roots "
 681                                  "with no pointers into region",
 682                                  _hr->bottom(), _hr->end(), nm);
 683           _failures = true;
 684         } else if (oop_cl.failures()) {
 685           gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] has other "
 686                                  "failures for nmethod "PTR_FORMAT,
 687                                  _hr->bottom(), _hr->end(), nm);
 688           _failures = true;
 689         }
 690       }
 691     }
 692   }
 693 
 694   bool failures()       { return _failures; }
 695 };
 696 
 697 void HeapRegion::verify_strong_code_roots(VerifyOption vo, bool* failures) const {
 698   if (!G1VerifyHeapRegionCodeRoots) {
 699     // We're not verifying code roots.
 700     return;
 701   }
 702   if (vo == VerifyOption_G1UseMarkWord) {
 703     // Marking verification during a full GC is performed after class
 704     // unloading, code cache unloading, etc so the strong code roots
 705     // attached to each heap region are in an inconsistent state. They won't
 706     // be consistent until the strong code roots are rebuilt after the
 707     // actual GC. Skip verifying the strong code roots in this particular
 708     // time.
 709     assert(VerifyDuringGC, "only way to get here");
 710     return;
 711   }
 712 
 713   HeapRegionRemSet* hrrs = rem_set();
 714   int strong_code_roots_length = hrrs->strong_code_roots_list_length();
 715 
 716   // if this region is empty then there should be no entries
 717   // on its strong code root list
 718   if (is_empty()) {
 719     if (strong_code_roots_length > 0) {
 720       gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] is empty "
 721                              "but has "INT32_FORMAT" code root entries",
 722                              bottom(), end(), strong_code_roots_length);
 723       *failures = true;
 724     }
 725     return;
 726   }
 727 
 728   // An H-region should have an empty strong code root list
 729   if (isHumongous()) {
 730     if (strong_code_roots_length > 0) {
 731       gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] is humongous "
 732                              "but has "INT32_FORMAT" code root entries",
 733                              bottom(), end(), strong_code_roots_length);
 734       *failures = true;
 735     }
 736     return;
 737   }
 738 
 739   VerifyStrongCodeRootCodeBlobClosure cb_cl(this);
 740   strong_code_roots_do(&cb_cl);
 741 
 742   if (cb_cl.failures()) {
 743     *failures = true;
 744   }
 745 }
 746 
 747 void HeapRegion::print() const { print_on(gclog_or_tty); }
 748 void HeapRegion::print_on(outputStream* st) const {
 749   if (isHumongous()) {
 750     if (startsHumongous())
 751       st->print(" HS");
 752     else
 753       st->print(" HC");
 754   } else {
 755     st->print("   ");
 756   }
 757   if (in_collection_set())
 758     st->print(" CS");
 759   else
 760     st->print("   ");
 761   if (is_young())
 762     st->print(is_survivor() ? " SU" : " Y ");
 763   else
 764     st->print("   ");
 765   if (is_empty())
 766     st->print(" F");
 767   else
 768     st->print("  ");
 769   st->print(" TS %5d", _gc_time_stamp);
 770   st->print(" PTAMS "PTR_FORMAT" NTAMS "PTR_FORMAT,
 771             prev_top_at_mark_start(), next_top_at_mark_start());
 772   G1OffsetTableContigSpace::print_on(st);
 773 }
 774 
 775 class VerifyLiveClosure: public OopClosure {
 776 private:
 777   G1CollectedHeap* _g1h;
 778   CardTableModRefBS* _bs;
 779   oop _containing_obj;
 780   bool _failures;
 781   int _n_failures;
 782   VerifyOption _vo;
 783 public:
 784   // _vo == UsePrevMarking -> use "prev" marking information,
 785   // _vo == UseNextMarking -> use "next" marking information,
 786   // _vo == UseMarkWord    -> use mark word from object header.
 787   VerifyLiveClosure(G1CollectedHeap* g1h, VerifyOption vo) :
 788     _g1h(g1h), _bs(NULL), _containing_obj(NULL),
 789     _failures(false), _n_failures(0), _vo(vo)
 790   {
 791     BarrierSet* bs = _g1h->barrier_set();
 792     if (bs->is_a(BarrierSet::CardTableModRef))
 793       _bs = (CardTableModRefBS*)bs;
 794   }
 795 
 796   void set_containing_obj(oop obj) {
 797     _containing_obj = obj;
 798   }
 799 
 800   bool failures() { return _failures; }
 801   int n_failures() { return _n_failures; }
 802 
 803   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 804   virtual void do_oop(      oop* p) { do_oop_work(p); }
 805 
 806   void print_object(outputStream* out, oop obj) {
 807 #ifdef PRODUCT
 808     Klass* k = obj->klass();
 809     const char* class_name = InstanceKlass::cast(k)->external_name();
 810     out->print_cr("class name %s", class_name);
 811 #else // PRODUCT
 812     obj->print_on(out);
 813 #endif // PRODUCT
 814   }
 815 
 816   template <class T>
 817   void do_oop_work(T* p) {
 818     assert(_containing_obj != NULL, "Precondition");
 819     assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo),
 820            "Precondition");
 821     T heap_oop = oopDesc::load_heap_oop(p);
 822     if (!oopDesc::is_null(heap_oop)) {
 823       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
 824       bool failed = false;
 825       if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead_cond(obj, _vo)) {
 826         MutexLockerEx x(ParGCRareEvent_lock,
 827                         Mutex::_no_safepoint_check_flag);
 828 
 829         if (!_failures) {
 830           gclog_or_tty->print_cr("");
 831           gclog_or_tty->print_cr("----------");
 832         }
 833         if (!_g1h->is_in_closed_subset(obj)) {
 834           HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 835           gclog_or_tty->print_cr("Field "PTR_FORMAT
 836                                  " of live obj "PTR_FORMAT" in region "
 837                                  "["PTR_FORMAT", "PTR_FORMAT")",
 838                                  p, (void*) _containing_obj,
 839                                  from->bottom(), from->end());
 840           print_object(gclog_or_tty, _containing_obj);
 841           gclog_or_tty->print_cr("points to obj "PTR_FORMAT" not in the heap",
 842                                  (void*) obj);
 843         } else {
 844           HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 845           HeapRegion* to   = _g1h->heap_region_containing((HeapWord*)obj);
 846           gclog_or_tty->print_cr("Field "PTR_FORMAT
 847                                  " of live obj "PTR_FORMAT" in region "
 848                                  "["PTR_FORMAT", "PTR_FORMAT")",
 849                                  p, (void*) _containing_obj,
 850                                  from->bottom(), from->end());
 851           print_object(gclog_or_tty, _containing_obj);
 852           gclog_or_tty->print_cr("points to dead obj "PTR_FORMAT" in region "
 853                                  "["PTR_FORMAT", "PTR_FORMAT")",
 854                                  (void*) obj, to->bottom(), to->end());
 855           print_object(gclog_or_tty, obj);
 856         }
 857         gclog_or_tty->print_cr("----------");
 858         gclog_or_tty->flush();
 859         _failures = true;
 860         failed = true;
 861         _n_failures++;
 862       }
 863 
 864       if (!_g1h->full_collection() || G1VerifyRSetsDuringFullGC) {
 865         HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 866         HeapRegion* to   = _g1h->heap_region_containing(obj);
 867         if (from != NULL && to != NULL &&
 868             from != to &&
 869             !to->isHumongous()) {
 870           jbyte cv_obj = *_bs->byte_for_const(_containing_obj);
 871           jbyte cv_field = *_bs->byte_for_const(p);
 872           const jbyte dirty = CardTableModRefBS::dirty_card_val();
 873 
 874           bool is_bad = !(from->is_young()
 875                           || to->rem_set()->contains_reference(p)
 876                           || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed
 877                               (_containing_obj->is_objArray() ?
 878                                   cv_field == dirty
 879                                : cv_obj == dirty || cv_field == dirty));
 880           if (is_bad) {
 881             MutexLockerEx x(ParGCRareEvent_lock,
 882                             Mutex::_no_safepoint_check_flag);
 883 
 884             if (!_failures) {
 885               gclog_or_tty->print_cr("");
 886               gclog_or_tty->print_cr("----------");
 887             }
 888             gclog_or_tty->print_cr("Missing rem set entry:");
 889             gclog_or_tty->print_cr("Field "PTR_FORMAT" "
 890                                    "of obj "PTR_FORMAT", "
 891                                    "in region "HR_FORMAT,
 892                                    p, (void*) _containing_obj,
 893                                    HR_FORMAT_PARAMS(from));
 894             _containing_obj->print_on(gclog_or_tty);
 895             gclog_or_tty->print_cr("points to obj "PTR_FORMAT" "
 896                                    "in region "HR_FORMAT,
 897                                    (void*) obj,
 898                                    HR_FORMAT_PARAMS(to));
 899             obj->print_on(gclog_or_tty);
 900             gclog_or_tty->print_cr("Obj head CTE = %d, field CTE = %d.",
 901                           cv_obj, cv_field);
 902             gclog_or_tty->print_cr("----------");
 903             gclog_or_tty->flush();
 904             _failures = true;
 905             if (!failed) _n_failures++;
 906           }
 907         }
 908       }
 909     }
 910   }
 911 };
 912 
 913 // This really ought to be commoned up into OffsetTableContigSpace somehow.
 914 // We would need a mechanism to make that code skip dead objects.
 915 
 916 void HeapRegion::verify(VerifyOption vo,
 917                         bool* failures) const {
 918   G1CollectedHeap* g1 = G1CollectedHeap::heap();
 919   *failures = false;
 920   HeapWord* p = bottom();
 921   HeapWord* prev_p = NULL;
 922   VerifyLiveClosure vl_cl(g1, vo);
 923   bool is_humongous = isHumongous();
 924   bool do_bot_verify = !is_young();
 925   size_t object_num = 0;
 926   while (p < top()) {
 927     oop obj = oop(p);
 928     size_t obj_size = obj->size();
 929     object_num += 1;
 930 
 931     if (is_humongous != g1->isHumongous(obj_size)) {


1031 
1032     // Loook up end - 1
1033     HeapWord* addr_4 = the_end - 1;
1034     HeapWord* b_start_4 = _offsets.block_start_const(addr_4);
1035     if (b_start_4 != p) {
1036       gclog_or_tty->print_cr("BOT look up for end - 1: "PTR_FORMAT" "
1037                              " yielded "PTR_FORMAT", expecting "PTR_FORMAT,
1038                              addr_4, b_start_4, p);
1039       *failures = true;
1040       return;
1041     }
1042   }
1043 
1044   if (is_humongous && object_num > 1) {
1045     gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] is humongous "
1046                            "but has "SIZE_FORMAT", objects",
1047                            bottom(), end(), object_num);
1048     *failures = true;
1049     return;
1050   }
1051 
1052   verify_strong_code_roots(vo, failures);
1053 }
1054 
1055 void HeapRegion::verify() const {
1056   bool dummy = false;
1057   verify(VerifyOption_G1UsePrevMarking, /* failures */ &dummy);
1058 }
1059 
1060 // G1OffsetTableContigSpace code; copied from space.cpp.  Hope this can go
1061 // away eventually.
1062 
1063 void G1OffsetTableContigSpace::clear(bool mangle_space) {
1064   ContiguousSpace::clear(mangle_space);
1065   _offsets.zero_bottom_entry();
1066   _offsets.initialize_threshold();
1067 }
1068 
1069 void G1OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) {
1070   Space::set_bottom(new_bottom);
1071   _offsets.set_bottom(new_bottom);
1072 }
1073 
1074 void G1OffsetTableContigSpace::set_end(HeapWord* new_end) {
1075   Space::set_end(new_end);
1076   _offsets.resize(new_end - bottom());
1077 }