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_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 "utilities/stack.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 // 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(int index) { 79 assert(index >= 0 && index < (int)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*)Universe::heap(); 91 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 92 93 _young_space = heap->young_gen()->to_space(); 94 95 for(uint i=0; i<ParallelGCThreads+1; i++) { 96 manager_array(i)->reset(); 97 } 98 } 99 100 bool PSPromotionManager::post_scavenge(YoungGCTracer& gc_tracer) { 101 bool promotion_failure_occurred = false; 102 103 TASKQUEUE_STATS_ONLY(if (PrintTaskqueue) print_taskqueue_stats()); 104 for (uint i = 0; i < ParallelGCThreads + 1; i++) { 105 PSPromotionManager* manager = manager_array(i); 106 assert(manager->claimed_stack_depth()->is_empty(), "should be empty"); 107 if (manager->_promotion_failed_info.has_failed()) { 108 gc_tracer.report_promotion_failed(manager->_promotion_failed_info); 109 promotion_failure_occurred = true; 110 } 111 manager->flush_labs(); 112 } 113 return promotion_failure_occurred; 114 } 115 116 #if TASKQUEUE_STATS 117 void 118 PSPromotionManager::print_local_stats(outputStream* const out, uint i) const { 119 #define FMT " " SIZE_FORMAT_W(10) 120 out->print_cr("%3u" FMT FMT FMT FMT, i, _masked_pushes, _masked_steals, 121 _arrays_chunked, _array_chunks_processed); 122 #undef FMT 123 } 124 125 static const char* const pm_stats_hdr[] = { 126 " --------masked------- arrays array", 127 "thr push steal chunked chunks", 128 "--- ---------- ---------- ---------- ----------" 129 }; 130 131 void 132 PSPromotionManager::print_taskqueue_stats(outputStream* const out) { 133 out->print_cr("== GC Tasks Stats, GC %3d", 134 Universe::heap()->total_collections()); 135 136 TaskQueueStats totals; 137 out->print("thr "); TaskQueueStats::print_header(1, out); out->cr(); 138 out->print("--- "); TaskQueueStats::print_header(2, out); out->cr(); 139 for (uint i = 0; i < ParallelGCThreads + 1; ++i) { 140 TaskQueueStats& next = manager_array(i)->_claimed_stack_depth.stats; 141 out->print("%3d ", i); next.print(out); out->cr(); 142 totals += next; 143 } 144 out->print("tot "); totals.print(out); out->cr(); 145 146 const uint hlines = sizeof(pm_stats_hdr) / sizeof(pm_stats_hdr[0]); 147 for (uint i = 0; i < hlines; ++i) out->print_cr("%s", pm_stats_hdr[i]); 148 for (uint i = 0; i < ParallelGCThreads + 1; ++i) { 149 manager_array(i)->print_local_stats(out, i); 150 } 151 } 152 153 void 154 PSPromotionManager::reset_stats() { 155 claimed_stack_depth()->stats.reset(); 156 _masked_pushes = _masked_steals = 0; 157 _arrays_chunked = _array_chunks_processed = 0; 158 } 159 #endif // TASKQUEUE_STATS 160 161 PSPromotionManager::PSPromotionManager() { 162 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 163 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 164 165 // We set the old lab's start array. 166 _old_lab.set_start_array(old_gen()->start_array()); 167 168 uint queue_size; 169 claimed_stack_depth()->initialize(); 170 queue_size = claimed_stack_depth()->max_elems(); 171 172 _totally_drain = (ParallelGCThreads == 1) || (GCDrainStackTargetSize == 0); 173 if (_totally_drain) { 174 _target_stack_size = 0; 175 } else { 176 // don't let the target stack size to be more than 1/4 of the entries 177 _target_stack_size = (uint) MIN2((uint) GCDrainStackTargetSize, 178 (uint) (queue_size / 4)); 179 } 180 181 _array_chunk_size = ParGCArrayScanChunk; 182 // let's choose 1.5x the chunk size 183 _min_array_size_for_chunking = 3 * _array_chunk_size / 2; 184 185 reset(); 186 } 187 188 void PSPromotionManager::reset() { 189 assert(stacks_empty(), "reset of non-empty stack"); 190 191 // We need to get an assert in here to make sure the labs are always flushed. 192 193 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 194 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 195 196 // Do not prefill the LAB's, save heap wastage! 197 HeapWord* lab_base = young_space()->top(); 198 _young_lab.initialize(MemRegion(lab_base, (size_t)0)); 199 _young_gen_is_full = false; 200 201 lab_base = old_gen()->object_space()->top(); 202 _old_lab.initialize(MemRegion(lab_base, (size_t)0)); 203 _old_gen_is_full = false; 204 205 _promotion_failed_info.reset(); 206 207 TASKQUEUE_STATS_ONLY(reset_stats()); 208 } 209 210 211 void PSPromotionManager::drain_stacks_depth(bool totally_drain) { 212 totally_drain = totally_drain || _totally_drain; 213 214 #ifdef ASSERT 215 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 216 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 217 MutableSpace* to_space = heap->young_gen()->to_space(); 218 MutableSpace* old_space = heap->old_gen()->object_space(); 219 #endif /* ASSERT */ 220 221 OopStarTaskQueue* const tq = claimed_stack_depth(); 222 do { 223 StarTask p; 224 225 // Drain overflow stack first, so other threads can steal from 226 // claimed stack while we work. 227 while (tq->pop_overflow(p)) { 228 process_popped_location_depth(p); 229 } 230 231 if (totally_drain) { 232 while (tq->pop_local(p)) { 233 process_popped_location_depth(p); 234 } 235 } else { 236 while (tq->size() > _target_stack_size && tq->pop_local(p)) { 237 process_popped_location_depth(p); 238 } 239 } 240 } while (totally_drain && !tq->taskqueue_empty() || !tq->overflow_empty()); 241 242 assert(!totally_drain || tq->taskqueue_empty(), "Sanity"); 243 assert(totally_drain || tq->size() <= _target_stack_size, "Sanity"); 244 assert(tq->overflow_empty(), "Sanity"); 245 } 246 247 void PSPromotionManager::flush_labs() { 248 assert(stacks_empty(), "Attempt to flush lab with live stack"); 249 250 // If either promotion lab fills up, we can flush the 251 // lab but not refill it, so check first. 252 assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity"); 253 if (!_young_lab.is_flushed()) 254 _young_lab.flush(); 255 256 assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity"); 257 if (!_old_lab.is_flushed()) 258 _old_lab.flush(); 259 260 // Let PSScavenge know if we overflowed 261 if (_young_gen_is_full) { 262 PSScavenge::set_survivor_overflow(true); 263 } 264 } 265 266 template <class T> void PSPromotionManager::process_array_chunk_work( 267 oop obj, 268 int start, int end) { 269 assert(start <= end, "invariant"); 270 T* const base = (T*)objArrayOop(obj)->base(); 271 T* p = base + start; 272 T* const chunk_end = base + end; 273 while (p < chunk_end) { 274 if (PSScavenge::should_scavenge(p)) { 275 claim_or_forward_depth(p); 276 } 277 ++p; 278 } 279 } 280 281 void PSPromotionManager::process_array_chunk(oop old) { 282 assert(PSChunkLargeArrays, "invariant"); 283 assert(old->is_objArray(), "invariant"); 284 assert(old->is_forwarded(), "invariant"); 285 286 TASKQUEUE_STATS_ONLY(++_array_chunks_processed); 287 288 oop const obj = old->forwardee(); 289 290 int start; 291 int const end = arrayOop(old)->length(); 292 if (end > (int) _min_array_size_for_chunking) { 293 // we'll chunk more 294 start = end - _array_chunk_size; 295 assert(start > 0, "invariant"); 296 arrayOop(old)->set_length(start); 297 push_depth(mask_chunked_array_oop(old)); 298 TASKQUEUE_STATS_ONLY(++_masked_pushes); 299 } else { 300 // this is the final chunk for this array 301 start = 0; 302 int const actual_length = arrayOop(obj)->length(); 303 arrayOop(old)->set_length(actual_length); 304 } 305 306 if (UseCompressedOops) { 307 process_array_chunk_work<narrowOop>(obj, start, end); 308 } else { 309 process_array_chunk_work<oop>(obj, start, end); 310 } 311 } 312 313 oop PSPromotionManager::oop_promotion_failed(oop obj, markOop obj_mark) { 314 assert(_old_gen_is_full || PromotionFailureALot, "Sanity"); 315 316 // Attempt to CAS in the header. 317 // This tests if the header is still the same as when 318 // this started. If it is the same (i.e., no forwarding 319 // pointer has been installed), then this thread owns 320 // it. 321 if (obj->cas_forward_to(obj, obj_mark)) { 322 // We won any races, we "own" this object. 323 assert(obj == obj->forwardee(), "Sanity"); 324 325 _promotion_failed_info.register_copy_failure(obj->size()); 326 327 push_contents(obj); 328 329 // Save the mark if needed 330 PSScavenge::oop_promotion_failed(obj, obj_mark); 331 } else { 332 // We lost, someone else "owns" this object 333 guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed."); 334 335 // No unallocation to worry about. 336 obj = obj->forwardee(); 337 } 338 339 #ifndef PRODUCT 340 if (TraceScavenge) { 341 gclog_or_tty->print_cr("{%s %s " PTR_FORMAT " (%d)}", 342 "promotion-failure", 343 obj->klass()->internal_name(), 344 (void *)obj, obj->size()); 345 346 } 347 #endif 348 349 return obj; 350 }