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 #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/parallelScavengeHeap.hpp" 29 #include "gc_implementation/parallelScavenge/psOldGen.hpp" 30 #include "gc_implementation/parallelScavenge/psPromotionManager.hpp" 31 #include "gc_implementation/parallelScavenge/psPromotionLAB.inline.hpp" 32 #include "gc_implementation/parallelScavenge/psScavenge.hpp" 33 #include "oops/oop.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(should_scavenge(p, true), "revisiting object?"); 61 assert(ParallelScavengeHeap::heap()->is_in(p), "pointer outside heap"); 62 63 claim_or_forward_internal_depth(p); 64 } 65 66 inline void PSPromotionManager::promotion_trace_event(oop new_obj, oop old_obj, 67 size_t obj_size, 68 uint age, bool tenured, 69 const PSPromotionLAB* lab) { 70 // Skip if memory allocation failed 71 if (new_obj != NULL) { 72 const ParallelScavengeTracer* gc_tracer = PSScavenge::gc_tracer(); 73 74 if (lab != NULL) { 75 // Promotion of object through newly allocated PLAB 76 if (gc_tracer->should_report_promotion_in_new_plab_event()) { 77 size_t obj_bytes = obj_size * HeapWordSize; 78 size_t lab_size = lab->capacity(); 79 gc_tracer->report_promotion_in_new_plab_event(old_obj->klass(), obj_bytes, 80 age, tenured, lab_size); 81 } 82 } else { 83 // Promotion of object directly to heap 84 if (gc_tracer->should_report_promotion_outside_plab_event()) { 85 size_t obj_bytes = obj_size * HeapWordSize; 86 gc_tracer->report_promotion_outside_plab_event(old_obj->klass(), obj_bytes, 87 age, tenured); 88 } 89 } 90 } 91 } 92 93 inline void PSPromotionManager::push_contents(oop obj) { 94 obj->ps_push_contents(this); 95 } 96 // 97 // This method is pretty bulky. It would be nice to split it up 98 // into smaller submethods, but we need to be careful not to hurt 99 // performance. 100 // 101 template<bool promote_immediately> 102 oop PSPromotionManager::copy_to_survivor_space(oop o) { 103 assert(should_scavenge(&o), "Sanity"); 104 105 oop new_obj = NULL; 106 107 // NOTE! We must be very careful with any methods that access the mark 108 // in o. There may be multiple threads racing on it, and it may be forwarded 109 // at any time. Do not use oop methods for accessing the mark! 110 markOop test_mark = o->mark(); 111 112 // The same test as "o->is_forwarded()" 113 if (!test_mark->is_marked()) { 114 bool new_obj_is_tenured = false; 115 size_t new_obj_size = o->size(); 116 117 // Find the objects age, MT safe. 118 uint age = (test_mark->has_displaced_mark_helper() /* o->has_displaced_mark() */) ? 119 test_mark->displaced_mark_helper()->age() : test_mark->age(); 120 121 if (!promote_immediately) { 122 // Try allocating obj in to-space (unless too old) 123 if (age < PSScavenge::tenuring_threshold()) { 124 new_obj = (oop) _young_lab.allocate(new_obj_size); 125 if (new_obj == NULL && !_young_gen_is_full) { 126 // Do we allocate directly, or flush and refill? 127 if (new_obj_size > (YoungPLABSize / 2)) { 128 // Allocate this object directly 129 new_obj = (oop)young_space()->cas_allocate(new_obj_size); 130 promotion_trace_event(new_obj, o, new_obj_size, age, false, NULL); 131 } else { 132 // Flush and fill 133 _young_lab.flush(); 134 135 HeapWord* lab_base = young_space()->cas_allocate(YoungPLABSize); 136 if (lab_base != NULL) { 137 _young_lab.initialize(MemRegion(lab_base, YoungPLABSize)); 138 // Try the young lab allocation again. 139 new_obj = (oop) _young_lab.allocate(new_obj_size); 140 promotion_trace_event(new_obj, o, new_obj_size, age, false, &_young_lab); 141 } else { 142 _young_gen_is_full = true; 143 } 144 } 145 } 146 } 147 } 148 149 // Otherwise try allocating obj tenured 150 if (new_obj == NULL) { 151 #ifndef PRODUCT 152 if (ParallelScavengeHeap::heap()->promotion_should_fail()) { 153 return oop_promotion_failed(o, test_mark); 154 } 155 #endif // #ifndef PRODUCT 156 157 new_obj = (oop) _old_lab.allocate(new_obj_size); 158 new_obj_is_tenured = true; 159 160 if (new_obj == NULL) { 161 if (!_old_gen_is_full) { 162 // Do we allocate directly, or flush and refill? 163 if (new_obj_size > (OldPLABSize / 2)) { 164 // Allocate this object directly 165 new_obj = (oop)old_gen()->cas_allocate(new_obj_size); 166 promotion_trace_event(new_obj, o, new_obj_size, age, true, NULL); 167 } else { 168 // Flush and fill 169 _old_lab.flush(); 170 171 HeapWord* lab_base = old_gen()->cas_allocate(OldPLABSize); 172 if(lab_base != NULL) { 173 #ifdef ASSERT 174 // Delay the initialization of the promotion lab (plab). 175 // This exposes uninitialized plabs to card table processing. 176 if (GCWorkerDelayMillis > 0) { 177 os::sleep(Thread::current(), GCWorkerDelayMillis, false); 178 } 179 #endif 180 _old_lab.initialize(MemRegion(lab_base, OldPLABSize)); 181 // Try the old lab allocation again. 182 new_obj = (oop) _old_lab.allocate(new_obj_size); 183 promotion_trace_event(new_obj, o, new_obj_size, age, true, &_old_lab); 184 } 185 } 186 } 187 188 // This is the promotion failed test, and code handling. 189 // The code belongs here for two reasons. It is slightly 190 // different than the code below, and cannot share the 191 // CAS testing code. Keeping the code here also minimizes 192 // the impact on the common case fast path code. 193 194 if (new_obj == NULL) { 195 _old_gen_is_full = true; 196 return oop_promotion_failed(o, test_mark); 197 } 198 } 199 } 200 201 assert(new_obj != NULL, "allocation should have succeeded"); 202 203 // Copy obj 204 Copy::aligned_disjoint_words((HeapWord*)o, (HeapWord*)new_obj, new_obj_size); 205 206 // Now we have to CAS in the header. 207 if (o->cas_forward_to(new_obj, test_mark)) { 208 // We won any races, we "own" this object. 209 assert(new_obj == o->forwardee(), "Sanity"); 210 211 // Increment age if obj still in new generation. Now that 212 // we're dealing with a markOop that cannot change, it is 213 // okay to use the non mt safe oop methods. 214 if (!new_obj_is_tenured) { 215 new_obj->incr_age(); 216 assert(young_space()->contains(new_obj), "Attempt to push non-promoted obj"); 217 } 218 219 // Do the size comparison first with new_obj_size, which we 220 // already have. Hopefully, only a few objects are larger than 221 // _min_array_size_for_chunking, and most of them will be arrays. 222 // So, the is->objArray() test would be very infrequent. 223 if (new_obj_size > _min_array_size_for_chunking && 224 new_obj->is_objArray() && 225 PSChunkLargeArrays) { 226 // we'll chunk it 227 oop* const masked_o = mask_chunked_array_oop(o); 228 push_depth(masked_o); 229 TASKQUEUE_STATS_ONLY(++_arrays_chunked; ++_masked_pushes); 230 } else { 231 // we'll just push its contents 232 push_contents(new_obj); 233 } 234 } else { 235 // We lost, someone else "owns" this object 236 guarantee(o->is_forwarded(), "Object must be forwarded if the cas failed."); 237 238 // Try to deallocate the space. If it was directly allocated we cannot 239 // deallocate it, so we have to test. If the deallocation fails, 240 // overwrite with a filler object. 241 if (new_obj_is_tenured) { 242 if (!_old_lab.unallocate_object((HeapWord*) new_obj, new_obj_size)) { 243 CollectedHeap::fill_with_object((HeapWord*) new_obj, new_obj_size); 244 } 245 } else if (!_young_lab.unallocate_object((HeapWord*) new_obj, new_obj_size)) { 246 CollectedHeap::fill_with_object((HeapWord*) new_obj, new_obj_size); 247 } 248 249 // don't update this before the unallocation! 250 new_obj = o->forwardee(); 251 } 252 } else { 253 assert(o->is_forwarded(), "Sanity"); 254 new_obj = o->forwardee(); 255 } 256 257 #ifndef PRODUCT 258 // This code must come after the CAS test, or it will print incorrect 259 // information. 260 if (TraceScavenge) { 261 gclog_or_tty->print_cr("{%s %s " PTR_FORMAT " -> " PTR_FORMAT " (%d)}", 262 should_scavenge(&new_obj) ? "copying" : "tenuring", 263 new_obj->klass()->internal_name(), p2i((void *)o), p2i((void *)new_obj), new_obj->size()); 264 } 265 #endif 266 267 return new_obj; 268 } 269 270 // Attempt to "claim" oop at p via CAS, push the new obj if successful 271 // This version tests the oop* to make sure it is within the heap before 272 // attempting marking. 273 template <class T, bool promote_immediately> 274 inline void PSPromotionManager::copy_and_push_safe_barrier(T* p) { 275 assert(should_scavenge(p, true), "revisiting object?"); 276 277 oop o = oopDesc::load_decode_heap_oop_not_null(p); 278 oop new_obj = o->is_forwarded() 279 ? o->forwardee() 280 : copy_to_survivor_space<promote_immediately>(o); 281 282 #ifndef PRODUCT 283 // This code must come after the CAS test, or it will print incorrect 284 // information. 285 if (TraceScavenge && o->is_forwarded()) { 286 gclog_or_tty->print_cr("{%s %s " PTR_FORMAT " -> " PTR_FORMAT " (%d)}", 287 "forwarding", 288 new_obj->klass()->internal_name(), p2i((void *)o), p2i((void *)new_obj), new_obj->size()); 289 } 290 #endif 291 292 oopDesc::encode_store_heap_oop_not_null(p, new_obj); 293 294 // We cannot mark without test, as some code passes us pointers 295 // that are outside the heap. These pointers are either from roots 296 // or from metadata. 297 if ((!PSScavenge::is_obj_in_young((HeapWord*)p)) && 298 ParallelScavengeHeap::heap()->is_in_reserved(p)) { 299 if (PSScavenge::is_obj_in_young(new_obj)) { 300 PSScavenge::card_table()->inline_write_ref_field_gc(p, new_obj); 301 } 302 } 303 } 304 305 inline void PSPromotionManager::process_popped_location_depth(StarTask p) { 306 if (is_oop_masked(p)) { 307 assert(PSChunkLargeArrays, "invariant"); 308 oop const old = unmask_chunked_array_oop(p); 309 process_array_chunk(old); 310 } else { 311 if (p.is_narrow()) { 312 assert(UseCompressedOops, "Error"); 313 copy_and_push_safe_barrier<narrowOop, /*promote_immediately=*/false>(p); 314 } else { 315 copy_and_push_safe_barrier<oop, /*promote_immediately=*/false>(p); 316 } 317 } 318 } 319 320 #if TASKQUEUE_STATS 321 void PSPromotionManager::record_steal(StarTask& p) { 322 if (is_oop_masked(p)) { 323 ++_masked_steals; 324 } 325 } 326 #endif // TASKQUEUE_STATS 327 328 #endif // SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_PSPROMOTIONMANAGER_INLINE_HPP