1 /*
   2  * Copyright (c) 2002, 2015, 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/parallel/mutableSpace.hpp"
  27 #include "gc/parallel/parallelScavengeHeap.hpp"
  28 #include "gc/parallel/psOldGen.hpp"
  29 #include "gc/parallel/psPromotionManager.inline.hpp"
  30 #include "gc/parallel/psScavenge.inline.hpp"
  31 #include "gc/shared/gcTrace.hpp"
  32 #include "gc/shared/taskqueue.inline.hpp"

  33 #include "memory/allocation.inline.hpp"
  34 #include "memory/memRegion.hpp"
  35 #include "memory/padded.inline.hpp"
  36 #include "oops/instanceKlass.inline.hpp"
  37 #include "oops/instanceMirrorKlass.inline.hpp"
  38 #include "oops/objArrayKlass.inline.hpp"
  39 #include "oops/oop.inline.hpp"
  40 
  41 PaddedEnd<PSPromotionManager>* PSPromotionManager::_manager_array = NULL;
  42 OopStarTaskQueueSet*           PSPromotionManager::_stack_array_depth = NULL;
  43 PSOldGen*                      PSPromotionManager::_old_gen = NULL;
  44 MutableSpace*                  PSPromotionManager::_young_space = NULL;
  45 
  46 void PSPromotionManager::initialize() {
  47   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
  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 // Helper functions to get around the circular dependency between
  70 // psScavenge.inline.hpp and psPromotionManager.inline.hpp.
  71 bool PSPromotionManager::should_scavenge(oop* p, bool check_to_space) {
  72   return PSScavenge::should_scavenge(p, check_to_space);
  73 }
  74 bool PSPromotionManager::should_scavenge(narrowOop* p, bool check_to_space) {
  75   return PSScavenge::should_scavenge(p, check_to_space);
  76 }
  77 
  78 PSPromotionManager* PSPromotionManager::gc_thread_promotion_manager(uint index) {
  79   assert(index < ParallelGCThreads, "index out of range");
  80   assert(_manager_array != NULL, "Sanity");
  81   return &_manager_array[index];
  82 }
  83 
  84 PSPromotionManager* PSPromotionManager::vm_thread_promotion_manager() {
  85   assert(_manager_array != NULL, "Sanity");
  86   return &_manager_array[ParallelGCThreads];
  87 }
  88 
  89 void PSPromotionManager::pre_scavenge() {
  90   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
  91 
  92   _young_space = heap->young_gen()->to_space();
  93 
  94   for(uint i=0; i<ParallelGCThreads+1; i++) {
  95     manager_array(i)->reset();
  96   }
  97 }
  98 
  99 bool PSPromotionManager::post_scavenge(YoungGCTracer& gc_tracer) {
 100   bool promotion_failure_occurred = false;
 101 
 102   TASKQUEUE_STATS_ONLY(if (PrintTaskqueue) print_taskqueue_stats());
 103   for (uint i = 0; i < ParallelGCThreads + 1; i++) {
 104     PSPromotionManager* manager = manager_array(i);
 105     assert(manager->claimed_stack_depth()->is_empty(), "should be empty");
 106     if (manager->_promotion_failed_info.has_failed()) {
 107       gc_tracer.report_promotion_failed(manager->_promotion_failed_info);
 108       promotion_failure_occurred = true;
 109     }
 110     manager->flush_labs();
 111   }
 112   return promotion_failure_occurred;
 113 }
 114 
 115 #if TASKQUEUE_STATS
 116 void
 117 PSPromotionManager::print_local_stats(outputStream* const out, uint i) const {
 118   #define FMT " " SIZE_FORMAT_W(10)
 119   out->print_cr("%3u" FMT FMT FMT FMT, i, _masked_pushes, _masked_steals,
 120                 _arrays_chunked, _array_chunks_processed);
 121   #undef FMT
 122 }
 123 
 124 static const char* const pm_stats_hdr[] = {
 125   "    --------masked-------     arrays      array",
 126   "thr       push      steal    chunked     chunks",
 127   "--- ---------- ---------- ---------- ----------"
 128 };
 129 
 130 void
 131 PSPromotionManager::print_taskqueue_stats(outputStream* const out) {






 132   out->print_cr("== GC Tasks Stats, GC %3d",
 133                 ParallelScavengeHeap::heap()->total_collections());
 134 
 135   TaskQueueStats totals;
 136   out->print("thr "); TaskQueueStats::print_header(1, out); out->cr();
 137   out->print("--- "); TaskQueueStats::print_header(2, out); out->cr();
 138   for (uint i = 0; i < ParallelGCThreads + 1; ++i) {
 139     TaskQueueStats& next = manager_array(i)->_claimed_stack_depth.stats;
 140     out->print("%3d ", i); next.print(out); out->cr();
 141     totals += next;
 142   }
 143   out->print("tot "); totals.print(out); out->cr();
 144 
 145   const uint hlines = sizeof(pm_stats_hdr) / sizeof(pm_stats_hdr[0]);
 146   for (uint i = 0; i < hlines; ++i) out->print_cr("%s", pm_stats_hdr[i]);
 147   for (uint i = 0; i < ParallelGCThreads + 1; ++i) {
 148     manager_array(i)->print_local_stats(out, i);
 149   }
 150 }
 151 
 152 void
 153 PSPromotionManager::reset_stats() {
 154   claimed_stack_depth()->stats.reset();
 155   _masked_pushes = _masked_steals = 0;
 156   _arrays_chunked = _array_chunks_processed = 0;
 157 }
 158 #endif // TASKQUEUE_STATS
 159 
 160 PSPromotionManager::PSPromotionManager() {
 161   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 162 
 163   // We set the old lab's start array.
 164   _old_lab.set_start_array(old_gen()->start_array());
 165 
 166   uint queue_size;
 167   claimed_stack_depth()->initialize();
 168   queue_size = claimed_stack_depth()->max_elems();
 169 
 170   _totally_drain = (ParallelGCThreads == 1) || (GCDrainStackTargetSize == 0);
 171   if (_totally_drain) {
 172     _target_stack_size = 0;
 173   } else {
 174     // don't let the target stack size to be more than 1/4 of the entries
 175     _target_stack_size = (uint) MIN2((uint) GCDrainStackTargetSize,
 176                                      (uint) (queue_size / 4));
 177   }
 178 
 179   _array_chunk_size = ParGCArrayScanChunk;
 180   // let's choose 1.5x the chunk size
 181   _min_array_size_for_chunking = 3 * _array_chunk_size / 2;
 182 
 183   reset();
 184 }
 185 
 186 void PSPromotionManager::reset() {
 187   assert(stacks_empty(), "reset of non-empty stack");
 188 
 189   // We need to get an assert in here to make sure the labs are always flushed.
 190 
 191   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 192 
 193   // Do not prefill the LAB's, save heap wastage!
 194   HeapWord* lab_base = young_space()->top();
 195   _young_lab.initialize(MemRegion(lab_base, (size_t)0));
 196   _young_gen_is_full = false;
 197 
 198   lab_base = old_gen()->object_space()->top();
 199   _old_lab.initialize(MemRegion(lab_base, (size_t)0));
 200   _old_gen_is_full = false;
 201 
 202   _promotion_failed_info.reset();
 203 
 204   TASKQUEUE_STATS_ONLY(reset_stats());
 205 }
 206 
 207 
 208 void PSPromotionManager::drain_stacks_depth(bool totally_drain) {
 209   totally_drain = totally_drain || _totally_drain;
 210 
 211 #ifdef ASSERT
 212   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 213   MutableSpace* to_space = heap->young_gen()->to_space();
 214   MutableSpace* old_space = heap->old_gen()->object_space();
 215 #endif /* ASSERT */
 216 
 217   OopStarTaskQueue* const tq = claimed_stack_depth();
 218   do {
 219     StarTask p;
 220 
 221     // Drain overflow stack first, so other threads can steal from
 222     // claimed stack while we work.
 223     while (tq->pop_overflow(p)) {
 224       process_popped_location_depth(p);
 225     }
 226 
 227     if (totally_drain) {
 228       while (tq->pop_local(p)) {
 229         process_popped_location_depth(p);
 230       }
 231     } else {
 232       while (tq->size() > _target_stack_size && tq->pop_local(p)) {
 233         process_popped_location_depth(p);
 234       }
 235     }
 236   } while (totally_drain && !tq->taskqueue_empty() || !tq->overflow_empty());
 237 
 238   assert(!totally_drain || tq->taskqueue_empty(), "Sanity");
 239   assert(totally_drain || tq->size() <= _target_stack_size, "Sanity");
 240   assert(tq->overflow_empty(), "Sanity");
 241 }
 242 
 243 void PSPromotionManager::flush_labs() {
 244   assert(stacks_empty(), "Attempt to flush lab with live stack");
 245 
 246   // If either promotion lab fills up, we can flush the
 247   // lab but not refill it, so check first.
 248   assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity");
 249   if (!_young_lab.is_flushed())
 250     _young_lab.flush();
 251 
 252   assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity");
 253   if (!_old_lab.is_flushed())
 254     _old_lab.flush();
 255 
 256   // Let PSScavenge know if we overflowed
 257   if (_young_gen_is_full) {
 258     PSScavenge::set_survivor_overflow(true);
 259   }
 260 }
 261 
 262 template <class T> void PSPromotionManager::process_array_chunk_work(
 263                                                  oop obj,
 264                                                  int start, int end) {
 265   assert(start <= end, "invariant");
 266   T* const base      = (T*)objArrayOop(obj)->base();
 267   T* p               = base + start;
 268   T* const chunk_end = base + end;
 269   while (p < chunk_end) {
 270     if (PSScavenge::should_scavenge(p)) {
 271       claim_or_forward_depth(p);
 272     }
 273     ++p;
 274   }
 275 }
 276 
 277 void PSPromotionManager::process_array_chunk(oop old) {
 278   assert(PSChunkLargeArrays, "invariant");
 279   assert(old->is_objArray(), "invariant");
 280   assert(old->is_forwarded(), "invariant");
 281 
 282   TASKQUEUE_STATS_ONLY(++_array_chunks_processed);
 283 
 284   oop const obj = old->forwardee();
 285 
 286   int start;
 287   int const end = arrayOop(old)->length();
 288   if (end > (int) _min_array_size_for_chunking) {
 289     // we'll chunk more
 290     start = end - _array_chunk_size;
 291     assert(start > 0, "invariant");
 292     arrayOop(old)->set_length(start);
 293     push_depth(mask_chunked_array_oop(old));
 294     TASKQUEUE_STATS_ONLY(++_masked_pushes);
 295   } else {
 296     // this is the final chunk for this array
 297     start = 0;
 298     int const actual_length = arrayOop(obj)->length();
 299     arrayOop(old)->set_length(actual_length);
 300   }
 301 
 302   if (UseCompressedOops) {
 303     process_array_chunk_work<narrowOop>(obj, start, end);
 304   } else {
 305     process_array_chunk_work<oop>(obj, start, end);
 306   }
 307 }
 308 
 309 class PushContentsClosure : public ExtendedOopClosure {
 310   PSPromotionManager* _pm;
 311  public:
 312   PushContentsClosure(PSPromotionManager* pm) : _pm(pm) {}
 313 
 314   template <typename T> void do_oop_nv(T* p) {
 315     if (PSScavenge::should_scavenge(p)) {
 316       _pm->claim_or_forward_depth(p);
 317     }
 318   }
 319 
 320   virtual void do_oop(oop* p)       { do_oop_nv(p); }
 321   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 322 
 323   // Don't use the oop verification code in the oop_oop_iterate framework.
 324   debug_only(virtual bool should_verify_oops() { return false; })
 325 };
 326 
 327 void InstanceKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) {
 328   PushContentsClosure cl(pm);
 329   oop_oop_iterate_oop_maps_reverse<true>(obj, &cl);
 330 }
 331 
 332 void InstanceMirrorKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) {
 333     // Note that we don't have to follow the mirror -> klass pointer, since all
 334     // klasses that are dirty will be scavenged when we iterate over the
 335     // ClassLoaderData objects.
 336 
 337   InstanceKlass::oop_ps_push_contents(obj, pm);
 338 
 339   PushContentsClosure cl(pm);
 340   oop_oop_iterate_statics<true>(obj, &cl);
 341 }
 342 
 343 void InstanceClassLoaderKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) {
 344   InstanceKlass::oop_ps_push_contents(obj, pm);
 345 
 346   // This is called by the young collector. It will already have taken care of
 347   // all class loader data. So, we don't have to follow the class loader ->
 348   // class loader data link.
 349 }
 350 
 351 template <class T>
 352 static void oop_ps_push_contents_specialized(oop obj, InstanceRefKlass *klass, PSPromotionManager* pm) {
 353   T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);
 354   if (PSScavenge::should_scavenge(referent_addr)) {
 355     ReferenceProcessor* rp = PSScavenge::reference_processor();
 356     if (rp->discover_reference(obj, klass->reference_type())) {
 357       // reference already enqueued, referent and next will be traversed later
 358       klass->InstanceKlass::oop_ps_push_contents(obj, pm);
 359       return;
 360     } else {
 361       // treat referent as normal oop
 362       pm->claim_or_forward_depth(referent_addr);
 363     }
 364   }
 365   // Treat discovered as normal oop, if ref is not "active",
 366   // i.e. if next is non-NULL.
 367   T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);
 368   T  next_oop = oopDesc::load_heap_oop(next_addr);
 369   if (!oopDesc::is_null(next_oop)) { // i.e. ref is not "active"
 370     T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);
 371     debug_only(
 372       if(TraceReferenceGC && PrintGCDetails) {
 373         gclog_or_tty->print_cr("   Process discovered as normal "
 374                                PTR_FORMAT, p2i(discovered_addr));
 375       }
 376     )
 377     if (PSScavenge::should_scavenge(discovered_addr)) {
 378       pm->claim_or_forward_depth(discovered_addr);
 379     }
 380   }
 381   // Treat next as normal oop;  next is a link in the reference queue.
 382   if (PSScavenge::should_scavenge(next_addr)) {
 383     pm->claim_or_forward_depth(next_addr);
 384   }
 385   klass->InstanceKlass::oop_ps_push_contents(obj, pm);
 386 }
 387 
 388 void InstanceRefKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) {
 389   if (UseCompressedOops) {
 390     oop_ps_push_contents_specialized<narrowOop>(obj, this, pm);
 391   } else {
 392     oop_ps_push_contents_specialized<oop>(obj, this, pm);
 393   }
 394 }
 395 
 396 void ObjArrayKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) {
 397   assert(obj->is_objArray(), "obj must be obj array");
 398   PushContentsClosure cl(pm);
 399   oop_oop_iterate_elements<true>(objArrayOop(obj), &cl);
 400 }
 401 
 402 void TypeArrayKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) {
 403   assert(obj->is_typeArray(),"must be a type array");
 404   ShouldNotReachHere();
 405 }
 406 
 407 oop PSPromotionManager::oop_promotion_failed(oop obj, markOop obj_mark) {
 408   assert(_old_gen_is_full || PromotionFailureALot, "Sanity");
 409 
 410   // Attempt to CAS in the header.
 411   // This tests if the header is still the same as when
 412   // this started.  If it is the same (i.e., no forwarding
 413   // pointer has been installed), then this thread owns
 414   // it.
 415   if (obj->cas_forward_to(obj, obj_mark)) {
 416     // We won any races, we "own" this object.
 417     assert(obj == obj->forwardee(), "Sanity");
 418 
 419     _promotion_failed_info.register_copy_failure(obj->size());
 420 
 421     push_contents(obj);
 422 
 423     // Save the mark if needed
 424     PSScavenge::oop_promotion_failed(obj, obj_mark);
 425   }  else {
 426     // We lost, someone else "owns" this object
 427     guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed.");
 428 
 429     // No unallocation to worry about.
 430     obj = obj->forwardee();
 431   }
 432 
 433 #ifndef PRODUCT
 434   if (TraceScavenge) {
 435     gclog_or_tty->print_cr("{%s %s " PTR_FORMAT " (%d)}",
 436                            "promotion-failure",
 437                            obj->klass()->internal_name(),
 438                            p2i(obj), obj->size());
 439 
 440   }
 441 #endif
 442 
 443   return obj;
 444 }
--- EOF ---