1 /*
   2  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_HPP
  27 
  28 #include "gc_implementation/g1/g1BlockOffsetTable.inline.hpp"
  29 #include "gc_implementation/g1/g1_specialized_oop_closures.hpp"
  30 #include "gc_implementation/g1/survRateGroup.hpp"
  31 #include "gc_implementation/shared/ageTable.hpp"
  32 #include "gc_implementation/shared/spaceDecorator.hpp"
  33 #include "memory/space.inline.hpp"
  34 #include "memory/watermark.hpp"
  35 
  36 #ifndef SERIALGC
  37 
  38 // A HeapRegion is the smallest piece of a G1CollectedHeap that
  39 // can be collected independently.
  40 
  41 // NOTE: Although a HeapRegion is a Space, its
  42 // Space::initDirtyCardClosure method must not be called.
  43 // The problem is that the existence of this method breaks
  44 // the independence of barrier sets from remembered sets.
  45 // The solution is to remove this method from the definition
  46 // of a Space.
  47 
  48 class CompactibleSpace;
  49 class ContiguousSpace;
  50 class HeapRegionRemSet;
  51 class HeapRegionRemSetIterator;
  52 class HeapRegion;
  53 
  54 // A dirty card to oop closure for heap regions. It
  55 // knows how to get the G1 heap and how to use the bitmap
  56 // in the concurrent marker used by G1 to filter remembered
  57 // sets.
  58 
  59 class HeapRegionDCTOC : public ContiguousSpaceDCTOC {
  60 public:
  61   // Specification of possible DirtyCardToOopClosure filtering.
  62   enum FilterKind {
  63     NoFilterKind,
  64     IntoCSFilterKind,
  65     OutOfRegionFilterKind
  66   };
  67 
  68 protected:
  69   HeapRegion* _hr;
  70   FilterKind _fk;
  71   G1CollectedHeap* _g1;
  72 
  73   void walk_mem_region_with_cl(MemRegion mr,
  74                                HeapWord* bottom, HeapWord* top,
  75                                OopClosure* cl);
  76 
  77   // We don't specialize this for FilteringClosure; filtering is handled by
  78   // the "FilterKind" mechanism.  But we provide this to avoid a compiler
  79   // warning.
  80   void walk_mem_region_with_cl(MemRegion mr,
  81                                HeapWord* bottom, HeapWord* top,
  82                                FilteringClosure* cl) {
  83     HeapRegionDCTOC::walk_mem_region_with_cl(mr, bottom, top,
  84                                                        (OopClosure*)cl);
  85   }
  86 
  87   // Get the actual top of the area on which the closure will
  88   // operate, given where the top is assumed to be (the end of the
  89   // memory region passed to do_MemRegion) and where the object
  90   // at the top is assumed to start. For example, an object may
  91   // start at the top but actually extend past the assumed top,
  92   // in which case the top becomes the end of the object.
  93   HeapWord* get_actual_top(HeapWord* top, HeapWord* top_obj) {
  94     return ContiguousSpaceDCTOC::get_actual_top(top, top_obj);
  95   }
  96 
  97   // Walk the given memory region from bottom to (actual) top
  98   // looking for objects and applying the oop closure (_cl) to
  99   // them. The base implementation of this treats the area as
 100   // blocks, where a block may or may not be an object. Sub-
 101   // classes should override this to provide more accurate
 102   // or possibly more efficient walking.
 103   void walk_mem_region(MemRegion mr, HeapWord* bottom, HeapWord* top) {
 104     Filtering_DCTOC::walk_mem_region(mr, bottom, top);
 105   }
 106 
 107 public:
 108   HeapRegionDCTOC(G1CollectedHeap* g1,
 109                   HeapRegion* hr, OopClosure* cl,
 110                   CardTableModRefBS::PrecisionStyle precision,
 111                   FilterKind fk);
 112 };
 113 
 114 
 115 // The complicating factor is that BlockOffsetTable diverged
 116 // significantly, and we need functionality that is only in the G1 version.
 117 // So I copied that code, which led to an alternate G1 version of
 118 // OffsetTableContigSpace.  If the two versions of BlockOffsetTable could
 119 // be reconciled, then G1OffsetTableContigSpace could go away.
 120 
 121 // The idea behind time stamps is the following. Doing a save_marks on
 122 // all regions at every GC pause is time consuming (if I remember
 123 // well, 10ms or so). So, we would like to do that only for regions
 124 // that are GC alloc regions. To achieve this, we use time
 125 // stamps. For every evacuation pause, G1CollectedHeap generates a
 126 // unique time stamp (essentially a counter that gets
 127 // incremented). Every time we want to call save_marks on a region,
 128 // we set the saved_mark_word to top and also copy the current GC
 129 // time stamp to the time stamp field of the space. Reading the
 130 // saved_mark_word involves checking the time stamp of the
 131 // region. If it is the same as the current GC time stamp, then we
 132 // can safely read the saved_mark_word field, as it is valid. If the
 133 // time stamp of the region is not the same as the current GC time
 134 // stamp, then we instead read top, as the saved_mark_word field is
 135 // invalid. Time stamps (on the regions and also on the
 136 // G1CollectedHeap) are reset at every cleanup (we iterate over
 137 // the regions anyway) and at the end of a Full GC. The current scheme
 138 // that uses sequential unsigned ints will fail only if we have 4b
 139 // evacuation pauses between two cleanups, which is _highly_ unlikely.
 140 
 141 class G1OffsetTableContigSpace: public ContiguousSpace {
 142   friend class VMStructs;
 143  protected:
 144   G1BlockOffsetArrayContigSpace _offsets;
 145   Mutex _par_alloc_lock;
 146   volatile unsigned _gc_time_stamp;
 147 
 148  public:
 149   // Constructor.  If "is_zeroed" is true, the MemRegion "mr" may be
 150   // assumed to contain zeros.
 151   G1OffsetTableContigSpace(G1BlockOffsetSharedArray* sharedOffsetArray,
 152                            MemRegion mr, bool is_zeroed = false);
 153 
 154   void set_bottom(HeapWord* value);
 155   void set_end(HeapWord* value);
 156 
 157   virtual HeapWord* saved_mark_word() const;
 158   virtual void set_saved_mark();
 159   void reset_gc_time_stamp() { _gc_time_stamp = 0; }
 160 
 161   virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);
 162   virtual void clear(bool mangle_space);
 163 
 164   HeapWord* block_start(const void* p);
 165   HeapWord* block_start_const(const void* p) const;
 166 
 167   // Add offset table update.
 168   virtual HeapWord* allocate(size_t word_size);
 169   HeapWord* par_allocate(size_t word_size);
 170 
 171   // MarkSweep support phase3
 172   virtual HeapWord* initialize_threshold();
 173   virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* end);
 174 
 175   virtual void print() const;
 176 };
 177 
 178 class HeapRegion: public G1OffsetTableContigSpace {
 179   friend class VMStructs;
 180  private:
 181 
 182   enum HumongousType {
 183     NotHumongous = 0,
 184     StartsHumongous,
 185     ContinuesHumongous
 186   };
 187 
 188   // The next filter kind that should be used for a "new_dcto_cl" call with
 189   // the "traditional" signature.
 190   HeapRegionDCTOC::FilterKind _next_fk;
 191 
 192   // Requires that the region "mr" be dense with objects, and begin and end
 193   // with an object.
 194   void oops_in_mr_iterate(MemRegion mr, OopClosure* cl);
 195 
 196   // The remembered set for this region.
 197   // (Might want to make this "inline" later, to avoid some alloc failure
 198   // issues.)
 199   HeapRegionRemSet* _rem_set;
 200 
 201   G1BlockOffsetArrayContigSpace* offsets() { return &_offsets; }
 202 
 203  protected:
 204   // If this region is a member of a HeapRegionSeq, the index in that
 205   // sequence, otherwise -1.
 206   int  _hrs_index;
 207 
 208   HumongousType _humongous_type;
 209   // For a humongous region, region in which it starts.
 210   HeapRegion* _humongous_start_region;
 211   // For the start region of a humongous sequence, it's original end().
 212   HeapWord* _orig_end;
 213 
 214   // True iff the region is in current collection_set.
 215   bool _in_collection_set;
 216 
 217     // True iff the region is on the unclean list, waiting to be zero filled.
 218   bool _is_on_unclean_list;
 219 
 220   // True iff the region is on the free list, ready for allocation.
 221   bool _is_on_free_list;
 222 
 223   // Is this or has it been an allocation region in the current collection
 224   // pause.
 225   bool _is_gc_alloc_region;
 226 
 227   // True iff an attempt to evacuate an object in the region failed.
 228   bool _evacuation_failed;
 229 
 230   // A heap region may be a member one of a number of special subsets, each
 231   // represented as linked lists through the field below.  Currently, these
 232   // sets include:
 233   //   The collection set.
 234   //   The set of allocation regions used in a collection pause.
 235   //   Spaces that may contain gray objects.
 236   HeapRegion* _next_in_special_set;
 237 
 238   // next region in the young "generation" region set
 239   HeapRegion* _next_young_region;
 240 
 241   // Next region whose cards need cleaning
 242   HeapRegion* _next_dirty_cards_region;
 243 
 244   // For parallel heapRegion traversal.
 245   jint _claimed;
 246 
 247   // We use concurrent marking to determine the amount of live data
 248   // in each heap region.
 249   size_t _prev_marked_bytes;    // Bytes known to be live via last completed marking.
 250   size_t _next_marked_bytes;    // Bytes known to be live via in-progress marking.
 251 
 252   // See "sort_index" method.  -1 means is not in the array.
 253   int _sort_index;
 254 
 255   // <PREDICTION>
 256   double _gc_efficiency;
 257   // </PREDICTION>
 258 
 259   enum YoungType {
 260     NotYoung,                   // a region is not young
 261     Young,                      // a region is young
 262     Survivor                    // a region is young and it contains
 263                                 // survivor
 264   };
 265 
 266   volatile YoungType _young_type;
 267   int  _young_index_in_cset;
 268   SurvRateGroup* _surv_rate_group;
 269   int  _age_index;
 270 
 271   // The start of the unmarked area. The unmarked area extends from this
 272   // word until the top and/or end of the region, and is the part
 273   // of the region for which no marking was done, i.e. objects may
 274   // have been allocated in this part since the last mark phase.
 275   // "prev" is the top at the start of the last completed marking.
 276   // "next" is the top at the start of the in-progress marking (if any.)
 277   HeapWord* _prev_top_at_mark_start;
 278   HeapWord* _next_top_at_mark_start;
 279   // If a collection pause is in progress, this is the top at the start
 280   // of that pause.
 281 
 282   // We've counted the marked bytes of objects below here.
 283   HeapWord* _top_at_conc_mark_count;
 284 
 285   void init_top_at_mark_start() {
 286     assert(_prev_marked_bytes == 0 &&
 287            _next_marked_bytes == 0,
 288            "Must be called after zero_marked_bytes.");
 289     HeapWord* bot = bottom();
 290     _prev_top_at_mark_start = bot;
 291     _next_top_at_mark_start = bot;
 292     _top_at_conc_mark_count = bot;
 293   }
 294 
 295   jint _zfs;  // A member of ZeroFillState.  Protected by ZF_lock.
 296   Thread* _zero_filler; // If _zfs is ZeroFilling, the thread that (last)
 297                         // made it so.
 298 
 299   void set_young_type(YoungType new_type) {
 300     //assert(_young_type != new_type, "setting the same type" );
 301     // TODO: add more assertions here
 302     _young_type = new_type;
 303   }
 304 
 305   // Cached attributes used in the collection set policy information
 306 
 307   // The RSet length that was added to the total value
 308   // for the collection set.
 309   size_t _recorded_rs_length;
 310 
 311   // The predicted elapsed time that was added to total value
 312   // for the collection set.
 313   double _predicted_elapsed_time_ms;
 314 
 315   // The predicted number of bytes to copy that was added to
 316   // the total value for the collection set.
 317   size_t _predicted_bytes_to_copy;
 318 
 319  public:
 320   // If "is_zeroed" is "true", the region "mr" can be assumed to contain zeros.
 321   HeapRegion(G1BlockOffsetSharedArray* sharedOffsetArray,
 322              MemRegion mr, bool is_zeroed);
 323 
 324   static int LogOfHRGrainBytes;
 325   static int LogOfHRGrainWords;
 326   // The normal type of these should be size_t. However, they used to
 327   // be members of an enum before and they are assumed by the
 328   // compilers to be ints. To avoid going and fixing all their uses,
 329   // I'm declaring them as ints. I'm not anticipating heap region
 330   // sizes to reach anywhere near 2g, so using an int here is safe.
 331   static int GrainBytes;
 332   static int GrainWords;
 333   static int CardsPerRegion;
 334 
 335   // It sets up the heap region size (GrainBytes / GrainWords), as
 336   // well as other related fields that are based on the heap region
 337   // size (LogOfHRGrainBytes / LogOfHRGrainWords /
 338   // CardsPerRegion). All those fields are considered constant
 339   // throughout the JVM's execution, therefore they should only be set
 340   // up once during initialization time.
 341   static void setup_heap_region_size(uintx min_heap_size);
 342 
 343   enum ClaimValues {
 344     InitialClaimValue     = 0,
 345     FinalCountClaimValue  = 1,
 346     NoteEndClaimValue     = 2,
 347     ScrubRemSetClaimValue = 3,
 348     ParVerifyClaimValue   = 4,
 349     RebuildRSClaimValue   = 5
 350   };
 351 
 352   // Concurrent refinement requires contiguous heap regions (in which TLABs
 353   // might be allocated) to be zero-filled.  Each region therefore has a
 354   // zero-fill-state.
 355   enum ZeroFillState {
 356     NotZeroFilled,
 357     ZeroFilling,
 358     ZeroFilled,
 359     Allocated
 360   };
 361 
 362   // If this region is a member of a HeapRegionSeq, the index in that
 363   // sequence, otherwise -1.
 364   int hrs_index() const { return _hrs_index; }
 365   void set_hrs_index(int index) { _hrs_index = index; }
 366 
 367   // The number of bytes marked live in the region in the last marking phase.
 368   size_t marked_bytes()    { return _prev_marked_bytes; }
 369   // The number of bytes counted in the next marking.
 370   size_t next_marked_bytes() { return _next_marked_bytes; }
 371   // The number of bytes live wrt the next marking.
 372   size_t next_live_bytes() {
 373     return (top() - next_top_at_mark_start())
 374       * HeapWordSize
 375       + next_marked_bytes();
 376   }
 377 
 378   // A lower bound on the amount of garbage bytes in the region.
 379   size_t garbage_bytes() {
 380     size_t used_at_mark_start_bytes =
 381       (prev_top_at_mark_start() - bottom()) * HeapWordSize;
 382     assert(used_at_mark_start_bytes >= marked_bytes(),
 383            "Can't mark more than we have.");
 384     return used_at_mark_start_bytes - marked_bytes();
 385   }
 386 
 387   // An upper bound on the number of live bytes in the region.
 388   size_t max_live_bytes() { return used() - garbage_bytes(); }
 389 
 390   void add_to_marked_bytes(size_t incr_bytes) {
 391     _next_marked_bytes = _next_marked_bytes + incr_bytes;
 392     guarantee( _next_marked_bytes <= used(), "invariant" );
 393   }
 394 
 395   void zero_marked_bytes()      {
 396     _prev_marked_bytes = _next_marked_bytes = 0;
 397   }
 398 
 399   bool isHumongous() const { return _humongous_type != NotHumongous; }
 400   bool startsHumongous() const { return _humongous_type == StartsHumongous; }
 401   bool continuesHumongous() const { return _humongous_type == ContinuesHumongous; }
 402   // For a humongous region, region in which it starts.
 403   HeapRegion* humongous_start_region() const {
 404     return _humongous_start_region;
 405   }
 406 
 407   // Causes the current region to represent a humongous object spanning "n"
 408   // regions.
 409   virtual void set_startsHumongous();
 410 
 411   // The regions that continue a humongous sequence should be added using
 412   // this method, in increasing address order.
 413   void set_continuesHumongous(HeapRegion* start);
 414 
 415   void add_continuingHumongousRegion(HeapRegion* cont);
 416 
 417   // If the region has a remembered set, return a pointer to it.
 418   HeapRegionRemSet* rem_set() const {
 419     return _rem_set;
 420   }
 421 
 422   // True iff the region is in current collection_set.
 423   bool in_collection_set() const {
 424     return _in_collection_set;
 425   }
 426   void set_in_collection_set(bool b) {
 427     _in_collection_set = b;
 428   }
 429   HeapRegion* next_in_collection_set() {
 430     assert(in_collection_set(), "should only invoke on member of CS.");
 431     assert(_next_in_special_set == NULL ||
 432            _next_in_special_set->in_collection_set(),
 433            "Malformed CS.");
 434     return _next_in_special_set;
 435   }
 436   void set_next_in_collection_set(HeapRegion* r) {
 437     assert(in_collection_set(), "should only invoke on member of CS.");
 438     assert(r == NULL || r->in_collection_set(), "Malformed CS.");
 439     _next_in_special_set = r;
 440   }
 441 
 442   // True iff it is or has been an allocation region in the current
 443   // collection pause.
 444   bool is_gc_alloc_region() const {
 445     return _is_gc_alloc_region;
 446   }
 447   void set_is_gc_alloc_region(bool b) {
 448     _is_gc_alloc_region = b;
 449   }
 450   HeapRegion* next_gc_alloc_region() {
 451     assert(is_gc_alloc_region(), "should only invoke on member of CS.");
 452     assert(_next_in_special_set == NULL ||
 453            _next_in_special_set->is_gc_alloc_region(),
 454            "Malformed CS.");
 455     return _next_in_special_set;
 456   }
 457   void set_next_gc_alloc_region(HeapRegion* r) {
 458     assert(is_gc_alloc_region(), "should only invoke on member of CS.");
 459     assert(r == NULL || r->is_gc_alloc_region(), "Malformed CS.");
 460     _next_in_special_set = r;
 461   }
 462 
 463   bool is_on_free_list() {
 464     return _is_on_free_list;
 465   }
 466 
 467   void set_on_free_list(bool b) {
 468     _is_on_free_list = b;
 469   }
 470 
 471   HeapRegion* next_from_free_list() {
 472     assert(is_on_free_list(),
 473            "Should only invoke on free space.");
 474     assert(_next_in_special_set == NULL ||
 475            _next_in_special_set->is_on_free_list(),
 476            "Malformed Free List.");
 477     return _next_in_special_set;
 478   }
 479 
 480   void set_next_on_free_list(HeapRegion* r) {
 481     assert(r == NULL || r->is_on_free_list(), "Malformed free list.");
 482     _next_in_special_set = r;
 483   }
 484 
 485   bool is_on_unclean_list() {
 486     return _is_on_unclean_list;
 487   }
 488 
 489   void set_on_unclean_list(bool b);
 490 
 491   HeapRegion* next_from_unclean_list() {
 492     assert(is_on_unclean_list(),
 493            "Should only invoke on unclean space.");
 494     assert(_next_in_special_set == NULL ||
 495            _next_in_special_set->is_on_unclean_list(),
 496            "Malformed unclean List.");
 497     return _next_in_special_set;
 498   }
 499 
 500   void set_next_on_unclean_list(HeapRegion* r);
 501 
 502   HeapRegion* get_next_young_region() { return _next_young_region; }
 503   void set_next_young_region(HeapRegion* hr) {
 504     _next_young_region = hr;
 505   }
 506 
 507   HeapRegion* get_next_dirty_cards_region() const { return _next_dirty_cards_region; }
 508   HeapRegion** next_dirty_cards_region_addr() { return &_next_dirty_cards_region; }
 509   void set_next_dirty_cards_region(HeapRegion* hr) { _next_dirty_cards_region = hr; }
 510   bool is_on_dirty_cards_region_list() const { return get_next_dirty_cards_region() != NULL; }
 511 
 512   // Allows logical separation between objects allocated before and after.
 513   void save_marks();
 514 
 515   // Reset HR stuff to default values.
 516   void hr_clear(bool par, bool clear_space);
 517 
 518   void initialize(MemRegion mr, bool clear_space, bool mangle_space);
 519 
 520   // Ensure that "this" is zero-filled.
 521   void ensure_zero_filled();
 522   // This one requires that the calling thread holds ZF_mon.
 523   void ensure_zero_filled_locked();
 524 
 525   // Get the start of the unmarked area in this region.
 526   HeapWord* prev_top_at_mark_start() const { return _prev_top_at_mark_start; }
 527   HeapWord* next_top_at_mark_start() const { return _next_top_at_mark_start; }
 528 
 529   // Apply "cl->do_oop" to (the addresses of) all reference fields in objects
 530   // allocated in the current region before the last call to "save_mark".
 531   void oop_before_save_marks_iterate(OopClosure* cl);
 532 
 533   // This call determines the "filter kind" argument that will be used for
 534   // the next call to "new_dcto_cl" on this region with the "traditional"
 535   // signature (i.e., the call below.)  The default, in the absence of a
 536   // preceding call to this method, is "NoFilterKind", and a call to this
 537   // method is necessary for each such call, or else it reverts to the
 538   // default.
 539   // (This is really ugly, but all other methods I could think of changed a
 540   // lot of main-line code for G1.)
 541   void set_next_filter_kind(HeapRegionDCTOC::FilterKind nfk) {
 542     _next_fk = nfk;
 543   }
 544 
 545   DirtyCardToOopClosure*
 546   new_dcto_closure(OopClosure* cl,
 547                    CardTableModRefBS::PrecisionStyle precision,
 548                    HeapRegionDCTOC::FilterKind fk);
 549 
 550 #if WHASSUP
 551   DirtyCardToOopClosure*
 552   new_dcto_closure(OopClosure* cl,
 553                    CardTableModRefBS::PrecisionStyle precision,
 554                    HeapWord* boundary) {
 555     assert(boundary == NULL, "This arg doesn't make sense here.");
 556     DirtyCardToOopClosure* res = new_dcto_closure(cl, precision, _next_fk);
 557     _next_fk = HeapRegionDCTOC::NoFilterKind;
 558     return res;
 559   }
 560 #endif
 561 
 562   //
 563   // Note the start or end of marking. This tells the heap region
 564   // that the collector is about to start or has finished (concurrently)
 565   // marking the heap.
 566   //
 567 
 568   // Note the start of a marking phase. Record the
 569   // start of the unmarked area of the region here.
 570   void note_start_of_marking(bool during_initial_mark) {
 571     init_top_at_conc_mark_count();
 572     _next_marked_bytes = 0;
 573     if (during_initial_mark && is_young() && !is_survivor())
 574       _next_top_at_mark_start = bottom();
 575     else
 576       _next_top_at_mark_start = top();
 577   }
 578 
 579   // Note the end of a marking phase. Install the start of
 580   // the unmarked area that was captured at start of marking.
 581   void note_end_of_marking() {
 582     _prev_top_at_mark_start = _next_top_at_mark_start;
 583     _prev_marked_bytes = _next_marked_bytes;
 584     _next_marked_bytes = 0;
 585 
 586     guarantee(_prev_marked_bytes <=
 587               (size_t) (prev_top_at_mark_start() - bottom()) * HeapWordSize,
 588               "invariant");
 589   }
 590 
 591   // After an evacuation, we need to update _next_top_at_mark_start
 592   // to be the current top.  Note this is only valid if we have only
 593   // ever evacuated into this region.  If we evacuate, allocate, and
 594   // then evacuate we are in deep doodoo.
 595   void note_end_of_copying() {
 596     assert(top() >= _next_top_at_mark_start, "Increase only");
 597     _next_top_at_mark_start = top();
 598   }
 599 
 600   // Returns "false" iff no object in the region was allocated when the
 601   // last mark phase ended.
 602   bool is_marked() { return _prev_top_at_mark_start != bottom(); }
 603 
 604   // If "is_marked()" is true, then this is the index of the region in
 605   // an array constructed at the end of marking of the regions in a
 606   // "desirability" order.
 607   int sort_index() {
 608     return _sort_index;
 609   }
 610   void set_sort_index(int i) {
 611     _sort_index = i;
 612   }
 613 
 614   void init_top_at_conc_mark_count() {
 615     _top_at_conc_mark_count = bottom();
 616   }
 617 
 618   void set_top_at_conc_mark_count(HeapWord *cur) {
 619     assert(bottom() <= cur && cur <= end(), "Sanity.");
 620     _top_at_conc_mark_count = cur;
 621   }
 622 
 623   HeapWord* top_at_conc_mark_count() {
 624     return _top_at_conc_mark_count;
 625   }
 626 
 627   void reset_during_compaction() {
 628     guarantee( isHumongous() && startsHumongous(),
 629                "should only be called for humongous regions");
 630 
 631     zero_marked_bytes();
 632     init_top_at_mark_start();
 633   }
 634 
 635   // <PREDICTION>
 636   void calc_gc_efficiency(void);
 637   double gc_efficiency() { return _gc_efficiency;}
 638   // </PREDICTION>
 639 
 640   bool is_young() const     { return _young_type != NotYoung; }
 641   bool is_survivor() const  { return _young_type == Survivor; }
 642 
 643   int  young_index_in_cset() const { return _young_index_in_cset; }
 644   void set_young_index_in_cset(int index) {
 645     assert( (index == -1) || is_young(), "pre-condition" );
 646     _young_index_in_cset = index;
 647   }
 648 
 649   int age_in_surv_rate_group() {
 650     assert( _surv_rate_group != NULL, "pre-condition" );
 651     assert( _age_index > -1, "pre-condition" );
 652     return _surv_rate_group->age_in_group(_age_index);
 653   }
 654 
 655   void record_surv_words_in_group(size_t words_survived) {
 656     assert( _surv_rate_group != NULL, "pre-condition" );
 657     assert( _age_index > -1, "pre-condition" );
 658     int age_in_group = age_in_surv_rate_group();
 659     _surv_rate_group->record_surviving_words(age_in_group, words_survived);
 660   }
 661 
 662   int age_in_surv_rate_group_cond() {
 663     if (_surv_rate_group != NULL)
 664       return age_in_surv_rate_group();
 665     else
 666       return -1;
 667   }
 668 
 669   SurvRateGroup* surv_rate_group() {
 670     return _surv_rate_group;
 671   }
 672 
 673   void install_surv_rate_group(SurvRateGroup* surv_rate_group) {
 674     assert( surv_rate_group != NULL, "pre-condition" );
 675     assert( _surv_rate_group == NULL, "pre-condition" );
 676     assert( is_young(), "pre-condition" );
 677 
 678     _surv_rate_group = surv_rate_group;
 679     _age_index = surv_rate_group->next_age_index();
 680   }
 681 
 682   void uninstall_surv_rate_group() {
 683     if (_surv_rate_group != NULL) {
 684       assert( _age_index > -1, "pre-condition" );
 685       assert( is_young(), "pre-condition" );
 686 
 687       _surv_rate_group = NULL;
 688       _age_index = -1;
 689     } else {
 690       assert( _age_index == -1, "pre-condition" );
 691     }
 692   }
 693 
 694   void set_young() { set_young_type(Young); }
 695 
 696   void set_survivor() { set_young_type(Survivor); }
 697 
 698   void set_not_young() { set_young_type(NotYoung); }
 699 
 700   // Determine if an object has been allocated since the last
 701   // mark performed by the collector. This returns true iff the object
 702   // is within the unmarked area of the region.
 703   bool obj_allocated_since_prev_marking(oop obj) const {
 704     return (HeapWord *) obj >= prev_top_at_mark_start();
 705   }
 706   bool obj_allocated_since_next_marking(oop obj) const {
 707     return (HeapWord *) obj >= next_top_at_mark_start();
 708   }
 709 
 710   // For parallel heapRegion traversal.
 711   bool claimHeapRegion(int claimValue);
 712   jint claim_value() { return _claimed; }
 713   // Use this carefully: only when you're sure no one is claiming...
 714   void set_claim_value(int claimValue) { _claimed = claimValue; }
 715 
 716   // Returns the "evacuation_failed" property of the region.
 717   bool evacuation_failed() { return _evacuation_failed; }
 718 
 719   // Sets the "evacuation_failed" property of the region.
 720   void set_evacuation_failed(bool b) {
 721     _evacuation_failed = b;
 722 
 723     if (b) {
 724       init_top_at_conc_mark_count();
 725       _next_marked_bytes = 0;
 726     }
 727   }
 728 
 729   // Requires that "mr" be entirely within the region.
 730   // Apply "cl->do_object" to all objects that intersect with "mr".
 731   // If the iteration encounters an unparseable portion of the region,
 732   // or if "cl->abort()" is true after a closure application,
 733   // terminate the iteration and return the address of the start of the
 734   // subregion that isn't done.  (The two can be distinguished by querying
 735   // "cl->abort()".)  Return of "NULL" indicates that the iteration
 736   // completed.
 737   HeapWord*
 738   object_iterate_mem_careful(MemRegion mr, ObjectClosure* cl);
 739 
 740   // In this version - if filter_young is true and the region
 741   // is a young region then we skip the iteration.
 742   HeapWord*
 743   oops_on_card_seq_iterate_careful(MemRegion mr,
 744                                    FilterOutOfRegionClosure* cl,
 745                                    bool filter_young);
 746 
 747   // The region "mr" is entirely in "this", and starts and ends at block
 748   // boundaries. The caller declares that all the contained blocks are
 749   // coalesced into one.
 750   void declare_filled_region_to_BOT(MemRegion mr) {
 751     _offsets.single_block(mr.start(), mr.end());
 752   }
 753 
 754   // A version of block start that is guaranteed to find *some* block
 755   // boundary at or before "p", but does not object iteration, and may
 756   // therefore be used safely when the heap is unparseable.
 757   HeapWord* block_start_careful(const void* p) const {
 758     return _offsets.block_start_careful(p);
 759   }
 760 
 761   // Requires that "addr" is within the region.  Returns the start of the
 762   // first ("careful") block that starts at or after "addr", or else the
 763   // "end" of the region if there is no such block.
 764   HeapWord* next_block_start_careful(HeapWord* addr);
 765 
 766   // Returns the zero-fill-state of the current region.
 767   ZeroFillState zero_fill_state() { return (ZeroFillState)_zfs; }
 768   bool zero_fill_is_allocated() { return _zfs == Allocated; }
 769   Thread* zero_filler() { return _zero_filler; }
 770 
 771   // Indicate that the contents of the region are unknown, and therefore
 772   // might require zero-filling.
 773   void set_zero_fill_needed() {
 774     set_zero_fill_state_work(NotZeroFilled);
 775   }
 776   void set_zero_fill_in_progress(Thread* t) {
 777     set_zero_fill_state_work(ZeroFilling);
 778     _zero_filler = t;
 779   }
 780   void set_zero_fill_complete();
 781   void set_zero_fill_allocated() {
 782     set_zero_fill_state_work(Allocated);
 783   }
 784 
 785   void set_zero_fill_state_work(ZeroFillState zfs);
 786 
 787   // This is called when a full collection shrinks the heap.
 788   // We want to set the heap region to a value which says
 789   // it is no longer part of the heap.  For now, we'll let "NotZF" fill
 790   // that role.
 791   void reset_zero_fill() {
 792     set_zero_fill_state_work(NotZeroFilled);
 793     _zero_filler = NULL;
 794   }
 795 
 796   size_t recorded_rs_length() const        { return _recorded_rs_length; }
 797   double predicted_elapsed_time_ms() const { return _predicted_elapsed_time_ms; }
 798   size_t predicted_bytes_to_copy() const   { return _predicted_bytes_to_copy; }
 799 
 800   void set_recorded_rs_length(size_t rs_length) {
 801     _recorded_rs_length = rs_length;
 802   }
 803 
 804   void set_predicted_elapsed_time_ms(double ms) {
 805     _predicted_elapsed_time_ms = ms;
 806   }
 807 
 808   void set_predicted_bytes_to_copy(size_t bytes) {
 809     _predicted_bytes_to_copy = bytes;
 810   }
 811 
 812 #define HeapRegion_OOP_SINCE_SAVE_MARKS_DECL(OopClosureType, nv_suffix)  \
 813   virtual void oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl);
 814   SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(HeapRegion_OOP_SINCE_SAVE_MARKS_DECL)
 815 
 816   CompactibleSpace* next_compaction_space() const;
 817 
 818   virtual void reset_after_compaction();
 819 
 820   void print() const;
 821   void print_on(outputStream* st) const;
 822 
 823   // use_prev_marking == true  -> use "prev" marking information,
 824   // use_prev_marking == false -> use "next" marking information
 825   // NOTE: Only the "prev" marking information is guaranteed to be
 826   // consistent most of the time, so most calls to this should use
 827   // use_prev_marking == true. Currently, there is only one case where
 828   // this is called with use_prev_marking == false, which is to verify
 829   // the "next" marking information at the end of remark.
 830   void verify(bool allow_dirty, bool use_prev_marking, bool *failures) const;
 831 
 832   // Override; it uses the "prev" marking information
 833   virtual void verify(bool allow_dirty) const;
 834 
 835 #ifdef DEBUG
 836   HeapWord* allocate(size_t size);
 837 #endif
 838 };
 839 
 840 // HeapRegionClosure is used for iterating over regions.
 841 // Terminates the iteration when the "doHeapRegion" method returns "true".
 842 class HeapRegionClosure : public StackObj {
 843   friend class HeapRegionSeq;
 844   friend class G1CollectedHeap;
 845 
 846   bool _complete;
 847   void incomplete() { _complete = false; }
 848 
 849  public:
 850   HeapRegionClosure(): _complete(true) {}
 851 
 852   // Typically called on each region until it returns true.
 853   virtual bool doHeapRegion(HeapRegion* r) = 0;
 854 
 855   // True after iteration if the closure was applied to all heap regions
 856   // and returned "false" in all cases.
 857   bool complete() { return _complete; }
 858 };
 859 
 860 // A linked lists of heap regions.  It leaves the "next" field
 861 // unspecified; that's up to subtypes.
 862 class RegionList VALUE_OBJ_CLASS_SPEC {
 863 protected:
 864   virtual HeapRegion* get_next(HeapRegion* chr) = 0;
 865   virtual void set_next(HeapRegion* chr,
 866                         HeapRegion* new_next) = 0;
 867 
 868   HeapRegion* _hd;
 869   HeapRegion* _tl;
 870   size_t _sz;
 871 
 872   // Protected constructor because this type is only meaningful
 873   // when the _get/_set next functions are defined.
 874   RegionList() : _hd(NULL), _tl(NULL), _sz(0) {}
 875 public:
 876   void reset() {
 877     _hd = NULL;
 878     _tl = NULL;
 879     _sz = 0;
 880   }
 881   HeapRegion* hd() { return _hd; }
 882   HeapRegion* tl() { return _tl; }
 883   size_t sz() { return _sz; }
 884   size_t length();
 885 
 886   bool well_formed() {
 887     return
 888       ((hd() == NULL && tl() == NULL && sz() == 0)
 889        || (hd() != NULL && tl() != NULL && sz() > 0))
 890       && (sz() == length());
 891   }
 892   virtual void insert_before_head(HeapRegion* r);
 893   void prepend_list(RegionList* new_list);
 894   virtual HeapRegion* pop();
 895   void dec_sz() { _sz--; }
 896   // Requires that "r" is an element of the list, and is not the tail.
 897   void delete_after(HeapRegion* r);
 898 };
 899 
 900 class EmptyNonHRegionList: public RegionList {
 901 protected:
 902   // Protected constructor because this type is only meaningful
 903   // when the _get/_set next functions are defined.
 904   EmptyNonHRegionList() : RegionList() {}
 905 
 906 public:
 907   void insert_before_head(HeapRegion* r) {
 908     //    assert(r->is_empty(), "Better be empty");
 909     assert(!r->isHumongous(), "Better not be humongous.");
 910     RegionList::insert_before_head(r);
 911   }
 912   void prepend_list(EmptyNonHRegionList* new_list) {
 913     //    assert(new_list->hd() == NULL || new_list->hd()->is_empty(),
 914     //     "Better be empty");
 915     assert(new_list->hd() == NULL || !new_list->hd()->isHumongous(),
 916            "Better not be humongous.");
 917     //    assert(new_list->tl() == NULL || new_list->tl()->is_empty(),
 918     //     "Better be empty");
 919     assert(new_list->tl() == NULL || !new_list->tl()->isHumongous(),
 920            "Better not be humongous.");
 921     RegionList::prepend_list(new_list);
 922   }
 923 };
 924 
 925 class UncleanRegionList: public EmptyNonHRegionList {
 926 public:
 927   HeapRegion* get_next(HeapRegion* hr) {
 928     return hr->next_from_unclean_list();
 929   }
 930   void set_next(HeapRegion* hr, HeapRegion* new_next) {
 931     hr->set_next_on_unclean_list(new_next);
 932   }
 933 
 934   UncleanRegionList() : EmptyNonHRegionList() {}
 935 
 936   void insert_before_head(HeapRegion* r) {
 937     assert(!r->is_on_free_list(),
 938            "Better not already be on free list");
 939     assert(!r->is_on_unclean_list(),
 940            "Better not already be on unclean list");
 941     r->set_zero_fill_needed();
 942     r->set_on_unclean_list(true);
 943     EmptyNonHRegionList::insert_before_head(r);
 944   }
 945   void prepend_list(UncleanRegionList* new_list) {
 946     assert(new_list->tl() == NULL || !new_list->tl()->is_on_free_list(),
 947            "Better not already be on free list");
 948     assert(new_list->tl() == NULL || new_list->tl()->is_on_unclean_list(),
 949            "Better already be marked as on unclean list");
 950     assert(new_list->hd() == NULL || !new_list->hd()->is_on_free_list(),
 951            "Better not already be on free list");
 952     assert(new_list->hd() == NULL || new_list->hd()->is_on_unclean_list(),
 953            "Better already be marked as on unclean list");
 954     EmptyNonHRegionList::prepend_list(new_list);
 955   }
 956   HeapRegion* pop() {
 957     HeapRegion* res = RegionList::pop();
 958     if (res != NULL) res->set_on_unclean_list(false);
 959     return res;
 960   }
 961 };
 962 
 963 // Local Variables: ***
 964 // c-indentation-style: gnu ***
 965 // End: ***
 966 
 967 #endif // SERIALGC
 968 
 969 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_HPP