Path: blob/master/src/hotspot/share/gc/g1/g1Allocator.hpp
66644 views
/*1* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_GC_G1_G1ALLOCATOR_HPP25#define SHARE_GC_G1_G1ALLOCATOR_HPP2627#include "gc/g1/g1AllocRegion.hpp"28#include "gc/g1/g1HeapRegionAttr.hpp"29#include "gc/shared/collectedHeap.hpp"30#include "gc/shared/plab.hpp"3132class G1EvacuationInfo;33class G1NUMA;3435// Interface to keep track of which regions G1 is currently allocating into. Provides36// some accessors (e.g. allocating into them, or getting their occupancy).37// Also keeps track of retained regions across GCs.38class G1Allocator : public CHeapObj<mtGC> {39friend class VMStructs;4041private:42G1CollectedHeap* _g1h;43G1NUMA* _numa;4445bool _survivor_is_full;46bool _old_is_full;4748// The number of MutatorAllocRegions used, one per memory node.49size_t _num_alloc_regions;5051// Alloc region used to satisfy mutator allocation requests.52MutatorAllocRegion* _mutator_alloc_regions;5354// Alloc region used to satisfy allocation requests by the GC for55// survivor objects.56SurvivorGCAllocRegion* _survivor_gc_alloc_regions;5758// Alloc region used to satisfy allocation requests by the GC for59// old objects.60OldGCAllocRegion _old_gc_alloc_region;6162HeapRegion* _retained_old_gc_alloc_region;6364bool survivor_is_full() const;65bool old_is_full() const;6667void set_survivor_full();68void set_old_full();6970void reuse_retained_old_region(G1EvacuationInfo& evacuation_info,71OldGCAllocRegion* old,72HeapRegion** retained);7374// Accessors to the allocation regions.75inline MutatorAllocRegion* mutator_alloc_region(uint node_index);76inline SurvivorGCAllocRegion* survivor_gc_alloc_region(uint node_index);77inline OldGCAllocRegion* old_gc_alloc_region();7879// Allocation attempt during GC for a survivor object / PLAB.80HeapWord* survivor_attempt_allocation(size_t min_word_size,81size_t desired_word_size,82size_t* actual_word_size,83uint node_index);8485// Allocation attempt during GC for an old object / PLAB.86HeapWord* old_attempt_allocation(size_t min_word_size,87size_t desired_word_size,88size_t* actual_word_size);8990// Node index of current thread.91inline uint current_node_index() const;9293public:94G1Allocator(G1CollectedHeap* heap);95~G1Allocator();9697uint num_nodes() { return (uint)_num_alloc_regions; }9899#ifdef ASSERT100// Do we currently have an active mutator region to allocate into?101bool has_mutator_alloc_region();102#endif103104void init_mutator_alloc_regions();105void release_mutator_alloc_regions();106107void init_gc_alloc_regions(G1EvacuationInfo& evacuation_info);108void release_gc_alloc_regions(G1EvacuationInfo& evacuation_info);109void abandon_gc_alloc_regions();110bool is_retained_old_region(HeapRegion* hr);111112// Allocate blocks of memory during mutator time.113114// Attempt allocation in the current alloc region.115inline HeapWord* attempt_allocation(size_t min_word_size,116size_t desired_word_size,117size_t* actual_word_size);118119// Attempt allocation, retiring the current region and allocating a new one. It is120// assumed that attempt_allocation() has been tried and failed already first.121inline HeapWord* attempt_allocation_using_new_region(size_t word_size);122123// This is to be called when holding an appropriate lock. It first tries in the124// current allocation region, and then attempts an allocation using a new region.125inline HeapWord* attempt_allocation_locked(size_t word_size);126127inline HeapWord* attempt_allocation_force(size_t word_size);128129size_t unsafe_max_tlab_alloc();130size_t used_in_alloc_regions();131132// Allocate blocks of memory during garbage collection. Will ensure an133// allocation region, either by picking one or expanding the134// heap, and then allocate a block of the given size. The block135// may not be a humongous - it must fit into a single heap region.136HeapWord* par_allocate_during_gc(G1HeapRegionAttr dest,137size_t word_size,138uint node_index);139140HeapWord* par_allocate_during_gc(G1HeapRegionAttr dest,141size_t min_word_size,142size_t desired_word_size,143size_t* actual_word_size,144uint node_index);145};146147// Manages the PLABs used during garbage collection. Interface for allocation from PLABs.148// Needs to handle multiple contexts, extra alignment in any "survivor" area and some149// statistics.150class G1PLABAllocator : public CHeapObj<mtGC> {151friend class G1ParScanThreadState;152private:153typedef G1HeapRegionAttr::region_type_t region_type_t;154155G1CollectedHeap* _g1h;156G1Allocator* _allocator;157158PLAB** _alloc_buffers[G1HeapRegionAttr::Num];159160// Number of words allocated directly (not counting PLAB allocation).161size_t _direct_allocated[G1HeapRegionAttr::Num];162163void flush_and_retire_stats();164inline PLAB* alloc_buffer(G1HeapRegionAttr dest, uint node_index) const;165inline PLAB* alloc_buffer(region_type_t dest, uint node_index) const;166167// Returns the number of allocation buffers for the given dest.168// There is only 1 buffer for Old while Young may have multiple buffers depending on169// active NUMA nodes.170inline uint alloc_buffers_length(region_type_t dest) const;171172bool may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const;173public:174G1PLABAllocator(G1Allocator* allocator);175~G1PLABAllocator();176177size_t waste() const;178size_t undo_waste() const;179180// Allocate word_sz words in dest, either directly into the regions or by181// allocating a new PLAB. Returns the address of the allocated memory, NULL if182// not successful. Plab_refill_failed indicates whether an attempt to refill the183// PLAB failed or not.184HeapWord* allocate_direct_or_new_plab(G1HeapRegionAttr dest,185size_t word_sz,186bool* plab_refill_failed,187uint node_index);188189// Allocate word_sz words in the PLAB of dest. Returns the address of the190// allocated memory, NULL if not successful.191inline HeapWord* plab_allocate(G1HeapRegionAttr dest,192size_t word_sz,193uint node_index);194195inline HeapWord* allocate(G1HeapRegionAttr dest,196size_t word_sz,197bool* refill_failed,198uint node_index);199200void undo_allocation(G1HeapRegionAttr dest, HeapWord* obj, size_t word_sz, uint node_index);201};202203// G1ArchiveAllocator is used to allocate memory in archive204// regions. Such regions are not scavenged nor compacted by GC.205// There are two types of archive regions, which are206// differ in the kind of references allowed for the contained objects:207//208// - 'Closed' archive region contain no references outside of other209// closed archive regions. The region is immutable by GC. GC does210// not mark object header in 'closed' archive region.211// - An 'open' archive region allow references to any other regions,212// including closed archive, open archive and other java heap regions.213// GC can adjust pointers and mark object header in 'open' archive region.214class G1ArchiveAllocator : public CHeapObj<mtGC> {215protected:216bool _open; // Indicate if the region is 'open' archive.217G1CollectedHeap* _g1h;218219// The current allocation region220HeapRegion* _allocation_region;221222// Regions allocated for the current archive range.223GrowableArray<HeapRegion*> _allocated_regions;224225// The number of bytes used in the current range.226size_t _summary_bytes_used;227228// Current allocation window within the current region.229HeapWord* _bottom;230HeapWord* _top;231HeapWord* _max;232233// Allocate a new region for this archive allocator.234// Allocation is from the top of the reserved heap downward.235bool alloc_new_region();236237public:238G1ArchiveAllocator(G1CollectedHeap* g1h, bool open) :239_open(open),240_g1h(g1h),241_allocation_region(NULL),242_allocated_regions((ResourceObj::set_allocation_type((address) &_allocated_regions,243ResourceObj::C_HEAP),2442), mtGC),245_summary_bytes_used(0),246_bottom(NULL),247_top(NULL),248_max(NULL) { }249250virtual ~G1ArchiveAllocator() {251assert(_allocation_region == NULL, "_allocation_region not NULL");252}253254static G1ArchiveAllocator* create_allocator(G1CollectedHeap* g1h, bool open);255256// Allocate memory for an individual object.257HeapWord* archive_mem_allocate(size_t word_size);258259// Return the memory ranges used in the current archive, after260// aligning to the requested alignment.261void complete_archive(GrowableArray<MemRegion>* ranges,262size_t end_alignment_in_bytes);263264// The number of bytes allocated by this allocator.265size_t used() {266return _summary_bytes_used;267}268269// Clear the count of bytes allocated in prior G1 regions. This270// must be done when recalculate_use is used to reset the counter271// for the generic allocator, since it counts bytes in all G1272// regions, including those still associated with this allocator.273void clear_used() {274_summary_bytes_used = 0;275}276};277278#endif // SHARE_GC_G1_G1ALLOCATOR_HPP279280281