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