sjohanss@7118: /* sjohanss@7118: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. sjohanss@7118: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. sjohanss@7118: * sjohanss@7118: * This code is free software; you can redistribute it and/or modify it sjohanss@7118: * under the terms of the GNU General Public License version 2 only, as sjohanss@7118: * published by the Free Software Foundation. sjohanss@7118: * sjohanss@7118: * This code is distributed in the hope that it will be useful, but WITHOUT sjohanss@7118: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or sjohanss@7118: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License sjohanss@7118: * version 2 for more details (a copy is included in the LICENSE file that sjohanss@7118: * accompanied this code). sjohanss@7118: * sjohanss@7118: * You should have received a copy of the GNU General Public License version sjohanss@7118: * 2 along with this work; if not, write to the Free Software Foundation, sjohanss@7118: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. sjohanss@7118: * sjohanss@7118: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA sjohanss@7118: * or visit www.oracle.com if you need additional information or have any sjohanss@7118: * questions. sjohanss@7118: * sjohanss@7118: */ sjohanss@7118: sjohanss@7118: #include "precompiled.hpp" sjohanss@7118: #include "gc_implementation/g1/g1Allocator.hpp" sjohanss@7118: #include "gc_implementation/g1/g1CollectedHeap.hpp" sjohanss@7118: #include "gc_implementation/g1/g1CollectorPolicy.hpp" sjohanss@7118: #include "gc_implementation/g1/heapRegion.inline.hpp" sjohanss@7118: #include "gc_implementation/g1/heapRegionSet.inline.hpp" sjohanss@7118: sjohanss@7118: void G1DefaultAllocator::init_mutator_alloc_region() { sjohanss@7118: assert(_mutator_alloc_region.get() == NULL, "pre-condition"); sjohanss@7118: _mutator_alloc_region.init(); sjohanss@7118: } sjohanss@7118: sjohanss@7118: void G1DefaultAllocator::release_mutator_alloc_region() { sjohanss@7118: _mutator_alloc_region.release(); sjohanss@7118: assert(_mutator_alloc_region.get() == NULL, "post-condition"); sjohanss@7118: } sjohanss@7118: sjohanss@7118: void G1Allocator::reuse_retained_old_region(EvacuationInfo& evacuation_info, sjohanss@7118: OldGCAllocRegion* old, sjohanss@7118: HeapRegion** retained_old) { sjohanss@7118: HeapRegion* retained_region = *retained_old; sjohanss@7118: *retained_old = NULL; sjohanss@7118: sjohanss@7118: // We will discard the current GC alloc region if: sjohanss@7118: // a) it's in the collection set (it can happen!), sjohanss@7118: // b) it's already full (no point in using it), sjohanss@7118: // c) it's empty (this means that it was emptied during sjohanss@7118: // a cleanup and it should be on the free list now), or sjohanss@7118: // d) it's humongous (this means that it was emptied sjohanss@7118: // during a cleanup and was added to the free list, but sjohanss@7118: // has been subsequently used to allocate a humongous sjohanss@7118: // object that may be less than the region size). sjohanss@7118: if (retained_region != NULL && sjohanss@7118: !retained_region->in_collection_set() && sjohanss@7118: !(retained_region->top() == retained_region->end()) && sjohanss@7118: !retained_region->is_empty() && sjohanss@7118: !retained_region->isHumongous()) { mgerdin@7647: retained_region->record_timestamp(); sjohanss@7118: // The retained region was added to the old region set when it was sjohanss@7118: // retired. We have to remove it now, since we don't allow regions sjohanss@7118: // we allocate to in the region sets. We'll re-add it later, when sjohanss@7118: // it's retired again. sjohanss@7118: _g1h->_old_set.remove(retained_region); sjohanss@7118: bool during_im = _g1h->g1_policy()->during_initial_mark_pause(); sjohanss@7118: retained_region->note_start_of_copying(during_im); sjohanss@7118: old->set(retained_region); sjohanss@7118: _g1h->_hr_printer.reuse(retained_region); sjohanss@7118: evacuation_info.set_alloc_regions_used_before(retained_region->used()); sjohanss@7118: } sjohanss@7118: } sjohanss@7118: sjohanss@7118: void G1DefaultAllocator::init_gc_alloc_regions(EvacuationInfo& evacuation_info) { sjohanss@7118: assert_at_safepoint(true /* should_be_vm_thread */); sjohanss@7118: sjohanss@7118: _survivor_gc_alloc_region.init(); sjohanss@7118: _old_gc_alloc_region.init(); sjohanss@7118: reuse_retained_old_region(evacuation_info, sjohanss@7118: &_old_gc_alloc_region, sjohanss@7118: &_retained_old_gc_alloc_region); sjohanss@7118: } sjohanss@7118: sjohanss@7118: void G1DefaultAllocator::release_gc_alloc_regions(uint no_of_gc_workers, EvacuationInfo& evacuation_info) { sjohanss@7118: AllocationContext_t context = AllocationContext::current(); sjohanss@7118: evacuation_info.set_allocation_regions(survivor_gc_alloc_region(context)->count() + sjohanss@7118: old_gc_alloc_region(context)->count()); sjohanss@7118: survivor_gc_alloc_region(context)->release(); sjohanss@7118: // If we have an old GC alloc region to release, we'll save it in sjohanss@7118: // _retained_old_gc_alloc_region. If we don't sjohanss@7118: // _retained_old_gc_alloc_region will become NULL. This is what we sjohanss@7118: // want either way so no reason to check explicitly for either sjohanss@7118: // condition. sjohanss@7118: _retained_old_gc_alloc_region = old_gc_alloc_region(context)->release(); mgerdin@7647: if (_retained_old_gc_alloc_region != NULL) { mgerdin@7647: _retained_old_gc_alloc_region->record_retained_region(); mgerdin@7647: } sjohanss@7118: sjohanss@7118: if (ResizePLAB) { sjohanss@7118: _g1h->_survivor_plab_stats.adjust_desired_plab_sz(no_of_gc_workers); sjohanss@7118: _g1h->_old_plab_stats.adjust_desired_plab_sz(no_of_gc_workers); sjohanss@7118: } sjohanss@7118: } sjohanss@7118: sjohanss@7118: void G1DefaultAllocator::abandon_gc_alloc_regions() { sjohanss@7118: assert(survivor_gc_alloc_region(AllocationContext::current())->get() == NULL, "pre-condition"); sjohanss@7118: assert(old_gc_alloc_region(AllocationContext::current())->get() == NULL, "pre-condition"); sjohanss@7118: _retained_old_gc_alloc_region = NULL; sjohanss@7118: } sjohanss@7118: sjohanss@7118: G1ParGCAllocBuffer::G1ParGCAllocBuffer(size_t gclab_word_size) : sjohanss@7118: ParGCAllocBuffer(gclab_word_size), _retired(true) { } sjohanss@7118: sjohanss@7118: HeapWord* G1ParGCAllocator::allocate_slow(GCAllocPurpose purpose, size_t word_sz, AllocationContext_t context) { sjohanss@7118: HeapWord* obj = NULL; sjohanss@7118: size_t gclab_word_size = _g1h->desired_plab_sz(purpose); sjohanss@7118: if (word_sz * 100 < gclab_word_size * ParallelGCBufferWastePct) { sjohanss@7118: G1ParGCAllocBuffer* alloc_buf = alloc_buffer(purpose, context); sjohanss@7118: add_to_alloc_buffer_waste(alloc_buf->words_remaining()); sjohanss@7118: alloc_buf->retire(false /* end_of_gc */, false /* retain */); sjohanss@7118: sjohanss@7118: HeapWord* buf = _g1h->par_allocate_during_gc(purpose, gclab_word_size, context); sjohanss@7118: if (buf == NULL) { sjohanss@7118: return NULL; // Let caller handle allocation failure. sjohanss@7118: } sjohanss@7118: // Otherwise. sjohanss@7118: alloc_buf->set_word_size(gclab_word_size); sjohanss@7118: alloc_buf->set_buf(buf); sjohanss@7118: sjohanss@7118: obj = alloc_buf->allocate(word_sz); sjohanss@7118: assert(obj != NULL, "buffer was definitely big enough..."); sjohanss@7118: } else { sjohanss@7118: obj = _g1h->par_allocate_during_gc(purpose, word_sz, context); sjohanss@7118: } sjohanss@7118: return obj; sjohanss@7118: } sjohanss@7118: sjohanss@7118: G1DefaultParGCAllocator::G1DefaultParGCAllocator(G1CollectedHeap* g1h) : sjohanss@7118: G1ParGCAllocator(g1h), sjohanss@7118: _surviving_alloc_buffer(g1h->desired_plab_sz(GCAllocForSurvived)), sjohanss@7118: _tenured_alloc_buffer(g1h->desired_plab_sz(GCAllocForTenured)) { sjohanss@7118: sjohanss@7118: _alloc_buffers[GCAllocForSurvived] = &_surviving_alloc_buffer; sjohanss@7118: _alloc_buffers[GCAllocForTenured] = &_tenured_alloc_buffer; sjohanss@7118: sjohanss@7118: } sjohanss@7118: sjohanss@7118: void G1DefaultParGCAllocator::retire_alloc_buffers() { sjohanss@7118: for (int ap = 0; ap < GCAllocPurposeCount; ++ap) { sjohanss@7118: size_t waste = _alloc_buffers[ap]->words_remaining(); sjohanss@7118: add_to_alloc_buffer_waste(waste); sjohanss@7118: _alloc_buffers[ap]->flush_stats_and_retire(_g1h->stats_for_purpose((GCAllocPurpose)ap), sjohanss@7118: true /* end_of_gc */, sjohanss@7118: false /* retain */); sjohanss@7118: } sjohanss@7118: }