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

Thu, 30 Jul 2009 16:22:58 -0400

author
tonyp
date
Thu, 30 Jul 2009 16:22:58 -0400
changeset 1377
2c79770d1f6e
parent 1301
18f526145aea
child 1455
ff2402f6a50b
permissions
-rw-r--r--

6819085: G1: use larger and/or user settable region size
Summary: Instead of the region size being hard-coded, allow the user to set it.
Reviewed-by: jmasa, johnc, apetrusenko

     1 /*
     2  * Copyright 2001-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 #include "incls/_precompiled.incl"
    26 #include "incls/_heapRegion.cpp.incl"
    28 int HeapRegion::LogOfHRGrainBytes = 0;
    29 int HeapRegion::LogOfHRGrainWords = 0;
    30 int HeapRegion::GrainBytes        = 0;
    31 int HeapRegion::GrainWords        = 0;
    32 int HeapRegion::CardsPerRegion    = 0;
    34 HeapRegionDCTOC::HeapRegionDCTOC(G1CollectedHeap* g1,
    35                                  HeapRegion* hr, OopClosure* cl,
    36                                  CardTableModRefBS::PrecisionStyle precision,
    37                                  FilterKind fk) :
    38   ContiguousSpaceDCTOC(hr, cl, precision, NULL),
    39   _hr(hr), _fk(fk), _g1(g1)
    40 {}
    42 FilterOutOfRegionClosure::FilterOutOfRegionClosure(HeapRegion* r,
    43                                                    OopClosure* oc) :
    44   _r_bottom(r->bottom()), _r_end(r->end()),
    45   _oc(oc), _out_of_region(0)
    46 {}
    48 class VerifyLiveClosure: public OopClosure {
    49 private:
    50   G1CollectedHeap* _g1h;
    51   CardTableModRefBS* _bs;
    52   oop _containing_obj;
    53   bool _failures;
    54   int _n_failures;
    55   bool _use_prev_marking;
    56 public:
    57   // use_prev_marking == true  -> use "prev" marking information,
    58   // use_prev_marking == false -> use "next" marking information
    59   VerifyLiveClosure(G1CollectedHeap* g1h, bool use_prev_marking) :
    60     _g1h(g1h), _bs(NULL), _containing_obj(NULL),
    61     _failures(false), _n_failures(0), _use_prev_marking(use_prev_marking)
    62   {
    63     BarrierSet* bs = _g1h->barrier_set();
    64     if (bs->is_a(BarrierSet::CardTableModRef))
    65       _bs = (CardTableModRefBS*)bs;
    66   }
    68   void set_containing_obj(oop obj) {
    69     _containing_obj = obj;
    70   }
    72   bool failures() { return _failures; }
    73   int n_failures() { return _n_failures; }
    75   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
    76   virtual void do_oop(      oop* p) { do_oop_work(p); }
    78   template <class T> void do_oop_work(T* p) {
    79     assert(_containing_obj != NULL, "Precondition");
    80     assert(!_g1h->is_obj_dead_cond(_containing_obj, _use_prev_marking),
    81            "Precondition");
    82     T heap_oop = oopDesc::load_heap_oop(p);
    83     if (!oopDesc::is_null(heap_oop)) {
    84       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
    85       bool failed = false;
    86       if (!_g1h->is_in_closed_subset(obj) ||
    87           _g1h->is_obj_dead_cond(obj, _use_prev_marking)) {
    88         if (!_failures) {
    89           gclog_or_tty->print_cr("");
    90           gclog_or_tty->print_cr("----------");
    91         }
    92         if (!_g1h->is_in_closed_subset(obj)) {
    93           gclog_or_tty->print_cr("Field "PTR_FORMAT
    94                         " of live obj "PTR_FORMAT
    95                         " points to obj "PTR_FORMAT
    96                         " not in the heap.",
    97                         p, (void*) _containing_obj, (void*) obj);
    98         } else {
    99           gclog_or_tty->print_cr("Field "PTR_FORMAT
   100                         " of live obj "PTR_FORMAT
   101                         " points to dead obj "PTR_FORMAT".",
   102                         p, (void*) _containing_obj, (void*) obj);
   103         }
   104         gclog_or_tty->print_cr("Live obj:");
   105         _containing_obj->print_on(gclog_or_tty);
   106         gclog_or_tty->print_cr("Bad referent:");
   107         obj->print_on(gclog_or_tty);
   108         gclog_or_tty->print_cr("----------");
   109         _failures = true;
   110         failed = true;
   111         _n_failures++;
   112       }
   114       if (!_g1h->full_collection()) {
   115         HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
   116         HeapRegion* to   = _g1h->heap_region_containing(obj);
   117         if (from != NULL && to != NULL &&
   118             from != to &&
   119             !to->isHumongous()) {
   120           jbyte cv_obj = *_bs->byte_for_const(_containing_obj);
   121           jbyte cv_field = *_bs->byte_for_const(p);
   122           const jbyte dirty = CardTableModRefBS::dirty_card_val();
   124           bool is_bad = !(from->is_young()
   125                           || to->rem_set()->contains_reference(p)
   126                           || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed
   127                               (_containing_obj->is_objArray() ?
   128                                   cv_field == dirty
   129                                : cv_obj == dirty || cv_field == dirty));
   130           if (is_bad) {
   131             if (!_failures) {
   132               gclog_or_tty->print_cr("");
   133               gclog_or_tty->print_cr("----------");
   134             }
   135             gclog_or_tty->print_cr("Missing rem set entry:");
   136             gclog_or_tty->print_cr("Field "PTR_FORMAT
   137                           " of obj "PTR_FORMAT
   138                           ", in region %d ["PTR_FORMAT
   139                           ", "PTR_FORMAT"),",
   140                           p, (void*) _containing_obj,
   141                           from->hrs_index(),
   142                           from->bottom(),
   143                           from->end());
   144             _containing_obj->print_on(gclog_or_tty);
   145             gclog_or_tty->print_cr("points to obj "PTR_FORMAT
   146                           " in region %d ["PTR_FORMAT
   147                           ", "PTR_FORMAT").",
   148                           (void*) obj, to->hrs_index(),
   149                           to->bottom(), to->end());
   150             obj->print_on(gclog_or_tty);
   151             gclog_or_tty->print_cr("Obj head CTE = %d, field CTE = %d.",
   152                           cv_obj, cv_field);
   153             gclog_or_tty->print_cr("----------");
   154             _failures = true;
   155             if (!failed) _n_failures++;
   156           }
   157         }
   158       }
   159     }
   160   }
   161 };
   163 template<class ClosureType>
   164 HeapWord* walk_mem_region_loop(ClosureType* cl, G1CollectedHeap* g1h,
   165                                HeapRegion* hr,
   166                                HeapWord* cur, HeapWord* top) {
   167   oop cur_oop = oop(cur);
   168   int oop_size = cur_oop->size();
   169   HeapWord* next_obj = cur + oop_size;
   170   while (next_obj < top) {
   171     // Keep filtering the remembered set.
   172     if (!g1h->is_obj_dead(cur_oop, hr)) {
   173       // Bottom lies entirely below top, so we can call the
   174       // non-memRegion version of oop_iterate below.
   175       cur_oop->oop_iterate(cl);
   176     }
   177     cur = next_obj;
   178     cur_oop = oop(cur);
   179     oop_size = cur_oop->size();
   180     next_obj = cur + oop_size;
   181   }
   182   return cur;
   183 }
   185 void HeapRegionDCTOC::walk_mem_region_with_cl(MemRegion mr,
   186                                               HeapWord* bottom,
   187                                               HeapWord* top,
   188                                               OopClosure* cl) {
   189   G1CollectedHeap* g1h = _g1;
   191   int oop_size;
   193   OopClosure* cl2 = cl;
   194   FilterIntoCSClosure intoCSFilt(this, g1h, cl);
   195   FilterOutOfRegionClosure outOfRegionFilt(_hr, cl);
   196   switch (_fk) {
   197   case IntoCSFilterKind:      cl2 = &intoCSFilt; break;
   198   case OutOfRegionFilterKind: cl2 = &outOfRegionFilt; break;
   199   }
   201   // Start filtering what we add to the remembered set. If the object is
   202   // not considered dead, either because it is marked (in the mark bitmap)
   203   // or it was allocated after marking finished, then we add it. Otherwise
   204   // we can safely ignore the object.
   205   if (!g1h->is_obj_dead(oop(bottom), _hr)) {
   206     oop_size = oop(bottom)->oop_iterate(cl2, mr);
   207   } else {
   208     oop_size = oop(bottom)->size();
   209   }
   211   bottom += oop_size;
   213   if (bottom < top) {
   214     // We replicate the loop below for several kinds of possible filters.
   215     switch (_fk) {
   216     case NoFilterKind:
   217       bottom = walk_mem_region_loop(cl, g1h, _hr, bottom, top);
   218       break;
   219     case IntoCSFilterKind: {
   220       FilterIntoCSClosure filt(this, g1h, cl);
   221       bottom = walk_mem_region_loop(&filt, g1h, _hr, bottom, top);
   222       break;
   223     }
   224     case OutOfRegionFilterKind: {
   225       FilterOutOfRegionClosure filt(_hr, cl);
   226       bottom = walk_mem_region_loop(&filt, g1h, _hr, bottom, top);
   227       break;
   228     }
   229     default:
   230       ShouldNotReachHere();
   231     }
   233     // Last object. Need to do dead-obj filtering here too.
   234     if (!g1h->is_obj_dead(oop(bottom), _hr)) {
   235       oop(bottom)->oop_iterate(cl2, mr);
   236     }
   237   }
   238 }
   240 // Minimum region size; we won't go lower than that.
   241 // We might want to decrease this in the future, to deal with small
   242 // heaps a bit more efficiently.
   243 #define MIN_REGION_SIZE  (      1024 * 1024 )
   245 // Maximum region size; we don't go higher than that. There's a good
   246 // reason for having an upper bound. We don't want regions to get too
   247 // large, otherwise cleanup's effectiveness would decrease as there
   248 // will be fewer opportunities to find totally empty regions after
   249 // marking.
   250 #define MAX_REGION_SIZE  ( 32 * 1024 * 1024 )
   252 // The automatic region size calculation will try to have around this
   253 // many regions in the heap (based on the min heap size).
   254 #define TARGET_REGION_NUMBER          2048
   256 void HeapRegion::setup_heap_region_size(uintx min_heap_size) {
   257   // region_size in bytes
   258   uintx region_size = G1HeapRegionSize;
   259   if (FLAG_IS_DEFAULT(G1HeapRegionSize)) {
   260     // We base the automatic calculation on the min heap size. This
   261     // can be problematic if the spread between min and max is quite
   262     // wide, imagine -Xms128m -Xmx32g. But, if we decided it based on
   263     // the max size, the region size might be way too large for the
   264     // min size. Either way, some users might have to set the region
   265     // size manually for some -Xms / -Xmx combos.
   267     region_size = MAX2(min_heap_size / TARGET_REGION_NUMBER,
   268                        (uintx) MIN_REGION_SIZE);
   269   }
   271   int region_size_log = log2_long((jlong) region_size);
   272   // Recalculate the region size to make sure it's a power of
   273   // 2. This means that region_size is the largest power of 2 that's
   274   // <= what we've calculated so far.
   275   region_size = 1 << region_size_log;
   277   // Now make sure that we don't go over or under our limits.
   278   if (region_size < MIN_REGION_SIZE) {
   279     region_size = MIN_REGION_SIZE;
   280   } else if (region_size > MAX_REGION_SIZE) {
   281     region_size = MAX_REGION_SIZE;
   282   }
   284   // And recalculate the log.
   285   region_size_log = log2_long((jlong) region_size);
   287   // Now, set up the globals.
   288   guarantee(LogOfHRGrainBytes == 0, "we should only set it once");
   289   LogOfHRGrainBytes = region_size_log;
   291   guarantee(LogOfHRGrainWords == 0, "we should only set it once");
   292   LogOfHRGrainWords = LogOfHRGrainBytes - LogHeapWordSize;
   294   guarantee(GrainBytes == 0, "we should only set it once");
   295   // The cast to int is safe, given that we've bounded region_size by
   296   // MIN_REGION_SIZE and MAX_REGION_SIZE.
   297   GrainBytes = (int) region_size;
   299   guarantee(GrainWords == 0, "we should only set it once");
   300   GrainWords = GrainBytes >> LogHeapWordSize;
   301   guarantee(1 << LogOfHRGrainWords == GrainWords, "sanity");
   303   guarantee(CardsPerRegion == 0, "we should only set it once");
   304   CardsPerRegion = GrainBytes >> CardTableModRefBS::card_shift;
   305 }
   307 void HeapRegion::reset_after_compaction() {
   308   G1OffsetTableContigSpace::reset_after_compaction();
   309   // After a compaction the mark bitmap is invalid, so we must
   310   // treat all objects as being inside the unmarked area.
   311   zero_marked_bytes();
   312   init_top_at_mark_start();
   313 }
   315 DirtyCardToOopClosure*
   316 HeapRegion::new_dcto_closure(OopClosure* cl,
   317                              CardTableModRefBS::PrecisionStyle precision,
   318                              HeapRegionDCTOC::FilterKind fk) {
   319   return new HeapRegionDCTOC(G1CollectedHeap::heap(),
   320                              this, cl, precision, fk);
   321 }
   323 void HeapRegion::hr_clear(bool par, bool clear_space) {
   324   _humongous_type = NotHumongous;
   325   _humongous_start_region = NULL;
   326   _in_collection_set = false;
   327   _is_gc_alloc_region = false;
   329   // Age stuff (if parallel, this will be done separately, since it needs
   330   // to be sequential).
   331   G1CollectedHeap* g1h = G1CollectedHeap::heap();
   333   set_young_index_in_cset(-1);
   334   uninstall_surv_rate_group();
   335   set_young_type(NotYoung);
   337   // In case it had been the start of a humongous sequence, reset its end.
   338   set_end(_orig_end);
   340   if (!par) {
   341     // If this is parallel, this will be done later.
   342     HeapRegionRemSet* hrrs = rem_set();
   343     if (hrrs != NULL) hrrs->clear();
   344     _claimed = InitialClaimValue;
   345   }
   346   zero_marked_bytes();
   347   set_sort_index(-1);
   349   _offsets.resize(HeapRegion::GrainWords);
   350   init_top_at_mark_start();
   351   if (clear_space) clear(SpaceDecorator::Mangle);
   352 }
   354 // <PREDICTION>
   355 void HeapRegion::calc_gc_efficiency() {
   356   G1CollectedHeap* g1h = G1CollectedHeap::heap();
   357   _gc_efficiency = (double) garbage_bytes() /
   358                             g1h->predict_region_elapsed_time_ms(this, false);
   359 }
   360 // </PREDICTION>
   362 void HeapRegion::set_startsHumongous() {
   363   _humongous_type = StartsHumongous;
   364   _humongous_start_region = this;
   365   assert(end() == _orig_end, "Should be normal before alloc.");
   366 }
   368 bool HeapRegion::claimHeapRegion(jint claimValue) {
   369   jint current = _claimed;
   370   if (current != claimValue) {
   371     jint res = Atomic::cmpxchg(claimValue, &_claimed, current);
   372     if (res == current) {
   373       return true;
   374     }
   375   }
   376   return false;
   377 }
   379 HeapWord* HeapRegion::next_block_start_careful(HeapWord* addr) {
   380   HeapWord* low = addr;
   381   HeapWord* high = end();
   382   while (low < high) {
   383     size_t diff = pointer_delta(high, low);
   384     // Must add one below to bias toward the high amount.  Otherwise, if
   385   // "high" were at the desired value, and "low" were one less, we
   386     // would not converge on "high".  This is not symmetric, because
   387     // we set "high" to a block start, which might be the right one,
   388     // which we don't do for "low".
   389     HeapWord* middle = low + (diff+1)/2;
   390     if (middle == high) return high;
   391     HeapWord* mid_bs = block_start_careful(middle);
   392     if (mid_bs < addr) {
   393       low = middle;
   394     } else {
   395       high = mid_bs;
   396     }
   397   }
   398   assert(low == high && low >= addr, "Didn't work.");
   399   return low;
   400 }
   402 void HeapRegion::set_next_on_unclean_list(HeapRegion* r) {
   403   assert(r == NULL || r->is_on_unclean_list(), "Malformed unclean list.");
   404   _next_in_special_set = r;
   405 }
   407 void HeapRegion::set_on_unclean_list(bool b) {
   408   _is_on_unclean_list = b;
   409 }
   411 void HeapRegion::initialize(MemRegion mr, bool clear_space, bool mangle_space) {
   412   G1OffsetTableContigSpace::initialize(mr, false, mangle_space);
   413   hr_clear(false/*par*/, clear_space);
   414 }
   415 #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
   416 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
   417 #endif // _MSC_VER
   420 HeapRegion::
   421 HeapRegion(G1BlockOffsetSharedArray* sharedOffsetArray,
   422                      MemRegion mr, bool is_zeroed)
   423   : G1OffsetTableContigSpace(sharedOffsetArray, mr, is_zeroed),
   424     _next_fk(HeapRegionDCTOC::NoFilterKind),
   425     _hrs_index(-1),
   426     _humongous_type(NotHumongous), _humongous_start_region(NULL),
   427     _in_collection_set(false), _is_gc_alloc_region(false),
   428     _is_on_free_list(false), _is_on_unclean_list(false),
   429     _next_in_special_set(NULL), _orig_end(NULL),
   430     _claimed(InitialClaimValue), _evacuation_failed(false),
   431     _prev_marked_bytes(0), _next_marked_bytes(0), _sort_index(-1),
   432     _young_type(NotYoung), _next_young_region(NULL),
   433     _next_dirty_cards_region(NULL),
   434     _young_index_in_cset(-1), _surv_rate_group(NULL), _age_index(-1),
   435     _rem_set(NULL), _zfs(NotZeroFilled)
   436 {
   437   _orig_end = mr.end();
   438   // Note that initialize() will set the start of the unmarked area of the
   439   // region.
   440   this->initialize(mr, !is_zeroed, SpaceDecorator::Mangle);
   441   set_top(bottom());
   442   set_saved_mark();
   444   _rem_set =  new HeapRegionRemSet(sharedOffsetArray, this);
   446   assert(HeapRegionRemSet::num_par_rem_sets() > 0, "Invariant.");
   447   // In case the region is allocated during a pause, note the top.
   448   // We haven't done any counting on a brand new region.
   449   _top_at_conc_mark_count = bottom();
   450 }
   452 class NextCompactionHeapRegionClosure: public HeapRegionClosure {
   453   const HeapRegion* _target;
   454   bool _target_seen;
   455   HeapRegion* _last;
   456   CompactibleSpace* _res;
   457 public:
   458   NextCompactionHeapRegionClosure(const HeapRegion* target) :
   459     _target(target), _target_seen(false), _res(NULL) {}
   460   bool doHeapRegion(HeapRegion* cur) {
   461     if (_target_seen) {
   462       if (!cur->isHumongous()) {
   463         _res = cur;
   464         return true;
   465       }
   466     } else if (cur == _target) {
   467       _target_seen = true;
   468     }
   469     return false;
   470   }
   471   CompactibleSpace* result() { return _res; }
   472 };
   474 CompactibleSpace* HeapRegion::next_compaction_space() const {
   475   G1CollectedHeap* g1h = G1CollectedHeap::heap();
   476   // cast away const-ness
   477   HeapRegion* r = (HeapRegion*) this;
   478   NextCompactionHeapRegionClosure blk(r);
   479   g1h->heap_region_iterate_from(r, &blk);
   480   return blk.result();
   481 }
   483 void HeapRegion::set_continuesHumongous(HeapRegion* start) {
   484   // The order is important here.
   485   start->add_continuingHumongousRegion(this);
   486   _humongous_type = ContinuesHumongous;
   487   _humongous_start_region = start;
   488 }
   490 void HeapRegion::add_continuingHumongousRegion(HeapRegion* cont) {
   491   // Must join the blocks of the current H region seq with the block of the
   492   // added region.
   493   offsets()->join_blocks(bottom(), cont->bottom());
   494   arrayOop obj = (arrayOop)(bottom());
   495   obj->set_length((int) (obj->length() + cont->capacity()/jintSize));
   496   set_end(cont->end());
   497   set_top(cont->end());
   498 }
   500 void HeapRegion::save_marks() {
   501   set_saved_mark();
   502 }
   504 void HeapRegion::oops_in_mr_iterate(MemRegion mr, OopClosure* cl) {
   505   HeapWord* p = mr.start();
   506   HeapWord* e = mr.end();
   507   oop obj;
   508   while (p < e) {
   509     obj = oop(p);
   510     p += obj->oop_iterate(cl);
   511   }
   512   assert(p == e, "bad memregion: doesn't end on obj boundary");
   513 }
   515 #define HeapRegion_OOP_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \
   516 void HeapRegion::oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl) { \
   517   ContiguousSpace::oop_since_save_marks_iterate##nv_suffix(cl);              \
   518 }
   519 SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(HeapRegion_OOP_SINCE_SAVE_MARKS_DEFN)
   522 void HeapRegion::oop_before_save_marks_iterate(OopClosure* cl) {
   523   oops_in_mr_iterate(MemRegion(bottom(), saved_mark_word()), cl);
   524 }
   526 #ifdef DEBUG
   527 HeapWord* HeapRegion::allocate(size_t size) {
   528   jint state = zero_fill_state();
   529   assert(!G1CollectedHeap::heap()->allocs_are_zero_filled() ||
   530          zero_fill_is_allocated(),
   531          "When ZF is on, only alloc in ZF'd regions");
   532   return G1OffsetTableContigSpace::allocate(size);
   533 }
   534 #endif
   536 void HeapRegion::set_zero_fill_state_work(ZeroFillState zfs) {
   537   assert(top() == bottom() || zfs == Allocated,
   538          "Region must be empty, or we must be setting it to allocated.");
   539   assert(ZF_mon->owned_by_self() ||
   540          Universe::heap()->is_gc_active(),
   541          "Must hold the lock or be a full GC to modify.");
   542   _zfs = zfs;
   543 }
   545 void HeapRegion::set_zero_fill_complete() {
   546   set_zero_fill_state_work(ZeroFilled);
   547   if (ZF_mon->owned_by_self()) {
   548     ZF_mon->notify_all();
   549   }
   550 }
   553 void HeapRegion::ensure_zero_filled() {
   554   MutexLockerEx x(ZF_mon, Mutex::_no_safepoint_check_flag);
   555   ensure_zero_filled_locked();
   556 }
   558 void HeapRegion::ensure_zero_filled_locked() {
   559   assert(ZF_mon->owned_by_self(), "Precondition");
   560   bool should_ignore_zf = SafepointSynchronize::is_at_safepoint();
   561   assert(should_ignore_zf || Heap_lock->is_locked(),
   562          "Either we're in a GC or we're allocating a region.");
   563   switch (zero_fill_state()) {
   564   case HeapRegion::NotZeroFilled:
   565     set_zero_fill_in_progress(Thread::current());
   566     {
   567       ZF_mon->unlock();
   568       Copy::fill_to_words(bottom(), capacity()/HeapWordSize);
   569       ZF_mon->lock_without_safepoint_check();
   570     }
   571     // A trap.
   572     guarantee(zero_fill_state() == HeapRegion::ZeroFilling
   573               && zero_filler() == Thread::current(),
   574               "AHA!  Tell Dave D if you see this...");
   575     set_zero_fill_complete();
   576     // gclog_or_tty->print_cr("Did sync ZF.");
   577     ConcurrentZFThread::note_sync_zfs();
   578     break;
   579   case HeapRegion::ZeroFilling:
   580     if (should_ignore_zf) {
   581       // We can "break" the lock and take over the work.
   582       Copy::fill_to_words(bottom(), capacity()/HeapWordSize);
   583       set_zero_fill_complete();
   584       ConcurrentZFThread::note_sync_zfs();
   585       break;
   586     } else {
   587       ConcurrentZFThread::wait_for_ZF_completed(this);
   588     }
   589   case HeapRegion::ZeroFilled:
   590     // Nothing to do.
   591     break;
   592   case HeapRegion::Allocated:
   593     guarantee(false, "Should not call on allocated regions.");
   594   }
   595   assert(zero_fill_state() == HeapRegion::ZeroFilled, "Post");
   596 }
   598 HeapWord*
   599 HeapRegion::object_iterate_mem_careful(MemRegion mr,
   600                                                  ObjectClosure* cl) {
   601   G1CollectedHeap* g1h = G1CollectedHeap::heap();
   602   // We used to use "block_start_careful" here.  But we're actually happy
   603   // to update the BOT while we do this...
   604   HeapWord* cur = block_start(mr.start());
   605   mr = mr.intersection(used_region());
   606   if (mr.is_empty()) return NULL;
   607   // Otherwise, find the obj that extends onto mr.start().
   609   assert(cur <= mr.start()
   610          && (oop(cur)->klass_or_null() == NULL ||
   611              cur + oop(cur)->size() > mr.start()),
   612          "postcondition of block_start");
   613   oop obj;
   614   while (cur < mr.end()) {
   615     obj = oop(cur);
   616     if (obj->klass_or_null() == NULL) {
   617       // Ran into an unparseable point.
   618       return cur;
   619     } else if (!g1h->is_obj_dead(obj)) {
   620       cl->do_object(obj);
   621     }
   622     if (cl->abort()) return cur;
   623     // The check above must occur before the operation below, since an
   624     // abort might invalidate the "size" operation.
   625     cur += obj->size();
   626   }
   627   return NULL;
   628 }
   630 HeapWord*
   631 HeapRegion::
   632 oops_on_card_seq_iterate_careful(MemRegion mr,
   633                                      FilterOutOfRegionClosure* cl) {
   634   G1CollectedHeap* g1h = G1CollectedHeap::heap();
   636   // If we're within a stop-world GC, then we might look at a card in a
   637   // GC alloc region that extends onto a GC LAB, which may not be
   638   // parseable.  Stop such at the "saved_mark" of the region.
   639   if (G1CollectedHeap::heap()->is_gc_active()) {
   640     mr = mr.intersection(used_region_at_save_marks());
   641   } else {
   642     mr = mr.intersection(used_region());
   643   }
   644   if (mr.is_empty()) return NULL;
   645   // Otherwise, find the obj that extends onto mr.start().
   647   // We used to use "block_start_careful" here.  But we're actually happy
   648   // to update the BOT while we do this...
   649   HeapWord* cur = block_start(mr.start());
   650   assert(cur <= mr.start(), "Postcondition");
   652   while (cur <= mr.start()) {
   653     if (oop(cur)->klass_or_null() == NULL) {
   654       // Ran into an unparseable point.
   655       return cur;
   656     }
   657     // Otherwise...
   658     int sz = oop(cur)->size();
   659     if (cur + sz > mr.start()) break;
   660     // Otherwise, go on.
   661     cur = cur + sz;
   662   }
   663   oop obj;
   664   obj = oop(cur);
   665   // If we finish this loop...
   666   assert(cur <= mr.start()
   667          && obj->klass_or_null() != NULL
   668          && cur + obj->size() > mr.start(),
   669          "Loop postcondition");
   670   if (!g1h->is_obj_dead(obj)) {
   671     obj->oop_iterate(cl, mr);
   672   }
   674   HeapWord* next;
   675   while (cur < mr.end()) {
   676     obj = oop(cur);
   677     if (obj->klass_or_null() == NULL) {
   678       // Ran into an unparseable point.
   679       return cur;
   680     };
   681     // Otherwise:
   682     next = (cur + obj->size());
   683     if (!g1h->is_obj_dead(obj)) {
   684       if (next < mr.end()) {
   685         obj->oop_iterate(cl);
   686       } else {
   687         // this obj spans the boundary.  If it's an array, stop at the
   688         // boundary.
   689         if (obj->is_objArray()) {
   690           obj->oop_iterate(cl, mr);
   691         } else {
   692           obj->oop_iterate(cl);
   693         }
   694       }
   695     }
   696     cur = next;
   697   }
   698   return NULL;
   699 }
   701 void HeapRegion::print() const { print_on(gclog_or_tty); }
   702 void HeapRegion::print_on(outputStream* st) const {
   703   if (isHumongous()) {
   704     if (startsHumongous())
   705       st->print(" HS");
   706     else
   707       st->print(" HC");
   708   } else {
   709     st->print("   ");
   710   }
   711   if (in_collection_set())
   712     st->print(" CS");
   713   else if (is_gc_alloc_region())
   714     st->print(" A ");
   715   else
   716     st->print("   ");
   717   if (is_young())
   718     st->print(is_scan_only() ? " SO" : (is_survivor() ? " SU" : " Y "));
   719   else
   720     st->print("   ");
   721   if (is_empty())
   722     st->print(" F");
   723   else
   724     st->print("  ");
   725   st->print(" %d", _gc_time_stamp);
   726   G1OffsetTableContigSpace::print_on(st);
   727 }
   729 void HeapRegion::verify(bool allow_dirty) const {
   730   verify(allow_dirty, /* use_prev_marking */ true);
   731 }
   733 #define OBJ_SAMPLE_INTERVAL 0
   734 #define BLOCK_SAMPLE_INTERVAL 100
   736 // This really ought to be commoned up into OffsetTableContigSpace somehow.
   737 // We would need a mechanism to make that code skip dead objects.
   739 void HeapRegion::verify(bool allow_dirty, bool use_prev_marking) const {
   740   G1CollectedHeap* g1 = G1CollectedHeap::heap();
   741   HeapWord* p = bottom();
   742   HeapWord* prev_p = NULL;
   743   int objs = 0;
   744   int blocks = 0;
   745   VerifyLiveClosure vl_cl(g1, use_prev_marking);
   746   while (p < top()) {
   747     size_t size = oop(p)->size();
   748     if (blocks == BLOCK_SAMPLE_INTERVAL) {
   749       guarantee(p == block_start_const(p + (size/2)),
   750                 "check offset computation");
   751       blocks = 0;
   752     } else {
   753       blocks++;
   754     }
   755     if (objs == OBJ_SAMPLE_INTERVAL) {
   756       oop obj = oop(p);
   757       if (!g1->is_obj_dead_cond(obj, this, use_prev_marking)) {
   758         obj->verify();
   759         vl_cl.set_containing_obj(obj);
   760         obj->oop_iterate(&vl_cl);
   761         if (G1MaxVerifyFailures >= 0
   762             && vl_cl.n_failures() >= G1MaxVerifyFailures) break;
   763       }
   764       objs = 0;
   765     } else {
   766       objs++;
   767     }
   768     prev_p = p;
   769     p += size;
   770   }
   771   HeapWord* rend = end();
   772   HeapWord* rtop = top();
   773   if (rtop < rend) {
   774     guarantee(block_start_const(rtop + (rend - rtop) / 2) == rtop,
   775               "check offset computation");
   776   }
   777   if (vl_cl.failures()) {
   778     gclog_or_tty->print_cr("Heap:");
   779     G1CollectedHeap::heap()->print_on(gclog_or_tty, true /* extended */);
   780     gclog_or_tty->print_cr("");
   781   }
   782   if (VerifyDuringGC &&
   783       G1VerifyConcMarkPrintReachable &&
   784       vl_cl.failures()) {
   785     g1->concurrent_mark()->print_prev_bitmap_reachable();
   786   }
   787   guarantee(!vl_cl.failures(), "region verification failed");
   788   guarantee(p == top(), "end of last object must match end of space");
   789 }
   791 // G1OffsetTableContigSpace code; copied from space.cpp.  Hope this can go
   792 // away eventually.
   794 void G1OffsetTableContigSpace::initialize(MemRegion mr, bool clear_space, bool mangle_space) {
   795   // false ==> we'll do the clearing if there's clearing to be done.
   796   ContiguousSpace::initialize(mr, false, mangle_space);
   797   _offsets.zero_bottom_entry();
   798   _offsets.initialize_threshold();
   799   if (clear_space) clear(mangle_space);
   800 }
   802 void G1OffsetTableContigSpace::clear(bool mangle_space) {
   803   ContiguousSpace::clear(mangle_space);
   804   _offsets.zero_bottom_entry();
   805   _offsets.initialize_threshold();
   806 }
   808 void G1OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) {
   809   Space::set_bottom(new_bottom);
   810   _offsets.set_bottom(new_bottom);
   811 }
   813 void G1OffsetTableContigSpace::set_end(HeapWord* new_end) {
   814   Space::set_end(new_end);
   815   _offsets.resize(new_end - bottom());
   816 }
   818 void G1OffsetTableContigSpace::print() const {
   819   print_short();
   820   gclog_or_tty->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", "
   821                 INTPTR_FORMAT ", " INTPTR_FORMAT ")",
   822                 bottom(), top(), _offsets.threshold(), end());
   823 }
   825 HeapWord* G1OffsetTableContigSpace::initialize_threshold() {
   826   return _offsets.initialize_threshold();
   827 }
   829 HeapWord* G1OffsetTableContigSpace::cross_threshold(HeapWord* start,
   830                                                     HeapWord* end) {
   831   _offsets.alloc_block(start, end);
   832   return _offsets.threshold();
   833 }
   835 HeapWord* G1OffsetTableContigSpace::saved_mark_word() const {
   836   G1CollectedHeap* g1h = G1CollectedHeap::heap();
   837   assert( _gc_time_stamp <= g1h->get_gc_time_stamp(), "invariant" );
   838   if (_gc_time_stamp < g1h->get_gc_time_stamp())
   839     return top();
   840   else
   841     return ContiguousSpace::saved_mark_word();
   842 }
   844 void G1OffsetTableContigSpace::set_saved_mark() {
   845   G1CollectedHeap* g1h = G1CollectedHeap::heap();
   846   unsigned curr_gc_time_stamp = g1h->get_gc_time_stamp();
   848   if (_gc_time_stamp < curr_gc_time_stamp) {
   849     // The order of these is important, as another thread might be
   850     // about to start scanning this region. If it does so after
   851     // set_saved_mark and before _gc_time_stamp = ..., then the latter
   852     // will be false, and it will pick up top() as the high water mark
   853     // of region. If it does so after _gc_time_stamp = ..., then it
   854     // will pick up the right saved_mark_word() as the high water mark
   855     // of the region. Either way, the behaviour will be correct.
   856     ContiguousSpace::set_saved_mark();
   857     OrderAccess::storestore();
   858     _gc_time_stamp = curr_gc_time_stamp;
   859     // The following fence is to force a flush of the writes above, but
   860     // is strictly not needed because when an allocating worker thread
   861     // calls set_saved_mark() it does so under the ParGCRareEvent_lock;
   862     // when the lock is released, the write will be flushed.
   863     // OrderAccess::fence();
   864   }
   865 }
   867 G1OffsetTableContigSpace::
   868 G1OffsetTableContigSpace(G1BlockOffsetSharedArray* sharedOffsetArray,
   869                          MemRegion mr, bool is_zeroed) :
   870   _offsets(sharedOffsetArray, mr),
   871   _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true),
   872   _gc_time_stamp(0)
   873 {
   874   _offsets.set_space(this);
   875   initialize(mr, !is_zeroed, SpaceDecorator::Mangle);
   876 }
   878 size_t RegionList::length() {
   879   size_t len = 0;
   880   HeapRegion* cur = hd();
   881   DEBUG_ONLY(HeapRegion* last = NULL);
   882   while (cur != NULL) {
   883     len++;
   884     DEBUG_ONLY(last = cur);
   885     cur = get_next(cur);
   886   }
   887   assert(last == tl(), "Invariant");
   888   return len;
   889 }
   891 void RegionList::insert_before_head(HeapRegion* r) {
   892   assert(well_formed(), "Inv");
   893   set_next(r, hd());
   894   _hd = r;
   895   _sz++;
   896   if (tl() == NULL) _tl = r;
   897   assert(well_formed(), "Inv");
   898 }
   900 void RegionList::prepend_list(RegionList* new_list) {
   901   assert(well_formed(), "Precondition");
   902   assert(new_list->well_formed(), "Precondition");
   903   HeapRegion* new_tl = new_list->tl();
   904   if (new_tl != NULL) {
   905     set_next(new_tl, hd());
   906     _hd = new_list->hd();
   907     _sz += new_list->sz();
   908     if (tl() == NULL) _tl = new_list->tl();
   909   } else {
   910     assert(new_list->hd() == NULL && new_list->sz() == 0, "Inv");
   911   }
   912   assert(well_formed(), "Inv");
   913 }
   915 void RegionList::delete_after(HeapRegion* r) {
   916   assert(well_formed(), "Precondition");
   917   HeapRegion* next = get_next(r);
   918   assert(r != NULL, "Precondition");
   919   HeapRegion* next_tl = get_next(next);
   920   set_next(r, next_tl);
   921   dec_sz();
   922   if (next == tl()) {
   923     assert(next_tl == NULL, "Inv");
   924     _tl = r;
   925   }
   926   assert(well_formed(), "Inv");
   927 }
   929 HeapRegion* RegionList::pop() {
   930   assert(well_formed(), "Inv");
   931   HeapRegion* res = hd();
   932   if (res != NULL) {
   933     _hd = get_next(res);
   934     _sz--;
   935     set_next(res, NULL);
   936     if (sz() == 0) _tl = NULL;
   937   }
   938   assert(well_formed(), "Inv");
   939   return res;
   940 }

mercurial