src/share/vm/gc_implementation/parallelScavenge/psParallelCompact.hpp

Mon, 28 Jul 2008 15:30:23 -0700

author
jmasa
date
Mon, 28 Jul 2008 15:30:23 -0700
changeset 704
850fdf70db2b
parent 631
d1605aabd0a1
parent 698
12eea04c8b06
child 810
81cd571500b0
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 class ParallelScavengeHeap;
    26 class PSAdaptiveSizePolicy;
    27 class PSYoungGen;
    28 class PSOldGen;
    29 class PSPermGen;
    30 class ParCompactionManager;
    31 class ParallelTaskTerminator;
    32 class PSParallelCompact;
    33 class GCTaskManager;
    34 class GCTaskQueue;
    35 class PreGCValues;
    36 class MoveAndUpdateClosure;
    37 class RefProcTaskExecutor;
    39 class SpaceInfo
    40 {
    41  public:
    42   MutableSpace* space() const { return _space; }
    44   // Where the free space will start after the collection.  Valid only after the
    45   // summary phase completes.
    46   HeapWord* new_top() const { return _new_top; }
    48   // Allows new_top to be set.
    49   HeapWord** new_top_addr() { return &_new_top; }
    51   // Where the smallest allowable dense prefix ends (used only for perm gen).
    52   HeapWord* min_dense_prefix() const { return _min_dense_prefix; }
    54   // Where the dense prefix ends, or the compacted region begins.
    55   HeapWord* dense_prefix() const { return _dense_prefix; }
    57   // The start array for the (generation containing the) space, or NULL if there
    58   // is no start array.
    59   ObjectStartArray* start_array() const { return _start_array; }
    61   void set_space(MutableSpace* s)           { _space = s; }
    62   void set_new_top(HeapWord* addr)          { _new_top = addr; }
    63   void set_min_dense_prefix(HeapWord* addr) { _min_dense_prefix = addr; }
    64   void set_dense_prefix(HeapWord* addr)     { _dense_prefix = addr; }
    65   void set_start_array(ObjectStartArray* s) { _start_array = s; }
    67  private:
    68   MutableSpace*     _space;
    69   HeapWord*         _new_top;
    70   HeapWord*         _min_dense_prefix;
    71   HeapWord*         _dense_prefix;
    72   ObjectStartArray* _start_array;
    73 };
    75 class ParallelCompactData
    76 {
    77 public:
    78   // Sizes are in HeapWords, unless indicated otherwise.
    79   static const size_t Log2ChunkSize;
    80   static const size_t ChunkSize;
    81   static const size_t ChunkSizeBytes;
    83   // Mask for the bits in a size_t to get an offset within a chunk.
    84   static const size_t ChunkSizeOffsetMask;
    85   // Mask for the bits in a pointer to get an offset within a chunk.
    86   static const size_t ChunkAddrOffsetMask;
    87   // Mask for the bits in a pointer to get the address of the start of a chunk.
    88   static const size_t ChunkAddrMask;
    90   static const size_t Log2BlockSize;
    91   static const size_t BlockSize;
    92   static const size_t BlockOffsetMask;
    93   static const size_t BlockMask;
    95   static const size_t BlocksPerChunk;
    97   class ChunkData
    98   {
    99   public:
   100     // Destination address of the chunk.
   101     HeapWord* destination() const { return _destination; }
   103     // The first chunk containing data destined for this chunk.
   104     size_t source_chunk() const { return _source_chunk; }
   106     // The object (if any) starting in this chunk and ending in a different
   107     // chunk that could not be updated during the main (parallel) compaction
   108     // phase.  This is different from _partial_obj_addr, which is an object that
   109     // extends onto a source chunk.  However, the two uses do not overlap in
   110     // time, so the same field is used to save space.
   111     HeapWord* deferred_obj_addr() const { return _partial_obj_addr; }
   113     // The starting address of the partial object extending onto the chunk.
   114     HeapWord* partial_obj_addr() const { return _partial_obj_addr; }
   116     // Size of the partial object extending onto the chunk (words).
   117     size_t partial_obj_size() const { return _partial_obj_size; }
   119     // Size of live data that lies within this chunk due to objects that start
   120     // in this chunk (words).  This does not include the partial object
   121     // extending onto the chunk (if any), or the part of an object that extends
   122     // onto the next chunk (if any).
   123     size_t live_obj_size() const { return _dc_and_los & los_mask; }
   125     // Total live data that lies within the chunk (words).
   126     size_t data_size() const { return partial_obj_size() + live_obj_size(); }
   128     // The destination_count is the number of other chunks to which data from
   129     // this chunk will be copied.  At the end of the summary phase, the valid
   130     // values of destination_count are
   131     //
   132     // 0 - data from the chunk will be compacted completely into itself, or the
   133     //     chunk is empty.  The chunk can be claimed and then filled.
   134     // 1 - data from the chunk will be compacted into 1 other chunk; some
   135     //     data from the chunk may also be compacted into the chunk itself.
   136     // 2 - data from the chunk will be copied to 2 other chunks.
   137     //
   138     // During compaction as chunks are emptied, the destination_count is
   139     // decremented (atomically) and when it reaches 0, it can be claimed and
   140     // then filled.
   141     //
   142     // A chunk is claimed for processing by atomically changing the
   143     // destination_count to the claimed value (dc_claimed).  After a chunk has
   144     // been filled, the destination_count should be set to the completed value
   145     // (dc_completed).
   146     inline uint destination_count() const;
   147     inline uint destination_count_raw() const;
   149     // The location of the java heap data that corresponds to this chunk.
   150     inline HeapWord* data_location() const;
   152     // The highest address referenced by objects in this chunk.
   153     inline HeapWord* highest_ref() const;
   155     // Whether this chunk is available to be claimed, has been claimed, or has
   156     // been completed.
   157     //
   158     // Minor subtlety:  claimed() returns true if the chunk is marked
   159     // completed(), which is desirable since a chunk must be claimed before it
   160     // can be completed.
   161     bool available() const { return _dc_and_los < dc_one; }
   162     bool claimed() const   { return _dc_and_los >= dc_claimed; }
   163     bool completed() const { return _dc_and_los >= dc_completed; }
   165     // These are not atomic.
   166     void set_destination(HeapWord* addr)       { _destination = addr; }
   167     void set_source_chunk(size_t chunk)        { _source_chunk = chunk; }
   168     void set_deferred_obj_addr(HeapWord* addr) { _partial_obj_addr = addr; }
   169     void set_partial_obj_addr(HeapWord* addr)  { _partial_obj_addr = addr; }
   170     void set_partial_obj_size(size_t words)    {
   171       _partial_obj_size = (chunk_sz_t) words;
   172     }
   174     inline void set_destination_count(uint count);
   175     inline void set_live_obj_size(size_t words);
   176     inline void set_data_location(HeapWord* addr);
   177     inline void set_completed();
   178     inline bool claim_unsafe();
   180     // These are atomic.
   181     inline void add_live_obj(size_t words);
   182     inline void set_highest_ref(HeapWord* addr);
   183     inline void decrement_destination_count();
   184     inline bool claim();
   186   private:
   187     // The type used to represent object sizes within a chunk.
   188     typedef uint chunk_sz_t;
   190     // Constants for manipulating the _dc_and_los field, which holds both the
   191     // destination count and live obj size.  The live obj size lives at the
   192     // least significant end so no masking is necessary when adding.
   193     static const chunk_sz_t dc_shift;           // Shift amount.
   194     static const chunk_sz_t dc_mask;            // Mask for destination count.
   195     static const chunk_sz_t dc_one;             // 1, shifted appropriately.
   196     static const chunk_sz_t dc_claimed;         // Chunk has been claimed.
   197     static const chunk_sz_t dc_completed;       // Chunk has been completed.
   198     static const chunk_sz_t los_mask;           // Mask for live obj size.
   200     HeapWord*           _destination;
   201     size_t              _source_chunk;
   202     HeapWord*           _partial_obj_addr;
   203     chunk_sz_t          _partial_obj_size;
   204     chunk_sz_t volatile _dc_and_los;
   205 #ifdef ASSERT
   206     // These enable optimizations that are only partially implemented.  Use
   207     // debug builds to prevent the code fragments from breaking.
   208     HeapWord*           _data_location;
   209     HeapWord*           _highest_ref;
   210 #endif  // #ifdef ASSERT
   212 #ifdef ASSERT
   213    public:
   214     uint            _pushed;    // 0 until chunk is pushed onto a worker's stack
   215    private:
   216 #endif
   217   };
   219   // 'Blocks' allow shorter sections of the bitmap to be searched.  Each Block
   220   // holds an offset, which is the amount of live data in the Chunk to the left
   221   // of the first live object in the Block.  This amount of live data will
   222   // include any object extending into the block. The first block in
   223   // a chunk does not include any partial object extending into the
   224   // the chunk.
   225   //
   226   // The offset also encodes the
   227   // 'parity' of the first 1 bit in the Block:  a positive offset means the
   228   // first 1 bit marks the start of an object, a negative offset means the first
   229   // 1 bit marks the end of an object.
   230   class BlockData
   231   {
   232    public:
   233     typedef short int blk_ofs_t;
   235     blk_ofs_t offset() const { return _offset >= 0 ? _offset : -_offset; }
   236     blk_ofs_t raw_offset() const { return _offset; }
   237     void set_first_is_start_bit(bool v) { _first_is_start_bit = v; }
   239 #if 0
   240     // The need for this method was anticipated but it is
   241     // never actually used.  Do not include it for now.  If
   242     // it is needed, consider the problem of what is passed
   243     // as "v".  To avoid warning errors the method set_start_bit_offset()
   244     // was changed to take a size_t as the parameter and to do the
   245     // check for the possible overflow.  Doing the cast in these
   246     // methods better limits the potential problems because of
   247     // the size of the field to this class.
   248     void set_raw_offset(blk_ofs_t v) { _offset = v; }
   249 #endif
   250     void set_start_bit_offset(size_t val) {
   251       assert(val >= 0, "sanity");
   252       _offset = (blk_ofs_t) val;
   253       assert(val == (size_t) _offset, "Value is too large");
   254       _first_is_start_bit = true;
   255     }
   256     void set_end_bit_offset(size_t val) {
   257       assert(val >= 0, "sanity");
   258       _offset = (blk_ofs_t) val;
   259       assert(val == (size_t) _offset, "Value is too large");
   260       _offset = - _offset;
   261       _first_is_start_bit = false;
   262     }
   263     bool first_is_start_bit() {
   264       assert(_set_phase > 0, "Not initialized");
   265       return _first_is_start_bit;
   266     }
   267     bool first_is_end_bit() {
   268       assert(_set_phase > 0, "Not initialized");
   269       return !_first_is_start_bit;
   270     }
   272    private:
   273     blk_ofs_t _offset;
   274     // This is temporary until the mark_bitmap is separated into
   275     // a start bit array and an end bit array.
   276     bool      _first_is_start_bit;
   277 #ifdef ASSERT
   278     short     _set_phase;
   279     static short _cur_phase;
   280    public:
   281     static void set_cur_phase(short v) { _cur_phase = v; }
   282 #endif
   283   };
   285 public:
   286   ParallelCompactData();
   287   bool initialize(MemRegion covered_region);
   289   size_t chunk_count() const { return _chunk_count; }
   291   // Convert chunk indices to/from ChunkData pointers.
   292   inline ChunkData* chunk(size_t chunk_idx) const;
   293   inline size_t     chunk(const ChunkData* const chunk_ptr) const;
   295   // Returns true if the given address is contained within the chunk
   296   bool chunk_contains(size_t chunk_index, HeapWord* addr);
   298   size_t block_count() const { return _block_count; }
   299   inline BlockData* block(size_t n) const;
   301   // Returns true if the given block is in the given chunk.
   302   static bool chunk_contains_block(size_t chunk_index, size_t block_index);
   304   void add_obj(HeapWord* addr, size_t len);
   305   void add_obj(oop p, size_t len) { add_obj((HeapWord*)p, len); }
   307   // Fill in the chunks covering [beg, end) so that no data moves; i.e., the
   308   // destination of chunk n is simply the start of chunk n.  The argument beg
   309   // must be chunk-aligned; end need not be.
   310   void summarize_dense_prefix(HeapWord* beg, HeapWord* end);
   312   bool summarize(HeapWord* target_beg, HeapWord* target_end,
   313                  HeapWord* source_beg, HeapWord* source_end,
   314                  HeapWord** target_next, HeapWord** source_next = 0);
   316   void clear();
   317   void clear_range(size_t beg_chunk, size_t end_chunk);
   318   void clear_range(HeapWord* beg, HeapWord* end) {
   319     clear_range(addr_to_chunk_idx(beg), addr_to_chunk_idx(end));
   320   }
   322   // Return the number of words between addr and the start of the chunk
   323   // containing addr.
   324   inline size_t     chunk_offset(const HeapWord* addr) const;
   326   // Convert addresses to/from a chunk index or chunk pointer.
   327   inline size_t     addr_to_chunk_idx(const HeapWord* addr) const;
   328   inline ChunkData* addr_to_chunk_ptr(const HeapWord* addr) const;
   329   inline HeapWord*  chunk_to_addr(size_t chunk) const;
   330   inline HeapWord*  chunk_to_addr(size_t chunk, size_t offset) const;
   331   inline HeapWord*  chunk_to_addr(const ChunkData* chunk) const;
   333   inline HeapWord*  chunk_align_down(HeapWord* addr) const;
   334   inline HeapWord*  chunk_align_up(HeapWord* addr) const;
   335   inline bool       is_chunk_aligned(HeapWord* addr) const;
   337   // Analogous to chunk_offset() for blocks.
   338   size_t     block_offset(const HeapWord* addr) const;
   339   size_t     addr_to_block_idx(const HeapWord* addr) const;
   340   size_t     addr_to_block_idx(const oop obj) const {
   341     return addr_to_block_idx((HeapWord*) obj);
   342   }
   343   inline BlockData* addr_to_block_ptr(const HeapWord* addr) const;
   344   inline HeapWord*  block_to_addr(size_t block) const;
   346   // Return the address one past the end of the partial object.
   347   HeapWord* partial_obj_end(size_t chunk_idx) const;
   349   // Return the new location of the object p after the
   350   // the compaction.
   351   HeapWord* calc_new_pointer(HeapWord* addr);
   353   // Same as calc_new_pointer() using blocks.
   354   HeapWord* block_calc_new_pointer(HeapWord* addr);
   356   // Same as calc_new_pointer() using chunks.
   357   HeapWord* chunk_calc_new_pointer(HeapWord* addr);
   359   HeapWord* calc_new_pointer(oop p) {
   360     return calc_new_pointer((HeapWord*) p);
   361   }
   363   // Return the updated address for the given klass
   364   klassOop calc_new_klass(klassOop);
   366   // Given a block returns true if the partial object for the
   367   // corresponding chunk ends in the block.  Returns false, otherwise
   368   // If there is no partial object, returns false.
   369   bool partial_obj_ends_in_block(size_t block_index);
   371   // Returns the block index for the block
   372   static size_t block_idx(BlockData* block);
   374 #ifdef  ASSERT
   375   void verify_clear(const PSVirtualSpace* vspace);
   376   void verify_clear();
   377 #endif  // #ifdef ASSERT
   379 private:
   380   bool initialize_block_data(size_t region_size);
   381   bool initialize_chunk_data(size_t region_size);
   382   PSVirtualSpace* create_vspace(size_t count, size_t element_size);
   384 private:
   385   HeapWord*       _region_start;
   386 #ifdef  ASSERT
   387   HeapWord*       _region_end;
   388 #endif  // #ifdef ASSERT
   390   PSVirtualSpace* _chunk_vspace;
   391   ChunkData*      _chunk_data;
   392   size_t          _chunk_count;
   394   PSVirtualSpace* _block_vspace;
   395   BlockData*      _block_data;
   396   size_t          _block_count;
   397 };
   399 inline uint
   400 ParallelCompactData::ChunkData::destination_count_raw() const
   401 {
   402   return _dc_and_los & dc_mask;
   403 }
   405 inline uint
   406 ParallelCompactData::ChunkData::destination_count() const
   407 {
   408   return destination_count_raw() >> dc_shift;
   409 }
   411 inline void
   412 ParallelCompactData::ChunkData::set_destination_count(uint count)
   413 {
   414   assert(count <= (dc_completed >> dc_shift), "count too large");
   415   const chunk_sz_t live_sz = (chunk_sz_t) live_obj_size();
   416   _dc_and_los = (count << dc_shift) | live_sz;
   417 }
   419 inline void ParallelCompactData::ChunkData::set_live_obj_size(size_t words)
   420 {
   421   assert(words <= los_mask, "would overflow");
   422   _dc_and_los = destination_count_raw() | (chunk_sz_t)words;
   423 }
   425 inline void ParallelCompactData::ChunkData::decrement_destination_count()
   426 {
   427   assert(_dc_and_los < dc_claimed, "already claimed");
   428   assert(_dc_and_los >= dc_one, "count would go negative");
   429   Atomic::add((int)dc_mask, (volatile int*)&_dc_and_los);
   430 }
   432 inline HeapWord* ParallelCompactData::ChunkData::data_location() const
   433 {
   434   DEBUG_ONLY(return _data_location;)
   435   NOT_DEBUG(return NULL;)
   436 }
   438 inline HeapWord* ParallelCompactData::ChunkData::highest_ref() const
   439 {
   440   DEBUG_ONLY(return _highest_ref;)
   441   NOT_DEBUG(return NULL;)
   442 }
   444 inline void ParallelCompactData::ChunkData::set_data_location(HeapWord* addr)
   445 {
   446   DEBUG_ONLY(_data_location = addr;)
   447 }
   449 inline void ParallelCompactData::ChunkData::set_completed()
   450 {
   451   assert(claimed(), "must be claimed first");
   452   _dc_and_los = dc_completed | (chunk_sz_t) live_obj_size();
   453 }
   455 // MT-unsafe claiming of a chunk.  Should only be used during single threaded
   456 // execution.
   457 inline bool ParallelCompactData::ChunkData::claim_unsafe()
   458 {
   459   if (available()) {
   460     _dc_and_los |= dc_claimed;
   461     return true;
   462   }
   463   return false;
   464 }
   466 inline void ParallelCompactData::ChunkData::add_live_obj(size_t words)
   467 {
   468   assert(words <= (size_t)los_mask - live_obj_size(), "overflow");
   469   Atomic::add((int) words, (volatile int*) &_dc_and_los);
   470 }
   472 inline void ParallelCompactData::ChunkData::set_highest_ref(HeapWord* addr)
   473 {
   474 #ifdef ASSERT
   475   HeapWord* tmp = _highest_ref;
   476   while (addr > tmp) {
   477     tmp = (HeapWord*)Atomic::cmpxchg_ptr(addr, &_highest_ref, tmp);
   478   }
   479 #endif  // #ifdef ASSERT
   480 }
   482 inline bool ParallelCompactData::ChunkData::claim()
   483 {
   484   const int los = (int) live_obj_size();
   485   const int old = Atomic::cmpxchg(dc_claimed | los,
   486                                   (volatile int*) &_dc_and_los, los);
   487   return old == los;
   488 }
   490 inline ParallelCompactData::ChunkData*
   491 ParallelCompactData::chunk(size_t chunk_idx) const
   492 {
   493   assert(chunk_idx <= chunk_count(), "bad arg");
   494   return _chunk_data + chunk_idx;
   495 }
   497 inline size_t
   498 ParallelCompactData::chunk(const ChunkData* const chunk_ptr) const
   499 {
   500   assert(chunk_ptr >= _chunk_data, "bad arg");
   501   assert(chunk_ptr <= _chunk_data + chunk_count(), "bad arg");
   502   return pointer_delta(chunk_ptr, _chunk_data, sizeof(ChunkData));
   503 }
   505 inline ParallelCompactData::BlockData*
   506 ParallelCompactData::block(size_t n) const {
   507   assert(n < block_count(), "bad arg");
   508   return _block_data + n;
   509 }
   511 inline size_t
   512 ParallelCompactData::chunk_offset(const HeapWord* addr) const
   513 {
   514   assert(addr >= _region_start, "bad addr");
   515   assert(addr <= _region_end, "bad addr");
   516   return (size_t(addr) & ChunkAddrOffsetMask) >> LogHeapWordSize;
   517 }
   519 inline size_t
   520 ParallelCompactData::addr_to_chunk_idx(const HeapWord* addr) const
   521 {
   522   assert(addr >= _region_start, "bad addr");
   523   assert(addr <= _region_end, "bad addr");
   524   return pointer_delta(addr, _region_start) >> Log2ChunkSize;
   525 }
   527 inline ParallelCompactData::ChunkData*
   528 ParallelCompactData::addr_to_chunk_ptr(const HeapWord* addr) const
   529 {
   530   return chunk(addr_to_chunk_idx(addr));
   531 }
   533 inline HeapWord*
   534 ParallelCompactData::chunk_to_addr(size_t chunk) const
   535 {
   536   assert(chunk <= _chunk_count, "chunk out of range");
   537   return _region_start + (chunk << Log2ChunkSize);
   538 }
   540 inline HeapWord*
   541 ParallelCompactData::chunk_to_addr(const ChunkData* chunk) const
   542 {
   543   return chunk_to_addr(pointer_delta(chunk, _chunk_data, sizeof(ChunkData)));
   544 }
   546 inline HeapWord*
   547 ParallelCompactData::chunk_to_addr(size_t chunk, size_t offset) const
   548 {
   549   assert(chunk <= _chunk_count, "chunk out of range");
   550   assert(offset < ChunkSize, "offset too big");  // This may be too strict.
   551   return chunk_to_addr(chunk) + offset;
   552 }
   554 inline HeapWord*
   555 ParallelCompactData::chunk_align_down(HeapWord* addr) const
   556 {
   557   assert(addr >= _region_start, "bad addr");
   558   assert(addr < _region_end + ChunkSize, "bad addr");
   559   return (HeapWord*)(size_t(addr) & ChunkAddrMask);
   560 }
   562 inline HeapWord*
   563 ParallelCompactData::chunk_align_up(HeapWord* addr) const
   564 {
   565   assert(addr >= _region_start, "bad addr");
   566   assert(addr <= _region_end, "bad addr");
   567   return chunk_align_down(addr + ChunkSizeOffsetMask);
   568 }
   570 inline bool
   571 ParallelCompactData::is_chunk_aligned(HeapWord* addr) const
   572 {
   573   return chunk_offset(addr) == 0;
   574 }
   576 inline size_t
   577 ParallelCompactData::block_offset(const HeapWord* addr) const
   578 {
   579   assert(addr >= _region_start, "bad addr");
   580   assert(addr <= _region_end, "bad addr");
   581   return pointer_delta(addr, _region_start) & BlockOffsetMask;
   582 }
   584 inline size_t
   585 ParallelCompactData::addr_to_block_idx(const HeapWord* addr) const
   586 {
   587   assert(addr >= _region_start, "bad addr");
   588   assert(addr <= _region_end, "bad addr");
   589   return pointer_delta(addr, _region_start) >> Log2BlockSize;
   590 }
   592 inline ParallelCompactData::BlockData*
   593 ParallelCompactData::addr_to_block_ptr(const HeapWord* addr) const
   594 {
   595   return block(addr_to_block_idx(addr));
   596 }
   598 inline HeapWord*
   599 ParallelCompactData::block_to_addr(size_t block) const
   600 {
   601   assert(block < _block_count, "block out of range");
   602   return _region_start + (block << Log2BlockSize);
   603 }
   605 // Abstract closure for use with ParMarkBitMap::iterate(), which will invoke the
   606 // do_addr() method.
   607 //
   608 // The closure is initialized with the number of heap words to process
   609 // (words_remaining()), and becomes 'full' when it reaches 0.  The do_addr()
   610 // methods in subclasses should update the total as words are processed.  Since
   611 // only one subclass actually uses this mechanism to terminate iteration, the
   612 // default initial value is > 0.  The implementation is here and not in the
   613 // single subclass that uses it to avoid making is_full() virtual, and thus
   614 // adding a virtual call per live object.
   616 class ParMarkBitMapClosure: public StackObj {
   617  public:
   618   typedef ParMarkBitMap::idx_t idx_t;
   619   typedef ParMarkBitMap::IterationStatus IterationStatus;
   621  public:
   622   inline ParMarkBitMapClosure(ParMarkBitMap* mbm, ParCompactionManager* cm,
   623                               size_t words = max_uintx);
   625   inline ParCompactionManager* compaction_manager() const;
   626   inline ParMarkBitMap*        bitmap() const;
   627   inline size_t                words_remaining() const;
   628   inline bool                  is_full() const;
   629   inline HeapWord*             source() const;
   631   inline void                  set_source(HeapWord* addr);
   633   virtual IterationStatus do_addr(HeapWord* addr, size_t words) = 0;
   635  protected:
   636   inline void decrement_words_remaining(size_t words);
   638  private:
   639   ParMarkBitMap* const        _bitmap;
   640   ParCompactionManager* const _compaction_manager;
   641   DEBUG_ONLY(const size_t     _initial_words_remaining;) // Useful in debugger.
   642   size_t                      _words_remaining; // Words left to copy.
   644  protected:
   645   HeapWord*                   _source;          // Next addr that would be read.
   646 };
   648 inline
   649 ParMarkBitMapClosure::ParMarkBitMapClosure(ParMarkBitMap* bitmap,
   650                                            ParCompactionManager* cm,
   651                                            size_t words):
   652   _bitmap(bitmap), _compaction_manager(cm)
   653 #ifdef  ASSERT
   654   , _initial_words_remaining(words)
   655 #endif
   656 {
   657   _words_remaining = words;
   658   _source = NULL;
   659 }
   661 inline ParCompactionManager* ParMarkBitMapClosure::compaction_manager() const {
   662   return _compaction_manager;
   663 }
   665 inline ParMarkBitMap* ParMarkBitMapClosure::bitmap() const {
   666   return _bitmap;
   667 }
   669 inline size_t ParMarkBitMapClosure::words_remaining() const {
   670   return _words_remaining;
   671 }
   673 inline bool ParMarkBitMapClosure::is_full() const {
   674   return words_remaining() == 0;
   675 }
   677 inline HeapWord* ParMarkBitMapClosure::source() const {
   678   return _source;
   679 }
   681 inline void ParMarkBitMapClosure::set_source(HeapWord* addr) {
   682   _source = addr;
   683 }
   685 inline void ParMarkBitMapClosure::decrement_words_remaining(size_t words) {
   686   assert(_words_remaining >= words, "processed too many words");
   687   _words_remaining -= words;
   688 }
   690 // Closure for updating the block data during the summary phase.
   691 class BitBlockUpdateClosure: public ParMarkBitMapClosure {
   692   // ParallelCompactData::BlockData::blk_ofs_t _live_data_left;
   693   size_t    _live_data_left;
   694   size_t    _cur_block;
   695   HeapWord* _chunk_start;
   696   HeapWord* _chunk_end;
   697   size_t    _chunk_index;
   699  public:
   700   BitBlockUpdateClosure(ParMarkBitMap* mbm,
   701                         ParCompactionManager* cm,
   702                         size_t chunk_index);
   704   size_t cur_block() { return _cur_block; }
   705   size_t chunk_index() { return _chunk_index; }
   706   size_t live_data_left() { return _live_data_left; }
   707   // Returns true the first bit in the current block (cur_block) is
   708   // a start bit.
   709   // Returns true if the current block is within the chunk for the closure;
   710   bool chunk_contains_cur_block();
   712   // Set the chunk index and related chunk values for
   713   // a new chunk.
   714   void reset_chunk(size_t chunk_index);
   716   virtual IterationStatus do_addr(HeapWord* addr, size_t words);
   717 };
   719 // The UseParallelOldGC collector is a stop-the-world garbage
   720 // collector that does parts of the collection using parallel threads.
   721 // The collection includes the tenured generation and the young
   722 // generation.  The permanent generation is collected at the same
   723 // time as the other two generations but the permanent generation
   724 // is collect by a single GC thread.  The permanent generation is
   725 // collected serially because of the requirement that during the
   726 // processing of a klass AAA, any objects reference by AAA must
   727 // already have been processed.  This requirement is enforced by
   728 // a left (lower address) to right (higher address) sliding compaction.
   729 //
   730 // There are four phases of the collection.
   731 //
   732 //      - marking phase
   733 //      - summary phase
   734 //      - compacting phase
   735 //      - clean up phase
   736 //
   737 // Roughly speaking these phases correspond, respectively, to
   738 //      - mark all the live objects
   739 //      - calculate the destination of each object at the end of the collection
   740 //      - move the objects to their destination
   741 //      - update some references and reinitialize some variables
   742 //
   743 // These three phases are invoked in PSParallelCompact::invoke_no_policy().
   744 // The marking phase is implemented in PSParallelCompact::marking_phase()
   745 // and does a complete marking of the heap.
   746 // The summary phase is implemented in PSParallelCompact::summary_phase().
   747 // The move and update phase is implemented in PSParallelCompact::compact().
   748 //
   749 // A space that is being collected is divided into chunks and with
   750 // each chunk is associated an object of type ParallelCompactData.
   751 // Each chunk is of a fixed size and typically will contain more than
   752 // 1 object and may have parts of objects at the front and back of the
   753 // chunk.
   754 //
   755 // chunk            -----+---------------------+----------
   756 // objects covered   [ AAA  )[ BBB )[ CCC   )[ DDD     )
   757 //
   758 // The marking phase does a complete marking of all live objects in the
   759 // heap.  The marking also compiles the size of the data for
   760 // all live objects covered by the chunk.  This size includes the
   761 // part of any live object spanning onto the chunk (part of AAA
   762 // if it is live) from the front, all live objects contained in the chunk
   763 // (BBB and/or CCC if they are live), and the part of any live objects
   764 // covered by the chunk that extends off the chunk (part of DDD if it is
   765 // live).  The marking phase uses multiple GC threads and marking is
   766 // done in a bit array of type ParMarkBitMap.  The marking of the
   767 // bit map is done atomically as is the accumulation of the size of the
   768 // live objects covered by a chunk.
   769 //
   770 // The summary phase calculates the total live data to the left of
   771 // each chunk XXX.  Based on that total and the bottom of the space,
   772 // it can calculate the starting location of the live data in XXX.
   773 // The summary phase calculates for each chunk XXX quantites such as
   774 //
   775 //      - the amount of live data at the beginning of a chunk from an object
   776 //      entering the chunk.
   777 //      - the location of the first live data on the chunk
   778 //      - a count of the number of chunks receiving live data from XXX.
   779 //
   780 // See ParallelCompactData for precise details.  The summary phase also
   781 // calculates the dense prefix for the compaction.  The dense prefix
   782 // is a portion at the beginning of the space that is not moved.  The
   783 // objects in the dense prefix do need to have their object references
   784 // updated.  See method summarize_dense_prefix().
   785 //
   786 // The summary phase is done using 1 GC thread.
   787 //
   788 // The compaction phase moves objects to their new location and updates
   789 // all references in the object.
   790 //
   791 // A current exception is that objects that cross a chunk boundary
   792 // are moved but do not have their references updated.  References are
   793 // not updated because it cannot easily be determined if the klass
   794 // pointer KKK for the object AAA has been updated.  KKK likely resides
   795 // in a chunk to the left of the chunk containing AAA.  These AAA's
   796 // have there references updated at the end in a clean up phase.
   797 // See the method PSParallelCompact::update_deferred_objects().  An
   798 // alternate strategy is being investigated for this deferral of updating.
   799 //
   800 // Compaction is done on a chunk basis.  A chunk that is ready to be
   801 // filled is put on a ready list and GC threads take chunk off the list
   802 // and fill them.  A chunk is ready to be filled if it
   803 // empty of live objects.  Such a chunk may have been initially
   804 // empty (only contained
   805 // dead objects) or may have had all its live objects copied out already.
   806 // A chunk that compacts into itself is also ready for filling.  The
   807 // ready list is initially filled with empty chunks and chunks compacting
   808 // into themselves.  There is always at least 1 chunk that can be put on
   809 // the ready list.  The chunks are atomically added and removed from
   810 // the ready list.
   811 //
   812 class PSParallelCompact : AllStatic {
   813  public:
   814   // Convenient access to type names.
   815   typedef ParMarkBitMap::idx_t idx_t;
   816   typedef ParallelCompactData::ChunkData ChunkData;
   817   typedef ParallelCompactData::BlockData BlockData;
   819   typedef enum {
   820     perm_space_id, old_space_id, eden_space_id,
   821     from_space_id, to_space_id, last_space_id
   822   } SpaceId;
   824  public:
   825   // Inline closure decls
   826   //
   827   class IsAliveClosure: public BoolObjectClosure {
   828    public:
   829     virtual void do_object(oop p);
   830     virtual bool do_object_b(oop p);
   831   };
   833   class KeepAliveClosure: public OopClosure {
   834    private:
   835     ParCompactionManager* _compaction_manager;
   836    protected:
   837     template <class T> inline void do_oop_work(T* p);
   838    public:
   839     KeepAliveClosure(ParCompactionManager* cm) : _compaction_manager(cm) { }
   840     virtual void do_oop(oop* p);
   841     virtual void do_oop(narrowOop* p);
   842   };
   844   // Current unused
   845   class FollowRootClosure: public OopsInGenClosure {
   846    private:
   847     ParCompactionManager* _compaction_manager;
   848    public:
   849     FollowRootClosure(ParCompactionManager* cm) : _compaction_manager(cm) { }
   850     virtual void do_oop(oop* p);
   851     virtual void do_oop(narrowOop* p);
   852     virtual const bool do_nmethods() const { return true; }
   853   };
   855   class FollowStackClosure: public VoidClosure {
   856    private:
   857     ParCompactionManager* _compaction_manager;
   858    public:
   859     FollowStackClosure(ParCompactionManager* cm) : _compaction_manager(cm) { }
   860     virtual void do_void();
   861   };
   863   class AdjustPointerClosure: public OopsInGenClosure {
   864    private:
   865     bool _is_root;
   866    public:
   867     AdjustPointerClosure(bool is_root) : _is_root(is_root) { }
   868     virtual void do_oop(oop* p);
   869     virtual void do_oop(narrowOop* p);
   870   };
   872   // Closure for verifying update of pointers.  Does not
   873   // have any side effects.
   874   class VerifyUpdateClosure: public ParMarkBitMapClosure {
   875     const MutableSpace* _space; // Is this ever used?
   877    public:
   878     VerifyUpdateClosure(ParCompactionManager* cm, const MutableSpace* sp) :
   879       ParMarkBitMapClosure(PSParallelCompact::mark_bitmap(), cm), _space(sp)
   880     { }
   882     virtual IterationStatus do_addr(HeapWord* addr, size_t words);
   884     const MutableSpace* space() { return _space; }
   885   };
   887   // Closure for updating objects altered for debug checking
   888   class ResetObjectsClosure: public ParMarkBitMapClosure {
   889    public:
   890     ResetObjectsClosure(ParCompactionManager* cm):
   891       ParMarkBitMapClosure(PSParallelCompact::mark_bitmap(), cm)
   892     { }
   894     virtual IterationStatus do_addr(HeapWord* addr, size_t words);
   895   };
   897   friend class KeepAliveClosure;
   898   friend class FollowStackClosure;
   899   friend class AdjustPointerClosure;
   900   friend class FollowRootClosure;
   901   friend class instanceKlassKlass;
   902   friend class RefProcTaskProxy;
   904  private:
   905   static elapsedTimer         _accumulated_time;
   906   static unsigned int         _total_invocations;
   907   static unsigned int         _maximum_compaction_gc_num;
   908   static jlong                _time_of_last_gc;   // ms
   909   static CollectorCounters*   _counters;
   910   static ParMarkBitMap        _mark_bitmap;
   911   static ParallelCompactData  _summary_data;
   912   static IsAliveClosure       _is_alive_closure;
   913   static SpaceInfo            _space_info[last_space_id];
   914   static bool                 _print_phases;
   915   static AdjustPointerClosure _adjust_root_pointer_closure;
   916   static AdjustPointerClosure _adjust_pointer_closure;
   918   // Reference processing (used in ...follow_contents)
   919   static ReferenceProcessor*  _ref_processor;
   921   // Updated location of intArrayKlassObj.
   922   static klassOop _updated_int_array_klass_obj;
   924   // Values computed at initialization and used by dead_wood_limiter().
   925   static double _dwl_mean;
   926   static double _dwl_std_dev;
   927   static double _dwl_first_term;
   928   static double _dwl_adjustment;
   929 #ifdef  ASSERT
   930   static bool   _dwl_initialized;
   931 #endif  // #ifdef ASSERT
   933  private:
   934   // Closure accessors
   935   static OopClosure* adjust_pointer_closure()      { return (OopClosure*)&_adjust_pointer_closure; }
   936   static OopClosure* adjust_root_pointer_closure() { return (OopClosure*)&_adjust_root_pointer_closure; }
   937   static BoolObjectClosure* is_alive_closure()     { return (BoolObjectClosure*)&_is_alive_closure; }
   939   static void initialize_space_info();
   941   // Return true if details about individual phases should be printed.
   942   static inline bool print_phases();
   944   // Clear the marking bitmap and summary data that cover the specified space.
   945   static void clear_data_covering_space(SpaceId id);
   947   static void pre_compact(PreGCValues* pre_gc_values);
   948   static void post_compact();
   950   // Mark live objects
   951   static void marking_phase(ParCompactionManager* cm,
   952                             bool maximum_heap_compaction);
   953   static void follow_stack(ParCompactionManager* cm);
   954   static void follow_weak_klass_links(ParCompactionManager* cm);
   956   template <class T> static inline void adjust_pointer(T* p, bool is_root);
   957   static void adjust_root_pointer(oop* p) { adjust_pointer(p, true); }
   959   template <class T>
   960   static inline void follow_root(ParCompactionManager* cm, T* p);
   962   // Compute the dense prefix for the designated space.  This is an experimental
   963   // implementation currently not used in production.
   964   static HeapWord* compute_dense_prefix_via_density(const SpaceId id,
   965                                                     bool maximum_compaction);
   967   // Methods used to compute the dense prefix.
   969   // Compute the value of the normal distribution at x = density.  The mean and
   970   // standard deviation are values saved by initialize_dead_wood_limiter().
   971   static inline double normal_distribution(double density);
   973   // Initialize the static vars used by dead_wood_limiter().
   974   static void initialize_dead_wood_limiter();
   976   // Return the percentage of space that can be treated as "dead wood" (i.e.,
   977   // not reclaimed).
   978   static double dead_wood_limiter(double density, size_t min_percent);
   980   // Find the first (left-most) chunk in the range [beg, end) that has at least
   981   // dead_words of dead space to the left.  The argument beg must be the first
   982   // chunk in the space that is not completely live.
   983   static ChunkData* dead_wood_limit_chunk(const ChunkData* beg,
   984                                           const ChunkData* end,
   985                                           size_t dead_words);
   987   // Return a pointer to the first chunk in the range [beg, end) that is not
   988   // completely full.
   989   static ChunkData* first_dead_space_chunk(const ChunkData* beg,
   990                                            const ChunkData* end);
   992   // Return a value indicating the benefit or 'yield' if the compacted region
   993   // were to start (or equivalently if the dense prefix were to end) at the
   994   // candidate chunk.  Higher values are better.
   995   //
   996   // The value is based on the amount of space reclaimed vs. the costs of (a)
   997   // updating references in the dense prefix plus (b) copying objects and
   998   // updating references in the compacted region.
   999   static inline double reclaimed_ratio(const ChunkData* const candidate,
  1000                                        HeapWord* const bottom,
  1001                                        HeapWord* const top,
  1002                                        HeapWord* const new_top);
  1004   // Compute the dense prefix for the designated space.
  1005   static HeapWord* compute_dense_prefix(const SpaceId id,
  1006                                         bool maximum_compaction);
  1008   // Return true if dead space crosses onto the specified Chunk; bit must be the
  1009   // bit index corresponding to the first word of the Chunk.
  1010   static inline bool dead_space_crosses_boundary(const ChunkData* chunk,
  1011                                                  idx_t bit);
  1013   // Summary phase utility routine to fill dead space (if any) at the dense
  1014   // prefix boundary.  Should only be called if the the dense prefix is
  1015   // non-empty.
  1016   static void fill_dense_prefix_end(SpaceId id);
  1018   static void summarize_spaces_quick();
  1019   static void summarize_space(SpaceId id, bool maximum_compaction);
  1020   static void summary_phase(ParCompactionManager* cm, bool maximum_compaction);
  1022   static bool block_first_offset(size_t block_index, idx_t* block_offset_ptr);
  1024   // Fill in the BlockData
  1025   static void summarize_blocks(ParCompactionManager* cm,
  1026                                SpaceId first_compaction_space_id);
  1028   // The space that is compacted after space_id.
  1029   static SpaceId next_compaction_space_id(SpaceId space_id);
  1031   // Adjust addresses in roots.  Does not adjust addresses in heap.
  1032   static void adjust_roots();
  1034   // Serial code executed in preparation for the compaction phase.
  1035   static void compact_prologue();
  1037   // Move objects to new locations.
  1038   static void compact_perm(ParCompactionManager* cm);
  1039   static void compact();
  1041   // Add available chunks to the stack and draining tasks to the task queue.
  1042   static void enqueue_chunk_draining_tasks(GCTaskQueue* q,
  1043                                            uint parallel_gc_threads);
  1045   // Add dense prefix update tasks to the task queue.
  1046   static void enqueue_dense_prefix_tasks(GCTaskQueue* q,
  1047                                          uint parallel_gc_threads);
  1049   // Add chunk stealing tasks to the task queue.
  1050   static void enqueue_chunk_stealing_tasks(
  1051                                        GCTaskQueue* q,
  1052                                        ParallelTaskTerminator* terminator_ptr,
  1053                                        uint parallel_gc_threads);
  1055   // For debugging only - compacts the old gen serially
  1056   static void compact_serial(ParCompactionManager* cm);
  1058   // If objects are left in eden after a collection, try to move the boundary
  1059   // and absorb them into the old gen.  Returns true if eden was emptied.
  1060   static bool absorb_live_data_from_eden(PSAdaptiveSizePolicy* size_policy,
  1061                                          PSYoungGen* young_gen,
  1062                                          PSOldGen* old_gen);
  1064   // Reset time since last full gc
  1065   static void reset_millis_since_last_gc();
  1067  protected:
  1068 #ifdef VALIDATE_MARK_SWEEP
  1069   static GrowableArray<void*>*           _root_refs_stack;
  1070   static GrowableArray<oop> *            _live_oops;
  1071   static GrowableArray<oop> *            _live_oops_moved_to;
  1072   static GrowableArray<size_t>*          _live_oops_size;
  1073   static size_t                          _live_oops_index;
  1074   static size_t                          _live_oops_index_at_perm;
  1075   static GrowableArray<void*>*           _other_refs_stack;
  1076   static GrowableArray<void*>*           _adjusted_pointers;
  1077   static bool                            _pointer_tracking;
  1078   static bool                            _root_tracking;
  1080   // The following arrays are saved since the time of the last GC and
  1081   // assist in tracking down problems where someone has done an errant
  1082   // store into the heap, usually to an oop that wasn't properly
  1083   // handleized across a GC. If we crash or otherwise fail before the
  1084   // next GC, we can query these arrays to find out the object we had
  1085   // intended to do the store to (assuming it is still alive) and the
  1086   // offset within that object. Covered under RecordMarkSweepCompaction.
  1087   static GrowableArray<HeapWord*> *      _cur_gc_live_oops;
  1088   static GrowableArray<HeapWord*> *      _cur_gc_live_oops_moved_to;
  1089   static GrowableArray<size_t>*          _cur_gc_live_oops_size;
  1090   static GrowableArray<HeapWord*> *      _last_gc_live_oops;
  1091   static GrowableArray<HeapWord*> *      _last_gc_live_oops_moved_to;
  1092   static GrowableArray<size_t>*          _last_gc_live_oops_size;
  1093 #endif
  1095  public:
  1096   class MarkAndPushClosure: public OopClosure {
  1097    private:
  1098     ParCompactionManager* _compaction_manager;
  1099    public:
  1100     MarkAndPushClosure(ParCompactionManager* cm) : _compaction_manager(cm) { }
  1101     virtual void do_oop(oop* p);
  1102     virtual void do_oop(narrowOop* p);
  1103     virtual const bool do_nmethods() const { return true; }
  1104   };
  1106   PSParallelCompact();
  1108   // Convenient accessor for Universe::heap().
  1109   static ParallelScavengeHeap* gc_heap() {
  1110     return (ParallelScavengeHeap*)Universe::heap();
  1113   static void invoke(bool maximum_heap_compaction);
  1114   static void invoke_no_policy(bool maximum_heap_compaction);
  1116   static void post_initialize();
  1117   // Perform initialization for PSParallelCompact that requires
  1118   // allocations.  This should be called during the VM initialization
  1119   // at a pointer where it would be appropriate to return a JNI_ENOMEM
  1120   // in the event of a failure.
  1121   static bool initialize();
  1123   // Public accessors
  1124   static elapsedTimer* accumulated_time() { return &_accumulated_time; }
  1125   static unsigned int total_invocations() { return _total_invocations; }
  1126   static CollectorCounters* counters()    { return _counters; }
  1128   // Used to add tasks
  1129   static GCTaskManager* const gc_task_manager();
  1130   static klassOop updated_int_array_klass_obj() {
  1131     return _updated_int_array_klass_obj;
  1134   // Marking support
  1135   static inline bool mark_obj(oop obj);
  1136   // Check mark and maybe push on marking stack
  1137   template <class T> static inline void mark_and_push(ParCompactionManager* cm,
  1138                                                       T* p);
  1140   // Compaction support.
  1141   // Return true if p is in the range [beg_addr, end_addr).
  1142   static inline bool is_in(HeapWord* p, HeapWord* beg_addr, HeapWord* end_addr);
  1143   static inline bool is_in(oop* p, HeapWord* beg_addr, HeapWord* end_addr);
  1145   // Convenience wrappers for per-space data kept in _space_info.
  1146   static inline MutableSpace*     space(SpaceId space_id);
  1147   static inline HeapWord*         new_top(SpaceId space_id);
  1148   static inline HeapWord*         dense_prefix(SpaceId space_id);
  1149   static inline ObjectStartArray* start_array(SpaceId space_id);
  1151   // Return true if the klass should be updated.
  1152   static inline bool should_update_klass(klassOop k);
  1154   // Move and update the live objects in the specified space.
  1155   static void move_and_update(ParCompactionManager* cm, SpaceId space_id);
  1157   // Process the end of the given chunk range in the dense prefix.
  1158   // This includes saving any object not updated.
  1159   static void dense_prefix_chunks_epilogue(ParCompactionManager* cm,
  1160                                            size_t chunk_start_index,
  1161                                            size_t chunk_end_index,
  1162                                            idx_t exiting_object_offset,
  1163                                            idx_t chunk_offset_start,
  1164                                            idx_t chunk_offset_end);
  1166   // Update a chunk in the dense prefix.  For each live object
  1167   // in the chunk, update it's interior references.  For each
  1168   // dead object, fill it with deadwood. Dead space at the end
  1169   // of a chunk range will be filled to the start of the next
  1170   // live object regardless of the chunk_index_end.  None of the
  1171   // objects in the dense prefix move and dead space is dead
  1172   // (holds only dead objects that don't need any processing), so
  1173   // dead space can be filled in any order.
  1174   static void update_and_deadwood_in_dense_prefix(ParCompactionManager* cm,
  1175                                                   SpaceId space_id,
  1176                                                   size_t chunk_index_start,
  1177                                                   size_t chunk_index_end);
  1179   // Return the address of the count + 1st live word in the range [beg, end).
  1180   static HeapWord* skip_live_words(HeapWord* beg, HeapWord* end, size_t count);
  1182   // Return the address of the word to be copied to dest_addr, which must be
  1183   // aligned to a chunk boundary.
  1184   static HeapWord* first_src_addr(HeapWord* const dest_addr,
  1185                                   size_t src_chunk_idx);
  1187   // Determine the next source chunk, set closure.source() to the start of the
  1188   // new chunk return the chunk index.  Parameter end_addr is the address one
  1189   // beyond the end of source range just processed.  If necessary, switch to a
  1190   // new source space and set src_space_id (in-out parameter) and src_space_top
  1191   // (out parameter) accordingly.
  1192   static size_t next_src_chunk(MoveAndUpdateClosure& closure,
  1193                                SpaceId& src_space_id,
  1194                                HeapWord*& src_space_top,
  1195                                HeapWord* end_addr);
  1197   // Decrement the destination count for each non-empty source chunk in the
  1198   // range [beg_chunk, chunk(chunk_align_up(end_addr))).
  1199   static void decrement_destination_counts(ParCompactionManager* cm,
  1200                                            size_t beg_chunk,
  1201                                            HeapWord* end_addr);
  1203   // Fill a chunk, copying objects from one or more source chunks.
  1204   static void fill_chunk(ParCompactionManager* cm, size_t chunk_idx);
  1205   static void fill_and_update_chunk(ParCompactionManager* cm, size_t chunk) {
  1206     fill_chunk(cm, chunk);
  1209   // Update the deferred objects in the space.
  1210   static void update_deferred_objects(ParCompactionManager* cm, SpaceId id);
  1212   // Mark pointer and follow contents.
  1213   template <class T>
  1214   static inline void mark_and_follow(ParCompactionManager* cm, T* p);
  1216   static ParMarkBitMap* mark_bitmap() { return &_mark_bitmap; }
  1217   static ParallelCompactData& summary_data() { return _summary_data; }
  1219   static inline void adjust_pointer(oop* p)       { adjust_pointer(p, false); }
  1220   static inline void adjust_pointer(narrowOop* p) { adjust_pointer(p, false); }
  1222   template <class T>
  1223   static inline void adjust_pointer(T* p,
  1224                                     HeapWord* beg_addr,
  1225                                     HeapWord* end_addr);
  1227   // Reference Processing
  1228   static ReferenceProcessor* const ref_processor() { return _ref_processor; }
  1230   // Return the SpaceId for the given address.
  1231   static SpaceId space_id(HeapWord* addr);
  1233   // Time since last full gc (in milliseconds).
  1234   static jlong millis_since_last_gc();
  1236 #ifdef VALIDATE_MARK_SWEEP
  1237   static void track_adjusted_pointer(void* p, bool isroot);
  1238   static void check_adjust_pointer(void* p);
  1239   static void track_interior_pointers(oop obj);
  1240   static void check_interior_pointers();
  1242   static void reset_live_oop_tracking(bool at_perm);
  1243   static void register_live_oop(oop p, size_t size);
  1244   static void validate_live_oop(oop p, size_t size);
  1245   static void live_oop_moved_to(HeapWord* q, size_t size, HeapWord* compaction_top);
  1246   static void compaction_complete();
  1248   // Querying operation of RecordMarkSweepCompaction results.
  1249   // Finds and prints the current base oop and offset for a word
  1250   // within an oop that was live during the last GC. Helpful for
  1251   // tracking down heap stomps.
  1252   static void print_new_location_of_heap_address(HeapWord* q);
  1253 #endif  // #ifdef VALIDATE_MARK_SWEEP
  1255   // Call backs for class unloading
  1256   // Update subklass/sibling/implementor links at end of marking.
  1257   static void revisit_weak_klass_link(ParCompactionManager* cm, Klass* k);
  1259 #ifndef PRODUCT
  1260   // Debugging support.
  1261   static const char* space_names[last_space_id];
  1262   static void print_chunk_ranges();
  1263   static void print_dense_prefix_stats(const char* const algorithm,
  1264                                        const SpaceId id,
  1265                                        const bool maximum_compaction,
  1266                                        HeapWord* const addr);
  1267 #endif  // #ifndef PRODUCT
  1269 #ifdef  ASSERT
  1270   // Verify that all the chunks have been emptied.
  1271   static void verify_complete(SpaceId space_id);
  1272 #endif  // #ifdef ASSERT
  1273 };
  1275 inline bool PSParallelCompact::mark_obj(oop obj) {
  1276   const int obj_size = obj->size();
  1277   if (mark_bitmap()->mark_obj(obj, obj_size)) {
  1278     _summary_data.add_obj(obj, obj_size);
  1279     return true;
  1280   } else {
  1281     return false;
  1285 template <class T>
  1286 inline void PSParallelCompact::follow_root(ParCompactionManager* cm, T* p) {
  1287   assert(!Universe::heap()->is_in_reserved(p),
  1288          "roots shouldn't be things within the heap");
  1289 #ifdef VALIDATE_MARK_SWEEP
  1290   if (ValidateMarkSweep) {
  1291     guarantee(!_root_refs_stack->contains(p), "should only be in here once");
  1292     _root_refs_stack->push(p);
  1294 #endif
  1295   T heap_oop = oopDesc::load_heap_oop(p);
  1296   if (!oopDesc::is_null(heap_oop)) {
  1297     oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
  1298     if (mark_bitmap()->is_unmarked(obj)) {
  1299       if (mark_obj(obj)) {
  1300         obj->follow_contents(cm);
  1304   follow_stack(cm);
  1307 template <class T>
  1308 inline void PSParallelCompact::mark_and_follow(ParCompactionManager* cm,
  1309                                                T* p) {
  1310   T heap_oop = oopDesc::load_heap_oop(p);
  1311   if (!oopDesc::is_null(heap_oop)) {
  1312     oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
  1313     if (mark_bitmap()->is_unmarked(obj)) {
  1314       if (mark_obj(obj)) {
  1315         obj->follow_contents(cm);
  1321 template <class T>
  1322 inline void PSParallelCompact::mark_and_push(ParCompactionManager* cm, T* p) {
  1323   T heap_oop = oopDesc::load_heap_oop(p);
  1324   if (!oopDesc::is_null(heap_oop)) {
  1325     oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
  1326     if (mark_bitmap()->is_unmarked(obj)) {
  1327       if (mark_obj(obj)) {
  1328         // This thread marked the object and owns the subsequent processing of it.
  1329         cm->save_for_scanning(obj);
  1335 template <class T>
  1336 inline void PSParallelCompact::adjust_pointer(T* p, bool isroot) {
  1337   T heap_oop = oopDesc::load_heap_oop(p);
  1338   if (!oopDesc::is_null(heap_oop)) {
  1339     oop obj     = oopDesc::decode_heap_oop_not_null(heap_oop);
  1340     oop new_obj = (oop)summary_data().calc_new_pointer(obj);
  1341     assert(new_obj != NULL ||                     // is forwarding ptr?
  1342            obj->is_shared(),                      // never forwarded?
  1343            "should be forwarded");
  1344     // Just always do the update unconditionally?
  1345     if (new_obj != NULL) {
  1346       assert(Universe::heap()->is_in_reserved(new_obj),
  1347              "should be in object space");
  1348       oopDesc::encode_store_heap_oop_not_null(p, new_obj);
  1351   VALIDATE_MARK_SWEEP_ONLY(track_adjusted_pointer(p, isroot));
  1354 template <class T>
  1355 inline void PSParallelCompact::KeepAliveClosure::do_oop_work(T* p) {
  1356 #ifdef VALIDATE_MARK_SWEEP
  1357   if (ValidateMarkSweep) {
  1358     if (!Universe::heap()->is_in_reserved(p)) {
  1359       _root_refs_stack->push(p);
  1360     } else {
  1361       _other_refs_stack->push(p);
  1364 #endif
  1365   mark_and_push(_compaction_manager, p);
  1368 inline bool PSParallelCompact::print_phases() {
  1369   return _print_phases;
  1372 inline double PSParallelCompact::normal_distribution(double density) {
  1373   assert(_dwl_initialized, "uninitialized");
  1374   const double squared_term = (density - _dwl_mean) / _dwl_std_dev;
  1375   return _dwl_first_term * exp(-0.5 * squared_term * squared_term);
  1378 inline bool
  1379 PSParallelCompact::dead_space_crosses_boundary(const ChunkData* chunk,
  1380                                                idx_t bit)
  1382   assert(bit > 0, "cannot call this for the first bit/chunk");
  1383   assert(_summary_data.chunk_to_addr(chunk) == _mark_bitmap.bit_to_addr(bit),
  1384          "sanity check");
  1386   // Dead space crosses the boundary if (1) a partial object does not extend
  1387   // onto the chunk, (2) an object does not start at the beginning of the chunk,
  1388   // and (3) an object does not end at the end of the prior chunk.
  1389   return chunk->partial_obj_size() == 0 &&
  1390     !_mark_bitmap.is_obj_beg(bit) &&
  1391     !_mark_bitmap.is_obj_end(bit - 1);
  1394 inline bool
  1395 PSParallelCompact::is_in(HeapWord* p, HeapWord* beg_addr, HeapWord* end_addr) {
  1396   return p >= beg_addr && p < end_addr;
  1399 inline bool
  1400 PSParallelCompact::is_in(oop* p, HeapWord* beg_addr, HeapWord* end_addr) {
  1401   return is_in((HeapWord*)p, beg_addr, end_addr);
  1404 inline MutableSpace* PSParallelCompact::space(SpaceId id) {
  1405   assert(id < last_space_id, "id out of range");
  1406   return _space_info[id].space();
  1409 inline HeapWord* PSParallelCompact::new_top(SpaceId id) {
  1410   assert(id < last_space_id, "id out of range");
  1411   return _space_info[id].new_top();
  1414 inline HeapWord* PSParallelCompact::dense_prefix(SpaceId id) {
  1415   assert(id < last_space_id, "id out of range");
  1416   return _space_info[id].dense_prefix();
  1419 inline ObjectStartArray* PSParallelCompact::start_array(SpaceId id) {
  1420   assert(id < last_space_id, "id out of range");
  1421   return _space_info[id].start_array();
  1424 inline bool PSParallelCompact::should_update_klass(klassOop k) {
  1425   return ((HeapWord*) k) >= dense_prefix(perm_space_id);
  1428 template <class T>
  1429 inline void PSParallelCompact::adjust_pointer(T* p,
  1430                                               HeapWord* beg_addr,
  1431                                               HeapWord* end_addr) {
  1432   if (is_in((HeapWord*)p, beg_addr, end_addr)) {
  1433     adjust_pointer(p);
  1437 class MoveAndUpdateClosure: public ParMarkBitMapClosure {
  1438  public:
  1439   inline MoveAndUpdateClosure(ParMarkBitMap* bitmap, ParCompactionManager* cm,
  1440                               ObjectStartArray* start_array,
  1441                               HeapWord* destination, size_t words);
  1443   // Accessors.
  1444   HeapWord* destination() const         { return _destination; }
  1446   // If the object will fit (size <= words_remaining()), copy it to the current
  1447   // destination, update the interior oops and the start array and return either
  1448   // full (if the closure is full) or incomplete.  If the object will not fit,
  1449   // return would_overflow.
  1450   virtual IterationStatus do_addr(HeapWord* addr, size_t size);
  1452   // Copy enough words to fill this closure, starting at source().  Interior
  1453   // oops and the start array are not updated.  Return full.
  1454   IterationStatus copy_until_full();
  1456   // Copy enough words to fill this closure or to the end of an object,
  1457   // whichever is smaller, starting at source().  Interior oops and the start
  1458   // array are not updated.
  1459   void copy_partial_obj();
  1461  protected:
  1462   // Update variables to indicate that word_count words were processed.
  1463   inline void update_state(size_t word_count);
  1465  protected:
  1466   ObjectStartArray* const _start_array;
  1467   HeapWord*               _destination;         // Next addr to be written.
  1468 };
  1470 inline
  1471 MoveAndUpdateClosure::MoveAndUpdateClosure(ParMarkBitMap* bitmap,
  1472                                            ParCompactionManager* cm,
  1473                                            ObjectStartArray* start_array,
  1474                                            HeapWord* destination,
  1475                                            size_t words) :
  1476   ParMarkBitMapClosure(bitmap, cm, words), _start_array(start_array)
  1478   _destination = destination;
  1481 inline void MoveAndUpdateClosure::update_state(size_t words)
  1483   decrement_words_remaining(words);
  1484   _source += words;
  1485   _destination += words;
  1488 class UpdateOnlyClosure: public ParMarkBitMapClosure {
  1489  private:
  1490   const PSParallelCompact::SpaceId _space_id;
  1491   ObjectStartArray* const          _start_array;
  1493  public:
  1494   UpdateOnlyClosure(ParMarkBitMap* mbm,
  1495                     ParCompactionManager* cm,
  1496                     PSParallelCompact::SpaceId space_id);
  1498   // Update the object.
  1499   virtual IterationStatus do_addr(HeapWord* addr, size_t words);
  1501   inline void do_addr(HeapWord* addr);
  1502 };
  1504 inline void UpdateOnlyClosure::do_addr(HeapWord* addr)
  1506   _start_array->allocate_block(addr);
  1507   oop(addr)->update_contents(compaction_manager());
  1510 class FillClosure: public ParMarkBitMapClosure {
  1511  public:
  1512   FillClosure(ParCompactionManager* cm, PSParallelCompact::SpaceId space_id) :
  1513     ParMarkBitMapClosure(PSParallelCompact::mark_bitmap(), cm),
  1514     _space_id(space_id),
  1515     _start_array(PSParallelCompact::start_array(space_id)) {
  1516     assert(_space_id == PSParallelCompact::perm_space_id ||
  1517            _space_id == PSParallelCompact::old_space_id,
  1518            "cannot use FillClosure in the young gen");
  1519     assert(bitmap() != NULL, "need a bitmap");
  1520     assert(_start_array != NULL, "need a start array");
  1523   void fill_region(HeapWord* addr, size_t size) {
  1524     MemRegion region(addr, size);
  1525     SharedHeap::fill_region_with_object(region);
  1526     _start_array->allocate_block(addr);
  1529   virtual IterationStatus do_addr(HeapWord* addr, size_t size) {
  1530     fill_region(addr, size);
  1531     return ParMarkBitMap::incomplete;
  1534 private:
  1535   const PSParallelCompact::SpaceId _space_id;
  1536   ObjectStartArray* const          _start_array;
  1537 };

mercurial