1 /*
   2  * Copyright (c) 2001, 2011, 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 class HeapRegionSetBase;
  54 
  55 #define HR_FORMAT SIZE_FORMAT":(%s)["PTR_FORMAT","PTR_FORMAT","PTR_FORMAT"]"
  56 #define HR_FORMAT_PARAMS(_hr_) \
  57                 (_hr_)->hrs_index(), \
  58                 (_hr_)->is_survivor() ? "S" : (_hr_)->is_young() ? "E" : "-", \
  59                 (_hr_)->bottom(), (_hr_)->top(), (_hr_)->end()
  60 
  61 // A dirty card to oop closure for heap regions. It
  62 // knows how to get the G1 heap and how to use the bitmap
  63 // in the concurrent marker used by G1 to filter remembered
  64 // sets.
  65 
  66 class HeapRegionDCTOC : public ContiguousSpaceDCTOC {
  67 public:
  68   // Specification of possible DirtyCardToOopClosure filtering.
  69   enum FilterKind {
  70     NoFilterKind,
  71     IntoCSFilterKind,
  72     OutOfRegionFilterKind
  73   };
  74 
  75 protected:
  76   HeapRegion* _hr;
  77   FilterKind _fk;
  78   G1CollectedHeap* _g1;
  79 
  80   void walk_mem_region_with_cl(MemRegion mr,
  81                                HeapWord* bottom, HeapWord* top,
  82                                OopClosure* cl);
  83 
  84   // We don't specialize this for FilteringClosure; filtering is handled by
  85   // the "FilterKind" mechanism.  But we provide this to avoid a compiler
  86   // warning.
  87   void walk_mem_region_with_cl(MemRegion mr,
  88                                HeapWord* bottom, HeapWord* top,
  89                                FilteringClosure* cl) {
  90     HeapRegionDCTOC::walk_mem_region_with_cl(mr, bottom, top,
  91                                                        (OopClosure*)cl);
  92   }
  93 
  94   // Get the actual top of the area on which the closure will
  95   // operate, given where the top is assumed to be (the end of the
  96   // memory region passed to do_MemRegion) and where the object
  97   // at the top is assumed to start. For example, an object may
  98   // start at the top but actually extend past the assumed top,
  99   // in which case the top becomes the end of the object.
 100   HeapWord* get_actual_top(HeapWord* top, HeapWord* top_obj) {
 101     return ContiguousSpaceDCTOC::get_actual_top(top, top_obj);
 102   }
 103 
 104   // Walk the given memory region from bottom to (actual) top
 105   // looking for objects and applying the oop closure (_cl) to
 106   // them. The base implementation of this treats the area as
 107   // blocks, where a block may or may not be an object. Sub-
 108   // classes should override this to provide more accurate
 109   // or possibly more efficient walking.
 110   void walk_mem_region(MemRegion mr, HeapWord* bottom, HeapWord* top) {
 111     Filtering_DCTOC::walk_mem_region(mr, bottom, top);
 112   }
 113 
 114 public:
 115   HeapRegionDCTOC(G1CollectedHeap* g1,
 116                   HeapRegion* hr, OopClosure* cl,
 117                   CardTableModRefBS::PrecisionStyle precision,
 118                   FilterKind fk);
 119 };
 120 
 121 // The complicating factor is that BlockOffsetTable diverged
 122 // significantly, and we need functionality that is only in the G1 version.
 123 // So I copied that code, which led to an alternate G1 version of
 124 // OffsetTableContigSpace.  If the two versions of BlockOffsetTable could
 125 // be reconciled, then G1OffsetTableContigSpace could go away.
 126 
 127 // The idea behind time stamps is the following. Doing a save_marks on
 128 // all regions at every GC pause is time consuming (if I remember
 129 // well, 10ms or so). So, we would like to do that only for regions
 130 // that are GC alloc regions. To achieve this, we use time
 131 // stamps. For every evacuation pause, G1CollectedHeap generates a
 132 // unique time stamp (essentially a counter that gets
 133 // incremented). Every time we want to call save_marks on a region,
 134 // we set the saved_mark_word to top and also copy the current GC
 135 // time stamp to the time stamp field of the space. Reading the
 136 // saved_mark_word involves checking the time stamp of the
 137 // region. If it is the same as the current GC time stamp, then we
 138 // can safely read the saved_mark_word field, as it is valid. If the
 139 // time stamp of the region is not the same as the current GC time
 140 // stamp, then we instead read top, as the saved_mark_word field is
 141 // invalid. Time stamps (on the regions and also on the
 142 // G1CollectedHeap) are reset at every cleanup (we iterate over
 143 // the regions anyway) and at the end of a Full GC. The current scheme
 144 // that uses sequential unsigned ints will fail only if we have 4b
 145 // evacuation pauses between two cleanups, which is _highly_ unlikely.
 146 
 147 class G1OffsetTableContigSpace: public ContiguousSpace {
 148   friend class VMStructs;
 149  protected:
 150   G1BlockOffsetArrayContigSpace _offsets;
 151   Mutex _par_alloc_lock;
 152   volatile unsigned _gc_time_stamp;
 153   // When we need to retire an allocation region, while other threads
 154   // are also concurrently trying to allocate into it, we typically
 155   // allocate a dummy object at the end of the region to ensure that
 156   // no more allocations can take place in it. However, sometimes we
 157   // want to know where the end of the last "real" object we allocated
 158   // into the region was and this is what this keeps track.
 159   HeapWord* _pre_dummy_top;
 160 
 161  public:
 162   // Constructor.  If "is_zeroed" is true, the MemRegion "mr" may be
 163   // assumed to contain zeros.
 164   G1OffsetTableContigSpace(G1BlockOffsetSharedArray* sharedOffsetArray,
 165                            MemRegion mr, bool is_zeroed = false);
 166 
 167   void set_bottom(HeapWord* value);
 168   void set_end(HeapWord* value);
 169 
 170   virtual HeapWord* saved_mark_word() const;
 171   virtual void set_saved_mark();
 172   void reset_gc_time_stamp() { _gc_time_stamp = 0; }
 173 
 174   // See the comment above in the declaration of _pre_dummy_top for an
 175   // explanation of what it is.
 176   void set_pre_dummy_top(HeapWord* pre_dummy_top) {
 177     assert(is_in(pre_dummy_top) && pre_dummy_top <= top(), "pre-condition");
 178     _pre_dummy_top = pre_dummy_top;
 179   }
 180   HeapWord* pre_dummy_top() {
 181     return (_pre_dummy_top == NULL) ? top() : _pre_dummy_top;
 182   }
 183   void reset_pre_dummy_top() { _pre_dummy_top = NULL; }
 184 
 185   virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);
 186   virtual void clear(bool mangle_space);
 187 
 188   HeapWord* block_start(const void* p);
 189   HeapWord* block_start_const(const void* p) const;
 190 
 191   // Add offset table update.
 192   virtual HeapWord* allocate(size_t word_size);
 193   HeapWord* par_allocate(size_t word_size);
 194 
 195   // MarkSweep support phase3
 196   virtual HeapWord* initialize_threshold();
 197   virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* end);
 198 
 199   virtual void print() const;
 200 
 201   void reset_bot() {
 202     _offsets.zero_bottom_entry();
 203     _offsets.initialize_threshold();
 204   }
 205 
 206   void update_bot_for_object(HeapWord* start, size_t word_size) {
 207     _offsets.alloc_block(start, word_size);
 208   }
 209 
 210   void print_bot_on(outputStream* out) {
 211     _offsets.print_on(out);
 212   }
 213 };
 214 
 215 class HeapRegion: public G1OffsetTableContigSpace {
 216   friend class VMStructs;
 217  private:
 218 
 219   enum HumongousType {
 220     NotHumongous = 0,
 221     StartsHumongous,
 222     ContinuesHumongous
 223   };
 224 
 225   // Requires that the region "mr" be dense with objects, and begin and end
 226   // with an object.
 227   void oops_in_mr_iterate(MemRegion mr, OopClosure* cl);
 228 
 229   // The remembered set for this region.
 230   // (Might want to make this "inline" later, to avoid some alloc failure
 231   // issues.)
 232   HeapRegionRemSet* _rem_set;
 233 
 234   G1BlockOffsetArrayContigSpace* offsets() { return &_offsets; }
 235 
 236  protected:
 237   // The index of this region in the heap region sequence.
 238   size_t  _hrs_index;
 239 
 240   HumongousType _humongous_type;
 241   // For a humongous region, region in which it starts.
 242   HeapRegion* _humongous_start_region;
 243   // For the start region of a humongous sequence, it's original end().
 244   HeapWord* _orig_end;
 245 
 246   // True iff the region is in current collection_set.
 247   bool _in_collection_set;
 248 
 249   // True iff an attempt to evacuate an object in the region failed.
 250   bool _evacuation_failed;
 251 
 252   // A heap region may be a member one of a number of special subsets, each
 253   // represented as linked lists through the field below.  Currently, these
 254   // sets include:
 255   //   The collection set.
 256   //   The set of allocation regions used in a collection pause.
 257   //   Spaces that may contain gray objects.
 258   HeapRegion* _next_in_special_set;
 259 
 260   // next region in the young "generation" region set
 261   HeapRegion* _next_young_region;
 262 
 263   // Next region whose cards need cleaning
 264   HeapRegion* _next_dirty_cards_region;
 265 
 266   // Fields used by the HeapRegionSetBase class and subclasses.
 267   HeapRegion* _next;
 268 #ifdef ASSERT
 269   HeapRegionSetBase* _containing_set;
 270 #endif // ASSERT
 271   bool _pending_removal;
 272 
 273   // For parallel heapRegion traversal.
 274   jint _claimed;
 275 
 276   // We use concurrent marking to determine the amount of live data
 277   // in each heap region.
 278   size_t _prev_marked_bytes;    // Bytes known to be live via last completed marking.
 279   size_t _next_marked_bytes;    // Bytes known to be live via in-progress marking.
 280 
 281   // See "sort_index" method.  -1 means is not in the array.
 282   int _sort_index;
 283 
 284   // <PREDICTION>
 285   double _gc_efficiency;
 286   // </PREDICTION>
 287 
 288   enum YoungType {
 289     NotYoung,                   // a region is not young
 290     Young,                      // a region is young
 291     Survivor                    // a region is young and it contains survivors
 292   };
 293 
 294   volatile YoungType _young_type;
 295   int  _young_index_in_cset;
 296   SurvRateGroup* _surv_rate_group;
 297   int  _age_index;
 298 
 299   // The start of the unmarked area. The unmarked area extends from this
 300   // word until the top and/or end of the region, and is the part
 301   // of the region for which no marking was done, i.e. objects may
 302   // have been allocated in this part since the last mark phase.
 303   // "prev" is the top at the start of the last completed marking.
 304   // "next" is the top at the start of the in-progress marking (if any.)
 305   HeapWord* _prev_top_at_mark_start;
 306   HeapWord* _next_top_at_mark_start;
 307   // If a collection pause is in progress, this is the top at the start
 308   // of that pause.
 309 
 310   // We've counted the marked bytes of objects below here.
 311   HeapWord* _top_at_conc_mark_count;
 312 
 313   void init_top_at_mark_start() {
 314     assert(_prev_marked_bytes == 0 &&
 315            _next_marked_bytes == 0,
 316            "Must be called after zero_marked_bytes.");
 317     HeapWord* bot = bottom();
 318     _prev_top_at_mark_start = bot;
 319     _next_top_at_mark_start = bot;
 320     _top_at_conc_mark_count = bot;
 321   }
 322 
 323   void set_young_type(YoungType new_type) {
 324     //assert(_young_type != new_type, "setting the same type" );
 325     // TODO: add more assertions here
 326     _young_type = new_type;
 327   }
 328 
 329   // Cached attributes used in the collection set policy information
 330 
 331   // The RSet length that was added to the total value
 332   // for the collection set.
 333   size_t _recorded_rs_length;
 334 
 335   // The predicted elapsed time that was added to total value
 336   // for the collection set.
 337   double _predicted_elapsed_time_ms;
 338 
 339   // The predicted number of bytes to copy that was added to
 340   // the total value for the collection set.
 341   size_t _predicted_bytes_to_copy;
 342 
 343  public:
 344   // If "is_zeroed" is "true", the region "mr" can be assumed to contain zeros.
 345   HeapRegion(size_t hrs_index,
 346              G1BlockOffsetSharedArray* sharedOffsetArray,
 347              MemRegion mr, bool is_zeroed);
 348 
 349   static int    LogOfHRGrainBytes;
 350   static int    LogOfHRGrainWords;
 351 
 352   static size_t GrainBytes;
 353   static size_t GrainWords;
 354   static size_t CardsPerRegion;
 355 
 356   static size_t align_up_to_region_byte_size(size_t sz) {
 357     return (sz + (size_t) GrainBytes - 1) &
 358                                       ~((1 << (size_t) LogOfHRGrainBytes) - 1);
 359   }
 360 
 361   // It sets up the heap region size (GrainBytes / GrainWords), as
 362   // well as other related fields that are based on the heap region
 363   // size (LogOfHRGrainBytes / LogOfHRGrainWords /
 364   // CardsPerRegion). All those fields are considered constant
 365   // throughout the JVM's execution, therefore they should only be set
 366   // up once during initialization time.
 367   static void setup_heap_region_size(uintx min_heap_size);
 368 
 369   enum ClaimValues {
 370     InitialClaimValue     = 0,
 371     FinalCountClaimValue  = 1,
 372     NoteEndClaimValue     = 2,
 373     ScrubRemSetClaimValue = 3,
 374     ParVerifyClaimValue   = 4,
 375     RebuildRSClaimValue   = 5
 376   };
 377 
 378   inline HeapWord* par_allocate_no_bot_updates(size_t word_size) {
 379     assert(is_young(), "we can only skip BOT updates on young regions");
 380     return ContiguousSpace::par_allocate(word_size);
 381   }
 382   inline HeapWord* allocate_no_bot_updates(size_t word_size) {
 383     assert(is_young(), "we can only skip BOT updates on young regions");
 384     return ContiguousSpace::allocate(word_size);
 385   }
 386 
 387   // If this region is a member of a HeapRegionSeq, the index in that
 388   // sequence, otherwise -1.
 389   size_t hrs_index() const { return _hrs_index; }
 390 
 391   // The number of bytes marked live in the region in the last marking phase.
 392   size_t marked_bytes()    { return _prev_marked_bytes; }
 393   size_t live_bytes() {
 394     return (top() - prev_top_at_mark_start()) * HeapWordSize + marked_bytes();
 395   }
 396 
 397   // The number of bytes counted in the next marking.
 398   size_t next_marked_bytes() { return _next_marked_bytes; }
 399   // The number of bytes live wrt the next marking.
 400   size_t next_live_bytes() {
 401     return
 402       (top() - next_top_at_mark_start()) * HeapWordSize + next_marked_bytes();
 403   }
 404 
 405   // A lower bound on the amount of garbage bytes in the region.
 406   size_t garbage_bytes() {
 407     size_t used_at_mark_start_bytes =
 408       (prev_top_at_mark_start() - bottom()) * HeapWordSize;
 409     assert(used_at_mark_start_bytes >= marked_bytes(),
 410            "Can't mark more than we have.");
 411     return used_at_mark_start_bytes - marked_bytes();
 412   }
 413 
 414   // An upper bound on the number of live bytes in the region.
 415   size_t max_live_bytes() { return used() - garbage_bytes(); }
 416 
 417   void add_to_marked_bytes(size_t incr_bytes) {
 418     _next_marked_bytes = _next_marked_bytes + incr_bytes;
 419     guarantee( _next_marked_bytes <= used(), "invariant" );
 420   }
 421 
 422   void zero_marked_bytes()      {
 423     _prev_marked_bytes = _next_marked_bytes = 0;
 424   }
 425 
 426   bool isHumongous() const { return _humongous_type != NotHumongous; }
 427   bool startsHumongous() const { return _humongous_type == StartsHumongous; }
 428   bool continuesHumongous() const { return _humongous_type == ContinuesHumongous; }
 429   // For a humongous region, region in which it starts.
 430   HeapRegion* humongous_start_region() const {
 431     return _humongous_start_region;
 432   }
 433 
 434   // Makes the current region be a "starts humongous" region, i.e.,
 435   // the first region in a series of one or more contiguous regions
 436   // that will contain a single "humongous" object. The two parameters
 437   // are as follows:
 438   //
 439   // new_top : The new value of the top field of this region which
 440   // points to the end of the humongous object that's being
 441   // allocated. If there is more than one region in the series, top
 442   // will lie beyond this region's original end field and on the last
 443   // region in the series.
 444   //
 445   // new_end : The new value of the end field of this region which
 446   // points to the end of the last region in the series. If there is
 447   // one region in the series (namely: this one) end will be the same
 448   // as the original end of this region.
 449   //
 450   // Updating top and end as described above makes this region look as
 451   // if it spans the entire space taken up by all the regions in the
 452   // series and an single allocation moved its top to new_top. This
 453   // ensures that the space (capacity / allocated) taken up by all
 454   // humongous regions can be calculated by just looking at the
 455   // "starts humongous" regions and by ignoring the "continues
 456   // humongous" regions.
 457   void set_startsHumongous(HeapWord* new_top, HeapWord* new_end);
 458 
 459   // Makes the current region be a "continues humongous'
 460   // region. first_hr is the "start humongous" region of the series
 461   // which this region will be part of.
 462   void set_continuesHumongous(HeapRegion* first_hr);
 463 
 464   // Unsets the humongous-related fields on the region.
 465   void set_notHumongous();
 466 
 467   // If the region has a remembered set, return a pointer to it.
 468   HeapRegionRemSet* rem_set() const {
 469     return _rem_set;
 470   }
 471 
 472   // True iff the region is in current collection_set.
 473   bool in_collection_set() const {
 474     return _in_collection_set;
 475   }
 476   void set_in_collection_set(bool b) {
 477     _in_collection_set = b;
 478   }
 479   HeapRegion* next_in_collection_set() {
 480     assert(in_collection_set(), "should only invoke on member of CS.");
 481     assert(_next_in_special_set == NULL ||
 482            _next_in_special_set->in_collection_set(),
 483            "Malformed CS.");
 484     return _next_in_special_set;
 485   }
 486   void set_next_in_collection_set(HeapRegion* r) {
 487     assert(in_collection_set(), "should only invoke on member of CS.");
 488     assert(r == NULL || r->in_collection_set(), "Malformed CS.");
 489     _next_in_special_set = r;
 490   }
 491 
 492   // Methods used by the HeapRegionSetBase class and subclasses.
 493 
 494   // Getter and setter for the next field used to link regions into
 495   // linked lists.
 496   HeapRegion* next()              { return _next; }
 497 
 498   void set_next(HeapRegion* next) { _next = next; }
 499 
 500   // Every region added to a set is tagged with a reference to that
 501   // set. This is used for doing consistency checking to make sure that
 502   // the contents of a set are as they should be and it's only
 503   // available in non-product builds.
 504 #ifdef ASSERT
 505   void set_containing_set(HeapRegionSetBase* containing_set) {
 506     assert((containing_set == NULL && _containing_set != NULL) ||
 507            (containing_set != NULL && _containing_set == NULL),
 508            err_msg("containing_set: "PTR_FORMAT" "
 509                    "_containing_set: "PTR_FORMAT,
 510                    containing_set, _containing_set));
 511 
 512     _containing_set = containing_set;
 513   }
 514 
 515   HeapRegionSetBase* containing_set() { return _containing_set; }
 516 #else // ASSERT
 517   void set_containing_set(HeapRegionSetBase* containing_set) { }
 518 
 519   // containing_set() is only used in asserts so there's no reason
 520   // to provide a dummy version of it.
 521 #endif // ASSERT
 522 
 523   // If we want to remove regions from a list in bulk we can simply tag
 524   // them with the pending_removal tag and call the
 525   // remove_all_pending() method on the list.
 526 
 527   bool pending_removal() { return _pending_removal; }
 528 
 529   void set_pending_removal(bool pending_removal) {
 530     if (pending_removal) {
 531       assert(!_pending_removal && containing_set() != NULL,
 532              "can only set pending removal to true if it's false and "
 533              "the region belongs to a region set");
 534     } else {
 535       assert( _pending_removal && containing_set() == NULL,
 536               "can only set pending removal to false if it's true and "
 537               "the region does not belong to a region set");
 538     }
 539 
 540     _pending_removal = pending_removal;
 541   }
 542 
 543   HeapRegion* get_next_young_region() { return _next_young_region; }
 544   void set_next_young_region(HeapRegion* hr) {
 545     _next_young_region = hr;
 546   }
 547 
 548   HeapRegion* get_next_dirty_cards_region() const { return _next_dirty_cards_region; }
 549   HeapRegion** next_dirty_cards_region_addr() { return &_next_dirty_cards_region; }
 550   void set_next_dirty_cards_region(HeapRegion* hr) { _next_dirty_cards_region = hr; }
 551   bool is_on_dirty_cards_region_list() const { return get_next_dirty_cards_region() != NULL; }
 552 
 553   HeapWord* orig_end() { return _orig_end; }
 554 
 555   // Allows logical separation between objects allocated before and after.
 556   void save_marks();
 557 
 558   // Reset HR stuff to default values.
 559   void hr_clear(bool par, bool clear_space);
 560   void par_clear();
 561 
 562   void initialize(MemRegion mr, bool clear_space, bool mangle_space);
 563 
 564   // Get the start of the unmarked area in this region.
 565   HeapWord* prev_top_at_mark_start() const { return _prev_top_at_mark_start; }
 566   HeapWord* next_top_at_mark_start() const { return _next_top_at_mark_start; }
 567 
 568   // Apply "cl->do_oop" to (the addresses of) all reference fields in objects
 569   // allocated in the current region before the last call to "save_mark".
 570   void oop_before_save_marks_iterate(OopClosure* cl);
 571 
 572   DirtyCardToOopClosure*
 573   new_dcto_closure(OopClosure* cl,
 574                    CardTableModRefBS::PrecisionStyle precision,
 575                    HeapRegionDCTOC::FilterKind fk);
 576 
 577   // Note the start or end of marking. This tells the heap region
 578   // that the collector is about to start or has finished (concurrently)
 579   // marking the heap.
 580 
 581   // Note the start of a marking phase. Record the
 582   // start of the unmarked area of the region here.
 583   void note_start_of_marking(bool during_initial_mark) {
 584     init_top_at_conc_mark_count();
 585     _next_marked_bytes = 0;
 586     if (during_initial_mark && is_young() && !is_survivor())
 587       _next_top_at_mark_start = bottom();
 588     else
 589       _next_top_at_mark_start = top();
 590   }
 591 
 592   // Note the end of a marking phase. Install the start of
 593   // the unmarked area that was captured at start of marking.
 594   void note_end_of_marking() {
 595     _prev_top_at_mark_start = _next_top_at_mark_start;
 596     _prev_marked_bytes = _next_marked_bytes;
 597     _next_marked_bytes = 0;
 598 
 599     guarantee(_prev_marked_bytes <=
 600               (size_t) (prev_top_at_mark_start() - bottom()) * HeapWordSize,
 601               "invariant");
 602   }
 603 
 604   // After an evacuation, we need to update _next_top_at_mark_start
 605   // to be the current top.  Note this is only valid if we have only
 606   // ever evacuated into this region.  If we evacuate, allocate, and
 607   // then evacuate we are in deep doodoo.
 608   void note_end_of_copying() {
 609     assert(top() >= _next_top_at_mark_start, "Increase only");
 610     _next_top_at_mark_start = top();
 611   }
 612 
 613   // Returns "false" iff no object in the region was allocated when the
 614   // last mark phase ended.
 615   bool is_marked() { return _prev_top_at_mark_start != bottom(); }
 616 
 617   // If "is_marked()" is true, then this is the index of the region in
 618   // an array constructed at the end of marking of the regions in a
 619   // "desirability" order.
 620   int sort_index() {
 621     return _sort_index;
 622   }
 623   void set_sort_index(int i) {
 624     _sort_index = i;
 625   }
 626 
 627   void init_top_at_conc_mark_count() {
 628     _top_at_conc_mark_count = bottom();
 629   }
 630 
 631   void set_top_at_conc_mark_count(HeapWord *cur) {
 632     assert(bottom() <= cur && cur <= end(), "Sanity.");
 633     _top_at_conc_mark_count = cur;
 634   }
 635 
 636   HeapWord* top_at_conc_mark_count() {
 637     return _top_at_conc_mark_count;
 638   }
 639 
 640   void reset_during_compaction() {
 641     guarantee( isHumongous() && startsHumongous(),
 642                "should only be called for humongous regions");
 643 
 644     zero_marked_bytes();
 645     init_top_at_mark_start();
 646   }
 647 
 648   // <PREDICTION>
 649   void calc_gc_efficiency(void);
 650   double gc_efficiency() { return _gc_efficiency;}
 651   // </PREDICTION>
 652 
 653   bool is_young() const     { return _young_type != NotYoung; }
 654   bool is_survivor() const  { return _young_type == Survivor; }
 655 
 656   int  young_index_in_cset() const { return _young_index_in_cset; }
 657   void set_young_index_in_cset(int index) {
 658     assert( (index == -1) || is_young(), "pre-condition" );
 659     _young_index_in_cset = index;
 660   }
 661 
 662   int age_in_surv_rate_group() {
 663     assert( _surv_rate_group != NULL, "pre-condition" );
 664     assert( _age_index > -1, "pre-condition" );
 665     return _surv_rate_group->age_in_group(_age_index);
 666   }
 667 
 668   void record_surv_words_in_group(size_t words_survived) {
 669     assert( _surv_rate_group != NULL, "pre-condition" );
 670     assert( _age_index > -1, "pre-condition" );
 671     int age_in_group = age_in_surv_rate_group();
 672     _surv_rate_group->record_surviving_words(age_in_group, words_survived);
 673   }
 674 
 675   int age_in_surv_rate_group_cond() {
 676     if (_surv_rate_group != NULL)
 677       return age_in_surv_rate_group();
 678     else
 679       return -1;
 680   }
 681 
 682   SurvRateGroup* surv_rate_group() {
 683     return _surv_rate_group;
 684   }
 685 
 686   void install_surv_rate_group(SurvRateGroup* surv_rate_group) {
 687     assert( surv_rate_group != NULL, "pre-condition" );
 688     assert( _surv_rate_group == NULL, "pre-condition" );
 689     assert( is_young(), "pre-condition" );
 690 
 691     _surv_rate_group = surv_rate_group;
 692     _age_index = surv_rate_group->next_age_index();
 693   }
 694 
 695   void uninstall_surv_rate_group() {
 696     if (_surv_rate_group != NULL) {
 697       assert( _age_index > -1, "pre-condition" );
 698       assert( is_young(), "pre-condition" );
 699 
 700       _surv_rate_group = NULL;
 701       _age_index = -1;
 702     } else {
 703       assert( _age_index == -1, "pre-condition" );
 704     }
 705   }
 706 
 707   void set_young() { set_young_type(Young); }
 708 
 709   void set_survivor() { set_young_type(Survivor); }
 710 
 711   void set_not_young() { set_young_type(NotYoung); }
 712 
 713   // Determine if an object has been allocated since the last
 714   // mark performed by the collector. This returns true iff the object
 715   // is within the unmarked area of the region.
 716   bool obj_allocated_since_prev_marking(oop obj) const {
 717     return (HeapWord *) obj >= prev_top_at_mark_start();
 718   }
 719   bool obj_allocated_since_next_marking(oop obj) const {
 720     return (HeapWord *) obj >= next_top_at_mark_start();
 721   }
 722 
 723   // For parallel heapRegion traversal.
 724   bool claimHeapRegion(int claimValue);
 725   jint claim_value() { return _claimed; }
 726   // Use this carefully: only when you're sure no one is claiming...
 727   void set_claim_value(int claimValue) { _claimed = claimValue; }
 728 
 729   // Returns the "evacuation_failed" property of the region.
 730   bool evacuation_failed() { return _evacuation_failed; }
 731 
 732   // Sets the "evacuation_failed" property of the region.
 733   void set_evacuation_failed(bool b) {
 734     _evacuation_failed = b;
 735 
 736     if (b) {
 737       init_top_at_conc_mark_count();
 738       _next_marked_bytes = 0;
 739     }
 740   }
 741 
 742   // Requires that "mr" be entirely within the region.
 743   // Apply "cl->do_object" to all objects that intersect with "mr".
 744   // If the iteration encounters an unparseable portion of the region,
 745   // or if "cl->abort()" is true after a closure application,
 746   // terminate the iteration and return the address of the start of the
 747   // subregion that isn't done.  (The two can be distinguished by querying
 748   // "cl->abort()".)  Return of "NULL" indicates that the iteration
 749   // completed.
 750   HeapWord*
 751   object_iterate_mem_careful(MemRegion mr, ObjectClosure* cl);
 752 
 753   // filter_young: if true and the region is a young region then we
 754   // skip the iteration.
 755   // card_ptr: if not NULL, and we decide that the card is not young
 756   // and we iterate over it, we'll clean the card before we start the
 757   // iteration.
 758   HeapWord*
 759   oops_on_card_seq_iterate_careful(MemRegion mr,
 760                                    FilterOutOfRegionClosure* cl,
 761                                    bool filter_young,
 762                                    jbyte* card_ptr);
 763 
 764   // A version of block start that is guaranteed to find *some* block
 765   // boundary at or before "p", but does not object iteration, and may
 766   // therefore be used safely when the heap is unparseable.
 767   HeapWord* block_start_careful(const void* p) const {
 768     return _offsets.block_start_careful(p);
 769   }
 770 
 771   // Requires that "addr" is within the region.  Returns the start of the
 772   // first ("careful") block that starts at or after "addr", or else the
 773   // "end" of the region if there is no such block.
 774   HeapWord* next_block_start_careful(HeapWord* addr);
 775 
 776   size_t recorded_rs_length() const        { return _recorded_rs_length; }
 777   double predicted_elapsed_time_ms() const { return _predicted_elapsed_time_ms; }
 778   size_t predicted_bytes_to_copy() const   { return _predicted_bytes_to_copy; }
 779 
 780   void set_recorded_rs_length(size_t rs_length) {
 781     _recorded_rs_length = rs_length;
 782   }
 783 
 784   void set_predicted_elapsed_time_ms(double ms) {
 785     _predicted_elapsed_time_ms = ms;
 786   }
 787 
 788   void set_predicted_bytes_to_copy(size_t bytes) {
 789     _predicted_bytes_to_copy = bytes;
 790   }
 791 
 792 #define HeapRegion_OOP_SINCE_SAVE_MARKS_DECL(OopClosureType, nv_suffix)  \
 793   virtual void oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl);
 794   SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(HeapRegion_OOP_SINCE_SAVE_MARKS_DECL)
 795 
 796   CompactibleSpace* next_compaction_space() const;
 797 
 798   virtual void reset_after_compaction();
 799 
 800   void print() const;
 801   void print_on(outputStream* st) const;
 802 
 803   // vo == UsePrevMarking  -> use "prev" marking information,
 804   // vo == UseNextMarking -> use "next" marking information
 805   // vo == UseMarkWord    -> use the mark word in the object header
 806   //
 807   // NOTE: Only the "prev" marking information is guaranteed to be
 808   // consistent most of the time, so most calls to this should use
 809   // vo == UsePrevMarking.
 810   // Currently, there is only one case where this is called with
 811   // vo == UseNextMarking, which is to verify the "next" marking
 812   // information at the end of remark.
 813   // Currently there is only one place where this is called with
 814   // vo == UseMarkWord, which is to verify the marking during a
 815   // full GC.
 816   void verify(bool allow_dirty, VerifyOption vo, bool *failures) const;
 817 
 818   // Override; it uses the "prev" marking information
 819   virtual void verify(bool allow_dirty) const;
 820 };
 821 
 822 // HeapRegionClosure is used for iterating over regions.
 823 // Terminates the iteration when the "doHeapRegion" method returns "true".
 824 class HeapRegionClosure : public StackObj {
 825   friend class HeapRegionSeq;
 826   friend class G1CollectedHeap;
 827 
 828   bool _complete;
 829   void incomplete() { _complete = false; }
 830 
 831  public:
 832   HeapRegionClosure(): _complete(true) {}
 833 
 834   // Typically called on each region until it returns true.
 835   virtual bool doHeapRegion(HeapRegion* r) = 0;
 836 
 837   // True after iteration if the closure was applied to all heap regions
 838   // and returned "false" in all cases.
 839   bool complete() { return _complete; }
 840 };
 841 
 842 #endif // SERIALGC
 843 
 844 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_HPP