collectedHeap.hpp revision 342
/*
* Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
// A "CollectedHeap" is an implementation of a java heap for HotSpot. This
// is an abstract class: there may be many different kinds of heaps. This
// class defines the functions that a heap must implement, and contains
// infrastructure common to all heaps.
class BarrierSet;
class ThreadClosure;
class AdaptiveSizePolicy;
class Thread;
//
// CollectedHeap
// SharedHeap
// GenCollectedHeap
// G1CollectedHeap
// ParallelScavengeHeap
//
class CollectedHeap : public CHeapObj {
friend class VMStructs;
friend class IsGCActiveMark; // Block structured external access to _is_gc_active
#ifdef ASSERT
static int _fire_out_of_memory_count;
#endif
protected:
bool _is_gc_active;
unsigned int _total_collections; // ... started
unsigned int _total_full_collections; // ... started
// Reason for current garbage collection. Should be set to
// a value reflecting no collection between collections.
// Constructor
// Create a new tlab
// Fix up tlabs to make the heap well-formed again,
// optionally retiring the tlabs.
virtual void fill_all_tlabs(bool retire);
// Accumulate statistics on all tlabs.
virtual void accumulate_statistics_all_tlabs();
// Reinitialize tlabs before resuming mutators.
virtual void resize_all_tlabs();
debug_only(static void check_for_valid_allocation_state();)
protected:
// Allocate from the current thread's TLAB, with broken-out slow path.
// Allocate an uninitialized block of the given size, or returns NULL if
// this is impossible.
// Like allocate_init, but the block returned by a successful allocation
// is guaranteed initialized to zeros.
// Same as common_mem version, except memory is allocated in the permanent area
// If there is no permanent area, revert to common_mem_allocate_noinit
// Same as common_mem version, except memory is allocated in the permanent area
// If there is no permanent area, revert to common_mem_allocate_init
// Helper functions for (VM) allocation.
int length);
// Clears an allocated object.
// Verification functions
public:
enum Name {
};
/**
* Returns JNI error code JNI_ENOMEM if memory could not be allocated,
* and JNI_OK on success.
*/
virtual jint initialize() = 0;
// In many heaps, there will be a need to perform some initialization activities
// after the Universe is fully formed, but before general heap allocation is allowed.
// This is the correct place to place such initialization methods.
virtual void post_initialize() = 0;
// Future cleanup here. The following functions should specify bytes or
// heapwords as part of their signature.
// Return "true" if the part of the heap that allocates Java
// objects has reached the maximal committed limit that it can
// reach, without a garbage collection.
virtual bool is_maximal_no_gc() const = 0;
virtual size_t permanent_capacity() const = 0;
virtual size_t permanent_used() const = 0;
// Support for java.lang.Runtime.maxMemory(): return the maximum amount of
// memory that the vm could make available for storing 'normal' java objects.
// This is based on the reserved address space, but should not include space
// that the vm uses internally for bookkeeping or temporary storage (e.g.,
// perm gen space or, in the case of the young gen, one of the survivor
// spaces).
virtual size_t max_capacity() const = 0;
// Returns "TRUE" if "p" points into the reserved area of the heap.
bool is_in_reserved(const void* p) const {
}
bool is_in_reserved_or_null(const void* p) const {
return p == NULL || is_in_reserved(p);
}
// Returns "TRUE" if "p" points to the head of an allocated object in the
// heap. Since this method can be expensive in general, we restrict its
// use to assertion checking only.
virtual bool is_in(const void* p) const = 0;
bool is_in_or_null(const void* p) const {
}
// Let's define some terms: a "closed" subset of a heap is one that
//
// 1) contains all currently-allocated objects, and
//
// 2) is closed under reference: no object in the closed subset
// references one outside the closed subset.
//
// Membership in a heap's closed subset is useful for assertions.
// Clearly, the entire heap is a closed subset, so the default
// implementation is to use "is_in_reserved". But this may not be too
// liberal to perform useful checking. Also, the "is_in" predicate
// defines a closed subset, but may be too expensive, since "is_in"
// verifies that its argument points to an object head. The
// "closed_subset" method allows a heap to define an intermediate
// predicate, allowing more precise checking than "is_in_reserved" at
// lower cost than "is_in."
// One important case is a heap composed of disjoint contiguous spaces,
// such as the Garbage-First collector. Such heaps have a convenient
// closed subset consisting of the allocated portions of those
// contiguous spaces.
// Return "TRUE" iff the given pointer points into the heap's defined
// closed subset (which defaults to the entire heap).
virtual bool is_in_closed_subset(const void* p) const {
return is_in_reserved(p);
}
bool is_in_closed_subset_or_null(const void* p) const {
return p == NULL || is_in_closed_subset(p);
}
// Returns "TRUE" if "p" is allocated as "permanent" data.
// If the heap does not use "permanent" data, returns the same
// value is_in_reserved() would return.
// NOTE: this actually returns true if "p" is in reserved space
// for the space not that it is actually allocated (i.e. in committed
// space). If you need the more conservative answer use is_permanent().
virtual bool is_in_permanent(const void *p) const = 0;
// Returns "TRUE" if "p" is in the committed area of "permanent" data.
// If the heap does not use "permanent" data, returns the same
// value is_in() would return.
virtual bool is_permanent(const void *p) const = 0;
bool is_in_permanent_or_null(const void *p) const {
return p == NULL || is_in_permanent(p);
}
// Returns "TRUE" if "p" is a method oop in the
// current heap, with high probability. This predicate
// is not stable, in general.
bool is_valid_method(oop p) const;
if (UsePerfData) {
}
_gc_cause = v;
}
// Preload classes into the shared portion of the heap, and then dump
// that data to a file so that it can be loaded directly by another
// VM (then terminate).
// Some heaps may want to manage "permanent" data uniquely. These default
// to the general routines if the heap does not support such handling.
// permanent_obj_allocate_no_klass_install() does not do the installation of
// the klass pointer in the newly created object (as permanent_obj_allocate()
// above does). This allows for a delay in the installation of the klass
// pointer that is needed during the create of klassKlass's. The
// method post_allocation_install_obj_klass() is used to install the
// klass pointer.
int size,
TRAPS);
int size);
// Raw memory allocation facilities
// The obj and array allocate methods are covers for these methods.
// The permanent allocation method should default to mem_allocate if
// permanent memory isn't supported.
bool is_noref,
bool is_tlab,
bool* gc_overhead_limit_was_exceeded) = 0;
// The boundary between a "large" and "small" array of primitives, in words.
virtual size_t large_typearray_limit() = 0;
// Some heaps may offer a contiguous region for shared non-blocking
// allocation, via inlined code (by exporting the address of the top and
// end fields defining the extent of the contiguous allocation region.)
// This function returns "true" iff the heap supports this kind of
// allocation. (Default is "no".)
virtual bool supports_inline_contig_alloc() const {
return false;
}
// These functions return the addresses of the fields that define the
// boundaries of the contiguous allocation area. (These fields should be
// physically near to one another.)
guarantee(false, "inline contiguous allocation not supported");
return NULL;
}
guarantee(false, "inline contiguous allocation not supported");
return NULL;
}
// Some heaps may be in an unparseable state at certain times between
// collections. This may be necessary for efficient implementation of
// certain allocation-related activities. Calling this function before
// attempting to parse a heap ensures that the heap is in a parsable
// state (provided other concurrent activity does not introduce
// unparsability). It is normally expected, therefore, that this
// method is invoked with the world stopped.
// NOTE: if you override this method, make sure you call
// super::ensure_parsability so that the non-generational
// part of the work gets done. See implementation of
// CollectedHeap::ensure_parsability and, for instance,
// that of GenCollectedHeap::ensure_parsability().
// The argument "retire_tlabs" controls whether existing TLABs
// are merely filled or also retired, thus preventing further
// allocation from them and necessitating allocation of new TLABs.
virtual void ensure_parsability(bool retire_tlabs);
// Return an estimate of the maximum allocation that could be performed
// without triggering any collection or expansion activity. In a
// generational collector, for example, this is probably the largest
// allocation that could be supported (without expansion) in the youngest
// generation. It is "unsafe" because no locks are taken; the result
// should be treated as an approximation, not a guarantee, for use in
// heuristic resizing decisions.
virtual size_t unsafe_max_alloc() = 0;
// Section on thread-local allocation buffers (TLABs)
// If the heap supports thread-local allocation buffers, it should override
// the following methods:
// Returns "true" iff the heap supports thread-local allocation buffers.
// The default is "no".
virtual bool supports_tlab_allocation() const {
return false;
}
// The amount of space available for thread-local allocation buffers.
guarantee(false, "thread-local allocation buffers not supported");
return 0;
}
// An estimate of the maximum allocation that could be performed
// for thread-local allocation buffers without triggering any
// collection or expansion activity.
guarantee(false, "thread-local allocation buffers not supported");
return 0;
}
// Can a compiler initialize a new object without store barriers?
// This permission only extends from the creation of a new object
// via a TLAB up to the first subsequent safepoint.
virtual bool can_elide_tlab_store_barriers() const = 0;
// If a compiler is eliding store barriers for TLAB-allocated objects,
// there is probably a corresponding slow path which can produce
// an object allocated anywhere. The compiler's runtime support
// promises to call this function on such a slow-path-allocated
// object before performing initializations that have elided
// store barriers. Returns new_obj, or maybe a safer copy thereof.
// Can a compiler elide a store barrier when it writes
// a permanent oop into the heap? Applies when the compiler
// is storing x to the heap, where x->is_perm() is true.
virtual bool can_elide_permanent_oop_store_barriers() const = 0;
// Does this heap support heap inspection (+PrintClassHistogram?)
virtual bool supports_heap_inspection() const = 0;
// Perform a collection of the heap; intended for use in implementing
// "System.gc". This probably implies as full a collection as the
// "CollectedHeap" supports.
// This interface assumes that it's being called by the
// vm thread. It collects the heap assuming that the
// heap lock is already held and that we are executing in
// the context of the vm thread.
// Returns the barrier set for this heap
// Returns "true" iff there is a stop-world GC in progress. (I assume
// that it should answer "false" for the concurrent part of a concurrent
// collector -- dld).
bool is_gc_active() const { return _is_gc_active; }
// Total number of GC collections (started)
unsigned int total_collections() const { return _total_collections; }
unsigned int total_full_collections() const { return _total_full_collections;}
// Increment total number of GC collections (started)
// Should be protected but used by PSMarkSweep - cleanup for 1.4.2
void increment_total_collections(bool full = false) {
if (full) {
}
}
void increment_total_full_collections() { _total_full_collections++; }
// Return the AdaptiveSizePolicy for the heap.
virtual AdaptiveSizePolicy* size_policy() = 0;
// Iterate over all the ref-containing fields of all objects, calling
// "cl.do_oop" on each. This includes objects in permanent memory.
// Iterate over all objects, calling "cl.do_object" on each.
// This includes objects in permanent memory.
// Behaves the same as oop_iterate, except only traverses
// interior pointers contained in permanent memory. If there
// is no permanent memory, does nothing.
// Behaves the same as object_iterate, except only traverses
// object contained in permanent memory. If there is no
// permanent memory, does nothing.
// NOTE! There is no requirement that a collector implement these
// functions.
//
// A CollectedHeap is divided into a dense sequence of "blocks"; that is,
// each address in the (reserved) heap is a member of exactly
// one block. The defining characteristic of a block is that it is
// possible to find its size, and thus to progress forward to the next
// block. (Blocks may be of different sizes.) Thus, blocks may
// represent Java objects, or they might be free blocks in a
// free-list-based heap (or subheap), as long as the two kinds are
// distinguishable and the size of each is determinable.
// Returns the address of the start of the "block" that contains the
// address "addr". We say "blocks" instead of "object" since some heaps
// may not pack objects densely; a chunk may either be an object or a
// non-object.
// Requires "addr" to be the start of a chunk, and returns its size.
// "addr + size" is required to be the start of a new chunk, or the end
// of the active area of the heap.
// Requires "addr" to be the start of a block, and returns "TRUE" iff
// the block is an object.
// Returns the longest time (in ms) that has elapsed since the last
// time that any part of the heap was examined by a garbage collection.
virtual jlong millis_since_last_gc() = 0;
// Perform any cleanup actions necessary before allowing a verification.
virtual void prepare_for_verify() = 0;
virtual void print() const = 0;
// Print all GC threads (other than the VM thread)
// used by this heap.
// Iterator for all GC threads (other than VM thread)
// Print any relevant tracing info that flags imply.
// Default implementation does nothing.
virtual void print_tracing_info() const = 0;
// Heap verification
// Non product verification and debugging.
#ifndef PRODUCT
// Support for PromotionFailureALot. Return true if it's time to cause a
// promotion failure. The no-argument version uses
// this->_promotion_failure_alot_count as the counter.
inline bool promotion_should_fail();
// Reset the PromotionFailureALot counters. Should be called at the end of a
// GC in which promotion failure ocurred.
inline void reset_promotion_should_fail();
#endif // #ifndef PRODUCT
#ifdef ASSERT
static int fired_fake_oom() {
}
#endif
};
// Class to set and reset the GC cause for a CollectedHeap.
class GCCauseSetter : StackObj {
public:
"This method manipulates heap state without locking");
}
~GCCauseSetter() {
"This method manipulates heap state without locking");
}
};