tonyp@2715: /* tonyp@3713: * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. tonyp@2715: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. tonyp@2715: * tonyp@2715: * This code is free software; you can redistribute it and/or modify it tonyp@2715: * under the terms of the GNU General Public License version 2 only, as tonyp@2715: * published by the Free Software Foundation. tonyp@2715: * tonyp@2715: * This code is distributed in the hope that it will be useful, but WITHOUT tonyp@2715: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or tonyp@2715: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License tonyp@2715: * version 2 for more details (a copy is included in the LICENSE file that tonyp@2715: * accompanied this code). tonyp@2715: * tonyp@2715: * You should have received a copy of the GNU General Public License version tonyp@2715: * 2 along with this work; if not, write to the Free Software Foundation, tonyp@2715: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. tonyp@2715: * tonyp@2715: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA tonyp@2715: * or visit www.oracle.com if you need additional information or have any tonyp@2715: * questions. tonyp@2715: * tonyp@2715: */ tonyp@2715: tonyp@2715: #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP tonyp@2715: #define SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP tonyp@2715: tonyp@2715: #include "gc_implementation/g1/heapRegion.hpp" tonyp@2715: tonyp@2715: class G1CollectedHeap; tonyp@2715: tonyp@2715: // 0 -> no tracing, 1 -> basic tracing, 2 -> basic + allocation tracing tonyp@2715: #define G1_ALLOC_REGION_TRACING 0 tonyp@2715: tonyp@2715: class ar_ext_msg; tonyp@2715: tonyp@2715: // A class that holds a region that is active in satisfying allocation tonyp@2715: // requests, potentially issued in parallel. When the active region is tonyp@3028: // full it will be retired and replaced with a new one. The tonyp@2715: // implementation assumes that fast-path allocations will be lock-free tonyp@2715: // and a lock will need to be taken when the active region needs to be tonyp@2715: // replaced. tonyp@2715: tonyp@2715: class G1AllocRegion VALUE_OBJ_CLASS_SPEC { tonyp@2715: friend class ar_ext_msg; tonyp@2715: tonyp@2715: private: tonyp@2715: // The active allocating region we are currently allocating out tonyp@2715: // of. The invariant is that if this object is initialized (i.e., tonyp@2715: // init() has been called and release() has not) then _alloc_region tonyp@2715: // is either an active allocating region or the dummy region (i.e., tonyp@2715: // it can never be NULL) and this object can be used to satisfy tonyp@2715: // allocation requests. If this object is not initialized tonyp@2715: // (i.e. init() has not been called or release() has been called) tonyp@2715: // then _alloc_region is NULL and this object should not be used to tonyp@2715: // satisfy allocation requests (it was done this way to force the tonyp@2715: // correct use of init() and release()). tonyp@2715: HeapRegion* _alloc_region; tonyp@2715: tonyp@3028: // It keeps track of the distinct number of regions that are used tonyp@3028: // for allocation in the active interval of this object, i.e., tonyp@3028: // between a call to init() and a call to release(). The count tonyp@3028: // mostly includes regions that are freshly allocated, as well as tonyp@3028: // the region that is re-used using the set() method. This count can tonyp@3028: // be used in any heuristics that might want to bound how many tonyp@3028: // distinct regions this object can used during an active interval. tonyp@3713: uint _count; tonyp@3028: tonyp@2715: // When we set up a new active region we save its used bytes in this tonyp@2715: // field so that, when we retire it, we can calculate how much space tonyp@2715: // we allocated in it. tonyp@2715: size_t _used_bytes_before; tonyp@2715: tonyp@3028: // When true, indicates that allocate calls should do BOT updates. tonyp@3028: const bool _bot_updates; tonyp@2715: tonyp@2715: // Useful for debugging and tracing. tonyp@2715: const char* _name; tonyp@2715: tonyp@2715: // A dummy region (i.e., it's been allocated specially for this tonyp@2715: // purpose and it is not part of the heap) that is full (i.e., top() tonyp@2715: // == end()). When we don't have a valid active region we make tonyp@2715: // _alloc_region point to this. This allows us to skip checking tonyp@2715: // whether the _alloc_region is NULL or not. tonyp@2715: static HeapRegion* _dummy_region; tonyp@2715: tonyp@2715: // Some of the methods below take a bot_updates parameter. Its value tonyp@2715: // should be the same as the _bot_updates field. The idea is that tonyp@2715: // the parameter will be a constant for a particular alloc region tonyp@2715: // and, given that these methods will be hopefully inlined, the tonyp@2715: // compiler should compile out the test. tonyp@2715: tonyp@2715: // Perform a non-MT-safe allocation out of the given region. tonyp@2715: static inline HeapWord* allocate(HeapRegion* alloc_region, tonyp@2715: size_t word_size, tonyp@2715: bool bot_updates); tonyp@2715: tonyp@2715: // Perform a MT-safe allocation out of the given region. tonyp@2715: static inline HeapWord* par_allocate(HeapRegion* alloc_region, tonyp@2715: size_t word_size, tonyp@2715: bool bot_updates); tonyp@2715: tonyp@2715: // Ensure that the region passed as a parameter has been filled up tonyp@2715: // so that noone else can allocate out of it any more. tonyp@2715: static void fill_up_remaining_space(HeapRegion* alloc_region, tonyp@2715: bool bot_updates); tonyp@2715: tonyp@2715: // Retire the active allocating region. If fill_up is true then make tonyp@2715: // sure that the region is full before we retire it so that noone tonyp@2715: // else can allocate out of it. tonyp@2715: void retire(bool fill_up); tonyp@2715: tonyp@2715: // Allocate a new active region and use it to perform a word_size tonyp@2715: // allocation. The force parameter will be passed on to tonyp@2715: // G1CollectedHeap::allocate_new_alloc_region() and tells it to try tonyp@2715: // to allocate a new region even if the max has been reached. tonyp@2715: HeapWord* new_alloc_region_and_allocate(size_t word_size, bool force); tonyp@2715: tonyp@2715: void fill_in_ext_msg(ar_ext_msg* msg, const char* message); tonyp@2715: tonyp@2715: protected: tonyp@2715: // For convenience as subclasses use it. tonyp@2715: static G1CollectedHeap* _g1h; tonyp@2715: tonyp@2715: virtual HeapRegion* allocate_new_region(size_t word_size, bool force) = 0; tonyp@2715: virtual void retire_region(HeapRegion* alloc_region, tonyp@2715: size_t allocated_bytes) = 0; tonyp@2715: tonyp@2715: G1AllocRegion(const char* name, bool bot_updates); tonyp@2715: tonyp@2715: public: tonyp@2715: static void setup(G1CollectedHeap* g1h, HeapRegion* dummy_region); tonyp@2715: tonyp@2715: HeapRegion* get() const { tonyp@2715: // Make sure that the dummy region does not escape this class. tonyp@2715: return (_alloc_region == _dummy_region) ? NULL : _alloc_region; tonyp@2715: } tonyp@2715: tonyp@3713: uint count() { return _count; } tonyp@3028: tonyp@2715: // The following two are the building blocks for the allocation method. tonyp@2715: tonyp@2715: // First-level allocation: Should be called without holding a tonyp@2715: // lock. It will try to allocate lock-free out of the active region, tonyp@2715: // or return NULL if it was unable to. tonyp@2715: inline HeapWord* attempt_allocation(size_t word_size, bool bot_updates); tonyp@2715: tonyp@2715: // Second-level allocation: Should be called while holding a tonyp@2715: // lock. It will try to first allocate lock-free out of the active tonyp@2715: // region or, if it's unable to, it will try to replace the active tonyp@2715: // alloc region with a new one. We require that the caller takes the tonyp@2715: // appropriate lock before calling this so that it is easier to make tonyp@2715: // it conform to its locking protocol. tonyp@2715: inline HeapWord* attempt_allocation_locked(size_t word_size, tonyp@2715: bool bot_updates); tonyp@2715: tonyp@2715: // Should be called to allocate a new region even if the max of this tonyp@2715: // type of regions has been reached. Should only be called if other tonyp@2715: // allocation attempts have failed and we are not holding a valid tonyp@2715: // active region. tonyp@2715: inline HeapWord* attempt_allocation_force(size_t word_size, tonyp@2715: bool bot_updates); tonyp@2715: tonyp@2715: // Should be called before we start using this object. tonyp@2715: void init(); tonyp@2715: tonyp@3028: // This can be used to set the active region to a specific tonyp@3028: // region. (Use Example: we try to retain the last old GC alloc tonyp@3028: // region that we've used during a GC and we can use set() to tonyp@3028: // re-instate it at the beginning of the next GC.) tonyp@3028: void set(HeapRegion* alloc_region); tonyp@3028: tonyp@2715: // Should be called when we want to release the active region which tonyp@2715: // is returned after it's been retired. tonyp@2715: HeapRegion* release(); tonyp@2715: tonyp@2715: #if G1_ALLOC_REGION_TRACING tonyp@2715: void trace(const char* str, size_t word_size = 0, HeapWord* result = NULL); tonyp@2715: #else // G1_ALLOC_REGION_TRACING tonyp@2715: void trace(const char* str, size_t word_size = 0, HeapWord* result = NULL) { } tonyp@2715: #endif // G1_ALLOC_REGION_TRACING tonyp@2715: }; tonyp@2715: tonyp@2715: class ar_ext_msg : public err_msg { tonyp@2715: public: tonyp@2715: ar_ext_msg(G1AllocRegion* alloc_region, const char *message) : err_msg("") { tonyp@2715: alloc_region->fill_in_ext_msg(this, message); tonyp@2715: } tonyp@2715: }; tonyp@2715: tonyp@2715: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP