Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/g1/g1AllocRegion.hpp
38920 views
/*1* Copyright (c) 2011, 2014, 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_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP25#define SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP2627#include "gc_implementation/g1/heapRegion.hpp"2829class G1CollectedHeap;3031// 0 -> no tracing, 1 -> basic tracing, 2 -> basic + allocation tracing32#define G1_ALLOC_REGION_TRACING 03334class ar_ext_msg;3536// A class that holds a region that is active in satisfying allocation37// requests, potentially issued in parallel. When the active region is38// full it will be retired and replaced with a new one. The39// implementation assumes that fast-path allocations will be lock-free40// and a lock will need to be taken when the active region needs to be41// replaced.4243class G1AllocRegion VALUE_OBJ_CLASS_SPEC {44friend class ar_ext_msg;4546private:47// The active allocating region we are currently allocating out48// of. The invariant is that if this object is initialized (i.e.,49// init() has been called and release() has not) then _alloc_region50// is either an active allocating region or the dummy region (i.e.,51// it can never be NULL) and this object can be used to satisfy52// allocation requests. If this object is not initialized53// (i.e. init() has not been called or release() has been called)54// then _alloc_region is NULL and this object should not be used to55// satisfy allocation requests (it was done this way to force the56// correct use of init() and release()).57HeapRegion* volatile _alloc_region;5859// Allocation context associated with this alloc region.60AllocationContext_t _allocation_context;6162// It keeps track of the distinct number of regions that are used63// for allocation in the active interval of this object, i.e.,64// between a call to init() and a call to release(). The count65// mostly includes regions that are freshly allocated, as well as66// the region that is re-used using the set() method. This count can67// be used in any heuristics that might want to bound how many68// distinct regions this object can used during an active interval.69uint _count;7071// When we set up a new active region we save its used bytes in this72// field so that, when we retire it, we can calculate how much space73// we allocated in it.74size_t _used_bytes_before;7576// When true, indicates that allocate calls should do BOT updates.77const bool _bot_updates;7879// Useful for debugging and tracing.80const char* _name;8182// A dummy region (i.e., it's been allocated specially for this83// purpose and it is not part of the heap) that is full (i.e., top()84// == end()). When we don't have a valid active region we make85// _alloc_region point to this. This allows us to skip checking86// whether the _alloc_region is NULL or not.87static HeapRegion* _dummy_region;8889// Some of the methods below take a bot_updates parameter. Its value90// should be the same as the _bot_updates field. The idea is that91// the parameter will be a constant for a particular alloc region92// and, given that these methods will be hopefully inlined, the93// compiler should compile out the test.9495// Perform a non-MT-safe allocation out of the given region.96static inline HeapWord* allocate(HeapRegion* alloc_region,97size_t word_size,98bool bot_updates);99100// Perform a MT-safe allocation out of the given region.101static inline HeapWord* par_allocate(HeapRegion* alloc_region,102size_t word_size,103bool bot_updates);104105// Ensure that the region passed as a parameter has been filled up106// so that noone else can allocate out of it any more.107static void fill_up_remaining_space(HeapRegion* alloc_region,108bool bot_updates);109110// Retire the active allocating region. If fill_up is true then make111// sure that the region is full before we retire it so that noone112// else can allocate out of it.113void retire(bool fill_up);114115// After a region is allocated by alloc_new_region, this116// method is used to set it as the active alloc_region117void update_alloc_region(HeapRegion* alloc_region);118119// Allocate a new active region and use it to perform a word_size120// allocation. The force parameter will be passed on to121// G1CollectedHeap::allocate_new_alloc_region() and tells it to try122// to allocate a new region even if the max has been reached.123HeapWord* new_alloc_region_and_allocate(size_t word_size, bool force);124125void fill_in_ext_msg(ar_ext_msg* msg, const char* message);126127protected:128// For convenience as subclasses use it.129static G1CollectedHeap* _g1h;130131virtual HeapRegion* allocate_new_region(size_t word_size, bool force) = 0;132virtual void retire_region(HeapRegion* alloc_region,133size_t allocated_bytes) = 0;134135G1AllocRegion(const char* name, bool bot_updates);136137public:138static void setup(G1CollectedHeap* g1h, HeapRegion* dummy_region);139140HeapRegion* get() const {141HeapRegion * hr = _alloc_region;142// Make sure that the dummy region does not escape this class.143return (hr == _dummy_region) ? NULL : hr;144}145146void set_allocation_context(AllocationContext_t context) { _allocation_context = context; }147AllocationContext_t allocation_context() { return _allocation_context; }148149uint count() { return _count; }150151// The following two are the building blocks for the allocation method.152153// First-level allocation: Should be called without holding a154// lock. It will try to allocate lock-free out of the active region,155// or return NULL if it was unable to.156inline HeapWord* attempt_allocation(size_t word_size, bool bot_updates);157158// Second-level allocation: Should be called while holding a159// lock. It will try to first allocate lock-free out of the active160// region or, if it's unable to, it will try to replace the active161// alloc region with a new one. We require that the caller takes the162// appropriate lock before calling this so that it is easier to make163// it conform to its locking protocol.164inline HeapWord* attempt_allocation_locked(size_t word_size,165bool bot_updates);166167// Should be called to allocate a new region even if the max of this168// type of regions has been reached. Should only be called if other169// allocation attempts have failed and we are not holding a valid170// active region.171inline HeapWord* attempt_allocation_force(size_t word_size,172bool bot_updates);173174// Should be called before we start using this object.175void init();176177// This can be used to set the active region to a specific178// region. (Use Example: we try to retain the last old GC alloc179// region that we've used during a GC and we can use set() to180// re-instate it at the beginning of the next GC.)181void set(HeapRegion* alloc_region);182183// Should be called when we want to release the active region which184// is returned after it's been retired.185virtual HeapRegion* release();186187#if G1_ALLOC_REGION_TRACING188void trace(const char* str, size_t word_size = 0, HeapWord* result = NULL);189#else // G1_ALLOC_REGION_TRACING190void trace(const char* str, size_t word_size = 0, HeapWord* result = NULL) { }191#endif // G1_ALLOC_REGION_TRACING192};193194class MutatorAllocRegion : public G1AllocRegion {195protected:196virtual HeapRegion* allocate_new_region(size_t word_size, bool force);197virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);198public:199MutatorAllocRegion()200: G1AllocRegion("Mutator Alloc Region", false /* bot_updates */) { }201};202203class SurvivorGCAllocRegion : public G1AllocRegion {204protected:205virtual HeapRegion* allocate_new_region(size_t word_size, bool force);206virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);207public:208SurvivorGCAllocRegion()209: G1AllocRegion("Survivor GC Alloc Region", false /* bot_updates */) { }210};211212class OldGCAllocRegion : public G1AllocRegion {213protected:214virtual HeapRegion* allocate_new_region(size_t word_size, bool force);215virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);216public:217OldGCAllocRegion()218: G1AllocRegion("Old GC Alloc Region", true /* bot_updates */) { }219220// This specialization of release() makes sure that the last card that has221// been allocated into has been completely filled by a dummy object. This222// avoids races when remembered set scanning wants to update the BOT of the223// last card in the retained old gc alloc region, and allocation threads224// allocating into that card at the same time.225virtual HeapRegion* release();226};227228class ar_ext_msg : public err_msg {229public:230ar_ext_msg(G1AllocRegion* alloc_region, const char *message) : err_msg("%s", "") {231alloc_region->fill_in_ext_msg(this, message);232}233};234235#endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP236237238