1 /*
   2  * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
  27 #include "gc_implementation/parallelScavenge/psOldGen.hpp"
  28 #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp"
  29 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
  30 #include "gc_implementation/shared/gcTrace.hpp"
  31 #include "gc_implementation/shared/mutableSpace.hpp"
  32 #include "memory/allocation.inline.hpp"
  33 #include "memory/memRegion.hpp"
  34 #include "memory/padded.inline.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "oops/oop.psgc.inline.hpp"
  37 
  38 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  39 
  40 PaddedEnd<PSPromotionManager>* PSPromotionManager::_manager_array = NULL;
  41 OopStarTaskQueueSet*           PSPromotionManager::_stack_array_depth = NULL;
  42 PSOldGen*                      PSPromotionManager::_old_gen = NULL;
  43 MutableSpace*                  PSPromotionManager::_young_space = NULL;
  44 
  45 void PSPromotionManager::initialize() {
  46   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
  47   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
  48 
  49   _old_gen = heap->old_gen();
  50   _young_space = heap->young_gen()->to_space();
  51 
  52   // To prevent false sharing, we pad the PSPromotionManagers
  53   // and make sure that the first instance starts at a cache line.
  54   assert(_manager_array == NULL, "Attempt to initialize twice");
  55   _manager_array = PaddedArray<PSPromotionManager, mtGC>::create_unfreeable(ParallelGCThreads + 1);
  56   guarantee(_manager_array != NULL, "Could not initialize promotion manager");
  57 
  58   _stack_array_depth = new OopStarTaskQueueSet(ParallelGCThreads);
  59   guarantee(_stack_array_depth != NULL, "Could not initialize promotion manager");
  60 
  61   // Create and register the PSPromotionManager(s) for the worker threads.
  62   for(uint i=0; i<ParallelGCThreads; i++) {
  63     stack_array_depth()->register_queue(i, _manager_array[i].claimed_stack_depth());
  64   }
  65   // The VMThread gets its own PSPromotionManager, which is not available
  66   // for work stealing.
  67 }
  68 
  69 PSPromotionManager* PSPromotionManager::gc_thread_promotion_manager(int index) {
  70   assert(index >= 0 && index < (int)ParallelGCThreads, "index out of range");
  71   assert(_manager_array != NULL, "Sanity");
  72   return &_manager_array[index];
  73 }
  74 
  75 PSPromotionManager* PSPromotionManager::vm_thread_promotion_manager() {
  76   assert(_manager_array != NULL, "Sanity");
  77   return &_manager_array[ParallelGCThreads];
  78 }
  79 
  80 void PSPromotionManager::pre_scavenge() {
  81   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
  82   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
  83 
  84   _young_space = heap->young_gen()->to_space();
  85 
  86   for(uint i=0; i<ParallelGCThreads+1; i++) {
  87     manager_array(i)->reset();
  88   }
  89 }
  90 
  91 bool PSPromotionManager::post_scavenge(YoungGCTracer& gc_tracer) {
  92   bool promotion_failure_occurred = false;
  93 
  94   TASKQUEUE_STATS_ONLY(if (PrintTaskqueue) print_taskqueue_stats());
  95   for (uint i = 0; i < ParallelGCThreads + 1; i++) {
  96     PSPromotionManager* manager = manager_array(i);
  97     assert(manager->claimed_stack_depth()->is_empty(), "should be empty");
  98     if (manager->_promotion_failed_info.has_failed()) {
  99       gc_tracer.report_promotion_failed(manager->_promotion_failed_info);
 100       promotion_failure_occurred = true;
 101     }
 102     manager->flush_labs();
 103   }
 104   return promotion_failure_occurred;
 105 }
 106 
 107 #if TASKQUEUE_STATS
 108 void
 109 PSPromotionManager::print_local_stats(outputStream* const out, uint i) const {
 110   #define FMT " " SIZE_FORMAT_W(10)
 111   out->print_cr("%3u" FMT FMT FMT FMT, i, _masked_pushes, _masked_steals,
 112                 _arrays_chunked, _array_chunks_processed);
 113   #undef FMT
 114 }
 115 
 116 static const char* const pm_stats_hdr[] = {
 117   "    --------masked-------     arrays      array",
 118   "thr       push      steal    chunked     chunks",
 119   "--- ---------- ---------- ---------- ----------"
 120 };
 121 
 122 void
 123 PSPromotionManager::print_taskqueue_stats(outputStream* const out) {
 124   out->print_cr("== GC Tasks Stats, GC %3d",
 125                 Universe::heap()->total_collections());
 126 
 127   TaskQueueStats totals;
 128   out->print("thr "); TaskQueueStats::print_header(1, out); out->cr();
 129   out->print("--- "); TaskQueueStats::print_header(2, out); out->cr();
 130   for (uint i = 0; i < ParallelGCThreads + 1; ++i) {
 131     TaskQueueStats& next = manager_array(i)->_claimed_stack_depth.stats;
 132     out->print("%3d ", i); next.print(out); out->cr();
 133     totals += next;
 134   }
 135   out->print("tot "); totals.print(out); out->cr();
 136 
 137   const uint hlines = sizeof(pm_stats_hdr) / sizeof(pm_stats_hdr[0]);
 138   for (uint i = 0; i < hlines; ++i) out->print_cr("%s", pm_stats_hdr[i]);
 139   for (uint i = 0; i < ParallelGCThreads + 1; ++i) {
 140     manager_array(i)->print_local_stats(out, i);
 141   }
 142 }
 143 
 144 void
 145 PSPromotionManager::reset_stats() {
 146   claimed_stack_depth()->stats.reset();
 147   _masked_pushes = _masked_steals = 0;
 148   _arrays_chunked = _array_chunks_processed = 0;
 149 }
 150 #endif // TASKQUEUE_STATS
 151 
 152 PSPromotionManager::PSPromotionManager() {
 153   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 154   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 155 
 156   // We set the old lab's start array.
 157   _old_lab.set_start_array(old_gen()->start_array());
 158 
 159   uint queue_size;
 160   claimed_stack_depth()->initialize();
 161   queue_size = claimed_stack_depth()->max_elems();
 162 
 163   _totally_drain = (ParallelGCThreads == 1) || (GCDrainStackTargetSize == 0);
 164   if (_totally_drain) {
 165     _target_stack_size = 0;
 166   } else {
 167     // don't let the target stack size to be more than 1/4 of the entries
 168     _target_stack_size = (uint) MIN2((uint) GCDrainStackTargetSize,
 169                                      (uint) (queue_size / 4));
 170   }
 171 
 172   _array_chunk_size = ParGCArrayScanChunk;
 173   // let's choose 1.5x the chunk size
 174   _min_array_size_for_chunking = 3 * _array_chunk_size / 2;
 175 
 176   reset();
 177 }
 178 
 179 void PSPromotionManager::reset() {
 180   assert(stacks_empty(), "reset of non-empty stack");
 181 
 182   // We need to get an assert in here to make sure the labs are always flushed.
 183 
 184   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 185   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 186 
 187   // Do not prefill the LAB's, save heap wastage!
 188   HeapWord* lab_base = young_space()->top();
 189   _young_lab.initialize(MemRegion(lab_base, (size_t)0));
 190   _young_gen_is_full = false;
 191 
 192   lab_base = old_gen()->object_space()->top();
 193   _old_lab.initialize(MemRegion(lab_base, (size_t)0));
 194   _old_gen_is_full = false;
 195 
 196   _promotion_failed_info.reset();
 197 
 198   TASKQUEUE_STATS_ONLY(reset_stats());
 199 }
 200 
 201 
 202 void PSPromotionManager::drain_stacks_depth(bool totally_drain) {
 203   totally_drain = totally_drain || _totally_drain;
 204 
 205 #ifdef ASSERT
 206   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 207   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 208   MutableSpace* to_space = heap->young_gen()->to_space();
 209   MutableSpace* old_space = heap->old_gen()->object_space();
 210 #endif /* ASSERT */
 211 
 212   OopStarTaskQueue* const tq = claimed_stack_depth();
 213   do {
 214     StarTask p;
 215 
 216     // Drain overflow stack first, so other threads can steal from
 217     // claimed stack while we work.
 218     while (tq->pop_overflow(p)) {
 219       process_popped_location_depth(p);
 220     }
 221 
 222     if (totally_drain) {
 223       while (tq->pop_local(p)) {
 224         process_popped_location_depth(p);
 225       }
 226     } else {
 227       while (tq->size() > _target_stack_size && tq->pop_local(p)) {
 228         process_popped_location_depth(p);
 229       }
 230     }
 231   } while (totally_drain && !tq->taskqueue_empty() || !tq->overflow_empty());
 232 
 233   assert(!totally_drain || tq->taskqueue_empty(), "Sanity");
 234   assert(totally_drain || tq->size() <= _target_stack_size, "Sanity");
 235   assert(tq->overflow_empty(), "Sanity");
 236 }
 237 
 238 void PSPromotionManager::flush_labs() {
 239   assert(stacks_empty(), "Attempt to flush lab with live stack");
 240 
 241   // If either promotion lab fills up, we can flush the
 242   // lab but not refill it, so check first.
 243   assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity");
 244   if (!_young_lab.is_flushed())
 245     _young_lab.flush();
 246 
 247   assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity");
 248   if (!_old_lab.is_flushed())
 249     _old_lab.flush();
 250 
 251   // Let PSScavenge know if we overflowed
 252   if (_young_gen_is_full) {
 253     PSScavenge::set_survivor_overflow(true);
 254   }
 255 }
 256 
 257 template <class T> void PSPromotionManager::process_array_chunk_work(
 258                                                  oop obj,
 259                                                  int start, int end) {
 260   assert(start <= end, "invariant");
 261   T* const base      = (T*)objArrayOop(obj)->base();
 262   T* p               = base + start;
 263   T* const chunk_end = base + end;
 264   while (p < chunk_end) {
 265     if (PSScavenge::should_scavenge(p)) {
 266       claim_or_forward_depth(p);
 267     }
 268     ++p;
 269   }
 270 }
 271 
 272 void PSPromotionManager::process_array_chunk(oop old) {
 273   assert(PSChunkLargeArrays, "invariant");
 274   assert(old->is_objArray(), "invariant");
 275   assert(old->is_forwarded(), "invariant");
 276 
 277   TASKQUEUE_STATS_ONLY(++_array_chunks_processed);
 278 
 279   oop const obj = old->forwardee();
 280 
 281   int start;
 282   int const end = arrayOop(old)->length();
 283   if (end > (int) _min_array_size_for_chunking) {
 284     // we'll chunk more
 285     start = end - _array_chunk_size;
 286     assert(start > 0, "invariant");
 287     arrayOop(old)->set_length(start);
 288     push_depth(mask_chunked_array_oop(old));
 289     TASKQUEUE_STATS_ONLY(++_masked_pushes);
 290   } else {
 291     // this is the final chunk for this array
 292     start = 0;
 293     int const actual_length = arrayOop(obj)->length();
 294     arrayOop(old)->set_length(actual_length);
 295   }
 296 
 297   if (UseCompressedOops) {
 298     process_array_chunk_work<narrowOop>(obj, start, end);
 299   } else {
 300     process_array_chunk_work<oop>(obj, start, end);
 301   }
 302 }
 303 
 304 oop PSPromotionManager::oop_promotion_failed(oop obj, markOop obj_mark) {
 305   assert(_old_gen_is_full || PromotionFailureALot, "Sanity");
 306 
 307   // Attempt to CAS in the header.
 308   // This tests if the header is still the same as when
 309   // this started.  If it is the same (i.e., no forwarding
 310   // pointer has been installed), then this thread owns
 311   // it.
 312   if (obj->cas_forward_to(obj, obj_mark)) {
 313     // We won any races, we "own" this object.
 314     assert(obj == obj->forwardee(), "Sanity");
 315 
 316     _promotion_failed_info.register_copy_failure(obj->size());
 317 
 318     obj->push_contents(this);
 319 
 320     // Save the mark if needed
 321     PSScavenge::oop_promotion_failed(obj, obj_mark);
 322   }  else {
 323     // We lost, someone else "owns" this object
 324     guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed.");
 325 
 326     // No unallocation to worry about.
 327     obj = obj->forwardee();
 328   }
 329 
 330 #ifndef PRODUCT
 331   if (TraceScavenge) {
 332     gclog_or_tty->print_cr("{%s %s " PTR_FORMAT " (%d)}",
 333                            "promotion-failure",
 334                            obj->klass()->internal_name(),
 335                            (void *)obj, obj->size());
 336 
 337   }
 338 #endif
 339 
 340   return obj;
 341 }