jmasa@4196: /* jmasa@4196: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. jmasa@4196: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jmasa@4196: * jmasa@4196: * This code is free software; you can redistribute it and/or modify it jmasa@4196: * under the terms of the GNU General Public License version 2 only, as jmasa@4196: * published by the Free Software Foundation. jmasa@4196: * jmasa@4196: * This code is distributed in the hope that it will be useful, but WITHOUT jmasa@4196: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jmasa@4196: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jmasa@4196: * version 2 for more details (a copy is included in the LICENSE file that jmasa@4196: * accompanied this code). jmasa@4196: * jmasa@4196: * You should have received a copy of the GNU General Public License version jmasa@4196: * 2 along with this work; if not, write to the Free Software Foundation, jmasa@4196: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jmasa@4196: * jmasa@4196: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jmasa@4196: * or visit www.oracle.com if you need additional information or have any jmasa@4196: * questions. jmasa@4196: * jmasa@4196: */ jmasa@4196: jmasa@4196: #include "precompiled.hpp" jmasa@4196: #include "gc_implementation/concurrentMarkSweep/adaptiveFreeList.hpp" jmasa@4196: #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp" jmasa@4196: #include "memory/freeBlockDictionary.hpp" jmasa@4196: #include "memory/sharedHeap.hpp" jmasa@4196: #include "runtime/globals.hpp" jmasa@4196: #include "runtime/mutex.hpp" jmasa@4196: #include "runtime/vmThread.hpp" jmasa@4196: jmasa@4196: template <> jmasa@4196: void AdaptiveFreeList::print_on(outputStream* st, const char* c) const { jmasa@4196: if (c != NULL) { jmasa@4196: st->print("%16s", c); jmasa@4196: } else { jmasa@4196: st->print(SIZE_FORMAT_W(16), size()); jmasa@4196: } jmasa@4196: st->print("\t" jmasa@4196: SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" jmasa@4196: SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\n", jmasa@4196: bfr_surp(), surplus(), desired(), prev_sweep(), before_sweep(), jmasa@4196: count(), coal_births(), coal_deaths(), split_births(), split_deaths()); jmasa@4196: } jmasa@4196: jmasa@4196: template jmasa@4196: AdaptiveFreeList::AdaptiveFreeList() : FreeList(), _hint(0) { jmasa@4196: init_statistics(); jmasa@4196: } jmasa@4196: jmasa@4196: template jmasa@4196: void AdaptiveFreeList::initialize() { jmasa@4196: FreeList::initialize(); jmasa@4196: set_hint(0); jmasa@4196: init_statistics(true /* split_birth */); jmasa@4196: } jmasa@4196: jmasa@4196: template jmasa@4196: void AdaptiveFreeList::reset(size_t hint) { jmasa@4196: FreeList::reset(); jmasa@4196: set_hint(hint); jmasa@4196: } jmasa@4196: jmasa@4196: #ifndef PRODUCT jmasa@4196: template jmasa@4196: void AdaptiveFreeList::assert_proper_lock_protection_work() const { jmasa@4196: assert(protecting_lock() != NULL, "Don't call this directly"); jmasa@4196: assert(ParallelGCThreads > 0, "Don't call this directly"); jmasa@4196: Thread* thr = Thread::current(); jmasa@4196: if (thr->is_VM_thread() || thr->is_ConcurrentGC_thread()) { jmasa@4196: // assert that we are holding the freelist lock jmasa@4196: } else if (thr->is_GC_task_thread()) { jmasa@4196: assert(protecting_lock()->owned_by_self(), "FreeList RACE DETECTED"); jmasa@4196: } else if (thr->is_Java_thread()) { jmasa@4196: assert(!SafepointSynchronize::is_at_safepoint(), "Should not be executing"); jmasa@4196: } else { jmasa@4196: ShouldNotReachHere(); // unaccounted thread type? jmasa@4196: } jmasa@4196: } jmasa@4196: #endif jmasa@4196: template jmasa@4196: void AdaptiveFreeList::init_statistics(bool split_birth) { jmasa@4196: _allocation_stats.initialize(split_birth); jmasa@4196: } jmasa@4196: jmasa@4196: template jmasa@4196: size_t AdaptiveFreeList::get_better_size() { jmasa@4196: jmasa@4196: // A candidate chunk has been found. If it is already under jmasa@4196: // populated and there is a hinT, REturn the hint(). Else jmasa@4196: // return the size of this chunk. jmasa@4196: if (surplus() <= 0) { jmasa@4196: if (hint() != 0) { jmasa@4196: return hint(); jmasa@4196: } else { jmasa@4196: return size(); jmasa@4196: } jmasa@4196: } else { jmasa@4196: // This list has a surplus so use it. jmasa@4196: return size(); jmasa@4196: } jmasa@4196: } jmasa@4196: jmasa@4196: jmasa@4196: template jmasa@4196: void AdaptiveFreeList::return_chunk_at_head(Chunk* chunk) { jmasa@4196: assert_proper_lock_protection(); jmasa@4196: return_chunk_at_head(chunk, true); jmasa@4196: } jmasa@4196: jmasa@4196: template jmasa@4196: void AdaptiveFreeList::return_chunk_at_head(Chunk* chunk, bool record_return) { jmasa@4196: FreeList::return_chunk_at_head(chunk, record_return); jmasa@4196: #ifdef ASSERT jmasa@4196: if (record_return) { jmasa@4196: increment_returned_bytes_by(size()*HeapWordSize); jmasa@4196: } jmasa@4196: #endif jmasa@4196: } jmasa@4196: jmasa@4196: template jmasa@4196: void AdaptiveFreeList::return_chunk_at_tail(Chunk* chunk) { jmasa@4196: return_chunk_at_tail(chunk, true); jmasa@4196: } jmasa@4196: jmasa@4196: template jmasa@4196: void AdaptiveFreeList::return_chunk_at_tail(Chunk* chunk, bool record_return) { jmasa@4196: FreeList::return_chunk_at_tail(chunk, record_return); jmasa@4196: #ifdef ASSERT jmasa@4196: if (record_return) { jmasa@4196: increment_returned_bytes_by(size()*HeapWordSize); jmasa@4196: } jmasa@4196: #endif jmasa@4196: } jmasa@4196: jmasa@4196: #ifndef PRODUCT jmasa@4196: template jmasa@4196: void AdaptiveFreeList::verify_stats() const { jmasa@4196: // The +1 of the LH comparand is to allow some "looseness" in jmasa@4196: // checking: we usually call this interface when adding a block jmasa@4196: // and we'll subsequently update the stats; we cannot update the jmasa@4196: // stats beforehand because in the case of the large-block BT jmasa@4196: // dictionary for example, this might be the first block and jmasa@4196: // in that case there would be no place that we could record jmasa@4196: // the stats (which are kept in the block itself). jmasa@4196: assert((_allocation_stats.prev_sweep() + _allocation_stats.split_births() jmasa@4196: + _allocation_stats.coal_births() + 1) // Total Production Stock + 1 jmasa@4196: >= (_allocation_stats.split_deaths() + _allocation_stats.coal_deaths() jmasa@4196: + (ssize_t)count()), // Total Current Stock + depletion jmasa@4196: err_msg("FreeList " PTR_FORMAT " of size " SIZE_FORMAT jmasa@4196: " violates Conservation Principle: " jmasa@4196: "prev_sweep(" SIZE_FORMAT ")" jmasa@4196: " + split_births(" SIZE_FORMAT ")" jmasa@4196: " + coal_births(" SIZE_FORMAT ") + 1 >= " jmasa@4196: " split_deaths(" SIZE_FORMAT ")" jmasa@4196: " coal_deaths(" SIZE_FORMAT ")" jmasa@4196: " + count(" SSIZE_FORMAT ")", jmasa@4196: this, size(), _allocation_stats.prev_sweep(), _allocation_stats.split_births(), jmasa@4196: _allocation_stats.split_births(), _allocation_stats.split_deaths(), jmasa@4196: _allocation_stats.coal_deaths(), count())); jmasa@4196: } jmasa@4196: #endif jmasa@4196: jmasa@4196: // Needs to be after the definitions have been seen. jmasa@4196: template class AdaptiveFreeList;