heapRegion.hpp 31.0 KB
Newer Older
1
/*
2
 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24
 *
 */

25 26 27
#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_HPP
#define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_HPP

28
#include "gc_implementation/g1/g1AllocationContext.hpp"
29
#include "gc_implementation/g1/g1BlockOffsetTable.hpp"
30 31 32 33 34 35
#include "gc_implementation/g1/g1_specialized_oop_closures.hpp"
#include "gc_implementation/g1/survRateGroup.hpp"
#include "gc_implementation/shared/ageTable.hpp"
#include "gc_implementation/shared/spaceDecorator.hpp"
#include "memory/space.inline.hpp"
#include "memory/watermark.hpp"
36
#include "utilities/macros.hpp"
37

38
#if INCLUDE_ALL_GCS
39 40 41 42 43 44 45 46 47 48 49 50 51 52

// A HeapRegion is the smallest piece of a G1CollectedHeap that
// can be collected independently.

// NOTE: Although a HeapRegion is a Space, its
// Space::initDirtyCardClosure method must not be called.
// The problem is that the existence of this method breaks
// the independence of barrier sets from remembered sets.
// The solution is to remove this method from the definition
// of a Space.

class HeapRegionRemSet;
class HeapRegionRemSetIterator;
class HeapRegion;
53
class HeapRegionSetBase;
J
johnc 已提交
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 60 61 62
                (_hr_)->is_survivor() ? "S" : (_hr_)->is_young() ? "E" : \
                (_hr_)->startsHumongous() ? "HS" : \
                (_hr_)->continuesHumongous() ? "HC" : \
                !(_hr_)->is_empty() ? "O" : "F", \
63
                p2i((_hr_)->bottom()), p2i((_hr_)->top()), p2i((_hr_)->end())
64

65 66
// sentinel value for hrm_index
#define G1_NO_HRM_INDEX ((uint) -1)
67

68 69 70 71 72
// A dirty card to oop closure for heap regions. It
// knows how to get the G1 heap and how to use the bitmap
// in the concurrent marker used by G1 to filter remembered
// sets.

73
class HeapRegionDCTOC : public DirtyCardToOopClosure {
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
public:
  // Specification of possible DirtyCardToOopClosure filtering.
  enum FilterKind {
    NoFilterKind,
    IntoCSFilterKind,
    OutOfRegionFilterKind
  };

protected:
  HeapRegion* _hr;
  FilterKind _fk;
  G1CollectedHeap* _g1;

  // Walk the given memory region from bottom to (actual) top
  // looking for objects and applying the oop closure (_cl) to
  // them. The base implementation of this treats the area as
  // blocks, where a block may or may not be an object. Sub-
  // classes should override this to provide more accurate
  // or possibly more efficient walking.
93
  void walk_mem_region(MemRegion mr, HeapWord* bottom, HeapWord* top);
94 95 96

public:
  HeapRegionDCTOC(G1CollectedHeap* g1,
97
                  HeapRegion* hr, ExtendedOopClosure* cl,
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
                  CardTableModRefBS::PrecisionStyle precision,
                  FilterKind fk);
};

// The complicating factor is that BlockOffsetTable diverged
// significantly, and we need functionality that is only in the G1 version.
// So I copied that code, which led to an alternate G1 version of
// OffsetTableContigSpace.  If the two versions of BlockOffsetTable could
// be reconciled, then G1OffsetTableContigSpace could go away.

// The idea behind time stamps is the following. Doing a save_marks on
// all regions at every GC pause is time consuming (if I remember
// well, 10ms or so). So, we would like to do that only for regions
// that are GC alloc regions. To achieve this, we use time
// stamps. For every evacuation pause, G1CollectedHeap generates a
// unique time stamp (essentially a counter that gets
// incremented). Every time we want to call save_marks on a region,
// we set the saved_mark_word to top and also copy the current GC
// time stamp to the time stamp field of the space. Reading the
// saved_mark_word involves checking the time stamp of the
// region. If it is the same as the current GC time stamp, then we
// can safely read the saved_mark_word field, as it is valid. If the
// time stamp of the region is not the same as the current GC time
// stamp, then we instead read top, as the saved_mark_word field is
// invalid. Time stamps (on the regions and also on the
// G1CollectedHeap) are reset at every cleanup (we iterate over
// the regions anyway) and at the end of a Full GC. The current scheme
// that uses sequential unsigned ints will fail only if we have 4b
// evacuation pauses between two cleanups, which is _highly_ unlikely.
127
class G1OffsetTableContigSpace: public CompactibleSpace {
128
  friend class VMStructs;
129
  HeapWord* _top;
130 131 132 133
 protected:
  G1BlockOffsetArrayContigSpace _offsets;
  Mutex _par_alloc_lock;
  volatile unsigned _gc_time_stamp;
134 135 136 137 138 139 140
  // When we need to retire an allocation region, while other threads
  // are also concurrently trying to allocate into it, we typically
  // allocate a dummy object at the end of the region to ensure that
  // no more allocations can take place in it. However, sometimes we
  // want to know where the end of the last "real" object we allocated
  // into the region was and this is what this keeps track.
  HeapWord* _pre_dummy_top;
141 142 143

 public:
  G1OffsetTableContigSpace(G1BlockOffsetSharedArray* sharedOffsetArray,
144
                           MemRegion mr);
145

146 147 148 149
  void set_top(HeapWord* value) { _top = value; }
  HeapWord* top() const { return _top; }

 protected:
150 151 152
  // Reset the G1OffsetTableContigSpace.
  virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  HeapWord** top_addr() { return &_top; }
  // Allocation helpers (return NULL if full).
  inline HeapWord* allocate_impl(size_t word_size, HeapWord* end_value);
  inline HeapWord* par_allocate_impl(size_t word_size, HeapWord* end_value);

 public:
  void reset_after_compaction() { set_top(compaction_top()); }

  size_t used() const { return byte_size(bottom(), top()); }
  size_t free() const { return byte_size(top(), end()); }
  bool is_free_block(const HeapWord* p) const { return p >= top(); }

  MemRegion used_region() const { return MemRegion(bottom(), top()); }

  void object_iterate(ObjectClosure* blk);
  void safe_object_iterate(ObjectClosure* blk);

170 171 172 173
  void set_bottom(HeapWord* value);
  void set_end(HeapWord* value);

  virtual HeapWord* saved_mark_word() const;
174
  void record_top_and_timestamp();
175
  void reset_gc_time_stamp() { _gc_time_stamp = 0; }
176
  unsigned get_gc_time_stamp() { return _gc_time_stamp; }
177

178 179 180 181 182 183 184 185 186 187 188
  // See the comment above in the declaration of _pre_dummy_top for an
  // explanation of what it is.
  void set_pre_dummy_top(HeapWord* pre_dummy_top) {
    assert(is_in(pre_dummy_top) && pre_dummy_top <= top(), "pre-condition");
    _pre_dummy_top = pre_dummy_top;
  }
  HeapWord* pre_dummy_top() {
    return (_pre_dummy_top == NULL) ? top() : _pre_dummy_top;
  }
  void reset_pre_dummy_top() { _pre_dummy_top = NULL; }

T
Merge  
tonyp 已提交
189
  virtual void clear(bool mangle_space);
190 191 192 193

  HeapWord* block_start(const void* p);
  HeapWord* block_start_const(const void* p) const;

194 195
  void prepare_for_compaction(CompactPoint* cp);

196 197 198 199 200 201 202 203 204
  // Add offset table update.
  virtual HeapWord* allocate(size_t word_size);
  HeapWord* par_allocate(size_t word_size);

  // MarkSweep support phase3
  virtual HeapWord* initialize_threshold();
  virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* end);

  virtual void print() const;
205 206

  void reset_bot() {
207
    _offsets.reset_bot();
208 209 210 211 212 213 214 215 216
  }

  void update_bot_for_object(HeapWord* start, size_t word_size) {
    _offsets.alloc_block(start, word_size);
  }

  void print_bot_on(outputStream* out) {
    _offsets.print_on(out);
  }
217 218 219 220 221 222
};

class HeapRegion: public G1OffsetTableContigSpace {
  friend class VMStructs;
 private:

223 224 225 226 227 228
  enum HumongousType {
    NotHumongous = 0,
    StartsHumongous,
    ContinuesHumongous
  };

229 230 231 232 233 234 235 236
  // The remembered set for this region.
  // (Might want to make this "inline" later, to avoid some alloc failure
  // issues.)
  HeapRegionRemSet* _rem_set;

  G1BlockOffsetArrayContigSpace* offsets() { return &_offsets; }

 protected:
237
  // The index of this region in the heap region sequence.
238
  uint  _hrm_index;
239

240 241
  AllocationContext_t _allocation_context;

242
  HumongousType _humongous_type;
243 244 245 246 247 248 249 250 251 252 253 254
  // For a humongous region, region in which it starts.
  HeapRegion* _humongous_start_region;
  // For the start region of a humongous sequence, it's original end().
  HeapWord* _orig_end;

  // True iff the region is in current collection_set.
  bool _in_collection_set;

  // True iff an attempt to evacuate an object in the region failed.
  bool _evacuation_failed;

  // A heap region may be a member one of a number of special subsets, each
255 256
  // represented as linked lists through the field below.  Currently, there
  // is only one set:
257 258 259 260 261 262
  //   The collection set.
  HeapRegion* _next_in_special_set;

  // next region in the young "generation" region set
  HeapRegion* _next_young_region;

263 264 265
  // Next region whose cards need cleaning
  HeapRegion* _next_dirty_cards_region;

266 267
  // Fields used by the HeapRegionSetBase class and subclasses.
  HeapRegion* _next;
268
  HeapRegion* _prev;
269 270 271 272
#ifdef ASSERT
  HeapRegionSetBase* _containing_set;
#endif // ASSERT

273 274 275 276 277 278 279 280
  // For parallel heapRegion traversal.
  jint _claimed;

  // We use concurrent marking to determine the amount of live data
  // in each heap region.
  size_t _prev_marked_bytes;    // Bytes known to be live via last completed marking.
  size_t _next_marked_bytes;    // Bytes known to be live via in-progress marking.

281
  // The calculated GC efficiency of the region.
282 283 284 285 286
  double _gc_efficiency;

  enum YoungType {
    NotYoung,                   // a region is not young
    Young,                      // a region is young
287
    Survivor                    // a region is young and it contains survivors
288 289
  };

290
  volatile YoungType _young_type;
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  int  _young_index_in_cset;
  SurvRateGroup* _surv_rate_group;
  int  _age_index;

  // The start of the unmarked area. The unmarked area extends from this
  // word until the top and/or end of the region, and is the part
  // of the region for which no marking was done, i.e. objects may
  // have been allocated in this part since the last mark phase.
  // "prev" is the top at the start of the last completed marking.
  // "next" is the top at the start of the in-progress marking (if any.)
  HeapWord* _prev_top_at_mark_start;
  HeapWord* _next_top_at_mark_start;
  // If a collection pause is in progress, this is the top at the start
  // of that pause.

  void init_top_at_mark_start() {
    assert(_prev_marked_bytes == 0 &&
           _next_marked_bytes == 0,
           "Must be called after zero_marked_bytes.");
    HeapWord* bot = bottom();
    _prev_top_at_mark_start = bot;
    _next_top_at_mark_start = bot;
  }

  void set_young_type(YoungType new_type) {
    //assert(_young_type != new_type, "setting the same type" );
    // TODO: add more assertions here
    _young_type = new_type;
  }

321 322 323 324 325 326 327 328 329 330 331 332 333 334
  // Cached attributes used in the collection set policy information

  // The RSet length that was added to the total value
  // for the collection set.
  size_t _recorded_rs_length;

  // The predicted elapsed time that was added to total value
  // for the collection set.
  double _predicted_elapsed_time_ms;

  // The predicted number of bytes to copy that was added to
  // the total value for the collection set.
  size_t _predicted_bytes_to_copy;

335
 public:
336
  HeapRegion(uint hrm_index,
337
             G1BlockOffsetSharedArray* sharedOffsetArray,
S
sjohanss 已提交
338
             MemRegion mr);
339

340 341 342 343 344 345
  // Initializing the HeapRegion not only resets the data structure, but also
  // resets the BOT for that heap region.
  // The default values for clear_space means that we will do the clearing if
  // there's clearing to be done ourselves. We also always mangle the space.
  virtual void initialize(MemRegion mr, bool clear_space = false, bool mangle_space = SpaceDecorator::Mangle);

346 347 348 349 350 351
  static int    LogOfHRGrainBytes;
  static int    LogOfHRGrainWords;

  static size_t GrainBytes;
  static size_t GrainWords;
  static size_t CardsPerRegion;
352

353 354 355 356 357
  static size_t align_up_to_region_byte_size(size_t sz) {
    return (sz + (size_t) GrainBytes - 1) &
                                      ~((1 << (size_t) LogOfHRGrainBytes) - 1);
  }

358 359
  static size_t max_region_size();

360 361 362 363 364 365
  // It sets up the heap region size (GrainBytes / GrainWords), as
  // well as other related fields that are based on the heap region
  // size (LogOfHRGrainBytes / LogOfHRGrainWords /
  // CardsPerRegion). All those fields are considered constant
  // throughout the JVM's execution, therefore they should only be set
  // up once during initialization time.
366
  static void setup_heap_region_size(size_t initial_heap_size, size_t max_heap_size);
367

368
  enum ClaimValues {
369 370 371 372 373 374
    InitialClaimValue          = 0,
    FinalCountClaimValue       = 1,
    NoteEndClaimValue          = 2,
    ScrubRemSetClaimValue      = 3,
    ParVerifyClaimValue        = 4,
    RebuildRSClaimValue        = 5,
375 376
    ParEvacFailureClaimValue   = 6,
    AggregateCountClaimValue   = 7,
J
johnc 已提交
377 378
    VerifyCountClaimValue      = 8,
    ParMarkRootClaimValue      = 9
379 380
  };

381 382 383 384 385 386 387 388 389
  // All allocated blocks are occupied by objects in a HeapRegion
  bool block_is_obj(const HeapWord* p) const;

  // Returns the object size for all valid block starts
  // and the amount of unallocated words if called on top()
  size_t block_size(const HeapWord* p) const;

  inline HeapWord* par_allocate_no_bot_updates(size_t word_size);
  inline HeapWord* allocate_no_bot_updates(size_t word_size);
390

391
  // If this region is a member of a HeapRegionManager, the index in that
392
  // sequence, otherwise -1.
393
  uint hrm_index() const { return _hrm_index; }
394 395 396

  // The number of bytes marked live in the region in the last marking phase.
  size_t marked_bytes()    { return _prev_marked_bytes; }
397 398 399 400
  size_t live_bytes() {
    return (top() - prev_top_at_mark_start()) * HeapWordSize + marked_bytes();
  }

401 402 403 404
  // The number of bytes counted in the next marking.
  size_t next_marked_bytes() { return _next_marked_bytes; }
  // The number of bytes live wrt the next marking.
  size_t next_live_bytes() {
405 406
    return
      (top() - next_top_at_mark_start()) * HeapWordSize + next_marked_bytes();
407 408 409 410 411 412 413 414 415 416 417
  }

  // A lower bound on the amount of garbage bytes in the region.
  size_t garbage_bytes() {
    size_t used_at_mark_start_bytes =
      (prev_top_at_mark_start() - bottom()) * HeapWordSize;
    assert(used_at_mark_start_bytes >= marked_bytes(),
           "Can't mark more than we have.");
    return used_at_mark_start_bytes - marked_bytes();
  }

418 419 420 421 422 423 424 425 426 427
  // Return the amount of bytes we'll reclaim if we collect this
  // region. This includes not only the known garbage bytes in the
  // region but also any unallocated space in it, i.e., [top, end),
  // since it will also be reclaimed if we collect the region.
  size_t reclaimable_bytes() {
    size_t known_live_bytes = live_bytes();
    assert(known_live_bytes <= capacity(), "sanity");
    return capacity() - known_live_bytes;
  }

428 429 430 431 432
  // An upper bound on the number of live bytes in the region.
  size_t max_live_bytes() { return used() - garbage_bytes(); }

  void add_to_marked_bytes(size_t incr_bytes) {
    _next_marked_bytes = _next_marked_bytes + incr_bytes;
433
    assert(_next_marked_bytes <= used(), "invariant" );
434 435 436 437 438 439
  }

  void zero_marked_bytes()      {
    _prev_marked_bytes = _next_marked_bytes = 0;
  }

440 441 442
  bool isHumongous() const { return _humongous_type != NotHumongous; }
  bool startsHumongous() const { return _humongous_type == StartsHumongous; }
  bool continuesHumongous() const { return _humongous_type == ContinuesHumongous; }
443 444 445 446 447
  // For a humongous region, region in which it starts.
  HeapRegion* humongous_start_region() const {
    return _humongous_start_region;
  }

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
  // Return the number of distinct regions that are covered by this region:
  // 1 if the region is not humongous, >= 1 if the region is humongous.
  uint region_num() const {
    if (!isHumongous()) {
      return 1U;
    } else {
      assert(startsHumongous(), "doesn't make sense on HC regions");
      assert(capacity() % HeapRegion::GrainBytes == 0, "sanity");
      return (uint) (capacity() >> HeapRegion::LogOfHRGrainBytes);
    }
  }

  // Return the index + 1 of the last HC regions that's associated
  // with this HS region.
  uint last_hc_index() const {
    assert(startsHumongous(), "don't call this otherwise");
464
    return hrm_index() + region_num();
465 466
  }

467 468 469 470 471 472 473 474
  // Same as Space::is_in_reserved, but will use the original size of the region.
  // The original size is different only for start humongous regions. They get
  // their _end set up to be the end of the last continues region of the
  // corresponding humongous object.
  bool is_in_reserved_raw(const void* p) const {
    return _bottom <= p && p < _orig_end;
  }

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  // Makes the current region be a "starts humongous" region, i.e.,
  // the first region in a series of one or more contiguous regions
  // that will contain a single "humongous" object. The two parameters
  // are as follows:
  //
  // new_top : The new value of the top field of this region which
  // points to the end of the humongous object that's being
  // allocated. If there is more than one region in the series, top
  // will lie beyond this region's original end field and on the last
  // region in the series.
  //
  // new_end : The new value of the end field of this region which
  // points to the end of the last region in the series. If there is
  // one region in the series (namely: this one) end will be the same
  // as the original end of this region.
  //
  // Updating top and end as described above makes this region look as
  // if it spans the entire space taken up by all the regions in the
  // series and an single allocation moved its top to new_top. This
  // ensures that the space (capacity / allocated) taken up by all
  // humongous regions can be calculated by just looking at the
  // "starts humongous" regions and by ignoring the "continues
  // humongous" regions.
  void set_startsHumongous(HeapWord* new_top, HeapWord* new_end);

  // Makes the current region be a "continues humongous'
  // region. first_hr is the "start humongous" region of the series
  // which this region will be part of.
  void set_continuesHumongous(HeapRegion* first_hr);
504

505 506 507
  // Unsets the humongous-related fields on the region.
  void set_notHumongous();

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
  // If the region has a remembered set, return a pointer to it.
  HeapRegionRemSet* rem_set() const {
    return _rem_set;
  }

  // True iff the region is in current collection_set.
  bool in_collection_set() const {
    return _in_collection_set;
  }
  void set_in_collection_set(bool b) {
    _in_collection_set = b;
  }
  HeapRegion* next_in_collection_set() {
    assert(in_collection_set(), "should only invoke on member of CS.");
    assert(_next_in_special_set == NULL ||
           _next_in_special_set->in_collection_set(),
           "Malformed CS.");
    return _next_in_special_set;
  }
  void set_next_in_collection_set(HeapRegion* r) {
    assert(in_collection_set(), "should only invoke on member of CS.");
    assert(r == NULL || r->in_collection_set(), "Malformed CS.");
    _next_in_special_set = r;
  }

533 534 535 536 537 538 539 540
  void set_allocation_context(AllocationContext_t context) {
    _allocation_context = context;
  }

  AllocationContext_t  allocation_context() const {
    return _allocation_context;
  }

541
  // Methods used by the HeapRegionSetBase class and subclasses.
542

543
  // Getter and setter for the next and prev fields used to link regions into
544 545
  // linked lists.
  HeapRegion* next()              { return _next; }
546
  HeapRegion* prev()              { return _prev; }
547

548
  void set_next(HeapRegion* next) { _next = next; }
549
  void set_prev(HeapRegion* prev) { _prev = prev; }
550

551 552 553 554 555 556 557 558 559 560
  // Every region added to a set is tagged with a reference to that
  // set. This is used for doing consistency checking to make sure that
  // the contents of a set are as they should be and it's only
  // available in non-product builds.
#ifdef ASSERT
  void set_containing_set(HeapRegionSetBase* containing_set) {
    assert((containing_set == NULL && _containing_set != NULL) ||
           (containing_set != NULL && _containing_set == NULL),
           err_msg("containing_set: "PTR_FORMAT" "
                   "_containing_set: "PTR_FORMAT,
561
                   p2i(containing_set), p2i(_containing_set)));
562

563
    _containing_set = containing_set;
T
tonyp 已提交
564
  }
565

566 567 568
  HeapRegionSetBase* containing_set() { return _containing_set; }
#else // ASSERT
  void set_containing_set(HeapRegionSetBase* containing_set) { }
569

T
tonyp 已提交
570
  // containing_set() is only used in asserts so there's no reason
571 572 573
  // to provide a dummy version of it.
#endif // ASSERT

574 575 576 577 578
  HeapRegion* get_next_young_region() { return _next_young_region; }
  void set_next_young_region(HeapRegion* hr) {
    _next_young_region = hr;
  }

579 580 581 582 583
  HeapRegion* get_next_dirty_cards_region() const { return _next_dirty_cards_region; }
  HeapRegion** next_dirty_cards_region_addr() { return &_next_dirty_cards_region; }
  void set_next_dirty_cards_region(HeapRegion* hr) { _next_dirty_cards_region = hr; }
  bool is_on_dirty_cards_region_list() const { return get_next_dirty_cards_region() != NULL; }

584
  HeapWord* orig_end() const { return _orig_end; }
585

586
  // Reset HR stuff to default values.
587
  void hr_clear(bool par, bool clear_space, bool locked = false);
588
  void par_clear();
589 590 591 592 593 594 595 596 597

  // Get the start of the unmarked area in this region.
  HeapWord* prev_top_at_mark_start() const { return _prev_top_at_mark_start; }
  HeapWord* next_top_at_mark_start() const { return _next_top_at_mark_start; }

  // Note the start or end of marking. This tells the heap region
  // that the collector is about to start or has finished (concurrently)
  // marking the heap.

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
  // Notify the region that concurrent marking is starting. Initialize
  // all fields related to the next marking info.
  inline void note_start_of_marking();

  // Notify the region that concurrent marking has finished. Copy the
  // (now finalized) next marking info fields into the prev marking
  // info fields.
  inline void note_end_of_marking();

  // Notify the region that it will be used as to-space during a GC
  // and we are about to start copying objects into it.
  inline void note_start_of_copying(bool during_initial_mark);

  // Notify the region that it ceases being to-space during a GC and
  // we will not copy objects into it any more.
  inline void note_end_of_copying(bool during_initial_mark);

  // Notify the region that we are about to start processing
  // self-forwarded objects during evac failure handling.
  void note_self_forwarding_removal_start(bool during_initial_mark,
                                          bool during_conc_mark);

  // Notify the region that we have finished processing self-forwarded
  // objects during evac failure handling.
  void note_self_forwarding_removal_end(bool during_initial_mark,
                                        bool during_conc_mark,
                                        size_t marked_bytes);
625 626 627 628 629 630

  // Returns "false" iff no object in the region was allocated when the
  // last mark phase ended.
  bool is_marked() { return _prev_top_at_mark_start != bottom(); }

  void reset_during_compaction() {
631 632
    assert(isHumongous() && startsHumongous(),
           "should only be called for starts humongous regions");
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739

    zero_marked_bytes();
    init_top_at_mark_start();
  }

  void calc_gc_efficiency(void);
  double gc_efficiency() { return _gc_efficiency;}

  bool is_young() const     { return _young_type != NotYoung; }
  bool is_survivor() const  { return _young_type == Survivor; }

  int  young_index_in_cset() const { return _young_index_in_cset; }
  void set_young_index_in_cset(int index) {
    assert( (index == -1) || is_young(), "pre-condition" );
    _young_index_in_cset = index;
  }

  int age_in_surv_rate_group() {
    assert( _surv_rate_group != NULL, "pre-condition" );
    assert( _age_index > -1, "pre-condition" );
    return _surv_rate_group->age_in_group(_age_index);
  }

  void record_surv_words_in_group(size_t words_survived) {
    assert( _surv_rate_group != NULL, "pre-condition" );
    assert( _age_index > -1, "pre-condition" );
    int age_in_group = age_in_surv_rate_group();
    _surv_rate_group->record_surviving_words(age_in_group, words_survived);
  }

  int age_in_surv_rate_group_cond() {
    if (_surv_rate_group != NULL)
      return age_in_surv_rate_group();
    else
      return -1;
  }

  SurvRateGroup* surv_rate_group() {
    return _surv_rate_group;
  }

  void install_surv_rate_group(SurvRateGroup* surv_rate_group) {
    assert( surv_rate_group != NULL, "pre-condition" );
    assert( _surv_rate_group == NULL, "pre-condition" );
    assert( is_young(), "pre-condition" );

    _surv_rate_group = surv_rate_group;
    _age_index = surv_rate_group->next_age_index();
  }

  void uninstall_surv_rate_group() {
    if (_surv_rate_group != NULL) {
      assert( _age_index > -1, "pre-condition" );
      assert( is_young(), "pre-condition" );

      _surv_rate_group = NULL;
      _age_index = -1;
    } else {
      assert( _age_index == -1, "pre-condition" );
    }
  }

  void set_young() { set_young_type(Young); }

  void set_survivor() { set_young_type(Survivor); }

  void set_not_young() { set_young_type(NotYoung); }

  // Determine if an object has been allocated since the last
  // mark performed by the collector. This returns true iff the object
  // is within the unmarked area of the region.
  bool obj_allocated_since_prev_marking(oop obj) const {
    return (HeapWord *) obj >= prev_top_at_mark_start();
  }
  bool obj_allocated_since_next_marking(oop obj) const {
    return (HeapWord *) obj >= next_top_at_mark_start();
  }

  // For parallel heapRegion traversal.
  bool claimHeapRegion(int claimValue);
  jint claim_value() { return _claimed; }
  // Use this carefully: only when you're sure no one is claiming...
  void set_claim_value(int claimValue) { _claimed = claimValue; }

  // Returns the "evacuation_failed" property of the region.
  bool evacuation_failed() { return _evacuation_failed; }

  // Sets the "evacuation_failed" property of the region.
  void set_evacuation_failed(bool b) {
    _evacuation_failed = b;

    if (b) {
      _next_marked_bytes = 0;
    }
  }

  // Requires that "mr" be entirely within the region.
  // Apply "cl->do_object" to all objects that intersect with "mr".
  // If the iteration encounters an unparseable portion of the region,
  // or if "cl->abort()" is true after a closure application,
  // terminate the iteration and return the address of the start of the
  // subregion that isn't done.  (The two can be distinguished by querying
  // "cl->abort()".)  Return of "NULL" indicates that the iteration
  // completed.
  HeapWord*
  object_iterate_mem_careful(MemRegion mr, ObjectClosure* cl);

740 741 742 743 744
  // filter_young: if true and the region is a young region then we
  // skip the iteration.
  // card_ptr: if not NULL, and we decide that the card is not young
  // and we iterate over it, we'll clean the card before we start the
  // iteration.
745 746
  HeapWord*
  oops_on_card_seq_iterate_careful(MemRegion mr,
747
                                   FilterOutOfRegionClosure* cl,
748 749
                                   bool filter_young,
                                   jbyte* card_ptr);
750 751 752 753 754 755 756 757 758 759 760 761 762

  // A version of block start that is guaranteed to find *some* block
  // boundary at or before "p", but does not object iteration, and may
  // therefore be used safely when the heap is unparseable.
  HeapWord* block_start_careful(const void* p) const {
    return _offsets.block_start_careful(p);
  }

  // Requires that "addr" is within the region.  Returns the start of the
  // first ("careful") block that starts at or after "addr", or else the
  // "end" of the region if there is no such block.
  HeapWord* next_block_start_careful(HeapWord* addr);

763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
  size_t recorded_rs_length() const        { return _recorded_rs_length; }
  double predicted_elapsed_time_ms() const { return _predicted_elapsed_time_ms; }
  size_t predicted_bytes_to_copy() const   { return _predicted_bytes_to_copy; }

  void set_recorded_rs_length(size_t rs_length) {
    _recorded_rs_length = rs_length;
  }

  void set_predicted_elapsed_time_ms(double ms) {
    _predicted_elapsed_time_ms = ms;
  }

  void set_predicted_bytes_to_copy(size_t bytes) {
    _predicted_bytes_to_copy = bytes;
  }

779
  virtual CompactibleSpace* next_compaction_space() const;
780 781 782

  virtual void reset_after_compaction();

J
johnc 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
  // Routines for managing a list of code roots (attached to the
  // this region's RSet) that point into this heap region.
  void add_strong_code_root(nmethod* nm);
  void remove_strong_code_root(nmethod* nm);

  // During a collection, migrate the successfully evacuated
  // strong code roots that referenced into this region to the
  // new regions that they now point into. Unsuccessfully
  // evacuated code roots are not migrated.
  void migrate_strong_code_roots();

  // Applies blk->do_code_blob() to each of the entries in
  // the strong code roots list for this region
  void strong_code_roots_do(CodeBlobClosure* blk) const;

  // Verify that the entries on the strong code root list for this
  // region are live and include at least one pointer into this region.
  void verify_strong_code_roots(VerifyOption vo, bool* failures) const;

802 803 804
  void print() const;
  void print_on(outputStream* st) const;

805 806 807 808
  // vo == UsePrevMarking  -> use "prev" marking information,
  // vo == UseNextMarking -> use "next" marking information
  // vo == UseMarkWord    -> use the mark word in the object header
  //
809 810
  // NOTE: Only the "prev" marking information is guaranteed to be
  // consistent most of the time, so most calls to this should use
811 812 813 814 815 816 817
  // vo == UsePrevMarking.
  // Currently, there is only one case where this is called with
  // vo == UseNextMarking, which is to verify the "next" marking
  // information at the end of remark.
  // Currently there is only one place where this is called with
  // vo == UseMarkWord, which is to verify the marking during a
  // full GC.
818
  void verify(VerifyOption vo, bool *failures) const;
819 820

  // Override; it uses the "prev" marking information
821
  virtual void verify() const;
822 823 824 825 826
};

// HeapRegionClosure is used for iterating over regions.
// Terminates the iteration when the "doHeapRegion" method returns "true".
class HeapRegionClosure : public StackObj {
827
  friend class HeapRegionManager;
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
  friend class G1CollectedHeap;

  bool _complete;
  void incomplete() { _complete = false; }

 public:
  HeapRegionClosure(): _complete(true) {}

  // Typically called on each region until it returns true.
  virtual bool doHeapRegion(HeapRegion* r) = 0;

  // True after iteration if the closure was applied to all heap regions
  // and returned "false" in all cases.
  bool complete() { return _complete; }
};

844
#endif // INCLUDE_ALL_GCS
845 846

#endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_HPP