aoqi@0: /* aoqi@0: * Copyright (c) 2001, 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 "utilities/macros.hpp" aoqi@0: #include "gc_implementation/shared/allocationStats.hpp" aoqi@0: #include "memory/binaryTreeDictionary.hpp" aoqi@0: #include "memory/freeList.hpp" aoqi@0: #include "memory/freeBlockDictionary.hpp" aoqi@0: #include "memory/metachunk.hpp" aoqi@0: #include "runtime/globals.hpp" aoqi@0: #include "utilities/ostream.hpp" aoqi@0: #include "utilities/macros.hpp" aoqi@0: #include "gc_implementation/shared/spaceDecorator.hpp" aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: #include "gc_implementation/concurrentMarkSweep/adaptiveFreeList.hpp" aoqi@0: #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp" aoqi@0: #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp" aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////////////////// aoqi@0: // A binary tree based search structure for free blocks. aoqi@0: // This is currently used in the Concurrent Mark&Sweep implementation. aoqi@0: //////////////////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: template aoqi@0: size_t TreeChunk::_min_tree_chunk_size = sizeof(TreeChunk)/HeapWordSize; aoqi@0: aoqi@0: template aoqi@0: TreeChunk* TreeChunk::as_TreeChunk(Chunk_t* fc) { aoqi@0: // Do some assertion checking here. aoqi@0: return (TreeChunk*) fc; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: void TreeChunk::verify_tree_chunk_list() const { aoqi@0: TreeChunk* nextTC = (TreeChunk*)next(); aoqi@0: if (prev() != NULL) { // interior list node shouldn'r have tree fields aoqi@0: guarantee(embedded_list()->parent() == NULL && embedded_list()->left() == NULL && aoqi@0: embedded_list()->right() == NULL, "should be clear"); aoqi@0: } aoqi@0: if (nextTC != NULL) { aoqi@0: guarantee(as_TreeChunk(nextTC->prev()) == this, "broken chain"); aoqi@0: guarantee(nextTC->size() == size(), "wrong size"); aoqi@0: nextTC->verify_tree_chunk_list(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: TreeList::TreeList() : _parent(NULL), aoqi@0: _left(NULL), _right(NULL) {} aoqi@0: aoqi@0: template aoqi@0: TreeList* aoqi@0: TreeList::as_TreeList(TreeChunk* tc) { aoqi@0: // This first free chunk in the list will be the tree list. aoqi@0: assert((tc->size() >= (TreeChunk::min_size())), aoqi@0: "Chunk is too small for a TreeChunk"); aoqi@0: TreeList* tl = tc->embedded_list(); aoqi@0: tl->initialize(); aoqi@0: tc->set_list(tl); aoqi@0: tl->set_size(tc->size()); aoqi@0: tl->link_head(tc); aoqi@0: tl->link_tail(tc); aoqi@0: tl->set_count(1); aoqi@0: assert(tl->parent() == NULL, "Should be clear"); aoqi@0: return tl; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: TreeList* aoqi@0: TreeList::as_TreeList(HeapWord* addr, size_t size) { aoqi@0: TreeChunk* tc = (TreeChunk*) addr; aoqi@0: assert((size >= TreeChunk::min_size()), aoqi@0: "Chunk is too small for a TreeChunk"); aoqi@0: // The space will have been mangled initially but aoqi@0: // is not remangled when a Chunk_t is returned to the free list aoqi@0: // (since it is used to maintain the chunk on the free list). aoqi@0: tc->assert_is_mangled(); aoqi@0: tc->set_size(size); aoqi@0: tc->link_prev(NULL); aoqi@0: tc->link_next(NULL); aoqi@0: TreeList* tl = TreeList::as_TreeList(tc); aoqi@0: return tl; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: // Specialize for AdaptiveFreeList which tries to avoid aoqi@0: // splitting a chunk of a size that is under populated in favor of aoqi@0: // an over populated size. The general get_better_list() just returns aoqi@0: // the current list. aoqi@0: template <> aoqi@0: TreeList >* aoqi@0: TreeList >::get_better_list( aoqi@0: BinaryTreeDictionary >* dictionary) { aoqi@0: // A candidate chunk has been found. If it is already under aoqi@0: // populated, get a chunk associated with the hint for this aoqi@0: // chunk. aoqi@0: aoqi@0: TreeList >* curTL = this; aoqi@0: if (surplus() <= 0) { aoqi@0: /* Use the hint to find a size with a surplus, and reset the hint. */ aoqi@0: TreeList >* hintTL = this; aoqi@0: while (hintTL->hint() != 0) { aoqi@0: assert(hintTL->hint() > hintTL->size(), aoqi@0: "hint points in the wrong direction"); aoqi@0: hintTL = dictionary->find_list(hintTL->hint()); aoqi@0: assert(curTL != hintTL, "Infinite loop"); aoqi@0: if (hintTL == NULL || aoqi@0: hintTL == curTL /* Should not happen but protect against it */ ) { aoqi@0: // No useful hint. Set the hint to NULL and go on. aoqi@0: curTL->set_hint(0); aoqi@0: break; aoqi@0: } aoqi@0: assert(hintTL->size() > curTL->size(), "hint is inconsistent"); aoqi@0: if (hintTL->surplus() > 0) { aoqi@0: // The hint led to a list that has a surplus. Use it. aoqi@0: // Set the hint for the candidate to an overpopulated aoqi@0: // size. aoqi@0: curTL->set_hint(hintTL->size()); aoqi@0: // Change the candidate. aoqi@0: curTL = hintTL; aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return curTL; aoqi@0: } aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: aoqi@0: template aoqi@0: TreeList* aoqi@0: TreeList::get_better_list( aoqi@0: BinaryTreeDictionary* dictionary) { aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: TreeList* TreeList::remove_chunk_replace_if_needed(TreeChunk* tc) { aoqi@0: aoqi@0: TreeList* retTL = this; aoqi@0: Chunk_t* list = head(); aoqi@0: assert(!list || list != list->next(), "Chunk on list twice"); aoqi@0: assert(tc != NULL, "Chunk being removed is NULL"); aoqi@0: assert(parent() == NULL || this == parent()->left() || aoqi@0: this == parent()->right(), "list is inconsistent"); aoqi@0: assert(tc->is_free(), "Header is not marked correctly"); aoqi@0: assert(head() == NULL || head()->prev() == NULL, "list invariant"); aoqi@0: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); aoqi@0: aoqi@0: Chunk_t* prevFC = tc->prev(); aoqi@0: TreeChunk* nextTC = TreeChunk::as_TreeChunk(tc->next()); aoqi@0: assert(list != NULL, "should have at least the target chunk"); aoqi@0: aoqi@0: // Is this the first item on the list? aoqi@0: if (tc == list) { aoqi@0: // The "getChunk..." functions for a TreeList will not return the aoqi@0: // first chunk in the list unless it is the last chunk in the list aoqi@0: // because the first chunk is also acting as the tree node. aoqi@0: // When coalescing happens, however, the first chunk in the a tree aoqi@0: // list can be the start of a free range. Free ranges are removed aoqi@0: // from the free lists so that they are not available to be aoqi@0: // allocated when the sweeper yields (giving up the free list lock) aoqi@0: // to allow mutator activity. If this chunk is the first in the aoqi@0: // list and is not the last in the list, do the work to copy the aoqi@0: // TreeList from the first chunk to the next chunk and update all aoqi@0: // the TreeList pointers in the chunks in the list. aoqi@0: if (nextTC == NULL) { aoqi@0: assert(prevFC == NULL, "Not last chunk in the list"); aoqi@0: set_tail(NULL); aoqi@0: set_head(NULL); aoqi@0: } else { aoqi@0: // copy embedded list. aoqi@0: nextTC->set_embedded_list(tc->embedded_list()); aoqi@0: retTL = nextTC->embedded_list(); aoqi@0: // Fix the pointer to the list in each chunk in the list. aoqi@0: // This can be slow for a long list. Consider having aoqi@0: // an option that does not allow the first chunk on the aoqi@0: // list to be coalesced. aoqi@0: for (TreeChunk* curTC = nextTC; curTC != NULL; aoqi@0: curTC = TreeChunk::as_TreeChunk(curTC->next())) { aoqi@0: curTC->set_list(retTL); aoqi@0: } aoqi@0: // Fix the parent to point to the new TreeList. aoqi@0: if (retTL->parent() != NULL) { aoqi@0: if (this == retTL->parent()->left()) { aoqi@0: retTL->parent()->set_left(retTL); aoqi@0: } else { aoqi@0: assert(this == retTL->parent()->right(), "Parent is incorrect"); aoqi@0: retTL->parent()->set_right(retTL); aoqi@0: } aoqi@0: } aoqi@0: // Fix the children's parent pointers to point to the aoqi@0: // new list. aoqi@0: assert(right() == retTL->right(), "Should have been copied"); aoqi@0: if (retTL->right() != NULL) { aoqi@0: retTL->right()->set_parent(retTL); aoqi@0: } aoqi@0: assert(left() == retTL->left(), "Should have been copied"); aoqi@0: if (retTL->left() != NULL) { aoqi@0: retTL->left()->set_parent(retTL); aoqi@0: } aoqi@0: retTL->link_head(nextTC); aoqi@0: assert(nextTC->is_free(), "Should be a free chunk"); aoqi@0: } aoqi@0: } else { aoqi@0: if (nextTC == NULL) { aoqi@0: // Removing chunk at tail of list aoqi@0: this->link_tail(prevFC); aoqi@0: } aoqi@0: // Chunk is interior to the list aoqi@0: prevFC->link_after(nextTC); aoqi@0: } aoqi@0: aoqi@0: // Below this point the embeded TreeList being used for the aoqi@0: // tree node may have changed. Don't use "this" aoqi@0: // TreeList*. aoqi@0: // chunk should still be a free chunk (bit set in _prev) aoqi@0: assert(!retTL->head() || retTL->size() == retTL->head()->size(), aoqi@0: "Wrong sized chunk in list"); aoqi@0: debug_only( aoqi@0: tc->link_prev(NULL); aoqi@0: tc->link_next(NULL); aoqi@0: tc->set_list(NULL); aoqi@0: bool prev_found = false; aoqi@0: bool next_found = false; aoqi@0: for (Chunk_t* curFC = retTL->head(); aoqi@0: curFC != NULL; curFC = curFC->next()) { aoqi@0: assert(curFC != tc, "Chunk is still in list"); aoqi@0: if (curFC == prevFC) { aoqi@0: prev_found = true; aoqi@0: } aoqi@0: if (curFC == nextTC) { aoqi@0: next_found = true; aoqi@0: } aoqi@0: } aoqi@0: assert(prevFC == NULL || prev_found, "Chunk was lost from list"); aoqi@0: assert(nextTC == NULL || next_found, "Chunk was lost from list"); aoqi@0: assert(retTL->parent() == NULL || aoqi@0: retTL == retTL->parent()->left() || aoqi@0: retTL == retTL->parent()->right(), aoqi@0: "list is inconsistent"); aoqi@0: ) aoqi@0: retTL->decrement_count(); aoqi@0: aoqi@0: assert(tc->is_free(), "Should still be a free chunk"); aoqi@0: assert(retTL->head() == NULL || retTL->head()->prev() == NULL, aoqi@0: "list invariant"); aoqi@0: assert(retTL->tail() == NULL || retTL->tail()->next() == NULL, aoqi@0: "list invariant"); aoqi@0: return retTL; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: void TreeList::return_chunk_at_tail(TreeChunk* chunk) { aoqi@0: assert(chunk != NULL, "returning NULL chunk"); aoqi@0: assert(chunk->list() == this, "list should be set for chunk"); aoqi@0: assert(tail() != NULL, "The tree list is embedded in the first chunk"); aoqi@0: // which means that the list can never be empty. aoqi@0: assert(!this->verify_chunk_in_free_list(chunk), "Double entry"); aoqi@0: assert(head() == NULL || head()->prev() == NULL, "list invariant"); aoqi@0: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); aoqi@0: aoqi@0: Chunk_t* fc = tail(); aoqi@0: fc->link_after(chunk); aoqi@0: this->link_tail(chunk); aoqi@0: aoqi@0: assert(!tail() || size() == tail()->size(), "Wrong sized chunk in list"); aoqi@0: FreeList_t::increment_count(); aoqi@0: debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));) aoqi@0: assert(head() == NULL || head()->prev() == NULL, "list invariant"); aoqi@0: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); aoqi@0: } aoqi@0: aoqi@0: // Add this chunk at the head of the list. "At the head of the list" aoqi@0: // is defined to be after the chunk pointer to by head(). This is aoqi@0: // because the TreeList is embedded in the first TreeChunk in the aoqi@0: // list. See the definition of TreeChunk. aoqi@0: template aoqi@0: void TreeList::return_chunk_at_head(TreeChunk* chunk) { aoqi@0: assert(chunk->list() == this, "list should be set for chunk"); aoqi@0: assert(head() != NULL, "The tree list is embedded in the first chunk"); aoqi@0: assert(chunk != NULL, "returning NULL chunk"); aoqi@0: assert(!this->verify_chunk_in_free_list(chunk), "Double entry"); aoqi@0: assert(head() == NULL || head()->prev() == NULL, "list invariant"); aoqi@0: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); aoqi@0: aoqi@0: Chunk_t* fc = head()->next(); aoqi@0: if (fc != NULL) { aoqi@0: chunk->link_after(fc); aoqi@0: } else { aoqi@0: assert(tail() == NULL, "List is inconsistent"); aoqi@0: this->link_tail(chunk); aoqi@0: } aoqi@0: head()->link_after(chunk); aoqi@0: assert(!head() || size() == head()->size(), "Wrong sized chunk in list"); aoqi@0: FreeList_t::increment_count(); aoqi@0: debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));) aoqi@0: assert(head() == NULL || head()->prev() == NULL, "list invariant"); aoqi@0: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: void TreeChunk::assert_is_mangled() const { aoqi@0: assert((ZapUnusedHeapArea && aoqi@0: SpaceMangler::is_mangled((HeapWord*) Chunk_t::size_addr()) && aoqi@0: SpaceMangler::is_mangled((HeapWord*) Chunk_t::prev_addr()) && aoqi@0: SpaceMangler::is_mangled((HeapWord*) Chunk_t::next_addr())) || aoqi@0: (size() == 0 && prev() == NULL && next() == NULL), aoqi@0: "Space should be clear or mangled"); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: TreeChunk* TreeList::head_as_TreeChunk() { aoqi@0: assert(head() == NULL || (TreeChunk::as_TreeChunk(head())->list() == this), aoqi@0: "Wrong type of chunk?"); aoqi@0: return TreeChunk::as_TreeChunk(head()); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: TreeChunk* TreeList::first_available() { aoqi@0: assert(head() != NULL, "The head of the list cannot be NULL"); aoqi@0: Chunk_t* fc = head()->next(); aoqi@0: TreeChunk* retTC; aoqi@0: if (fc == NULL) { aoqi@0: retTC = head_as_TreeChunk(); aoqi@0: } else { aoqi@0: retTC = TreeChunk::as_TreeChunk(fc); aoqi@0: } aoqi@0: assert(retTC->list() == this, "Wrong type of chunk."); aoqi@0: return retTC; aoqi@0: } aoqi@0: aoqi@0: // Returns the block with the largest heap address amongst aoqi@0: // those in the list for this size; potentially slow and expensive, aoqi@0: // use with caution! aoqi@0: template aoqi@0: TreeChunk* TreeList::largest_address() { aoqi@0: assert(head() != NULL, "The head of the list cannot be NULL"); aoqi@0: Chunk_t* fc = head()->next(); aoqi@0: TreeChunk* retTC; aoqi@0: if (fc == NULL) { aoqi@0: retTC = head_as_TreeChunk(); aoqi@0: } else { aoqi@0: // walk down the list and return the one with the highest aoqi@0: // heap address among chunks of this size. aoqi@0: Chunk_t* last = fc; aoqi@0: while (fc->next() != NULL) { aoqi@0: if ((HeapWord*)last < (HeapWord*)fc) { aoqi@0: last = fc; aoqi@0: } aoqi@0: fc = fc->next(); aoqi@0: } aoqi@0: retTC = TreeChunk::as_TreeChunk(last); aoqi@0: } aoqi@0: assert(retTC->list() == this, "Wrong type of chunk."); aoqi@0: return retTC; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: BinaryTreeDictionary::BinaryTreeDictionary(MemRegion mr) { aoqi@0: assert((mr.byte_size() > min_size()), "minimum chunk size"); aoqi@0: aoqi@0: reset(mr); aoqi@0: assert(root()->left() == NULL, "reset check failed"); aoqi@0: assert(root()->right() == NULL, "reset check failed"); aoqi@0: assert(root()->head()->next() == NULL, "reset check failed"); aoqi@0: assert(root()->head()->prev() == NULL, "reset check failed"); aoqi@0: assert(total_size() == root()->size(), "reset check failed"); aoqi@0: assert(total_free_blocks() == 1, "reset check failed"); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::inc_total_size(size_t inc) { aoqi@0: _total_size = _total_size + inc; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::dec_total_size(size_t dec) { aoqi@0: _total_size = _total_size - dec; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::reset(MemRegion mr) { aoqi@0: assert((mr.byte_size() > min_size()), "minimum chunk size"); aoqi@0: set_root(TreeList::as_TreeList(mr.start(), mr.word_size())); aoqi@0: set_total_size(mr.word_size()); aoqi@0: set_total_free_blocks(1); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::reset(HeapWord* addr, size_t byte_size) { aoqi@0: MemRegion mr(addr, heap_word_size(byte_size)); aoqi@0: reset(mr); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::reset() { aoqi@0: set_root(NULL); aoqi@0: set_total_size(0); aoqi@0: set_total_free_blocks(0); aoqi@0: } aoqi@0: aoqi@0: // Get a free block of size at least size from tree, or NULL. aoqi@0: template aoqi@0: TreeChunk* aoqi@0: BinaryTreeDictionary::get_chunk_from_tree( aoqi@0: size_t size, aoqi@0: enum FreeBlockDictionary::Dither dither) aoqi@0: { aoqi@0: TreeList *curTL, *prevTL; aoqi@0: TreeChunk* retTC = NULL; aoqi@0: aoqi@0: assert((size >= min_size()), "minimum chunk size"); aoqi@0: if (FLSVerifyDictionary) { aoqi@0: verify_tree(); aoqi@0: } aoqi@0: // starting at the root, work downwards trying to find match. aoqi@0: // Remember the last node of size too great or too small. aoqi@0: for (prevTL = curTL = root(); curTL != NULL;) { aoqi@0: if (curTL->size() == size) { // exact match aoqi@0: break; aoqi@0: } aoqi@0: prevTL = curTL; aoqi@0: if (curTL->size() < size) { // proceed to right sub-tree aoqi@0: curTL = curTL->right(); aoqi@0: } else { // proceed to left sub-tree aoqi@0: assert(curTL->size() > size, "size inconsistency"); aoqi@0: curTL = curTL->left(); aoqi@0: } aoqi@0: } aoqi@0: if (curTL == NULL) { // couldn't find exact match aoqi@0: aoqi@0: if (dither == FreeBlockDictionary::exactly) return NULL; aoqi@0: aoqi@0: // try and find the next larger size by walking back up the search path aoqi@0: for (curTL = prevTL; curTL != NULL;) { aoqi@0: if (curTL->size() >= size) break; aoqi@0: else curTL = curTL->parent(); aoqi@0: } aoqi@0: assert(curTL == NULL || curTL->count() > 0, aoqi@0: "An empty list should not be in the tree"); aoqi@0: } aoqi@0: if (curTL != NULL) { aoqi@0: assert(curTL->size() >= size, "size inconsistency"); aoqi@0: aoqi@0: curTL = curTL->get_better_list(this); aoqi@0: aoqi@0: retTC = curTL->first_available(); aoqi@0: assert((retTC != NULL) && (curTL->count() > 0), aoqi@0: "A list in the binary tree should not be NULL"); aoqi@0: assert(retTC->size() >= size, aoqi@0: "A chunk of the wrong size was found"); aoqi@0: remove_chunk_from_tree(retTC); aoqi@0: assert(retTC->is_free(), "Header is not marked correctly"); aoqi@0: } aoqi@0: aoqi@0: if (FLSVerifyDictionary) { aoqi@0: verify(); aoqi@0: } aoqi@0: return retTC; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: TreeList* BinaryTreeDictionary::find_list(size_t size) const { aoqi@0: TreeList* curTL; aoqi@0: for (curTL = root(); curTL != NULL;) { aoqi@0: if (curTL->size() == size) { // exact match aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: if (curTL->size() < size) { // proceed to right sub-tree aoqi@0: curTL = curTL->right(); aoqi@0: } else { // proceed to left sub-tree aoqi@0: assert(curTL->size() > size, "size inconsistency"); aoqi@0: curTL = curTL->left(); aoqi@0: } aoqi@0: } aoqi@0: return curTL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: template aoqi@0: bool BinaryTreeDictionary::verify_chunk_in_free_list(Chunk_t* tc) const { aoqi@0: size_t size = tc->size(); aoqi@0: TreeList* tl = find_list(size); aoqi@0: if (tl == NULL) { aoqi@0: return false; aoqi@0: } else { aoqi@0: return tl->verify_chunk_in_free_list(tc); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: Chunk_t* BinaryTreeDictionary::find_largest_dict() const { aoqi@0: TreeList *curTL = root(); aoqi@0: if (curTL != NULL) { aoqi@0: while(curTL->right() != NULL) curTL = curTL->right(); aoqi@0: return curTL->largest_address(); aoqi@0: } else { aoqi@0: return NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Remove the current chunk from the tree. If it is not the last aoqi@0: // chunk in a list on a tree node, just unlink it. aoqi@0: // If it is the last chunk in the list (the next link is NULL), aoqi@0: // remove the node and repair the tree. aoqi@0: template aoqi@0: TreeChunk* aoqi@0: BinaryTreeDictionary::remove_chunk_from_tree(TreeChunk* tc) { aoqi@0: assert(tc != NULL, "Should not call with a NULL chunk"); aoqi@0: assert(tc->is_free(), "Header is not marked correctly"); aoqi@0: aoqi@0: TreeList *newTL, *parentTL; aoqi@0: TreeChunk* retTC; aoqi@0: TreeList* tl = tc->list(); aoqi@0: debug_only( aoqi@0: bool removing_only_chunk = false; aoqi@0: if (tl == _root) { aoqi@0: if ((_root->left() == NULL) && (_root->right() == NULL)) { aoqi@0: if (_root->count() == 1) { aoqi@0: assert(_root->head() == tc, "Should only be this one chunk"); aoqi@0: removing_only_chunk = true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: ) aoqi@0: assert(tl != NULL, "List should be set"); aoqi@0: assert(tl->parent() == NULL || tl == tl->parent()->left() || aoqi@0: tl == tl->parent()->right(), "list is inconsistent"); aoqi@0: aoqi@0: bool complicated_splice = false; aoqi@0: aoqi@0: retTC = tc; aoqi@0: // Removing this chunk can have the side effect of changing the node aoqi@0: // (TreeList*) in the tree. If the node is the root, update it. aoqi@0: TreeList* replacementTL = tl->remove_chunk_replace_if_needed(tc); aoqi@0: assert(tc->is_free(), "Chunk should still be free"); aoqi@0: assert(replacementTL->parent() == NULL || aoqi@0: replacementTL == replacementTL->parent()->left() || aoqi@0: replacementTL == replacementTL->parent()->right(), aoqi@0: "list is inconsistent"); aoqi@0: if (tl == root()) { aoqi@0: assert(replacementTL->parent() == NULL, "Incorrectly replacing root"); aoqi@0: set_root(replacementTL); aoqi@0: } aoqi@0: #ifdef ASSERT aoqi@0: if (tl != replacementTL) { aoqi@0: assert(replacementTL->head() != NULL, aoqi@0: "If the tree list was replaced, it should not be a NULL list"); aoqi@0: TreeList* rhl = replacementTL->head_as_TreeChunk()->list(); aoqi@0: TreeList* rtl = aoqi@0: TreeChunk::as_TreeChunk(replacementTL->tail())->list(); aoqi@0: assert(rhl == replacementTL, "Broken head"); aoqi@0: assert(rtl == replacementTL, "Broken tail"); aoqi@0: assert(replacementTL->size() == tc->size(), "Broken size"); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: // Does the tree need to be repaired? aoqi@0: if (replacementTL->count() == 0) { aoqi@0: assert(replacementTL->head() == NULL && aoqi@0: replacementTL->tail() == NULL, "list count is incorrect"); aoqi@0: // Find the replacement node for the (soon to be empty) node being removed. aoqi@0: // if we have a single (or no) child, splice child in our stead aoqi@0: if (replacementTL->left() == NULL) { aoqi@0: // left is NULL so pick right. right may also be NULL. aoqi@0: newTL = replacementTL->right(); aoqi@0: debug_only(replacementTL->clear_right();) aoqi@0: } else if (replacementTL->right() == NULL) { aoqi@0: // right is NULL aoqi@0: newTL = replacementTL->left(); aoqi@0: debug_only(replacementTL->clear_left();) aoqi@0: } else { // we have both children, so, by patriarchal convention, aoqi@0: // my replacement is least node in right sub-tree aoqi@0: complicated_splice = true; aoqi@0: newTL = remove_tree_minimum(replacementTL->right()); aoqi@0: assert(newTL != NULL && newTL->left() == NULL && aoqi@0: newTL->right() == NULL, "sub-tree minimum exists"); aoqi@0: } aoqi@0: // newTL is the replacement for the (soon to be empty) node. aoqi@0: // newTL may be NULL. aoqi@0: // should verify; we just cleanly excised our replacement aoqi@0: if (FLSVerifyDictionary) { aoqi@0: verify_tree(); aoqi@0: } aoqi@0: // first make newTL my parent's child aoqi@0: if ((parentTL = replacementTL->parent()) == NULL) { aoqi@0: // newTL should be root aoqi@0: assert(tl == root(), "Incorrectly replacing root"); aoqi@0: set_root(newTL); aoqi@0: if (newTL != NULL) { aoqi@0: newTL->clear_parent(); aoqi@0: } aoqi@0: } else if (parentTL->right() == replacementTL) { aoqi@0: // replacementTL is a right child aoqi@0: parentTL->set_right(newTL); aoqi@0: } else { // replacementTL is a left child aoqi@0: assert(parentTL->left() == replacementTL, "should be left child"); aoqi@0: parentTL->set_left(newTL); aoqi@0: } aoqi@0: debug_only(replacementTL->clear_parent();) aoqi@0: if (complicated_splice) { // we need newTL to get replacementTL's aoqi@0: // two children aoqi@0: assert(newTL != NULL && aoqi@0: newTL->left() == NULL && newTL->right() == NULL, aoqi@0: "newTL should not have encumbrances from the past"); aoqi@0: // we'd like to assert as below: aoqi@0: // assert(replacementTL->left() != NULL && replacementTL->right() != NULL, aoqi@0: // "else !complicated_splice"); aoqi@0: // ... however, the above assertion is too strong because we aren't aoqi@0: // guaranteed that replacementTL->right() is still NULL. aoqi@0: // Recall that we removed aoqi@0: // the right sub-tree minimum from replacementTL. aoqi@0: // That may well have been its right aoqi@0: // child! So we'll just assert half of the above: aoqi@0: assert(replacementTL->left() != NULL, "else !complicated_splice"); aoqi@0: newTL->set_left(replacementTL->left()); aoqi@0: newTL->set_right(replacementTL->right()); aoqi@0: debug_only( aoqi@0: replacementTL->clear_right(); aoqi@0: replacementTL->clear_left(); aoqi@0: ) aoqi@0: } aoqi@0: assert(replacementTL->right() == NULL && aoqi@0: replacementTL->left() == NULL && aoqi@0: replacementTL->parent() == NULL, aoqi@0: "delete without encumbrances"); aoqi@0: } aoqi@0: aoqi@0: assert(total_size() >= retTC->size(), "Incorrect total size"); aoqi@0: dec_total_size(retTC->size()); // size book-keeping aoqi@0: assert(total_free_blocks() > 0, "Incorrect total count"); aoqi@0: set_total_free_blocks(total_free_blocks() - 1); aoqi@0: aoqi@0: assert(retTC != NULL, "null chunk?"); aoqi@0: assert(retTC->prev() == NULL && retTC->next() == NULL, aoqi@0: "should return without encumbrances"); aoqi@0: if (FLSVerifyDictionary) { aoqi@0: verify_tree(); aoqi@0: } aoqi@0: assert(!removing_only_chunk || _root == NULL, "root should be NULL"); aoqi@0: return TreeChunk::as_TreeChunk(retTC); aoqi@0: } aoqi@0: aoqi@0: // Remove the leftmost node (lm) in the tree and return it. aoqi@0: // If lm has a right child, link it to the left node of aoqi@0: // the parent of lm. aoqi@0: template aoqi@0: TreeList* BinaryTreeDictionary::remove_tree_minimum(TreeList* tl) { aoqi@0: assert(tl != NULL && tl->parent() != NULL, "really need a proper sub-tree"); aoqi@0: // locate the subtree minimum by walking down left branches aoqi@0: TreeList* curTL = tl; aoqi@0: for (; curTL->left() != NULL; curTL = curTL->left()); aoqi@0: // obviously curTL now has at most one child, a right child aoqi@0: if (curTL != root()) { // Should this test just be removed? aoqi@0: TreeList* parentTL = curTL->parent(); aoqi@0: if (parentTL->left() == curTL) { // curTL is a left child aoqi@0: parentTL->set_left(curTL->right()); aoqi@0: } else { aoqi@0: // If the list tl has no left child, then curTL may be aoqi@0: // the right child of parentTL. aoqi@0: assert(parentTL->right() == curTL, "should be a right child"); aoqi@0: parentTL->set_right(curTL->right()); aoqi@0: } aoqi@0: } else { aoqi@0: // The only use of this method would not pass the root of the aoqi@0: // tree (as indicated by the assertion above that the tree list aoqi@0: // has a parent) but the specification does not explicitly exclude the aoqi@0: // passing of the root so accomodate it. aoqi@0: set_root(NULL); aoqi@0: } aoqi@0: debug_only( aoqi@0: curTL->clear_parent(); // Test if this needs to be cleared aoqi@0: curTL->clear_right(); // recall, above, left child is already null aoqi@0: ) aoqi@0: // we just excised a (non-root) node, we should still verify all tree invariants aoqi@0: if (FLSVerifyDictionary) { aoqi@0: verify_tree(); aoqi@0: } aoqi@0: return curTL; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::insert_chunk_in_tree(Chunk_t* fc) { aoqi@0: TreeList *curTL, *prevTL; aoqi@0: size_t size = fc->size(); aoqi@0: aoqi@0: assert((size >= min_size()), aoqi@0: err_msg(SIZE_FORMAT " is too small to be a TreeChunk " SIZE_FORMAT, aoqi@0: size, min_size())); aoqi@0: if (FLSVerifyDictionary) { aoqi@0: verify_tree(); aoqi@0: } aoqi@0: aoqi@0: fc->clear_next(); aoqi@0: fc->link_prev(NULL); aoqi@0: aoqi@0: // work down from the _root, looking for insertion point aoqi@0: for (prevTL = curTL = root(); curTL != NULL;) { aoqi@0: if (curTL->size() == size) // exact match aoqi@0: break; aoqi@0: prevTL = curTL; aoqi@0: if (curTL->size() > size) { // follow left branch aoqi@0: curTL = curTL->left(); aoqi@0: } else { // follow right branch aoqi@0: assert(curTL->size() < size, "size inconsistency"); aoqi@0: curTL = curTL->right(); aoqi@0: } aoqi@0: } aoqi@0: TreeChunk* tc = TreeChunk::as_TreeChunk(fc); aoqi@0: // This chunk is being returned to the binary tree. Its embedded aoqi@0: // TreeList should be unused at this point. aoqi@0: tc->initialize(); aoqi@0: if (curTL != NULL) { // exact match aoqi@0: tc->set_list(curTL); aoqi@0: curTL->return_chunk_at_tail(tc); aoqi@0: } else { // need a new node in tree aoqi@0: tc->clear_next(); aoqi@0: tc->link_prev(NULL); aoqi@0: TreeList* newTL = TreeList::as_TreeList(tc); aoqi@0: assert(((TreeChunk*)tc)->list() == newTL, aoqi@0: "List was not initialized correctly"); aoqi@0: if (prevTL == NULL) { // we are the only tree node aoqi@0: assert(root() == NULL, "control point invariant"); aoqi@0: set_root(newTL); aoqi@0: } else { // insert under prevTL ... aoqi@0: if (prevTL->size() < size) { // am right child aoqi@0: assert(prevTL->right() == NULL, "control point invariant"); aoqi@0: prevTL->set_right(newTL); aoqi@0: } else { // am left child aoqi@0: assert(prevTL->size() > size && prevTL->left() == NULL, "cpt pt inv"); aoqi@0: prevTL->set_left(newTL); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: assert(tc->list() != NULL, "Tree list should be set"); aoqi@0: aoqi@0: inc_total_size(size); aoqi@0: // Method 'total_size_in_tree' walks through the every block in the aoqi@0: // tree, so it can cause significant performance loss if there are aoqi@0: // many blocks in the tree aoqi@0: assert(!FLSVerifyDictionary || total_size_in_tree(root()) == total_size(), "_total_size inconsistency"); aoqi@0: set_total_free_blocks(total_free_blocks() + 1); aoqi@0: if (FLSVerifyDictionary) { aoqi@0: verify_tree(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: size_t BinaryTreeDictionary::max_chunk_size() const { aoqi@0: FreeBlockDictionary::verify_par_locked(); aoqi@0: TreeList* tc = root(); aoqi@0: if (tc == NULL) return 0; aoqi@0: for (; tc->right() != NULL; tc = tc->right()); aoqi@0: return tc->size(); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: size_t BinaryTreeDictionary::total_list_length(TreeList* tl) const { aoqi@0: size_t res; aoqi@0: res = tl->count(); aoqi@0: #ifdef ASSERT aoqi@0: size_t cnt; aoqi@0: Chunk_t* tc = tl->head(); aoqi@0: for (cnt = 0; tc != NULL; tc = tc->next(), cnt++); aoqi@0: assert(res == cnt, "The count is not being maintained correctly"); aoqi@0: #endif aoqi@0: return res; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: size_t BinaryTreeDictionary::total_size_in_tree(TreeList* tl) const { aoqi@0: if (tl == NULL) aoqi@0: return 0; aoqi@0: return (tl->size() * total_list_length(tl)) + aoqi@0: total_size_in_tree(tl->left()) + aoqi@0: total_size_in_tree(tl->right()); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: double BinaryTreeDictionary::sum_of_squared_block_sizes(TreeList* const tl) const { aoqi@0: if (tl == NULL) { aoqi@0: return 0.0; aoqi@0: } aoqi@0: double size = (double)(tl->size()); aoqi@0: double curr = size * size * total_list_length(tl); aoqi@0: curr += sum_of_squared_block_sizes(tl->left()); aoqi@0: curr += sum_of_squared_block_sizes(tl->right()); aoqi@0: return curr; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: size_t BinaryTreeDictionary::total_free_blocks_in_tree(TreeList* tl) const { aoqi@0: if (tl == NULL) aoqi@0: return 0; aoqi@0: return total_list_length(tl) + aoqi@0: total_free_blocks_in_tree(tl->left()) + aoqi@0: total_free_blocks_in_tree(tl->right()); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: size_t BinaryTreeDictionary::num_free_blocks() const { aoqi@0: assert(total_free_blocks_in_tree(root()) == total_free_blocks(), aoqi@0: "_total_free_blocks inconsistency"); aoqi@0: return total_free_blocks(); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: size_t BinaryTreeDictionary::tree_height_helper(TreeList* tl) const { aoqi@0: if (tl == NULL) aoqi@0: return 0; aoqi@0: return 1 + MAX2(tree_height_helper(tl->left()), aoqi@0: tree_height_helper(tl->right())); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: size_t BinaryTreeDictionary::tree_height() const { aoqi@0: return tree_height_helper(root()); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: size_t BinaryTreeDictionary::total_nodes_helper(TreeList* tl) const { aoqi@0: if (tl == NULL) { aoqi@0: return 0; aoqi@0: } aoqi@0: return 1 + total_nodes_helper(tl->left()) + aoqi@0: total_nodes_helper(tl->right()); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: size_t BinaryTreeDictionary::total_nodes_in_tree(TreeList* tl) const { aoqi@0: return total_nodes_helper(root()); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::dict_census_update(size_t size, bool split, bool birth){} aoqi@0: aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: template <> aoqi@0: void AFLBinaryTreeDictionary::dict_census_update(size_t size, bool split, bool birth) { aoqi@0: TreeList >* nd = find_list(size); aoqi@0: if (nd) { aoqi@0: if (split) { aoqi@0: if (birth) { aoqi@0: nd->increment_split_births(); aoqi@0: nd->increment_surplus(); aoqi@0: } else { aoqi@0: nd->increment_split_deaths(); aoqi@0: nd->decrement_surplus(); aoqi@0: } aoqi@0: } else { aoqi@0: if (birth) { aoqi@0: nd->increment_coal_births(); aoqi@0: nd->increment_surplus(); aoqi@0: } else { aoqi@0: nd->increment_coal_deaths(); aoqi@0: nd->decrement_surplus(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // A list for this size may not be found (nd == 0) if aoqi@0: // This is a death where the appropriate list is now aoqi@0: // empty and has been removed from the list. aoqi@0: // This is a birth associated with a LinAB. The chunk aoqi@0: // for the LinAB is not in the dictionary. aoqi@0: } aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: aoqi@0: template aoqi@0: bool BinaryTreeDictionary::coal_dict_over_populated(size_t size) { aoqi@0: // For the general type of freelists, encourage coalescing by aoqi@0: // returning true. aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: template <> aoqi@0: bool AFLBinaryTreeDictionary::coal_dict_over_populated(size_t size) { aoqi@0: if (FLSAlwaysCoalesceLarge) return true; aoqi@0: aoqi@0: TreeList >* list_of_size = find_list(size); aoqi@0: // None of requested size implies overpopulated. aoqi@0: return list_of_size == NULL || list_of_size->coal_desired() <= 0 || aoqi@0: list_of_size->count() > list_of_size->coal_desired(); aoqi@0: } aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: aoqi@0: // Closures for walking the binary tree. aoqi@0: // do_list() walks the free list in a node applying the closure aoqi@0: // to each free chunk in the list aoqi@0: // do_tree() walks the nodes in the binary tree applying do_list() aoqi@0: // to each list at each node. aoqi@0: aoqi@0: template aoqi@0: class TreeCensusClosure : public StackObj { aoqi@0: protected: aoqi@0: virtual void do_list(FreeList_t* fl) = 0; aoqi@0: public: aoqi@0: virtual void do_tree(TreeList* tl) = 0; aoqi@0: }; aoqi@0: aoqi@0: template aoqi@0: class AscendTreeCensusClosure : public TreeCensusClosure { aoqi@0: public: aoqi@0: void do_tree(TreeList* tl) { aoqi@0: if (tl != NULL) { aoqi@0: do_tree(tl->left()); aoqi@0: this->do_list(tl); aoqi@0: do_tree(tl->right()); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: template aoqi@0: class DescendTreeCensusClosure : public TreeCensusClosure { aoqi@0: public: aoqi@0: void do_tree(TreeList* tl) { aoqi@0: if (tl != NULL) { aoqi@0: do_tree(tl->right()); aoqi@0: this->do_list(tl); aoqi@0: do_tree(tl->left()); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // For each list in the tree, calculate the desired, desired aoqi@0: // coalesce, count before sweep, and surplus before sweep. aoqi@0: template aoqi@0: class BeginSweepClosure : public AscendTreeCensusClosure { aoqi@0: double _percentage; aoqi@0: float _inter_sweep_current; aoqi@0: float _inter_sweep_estimate; aoqi@0: float _intra_sweep_estimate; aoqi@0: aoqi@0: public: aoqi@0: BeginSweepClosure(double p, float inter_sweep_current, aoqi@0: float inter_sweep_estimate, aoqi@0: float intra_sweep_estimate) : aoqi@0: _percentage(p), aoqi@0: _inter_sweep_current(inter_sweep_current), aoqi@0: _inter_sweep_estimate(inter_sweep_estimate), aoqi@0: _intra_sweep_estimate(intra_sweep_estimate) { } aoqi@0: aoqi@0: void do_list(FreeList* fl) {} aoqi@0: aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: void do_list(AdaptiveFreeList* fl) { aoqi@0: double coalSurplusPercent = _percentage; aoqi@0: fl->compute_desired(_inter_sweep_current, _inter_sweep_estimate, _intra_sweep_estimate); aoqi@0: fl->set_coal_desired((ssize_t)((double)fl->desired() * coalSurplusPercent)); aoqi@0: fl->set_before_sweep(fl->count()); aoqi@0: fl->set_bfr_surp(fl->surplus()); aoqi@0: } aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: }; aoqi@0: aoqi@0: // Used to search the tree until a condition is met. aoqi@0: // Similar to TreeCensusClosure but searches the aoqi@0: // tree and returns promptly when found. aoqi@0: aoqi@0: template aoqi@0: class TreeSearchClosure : public StackObj { aoqi@0: protected: aoqi@0: virtual bool do_list(FreeList_t* fl) = 0; aoqi@0: public: aoqi@0: virtual bool do_tree(TreeList* tl) = 0; aoqi@0: }; aoqi@0: aoqi@0: #if 0 // Don't need this yet but here for symmetry. aoqi@0: template aoqi@0: class AscendTreeSearchClosure : public TreeSearchClosure { aoqi@0: public: aoqi@0: bool do_tree(TreeList* tl) { aoqi@0: if (tl != NULL) { aoqi@0: if (do_tree(tl->left())) return true; aoqi@0: if (do_list(tl)) return true; aoqi@0: if (do_tree(tl->right())) return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: }; aoqi@0: #endif aoqi@0: aoqi@0: template aoqi@0: class DescendTreeSearchClosure : public TreeSearchClosure { aoqi@0: public: aoqi@0: bool do_tree(TreeList* tl) { aoqi@0: if (tl != NULL) { aoqi@0: if (do_tree(tl->right())) return true; aoqi@0: if (this->do_list(tl)) return true; aoqi@0: if (do_tree(tl->left())) return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // Searches the tree for a chunk that ends at the aoqi@0: // specified address. aoqi@0: template aoqi@0: class EndTreeSearchClosure : public DescendTreeSearchClosure { aoqi@0: HeapWord* _target; aoqi@0: Chunk_t* _found; aoqi@0: aoqi@0: public: aoqi@0: EndTreeSearchClosure(HeapWord* target) : _target(target), _found(NULL) {} aoqi@0: bool do_list(FreeList_t* fl) { aoqi@0: Chunk_t* item = fl->head(); aoqi@0: while (item != NULL) { aoqi@0: if (item->end() == (uintptr_t*) _target) { aoqi@0: _found = item; aoqi@0: return true; aoqi@0: } aoqi@0: item = item->next(); aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: Chunk_t* found() { return _found; } aoqi@0: }; aoqi@0: aoqi@0: template aoqi@0: Chunk_t* BinaryTreeDictionary::find_chunk_ends_at(HeapWord* target) const { aoqi@0: EndTreeSearchClosure etsc(target); aoqi@0: bool found_target = etsc.do_tree(root()); aoqi@0: assert(found_target || etsc.found() == NULL, "Consistency check"); aoqi@0: assert(!found_target || etsc.found() != NULL, "Consistency check"); aoqi@0: return etsc.found(); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::begin_sweep_dict_census(double coalSurplusPercent, aoqi@0: float inter_sweep_current, float inter_sweep_estimate, float intra_sweep_estimate) { aoqi@0: BeginSweepClosure bsc(coalSurplusPercent, inter_sweep_current, aoqi@0: inter_sweep_estimate, aoqi@0: intra_sweep_estimate); aoqi@0: bsc.do_tree(root()); aoqi@0: } aoqi@0: aoqi@0: // Closures and methods for calculating total bytes returned to the aoqi@0: // free lists in the tree. aoqi@0: #ifndef PRODUCT aoqi@0: template aoqi@0: class InitializeDictReturnedBytesClosure : public AscendTreeCensusClosure { aoqi@0: public: aoqi@0: void do_list(FreeList_t* fl) { aoqi@0: fl->set_returned_bytes(0); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::initialize_dict_returned_bytes() { aoqi@0: InitializeDictReturnedBytesClosure idrb; aoqi@0: idrb.do_tree(root()); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: class ReturnedBytesClosure : public AscendTreeCensusClosure { aoqi@0: size_t _dict_returned_bytes; aoqi@0: public: aoqi@0: ReturnedBytesClosure() { _dict_returned_bytes = 0; } aoqi@0: void do_list(FreeList_t* fl) { aoqi@0: _dict_returned_bytes += fl->returned_bytes(); aoqi@0: } aoqi@0: size_t dict_returned_bytes() { return _dict_returned_bytes; } aoqi@0: }; aoqi@0: aoqi@0: template aoqi@0: size_t BinaryTreeDictionary::sum_dict_returned_bytes() { aoqi@0: ReturnedBytesClosure rbc; aoqi@0: rbc.do_tree(root()); aoqi@0: aoqi@0: return rbc.dict_returned_bytes(); aoqi@0: } aoqi@0: aoqi@0: // Count the number of entries in the tree. aoqi@0: template aoqi@0: class treeCountClosure : public DescendTreeCensusClosure { aoqi@0: public: aoqi@0: uint count; aoqi@0: treeCountClosure(uint c) { count = c; } aoqi@0: void do_list(FreeList_t* fl) { aoqi@0: count++; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: template aoqi@0: size_t BinaryTreeDictionary::total_count() { aoqi@0: treeCountClosure ctc(0); aoqi@0: ctc.do_tree(root()); aoqi@0: return ctc.count; aoqi@0: } aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: // Calculate surpluses for the lists in the tree. aoqi@0: template aoqi@0: class setTreeSurplusClosure : public AscendTreeCensusClosure { aoqi@0: double percentage; aoqi@0: public: aoqi@0: setTreeSurplusClosure(double v) { percentage = v; } aoqi@0: void do_list(FreeList* fl) {} aoqi@0: aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: void do_list(AdaptiveFreeList* fl) { aoqi@0: double splitSurplusPercent = percentage; aoqi@0: fl->set_surplus(fl->count() - aoqi@0: (ssize_t)((double)fl->desired() * splitSurplusPercent)); aoqi@0: } aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: }; aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::set_tree_surplus(double splitSurplusPercent) { aoqi@0: setTreeSurplusClosure sts(splitSurplusPercent); aoqi@0: sts.do_tree(root()); aoqi@0: } aoqi@0: aoqi@0: // Set hints for the lists in the tree. aoqi@0: template aoqi@0: class setTreeHintsClosure : public DescendTreeCensusClosure { aoqi@0: size_t hint; aoqi@0: public: aoqi@0: setTreeHintsClosure(size_t v) { hint = v; } aoqi@0: void do_list(FreeList* fl) {} aoqi@0: aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: void do_list(AdaptiveFreeList* fl) { aoqi@0: fl->set_hint(hint); aoqi@0: assert(fl->hint() == 0 || fl->hint() > fl->size(), aoqi@0: "Current hint is inconsistent"); aoqi@0: if (fl->surplus() > 0) { aoqi@0: hint = fl->size(); aoqi@0: } aoqi@0: } aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: }; aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::set_tree_hints(void) { aoqi@0: setTreeHintsClosure sth(0); aoqi@0: sth.do_tree(root()); aoqi@0: } aoqi@0: aoqi@0: // Save count before previous sweep and splits and coalesces. aoqi@0: template aoqi@0: class clearTreeCensusClosure : public AscendTreeCensusClosure { aoqi@0: void do_list(FreeList* fl) {} aoqi@0: aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: void do_list(AdaptiveFreeList* fl) { aoqi@0: fl->set_prev_sweep(fl->count()); aoqi@0: fl->set_coal_births(0); aoqi@0: fl->set_coal_deaths(0); aoqi@0: fl->set_split_births(0); aoqi@0: fl->set_split_deaths(0); aoqi@0: } aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: }; aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::clear_tree_census(void) { aoqi@0: clearTreeCensusClosure ctc; aoqi@0: ctc.do_tree(root()); aoqi@0: } aoqi@0: aoqi@0: // Do reporting and post sweep clean up. aoqi@0: template aoqi@0: void BinaryTreeDictionary::end_sweep_dict_census(double splitSurplusPercent) { aoqi@0: // Does walking the tree 3 times hurt? aoqi@0: set_tree_surplus(splitSurplusPercent); aoqi@0: set_tree_hints(); aoqi@0: if (PrintGC && Verbose) { aoqi@0: report_statistics(); aoqi@0: } aoqi@0: clear_tree_census(); aoqi@0: } aoqi@0: aoqi@0: // Print summary statistics aoqi@0: template aoqi@0: void BinaryTreeDictionary::report_statistics() const { aoqi@0: FreeBlockDictionary::verify_par_locked(); aoqi@0: gclog_or_tty->print("Statistics for BinaryTreeDictionary:\n" aoqi@0: "------------------------------------\n"); aoqi@0: size_t total_size = total_chunk_size(debug_only(NULL)); aoqi@0: size_t free_blocks = num_free_blocks(); aoqi@0: gclog_or_tty->print("Total Free Space: " SIZE_FORMAT "\n", total_size); aoqi@0: gclog_or_tty->print("Max Chunk Size: " SIZE_FORMAT "\n", max_chunk_size()); aoqi@0: gclog_or_tty->print("Number of Blocks: " SIZE_FORMAT "\n", free_blocks); aoqi@0: if (free_blocks > 0) { aoqi@0: gclog_or_tty->print("Av. Block Size: " SIZE_FORMAT "\n", total_size/free_blocks); aoqi@0: } aoqi@0: gclog_or_tty->print("Tree Height: " SIZE_FORMAT "\n", tree_height()); aoqi@0: } aoqi@0: aoqi@0: // Print census information - counts, births, deaths, etc. aoqi@0: // for each list in the tree. Also print some summary aoqi@0: // information. aoqi@0: template aoqi@0: class PrintTreeCensusClosure : public AscendTreeCensusClosure { aoqi@0: int _print_line; aoqi@0: size_t _total_free; aoqi@0: FreeList_t _total; aoqi@0: aoqi@0: public: aoqi@0: PrintTreeCensusClosure() { aoqi@0: _print_line = 0; aoqi@0: _total_free = 0; aoqi@0: } aoqi@0: FreeList_t* total() { return &_total; } aoqi@0: size_t total_free() { return _total_free; } aoqi@0: void do_list(FreeList* fl) { aoqi@0: if (++_print_line >= 40) { aoqi@0: FreeList_t::print_labels_on(gclog_or_tty, "size"); aoqi@0: _print_line = 0; aoqi@0: } aoqi@0: fl->print_on(gclog_or_tty); aoqi@0: _total_free += fl->count() * fl->size() ; aoqi@0: total()->set_count( total()->count() + fl->count() ); aoqi@0: } aoqi@0: aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: void do_list(AdaptiveFreeList* fl) { aoqi@0: if (++_print_line >= 40) { aoqi@0: FreeList_t::print_labels_on(gclog_or_tty, "size"); aoqi@0: _print_line = 0; aoqi@0: } aoqi@0: fl->print_on(gclog_or_tty); aoqi@0: _total_free += fl->count() * fl->size() ; aoqi@0: total()->set_count( total()->count() + fl->count() ); aoqi@0: total()->set_bfr_surp( total()->bfr_surp() + fl->bfr_surp() ); aoqi@0: total()->set_surplus( total()->split_deaths() + fl->surplus() ); aoqi@0: total()->set_desired( total()->desired() + fl->desired() ); aoqi@0: total()->set_prev_sweep( total()->prev_sweep() + fl->prev_sweep() ); aoqi@0: total()->set_before_sweep(total()->before_sweep() + fl->before_sweep()); aoqi@0: total()->set_coal_births( total()->coal_births() + fl->coal_births() ); aoqi@0: total()->set_coal_deaths( total()->coal_deaths() + fl->coal_deaths() ); aoqi@0: total()->set_split_births(total()->split_births() + fl->split_births()); aoqi@0: total()->set_split_deaths(total()->split_deaths() + fl->split_deaths()); aoqi@0: } aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: }; aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::print_dict_census(void) const { aoqi@0: aoqi@0: gclog_or_tty->print("\nBinaryTree\n"); aoqi@0: FreeList_t::print_labels_on(gclog_or_tty, "size"); aoqi@0: PrintTreeCensusClosure ptc; aoqi@0: ptc.do_tree(root()); aoqi@0: aoqi@0: FreeList_t* total = ptc.total(); aoqi@0: FreeList_t::print_labels_on(gclog_or_tty, " "); aoqi@0: } aoqi@0: aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: template <> aoqi@0: void AFLBinaryTreeDictionary::print_dict_census(void) const { aoqi@0: aoqi@0: gclog_or_tty->print("\nBinaryTree\n"); aoqi@0: AdaptiveFreeList::print_labels_on(gclog_or_tty, "size"); aoqi@0: PrintTreeCensusClosure > ptc; aoqi@0: ptc.do_tree(root()); aoqi@0: aoqi@0: AdaptiveFreeList* total = ptc.total(); aoqi@0: AdaptiveFreeList::print_labels_on(gclog_or_tty, " "); aoqi@0: total->print_on(gclog_or_tty, "TOTAL\t"); aoqi@0: gclog_or_tty->print( aoqi@0: "total_free(words): " SIZE_FORMAT_W(16) aoqi@0: " growth: %8.5f deficit: %8.5f\n", aoqi@0: ptc.total_free(), aoqi@0: (double)(total->split_births() + total->coal_births() aoqi@0: - total->split_deaths() - total->coal_deaths()) aoqi@0: /(total->prev_sweep() != 0 ? (double)total->prev_sweep() : 1.0), aoqi@0: (double)(total->desired() - total->count()) aoqi@0: /(total->desired() != 0 ? (double)total->desired() : 1.0)); aoqi@0: } aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: aoqi@0: template aoqi@0: class PrintFreeListsClosure : public AscendTreeCensusClosure { aoqi@0: outputStream* _st; aoqi@0: int _print_line; aoqi@0: aoqi@0: public: aoqi@0: PrintFreeListsClosure(outputStream* st) { aoqi@0: _st = st; aoqi@0: _print_line = 0; aoqi@0: } aoqi@0: void do_list(FreeList_t* fl) { aoqi@0: if (++_print_line >= 40) { aoqi@0: FreeList_t::print_labels_on(_st, "size"); aoqi@0: _print_line = 0; aoqi@0: } aoqi@0: fl->print_on(gclog_or_tty); aoqi@0: size_t sz = fl->size(); aoqi@0: for (Chunk_t* fc = fl->head(); fc != NULL; aoqi@0: fc = fc->next()) { aoqi@0: _st->print_cr("\t[" PTR_FORMAT "," PTR_FORMAT ") %s", aoqi@0: p2i(fc), p2i((HeapWord*)fc + sz), aoqi@0: fc->cantCoalesce() ? "\t CC" : ""); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::print_free_lists(outputStream* st) const { aoqi@0: aoqi@0: FreeList_t::print_labels_on(st, "size"); aoqi@0: PrintFreeListsClosure pflc(st); aoqi@0: pflc.do_tree(root()); aoqi@0: } aoqi@0: aoqi@0: // Verify the following tree invariants: aoqi@0: // . _root has no parent aoqi@0: // . parent and child point to each other aoqi@0: // . each node's key correctly related to that of its child(ren) aoqi@0: template aoqi@0: void BinaryTreeDictionary::verify_tree() const { aoqi@0: guarantee(root() == NULL || total_free_blocks() == 0 || aoqi@0: total_size() != 0, "_total_size should't be 0?"); aoqi@0: guarantee(root() == NULL || root()->parent() == NULL, "_root shouldn't have parent"); aoqi@0: verify_tree_helper(root()); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: size_t BinaryTreeDictionary::verify_prev_free_ptrs(TreeList* tl) { aoqi@0: size_t ct = 0; aoqi@0: for (Chunk_t* curFC = tl->head(); curFC != NULL; curFC = curFC->next()) { aoqi@0: ct++; aoqi@0: assert(curFC->prev() == NULL || curFC->prev()->is_free(), aoqi@0: "Chunk should be free"); aoqi@0: } aoqi@0: return ct; aoqi@0: } aoqi@0: aoqi@0: // Note: this helper is recursive rather than iterative, so use with aoqi@0: // caution on very deep trees; and watch out for stack overflow errors; aoqi@0: // In general, to be used only for debugging. aoqi@0: template aoqi@0: void BinaryTreeDictionary::verify_tree_helper(TreeList* tl) const { aoqi@0: if (tl == NULL) aoqi@0: return; aoqi@0: guarantee(tl->size() != 0, "A list must has a size"); aoqi@0: guarantee(tl->left() == NULL || tl->left()->parent() == tl, aoqi@0: "parent<-/->left"); aoqi@0: guarantee(tl->right() == NULL || tl->right()->parent() == tl, aoqi@0: "parent<-/->right");; aoqi@0: guarantee(tl->left() == NULL || tl->left()->size() < tl->size(), aoqi@0: "parent !> left"); aoqi@0: guarantee(tl->right() == NULL || tl->right()->size() > tl->size(), aoqi@0: "parent !< left"); aoqi@0: guarantee(tl->head() == NULL || tl->head()->is_free(), "!Free"); aoqi@0: guarantee(tl->head() == NULL || tl->head_as_TreeChunk()->list() == tl, aoqi@0: "list inconsistency"); aoqi@0: guarantee(tl->count() > 0 || (tl->head() == NULL && tl->tail() == NULL), aoqi@0: "list count is inconsistent"); aoqi@0: guarantee(tl->count() > 1 || tl->head() == tl->tail(), aoqi@0: "list is incorrectly constructed"); aoqi@0: size_t count = verify_prev_free_ptrs(tl); aoqi@0: guarantee(count == (size_t)tl->count(), "Node count is incorrect"); aoqi@0: if (tl->head() != NULL) { aoqi@0: tl->head_as_TreeChunk()->verify_tree_chunk_list(); aoqi@0: } aoqi@0: verify_tree_helper(tl->left()); aoqi@0: verify_tree_helper(tl->right()); aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: void BinaryTreeDictionary::verify() const { aoqi@0: verify_tree(); aoqi@0: guarantee(total_size() == total_size_in_tree(root()), "Total Size inconsistency"); aoqi@0: } aoqi@0: aoqi@0: template class TreeList >; aoqi@0: template class BinaryTreeDictionary >; aoqi@0: template class TreeChunk >; aoqi@0: aoqi@0: template class TreeList >; aoqi@0: template class BinaryTreeDictionary >; aoqi@0: template class TreeChunk >; aoqi@0: aoqi@0: aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: // Explicitly instantiate these types for FreeChunk. aoqi@0: template class TreeList >; aoqi@0: template class BinaryTreeDictionary >; aoqi@0: template class TreeChunk >; aoqi@0: aoqi@0: #endif // INCLUDE_ALL_GCS