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_VM_GC_IMPLEMENTATION_G1_HEAPREGION_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_HPP
  27 
  28 #include "gc_implementation/g1/g1AllocationContext.hpp"
  29 #include "gc_implementation/g1/g1BlockOffsetTable.hpp"
  30 #include "gc_implementation/g1/g1_specialized_oop_closures.hpp"
  31 #include "gc_implementation/g1/heapRegionType.hpp"
  32 #include "gc_implementation/g1/survRateGroup.hpp"
  33 #include "gc_implementation/g1/g1HeapRegionTraceType.hpp"
  34 #include "gc_implementation/shared/ageTable.hpp"
  35 #include "gc_implementation/shared/spaceDecorator.hpp"
  36 #include "memory/space.inline.hpp"
  37 #include "memory/watermark.hpp"
  38 #include "utilities/macros.hpp"
  39 
  40 // A HeapRegion is the smallest piece of a G1CollectedHeap that
  41 // can be collected independently.
  42 
  43 // NOTE: Although a HeapRegion is a Space, its
  44 // Space::initDirtyCardClosure method must not be called.
  45 // The problem is that the existence of this method breaks
  46 // the independence of barrier sets from remembered sets.
  47 // The solution is to remove this method from the definition
  48 // of a Space.
  49 
  50 class HeapRegionRemSet;
  51 class HeapRegionRemSetIterator;
  52 class HeapRegion;
  53 class HeapRegionSetBase;
  54 class nmethod;
  55 
  56 #define HR_FORMAT "%u:(%s)[" PTR_FORMAT "," PTR_FORMAT "," PTR_FORMAT "]"
  57 #define HR_FORMAT_PARAMS(_hr_) \
  58                 (_hr_)->hrm_index(), \
  59                 (_hr_)->get_short_type_str(), \
  60                 p2i((_hr_)->bottom()), p2i((_hr_)->top()), p2i((_hr_)->end())
  61 
  62 // sentinel value for hrm_index
  63 #define G1_NO_HRM_INDEX ((uint) -1)
  64 
  65 // A dirty card to oop closure for heap regions. It
  66 // knows how to get the G1 heap and how to use the bitmap
  67 // in the concurrent marker used by G1 to filter remembered
  68 // sets.
  69 
  70 class HeapRegionDCTOC : public DirtyCardToOopClosure {
  71 private:
  72   HeapRegion* _hr;
  73   G1ParPushHeapRSClosure* _rs_scan;
  74   G1CollectedHeap* _g1;
  75 
  76   // Walk the given memory region from bottom to (actual) top
  77   // looking for objects and applying the oop closure (_cl) to
  78   // them. The base implementation of this treats the area as
  79   // blocks, where a block may or may not be an object. Sub-
  80   // classes should override this to provide more accurate
  81   // or possibly more efficient walking.
  82   void walk_mem_region(MemRegion mr, HeapWord* bottom, HeapWord* top);
  83 
  84 public:
  85   HeapRegionDCTOC(G1CollectedHeap* g1,
  86                   HeapRegion* hr,
  87                   G1ParPushHeapRSClosure* cl,
  88                   CardTableModRefBS::PrecisionStyle precision);
  89 };
  90 
  91 // The complicating factor is that BlockOffsetTable diverged
  92 // significantly, and we need functionality that is only in the G1 version.
  93 // So I copied that code, which led to an alternate G1 version of
  94 // OffsetTableContigSpace.  If the two versions of BlockOffsetTable could
  95 // be reconciled, then G1OffsetTableContigSpace could go away.
  96 
  97 // The idea behind time stamps is the following. We want to keep track of
  98 // the highest address where it's safe to scan objects for each region.
  99 // This is only relevant for current GC alloc regions so we keep a time stamp
 100 // per region to determine if the region has been allocated during the current
 101 // GC or not. If the time stamp is current we report a scan_top value which
 102 // was saved at the end of the previous GC for retained alloc regions and which is
 103 // equal to the bottom for all other regions.
 104 // There is a race between card scanners and allocating gc workers where we must ensure
 105 // that card scanners do not read the memory allocated by the gc workers.
 106 // In order to enforce that, we must not return a value of _top which is more recent than the
 107 // time stamp. This is due to the fact that a region may become a gc alloc region at
 108 // some point after we've read the timestamp value as being < the current time stamp.
 109 // The time stamps are re-initialized to zero at cleanup and at Full GCs.
 110 // The current scheme that uses sequential unsigned ints will fail only if we have 4b
 111 // evacuation pauses between two cleanups, which is _highly_ unlikely.
 112 class G1OffsetTableContigSpace: public CompactibleSpace {
 113   friend class VMStructs;
 114   HeapWord* _top;
 115   HeapWord* volatile _scan_top;
 116  protected:
 117   G1BlockOffsetArrayContigSpace _offsets;
 118   Mutex _par_alloc_lock;
 119   volatile unsigned _gc_time_stamp;
 120   // When we need to retire an allocation region, while other threads
 121   // are also concurrently trying to allocate into it, we typically
 122   // allocate a dummy object at the end of the region to ensure that
 123   // no more allocations can take place in it. However, sometimes we
 124   // want to know where the end of the last "real" object we allocated
 125   // into the region was and this is what this keeps track.
 126   HeapWord* _pre_dummy_top;
 127 
 128  public:
 129   G1OffsetTableContigSpace(G1BlockOffsetSharedArray* sharedOffsetArray,
 130                            MemRegion mr);
 131 
 132   void set_top(HeapWord* value) { _top = value; }
 133   HeapWord* top() const { return _top; }
 134 
 135  protected:
 136   // Reset the G1OffsetTableContigSpace.
 137   virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);
 138 
 139   HeapWord** top_addr() { return &_top; }
 140   // Allocation helpers (return NULL if full).
 141   inline HeapWord* allocate_impl(size_t word_size, HeapWord* end_value);
 142   inline HeapWord* par_allocate_impl(size_t word_size, HeapWord* end_value);
 143 
 144  public:
 145   void reset_after_compaction() { set_top(compaction_top()); }
 146 
 147   size_t used() const { return byte_size(bottom(), top()); }
 148   size_t free() const { return byte_size(top(), end()); }
 149   bool is_free_block(const HeapWord* p) const { return p >= top(); }
 150 
 151   MemRegion used_region() const { return MemRegion(bottom(), top()); }
 152 
 153   void object_iterate(ObjectClosure* blk);
 154   void safe_object_iterate(ObjectClosure* blk);
 155 
 156   void set_bottom(HeapWord* value);
 157   void set_end(HeapWord* value);
 158 
 159   HeapWord* scan_top() const;
 160   void record_timestamp();
 161   void reset_gc_time_stamp() { _gc_time_stamp = 0; }
 162   unsigned get_gc_time_stamp() { return _gc_time_stamp; }
 163   void record_retained_region();
 164 
 165   // See the comment above in the declaration of _pre_dummy_top for an
 166   // explanation of what it is.
 167   void set_pre_dummy_top(HeapWord* pre_dummy_top) {
 168     assert(is_in(pre_dummy_top) && pre_dummy_top <= top(), "pre-condition");
 169     _pre_dummy_top = pre_dummy_top;
 170   }
 171   HeapWord* pre_dummy_top() {
 172     return (_pre_dummy_top == NULL) ? top() : _pre_dummy_top;
 173   }
 174   void reset_pre_dummy_top() { _pre_dummy_top = NULL; }
 175 
 176   virtual void clear(bool mangle_space);
 177 
 178   HeapWord* block_start(const void* p);
 179   HeapWord* block_start_const(const void* p) const;
 180 
 181   void prepare_for_compaction(CompactPoint* cp);
 182 
 183   // Add offset table update.
 184   virtual HeapWord* allocate(size_t word_size);
 185   HeapWord* par_allocate(size_t word_size);
 186 
 187   HeapWord* saved_mark_word() const { ShouldNotReachHere(); return NULL; }
 188 
 189   // MarkSweep support phase3
 190   virtual HeapWord* initialize_threshold();
 191   virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* end);
 192 
 193   virtual void print() const;
 194 
 195   void reset_bot() {
 196     _offsets.reset_bot();
 197   }
 198 
 199   void print_bot_on(outputStream* out) {
 200     _offsets.print_on(out);
 201   }
 202 };
 203 
 204 class HeapRegion: public G1OffsetTableContigSpace {
 205   friend class VMStructs;
 206  private:
 207 
 208   // The remembered set for this region.
 209   // (Might want to make this "inline" later, to avoid some alloc failure
 210   // issues.)
 211   HeapRegionRemSet* _rem_set;
 212 
 213   G1BlockOffsetArrayContigSpace* offsets() { return &_offsets; }
 214 
 215   void report_region_type_change(G1HeapRegionTraceType::Type to);
 216 
 217  protected:
 218   // The index of this region in the heap region sequence.
 219   uint  _hrm_index;
 220 
 221   AllocationContext_t _allocation_context;
 222 
 223   HeapRegionType _type;
 224 
 225   // For a humongous region, region in which it starts.
 226   HeapRegion* _humongous_start_region;
 227   // For the start region of a humongous sequence, it's original end().
 228   HeapWord* _orig_end;
 229 
 230   // True iff the region is in current collection_set.
 231   bool _in_collection_set;
 232 
 233   // True iff an attempt to evacuate an object in the region failed.
 234   bool _evacuation_failed;
 235 
 236   // A heap region may be a member one of a number of special subsets, each
 237   // represented as linked lists through the field below.  Currently, there
 238   // is only one set:
 239   //   The collection set.
 240   HeapRegion* _next_in_special_set;
 241 
 242   // next region in the young "generation" region set
 243   HeapRegion* _next_young_region;
 244 
 245   // Next region whose cards need cleaning
 246   HeapRegion* _next_dirty_cards_region;
 247 
 248   // Fields used by the HeapRegionSetBase class and subclasses.
 249   HeapRegion* _next;
 250   HeapRegion* _prev;
 251 #ifdef ASSERT
 252   HeapRegionSetBase* _containing_set;
 253 #endif // ASSERT
 254 
 255   // For parallel heapRegion traversal.
 256   jint _claimed;
 257 
 258   // We use concurrent marking to determine the amount of live data
 259   // in each heap region.
 260   size_t _prev_marked_bytes;    // Bytes known to be live via last completed marking.
 261   size_t _next_marked_bytes;    // Bytes known to be live via in-progress marking.
 262 
 263   // The calculated GC efficiency of the region.
 264   double _gc_efficiency;
 265 
 266   int  _young_index_in_cset;
 267   SurvRateGroup* _surv_rate_group;
 268   int  _age_index;
 269 
 270   // The start of the unmarked area. The unmarked area extends from this
 271   // word until the top and/or end of the region, and is the part
 272   // of the region for which no marking was done, i.e. objects may
 273   // have been allocated in this part since the last mark phase.
 274   // "prev" is the top at the start of the last completed marking.
 275   // "next" is the top at the start of the in-progress marking (if any.)
 276   HeapWord* _prev_top_at_mark_start;
 277   HeapWord* _next_top_at_mark_start;
 278   // If a collection pause is in progress, this is the top at the start
 279   // of that pause.
 280 
 281   void init_top_at_mark_start() {
 282     assert(_prev_marked_bytes == 0 &&
 283            _next_marked_bytes == 0,
 284            "Must be called after zero_marked_bytes.");
 285     HeapWord* bot = bottom();
 286     _prev_top_at_mark_start = bot;
 287     _next_top_at_mark_start = bot;
 288   }
 289 
 290   // Cached attributes used in the collection set policy information
 291 
 292   // The RSet length that was added to the total value
 293   // for the collection set.
 294   size_t _recorded_rs_length;
 295 
 296   // The predicted elapsed time that was added to total value
 297   // for the collection set.
 298   double _predicted_elapsed_time_ms;
 299 
 300   // The predicted number of bytes to copy that was added to
 301   // the total value for the collection set.
 302   size_t _predicted_bytes_to_copy;
 303 
 304  public:
 305   HeapRegion(uint hrm_index,
 306              G1BlockOffsetSharedArray* sharedOffsetArray,
 307              MemRegion mr);
 308 
 309   // Initializing the HeapRegion not only resets the data structure, but also
 310   // resets the BOT for that heap region.
 311   // The default values for clear_space means that we will do the clearing if
 312   // there's clearing to be done ourselves. We also always mangle the space.
 313   virtual void initialize(MemRegion mr, bool clear_space = false, bool mangle_space = SpaceDecorator::Mangle);
 314 
 315   static int    LogOfHRGrainBytes;
 316   static int    LogOfHRGrainWords;
 317 
 318   static size_t GrainBytes;
 319   static size_t GrainWords;
 320   static size_t CardsPerRegion;
 321 
 322   static size_t align_up_to_region_byte_size(size_t sz) {
 323     return (sz + (size_t) GrainBytes - 1) &
 324                                       ~((1 << (size_t) LogOfHRGrainBytes) - 1);
 325   }
 326 
 327   static size_t max_region_size();
 328 
 329   // It sets up the heap region size (GrainBytes / GrainWords), as
 330   // well as other related fields that are based on the heap region
 331   // size (LogOfHRGrainBytes / LogOfHRGrainWords /
 332   // CardsPerRegion). All those fields are considered constant
 333   // throughout the JVM's execution, therefore they should only be set
 334   // up once during initialization time.
 335   static void setup_heap_region_size(size_t initial_heap_size, size_t max_heap_size);
 336 
 337   enum ClaimValues {
 338     InitialClaimValue          = 0,
 339     FinalCountClaimValue       = 1,
 340     NoteEndClaimValue          = 2,
 341     ScrubRemSetClaimValue      = 3,
 342     ParVerifyClaimValue        = 4,
 343     RebuildRSClaimValue        = 5,
 344     ParEvacFailureClaimValue   = 6,
 345     AggregateCountClaimValue   = 7,
 346     VerifyCountClaimValue      = 8,
 347     ParMarkRootClaimValue      = 9
 348   };
 349 
 350   // All allocated blocks are occupied by objects in a HeapRegion
 351   bool block_is_obj(const HeapWord* p) const;
 352 
 353   // Returns the object size for all valid block starts
 354   // and the amount of unallocated words if called on top()
 355   size_t block_size(const HeapWord* p) const;
 356 
 357   inline HeapWord* par_allocate_no_bot_updates(size_t word_size);
 358   inline HeapWord* allocate_no_bot_updates(size_t word_size);
 359 
 360   // If this region is a member of a HeapRegionManager, the index in that
 361   // sequence, otherwise -1.
 362   uint hrm_index() const { return _hrm_index; }
 363 
 364   // The number of bytes marked live in the region in the last marking phase.
 365   size_t marked_bytes()    { return _prev_marked_bytes; }
 366   size_t live_bytes() {
 367     return (top() - prev_top_at_mark_start()) * HeapWordSize + marked_bytes();
 368   }
 369 
 370   // The number of bytes counted in the next marking.
 371   size_t next_marked_bytes() { return _next_marked_bytes; }
 372   // The number of bytes live wrt the next marking.
 373   size_t next_live_bytes() {
 374     return
 375       (top() - next_top_at_mark_start()) * HeapWordSize + 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   // Return the amount of bytes we'll reclaim if we collect this
 388   // region. This includes not only the known garbage bytes in the
 389   // region but also any unallocated space in it, i.e., [top, end),
 390   // since it will also be reclaimed if we collect the region.
 391   size_t reclaimable_bytes() {
 392     size_t known_live_bytes = live_bytes();
 393     assert(known_live_bytes <= capacity(), "sanity");
 394     return capacity() - known_live_bytes;
 395   }
 396 
 397   // An upper bound on the number of live bytes in the region.
 398   size_t max_live_bytes() { return used() - garbage_bytes(); }
 399 
 400   void add_to_marked_bytes(size_t incr_bytes) {
 401     _next_marked_bytes = _next_marked_bytes + incr_bytes;
 402     assert(_next_marked_bytes <= used(), "invariant" );
 403   }
 404 
 405   void zero_marked_bytes()      {
 406     _prev_marked_bytes = _next_marked_bytes = 0;
 407   }
 408 
 409   const char* get_type_str() const { return _type.get_str(); }
 410   const char* get_short_type_str() const { return _type.get_short_str(); }
 411   G1HeapRegionTraceType::Type get_trace_type() { return _type.get_trace_type(); }
 412 
 413   bool is_free() const { return _type.is_free(); }
 414 
 415   bool is_young()    const { return _type.is_young();    }
 416   bool is_eden()     const { return _type.is_eden();     }
 417   bool is_survivor() const { return _type.is_survivor(); }
 418 
 419   bool isHumongous() const { return _type.is_humongous(); }
 420   bool startsHumongous() const { return _type.is_starts_humongous(); }
 421   bool continuesHumongous() const { return _type.is_continues_humongous();   }
 422 
 423   bool is_old() const { return _type.is_old(); }
 424 
 425   // For a humongous region, region in which it starts.
 426   HeapRegion* humongous_start_region() const {
 427     return _humongous_start_region;
 428   }
 429 
 430   // Return the number of distinct regions that are covered by this region:
 431   // 1 if the region is not humongous, >= 1 if the region is humongous.
 432   uint region_num() const {
 433     if (!isHumongous()) {
 434       return 1U;
 435     } else {
 436       assert(startsHumongous(), "doesn't make sense on HC regions");
 437       assert(capacity() % HeapRegion::GrainBytes == 0, "sanity");
 438       return (uint) (capacity() >> HeapRegion::LogOfHRGrainBytes);
 439     }
 440   }
 441 
 442   // Return the index + 1 of the last HC regions that's associated
 443   // with this HS region.
 444   uint last_hc_index() const {
 445     assert(startsHumongous(), "don't call this otherwise");
 446     return hrm_index() + region_num();
 447   }
 448 
 449   // Same as Space::is_in_reserved, but will use the original size of the region.
 450   // The original size is different only for start humongous regions. They get
 451   // their _end set up to be the end of the last continues region of the
 452   // corresponding humongous object.
 453   bool is_in_reserved_raw(const void* p) const {
 454     return _bottom <= p && p < _orig_end;
 455   }
 456 
 457   // Makes the current region be a "starts humongous" region, i.e.,
 458   // the first region in a series of one or more contiguous regions
 459   // that will contain a single "humongous" object. The two parameters
 460   // are as follows:
 461   //
 462   // new_top : The new value of the top field of this region which
 463   // points to the end of the humongous object that's being
 464   // allocated. If there is more than one region in the series, top
 465   // will lie beyond this region's original end field and on the last
 466   // region in the series.
 467   //
 468   // new_end : The new value of the end field of this region which
 469   // points to the end of the last region in the series. If there is
 470   // one region in the series (namely: this one) end will be the same
 471   // as the original end of this region.
 472   //
 473   // Updating top and end as described above makes this region look as
 474   // if it spans the entire space taken up by all the regions in the
 475   // series and an single allocation moved its top to new_top. This
 476   // ensures that the space (capacity / allocated) taken up by all
 477   // humongous regions can be calculated by just looking at the
 478   // "starts humongous" regions and by ignoring the "continues
 479   // humongous" regions.
 480   void set_startsHumongous(HeapWord* new_top, HeapWord* new_end);
 481 
 482   // Makes the current region be a "continues humongous'
 483   // region. first_hr is the "start humongous" region of the series
 484   // which this region will be part of.
 485   void set_continuesHumongous(HeapRegion* first_hr);
 486 
 487   // Unsets the humongous-related fields on the region.
 488   void clear_humongous();
 489 
 490   // If the region has a remembered set, return a pointer to it.
 491   HeapRegionRemSet* rem_set() const {
 492     return _rem_set;
 493   }
 494 
 495   // True iff the region is in current collection_set.
 496   bool in_collection_set() const {
 497     return _in_collection_set;
 498   }
 499   void set_in_collection_set(bool b) {
 500     _in_collection_set = b;
 501   }
 502   HeapRegion* next_in_collection_set() {
 503     assert(in_collection_set(), "should only invoke on member of CS.");
 504     assert(_next_in_special_set == NULL ||
 505            _next_in_special_set->in_collection_set(),
 506            "Malformed CS.");
 507     return _next_in_special_set;
 508   }
 509   void set_next_in_collection_set(HeapRegion* r) {
 510     assert(in_collection_set(), "should only invoke on member of CS.");
 511     assert(r == NULL || r->in_collection_set(), "Malformed CS.");
 512     _next_in_special_set = r;
 513   }
 514 
 515   void set_allocation_context(AllocationContext_t context) {
 516     _allocation_context = context;
 517   }
 518 
 519   AllocationContext_t  allocation_context() const {
 520     return _allocation_context;
 521   }
 522 
 523   // Methods used by the HeapRegionSetBase class and subclasses.
 524 
 525   // Getter and setter for the next and prev fields used to link regions into
 526   // linked lists.
 527   HeapRegion* next()              { return _next; }
 528   HeapRegion* prev()              { return _prev; }
 529 
 530   void set_next(HeapRegion* next) { _next = next; }
 531   void set_prev(HeapRegion* prev) { _prev = prev; }
 532 
 533   // Every region added to a set is tagged with a reference to that
 534   // set. This is used for doing consistency checking to make sure that
 535   // the contents of a set are as they should be and it's only
 536   // available in non-product builds.
 537 #ifdef ASSERT
 538   void set_containing_set(HeapRegionSetBase* containing_set) {
 539     assert((containing_set == NULL && _containing_set != NULL) ||
 540            (containing_set != NULL && _containing_set == NULL),
 541            err_msg("containing_set: " PTR_FORMAT " "
 542                    "_containing_set: " PTR_FORMAT,
 543                    p2i(containing_set), p2i(_containing_set)));
 544 
 545     _containing_set = containing_set;
 546   }
 547 
 548   HeapRegionSetBase* containing_set() { return _containing_set; }
 549 #else // ASSERT
 550   void set_containing_set(HeapRegionSetBase* containing_set) { }
 551 
 552   // containing_set() is only used in asserts so there's no reason
 553   // to provide a dummy version of it.
 554 #endif // ASSERT
 555 
 556   HeapRegion* get_next_young_region() { return _next_young_region; }
 557   void set_next_young_region(HeapRegion* hr) {
 558     _next_young_region = hr;
 559   }
 560 
 561   HeapRegion* get_next_dirty_cards_region() const { return _next_dirty_cards_region; }
 562   HeapRegion** next_dirty_cards_region_addr() { return &_next_dirty_cards_region; }
 563   void set_next_dirty_cards_region(HeapRegion* hr) { _next_dirty_cards_region = hr; }
 564   bool is_on_dirty_cards_region_list() const { return get_next_dirty_cards_region() != NULL; }
 565 
 566   HeapWord* orig_end() const { return _orig_end; }
 567 
 568   // Reset HR stuff to default values.
 569   void hr_clear(bool par, bool clear_space, bool locked = false);
 570   void par_clear();
 571 
 572   // Get the start of the unmarked area in this region.
 573   HeapWord* prev_top_at_mark_start() const { return _prev_top_at_mark_start; }
 574   HeapWord* next_top_at_mark_start() const { return _next_top_at_mark_start; }
 575 
 576   // Note the start or end of marking. This tells the heap region
 577   // that the collector is about to start or has finished (concurrently)
 578   // marking the heap.
 579 
 580   // Notify the region that concurrent marking is starting. Initialize
 581   // all fields related to the next marking info.
 582   inline void note_start_of_marking();
 583 
 584   // Notify the region that concurrent marking has finished. Copy the
 585   // (now finalized) next marking info fields into the prev marking
 586   // info fields.
 587   inline void note_end_of_marking();
 588 
 589   // Notify the region that it will be used as to-space during a GC
 590   // and we are about to start copying objects into it.
 591   inline void note_start_of_copying(bool during_initial_mark);
 592 
 593   // Notify the region that it ceases being to-space during a GC and
 594   // we will not copy objects into it any more.
 595   inline void note_end_of_copying(bool during_initial_mark);
 596 
 597   // Notify the region that we are about to start processing
 598   // self-forwarded objects during evac failure handling.
 599   void note_self_forwarding_removal_start(bool during_initial_mark,
 600                                           bool during_conc_mark);
 601 
 602   // Notify the region that we have finished processing self-forwarded
 603   // objects during evac failure handling.
 604   void note_self_forwarding_removal_end(bool during_initial_mark,
 605                                         bool during_conc_mark,
 606                                         size_t marked_bytes);
 607 
 608   // Returns "false" iff no object in the region was allocated when the
 609   // last mark phase ended.
 610   bool is_marked() { return _prev_top_at_mark_start != bottom(); }
 611 
 612   void reset_during_compaction() {
 613     assert(isHumongous() && startsHumongous(),
 614            "should only be called for starts humongous regions");
 615 
 616     zero_marked_bytes();
 617     init_top_at_mark_start();
 618   }
 619 
 620   void calc_gc_efficiency(void);
 621   double gc_efficiency() { return _gc_efficiency;}
 622 
 623   int  young_index_in_cset() const { return _young_index_in_cset; }
 624   void set_young_index_in_cset(int index) {
 625     assert( (index == -1) || is_young(), "pre-condition" );
 626     _young_index_in_cset = index;
 627   }
 628 
 629   int age_in_surv_rate_group() {
 630     assert( _surv_rate_group != NULL, "pre-condition" );
 631     assert( _age_index > -1, "pre-condition" );
 632     return _surv_rate_group->age_in_group(_age_index);
 633   }
 634 
 635   void record_surv_words_in_group(size_t words_survived) {
 636     assert( _surv_rate_group != NULL, "pre-condition" );
 637     assert( _age_index > -1, "pre-condition" );
 638     int age_in_group = age_in_surv_rate_group();
 639     _surv_rate_group->record_surviving_words(age_in_group, words_survived);
 640   }
 641 
 642   int age_in_surv_rate_group_cond() {
 643     if (_surv_rate_group != NULL)
 644       return age_in_surv_rate_group();
 645     else
 646       return -1;
 647   }
 648 
 649   SurvRateGroup* surv_rate_group() {
 650     return _surv_rate_group;
 651   }
 652 
 653   void install_surv_rate_group(SurvRateGroup* surv_rate_group) {
 654     assert( surv_rate_group != NULL, "pre-condition" );
 655     assert( _surv_rate_group == NULL, "pre-condition" );
 656     assert( is_young(), "pre-condition" );
 657 
 658     _surv_rate_group = surv_rate_group;
 659     _age_index = surv_rate_group->next_age_index();
 660   }
 661 
 662   void uninstall_surv_rate_group() {
 663     if (_surv_rate_group != NULL) {
 664       assert( _age_index > -1, "pre-condition" );
 665       assert( is_young(), "pre-condition" );
 666 
 667       _surv_rate_group = NULL;
 668       _age_index = -1;
 669     } else {
 670       assert( _age_index == -1, "pre-condition" );
 671     }
 672   }
 673 
 674   void set_free() {
 675     if (EnableJFR) {
 676       report_region_type_change(G1HeapRegionTraceType::Free);
 677     }
 678     _type.set_free();
 679   }
 680 
 681   void set_eden() {
 682     if (EnableJFR) {
 683       report_region_type_change(G1HeapRegionTraceType::Eden);
 684     }
 685     _type.set_eden();
 686   }
 687 
 688   void set_old() {
 689     if (EnableJFR) {
 690       report_region_type_change(G1HeapRegionTraceType::Old);
 691     }
 692     _type.set_old();
 693   }
 694 
 695   void set_eden_pre_gc() {
 696     if (EnableJFR) {
 697       report_region_type_change(G1HeapRegionTraceType::Eden);
 698     }
 699     _type.set_eden_pre_gc();
 700   }
 701 
 702   void set_survivor() {
 703     if (EnableJFR) {
 704       report_region_type_change(G1HeapRegionTraceType::Survivor);
 705     }
 706     _type.set_survivor();
 707   }
 708 
 709   // Determine if an object has been allocated since the last
 710   // mark performed by the collector. This returns true iff the object
 711   // is within the unmarked area of the region.
 712   bool obj_allocated_since_prev_marking(oop obj) const {
 713     return (HeapWord *) obj >= prev_top_at_mark_start();
 714   }
 715   bool obj_allocated_since_next_marking(oop obj) const {
 716     return (HeapWord *) obj >= next_top_at_mark_start();
 717   }
 718 
 719   // For parallel heapRegion traversal.
 720   bool claimHeapRegion(int claimValue);
 721   jint claim_value() { return _claimed; }
 722   // Use this carefully: only when you're sure no one is claiming...
 723   void set_claim_value(int claimValue) { _claimed = claimValue; }
 724 
 725   // Returns the "evacuation_failed" property of the region.
 726   bool evacuation_failed() { return _evacuation_failed; }
 727 
 728   // Sets the "evacuation_failed" property of the region.
 729   void set_evacuation_failed(bool b) {
 730     _evacuation_failed = b;
 731 
 732     if (b) {
 733       _next_marked_bytes = 0;
 734     }
 735   }
 736 
 737   // Requires that "mr" be entirely within the region.
 738   // Apply "cl->do_object" to all objects that intersect with "mr".
 739   // If the iteration encounters an unparseable portion of the region,
 740   // or if "cl->abort()" is true after a closure application,
 741   // terminate the iteration and return the address of the start of the
 742   // subregion that isn't done.  (The two can be distinguished by querying
 743   // "cl->abort()".)  Return of "NULL" indicates that the iteration
 744   // completed.
 745   HeapWord*
 746   object_iterate_mem_careful(MemRegion mr, ObjectClosure* cl);
 747 
 748   // filter_young: if true and the region is a young region then we
 749   // skip the iteration.
 750   // card_ptr: if not NULL, and we decide that the card is not young
 751   // and we iterate over it, we'll clean the card before we start the
 752   // iteration.
 753   HeapWord*
 754   oops_on_card_seq_iterate_careful(MemRegion mr,
 755                                    FilterOutOfRegionClosure* cl,
 756                                    bool filter_young,
 757                                    jbyte* card_ptr);
 758 
 759   size_t recorded_rs_length() const        { return _recorded_rs_length; }
 760   double predicted_elapsed_time_ms() const { return _predicted_elapsed_time_ms; }
 761   size_t predicted_bytes_to_copy() const   { return _predicted_bytes_to_copy; }
 762 
 763   void set_recorded_rs_length(size_t rs_length) {
 764     _recorded_rs_length = rs_length;
 765   }
 766 
 767   void set_predicted_elapsed_time_ms(double ms) {
 768     _predicted_elapsed_time_ms = ms;
 769   }
 770 
 771   void set_predicted_bytes_to_copy(size_t bytes) {
 772     _predicted_bytes_to_copy = bytes;
 773   }
 774 
 775   virtual CompactibleSpace* next_compaction_space() const;
 776 
 777   virtual void reset_after_compaction();
 778 
 779   // Routines for managing a list of code roots (attached to the
 780   // this region's RSet) that point into this heap region.
 781   void add_strong_code_root(nmethod* nm);
 782   void add_strong_code_root_locked(nmethod* nm);
 783   void remove_strong_code_root(nmethod* nm);
 784 
 785   // Applies blk->do_code_blob() to each of the entries in
 786   // the strong code roots list for this region
 787   void strong_code_roots_do(CodeBlobClosure* blk) const;
 788 
 789   // Verify that the entries on the strong code root list for this
 790   // region are live and include at least one pointer into this region.
 791   void verify_strong_code_roots(VerifyOption vo, bool* failures) const;
 792 
 793   void print() const;
 794   void print_on(outputStream* st) const;
 795 
 796   // vo == UsePrevMarking  -> use "prev" marking information,
 797   // vo == UseNextMarking -> use "next" marking information
 798   // vo == UseMarkWord    -> use the mark word in the object header
 799   //
 800   // NOTE: Only the "prev" marking information is guaranteed to be
 801   // consistent most of the time, so most calls to this should use
 802   // vo == UsePrevMarking.
 803   // Currently, there is only one case where this is called with
 804   // vo == UseNextMarking, which is to verify the "next" marking
 805   // information at the end of remark.
 806   // Currently there is only one place where this is called with
 807   // vo == UseMarkWord, which is to verify the marking during a
 808   // full GC.
 809   void verify(VerifyOption vo, bool *failures) const;
 810 
 811   // Override; it uses the "prev" marking information
 812   virtual void verify() const;
 813 
 814   void verify_rem_set(VerifyOption vo, bool *failures) const;
 815   void verify_rem_set() const;
 816 };
 817 
 818 // HeapRegionClosure is used for iterating over regions.
 819 // Terminates the iteration when the "doHeapRegion" method returns "true".
 820 class HeapRegionClosure : public StackObj {
 821   friend class HeapRegionManager;
 822   friend class G1CollectedHeap;
 823 
 824   bool _complete;
 825   void incomplete() { _complete = false; }
 826 
 827  public:
 828   HeapRegionClosure(): _complete(true) {}
 829 
 830   // Typically called on each region until it returns true.
 831   virtual bool doHeapRegion(HeapRegion* r) = 0;
 832 
 833   // True after iteration if the closure was applied to all heap regions
 834   // and returned "false" in all cases.
 835   bool complete() { return _complete; }
 836 };
 837 
 838 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_HPP