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