Path: blob/master/src/hotspot/share/gc/g1/g1Allocator.hpp
40961 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.113114inline HeapWord* attempt_allocation(size_t min_word_size,115size_t desired_word_size,116size_t* actual_word_size);117inline HeapWord* attempt_allocation_locked(size_t word_size);118inline HeapWord* attempt_allocation_force(size_t word_size);119120size_t unsafe_max_tlab_alloc();121size_t used_in_alloc_regions();122123// Allocate blocks of memory during garbage collection. Will ensure an124// allocation region, either by picking one or expanding the125// heap, and then allocate a block of the given size. The block126// may not be a humongous - it must fit into a single heap region.127HeapWord* par_allocate_during_gc(G1HeapRegionAttr dest,128size_t word_size,129uint node_index);130131HeapWord* par_allocate_during_gc(G1HeapRegionAttr dest,132size_t min_word_size,133size_t desired_word_size,134size_t* actual_word_size,135uint node_index);136};137138// Manages the PLABs used during garbage collection. Interface for allocation from PLABs.139// Needs to handle multiple contexts, extra alignment in any "survivor" area and some140// statistics.141class G1PLABAllocator : public CHeapObj<mtGC> {142friend class G1ParScanThreadState;143private:144typedef G1HeapRegionAttr::region_type_t region_type_t;145146G1CollectedHeap* _g1h;147G1Allocator* _allocator;148149PLAB** _alloc_buffers[G1HeapRegionAttr::Num];150151// Number of words allocated directly (not counting PLAB allocation).152size_t _direct_allocated[G1HeapRegionAttr::Num];153154void flush_and_retire_stats();155inline PLAB* alloc_buffer(G1HeapRegionAttr dest, uint node_index) const;156inline PLAB* alloc_buffer(region_type_t dest, uint node_index) const;157158// Returns the number of allocation buffers for the given dest.159// There is only 1 buffer for Old while Young may have multiple buffers depending on160// active NUMA nodes.161inline uint alloc_buffers_length(region_type_t dest) const;162163bool may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const;164public:165G1PLABAllocator(G1Allocator* allocator);166~G1PLABAllocator();167168size_t waste() const;169size_t undo_waste() const;170171// Allocate word_sz words in dest, either directly into the regions or by172// allocating a new PLAB. Returns the address of the allocated memory, NULL if173// not successful. Plab_refill_failed indicates whether an attempt to refill the174// PLAB failed or not.175HeapWord* allocate_direct_or_new_plab(G1HeapRegionAttr dest,176size_t word_sz,177bool* plab_refill_failed,178uint node_index);179180// Allocate word_sz words in the PLAB of dest. Returns the address of the181// allocated memory, NULL if not successful.182inline HeapWord* plab_allocate(G1HeapRegionAttr dest,183size_t word_sz,184uint node_index);185186inline HeapWord* allocate(G1HeapRegionAttr dest,187size_t word_sz,188bool* refill_failed,189uint node_index);190191void undo_allocation(G1HeapRegionAttr dest, HeapWord* obj, size_t word_sz, uint node_index);192};193194// G1ArchiveAllocator is used to allocate memory in archive195// regions. Such regions are not scavenged nor compacted by GC.196// There are two types of archive regions, which are197// differ in the kind of references allowed for the contained objects:198//199// - 'Closed' archive region contain no references outside of other200// closed archive regions. The region is immutable by GC. GC does201// not mark object header in 'closed' archive region.202// - An 'open' archive region allow references to any other regions,203// including closed archive, open archive and other java heap regions.204// GC can adjust pointers and mark object header in 'open' archive region.205class G1ArchiveAllocator : public CHeapObj<mtGC> {206protected:207bool _open; // Indicate if the region is 'open' archive.208G1CollectedHeap* _g1h;209210// The current allocation region211HeapRegion* _allocation_region;212213// Regions allocated for the current archive range.214GrowableArray<HeapRegion*> _allocated_regions;215216// The number of bytes used in the current range.217size_t _summary_bytes_used;218219// Current allocation window within the current region.220HeapWord* _bottom;221HeapWord* _top;222HeapWord* _max;223224// Allocate a new region for this archive allocator.225// Allocation is from the top of the reserved heap downward.226bool alloc_new_region();227228public:229G1ArchiveAllocator(G1CollectedHeap* g1h, bool open) :230_open(open),231_g1h(g1h),232_allocation_region(NULL),233_allocated_regions((ResourceObj::set_allocation_type((address) &_allocated_regions,234ResourceObj::C_HEAP),2352), mtGC),236_summary_bytes_used(0),237_bottom(NULL),238_top(NULL),239_max(NULL) { }240241virtual ~G1ArchiveAllocator() {242assert(_allocation_region == NULL, "_allocation_region not NULL");243}244245static G1ArchiveAllocator* create_allocator(G1CollectedHeap* g1h, bool open);246247// Allocate memory for an individual object.248HeapWord* archive_mem_allocate(size_t word_size);249250// Return the memory ranges used in the current archive, after251// aligning to the requested alignment.252void complete_archive(GrowableArray<MemRegion>* ranges,253size_t end_alignment_in_bytes);254255// The number of bytes allocated by this allocator.256size_t used() {257return _summary_bytes_used;258}259260// Clear the count of bytes allocated in prior G1 regions. This261// must be done when recalculate_use is used to reset the counter262// for the generic allocator, since it counts bytes in all G1263// regions, including those still associated with this allocator.264void clear_used() {265_summary_bytes_used = 0;266}267};268269#endif // SHARE_GC_G1_G1ALLOCATOR_HPP270271272