1 /*
   2  * Copyright (c) 2001, 2019, 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_GC_G1_HEAPREGION_HPP
  26 #define SHARE_GC_G1_HEAPREGION_HPP
  27 
  28 #include "gc/g1/g1BlockOffsetTable.hpp"
  29 #include "gc/g1/g1HeapRegionTraceType.hpp"
  30 #include "gc/g1/heapRegionTracer.hpp"
  31 #include "gc/g1/heapRegionType.hpp"
  32 #include "gc/g1/survRateGroup.hpp"
  33 #include "gc/shared/ageTable.hpp"
  34 #include "gc/shared/spaceDecorator.hpp"
  35 #include "gc/shared/verifyOption.hpp"
  36 #include "runtime/mutex.hpp"
  37 #include "utilities/macros.hpp"
  38 
  39 class G1CollectedHeap;
  40 class G1CMBitMap;
  41 class HeapRegionRemSet;
  42 class HeapRegion;
  43 class HeapRegionSetBase;
  44 class nmethod;
  45 
  46 #define HR_FORMAT "%u:(%s)[" PTR_FORMAT "," PTR_FORMAT "," PTR_FORMAT "]"
  47 #define HR_FORMAT_PARAMS(_hr_) \
  48                 (_hr_)->hrm_index(), \
  49                 (_hr_)->get_short_type_str(), \
  50                 p2i((_hr_)->bottom()), p2i((_hr_)->top()), p2i((_hr_)->end())
  51 
  52 // sentinel value for hrm_index
  53 #define G1_NO_HRM_INDEX ((uint) -1)
  54 
  55 // A HeapRegion is the smallest piece of a G1CollectedHeap that
  56 // can be collected independently.
  57 
  58 // Each heap region is self contained. top() and end() can never
  59 // be set beyond the end of the region. For humongous objects,
  60 // the first region is a StartsHumongous region. If the humongous
  61 // object is larger than a heap region, the following regions will
  62 // be of type ContinuesHumongous. In this case the top() of the
  63 // StartHumongous region and all ContinuesHumongous regions except
  64 // the last will point to their own end. The last ContinuesHumongous
  65 // region may have top() equal the end of object if there isn't
  66 // room for filler objects to pad out to the end of the region.
  67 class HeapRegion : public CHeapObj<mtGC> {
  68   friend class VMStructs;
  69 
  70   HeapWord* _bottom;
  71   HeapWord* _end;
  72 
  73   HeapWord* volatile _top;
  74   HeapWord* _compaction_top;
  75 
  76   G1BlockOffsetTablePart _bot_part;
  77   Mutex _par_alloc_lock;
  78   // When we need to retire an allocation region, while other threads
  79   // are also concurrently trying to allocate into it, we typically
  80   // allocate a dummy object at the end of the region to ensure that
  81   // no more allocations can take place in it. However, sometimes we
  82   // want to know where the end of the last "real" object we allocated
  83   // into the region was and this is what this keeps track.
  84   HeapWord* _pre_dummy_top;
  85 
  86 public:
  87   void set_bottom(HeapWord* value) { _bottom = value; }
  88   HeapWord* bottom() const         { return _bottom; }
  89 
  90   void set_end(HeapWord* value)    { _end = value; }
  91   HeapWord* end() const            { return _end;    }
  92 
  93   void set_compaction_top(HeapWord* compaction_top) { _compaction_top = compaction_top; }
  94   HeapWord* compaction_top() const { return _compaction_top; }
  95 
  96   void set_top(HeapWord* value) { _top = value; }
  97   HeapWord* top() const { return _top; }
  98 
  99   // Returns true iff the given the heap  region contains the
 100   // given address as part of an allocated object. This may
 101   // be a potentially, so we restrict its use to assertion checks only.
 102   bool is_in(const void* p) const {
 103     return is_in_reserved(p);
 104   }
 105   bool is_in(oop obj) const {
 106     return is_in((void*)obj);
 107   }
 108   // Returns true iff the given reserved memory of the space contains the
 109   // given address.
 110   bool is_in_reserved(const void* p) const { return _bottom <= p && p < _end; }
 111 
 112   size_t capacity()     const { return byte_size(bottom(), end()); }
 113   size_t used() const { return byte_size(bottom(), top()); }
 114   size_t free() const { return byte_size(top(), end()); }
 115 
 116   bool is_empty() const { return used() == 0; }
 117 
 118 private:
 119   void reset_after_compaction() { set_top(compaction_top()); }
 120 
 121   // Try to allocate at least min_word_size and up to desired_size from this region.
 122   // Returns NULL if not possible, otherwise sets actual_word_size to the amount of
 123   // space allocated.
 124   // This version assumes that all allocation requests to this HeapRegion are properly
 125   // synchronized.
 126   inline HeapWord* allocate_impl(size_t min_word_size, size_t desired_word_size, size_t* actual_word_size);
 127   // Try to allocate at least min_word_size and up to desired_size from this HeapRegion.
 128   // Returns NULL if not possible, otherwise sets actual_word_size to the amount of
 129   // space allocated.
 130   // This version synchronizes with other calls to par_allocate_impl().
 131   inline HeapWord* par_allocate_impl(size_t min_word_size, size_t desired_word_size, size_t* actual_word_size);
 132 
 133   void mangle_unused_area() PRODUCT_RETURN;
 134 
 135 public:
 136   void object_iterate(ObjectClosure* blk);
 137 
 138   // See the comment above in the declaration of _pre_dummy_top for an
 139   // explanation of what it is.
 140   void set_pre_dummy_top(HeapWord* pre_dummy_top) {
 141     assert(is_in(pre_dummy_top) && pre_dummy_top <= top(), "pre-condition");
 142     _pre_dummy_top = pre_dummy_top;
 143   }
 144   
 145   HeapWord* pre_dummy_top() {
 146     return (_pre_dummy_top == NULL) ? top() : _pre_dummy_top;
 147   }
 148   void reset_pre_dummy_top() { _pre_dummy_top = NULL; }
 149 
 150   void clear(bool mangle_space);
 151 
 152   HeapWord* block_start(const void* p);
 153   HeapWord* block_start_const(const void* p) const;
 154 
 155   // Allocation (return NULL if full).  Assumes the caller has established
 156   // mutually exclusive access to the HeapRegion.
 157   HeapWord* allocate(size_t min_word_size, size_t desired_word_size, size_t* actual_word_size);
 158   // Allocation (return NULL if full).  Enforces mutual exclusion internally.
 159   HeapWord* par_allocate(size_t min_word_size, size_t desired_word_size, size_t* actual_word_size);
 160 
 161   HeapWord* allocate(size_t word_size);
 162   HeapWord* par_allocate(size_t word_size);
 163 
 164   HeapWord* saved_mark_word() const { ShouldNotReachHere(); return NULL; }
 165 
 166   // MarkSweep support phase3
 167   HeapWord* initialize_threshold();
 168   HeapWord* cross_threshold(HeapWord* start, HeapWord* end);
 169 
 170   void reset_bot() {
 171     _bot_part.reset_bot();
 172   }
 173 
 174   void print_bot_on(outputStream* out) {
 175     _bot_part.print_on(out);
 176   }
 177 
 178 private:
 179   // The remembered set for this region.
 180   HeapRegionRemSet* _rem_set;
 181 
 182   void report_region_type_change(G1HeapRegionTraceType::Type to);
 183 
 184   // Returns whether the given object address refers to a dead object, and either the
 185   // size of the object (if live) or the size of the block (if dead) in size.
 186   // May
 187   // - only called with obj < top()
 188   // - not called on humongous objects or archive regions
 189   inline bool is_obj_dead_with_size(const oop obj, const G1CMBitMap* const prev_bitmap, size_t* size) const;
 190 
 191   // The index of this region in the heap region sequence.
 192   uint  _hrm_index;
 193 
 194   HeapRegionType _type;
 195 
 196   // For a humongous region, region in which it starts.
 197   HeapRegion* _humongous_start_region;
 198 
 199   // True iff an attempt to evacuate an object in the region failed.
 200   bool _evacuation_failed;
 201 
 202   // Fields used by the HeapRegionSetBase class and subclasses.
 203   HeapRegion* _next;
 204   HeapRegion* _prev;
 205 #ifdef ASSERT
 206   HeapRegionSetBase* _containing_set;
 207 #endif // ASSERT
 208 
 209   // We use concurrent marking to determine the amount of live data
 210   // in each heap region.
 211   size_t _prev_marked_bytes;    // Bytes known to be live via last completed marking.
 212   size_t _next_marked_bytes;    // Bytes known to be live via in-progress marking.
 213 
 214   // The calculated GC efficiency of the region.
 215   double _gc_efficiency;
 216 
 217   static const uint InvalidCSetIndex = UINT_MAX;
 218 
 219   // The index in the optional regions array, if this region
 220   // is considered optional during a mixed collections.
 221   uint _index_in_opt_cset;
 222 
 223   // Data for young region survivor prediction.
 224   uint  _young_index_in_cset;
 225   SurvRateGroup* _surv_rate_group;
 226   int  _age_index;
 227 
 228   // The start of the unmarked area. The unmarked area extends from this
 229   // word until the top and/or end of the region, and is the part
 230   // of the region for which no marking was done, i.e. objects may
 231   // have been allocated in this part since the last mark phase.
 232   // "prev" is the top at the start of the last completed marking.
 233   // "next" is the top at the start of the in-progress marking (if any.)
 234   HeapWord* _prev_top_at_mark_start;
 235   HeapWord* _next_top_at_mark_start;
 236 
 237   void init_top_at_mark_start() {
 238     assert(_prev_marked_bytes == 0 &&
 239            _next_marked_bytes == 0,
 240            "Must be called after zero_marked_bytes.");
 241     HeapWord* bot = bottom();
 242     _prev_top_at_mark_start = bot;
 243     _next_top_at_mark_start = bot;
 244   }
 245 
 246   // Cached attributes used in the collection set policy information
 247 
 248   // The RSet length that was added to the total value
 249   // for the collection set.
 250   size_t _recorded_rs_length;
 251 
 252   // The predicted elapsed time that was added to total value
 253   // for the collection set.
 254   double _predicted_elapsed_time_ms;
 255 
 256   // Iterate over the references covered by the given MemRegion in a humongous
 257   // object and apply the given closure to them.
 258   // Humongous objects are allocated directly in the old-gen. So we need special
 259   // handling for concurrent processing encountering an in-progress allocation.
 260   // Returns the address after the last actually scanned or NULL if the area could
 261   // not be scanned (That should only happen when invoked concurrently with the
 262   // mutator).
 263   template <class Closure, bool is_gc_active>
 264   inline HeapWord* do_oops_on_memregion_in_humongous(MemRegion mr,
 265                                                      Closure* cl,
 266                                                      G1CollectedHeap* g1h);
 267 
 268   // Returns the block size of the given (dead, potentially having its class unloaded) object
 269   // starting at p extending to at most the prev TAMS using the given mark bitmap.
 270   inline size_t block_size_using_bitmap(const HeapWord* p, const G1CMBitMap* const prev_bitmap) const;
 271 public:
 272   HeapRegion(uint hrm_index, G1BlockOffsetTable* bot, MemRegion mr);
 273 
 274   // Initializing the HeapRegion not only resets the data structure, but also
 275   // resets the BOT for that heap region.
 276   // The default values for clear_space means that we will do the clearing if
 277   // there's clearing to be done ourselves. We also always mangle the space.
 278   void initialize(MemRegion mr, bool clear_space = false, bool mangle_space = SpaceDecorator::Mangle);
 279 
 280   static int    LogOfHRGrainBytes;
 281   static int    LogOfHRGrainWords;
 282   static int    LogCardsPerRegion;
 283 
 284   static size_t GrainBytes;
 285   static size_t GrainWords;
 286   static size_t CardsPerRegion;
 287 
 288   static size_t align_up_to_region_byte_size(size_t sz) {
 289     return (sz + (size_t) GrainBytes - 1) &
 290                                       ~((1 << (size_t) LogOfHRGrainBytes) - 1);
 291   }
 292 
 293 
 294   // Returns whether a field is in the same region as the obj it points to.
 295   template <typename T>
 296   static bool is_in_same_region(T* p, oop obj) {
 297     assert(p != NULL, "p can't be NULL");
 298     assert(obj != NULL, "obj can't be NULL");
 299     return (((uintptr_t) p ^ cast_from_oop<uintptr_t>(obj)) >> LogOfHRGrainBytes) == 0;
 300   }
 301 
 302   static size_t max_region_size();
 303   static size_t min_region_size_in_words();
 304 
 305   // It sets up the heap region size (GrainBytes / GrainWords), as
 306   // well as other related fields that are based on the heap region
 307   // size (LogOfHRGrainBytes / LogOfHRGrainWords /
 308   // CardsPerRegion). All those fields are considered constant
 309   // throughout the JVM's execution, therefore they should only be set
 310   // up once during initialization time.
 311   static void setup_heap_region_size(size_t initial_heap_size, size_t max_heap_size);
 312 
 313   // All allocated blocks are occupied by objects in a HeapRegion
 314   bool block_is_obj(const HeapWord* p) const;
 315 
 316   // Returns whether the given object is dead based on TAMS and bitmap.
 317   bool is_obj_dead(const oop obj, const G1CMBitMap* const prev_bitmap) const;
 318 
 319   // Returns the object size for all valid block starts
 320   // and the amount of unallocated words if called on top()
 321   size_t block_size(const HeapWord* p) const;
 322 
 323   // Scans through the region using the bitmap to determine what
 324   // objects to call size_t ApplyToMarkedClosure::apply(oop) for.
 325   template<typename ApplyToMarkedClosure>
 326   inline void apply_to_marked_objects(G1CMBitMap* bitmap, ApplyToMarkedClosure* closure);
 327   // Update heap region to be consistent after compaction.
 328   void complete_compaction();
 329 
 330   inline HeapWord* par_allocate_no_bot_updates(size_t min_word_size, size_t desired_word_size, size_t* word_size);
 331   inline HeapWord* allocate_no_bot_updates(size_t word_size);
 332   inline HeapWord* allocate_no_bot_updates(size_t min_word_size, size_t desired_word_size, size_t* actual_size);
 333 
 334   // If this region is a member of a HeapRegionManager, the index in that
 335   // sequence, otherwise -1.
 336   uint hrm_index() const { return _hrm_index; }
 337 
 338   // The number of bytes marked live in the region in the last marking phase.
 339   size_t marked_bytes()    { return _prev_marked_bytes; }
 340   size_t live_bytes() {
 341     return (top() - prev_top_at_mark_start()) * HeapWordSize + marked_bytes();
 342   }
 343 
 344   // The number of bytes counted in the next marking.
 345   size_t next_marked_bytes() { return _next_marked_bytes; }
 346   // The number of bytes live wrt the next marking.
 347   size_t next_live_bytes() {
 348     return
 349       (top() - next_top_at_mark_start()) * HeapWordSize + next_marked_bytes();
 350   }
 351 
 352   // A lower bound on the amount of garbage bytes in the region.
 353   size_t garbage_bytes() {
 354     size_t used_at_mark_start_bytes =
 355       (prev_top_at_mark_start() - bottom()) * HeapWordSize;
 356     return used_at_mark_start_bytes - marked_bytes();
 357   }
 358 
 359   // Return the amount of bytes we'll reclaim if we collect this
 360   // region. This includes not only the known garbage bytes in the
 361   // region but also any unallocated space in it, i.e., [top, end),
 362   // since it will also be reclaimed if we collect the region.
 363   size_t reclaimable_bytes() {
 364     size_t known_live_bytes = live_bytes();
 365     assert(known_live_bytes <= capacity(), "sanity");
 366     return capacity() - known_live_bytes;
 367   }
 368 
 369   // An upper bound on the number of live bytes in the region.
 370   size_t max_live_bytes() { return used() - garbage_bytes(); }
 371 
 372   void add_to_marked_bytes(size_t incr_bytes) {
 373     _next_marked_bytes = _next_marked_bytes + incr_bytes;
 374   }
 375 
 376   void zero_marked_bytes()      {
 377     _prev_marked_bytes = _next_marked_bytes = 0;
 378   }
 379 
 380   const char* get_type_str() const { return _type.get_str(); }
 381   const char* get_short_type_str() const { return _type.get_short_str(); }
 382   G1HeapRegionTraceType::Type get_trace_type() { return _type.get_trace_type(); }
 383 
 384   bool is_free() const { return _type.is_free(); }
 385 
 386   bool is_young()    const { return _type.is_young();    }
 387   bool is_eden()     const { return _type.is_eden();     }
 388   bool is_survivor() const { return _type.is_survivor(); }
 389 
 390   bool is_humongous() const { return _type.is_humongous(); }
 391   bool is_starts_humongous() const { return _type.is_starts_humongous(); }
 392   bool is_continues_humongous() const { return _type.is_continues_humongous();   }
 393 
 394   bool is_old() const { return _type.is_old(); }
 395 
 396   bool is_old_or_humongous() const { return _type.is_old_or_humongous(); }
 397 
 398   bool is_old_or_humongous_or_archive() const { return _type.is_old_or_humongous_or_archive(); }
 399 
 400   // A pinned region contains objects which are not moved by garbage collections.
 401   // Humongous regions and archive regions are pinned.
 402   bool is_pinned() const { return _type.is_pinned(); }
 403 
 404   // An archive region is a pinned region, also tagged as old, which
 405   // should not be marked during mark/sweep. This allows the address
 406   // space to be shared by JVM instances.
 407   bool is_archive()        const { return _type.is_archive(); }
 408   bool is_open_archive()   const { return _type.is_open_archive(); }
 409   bool is_closed_archive() const { return _type.is_closed_archive(); }
 410 
 411   // For a humongous region, region in which it starts.
 412   HeapRegion* humongous_start_region() const {
 413     return _humongous_start_region;
 414   }
 415 
 416   // Makes the current region be a "starts humongous" region, i.e.,
 417   // the first region in a series of one or more contiguous regions
 418   // that will contain a single "humongous" object.
 419   //
 420   // obj_top : points to the top of the humongous object.
 421   // fill_size : size of the filler object at the end of the region series.
 422   void set_starts_humongous(HeapWord* obj_top, size_t fill_size);
 423 
 424   // Makes the current region be a "continues humongous'
 425   // region. first_hr is the "start humongous" region of the series
 426   // which this region will be part of.
 427   void set_continues_humongous(HeapRegion* first_hr);
 428 
 429   // Unsets the humongous-related fields on the region.
 430   void clear_humongous();
 431 
 432   // If the region has a remembered set, return a pointer to it.
 433   HeapRegionRemSet* rem_set() const {
 434     return _rem_set;
 435   }
 436 
 437   inline bool in_collection_set() const;
 438 
 439   // Methods used by the HeapRegionSetBase class and subclasses.
 440 
 441   // Getter and setter for the next and prev fields used to link regions into
 442   // linked lists.
 443   HeapRegion* next()              { return _next; }
 444   HeapRegion* prev()              { return _prev; }
 445 
 446   void set_next(HeapRegion* next) { _next = next; }
 447   void set_prev(HeapRegion* prev) { _prev = prev; }
 448 
 449   // Every region added to a set is tagged with a reference to that
 450   // set. This is used for doing consistency checking to make sure that
 451   // the contents of a set are as they should be and it's only
 452   // available in non-product builds.
 453 #ifdef ASSERT
 454   void set_containing_set(HeapRegionSetBase* containing_set) {
 455     assert((containing_set == NULL && _containing_set != NULL) ||
 456            (containing_set != NULL && _containing_set == NULL),
 457            "containing_set: " PTR_FORMAT " "
 458            "_containing_set: " PTR_FORMAT,
 459            p2i(containing_set), p2i(_containing_set));
 460 
 461     _containing_set = containing_set;
 462   }
 463 
 464   HeapRegionSetBase* containing_set() { return _containing_set; }
 465 #else // ASSERT
 466   void set_containing_set(HeapRegionSetBase* containing_set) { }
 467 
 468   // containing_set() is only used in asserts so there's no reason
 469   // to provide a dummy version of it.
 470 #endif // ASSERT
 471 
 472 
 473   // Reset the HeapRegion to default values.
 474   // If skip_remset is true, do not clear the remembered set.
 475   // If clear_space is true, clear the HeapRegion's memory.
 476   // If locked is true, assume we are the only thread doing this operation.
 477   void hr_clear(bool skip_remset, bool clear_space, bool locked = false);
 478   // Clear the card table corresponding to this region.
 479   void clear_cardtable();
 480 
 481   // Get the start of the unmarked area in this region.
 482   HeapWord* prev_top_at_mark_start() const { return _prev_top_at_mark_start; }
 483   HeapWord* next_top_at_mark_start() const { return _next_top_at_mark_start; }
 484 
 485   // Note the start or end of marking. This tells the heap region
 486   // that the collector is about to start or has finished (concurrently)
 487   // marking the heap.
 488 
 489   // Notify the region that concurrent marking is starting. Initialize
 490   // all fields related to the next marking info.
 491   inline void note_start_of_marking();
 492 
 493   // Notify the region that concurrent marking has finished. Copy the
 494   // (now finalized) next marking info fields into the prev marking
 495   // info fields.
 496   inline void note_end_of_marking();
 497 
 498   // Notify the region that we are about to start processing
 499   // self-forwarded objects during evac failure handling.
 500   void note_self_forwarding_removal_start(bool during_initial_mark,
 501                                           bool during_conc_mark);
 502 
 503   // Notify the region that we have finished processing self-forwarded
 504   // objects during evac failure handling.
 505   void note_self_forwarding_removal_end(size_t marked_bytes);
 506 
 507   void reset_during_compaction() {
 508     assert(is_humongous(),
 509            "should only be called for humongous regions");
 510 
 511     zero_marked_bytes();
 512     init_top_at_mark_start();
 513   }
 514 
 515   void calc_gc_efficiency(void);
 516   double gc_efficiency() const { return _gc_efficiency;}
 517 
 518   uint index_in_opt_cset() const {
 519     assert(has_index_in_opt_cset(), "Opt cset index not set.");
 520     return _index_in_opt_cset;
 521   }
 522   bool has_index_in_opt_cset() const { return _index_in_opt_cset != InvalidCSetIndex; }
 523   void set_index_in_opt_cset(uint index) { _index_in_opt_cset = index; }
 524   void clear_index_in_opt_cset() { _index_in_opt_cset = InvalidCSetIndex; }
 525 
 526   uint  young_index_in_cset() const { return _young_index_in_cset; }
 527   void clear_young_index_in_cset() { _young_index_in_cset = 0; }
 528   void set_young_index_in_cset(uint index) {
 529     assert(index != UINT_MAX, "just checking");
 530     assert(index != 0, "just checking");
 531     assert(is_young(), "pre-condition");
 532     _young_index_in_cset = index;
 533   }
 534 
 535   int age_in_surv_rate_group() {
 536     assert(_surv_rate_group != NULL, "pre-condition");
 537     assert(_age_index > -1, "pre-condition");
 538     return _surv_rate_group->age_in_group(_age_index);
 539   }
 540 
 541   void record_surv_words_in_group(size_t words_survived) {
 542     assert(_surv_rate_group != NULL, "pre-condition");
 543     assert(_age_index > -1, "pre-condition");
 544     int age_in_group = age_in_surv_rate_group();
 545     _surv_rate_group->record_surviving_words(age_in_group, words_survived);
 546   }
 547 
 548   int age_in_surv_rate_group_cond() {
 549     if (_surv_rate_group != NULL)
 550       return age_in_surv_rate_group();
 551     else
 552       return -1;
 553   }
 554 
 555   SurvRateGroup* surv_rate_group() {
 556     return _surv_rate_group;
 557   }
 558 
 559   void install_surv_rate_group(SurvRateGroup* surv_rate_group) {
 560     assert(surv_rate_group != NULL, "pre-condition");
 561     assert(_surv_rate_group == NULL, "pre-condition");
 562     assert(is_young(), "pre-condition");
 563 
 564     _surv_rate_group = surv_rate_group;
 565     _age_index = surv_rate_group->next_age_index();
 566   }
 567 
 568   void uninstall_surv_rate_group() {
 569     if (_surv_rate_group != NULL) {
 570       assert(_age_index > -1, "pre-condition");
 571       assert(is_young(), "pre-condition");
 572 
 573       _surv_rate_group = NULL;
 574       _age_index = -1;
 575     } else {
 576       assert(_age_index == -1, "pre-condition");
 577     }
 578   }
 579 
 580   void set_free();
 581 
 582   void set_eden();
 583   void set_eden_pre_gc();
 584   void set_survivor();
 585 
 586   void move_to_old();
 587   void set_old();
 588 
 589   void set_open_archive();
 590   void set_closed_archive();
 591 
 592   // Determine if an object has been allocated since the last
 593   // mark performed by the collector. This returns true iff the object
 594   // is within the unmarked area of the region.
 595   bool obj_allocated_since_prev_marking(oop obj) const {
 596     return (HeapWord *) obj >= prev_top_at_mark_start();
 597   }
 598   bool obj_allocated_since_next_marking(oop obj) const {
 599     return (HeapWord *) obj >= next_top_at_mark_start();
 600   }
 601 
 602   // Returns the "evacuation_failed" property of the region.
 603   bool evacuation_failed() { return _evacuation_failed; }
 604 
 605   // Sets the "evacuation_failed" property of the region.
 606   void set_evacuation_failed(bool b) {
 607     _evacuation_failed = b;
 608 
 609     if (b) {
 610       _next_marked_bytes = 0;
 611     }
 612   }
 613 
 614   // Iterate over the objects overlapping the given memory region, applying cl
 615   // to all references in the region.  This is a helper for
 616   // G1RemSet::refine_card*, and is tightly coupled with them.
 617   // mr must not be empty. Must be trimmed to the allocated/parseable space in this region.
 618   // This region must be old or humongous.
 619   // Returns the next unscanned address if the designated objects were successfully
 620   // processed, NULL if an unparseable part of the heap was encountered (That should
 621   // only happen when invoked concurrently with the mutator).
 622   template <bool is_gc_active, class Closure>
 623   inline HeapWord* oops_on_memregion_seq_iterate_careful(MemRegion mr, Closure* cl);
 624 
 625   size_t recorded_rs_length() const        { return _recorded_rs_length; }
 626   double predicted_elapsed_time_ms() const { return _predicted_elapsed_time_ms; }
 627 
 628   void set_recorded_rs_length(size_t rs_length) {
 629     _recorded_rs_length = rs_length;
 630   }
 631 
 632   void set_predicted_elapsed_time_ms(double ms) {
 633     _predicted_elapsed_time_ms = ms;
 634   }
 635 
 636   // Routines for managing a list of code roots (attached to the
 637   // this region's RSet) that point into this heap region.
 638   void add_strong_code_root(nmethod* nm);
 639   void add_strong_code_root_locked(nmethod* nm);
 640   void remove_strong_code_root(nmethod* nm);
 641 
 642   // Applies blk->do_code_blob() to each of the entries in
 643   // the strong code roots list for this region
 644   void strong_code_roots_do(CodeBlobClosure* blk) const;
 645 
 646   // Verify that the entries on the strong code root list for this
 647   // region are live and include at least one pointer into this region.
 648   void verify_strong_code_roots(VerifyOption vo, bool* failures) const;
 649 
 650   void print() const;
 651   void print_on(outputStream* st) const;
 652 
 653   // vo == UsePrevMarking -> use "prev" marking information,
 654   // vo == UseNextMarking -> use "next" marking information
 655   // vo == UseFullMarking -> use "next" marking bitmap but no TAMS
 656   //
 657   // NOTE: Only the "prev" marking information is guaranteed to be
 658   // consistent most of the time, so most calls to this should use
 659   // vo == UsePrevMarking.
 660   // Currently, there is only one case where this is called with
 661   // vo == UseNextMarking, which is to verify the "next" marking
 662   // information at the end of remark.
 663   // Currently there is only one place where this is called with
 664   // vo == UseFullMarking, which is to verify the marking during a
 665   // full GC.
 666   void verify(VerifyOption vo, bool *failures) const;
 667 
 668   // Verify using the "prev" marking information
 669   void verify() const;
 670 
 671   void verify_rem_set(VerifyOption vo, bool *failures) const;
 672   void verify_rem_set() const;
 673 };
 674 
 675 // HeapRegionClosure is used for iterating over regions.
 676 // Terminates the iteration when the "do_heap_region" method returns "true".
 677 class HeapRegionClosure : public StackObj {
 678   friend class HeapRegionManager;
 679   friend class G1CollectionSet;
 680   friend class G1CollectionSetCandidates;
 681 
 682   bool _is_complete;
 683   void set_incomplete() { _is_complete = false; }
 684 
 685 public:
 686   HeapRegionClosure(): _is_complete(true) {}
 687 
 688   // Typically called on each region until it returns true.
 689   virtual bool do_heap_region(HeapRegion* r) = 0;
 690 
 691   // True after iteration if the closure was applied to all heap regions
 692   // and returned "false" in all cases.
 693   bool is_complete() { return _is_complete; }
 694 };
 695 
 696 #endif // SHARE_GC_G1_HEAPREGION_HPP