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 #ifndef SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_PSPROMOTIONMANAGER_INLINE_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_PSPROMOTIONMANAGER_INLINE_HPP
  27 
  28 #include "gc_implementation/parallelScavenge/psOldGen.hpp"
  29 #include "gc_implementation/parallelScavenge/psPromotionManager.hpp"
  30 #include "gc_implementation/parallelScavenge/psPromotionLAB.inline.hpp"
  31 #include "gc_implementation/parallelScavenge/psScavenge.hpp"
  32 #include "gc_interface/collectedHeap.inline.hpp"
  33 #include "oops/oop.psgc.inline.hpp"
  34 
  35 inline PSPromotionManager* PSPromotionManager::manager_array(int index) {
  36   assert(_manager_array != NULL, "access of NULL manager_array");
  37   assert(index >= 0 && index <= (int)ParallelGCThreads, "out of range manager_array access");
  38   return &_manager_array[index];
  39 }
  40 
  41 template <class T>
  42 inline void PSPromotionManager::claim_or_forward_internal_depth(T* p) {
  43   if (p != NULL) { // XXX: error if p != NULL here
  44     oop o = oopDesc::load_decode_heap_oop_not_null(p);
  45     if (o->is_forwarded()) {
  46       o = o->forwardee();
  47       // Card mark
  48       if (PSScavenge::is_obj_in_young(o)) {
  49         PSScavenge::card_table()->inline_write_ref_field_gc(p, o);
  50       }
  51       oopDesc::encode_store_heap_oop_not_null(p, o);
  52     } else {
  53       push_depth(p);
  54     }
  55   }
  56 }
  57 
  58 template <class T>
  59 inline void PSPromotionManager::claim_or_forward_depth(T* p) {
  60   assert(PSScavenge::should_scavenge(p, true), "revisiting object?");
  61   assert(Universe::heap()->kind() == CollectedHeap::ParallelScavengeHeap,
  62          "Sanity");
  63   assert(Universe::heap()->is_in(p), "pointer outside heap");
  64 
  65   claim_or_forward_internal_depth(p);
  66 }
  67 
  68 //
  69 // This method is pretty bulky. It would be nice to split it up
  70 // into smaller submethods, but we need to be careful not to hurt
  71 // performance.
  72 //
  73 template<bool promote_immediately>
  74 oop PSPromotionManager::copy_to_survivor_space(oop o) {
  75   assert(PSScavenge::should_scavenge(&o), "Sanity");
  76 
  77   oop new_obj = NULL;
  78 
  79   // NOTE! We must be very careful with any methods that access the mark
  80   // in o. There may be multiple threads racing on it, and it may be forwarded
  81   // at any time. Do not use oop methods for accessing the mark!
  82   markOop test_mark = o->mark();
  83 
  84   // The same test as "o->is_forwarded()"
  85   if (!test_mark->is_marked()) {
  86     bool new_obj_is_tenured = false;
  87     size_t new_obj_size = o->size();
  88 
  89     if (!promote_immediately) {
  90       // Find the objects age, MT safe.
  91       uint age = (test_mark->has_displaced_mark_helper() /* o->has_displaced_mark() */) ?
  92         test_mark->displaced_mark_helper()->age() : test_mark->age();
  93 
  94       // Try allocating obj in to-space (unless too old)
  95       if (age < PSScavenge::tenuring_threshold()) {
  96         new_obj = (oop) _young_lab.allocate(new_obj_size);
  97         if (new_obj == NULL && !_young_gen_is_full) {
  98           // Do we allocate directly, or flush and refill?
  99           if (new_obj_size > (YoungPLABSize / 2)) {
 100             // Allocate this object directly
 101             new_obj = (oop)young_space()->cas_allocate(new_obj_size);
 102           } else {
 103             // Flush and fill
 104             _young_lab.flush();
 105 
 106             HeapWord* lab_base = young_space()->cas_allocate(YoungPLABSize);
 107             if (lab_base != NULL) {
 108               _young_lab.initialize(MemRegion(lab_base, YoungPLABSize));
 109               // Try the young lab allocation again.
 110               new_obj = (oop) _young_lab.allocate(new_obj_size);
 111             } else {
 112               _young_gen_is_full = true;
 113             }
 114           }
 115         }
 116       }
 117     }
 118 
 119     // Otherwise try allocating obj tenured
 120     if (new_obj == NULL) {
 121 #ifndef PRODUCT
 122       if (Universe::heap()->promotion_should_fail()) {
 123         return oop_promotion_failed(o, test_mark);
 124       }
 125 #endif  // #ifndef PRODUCT
 126 
 127       new_obj = (oop) _old_lab.allocate(new_obj_size);
 128       new_obj_is_tenured = true;
 129 
 130       if (new_obj == NULL) {
 131         if (!_old_gen_is_full) {
 132           // Do we allocate directly, or flush and refill?
 133           if (new_obj_size > (OldPLABSize / 2)) {
 134             // Allocate this object directly
 135             new_obj = (oop)old_gen()->cas_allocate(new_obj_size);
 136           } else {
 137             // Flush and fill
 138             _old_lab.flush();
 139 
 140             HeapWord* lab_base = old_gen()->cas_allocate(OldPLABSize);
 141             if(lab_base != NULL) {
 142 #ifdef ASSERT
 143               // Delay the initialization of the promotion lab (plab).
 144               // This exposes uninitialized plabs to card table processing.
 145               if (GCWorkerDelayMillis > 0) {
 146                 os::sleep(Thread::current(), GCWorkerDelayMillis, false);
 147               }
 148 #endif
 149               _old_lab.initialize(MemRegion(lab_base, OldPLABSize));
 150               // Try the old lab allocation again.
 151               new_obj = (oop) _old_lab.allocate(new_obj_size);
 152             }
 153           }
 154         }
 155 
 156         // This is the promotion failed test, and code handling.
 157         // The code belongs here for two reasons. It is slightly
 158         // different than the code below, and cannot share the
 159         // CAS testing code. Keeping the code here also minimizes
 160         // the impact on the common case fast path code.
 161 
 162         if (new_obj == NULL) {
 163           _old_gen_is_full = true;
 164           return oop_promotion_failed(o, test_mark);
 165         }
 166       }
 167     }
 168 
 169     assert(new_obj != NULL, "allocation should have succeeded");
 170 
 171     // Copy obj
 172     Copy::aligned_disjoint_words((HeapWord*)o, (HeapWord*)new_obj, new_obj_size);
 173 
 174     // Now we have to CAS in the header.
 175     if (o->cas_forward_to(new_obj, test_mark)) {
 176       // We won any races, we "own" this object.
 177       assert(new_obj == o->forwardee(), "Sanity");
 178 
 179       // Increment age if obj still in new generation. Now that
 180       // we're dealing with a markOop that cannot change, it is
 181       // okay to use the non mt safe oop methods.
 182       if (!new_obj_is_tenured) {
 183         new_obj->incr_age();
 184         assert(young_space()->contains(new_obj), "Attempt to push non-promoted obj");
 185       }
 186 
 187       // Do the size comparison first with new_obj_size, which we
 188       // already have. Hopefully, only a few objects are larger than
 189       // _min_array_size_for_chunking, and most of them will be arrays.
 190       // So, the is->objArray() test would be very infrequent.
 191       if (new_obj_size > _min_array_size_for_chunking &&
 192           new_obj->is_objArray() &&
 193           PSChunkLargeArrays) {
 194         // we'll chunk it
 195         oop* const masked_o = mask_chunked_array_oop(o);
 196         push_depth(masked_o);
 197         TASKQUEUE_STATS_ONLY(++_arrays_chunked; ++_masked_pushes);
 198       } else {
 199         // we'll just push its contents
 200         new_obj->push_contents(this);
 201       }
 202     }  else {
 203       // We lost, someone else "owns" this object
 204       guarantee(o->is_forwarded(), "Object must be forwarded if the cas failed.");
 205 
 206       // Try to deallocate the space.  If it was directly allocated we cannot
 207       // deallocate it, so we have to test.  If the deallocation fails,
 208       // overwrite with a filler object.
 209       if (new_obj_is_tenured) {
 210         if (!_old_lab.unallocate_object((HeapWord*) new_obj, new_obj_size)) {
 211           CollectedHeap::fill_with_object((HeapWord*) new_obj, new_obj_size);
 212         }
 213       } else if (!_young_lab.unallocate_object((HeapWord*) new_obj, new_obj_size)) {
 214         CollectedHeap::fill_with_object((HeapWord*) new_obj, new_obj_size);
 215       }
 216 
 217       // don't update this before the unallocation!
 218       new_obj = o->forwardee();
 219     }
 220   } else {
 221     assert(o->is_forwarded(), "Sanity");
 222     new_obj = o->forwardee();
 223   }
 224 
 225 #ifndef PRODUCT
 226   // This code must come after the CAS test, or it will print incorrect
 227   // information.
 228   if (TraceScavenge) {
 229     gclog_or_tty->print_cr("{%s %s " PTR_FORMAT " -> " PTR_FORMAT " (%d)}",
 230        PSScavenge::should_scavenge(&new_obj) ? "copying" : "tenuring",
 231        new_obj->klass()->internal_name(), p2i((void *)o), p2i((void *)new_obj), new_obj->size());
 232   }
 233 #endif
 234 
 235   return new_obj;
 236 }
 237 
 238 
 239 inline void PSPromotionManager::process_popped_location_depth(StarTask p) {
 240   if (is_oop_masked(p)) {
 241     assert(PSChunkLargeArrays, "invariant");
 242     oop const old = unmask_chunked_array_oop(p);
 243     process_array_chunk(old);
 244   } else {
 245     if (p.is_narrow()) {
 246       assert(UseCompressedOops, "Error");
 247       PSScavenge::copy_and_push_safe_barrier<narrowOop, /*promote_immediately=*/false>(this, p);
 248     } else {
 249       PSScavenge::copy_and_push_safe_barrier<oop, /*promote_immediately=*/false>(this, p);
 250     }
 251   }
 252 }
 253 
 254 #if TASKQUEUE_STATS
 255 void PSPromotionManager::record_steal(StarTask& p) {
 256   if (is_oop_masked(p)) {
 257     ++_masked_steals;
 258   }
 259 }
 260 #endif // TASKQUEUE_STATS
 261 
 262 #endif // SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_PSPROMOTIONMANAGER_INLINE_HPP