1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/g1/g1Allocator.cpp Fri Sep 05 09:49:19 2014 +0200 1.3 @@ -0,0 +1,155 @@ 1.4 +/* 1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "gc_implementation/g1/g1Allocator.hpp" 1.30 +#include "gc_implementation/g1/g1CollectedHeap.hpp" 1.31 +#include "gc_implementation/g1/g1CollectorPolicy.hpp" 1.32 +#include "gc_implementation/g1/heapRegion.inline.hpp" 1.33 +#include "gc_implementation/g1/heapRegionSet.inline.hpp" 1.34 + 1.35 +void G1DefaultAllocator::init_mutator_alloc_region() { 1.36 + assert(_mutator_alloc_region.get() == NULL, "pre-condition"); 1.37 + _mutator_alloc_region.init(); 1.38 +} 1.39 + 1.40 +void G1DefaultAllocator::release_mutator_alloc_region() { 1.41 + _mutator_alloc_region.release(); 1.42 + assert(_mutator_alloc_region.get() == NULL, "post-condition"); 1.43 +} 1.44 + 1.45 +void G1Allocator::reuse_retained_old_region(EvacuationInfo& evacuation_info, 1.46 + OldGCAllocRegion* old, 1.47 + HeapRegion** retained_old) { 1.48 + HeapRegion* retained_region = *retained_old; 1.49 + *retained_old = NULL; 1.50 + 1.51 + // We will discard the current GC alloc region if: 1.52 + // a) it's in the collection set (it can happen!), 1.53 + // b) it's already full (no point in using it), 1.54 + // c) it's empty (this means that it was emptied during 1.55 + // a cleanup and it should be on the free list now), or 1.56 + // d) it's humongous (this means that it was emptied 1.57 + // during a cleanup and was added to the free list, but 1.58 + // has been subsequently used to allocate a humongous 1.59 + // object that may be less than the region size). 1.60 + if (retained_region != NULL && 1.61 + !retained_region->in_collection_set() && 1.62 + !(retained_region->top() == retained_region->end()) && 1.63 + !retained_region->is_empty() && 1.64 + !retained_region->isHumongous()) { 1.65 + retained_region->record_top_and_timestamp(); 1.66 + // The retained region was added to the old region set when it was 1.67 + // retired. We have to remove it now, since we don't allow regions 1.68 + // we allocate to in the region sets. We'll re-add it later, when 1.69 + // it's retired again. 1.70 + _g1h->_old_set.remove(retained_region); 1.71 + bool during_im = _g1h->g1_policy()->during_initial_mark_pause(); 1.72 + retained_region->note_start_of_copying(during_im); 1.73 + old->set(retained_region); 1.74 + _g1h->_hr_printer.reuse(retained_region); 1.75 + evacuation_info.set_alloc_regions_used_before(retained_region->used()); 1.76 + } 1.77 +} 1.78 + 1.79 +void G1DefaultAllocator::init_gc_alloc_regions(EvacuationInfo& evacuation_info) { 1.80 + assert_at_safepoint(true /* should_be_vm_thread */); 1.81 + 1.82 + _survivor_gc_alloc_region.init(); 1.83 + _old_gc_alloc_region.init(); 1.84 + reuse_retained_old_region(evacuation_info, 1.85 + &_old_gc_alloc_region, 1.86 + &_retained_old_gc_alloc_region); 1.87 +} 1.88 + 1.89 +void G1DefaultAllocator::release_gc_alloc_regions(uint no_of_gc_workers, EvacuationInfo& evacuation_info) { 1.90 + AllocationContext_t context = AllocationContext::current(); 1.91 + evacuation_info.set_allocation_regions(survivor_gc_alloc_region(context)->count() + 1.92 + old_gc_alloc_region(context)->count()); 1.93 + survivor_gc_alloc_region(context)->release(); 1.94 + // If we have an old GC alloc region to release, we'll save it in 1.95 + // _retained_old_gc_alloc_region. If we don't 1.96 + // _retained_old_gc_alloc_region will become NULL. This is what we 1.97 + // want either way so no reason to check explicitly for either 1.98 + // condition. 1.99 + _retained_old_gc_alloc_region = old_gc_alloc_region(context)->release(); 1.100 + 1.101 + if (ResizePLAB) { 1.102 + _g1h->_survivor_plab_stats.adjust_desired_plab_sz(no_of_gc_workers); 1.103 + _g1h->_old_plab_stats.adjust_desired_plab_sz(no_of_gc_workers); 1.104 + } 1.105 +} 1.106 + 1.107 +void G1DefaultAllocator::abandon_gc_alloc_regions() { 1.108 + assert(survivor_gc_alloc_region(AllocationContext::current())->get() == NULL, "pre-condition"); 1.109 + assert(old_gc_alloc_region(AllocationContext::current())->get() == NULL, "pre-condition"); 1.110 + _retained_old_gc_alloc_region = NULL; 1.111 +} 1.112 + 1.113 +G1ParGCAllocBuffer::G1ParGCAllocBuffer(size_t gclab_word_size) : 1.114 + ParGCAllocBuffer(gclab_word_size), _retired(true) { } 1.115 + 1.116 +HeapWord* G1ParGCAllocator::allocate_slow(GCAllocPurpose purpose, size_t word_sz, AllocationContext_t context) { 1.117 + HeapWord* obj = NULL; 1.118 + size_t gclab_word_size = _g1h->desired_plab_sz(purpose); 1.119 + if (word_sz * 100 < gclab_word_size * ParallelGCBufferWastePct) { 1.120 + G1ParGCAllocBuffer* alloc_buf = alloc_buffer(purpose, context); 1.121 + add_to_alloc_buffer_waste(alloc_buf->words_remaining()); 1.122 + alloc_buf->retire(false /* end_of_gc */, false /* retain */); 1.123 + 1.124 + HeapWord* buf = _g1h->par_allocate_during_gc(purpose, gclab_word_size, context); 1.125 + if (buf == NULL) { 1.126 + return NULL; // Let caller handle allocation failure. 1.127 + } 1.128 + // Otherwise. 1.129 + alloc_buf->set_word_size(gclab_word_size); 1.130 + alloc_buf->set_buf(buf); 1.131 + 1.132 + obj = alloc_buf->allocate(word_sz); 1.133 + assert(obj != NULL, "buffer was definitely big enough..."); 1.134 + } else { 1.135 + obj = _g1h->par_allocate_during_gc(purpose, word_sz, context); 1.136 + } 1.137 + return obj; 1.138 +} 1.139 + 1.140 +G1DefaultParGCAllocator::G1DefaultParGCAllocator(G1CollectedHeap* g1h) : 1.141 + G1ParGCAllocator(g1h), 1.142 + _surviving_alloc_buffer(g1h->desired_plab_sz(GCAllocForSurvived)), 1.143 + _tenured_alloc_buffer(g1h->desired_plab_sz(GCAllocForTenured)) { 1.144 + 1.145 + _alloc_buffers[GCAllocForSurvived] = &_surviving_alloc_buffer; 1.146 + _alloc_buffers[GCAllocForTenured] = &_tenured_alloc_buffer; 1.147 + 1.148 +} 1.149 + 1.150 +void G1DefaultParGCAllocator::retire_alloc_buffers() { 1.151 + for (int ap = 0; ap < GCAllocPurposeCount; ++ap) { 1.152 + size_t waste = _alloc_buffers[ap]->words_remaining(); 1.153 + add_to_alloc_buffer_waste(waste); 1.154 + _alloc_buffers[ap]->flush_stats_and_retire(_g1h->stats_for_purpose((GCAllocPurpose)ap), 1.155 + true /* end_of_gc */, 1.156 + false /* retain */); 1.157 + } 1.158 +}