duke@435: /* sla@5237: * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/psOldGen.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp" sla@5237: #include "gc_implementation/shared/gcTrace.hpp" stefank@2314: #include "gc_implementation/shared/mutableSpace.hpp" stefank@5515: #include "memory/allocation.inline.hpp" stefank@2314: #include "memory/memRegion.hpp" stefank@5515: #include "memory/padded.inline.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "oops/oop.psgc.inline.hpp" duke@435: stefank@5515: PaddedEnd* PSPromotionManager::_manager_array = NULL; stefank@5515: OopStarTaskQueueSet* PSPromotionManager::_stack_array_depth = NULL; stefank@5515: PSOldGen* PSPromotionManager::_old_gen = NULL; stefank@5515: MutableSpace* PSPromotionManager::_young_space = NULL; duke@435: duke@435: void PSPromotionManager::initialize() { duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); duke@435: duke@435: _old_gen = heap->old_gen(); duke@435: _young_space = heap->young_gen()->to_space(); duke@435: stefank@5515: // To prevent false sharing, we pad the PSPromotionManagers stefank@5515: // and make sure that the first instance starts at a cache line. duke@435: assert(_manager_array == NULL, "Attempt to initialize twice"); stefank@5515: _manager_array = PaddedArray::create_unfreeable(ParallelGCThreads + 1); duke@435: guarantee(_manager_array != NULL, "Could not initialize promotion manager"); duke@435: tonyp@2061: _stack_array_depth = new OopStarTaskQueueSet(ParallelGCThreads); sla@5237: guarantee(_stack_array_depth != NULL, "Could not initialize promotion manager"); duke@435: duke@435: // Create and register the PSPromotionManager(s) for the worker threads. duke@435: for(uint i=0; iregister_queue(i, _manager_array[i].claimed_stack_depth()); duke@435: } duke@435: // The VMThread gets its own PSPromotionManager, which is not available duke@435: // for work stealing. duke@435: } duke@435: duke@435: PSPromotionManager* PSPromotionManager::gc_thread_promotion_manager(int index) { duke@435: assert(index >= 0 && index < (int)ParallelGCThreads, "index out of range"); duke@435: assert(_manager_array != NULL, "Sanity"); stefank@5515: return &_manager_array[index]; duke@435: } duke@435: duke@435: PSPromotionManager* PSPromotionManager::vm_thread_promotion_manager() { duke@435: assert(_manager_array != NULL, "Sanity"); stefank@5515: return &_manager_array[ParallelGCThreads]; duke@435: } duke@435: duke@435: void PSPromotionManager::pre_scavenge() { duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); duke@435: duke@435: _young_space = heap->young_gen()->to_space(); duke@435: duke@435: for(uint i=0; ireset(); duke@435: } duke@435: } duke@435: sla@5237: bool PSPromotionManager::post_scavenge(YoungGCTracer& gc_tracer) { sla@5237: bool promotion_failure_occurred = false; sla@5237: jcoomes@2020: TASKQUEUE_STATS_ONLY(if (PrintGCDetails && ParallelGCVerbose) print_stats()); jcoomes@1993: for (uint i = 0; i < ParallelGCThreads + 1; i++) { duke@435: PSPromotionManager* manager = manager_array(i); tonyp@2061: assert(manager->claimed_stack_depth()->is_empty(), "should be empty"); sla@5237: if (manager->_promotion_failed_info.has_failed()) { sla@5237: gc_tracer.report_promotion_failed(manager->_promotion_failed_info); sla@5237: promotion_failure_occurred = true; sla@5237: } duke@435: manager->flush_labs(); duke@435: } sla@5237: return promotion_failure_occurred; duke@435: } duke@435: jcoomes@2020: #if TASKQUEUE_STATS duke@435: void jcoomes@2020: PSPromotionManager::print_taskqueue_stats(uint i) const { jcoomes@2020: tty->print("%3u ", i); tonyp@2061: _claimed_stack_depth.stats.print(); jcoomes@2020: tty->cr(); duke@435: } duke@435: duke@435: void jcoomes@2020: PSPromotionManager::print_local_stats(uint i) const { jcoomes@2020: #define FMT " " SIZE_FORMAT_W(10) jcoomes@2020: tty->print_cr("%3u" FMT FMT FMT FMT, i, _masked_pushes, _masked_steals, jcoomes@2020: _arrays_chunked, _array_chunks_processed); jcoomes@2020: #undef FMT jcoomes@2020: } jcoomes@2020: jcoomes@2020: static const char* const pm_stats_hdr[] = { jcoomes@2020: " --------masked------- arrays array", jcoomes@2020: "thr push steal chunked chunks", jcoomes@2020: "--- ---------- ---------- ---------- ----------" jcoomes@2020: }; jcoomes@2020: jcoomes@2020: void duke@435: PSPromotionManager::print_stats() { tonyp@2061: tty->print_cr("== GC Tasks Stats, GC %3d", duke@435: Universe::heap()->total_collections()); duke@435: jcoomes@2020: tty->print("thr "); TaskQueueStats::print_header(1); tty->cr(); jcoomes@2020: tty->print("--- "); TaskQueueStats::print_header(2); tty->cr(); jcoomes@2020: for (uint i = 0; i < ParallelGCThreads + 1; ++i) { jcoomes@2020: manager_array(i)->print_taskqueue_stats(i); jcoomes@2020: } jcoomes@2020: jcoomes@2020: const uint hlines = sizeof(pm_stats_hdr) / sizeof(pm_stats_hdr[0]); jcoomes@2020: for (uint i = 0; i < hlines; ++i) tty->print_cr(pm_stats_hdr[i]); jcoomes@2020: for (uint i = 0; i < ParallelGCThreads + 1; ++i) { jcoomes@2020: manager_array(i)->print_local_stats(i); duke@435: } duke@435: } duke@435: jcoomes@2020: void jcoomes@2020: PSPromotionManager::reset_stats() { tonyp@2061: claimed_stack_depth()->stats.reset(); jcoomes@2020: _masked_pushes = _masked_steals = 0; jcoomes@2020: _arrays_chunked = _array_chunks_processed = 0; jcoomes@2020: } jcoomes@2020: #endif // TASKQUEUE_STATS duke@435: duke@435: PSPromotionManager::PSPromotionManager() { duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); duke@435: duke@435: // We set the old lab's start array. duke@435: _old_lab.set_start_array(old_gen()->start_array()); duke@435: duke@435: uint queue_size; tonyp@2061: claimed_stack_depth()->initialize(); tonyp@2061: queue_size = claimed_stack_depth()->max_elems(); duke@435: duke@435: _totally_drain = (ParallelGCThreads == 1) || (GCDrainStackTargetSize == 0); duke@435: if (_totally_drain) { duke@435: _target_stack_size = 0; duke@435: } else { duke@435: // don't let the target stack size to be more than 1/4 of the entries duke@435: _target_stack_size = (uint) MIN2((uint) GCDrainStackTargetSize, duke@435: (uint) (queue_size / 4)); duke@435: } duke@435: duke@435: _array_chunk_size = ParGCArrayScanChunk; duke@435: // let's choose 1.5x the chunk size duke@435: _min_array_size_for_chunking = 3 * _array_chunk_size / 2; duke@435: duke@435: reset(); duke@435: } duke@435: duke@435: void PSPromotionManager::reset() { jcoomes@1993: assert(stacks_empty(), "reset of non-empty stack"); duke@435: duke@435: // We need to get an assert in here to make sure the labs are always flushed. duke@435: duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); duke@435: duke@435: // Do not prefill the LAB's, save heap wastage! duke@435: HeapWord* lab_base = young_space()->top(); duke@435: _young_lab.initialize(MemRegion(lab_base, (size_t)0)); duke@435: _young_gen_is_full = false; duke@435: duke@435: lab_base = old_gen()->object_space()->top(); duke@435: _old_lab.initialize(MemRegion(lab_base, (size_t)0)); duke@435: _old_gen_is_full = false; duke@435: sla@5237: _promotion_failed_info.reset(); sla@5237: jcoomes@2020: TASKQUEUE_STATS_ONLY(reset_stats()); duke@435: } duke@435: coleenp@548: duke@435: void PSPromotionManager::drain_stacks_depth(bool totally_drain) { duke@435: totally_drain = totally_drain || _totally_drain; duke@435: duke@435: #ifdef ASSERT duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); duke@435: MutableSpace* to_space = heap->young_gen()->to_space(); duke@435: MutableSpace* old_space = heap->old_gen()->object_space(); duke@435: #endif /* ASSERT */ duke@435: jcoomes@1993: OopStarTaskQueue* const tq = claimed_stack_depth(); duke@435: do { coleenp@548: StarTask p; duke@435: duke@435: // Drain overflow stack first, so other threads can steal from duke@435: // claimed stack while we work. jcoomes@1993: while (tq->pop_overflow(p)) { jcoomes@1993: process_popped_location_depth(p); duke@435: } duke@435: duke@435: if (totally_drain) { jcoomes@1993: while (tq->pop_local(p)) { duke@435: process_popped_location_depth(p); duke@435: } duke@435: } else { jcoomes@1993: while (tq->size() > _target_stack_size && tq->pop_local(p)) { duke@435: process_popped_location_depth(p); duke@435: } duke@435: } jcoomes@1993: } while (totally_drain && !tq->taskqueue_empty() || !tq->overflow_empty()); duke@435: jcoomes@1993: assert(!totally_drain || tq->taskqueue_empty(), "Sanity"); jcoomes@1993: assert(totally_drain || tq->size() <= _target_stack_size, "Sanity"); jcoomes@1993: assert(tq->overflow_empty(), "Sanity"); duke@435: } duke@435: duke@435: void PSPromotionManager::flush_labs() { jcoomes@1993: assert(stacks_empty(), "Attempt to flush lab with live stack"); duke@435: duke@435: // If either promotion lab fills up, we can flush the duke@435: // lab but not refill it, so check first. duke@435: assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity"); duke@435: if (!_young_lab.is_flushed()) duke@435: _young_lab.flush(); duke@435: duke@435: assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity"); duke@435: if (!_old_lab.is_flushed()) duke@435: _old_lab.flush(); duke@435: duke@435: // Let PSScavenge know if we overflowed duke@435: if (_young_gen_is_full) { duke@435: PSScavenge::set_survivor_overflow(true); duke@435: } duke@435: } duke@435: coleenp@548: template void PSPromotionManager::process_array_chunk_work( coleenp@548: oop obj, coleenp@548: int start, int end) { jwilhelm@2648: assert(start <= end, "invariant"); coleenp@548: T* const base = (T*)objArrayOop(obj)->base(); coleenp@548: T* p = base + start; coleenp@548: T* const chunk_end = base + end; coleenp@548: while (p < chunk_end) { coleenp@548: if (PSScavenge::should_scavenge(p)) { coleenp@548: claim_or_forward_depth(p); coleenp@548: } coleenp@548: ++p; coleenp@548: } coleenp@548: } coleenp@548: duke@435: void PSPromotionManager::process_array_chunk(oop old) { duke@435: assert(PSChunkLargeArrays, "invariant"); duke@435: assert(old->is_objArray(), "invariant"); duke@435: assert(old->is_forwarded(), "invariant"); duke@435: jcoomes@2020: TASKQUEUE_STATS_ONLY(++_array_chunks_processed); duke@435: duke@435: oop const obj = old->forwardee(); duke@435: duke@435: int start; duke@435: int const end = arrayOop(old)->length(); duke@435: if (end > (int) _min_array_size_for_chunking) { duke@435: // we'll chunk more duke@435: start = end - _array_chunk_size; duke@435: assert(start > 0, "invariant"); duke@435: arrayOop(old)->set_length(start); duke@435: push_depth(mask_chunked_array_oop(old)); jcoomes@2020: TASKQUEUE_STATS_ONLY(++_masked_pushes); duke@435: } else { duke@435: // this is the final chunk for this array duke@435: start = 0; duke@435: int const actual_length = arrayOop(obj)->length(); duke@435: arrayOop(old)->set_length(actual_length); duke@435: } duke@435: coleenp@548: if (UseCompressedOops) { coleenp@548: process_array_chunk_work(obj, start, end); coleenp@548: } else { coleenp@548: process_array_chunk_work(obj, start, end); duke@435: } duke@435: } duke@435: duke@435: oop PSPromotionManager::oop_promotion_failed(oop obj, markOop obj_mark) { duke@435: assert(_old_gen_is_full || PromotionFailureALot, "Sanity"); duke@435: duke@435: // Attempt to CAS in the header. duke@435: // This tests if the header is still the same as when duke@435: // this started. If it is the same (i.e., no forwarding duke@435: // pointer has been installed), then this thread owns duke@435: // it. duke@435: if (obj->cas_forward_to(obj, obj_mark)) { duke@435: // We won any races, we "own" this object. duke@435: assert(obj == obj->forwardee(), "Sanity"); duke@435: sla@5237: _promotion_failed_info.register_copy_failure(obj->size()); sla@5237: tonyp@2061: obj->push_contents(this); duke@435: duke@435: // Save the mark if needed duke@435: PSScavenge::oop_promotion_failed(obj, obj_mark); duke@435: } else { duke@435: // We lost, someone else "owns" this object duke@435: guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed."); duke@435: duke@435: // No unallocation to worry about. duke@435: obj = obj->forwardee(); duke@435: } duke@435: coleenp@4037: #ifndef PRODUCT duke@435: if (TraceScavenge) { duke@435: gclog_or_tty->print_cr("{%s %s 0x%x (%d)}", duke@435: "promotion-failure", coleenp@4037: obj->klass()->internal_name(), hseigel@5784: (void *)obj, obj->size()); duke@435: duke@435: } duke@435: #endif duke@435: duke@435: return obj; duke@435: }