psParallelCompact.hpp 56.6 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
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.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35 36
#ifndef SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_PSPARALLELCOMPACT_HPP
#define SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_PSPARALLELCOMPACT_HPP

#include "gc_implementation/parallelScavenge/objectStartArray.hpp"
#include "gc_implementation/parallelScavenge/parMarkBitMap.hpp"
#include "gc_implementation/parallelScavenge/psCompactionManager.hpp"
#include "gc_implementation/shared/collectorCounters.hpp"
#include "gc_implementation/shared/markSweep.hpp"
#include "gc_implementation/shared/mutableSpace.hpp"
#include "memory/sharedHeap.hpp"
#include "oops/oop.hpp"

D
duke 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50
class ParallelScavengeHeap;
class PSAdaptiveSizePolicy;
class PSYoungGen;
class PSOldGen;
class PSPermGen;
class ParCompactionManager;
class ParallelTaskTerminator;
class PSParallelCompact;
class GCTaskManager;
class GCTaskQueue;
class PreGCValues;
class MoveAndUpdateClosure;
class RefProcTaskExecutor;

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
// The SplitInfo class holds the information needed to 'split' a source region
// so that the live data can be copied to two destination *spaces*.  Normally,
// all the live data in a region is copied to a single destination space (e.g.,
// everything live in a region in eden is copied entirely into the old gen).
// However, when the heap is nearly full, all the live data in eden may not fit
// into the old gen.  Copying only some of the regions from eden to old gen
// requires finding a region that does not contain a partial object (i.e., no
// live object crosses the region boundary) somewhere near the last object that
// does fit into the old gen.  Since it's not always possible to find such a
// region, splitting is necessary for predictable behavior.
//
// A region is always split at the end of the partial object.  This avoids
// additional tests when calculating the new location of a pointer, which is a
// very hot code path.  The partial object and everything to its left will be
// copied to another space (call it dest_space_1).  The live data to the right
// of the partial object will be copied either within the space itself, or to a
// different destination space (distinct from dest_space_1).
//
// Split points are identified during the summary phase, when region
// destinations are computed:  data about the split, including the
// partial_object_size, is recorded in a SplitInfo record and the
// partial_object_size field in the summary data is set to zero.  The zeroing is
// possible (and necessary) since the partial object will move to a different
// destination space than anything to its right, thus the partial object should
// not affect the locations of any objects to its right.
//
// The recorded data is used during the compaction phase, but only rarely:  when
// the partial object on the split region will be copied across a destination
// region boundary.  This test is made once each time a region is filled, and is
// a simple address comparison, so the overhead is negligible (see
// PSParallelCompact::first_src_addr()).
//
// Notes:
//
// Only regions with partial objects are split; a region without a partial
// object does not need any extra bookkeeping.
//
// At most one region is split per space, so the amount of data required is
// constant.
//
// A region is split only when the destination space would overflow.  Once that
// happens, the destination space is abandoned and no other data (even from
// other source spaces) is targeted to that destination space.  Abandoning the
// destination space may leave a somewhat large unused area at the end, if a
// large object caused the overflow.
//
// Future work:
//
// More bookkeeping would be required to continue to use the destination space.
// The most general solution would allow data from regions in two different
// source spaces to be "joined" in a single destination region.  At the very
// least, additional code would be required in next_src_region() to detect the
// join and skip to an out-of-order source region.  If the join region was also
// the last destination region to which a split region was copied (the most
// likely case), then additional work would be needed to get fill_region() to
// stop iteration and switch to a new source region at the right point.  Basic
// idea would be to use a fake value for the top of the source space.  It is
// doable, if a bit tricky.
//
// A simpler (but less general) solution would fill the remainder of the
// destination region with a dummy object and continue filling the next
// destination region.

class SplitInfo
{
public:
  // Return true if this split info is valid (i.e., if a split has been
  // recorded).  The very first region cannot have a partial object and thus is
  // never split, so 0 is the 'invalid' value.
  bool is_valid() const { return _src_region_idx > 0; }

  // Return true if this split holds data for the specified source region.
  inline bool is_split(size_t source_region) const;

  // The index of the split region, the size of the partial object on that
  // region and the destination of the partial object.
  size_t    src_region_idx() const   { return _src_region_idx; }
  size_t    partial_obj_size() const { return _partial_obj_size; }
  HeapWord* destination() const      { return _destination; }

  // The destination count of the partial object referenced by this split
  // (either 1 or 2).  This must be added to the destination count of the
  // remainder of the source region.
  unsigned int destination_count() const { return _destination_count; }

  // If a word within the partial object will be written to the first word of a
  // destination region, this is the address of the destination region;
  // otherwise this is NULL.
  HeapWord* dest_region_addr() const     { return _dest_region_addr; }

  // If a word within the partial object will be written to the first word of a
  // destination region, this is the address of that word within the partial
  // object; otherwise this is NULL.
  HeapWord* first_src_addr() const       { return _first_src_addr; }

  // Record the data necessary to split the region src_region_idx.
  void record(size_t src_region_idx, size_t partial_obj_size,
              HeapWord* destination);

  void clear();

  DEBUG_ONLY(void verify_clear();)

private:
  size_t       _src_region_idx;
  size_t       _partial_obj_size;
  HeapWord*    _destination;
  unsigned int _destination_count;
  HeapWord*    _dest_region_addr;
  HeapWord*    _first_src_addr;
};

inline bool SplitInfo::is_split(size_t region_idx) const
{
  return _src_region_idx == region_idx && is_valid();
}

D
duke 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
class SpaceInfo
{
 public:
  MutableSpace* space() const { return _space; }

  // Where the free space will start after the collection.  Valid only after the
  // summary phase completes.
  HeapWord* new_top() const { return _new_top; }

  // Allows new_top to be set.
  HeapWord** new_top_addr() { return &_new_top; }

  // Where the smallest allowable dense prefix ends (used only for perm gen).
  HeapWord* min_dense_prefix() const { return _min_dense_prefix; }

  // Where the dense prefix ends, or the compacted region begins.
  HeapWord* dense_prefix() const { return _dense_prefix; }

  // The start array for the (generation containing the) space, or NULL if there
  // is no start array.
  ObjectStartArray* start_array() const { return _start_array; }

190 191
  SplitInfo& split_info() { return _split_info; }

D
duke 已提交
192 193 194 195 196 197
  void set_space(MutableSpace* s)           { _space = s; }
  void set_new_top(HeapWord* addr)          { _new_top = addr; }
  void set_min_dense_prefix(HeapWord* addr) { _min_dense_prefix = addr; }
  void set_dense_prefix(HeapWord* addr)     { _dense_prefix = addr; }
  void set_start_array(ObjectStartArray* s) { _start_array = s; }

198 199
  void publish_new_top() const              { _space->set_top(_new_top); }

D
duke 已提交
200 201 202 203 204 205
 private:
  MutableSpace*     _space;
  HeapWord*         _new_top;
  HeapWord*         _min_dense_prefix;
  HeapWord*         _dense_prefix;
  ObjectStartArray* _start_array;
206
  SplitInfo         _split_info;
D
duke 已提交
207 208 209 210 211 212
};

class ParallelCompactData
{
public:
  // Sizes are in HeapWords, unless indicated otherwise.
213 214 215
  static const size_t Log2RegionSize;
  static const size_t RegionSize;
  static const size_t RegionSizeBytes;
D
duke 已提交
216

217 218 219 220 221 222
  // Mask for the bits in a size_t to get an offset within a region.
  static const size_t RegionSizeOffsetMask;
  // Mask for the bits in a pointer to get an offset within a region.
  static const size_t RegionAddrOffsetMask;
  // Mask for the bits in a pointer to get the address of the start of a region.
  static const size_t RegionAddrMask;
D
duke 已提交
223

224
  class RegionData
D
duke 已提交
225 226
  {
  public:
227
    // Destination address of the region.
D
duke 已提交
228 229
    HeapWord* destination() const { return _destination; }

230 231
    // The first region containing data destined for this region.
    size_t source_region() const { return _source_region; }
D
duke 已提交
232

233 234
    // The object (if any) starting in this region and ending in a different
    // region that could not be updated during the main (parallel) compaction
D
duke 已提交
235
    // phase.  This is different from _partial_obj_addr, which is an object that
236
    // extends onto a source region.  However, the two uses do not overlap in
D
duke 已提交
237 238 239
    // time, so the same field is used to save space.
    HeapWord* deferred_obj_addr() const { return _partial_obj_addr; }

240
    // The starting address of the partial object extending onto the region.
D
duke 已提交
241 242
    HeapWord* partial_obj_addr() const { return _partial_obj_addr; }

243
    // Size of the partial object extending onto the region (words).
D
duke 已提交
244 245
    size_t partial_obj_size() const { return _partial_obj_size; }

246 247 248 249
    // Size of live data that lies within this region due to objects that start
    // in this region (words).  This does not include the partial object
    // extending onto the region (if any), or the part of an object that extends
    // onto the next region (if any).
D
duke 已提交
250 251
    size_t live_obj_size() const { return _dc_and_los & los_mask; }

252
    // Total live data that lies within the region (words).
D
duke 已提交
253 254
    size_t data_size() const { return partial_obj_size() + live_obj_size(); }

255 256
    // The destination_count is the number of other regions to which data from
    // this region will be copied.  At the end of the summary phase, the valid
D
duke 已提交
257 258
    // values of destination_count are
    //
259 260 261 262 263
    // 0 - data from the region will be compacted completely into itself, or the
    //     region is empty.  The region can be claimed and then filled.
    // 1 - data from the region will be compacted into 1 other region; some
    //     data from the region may also be compacted into the region itself.
    // 2 - data from the region will be copied to 2 other regions.
D
duke 已提交
264
    //
265
    // During compaction as regions are emptied, the destination_count is
D
duke 已提交
266 267 268
    // decremented (atomically) and when it reaches 0, it can be claimed and
    // then filled.
    //
269 270
    // A region is claimed for processing by atomically changing the
    // destination_count to the claimed value (dc_claimed).  After a region has
D
duke 已提交
271 272 273 274 275
    // been filled, the destination_count should be set to the completed value
    // (dc_completed).
    inline uint destination_count() const;
    inline uint destination_count_raw() const;

276
    // The location of the java heap data that corresponds to this region.
D
duke 已提交
277 278
    inline HeapWord* data_location() const;

279
    // The highest address referenced by objects in this region.
D
duke 已提交
280 281
    inline HeapWord* highest_ref() const;

282
    // Whether this region is available to be claimed, has been claimed, or has
D
duke 已提交
283 284
    // been completed.
    //
285 286
    // Minor subtlety:  claimed() returns true if the region is marked
    // completed(), which is desirable since a region must be claimed before it
D
duke 已提交
287 288 289 290 291 292 293
    // can be completed.
    bool available() const { return _dc_and_los < dc_one; }
    bool claimed() const   { return _dc_and_los >= dc_claimed; }
    bool completed() const { return _dc_and_los >= dc_completed; }

    // These are not atomic.
    void set_destination(HeapWord* addr)       { _destination = addr; }
294
    void set_source_region(size_t region)      { _source_region = region; }
D
duke 已提交
295 296 297
    void set_deferred_obj_addr(HeapWord* addr) { _partial_obj_addr = addr; }
    void set_partial_obj_addr(HeapWord* addr)  { _partial_obj_addr = addr; }
    void set_partial_obj_size(size_t words)    {
298
      _partial_obj_size = (region_sz_t) words;
D
duke 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    }

    inline void set_destination_count(uint count);
    inline void set_live_obj_size(size_t words);
    inline void set_data_location(HeapWord* addr);
    inline void set_completed();
    inline bool claim_unsafe();

    // These are atomic.
    inline void add_live_obj(size_t words);
    inline void set_highest_ref(HeapWord* addr);
    inline void decrement_destination_count();
    inline bool claim();

  private:
314 315
    // The type used to represent object sizes within a region.
    typedef uint region_sz_t;
D
duke 已提交
316 317 318 319

    // Constants for manipulating the _dc_and_los field, which holds both the
    // destination count and live obj size.  The live obj size lives at the
    // least significant end so no masking is necessary when adding.
320 321 322 323 324 325 326 327 328 329 330 331
    static const region_sz_t dc_shift;           // Shift amount.
    static const region_sz_t dc_mask;            // Mask for destination count.
    static const region_sz_t dc_one;             // 1, shifted appropriately.
    static const region_sz_t dc_claimed;         // Region has been claimed.
    static const region_sz_t dc_completed;       // Region has been completed.
    static const region_sz_t los_mask;           // Mask for live obj size.

    HeapWord*            _destination;
    size_t               _source_region;
    HeapWord*            _partial_obj_addr;
    region_sz_t          _partial_obj_size;
    region_sz_t volatile _dc_and_los;
D
duke 已提交
332 333 334
#ifdef ASSERT
    // These enable optimizations that are only partially implemented.  Use
    // debug builds to prevent the code fragments from breaking.
335 336
    HeapWord*            _data_location;
    HeapWord*            _highest_ref;
D
duke 已提交
337 338 339 340
#endif  // #ifdef ASSERT

#ifdef ASSERT
   public:
341
    uint            _pushed;   // 0 until region is pushed onto a worker's stack
D
duke 已提交
342 343 344 345 346 347 348 349
   private:
#endif
  };

public:
  ParallelCompactData();
  bool initialize(MemRegion covered_region);

350
  size_t region_count() const { return _region_count; }
D
duke 已提交
351

352 353 354
  // Convert region indices to/from RegionData pointers.
  inline RegionData* region(size_t region_idx) const;
  inline size_t     region(const RegionData* const region_ptr) const;
D
duke 已提交
355

356 357
  // Returns true if the given address is contained within the region
  bool region_contains(size_t region_index, HeapWord* addr);
D
duke 已提交
358 359 360 361

  void add_obj(HeapWord* addr, size_t len);
  void add_obj(oop p, size_t len) { add_obj((HeapWord*)p, len); }

362 363 364
  // Fill in the regions covering [beg, end) so that no data moves; i.e., the
  // destination of region n is simply the start of region n.  The argument beg
  // must be region-aligned; end need not be.
D
duke 已提交
365 366
  void summarize_dense_prefix(HeapWord* beg, HeapWord* end);

367 368 369 370
  HeapWord* summarize_split_space(size_t src_region, SplitInfo& split_info,
                                  HeapWord* destination, HeapWord* target_end,
                                  HeapWord** target_next);
  bool summarize(SplitInfo& split_info,
D
duke 已提交
371
                 HeapWord* source_beg, HeapWord* source_end,
372 373 374
                 HeapWord** source_next,
                 HeapWord* target_beg, HeapWord* target_end,
                 HeapWord** target_next);
D
duke 已提交
375 376

  void clear();
377
  void clear_range(size_t beg_region, size_t end_region);
D
duke 已提交
378
  void clear_range(HeapWord* beg, HeapWord* end) {
379
    clear_range(addr_to_region_idx(beg), addr_to_region_idx(end));
D
duke 已提交
380 381
  }

382
  // Return the number of words between addr and the start of the region
D
duke 已提交
383
  // containing addr.
384
  inline size_t     region_offset(const HeapWord* addr) const;
D
duke 已提交
385

386 387 388 389 390 391
  // Convert addresses to/from a region index or region pointer.
  inline size_t     addr_to_region_idx(const HeapWord* addr) const;
  inline RegionData* addr_to_region_ptr(const HeapWord* addr) const;
  inline HeapWord*  region_to_addr(size_t region) const;
  inline HeapWord*  region_to_addr(size_t region, size_t offset) const;
  inline HeapWord*  region_to_addr(const RegionData* region) const;
D
duke 已提交
392

393 394 395
  inline HeapWord*  region_align_down(HeapWord* addr) const;
  inline HeapWord*  region_align_up(HeapWord* addr) const;
  inline bool       is_region_aligned(HeapWord* addr) const;
D
duke 已提交
396 397

  // Return the address one past the end of the partial object.
398
  HeapWord* partial_obj_end(size_t region_idx) const;
D
duke 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

  // Return the new location of the object p after the
  // the compaction.
  HeapWord* calc_new_pointer(HeapWord* addr);

  HeapWord* calc_new_pointer(oop p) {
    return calc_new_pointer((HeapWord*) p);
  }

  // Return the updated address for the given klass
  klassOop calc_new_klass(klassOop);

#ifdef  ASSERT
  void verify_clear(const PSVirtualSpace* vspace);
  void verify_clear();
#endif  // #ifdef ASSERT

private:
417
  bool initialize_region_data(size_t region_size);
D
duke 已提交
418 419 420 421 422 423 424 425
  PSVirtualSpace* create_vspace(size_t count, size_t element_size);

private:
  HeapWord*       _region_start;
#ifdef  ASSERT
  HeapWord*       _region_end;
#endif  // #ifdef ASSERT

426 427 428
  PSVirtualSpace* _region_vspace;
  RegionData*     _region_data;
  size_t          _region_count;
D
duke 已提交
429 430 431
};

inline uint
432
ParallelCompactData::RegionData::destination_count_raw() const
D
duke 已提交
433 434 435 436 437
{
  return _dc_and_los & dc_mask;
}

inline uint
438
ParallelCompactData::RegionData::destination_count() const
D
duke 已提交
439 440 441 442 443
{
  return destination_count_raw() >> dc_shift;
}

inline void
444
ParallelCompactData::RegionData::set_destination_count(uint count)
D
duke 已提交
445 446
{
  assert(count <= (dc_completed >> dc_shift), "count too large");
447
  const region_sz_t live_sz = (region_sz_t) live_obj_size();
D
duke 已提交
448 449 450
  _dc_and_los = (count << dc_shift) | live_sz;
}

451
inline void ParallelCompactData::RegionData::set_live_obj_size(size_t words)
D
duke 已提交
452 453
{
  assert(words <= los_mask, "would overflow");
454
  _dc_and_los = destination_count_raw() | (region_sz_t)words;
D
duke 已提交
455 456
}

457
inline void ParallelCompactData::RegionData::decrement_destination_count()
D
duke 已提交
458 459 460 461 462 463
{
  assert(_dc_and_los < dc_claimed, "already claimed");
  assert(_dc_and_los >= dc_one, "count would go negative");
  Atomic::add((int)dc_mask, (volatile int*)&_dc_and_los);
}

464
inline HeapWord* ParallelCompactData::RegionData::data_location() const
D
duke 已提交
465 466 467 468 469
{
  DEBUG_ONLY(return _data_location;)
  NOT_DEBUG(return NULL;)
}

470
inline HeapWord* ParallelCompactData::RegionData::highest_ref() const
D
duke 已提交
471 472 473 474 475
{
  DEBUG_ONLY(return _highest_ref;)
  NOT_DEBUG(return NULL;)
}

476
inline void ParallelCompactData::RegionData::set_data_location(HeapWord* addr)
D
duke 已提交
477 478 479 480
{
  DEBUG_ONLY(_data_location = addr;)
}

481
inline void ParallelCompactData::RegionData::set_completed()
D
duke 已提交
482 483
{
  assert(claimed(), "must be claimed first");
484
  _dc_and_los = dc_completed | (region_sz_t) live_obj_size();
D
duke 已提交
485 486
}

487
// MT-unsafe claiming of a region.  Should only be used during single threaded
D
duke 已提交
488
// execution.
489
inline bool ParallelCompactData::RegionData::claim_unsafe()
D
duke 已提交
490 491 492 493 494 495 496 497
{
  if (available()) {
    _dc_and_los |= dc_claimed;
    return true;
  }
  return false;
}

498
inline void ParallelCompactData::RegionData::add_live_obj(size_t words)
D
duke 已提交
499 500 501 502 503
{
  assert(words <= (size_t)los_mask - live_obj_size(), "overflow");
  Atomic::add((int) words, (volatile int*) &_dc_and_los);
}

504
inline void ParallelCompactData::RegionData::set_highest_ref(HeapWord* addr)
D
duke 已提交
505 506 507 508 509 510 511 512 513
{
#ifdef ASSERT
  HeapWord* tmp = _highest_ref;
  while (addr > tmp) {
    tmp = (HeapWord*)Atomic::cmpxchg_ptr(addr, &_highest_ref, tmp);
  }
#endif  // #ifdef ASSERT
}

514
inline bool ParallelCompactData::RegionData::claim()
D
duke 已提交
515 516 517 518 519 520 521
{
  const int los = (int) live_obj_size();
  const int old = Atomic::cmpxchg(dc_claimed | los,
                                  (volatile int*) &_dc_and_los, los);
  return old == los;
}

522 523
inline ParallelCompactData::RegionData*
ParallelCompactData::region(size_t region_idx) const
D
duke 已提交
524
{
525 526
  assert(region_idx <= region_count(), "bad arg");
  return _region_data + region_idx;
D
duke 已提交
527 528 529
}

inline size_t
530
ParallelCompactData::region(const RegionData* const region_ptr) const
D
duke 已提交
531
{
532 533 534
  assert(region_ptr >= _region_data, "bad arg");
  assert(region_ptr <= _region_data + region_count(), "bad arg");
  return pointer_delta(region_ptr, _region_data, sizeof(RegionData));
D
duke 已提交
535 536 537
}

inline size_t
538
ParallelCompactData::region_offset(const HeapWord* addr) const
D
duke 已提交
539 540 541
{
  assert(addr >= _region_start, "bad addr");
  assert(addr <= _region_end, "bad addr");
542
  return (size_t(addr) & RegionAddrOffsetMask) >> LogHeapWordSize;
D
duke 已提交
543 544 545
}

inline size_t
546
ParallelCompactData::addr_to_region_idx(const HeapWord* addr) const
D
duke 已提交
547 548 549
{
  assert(addr >= _region_start, "bad addr");
  assert(addr <= _region_end, "bad addr");
550
  return pointer_delta(addr, _region_start) >> Log2RegionSize;
D
duke 已提交
551 552
}

553 554
inline ParallelCompactData::RegionData*
ParallelCompactData::addr_to_region_ptr(const HeapWord* addr) const
D
duke 已提交
555
{
556
  return region(addr_to_region_idx(addr));
D
duke 已提交
557 558 559
}

inline HeapWord*
560
ParallelCompactData::region_to_addr(size_t region) const
D
duke 已提交
561
{
562 563
  assert(region <= _region_count, "region out of range");
  return _region_start + (region << Log2RegionSize);
D
duke 已提交
564 565 566
}

inline HeapWord*
567
ParallelCompactData::region_to_addr(const RegionData* region) const
D
duke 已提交
568
{
569 570
  return region_to_addr(pointer_delta(region, _region_data,
                                      sizeof(RegionData)));
D
duke 已提交
571 572 573
}

inline HeapWord*
574
ParallelCompactData::region_to_addr(size_t region, size_t offset) const
D
duke 已提交
575
{
576 577 578
  assert(region <= _region_count, "region out of range");
  assert(offset < RegionSize, "offset too big");  // This may be too strict.
  return region_to_addr(region) + offset;
D
duke 已提交
579 580 581
}

inline HeapWord*
582
ParallelCompactData::region_align_down(HeapWord* addr) const
D
duke 已提交
583 584
{
  assert(addr >= _region_start, "bad addr");
585 586
  assert(addr < _region_end + RegionSize, "bad addr");
  return (HeapWord*)(size_t(addr) & RegionAddrMask);
D
duke 已提交
587 588 589
}

inline HeapWord*
590
ParallelCompactData::region_align_up(HeapWord* addr) const
D
duke 已提交
591 592 593
{
  assert(addr >= _region_start, "bad addr");
  assert(addr <= _region_end, "bad addr");
594
  return region_align_down(addr + RegionSizeOffsetMask);
D
duke 已提交
595 596 597
}

inline bool
598
ParallelCompactData::is_region_aligned(HeapWord* addr) const
D
duke 已提交
599
{
600
  return region_offset(addr) == 0;
D
duke 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 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
}

// Abstract closure for use with ParMarkBitMap::iterate(), which will invoke the
// do_addr() method.
//
// The closure is initialized with the number of heap words to process
// (words_remaining()), and becomes 'full' when it reaches 0.  The do_addr()
// methods in subclasses should update the total as words are processed.  Since
// only one subclass actually uses this mechanism to terminate iteration, the
// default initial value is > 0.  The implementation is here and not in the
// single subclass that uses it to avoid making is_full() virtual, and thus
// adding a virtual call per live object.

class ParMarkBitMapClosure: public StackObj {
 public:
  typedef ParMarkBitMap::idx_t idx_t;
  typedef ParMarkBitMap::IterationStatus IterationStatus;

 public:
  inline ParMarkBitMapClosure(ParMarkBitMap* mbm, ParCompactionManager* cm,
                              size_t words = max_uintx);

  inline ParCompactionManager* compaction_manager() const;
  inline ParMarkBitMap*        bitmap() const;
  inline size_t                words_remaining() const;
  inline bool                  is_full() const;
  inline HeapWord*             source() const;

  inline void                  set_source(HeapWord* addr);

  virtual IterationStatus do_addr(HeapWord* addr, size_t words) = 0;

 protected:
  inline void decrement_words_remaining(size_t words);

 private:
  ParMarkBitMap* const        _bitmap;
  ParCompactionManager* const _compaction_manager;
  DEBUG_ONLY(const size_t     _initial_words_remaining;) // Useful in debugger.
  size_t                      _words_remaining; // Words left to copy.

 protected:
  HeapWord*                   _source;          // Next addr that would be read.
};

inline
ParMarkBitMapClosure::ParMarkBitMapClosure(ParMarkBitMap* bitmap,
                                           ParCompactionManager* cm,
                                           size_t words):
  _bitmap(bitmap), _compaction_manager(cm)
#ifdef  ASSERT
  , _initial_words_remaining(words)
#endif
{
  _words_remaining = words;
  _source = NULL;
}

inline ParCompactionManager* ParMarkBitMapClosure::compaction_manager() const {
  return _compaction_manager;
}

inline ParMarkBitMap* ParMarkBitMapClosure::bitmap() const {
  return _bitmap;
}

inline size_t ParMarkBitMapClosure::words_remaining() const {
  return _words_remaining;
}

inline bool ParMarkBitMapClosure::is_full() const {
  return words_remaining() == 0;
}

inline HeapWord* ParMarkBitMapClosure::source() const {
  return _source;
}

inline void ParMarkBitMapClosure::set_source(HeapWord* addr) {
  _source = addr;
}

inline void ParMarkBitMapClosure::decrement_words_remaining(size_t words) {
  assert(_words_remaining >= words, "processed too many words");
  _words_remaining -= words;
}

688 689 690 691 692 693 694 695 696
// The UseParallelOldGC collector is a stop-the-world garbage collector that
// does parts of the collection using parallel threads.  The collection includes
// the tenured generation and the young generation.  The permanent generation is
// collected at the same time as the other two generations but the permanent
// generation is collect by a single GC thread.  The permanent generation is
// collected serially because of the requirement that during the processing of a
// klass AAA, any objects reference by AAA must already have been processed.
// This requirement is enforced by a left (lower address) to right (higher
// address) sliding compaction.
697 698 699 700 701 702 703 704 705 706 707 708 709 710
//
// There are four phases of the collection.
//
//      - marking phase
//      - summary phase
//      - compacting phase
//      - clean up phase
//
// Roughly speaking these phases correspond, respectively, to
//      - mark all the live objects
//      - calculate the destination of each object at the end of the collection
//      - move the objects to their destination
//      - update some references and reinitialize some variables
//
711 712 713 714 715
// These three phases are invoked in PSParallelCompact::invoke_no_policy().  The
// marking phase is implemented in PSParallelCompact::marking_phase() and does a
// complete marking of the heap.  The summary phase is implemented in
// PSParallelCompact::summary_phase().  The move and update phase is implemented
// in PSParallelCompact::compact().
716
//
717 718 719 720
// A space that is being collected is divided into regions and with each region
// is associated an object of type ParallelCompactData.  Each region is of a
// fixed size and typically will contain more than 1 object and may have parts
// of objects at the front and back of the region.
721
//
722
// region            -----+---------------------+----------
723 724
// objects covered   [ AAA  )[ BBB )[ CCC   )[ DDD     )
//
725 726 727 728 729 730 731 732 733 734
// The marking phase does a complete marking of all live objects in the heap.
// The marking also compiles the size of the data for all live objects covered
// by the region.  This size includes the part of any live object spanning onto
// the region (part of AAA if it is live) from the front, all live objects
// contained in the region (BBB and/or CCC if they are live), and the part of
// any live objects covered by the region that extends off the region (part of
// DDD if it is live).  The marking phase uses multiple GC threads and marking
// is done in a bit array of type ParMarkBitMap.  The marking of the bit map is
// done atomically as is the accumulation of the size of the live objects
// covered by a region.
735
//
736 737 738 739
// The summary phase calculates the total live data to the left of each region
// XXX.  Based on that total and the bottom of the space, it can calculate the
// starting location of the live data in XXX.  The summary phase calculates for
// each region XXX quantites such as
740
//
741 742 743 744
//      - the amount of live data at the beginning of a region from an object
//        entering the region.
//      - the location of the first live data on the region
//      - a count of the number of regions receiving live data from XXX.
745 746
//
// See ParallelCompactData for precise details.  The summary phase also
747 748 749 750
// calculates the dense prefix for the compaction.  The dense prefix is a
// portion at the beginning of the space that is not moved.  The objects in the
// dense prefix do need to have their object references updated.  See method
// summarize_dense_prefix().
751 752 753
//
// The summary phase is done using 1 GC thread.
//
754 755
// The compaction phase moves objects to their new location and updates all
// references in the object.
756
//
757 758 759 760 761 762 763
// A current exception is that objects that cross a region boundary are moved
// but do not have their references updated.  References are not updated because
// it cannot easily be determined if the klass pointer KKK for the object AAA
// has been updated.  KKK likely resides in a region to the left of the region
// containing AAA.  These AAA's have there references updated at the end in a
// clean up phase.  See the method PSParallelCompact::update_deferred_objects().
// An alternate strategy is being investigated for this deferral of updating.
764
//
765 766 767 768 769 770 771 772 773 774
// Compaction is done on a region basis.  A region that is ready to be filled is
// put on a ready list and GC threads take region off the list and fill them.  A
// region is ready to be filled if it empty of live objects.  Such a region may
// have been initially empty (only contained dead objects) or may have had all
// its live objects copied out already.  A region that compacts into itself is
// also ready for filling.  The ready list is initially filled with empty
// regions and regions compacting into themselves.  There is always at least 1
// region that can be put on the ready list.  The regions are atomically added
// and removed from the ready list.

D
duke 已提交
775 776 777 778
class PSParallelCompact : AllStatic {
 public:
  // Convenient access to type names.
  typedef ParMarkBitMap::idx_t idx_t;
779
  typedef ParallelCompactData::RegionData RegionData;
D
duke 已提交
780 781 782 783 784 785 786

  typedef enum {
    perm_space_id, old_space_id, eden_space_id,
    from_space_id, to_space_id, last_space_id
  } SpaceId;

 public:
787
  // Inline closure decls
D
duke 已提交
788 789 790
  //
  class IsAliveClosure: public BoolObjectClosure {
   public:
791 792
    virtual void do_object(oop p);
    virtual bool do_object_b(oop p);
D
duke 已提交
793 794 795
  };

  class KeepAliveClosure: public OopClosure {
796
   private:
D
duke 已提交
797
    ParCompactionManager* _compaction_manager;
798 799
   protected:
    template <class T> inline void do_oop_work(T* p);
D
duke 已提交
800
   public:
801 802 803
    KeepAliveClosure(ParCompactionManager* cm) : _compaction_manager(cm) { }
    virtual void do_oop(oop* p);
    virtual void do_oop(narrowOop* p);
D
duke 已提交
804 805
  };

806 807 808
  // Current unused
  class FollowRootClosure: public OopsInGenClosure {
   private:
D
duke 已提交
809 810
    ParCompactionManager* _compaction_manager;
   public:
811 812 813
    FollowRootClosure(ParCompactionManager* cm) : _compaction_manager(cm) { }
    virtual void do_oop(oop* p);
    virtual void do_oop(narrowOop* p);
814
 };
D
duke 已提交
815 816

  class FollowStackClosure: public VoidClosure {
817
   private:
D
duke 已提交
818 819
    ParCompactionManager* _compaction_manager;
   public:
820 821
    FollowStackClosure(ParCompactionManager* cm) : _compaction_manager(cm) { }
    virtual void do_void();
D
duke 已提交
822 823 824
  };

  class AdjustPointerClosure: public OopsInGenClosure {
825
   private:
D
duke 已提交
826 827
    bool _is_root;
   public:
828 829 830
    AdjustPointerClosure(bool is_root) : _is_root(is_root) { }
    virtual void do_oop(oop* p);
    virtual void do_oop(narrowOop* p);
831 832
    // do not walk from thread stacks to the code cache on this phase
    virtual void do_code_blob(CodeBlob* cb) const { }
D
duke 已提交
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
  };

  friend class KeepAliveClosure;
  friend class FollowStackClosure;
  friend class AdjustPointerClosure;
  friend class FollowRootClosure;
  friend class instanceKlassKlass;
  friend class RefProcTaskProxy;

 private:
  static elapsedTimer         _accumulated_time;
  static unsigned int         _total_invocations;
  static unsigned int         _maximum_compaction_gc_num;
  static jlong                _time_of_last_gc;   // ms
  static CollectorCounters*   _counters;
  static ParMarkBitMap        _mark_bitmap;
  static ParallelCompactData  _summary_data;
  static IsAliveClosure       _is_alive_closure;
  static SpaceInfo            _space_info[last_space_id];
  static bool                 _print_phases;
  static AdjustPointerClosure _adjust_root_pointer_closure;
  static AdjustPointerClosure _adjust_pointer_closure;

  // Reference processing (used in ...follow_contents)
  static ReferenceProcessor*  _ref_processor;

  // Updated location of intArrayKlassObj.
  static klassOop _updated_int_array_klass_obj;

  // Values computed at initialization and used by dead_wood_limiter().
  static double _dwl_mean;
  static double _dwl_std_dev;
  static double _dwl_first_term;
  static double _dwl_adjustment;
#ifdef  ASSERT
  static bool   _dwl_initialized;
#endif  // #ifdef ASSERT

 private:
  // Closure accessors
873
  static OopClosure* adjust_pointer_closure()      { return (OopClosure*)&_adjust_pointer_closure; }
D
duke 已提交
874
  static OopClosure* adjust_root_pointer_closure() { return (OopClosure*)&_adjust_root_pointer_closure; }
875
  static BoolObjectClosure* is_alive_closure()     { return (BoolObjectClosure*)&_is_alive_closure; }
D
duke 已提交
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890

  static void initialize_space_info();

  // Return true if details about individual phases should be printed.
  static inline bool print_phases();

  // Clear the marking bitmap and summary data that cover the specified space.
  static void clear_data_covering_space(SpaceId id);

  static void pre_compact(PreGCValues* pre_gc_values);
  static void post_compact();

  // Mark live objects
  static void marking_phase(ParCompactionManager* cm,
                            bool maximum_heap_compaction);
Y
ysr 已提交
891 892
  static void follow_weak_klass_links();
  static void follow_mdo_weak_refs();
D
duke 已提交
893

894
  template <class T> static inline void adjust_pointer(T* p, bool is_root);
D
duke 已提交
895 896
  static void adjust_root_pointer(oop* p) { adjust_pointer(p, true); }

897 898
  template <class T>
  static inline void follow_root(ParCompactionManager* cm, T* p);
D
duke 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917

  // Compute the dense prefix for the designated space.  This is an experimental
  // implementation currently not used in production.
  static HeapWord* compute_dense_prefix_via_density(const SpaceId id,
                                                    bool maximum_compaction);

  // Methods used to compute the dense prefix.

  // Compute the value of the normal distribution at x = density.  The mean and
  // standard deviation are values saved by initialize_dead_wood_limiter().
  static inline double normal_distribution(double density);

  // Initialize the static vars used by dead_wood_limiter().
  static void initialize_dead_wood_limiter();

  // Return the percentage of space that can be treated as "dead wood" (i.e.,
  // not reclaimed).
  static double dead_wood_limiter(double density, size_t min_percent);

918
  // Find the first (left-most) region in the range [beg, end) that has at least
D
duke 已提交
919
  // dead_words of dead space to the left.  The argument beg must be the first
920 921 922 923
  // region in the space that is not completely live.
  static RegionData* dead_wood_limit_region(const RegionData* beg,
                                            const RegionData* end,
                                            size_t dead_words);
D
duke 已提交
924

925
  // Return a pointer to the first region in the range [beg, end) that is not
D
duke 已提交
926
  // completely full.
927 928
  static RegionData* first_dead_space_region(const RegionData* beg,
                                             const RegionData* end);
D
duke 已提交
929 930 931

  // Return a value indicating the benefit or 'yield' if the compacted region
  // were to start (or equivalently if the dense prefix were to end) at the
932
  // candidate region.  Higher values are better.
D
duke 已提交
933 934 935 936
  //
  // The value is based on the amount of space reclaimed vs. the costs of (a)
  // updating references in the dense prefix plus (b) copying objects and
  // updating references in the compacted region.
937
  static inline double reclaimed_ratio(const RegionData* const candidate,
D
duke 已提交
938 939 940 941 942 943 944 945
                                       HeapWord* const bottom,
                                       HeapWord* const top,
                                       HeapWord* const new_top);

  // Compute the dense prefix for the designated space.
  static HeapWord* compute_dense_prefix(const SpaceId id,
                                        bool maximum_compaction);

946 947 948
  // Return true if dead space crosses onto the specified Region; bit must be
  // the bit index corresponding to the first word of the Region.
  static inline bool dead_space_crosses_boundary(const RegionData* region,
D
duke 已提交
949 950 951 952 953 954 955
                                                 idx_t bit);

  // Summary phase utility routine to fill dead space (if any) at the dense
  // prefix boundary.  Should only be called if the the dense prefix is
  // non-empty.
  static void fill_dense_prefix_end(SpaceId id);

956 957 958
  // Clear the summary data source_region field for the specified addresses.
  static void clear_source_region(HeapWord* beg_addr, HeapWord* end_addr);

959 960 961 962 963 964 965 966 967 968
#ifndef PRODUCT
  // Routines to provoke splitting a young gen space (ParallelOldGCSplitALot).

  // Fill the region [start, start + words) with live object(s).  Only usable
  // for the old and permanent generations.
  static void fill_with_live_objects(SpaceId id, HeapWord* const start,
                                     size_t words);
  // Include the new objects in the summary data.
  static void summarize_new_objects(SpaceId id, HeapWord* start);

969 970 971 972
  // Add live objects to a survivor space since it's rare that both survivors
  // are non-empty.
  static void provoke_split_fill_survivor(SpaceId id);

973 974 975 976
  // Add live objects and/or choose the dense prefix to provoke splitting.
  static void provoke_split(bool & maximum_compaction);
#endif

D
duke 已提交
977 978 979 980 981 982 983 984 985 986 987 988 989 990
  static void summarize_spaces_quick();
  static void summarize_space(SpaceId id, bool maximum_compaction);
  static void summary_phase(ParCompactionManager* cm, bool maximum_compaction);

  // Adjust addresses in roots.  Does not adjust addresses in heap.
  static void adjust_roots();

  // Serial code executed in preparation for the compaction phase.
  static void compact_prologue();

  // Move objects to new locations.
  static void compact_perm(ParCompactionManager* cm);
  static void compact();

991 992 993
  // Add available regions to the stack and draining tasks to the task queue.
  static void enqueue_region_draining_tasks(GCTaskQueue* q,
                                            uint parallel_gc_threads);
D
duke 已提交
994 995 996 997 998

  // Add dense prefix update tasks to the task queue.
  static void enqueue_dense_prefix_tasks(GCTaskQueue* q,
                                         uint parallel_gc_threads);

999 1000
  // Add region stealing tasks to the task queue.
  static void enqueue_region_stealing_tasks(
D
duke 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
                                       GCTaskQueue* q,
                                       ParallelTaskTerminator* terminator_ptr,
                                       uint parallel_gc_threads);

  // If objects are left in eden after a collection, try to move the boundary
  // and absorb them into the old gen.  Returns true if eden was emptied.
  static bool absorb_live_data_from_eden(PSAdaptiveSizePolicy* size_policy,
                                         PSYoungGen* young_gen,
                                         PSOldGen* old_gen);

  // Reset time since last full gc
  static void reset_millis_since_last_gc();

 protected:
#ifdef VALIDATE_MARK_SWEEP
1016
  static GrowableArray<void*>*           _root_refs_stack;
D
duke 已提交
1017 1018 1019 1020 1021
  static GrowableArray<oop> *            _live_oops;
  static GrowableArray<oop> *            _live_oops_moved_to;
  static GrowableArray<size_t>*          _live_oops_size;
  static size_t                          _live_oops_index;
  static size_t                          _live_oops_index_at_perm;
1022 1023
  static GrowableArray<void*>*           _other_refs_stack;
  static GrowableArray<void*>*           _adjusted_pointers;
D
duke 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
  static bool                            _pointer_tracking;
  static bool                            _root_tracking;

  // The following arrays are saved since the time of the last GC and
  // assist in tracking down problems where someone has done an errant
  // store into the heap, usually to an oop that wasn't properly
  // handleized across a GC. If we crash or otherwise fail before the
  // next GC, we can query these arrays to find out the object we had
  // intended to do the store to (assuming it is still alive) and the
  // offset within that object. Covered under RecordMarkSweepCompaction.
  static GrowableArray<HeapWord*> *      _cur_gc_live_oops;
  static GrowableArray<HeapWord*> *      _cur_gc_live_oops_moved_to;
  static GrowableArray<size_t>*          _cur_gc_live_oops_size;
  static GrowableArray<HeapWord*> *      _last_gc_live_oops;
  static GrowableArray<HeapWord*> *      _last_gc_live_oops_moved_to;
  static GrowableArray<size_t>*          _last_gc_live_oops_size;
#endif

 public:
  class MarkAndPushClosure: public OopClosure {
1044
   private:
D
duke 已提交
1045 1046
    ParCompactionManager* _compaction_manager;
   public:
1047 1048 1049
    MarkAndPushClosure(ParCompactionManager* cm) : _compaction_manager(cm) { }
    virtual void do_oop(oop* p);
    virtual void do_oop(narrowOop* p);
D
duke 已提交
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
  };

  PSParallelCompact();

  // Convenient accessor for Universe::heap().
  static ParallelScavengeHeap* gc_heap() {
    return (ParallelScavengeHeap*)Universe::heap();
  }

  static void invoke(bool maximum_heap_compaction);
  static void invoke_no_policy(bool maximum_heap_compaction);

  static void post_initialize();
  // Perform initialization for PSParallelCompact that requires
  // allocations.  This should be called during the VM initialization
  // at a pointer where it would be appropriate to return a JNI_ENOMEM
  // in the event of a failure.
  static bool initialize();

  // Public accessors
  static elapsedTimer* accumulated_time() { return &_accumulated_time; }
  static unsigned int total_invocations() { return _total_invocations; }
  static CollectorCounters* counters()    { return _counters; }

  // Used to add tasks
  static GCTaskManager* const gc_task_manager();
  static klassOop updated_int_array_klass_obj() {
    return _updated_int_array_klass_obj;
  }

  // Marking support
  static inline bool mark_obj(oop obj);
1082 1083 1084
  // Check mark and maybe push on marking stack
  template <class T> static inline void mark_and_push(ParCompactionManager* cm,
                                                      T* p);
D
duke 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102

  // Compaction support.
  // Return true if p is in the range [beg_addr, end_addr).
  static inline bool is_in(HeapWord* p, HeapWord* beg_addr, HeapWord* end_addr);
  static inline bool is_in(oop* p, HeapWord* beg_addr, HeapWord* end_addr);

  // Convenience wrappers for per-space data kept in _space_info.
  static inline MutableSpace*     space(SpaceId space_id);
  static inline HeapWord*         new_top(SpaceId space_id);
  static inline HeapWord*         dense_prefix(SpaceId space_id);
  static inline ObjectStartArray* start_array(SpaceId space_id);

  // Return true if the klass should be updated.
  static inline bool should_update_klass(klassOop k);

  // Move and update the live objects in the specified space.
  static void move_and_update(ParCompactionManager* cm, SpaceId space_id);

1103
  // Process the end of the given region range in the dense prefix.
D
duke 已提交
1104
  // This includes saving any object not updated.
1105 1106 1107 1108 1109 1110 1111 1112 1113
  static void dense_prefix_regions_epilogue(ParCompactionManager* cm,
                                            size_t region_start_index,
                                            size_t region_end_index,
                                            idx_t exiting_object_offset,
                                            idx_t region_offset_start,
                                            idx_t region_offset_end);

  // Update a region in the dense prefix.  For each live object
  // in the region, update it's interior references.  For each
D
duke 已提交
1114
  // dead object, fill it with deadwood. Dead space at the end
1115 1116
  // of a region range will be filled to the start of the next
  // live object regardless of the region_index_end.  None of the
D
duke 已提交
1117 1118 1119 1120 1121
  // objects in the dense prefix move and dead space is dead
  // (holds only dead objects that don't need any processing), so
  // dead space can be filled in any order.
  static void update_and_deadwood_in_dense_prefix(ParCompactionManager* cm,
                                                  SpaceId space_id,
1122 1123
                                                  size_t region_index_start,
                                                  size_t region_index_end);
D
duke 已提交
1124 1125 1126 1127 1128

  // Return the address of the count + 1st live word in the range [beg, end).
  static HeapWord* skip_live_words(HeapWord* beg, HeapWord* end, size_t count);

  // Return the address of the word to be copied to dest_addr, which must be
1129
  // aligned to a region boundary.
D
duke 已提交
1130
  static HeapWord* first_src_addr(HeapWord* const dest_addr,
1131
                                  SpaceId src_space_id,
1132
                                  size_t src_region_idx);
D
duke 已提交
1133

1134 1135
  // Determine the next source region, set closure.source() to the start of the
  // new region return the region index.  Parameter end_addr is the address one
D
duke 已提交
1136 1137 1138
  // beyond the end of source range just processed.  If necessary, switch to a
  // new source space and set src_space_id (in-out parameter) and src_space_top
  // (out parameter) accordingly.
1139 1140 1141 1142
  static size_t next_src_region(MoveAndUpdateClosure& closure,
                                SpaceId& src_space_id,
                                HeapWord*& src_space_top,
                                HeapWord* end_addr);
D
duke 已提交
1143

1144
  // Decrement the destination count for each non-empty source region in the
1145 1146
  // range [beg_region, region(region_align_up(end_addr))).  If the destination
  // count for a region goes to 0 and it needs to be filled, enqueue it.
D
duke 已提交
1147
  static void decrement_destination_counts(ParCompactionManager* cm,
1148
                                           SpaceId src_space_id,
1149
                                           size_t beg_region,
D
duke 已提交
1150 1151
                                           HeapWord* end_addr);

1152 1153 1154 1155
  // Fill a region, copying objects from one or more source regions.
  static void fill_region(ParCompactionManager* cm, size_t region_idx);
  static void fill_and_update_region(ParCompactionManager* cm, size_t region) {
    fill_region(cm, region);
D
duke 已提交
1156 1157 1158 1159 1160 1161 1162 1163
  }

  // Update the deferred objects in the space.
  static void update_deferred_objects(ParCompactionManager* cm, SpaceId id);

  static ParMarkBitMap* mark_bitmap() { return &_mark_bitmap; }
  static ParallelCompactData& summary_data() { return _summary_data; }

1164 1165 1166
  static inline void adjust_pointer(oop* p)       { adjust_pointer(p, false); }
  static inline void adjust_pointer(narrowOop* p) { adjust_pointer(p, false); }

D
duke 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
  // Reference Processing
  static ReferenceProcessor* const ref_processor() { return _ref_processor; }

  // Return the SpaceId for the given address.
  static SpaceId space_id(HeapWord* addr);

  // Time since last full gc (in milliseconds).
  static jlong millis_since_last_gc();

#ifdef VALIDATE_MARK_SWEEP
1177 1178
  static void track_adjusted_pointer(void* p, bool isroot);
  static void check_adjust_pointer(void* p);
D
duke 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
  static void track_interior_pointers(oop obj);
  static void check_interior_pointers();

  static void reset_live_oop_tracking(bool at_perm);
  static void register_live_oop(oop p, size_t size);
  static void validate_live_oop(oop p, size_t size);
  static void live_oop_moved_to(HeapWord* q, size_t size, HeapWord* compaction_top);
  static void compaction_complete();

  // Querying operation of RecordMarkSweepCompaction results.
  // Finds and prints the current base oop and offset for a word
  // within an oop that was live during the last GC. Helpful for
  // tracking down heap stomps.
  static void print_new_location_of_heap_address(HeapWord* q);
#endif  // #ifdef VALIDATE_MARK_SWEEP

  // Call backs for class unloading
  // Update subklass/sibling/implementor links at end of marking.
  static void revisit_weak_klass_link(ParCompactionManager* cm, Klass* k);

Y
ysr 已提交
1199 1200 1201
  // Clear unmarked oops in MDOs at the end of marking.
  static void revisit_mdo(ParCompactionManager* cm, DataLayout* p);

D
duke 已提交
1202 1203 1204
#ifndef PRODUCT
  // Debugging support.
  static const char* space_names[last_space_id];
1205
  static void print_region_ranges();
D
duke 已提交
1206 1207 1208 1209
  static void print_dense_prefix_stats(const char* const algorithm,
                                       const SpaceId id,
                                       const bool maximum_compaction,
                                       HeapWord* const addr);
1210 1211 1212 1213
  static void summary_phase_msg(SpaceId dst_space_id,
                                HeapWord* dst_beg, HeapWord* dst_end,
                                SpaceId src_space_id,
                                HeapWord* src_beg, HeapWord* src_end);
D
duke 已提交
1214 1215 1216
#endif  // #ifndef PRODUCT

#ifdef  ASSERT
1217 1218
  // Sanity check the new location of a word in the heap.
  static inline void check_new_location(HeapWord* old_addr, HeapWord* new_addr);
1219
  // Verify that all the regions have been emptied.
D
duke 已提交
1220 1221 1222 1223
  static void verify_complete(SpaceId space_id);
#endif  // #ifdef ASSERT
};

1224
inline bool PSParallelCompact::mark_obj(oop obj) {
D
duke 已提交
1225 1226 1227 1228 1229 1230 1231 1232 1233
  const int obj_size = obj->size();
  if (mark_bitmap()->mark_obj(obj, obj_size)) {
    _summary_data.add_obj(obj, obj_size);
    return true;
  } else {
    return false;
  }
}

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
template <class T>
inline void PSParallelCompact::follow_root(ParCompactionManager* cm, T* p) {
  assert(!Universe::heap()->is_in_reserved(p),
         "roots shouldn't be things within the heap");
#ifdef VALIDATE_MARK_SWEEP
  if (ValidateMarkSweep) {
    guarantee(!_root_refs_stack->contains(p), "should only be in here once");
    _root_refs_stack->push(p);
  }
#endif
  T heap_oop = oopDesc::load_heap_oop(p);
  if (!oopDesc::is_null(heap_oop)) {
    oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
    if (mark_bitmap()->is_unmarked(obj)) {
      if (mark_obj(obj)) {
        obj->follow_contents(cm);
      }
    }
  }
1253
  cm->follow_marking_stacks();
1254 1255 1256 1257 1258 1259 1260
}

template <class T>
inline void PSParallelCompact::mark_and_push(ParCompactionManager* cm, T* p) {
  T heap_oop = oopDesc::load_heap_oop(p);
  if (!oopDesc::is_null(heap_oop)) {
    oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
1261 1262
    if (mark_bitmap()->is_unmarked(obj) && mark_obj(obj)) {
      cm->push(obj);
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
    }
  }
}

template <class T>
inline void PSParallelCompact::adjust_pointer(T* p, bool isroot) {
  T heap_oop = oopDesc::load_heap_oop(p);
  if (!oopDesc::is_null(heap_oop)) {
    oop obj     = oopDesc::decode_heap_oop_not_null(heap_oop);
    oop new_obj = (oop)summary_data().calc_new_pointer(obj);
    assert(new_obj != NULL ||                     // is forwarding ptr?
           obj->is_shared(),                      // never forwarded?
           "should be forwarded");
    // Just always do the update unconditionally?
    if (new_obj != NULL) {
      assert(Universe::heap()->is_in_reserved(new_obj),
             "should be in object space");
      oopDesc::encode_store_heap_oop_not_null(p, new_obj);
    }
  }
  VALIDATE_MARK_SWEEP_ONLY(track_adjusted_pointer(p, isroot));
}

template <class T>
inline void PSParallelCompact::KeepAliveClosure::do_oop_work(T* p) {
#ifdef VALIDATE_MARK_SWEEP
  if (ValidateMarkSweep) {
    if (!Universe::heap()->is_in_reserved(p)) {
      _root_refs_stack->push(p);
    } else {
      _other_refs_stack->push(p);
    }
  }
#endif
  mark_and_push(_compaction_manager, p);
}

inline bool PSParallelCompact::print_phases() {
D
duke 已提交
1301 1302 1303
  return _print_phases;
}

1304
inline double PSParallelCompact::normal_distribution(double density) {
D
duke 已提交
1305 1306 1307 1308 1309 1310
  assert(_dwl_initialized, "uninitialized");
  const double squared_term = (density - _dwl_mean) / _dwl_std_dev;
  return _dwl_first_term * exp(-0.5 * squared_term * squared_term);
}

inline bool
1311
PSParallelCompact::dead_space_crosses_boundary(const RegionData* region,
D
duke 已提交
1312 1313
                                               idx_t bit)
{
1314 1315
  assert(bit > 0, "cannot call this for the first bit/region");
  assert(_summary_data.region_to_addr(region) == _mark_bitmap.bit_to_addr(bit),
D
duke 已提交
1316 1317 1318
         "sanity check");

  // Dead space crosses the boundary if (1) a partial object does not extend
1319 1320 1321
  // onto the region, (2) an object does not start at the beginning of the
  // region, and (3) an object does not end at the end of the prior region.
  return region->partial_obj_size() == 0 &&
D
duke 已提交
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
    !_mark_bitmap.is_obj_beg(bit) &&
    !_mark_bitmap.is_obj_end(bit - 1);
}

inline bool
PSParallelCompact::is_in(HeapWord* p, HeapWord* beg_addr, HeapWord* end_addr) {
  return p >= beg_addr && p < end_addr;
}

inline bool
PSParallelCompact::is_in(oop* p, HeapWord* beg_addr, HeapWord* end_addr) {
  return is_in((HeapWord*)p, beg_addr, end_addr);
}

inline MutableSpace* PSParallelCompact::space(SpaceId id) {
  assert(id < last_space_id, "id out of range");
  return _space_info[id].space();
}

inline HeapWord* PSParallelCompact::new_top(SpaceId id) {
  assert(id < last_space_id, "id out of range");
  return _space_info[id].new_top();
}

inline HeapWord* PSParallelCompact::dense_prefix(SpaceId id) {
  assert(id < last_space_id, "id out of range");
  return _space_info[id].dense_prefix();
}

inline ObjectStartArray* PSParallelCompact::start_array(SpaceId id) {
  assert(id < last_space_id, "id out of range");
  return _space_info[id].start_array();
}

inline bool PSParallelCompact::should_update_klass(klassOop k) {
  return ((HeapWord*) k) >= dense_prefix(perm_space_id);
}

1360 1361 1362 1363 1364 1365
#ifdef ASSERT
inline void
PSParallelCompact::check_new_location(HeapWord* old_addr, HeapWord* new_addr)
{
  assert(old_addr >= new_addr || space_id(old_addr) != space_id(new_addr),
         "must move left or to a different space");
1366 1367
  assert(is_object_aligned((intptr_t)old_addr) && is_object_aligned((intptr_t)new_addr),
         "checking alignment");
1368 1369 1370
}
#endif // ASSERT

D
duke 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
class MoveAndUpdateClosure: public ParMarkBitMapClosure {
 public:
  inline MoveAndUpdateClosure(ParMarkBitMap* bitmap, ParCompactionManager* cm,
                              ObjectStartArray* start_array,
                              HeapWord* destination, size_t words);

  // Accessors.
  HeapWord* destination() const         { return _destination; }

  // If the object will fit (size <= words_remaining()), copy it to the current
  // destination, update the interior oops and the start array and return either
  // full (if the closure is full) or incomplete.  If the object will not fit,
  // return would_overflow.
  virtual IterationStatus do_addr(HeapWord* addr, size_t size);

  // Copy enough words to fill this closure, starting at source().  Interior
  // oops and the start array are not updated.  Return full.
  IterationStatus copy_until_full();

  // Copy enough words to fill this closure or to the end of an object,
  // whichever is smaller, starting at source().  Interior oops and the start
  // array are not updated.
  void copy_partial_obj();

 protected:
  // Update variables to indicate that word_count words were processed.
  inline void update_state(size_t word_count);

 protected:
  ObjectStartArray* const _start_array;
  HeapWord*               _destination;         // Next addr to be written.
};

inline
MoveAndUpdateClosure::MoveAndUpdateClosure(ParMarkBitMap* bitmap,
                                           ParCompactionManager* cm,
                                           ObjectStartArray* start_array,
                                           HeapWord* destination,
                                           size_t words) :
  ParMarkBitMapClosure(bitmap, cm, words), _start_array(start_array)
{
  _destination = destination;
}

inline void MoveAndUpdateClosure::update_state(size_t words)
{
  decrement_words_remaining(words);
  _source += words;
  _destination += words;
}

class UpdateOnlyClosure: public ParMarkBitMapClosure {
 private:
  const PSParallelCompact::SpaceId _space_id;
  ObjectStartArray* const          _start_array;

 public:
  UpdateOnlyClosure(ParMarkBitMap* mbm,
                    ParCompactionManager* cm,
                    PSParallelCompact::SpaceId space_id);

  // Update the object.
  virtual IterationStatus do_addr(HeapWord* addr, size_t words);

  inline void do_addr(HeapWord* addr);
};

1438 1439
inline void UpdateOnlyClosure::do_addr(HeapWord* addr)
{
D
duke 已提交
1440 1441 1442 1443
  _start_array->allocate_block(addr);
  oop(addr)->update_contents(compaction_manager());
}

1444 1445 1446
class FillClosure: public ParMarkBitMapClosure
{
public:
1447
  FillClosure(ParCompactionManager* cm, PSParallelCompact::SpaceId space_id) :
D
duke 已提交
1448
    ParMarkBitMapClosure(PSParallelCompact::mark_bitmap(), cm),
1449 1450 1451 1452
    _start_array(PSParallelCompact::start_array(space_id))
  {
    assert(space_id == PSParallelCompact::perm_space_id ||
           space_id == PSParallelCompact::old_space_id,
D
duke 已提交
1453 1454 1455 1456
           "cannot use FillClosure in the young gen");
  }

  virtual IterationStatus do_addr(HeapWord* addr, size_t size) {
1457 1458 1459 1460 1461 1462
    CollectedHeap::fill_with_objects(addr, size);
    HeapWord* const end = addr + size;
    do {
      _start_array->allocate_block(addr);
      addr += oop(addr)->size();
    } while (addr < end);
D
duke 已提交
1463 1464 1465 1466
    return ParMarkBitMap::incomplete;
  }

private:
1467
  ObjectStartArray* const _start_array;
D
duke 已提交
1468
};
1469 1470

#endif // SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_PSPARALLELCOMPACT_HPP