src/share/vm/memory/binaryTreeDictionary.cpp

Thu, 23 May 2013 12:44:18 +0100

author
chegar
date
Thu, 23 May 2013 12:44:18 +0100
changeset 5249
ce9ecec70f99
parent 4544
3c9bc17b9403
child 5689
de88570fabfc
permissions
-rw-r--r--

Merge

duke@435 1 /*
jmasa@3730 2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
jprovino@4542 26 #include "utilities/macros.hpp"
stefank@2314 27 #include "gc_implementation/shared/allocationStats.hpp"
jmasa@3730 28 #include "memory/binaryTreeDictionary.hpp"
jmasa@4196 29 #include "memory/freeList.hpp"
jmasa@4196 30 #include "memory/freeBlockDictionary.hpp"
jmasa@4196 31 #include "memory/metablock.hpp"
jmasa@4196 32 #include "memory/metachunk.hpp"
stefank@2314 33 #include "runtime/globals.hpp"
stefank@2314 34 #include "utilities/ostream.hpp"
jprovino@4542 35 #include "utilities/macros.hpp"
jprovino@4542 36 #if INCLUDE_ALL_GCS
jmasa@4196 37 #include "gc_implementation/concurrentMarkSweep/adaptiveFreeList.hpp"
jmasa@4196 38 #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
jmasa@3730 39 #include "gc_implementation/shared/spaceDecorator.hpp"
jmasa@3730 40 #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
jprovino@4542 41 #endif // INCLUDE_ALL_GCS
duke@435 42
duke@435 43 ////////////////////////////////////////////////////////////////////////////////
duke@435 44 // A binary tree based search structure for free blocks.
duke@435 45 // This is currently used in the Concurrent Mark&Sweep implementation.
duke@435 46 ////////////////////////////////////////////////////////////////////////////////
duke@435 47
jmasa@4196 48 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 49 size_t TreeChunk<Chunk_t, FreeList_t>::_min_tree_chunk_size = sizeof(TreeChunk<Chunk_t, FreeList_t>)/HeapWordSize;
jmasa@4196 50
jmasa@4196 51 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 52 TreeChunk<Chunk_t, FreeList_t>* TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(Chunk_t* fc) {
duke@435 53 // Do some assertion checking here.
jmasa@4196 54 return (TreeChunk<Chunk_t, FreeList_t>*) fc;
duke@435 55 }
duke@435 56
jmasa@4196 57 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 58 void TreeChunk<Chunk_t, FreeList_t>::verify_tree_chunk_list() const {
jmasa@4196 59 TreeChunk<Chunk_t, FreeList_t>* nextTC = (TreeChunk<Chunk_t, FreeList_t>*)next();
duke@435 60 if (prev() != NULL) { // interior list node shouldn'r have tree fields
duke@435 61 guarantee(embedded_list()->parent() == NULL && embedded_list()->left() == NULL &&
duke@435 62 embedded_list()->right() == NULL, "should be clear");
duke@435 63 }
duke@435 64 if (nextTC != NULL) {
duke@435 65 guarantee(as_TreeChunk(nextTC->prev()) == this, "broken chain");
duke@435 66 guarantee(nextTC->size() == size(), "wrong size");
jmasa@3732 67 nextTC->verify_tree_chunk_list();
duke@435 68 }
duke@435 69 }
duke@435 70
jmasa@4196 71 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4382 72 TreeList<Chunk_t, FreeList_t>::TreeList() : _parent(NULL),
jmasa@4382 73 _left(NULL), _right(NULL) {}
duke@435 74
jmasa@4196 75 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 76 TreeList<Chunk_t, FreeList_t>*
jmasa@4196 77 TreeList<Chunk_t, FreeList_t>::as_TreeList(TreeChunk<Chunk_t,FreeList_t>* tc) {
duke@435 78 // This first free chunk in the list will be the tree list.
jmasa@4196 79 assert((tc->size() >= (TreeChunk<Chunk_t, FreeList_t>::min_size())),
jmasa@4196 80 "Chunk is too small for a TreeChunk");
jmasa@4196 81 TreeList<Chunk_t, FreeList_t>* tl = tc->embedded_list();
jmasa@4196 82 tl->initialize();
duke@435 83 tc->set_list(tl);
duke@435 84 tl->set_size(tc->size());
duke@435 85 tl->link_head(tc);
duke@435 86 tl->link_tail(tc);
duke@435 87 tl->set_count(1);
jmasa@4382 88 assert(tl->parent() == NULL, "Should be clear");
duke@435 89 return tl;
duke@435 90 }
ysr@1580 91
jmasa@4196 92
jmasa@4196 93 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 94 TreeList<Chunk_t, FreeList_t>*
jmasa@4196 95 get_chunk(size_t size, enum FreeBlockDictionary<Chunk_t>::Dither dither) {
jmasa@4196 96 FreeBlockDictionary<Chunk_t>::verify_par_locked();
jmasa@4196 97 Chunk_t* res = get_chunk_from_tree(size, dither);
jmasa@4196 98 assert(res == NULL || res->is_free(),
jmasa@4196 99 "Should be returning a free chunk");
jmasa@4196 100 assert(dither != FreeBlockDictionary<Chunk_t>::exactly ||
jmasa@4196 101 res->size() == size, "Not correct size");
jmasa@4196 102 return res;
jmasa@4196 103 }
jmasa@4196 104
jmasa@4196 105 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 106 TreeList<Chunk_t, FreeList_t>*
jmasa@4196 107 TreeList<Chunk_t, FreeList_t>::as_TreeList(HeapWord* addr, size_t size) {
jmasa@4196 108 TreeChunk<Chunk_t, FreeList_t>* tc = (TreeChunk<Chunk_t, FreeList_t>*) addr;
jmasa@4196 109 assert((size >= TreeChunk<Chunk_t, FreeList_t>::min_size()),
jmasa@4196 110 "Chunk is too small for a TreeChunk");
jmasa@4196 111 // The space will have been mangled initially but
jmasa@4196 112 // is not remangled when a Chunk_t is returned to the free list
jmasa@698 113 // (since it is used to maintain the chunk on the free list).
jmasa@4196 114 tc->assert_is_mangled();
jmasa@3732 115 tc->set_size(size);
jmasa@3732 116 tc->link_prev(NULL);
jmasa@3732 117 tc->link_next(NULL);
jmasa@4196 118 TreeList<Chunk_t, FreeList_t>* tl = TreeList<Chunk_t, FreeList_t>::as_TreeList(tc);
duke@435 119 return tl;
duke@435 120 }
duke@435 121
duke@435 122
jprovino@4542 123 #if INCLUDE_ALL_GCS
jmasa@4196 124 // Specialize for AdaptiveFreeList which tries to avoid
jmasa@4196 125 // splitting a chunk of a size that is under populated in favor of
jmasa@4196 126 // an over populated size. The general get_better_list() just returns
jmasa@4196 127 // the current list.
jmasa@4196 128 template <>
jmasa@4196 129 TreeList<FreeChunk, AdaptiveFreeList>*
jmasa@4196 130 TreeList<FreeChunk, AdaptiveFreeList>::get_better_list(
jmasa@4196 131 BinaryTreeDictionary<FreeChunk, ::AdaptiveFreeList>* dictionary) {
jmasa@4196 132 // A candidate chunk has been found. If it is already under
jmasa@4196 133 // populated, get a chunk associated with the hint for this
jmasa@4196 134 // chunk.
jmasa@4196 135
jmasa@4196 136 TreeList<FreeChunk, ::AdaptiveFreeList>* curTL = this;
jmasa@4196 137 if (surplus() <= 0) {
jmasa@4196 138 /* Use the hint to find a size with a surplus, and reset the hint. */
jmasa@4196 139 TreeList<FreeChunk, ::AdaptiveFreeList>* hintTL = this;
jmasa@4196 140 while (hintTL->hint() != 0) {
jmasa@4196 141 assert(hintTL->hint() > hintTL->size(),
jmasa@4196 142 "hint points in the wrong direction");
jmasa@4196 143 hintTL = dictionary->find_list(hintTL->hint());
jmasa@4196 144 assert(curTL != hintTL, "Infinite loop");
jmasa@4196 145 if (hintTL == NULL ||
jmasa@4196 146 hintTL == curTL /* Should not happen but protect against it */ ) {
jmasa@4196 147 // No useful hint. Set the hint to NULL and go on.
jmasa@4196 148 curTL->set_hint(0);
jmasa@4196 149 break;
jmasa@4196 150 }
jmasa@4196 151 assert(hintTL->size() > curTL->size(), "hint is inconsistent");
jmasa@4196 152 if (hintTL->surplus() > 0) {
jmasa@4196 153 // The hint led to a list that has a surplus. Use it.
jmasa@4196 154 // Set the hint for the candidate to an overpopulated
jmasa@4196 155 // size.
jmasa@4196 156 curTL->set_hint(hintTL->size());
jmasa@4196 157 // Change the candidate.
jmasa@4196 158 curTL = hintTL;
jmasa@4196 159 break;
jmasa@4196 160 }
jmasa@4196 161 }
jmasa@4196 162 }
jmasa@4196 163 return curTL;
jmasa@4196 164 }
jprovino@4542 165 #endif // INCLUDE_ALL_GCS
jmasa@4196 166
jmasa@4196 167 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 168 TreeList<Chunk_t, FreeList_t>*
jmasa@4196 169 TreeList<Chunk_t, FreeList_t>::get_better_list(
jmasa@4196 170 BinaryTreeDictionary<Chunk_t, FreeList_t>* dictionary) {
jmasa@4196 171 return this;
jmasa@4196 172 }
jmasa@4196 173
jmasa@4196 174 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 175 TreeList<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::remove_chunk_replace_if_needed(TreeChunk<Chunk_t, FreeList_t>* tc) {
jmasa@4196 176
jmasa@4196 177 TreeList<Chunk_t, FreeList_t>* retTL = this;
jmasa@4196 178 Chunk_t* list = head();
duke@435 179 assert(!list || list != list->next(), "Chunk on list twice");
duke@435 180 assert(tc != NULL, "Chunk being removed is NULL");
duke@435 181 assert(parent() == NULL || this == parent()->left() ||
duke@435 182 this == parent()->right(), "list is inconsistent");
jmasa@3732 183 assert(tc->is_free(), "Header is not marked correctly");
duke@435 184 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 185 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 186
jmasa@4196 187 Chunk_t* prevFC = tc->prev();
jmasa@4196 188 TreeChunk<Chunk_t, FreeList_t>* nextTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(tc->next());
duke@435 189 assert(list != NULL, "should have at least the target chunk");
duke@435 190
duke@435 191 // Is this the first item on the list?
duke@435 192 if (tc == list) {
jmasa@4196 193 // The "getChunk..." functions for a TreeList<Chunk_t, FreeList_t> will not return the
duke@435 194 // first chunk in the list unless it is the last chunk in the list
duke@435 195 // because the first chunk is also acting as the tree node.
duke@435 196 // When coalescing happens, however, the first chunk in the a tree
duke@435 197 // list can be the start of a free range. Free ranges are removed
duke@435 198 // from the free lists so that they are not available to be
duke@435 199 // allocated when the sweeper yields (giving up the free list lock)
duke@435 200 // to allow mutator activity. If this chunk is the first in the
duke@435 201 // list and is not the last in the list, do the work to copy the
jmasa@4196 202 // TreeList<Chunk_t, FreeList_t> from the first chunk to the next chunk and update all
jmasa@4196 203 // the TreeList<Chunk_t, FreeList_t> pointers in the chunks in the list.
duke@435 204 if (nextTC == NULL) {
jcoomes@1844 205 assert(prevFC == NULL, "Not last chunk in the list");
duke@435 206 set_tail(NULL);
duke@435 207 set_head(NULL);
duke@435 208 } else {
duke@435 209 // copy embedded list.
duke@435 210 nextTC->set_embedded_list(tc->embedded_list());
duke@435 211 retTL = nextTC->embedded_list();
duke@435 212 // Fix the pointer to the list in each chunk in the list.
duke@435 213 // This can be slow for a long list. Consider having
duke@435 214 // an option that does not allow the first chunk on the
duke@435 215 // list to be coalesced.
jmasa@4196 216 for (TreeChunk<Chunk_t, FreeList_t>* curTC = nextTC; curTC != NULL;
jmasa@4196 217 curTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(curTC->next())) {
duke@435 218 curTC->set_list(retTL);
duke@435 219 }
jmasa@4196 220 // Fix the parent to point to the new TreeList<Chunk_t, FreeList_t>.
duke@435 221 if (retTL->parent() != NULL) {
duke@435 222 if (this == retTL->parent()->left()) {
jmasa@3732 223 retTL->parent()->set_left(retTL);
duke@435 224 } else {
duke@435 225 assert(this == retTL->parent()->right(), "Parent is incorrect");
jmasa@3732 226 retTL->parent()->set_right(retTL);
duke@435 227 }
duke@435 228 }
duke@435 229 // Fix the children's parent pointers to point to the
duke@435 230 // new list.
duke@435 231 assert(right() == retTL->right(), "Should have been copied");
duke@435 232 if (retTL->right() != NULL) {
jmasa@3732 233 retTL->right()->set_parent(retTL);
duke@435 234 }
duke@435 235 assert(left() == retTL->left(), "Should have been copied");
duke@435 236 if (retTL->left() != NULL) {
jmasa@3732 237 retTL->left()->set_parent(retTL);
duke@435 238 }
duke@435 239 retTL->link_head(nextTC);
jmasa@3732 240 assert(nextTC->is_free(), "Should be a free chunk");
duke@435 241 }
duke@435 242 } else {
duke@435 243 if (nextTC == NULL) {
duke@435 244 // Removing chunk at tail of list
coleenp@4265 245 this->link_tail(prevFC);
duke@435 246 }
duke@435 247 // Chunk is interior to the list
jmasa@3732 248 prevFC->link_after(nextTC);
duke@435 249 }
duke@435 250
jmasa@4196 251 // Below this point the embeded TreeList<Chunk_t, FreeList_t> being used for the
duke@435 252 // tree node may have changed. Don't use "this"
jmasa@4196 253 // TreeList<Chunk_t, FreeList_t>*.
duke@435 254 // chunk should still be a free chunk (bit set in _prev)
duke@435 255 assert(!retTL->head() || retTL->size() == retTL->head()->size(),
duke@435 256 "Wrong sized chunk in list");
duke@435 257 debug_only(
jmasa@3732 258 tc->link_prev(NULL);
jmasa@3732 259 tc->link_next(NULL);
duke@435 260 tc->set_list(NULL);
duke@435 261 bool prev_found = false;
duke@435 262 bool next_found = false;
jmasa@4196 263 for (Chunk_t* curFC = retTL->head();
duke@435 264 curFC != NULL; curFC = curFC->next()) {
duke@435 265 assert(curFC != tc, "Chunk is still in list");
duke@435 266 if (curFC == prevFC) {
duke@435 267 prev_found = true;
duke@435 268 }
duke@435 269 if (curFC == nextTC) {
duke@435 270 next_found = true;
duke@435 271 }
duke@435 272 }
duke@435 273 assert(prevFC == NULL || prev_found, "Chunk was lost from list");
duke@435 274 assert(nextTC == NULL || next_found, "Chunk was lost from list");
duke@435 275 assert(retTL->parent() == NULL ||
duke@435 276 retTL == retTL->parent()->left() ||
duke@435 277 retTL == retTL->parent()->right(),
duke@435 278 "list is inconsistent");
duke@435 279 )
duke@435 280 retTL->decrement_count();
duke@435 281
jmasa@3732 282 assert(tc->is_free(), "Should still be a free chunk");
duke@435 283 assert(retTL->head() == NULL || retTL->head()->prev() == NULL,
duke@435 284 "list invariant");
duke@435 285 assert(retTL->tail() == NULL || retTL->tail()->next() == NULL,
duke@435 286 "list invariant");
duke@435 287 return retTL;
duke@435 288 }
jmasa@3730 289
jmasa@4196 290 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 291 void TreeList<Chunk_t, FreeList_t>::return_chunk_at_tail(TreeChunk<Chunk_t, FreeList_t>* chunk) {
duke@435 292 assert(chunk != NULL, "returning NULL chunk");
duke@435 293 assert(chunk->list() == this, "list should be set for chunk");
duke@435 294 assert(tail() != NULL, "The tree list is embedded in the first chunk");
duke@435 295 // which means that the list can never be empty.
coleenp@4297 296 assert(!this->verify_chunk_in_free_list(chunk), "Double entry");
duke@435 297 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 298 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 299
jmasa@4196 300 Chunk_t* fc = tail();
jmasa@3732 301 fc->link_after(chunk);
coleenp@4265 302 this->link_tail(chunk);
duke@435 303
duke@435 304 assert(!tail() || size() == tail()->size(), "Wrong sized chunk in list");
jmasa@4196 305 FreeList_t<Chunk_t>::increment_count();
coleenp@4297 306 debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)
duke@435 307 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 308 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 309 }
duke@435 310
duke@435 311 // Add this chunk at the head of the list. "At the head of the list"
duke@435 312 // is defined to be after the chunk pointer to by head(). This is
jmasa@4196 313 // because the TreeList<Chunk_t, FreeList_t> is embedded in the first TreeChunk<Chunk_t, FreeList_t> in the
jmasa@4196 314 // list. See the definition of TreeChunk<Chunk_t, FreeList_t>.
jmasa@4196 315 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 316 void TreeList<Chunk_t, FreeList_t>::return_chunk_at_head(TreeChunk<Chunk_t, FreeList_t>* chunk) {
duke@435 317 assert(chunk->list() == this, "list should be set for chunk");
duke@435 318 assert(head() != NULL, "The tree list is embedded in the first chunk");
duke@435 319 assert(chunk != NULL, "returning NULL chunk");
coleenp@4297 320 assert(!this->verify_chunk_in_free_list(chunk), "Double entry");
duke@435 321 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 322 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 323
jmasa@4196 324 Chunk_t* fc = head()->next();
duke@435 325 if (fc != NULL) {
jmasa@3732 326 chunk->link_after(fc);
duke@435 327 } else {
duke@435 328 assert(tail() == NULL, "List is inconsistent");
coleenp@4265 329 this->link_tail(chunk);
duke@435 330 }
jmasa@3732 331 head()->link_after(chunk);
duke@435 332 assert(!head() || size() == head()->size(), "Wrong sized chunk in list");
jmasa@4196 333 FreeList_t<Chunk_t>::increment_count();
coleenp@4297 334 debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)
duke@435 335 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 336 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 337 }
duke@435 338
jmasa@4196 339 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 340 void TreeChunk<Chunk_t, FreeList_t>::assert_is_mangled() const {
jmasa@4196 341 assert((ZapUnusedHeapArea &&
jmasa@4196 342 SpaceMangler::is_mangled((HeapWord*) Chunk_t::size_addr()) &&
jmasa@4196 343 SpaceMangler::is_mangled((HeapWord*) Chunk_t::prev_addr()) &&
jmasa@4196 344 SpaceMangler::is_mangled((HeapWord*) Chunk_t::next_addr())) ||
jmasa@4196 345 (size() == 0 && prev() == NULL && next() == NULL),
jmasa@4196 346 "Space should be clear or mangled");
duke@435 347 }
duke@435 348
jmasa@4196 349 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 350 TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::head_as_TreeChunk() {
jmasa@4196 351 assert(head() == NULL || (TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(head())->list() == this),
jmasa@4196 352 "Wrong type of chunk?");
jmasa@4196 353 return TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(head());
jmasa@4196 354 }
jmasa@4196 355
jmasa@4196 356 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 357 TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::first_available() {
ysr@2132 358 assert(head() != NULL, "The head of the list cannot be NULL");
jmasa@4196 359 Chunk_t* fc = head()->next();
jmasa@4196 360 TreeChunk<Chunk_t, FreeList_t>* retTC;
duke@435 361 if (fc == NULL) {
duke@435 362 retTC = head_as_TreeChunk();
duke@435 363 } else {
jmasa@4196 364 retTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(fc);
duke@435 365 }
duke@435 366 assert(retTC->list() == this, "Wrong type of chunk.");
duke@435 367 return retTC;
duke@435 368 }
duke@435 369
ysr@1580 370 // Returns the block with the largest heap address amongst
ysr@1580 371 // those in the list for this size; potentially slow and expensive,
ysr@1580 372 // use with caution!
jmasa@4196 373 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 374 TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::largest_address() {
ysr@2132 375 assert(head() != NULL, "The head of the list cannot be NULL");
jmasa@4196 376 Chunk_t* fc = head()->next();
jmasa@4196 377 TreeChunk<Chunk_t, FreeList_t>* retTC;
ysr@1580 378 if (fc == NULL) {
ysr@1580 379 retTC = head_as_TreeChunk();
ysr@1580 380 } else {
ysr@1580 381 // walk down the list and return the one with the highest
ysr@1580 382 // heap address among chunks of this size.
jmasa@4196 383 Chunk_t* last = fc;
ysr@1580 384 while (fc->next() != NULL) {
ysr@1580 385 if ((HeapWord*)last < (HeapWord*)fc) {
ysr@1580 386 last = fc;
ysr@1580 387 }
ysr@1580 388 fc = fc->next();
ysr@1580 389 }
jmasa@4196 390 retTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(last);
ysr@1580 391 }
ysr@1580 392 assert(retTC->list() == this, "Wrong type of chunk.");
ysr@1580 393 return retTC;
ysr@1580 394 }
ysr@1580 395
jmasa@4196 396 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 397 BinaryTreeDictionary<Chunk_t, FreeList_t>::BinaryTreeDictionary(MemRegion mr) {
jmasa@4196 398 assert((mr.byte_size() > min_size()), "minimum chunk size");
duke@435 399
duke@435 400 reset(mr);
duke@435 401 assert(root()->left() == NULL, "reset check failed");
duke@435 402 assert(root()->right() == NULL, "reset check failed");
duke@435 403 assert(root()->head()->next() == NULL, "reset check failed");
duke@435 404 assert(root()->head()->prev() == NULL, "reset check failed");
jmasa@3732 405 assert(total_size() == root()->size(), "reset check failed");
jmasa@3732 406 assert(total_free_blocks() == 1, "reset check failed");
duke@435 407 }
duke@435 408
jmasa@4196 409 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 410 void BinaryTreeDictionary<Chunk_t, FreeList_t>::inc_total_size(size_t inc) {
jmasa@3732 411 _total_size = _total_size + inc;
duke@435 412 }
duke@435 413
jmasa@4196 414 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 415 void BinaryTreeDictionary<Chunk_t, FreeList_t>::dec_total_size(size_t dec) {
jmasa@3732 416 _total_size = _total_size - dec;
duke@435 417 }
duke@435 418
jmasa@4196 419 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 420 void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset(MemRegion mr) {
jmasa@4196 421 assert((mr.byte_size() > min_size()), "minimum chunk size");
jmasa@4196 422 set_root(TreeList<Chunk_t, FreeList_t>::as_TreeList(mr.start(), mr.word_size()));
jmasa@3732 423 set_total_size(mr.word_size());
jmasa@3732 424 set_total_free_blocks(1);
duke@435 425 }
duke@435 426
jmasa@4196 427 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 428 void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset(HeapWord* addr, size_t byte_size) {
duke@435 429 MemRegion mr(addr, heap_word_size(byte_size));
duke@435 430 reset(mr);
duke@435 431 }
duke@435 432
jmasa@4196 433 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 434 void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset() {
duke@435 435 set_root(NULL);
jmasa@3732 436 set_total_size(0);
jmasa@3732 437 set_total_free_blocks(0);
duke@435 438 }
duke@435 439
duke@435 440 // Get a free block of size at least size from tree, or NULL.
jmasa@4196 441 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 442 TreeChunk<Chunk_t, FreeList_t>*
jmasa@4196 443 BinaryTreeDictionary<Chunk_t, FreeList_t>::get_chunk_from_tree(
jmasa@4196 444 size_t size,
jmasa@4196 445 enum FreeBlockDictionary<Chunk_t>::Dither dither)
duke@435 446 {
jmasa@4196 447 TreeList<Chunk_t, FreeList_t> *curTL, *prevTL;
jmasa@4196 448 TreeChunk<Chunk_t, FreeList_t>* retTC = NULL;
jmasa@4196 449
jmasa@4196 450 assert((size >= min_size()), "minimum chunk size");
duke@435 451 if (FLSVerifyDictionary) {
jmasa@3732 452 verify_tree();
duke@435 453 }
duke@435 454 // starting at the root, work downwards trying to find match.
duke@435 455 // Remember the last node of size too great or too small.
duke@435 456 for (prevTL = curTL = root(); curTL != NULL;) {
duke@435 457 if (curTL->size() == size) { // exact match
duke@435 458 break;
duke@435 459 }
duke@435 460 prevTL = curTL;
duke@435 461 if (curTL->size() < size) { // proceed to right sub-tree
duke@435 462 curTL = curTL->right();
duke@435 463 } else { // proceed to left sub-tree
duke@435 464 assert(curTL->size() > size, "size inconsistency");
duke@435 465 curTL = curTL->left();
duke@435 466 }
duke@435 467 }
duke@435 468 if (curTL == NULL) { // couldn't find exact match
jmasa@3730 469
jmasa@4196 470 if (dither == FreeBlockDictionary<Chunk_t>::exactly) return NULL;
jmasa@3730 471
duke@435 472 // try and find the next larger size by walking back up the search path
duke@435 473 for (curTL = prevTL; curTL != NULL;) {
duke@435 474 if (curTL->size() >= size) break;
duke@435 475 else curTL = curTL->parent();
duke@435 476 }
duke@435 477 assert(curTL == NULL || curTL->count() > 0,
duke@435 478 "An empty list should not be in the tree");
duke@435 479 }
duke@435 480 if (curTL != NULL) {
duke@435 481 assert(curTL->size() >= size, "size inconsistency");
duke@435 482
jmasa@4196 483 curTL = curTL->get_better_list(this);
jmasa@4196 484
duke@435 485 retTC = curTL->first_available();
duke@435 486 assert((retTC != NULL) && (curTL->count() > 0),
duke@435 487 "A list in the binary tree should not be NULL");
duke@435 488 assert(retTC->size() >= size,
duke@435 489 "A chunk of the wrong size was found");
jmasa@3732 490 remove_chunk_from_tree(retTC);
jmasa@3732 491 assert(retTC->is_free(), "Header is not marked correctly");
duke@435 492 }
duke@435 493
duke@435 494 if (FLSVerifyDictionary) {
duke@435 495 verify();
duke@435 496 }
duke@435 497 return retTC;
duke@435 498 }
duke@435 499
jmasa@4196 500 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 501 TreeList<Chunk_t, FreeList_t>* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_list(size_t size) const {
jmasa@4196 502 TreeList<Chunk_t, FreeList_t>* curTL;
duke@435 503 for (curTL = root(); curTL != NULL;) {
duke@435 504 if (curTL->size() == size) { // exact match
duke@435 505 break;
duke@435 506 }
duke@435 507
duke@435 508 if (curTL->size() < size) { // proceed to right sub-tree
duke@435 509 curTL = curTL->right();
duke@435 510 } else { // proceed to left sub-tree
duke@435 511 assert(curTL->size() > size, "size inconsistency");
duke@435 512 curTL = curTL->left();
duke@435 513 }
duke@435 514 }
duke@435 515 return curTL;
duke@435 516 }
duke@435 517
duke@435 518
jmasa@4196 519 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 520 bool BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_chunk_in_free_list(Chunk_t* tc) const {
duke@435 521 size_t size = tc->size();
jmasa@4196 522 TreeList<Chunk_t, FreeList_t>* tl = find_list(size);
duke@435 523 if (tl == NULL) {
duke@435 524 return false;
duke@435 525 } else {
jmasa@3732 526 return tl->verify_chunk_in_free_list(tc);
duke@435 527 }
duke@435 528 }
duke@435 529
jmasa@4196 530 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 531 Chunk_t* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_largest_dict() const {
jmasa@4196 532 TreeList<Chunk_t, FreeList_t> *curTL = root();
duke@435 533 if (curTL != NULL) {
duke@435 534 while(curTL->right() != NULL) curTL = curTL->right();
ysr@1580 535 return curTL->largest_address();
duke@435 536 } else {
duke@435 537 return NULL;
duke@435 538 }
duke@435 539 }
duke@435 540
duke@435 541 // Remove the current chunk from the tree. If it is not the last
duke@435 542 // chunk in a list on a tree node, just unlink it.
duke@435 543 // If it is the last chunk in the list (the next link is NULL),
duke@435 544 // remove the node and repair the tree.
jmasa@4196 545 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 546 TreeChunk<Chunk_t, FreeList_t>*
jmasa@4196 547 BinaryTreeDictionary<Chunk_t, FreeList_t>::remove_chunk_from_tree(TreeChunk<Chunk_t, FreeList_t>* tc) {
duke@435 548 assert(tc != NULL, "Should not call with a NULL chunk");
jmasa@3732 549 assert(tc->is_free(), "Header is not marked correctly");
duke@435 550
jmasa@4196 551 TreeList<Chunk_t, FreeList_t> *newTL, *parentTL;
jmasa@4196 552 TreeChunk<Chunk_t, FreeList_t>* retTC;
jmasa@4196 553 TreeList<Chunk_t, FreeList_t>* tl = tc->list();
duke@435 554 debug_only(
duke@435 555 bool removing_only_chunk = false;
duke@435 556 if (tl == _root) {
duke@435 557 if ((_root->left() == NULL) && (_root->right() == NULL)) {
duke@435 558 if (_root->count() == 1) {
duke@435 559 assert(_root->head() == tc, "Should only be this one chunk");
duke@435 560 removing_only_chunk = true;
duke@435 561 }
duke@435 562 }
duke@435 563 }
duke@435 564 )
duke@435 565 assert(tl != NULL, "List should be set");
duke@435 566 assert(tl->parent() == NULL || tl == tl->parent()->left() ||
duke@435 567 tl == tl->parent()->right(), "list is inconsistent");
duke@435 568
jmasa@3732 569 bool complicated_splice = false;
duke@435 570
duke@435 571 retTC = tc;
duke@435 572 // Removing this chunk can have the side effect of changing the node
jmasa@4196 573 // (TreeList<Chunk_t, FreeList_t>*) in the tree. If the node is the root, update it.
jmasa@4196 574 TreeList<Chunk_t, FreeList_t>* replacementTL = tl->remove_chunk_replace_if_needed(tc);
jmasa@3732 575 assert(tc->is_free(), "Chunk should still be free");
duke@435 576 assert(replacementTL->parent() == NULL ||
duke@435 577 replacementTL == replacementTL->parent()->left() ||
duke@435 578 replacementTL == replacementTL->parent()->right(),
duke@435 579 "list is inconsistent");
duke@435 580 if (tl == root()) {
duke@435 581 assert(replacementTL->parent() == NULL, "Incorrectly replacing root");
duke@435 582 set_root(replacementTL);
duke@435 583 }
jmasa@4196 584 #ifdef ASSERT
duke@435 585 if (tl != replacementTL) {
duke@435 586 assert(replacementTL->head() != NULL,
duke@435 587 "If the tree list was replaced, it should not be a NULL list");
jmasa@4196 588 TreeList<Chunk_t, FreeList_t>* rhl = replacementTL->head_as_TreeChunk()->list();
jmasa@4196 589 TreeList<Chunk_t, FreeList_t>* rtl =
jmasa@4196 590 TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(replacementTL->tail())->list();
duke@435 591 assert(rhl == replacementTL, "Broken head");
duke@435 592 assert(rtl == replacementTL, "Broken tail");
duke@435 593 assert(replacementTL->size() == tc->size(), "Broken size");
duke@435 594 }
jmasa@4196 595 #endif
duke@435 596
duke@435 597 // Does the tree need to be repaired?
duke@435 598 if (replacementTL->count() == 0) {
duke@435 599 assert(replacementTL->head() == NULL &&
duke@435 600 replacementTL->tail() == NULL, "list count is incorrect");
duke@435 601 // Find the replacement node for the (soon to be empty) node being removed.
duke@435 602 // if we have a single (or no) child, splice child in our stead
duke@435 603 if (replacementTL->left() == NULL) {
duke@435 604 // left is NULL so pick right. right may also be NULL.
duke@435 605 newTL = replacementTL->right();
jmasa@3732 606 debug_only(replacementTL->clear_right();)
duke@435 607 } else if (replacementTL->right() == NULL) {
duke@435 608 // right is NULL
duke@435 609 newTL = replacementTL->left();
jmasa@4196 610 debug_only(replacementTL->clear_left();)
duke@435 611 } else { // we have both children, so, by patriarchal convention,
duke@435 612 // my replacement is least node in right sub-tree
jmasa@3732 613 complicated_splice = true;
jmasa@3732 614 newTL = remove_tree_minimum(replacementTL->right());
duke@435 615 assert(newTL != NULL && newTL->left() == NULL &&
duke@435 616 newTL->right() == NULL, "sub-tree minimum exists");
duke@435 617 }
duke@435 618 // newTL is the replacement for the (soon to be empty) node.
duke@435 619 // newTL may be NULL.
duke@435 620 // should verify; we just cleanly excised our replacement
duke@435 621 if (FLSVerifyDictionary) {
jmasa@3732 622 verify_tree();
duke@435 623 }
duke@435 624 // first make newTL my parent's child
duke@435 625 if ((parentTL = replacementTL->parent()) == NULL) {
duke@435 626 // newTL should be root
duke@435 627 assert(tl == root(), "Incorrectly replacing root");
duke@435 628 set_root(newTL);
duke@435 629 if (newTL != NULL) {
jmasa@3732 630 newTL->clear_parent();
duke@435 631 }
duke@435 632 } else if (parentTL->right() == replacementTL) {
duke@435 633 // replacementTL is a right child
jmasa@3732 634 parentTL->set_right(newTL);
duke@435 635 } else { // replacementTL is a left child
duke@435 636 assert(parentTL->left() == replacementTL, "should be left child");
jmasa@3732 637 parentTL->set_left(newTL);
duke@435 638 }
jmasa@3732 639 debug_only(replacementTL->clear_parent();)
jmasa@3732 640 if (complicated_splice) { // we need newTL to get replacementTL's
duke@435 641 // two children
duke@435 642 assert(newTL != NULL &&
duke@435 643 newTL->left() == NULL && newTL->right() == NULL,
duke@435 644 "newTL should not have encumbrances from the past");
duke@435 645 // we'd like to assert as below:
duke@435 646 // assert(replacementTL->left() != NULL && replacementTL->right() != NULL,
jmasa@3732 647 // "else !complicated_splice");
duke@435 648 // ... however, the above assertion is too strong because we aren't
duke@435 649 // guaranteed that replacementTL->right() is still NULL.
duke@435 650 // Recall that we removed
duke@435 651 // the right sub-tree minimum from replacementTL.
duke@435 652 // That may well have been its right
duke@435 653 // child! So we'll just assert half of the above:
jmasa@3732 654 assert(replacementTL->left() != NULL, "else !complicated_splice");
jmasa@3732 655 newTL->set_left(replacementTL->left());
jmasa@3732 656 newTL->set_right(replacementTL->right());
duke@435 657 debug_only(
jmasa@3732 658 replacementTL->clear_right();
jmasa@4196 659 replacementTL->clear_left();
duke@435 660 )
duke@435 661 }
duke@435 662 assert(replacementTL->right() == NULL &&
duke@435 663 replacementTL->left() == NULL &&
duke@435 664 replacementTL->parent() == NULL,
duke@435 665 "delete without encumbrances");
duke@435 666 }
duke@435 667
jmasa@3732 668 assert(total_size() >= retTC->size(), "Incorrect total size");
jmasa@3732 669 dec_total_size(retTC->size()); // size book-keeping
jmasa@3732 670 assert(total_free_blocks() > 0, "Incorrect total count");
jmasa@3732 671 set_total_free_blocks(total_free_blocks() - 1);
duke@435 672
duke@435 673 assert(retTC != NULL, "null chunk?");
duke@435 674 assert(retTC->prev() == NULL && retTC->next() == NULL,
duke@435 675 "should return without encumbrances");
duke@435 676 if (FLSVerifyDictionary) {
jmasa@3732 677 verify_tree();
duke@435 678 }
duke@435 679 assert(!removing_only_chunk || _root == NULL, "root should be NULL");
jmasa@4196 680 return TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(retTC);
duke@435 681 }
duke@435 682
duke@435 683 // Remove the leftmost node (lm) in the tree and return it.
duke@435 684 // If lm has a right child, link it to the left node of
duke@435 685 // the parent of lm.
jmasa@4196 686 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 687 TreeList<Chunk_t, FreeList_t>* BinaryTreeDictionary<Chunk_t, FreeList_t>::remove_tree_minimum(TreeList<Chunk_t, FreeList_t>* tl) {
duke@435 688 assert(tl != NULL && tl->parent() != NULL, "really need a proper sub-tree");
duke@435 689 // locate the subtree minimum by walking down left branches
jmasa@4196 690 TreeList<Chunk_t, FreeList_t>* curTL = tl;
duke@435 691 for (; curTL->left() != NULL; curTL = curTL->left());
duke@435 692 // obviously curTL now has at most one child, a right child
duke@435 693 if (curTL != root()) { // Should this test just be removed?
jmasa@4196 694 TreeList<Chunk_t, FreeList_t>* parentTL = curTL->parent();
duke@435 695 if (parentTL->left() == curTL) { // curTL is a left child
jmasa@3732 696 parentTL->set_left(curTL->right());
duke@435 697 } else {
duke@435 698 // If the list tl has no left child, then curTL may be
duke@435 699 // the right child of parentTL.
duke@435 700 assert(parentTL->right() == curTL, "should be a right child");
jmasa@3732 701 parentTL->set_right(curTL->right());
duke@435 702 }
duke@435 703 } else {
duke@435 704 // The only use of this method would not pass the root of the
duke@435 705 // tree (as indicated by the assertion above that the tree list
duke@435 706 // has a parent) but the specification does not explicitly exclude the
duke@435 707 // passing of the root so accomodate it.
duke@435 708 set_root(NULL);
duke@435 709 }
duke@435 710 debug_only(
jmasa@3732 711 curTL->clear_parent(); // Test if this needs to be cleared
jmasa@3732 712 curTL->clear_right(); // recall, above, left child is already null
duke@435 713 )
duke@435 714 // we just excised a (non-root) node, we should still verify all tree invariants
duke@435 715 if (FLSVerifyDictionary) {
jmasa@3732 716 verify_tree();
duke@435 717 }
duke@435 718 return curTL;
duke@435 719 }
duke@435 720
jmasa@4196 721 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 722 void BinaryTreeDictionary<Chunk_t, FreeList_t>::insert_chunk_in_tree(Chunk_t* fc) {
jmasa@4196 723 TreeList<Chunk_t, FreeList_t> *curTL, *prevTL;
duke@435 724 size_t size = fc->size();
duke@435 725
jmasa@4196 726 assert((size >= min_size()),
jmasa@4196 727 err_msg(SIZE_FORMAT " is too small to be a TreeChunk<Chunk_t, FreeList_t> " SIZE_FORMAT,
jmasa@4196 728 size, min_size()));
duke@435 729 if (FLSVerifyDictionary) {
jmasa@3732 730 verify_tree();
duke@435 731 }
duke@435 732
jmasa@3732 733 fc->clear_next();
jmasa@3732 734 fc->link_prev(NULL);
duke@435 735
duke@435 736 // work down from the _root, looking for insertion point
duke@435 737 for (prevTL = curTL = root(); curTL != NULL;) {
duke@435 738 if (curTL->size() == size) // exact match
duke@435 739 break;
duke@435 740 prevTL = curTL;
duke@435 741 if (curTL->size() > size) { // follow left branch
duke@435 742 curTL = curTL->left();
duke@435 743 } else { // follow right branch
duke@435 744 assert(curTL->size() < size, "size inconsistency");
duke@435 745 curTL = curTL->right();
duke@435 746 }
duke@435 747 }
jmasa@4196 748 TreeChunk<Chunk_t, FreeList_t>* tc = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(fc);
ysr@1580 749 // This chunk is being returned to the binary tree. Its embedded
jmasa@4196 750 // TreeList<Chunk_t, FreeList_t> should be unused at this point.
duke@435 751 tc->initialize();
duke@435 752 if (curTL != NULL) { // exact match
duke@435 753 tc->set_list(curTL);
jmasa@3732 754 curTL->return_chunk_at_tail(tc);
duke@435 755 } else { // need a new node in tree
jmasa@3732 756 tc->clear_next();
jmasa@3732 757 tc->link_prev(NULL);
jmasa@4196 758 TreeList<Chunk_t, FreeList_t>* newTL = TreeList<Chunk_t, FreeList_t>::as_TreeList(tc);
jmasa@4196 759 assert(((TreeChunk<Chunk_t, FreeList_t>*)tc)->list() == newTL,
duke@435 760 "List was not initialized correctly");
duke@435 761 if (prevTL == NULL) { // we are the only tree node
duke@435 762 assert(root() == NULL, "control point invariant");
duke@435 763 set_root(newTL);
duke@435 764 } else { // insert under prevTL ...
duke@435 765 if (prevTL->size() < size) { // am right child
duke@435 766 assert(prevTL->right() == NULL, "control point invariant");
jmasa@3732 767 prevTL->set_right(newTL);
duke@435 768 } else { // am left child
duke@435 769 assert(prevTL->size() > size && prevTL->left() == NULL, "cpt pt inv");
jmasa@3732 770 prevTL->set_left(newTL);
duke@435 771 }
duke@435 772 }
duke@435 773 }
duke@435 774 assert(tc->list() != NULL, "Tree list should be set");
duke@435 775
jmasa@3732 776 inc_total_size(size);
jmasa@3732 777 // Method 'total_size_in_tree' walks through the every block in the
duke@435 778 // tree, so it can cause significant performance loss if there are
duke@435 779 // many blocks in the tree
jmasa@3732 780 assert(!FLSVerifyDictionary || total_size_in_tree(root()) == total_size(), "_total_size inconsistency");
jmasa@3732 781 set_total_free_blocks(total_free_blocks() + 1);
duke@435 782 if (FLSVerifyDictionary) {
jmasa@3732 783 verify_tree();
duke@435 784 }
duke@435 785 }
duke@435 786
jmasa@4196 787 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 788 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::max_chunk_size() const {
jmasa@4196 789 FreeBlockDictionary<Chunk_t>::verify_par_locked();
jmasa@4196 790 TreeList<Chunk_t, FreeList_t>* tc = root();
duke@435 791 if (tc == NULL) return 0;
duke@435 792 for (; tc->right() != NULL; tc = tc->right());
duke@435 793 return tc->size();
duke@435 794 }
duke@435 795
jmasa@4196 796 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 797 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_list_length(TreeList<Chunk_t, FreeList_t>* tl) const {
duke@435 798 size_t res;
duke@435 799 res = tl->count();
duke@435 800 #ifdef ASSERT
duke@435 801 size_t cnt;
jmasa@4196 802 Chunk_t* tc = tl->head();
duke@435 803 for (cnt = 0; tc != NULL; tc = tc->next(), cnt++);
duke@435 804 assert(res == cnt, "The count is not being maintained correctly");
duke@435 805 #endif
duke@435 806 return res;
duke@435 807 }
duke@435 808
jmasa@4196 809 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 810 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_size_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
duke@435 811 if (tl == NULL)
duke@435 812 return 0;
jmasa@3732 813 return (tl->size() * total_list_length(tl)) +
jmasa@3732 814 total_size_in_tree(tl->left()) +
jmasa@3732 815 total_size_in_tree(tl->right());
duke@435 816 }
duke@435 817
jmasa@4196 818 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 819 double BinaryTreeDictionary<Chunk_t, FreeList_t>::sum_of_squared_block_sizes(TreeList<Chunk_t, FreeList_t>* const tl) const {
duke@435 820 if (tl == NULL) {
duke@435 821 return 0.0;
duke@435 822 }
duke@435 823 double size = (double)(tl->size());
jmasa@3732 824 double curr = size * size * total_list_length(tl);
duke@435 825 curr += sum_of_squared_block_sizes(tl->left());
duke@435 826 curr += sum_of_squared_block_sizes(tl->right());
duke@435 827 return curr;
duke@435 828 }
duke@435 829
jmasa@4196 830 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 831 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_free_blocks_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
duke@435 832 if (tl == NULL)
duke@435 833 return 0;
jmasa@3732 834 return total_list_length(tl) +
jmasa@3732 835 total_free_blocks_in_tree(tl->left()) +
jmasa@3732 836 total_free_blocks_in_tree(tl->right());
duke@435 837 }
duke@435 838
jmasa@4196 839 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 840 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::num_free_blocks() const {
jmasa@3732 841 assert(total_free_blocks_in_tree(root()) == total_free_blocks(),
jmasa@3732 842 "_total_free_blocks inconsistency");
jmasa@3732 843 return total_free_blocks();
duke@435 844 }
duke@435 845
jmasa@4196 846 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 847 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::tree_height_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
duke@435 848 if (tl == NULL)
duke@435 849 return 0;
jmasa@3732 850 return 1 + MAX2(tree_height_helper(tl->left()),
jmasa@3732 851 tree_height_helper(tl->right()));
duke@435 852 }
duke@435 853
jmasa@4196 854 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 855 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::tree_height() const {
jmasa@3732 856 return tree_height_helper(root());
duke@435 857 }
duke@435 858
jmasa@4196 859 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 860 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_nodes_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
duke@435 861 if (tl == NULL) {
duke@435 862 return 0;
duke@435 863 }
jmasa@3732 864 return 1 + total_nodes_helper(tl->left()) +
jmasa@3732 865 total_nodes_helper(tl->right());
duke@435 866 }
duke@435 867
jmasa@4196 868 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 869 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_nodes_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
jmasa@3732 870 return total_nodes_helper(root());
duke@435 871 }
duke@435 872
jmasa@4196 873 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 874 void BinaryTreeDictionary<Chunk_t, FreeList_t>::dict_census_update(size_t size, bool split, bool birth){}
jmasa@4196 875
jprovino@4542 876 #if INCLUDE_ALL_GCS
jmasa@4196 877 template <>
jmasa@4488 878 void AFLBinaryTreeDictionary::dict_census_update(size_t size, bool split, bool birth){
jmasa@4196 879 TreeList<FreeChunk, AdaptiveFreeList>* nd = find_list(size);
duke@435 880 if (nd) {
duke@435 881 if (split) {
duke@435 882 if (birth) {
jmasa@3732 883 nd->increment_split_births();
duke@435 884 nd->increment_surplus();
duke@435 885 } else {
jmasa@3732 886 nd->increment_split_deaths();
duke@435 887 nd->decrement_surplus();
duke@435 888 }
duke@435 889 } else {
duke@435 890 if (birth) {
jmasa@3732 891 nd->increment_coal_births();
duke@435 892 nd->increment_surplus();
duke@435 893 } else {
jmasa@3732 894 nd->increment_coal_deaths();
duke@435 895 nd->decrement_surplus();
duke@435 896 }
duke@435 897 }
duke@435 898 }
duke@435 899 // A list for this size may not be found (nd == 0) if
duke@435 900 // This is a death where the appropriate list is now
duke@435 901 // empty and has been removed from the list.
duke@435 902 // This is a birth associated with a LinAB. The chunk
duke@435 903 // for the LinAB is not in the dictionary.
duke@435 904 }
jprovino@4542 905 #endif // INCLUDE_ALL_GCS
duke@435 906
jmasa@4196 907 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 908 bool BinaryTreeDictionary<Chunk_t, FreeList_t>::coal_dict_over_populated(size_t size) {
jmasa@4196 909 // For the general type of freelists, encourage coalescing by
jmasa@4196 910 // returning true.
jmasa@4196 911 return true;
jmasa@4196 912 }
jmasa@4196 913
jprovino@4542 914 #if INCLUDE_ALL_GCS
jmasa@4196 915 template <>
jmasa@4488 916 bool AFLBinaryTreeDictionary::coal_dict_over_populated(size_t size) {
ysr@1580 917 if (FLSAlwaysCoalesceLarge) return true;
ysr@1580 918
jmasa@4196 919 TreeList<FreeChunk, AdaptiveFreeList>* list_of_size = find_list(size);
duke@435 920 // None of requested size implies overpopulated.
jmasa@3732 921 return list_of_size == NULL || list_of_size->coal_desired() <= 0 ||
jmasa@3732 922 list_of_size->count() > list_of_size->coal_desired();
duke@435 923 }
jprovino@4542 924 #endif // INCLUDE_ALL_GCS
duke@435 925
duke@435 926 // Closures for walking the binary tree.
duke@435 927 // do_list() walks the free list in a node applying the closure
duke@435 928 // to each free chunk in the list
duke@435 929 // do_tree() walks the nodes in the binary tree applying do_list()
duke@435 930 // to each list at each node.
duke@435 931
jmasa@4196 932 template <class Chunk_t, template <class> class FreeList_t>
duke@435 933 class TreeCensusClosure : public StackObj {
duke@435 934 protected:
jmasa@4196 935 virtual void do_list(FreeList_t<Chunk_t>* fl) = 0;
duke@435 936 public:
jmasa@4196 937 virtual void do_tree(TreeList<Chunk_t, FreeList_t>* tl) = 0;
duke@435 938 };
duke@435 939
jmasa@4196 940 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 941 class AscendTreeCensusClosure : public TreeCensusClosure<Chunk_t, FreeList_t> {
duke@435 942 public:
jmasa@4196 943 void do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
duke@435 944 if (tl != NULL) {
duke@435 945 do_tree(tl->left());
coleenp@4265 946 this->do_list(tl);
duke@435 947 do_tree(tl->right());
duke@435 948 }
duke@435 949 }
duke@435 950 };
duke@435 951
jmasa@4196 952 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 953 class DescendTreeCensusClosure : public TreeCensusClosure<Chunk_t, FreeList_t> {
duke@435 954 public:
jmasa@4196 955 void do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
duke@435 956 if (tl != NULL) {
duke@435 957 do_tree(tl->right());
coleenp@4265 958 this->do_list(tl);
duke@435 959 do_tree(tl->left());
duke@435 960 }
duke@435 961 }
duke@435 962 };
duke@435 963
duke@435 964 // For each list in the tree, calculate the desired, desired
duke@435 965 // coalesce, count before sweep, and surplus before sweep.
jmasa@4196 966 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 967 class BeginSweepClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
duke@435 968 double _percentage;
duke@435 969 float _inter_sweep_current;
duke@435 970 float _inter_sweep_estimate;
ysr@1580 971 float _intra_sweep_estimate;
duke@435 972
duke@435 973 public:
duke@435 974 BeginSweepClosure(double p, float inter_sweep_current,
ysr@1580 975 float inter_sweep_estimate,
ysr@1580 976 float intra_sweep_estimate) :
duke@435 977 _percentage(p),
duke@435 978 _inter_sweep_current(inter_sweep_current),
ysr@1580 979 _inter_sweep_estimate(inter_sweep_estimate),
ysr@1580 980 _intra_sweep_estimate(intra_sweep_estimate) { }
duke@435 981
jmasa@4196 982 void do_list(FreeList<Chunk_t>* fl) {}
jmasa@4196 983
jprovino@4542 984 #if INCLUDE_ALL_GCS
jmasa@4196 985 void do_list(AdaptiveFreeList<Chunk_t>* fl) {
duke@435 986 double coalSurplusPercent = _percentage;
ysr@1580 987 fl->compute_desired(_inter_sweep_current, _inter_sweep_estimate, _intra_sweep_estimate);
jmasa@3732 988 fl->set_coal_desired((ssize_t)((double)fl->desired() * coalSurplusPercent));
jmasa@3732 989 fl->set_before_sweep(fl->count());
jmasa@3732 990 fl->set_bfr_surp(fl->surplus());
duke@435 991 }
jprovino@4542 992 #endif // INCLUDE_ALL_GCS
duke@435 993 };
duke@435 994
duke@435 995 // Used to search the tree until a condition is met.
duke@435 996 // Similar to TreeCensusClosure but searches the
duke@435 997 // tree and returns promptly when found.
duke@435 998
jmasa@4196 999 template <class Chunk_t, template <class> class FreeList_t>
duke@435 1000 class TreeSearchClosure : public StackObj {
duke@435 1001 protected:
jmasa@4196 1002 virtual bool do_list(FreeList_t<Chunk_t>* fl) = 0;
duke@435 1003 public:
jmasa@4196 1004 virtual bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) = 0;
duke@435 1005 };
duke@435 1006
duke@435 1007 #if 0 // Don't need this yet but here for symmetry.
jmasa@4196 1008 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1009 class AscendTreeSearchClosure : public TreeSearchClosure<Chunk_t> {
duke@435 1010 public:
jmasa@4196 1011 bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
duke@435 1012 if (tl != NULL) {
duke@435 1013 if (do_tree(tl->left())) return true;
duke@435 1014 if (do_list(tl)) return true;
duke@435 1015 if (do_tree(tl->right())) return true;
duke@435 1016 }
duke@435 1017 return false;
duke@435 1018 }
duke@435 1019 };
duke@435 1020 #endif
duke@435 1021
jmasa@4196 1022 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1023 class DescendTreeSearchClosure : public TreeSearchClosure<Chunk_t, FreeList_t> {
duke@435 1024 public:
jmasa@4196 1025 bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
duke@435 1026 if (tl != NULL) {
duke@435 1027 if (do_tree(tl->right())) return true;
coleenp@4265 1028 if (this->do_list(tl)) return true;
duke@435 1029 if (do_tree(tl->left())) return true;
duke@435 1030 }
duke@435 1031 return false;
duke@435 1032 }
duke@435 1033 };
duke@435 1034
duke@435 1035 // Searches the tree for a chunk that ends at the
duke@435 1036 // specified address.
jmasa@4196 1037 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1038 class EndTreeSearchClosure : public DescendTreeSearchClosure<Chunk_t, FreeList_t> {
duke@435 1039 HeapWord* _target;
jmasa@4196 1040 Chunk_t* _found;
duke@435 1041
duke@435 1042 public:
duke@435 1043 EndTreeSearchClosure(HeapWord* target) : _target(target), _found(NULL) {}
jmasa@4196 1044 bool do_list(FreeList_t<Chunk_t>* fl) {
jmasa@4196 1045 Chunk_t* item = fl->head();
duke@435 1046 while (item != NULL) {
jmasa@4196 1047 if (item->end() == (uintptr_t*) _target) {
duke@435 1048 _found = item;
duke@435 1049 return true;
duke@435 1050 }
duke@435 1051 item = item->next();
duke@435 1052 }
duke@435 1053 return false;
duke@435 1054 }
jmasa@4196 1055 Chunk_t* found() { return _found; }
duke@435 1056 };
duke@435 1057
jmasa@4196 1058 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1059 Chunk_t* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_chunk_ends_at(HeapWord* target) const {
jmasa@4196 1060 EndTreeSearchClosure<Chunk_t, FreeList_t> etsc(target);
duke@435 1061 bool found_target = etsc.do_tree(root());
duke@435 1062 assert(found_target || etsc.found() == NULL, "Consistency check");
duke@435 1063 assert(!found_target || etsc.found() != NULL, "Consistency check");
duke@435 1064 return etsc.found();
duke@435 1065 }
duke@435 1066
jmasa@4196 1067 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1068 void BinaryTreeDictionary<Chunk_t, FreeList_t>::begin_sweep_dict_census(double coalSurplusPercent,
ysr@1580 1069 float inter_sweep_current, float inter_sweep_estimate, float intra_sweep_estimate) {
jmasa@4196 1070 BeginSweepClosure<Chunk_t, FreeList_t> bsc(coalSurplusPercent, inter_sweep_current,
ysr@1580 1071 inter_sweep_estimate,
ysr@1580 1072 intra_sweep_estimate);
duke@435 1073 bsc.do_tree(root());
duke@435 1074 }
duke@435 1075
duke@435 1076 // Closures and methods for calculating total bytes returned to the
duke@435 1077 // free lists in the tree.
jmasa@3730 1078 #ifndef PRODUCT
jmasa@4196 1079 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1080 class InitializeDictReturnedBytesClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
duke@435 1081 public:
jmasa@4196 1082 void do_list(FreeList_t<Chunk_t>* fl) {
jmasa@3732 1083 fl->set_returned_bytes(0);
jmasa@3730 1084 }
jmasa@3730 1085 };
duke@435 1086
jmasa@4196 1087 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1088 void BinaryTreeDictionary<Chunk_t, FreeList_t>::initialize_dict_returned_bytes() {
jmasa@4196 1089 InitializeDictReturnedBytesClosure<Chunk_t, FreeList_t> idrb;
jmasa@3730 1090 idrb.do_tree(root());
jmasa@3730 1091 }
jmasa@3730 1092
jmasa@4196 1093 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1094 class ReturnedBytesClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
jmasa@3732 1095 size_t _dict_returned_bytes;
jmasa@3730 1096 public:
jmasa@3732 1097 ReturnedBytesClosure() { _dict_returned_bytes = 0; }
jmasa@4196 1098 void do_list(FreeList_t<Chunk_t>* fl) {
jmasa@3732 1099 _dict_returned_bytes += fl->returned_bytes();
duke@435 1100 }
jmasa@3732 1101 size_t dict_returned_bytes() { return _dict_returned_bytes; }
jmasa@3730 1102 };
duke@435 1103
jmasa@4196 1104 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1105 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::sum_dict_returned_bytes() {
jmasa@4196 1106 ReturnedBytesClosure<Chunk_t, FreeList_t> rbc;
jmasa@3730 1107 rbc.do_tree(root());
duke@435 1108
jmasa@3732 1109 return rbc.dict_returned_bytes();
jmasa@3730 1110 }
duke@435 1111
jmasa@3730 1112 // Count the number of entries in the tree.
jmasa@4196 1113 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1114 class treeCountClosure : public DescendTreeCensusClosure<Chunk_t, FreeList_t> {
jmasa@3730 1115 public:
jmasa@3730 1116 uint count;
jmasa@3730 1117 treeCountClosure(uint c) { count = c; }
jmasa@4196 1118 void do_list(FreeList_t<Chunk_t>* fl) {
jmasa@3730 1119 count++;
duke@435 1120 }
jmasa@3730 1121 };
duke@435 1122
jmasa@4196 1123 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1124 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_count() {
jmasa@4196 1125 treeCountClosure<Chunk_t, FreeList_t> ctc(0);
jmasa@3730 1126 ctc.do_tree(root());
jmasa@3730 1127 return ctc.count;
jmasa@3730 1128 }
jmasa@3730 1129 #endif // PRODUCT
duke@435 1130
duke@435 1131 // Calculate surpluses for the lists in the tree.
jmasa@4196 1132 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1133 class setTreeSurplusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
duke@435 1134 double percentage;
duke@435 1135 public:
duke@435 1136 setTreeSurplusClosure(double v) { percentage = v; }
jmasa@4196 1137 void do_list(FreeList<Chunk_t>* fl) {}
jmasa@4196 1138
jprovino@4542 1139 #if INCLUDE_ALL_GCS
jmasa@4196 1140 void do_list(AdaptiveFreeList<Chunk_t>* fl) {
duke@435 1141 double splitSurplusPercent = percentage;
duke@435 1142 fl->set_surplus(fl->count() -
duke@435 1143 (ssize_t)((double)fl->desired() * splitSurplusPercent));
duke@435 1144 }
jprovino@4542 1145 #endif // INCLUDE_ALL_GCS
duke@435 1146 };
duke@435 1147
jmasa@4196 1148 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1149 void BinaryTreeDictionary<Chunk_t, FreeList_t>::set_tree_surplus(double splitSurplusPercent) {
jmasa@4196 1150 setTreeSurplusClosure<Chunk_t, FreeList_t> sts(splitSurplusPercent);
duke@435 1151 sts.do_tree(root());
duke@435 1152 }
duke@435 1153
duke@435 1154 // Set hints for the lists in the tree.
jmasa@4196 1155 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1156 class setTreeHintsClosure : public DescendTreeCensusClosure<Chunk_t, FreeList_t> {
duke@435 1157 size_t hint;
duke@435 1158 public:
duke@435 1159 setTreeHintsClosure(size_t v) { hint = v; }
jmasa@4196 1160 void do_list(FreeList<Chunk_t>* fl) {}
jmasa@4196 1161
jprovino@4542 1162 #if INCLUDE_ALL_GCS
jmasa@4196 1163 void do_list(AdaptiveFreeList<Chunk_t>* fl) {
duke@435 1164 fl->set_hint(hint);
duke@435 1165 assert(fl->hint() == 0 || fl->hint() > fl->size(),
duke@435 1166 "Current hint is inconsistent");
duke@435 1167 if (fl->surplus() > 0) {
duke@435 1168 hint = fl->size();
duke@435 1169 }
duke@435 1170 }
jprovino@4542 1171 #endif // INCLUDE_ALL_GCS
duke@435 1172 };
duke@435 1173
jmasa@4196 1174 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1175 void BinaryTreeDictionary<Chunk_t, FreeList_t>::set_tree_hints(void) {
jmasa@4196 1176 setTreeHintsClosure<Chunk_t, FreeList_t> sth(0);
duke@435 1177 sth.do_tree(root());
duke@435 1178 }
duke@435 1179
duke@435 1180 // Save count before previous sweep and splits and coalesces.
jmasa@4196 1181 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1182 class clearTreeCensusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
jmasa@4196 1183 void do_list(FreeList<Chunk_t>* fl) {}
jmasa@4196 1184
jprovino@4542 1185 #if INCLUDE_ALL_GCS
jmasa@4196 1186 void do_list(AdaptiveFreeList<Chunk_t>* fl) {
jmasa@3732 1187 fl->set_prev_sweep(fl->count());
jmasa@3732 1188 fl->set_coal_births(0);
jmasa@3732 1189 fl->set_coal_deaths(0);
jmasa@3732 1190 fl->set_split_births(0);
jmasa@3732 1191 fl->set_split_deaths(0);
duke@435 1192 }
jprovino@4542 1193 #endif // INCLUDE_ALL_GCS
duke@435 1194 };
duke@435 1195
jmasa@4196 1196 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1197 void BinaryTreeDictionary<Chunk_t, FreeList_t>::clear_tree_census(void) {
jmasa@4196 1198 clearTreeCensusClosure<Chunk_t, FreeList_t> ctc;
duke@435 1199 ctc.do_tree(root());
duke@435 1200 }
duke@435 1201
duke@435 1202 // Do reporting and post sweep clean up.
jmasa@4196 1203 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1204 void BinaryTreeDictionary<Chunk_t, FreeList_t>::end_sweep_dict_census(double splitSurplusPercent) {
duke@435 1205 // Does walking the tree 3 times hurt?
jmasa@3732 1206 set_tree_surplus(splitSurplusPercent);
jmasa@3732 1207 set_tree_hints();
duke@435 1208 if (PrintGC && Verbose) {
jmasa@3732 1209 report_statistics();
duke@435 1210 }
jmasa@3732 1211 clear_tree_census();
duke@435 1212 }
duke@435 1213
duke@435 1214 // Print summary statistics
jmasa@4196 1215 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1216 void BinaryTreeDictionary<Chunk_t, FreeList_t>::report_statistics() const {
jmasa@4196 1217 FreeBlockDictionary<Chunk_t>::verify_par_locked();
duke@435 1218 gclog_or_tty->print("Statistics for BinaryTreeDictionary:\n"
duke@435 1219 "------------------------------------\n");
jmasa@3732 1220 size_t total_size = total_chunk_size(debug_only(NULL));
jmasa@3732 1221 size_t free_blocks = num_free_blocks();
jmasa@3732 1222 gclog_or_tty->print("Total Free Space: %d\n", total_size);
jmasa@3732 1223 gclog_or_tty->print("Max Chunk Size: %d\n", max_chunk_size());
jmasa@3732 1224 gclog_or_tty->print("Number of Blocks: %d\n", free_blocks);
jmasa@3732 1225 if (free_blocks > 0) {
jmasa@3732 1226 gclog_or_tty->print("Av. Block Size: %d\n", total_size/free_blocks);
duke@435 1227 }
jmasa@4196 1228 gclog_or_tty->print("Tree Height: %d\n", tree_height());
duke@435 1229 }
duke@435 1230
duke@435 1231 // Print census information - counts, births, deaths, etc.
duke@435 1232 // for each list in the tree. Also print some summary
duke@435 1233 // information.
jmasa@4196 1234 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1235 class PrintTreeCensusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
ysr@447 1236 int _print_line;
jmasa@3732 1237 size_t _total_free;
jmasa@4196 1238 FreeList_t<Chunk_t> _total;
duke@435 1239
duke@435 1240 public:
ysr@1580 1241 PrintTreeCensusClosure() {
ysr@447 1242 _print_line = 0;
jmasa@3732 1243 _total_free = 0;
duke@435 1244 }
jmasa@4196 1245 FreeList_t<Chunk_t>* total() { return &_total; }
jmasa@3732 1246 size_t total_free() { return _total_free; }
jmasa@4196 1247 void do_list(FreeList<Chunk_t>* fl) {
ysr@447 1248 if (++_print_line >= 40) {
jmasa@4196 1249 FreeList_t<Chunk_t>::print_labels_on(gclog_or_tty, "size");
ysr@447 1250 _print_line = 0;
ysr@447 1251 }
ysr@447 1252 fl->print_on(gclog_or_tty);
jmasa@3732 1253 _total_free += fl->count() * fl->size() ;
ysr@447 1254 total()->set_count( total()->count() + fl->count() );
jmasa@4196 1255 }
jmasa@4196 1256
jprovino@4542 1257 #if INCLUDE_ALL_GCS
jmasa@4196 1258 void do_list(AdaptiveFreeList<Chunk_t>* fl) {
jmasa@4196 1259 if (++_print_line >= 40) {
jmasa@4196 1260 FreeList_t<Chunk_t>::print_labels_on(gclog_or_tty, "size");
jmasa@4196 1261 _print_line = 0;
jmasa@4196 1262 }
jmasa@4196 1263 fl->print_on(gclog_or_tty);
jmasa@4196 1264 _total_free += fl->count() * fl->size() ;
jmasa@4196 1265 total()->set_count( total()->count() + fl->count() );
jmasa@4196 1266 total()->set_bfr_surp( total()->bfr_surp() + fl->bfr_surp() );
jmasa@3732 1267 total()->set_surplus( total()->split_deaths() + fl->surplus() );
jmasa@4196 1268 total()->set_desired( total()->desired() + fl->desired() );
jmasa@3732 1269 total()->set_prev_sweep( total()->prev_sweep() + fl->prev_sweep() );
jmasa@3732 1270 total()->set_before_sweep(total()->before_sweep() + fl->before_sweep());
jmasa@3732 1271 total()->set_coal_births( total()->coal_births() + fl->coal_births() );
jmasa@3732 1272 total()->set_coal_deaths( total()->coal_deaths() + fl->coal_deaths() );
jmasa@3732 1273 total()->set_split_births(total()->split_births() + fl->split_births());
jmasa@3732 1274 total()->set_split_deaths(total()->split_deaths() + fl->split_deaths());
duke@435 1275 }
jprovino@4542 1276 #endif // INCLUDE_ALL_GCS
duke@435 1277 };
duke@435 1278
jmasa@4196 1279 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1280 void BinaryTreeDictionary<Chunk_t, FreeList_t>::print_dict_census(void) const {
duke@435 1281
duke@435 1282 gclog_or_tty->print("\nBinaryTree\n");
jmasa@4196 1283 FreeList_t<Chunk_t>::print_labels_on(gclog_or_tty, "size");
jmasa@4196 1284 PrintTreeCensusClosure<Chunk_t, FreeList_t> ptc;
duke@435 1285 ptc.do_tree(root());
duke@435 1286
jmasa@4196 1287 FreeList_t<Chunk_t>* total = ptc.total();
jmasa@4196 1288 FreeList_t<Chunk_t>::print_labels_on(gclog_or_tty, " ");
jmasa@4196 1289 }
jmasa@4196 1290
jprovino@4542 1291 #if INCLUDE_ALL_GCS
jmasa@4196 1292 template <>
jmasa@4488 1293 void AFLBinaryTreeDictionary::print_dict_census(void) const {
jmasa@4196 1294
jmasa@4196 1295 gclog_or_tty->print("\nBinaryTree\n");
jmasa@4196 1296 AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, "size");
jmasa@4196 1297 PrintTreeCensusClosure<FreeChunk, AdaptiveFreeList> ptc;
jmasa@4196 1298 ptc.do_tree(root());
jmasa@4196 1299
jmasa@4196 1300 AdaptiveFreeList<FreeChunk>* total = ptc.total();
jmasa@4196 1301 AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, " ");
ysr@447 1302 total->print_on(gclog_or_tty, "TOTAL\t");
duke@435 1303 gclog_or_tty->print(
jmasa@3732 1304 "total_free(words): " SIZE_FORMAT_W(16)
ysr@447 1305 " growth: %8.5f deficit: %8.5f\n",
jmasa@3732 1306 ptc.total_free(),
jmasa@3732 1307 (double)(total->split_births() + total->coal_births()
jmasa@3732 1308 - total->split_deaths() - total->coal_deaths())
jmasa@3732 1309 /(total->prev_sweep() != 0 ? (double)total->prev_sweep() : 1.0),
ysr@447 1310 (double)(total->desired() - total->count())
ysr@447 1311 /(total->desired() != 0 ? (double)total->desired() : 1.0));
duke@435 1312 }
jprovino@4542 1313 #endif // INCLUDE_ALL_GCS
duke@435 1314
jmasa@4196 1315 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1316 class PrintFreeListsClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
ysr@1580 1317 outputStream* _st;
ysr@1580 1318 int _print_line;
ysr@1580 1319
ysr@1580 1320 public:
ysr@1580 1321 PrintFreeListsClosure(outputStream* st) {
ysr@1580 1322 _st = st;
ysr@1580 1323 _print_line = 0;
ysr@1580 1324 }
jmasa@4196 1325 void do_list(FreeList_t<Chunk_t>* fl) {
ysr@1580 1326 if (++_print_line >= 40) {
jmasa@4196 1327 FreeList_t<Chunk_t>::print_labels_on(_st, "size");
ysr@1580 1328 _print_line = 0;
ysr@1580 1329 }
ysr@1580 1330 fl->print_on(gclog_or_tty);
ysr@1580 1331 size_t sz = fl->size();
jmasa@4196 1332 for (Chunk_t* fc = fl->head(); fc != NULL;
ysr@1580 1333 fc = fc->next()) {
ysr@1580 1334 _st->print_cr("\t[" PTR_FORMAT "," PTR_FORMAT ") %s",
ysr@1580 1335 fc, (HeapWord*)fc + sz,
ysr@1580 1336 fc->cantCoalesce() ? "\t CC" : "");
ysr@1580 1337 }
ysr@1580 1338 }
ysr@1580 1339 };
ysr@1580 1340
jmasa@4196 1341 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1342 void BinaryTreeDictionary<Chunk_t, FreeList_t>::print_free_lists(outputStream* st) const {
ysr@1580 1343
jmasa@4196 1344 FreeList_t<Chunk_t>::print_labels_on(st, "size");
jmasa@4196 1345 PrintFreeListsClosure<Chunk_t, FreeList_t> pflc(st);
ysr@1580 1346 pflc.do_tree(root());
ysr@1580 1347 }
ysr@1580 1348
duke@435 1349 // Verify the following tree invariants:
duke@435 1350 // . _root has no parent
duke@435 1351 // . parent and child point to each other
duke@435 1352 // . each node's key correctly related to that of its child(ren)
jmasa@4196 1353 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1354 void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_tree() const {
jmasa@3732 1355 guarantee(root() == NULL || total_free_blocks() == 0 ||
jmasa@3732 1356 total_size() != 0, "_total_size should't be 0?");
duke@435 1357 guarantee(root() == NULL || root()->parent() == NULL, "_root shouldn't have parent");
jmasa@3732 1358 verify_tree_helper(root());
duke@435 1359 }
duke@435 1360
jmasa@4196 1361 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1362 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_prev_free_ptrs(TreeList<Chunk_t, FreeList_t>* tl) {
duke@435 1363 size_t ct = 0;
jmasa@4196 1364 for (Chunk_t* curFC = tl->head(); curFC != NULL; curFC = curFC->next()) {
duke@435 1365 ct++;
jmasa@3732 1366 assert(curFC->prev() == NULL || curFC->prev()->is_free(),
duke@435 1367 "Chunk should be free");
duke@435 1368 }
duke@435 1369 return ct;
duke@435 1370 }
duke@435 1371
duke@435 1372 // Note: this helper is recursive rather than iterative, so use with
duke@435 1373 // caution on very deep trees; and watch out for stack overflow errors;
duke@435 1374 // In general, to be used only for debugging.
jmasa@4196 1375 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1376 void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_tree_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
duke@435 1377 if (tl == NULL)
duke@435 1378 return;
duke@435 1379 guarantee(tl->size() != 0, "A list must has a size");
duke@435 1380 guarantee(tl->left() == NULL || tl->left()->parent() == tl,
duke@435 1381 "parent<-/->left");
duke@435 1382 guarantee(tl->right() == NULL || tl->right()->parent() == tl,
duke@435 1383 "parent<-/->right");;
duke@435 1384 guarantee(tl->left() == NULL || tl->left()->size() < tl->size(),
duke@435 1385 "parent !> left");
duke@435 1386 guarantee(tl->right() == NULL || tl->right()->size() > tl->size(),
duke@435 1387 "parent !< left");
jmasa@3732 1388 guarantee(tl->head() == NULL || tl->head()->is_free(), "!Free");
duke@435 1389 guarantee(tl->head() == NULL || tl->head_as_TreeChunk()->list() == tl,
duke@435 1390 "list inconsistency");
duke@435 1391 guarantee(tl->count() > 0 || (tl->head() == NULL && tl->tail() == NULL),
duke@435 1392 "list count is inconsistent");
duke@435 1393 guarantee(tl->count() > 1 || tl->head() == tl->tail(),
duke@435 1394 "list is incorrectly constructed");
jmasa@3732 1395 size_t count = verify_prev_free_ptrs(tl);
duke@435 1396 guarantee(count == (size_t)tl->count(), "Node count is incorrect");
duke@435 1397 if (tl->head() != NULL) {
jmasa@3732 1398 tl->head_as_TreeChunk()->verify_tree_chunk_list();
duke@435 1399 }
jmasa@3732 1400 verify_tree_helper(tl->left());
jmasa@3732 1401 verify_tree_helper(tl->right());
duke@435 1402 }
duke@435 1403
jmasa@4196 1404 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1405 void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify() const {
jmasa@3732 1406 verify_tree();
jmasa@3732 1407 guarantee(total_size() == total_size_in_tree(root()), "Total Size inconsistency");
duke@435 1408 }
jmasa@3730 1409
jmasa@4196 1410 template class TreeList<Metablock, FreeList>;
jmasa@4196 1411 template class BinaryTreeDictionary<Metablock, FreeList>;
jmasa@4196 1412 template class TreeChunk<Metablock, FreeList>;
jmasa@4196 1413
jmasa@4196 1414 template class TreeList<Metachunk, FreeList>;
jmasa@4196 1415 template class BinaryTreeDictionary<Metachunk, FreeList>;
jmasa@4196 1416 template class TreeChunk<Metachunk, FreeList>;
jmasa@4196 1417
jmasa@4196 1418
jprovino@4542 1419 #if INCLUDE_ALL_GCS
jmasa@3730 1420 // Explicitly instantiate these types for FreeChunk.
jmasa@4196 1421 template class TreeList<FreeChunk, AdaptiveFreeList>;
jmasa@4196 1422 template class BinaryTreeDictionary<FreeChunk, AdaptiveFreeList>;
jmasa@4196 1423 template class TreeChunk<FreeChunk, AdaptiveFreeList>;
jmasa@4196 1424
jprovino@4542 1425 #endif // INCLUDE_ALL_GCS

mercurial