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: #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCATOR_HPP sjohanss@7118: #define SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCATOR_HPP sjohanss@7118: sjohanss@7118: #include "gc_implementation/g1/g1AllocationContext.hpp" sjohanss@7118: #include "gc_implementation/g1/g1AllocRegion.hpp" sjohanss@7118: #include "gc_implementation/shared/parGCAllocBuffer.hpp" sjohanss@7118: sjohanss@7118: enum GCAllocPurpose { sjohanss@7118: GCAllocForTenured, sjohanss@7118: GCAllocForSurvived, sjohanss@7118: GCAllocPurposeCount sjohanss@7118: }; sjohanss@7118: sjohanss@7118: // Base class for G1 allocators. sjohanss@7118: class G1Allocator : public CHeapObj { sjohanss@7118: friend class VMStructs; sjohanss@7118: protected: sjohanss@7118: G1CollectedHeap* _g1h; sjohanss@7118: sjohanss@7118: // Outside of GC pauses, the number of bytes used in all regions other sjohanss@7118: // than the current allocation region. sjohanss@7118: size_t _summary_bytes_used; sjohanss@7118: sjohanss@7118: public: sjohanss@7118: G1Allocator(G1CollectedHeap* heap) : sjohanss@7118: _g1h(heap), _summary_bytes_used(0) { } sjohanss@7118: sjohanss@7118: static G1Allocator* create_allocator(G1CollectedHeap* g1h); sjohanss@7118: sjohanss@7118: virtual void init_mutator_alloc_region() = 0; sjohanss@7118: virtual void release_mutator_alloc_region() = 0; sjohanss@7118: sjohanss@7118: virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info) = 0; sjohanss@7118: virtual void release_gc_alloc_regions(uint no_of_gc_workers, EvacuationInfo& evacuation_info) = 0; sjohanss@7118: virtual void abandon_gc_alloc_regions() = 0; sjohanss@7118: sjohanss@7118: virtual MutatorAllocRegion* mutator_alloc_region(AllocationContext_t context) = 0; sjohanss@7118: virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) = 0; sjohanss@7118: virtual OldGCAllocRegion* old_gc_alloc_region(AllocationContext_t context) = 0; sjohanss@7118: virtual size_t used() = 0; sjohanss@7118: virtual bool is_retained_old_region(HeapRegion* hr) = 0; sjohanss@7118: sjohanss@7118: void reuse_retained_old_region(EvacuationInfo& evacuation_info, sjohanss@7118: OldGCAllocRegion* old, sjohanss@7118: HeapRegion** retained); sjohanss@7118: sjohanss@7118: size_t used_unlocked() const { sjohanss@7118: return _summary_bytes_used; sjohanss@7118: } sjohanss@7118: sjohanss@7118: void increase_used(size_t bytes) { sjohanss@7118: _summary_bytes_used += bytes; sjohanss@7118: } sjohanss@7118: sjohanss@7118: void decrease_used(size_t bytes) { sjohanss@7118: assert(_summary_bytes_used >= bytes, sjohanss@7118: err_msg("invariant: _summary_bytes_used: "SIZE_FORMAT" should be >= bytes: "SIZE_FORMAT, sjohanss@7118: _summary_bytes_used, bytes)); sjohanss@7118: _summary_bytes_used -= bytes; sjohanss@7118: } sjohanss@7118: sjohanss@7118: void set_used(size_t bytes) { sjohanss@7118: _summary_bytes_used = bytes; sjohanss@7118: } sjohanss@7131: sjohanss@7131: virtual HeapRegion* new_heap_region(uint hrs_index, sjohanss@7131: G1BlockOffsetSharedArray* sharedOffsetArray, sjohanss@7131: MemRegion mr) { sjohanss@7131: return new HeapRegion(hrs_index, sharedOffsetArray, mr); sjohanss@7131: } sjohanss@7118: }; sjohanss@7118: sjohanss@7118: // The default allocator for G1. sjohanss@7118: class G1DefaultAllocator : public G1Allocator { sjohanss@7118: protected: sjohanss@7118: // Alloc region used to satisfy mutator allocation requests. sjohanss@7118: MutatorAllocRegion _mutator_alloc_region; sjohanss@7118: sjohanss@7118: // Alloc region used to satisfy allocation requests by the GC for sjohanss@7118: // survivor objects. sjohanss@7118: SurvivorGCAllocRegion _survivor_gc_alloc_region; sjohanss@7118: sjohanss@7118: // Alloc region used to satisfy allocation requests by the GC for sjohanss@7118: // old objects. sjohanss@7118: OldGCAllocRegion _old_gc_alloc_region; sjohanss@7118: sjohanss@7118: HeapRegion* _retained_old_gc_alloc_region; sjohanss@7118: public: sjohanss@7118: G1DefaultAllocator(G1CollectedHeap* heap) : G1Allocator(heap), _retained_old_gc_alloc_region(NULL) { } sjohanss@7118: sjohanss@7118: virtual void init_mutator_alloc_region(); sjohanss@7118: virtual void release_mutator_alloc_region(); sjohanss@7118: sjohanss@7118: virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info); sjohanss@7118: virtual void release_gc_alloc_regions(uint no_of_gc_workers, EvacuationInfo& evacuation_info); sjohanss@7118: virtual void abandon_gc_alloc_regions(); sjohanss@7118: sjohanss@7118: virtual bool is_retained_old_region(HeapRegion* hr) { sjohanss@7118: return _retained_old_gc_alloc_region == hr; sjohanss@7118: } sjohanss@7118: sjohanss@7118: virtual MutatorAllocRegion* mutator_alloc_region(AllocationContext_t context) { sjohanss@7118: return &_mutator_alloc_region; sjohanss@7118: } sjohanss@7118: sjohanss@7118: virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) { sjohanss@7118: return &_survivor_gc_alloc_region; sjohanss@7118: } sjohanss@7118: sjohanss@7118: virtual OldGCAllocRegion* old_gc_alloc_region(AllocationContext_t context) { sjohanss@7118: return &_old_gc_alloc_region; sjohanss@7118: } sjohanss@7118: sjohanss@7118: virtual size_t used() { sjohanss@7118: assert(Heap_lock->owner() != NULL, sjohanss@7118: "Should be owned on this thread's behalf."); sjohanss@7118: size_t result = _summary_bytes_used; sjohanss@7118: sjohanss@7118: // Read only once in case it is set to NULL concurrently sjohanss@7118: HeapRegion* hr = mutator_alloc_region(AllocationContext::current())->get(); sjohanss@7118: if (hr != NULL) { sjohanss@7118: result += hr->used(); sjohanss@7118: } sjohanss@7118: return result; sjohanss@7118: } sjohanss@7118: }; sjohanss@7118: sjohanss@7118: class G1ParGCAllocBuffer: public ParGCAllocBuffer { sjohanss@7118: private: sjohanss@7118: bool _retired; sjohanss@7118: sjohanss@7118: public: sjohanss@7118: G1ParGCAllocBuffer(size_t gclab_word_size); sjohanss@7118: virtual ~G1ParGCAllocBuffer() { sjohanss@7118: guarantee(_retired, "Allocation buffer has not been retired"); sjohanss@7118: } sjohanss@7118: sjohanss@7118: virtual void set_buf(HeapWord* buf) { sjohanss@7118: ParGCAllocBuffer::set_buf(buf); sjohanss@7118: _retired = false; sjohanss@7118: } sjohanss@7118: sjohanss@7118: virtual void retire(bool end_of_gc, bool retain) { sjohanss@7118: if (_retired) { sjohanss@7118: return; sjohanss@7118: } sjohanss@7118: ParGCAllocBuffer::retire(end_of_gc, retain); sjohanss@7118: _retired = true; sjohanss@7118: } sjohanss@7118: }; sjohanss@7118: sjohanss@7118: class G1ParGCAllocator : public CHeapObj { sjohanss@7118: friend class G1ParScanThreadState; sjohanss@7118: protected: sjohanss@7118: G1CollectedHeap* _g1h; sjohanss@7118: sjohanss@7118: size_t _alloc_buffer_waste; sjohanss@7118: size_t _undo_waste; sjohanss@7118: sjohanss@7118: void add_to_alloc_buffer_waste(size_t waste) { _alloc_buffer_waste += waste; } sjohanss@7118: void add_to_undo_waste(size_t waste) { _undo_waste += waste; } sjohanss@7118: sjohanss@7118: HeapWord* allocate_slow(GCAllocPurpose purpose, size_t word_sz, AllocationContext_t context); sjohanss@7118: sjohanss@7118: virtual void retire_alloc_buffers() = 0; sjohanss@7118: virtual G1ParGCAllocBuffer* alloc_buffer(GCAllocPurpose purpose, AllocationContext_t context) = 0; sjohanss@7118: sjohanss@7118: public: sjohanss@7118: G1ParGCAllocator(G1CollectedHeap* g1h) : sjohanss@7118: _g1h(g1h), _alloc_buffer_waste(0), _undo_waste(0) { sjohanss@7118: } sjohanss@7118: sjohanss@7118: static G1ParGCAllocator* create_allocator(G1CollectedHeap* g1h); sjohanss@7118: sjohanss@7118: size_t alloc_buffer_waste() { return _alloc_buffer_waste; } sjohanss@7118: size_t undo_waste() {return _undo_waste; } sjohanss@7118: sjohanss@7118: HeapWord* allocate(GCAllocPurpose purpose, size_t word_sz, AllocationContext_t context) { sjohanss@7118: HeapWord* obj = NULL; sjohanss@7118: if (purpose == GCAllocForSurvived) { sjohanss@7118: obj = alloc_buffer(purpose, context)->allocate_aligned(word_sz, SurvivorAlignmentInBytes); sjohanss@7118: } else { sjohanss@7118: obj = alloc_buffer(purpose, context)->allocate(word_sz); sjohanss@7118: } sjohanss@7118: if (obj != NULL) { sjohanss@7118: return obj; sjohanss@7118: } sjohanss@7118: return allocate_slow(purpose, word_sz, context); sjohanss@7118: } sjohanss@7118: sjohanss@7118: void undo_allocation(GCAllocPurpose purpose, HeapWord* obj, size_t word_sz, AllocationContext_t context) { sjohanss@7118: if (alloc_buffer(purpose, context)->contains(obj)) { sjohanss@7118: assert(alloc_buffer(purpose, context)->contains(obj + word_sz - 1), sjohanss@7118: "should contain whole object"); sjohanss@7118: alloc_buffer(purpose, context)->undo_allocation(obj, word_sz); sjohanss@7118: } else { sjohanss@7118: CollectedHeap::fill_with_object(obj, word_sz); sjohanss@7118: add_to_undo_waste(word_sz); sjohanss@7118: } sjohanss@7118: } sjohanss@7118: }; sjohanss@7118: sjohanss@7118: class G1DefaultParGCAllocator : public G1ParGCAllocator { sjohanss@7118: G1ParGCAllocBuffer _surviving_alloc_buffer; sjohanss@7118: G1ParGCAllocBuffer _tenured_alloc_buffer; sjohanss@7118: G1ParGCAllocBuffer* _alloc_buffers[GCAllocPurposeCount]; sjohanss@7118: sjohanss@7118: public: sjohanss@7118: G1DefaultParGCAllocator(G1CollectedHeap* g1h); sjohanss@7118: sjohanss@7118: virtual G1ParGCAllocBuffer* alloc_buffer(GCAllocPurpose purpose, AllocationContext_t context) { sjohanss@7118: return _alloc_buffers[purpose]; sjohanss@7118: } sjohanss@7118: sjohanss@7118: virtual void retire_alloc_buffers() ; sjohanss@7118: }; sjohanss@7118: sjohanss@7118: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCATOR_HPP