Path: blob/master/src/hotspot/share/gc/g1/g1AllocRegion.hpp
40961 views
/*1* Copyright (c) 2011, 2019, 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_G1ALLOCREGION_HPP25#define SHARE_GC_G1_G1ALLOCREGION_HPP2627#include "gc/g1/heapRegion.hpp"28#include "gc/g1/g1EvacStats.hpp"29#include "gc/g1/g1HeapRegionAttr.hpp"30#include "gc/g1/g1NUMA.hpp"3132class G1CollectedHeap;3334// A class that holds a region that is active in satisfying allocation35// requests, potentially issued in parallel. When the active region is36// full it will be retired and replaced with a new one. The37// implementation assumes that fast-path allocations will be lock-free38// and a lock will need to be taken when the active region needs to be39// replaced.4041class G1AllocRegion : public CHeapObj<mtGC> {4243private:44// The active allocating region we are currently allocating out45// of. The invariant is that if this object is initialized (i.e.,46// init() has been called and release() has not) then _alloc_region47// is either an active allocating region or the dummy region (i.e.,48// it can never be NULL) and this object can be used to satisfy49// allocation requests. If this object is not initialized50// (i.e. init() has not been called or release() has been called)51// then _alloc_region is NULL and this object should not be used to52// satisfy allocation requests (it was done this way to force the53// correct use of init() and release()).54HeapRegion* volatile _alloc_region;5556// It keeps track of the distinct number of regions that are used57// for allocation in the active interval of this object, i.e.,58// between a call to init() and a call to release(). The count59// mostly includes regions that are freshly allocated, as well as60// the region that is re-used using the set() method. This count can61// be used in any heuristics that might want to bound how many62// distinct regions this object can used during an active interval.63uint _count;6465// When we set up a new active region we save its used bytes in this66// field so that, when we retire it, we can calculate how much space67// we allocated in it.68size_t _used_bytes_before;6970// When true, indicates that allocate calls should do BOT updates.71const bool _bot_updates;7273// Useful for debugging and tracing.74const char* _name;7576// A dummy region (i.e., it's been allocated specially for this77// purpose and it is not part of the heap) that is full (i.e., top()78// == end()). When we don't have a valid active region we make79// _alloc_region point to this. This allows us to skip checking80// whether the _alloc_region is NULL or not.81static HeapRegion* _dummy_region;8283// After a region is allocated by alloc_new_region, this84// method is used to set it as the active alloc_region85void update_alloc_region(HeapRegion* alloc_region);8687// Allocate a new active region and use it to perform a word_size88// allocation. The force parameter will be passed on to89// G1CollectedHeap::allocate_new_alloc_region() and tells it to try90// to allocate a new region even if the max has been reached.91HeapWord* new_alloc_region_and_allocate(size_t word_size, bool force);9293protected:94// The memory node index this allocation region belongs to.95uint _node_index;9697// Reset the alloc region to point a the dummy region.98void reset_alloc_region();99100// Perform a non-MT-safe allocation out of the given region.101inline HeapWord* allocate(HeapRegion* alloc_region,102size_t word_size);103104// Perform a MT-safe allocation out of the given region.105inline HeapWord* par_allocate(HeapRegion* alloc_region,106size_t word_size);107// Perform a MT-safe allocation out of the given region, with the given108// minimum and desired size. Returns the actual size allocated (between109// minimum and desired size) in actual_word_size if the allocation has been110// successful.111inline HeapWord* par_allocate(HeapRegion* alloc_region,112size_t min_word_size,113size_t desired_word_size,114size_t* actual_word_size);115116// Ensure that the region passed as a parameter has been filled up117// so that noone else can allocate out of it any more.118// Returns the number of bytes that have been wasted by filled up119// the space.120size_t fill_up_remaining_space(HeapRegion* alloc_region);121122// Retire the active allocating region. If fill_up is true then make123// sure that the region is full before we retire it so that no one124// else can allocate out of it.125// Returns the number of bytes that have been filled up during retire.126virtual size_t retire(bool fill_up);127128size_t retire_internal(HeapRegion* alloc_region, bool fill_up);129130// For convenience as subclasses use it.131static G1CollectedHeap* _g1h;132133virtual HeapRegion* allocate_new_region(size_t word_size, bool force) = 0;134virtual void retire_region(HeapRegion* alloc_region,135size_t allocated_bytes) = 0;136137G1AllocRegion(const char* name, bool bot_updates, uint node_index);138139public:140static void setup(G1CollectedHeap* g1h, HeapRegion* dummy_region);141142HeapRegion* get() const {143HeapRegion * hr = _alloc_region;144// Make sure that the dummy region does not escape this class.145return (hr == _dummy_region) ? NULL : hr;146}147148uint count() { return _count; }149150// The following two are the building blocks for the allocation method.151152// First-level allocation: Should be called without holding a153// lock. It will try to allocate lock-free out of the active region,154// or return NULL if it was unable to.155inline HeapWord* attempt_allocation(size_t word_size);156// Perform an allocation out of the current allocation region, with the given157// minimum and desired size. Returns the actual size allocated (between158// minimum and desired size) in actual_word_size if the allocation has been159// successful.160// Should be called without holding a lock. It will try to allocate lock-free161// out of the active region, or return NULL if it was unable to.162inline HeapWord* attempt_allocation(size_t min_word_size,163size_t desired_word_size,164size_t* actual_word_size);165166// Second-level allocation: Should be called while holding a167// lock. It will try to first allocate lock-free out of the active168// region or, if it's unable to, it will try to replace the active169// alloc region with a new one. We require that the caller takes the170// appropriate lock before calling this so that it is easier to make171// it conform to its locking protocol.172inline HeapWord* attempt_allocation_locked(size_t word_size);173// Same as attempt_allocation_locked(size_t, bool), but allowing specification174// of minimum word size of the block in min_word_size, and the maximum word175// size of the allocation in desired_word_size. The actual size of the block is176// returned in actual_word_size.177inline HeapWord* attempt_allocation_locked(size_t min_word_size,178size_t desired_word_size,179size_t* actual_word_size);180181// Should be called to allocate a new region even if the max of this182// type of regions has been reached. Should only be called if other183// allocation attempts have failed and we are not holding a valid184// active region.185inline HeapWord* attempt_allocation_force(size_t word_size);186187// Should be called before we start using this object.188virtual void init();189190// This can be used to set the active region to a specific191// region. (Use Example: we try to retain the last old GC alloc192// region that we've used during a GC and we can use set() to193// re-instate it at the beginning of the next GC.)194void set(HeapRegion* alloc_region);195196// Should be called when we want to release the active region which197// is returned after it's been retired.198virtual HeapRegion* release();199200void trace(const char* str,201size_t min_word_size = 0,202size_t desired_word_size = 0,203size_t actual_word_size = 0,204HeapWord* result = NULL) PRODUCT_RETURN;205};206207class MutatorAllocRegion : public G1AllocRegion {208private:209// Keeps track of the total waste generated during the current210// mutator phase.211size_t _wasted_bytes;212213// Retained allocation region. Used to lower the waste generated214// during mutation by having two active regions if the free space215// in a region about to be retired still could fit a TLAB.216HeapRegion* volatile _retained_alloc_region;217218// Decide if the region should be retained, based on the free size219// in it and the free size in the currently retained region, if any.220bool should_retain(HeapRegion* region);221protected:222virtual HeapRegion* allocate_new_region(size_t word_size, bool force);223virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);224virtual size_t retire(bool fill_up);225public:226MutatorAllocRegion(uint node_index)227: G1AllocRegion("Mutator Alloc Region", false /* bot_updates */, node_index),228_wasted_bytes(0),229_retained_alloc_region(NULL) { }230231// Returns the combined used memory in the current alloc region and232// the retained alloc region.233size_t used_in_alloc_regions();234235// Perform an allocation out of the retained allocation region, with the given236// minimum and desired size. Returns the actual size allocated (between237// minimum and desired size) in actual_word_size if the allocation has been238// successful.239// Should be called without holding a lock. It will try to allocate lock-free240// out of the retained region, or return NULL if it was unable to.241inline HeapWord* attempt_retained_allocation(size_t min_word_size,242size_t desired_word_size,243size_t* actual_word_size);244245// This specialization of release() makes sure that the retained alloc246// region is retired and set to NULL.247virtual HeapRegion* release();248249virtual void init();250};251252// Common base class for allocation regions used during GC.253class G1GCAllocRegion : public G1AllocRegion {254protected:255G1EvacStats* _stats;256G1HeapRegionAttr::region_type_t _purpose;257258virtual HeapRegion* allocate_new_region(size_t word_size, bool force);259virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);260261virtual size_t retire(bool fill_up);262263G1GCAllocRegion(const char* name, bool bot_updates, G1EvacStats* stats,264G1HeapRegionAttr::region_type_t purpose, uint node_index = G1NUMA::AnyNodeIndex)265: G1AllocRegion(name, bot_updates, node_index), _stats(stats), _purpose(purpose) {266assert(stats != NULL, "Must pass non-NULL PLAB statistics");267}268};269270class SurvivorGCAllocRegion : public G1GCAllocRegion {271public:272SurvivorGCAllocRegion(G1EvacStats* stats, uint node_index)273: G1GCAllocRegion("Survivor GC Alloc Region", false /* bot_updates */, stats, G1HeapRegionAttr::Young, node_index) { }274};275276class OldGCAllocRegion : public G1GCAllocRegion {277public:278OldGCAllocRegion(G1EvacStats* stats)279: G1GCAllocRegion("Old GC Alloc Region", true /* bot_updates */, stats, G1HeapRegionAttr::Old) { }280281// This specialization of release() makes sure that the last card that has282// been allocated into has been completely filled by a dummy object. This283// avoids races when remembered set scanning wants to update the BOT of the284// last card in the retained old gc alloc region, and allocation threads285// allocating into that card at the same time.286virtual HeapRegion* release();287};288289#endif // SHARE_GC_G1_G1ALLOCREGION_HPP290291292