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

Tue, 30 Sep 2008 11:49:31 -0700

author
jcoomes
date
Tue, 30 Sep 2008 11:49:31 -0700
changeset 809
a4b729f5b611
parent 704
850fdf70db2b
child 810
81cd571500b0
permissions
-rw-r--r--

6716466: par compact - remove VerifyParallelOldWithMarkSweep code
Reviewed-by: jmasa

     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 #include "incls/_precompiled.incl"
    26 #include "incls/_psParallelCompact.cpp.incl"
    28 #include <math.h>
    30 // All sizes are in HeapWords.
    31 const size_t ParallelCompactData::Log2ChunkSize  = 9; // 512 words
    32 const size_t ParallelCompactData::ChunkSize      = (size_t)1 << Log2ChunkSize;
    33 const size_t ParallelCompactData::ChunkSizeBytes = ChunkSize << LogHeapWordSize;
    34 const size_t ParallelCompactData::ChunkSizeOffsetMask = ChunkSize - 1;
    35 const size_t ParallelCompactData::ChunkAddrOffsetMask = ChunkSizeBytes - 1;
    36 const size_t ParallelCompactData::ChunkAddrMask  = ~ChunkAddrOffsetMask;
    38 // 32-bit:  128 words covers 4 bitmap words
    39 // 64-bit:  128 words covers 2 bitmap words
    40 const size_t ParallelCompactData::Log2BlockSize   = 7; // 128 words
    41 const size_t ParallelCompactData::BlockSize       = (size_t)1 << Log2BlockSize;
    42 const size_t ParallelCompactData::BlockOffsetMask = BlockSize - 1;
    43 const size_t ParallelCompactData::BlockMask       = ~BlockOffsetMask;
    45 const size_t ParallelCompactData::BlocksPerChunk = ChunkSize / BlockSize;
    47 const ParallelCompactData::ChunkData::chunk_sz_t
    48 ParallelCompactData::ChunkData::dc_shift = 27;
    50 const ParallelCompactData::ChunkData::chunk_sz_t
    51 ParallelCompactData::ChunkData::dc_mask = ~0U << dc_shift;
    53 const ParallelCompactData::ChunkData::chunk_sz_t
    54 ParallelCompactData::ChunkData::dc_one = 0x1U << dc_shift;
    56 const ParallelCompactData::ChunkData::chunk_sz_t
    57 ParallelCompactData::ChunkData::los_mask = ~dc_mask;
    59 const ParallelCompactData::ChunkData::chunk_sz_t
    60 ParallelCompactData::ChunkData::dc_claimed = 0x8U << dc_shift;
    62 const ParallelCompactData::ChunkData::chunk_sz_t
    63 ParallelCompactData::ChunkData::dc_completed = 0xcU << dc_shift;
    65 #ifdef ASSERT
    66 short   ParallelCompactData::BlockData::_cur_phase = 0;
    67 #endif
    69 SpaceInfo PSParallelCompact::_space_info[PSParallelCompact::last_space_id];
    70 bool      PSParallelCompact::_print_phases = false;
    72 ReferenceProcessor* PSParallelCompact::_ref_processor = NULL;
    73 klassOop            PSParallelCompact::_updated_int_array_klass_obj = NULL;
    75 double PSParallelCompact::_dwl_mean;
    76 double PSParallelCompact::_dwl_std_dev;
    77 double PSParallelCompact::_dwl_first_term;
    78 double PSParallelCompact::_dwl_adjustment;
    79 #ifdef  ASSERT
    80 bool   PSParallelCompact::_dwl_initialized = false;
    81 #endif  // #ifdef ASSERT
    83 #ifdef VALIDATE_MARK_SWEEP
    84 GrowableArray<void*>*   PSParallelCompact::_root_refs_stack = NULL;
    85 GrowableArray<oop> *    PSParallelCompact::_live_oops = NULL;
    86 GrowableArray<oop> *    PSParallelCompact::_live_oops_moved_to = NULL;
    87 GrowableArray<size_t>*  PSParallelCompact::_live_oops_size = NULL;
    88 size_t                  PSParallelCompact::_live_oops_index = 0;
    89 size_t                  PSParallelCompact::_live_oops_index_at_perm = 0;
    90 GrowableArray<void*>*   PSParallelCompact::_other_refs_stack = NULL;
    91 GrowableArray<void*>*   PSParallelCompact::_adjusted_pointers = NULL;
    92 bool                    PSParallelCompact::_pointer_tracking = false;
    93 bool                    PSParallelCompact::_root_tracking = true;
    95 GrowableArray<HeapWord*>* PSParallelCompact::_cur_gc_live_oops = NULL;
    96 GrowableArray<HeapWord*>* PSParallelCompact::_cur_gc_live_oops_moved_to = NULL;
    97 GrowableArray<size_t>   * PSParallelCompact::_cur_gc_live_oops_size = NULL;
    98 GrowableArray<HeapWord*>* PSParallelCompact::_last_gc_live_oops = NULL;
    99 GrowableArray<HeapWord*>* PSParallelCompact::_last_gc_live_oops_moved_to = NULL;
   100 GrowableArray<size_t>   * PSParallelCompact::_last_gc_live_oops_size = NULL;
   101 #endif
   103 #ifndef PRODUCT
   104 const char* PSParallelCompact::space_names[] = {
   105   "perm", "old ", "eden", "from", "to  "
   106 };
   108 void PSParallelCompact::print_chunk_ranges()
   109 {
   110   tty->print_cr("space  bottom     top        end        new_top");
   111   tty->print_cr("------ ---------- ---------- ---------- ----------");
   113   for (unsigned int id = 0; id < last_space_id; ++id) {
   114     const MutableSpace* space = _space_info[id].space();
   115     tty->print_cr("%u %s "
   116                   SIZE_FORMAT_W(10) " " SIZE_FORMAT_W(10) " "
   117                   SIZE_FORMAT_W(10) " " SIZE_FORMAT_W(10) " ",
   118                   id, space_names[id],
   119                   summary_data().addr_to_chunk_idx(space->bottom()),
   120                   summary_data().addr_to_chunk_idx(space->top()),
   121                   summary_data().addr_to_chunk_idx(space->end()),
   122                   summary_data().addr_to_chunk_idx(_space_info[id].new_top()));
   123   }
   124 }
   126 void
   127 print_generic_summary_chunk(size_t i, const ParallelCompactData::ChunkData* c)
   128 {
   129 #define CHUNK_IDX_FORMAT        SIZE_FORMAT_W(7)
   130 #define CHUNK_DATA_FORMAT       SIZE_FORMAT_W(5)
   132   ParallelCompactData& sd = PSParallelCompact::summary_data();
   133   size_t dci = c->destination() ? sd.addr_to_chunk_idx(c->destination()) : 0;
   134   tty->print_cr(CHUNK_IDX_FORMAT " " PTR_FORMAT " "
   135                 CHUNK_IDX_FORMAT " " PTR_FORMAT " "
   136                 CHUNK_DATA_FORMAT " " CHUNK_DATA_FORMAT " "
   137                 CHUNK_DATA_FORMAT " " CHUNK_IDX_FORMAT " %d",
   138                 i, c->data_location(), dci, c->destination(),
   139                 c->partial_obj_size(), c->live_obj_size(),
   140                 c->data_size(), c->source_chunk(), c->destination_count());
   142 #undef  CHUNK_IDX_FORMAT
   143 #undef  CHUNK_DATA_FORMAT
   144 }
   146 void
   147 print_generic_summary_data(ParallelCompactData& summary_data,
   148                            HeapWord* const beg_addr,
   149                            HeapWord* const end_addr)
   150 {
   151   size_t total_words = 0;
   152   size_t i = summary_data.addr_to_chunk_idx(beg_addr);
   153   const size_t last = summary_data.addr_to_chunk_idx(end_addr);
   154   HeapWord* pdest = 0;
   156   while (i <= last) {
   157     ParallelCompactData::ChunkData* c = summary_data.chunk(i);
   158     if (c->data_size() != 0 || c->destination() != pdest) {
   159       print_generic_summary_chunk(i, c);
   160       total_words += c->data_size();
   161       pdest = c->destination();
   162     }
   163     ++i;
   164   }
   166   tty->print_cr("summary_data_bytes=" SIZE_FORMAT, total_words * HeapWordSize);
   167 }
   169 void
   170 print_generic_summary_data(ParallelCompactData& summary_data,
   171                            SpaceInfo* space_info)
   172 {
   173   for (unsigned int id = 0; id < PSParallelCompact::last_space_id; ++id) {
   174     const MutableSpace* space = space_info[id].space();
   175     print_generic_summary_data(summary_data, space->bottom(),
   176                                MAX2(space->top(), space_info[id].new_top()));
   177   }
   178 }
   180 void
   181 print_initial_summary_chunk(size_t i,
   182                             const ParallelCompactData::ChunkData* c,
   183                             bool newline = true)
   184 {
   185   tty->print(SIZE_FORMAT_W(5) " " PTR_FORMAT " "
   186              SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " "
   187              SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " %d",
   188              i, c->destination(),
   189              c->partial_obj_size(), c->live_obj_size(),
   190              c->data_size(), c->source_chunk(), c->destination_count());
   191   if (newline) tty->cr();
   192 }
   194 void
   195 print_initial_summary_data(ParallelCompactData& summary_data,
   196                            const MutableSpace* space) {
   197   if (space->top() == space->bottom()) {
   198     return;
   199   }
   201   const size_t chunk_size = ParallelCompactData::ChunkSize;
   202   HeapWord* const top_aligned_up = summary_data.chunk_align_up(space->top());
   203   const size_t end_chunk = summary_data.addr_to_chunk_idx(top_aligned_up);
   204   const ParallelCompactData::ChunkData* c = summary_data.chunk(end_chunk - 1);
   205   HeapWord* end_addr = c->destination() + c->data_size();
   206   const size_t live_in_space = pointer_delta(end_addr, space->bottom());
   208   // Print (and count) the full chunks at the beginning of the space.
   209   size_t full_chunk_count = 0;
   210   size_t i = summary_data.addr_to_chunk_idx(space->bottom());
   211   while (i < end_chunk && summary_data.chunk(i)->data_size() == chunk_size) {
   212     print_initial_summary_chunk(i, summary_data.chunk(i));
   213     ++full_chunk_count;
   214     ++i;
   215   }
   217   size_t live_to_right = live_in_space - full_chunk_count * chunk_size;
   219   double max_reclaimed_ratio = 0.0;
   220   size_t max_reclaimed_ratio_chunk = 0;
   221   size_t max_dead_to_right = 0;
   222   size_t max_live_to_right = 0;
   224   // Print the 'reclaimed ratio' for chunks while there is something live in the
   225   // chunk or to the right of it.  The remaining chunks are empty (and
   226   // uninteresting), and computing the ratio will result in division by 0.
   227   while (i < end_chunk && live_to_right > 0) {
   228     c = summary_data.chunk(i);
   229     HeapWord* const chunk_addr = summary_data.chunk_to_addr(i);
   230     const size_t used_to_right = pointer_delta(space->top(), chunk_addr);
   231     const size_t dead_to_right = used_to_right - live_to_right;
   232     const double reclaimed_ratio = double(dead_to_right) / live_to_right;
   234     if (reclaimed_ratio > max_reclaimed_ratio) {
   235             max_reclaimed_ratio = reclaimed_ratio;
   236             max_reclaimed_ratio_chunk = i;
   237             max_dead_to_right = dead_to_right;
   238             max_live_to_right = live_to_right;
   239     }
   241     print_initial_summary_chunk(i, c, false);
   242     tty->print_cr(" %12.10f " SIZE_FORMAT_W(10) " " SIZE_FORMAT_W(10),
   243                   reclaimed_ratio, dead_to_right, live_to_right);
   245     live_to_right -= c->data_size();
   246     ++i;
   247   }
   249   // Any remaining chunks are empty.  Print one more if there is one.
   250   if (i < end_chunk) {
   251     print_initial_summary_chunk(i, summary_data.chunk(i));
   252   }
   254   tty->print_cr("max:  " SIZE_FORMAT_W(4) " d2r=" SIZE_FORMAT_W(10) " "
   255                 "l2r=" SIZE_FORMAT_W(10) " max_ratio=%14.12f",
   256                 max_reclaimed_ratio_chunk, max_dead_to_right,
   257                 max_live_to_right, max_reclaimed_ratio);
   258 }
   260 void
   261 print_initial_summary_data(ParallelCompactData& summary_data,
   262                            SpaceInfo* space_info) {
   263   unsigned int id = PSParallelCompact::perm_space_id;
   264   const MutableSpace* space;
   265   do {
   266     space = space_info[id].space();
   267     print_initial_summary_data(summary_data, space);
   268   } while (++id < PSParallelCompact::eden_space_id);
   270   do {
   271     space = space_info[id].space();
   272     print_generic_summary_data(summary_data, space->bottom(), space->top());
   273   } while (++id < PSParallelCompact::last_space_id);
   274 }
   275 #endif  // #ifndef PRODUCT
   277 #ifdef  ASSERT
   278 size_t add_obj_count;
   279 size_t add_obj_size;
   280 size_t mark_bitmap_count;
   281 size_t mark_bitmap_size;
   282 #endif  // #ifdef ASSERT
   284 ParallelCompactData::ParallelCompactData()
   285 {
   286   _region_start = 0;
   288   _chunk_vspace = 0;
   289   _chunk_data = 0;
   290   _chunk_count = 0;
   292   _block_vspace = 0;
   293   _block_data = 0;
   294   _block_count = 0;
   295 }
   297 bool ParallelCompactData::initialize(MemRegion covered_region)
   298 {
   299   _region_start = covered_region.start();
   300   const size_t region_size = covered_region.word_size();
   301   DEBUG_ONLY(_region_end = _region_start + region_size;)
   303   assert(chunk_align_down(_region_start) == _region_start,
   304          "region start not aligned");
   305   assert((region_size & ChunkSizeOffsetMask) == 0,
   306          "region size not a multiple of ChunkSize");
   308   bool result = initialize_chunk_data(region_size);
   310   // Initialize the block data if it will be used for updating pointers, or if
   311   // this is a debug build.
   312   if (!UseParallelOldGCChunkPointerCalc || trueInDebug) {
   313     result = result && initialize_block_data(region_size);
   314   }
   316   return result;
   317 }
   319 PSVirtualSpace*
   320 ParallelCompactData::create_vspace(size_t count, size_t element_size)
   321 {
   322   const size_t raw_bytes = count * element_size;
   323   const size_t page_sz = os::page_size_for_region(raw_bytes, raw_bytes, 10);
   324   const size_t granularity = os::vm_allocation_granularity();
   325   const size_t bytes = align_size_up(raw_bytes, MAX2(page_sz, granularity));
   327   const size_t rs_align = page_sz == (size_t) os::vm_page_size() ? 0 :
   328     MAX2(page_sz, granularity);
   329   ReservedSpace rs(bytes, rs_align, rs_align > 0);
   330   os::trace_page_sizes("par compact", raw_bytes, raw_bytes, page_sz, rs.base(),
   331                        rs.size());
   332   PSVirtualSpace* vspace = new PSVirtualSpace(rs, page_sz);
   333   if (vspace != 0) {
   334     if (vspace->expand_by(bytes)) {
   335       return vspace;
   336     }
   337     delete vspace;
   338     // Release memory reserved in the space.
   339     rs.release();
   340   }
   342   return 0;
   343 }
   345 bool ParallelCompactData::initialize_chunk_data(size_t region_size)
   346 {
   347   const size_t count = (region_size + ChunkSizeOffsetMask) >> Log2ChunkSize;
   348   _chunk_vspace = create_vspace(count, sizeof(ChunkData));
   349   if (_chunk_vspace != 0) {
   350     _chunk_data = (ChunkData*)_chunk_vspace->reserved_low_addr();
   351     _chunk_count = count;
   352     return true;
   353   }
   354   return false;
   355 }
   357 bool ParallelCompactData::initialize_block_data(size_t region_size)
   358 {
   359   const size_t count = (region_size + BlockOffsetMask) >> Log2BlockSize;
   360   _block_vspace = create_vspace(count, sizeof(BlockData));
   361   if (_block_vspace != 0) {
   362     _block_data = (BlockData*)_block_vspace->reserved_low_addr();
   363     _block_count = count;
   364     return true;
   365   }
   366   return false;
   367 }
   369 void ParallelCompactData::clear()
   370 {
   371   if (_block_data) {
   372     memset(_block_data, 0, _block_vspace->committed_size());
   373   }
   374   memset(_chunk_data, 0, _chunk_vspace->committed_size());
   375 }
   377 void ParallelCompactData::clear_range(size_t beg_chunk, size_t end_chunk) {
   378   assert(beg_chunk <= _chunk_count, "beg_chunk out of range");
   379   assert(end_chunk <= _chunk_count, "end_chunk out of range");
   380   assert(ChunkSize % BlockSize == 0, "ChunkSize not a multiple of BlockSize");
   382   const size_t chunk_cnt = end_chunk - beg_chunk;
   384   if (_block_data) {
   385     const size_t blocks_per_chunk = ChunkSize / BlockSize;
   386     const size_t beg_block = beg_chunk * blocks_per_chunk;
   387     const size_t block_cnt = chunk_cnt * blocks_per_chunk;
   388     memset(_block_data + beg_block, 0, block_cnt * sizeof(BlockData));
   389   }
   390   memset(_chunk_data + beg_chunk, 0, chunk_cnt * sizeof(ChunkData));
   391 }
   393 HeapWord* ParallelCompactData::partial_obj_end(size_t chunk_idx) const
   394 {
   395   const ChunkData* cur_cp = chunk(chunk_idx);
   396   const ChunkData* const end_cp = chunk(chunk_count() - 1);
   398   HeapWord* result = chunk_to_addr(chunk_idx);
   399   if (cur_cp < end_cp) {
   400     do {
   401       result += cur_cp->partial_obj_size();
   402     } while (cur_cp->partial_obj_size() == ChunkSize && ++cur_cp < end_cp);
   403   }
   404   return result;
   405 }
   407 void ParallelCompactData::add_obj(HeapWord* addr, size_t len)
   408 {
   409   const size_t obj_ofs = pointer_delta(addr, _region_start);
   410   const size_t beg_chunk = obj_ofs >> Log2ChunkSize;
   411   const size_t end_chunk = (obj_ofs + len - 1) >> Log2ChunkSize;
   413   DEBUG_ONLY(Atomic::inc_ptr(&add_obj_count);)
   414   DEBUG_ONLY(Atomic::add_ptr(len, &add_obj_size);)
   416   if (beg_chunk == end_chunk) {
   417     // All in one chunk.
   418     _chunk_data[beg_chunk].add_live_obj(len);
   419     return;
   420   }
   422   // First chunk.
   423   const size_t beg_ofs = chunk_offset(addr);
   424   _chunk_data[beg_chunk].add_live_obj(ChunkSize - beg_ofs);
   426   klassOop klass = ((oop)addr)->klass();
   427   // Middle chunks--completely spanned by this object.
   428   for (size_t chunk = beg_chunk + 1; chunk < end_chunk; ++chunk) {
   429     _chunk_data[chunk].set_partial_obj_size(ChunkSize);
   430     _chunk_data[chunk].set_partial_obj_addr(addr);
   431   }
   433   // Last chunk.
   434   const size_t end_ofs = chunk_offset(addr + len - 1);
   435   _chunk_data[end_chunk].set_partial_obj_size(end_ofs + 1);
   436   _chunk_data[end_chunk].set_partial_obj_addr(addr);
   437 }
   439 void
   440 ParallelCompactData::summarize_dense_prefix(HeapWord* beg, HeapWord* end)
   441 {
   442   assert(chunk_offset(beg) == 0, "not ChunkSize aligned");
   443   assert(chunk_offset(end) == 0, "not ChunkSize aligned");
   445   size_t cur_chunk = addr_to_chunk_idx(beg);
   446   const size_t end_chunk = addr_to_chunk_idx(end);
   447   HeapWord* addr = beg;
   448   while (cur_chunk < end_chunk) {
   449     _chunk_data[cur_chunk].set_destination(addr);
   450     _chunk_data[cur_chunk].set_destination_count(0);
   451     _chunk_data[cur_chunk].set_source_chunk(cur_chunk);
   452     _chunk_data[cur_chunk].set_data_location(addr);
   454     // Update live_obj_size so the chunk appears completely full.
   455     size_t live_size = ChunkSize - _chunk_data[cur_chunk].partial_obj_size();
   456     _chunk_data[cur_chunk].set_live_obj_size(live_size);
   458     ++cur_chunk;
   459     addr += ChunkSize;
   460   }
   461 }
   463 bool ParallelCompactData::summarize(HeapWord* target_beg, HeapWord* target_end,
   464                                     HeapWord* source_beg, HeapWord* source_end,
   465                                     HeapWord** target_next,
   466                                     HeapWord** source_next) {
   467   // This is too strict.
   468   // assert(chunk_offset(source_beg) == 0, "not ChunkSize aligned");
   470   if (TraceParallelOldGCSummaryPhase) {
   471     tty->print_cr("tb=" PTR_FORMAT " te=" PTR_FORMAT " "
   472                   "sb=" PTR_FORMAT " se=" PTR_FORMAT " "
   473                   "tn=" PTR_FORMAT " sn=" PTR_FORMAT,
   474                   target_beg, target_end,
   475                   source_beg, source_end,
   476                   target_next != 0 ? *target_next : (HeapWord*) 0,
   477                   source_next != 0 ? *source_next : (HeapWord*) 0);
   478   }
   480   size_t cur_chunk = addr_to_chunk_idx(source_beg);
   481   const size_t end_chunk = addr_to_chunk_idx(chunk_align_up(source_end));
   483   HeapWord *dest_addr = target_beg;
   484   while (cur_chunk < end_chunk) {
   485     size_t words = _chunk_data[cur_chunk].data_size();
   487 #if     1
   488     assert(pointer_delta(target_end, dest_addr) >= words,
   489            "source region does not fit into target region");
   490 #else
   491     // XXX - need some work on the corner cases here.  If the chunk does not
   492     // fit, then must either make sure any partial_obj from the chunk fits, or
   493     // 'undo' the initial part of the partial_obj that is in the previous chunk.
   494     if (dest_addr + words >= target_end) {
   495       // Let the caller know where to continue.
   496       *target_next = dest_addr;
   497       *source_next = chunk_to_addr(cur_chunk);
   498       return false;
   499     }
   500 #endif  // #if 1
   502     _chunk_data[cur_chunk].set_destination(dest_addr);
   504     // Set the destination_count for cur_chunk, and if necessary, update
   505     // source_chunk for a destination chunk.  The source_chunk field is updated
   506     // if cur_chunk is the first (left-most) chunk to be copied to a destination
   507     // chunk.
   508     //
   509     // The destination_count calculation is a bit subtle.  A chunk that has data
   510     // that compacts into itself does not count itself as a destination.  This
   511     // maintains the invariant that a zero count means the chunk is available
   512     // and can be claimed and then filled.
   513     if (words > 0) {
   514       HeapWord* const last_addr = dest_addr + words - 1;
   515       const size_t dest_chunk_1 = addr_to_chunk_idx(dest_addr);
   516       const size_t dest_chunk_2 = addr_to_chunk_idx(last_addr);
   517 #if     0
   518       // Initially assume that the destination chunks will be the same and
   519       // adjust the value below if necessary.  Under this assumption, if
   520       // cur_chunk == dest_chunk_2, then cur_chunk will be compacted completely
   521       // into itself.
   522       uint destination_count = cur_chunk == dest_chunk_2 ? 0 : 1;
   523       if (dest_chunk_1 != dest_chunk_2) {
   524         // Destination chunks differ; adjust destination_count.
   525         destination_count += 1;
   526         // Data from cur_chunk will be copied to the start of dest_chunk_2.
   527         _chunk_data[dest_chunk_2].set_source_chunk(cur_chunk);
   528       } else if (chunk_offset(dest_addr) == 0) {
   529         // Data from cur_chunk will be copied to the start of the destination
   530         // chunk.
   531         _chunk_data[dest_chunk_1].set_source_chunk(cur_chunk);
   532       }
   533 #else
   534       // Initially assume that the destination chunks will be different and
   535       // adjust the value below if necessary.  Under this assumption, if
   536       // cur_chunk == dest_chunk2, then cur_chunk will be compacted partially
   537       // into dest_chunk_1 and partially into itself.
   538       uint destination_count = cur_chunk == dest_chunk_2 ? 1 : 2;
   539       if (dest_chunk_1 != dest_chunk_2) {
   540         // Data from cur_chunk will be copied to the start of dest_chunk_2.
   541         _chunk_data[dest_chunk_2].set_source_chunk(cur_chunk);
   542       } else {
   543         // Destination chunks are the same; adjust destination_count.
   544         destination_count -= 1;
   545         if (chunk_offset(dest_addr) == 0) {
   546           // Data from cur_chunk will be copied to the start of the destination
   547           // chunk.
   548           _chunk_data[dest_chunk_1].set_source_chunk(cur_chunk);
   549         }
   550       }
   551 #endif  // #if 0
   553       _chunk_data[cur_chunk].set_destination_count(destination_count);
   554       _chunk_data[cur_chunk].set_data_location(chunk_to_addr(cur_chunk));
   555       dest_addr += words;
   556     }
   558     ++cur_chunk;
   559   }
   561   *target_next = dest_addr;
   562   return true;
   563 }
   565 bool ParallelCompactData::partial_obj_ends_in_block(size_t block_index) {
   566   HeapWord* block_addr = block_to_addr(block_index);
   567   HeapWord* block_end_addr = block_addr + BlockSize;
   568   size_t chunk_index = addr_to_chunk_idx(block_addr);
   569   HeapWord* partial_obj_end_addr = partial_obj_end(chunk_index);
   571   // An object that ends at the end of the block, ends
   572   // in the block (the last word of the object is to
   573   // the left of the end).
   574   if ((block_addr < partial_obj_end_addr) &&
   575       (partial_obj_end_addr <= block_end_addr)) {
   576     return true;
   577   }
   579   return false;
   580 }
   582 HeapWord* ParallelCompactData::calc_new_pointer(HeapWord* addr) {
   583   HeapWord* result = NULL;
   584   if (UseParallelOldGCChunkPointerCalc) {
   585     result = chunk_calc_new_pointer(addr);
   586   } else {
   587     result = block_calc_new_pointer(addr);
   588   }
   589   return result;
   590 }
   592 // This method is overly complicated (expensive) to be called
   593 // for every reference.
   594 // Try to restructure this so that a NULL is returned if
   595 // the object is dead.  But don't wast the cycles to explicitly check
   596 // that it is dead since only live objects should be passed in.
   598 HeapWord* ParallelCompactData::chunk_calc_new_pointer(HeapWord* addr) {
   599   assert(addr != NULL, "Should detect NULL oop earlier");
   600   assert(PSParallelCompact::gc_heap()->is_in(addr), "addr not in heap");
   601 #ifdef ASSERT
   602   if (PSParallelCompact::mark_bitmap()->is_unmarked(addr)) {
   603     gclog_or_tty->print_cr("calc_new_pointer:: addr " PTR_FORMAT, addr);
   604   }
   605 #endif
   606   assert(PSParallelCompact::mark_bitmap()->is_marked(addr), "obj not marked");
   608   // Chunk covering the object.
   609   size_t chunk_index = addr_to_chunk_idx(addr);
   610   const ChunkData* const chunk_ptr = chunk(chunk_index);
   611   HeapWord* const chunk_addr = chunk_align_down(addr);
   613   assert(addr < chunk_addr + ChunkSize, "Chunk does not cover object");
   614   assert(addr_to_chunk_ptr(chunk_addr) == chunk_ptr, "sanity check");
   616   HeapWord* result = chunk_ptr->destination();
   618   // If all the data in the chunk is live, then the new location of the object
   619   // can be calculated from the destination of the chunk plus the offset of the
   620   // object in the chunk.
   621   if (chunk_ptr->data_size() == ChunkSize) {
   622     result += pointer_delta(addr, chunk_addr);
   623     return result;
   624   }
   626   // The new location of the object is
   627   //    chunk destination +
   628   //    size of the partial object extending onto the chunk +
   629   //    sizes of the live objects in the Chunk that are to the left of addr
   630   const size_t partial_obj_size = chunk_ptr->partial_obj_size();
   631   HeapWord* const search_start = chunk_addr + partial_obj_size;
   633   const ParMarkBitMap* bitmap = PSParallelCompact::mark_bitmap();
   634   size_t live_to_left = bitmap->live_words_in_range(search_start, oop(addr));
   636   result += partial_obj_size + live_to_left;
   637   assert(result <= addr, "object cannot move to the right");
   638   return result;
   639 }
   641 HeapWord* ParallelCompactData::block_calc_new_pointer(HeapWord* addr) {
   642   assert(addr != NULL, "Should detect NULL oop earlier");
   643   assert(PSParallelCompact::gc_heap()->is_in(addr), "addr not in heap");
   644 #ifdef ASSERT
   645   if (PSParallelCompact::mark_bitmap()->is_unmarked(addr)) {
   646     gclog_or_tty->print_cr("calc_new_pointer:: addr " PTR_FORMAT, addr);
   647   }
   648 #endif
   649   assert(PSParallelCompact::mark_bitmap()->is_marked(addr), "obj not marked");
   651   // Chunk covering the object.
   652   size_t chunk_index = addr_to_chunk_idx(addr);
   653   const ChunkData* const chunk_ptr = chunk(chunk_index);
   654   HeapWord* const chunk_addr = chunk_align_down(addr);
   656   assert(addr < chunk_addr + ChunkSize, "Chunk does not cover object");
   657   assert(addr_to_chunk_ptr(chunk_addr) == chunk_ptr, "sanity check");
   659   HeapWord* result = chunk_ptr->destination();
   661   // If all the data in the chunk is live, then the new location of the object
   662   // can be calculated from the destination of the chunk plus the offset of the
   663   // object in the chunk.
   664   if (chunk_ptr->data_size() == ChunkSize) {
   665     result += pointer_delta(addr, chunk_addr);
   666     return result;
   667   }
   669   // The new location of the object is
   670   //    chunk destination +
   671   //    block offset +
   672   //    sizes of the live objects in the Block that are to the left of addr
   673   const size_t block_offset = addr_to_block_ptr(addr)->offset();
   674   HeapWord* const search_start = chunk_addr + block_offset;
   676   const ParMarkBitMap* bitmap = PSParallelCompact::mark_bitmap();
   677   size_t live_to_left = bitmap->live_words_in_range(search_start, oop(addr));
   679   result += block_offset + live_to_left;
   680   assert(result <= addr, "object cannot move to the right");
   681   assert(result == chunk_calc_new_pointer(addr), "Should match");
   682   return result;
   683 }
   685 klassOop ParallelCompactData::calc_new_klass(klassOop old_klass) {
   686   klassOop updated_klass;
   687   if (PSParallelCompact::should_update_klass(old_klass)) {
   688     updated_klass = (klassOop) calc_new_pointer(old_klass);
   689   } else {
   690     updated_klass = old_klass;
   691   }
   693   return updated_klass;
   694 }
   696 #ifdef  ASSERT
   697 void ParallelCompactData::verify_clear(const PSVirtualSpace* vspace)
   698 {
   699   const size_t* const beg = (const size_t*)vspace->committed_low_addr();
   700   const size_t* const end = (const size_t*)vspace->committed_high_addr();
   701   for (const size_t* p = beg; p < end; ++p) {
   702     assert(*p == 0, "not zero");
   703   }
   704 }
   706 void ParallelCompactData::verify_clear()
   707 {
   708   verify_clear(_chunk_vspace);
   709   verify_clear(_block_vspace);
   710 }
   711 #endif  // #ifdef ASSERT
   713 #ifdef NOT_PRODUCT
   714 ParallelCompactData::ChunkData* debug_chunk(size_t chunk_index) {
   715   ParallelCompactData& sd = PSParallelCompact::summary_data();
   716   return sd.chunk(chunk_index);
   717 }
   718 #endif
   720 elapsedTimer        PSParallelCompact::_accumulated_time;
   721 unsigned int        PSParallelCompact::_total_invocations = 0;
   722 unsigned int        PSParallelCompact::_maximum_compaction_gc_num = 0;
   723 jlong               PSParallelCompact::_time_of_last_gc = 0;
   724 CollectorCounters*  PSParallelCompact::_counters = NULL;
   725 ParMarkBitMap       PSParallelCompact::_mark_bitmap;
   726 ParallelCompactData PSParallelCompact::_summary_data;
   728 PSParallelCompact::IsAliveClosure PSParallelCompact::_is_alive_closure;
   730 void PSParallelCompact::IsAliveClosure::do_object(oop p)   { ShouldNotReachHere(); }
   731 bool PSParallelCompact::IsAliveClosure::do_object_b(oop p) { return mark_bitmap()->is_marked(p); }
   733 void PSParallelCompact::KeepAliveClosure::do_oop(oop* p)       { PSParallelCompact::KeepAliveClosure::do_oop_work(p); }
   734 void PSParallelCompact::KeepAliveClosure::do_oop(narrowOop* p) { PSParallelCompact::KeepAliveClosure::do_oop_work(p); }
   736 PSParallelCompact::AdjustPointerClosure PSParallelCompact::_adjust_root_pointer_closure(true);
   737 PSParallelCompact::AdjustPointerClosure PSParallelCompact::_adjust_pointer_closure(false);
   739 void PSParallelCompact::AdjustPointerClosure::do_oop(oop* p)       { adjust_pointer(p, _is_root); }
   740 void PSParallelCompact::AdjustPointerClosure::do_oop(narrowOop* p) { adjust_pointer(p, _is_root); }
   742 void PSParallelCompact::FollowStackClosure::do_void() { follow_stack(_compaction_manager); }
   744 void PSParallelCompact::MarkAndPushClosure::do_oop(oop* p)       { mark_and_push(_compaction_manager, p); }
   745 void PSParallelCompact::MarkAndPushClosure::do_oop(narrowOop* p) { mark_and_push(_compaction_manager, p); }
   747 void PSParallelCompact::post_initialize() {
   748   ParallelScavengeHeap* heap = gc_heap();
   749   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
   751   MemRegion mr = heap->reserved_region();
   752   _ref_processor = ReferenceProcessor::create_ref_processor(
   753     mr,                         // span
   754     true,                       // atomic_discovery
   755     true,                       // mt_discovery
   756     &_is_alive_closure,
   757     ParallelGCThreads,
   758     ParallelRefProcEnabled);
   759   _counters = new CollectorCounters("PSParallelCompact", 1);
   761   // Initialize static fields in ParCompactionManager.
   762   ParCompactionManager::initialize(mark_bitmap());
   763 }
   765 bool PSParallelCompact::initialize() {
   766   ParallelScavengeHeap* heap = gc_heap();
   767   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
   768   MemRegion mr = heap->reserved_region();
   770   // Was the old gen get allocated successfully?
   771   if (!heap->old_gen()->is_allocated()) {
   772     return false;
   773   }
   775   initialize_space_info();
   776   initialize_dead_wood_limiter();
   778   if (!_mark_bitmap.initialize(mr)) {
   779     vm_shutdown_during_initialization("Unable to allocate bit map for "
   780       "parallel garbage collection for the requested heap size.");
   781     return false;
   782   }
   784   if (!_summary_data.initialize(mr)) {
   785     vm_shutdown_during_initialization("Unable to allocate tables for "
   786       "parallel garbage collection for the requested heap size.");
   787     return false;
   788   }
   790   return true;
   791 }
   793 void PSParallelCompact::initialize_space_info()
   794 {
   795   memset(&_space_info, 0, sizeof(_space_info));
   797   ParallelScavengeHeap* heap = gc_heap();
   798   PSYoungGen* young_gen = heap->young_gen();
   799   MutableSpace* perm_space = heap->perm_gen()->object_space();
   801   _space_info[perm_space_id].set_space(perm_space);
   802   _space_info[old_space_id].set_space(heap->old_gen()->object_space());
   803   _space_info[eden_space_id].set_space(young_gen->eden_space());
   804   _space_info[from_space_id].set_space(young_gen->from_space());
   805   _space_info[to_space_id].set_space(young_gen->to_space());
   807   _space_info[perm_space_id].set_start_array(heap->perm_gen()->start_array());
   808   _space_info[old_space_id].set_start_array(heap->old_gen()->start_array());
   810   _space_info[perm_space_id].set_min_dense_prefix(perm_space->top());
   811   if (TraceParallelOldGCDensePrefix) {
   812     tty->print_cr("perm min_dense_prefix=" PTR_FORMAT,
   813                   _space_info[perm_space_id].min_dense_prefix());
   814   }
   815 }
   817 void PSParallelCompact::initialize_dead_wood_limiter()
   818 {
   819   const size_t max = 100;
   820   _dwl_mean = double(MIN2(ParallelOldDeadWoodLimiterMean, max)) / 100.0;
   821   _dwl_std_dev = double(MIN2(ParallelOldDeadWoodLimiterStdDev, max)) / 100.0;
   822   _dwl_first_term = 1.0 / (sqrt(2.0 * M_PI) * _dwl_std_dev);
   823   DEBUG_ONLY(_dwl_initialized = true;)
   824   _dwl_adjustment = normal_distribution(1.0);
   825 }
   827 // Simple class for storing info about the heap at the start of GC, to be used
   828 // after GC for comparison/printing.
   829 class PreGCValues {
   830 public:
   831   PreGCValues() { }
   832   PreGCValues(ParallelScavengeHeap* heap) { fill(heap); }
   834   void fill(ParallelScavengeHeap* heap) {
   835     _heap_used      = heap->used();
   836     _young_gen_used = heap->young_gen()->used_in_bytes();
   837     _old_gen_used   = heap->old_gen()->used_in_bytes();
   838     _perm_gen_used  = heap->perm_gen()->used_in_bytes();
   839   };
   841   size_t heap_used() const      { return _heap_used; }
   842   size_t young_gen_used() const { return _young_gen_used; }
   843   size_t old_gen_used() const   { return _old_gen_used; }
   844   size_t perm_gen_used() const  { return _perm_gen_used; }
   846 private:
   847   size_t _heap_used;
   848   size_t _young_gen_used;
   849   size_t _old_gen_used;
   850   size_t _perm_gen_used;
   851 };
   853 void
   854 PSParallelCompact::clear_data_covering_space(SpaceId id)
   855 {
   856   // At this point, top is the value before GC, new_top() is the value that will
   857   // be set at the end of GC.  The marking bitmap is cleared to top; nothing
   858   // should be marked above top.  The summary data is cleared to the larger of
   859   // top & new_top.
   860   MutableSpace* const space = _space_info[id].space();
   861   HeapWord* const bot = space->bottom();
   862   HeapWord* const top = space->top();
   863   HeapWord* const max_top = MAX2(top, _space_info[id].new_top());
   865   const idx_t beg_bit = _mark_bitmap.addr_to_bit(bot);
   866   const idx_t end_bit = BitMap::word_align_up(_mark_bitmap.addr_to_bit(top));
   867   _mark_bitmap.clear_range(beg_bit, end_bit);
   869   const size_t beg_chunk = _summary_data.addr_to_chunk_idx(bot);
   870   const size_t end_chunk =
   871     _summary_data.addr_to_chunk_idx(_summary_data.chunk_align_up(max_top));
   872   _summary_data.clear_range(beg_chunk, end_chunk);
   873 }
   875 void PSParallelCompact::pre_compact(PreGCValues* pre_gc_values)
   876 {
   877   // Update the from & to space pointers in space_info, since they are swapped
   878   // at each young gen gc.  Do the update unconditionally (even though a
   879   // promotion failure does not swap spaces) because an unknown number of minor
   880   // collections will have swapped the spaces an unknown number of times.
   881   TraceTime tm("pre compact", print_phases(), true, gclog_or_tty);
   882   ParallelScavengeHeap* heap = gc_heap();
   883   _space_info[from_space_id].set_space(heap->young_gen()->from_space());
   884   _space_info[to_space_id].set_space(heap->young_gen()->to_space());
   886   pre_gc_values->fill(heap);
   888   ParCompactionManager::reset();
   889   NOT_PRODUCT(_mark_bitmap.reset_counters());
   890   DEBUG_ONLY(add_obj_count = add_obj_size = 0;)
   891   DEBUG_ONLY(mark_bitmap_count = mark_bitmap_size = 0;)
   893   // Increment the invocation count
   894   heap->increment_total_collections(true);
   896   // We need to track unique mark sweep invocations as well.
   897   _total_invocations++;
   899   if (PrintHeapAtGC) {
   900     Universe::print_heap_before_gc();
   901   }
   903   // Fill in TLABs
   904   heap->accumulate_statistics_all_tlabs();
   905   heap->ensure_parsability(true);  // retire TLABs
   907   if (VerifyBeforeGC && heap->total_collections() >= VerifyGCStartAt) {
   908     HandleMark hm;  // Discard invalid handles created during verification
   909     gclog_or_tty->print(" VerifyBeforeGC:");
   910     Universe::verify(true);
   911   }
   913   // Verify object start arrays
   914   if (VerifyObjectStartArray &&
   915       VerifyBeforeGC) {
   916     heap->old_gen()->verify_object_start_array();
   917     heap->perm_gen()->verify_object_start_array();
   918   }
   920   DEBUG_ONLY(mark_bitmap()->verify_clear();)
   921   DEBUG_ONLY(summary_data().verify_clear();)
   923   // Have worker threads release resources the next time they run a task.
   924   gc_task_manager()->release_all_resources();
   925 }
   927 void PSParallelCompact::post_compact()
   928 {
   929   TraceTime tm("post compact", print_phases(), true, gclog_or_tty);
   931   // Clear the marking bitmap and summary data and update top() in each space.
   932   for (unsigned int id = perm_space_id; id < last_space_id; ++id) {
   933     clear_data_covering_space(SpaceId(id));
   934     _space_info[id].space()->set_top(_space_info[id].new_top());
   935   }
   937   MutableSpace* const eden_space = _space_info[eden_space_id].space();
   938   MutableSpace* const from_space = _space_info[from_space_id].space();
   939   MutableSpace* const to_space   = _space_info[to_space_id].space();
   941   ParallelScavengeHeap* heap = gc_heap();
   942   bool eden_empty = eden_space->is_empty();
   943   if (!eden_empty) {
   944     eden_empty = absorb_live_data_from_eden(heap->size_policy(),
   945                                             heap->young_gen(), heap->old_gen());
   946   }
   948   // Update heap occupancy information which is used as input to the soft ref
   949   // clearing policy at the next gc.
   950   Universe::update_heap_info_at_gc();
   952   bool young_gen_empty = eden_empty && from_space->is_empty() &&
   953     to_space->is_empty();
   955   BarrierSet* bs = heap->barrier_set();
   956   if (bs->is_a(BarrierSet::ModRef)) {
   957     ModRefBarrierSet* modBS = (ModRefBarrierSet*)bs;
   958     MemRegion old_mr = heap->old_gen()->reserved();
   959     MemRegion perm_mr = heap->perm_gen()->reserved();
   960     assert(perm_mr.end() <= old_mr.start(), "Generations out of order");
   962     if (young_gen_empty) {
   963       modBS->clear(MemRegion(perm_mr.start(), old_mr.end()));
   964     } else {
   965       modBS->invalidate(MemRegion(perm_mr.start(), old_mr.end()));
   966     }
   967   }
   969   Threads::gc_epilogue();
   970   CodeCache::gc_epilogue();
   972   COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
   974   ref_processor()->enqueue_discovered_references(NULL);
   976   if (ZapUnusedHeapArea) {
   977     heap->gen_mangle_unused_area();
   978   }
   980   // Update time of last GC
   981   reset_millis_since_last_gc();
   982 }
   984 HeapWord*
   985 PSParallelCompact::compute_dense_prefix_via_density(const SpaceId id,
   986                                                     bool maximum_compaction)
   987 {
   988   const size_t chunk_size = ParallelCompactData::ChunkSize;
   989   const ParallelCompactData& sd = summary_data();
   991   const MutableSpace* const space = _space_info[id].space();
   992   HeapWord* const top_aligned_up = sd.chunk_align_up(space->top());
   993   const ChunkData* const beg_cp = sd.addr_to_chunk_ptr(space->bottom());
   994   const ChunkData* const end_cp = sd.addr_to_chunk_ptr(top_aligned_up);
   996   // Skip full chunks at the beginning of the space--they are necessarily part
   997   // of the dense prefix.
   998   size_t full_count = 0;
   999   const ChunkData* cp;
  1000   for (cp = beg_cp; cp < end_cp && cp->data_size() == chunk_size; ++cp) {
  1001     ++full_count;
  1004   assert(total_invocations() >= _maximum_compaction_gc_num, "sanity");
  1005   const size_t gcs_since_max = total_invocations() - _maximum_compaction_gc_num;
  1006   const bool interval_ended = gcs_since_max > HeapMaximumCompactionInterval;
  1007   if (maximum_compaction || cp == end_cp || interval_ended) {
  1008     _maximum_compaction_gc_num = total_invocations();
  1009     return sd.chunk_to_addr(cp);
  1012   HeapWord* const new_top = _space_info[id].new_top();
  1013   const size_t space_live = pointer_delta(new_top, space->bottom());
  1014   const size_t space_used = space->used_in_words();
  1015   const size_t space_capacity = space->capacity_in_words();
  1017   const double cur_density = double(space_live) / space_capacity;
  1018   const double deadwood_density =
  1019     (1.0 - cur_density) * (1.0 - cur_density) * cur_density * cur_density;
  1020   const size_t deadwood_goal = size_t(space_capacity * deadwood_density);
  1022   if (TraceParallelOldGCDensePrefix) {
  1023     tty->print_cr("cur_dens=%5.3f dw_dens=%5.3f dw_goal=" SIZE_FORMAT,
  1024                   cur_density, deadwood_density, deadwood_goal);
  1025     tty->print_cr("space_live=" SIZE_FORMAT " " "space_used=" SIZE_FORMAT " "
  1026                   "space_cap=" SIZE_FORMAT,
  1027                   space_live, space_used,
  1028                   space_capacity);
  1031   // XXX - Use binary search?
  1032   HeapWord* dense_prefix = sd.chunk_to_addr(cp);
  1033   const ChunkData* full_cp = cp;
  1034   const ChunkData* const top_cp = sd.addr_to_chunk_ptr(space->top() - 1);
  1035   while (cp < end_cp) {
  1036     HeapWord* chunk_destination = cp->destination();
  1037     const size_t cur_deadwood = pointer_delta(dense_prefix, chunk_destination);
  1038     if (TraceParallelOldGCDensePrefix && Verbose) {
  1039       tty->print_cr("c#=" SIZE_FORMAT_W(4) " dst=" PTR_FORMAT " "
  1040                     "dp=" SIZE_FORMAT_W(8) " " "cdw=" SIZE_FORMAT_W(8),
  1041                     sd.chunk(cp), chunk_destination,
  1042                     dense_prefix, cur_deadwood);
  1045     if (cur_deadwood >= deadwood_goal) {
  1046       // Found the chunk that has the correct amount of deadwood to the left.
  1047       // This typically occurs after crossing a fairly sparse set of chunks, so
  1048       // iterate backwards over those sparse chunks, looking for the chunk that
  1049       // has the lowest density of live objects 'to the right.'
  1050       size_t space_to_left = sd.chunk(cp) * chunk_size;
  1051       size_t live_to_left = space_to_left - cur_deadwood;
  1052       size_t space_to_right = space_capacity - space_to_left;
  1053       size_t live_to_right = space_live - live_to_left;
  1054       double density_to_right = double(live_to_right) / space_to_right;
  1055       while (cp > full_cp) {
  1056         --cp;
  1057         const size_t prev_chunk_live_to_right = live_to_right - cp->data_size();
  1058         const size_t prev_chunk_space_to_right = space_to_right + chunk_size;
  1059         double prev_chunk_density_to_right =
  1060           double(prev_chunk_live_to_right) / prev_chunk_space_to_right;
  1061         if (density_to_right <= prev_chunk_density_to_right) {
  1062           return dense_prefix;
  1064         if (TraceParallelOldGCDensePrefix && Verbose) {
  1065           tty->print_cr("backing up from c=" SIZE_FORMAT_W(4) " d2r=%10.8f "
  1066                         "pc_d2r=%10.8f", sd.chunk(cp), density_to_right,
  1067                         prev_chunk_density_to_right);
  1069         dense_prefix -= chunk_size;
  1070         live_to_right = prev_chunk_live_to_right;
  1071         space_to_right = prev_chunk_space_to_right;
  1072         density_to_right = prev_chunk_density_to_right;
  1074       return dense_prefix;
  1077     dense_prefix += chunk_size;
  1078     ++cp;
  1081   return dense_prefix;
  1084 #ifndef PRODUCT
  1085 void PSParallelCompact::print_dense_prefix_stats(const char* const algorithm,
  1086                                                  const SpaceId id,
  1087                                                  const bool maximum_compaction,
  1088                                                  HeapWord* const addr)
  1090   const size_t chunk_idx = summary_data().addr_to_chunk_idx(addr);
  1091   ChunkData* const cp = summary_data().chunk(chunk_idx);
  1092   const MutableSpace* const space = _space_info[id].space();
  1093   HeapWord* const new_top = _space_info[id].new_top();
  1095   const size_t space_live = pointer_delta(new_top, space->bottom());
  1096   const size_t dead_to_left = pointer_delta(addr, cp->destination());
  1097   const size_t space_cap = space->capacity_in_words();
  1098   const double dead_to_left_pct = double(dead_to_left) / space_cap;
  1099   const size_t live_to_right = new_top - cp->destination();
  1100   const size_t dead_to_right = space->top() - addr - live_to_right;
  1102   tty->print_cr("%s=" PTR_FORMAT " dpc=" SIZE_FORMAT_W(5) " "
  1103                 "spl=" SIZE_FORMAT " "
  1104                 "d2l=" SIZE_FORMAT " d2l%%=%6.4f "
  1105                 "d2r=" SIZE_FORMAT " l2r=" SIZE_FORMAT
  1106                 " ratio=%10.8f",
  1107                 algorithm, addr, chunk_idx,
  1108                 space_live,
  1109                 dead_to_left, dead_to_left_pct,
  1110                 dead_to_right, live_to_right,
  1111                 double(dead_to_right) / live_to_right);
  1113 #endif  // #ifndef PRODUCT
  1115 // Return a fraction indicating how much of the generation can be treated as
  1116 // "dead wood" (i.e., not reclaimed).  The function uses a normal distribution
  1117 // based on the density of live objects in the generation to determine a limit,
  1118 // which is then adjusted so the return value is min_percent when the density is
  1119 // 1.
  1120 //
  1121 // The following table shows some return values for a different values of the
  1122 // standard deviation (ParallelOldDeadWoodLimiterStdDev); the mean is 0.5 and
  1123 // min_percent is 1.
  1124 //
  1125 //                          fraction allowed as dead wood
  1126 //         -----------------------------------------------------------------
  1127 // density std_dev=70 std_dev=75 std_dev=80 std_dev=85 std_dev=90 std_dev=95
  1128 // ------- ---------- ---------- ---------- ---------- ---------- ----------
  1129 // 0.00000 0.01000000 0.01000000 0.01000000 0.01000000 0.01000000 0.01000000
  1130 // 0.05000 0.03193096 0.02836880 0.02550828 0.02319280 0.02130337 0.01974941
  1131 // 0.10000 0.05247504 0.04547452 0.03988045 0.03537016 0.03170171 0.02869272
  1132 // 0.15000 0.07135702 0.06111390 0.05296419 0.04641639 0.04110601 0.03676066
  1133 // 0.20000 0.08831616 0.07509618 0.06461766 0.05622444 0.04943437 0.04388975
  1134 // 0.25000 0.10311208 0.08724696 0.07471205 0.06469760 0.05661313 0.05002313
  1135 // 0.30000 0.11553050 0.09741183 0.08313394 0.07175114 0.06257797 0.05511132
  1136 // 0.35000 0.12538832 0.10545958 0.08978741 0.07731366 0.06727491 0.05911289
  1137 // 0.40000 0.13253818 0.11128511 0.09459590 0.08132834 0.07066107 0.06199500
  1138 // 0.45000 0.13687208 0.11481163 0.09750361 0.08375387 0.07270534 0.06373386
  1139 // 0.50000 0.13832410 0.11599237 0.09847664 0.08456518 0.07338887 0.06431510
  1140 // 0.55000 0.13687208 0.11481163 0.09750361 0.08375387 0.07270534 0.06373386
  1141 // 0.60000 0.13253818 0.11128511 0.09459590 0.08132834 0.07066107 0.06199500
  1142 // 0.65000 0.12538832 0.10545958 0.08978741 0.07731366 0.06727491 0.05911289
  1143 // 0.70000 0.11553050 0.09741183 0.08313394 0.07175114 0.06257797 0.05511132
  1144 // 0.75000 0.10311208 0.08724696 0.07471205 0.06469760 0.05661313 0.05002313
  1145 // 0.80000 0.08831616 0.07509618 0.06461766 0.05622444 0.04943437 0.04388975
  1146 // 0.85000 0.07135702 0.06111390 0.05296419 0.04641639 0.04110601 0.03676066
  1147 // 0.90000 0.05247504 0.04547452 0.03988045 0.03537016 0.03170171 0.02869272
  1148 // 0.95000 0.03193096 0.02836880 0.02550828 0.02319280 0.02130337 0.01974941
  1149 // 1.00000 0.01000000 0.01000000 0.01000000 0.01000000 0.01000000 0.01000000
  1151 double PSParallelCompact::dead_wood_limiter(double density, size_t min_percent)
  1153   assert(_dwl_initialized, "uninitialized");
  1155   // The raw limit is the value of the normal distribution at x = density.
  1156   const double raw_limit = normal_distribution(density);
  1158   // Adjust the raw limit so it becomes the minimum when the density is 1.
  1159   //
  1160   // First subtract the adjustment value (which is simply the precomputed value
  1161   // normal_distribution(1.0)); this yields a value of 0 when the density is 1.
  1162   // Then add the minimum value, so the minimum is returned when the density is
  1163   // 1.  Finally, prevent negative values, which occur when the mean is not 0.5.
  1164   const double min = double(min_percent) / 100.0;
  1165   const double limit = raw_limit - _dwl_adjustment + min;
  1166   return MAX2(limit, 0.0);
  1169 ParallelCompactData::ChunkData*
  1170 PSParallelCompact::first_dead_space_chunk(const ChunkData* beg,
  1171                                           const ChunkData* end)
  1173   const size_t chunk_size = ParallelCompactData::ChunkSize;
  1174   ParallelCompactData& sd = summary_data();
  1175   size_t left = sd.chunk(beg);
  1176   size_t right = end > beg ? sd.chunk(end) - 1 : left;
  1178   // Binary search.
  1179   while (left < right) {
  1180     // Equivalent to (left + right) / 2, but does not overflow.
  1181     const size_t middle = left + (right - left) / 2;
  1182     ChunkData* const middle_ptr = sd.chunk(middle);
  1183     HeapWord* const dest = middle_ptr->destination();
  1184     HeapWord* const addr = sd.chunk_to_addr(middle);
  1185     assert(dest != NULL, "sanity");
  1186     assert(dest <= addr, "must move left");
  1188     if (middle > left && dest < addr) {
  1189       right = middle - 1;
  1190     } else if (middle < right && middle_ptr->data_size() == chunk_size) {
  1191       left = middle + 1;
  1192     } else {
  1193       return middle_ptr;
  1196   return sd.chunk(left);
  1199 ParallelCompactData::ChunkData*
  1200 PSParallelCompact::dead_wood_limit_chunk(const ChunkData* beg,
  1201                                          const ChunkData* end,
  1202                                          size_t dead_words)
  1204   ParallelCompactData& sd = summary_data();
  1205   size_t left = sd.chunk(beg);
  1206   size_t right = end > beg ? sd.chunk(end) - 1 : left;
  1208   // Binary search.
  1209   while (left < right) {
  1210     // Equivalent to (left + right) / 2, but does not overflow.
  1211     const size_t middle = left + (right - left) / 2;
  1212     ChunkData* const middle_ptr = sd.chunk(middle);
  1213     HeapWord* const dest = middle_ptr->destination();
  1214     HeapWord* const addr = sd.chunk_to_addr(middle);
  1215     assert(dest != NULL, "sanity");
  1216     assert(dest <= addr, "must move left");
  1218     const size_t dead_to_left = pointer_delta(addr, dest);
  1219     if (middle > left && dead_to_left > dead_words) {
  1220       right = middle - 1;
  1221     } else if (middle < right && dead_to_left < dead_words) {
  1222       left = middle + 1;
  1223     } else {
  1224       return middle_ptr;
  1227   return sd.chunk(left);
  1230 // The result is valid during the summary phase, after the initial summarization
  1231 // of each space into itself, and before final summarization.
  1232 inline double
  1233 PSParallelCompact::reclaimed_ratio(const ChunkData* const cp,
  1234                                    HeapWord* const bottom,
  1235                                    HeapWord* const top,
  1236                                    HeapWord* const new_top)
  1238   ParallelCompactData& sd = summary_data();
  1240   assert(cp != NULL, "sanity");
  1241   assert(bottom != NULL, "sanity");
  1242   assert(top != NULL, "sanity");
  1243   assert(new_top != NULL, "sanity");
  1244   assert(top >= new_top, "summary data problem?");
  1245   assert(new_top > bottom, "space is empty; should not be here");
  1246   assert(new_top >= cp->destination(), "sanity");
  1247   assert(top >= sd.chunk_to_addr(cp), "sanity");
  1249   HeapWord* const destination = cp->destination();
  1250   const size_t dense_prefix_live  = pointer_delta(destination, bottom);
  1251   const size_t compacted_region_live = pointer_delta(new_top, destination);
  1252   const size_t compacted_region_used = pointer_delta(top, sd.chunk_to_addr(cp));
  1253   const size_t reclaimable = compacted_region_used - compacted_region_live;
  1255   const double divisor = dense_prefix_live + 1.25 * compacted_region_live;
  1256   return double(reclaimable) / divisor;
  1259 // Return the address of the end of the dense prefix, a.k.a. the start of the
  1260 // compacted region.  The address is always on a chunk boundary.
  1261 //
  1262 // Completely full chunks at the left are skipped, since no compaction can occur
  1263 // in those chunks.  Then the maximum amount of dead wood to allow is computed,
  1264 // based on the density (amount live / capacity) of the generation; the chunk
  1265 // with approximately that amount of dead space to the left is identified as the
  1266 // limit chunk.  Chunks between the last completely full chunk and the limit
  1267 // chunk are scanned and the one that has the best (maximum) reclaimed_ratio()
  1268 // is selected.
  1269 HeapWord*
  1270 PSParallelCompact::compute_dense_prefix(const SpaceId id,
  1271                                         bool maximum_compaction)
  1273   const size_t chunk_size = ParallelCompactData::ChunkSize;
  1274   const ParallelCompactData& sd = summary_data();
  1276   const MutableSpace* const space = _space_info[id].space();
  1277   HeapWord* const top = space->top();
  1278   HeapWord* const top_aligned_up = sd.chunk_align_up(top);
  1279   HeapWord* const new_top = _space_info[id].new_top();
  1280   HeapWord* const new_top_aligned_up = sd.chunk_align_up(new_top);
  1281   HeapWord* const bottom = space->bottom();
  1282   const ChunkData* const beg_cp = sd.addr_to_chunk_ptr(bottom);
  1283   const ChunkData* const top_cp = sd.addr_to_chunk_ptr(top_aligned_up);
  1284   const ChunkData* const new_top_cp = sd.addr_to_chunk_ptr(new_top_aligned_up);
  1286   // Skip full chunks at the beginning of the space--they are necessarily part
  1287   // of the dense prefix.
  1288   const ChunkData* const full_cp = first_dead_space_chunk(beg_cp, new_top_cp);
  1289   assert(full_cp->destination() == sd.chunk_to_addr(full_cp) ||
  1290          space->is_empty(), "no dead space allowed to the left");
  1291   assert(full_cp->data_size() < chunk_size || full_cp == new_top_cp - 1,
  1292          "chunk must have dead space");
  1294   // The gc number is saved whenever a maximum compaction is done, and used to
  1295   // determine when the maximum compaction interval has expired.  This avoids
  1296   // successive max compactions for different reasons.
  1297   assert(total_invocations() >= _maximum_compaction_gc_num, "sanity");
  1298   const size_t gcs_since_max = total_invocations() - _maximum_compaction_gc_num;
  1299   const bool interval_ended = gcs_since_max > HeapMaximumCompactionInterval ||
  1300     total_invocations() == HeapFirstMaximumCompactionCount;
  1301   if (maximum_compaction || full_cp == top_cp || interval_ended) {
  1302     _maximum_compaction_gc_num = total_invocations();
  1303     return sd.chunk_to_addr(full_cp);
  1306   const size_t space_live = pointer_delta(new_top, bottom);
  1307   const size_t space_used = space->used_in_words();
  1308   const size_t space_capacity = space->capacity_in_words();
  1310   const double density = double(space_live) / double(space_capacity);
  1311   const size_t min_percent_free =
  1312           id == perm_space_id ? PermMarkSweepDeadRatio : MarkSweepDeadRatio;
  1313   const double limiter = dead_wood_limiter(density, min_percent_free);
  1314   const size_t dead_wood_max = space_used - space_live;
  1315   const size_t dead_wood_limit = MIN2(size_t(space_capacity * limiter),
  1316                                       dead_wood_max);
  1318   if (TraceParallelOldGCDensePrefix) {
  1319     tty->print_cr("space_live=" SIZE_FORMAT " " "space_used=" SIZE_FORMAT " "
  1320                   "space_cap=" SIZE_FORMAT,
  1321                   space_live, space_used,
  1322                   space_capacity);
  1323     tty->print_cr("dead_wood_limiter(%6.4f, %d)=%6.4f "
  1324                   "dead_wood_max=" SIZE_FORMAT " dead_wood_limit=" SIZE_FORMAT,
  1325                   density, min_percent_free, limiter,
  1326                   dead_wood_max, dead_wood_limit);
  1329   // Locate the chunk with the desired amount of dead space to the left.
  1330   const ChunkData* const limit_cp =
  1331     dead_wood_limit_chunk(full_cp, top_cp, dead_wood_limit);
  1333   // Scan from the first chunk with dead space to the limit chunk and find the
  1334   // one with the best (largest) reclaimed ratio.
  1335   double best_ratio = 0.0;
  1336   const ChunkData* best_cp = full_cp;
  1337   for (const ChunkData* cp = full_cp; cp < limit_cp; ++cp) {
  1338     double tmp_ratio = reclaimed_ratio(cp, bottom, top, new_top);
  1339     if (tmp_ratio > best_ratio) {
  1340       best_cp = cp;
  1341       best_ratio = tmp_ratio;
  1345 #if     0
  1346   // Something to consider:  if the chunk with the best ratio is 'close to' the
  1347   // first chunk w/free space, choose the first chunk with free space
  1348   // ("first-free").  The first-free chunk is usually near the start of the
  1349   // heap, which means we are copying most of the heap already, so copy a bit
  1350   // more to get complete compaction.
  1351   if (pointer_delta(best_cp, full_cp, sizeof(ChunkData)) < 4) {
  1352     _maximum_compaction_gc_num = total_invocations();
  1353     best_cp = full_cp;
  1355 #endif  // #if 0
  1357   return sd.chunk_to_addr(best_cp);
  1360 void PSParallelCompact::summarize_spaces_quick()
  1362   for (unsigned int i = 0; i < last_space_id; ++i) {
  1363     const MutableSpace* space = _space_info[i].space();
  1364     bool result = _summary_data.summarize(space->bottom(), space->end(),
  1365                                           space->bottom(), space->top(),
  1366                                           _space_info[i].new_top_addr());
  1367     assert(result, "should never fail");
  1368     _space_info[i].set_dense_prefix(space->bottom());
  1372 void PSParallelCompact::fill_dense_prefix_end(SpaceId id)
  1374   HeapWord* const dense_prefix_end = dense_prefix(id);
  1375   const ChunkData* chunk = _summary_data.addr_to_chunk_ptr(dense_prefix_end);
  1376   const idx_t dense_prefix_bit = _mark_bitmap.addr_to_bit(dense_prefix_end);
  1377   if (dead_space_crosses_boundary(chunk, dense_prefix_bit)) {
  1378     // Only enough dead space is filled so that any remaining dead space to the
  1379     // left is larger than the minimum filler object.  (The remainder is filled
  1380     // during the copy/update phase.)
  1381     //
  1382     // The size of the dead space to the right of the boundary is not a
  1383     // concern, since compaction will be able to use whatever space is
  1384     // available.
  1385     //
  1386     // Here '||' is the boundary, 'x' represents a don't care bit and a box
  1387     // surrounds the space to be filled with an object.
  1388     //
  1389     // In the 32-bit VM, each bit represents two 32-bit words:
  1390     //                              +---+
  1391     // a) beg_bits:  ...  x   x   x | 0 | ||   0   x  x  ...
  1392     //    end_bits:  ...  x   x   x | 0 | ||   0   x  x  ...
  1393     //                              +---+
  1394     //
  1395     // In the 64-bit VM, each bit represents one 64-bit word:
  1396     //                              +------------+
  1397     // b) beg_bits:  ...  x   x   x | 0   ||   0 | x  x  ...
  1398     //    end_bits:  ...  x   x   1 | 0   ||   0 | x  x  ...
  1399     //                              +------------+
  1400     //                          +-------+
  1401     // c) beg_bits:  ...  x   x | 0   0 | ||   0   x  x  ...
  1402     //    end_bits:  ...  x   1 | 0   0 | ||   0   x  x  ...
  1403     //                          +-------+
  1404     //                      +-----------+
  1405     // d) beg_bits:  ...  x | 0   0   0 | ||   0   x  x  ...
  1406     //    end_bits:  ...  1 | 0   0   0 | ||   0   x  x  ...
  1407     //                      +-----------+
  1408     //                          +-------+
  1409     // e) beg_bits:  ...  0   0 | 0   0 | ||   0   x  x  ...
  1410     //    end_bits:  ...  0   0 | 0   0 | ||   0   x  x  ...
  1411     //                          +-------+
  1413     // Initially assume case a, c or e will apply.
  1414     size_t obj_len = (size_t)oopDesc::header_size();
  1415     HeapWord* obj_beg = dense_prefix_end - obj_len;
  1417 #ifdef  _LP64
  1418     if (_mark_bitmap.is_obj_end(dense_prefix_bit - 2)) {
  1419       // Case b above.
  1420       obj_beg = dense_prefix_end - 1;
  1421     } else if (!_mark_bitmap.is_obj_end(dense_prefix_bit - 3) &&
  1422                _mark_bitmap.is_obj_end(dense_prefix_bit - 4)) {
  1423       // Case d above.
  1424       obj_beg = dense_prefix_end - 3;
  1425       obj_len = 3;
  1427 #endif  // #ifdef _LP64
  1429     MemRegion region(obj_beg, obj_len);
  1430     SharedHeap::fill_region_with_object(region);
  1431     _mark_bitmap.mark_obj(obj_beg, obj_len);
  1432     _summary_data.add_obj(obj_beg, obj_len);
  1433     assert(start_array(id) != NULL, "sanity");
  1434     start_array(id)->allocate_block(obj_beg);
  1438 void
  1439 PSParallelCompact::summarize_space(SpaceId id, bool maximum_compaction)
  1441   assert(id < last_space_id, "id out of range");
  1442   assert(_space_info[id].dense_prefix() == _space_info[id].space()->bottom(),
  1443          "should have been set in summarize_spaces_quick()");
  1445   const MutableSpace* space = _space_info[id].space();
  1446   if (_space_info[id].new_top() != space->bottom()) {
  1447     HeapWord* dense_prefix_end = compute_dense_prefix(id, maximum_compaction);
  1448     _space_info[id].set_dense_prefix(dense_prefix_end);
  1450 #ifndef PRODUCT
  1451     if (TraceParallelOldGCDensePrefix) {
  1452       print_dense_prefix_stats("ratio", id, maximum_compaction,
  1453                                dense_prefix_end);
  1454       HeapWord* addr = compute_dense_prefix_via_density(id, maximum_compaction);
  1455       print_dense_prefix_stats("density", id, maximum_compaction, addr);
  1457 #endif  // #ifndef PRODUCT
  1459     // If dead space crosses the dense prefix boundary, it is (at least
  1460     // partially) filled with a dummy object, marked live and added to the
  1461     // summary data.  This simplifies the copy/update phase and must be done
  1462     // before the final locations of objects are determined, to prevent leaving
  1463     // a fragment of dead space that is too small to fill with an object.
  1464     if (!maximum_compaction && dense_prefix_end != space->bottom()) {
  1465       fill_dense_prefix_end(id);
  1468     // Compute the destination of each Chunk, and thus each object.
  1469     _summary_data.summarize_dense_prefix(space->bottom(), dense_prefix_end);
  1470     _summary_data.summarize(dense_prefix_end, space->end(),
  1471                             dense_prefix_end, space->top(),
  1472                             _space_info[id].new_top_addr());
  1475   if (TraceParallelOldGCSummaryPhase) {
  1476     const size_t chunk_size = ParallelCompactData::ChunkSize;
  1477     HeapWord* const dense_prefix_end = _space_info[id].dense_prefix();
  1478     const size_t dp_chunk = _summary_data.addr_to_chunk_idx(dense_prefix_end);
  1479     const size_t dp_words = pointer_delta(dense_prefix_end, space->bottom());
  1480     HeapWord* const new_top = _space_info[id].new_top();
  1481     const HeapWord* nt_aligned_up = _summary_data.chunk_align_up(new_top);
  1482     const size_t cr_words = pointer_delta(nt_aligned_up, dense_prefix_end);
  1483     tty->print_cr("id=%d cap=" SIZE_FORMAT " dp=" PTR_FORMAT " "
  1484                   "dp_chunk=" SIZE_FORMAT " " "dp_count=" SIZE_FORMAT " "
  1485                   "cr_count=" SIZE_FORMAT " " "nt=" PTR_FORMAT,
  1486                   id, space->capacity_in_words(), dense_prefix_end,
  1487                   dp_chunk, dp_words / chunk_size,
  1488                   cr_words / chunk_size, new_top);
  1492 void PSParallelCompact::summary_phase(ParCompactionManager* cm,
  1493                                       bool maximum_compaction)
  1495   EventMark m("2 summarize");
  1496   TraceTime tm("summary phase", print_phases(), true, gclog_or_tty);
  1497   // trace("2");
  1499 #ifdef  ASSERT
  1500   if (TraceParallelOldGCMarkingPhase) {
  1501     tty->print_cr("add_obj_count=" SIZE_FORMAT " "
  1502                   "add_obj_bytes=" SIZE_FORMAT,
  1503                   add_obj_count, add_obj_size * HeapWordSize);
  1504     tty->print_cr("mark_bitmap_count=" SIZE_FORMAT " "
  1505                   "mark_bitmap_bytes=" SIZE_FORMAT,
  1506                   mark_bitmap_count, mark_bitmap_size * HeapWordSize);
  1508 #endif  // #ifdef ASSERT
  1510   // Quick summarization of each space into itself, to see how much is live.
  1511   summarize_spaces_quick();
  1513   if (TraceParallelOldGCSummaryPhase) {
  1514     tty->print_cr("summary_phase:  after summarizing each space to self");
  1515     Universe::print();
  1516     NOT_PRODUCT(print_chunk_ranges());
  1517     if (Verbose) {
  1518       NOT_PRODUCT(print_initial_summary_data(_summary_data, _space_info));
  1522   // The amount of live data that will end up in old space (assuming it fits).
  1523   size_t old_space_total_live = 0;
  1524   unsigned int id;
  1525   for (id = old_space_id; id < last_space_id; ++id) {
  1526     old_space_total_live += pointer_delta(_space_info[id].new_top(),
  1527                                           _space_info[id].space()->bottom());
  1530   const MutableSpace* old_space = _space_info[old_space_id].space();
  1531   if (old_space_total_live > old_space->capacity_in_words()) {
  1532     // XXX - should also try to expand
  1533     maximum_compaction = true;
  1534   } else if (!UseParallelOldGCDensePrefix) {
  1535     maximum_compaction = true;
  1538   // Permanent and Old generations.
  1539   summarize_space(perm_space_id, maximum_compaction);
  1540   summarize_space(old_space_id, maximum_compaction);
  1542   // Summarize the remaining spaces (those in the young gen) into old space.  If
  1543   // the live data from a space doesn't fit, the existing summarization is left
  1544   // intact, so the data is compacted down within the space itself.
  1545   HeapWord** new_top_addr = _space_info[old_space_id].new_top_addr();
  1546   HeapWord* const target_space_end = old_space->end();
  1547   for (id = eden_space_id; id < last_space_id; ++id) {
  1548     const MutableSpace* space = _space_info[id].space();
  1549     const size_t live = pointer_delta(_space_info[id].new_top(),
  1550                                       space->bottom());
  1551     const size_t available = pointer_delta(target_space_end, *new_top_addr);
  1552     if (live > 0 && live <= available) {
  1553       // All the live data will fit.
  1554       if (TraceParallelOldGCSummaryPhase) {
  1555         tty->print_cr("summarizing %d into old_space @ " PTR_FORMAT,
  1556                       id, *new_top_addr);
  1558       _summary_data.summarize(*new_top_addr, target_space_end,
  1559                               space->bottom(), space->top(),
  1560                               new_top_addr);
  1562       // Clear the source_chunk field for each chunk in the space.
  1563       HeapWord* const new_top = _space_info[id].new_top();
  1564       HeapWord* const clear_end = _summary_data.chunk_align_up(new_top);
  1565       ChunkData* beg_chunk = _summary_data.addr_to_chunk_ptr(space->bottom());
  1566       ChunkData* end_chunk = _summary_data.addr_to_chunk_ptr(clear_end);
  1567       while (beg_chunk < end_chunk) {
  1568         beg_chunk->set_source_chunk(0);
  1569         ++beg_chunk;
  1572       // Reset the new_top value for the space.
  1573       _space_info[id].set_new_top(space->bottom());
  1577   // Fill in the block data after any changes to the chunks have
  1578   // been made.
  1579 #ifdef  ASSERT
  1580   summarize_blocks(cm, perm_space_id);
  1581   summarize_blocks(cm, old_space_id);
  1582 #else
  1583   if (!UseParallelOldGCChunkPointerCalc) {
  1584     summarize_blocks(cm, perm_space_id);
  1585     summarize_blocks(cm, old_space_id);
  1587 #endif
  1589   if (TraceParallelOldGCSummaryPhase) {
  1590     tty->print_cr("summary_phase:  after final summarization");
  1591     Universe::print();
  1592     NOT_PRODUCT(print_chunk_ranges());
  1593     if (Verbose) {
  1594       NOT_PRODUCT(print_generic_summary_data(_summary_data, _space_info));
  1599 // Fill in the BlockData.
  1600 // Iterate over the spaces and within each space iterate over
  1601 // the chunks and fill in the BlockData for each chunk.
  1603 void PSParallelCompact::summarize_blocks(ParCompactionManager* cm,
  1604                                          SpaceId first_compaction_space_id) {
  1605 #if     0
  1606   DEBUG_ONLY(ParallelCompactData::BlockData::set_cur_phase(1);)
  1607   for (SpaceId cur_space_id = first_compaction_space_id;
  1608        cur_space_id != last_space_id;
  1609        cur_space_id = next_compaction_space_id(cur_space_id)) {
  1610     // Iterate over the chunks in the space
  1611     size_t start_chunk_index =
  1612       _summary_data.addr_to_chunk_idx(space(cur_space_id)->bottom());
  1613     BitBlockUpdateClosure bbu(mark_bitmap(),
  1614                               cm,
  1615                               start_chunk_index);
  1616     // Iterate over blocks.
  1617     for (size_t chunk_index =  start_chunk_index;
  1618          chunk_index < _summary_data.chunk_count() &&
  1619          _summary_data.chunk_to_addr(chunk_index) < space(cur_space_id)->top();
  1620          chunk_index++) {
  1622       // Reset the closure for the new chunk.  Note that the closure
  1623       // maintains some data that does not get reset for each chunk
  1624       // so a new instance of the closure is no appropriate.
  1625       bbu.reset_chunk(chunk_index);
  1627       // Start the iteration with the first live object.  This
  1628       // may return the end of the chunk.  That is acceptable since
  1629       // it will properly limit the iterations.
  1630       ParMarkBitMap::idx_t left_offset = mark_bitmap()->addr_to_bit(
  1631         _summary_data.first_live_or_end_in_chunk(chunk_index));
  1633       // End the iteration at the end of the chunk.
  1634       HeapWord* chunk_addr = _summary_data.chunk_to_addr(chunk_index);
  1635       HeapWord* chunk_end = chunk_addr + ParallelCompactData::ChunkSize;
  1636       ParMarkBitMap::idx_t right_offset =
  1637         mark_bitmap()->addr_to_bit(chunk_end);
  1639       // Blocks that have not objects starting in them can be
  1640       // skipped because their data will never be used.
  1641       if (left_offset < right_offset) {
  1643         // Iterate through the objects in the chunk.
  1644         ParMarkBitMap::idx_t last_offset =
  1645           mark_bitmap()->pair_iterate(&bbu, left_offset, right_offset);
  1647         // If last_offset is less than right_offset, then the iterations
  1648         // terminated while it was looking for an end bit.  "last_offset"
  1649         // is then the offset for the last start bit.  In this situation
  1650         // the "offset" field for the next block to the right (_cur_block + 1)
  1651         // will not have been update although there may be live data
  1652         // to the left of the chunk.
  1654         size_t cur_block_plus_1 = bbu.cur_block() + 1;
  1655         HeapWord* cur_block_plus_1_addr =
  1656         _summary_data.block_to_addr(bbu.cur_block()) +
  1657         ParallelCompactData::BlockSize;
  1658         HeapWord* last_offset_addr = mark_bitmap()->bit_to_addr(last_offset);
  1659  #if 1  // This code works.  The else doesn't but should.  Why does it?
  1660         // The current block (cur_block()) has already been updated.
  1661         // The last block that may need to be updated is either the
  1662         // next block (current block + 1) or the block where the
  1663         // last object starts (which can be greater than the
  1664         // next block if there were no objects found in intervening
  1665         // blocks).
  1666         size_t last_block =
  1667           MAX2(bbu.cur_block() + 1,
  1668                _summary_data.addr_to_block_idx(last_offset_addr));
  1669  #else
  1670         // The current block has already been updated.  The only block
  1671         // that remains to be updated is the block where the last
  1672         // object in the chunk starts.
  1673         size_t last_block = _summary_data.addr_to_block_idx(last_offset_addr);
  1674  #endif
  1675         assert_bit_is_start(last_offset);
  1676         assert((last_block == _summary_data.block_count()) ||
  1677              (_summary_data.block(last_block)->raw_offset() == 0),
  1678           "Should not have been set");
  1679         // Is the last block still in the current chunk?  If still
  1680         // in this chunk, update the last block (the counting that
  1681         // included the current block is meant for the offset of the last
  1682         // block).  If not in this chunk, do nothing.  Should not
  1683         // update a block in the next chunk.
  1684         if (ParallelCompactData::chunk_contains_block(bbu.chunk_index(),
  1685                                                       last_block)) {
  1686           if (last_offset < right_offset) {
  1687             // The last object started in this chunk but ends beyond
  1688             // this chunk.  Update the block for this last object.
  1689             assert(mark_bitmap()->is_marked(last_offset), "Should be marked");
  1690             // No end bit was found.  The closure takes care of
  1691             // the cases where
  1692             //   an objects crosses over into the next block
  1693             //   an objects starts and ends in the next block
  1694             // It does not handle the case where an object is
  1695             // the first object in a later block and extends
  1696             // past the end of the chunk (i.e., the closure
  1697             // only handles complete objects that are in the range
  1698             // it is given).  That object is handed back here
  1699             // for any special consideration necessary.
  1700             //
  1701             // Is the first bit in the last block a start or end bit?
  1702             //
  1703             // If the partial object ends in the last block L,
  1704             // then the 1st bit in L may be an end bit.
  1705             //
  1706             // Else does the last object start in a block after the current
  1707             // block? A block AA will already have been updated if an
  1708             // object ends in the next block AA+1.  An object found to end in
  1709             // the AA+1 is the trigger that updates AA.  Objects are being
  1710             // counted in the current block for updaing a following
  1711             // block.  An object may start in later block
  1712             // block but may extend beyond the last block in the chunk.
  1713             // Updates are only done when the end of an object has been
  1714             // found. If the last object (covered by block L) starts
  1715             // beyond the current block, then no object ends in L (otherwise
  1716             // L would be the current block).  So the first bit in L is
  1717             // a start bit.
  1718             //
  1719             // Else the last objects start in the current block and ends
  1720             // beyond the chunk.  The current block has already been
  1721             // updated and there is no later block (with an object
  1722             // starting in it) that needs to be updated.
  1723             //
  1724             if (_summary_data.partial_obj_ends_in_block(last_block)) {
  1725               _summary_data.block(last_block)->set_end_bit_offset(
  1726                 bbu.live_data_left());
  1727             } else if (last_offset_addr >= cur_block_plus_1_addr) {
  1728               //   The start of the object is on a later block
  1729               // (to the right of the current block and there are no
  1730               // complete live objects to the left of this last object
  1731               // within the chunk.
  1732               //   The first bit in the block is for the start of the
  1733               // last object.
  1734               _summary_data.block(last_block)->set_start_bit_offset(
  1735                 bbu.live_data_left());
  1736             } else {
  1737               //   The start of the last object was found in
  1738               // the current chunk (which has already
  1739               // been updated).
  1740               assert(bbu.cur_block() ==
  1741                       _summary_data.addr_to_block_idx(last_offset_addr),
  1742                 "Should be a block already processed");
  1744 #ifdef ASSERT
  1745             // Is there enough block information to find this object?
  1746             // The destination of the chunk has not been set so the
  1747             // values returned by calc_new_pointer() and
  1748             // block_calc_new_pointer() will only be
  1749             // offsets.  But they should agree.
  1750             HeapWord* moved_obj_with_chunks =
  1751               _summary_data.chunk_calc_new_pointer(last_offset_addr);
  1752             HeapWord* moved_obj_with_blocks =
  1753               _summary_data.calc_new_pointer(last_offset_addr);
  1754             assert(moved_obj_with_chunks == moved_obj_with_blocks,
  1755               "Block calculation is wrong");
  1756 #endif
  1757           } else if (last_block < _summary_data.block_count()) {
  1758             // Iterations ended looking for a start bit (but
  1759             // did not run off the end of the block table).
  1760             _summary_data.block(last_block)->set_start_bit_offset(
  1761               bbu.live_data_left());
  1764 #ifdef ASSERT
  1765         // Is there enough block information to find this object?
  1766           HeapWord* left_offset_addr = mark_bitmap()->bit_to_addr(left_offset);
  1767         HeapWord* moved_obj_with_chunks =
  1768           _summary_data.calc_new_pointer(left_offset_addr);
  1769         HeapWord* moved_obj_with_blocks =
  1770           _summary_data.calc_new_pointer(left_offset_addr);
  1771           assert(moved_obj_with_chunks == moved_obj_with_blocks,
  1772           "Block calculation is wrong");
  1773 #endif
  1775         // Is there another block after the end of this chunk?
  1776 #ifdef ASSERT
  1777         if (last_block < _summary_data.block_count()) {
  1778         // No object may have been found in a block.  If that
  1779         // block is at the end of the chunk, the iteration will
  1780         // terminate without incrementing the current block so
  1781         // that the current block is not the last block in the
  1782         // chunk.  That situation precludes asserting that the
  1783         // current block is the last block in the chunk.  Assert
  1784         // the lesser condition that the current block does not
  1785         // exceed the chunk.
  1786           assert(_summary_data.block_to_addr(last_block) <=
  1787                (_summary_data.chunk_to_addr(chunk_index) +
  1788                  ParallelCompactData::ChunkSize),
  1789               "Chunk and block inconsistency");
  1790           assert(last_offset <= right_offset, "Iteration over ran end");
  1792 #endif
  1794 #ifdef ASSERT
  1795       if (PrintGCDetails && Verbose) {
  1796         if (_summary_data.chunk(chunk_index)->partial_obj_size() == 1) {
  1797           size_t first_block =
  1798             chunk_index / ParallelCompactData::BlocksPerChunk;
  1799           gclog_or_tty->print_cr("first_block " PTR_FORMAT
  1800             " _offset " PTR_FORMAT
  1801             "_first_is_start_bit %d",
  1802             first_block,
  1803             _summary_data.block(first_block)->raw_offset(),
  1804             _summary_data.block(first_block)->first_is_start_bit());
  1807 #endif
  1810   DEBUG_ONLY(ParallelCompactData::BlockData::set_cur_phase(16);)
  1811 #endif  // #if 0
  1814 // This method should contain all heap-specific policy for invoking a full
  1815 // collection.  invoke_no_policy() will only attempt to compact the heap; it
  1816 // will do nothing further.  If we need to bail out for policy reasons, scavenge
  1817 // before full gc, or any other specialized behavior, it needs to be added here.
  1818 //
  1819 // Note that this method should only be called from the vm_thread while at a
  1820 // safepoint.
  1821 void PSParallelCompact::invoke(bool maximum_heap_compaction) {
  1822   assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
  1823   assert(Thread::current() == (Thread*)VMThread::vm_thread(),
  1824          "should be in vm thread");
  1825   ParallelScavengeHeap* heap = gc_heap();
  1826   GCCause::Cause gc_cause = heap->gc_cause();
  1827   assert(!heap->is_gc_active(), "not reentrant");
  1829   PSAdaptiveSizePolicy* policy = heap->size_policy();
  1831   // Before each allocation/collection attempt, find out from the
  1832   // policy object if GCs are, on the whole, taking too long. If so,
  1833   // bail out without attempting a collection.  The exceptions are
  1834   // for explicitly requested GC's.
  1835   if (!policy->gc_time_limit_exceeded() ||
  1836       GCCause::is_user_requested_gc(gc_cause) ||
  1837       GCCause::is_serviceability_requested_gc(gc_cause)) {
  1838     IsGCActiveMark mark;
  1840     if (ScavengeBeforeFullGC) {
  1841       PSScavenge::invoke_no_policy();
  1844     PSParallelCompact::invoke_no_policy(maximum_heap_compaction);
  1848 bool ParallelCompactData::chunk_contains(size_t chunk_index, HeapWord* addr) {
  1849   size_t addr_chunk_index = addr_to_chunk_idx(addr);
  1850   return chunk_index == addr_chunk_index;
  1853 bool ParallelCompactData::chunk_contains_block(size_t chunk_index,
  1854                                                size_t block_index) {
  1855   size_t first_block_in_chunk = chunk_index * BlocksPerChunk;
  1856   size_t last_block_in_chunk = (chunk_index + 1) * BlocksPerChunk - 1;
  1858   return (first_block_in_chunk <= block_index) &&
  1859          (block_index <= last_block_in_chunk);
  1862 // This method contains no policy. You should probably
  1863 // be calling invoke() instead.
  1864 void PSParallelCompact::invoke_no_policy(bool maximum_heap_compaction) {
  1865   assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
  1866   assert(ref_processor() != NULL, "Sanity");
  1868   if (GC_locker::check_active_before_gc()) {
  1869     return;
  1872   TimeStamp marking_start;
  1873   TimeStamp compaction_start;
  1874   TimeStamp collection_exit;
  1876   ParallelScavengeHeap* heap = gc_heap();
  1877   GCCause::Cause gc_cause = heap->gc_cause();
  1878   PSYoungGen* young_gen = heap->young_gen();
  1879   PSOldGen* old_gen = heap->old_gen();
  1880   PSPermGen* perm_gen = heap->perm_gen();
  1881   PSAdaptiveSizePolicy* size_policy = heap->size_policy();
  1883   if (ZapUnusedHeapArea) {
  1884     // Save information needed to minimize mangling
  1885     heap->record_gen_tops_before_GC();
  1888   _print_phases = PrintGCDetails && PrintParallelOldGCPhaseTimes;
  1890   // Make sure data structures are sane, make the heap parsable, and do other
  1891   // miscellaneous bookkeeping.
  1892   PreGCValues pre_gc_values;
  1893   pre_compact(&pre_gc_values);
  1895   // Get the compaction manager reserved for the VM thread.
  1896   ParCompactionManager* const vmthread_cm =
  1897     ParCompactionManager::manager_array(gc_task_manager()->workers());
  1899   // Place after pre_compact() where the number of invocations is incremented.
  1900   AdaptiveSizePolicyOutput(size_policy, heap->total_collections());
  1903     ResourceMark rm;
  1904     HandleMark hm;
  1906     const bool is_system_gc = gc_cause == GCCause::_java_lang_system_gc;
  1908     // This is useful for debugging but don't change the output the
  1909     // the customer sees.
  1910     const char* gc_cause_str = "Full GC";
  1911     if (is_system_gc && PrintGCDetails) {
  1912       gc_cause_str = "Full GC (System)";
  1914     gclog_or_tty->date_stamp(PrintGC && PrintGCDateStamps);
  1915     TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);
  1916     TraceTime t1(gc_cause_str, PrintGC, !PrintGCDetails, gclog_or_tty);
  1917     TraceCollectorStats tcs(counters());
  1918     TraceMemoryManagerStats tms(true /* Full GC */);
  1920     if (TraceGen1Time) accumulated_time()->start();
  1922     // Let the size policy know we're starting
  1923     size_policy->major_collection_begin();
  1925     // When collecting the permanent generation methodOops may be moving,
  1926     // so we either have to flush all bcp data or convert it into bci.
  1927     CodeCache::gc_prologue();
  1928     Threads::gc_prologue();
  1930     NOT_PRODUCT(ref_processor()->verify_no_references_recorded());
  1931     COMPILER2_PRESENT(DerivedPointerTable::clear());
  1933     ref_processor()->enable_discovery();
  1935     bool marked_for_unloading = false;
  1937     marking_start.update();
  1938     marking_phase(vmthread_cm, maximum_heap_compaction);
  1940 #ifndef PRODUCT
  1941     if (TraceParallelOldGCMarkingPhase) {
  1942       gclog_or_tty->print_cr("marking_phase: cas_tries %d  cas_retries %d "
  1943         "cas_by_another %d",
  1944         mark_bitmap()->cas_tries(), mark_bitmap()->cas_retries(),
  1945         mark_bitmap()->cas_by_another());
  1947 #endif  // #ifndef PRODUCT
  1949     bool max_on_system_gc = UseMaximumCompactionOnSystemGC && is_system_gc;
  1950     summary_phase(vmthread_cm, maximum_heap_compaction || max_on_system_gc);
  1952     COMPILER2_PRESENT(assert(DerivedPointerTable::is_active(), "Sanity"));
  1953     COMPILER2_PRESENT(DerivedPointerTable::set_active(false));
  1955     // adjust_roots() updates Universe::_intArrayKlassObj which is
  1956     // needed by the compaction for filling holes in the dense prefix.
  1957     adjust_roots();
  1959     compaction_start.update();
  1960     // Does the perm gen always have to be done serially because
  1961     // klasses are used in the update of an object?
  1962     compact_perm(vmthread_cm);
  1964     if (UseParallelOldGCCompacting) {
  1965       compact();
  1966     } else {
  1967       compact_serial(vmthread_cm);
  1970     // Reset the mark bitmap, summary data, and do other bookkeeping.  Must be
  1971     // done before resizing.
  1972     post_compact();
  1974     // Let the size policy know we're done
  1975     size_policy->major_collection_end(old_gen->used_in_bytes(), gc_cause);
  1977     if (UseAdaptiveSizePolicy) {
  1978       if (PrintAdaptiveSizePolicy) {
  1979         gclog_or_tty->print("AdaptiveSizeStart: ");
  1980         gclog_or_tty->stamp();
  1981         gclog_or_tty->print_cr(" collection: %d ",
  1982                        heap->total_collections());
  1983         if (Verbose) {
  1984           gclog_or_tty->print("old_gen_capacity: %d young_gen_capacity: %d"
  1985             " perm_gen_capacity: %d ",
  1986             old_gen->capacity_in_bytes(), young_gen->capacity_in_bytes(),
  1987             perm_gen->capacity_in_bytes());
  1991       // Don't check if the size_policy is ready here.  Let
  1992       // the size_policy check that internally.
  1993       if (UseAdaptiveGenerationSizePolicyAtMajorCollection &&
  1994           ((gc_cause != GCCause::_java_lang_system_gc) ||
  1995             UseAdaptiveSizePolicyWithSystemGC)) {
  1996         // Calculate optimal free space amounts
  1997         assert(young_gen->max_size() >
  1998           young_gen->from_space()->capacity_in_bytes() +
  1999           young_gen->to_space()->capacity_in_bytes(),
  2000           "Sizes of space in young gen are out-of-bounds");
  2001         size_t max_eden_size = young_gen->max_size() -
  2002           young_gen->from_space()->capacity_in_bytes() -
  2003           young_gen->to_space()->capacity_in_bytes();
  2004         size_policy->compute_generation_free_space(
  2005                               young_gen->used_in_bytes(),
  2006                               young_gen->eden_space()->used_in_bytes(),
  2007                               old_gen->used_in_bytes(),
  2008                               perm_gen->used_in_bytes(),
  2009                               young_gen->eden_space()->capacity_in_bytes(),
  2010                               old_gen->max_gen_size(),
  2011                               max_eden_size,
  2012                               true /* full gc*/,
  2013                               gc_cause);
  2015         heap->resize_old_gen(
  2016           size_policy->calculated_old_free_size_in_bytes());
  2018         // Don't resize the young generation at an major collection.  A
  2019         // desired young generation size may have been calculated but
  2020         // resizing the young generation complicates the code because the
  2021         // resizing of the old generation may have moved the boundary
  2022         // between the young generation and the old generation.  Let the
  2023         // young generation resizing happen at the minor collections.
  2025       if (PrintAdaptiveSizePolicy) {
  2026         gclog_or_tty->print_cr("AdaptiveSizeStop: collection: %d ",
  2027                        heap->total_collections());
  2031     if (UsePerfData) {
  2032       PSGCAdaptivePolicyCounters* const counters = heap->gc_policy_counters();
  2033       counters->update_counters();
  2034       counters->update_old_capacity(old_gen->capacity_in_bytes());
  2035       counters->update_young_capacity(young_gen->capacity_in_bytes());
  2038     heap->resize_all_tlabs();
  2040     // We collected the perm gen, so we'll resize it here.
  2041     perm_gen->compute_new_size(pre_gc_values.perm_gen_used());
  2043     if (TraceGen1Time) accumulated_time()->stop();
  2045     if (PrintGC) {
  2046       if (PrintGCDetails) {
  2047         // No GC timestamp here.  This is after GC so it would be confusing.
  2048         young_gen->print_used_change(pre_gc_values.young_gen_used());
  2049         old_gen->print_used_change(pre_gc_values.old_gen_used());
  2050         heap->print_heap_change(pre_gc_values.heap_used());
  2051         // Print perm gen last (print_heap_change() excludes the perm gen).
  2052         perm_gen->print_used_change(pre_gc_values.perm_gen_used());
  2053       } else {
  2054         heap->print_heap_change(pre_gc_values.heap_used());
  2058     // Track memory usage and detect low memory
  2059     MemoryService::track_memory_usage();
  2060     heap->update_counters();
  2062     if (PrintGCDetails) {
  2063       if (size_policy->print_gc_time_limit_would_be_exceeded()) {
  2064         if (size_policy->gc_time_limit_exceeded()) {
  2065           gclog_or_tty->print_cr("      GC time is exceeding GCTimeLimit "
  2066             "of %d%%", GCTimeLimit);
  2067         } else {
  2068           gclog_or_tty->print_cr("      GC time would exceed GCTimeLimit "
  2069             "of %d%%", GCTimeLimit);
  2072       size_policy->set_print_gc_time_limit_would_be_exceeded(false);
  2076   if (VerifyAfterGC && heap->total_collections() >= VerifyGCStartAt) {
  2077     HandleMark hm;  // Discard invalid handles created during verification
  2078     gclog_or_tty->print(" VerifyAfterGC:");
  2079     Universe::verify(false);
  2082   // Re-verify object start arrays
  2083   if (VerifyObjectStartArray &&
  2084       VerifyAfterGC) {
  2085     old_gen->verify_object_start_array();
  2086     perm_gen->verify_object_start_array();
  2089   if (ZapUnusedHeapArea) {
  2090     old_gen->object_space()->check_mangled_unused_area_complete();
  2091     perm_gen->object_space()->check_mangled_unused_area_complete();
  2094   NOT_PRODUCT(ref_processor()->verify_no_references_recorded());
  2096   collection_exit.update();
  2098   if (PrintHeapAtGC) {
  2099     Universe::print_heap_after_gc();
  2101   if (PrintGCTaskTimeStamps) {
  2102     gclog_or_tty->print_cr("VM-Thread " INT64_FORMAT " " INT64_FORMAT " "
  2103                            INT64_FORMAT,
  2104                            marking_start.ticks(), compaction_start.ticks(),
  2105                            collection_exit.ticks());
  2106     gc_task_manager()->print_task_time_stamps();
  2110 bool PSParallelCompact::absorb_live_data_from_eden(PSAdaptiveSizePolicy* size_policy,
  2111                                              PSYoungGen* young_gen,
  2112                                              PSOldGen* old_gen) {
  2113   MutableSpace* const eden_space = young_gen->eden_space();
  2114   assert(!eden_space->is_empty(), "eden must be non-empty");
  2115   assert(young_gen->virtual_space()->alignment() ==
  2116          old_gen->virtual_space()->alignment(), "alignments do not match");
  2118   if (!(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary)) {
  2119     return false;
  2122   // Both generations must be completely committed.
  2123   if (young_gen->virtual_space()->uncommitted_size() != 0) {
  2124     return false;
  2126   if (old_gen->virtual_space()->uncommitted_size() != 0) {
  2127     return false;
  2130   // Figure out how much to take from eden.  Include the average amount promoted
  2131   // in the total; otherwise the next young gen GC will simply bail out to a
  2132   // full GC.
  2133   const size_t alignment = old_gen->virtual_space()->alignment();
  2134   const size_t eden_used = eden_space->used_in_bytes();
  2135   const size_t promoted = (size_t)size_policy->avg_promoted()->padded_average();
  2136   const size_t absorb_size = align_size_up(eden_used + promoted, alignment);
  2137   const size_t eden_capacity = eden_space->capacity_in_bytes();
  2139   if (absorb_size >= eden_capacity) {
  2140     return false; // Must leave some space in eden.
  2143   const size_t new_young_size = young_gen->capacity_in_bytes() - absorb_size;
  2144   if (new_young_size < young_gen->min_gen_size()) {
  2145     return false; // Respect young gen minimum size.
  2148   if (TraceAdaptiveGCBoundary && Verbose) {
  2149     gclog_or_tty->print(" absorbing " SIZE_FORMAT "K:  "
  2150                         "eden " SIZE_FORMAT "K->" SIZE_FORMAT "K "
  2151                         "from " SIZE_FORMAT "K, to " SIZE_FORMAT "K "
  2152                         "young_gen " SIZE_FORMAT "K->" SIZE_FORMAT "K ",
  2153                         absorb_size / K,
  2154                         eden_capacity / K, (eden_capacity - absorb_size) / K,
  2155                         young_gen->from_space()->used_in_bytes() / K,
  2156                         young_gen->to_space()->used_in_bytes() / K,
  2157                         young_gen->capacity_in_bytes() / K, new_young_size / K);
  2160   // Fill the unused part of the old gen.
  2161   MutableSpace* const old_space = old_gen->object_space();
  2162   MemRegion old_gen_unused(old_space->top(), old_space->end());
  2163   if (!old_gen_unused.is_empty()) {
  2164     SharedHeap::fill_region_with_object(old_gen_unused);
  2167   // Take the live data from eden and set both top and end in the old gen to
  2168   // eden top.  (Need to set end because reset_after_change() mangles the region
  2169   // from end to virtual_space->high() in debug builds).
  2170   HeapWord* const new_top = eden_space->top();
  2171   old_gen->virtual_space()->expand_into(young_gen->virtual_space(),
  2172                                         absorb_size);
  2173   young_gen->reset_after_change();
  2174   old_space->set_top(new_top);
  2175   old_space->set_end(new_top);
  2176   old_gen->reset_after_change();
  2178   // Update the object start array for the filler object and the data from eden.
  2179   ObjectStartArray* const start_array = old_gen->start_array();
  2180   HeapWord* const start = old_gen_unused.start();
  2181   for (HeapWord* addr = start; addr < new_top; addr += oop(addr)->size()) {
  2182     start_array->allocate_block(addr);
  2185   // Could update the promoted average here, but it is not typically updated at
  2186   // full GCs and the value to use is unclear.  Something like
  2187   //
  2188   // cur_promoted_avg + absorb_size / number_of_scavenges_since_last_full_gc.
  2190   size_policy->set_bytes_absorbed_from_eden(absorb_size);
  2191   return true;
  2194 GCTaskManager* const PSParallelCompact::gc_task_manager() {
  2195   assert(ParallelScavengeHeap::gc_task_manager() != NULL,
  2196     "shouldn't return NULL");
  2197   return ParallelScavengeHeap::gc_task_manager();
  2200 void PSParallelCompact::marking_phase(ParCompactionManager* cm,
  2201                                       bool maximum_heap_compaction) {
  2202   // Recursively traverse all live objects and mark them
  2203   EventMark m("1 mark object");
  2204   TraceTime tm("marking phase", print_phases(), true, gclog_or_tty);
  2206   ParallelScavengeHeap* heap = gc_heap();
  2207   uint parallel_gc_threads = heap->gc_task_manager()->workers();
  2208   TaskQueueSetSuper* qset = ParCompactionManager::chunk_array();
  2209   ParallelTaskTerminator terminator(parallel_gc_threads, qset);
  2211   PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm);
  2212   PSParallelCompact::FollowStackClosure follow_stack_closure(cm);
  2215     TraceTime tm_m("par mark", print_phases(), true, gclog_or_tty);
  2217     GCTaskQueue* q = GCTaskQueue::create();
  2219     q->enqueue(new MarkFromRootsTask(MarkFromRootsTask::universe));
  2220     q->enqueue(new MarkFromRootsTask(MarkFromRootsTask::jni_handles));
  2221     // We scan the thread roots in parallel
  2222     Threads::create_thread_roots_marking_tasks(q);
  2223     q->enqueue(new MarkFromRootsTask(MarkFromRootsTask::object_synchronizer));
  2224     q->enqueue(new MarkFromRootsTask(MarkFromRootsTask::flat_profiler));
  2225     q->enqueue(new MarkFromRootsTask(MarkFromRootsTask::management));
  2226     q->enqueue(new MarkFromRootsTask(MarkFromRootsTask::system_dictionary));
  2227     q->enqueue(new MarkFromRootsTask(MarkFromRootsTask::jvmti));
  2228     q->enqueue(new MarkFromRootsTask(MarkFromRootsTask::vm_symbols));
  2230     if (parallel_gc_threads > 1) {
  2231       for (uint j = 0; j < parallel_gc_threads; j++) {
  2232         q->enqueue(new StealMarkingTask(&terminator));
  2236     WaitForBarrierGCTask* fin = WaitForBarrierGCTask::create();
  2237     q->enqueue(fin);
  2239     gc_task_manager()->add_list(q);
  2241     fin->wait_for();
  2243     // We have to release the barrier tasks!
  2244     WaitForBarrierGCTask::destroy(fin);
  2247   // Process reference objects found during marking
  2249     TraceTime tm_r("reference processing", print_phases(), true, gclog_or_tty);
  2250     ReferencePolicy *soft_ref_policy;
  2251     if (maximum_heap_compaction) {
  2252       soft_ref_policy = new AlwaysClearPolicy();
  2253     } else {
  2254 #ifdef COMPILER2
  2255       soft_ref_policy = new LRUMaxHeapPolicy();
  2256 #else
  2257       soft_ref_policy = new LRUCurrentHeapPolicy();
  2258 #endif // COMPILER2
  2260     assert(soft_ref_policy != NULL, "No soft reference policy");
  2261     if (ref_processor()->processing_is_mt()) {
  2262       RefProcTaskExecutor task_executor;
  2263       ref_processor()->process_discovered_references(
  2264         soft_ref_policy, is_alive_closure(), &mark_and_push_closure,
  2265         &follow_stack_closure, &task_executor);
  2266     } else {
  2267       ref_processor()->process_discovered_references(
  2268         soft_ref_policy, is_alive_closure(), &mark_and_push_closure,
  2269         &follow_stack_closure, NULL);
  2273   TraceTime tm_c("class unloading", print_phases(), true, gclog_or_tty);
  2274   // Follow system dictionary roots and unload classes.
  2275   bool purged_class = SystemDictionary::do_unloading(is_alive_closure());
  2277   // Follow code cache roots.
  2278   CodeCache::do_unloading(is_alive_closure(), &mark_and_push_closure,
  2279                           purged_class);
  2280   follow_stack(cm); // Flush marking stack.
  2282   // Update subklass/sibling/implementor links of live klasses
  2283   // revisit_klass_stack is used in follow_weak_klass_links().
  2284   follow_weak_klass_links(cm);
  2286   // Visit symbol and interned string tables and delete unmarked oops
  2287   SymbolTable::unlink(is_alive_closure());
  2288   StringTable::unlink(is_alive_closure());
  2290   assert(cm->marking_stack()->size() == 0, "stack should be empty by now");
  2291   assert(cm->overflow_stack()->is_empty(), "stack should be empty by now");
  2294 // This should be moved to the shared markSweep code!
  2295 class PSAlwaysTrueClosure: public BoolObjectClosure {
  2296 public:
  2297   void do_object(oop p) { ShouldNotReachHere(); }
  2298   bool do_object_b(oop p) { return true; }
  2299 };
  2300 static PSAlwaysTrueClosure always_true;
  2302 void PSParallelCompact::adjust_roots() {
  2303   // Adjust the pointers to reflect the new locations
  2304   EventMark m("3 adjust roots");
  2305   TraceTime tm("adjust roots", print_phases(), true, gclog_or_tty);
  2307   // General strong roots.
  2308   Universe::oops_do(adjust_root_pointer_closure());
  2309   ReferenceProcessor::oops_do(adjust_root_pointer_closure());
  2310   JNIHandles::oops_do(adjust_root_pointer_closure());   // Global (strong) JNI handles
  2311   Threads::oops_do(adjust_root_pointer_closure());
  2312   ObjectSynchronizer::oops_do(adjust_root_pointer_closure());
  2313   FlatProfiler::oops_do(adjust_root_pointer_closure());
  2314   Management::oops_do(adjust_root_pointer_closure());
  2315   JvmtiExport::oops_do(adjust_root_pointer_closure());
  2316   // SO_AllClasses
  2317   SystemDictionary::oops_do(adjust_root_pointer_closure());
  2318   vmSymbols::oops_do(adjust_root_pointer_closure());
  2320   // Now adjust pointers in remaining weak roots.  (All of which should
  2321   // have been cleared if they pointed to non-surviving objects.)
  2322   // Global (weak) JNI handles
  2323   JNIHandles::weak_oops_do(&always_true, adjust_root_pointer_closure());
  2325   CodeCache::oops_do(adjust_pointer_closure());
  2326   SymbolTable::oops_do(adjust_root_pointer_closure());
  2327   StringTable::oops_do(adjust_root_pointer_closure());
  2328   ref_processor()->weak_oops_do(adjust_root_pointer_closure());
  2329   // Roots were visited so references into the young gen in roots
  2330   // may have been scanned.  Process them also.
  2331   // Should the reference processor have a span that excludes
  2332   // young gen objects?
  2333   PSScavenge::reference_processor()->weak_oops_do(
  2334                                               adjust_root_pointer_closure());
  2337 void PSParallelCompact::compact_perm(ParCompactionManager* cm) {
  2338   EventMark m("4 compact perm");
  2339   TraceTime tm("compact perm gen", print_phases(), true, gclog_or_tty);
  2340   // trace("4");
  2342   gc_heap()->perm_gen()->start_array()->reset();
  2343   move_and_update(cm, perm_space_id);
  2346 void PSParallelCompact::enqueue_chunk_draining_tasks(GCTaskQueue* q,
  2347                                                      uint parallel_gc_threads) {
  2348   TraceTime tm("drain task setup", print_phases(), true, gclog_or_tty);
  2350   const unsigned int task_count = MAX2(parallel_gc_threads, 1U);
  2351   for (unsigned int j = 0; j < task_count; j++) {
  2352     q->enqueue(new DrainStacksCompactionTask());
  2355   // Find all chunks that are available (can be filled immediately) and
  2356   // distribute them to the thread stacks.  The iteration is done in reverse
  2357   // order (high to low) so the chunks will be removed in ascending order.
  2359   const ParallelCompactData& sd = PSParallelCompact::summary_data();
  2361   size_t fillable_chunks = 0;   // A count for diagnostic purposes.
  2362   unsigned int which = 0;       // The worker thread number.
  2364   for (unsigned int id = to_space_id; id > perm_space_id; --id) {
  2365     SpaceInfo* const space_info = _space_info + id;
  2366     MutableSpace* const space = space_info->space();
  2367     HeapWord* const new_top = space_info->new_top();
  2369     const size_t beg_chunk = sd.addr_to_chunk_idx(space_info->dense_prefix());
  2370     const size_t end_chunk = sd.addr_to_chunk_idx(sd.chunk_align_up(new_top));
  2371     assert(end_chunk > 0, "perm gen cannot be empty");
  2373     for (size_t cur = end_chunk - 1; cur >= beg_chunk; --cur) {
  2374       if (sd.chunk(cur)->claim_unsafe()) {
  2375         ParCompactionManager* cm = ParCompactionManager::manager_array(which);
  2376         cm->save_for_processing(cur);
  2378         if (TraceParallelOldGCCompactionPhase && Verbose) {
  2379           const size_t count_mod_8 = fillable_chunks & 7;
  2380           if (count_mod_8 == 0) gclog_or_tty->print("fillable: ");
  2381           gclog_or_tty->print(" " SIZE_FORMAT_W(7), cur);
  2382           if (count_mod_8 == 7) gclog_or_tty->cr();
  2385         NOT_PRODUCT(++fillable_chunks;)
  2387         // Assign chunks to threads in round-robin fashion.
  2388         if (++which == task_count) {
  2389           which = 0;
  2395   if (TraceParallelOldGCCompactionPhase) {
  2396     if (Verbose && (fillable_chunks & 7) != 0) gclog_or_tty->cr();
  2397     gclog_or_tty->print_cr("%u initially fillable chunks", fillable_chunks);
  2401 #define PAR_OLD_DENSE_PREFIX_OVER_PARTITIONING 4
  2403 void PSParallelCompact::enqueue_dense_prefix_tasks(GCTaskQueue* q,
  2404                                                     uint parallel_gc_threads) {
  2405   TraceTime tm("dense prefix task setup", print_phases(), true, gclog_or_tty);
  2407   ParallelCompactData& sd = PSParallelCompact::summary_data();
  2409   // Iterate over all the spaces adding tasks for updating
  2410   // chunks in the dense prefix.  Assume that 1 gc thread
  2411   // will work on opening the gaps and the remaining gc threads
  2412   // will work on the dense prefix.
  2413   SpaceId space_id = old_space_id;
  2414   while (space_id != last_space_id) {
  2415     HeapWord* const dense_prefix_end = _space_info[space_id].dense_prefix();
  2416     const MutableSpace* const space = _space_info[space_id].space();
  2418     if (dense_prefix_end == space->bottom()) {
  2419       // There is no dense prefix for this space.
  2420       space_id = next_compaction_space_id(space_id);
  2421       continue;
  2424     // The dense prefix is before this chunk.
  2425     size_t chunk_index_end_dense_prefix =
  2426         sd.addr_to_chunk_idx(dense_prefix_end);
  2427     ChunkData* const dense_prefix_cp = sd.chunk(chunk_index_end_dense_prefix);
  2428     assert(dense_prefix_end == space->end() ||
  2429            dense_prefix_cp->available() ||
  2430            dense_prefix_cp->claimed(),
  2431            "The chunk after the dense prefix should always be ready to fill");
  2433     size_t chunk_index_start = sd.addr_to_chunk_idx(space->bottom());
  2435     // Is there dense prefix work?
  2436     size_t total_dense_prefix_chunks =
  2437       chunk_index_end_dense_prefix - chunk_index_start;
  2438     // How many chunks of the dense prefix should be given to
  2439     // each thread?
  2440     if (total_dense_prefix_chunks > 0) {
  2441       uint tasks_for_dense_prefix = 1;
  2442       if (UseParallelDensePrefixUpdate) {
  2443         if (total_dense_prefix_chunks <=
  2444             (parallel_gc_threads * PAR_OLD_DENSE_PREFIX_OVER_PARTITIONING)) {
  2445           // Don't over partition.  This assumes that
  2446           // PAR_OLD_DENSE_PREFIX_OVER_PARTITIONING is a small integer value
  2447           // so there are not many chunks to process.
  2448           tasks_for_dense_prefix = parallel_gc_threads;
  2449         } else {
  2450           // Over partition
  2451           tasks_for_dense_prefix = parallel_gc_threads *
  2452             PAR_OLD_DENSE_PREFIX_OVER_PARTITIONING;
  2455       size_t chunks_per_thread = total_dense_prefix_chunks /
  2456         tasks_for_dense_prefix;
  2457       // Give each thread at least 1 chunk.
  2458       if (chunks_per_thread == 0) {
  2459         chunks_per_thread = 1;
  2462       for (uint k = 0; k < tasks_for_dense_prefix; k++) {
  2463         if (chunk_index_start >= chunk_index_end_dense_prefix) {
  2464           break;
  2466         // chunk_index_end is not processed
  2467         size_t chunk_index_end = MIN2(chunk_index_start + chunks_per_thread,
  2468                                       chunk_index_end_dense_prefix);
  2469         q->enqueue(new UpdateDensePrefixTask(
  2470                                  space_id,
  2471                                  chunk_index_start,
  2472                                  chunk_index_end));
  2473         chunk_index_start = chunk_index_end;
  2476     // This gets any part of the dense prefix that did not
  2477     // fit evenly.
  2478     if (chunk_index_start < chunk_index_end_dense_prefix) {
  2479       q->enqueue(new UpdateDensePrefixTask(
  2480                                  space_id,
  2481                                  chunk_index_start,
  2482                                  chunk_index_end_dense_prefix));
  2484     space_id = next_compaction_space_id(space_id);
  2485   }  // End tasks for dense prefix
  2488 void PSParallelCompact::enqueue_chunk_stealing_tasks(
  2489                                      GCTaskQueue* q,
  2490                                      ParallelTaskTerminator* terminator_ptr,
  2491                                      uint parallel_gc_threads) {
  2492   TraceTime tm("steal task setup", print_phases(), true, gclog_or_tty);
  2494   // Once a thread has drained it's stack, it should try to steal chunks from
  2495   // other threads.
  2496   if (parallel_gc_threads > 1) {
  2497     for (uint j = 0; j < parallel_gc_threads; j++) {
  2498       q->enqueue(new StealChunkCompactionTask(terminator_ptr));
  2503 void PSParallelCompact::compact() {
  2504   EventMark m("5 compact");
  2505   // trace("5");
  2506   TraceTime tm("compaction phase", print_phases(), true, gclog_or_tty);
  2508   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
  2509   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
  2510   PSOldGen* old_gen = heap->old_gen();
  2511   old_gen->start_array()->reset();
  2512   uint parallel_gc_threads = heap->gc_task_manager()->workers();
  2513   TaskQueueSetSuper* qset = ParCompactionManager::chunk_array();
  2514   ParallelTaskTerminator terminator(parallel_gc_threads, qset);
  2516   GCTaskQueue* q = GCTaskQueue::create();
  2517   enqueue_chunk_draining_tasks(q, parallel_gc_threads);
  2518   enqueue_dense_prefix_tasks(q, parallel_gc_threads);
  2519   enqueue_chunk_stealing_tasks(q, &terminator, parallel_gc_threads);
  2522     TraceTime tm_pc("par compact", print_phases(), true, gclog_or_tty);
  2524     WaitForBarrierGCTask* fin = WaitForBarrierGCTask::create();
  2525     q->enqueue(fin);
  2527     gc_task_manager()->add_list(q);
  2529     fin->wait_for();
  2531     // We have to release the barrier tasks!
  2532     WaitForBarrierGCTask::destroy(fin);
  2534 #ifdef  ASSERT
  2535     // Verify that all chunks have been processed before the deferred updates.
  2536     // Note that perm_space_id is skipped; this type of verification is not
  2537     // valid until the perm gen is compacted by chunks.
  2538     for (unsigned int id = old_space_id; id < last_space_id; ++id) {
  2539       verify_complete(SpaceId(id));
  2541 #endif
  2545     // Update the deferred objects, if any.  Any compaction manager can be used.
  2546     TraceTime tm_du("deferred updates", print_phases(), true, gclog_or_tty);
  2547     ParCompactionManager* cm = ParCompactionManager::manager_array(0);
  2548     for (unsigned int id = old_space_id; id < last_space_id; ++id) {
  2549       update_deferred_objects(cm, SpaceId(id));
  2554 #ifdef  ASSERT
  2555 void PSParallelCompact::verify_complete(SpaceId space_id) {
  2556   // All Chunks between space bottom() to new_top() should be marked as filled
  2557   // and all Chunks between new_top() and top() should be available (i.e.,
  2558   // should have been emptied).
  2559   ParallelCompactData& sd = summary_data();
  2560   SpaceInfo si = _space_info[space_id];
  2561   HeapWord* new_top_addr = sd.chunk_align_up(si.new_top());
  2562   HeapWord* old_top_addr = sd.chunk_align_up(si.space()->top());
  2563   const size_t beg_chunk = sd.addr_to_chunk_idx(si.space()->bottom());
  2564   const size_t new_top_chunk = sd.addr_to_chunk_idx(new_top_addr);
  2565   const size_t old_top_chunk = sd.addr_to_chunk_idx(old_top_addr);
  2567   bool issued_a_warning = false;
  2569   size_t cur_chunk;
  2570   for (cur_chunk = beg_chunk; cur_chunk < new_top_chunk; ++cur_chunk) {
  2571     const ChunkData* const c = sd.chunk(cur_chunk);
  2572     if (!c->completed()) {
  2573       warning("chunk " SIZE_FORMAT " not filled:  "
  2574               "destination_count=" SIZE_FORMAT,
  2575               cur_chunk, c->destination_count());
  2576       issued_a_warning = true;
  2580   for (cur_chunk = new_top_chunk; cur_chunk < old_top_chunk; ++cur_chunk) {
  2581     const ChunkData* const c = sd.chunk(cur_chunk);
  2582     if (!c->available()) {
  2583       warning("chunk " SIZE_FORMAT " not empty:   "
  2584               "destination_count=" SIZE_FORMAT,
  2585               cur_chunk, c->destination_count());
  2586       issued_a_warning = true;
  2590   if (issued_a_warning) {
  2591     print_chunk_ranges();
  2594 #endif  // #ifdef ASSERT
  2596 void PSParallelCompact::compact_serial(ParCompactionManager* cm) {
  2597   EventMark m("5 compact serial");
  2598   TraceTime tm("compact serial", print_phases(), true, gclog_or_tty);
  2600   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
  2601   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
  2603   PSYoungGen* young_gen = heap->young_gen();
  2604   PSOldGen* old_gen = heap->old_gen();
  2606   old_gen->start_array()->reset();
  2607   old_gen->move_and_update(cm);
  2608   young_gen->move_and_update(cm);
  2612 void PSParallelCompact::follow_stack(ParCompactionManager* cm) {
  2613   while(!cm->overflow_stack()->is_empty()) {
  2614     oop obj = cm->overflow_stack()->pop();
  2615     obj->follow_contents(cm);
  2618   oop obj;
  2619   // obj is a reference!!!
  2620   while (cm->marking_stack()->pop_local(obj)) {
  2621     // It would be nice to assert about the type of objects we might
  2622     // pop, but they can come from anywhere, unfortunately.
  2623     obj->follow_contents(cm);
  2627 void
  2628 PSParallelCompact::follow_weak_klass_links(ParCompactionManager* serial_cm) {
  2629   // All klasses on the revisit stack are marked at this point.
  2630   // Update and follow all subklass, sibling and implementor links.
  2631   for (uint i = 0; i < ParallelGCThreads+1; i++) {
  2632     ParCompactionManager* cm = ParCompactionManager::manager_array(i);
  2633     KeepAliveClosure keep_alive_closure(cm);
  2634     for (int i = 0; i < cm->revisit_klass_stack()->length(); i++) {
  2635       cm->revisit_klass_stack()->at(i)->follow_weak_klass_links(
  2636         is_alive_closure(),
  2637         &keep_alive_closure);
  2639     follow_stack(cm);
  2643 void
  2644 PSParallelCompact::revisit_weak_klass_link(ParCompactionManager* cm, Klass* k) {
  2645   cm->revisit_klass_stack()->push(k);
  2648 #ifdef VALIDATE_MARK_SWEEP
  2650 void PSParallelCompact::track_adjusted_pointer(void* p, bool isroot) {
  2651   if (!ValidateMarkSweep)
  2652     return;
  2654   if (!isroot) {
  2655     if (_pointer_tracking) {
  2656       guarantee(_adjusted_pointers->contains(p), "should have seen this pointer");
  2657       _adjusted_pointers->remove(p);
  2659   } else {
  2660     ptrdiff_t index = _root_refs_stack->find(p);
  2661     if (index != -1) {
  2662       int l = _root_refs_stack->length();
  2663       if (l > 0 && l - 1 != index) {
  2664         void* last = _root_refs_stack->pop();
  2665         assert(last != p, "should be different");
  2666         _root_refs_stack->at_put(index, last);
  2667       } else {
  2668         _root_refs_stack->remove(p);
  2675 void PSParallelCompact::check_adjust_pointer(void* p) {
  2676   _adjusted_pointers->push(p);
  2680 class AdjusterTracker: public OopClosure {
  2681  public:
  2682   AdjusterTracker() {};
  2683   void do_oop(oop* o)         { PSParallelCompact::check_adjust_pointer(o); }
  2684   void do_oop(narrowOop* o)   { PSParallelCompact::check_adjust_pointer(o); }
  2685 };
  2688 void PSParallelCompact::track_interior_pointers(oop obj) {
  2689   if (ValidateMarkSweep) {
  2690     _adjusted_pointers->clear();
  2691     _pointer_tracking = true;
  2693     AdjusterTracker checker;
  2694     obj->oop_iterate(&checker);
  2699 void PSParallelCompact::check_interior_pointers() {
  2700   if (ValidateMarkSweep) {
  2701     _pointer_tracking = false;
  2702     guarantee(_adjusted_pointers->length() == 0, "should have processed the same pointers");
  2707 void PSParallelCompact::reset_live_oop_tracking(bool at_perm) {
  2708   if (ValidateMarkSweep) {
  2709     guarantee((size_t)_live_oops->length() == _live_oops_index, "should be at end of live oops");
  2710     _live_oops_index = at_perm ? _live_oops_index_at_perm : 0;
  2715 void PSParallelCompact::register_live_oop(oop p, size_t size) {
  2716   if (ValidateMarkSweep) {
  2717     _live_oops->push(p);
  2718     _live_oops_size->push(size);
  2719     _live_oops_index++;
  2723 void PSParallelCompact::validate_live_oop(oop p, size_t size) {
  2724   if (ValidateMarkSweep) {
  2725     oop obj = _live_oops->at((int)_live_oops_index);
  2726     guarantee(obj == p, "should be the same object");
  2727     guarantee(_live_oops_size->at((int)_live_oops_index) == size, "should be the same size");
  2728     _live_oops_index++;
  2732 void PSParallelCompact::live_oop_moved_to(HeapWord* q, size_t size,
  2733                                   HeapWord* compaction_top) {
  2734   assert(oop(q)->forwardee() == NULL || oop(q)->forwardee() == oop(compaction_top),
  2735          "should be moved to forwarded location");
  2736   if (ValidateMarkSweep) {
  2737     PSParallelCompact::validate_live_oop(oop(q), size);
  2738     _live_oops_moved_to->push(oop(compaction_top));
  2740   if (RecordMarkSweepCompaction) {
  2741     _cur_gc_live_oops->push(q);
  2742     _cur_gc_live_oops_moved_to->push(compaction_top);
  2743     _cur_gc_live_oops_size->push(size);
  2748 void PSParallelCompact::compaction_complete() {
  2749   if (RecordMarkSweepCompaction) {
  2750     GrowableArray<HeapWord*>* _tmp_live_oops          = _cur_gc_live_oops;
  2751     GrowableArray<HeapWord*>* _tmp_live_oops_moved_to = _cur_gc_live_oops_moved_to;
  2752     GrowableArray<size_t>   * _tmp_live_oops_size     = _cur_gc_live_oops_size;
  2754     _cur_gc_live_oops           = _last_gc_live_oops;
  2755     _cur_gc_live_oops_moved_to  = _last_gc_live_oops_moved_to;
  2756     _cur_gc_live_oops_size      = _last_gc_live_oops_size;
  2757     _last_gc_live_oops          = _tmp_live_oops;
  2758     _last_gc_live_oops_moved_to = _tmp_live_oops_moved_to;
  2759     _last_gc_live_oops_size     = _tmp_live_oops_size;
  2764 void PSParallelCompact::print_new_location_of_heap_address(HeapWord* q) {
  2765   if (!RecordMarkSweepCompaction) {
  2766     tty->print_cr("Requires RecordMarkSweepCompaction to be enabled");
  2767     return;
  2770   if (_last_gc_live_oops == NULL) {
  2771     tty->print_cr("No compaction information gathered yet");
  2772     return;
  2775   for (int i = 0; i < _last_gc_live_oops->length(); i++) {
  2776     HeapWord* old_oop = _last_gc_live_oops->at(i);
  2777     size_t    sz      = _last_gc_live_oops_size->at(i);
  2778     if (old_oop <= q && q < (old_oop + sz)) {
  2779       HeapWord* new_oop = _last_gc_live_oops_moved_to->at(i);
  2780       size_t offset = (q - old_oop);
  2781       tty->print_cr("Address " PTR_FORMAT, q);
  2782       tty->print_cr(" Was in oop " PTR_FORMAT ", size %d, at offset %d", old_oop, sz, offset);
  2783       tty->print_cr(" Now in oop " PTR_FORMAT ", actual address " PTR_FORMAT, new_oop, new_oop + offset);
  2784       return;
  2788   tty->print_cr("Address " PTR_FORMAT " not found in live oop information from last GC", q);
  2790 #endif //VALIDATE_MARK_SWEEP
  2792 // Update interior oops in the ranges of chunks [beg_chunk, end_chunk).
  2793 void
  2794 PSParallelCompact::update_and_deadwood_in_dense_prefix(ParCompactionManager* cm,
  2795                                                        SpaceId space_id,
  2796                                                        size_t beg_chunk,
  2797                                                        size_t end_chunk) {
  2798   ParallelCompactData& sd = summary_data();
  2799   ParMarkBitMap* const mbm = mark_bitmap();
  2801   HeapWord* beg_addr = sd.chunk_to_addr(beg_chunk);
  2802   HeapWord* const end_addr = sd.chunk_to_addr(end_chunk);
  2803   assert(beg_chunk <= end_chunk, "bad chunk range");
  2804   assert(end_addr <= dense_prefix(space_id), "not in the dense prefix");
  2806 #ifdef  ASSERT
  2807   // Claim the chunks to avoid triggering an assert when they are marked as
  2808   // filled.
  2809   for (size_t claim_chunk = beg_chunk; claim_chunk < end_chunk; ++claim_chunk) {
  2810     assert(sd.chunk(claim_chunk)->claim_unsafe(), "claim() failed");
  2812 #endif  // #ifdef ASSERT
  2814   if (beg_addr != space(space_id)->bottom()) {
  2815     // Find the first live object or block of dead space that *starts* in this
  2816     // range of chunks.  If a partial object crosses onto the chunk, skip it; it
  2817     // will be marked for 'deferred update' when the object head is processed.
  2818     // If dead space crosses onto the chunk, it is also skipped; it will be
  2819     // filled when the prior chunk is processed.  If neither of those apply, the
  2820     // first word in the chunk is the start of a live object or dead space.
  2821     assert(beg_addr > space(space_id)->bottom(), "sanity");
  2822     const ChunkData* const cp = sd.chunk(beg_chunk);
  2823     if (cp->partial_obj_size() != 0) {
  2824       beg_addr = sd.partial_obj_end(beg_chunk);
  2825     } else if (dead_space_crosses_boundary(cp, mbm->addr_to_bit(beg_addr))) {
  2826       beg_addr = mbm->find_obj_beg(beg_addr, end_addr);
  2830   if (beg_addr < end_addr) {
  2831     // A live object or block of dead space starts in this range of Chunks.
  2832      HeapWord* const dense_prefix_end = dense_prefix(space_id);
  2834     // Create closures and iterate.
  2835     UpdateOnlyClosure update_closure(mbm, cm, space_id);
  2836     FillClosure fill_closure(cm, space_id);
  2837     ParMarkBitMap::IterationStatus status;
  2838     status = mbm->iterate(&update_closure, &fill_closure, beg_addr, end_addr,
  2839                           dense_prefix_end);
  2840     if (status == ParMarkBitMap::incomplete) {
  2841       update_closure.do_addr(update_closure.source());
  2845   // Mark the chunks as filled.
  2846   ChunkData* const beg_cp = sd.chunk(beg_chunk);
  2847   ChunkData* const end_cp = sd.chunk(end_chunk);
  2848   for (ChunkData* cp = beg_cp; cp < end_cp; ++cp) {
  2849     cp->set_completed();
  2853 // Return the SpaceId for the space containing addr.  If addr is not in the
  2854 // heap, last_space_id is returned.  In debug mode it expects the address to be
  2855 // in the heap and asserts such.
  2856 PSParallelCompact::SpaceId PSParallelCompact::space_id(HeapWord* addr) {
  2857   assert(Universe::heap()->is_in_reserved(addr), "addr not in the heap");
  2859   for (unsigned int id = perm_space_id; id < last_space_id; ++id) {
  2860     if (_space_info[id].space()->contains(addr)) {
  2861       return SpaceId(id);
  2865   assert(false, "no space contains the addr");
  2866   return last_space_id;
  2869 void PSParallelCompact::update_deferred_objects(ParCompactionManager* cm,
  2870                                                 SpaceId id) {
  2871   assert(id < last_space_id, "bad space id");
  2873   ParallelCompactData& sd = summary_data();
  2874   const SpaceInfo* const space_info = _space_info + id;
  2875   ObjectStartArray* const start_array = space_info->start_array();
  2877   const MutableSpace* const space = space_info->space();
  2878   assert(space_info->dense_prefix() >= space->bottom(), "dense_prefix not set");
  2879   HeapWord* const beg_addr = space_info->dense_prefix();
  2880   HeapWord* const end_addr = sd.chunk_align_up(space_info->new_top());
  2882   const ChunkData* const beg_chunk = sd.addr_to_chunk_ptr(beg_addr);
  2883   const ChunkData* const end_chunk = sd.addr_to_chunk_ptr(end_addr);
  2884   const ChunkData* cur_chunk;
  2885   for (cur_chunk = beg_chunk; cur_chunk < end_chunk; ++cur_chunk) {
  2886     HeapWord* const addr = cur_chunk->deferred_obj_addr();
  2887     if (addr != NULL) {
  2888       if (start_array != NULL) {
  2889         start_array->allocate_block(addr);
  2891       oop(addr)->update_contents(cm);
  2892       assert(oop(addr)->is_oop_or_null(), "should be an oop now");
  2897 // Skip over count live words starting from beg, and return the address of the
  2898 // next live word.  Unless marked, the word corresponding to beg is assumed to
  2899 // be dead.  Callers must either ensure beg does not correspond to the middle of
  2900 // an object, or account for those live words in some other way.  Callers must
  2901 // also ensure that there are enough live words in the range [beg, end) to skip.
  2902 HeapWord*
  2903 PSParallelCompact::skip_live_words(HeapWord* beg, HeapWord* end, size_t count)
  2905   assert(count > 0, "sanity");
  2907   ParMarkBitMap* m = mark_bitmap();
  2908   idx_t bits_to_skip = m->words_to_bits(count);
  2909   idx_t cur_beg = m->addr_to_bit(beg);
  2910   const idx_t search_end = BitMap::word_align_up(m->addr_to_bit(end));
  2912   do {
  2913     cur_beg = m->find_obj_beg(cur_beg, search_end);
  2914     idx_t cur_end = m->find_obj_end(cur_beg, search_end);
  2915     const size_t obj_bits = cur_end - cur_beg + 1;
  2916     if (obj_bits > bits_to_skip) {
  2917       return m->bit_to_addr(cur_beg + bits_to_skip);
  2919     bits_to_skip -= obj_bits;
  2920     cur_beg = cur_end + 1;
  2921   } while (bits_to_skip > 0);
  2923   // Skipping the desired number of words landed just past the end of an object.
  2924   // Find the start of the next object.
  2925   cur_beg = m->find_obj_beg(cur_beg, search_end);
  2926   assert(cur_beg < m->addr_to_bit(end), "not enough live words to skip");
  2927   return m->bit_to_addr(cur_beg);
  2930 HeapWord*
  2931 PSParallelCompact::first_src_addr(HeapWord* const dest_addr,
  2932                                  size_t src_chunk_idx)
  2934   ParMarkBitMap* const bitmap = mark_bitmap();
  2935   const ParallelCompactData& sd = summary_data();
  2936   const size_t ChunkSize = ParallelCompactData::ChunkSize;
  2938   assert(sd.is_chunk_aligned(dest_addr), "not aligned");
  2940   const ChunkData* const src_chunk_ptr = sd.chunk(src_chunk_idx);
  2941   const size_t partial_obj_size = src_chunk_ptr->partial_obj_size();
  2942   HeapWord* const src_chunk_destination = src_chunk_ptr->destination();
  2944   assert(dest_addr >= src_chunk_destination, "wrong src chunk");
  2945   assert(src_chunk_ptr->data_size() > 0, "src chunk cannot be empty");
  2947   HeapWord* const src_chunk_beg = sd.chunk_to_addr(src_chunk_idx);
  2948   HeapWord* const src_chunk_end = src_chunk_beg + ChunkSize;
  2950   HeapWord* addr = src_chunk_beg;
  2951   if (dest_addr == src_chunk_destination) {
  2952     // Return the first live word in the source chunk.
  2953     if (partial_obj_size == 0) {
  2954       addr = bitmap->find_obj_beg(addr, src_chunk_end);
  2955       assert(addr < src_chunk_end, "no objects start in src chunk");
  2957     return addr;
  2960   // Must skip some live data.
  2961   size_t words_to_skip = dest_addr - src_chunk_destination;
  2962   assert(src_chunk_ptr->data_size() > words_to_skip, "wrong src chunk");
  2964   if (partial_obj_size >= words_to_skip) {
  2965     // All the live words to skip are part of the partial object.
  2966     addr += words_to_skip;
  2967     if (partial_obj_size == words_to_skip) {
  2968       // Find the first live word past the partial object.
  2969       addr = bitmap->find_obj_beg(addr, src_chunk_end);
  2970       assert(addr < src_chunk_end, "wrong src chunk");
  2972     return addr;
  2975   // Skip over the partial object (if any).
  2976   if (partial_obj_size != 0) {
  2977     words_to_skip -= partial_obj_size;
  2978     addr += partial_obj_size;
  2981   // Skip over live words due to objects that start in the chunk.
  2982   addr = skip_live_words(addr, src_chunk_end, words_to_skip);
  2983   assert(addr < src_chunk_end, "wrong src chunk");
  2984   return addr;
  2987 void PSParallelCompact::decrement_destination_counts(ParCompactionManager* cm,
  2988                                                      size_t beg_chunk,
  2989                                                      HeapWord* end_addr)
  2991   ParallelCompactData& sd = summary_data();
  2992   ChunkData* const beg = sd.chunk(beg_chunk);
  2993   HeapWord* const end_addr_aligned_up = sd.chunk_align_up(end_addr);
  2994   ChunkData* const end = sd.addr_to_chunk_ptr(end_addr_aligned_up);
  2995   size_t cur_idx = beg_chunk;
  2996   for (ChunkData* cur = beg; cur < end; ++cur, ++cur_idx) {
  2997     assert(cur->data_size() > 0, "chunk must have live data");
  2998     cur->decrement_destination_count();
  2999     if (cur_idx <= cur->source_chunk() && cur->available() && cur->claim()) {
  3000       cm->save_for_processing(cur_idx);
  3005 size_t PSParallelCompact::next_src_chunk(MoveAndUpdateClosure& closure,
  3006                                          SpaceId& src_space_id,
  3007                                          HeapWord*& src_space_top,
  3008                                          HeapWord* end_addr)
  3010   typedef ParallelCompactData::ChunkData ChunkData;
  3012   ParallelCompactData& sd = PSParallelCompact::summary_data();
  3013   const size_t chunk_size = ParallelCompactData::ChunkSize;
  3015   size_t src_chunk_idx = 0;
  3017   // Skip empty chunks (if any) up to the top of the space.
  3018   HeapWord* const src_aligned_up = sd.chunk_align_up(end_addr);
  3019   ChunkData* src_chunk_ptr = sd.addr_to_chunk_ptr(src_aligned_up);
  3020   HeapWord* const top_aligned_up = sd.chunk_align_up(src_space_top);
  3021   const ChunkData* const top_chunk_ptr = sd.addr_to_chunk_ptr(top_aligned_up);
  3022   while (src_chunk_ptr < top_chunk_ptr && src_chunk_ptr->data_size() == 0) {
  3023     ++src_chunk_ptr;
  3026   if (src_chunk_ptr < top_chunk_ptr) {
  3027     // The next source chunk is in the current space.  Update src_chunk_idx and
  3028     // the source address to match src_chunk_ptr.
  3029     src_chunk_idx = sd.chunk(src_chunk_ptr);
  3030     HeapWord* const src_chunk_addr = sd.chunk_to_addr(src_chunk_idx);
  3031     if (src_chunk_addr > closure.source()) {
  3032       closure.set_source(src_chunk_addr);
  3034     return src_chunk_idx;
  3037   // Switch to a new source space and find the first non-empty chunk.
  3038   unsigned int space_id = src_space_id + 1;
  3039   assert(space_id < last_space_id, "not enough spaces");
  3041   HeapWord* const destination = closure.destination();
  3043   do {
  3044     MutableSpace* space = _space_info[space_id].space();
  3045     HeapWord* const bottom = space->bottom();
  3046     const ChunkData* const bottom_cp = sd.addr_to_chunk_ptr(bottom);
  3048     // Iterate over the spaces that do not compact into themselves.
  3049     if (bottom_cp->destination() != bottom) {
  3050       HeapWord* const top_aligned_up = sd.chunk_align_up(space->top());
  3051       const ChunkData* const top_cp = sd.addr_to_chunk_ptr(top_aligned_up);
  3053       for (const ChunkData* src_cp = bottom_cp; src_cp < top_cp; ++src_cp) {
  3054         if (src_cp->live_obj_size() > 0) {
  3055           // Found it.
  3056           assert(src_cp->destination() == destination,
  3057                  "first live obj in the space must match the destination");
  3058           assert(src_cp->partial_obj_size() == 0,
  3059                  "a space cannot begin with a partial obj");
  3061           src_space_id = SpaceId(space_id);
  3062           src_space_top = space->top();
  3063           const size_t src_chunk_idx = sd.chunk(src_cp);
  3064           closure.set_source(sd.chunk_to_addr(src_chunk_idx));
  3065           return src_chunk_idx;
  3066         } else {
  3067           assert(src_cp->data_size() == 0, "sanity");
  3071   } while (++space_id < last_space_id);
  3073   assert(false, "no source chunk was found");
  3074   return 0;
  3077 void PSParallelCompact::fill_chunk(ParCompactionManager* cm, size_t chunk_idx)
  3079   typedef ParMarkBitMap::IterationStatus IterationStatus;
  3080   const size_t ChunkSize = ParallelCompactData::ChunkSize;
  3081   ParMarkBitMap* const bitmap = mark_bitmap();
  3082   ParallelCompactData& sd = summary_data();
  3083   ChunkData* const chunk_ptr = sd.chunk(chunk_idx);
  3085   // Get the items needed to construct the closure.
  3086   HeapWord* dest_addr = sd.chunk_to_addr(chunk_idx);
  3087   SpaceId dest_space_id = space_id(dest_addr);
  3088   ObjectStartArray* start_array = _space_info[dest_space_id].start_array();
  3089   HeapWord* new_top = _space_info[dest_space_id].new_top();
  3090   assert(dest_addr < new_top, "sanity");
  3091   const size_t words = MIN2(pointer_delta(new_top, dest_addr), ChunkSize);
  3093   // Get the source chunk and related info.
  3094   size_t src_chunk_idx = chunk_ptr->source_chunk();
  3095   SpaceId src_space_id = space_id(sd.chunk_to_addr(src_chunk_idx));
  3096   HeapWord* src_space_top = _space_info[src_space_id].space()->top();
  3098   MoveAndUpdateClosure closure(bitmap, cm, start_array, dest_addr, words);
  3099   closure.set_source(first_src_addr(dest_addr, src_chunk_idx));
  3101   // Adjust src_chunk_idx to prepare for decrementing destination counts (the
  3102   // destination count is not decremented when a chunk is copied to itself).
  3103   if (src_chunk_idx == chunk_idx) {
  3104     src_chunk_idx += 1;
  3107   if (bitmap->is_unmarked(closure.source())) {
  3108     // The first source word is in the middle of an object; copy the remainder
  3109     // of the object or as much as will fit.  The fact that pointer updates were
  3110     // deferred will be noted when the object header is processed.
  3111     HeapWord* const old_src_addr = closure.source();
  3112     closure.copy_partial_obj();
  3113     if (closure.is_full()) {
  3114       decrement_destination_counts(cm, src_chunk_idx, closure.source());
  3115       chunk_ptr->set_deferred_obj_addr(NULL);
  3116       chunk_ptr->set_completed();
  3117       return;
  3120     HeapWord* const end_addr = sd.chunk_align_down(closure.source());
  3121     if (sd.chunk_align_down(old_src_addr) != end_addr) {
  3122       // The partial object was copied from more than one source chunk.
  3123       decrement_destination_counts(cm, src_chunk_idx, end_addr);
  3125       // Move to the next source chunk, possibly switching spaces as well.  All
  3126       // args except end_addr may be modified.
  3127       src_chunk_idx = next_src_chunk(closure, src_space_id, src_space_top,
  3128                                      end_addr);
  3132   do {
  3133     HeapWord* const cur_addr = closure.source();
  3134     HeapWord* const end_addr = MIN2(sd.chunk_align_up(cur_addr + 1),
  3135                                     src_space_top);
  3136     IterationStatus status = bitmap->iterate(&closure, cur_addr, end_addr);
  3138     if (status == ParMarkBitMap::incomplete) {
  3139       // The last obj that starts in the source chunk does not end in the chunk.
  3140       assert(closure.source() < end_addr, "sanity")
  3141       HeapWord* const obj_beg = closure.source();
  3142       HeapWord* const range_end = MIN2(obj_beg + closure.words_remaining(),
  3143                                        src_space_top);
  3144       HeapWord* const obj_end = bitmap->find_obj_end(obj_beg, range_end);
  3145       if (obj_end < range_end) {
  3146         // The end was found; the entire object will fit.
  3147         status = closure.do_addr(obj_beg, bitmap->obj_size(obj_beg, obj_end));
  3148         assert(status != ParMarkBitMap::would_overflow, "sanity");
  3149       } else {
  3150         // The end was not found; the object will not fit.
  3151         assert(range_end < src_space_top, "obj cannot cross space boundary");
  3152         status = ParMarkBitMap::would_overflow;
  3156     if (status == ParMarkBitMap::would_overflow) {
  3157       // The last object did not fit.  Note that interior oop updates were
  3158       // deferred, then copy enough of the object to fill the chunk.
  3159       chunk_ptr->set_deferred_obj_addr(closure.destination());
  3160       status = closure.copy_until_full(); // copies from closure.source()
  3162       decrement_destination_counts(cm, src_chunk_idx, closure.source());
  3163       chunk_ptr->set_completed();
  3164       return;
  3167     if (status == ParMarkBitMap::full) {
  3168       decrement_destination_counts(cm, src_chunk_idx, closure.source());
  3169       chunk_ptr->set_deferred_obj_addr(NULL);
  3170       chunk_ptr->set_completed();
  3171       return;
  3174     decrement_destination_counts(cm, src_chunk_idx, end_addr);
  3176     // Move to the next source chunk, possibly switching spaces as well.  All
  3177     // args except end_addr may be modified.
  3178     src_chunk_idx = next_src_chunk(closure, src_space_id, src_space_top,
  3179                                    end_addr);
  3180   } while (true);
  3183 void
  3184 PSParallelCompact::move_and_update(ParCompactionManager* cm, SpaceId space_id) {
  3185   const MutableSpace* sp = space(space_id);
  3186   if (sp->is_empty()) {
  3187     return;
  3190   ParallelCompactData& sd = PSParallelCompact::summary_data();
  3191   ParMarkBitMap* const bitmap = mark_bitmap();
  3192   HeapWord* const dp_addr = dense_prefix(space_id);
  3193   HeapWord* beg_addr = sp->bottom();
  3194   HeapWord* end_addr = sp->top();
  3196 #ifdef ASSERT
  3197   assert(beg_addr <= dp_addr && dp_addr <= end_addr, "bad dense prefix");
  3198   if (cm->should_verify_only()) {
  3199     VerifyUpdateClosure verify_update(cm, sp);
  3200     bitmap->iterate(&verify_update, beg_addr, end_addr);
  3201     return;
  3204   if (cm->should_reset_only()) {
  3205     ResetObjectsClosure reset_objects(cm);
  3206     bitmap->iterate(&reset_objects, beg_addr, end_addr);
  3207     return;
  3209 #endif
  3211   const size_t beg_chunk = sd.addr_to_chunk_idx(beg_addr);
  3212   const size_t dp_chunk = sd.addr_to_chunk_idx(dp_addr);
  3213   if (beg_chunk < dp_chunk) {
  3214     update_and_deadwood_in_dense_prefix(cm, space_id, beg_chunk, dp_chunk);
  3217   // The destination of the first live object that starts in the chunk is one
  3218   // past the end of the partial object entering the chunk (if any).
  3219   HeapWord* const dest_addr = sd.partial_obj_end(dp_chunk);
  3220   HeapWord* const new_top = _space_info[space_id].new_top();
  3221   assert(new_top >= dest_addr, "bad new_top value");
  3222   const size_t words = pointer_delta(new_top, dest_addr);
  3224   if (words > 0) {
  3225     ObjectStartArray* start_array = _space_info[space_id].start_array();
  3226     MoveAndUpdateClosure closure(bitmap, cm, start_array, dest_addr, words);
  3228     ParMarkBitMap::IterationStatus status;
  3229     status = bitmap->iterate(&closure, dest_addr, end_addr);
  3230     assert(status == ParMarkBitMap::full, "iteration not complete");
  3231     assert(bitmap->find_obj_beg(closure.source(), end_addr) == end_addr,
  3232            "live objects skipped because closure is full");
  3236 jlong PSParallelCompact::millis_since_last_gc() {
  3237   jlong ret_val = os::javaTimeMillis() - _time_of_last_gc;
  3238   // XXX See note in genCollectedHeap::millis_since_last_gc().
  3239   if (ret_val < 0) {
  3240     NOT_PRODUCT(warning("time warp: %d", ret_val);)
  3241     return 0;
  3243   return ret_val;
  3246 void PSParallelCompact::reset_millis_since_last_gc() {
  3247   _time_of_last_gc = os::javaTimeMillis();
  3250 ParMarkBitMap::IterationStatus MoveAndUpdateClosure::copy_until_full()
  3252   if (source() != destination()) {
  3253     assert(source() > destination(), "must copy to the left");
  3254     Copy::aligned_conjoint_words(source(), destination(), words_remaining());
  3256   update_state(words_remaining());
  3257   assert(is_full(), "sanity");
  3258   return ParMarkBitMap::full;
  3261 void MoveAndUpdateClosure::copy_partial_obj()
  3263   size_t words = words_remaining();
  3265   HeapWord* const range_end = MIN2(source() + words, bitmap()->region_end());
  3266   HeapWord* const end_addr = bitmap()->find_obj_end(source(), range_end);
  3267   if (end_addr < range_end) {
  3268     words = bitmap()->obj_size(source(), end_addr);
  3271   // This test is necessary; if omitted, the pointer updates to a partial object
  3272   // that crosses the dense prefix boundary could be overwritten.
  3273   if (source() != destination()) {
  3274     assert(source() > destination(), "must copy to the left");
  3275     Copy::aligned_conjoint_words(source(), destination(), words);
  3277   update_state(words);
  3280 ParMarkBitMapClosure::IterationStatus
  3281 MoveAndUpdateClosure::do_addr(HeapWord* addr, size_t words) {
  3282   assert(destination() != NULL, "sanity");
  3283   assert(bitmap()->obj_size(addr) == words, "bad size");
  3285   _source = addr;
  3286   assert(PSParallelCompact::summary_data().calc_new_pointer(source()) ==
  3287          destination(), "wrong destination");
  3289   if (words > words_remaining()) {
  3290     return ParMarkBitMap::would_overflow;
  3293   // The start_array must be updated even if the object is not moving.
  3294   if (_start_array != NULL) {
  3295     _start_array->allocate_block(destination());
  3298   if (destination() != source()) {
  3299     assert(destination() < source(), "must copy to the left");
  3300     Copy::aligned_conjoint_words(source(), destination(), words);
  3303   oop moved_oop = (oop) destination();
  3304   moved_oop->update_contents(compaction_manager());
  3305   assert(moved_oop->is_oop_or_null(), "Object should be whole at this point");
  3307   update_state(words);
  3308   assert(destination() == (HeapWord*)moved_oop + moved_oop->size(), "sanity");
  3309   return is_full() ? ParMarkBitMap::full : ParMarkBitMap::incomplete;
  3312 UpdateOnlyClosure::UpdateOnlyClosure(ParMarkBitMap* mbm,
  3313                                      ParCompactionManager* cm,
  3314                                      PSParallelCompact::SpaceId space_id) :
  3315   ParMarkBitMapClosure(mbm, cm),
  3316   _space_id(space_id),
  3317   _start_array(PSParallelCompact::start_array(space_id))
  3321 // Updates the references in the object to their new values.
  3322 ParMarkBitMapClosure::IterationStatus
  3323 UpdateOnlyClosure::do_addr(HeapWord* addr, size_t words) {
  3324   do_addr(addr);
  3325   return ParMarkBitMap::incomplete;
  3328 BitBlockUpdateClosure::BitBlockUpdateClosure(ParMarkBitMap* mbm,
  3329                         ParCompactionManager* cm,
  3330                         size_t chunk_index) :
  3331                         ParMarkBitMapClosure(mbm, cm),
  3332                         _live_data_left(0),
  3333                         _cur_block(0) {
  3334   _chunk_start =
  3335     PSParallelCompact::summary_data().chunk_to_addr(chunk_index);
  3336   _chunk_end =
  3337     PSParallelCompact::summary_data().chunk_to_addr(chunk_index) +
  3338                  ParallelCompactData::ChunkSize;
  3339   _chunk_index = chunk_index;
  3340   _cur_block =
  3341     PSParallelCompact::summary_data().addr_to_block_idx(_chunk_start);
  3344 bool BitBlockUpdateClosure::chunk_contains_cur_block() {
  3345   return ParallelCompactData::chunk_contains_block(_chunk_index, _cur_block);
  3348 void BitBlockUpdateClosure::reset_chunk(size_t chunk_index) {
  3349   DEBUG_ONLY(ParallelCompactData::BlockData::set_cur_phase(7);)
  3350   ParallelCompactData& sd = PSParallelCompact::summary_data();
  3351   _chunk_index = chunk_index;
  3352   _live_data_left = 0;
  3353   _chunk_start = sd.chunk_to_addr(chunk_index);
  3354   _chunk_end = sd.chunk_to_addr(chunk_index) + ParallelCompactData::ChunkSize;
  3356   // The first block in this chunk
  3357   size_t first_block =  sd.addr_to_block_idx(_chunk_start);
  3358   size_t partial_live_size = sd.chunk(chunk_index)->partial_obj_size();
  3360   // Set the offset to 0. By definition it should have that value
  3361   // but it may have been written while processing an earlier chunk.
  3362   if (partial_live_size == 0) {
  3363     // No live object extends onto the chunk.  The first bit
  3364     // in the bit map for the first chunk must be a start bit.
  3365     // Although there may not be any marked bits, it is safe
  3366     // to set it as a start bit.
  3367     sd.block(first_block)->set_start_bit_offset(0);
  3368     sd.block(first_block)->set_first_is_start_bit(true);
  3369   } else if (sd.partial_obj_ends_in_block(first_block)) {
  3370     sd.block(first_block)->set_end_bit_offset(0);
  3371     sd.block(first_block)->set_first_is_start_bit(false);
  3372   } else {
  3373     // The partial object extends beyond the first block.
  3374     // There is no object starting in the first block
  3375     // so the offset and bit parity are not needed.
  3376     // Set the the bit parity to start bit so assertions
  3377     // work when not bit is found.
  3378     sd.block(first_block)->set_end_bit_offset(0);
  3379     sd.block(first_block)->set_first_is_start_bit(false);
  3381   _cur_block = first_block;
  3382 #ifdef ASSERT
  3383   if (sd.block(first_block)->first_is_start_bit()) {
  3384     assert(!sd.partial_obj_ends_in_block(first_block),
  3385       "Partial object cannot end in first block");
  3388   if (PrintGCDetails && Verbose) {
  3389     if (partial_live_size == 1) {
  3390     gclog_or_tty->print_cr("first_block " PTR_FORMAT
  3391       " _offset " PTR_FORMAT
  3392       " _first_is_start_bit %d",
  3393       first_block,
  3394       sd.block(first_block)->raw_offset(),
  3395       sd.block(first_block)->first_is_start_bit());
  3398 #endif
  3399   DEBUG_ONLY(ParallelCompactData::BlockData::set_cur_phase(17);)
  3402 // This method is called when a object has been found (both beginning
  3403 // and end of the object) in the range of iteration.  This method is
  3404 // calculating the words of live data to the left of a block.  That live
  3405 // data includes any object starting to the left of the block (i.e.,
  3406 // the live-data-to-the-left of block AAA will include the full size
  3407 // of any object entering AAA).
  3409 ParMarkBitMapClosure::IterationStatus
  3410 BitBlockUpdateClosure::do_addr(HeapWord* addr, size_t words) {
  3411   // add the size to the block data.
  3412   HeapWord* obj = addr;
  3413   ParallelCompactData& sd = PSParallelCompact::summary_data();
  3415   assert(bitmap()->obj_size(obj) == words, "bad size");
  3416   assert(_chunk_start <= obj, "object is not in chunk");
  3417   assert(obj + words <= _chunk_end, "object is not in chunk");
  3419   // Update the live data to the left
  3420   size_t prev_live_data_left = _live_data_left;
  3421   _live_data_left = _live_data_left + words;
  3423   // Is this object in the current block.
  3424   size_t block_of_obj = sd.addr_to_block_idx(obj);
  3425   size_t block_of_obj_last = sd.addr_to_block_idx(obj + words - 1);
  3426   HeapWord* block_of_obj_last_addr = sd.block_to_addr(block_of_obj_last);
  3427   if (_cur_block < block_of_obj) {
  3429     //
  3430     // No object crossed the block boundary and this object was found
  3431     // on the other side of the block boundary.  Update the offset for
  3432     // the new block with the data size that does not include this object.
  3433     //
  3434     // The first bit in block_of_obj is a start bit except in the
  3435     // case where the partial object for the chunk extends into
  3436     // this block.
  3437     if (sd.partial_obj_ends_in_block(block_of_obj)) {
  3438       sd.block(block_of_obj)->set_end_bit_offset(prev_live_data_left);
  3439     } else {
  3440       sd.block(block_of_obj)->set_start_bit_offset(prev_live_data_left);
  3443     // Does this object pass beyond the its block?
  3444     if (block_of_obj < block_of_obj_last) {
  3445       // Object crosses block boundary.  Two blocks need to be udpated:
  3446       //        the current block where the object started
  3447       //        the block where the object ends
  3448       //
  3449       // The offset for blocks with no objects starting in them
  3450       // (e.g., blocks between _cur_block and  block_of_obj_last)
  3451       // should not be needed.
  3452       // Note that block_of_obj_last may be in another chunk.  If so,
  3453       // it should be overwritten later.  This is a problem (writting
  3454       // into a block in a later chunk) for parallel execution.
  3455       assert(obj < block_of_obj_last_addr,
  3456         "Object should start in previous block");
  3458       // obj is crossing into block_of_obj_last so the first bit
  3459       // is and end bit.
  3460       sd.block(block_of_obj_last)->set_end_bit_offset(_live_data_left);
  3462       _cur_block = block_of_obj_last;
  3463     } else {
  3464       // _first_is_start_bit has already been set correctly
  3465       // in the if-then-else above so don't reset it here.
  3466       _cur_block = block_of_obj;
  3468   } else {
  3469     // The current block only changes if the object extends beyound
  3470     // the block it starts in.
  3471     //
  3472     // The object starts in the current block.
  3473     // Does this object pass beyond the end of it?
  3474     if (block_of_obj < block_of_obj_last) {
  3475       // Object crosses block boundary.
  3476       // See note above on possible blocks between block_of_obj and
  3477       // block_of_obj_last
  3478       assert(obj < block_of_obj_last_addr,
  3479         "Object should start in previous block");
  3481       sd.block(block_of_obj_last)->set_end_bit_offset(_live_data_left);
  3483       _cur_block = block_of_obj_last;
  3487   // Return incomplete if there are more blocks to be done.
  3488   if (chunk_contains_cur_block()) {
  3489     return ParMarkBitMap::incomplete;
  3491   return ParMarkBitMap::complete;
  3494 // Verify the new location using the forwarding pointer
  3495 // from MarkSweep::mark_sweep_phase2().  Set the mark_word
  3496 // to the initial value.
  3497 ParMarkBitMapClosure::IterationStatus
  3498 PSParallelCompact::VerifyUpdateClosure::do_addr(HeapWord* addr, size_t words) {
  3499   // The second arg (words) is not used.
  3500   oop obj = (oop) addr;
  3501   HeapWord* forwarding_ptr = (HeapWord*) obj->mark()->decode_pointer();
  3502   HeapWord* new_pointer = summary_data().calc_new_pointer(obj);
  3503   if (forwarding_ptr == NULL) {
  3504     // The object is dead or not moving.
  3505     assert(bitmap()->is_unmarked(obj) || (new_pointer == (HeapWord*) obj),
  3506            "Object liveness is wrong.");
  3507     return ParMarkBitMap::incomplete;
  3509   assert(UseParallelOldGCDensePrefix ||
  3510          (HeapMaximumCompactionInterval > 1) ||
  3511          (MarkSweepAlwaysCompactCount > 1) ||
  3512          (forwarding_ptr == new_pointer),
  3513     "Calculation of new location is incorrect");
  3514   return ParMarkBitMap::incomplete;
  3517 // Reset objects modified for debug checking.
  3518 ParMarkBitMapClosure::IterationStatus
  3519 PSParallelCompact::ResetObjectsClosure::do_addr(HeapWord* addr, size_t words) {
  3520   // The second arg (words) is not used.
  3521   oop obj = (oop) addr;
  3522   obj->init_mark();
  3523   return ParMarkBitMap::incomplete;
  3526 // Prepare for compaction.  This method is executed once
  3527 // (i.e., by a single thread) before compaction.
  3528 // Save the updated location of the intArrayKlassObj for
  3529 // filling holes in the dense prefix.
  3530 void PSParallelCompact::compact_prologue() {
  3531   _updated_int_array_klass_obj = (klassOop)
  3532     summary_data().calc_new_pointer(Universe::intArrayKlassObj());
  3535 // The initial implementation of this method created a field
  3536 // _next_compaction_space_id in SpaceInfo and initialized
  3537 // that field in SpaceInfo::initialize_space_info().  That
  3538 // required that _next_compaction_space_id be declared a
  3539 // SpaceId in SpaceInfo and that would have required that
  3540 // either SpaceId be declared in a separate class or that
  3541 // it be declared in SpaceInfo.  It didn't seem consistent
  3542 // to declare it in SpaceInfo (didn't really fit logically).
  3543 // Alternatively, defining a separate class to define SpaceId
  3544 // seem excessive.  This implementation is simple and localizes
  3545 // the knowledge.
  3547 PSParallelCompact::SpaceId
  3548 PSParallelCompact::next_compaction_space_id(SpaceId id) {
  3549   assert(id < last_space_id, "id out of range");
  3550   switch (id) {
  3551     case perm_space_id :
  3552       return last_space_id;
  3553     case old_space_id :
  3554       return eden_space_id;
  3555     case eden_space_id :
  3556       return from_space_id;
  3557     case from_space_id :
  3558       return to_space_id;
  3559     case to_space_id :
  3560       return last_space_id;
  3561     default:
  3562       assert(false, "Bad space id");
  3563       return last_space_id;
  3567 // Here temporarily for debugging
  3568 #ifdef ASSERT
  3569   size_t ParallelCompactData::block_idx(BlockData* block) {
  3570     size_t index = pointer_delta(block,
  3571       PSParallelCompact::summary_data()._block_data, sizeof(BlockData));
  3572     return index;
  3574 #endif

mercurial