duke@435: /* stefank@2314: * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" jmasa@3730: #include "memory/freeBlockDictionary.hpp" jmasa@3730: #include "memory/freeList.hpp" stefank@2314: #include "memory/sharedHeap.hpp" stefank@2314: #include "runtime/globals.hpp" stefank@2314: #include "runtime/mutex.hpp" stefank@2314: #include "runtime/vmThread.hpp" duke@435: jmasa@3730: #ifndef SERIALGC jmasa@3730: #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp" jmasa@3730: #endif // SERIALGC jmasa@3730: duke@435: // Free list. A FreeList is used to access a linked list of chunks duke@435: // of space in the heap. The head and tail are maintained so that duke@435: // items can be (as in the current implementation) added at the duke@435: // at the tail of the list and removed from the head of the list to duke@435: // maintain a FIFO queue. duke@435: jmasa@3730: template jmasa@3730: FreeList::FreeList() : duke@435: _head(NULL), _tail(NULL) duke@435: #ifdef ASSERT duke@435: , _protecting_lock(NULL) duke@435: #endif duke@435: { duke@435: _size = 0; duke@435: _count = 0; duke@435: _hint = 0; duke@435: init_statistics(); duke@435: } duke@435: jmasa@3730: template jmasa@3730: FreeList::FreeList(Chunk* fc) : duke@435: _head(fc), _tail(fc) duke@435: #ifdef ASSERT duke@435: , _protecting_lock(NULL) duke@435: #endif duke@435: { duke@435: _size = fc->size(); duke@435: _count = 1; duke@435: _hint = 0; duke@435: init_statistics(); duke@435: #ifndef PRODUCT jmasa@3732: _allocation_stats.set_returned_bytes(size() * HeapWordSize); duke@435: #endif duke@435: } duke@435: jmasa@3730: template jmasa@3730: void FreeList::reset(size_t hint) { duke@435: set_count(0); duke@435: set_head(NULL); duke@435: set_tail(NULL); duke@435: set_hint(hint); duke@435: } duke@435: jmasa@3730: template jmasa@3730: void FreeList::init_statistics(bool split_birth) { ysr@1580: _allocation_stats.initialize(split_birth); duke@435: } duke@435: jmasa@3730: template jmasa@3732: Chunk* FreeList::get_chunk_at_head() { duke@435: assert_proper_lock_protection(); duke@435: assert(head() == NULL || head()->prev() == NULL, "list invariant"); duke@435: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); jmasa@3730: Chunk* fc = head(); duke@435: if (fc != NULL) { jmasa@3730: Chunk* nextFC = fc->next(); duke@435: if (nextFC != NULL) { duke@435: // The chunk fc being removed has a "next". Set the "next" to the duke@435: // "prev" of fc. jmasa@3732: nextFC->link_prev(NULL); duke@435: } else { // removed tail of list duke@435: link_tail(NULL); duke@435: } duke@435: link_head(nextFC); duke@435: decrement_count(); duke@435: } duke@435: assert(head() == NULL || head()->prev() == NULL, "list invariant"); duke@435: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); duke@435: return fc; duke@435: } duke@435: duke@435: jmasa@3730: template jmasa@3730: void FreeList::getFirstNChunksFromList(size_t n, FreeList* fl) { duke@435: assert_proper_lock_protection(); duke@435: assert(fl->count() == 0, "Precondition"); duke@435: if (count() > 0) { duke@435: int k = 1; duke@435: fl->set_head(head()); n--; jmasa@3730: Chunk* tl = head(); duke@435: while (tl->next() != NULL && n > 0) { duke@435: tl = tl->next(); n--; k++; duke@435: } duke@435: assert(tl != NULL, "Loop Inv."); duke@435: duke@435: // First, fix up the list we took from. jmasa@3730: Chunk* new_head = tl->next(); duke@435: set_head(new_head); duke@435: set_count(count() - k); duke@435: if (new_head == NULL) { duke@435: set_tail(NULL); duke@435: } else { jmasa@3732: new_head->link_prev(NULL); duke@435: } duke@435: // Now we can fix up the tail. jmasa@3732: tl->link_next(NULL); duke@435: // And return the result. duke@435: fl->set_tail(tl); duke@435: fl->set_count(k); duke@435: } duke@435: } duke@435: duke@435: // Remove this chunk from the list jmasa@3730: template jmasa@3732: void FreeList::remove_chunk(Chunk*fc) { duke@435: assert_proper_lock_protection(); duke@435: assert(head() != NULL, "Remove from empty list"); duke@435: assert(fc != NULL, "Remove a NULL chunk"); duke@435: assert(size() == fc->size(), "Wrong list"); duke@435: assert(head() == NULL || head()->prev() == NULL, "list invariant"); duke@435: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); duke@435: jmasa@3730: Chunk* prevFC = fc->prev(); jmasa@3730: Chunk* nextFC = fc->next(); duke@435: if (nextFC != NULL) { duke@435: // The chunk fc being removed has a "next". Set the "next" to the duke@435: // "prev" of fc. jmasa@3732: nextFC->link_prev(prevFC); duke@435: } else { // removed tail of list duke@435: link_tail(prevFC); duke@435: } duke@435: if (prevFC == NULL) { // removed head of list duke@435: link_head(nextFC); duke@435: assert(nextFC == NULL || nextFC->prev() == NULL, duke@435: "Prev of head should be NULL"); duke@435: } else { jmasa@3732: prevFC->link_next(nextFC); duke@435: assert(tail() != prevFC || prevFC->next() == NULL, duke@435: "Next of tail should be NULL"); duke@435: } duke@435: decrement_count(); ysr@2132: assert(((head() == NULL) + (tail() == NULL) + (count() == 0)) % 3 == 0, ysr@2132: "H/T/C Inconsistency"); duke@435: // clear next and prev fields of fc, debug only duke@435: NOT_PRODUCT( jmasa@3732: fc->link_prev(NULL); jmasa@3732: fc->link_next(NULL); duke@435: ) jmasa@3732: assert(fc->is_free(), "Should still be a free chunk"); duke@435: assert(head() == NULL || head()->prev() == NULL, "list invariant"); duke@435: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); duke@435: assert(head() == NULL || head()->size() == size(), "wrong item on list"); duke@435: assert(tail() == NULL || tail()->size() == size(), "wrong item on list"); duke@435: } duke@435: duke@435: // Add this chunk at the head of the list. jmasa@3730: template jmasa@3732: void FreeList::return_chunk_at_head(Chunk* chunk, bool record_return) { duke@435: assert_proper_lock_protection(); duke@435: assert(chunk != NULL, "insert a NULL chunk"); duke@435: assert(size() == chunk->size(), "Wrong size"); duke@435: assert(head() == NULL || head()->prev() == NULL, "list invariant"); duke@435: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); duke@435: jmasa@3730: Chunk* oldHead = head(); duke@435: assert(chunk != oldHead, "double insertion"); jmasa@3732: chunk->link_after(oldHead); duke@435: link_head(chunk); duke@435: if (oldHead == NULL) { // only chunk in list duke@435: assert(tail() == NULL, "inconsistent FreeList"); duke@435: link_tail(chunk); duke@435: } duke@435: increment_count(); // of # of chunks in list duke@435: DEBUG_ONLY( duke@435: if (record_return) { jmasa@3732: increment_returned_bytes_by(size()*HeapWordSize); duke@435: } duke@435: ) duke@435: assert(head() == NULL || head()->prev() == NULL, "list invariant"); duke@435: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); duke@435: assert(head() == NULL || head()->size() == size(), "wrong item on list"); duke@435: assert(tail() == NULL || tail()->size() == size(), "wrong item on list"); duke@435: } duke@435: jmasa@3730: template jmasa@3732: void FreeList::return_chunk_at_head(Chunk* chunk) { duke@435: assert_proper_lock_protection(); jmasa@3732: return_chunk_at_head(chunk, true); duke@435: } duke@435: duke@435: // Add this chunk at the tail of the list. jmasa@3730: template jmasa@3732: void FreeList::return_chunk_at_tail(Chunk* chunk, bool record_return) { duke@435: assert_proper_lock_protection(); duke@435: assert(head() == NULL || head()->prev() == NULL, "list invariant"); duke@435: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); duke@435: assert(chunk != NULL, "insert a NULL chunk"); duke@435: assert(size() == chunk->size(), "wrong size"); duke@435: jmasa@3730: Chunk* oldTail = tail(); duke@435: assert(chunk != oldTail, "double insertion"); duke@435: if (oldTail != NULL) { jmasa@3732: oldTail->link_after(chunk); duke@435: } else { // only chunk in list duke@435: assert(head() == NULL, "inconsistent FreeList"); duke@435: link_head(chunk); duke@435: } duke@435: link_tail(chunk); duke@435: increment_count(); // of # of chunks in list duke@435: DEBUG_ONLY( duke@435: if (record_return) { jmasa@3732: increment_returned_bytes_by(size()*HeapWordSize); duke@435: } duke@435: ) duke@435: assert(head() == NULL || head()->prev() == NULL, "list invariant"); duke@435: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); duke@435: assert(head() == NULL || head()->size() == size(), "wrong item on list"); duke@435: assert(tail() == NULL || tail()->size() == size(), "wrong item on list"); duke@435: } duke@435: jmasa@3730: template jmasa@3732: void FreeList::return_chunk_at_tail(Chunk* chunk) { jmasa@3732: return_chunk_at_tail(chunk, true); duke@435: } duke@435: jmasa@3730: template jmasa@3730: void FreeList::prepend(FreeList* fl) { duke@435: assert_proper_lock_protection(); duke@435: if (fl->count() > 0) { duke@435: if (count() == 0) { duke@435: set_head(fl->head()); duke@435: set_tail(fl->tail()); duke@435: set_count(fl->count()); duke@435: } else { duke@435: // Both are non-empty. jmasa@3730: Chunk* fl_tail = fl->tail(); jmasa@3730: Chunk* this_head = head(); duke@435: assert(fl_tail->next() == NULL, "Well-formedness of fl"); jmasa@3732: fl_tail->link_next(this_head); jmasa@3732: this_head->link_prev(fl_tail); duke@435: set_head(fl->head()); duke@435: set_count(count() + fl->count()); duke@435: } duke@435: fl->set_head(NULL); duke@435: fl->set_tail(NULL); duke@435: fl->set_count(0); duke@435: } duke@435: } duke@435: jmasa@3732: // verify_chunk_in_free_list() is used to verify that an item is in this free list. duke@435: // It is used as a debugging aid. jmasa@3730: template jmasa@3732: bool FreeList::verify_chunk_in_free_list(Chunk* fc) const { duke@435: // This is an internal consistency check, not part of the check that the duke@435: // chunk is in the free lists. duke@435: guarantee(fc->size() == size(), "Wrong list is being searched"); jmasa@3730: Chunk* curFC = head(); duke@435: while (curFC) { duke@435: // This is an internal consistency check. duke@435: guarantee(size() == curFC->size(), "Chunk is in wrong list."); duke@435: if (fc == curFC) { duke@435: return true; duke@435: } duke@435: curFC = curFC->next(); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: #ifndef PRODUCT jmasa@3730: template jmasa@3730: void FreeList::verify_stats() const { ysr@1580: // The +1 of the LH comparand is to allow some "looseness" in ysr@1580: // checking: we usually call this interface when adding a block ysr@1580: // and we'll subsequently update the stats; we cannot update the ysr@1580: // stats beforehand because in the case of the large-block BT ysr@1580: // dictionary for example, this might be the first block and ysr@1580: // in that case there would be no place that we could record ysr@1580: // the stats (which are kept in the block itself). jmasa@3732: assert((_allocation_stats.prev_sweep() + _allocation_stats.split_births() jmasa@3732: + _allocation_stats.coal_births() + 1) // Total Production Stock + 1 jmasa@3732: >= (_allocation_stats.split_deaths() + _allocation_stats.coal_deaths() ysr@2972: + (ssize_t)count()), // Total Current Stock + depletion ysr@2972: err_msg("FreeList " PTR_FORMAT " of size " SIZE_FORMAT ysr@2972: " violates Conservation Principle: " jmasa@3732: "prev_sweep(" SIZE_FORMAT ")" jmasa@3732: " + split_births(" SIZE_FORMAT ")" jmasa@3732: " + coal_births(" SIZE_FORMAT ") + 1 >= " jmasa@3732: " split_deaths(" SIZE_FORMAT ")" jmasa@3732: " coal_deaths(" SIZE_FORMAT ")" ysr@2972: " + count(" SSIZE_FORMAT ")", jmasa@3732: this, _size, _allocation_stats.prev_sweep(), _allocation_stats.split_births(), jmasa@3732: _allocation_stats.split_births(), _allocation_stats.split_deaths(), jmasa@3732: _allocation_stats.coal_deaths(), count())); ysr@1580: } ysr@1580: jmasa@3730: template jmasa@3730: void FreeList::assert_proper_lock_protection_work() const { ysr@1580: assert(_protecting_lock != NULL, "Don't call this directly"); ysr@1580: assert(ParallelGCThreads > 0, "Don't call this directly"); ysr@1580: Thread* thr = Thread::current(); ysr@1580: if (thr->is_VM_thread() || thr->is_ConcurrentGC_thread()) { ysr@1580: // assert that we are holding the freelist lock ysr@1580: } else if (thr->is_GC_task_thread()) { ysr@1580: assert(_protecting_lock->owned_by_self(), "FreeList RACE DETECTED"); ysr@1580: } else if (thr->is_Java_thread()) { ysr@1580: assert(!SafepointSynchronize::is_at_safepoint(), "Should not be executing"); ysr@1580: } else { ysr@1580: ShouldNotReachHere(); // unaccounted thread type? duke@435: } duke@435: } duke@435: #endif ysr@447: ysr@447: // Print the "label line" for free list stats. jmasa@3730: template jmasa@3730: void FreeList::print_labels_on(outputStream* st, const char* c) { ysr@447: st->print("%16s\t", c); ysr@447: st->print("%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t" ysr@447: "%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t" "\n", ysr@447: "bfrsurp", "surplus", "desired", "prvSwep", "bfrSwep", ysr@447: "count", "cBirths", "cDeaths", "sBirths", "sDeaths"); ysr@447: } ysr@447: ysr@447: // Print the AllocationStats for the given free list. If the second argument ysr@447: // to the call is a non-null string, it is printed in the first column; ysr@447: // otherwise, if the argument is null (the default), then the size of the ysr@447: // (free list) block is printed in the first column. jmasa@3730: template jmasa@3730: void FreeList::print_on(outputStream* st, const char* c) const { ysr@447: if (c != NULL) { ysr@447: st->print("%16s", c); ysr@447: } else { ysr@447: st->print(SIZE_FORMAT_W(16), size()); ysr@447: } ysr@447: st->print("\t" ysr@447: 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" ysr@447: 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@3732: bfr_surp(), surplus(), desired(), prev_sweep(), before_sweep(), jmasa@3732: count(), coal_births(), coal_deaths(), split_births(), split_deaths()); ysr@447: } jmasa@3730: jmasa@3730: #ifndef SERIALGC jmasa@3730: // Needs to be after the definitions have been seen. jmasa@3730: template class FreeList; jmasa@3730: #endif // SERIALGC