collectedHeap.cpp revision 1879
893N/A/*
3909N/A * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
2362N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
893N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
893N/A * or visit www.oracle.com if you need additional information or have any
893N/A * questions.
893N/A *
893N/A */
893N/A
893N/A#include "precompiled.hpp"
893N/A#include "classfile/systemDictionary.hpp"
893N/A#include "gc_implementation/shared/vmGCOperations.hpp"
893N/A#include "gc_interface/collectedHeap.hpp"
893N/A#include "gc_interface/collectedHeap.inline.hpp"
893N/A#include "oops/oop.inline.hpp"
893N/A#include "runtime/init.hpp"
893N/A#include "services/heapDumper.hpp"
893N/A#ifdef TARGET_OS_FAMILY_linux
893N/A# include "thread_linux.inline.hpp"
893N/A#endif
893N/A#ifdef TARGET_OS_FAMILY_solaris
893N/A# include "thread_solaris.inline.hpp"
893N/A#endif
893N/A#ifdef TARGET_OS_FAMILY_windows
893N/A# include "thread_windows.inline.hpp"
893N/A#endif
893N/A
893N/A
893N/A#ifdef ASSERT
893N/Aint CollectedHeap::_fire_out_of_memory_count = 0;
893N/A#endif
893N/A
893N/Asize_t CollectedHeap::_filler_array_max_size = 0;
893N/A
893N/A// Memory state functions.
893N/A
893N/A
893N/ACollectedHeap::CollectedHeap() : _n_par_threads(0)
893N/A
893N/A{
893N/A const size_t max_len = size_t(arrayOopDesc::max_array_length(T_INT));
893N/A const size_t elements_per_word = HeapWordSize / sizeof(jint);
893N/A _filler_array_max_size = align_object_size(filler_array_hdr_size() +
893N/A max_len * elements_per_word);
893N/A
893N/A _barrier_set = NULL;
893N/A _is_gc_active = false;
893N/A _total_collections = _total_full_collections = 0;
893N/A _gc_cause = _gc_lastcause = GCCause::_no_gc;
893N/A NOT_PRODUCT(_promotion_failure_alot_count = 0;)
893N/A NOT_PRODUCT(_promotion_failure_alot_gc_number = 0;)
893N/A
893N/A if (UsePerfData) {
893N/A EXCEPTION_MARK;
893N/A
893N/A // create the gc cause jvmstat counters
893N/A _perf_gc_cause = PerfDataManager::create_string_variable(SUN_GC, "cause",
893N/A 80, GCCause::to_string(_gc_cause), CHECK);
893N/A
893N/A _perf_gc_lastcause =
893N/A PerfDataManager::create_string_variable(SUN_GC, "lastCause",
893N/A 80, GCCause::to_string(_gc_lastcause), CHECK);
893N/A }
893N/A _defer_initial_card_mark = false; // strengthened by subclass in pre_initialize() below.
893N/A}
893N/A
893N/Avoid CollectedHeap::pre_initialize() {
893N/A // Used for ReduceInitialCardMarks (when COMPILER2 is used);
893N/A // otherwise remains unused.
893N/A#ifdef COMPILER2
893N/A _defer_initial_card_mark = ReduceInitialCardMarks && can_elide_tlab_store_barriers()
893N/A && (DeferInitialCardMark || card_mark_must_follow_store());
893N/A#else
893N/A assert(_defer_initial_card_mark == false, "Who would set it?");
893N/A#endif
893N/A}
893N/A
893N/A#ifndef PRODUCT
893N/Avoid CollectedHeap::check_for_bad_heap_word_value(HeapWord* addr, size_t size) {
893N/A if (CheckMemoryInitialization && ZapUnusedHeapArea) {
893N/A for (size_t slot = 0; slot < size; slot += 1) {
893N/A assert((*(intptr_t*) (addr + slot)) != ((intptr_t) badHeapWordVal),
893N/A "Found badHeapWordValue in post-allocation check");
893N/A }
893N/A }
893N/A}
893N/A
893N/Avoid CollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr, size_t size)
893N/A {
893N/A if (CheckMemoryInitialization && ZapUnusedHeapArea) {
893N/A for (size_t slot = 0; slot < size; slot += 1) {
893N/A assert((*(intptr_t*) (addr + slot)) == ((intptr_t) badHeapWordVal),
893N/A "Found non badHeapWordValue in pre-allocation check");
893N/A }
893N/A }
893N/A}
893N/A#endif // PRODUCT
893N/A
893N/A#ifdef ASSERT
893N/Avoid CollectedHeap::check_for_valid_allocation_state() {
893N/A Thread *thread = Thread::current();
893N/A // How to choose between a pending exception and a potential
893N/A // OutOfMemoryError? Don't allow pending exceptions.
893N/A // This is a VM policy failure, so how do we exhaustively test it?
893N/A assert(!thread->has_pending_exception(),
893N/A "shouldn't be allocating with pending exception");
893N/A if (StrictSafepointChecks) {
893N/A assert(thread->allow_allocation(),
893N/A "Allocation done by thread for which allocation is blocked "
893N/A "by No_Allocation_Verifier!");
893N/A // Allocation of an oop can always invoke a safepoint,
893N/A // hence, the true argument
893N/A thread->check_for_valid_safepoint_state(true);
893N/A }
893N/A}
893N/A#endif
893N/A
893N/AHeapWord* CollectedHeap::allocate_from_tlab_slow(Thread* thread, size_t size) {
893N/A
893N/A // Retain tlab and allocate object in shared space if
893N/A // the amount free in the tlab is too large to discard.
893N/A if (thread->tlab().free() > thread->tlab().refill_waste_limit()) {
893N/A thread->tlab().record_slow_allocation(size);
893N/A return NULL;
893N/A }
893N/A
893N/A // Discard tlab and allocate a new one.
893N/A // To minimize fragmentation, the last TLAB may be smaller than the rest.
893N/A size_t new_tlab_size = thread->tlab().compute_size(size);
893N/A
893N/A thread->tlab().clear_before_allocation();
893N/A
893N/A if (new_tlab_size == 0) {
893N/A return NULL;
893N/A }
893N/A
893N/A // Allocate a new TLAB...
893N/A HeapWord* obj = Universe::heap()->allocate_new_tlab(new_tlab_size);
893N/A if (obj == NULL) {
893N/A return NULL;
893N/A }
893N/A if (ZeroTLAB) {
893N/A // ..and clear it.
893N/A Copy::zero_to_words(obj, new_tlab_size);
893N/A } else {
893N/A // ...and clear just the allocated object.
893N/A Copy::zero_to_words(obj, size);
893N/A }
893N/A thread->tlab().fill(obj, obj + size, new_tlab_size);
893N/A return obj;
893N/A}
893N/A
893N/Avoid CollectedHeap::flush_deferred_store_barrier(JavaThread* thread) {
893N/A MemRegion deferred = thread->deferred_card_mark();
893N/A if (!deferred.is_empty()) {
893N/A assert(_defer_initial_card_mark, "Otherwise should be empty");
893N/A {
893N/A // Verify that the storage points to a parsable object in heap
893N/A DEBUG_ONLY(oop old_obj = oop(deferred.start());)
893N/A assert(is_in(old_obj), "Not in allocated heap");
893N/A assert(!can_elide_initializing_store_barrier(old_obj),
893N/A "Else should have been filtered in new_store_pre_barrier()");
893N/A assert(!is_in_permanent(old_obj), "Sanity: not expected");
893N/A assert(old_obj->is_oop(true), "Not an oop");
893N/A assert(old_obj->is_parsable(), "Will not be concurrently parsable");
893N/A assert(deferred.word_size() == (size_t)(old_obj->size()),
893N/A "Mismatch: multiple objects?");
893N/A }
893N/A BarrierSet* bs = barrier_set();
893N/A assert(bs->has_write_region_opt(), "No write_region() on BarrierSet");
893N/A bs->write_region(deferred);
893N/A // "Clear" the deferred_card_mark field
893N/A thread->set_deferred_card_mark(MemRegion());
893N/A }
893N/A assert(thread->deferred_card_mark().is_empty(), "invariant");
893N/A}
893N/A
893N/A// Helper for ReduceInitialCardMarks. For performance,
893N/A// compiled code may elide card-marks for initializing stores
893N/A// to a newly allocated object along the fast-path. We
893N/A// compensate for such elided card-marks as follows:
893N/A// (a) Generational, non-concurrent collectors, such as
893N/A// GenCollectedHeap(ParNew,DefNew,Tenured) and
893N/A// ParallelScavengeHeap(ParallelGC, ParallelOldGC)
893N/A// need the card-mark if and only if the region is
893N/A// in the old gen, and do not care if the card-mark
893N/A// succeeds or precedes the initializing stores themselves,
893N/A// so long as the card-mark is completed before the next
893N/A// scavenge. For all these cases, we can do a card mark
893N/A// at the point at which we do a slow path allocation
893N/A// in the old gen, i.e. in this call.
893N/A// (b) GenCollectedHeap(ConcurrentMarkSweepGeneration) requires
893N/A// in addition that the card-mark for an old gen allocated
893N/A// object strictly follow any associated initializing stores.
893N/A// In these cases, the memRegion remembered below is
893N/A// used to card-mark the entire region either just before the next
893N/A// slow-path allocation by this thread or just before the next scavenge or
893N/A// CMS-associated safepoint, whichever of these events happens first.
893N/A// (The implicit assumption is that the object has been fully
893N/A// initialized by this point, a fact that we assert when doing the
893N/A// card-mark.)
893N/A// (c) G1CollectedHeap(G1) uses two kinds of write barriers. When a
893N/A// G1 concurrent marking is in progress an SATB (pre-write-)barrier is
893N/A// is used to remember the pre-value of any store. Initializing
893N/A// stores will not need this barrier, so we need not worry about
893N/A// compensating for the missing pre-barrier here. Turning now
893N/A// to the post-barrier, we note that G1 needs a RS update barrier
893N/A// which simply enqueues a (sequence of) dirty cards which may
893N/A// optionally be refined by the concurrent update threads. Note
893N/A// that this barrier need only be applied to a non-young write,
893N/A// but, like in CMS, because of the presence of concurrent refinement
893N/A// (much like CMS' precleaning), must strictly follow the oop-store.
893N/A// Thus, using the same protocol for maintaining the intended
893N/A// invariants turns out, serendepitously, to be the same for both
893N/A// G1 and CMS.
893N/A//
893N/A// For any future collector, this code should be reexamined with
893N/A// that specific collector in mind, and the documentation above suitably
893N/A// extended and updated.
893N/Aoop CollectedHeap::new_store_pre_barrier(JavaThread* thread, oop new_obj) {
893N/A // If a previous card-mark was deferred, flush it now.
893N/A flush_deferred_store_barrier(thread);
893N/A if (can_elide_initializing_store_barrier(new_obj)) {
893N/A // The deferred_card_mark region should be empty
893N/A // following the flush above.
893N/A assert(thread->deferred_card_mark().is_empty(), "Error");
3471N/A } else {
893N/A MemRegion mr((HeapWord*)new_obj, new_obj->size());
893N/A assert(!mr.is_empty(), "Error");
893N/A if (_defer_initial_card_mark) {
893N/A // Defer the card mark
893N/A thread->set_deferred_card_mark(mr);
893N/A } else {
893N/A // Do the card mark
893N/A BarrierSet* bs = barrier_set();
893N/A assert(bs->has_write_region_opt(), "No write_region() on BarrierSet");
893N/A bs->write_region(mr);
893N/A }
893N/A }
893N/A return new_obj;
893N/A}
893N/A
893N/Asize_t CollectedHeap::filler_array_hdr_size() {
893N/A return size_t(align_object_offset(arrayOopDesc::header_size(T_INT))); // align to Long
893N/A}
893N/A
893N/Asize_t CollectedHeap::filler_array_min_size() {
893N/A return align_object_size(filler_array_hdr_size()); // align to MinObjAlignment
893N/A}
893N/A
893N/Asize_t CollectedHeap::filler_array_max_size() {
893N/A return _filler_array_max_size;
893N/A}
893N/A
893N/A#ifdef ASSERT
893N/Avoid CollectedHeap::fill_args_check(HeapWord* start, size_t words)
893N/A{
893N/A assert(words >= min_fill_size(), "too small to fill");
893N/A assert(words % MinObjAlignment == 0, "unaligned size");
893N/A assert(Universe::heap()->is_in_reserved(start), "not in heap");
893N/A assert(Universe::heap()->is_in_reserved(start + words - 1), "not in heap");
893N/A}
893N/A
893N/Avoid CollectedHeap::zap_filler_array(HeapWord* start, size_t words, bool zap)
893N/A{
893N/A if (ZapFillerObjects && zap) {
893N/A Copy::fill_to_words(start + filler_array_hdr_size(),
893N/A words - filler_array_hdr_size(), 0XDEAFBABE);
893N/A }
893N/A}
893N/A#endif // ASSERT
893N/A
893N/Avoid
893N/ACollectedHeap::fill_with_array(HeapWord* start, size_t words, bool zap)
893N/A{
893N/A assert(words >= filler_array_min_size(), "too small for an array");
893N/A assert(words <= filler_array_max_size(), "too big for a single object");
893N/A
893N/A const size_t payload_size = words - filler_array_hdr_size();
893N/A const size_t len = payload_size * HeapWordSize / sizeof(jint);
893N/A
// Set the length first for concurrent GC.
((arrayOop)start)->set_length((int)len);
post_allocation_setup_common(Universe::intArrayKlassObj(), start, words);
DEBUG_ONLY(zap_filler_array(start, words, zap);)
}
void
CollectedHeap::fill_with_object_impl(HeapWord* start, size_t words, bool zap)
{
assert(words <= filler_array_max_size(), "too big for a single object");
if (words >= filler_array_min_size()) {
fill_with_array(start, words, zap);
} else if (words > 0) {
assert(words == min_fill_size(), "unaligned size");
post_allocation_setup_common(SystemDictionary::Object_klass(), start,
words);
}
}
void CollectedHeap::fill_with_object(HeapWord* start, size_t words, bool zap)
{
DEBUG_ONLY(fill_args_check(start, words);)
HandleMark hm; // Free handles before leaving.
fill_with_object_impl(start, words, zap);
}
void CollectedHeap::fill_with_objects(HeapWord* start, size_t words, bool zap)
{
DEBUG_ONLY(fill_args_check(start, words);)
HandleMark hm; // Free handles before leaving.
#ifdef _LP64
// A single array can fill ~8G, so multiple objects are needed only in 64-bit.
// First fill with arrays, ensuring that any remaining space is big enough to
// fill. The remainder is filled with a single object.
const size_t min = min_fill_size();
const size_t max = filler_array_max_size();
while (words > max) {
const size_t cur = words - max >= min ? max : max - min;
fill_with_array(start, cur, zap);
start += cur;
words -= cur;
}
#endif
fill_with_object_impl(start, words, zap);
}
HeapWord* CollectedHeap::allocate_new_tlab(size_t size) {
guarantee(false, "thread-local allocation buffers not supported");
return NULL;
}
void CollectedHeap::ensure_parsability(bool retire_tlabs) {
// The second disjunct in the assertion below makes a concession
// for the start-up verification done while the VM is being
// created. Callers be careful that you know that mutators
// aren't going to interfere -- for instance, this is permissible
// if we are still single-threaded and have either not yet
// started allocating (nothing much to verify) or we have
// started allocating but are now a full-fledged JavaThread
// (and have thus made our TLAB's) available for filling.
assert(SafepointSynchronize::is_at_safepoint() ||
!is_init_completed(),
"Should only be called at a safepoint or at start-up"
" otherwise concurrent mutator activity may make heap "
" unparsable again");
const bool use_tlab = UseTLAB;
const bool deferred = _defer_initial_card_mark;
// The main thread starts allocating via a TLAB even before it
// has added itself to the threads list at vm boot-up.
assert(!use_tlab || Threads::first() != NULL,
"Attempt to fill tlabs before main thread has been added"
" to threads list is doomed to failure!");
for (JavaThread *thread = Threads::first(); thread; thread = thread->next()) {
if (use_tlab) thread->tlab().make_parsable(retire_tlabs);
#ifdef COMPILER2
// The deferred store barriers must all have been flushed to the
// card-table (or other remembered set structure) before GC starts
// processing the card-table (or other remembered set).
if (deferred) flush_deferred_store_barrier(thread);
#else
assert(!deferred, "Should be false");
assert(thread->deferred_card_mark().is_empty(), "Should be empty");
#endif
}
}
void CollectedHeap::accumulate_statistics_all_tlabs() {
if (UseTLAB) {
assert(SafepointSynchronize::is_at_safepoint() ||
!is_init_completed(),
"should only accumulate statistics on tlabs at safepoint");
ThreadLocalAllocBuffer::accumulate_statistics_before_gc();
}
}
void CollectedHeap::resize_all_tlabs() {
if (UseTLAB) {
assert(SafepointSynchronize::is_at_safepoint() ||
!is_init_completed(),
"should only resize tlabs at safepoint");
ThreadLocalAllocBuffer::resize_all_tlabs();
}
}
void CollectedHeap::pre_full_gc_dump() {
if (HeapDumpBeforeFullGC) {
TraceTime tt("Heap Dump: ", PrintGCDetails, false, gclog_or_tty);
// We are doing a "major" collection and a heap dump before
// major collection has been requested.
HeapDumper::dump_heap();
}
if (PrintClassHistogramBeforeFullGC) {
TraceTime tt("Class Histogram: ", PrintGCDetails, true, gclog_or_tty);
VM_GC_HeapInspection inspector(gclog_or_tty, false /* ! full gc */, false /* ! prologue */);
inspector.doit();
}
}
void CollectedHeap::post_full_gc_dump() {
if (HeapDumpAfterFullGC) {
TraceTime tt("Heap Dump", PrintGCDetails, false, gclog_or_tty);
HeapDumper::dump_heap();
}
if (PrintClassHistogramAfterFullGC) {
TraceTime tt("Class Histogram", PrintGCDetails, true, gclog_or_tty);
VM_GC_HeapInspection inspector(gclog_or_tty, false /* ! full gc */, false /* ! prologue */);
inspector.doit();
}
}