src/share/vm/gc_implementation/concurrentMarkSweep/binaryTreeDictionary.cpp

Wed, 09 Jul 2008 15:08:55 -0700

author
jmasa
date
Wed, 09 Jul 2008 15:08:55 -0700
changeset 698
12eea04c8b06
parent 447
6432c3bb6240
child 704
850fdf70db2b
permissions
-rw-r--r--

6672698: mangle_unused_area() should not remangle the entire heap at each collection.
Summary: Maintain a high water mark for the allocations in a space and mangle only up to that high water mark.
Reviewed-by: ysr, apetrusenko

duke@435 1 /*
duke@435 2 * Copyright 2001-2006 Sun Microsystems, Inc. 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 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_binaryTreeDictionary.cpp.incl"
duke@435 27
duke@435 28 ////////////////////////////////////////////////////////////////////////////////
duke@435 29 // A binary tree based search structure for free blocks.
duke@435 30 // This is currently used in the Concurrent Mark&Sweep implementation.
duke@435 31 ////////////////////////////////////////////////////////////////////////////////
duke@435 32
duke@435 33 TreeChunk* TreeChunk::as_TreeChunk(FreeChunk* fc) {
duke@435 34 // Do some assertion checking here.
duke@435 35 return (TreeChunk*) fc;
duke@435 36 }
duke@435 37
duke@435 38 void TreeChunk::verifyTreeChunkList() const {
duke@435 39 TreeChunk* nextTC = (TreeChunk*)next();
duke@435 40 if (prev() != NULL) { // interior list node shouldn'r have tree fields
duke@435 41 guarantee(embedded_list()->parent() == NULL && embedded_list()->left() == NULL &&
duke@435 42 embedded_list()->right() == NULL, "should be clear");
duke@435 43 }
duke@435 44 if (nextTC != NULL) {
duke@435 45 guarantee(as_TreeChunk(nextTC->prev()) == this, "broken chain");
duke@435 46 guarantee(nextTC->size() == size(), "wrong size");
duke@435 47 nextTC->verifyTreeChunkList();
duke@435 48 }
duke@435 49 }
duke@435 50
duke@435 51
duke@435 52 TreeList* TreeList::as_TreeList(TreeChunk* tc) {
duke@435 53 // This first free chunk in the list will be the tree list.
duke@435 54 assert(tc->size() >= sizeof(TreeChunk), "Chunk is too small for a TreeChunk");
duke@435 55 TreeList* tl = tc->embedded_list();
duke@435 56 tc->set_list(tl);
duke@435 57 #ifdef ASSERT
duke@435 58 tl->set_protecting_lock(NULL);
duke@435 59 #endif
duke@435 60 tl->set_hint(0);
duke@435 61 tl->set_size(tc->size());
duke@435 62 tl->link_head(tc);
duke@435 63 tl->link_tail(tc);
duke@435 64 tl->set_count(1);
duke@435 65 tl->init_statistics();
duke@435 66 tl->setParent(NULL);
duke@435 67 tl->setLeft(NULL);
duke@435 68 tl->setRight(NULL);
duke@435 69 return tl;
duke@435 70 }
duke@435 71 TreeList* TreeList::as_TreeList(HeapWord* addr, size_t size) {
duke@435 72 TreeChunk* tc = (TreeChunk*) addr;
duke@435 73 assert(size >= sizeof(TreeChunk), "Chunk is too small for a TreeChunk");
jmasa@698 74 // The space in the heap will have been mangled initially but
jmasa@698 75 // is not remangled when a free chunk is returned to the free list
jmasa@698 76 // (since it is used to maintain the chunk on the free list).
jmasa@698 77 assert((ZapUnusedHeapArea &&
jmasa@698 78 SpaceMangler::is_mangled((HeapWord*) tc->size_addr()) &&
jmasa@698 79 SpaceMangler::is_mangled((HeapWord*) tc->prev_addr()) &&
jmasa@698 80 SpaceMangler::is_mangled((HeapWord*) tc->next_addr())) ||
jmasa@698 81 (tc->size() == 0 && tc->prev() == NULL && tc->next() == NULL),
jmasa@698 82 "Space should be clear or mangled");
duke@435 83 tc->setSize(size);
duke@435 84 tc->linkPrev(NULL);
duke@435 85 tc->linkNext(NULL);
duke@435 86 TreeList* tl = TreeList::as_TreeList(tc);
duke@435 87 return tl;
duke@435 88 }
duke@435 89
duke@435 90 TreeList* TreeList::removeChunkReplaceIfNeeded(TreeChunk* tc) {
duke@435 91
duke@435 92 TreeList* retTL = this;
duke@435 93 FreeChunk* list = head();
duke@435 94 assert(!list || list != list->next(), "Chunk on list twice");
duke@435 95 assert(tc != NULL, "Chunk being removed is NULL");
duke@435 96 assert(parent() == NULL || this == parent()->left() ||
duke@435 97 this == parent()->right(), "list is inconsistent");
duke@435 98 assert(tc->isFree(), "Header is not marked correctly");
duke@435 99 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 100 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 101
duke@435 102 FreeChunk* prevFC = tc->prev();
duke@435 103 TreeChunk* nextTC = TreeChunk::as_TreeChunk(tc->next());
duke@435 104 assert(list != NULL, "should have at least the target chunk");
duke@435 105
duke@435 106 // Is this the first item on the list?
duke@435 107 if (tc == list) {
duke@435 108 // The "getChunk..." functions for a TreeList will not return the
duke@435 109 // first chunk in the list unless it is the last chunk in the list
duke@435 110 // because the first chunk is also acting as the tree node.
duke@435 111 // When coalescing happens, however, the first chunk in the a tree
duke@435 112 // list can be the start of a free range. Free ranges are removed
duke@435 113 // from the free lists so that they are not available to be
duke@435 114 // allocated when the sweeper yields (giving up the free list lock)
duke@435 115 // to allow mutator activity. If this chunk is the first in the
duke@435 116 // list and is not the last in the list, do the work to copy the
duke@435 117 // TreeList from the first chunk to the next chunk and update all
duke@435 118 // the TreeList pointers in the chunks in the list.
duke@435 119 if (nextTC == NULL) {
duke@435 120 assert(prevFC == NULL, "Not last chunk in the list")
duke@435 121 set_tail(NULL);
duke@435 122 set_head(NULL);
duke@435 123 } else {
duke@435 124 // copy embedded list.
duke@435 125 nextTC->set_embedded_list(tc->embedded_list());
duke@435 126 retTL = nextTC->embedded_list();
duke@435 127 // Fix the pointer to the list in each chunk in the list.
duke@435 128 // This can be slow for a long list. Consider having
duke@435 129 // an option that does not allow the first chunk on the
duke@435 130 // list to be coalesced.
duke@435 131 for (TreeChunk* curTC = nextTC; curTC != NULL;
duke@435 132 curTC = TreeChunk::as_TreeChunk(curTC->next())) {
duke@435 133 curTC->set_list(retTL);
duke@435 134 }
duke@435 135 // Fix the parent to point to the new TreeList.
duke@435 136 if (retTL->parent() != NULL) {
duke@435 137 if (this == retTL->parent()->left()) {
duke@435 138 retTL->parent()->setLeft(retTL);
duke@435 139 } else {
duke@435 140 assert(this == retTL->parent()->right(), "Parent is incorrect");
duke@435 141 retTL->parent()->setRight(retTL);
duke@435 142 }
duke@435 143 }
duke@435 144 // Fix the children's parent pointers to point to the
duke@435 145 // new list.
duke@435 146 assert(right() == retTL->right(), "Should have been copied");
duke@435 147 if (retTL->right() != NULL) {
duke@435 148 retTL->right()->setParent(retTL);
duke@435 149 }
duke@435 150 assert(left() == retTL->left(), "Should have been copied");
duke@435 151 if (retTL->left() != NULL) {
duke@435 152 retTL->left()->setParent(retTL);
duke@435 153 }
duke@435 154 retTL->link_head(nextTC);
duke@435 155 assert(nextTC->isFree(), "Should be a free chunk");
duke@435 156 }
duke@435 157 } else {
duke@435 158 if (nextTC == NULL) {
duke@435 159 // Removing chunk at tail of list
duke@435 160 link_tail(prevFC);
duke@435 161 }
duke@435 162 // Chunk is interior to the list
duke@435 163 prevFC->linkAfter(nextTC);
duke@435 164 }
duke@435 165
duke@435 166 // Below this point the embeded TreeList being used for the
duke@435 167 // tree node may have changed. Don't use "this"
duke@435 168 // TreeList*.
duke@435 169 // chunk should still be a free chunk (bit set in _prev)
duke@435 170 assert(!retTL->head() || retTL->size() == retTL->head()->size(),
duke@435 171 "Wrong sized chunk in list");
duke@435 172 debug_only(
duke@435 173 tc->linkPrev(NULL);
duke@435 174 tc->linkNext(NULL);
duke@435 175 tc->set_list(NULL);
duke@435 176 bool prev_found = false;
duke@435 177 bool next_found = false;
duke@435 178 for (FreeChunk* curFC = retTL->head();
duke@435 179 curFC != NULL; curFC = curFC->next()) {
duke@435 180 assert(curFC != tc, "Chunk is still in list");
duke@435 181 if (curFC == prevFC) {
duke@435 182 prev_found = true;
duke@435 183 }
duke@435 184 if (curFC == nextTC) {
duke@435 185 next_found = true;
duke@435 186 }
duke@435 187 }
duke@435 188 assert(prevFC == NULL || prev_found, "Chunk was lost from list");
duke@435 189 assert(nextTC == NULL || next_found, "Chunk was lost from list");
duke@435 190 assert(retTL->parent() == NULL ||
duke@435 191 retTL == retTL->parent()->left() ||
duke@435 192 retTL == retTL->parent()->right(),
duke@435 193 "list is inconsistent");
duke@435 194 )
duke@435 195 retTL->decrement_count();
duke@435 196
duke@435 197 assert(tc->isFree(), "Should still be a free chunk");
duke@435 198 assert(retTL->head() == NULL || retTL->head()->prev() == NULL,
duke@435 199 "list invariant");
duke@435 200 assert(retTL->tail() == NULL || retTL->tail()->next() == NULL,
duke@435 201 "list invariant");
duke@435 202 return retTL;
duke@435 203 }
duke@435 204 void TreeList::returnChunkAtTail(TreeChunk* chunk) {
duke@435 205 assert(chunk != NULL, "returning NULL chunk");
duke@435 206 assert(chunk->list() == this, "list should be set for chunk");
duke@435 207 assert(tail() != NULL, "The tree list is embedded in the first chunk");
duke@435 208 // which means that the list can never be empty.
duke@435 209 assert(!verifyChunkInFreeLists(chunk), "Double entry");
duke@435 210 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 211 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 212
duke@435 213 FreeChunk* fc = tail();
duke@435 214 fc->linkAfter(chunk);
duke@435 215 link_tail(chunk);
duke@435 216
duke@435 217 assert(!tail() || size() == tail()->size(), "Wrong sized chunk in list");
duke@435 218 increment_count();
duke@435 219 debug_only(increment_returnedBytes_by(chunk->size()*sizeof(HeapWord));)
duke@435 220 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 221 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 222 }
duke@435 223
duke@435 224 // Add this chunk at the head of the list. "At the head of the list"
duke@435 225 // is defined to be after the chunk pointer to by head(). This is
duke@435 226 // because the TreeList is embedded in the first TreeChunk in the
duke@435 227 // list. See the definition of TreeChunk.
duke@435 228 void TreeList::returnChunkAtHead(TreeChunk* chunk) {
duke@435 229 assert(chunk->list() == this, "list should be set for chunk");
duke@435 230 assert(head() != NULL, "The tree list is embedded in the first chunk");
duke@435 231 assert(chunk != NULL, "returning NULL chunk");
duke@435 232 assert(!verifyChunkInFreeLists(chunk), "Double entry");
duke@435 233 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 234 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 235
duke@435 236 FreeChunk* fc = head()->next();
duke@435 237 if (fc != NULL) {
duke@435 238 chunk->linkAfter(fc);
duke@435 239 } else {
duke@435 240 assert(tail() == NULL, "List is inconsistent");
duke@435 241 link_tail(chunk);
duke@435 242 }
duke@435 243 head()->linkAfter(chunk);
duke@435 244 assert(!head() || size() == head()->size(), "Wrong sized chunk in list");
duke@435 245 increment_count();
duke@435 246 debug_only(increment_returnedBytes_by(chunk->size()*sizeof(HeapWord));)
duke@435 247 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 248 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 249 }
duke@435 250
duke@435 251 TreeChunk* TreeList::head_as_TreeChunk() {
duke@435 252 assert(head() == NULL || TreeChunk::as_TreeChunk(head())->list() == this,
duke@435 253 "Wrong type of chunk?");
duke@435 254 return TreeChunk::as_TreeChunk(head());
duke@435 255 }
duke@435 256
duke@435 257 TreeChunk* TreeList::first_available() {
duke@435 258 guarantee(head() != NULL, "The head of the list cannot be NULL");
duke@435 259 FreeChunk* fc = head()->next();
duke@435 260 TreeChunk* retTC;
duke@435 261 if (fc == NULL) {
duke@435 262 retTC = head_as_TreeChunk();
duke@435 263 } else {
duke@435 264 retTC = TreeChunk::as_TreeChunk(fc);
duke@435 265 }
duke@435 266 assert(retTC->list() == this, "Wrong type of chunk.");
duke@435 267 return retTC;
duke@435 268 }
duke@435 269
duke@435 270 BinaryTreeDictionary::BinaryTreeDictionary(MemRegion mr, bool splay):
duke@435 271 _splay(splay)
duke@435 272 {
duke@435 273 assert(mr.byte_size() > MIN_TREE_CHUNK_SIZE, "minimum chunk size");
duke@435 274
duke@435 275 reset(mr);
duke@435 276 assert(root()->left() == NULL, "reset check failed");
duke@435 277 assert(root()->right() == NULL, "reset check failed");
duke@435 278 assert(root()->head()->next() == NULL, "reset check failed");
duke@435 279 assert(root()->head()->prev() == NULL, "reset check failed");
duke@435 280 assert(totalSize() == root()->size(), "reset check failed");
duke@435 281 assert(totalFreeBlocks() == 1, "reset check failed");
duke@435 282 }
duke@435 283
duke@435 284 void BinaryTreeDictionary::inc_totalSize(size_t inc) {
duke@435 285 _totalSize = _totalSize + inc;
duke@435 286 }
duke@435 287
duke@435 288 void BinaryTreeDictionary::dec_totalSize(size_t dec) {
duke@435 289 _totalSize = _totalSize - dec;
duke@435 290 }
duke@435 291
duke@435 292 void BinaryTreeDictionary::reset(MemRegion mr) {
duke@435 293 assert(mr.byte_size() > MIN_TREE_CHUNK_SIZE, "minimum chunk size");
duke@435 294 set_root(TreeList::as_TreeList(mr.start(), mr.word_size()));
duke@435 295 set_totalSize(mr.word_size());
duke@435 296 set_totalFreeBlocks(1);
duke@435 297 }
duke@435 298
duke@435 299 void BinaryTreeDictionary::reset(HeapWord* addr, size_t byte_size) {
duke@435 300 MemRegion mr(addr, heap_word_size(byte_size));
duke@435 301 reset(mr);
duke@435 302 }
duke@435 303
duke@435 304 void BinaryTreeDictionary::reset() {
duke@435 305 set_root(NULL);
duke@435 306 set_totalSize(0);
duke@435 307 set_totalFreeBlocks(0);
duke@435 308 }
duke@435 309
duke@435 310 // Get a free block of size at least size from tree, or NULL.
duke@435 311 // If a splay step is requested, the removal algorithm (only) incorporates
duke@435 312 // a splay step as follows:
duke@435 313 // . the search proceeds down the tree looking for a possible
duke@435 314 // match. At the (closest) matching location, an appropriate splay step is applied
duke@435 315 // (zig, zig-zig or zig-zag). A chunk of the appropriate size is then returned
duke@435 316 // if available, and if it's the last chunk, the node is deleted. A deteleted
duke@435 317 // node is replaced in place by its tree successor.
duke@435 318 TreeChunk*
duke@435 319 BinaryTreeDictionary::getChunkFromTree(size_t size, Dither dither, bool splay)
duke@435 320 {
duke@435 321 TreeList *curTL, *prevTL;
duke@435 322 TreeChunk* retTC = NULL;
duke@435 323 assert(size >= MIN_TREE_CHUNK_SIZE, "minimum chunk size");
duke@435 324 if (FLSVerifyDictionary) {
duke@435 325 verifyTree();
duke@435 326 }
duke@435 327 // starting at the root, work downwards trying to find match.
duke@435 328 // Remember the last node of size too great or too small.
duke@435 329 for (prevTL = curTL = root(); curTL != NULL;) {
duke@435 330 if (curTL->size() == size) { // exact match
duke@435 331 break;
duke@435 332 }
duke@435 333 prevTL = curTL;
duke@435 334 if (curTL->size() < size) { // proceed to right sub-tree
duke@435 335 curTL = curTL->right();
duke@435 336 } else { // proceed to left sub-tree
duke@435 337 assert(curTL->size() > size, "size inconsistency");
duke@435 338 curTL = curTL->left();
duke@435 339 }
duke@435 340 }
duke@435 341 if (curTL == NULL) { // couldn't find exact match
duke@435 342 // try and find the next larger size by walking back up the search path
duke@435 343 for (curTL = prevTL; curTL != NULL;) {
duke@435 344 if (curTL->size() >= size) break;
duke@435 345 else curTL = curTL->parent();
duke@435 346 }
duke@435 347 assert(curTL == NULL || curTL->count() > 0,
duke@435 348 "An empty list should not be in the tree");
duke@435 349 }
duke@435 350 if (curTL != NULL) {
duke@435 351 assert(curTL->size() >= size, "size inconsistency");
duke@435 352 if (UseCMSAdaptiveFreeLists) {
duke@435 353
duke@435 354 // A candidate chunk has been found. If it is already under
duke@435 355 // populated, get a chunk associated with the hint for this
duke@435 356 // chunk.
duke@435 357 if (curTL->surplus() <= 0) {
duke@435 358 /* Use the hint to find a size with a surplus, and reset the hint. */
duke@435 359 TreeList* hintTL = curTL;
duke@435 360 while (hintTL->hint() != 0) {
duke@435 361 assert(hintTL->hint() == 0 || hintTL->hint() > hintTL->size(),
duke@435 362 "hint points in the wrong direction");
duke@435 363 hintTL = findList(hintTL->hint());
duke@435 364 assert(curTL != hintTL, "Infinite loop");
duke@435 365 if (hintTL == NULL ||
duke@435 366 hintTL == curTL /* Should not happen but protect against it */ ) {
duke@435 367 // No useful hint. Set the hint to NULL and go on.
duke@435 368 curTL->set_hint(0);
duke@435 369 break;
duke@435 370 }
duke@435 371 assert(hintTL->size() > size, "hint is inconsistent");
duke@435 372 if (hintTL->surplus() > 0) {
duke@435 373 // The hint led to a list that has a surplus. Use it.
duke@435 374 // Set the hint for the candidate to an overpopulated
duke@435 375 // size.
duke@435 376 curTL->set_hint(hintTL->size());
duke@435 377 // Change the candidate.
duke@435 378 curTL = hintTL;
duke@435 379 break;
duke@435 380 }
duke@435 381 // The evm code reset the hint of the candidate as
duke@435 382 // at an interrim point. Why? Seems like this leaves
duke@435 383 // the hint pointing to a list that didn't work.
duke@435 384 // curTL->set_hint(hintTL->size());
duke@435 385 }
duke@435 386 }
duke@435 387 }
duke@435 388 // don't waste time splaying if chunk's singleton
duke@435 389 if (splay && curTL->head()->next() != NULL) {
duke@435 390 semiSplayStep(curTL);
duke@435 391 }
duke@435 392 retTC = curTL->first_available();
duke@435 393 assert((retTC != NULL) && (curTL->count() > 0),
duke@435 394 "A list in the binary tree should not be NULL");
duke@435 395 assert(retTC->size() >= size,
duke@435 396 "A chunk of the wrong size was found");
duke@435 397 removeChunkFromTree(retTC);
duke@435 398 assert(retTC->isFree(), "Header is not marked correctly");
duke@435 399 }
duke@435 400
duke@435 401 if (FLSVerifyDictionary) {
duke@435 402 verify();
duke@435 403 }
duke@435 404 return retTC;
duke@435 405 }
duke@435 406
duke@435 407 TreeList* BinaryTreeDictionary::findList(size_t size) const {
duke@435 408 TreeList* curTL;
duke@435 409 for (curTL = root(); curTL != NULL;) {
duke@435 410 if (curTL->size() == size) { // exact match
duke@435 411 break;
duke@435 412 }
duke@435 413
duke@435 414 if (curTL->size() < size) { // proceed to right sub-tree
duke@435 415 curTL = curTL->right();
duke@435 416 } else { // proceed to left sub-tree
duke@435 417 assert(curTL->size() > size, "size inconsistency");
duke@435 418 curTL = curTL->left();
duke@435 419 }
duke@435 420 }
duke@435 421 return curTL;
duke@435 422 }
duke@435 423
duke@435 424
duke@435 425 bool BinaryTreeDictionary::verifyChunkInFreeLists(FreeChunk* tc) const {
duke@435 426 size_t size = tc->size();
duke@435 427 TreeList* tl = findList(size);
duke@435 428 if (tl == NULL) {
duke@435 429 return false;
duke@435 430 } else {
duke@435 431 return tl->verifyChunkInFreeLists(tc);
duke@435 432 }
duke@435 433 }
duke@435 434
duke@435 435 FreeChunk* BinaryTreeDictionary::findLargestDict() const {
duke@435 436 TreeList *curTL = root();
duke@435 437 if (curTL != NULL) {
duke@435 438 while(curTL->right() != NULL) curTL = curTL->right();
duke@435 439 return curTL->first_available();
duke@435 440 } else {
duke@435 441 return NULL;
duke@435 442 }
duke@435 443 }
duke@435 444
duke@435 445 // Remove the current chunk from the tree. If it is not the last
duke@435 446 // chunk in a list on a tree node, just unlink it.
duke@435 447 // If it is the last chunk in the list (the next link is NULL),
duke@435 448 // remove the node and repair the tree.
duke@435 449 TreeChunk*
duke@435 450 BinaryTreeDictionary::removeChunkFromTree(TreeChunk* tc) {
duke@435 451 assert(tc != NULL, "Should not call with a NULL chunk");
duke@435 452 assert(tc->isFree(), "Header is not marked correctly");
duke@435 453
duke@435 454 TreeList *newTL, *parentTL;
duke@435 455 TreeChunk* retTC;
duke@435 456 TreeList* tl = tc->list();
duke@435 457 debug_only(
duke@435 458 bool removing_only_chunk = false;
duke@435 459 if (tl == _root) {
duke@435 460 if ((_root->left() == NULL) && (_root->right() == NULL)) {
duke@435 461 if (_root->count() == 1) {
duke@435 462 assert(_root->head() == tc, "Should only be this one chunk");
duke@435 463 removing_only_chunk = true;
duke@435 464 }
duke@435 465 }
duke@435 466 }
duke@435 467 )
duke@435 468 assert(tl != NULL, "List should be set");
duke@435 469 assert(tl->parent() == NULL || tl == tl->parent()->left() ||
duke@435 470 tl == tl->parent()->right(), "list is inconsistent");
duke@435 471
duke@435 472 bool complicatedSplice = false;
duke@435 473
duke@435 474 retTC = tc;
duke@435 475 // Removing this chunk can have the side effect of changing the node
duke@435 476 // (TreeList*) in the tree. If the node is the root, update it.
duke@435 477 TreeList* replacementTL = tl->removeChunkReplaceIfNeeded(tc);
duke@435 478 assert(tc->isFree(), "Chunk should still be free");
duke@435 479 assert(replacementTL->parent() == NULL ||
duke@435 480 replacementTL == replacementTL->parent()->left() ||
duke@435 481 replacementTL == replacementTL->parent()->right(),
duke@435 482 "list is inconsistent");
duke@435 483 if (tl == root()) {
duke@435 484 assert(replacementTL->parent() == NULL, "Incorrectly replacing root");
duke@435 485 set_root(replacementTL);
duke@435 486 }
duke@435 487 debug_only(
duke@435 488 if (tl != replacementTL) {
duke@435 489 assert(replacementTL->head() != NULL,
duke@435 490 "If the tree list was replaced, it should not be a NULL list");
duke@435 491 TreeList* rhl = replacementTL->head_as_TreeChunk()->list();
duke@435 492 TreeList* rtl = TreeChunk::as_TreeChunk(replacementTL->tail())->list();
duke@435 493 assert(rhl == replacementTL, "Broken head");
duke@435 494 assert(rtl == replacementTL, "Broken tail");
duke@435 495 assert(replacementTL->size() == tc->size(), "Broken size");
duke@435 496 }
duke@435 497 )
duke@435 498
duke@435 499 // Does the tree need to be repaired?
duke@435 500 if (replacementTL->count() == 0) {
duke@435 501 assert(replacementTL->head() == NULL &&
duke@435 502 replacementTL->tail() == NULL, "list count is incorrect");
duke@435 503 // Find the replacement node for the (soon to be empty) node being removed.
duke@435 504 // if we have a single (or no) child, splice child in our stead
duke@435 505 if (replacementTL->left() == NULL) {
duke@435 506 // left is NULL so pick right. right may also be NULL.
duke@435 507 newTL = replacementTL->right();
duke@435 508 debug_only(replacementTL->clearRight();)
duke@435 509 } else if (replacementTL->right() == NULL) {
duke@435 510 // right is NULL
duke@435 511 newTL = replacementTL->left();
duke@435 512 debug_only(replacementTL->clearLeft();)
duke@435 513 } else { // we have both children, so, by patriarchal convention,
duke@435 514 // my replacement is least node in right sub-tree
duke@435 515 complicatedSplice = true;
duke@435 516 newTL = removeTreeMinimum(replacementTL->right());
duke@435 517 assert(newTL != NULL && newTL->left() == NULL &&
duke@435 518 newTL->right() == NULL, "sub-tree minimum exists");
duke@435 519 }
duke@435 520 // newTL is the replacement for the (soon to be empty) node.
duke@435 521 // newTL may be NULL.
duke@435 522 // should verify; we just cleanly excised our replacement
duke@435 523 if (FLSVerifyDictionary) {
duke@435 524 verifyTree();
duke@435 525 }
duke@435 526 // first make newTL my parent's child
duke@435 527 if ((parentTL = replacementTL->parent()) == NULL) {
duke@435 528 // newTL should be root
duke@435 529 assert(tl == root(), "Incorrectly replacing root");
duke@435 530 set_root(newTL);
duke@435 531 if (newTL != NULL) {
duke@435 532 newTL->clearParent();
duke@435 533 }
duke@435 534 } else if (parentTL->right() == replacementTL) {
duke@435 535 // replacementTL is a right child
duke@435 536 parentTL->setRight(newTL);
duke@435 537 } else { // replacementTL is a left child
duke@435 538 assert(parentTL->left() == replacementTL, "should be left child");
duke@435 539 parentTL->setLeft(newTL);
duke@435 540 }
duke@435 541 debug_only(replacementTL->clearParent();)
duke@435 542 if (complicatedSplice) { // we need newTL to get replacementTL's
duke@435 543 // two children
duke@435 544 assert(newTL != NULL &&
duke@435 545 newTL->left() == NULL && newTL->right() == NULL,
duke@435 546 "newTL should not have encumbrances from the past");
duke@435 547 // we'd like to assert as below:
duke@435 548 // assert(replacementTL->left() != NULL && replacementTL->right() != NULL,
duke@435 549 // "else !complicatedSplice");
duke@435 550 // ... however, the above assertion is too strong because we aren't
duke@435 551 // guaranteed that replacementTL->right() is still NULL.
duke@435 552 // Recall that we removed
duke@435 553 // the right sub-tree minimum from replacementTL.
duke@435 554 // That may well have been its right
duke@435 555 // child! So we'll just assert half of the above:
duke@435 556 assert(replacementTL->left() != NULL, "else !complicatedSplice");
duke@435 557 newTL->setLeft(replacementTL->left());
duke@435 558 newTL->setRight(replacementTL->right());
duke@435 559 debug_only(
duke@435 560 replacementTL->clearRight();
duke@435 561 replacementTL->clearLeft();
duke@435 562 )
duke@435 563 }
duke@435 564 assert(replacementTL->right() == NULL &&
duke@435 565 replacementTL->left() == NULL &&
duke@435 566 replacementTL->parent() == NULL,
duke@435 567 "delete without encumbrances");
duke@435 568 }
duke@435 569
duke@435 570 assert(totalSize() >= retTC->size(), "Incorrect total size");
duke@435 571 dec_totalSize(retTC->size()); // size book-keeping
duke@435 572 assert(totalFreeBlocks() > 0, "Incorrect total count");
duke@435 573 set_totalFreeBlocks(totalFreeBlocks() - 1);
duke@435 574
duke@435 575 assert(retTC != NULL, "null chunk?");
duke@435 576 assert(retTC->prev() == NULL && retTC->next() == NULL,
duke@435 577 "should return without encumbrances");
duke@435 578 if (FLSVerifyDictionary) {
duke@435 579 verifyTree();
duke@435 580 }
duke@435 581 assert(!removing_only_chunk || _root == NULL, "root should be NULL");
duke@435 582 return TreeChunk::as_TreeChunk(retTC);
duke@435 583 }
duke@435 584
duke@435 585 // Remove the leftmost node (lm) in the tree and return it.
duke@435 586 // If lm has a right child, link it to the left node of
duke@435 587 // the parent of lm.
duke@435 588 TreeList* BinaryTreeDictionary::removeTreeMinimum(TreeList* tl) {
duke@435 589 assert(tl != NULL && tl->parent() != NULL, "really need a proper sub-tree");
duke@435 590 // locate the subtree minimum by walking down left branches
duke@435 591 TreeList* curTL = tl;
duke@435 592 for (; curTL->left() != NULL; curTL = curTL->left());
duke@435 593 // obviously curTL now has at most one child, a right child
duke@435 594 if (curTL != root()) { // Should this test just be removed?
duke@435 595 TreeList* parentTL = curTL->parent();
duke@435 596 if (parentTL->left() == curTL) { // curTL is a left child
duke@435 597 parentTL->setLeft(curTL->right());
duke@435 598 } else {
duke@435 599 // If the list tl has no left child, then curTL may be
duke@435 600 // the right child of parentTL.
duke@435 601 assert(parentTL->right() == curTL, "should be a right child");
duke@435 602 parentTL->setRight(curTL->right());
duke@435 603 }
duke@435 604 } else {
duke@435 605 // The only use of this method would not pass the root of the
duke@435 606 // tree (as indicated by the assertion above that the tree list
duke@435 607 // has a parent) but the specification does not explicitly exclude the
duke@435 608 // passing of the root so accomodate it.
duke@435 609 set_root(NULL);
duke@435 610 }
duke@435 611 debug_only(
duke@435 612 curTL->clearParent(); // Test if this needs to be cleared
duke@435 613 curTL->clearRight(); // recall, above, left child is already null
duke@435 614 )
duke@435 615 // we just excised a (non-root) node, we should still verify all tree invariants
duke@435 616 if (FLSVerifyDictionary) {
duke@435 617 verifyTree();
duke@435 618 }
duke@435 619 return curTL;
duke@435 620 }
duke@435 621
duke@435 622 // Based on a simplification of the algorithm by Sleator and Tarjan (JACM 1985).
duke@435 623 // The simplifications are the following:
duke@435 624 // . we splay only when we delete (not when we insert)
duke@435 625 // . we apply a single spay step per deletion/access
duke@435 626 // By doing such partial splaying, we reduce the amount of restructuring,
duke@435 627 // while getting a reasonably efficient search tree (we think).
duke@435 628 // [Measurements will be needed to (in)validate this expectation.]
duke@435 629
duke@435 630 void BinaryTreeDictionary::semiSplayStep(TreeList* tc) {
duke@435 631 // apply a semi-splay step at the given node:
duke@435 632 // . if root, norting needs to be done
duke@435 633 // . if child of root, splay once
duke@435 634 // . else zig-zig or sig-zag depending on path from grandparent
duke@435 635 if (root() == tc) return;
duke@435 636 warning("*** Splaying not yet implemented; "
duke@435 637 "tree operations may be inefficient ***");
duke@435 638 }
duke@435 639
duke@435 640 void BinaryTreeDictionary::insertChunkInTree(FreeChunk* fc) {
duke@435 641 TreeList *curTL, *prevTL;
duke@435 642 size_t size = fc->size();
duke@435 643
duke@435 644 assert(size >= MIN_TREE_CHUNK_SIZE, "too small to be a TreeList");
duke@435 645 if (FLSVerifyDictionary) {
duke@435 646 verifyTree();
duke@435 647 }
duke@435 648 // XXX: do i need to clear the FreeChunk fields, let me do it just in case
duke@435 649 // Revisit this later
duke@435 650
duke@435 651 fc->clearNext();
duke@435 652 fc->linkPrev(NULL);
duke@435 653
duke@435 654 // work down from the _root, looking for insertion point
duke@435 655 for (prevTL = curTL = root(); curTL != NULL;) {
duke@435 656 if (curTL->size() == size) // exact match
duke@435 657 break;
duke@435 658 prevTL = curTL;
duke@435 659 if (curTL->size() > size) { // follow left branch
duke@435 660 curTL = curTL->left();
duke@435 661 } else { // follow right branch
duke@435 662 assert(curTL->size() < size, "size inconsistency");
duke@435 663 curTL = curTL->right();
duke@435 664 }
duke@435 665 }
duke@435 666 TreeChunk* tc = TreeChunk::as_TreeChunk(fc);
duke@435 667 // This chunk is being returned to the binary try. It's embedded
duke@435 668 // TreeList should be unused at this point.
duke@435 669 tc->initialize();
duke@435 670 if (curTL != NULL) { // exact match
duke@435 671 tc->set_list(curTL);
duke@435 672 curTL->returnChunkAtTail(tc);
duke@435 673 } else { // need a new node in tree
duke@435 674 tc->clearNext();
duke@435 675 tc->linkPrev(NULL);
duke@435 676 TreeList* newTL = TreeList::as_TreeList(tc);
duke@435 677 assert(((TreeChunk*)tc)->list() == newTL,
duke@435 678 "List was not initialized correctly");
duke@435 679 if (prevTL == NULL) { // we are the only tree node
duke@435 680 assert(root() == NULL, "control point invariant");
duke@435 681 set_root(newTL);
duke@435 682 } else { // insert under prevTL ...
duke@435 683 if (prevTL->size() < size) { // am right child
duke@435 684 assert(prevTL->right() == NULL, "control point invariant");
duke@435 685 prevTL->setRight(newTL);
duke@435 686 } else { // am left child
duke@435 687 assert(prevTL->size() > size && prevTL->left() == NULL, "cpt pt inv");
duke@435 688 prevTL->setLeft(newTL);
duke@435 689 }
duke@435 690 }
duke@435 691 }
duke@435 692 assert(tc->list() != NULL, "Tree list should be set");
duke@435 693
duke@435 694 inc_totalSize(size);
duke@435 695 // Method 'totalSizeInTree' walks through the every block in the
duke@435 696 // tree, so it can cause significant performance loss if there are
duke@435 697 // many blocks in the tree
duke@435 698 assert(!FLSVerifyDictionary || totalSizeInTree(root()) == totalSize(), "_totalSize inconsistency");
duke@435 699 set_totalFreeBlocks(totalFreeBlocks() + 1);
duke@435 700 if (FLSVerifyDictionary) {
duke@435 701 verifyTree();
duke@435 702 }
duke@435 703 }
duke@435 704
duke@435 705 size_t BinaryTreeDictionary::maxChunkSize() const {
duke@435 706 verify_par_locked();
duke@435 707 TreeList* tc = root();
duke@435 708 if (tc == NULL) return 0;
duke@435 709 for (; tc->right() != NULL; tc = tc->right());
duke@435 710 return tc->size();
duke@435 711 }
duke@435 712
duke@435 713 size_t BinaryTreeDictionary::totalListLength(TreeList* tl) const {
duke@435 714 size_t res;
duke@435 715 res = tl->count();
duke@435 716 #ifdef ASSERT
duke@435 717 size_t cnt;
duke@435 718 FreeChunk* tc = tl->head();
duke@435 719 for (cnt = 0; tc != NULL; tc = tc->next(), cnt++);
duke@435 720 assert(res == cnt, "The count is not being maintained correctly");
duke@435 721 #endif
duke@435 722 return res;
duke@435 723 }
duke@435 724
duke@435 725 size_t BinaryTreeDictionary::totalSizeInTree(TreeList* tl) const {
duke@435 726 if (tl == NULL)
duke@435 727 return 0;
duke@435 728 return (tl->size() * totalListLength(tl)) +
duke@435 729 totalSizeInTree(tl->left()) +
duke@435 730 totalSizeInTree(tl->right());
duke@435 731 }
duke@435 732
duke@435 733 double BinaryTreeDictionary::sum_of_squared_block_sizes(TreeList* const tl) const {
duke@435 734 if (tl == NULL) {
duke@435 735 return 0.0;
duke@435 736 }
duke@435 737 double size = (double)(tl->size());
duke@435 738 double curr = size * size * totalListLength(tl);
duke@435 739 curr += sum_of_squared_block_sizes(tl->left());
duke@435 740 curr += sum_of_squared_block_sizes(tl->right());
duke@435 741 return curr;
duke@435 742 }
duke@435 743
duke@435 744 size_t BinaryTreeDictionary::totalFreeBlocksInTree(TreeList* tl) const {
duke@435 745 if (tl == NULL)
duke@435 746 return 0;
duke@435 747 return totalListLength(tl) +
duke@435 748 totalFreeBlocksInTree(tl->left()) +
duke@435 749 totalFreeBlocksInTree(tl->right());
duke@435 750 }
duke@435 751
duke@435 752 size_t BinaryTreeDictionary::numFreeBlocks() const {
duke@435 753 assert(totalFreeBlocksInTree(root()) == totalFreeBlocks(),
duke@435 754 "_totalFreeBlocks inconsistency");
duke@435 755 return totalFreeBlocks();
duke@435 756 }
duke@435 757
duke@435 758 size_t BinaryTreeDictionary::treeHeightHelper(TreeList* tl) const {
duke@435 759 if (tl == NULL)
duke@435 760 return 0;
duke@435 761 return 1 + MAX2(treeHeightHelper(tl->left()),
duke@435 762 treeHeightHelper(tl->right()));
duke@435 763 }
duke@435 764
duke@435 765 size_t BinaryTreeDictionary::treeHeight() const {
duke@435 766 return treeHeightHelper(root());
duke@435 767 }
duke@435 768
duke@435 769 size_t BinaryTreeDictionary::totalNodesHelper(TreeList* tl) const {
duke@435 770 if (tl == NULL) {
duke@435 771 return 0;
duke@435 772 }
duke@435 773 return 1 + totalNodesHelper(tl->left()) +
duke@435 774 totalNodesHelper(tl->right());
duke@435 775 }
duke@435 776
duke@435 777 size_t BinaryTreeDictionary::totalNodesInTree(TreeList* tl) const {
duke@435 778 return totalNodesHelper(root());
duke@435 779 }
duke@435 780
duke@435 781 void BinaryTreeDictionary::dictCensusUpdate(size_t size, bool split, bool birth){
duke@435 782 TreeList* nd = findList(size);
duke@435 783 if (nd) {
duke@435 784 if (split) {
duke@435 785 if (birth) {
duke@435 786 nd->increment_splitBirths();
duke@435 787 nd->increment_surplus();
duke@435 788 } else {
duke@435 789 nd->increment_splitDeaths();
duke@435 790 nd->decrement_surplus();
duke@435 791 }
duke@435 792 } else {
duke@435 793 if (birth) {
duke@435 794 nd->increment_coalBirths();
duke@435 795 nd->increment_surplus();
duke@435 796 } else {
duke@435 797 nd->increment_coalDeaths();
duke@435 798 nd->decrement_surplus();
duke@435 799 }
duke@435 800 }
duke@435 801 }
duke@435 802 // A list for this size may not be found (nd == 0) if
duke@435 803 // This is a death where the appropriate list is now
duke@435 804 // empty and has been removed from the list.
duke@435 805 // This is a birth associated with a LinAB. The chunk
duke@435 806 // for the LinAB is not in the dictionary.
duke@435 807 }
duke@435 808
duke@435 809 bool BinaryTreeDictionary::coalDictOverPopulated(size_t size) {
duke@435 810 TreeList* list_of_size = findList(size);
duke@435 811 // None of requested size implies overpopulated.
duke@435 812 return list_of_size == NULL || list_of_size->coalDesired() <= 0 ||
duke@435 813 list_of_size->count() > list_of_size->coalDesired();
duke@435 814 }
duke@435 815
duke@435 816 // Closures for walking the binary tree.
duke@435 817 // do_list() walks the free list in a node applying the closure
duke@435 818 // to each free chunk in the list
duke@435 819 // do_tree() walks the nodes in the binary tree applying do_list()
duke@435 820 // to each list at each node.
duke@435 821
duke@435 822 class TreeCensusClosure : public StackObj {
duke@435 823 protected:
duke@435 824 virtual void do_list(FreeList* fl) = 0;
duke@435 825 public:
duke@435 826 virtual void do_tree(TreeList* tl) = 0;
duke@435 827 };
duke@435 828
duke@435 829 class AscendTreeCensusClosure : public TreeCensusClosure {
duke@435 830 public:
duke@435 831 void do_tree(TreeList* tl) {
duke@435 832 if (tl != NULL) {
duke@435 833 do_tree(tl->left());
duke@435 834 do_list(tl);
duke@435 835 do_tree(tl->right());
duke@435 836 }
duke@435 837 }
duke@435 838 };
duke@435 839
duke@435 840 class DescendTreeCensusClosure : public TreeCensusClosure {
duke@435 841 public:
duke@435 842 void do_tree(TreeList* tl) {
duke@435 843 if (tl != NULL) {
duke@435 844 do_tree(tl->right());
duke@435 845 do_list(tl);
duke@435 846 do_tree(tl->left());
duke@435 847 }
duke@435 848 }
duke@435 849 };
duke@435 850
duke@435 851 // For each list in the tree, calculate the desired, desired
duke@435 852 // coalesce, count before sweep, and surplus before sweep.
duke@435 853 class BeginSweepClosure : public AscendTreeCensusClosure {
duke@435 854 double _percentage;
duke@435 855 float _inter_sweep_current;
duke@435 856 float _inter_sweep_estimate;
duke@435 857
duke@435 858 public:
duke@435 859 BeginSweepClosure(double p, float inter_sweep_current,
duke@435 860 float inter_sweep_estimate) :
duke@435 861 _percentage(p),
duke@435 862 _inter_sweep_current(inter_sweep_current),
duke@435 863 _inter_sweep_estimate(inter_sweep_estimate) { }
duke@435 864
duke@435 865 void do_list(FreeList* fl) {
duke@435 866 double coalSurplusPercent = _percentage;
duke@435 867 fl->compute_desired(_inter_sweep_current, _inter_sweep_estimate);
duke@435 868 fl->set_coalDesired((ssize_t)((double)fl->desired() * coalSurplusPercent));
duke@435 869 fl->set_beforeSweep(fl->count());
duke@435 870 fl->set_bfrSurp(fl->surplus());
duke@435 871 }
duke@435 872 };
duke@435 873
duke@435 874 // Used to search the tree until a condition is met.
duke@435 875 // Similar to TreeCensusClosure but searches the
duke@435 876 // tree and returns promptly when found.
duke@435 877
duke@435 878 class TreeSearchClosure : public StackObj {
duke@435 879 protected:
duke@435 880 virtual bool do_list(FreeList* fl) = 0;
duke@435 881 public:
duke@435 882 virtual bool do_tree(TreeList* tl) = 0;
duke@435 883 };
duke@435 884
duke@435 885 #if 0 // Don't need this yet but here for symmetry.
duke@435 886 class AscendTreeSearchClosure : public TreeSearchClosure {
duke@435 887 public:
duke@435 888 bool do_tree(TreeList* tl) {
duke@435 889 if (tl != NULL) {
duke@435 890 if (do_tree(tl->left())) return true;
duke@435 891 if (do_list(tl)) return true;
duke@435 892 if (do_tree(tl->right())) return true;
duke@435 893 }
duke@435 894 return false;
duke@435 895 }
duke@435 896 };
duke@435 897 #endif
duke@435 898
duke@435 899 class DescendTreeSearchClosure : public TreeSearchClosure {
duke@435 900 public:
duke@435 901 bool do_tree(TreeList* tl) {
duke@435 902 if (tl != NULL) {
duke@435 903 if (do_tree(tl->right())) return true;
duke@435 904 if (do_list(tl)) return true;
duke@435 905 if (do_tree(tl->left())) return true;
duke@435 906 }
duke@435 907 return false;
duke@435 908 }
duke@435 909 };
duke@435 910
duke@435 911 // Searches the tree for a chunk that ends at the
duke@435 912 // specified address.
duke@435 913 class EndTreeSearchClosure : public DescendTreeSearchClosure {
duke@435 914 HeapWord* _target;
duke@435 915 FreeChunk* _found;
duke@435 916
duke@435 917 public:
duke@435 918 EndTreeSearchClosure(HeapWord* target) : _target(target), _found(NULL) {}
duke@435 919 bool do_list(FreeList* fl) {
duke@435 920 FreeChunk* item = fl->head();
duke@435 921 while (item != NULL) {
duke@435 922 if (item->end() == _target) {
duke@435 923 _found = item;
duke@435 924 return true;
duke@435 925 }
duke@435 926 item = item->next();
duke@435 927 }
duke@435 928 return false;
duke@435 929 }
duke@435 930 FreeChunk* found() { return _found; }
duke@435 931 };
duke@435 932
duke@435 933 FreeChunk* BinaryTreeDictionary::find_chunk_ends_at(HeapWord* target) const {
duke@435 934 EndTreeSearchClosure etsc(target);
duke@435 935 bool found_target = etsc.do_tree(root());
duke@435 936 assert(found_target || etsc.found() == NULL, "Consistency check");
duke@435 937 assert(!found_target || etsc.found() != NULL, "Consistency check");
duke@435 938 return etsc.found();
duke@435 939 }
duke@435 940
duke@435 941 void BinaryTreeDictionary::beginSweepDictCensus(double coalSurplusPercent,
duke@435 942 float inter_sweep_current, float inter_sweep_estimate) {
duke@435 943 BeginSweepClosure bsc(coalSurplusPercent, inter_sweep_current,
duke@435 944 inter_sweep_estimate);
duke@435 945 bsc.do_tree(root());
duke@435 946 }
duke@435 947
duke@435 948 // Closures and methods for calculating total bytes returned to the
duke@435 949 // free lists in the tree.
duke@435 950 NOT_PRODUCT(
duke@435 951 class InitializeDictReturnedBytesClosure : public AscendTreeCensusClosure {
duke@435 952 public:
duke@435 953 void do_list(FreeList* fl) {
duke@435 954 fl->set_returnedBytes(0);
duke@435 955 }
duke@435 956 };
duke@435 957
duke@435 958 void BinaryTreeDictionary::initializeDictReturnedBytes() {
duke@435 959 InitializeDictReturnedBytesClosure idrb;
duke@435 960 idrb.do_tree(root());
duke@435 961 }
duke@435 962
duke@435 963 class ReturnedBytesClosure : public AscendTreeCensusClosure {
duke@435 964 size_t _dictReturnedBytes;
duke@435 965 public:
duke@435 966 ReturnedBytesClosure() { _dictReturnedBytes = 0; }
duke@435 967 void do_list(FreeList* fl) {
duke@435 968 _dictReturnedBytes += fl->returnedBytes();
duke@435 969 }
duke@435 970 size_t dictReturnedBytes() { return _dictReturnedBytes; }
duke@435 971 };
duke@435 972
duke@435 973 size_t BinaryTreeDictionary::sumDictReturnedBytes() {
duke@435 974 ReturnedBytesClosure rbc;
duke@435 975 rbc.do_tree(root());
duke@435 976
duke@435 977 return rbc.dictReturnedBytes();
duke@435 978 }
duke@435 979
duke@435 980 // Count the number of entries in the tree.
duke@435 981 class treeCountClosure : public DescendTreeCensusClosure {
duke@435 982 public:
duke@435 983 uint count;
duke@435 984 treeCountClosure(uint c) { count = c; }
duke@435 985 void do_list(FreeList* fl) {
duke@435 986 count++;
duke@435 987 }
duke@435 988 };
duke@435 989
duke@435 990 size_t BinaryTreeDictionary::totalCount() {
duke@435 991 treeCountClosure ctc(0);
duke@435 992 ctc.do_tree(root());
duke@435 993 return ctc.count;
duke@435 994 }
duke@435 995 )
duke@435 996
duke@435 997 // Calculate surpluses for the lists in the tree.
duke@435 998 class setTreeSurplusClosure : public AscendTreeCensusClosure {
duke@435 999 double percentage;
duke@435 1000 public:
duke@435 1001 setTreeSurplusClosure(double v) { percentage = v; }
duke@435 1002 void do_list(FreeList* fl) {
duke@435 1003 double splitSurplusPercent = percentage;
duke@435 1004 fl->set_surplus(fl->count() -
duke@435 1005 (ssize_t)((double)fl->desired() * splitSurplusPercent));
duke@435 1006 }
duke@435 1007 };
duke@435 1008
duke@435 1009 void BinaryTreeDictionary::setTreeSurplus(double splitSurplusPercent) {
duke@435 1010 setTreeSurplusClosure sts(splitSurplusPercent);
duke@435 1011 sts.do_tree(root());
duke@435 1012 }
duke@435 1013
duke@435 1014 // Set hints for the lists in the tree.
duke@435 1015 class setTreeHintsClosure : public DescendTreeCensusClosure {
duke@435 1016 size_t hint;
duke@435 1017 public:
duke@435 1018 setTreeHintsClosure(size_t v) { hint = v; }
duke@435 1019 void do_list(FreeList* fl) {
duke@435 1020 fl->set_hint(hint);
duke@435 1021 assert(fl->hint() == 0 || fl->hint() > fl->size(),
duke@435 1022 "Current hint is inconsistent");
duke@435 1023 if (fl->surplus() > 0) {
duke@435 1024 hint = fl->size();
duke@435 1025 }
duke@435 1026 }
duke@435 1027 };
duke@435 1028
duke@435 1029 void BinaryTreeDictionary::setTreeHints(void) {
duke@435 1030 setTreeHintsClosure sth(0);
duke@435 1031 sth.do_tree(root());
duke@435 1032 }
duke@435 1033
duke@435 1034 // Save count before previous sweep and splits and coalesces.
duke@435 1035 class clearTreeCensusClosure : public AscendTreeCensusClosure {
duke@435 1036 void do_list(FreeList* fl) {
duke@435 1037 fl->set_prevSweep(fl->count());
duke@435 1038 fl->set_coalBirths(0);
duke@435 1039 fl->set_coalDeaths(0);
duke@435 1040 fl->set_splitBirths(0);
duke@435 1041 fl->set_splitDeaths(0);
duke@435 1042 }
duke@435 1043 };
duke@435 1044
duke@435 1045 void BinaryTreeDictionary::clearTreeCensus(void) {
duke@435 1046 clearTreeCensusClosure ctc;
duke@435 1047 ctc.do_tree(root());
duke@435 1048 }
duke@435 1049
duke@435 1050 // Do reporting and post sweep clean up.
duke@435 1051 void BinaryTreeDictionary::endSweepDictCensus(double splitSurplusPercent) {
duke@435 1052 // Does walking the tree 3 times hurt?
duke@435 1053 setTreeSurplus(splitSurplusPercent);
duke@435 1054 setTreeHints();
duke@435 1055 if (PrintGC && Verbose) {
duke@435 1056 reportStatistics();
duke@435 1057 }
duke@435 1058 clearTreeCensus();
duke@435 1059 }
duke@435 1060
duke@435 1061 // Print summary statistics
duke@435 1062 void BinaryTreeDictionary::reportStatistics() const {
duke@435 1063 verify_par_locked();
duke@435 1064 gclog_or_tty->print("Statistics for BinaryTreeDictionary:\n"
duke@435 1065 "------------------------------------\n");
duke@435 1066 size_t totalSize = totalChunkSize(debug_only(NULL));
duke@435 1067 size_t freeBlocks = numFreeBlocks();
duke@435 1068 gclog_or_tty->print("Total Free Space: %d\n", totalSize);
duke@435 1069 gclog_or_tty->print("Max Chunk Size: %d\n", maxChunkSize());
duke@435 1070 gclog_or_tty->print("Number of Blocks: %d\n", freeBlocks);
duke@435 1071 if (freeBlocks > 0) {
duke@435 1072 gclog_or_tty->print("Av. Block Size: %d\n", totalSize/freeBlocks);
duke@435 1073 }
duke@435 1074 gclog_or_tty->print("Tree Height: %d\n", treeHeight());
duke@435 1075 }
duke@435 1076
duke@435 1077 // Print census information - counts, births, deaths, etc.
duke@435 1078 // for each list in the tree. Also print some summary
duke@435 1079 // information.
duke@435 1080 class printTreeCensusClosure : public AscendTreeCensusClosure {
ysr@447 1081 int _print_line;
duke@435 1082 size_t _totalFree;
ysr@447 1083 FreeList _total;
duke@435 1084
duke@435 1085 public:
duke@435 1086 printTreeCensusClosure() {
ysr@447 1087 _print_line = 0;
duke@435 1088 _totalFree = 0;
duke@435 1089 }
ysr@447 1090 FreeList* total() { return &_total; }
duke@435 1091 size_t totalFree() { return _totalFree; }
duke@435 1092 void do_list(FreeList* fl) {
ysr@447 1093 if (++_print_line >= 40) {
ysr@447 1094 FreeList::print_labels_on(gclog_or_tty, "size");
ysr@447 1095 _print_line = 0;
ysr@447 1096 }
ysr@447 1097 fl->print_on(gclog_or_tty);
ysr@447 1098 _totalFree += fl->count() * fl->size() ;
ysr@447 1099 total()->set_count( total()->count() + fl->count() );
ysr@447 1100 total()->set_bfrSurp( total()->bfrSurp() + fl->bfrSurp() );
ysr@447 1101 total()->set_surplus( total()->splitDeaths() + fl->surplus() );
ysr@447 1102 total()->set_desired( total()->desired() + fl->desired() );
ysr@447 1103 total()->set_prevSweep( total()->prevSweep() + fl->prevSweep() );
ysr@447 1104 total()->set_beforeSweep(total()->beforeSweep() + fl->beforeSweep());
ysr@447 1105 total()->set_coalBirths( total()->coalBirths() + fl->coalBirths() );
ysr@447 1106 total()->set_coalDeaths( total()->coalDeaths() + fl->coalDeaths() );
ysr@447 1107 total()->set_splitBirths(total()->splitBirths() + fl->splitBirths());
ysr@447 1108 total()->set_splitDeaths(total()->splitDeaths() + fl->splitDeaths());
duke@435 1109 }
duke@435 1110 };
duke@435 1111
duke@435 1112 void BinaryTreeDictionary::printDictCensus(void) const {
duke@435 1113
duke@435 1114 gclog_or_tty->print("\nBinaryTree\n");
ysr@447 1115 FreeList::print_labels_on(gclog_or_tty, "size");
duke@435 1116 printTreeCensusClosure ptc;
duke@435 1117 ptc.do_tree(root());
duke@435 1118
ysr@447 1119 FreeList* total = ptc.total();
ysr@447 1120 FreeList::print_labels_on(gclog_or_tty, " ");
ysr@447 1121 total->print_on(gclog_or_tty, "TOTAL\t");
duke@435 1122 gclog_or_tty->print(
ysr@447 1123 "totalFree(words): " SIZE_FORMAT_W(16)
ysr@447 1124 " growth: %8.5f deficit: %8.5f\n",
duke@435 1125 ptc.totalFree(),
ysr@447 1126 (double)(total->splitBirths() + total->coalBirths()
ysr@447 1127 - total->splitDeaths() - total->coalDeaths())
ysr@447 1128 /(total->prevSweep() != 0 ? (double)total->prevSweep() : 1.0),
ysr@447 1129 (double)(total->desired() - total->count())
ysr@447 1130 /(total->desired() != 0 ? (double)total->desired() : 1.0));
duke@435 1131 }
duke@435 1132
duke@435 1133 // Verify the following tree invariants:
duke@435 1134 // . _root has no parent
duke@435 1135 // . parent and child point to each other
duke@435 1136 // . each node's key correctly related to that of its child(ren)
duke@435 1137 void BinaryTreeDictionary::verifyTree() const {
duke@435 1138 guarantee(root() == NULL || totalFreeBlocks() == 0 ||
duke@435 1139 totalSize() != 0, "_totalSize should't be 0?");
duke@435 1140 guarantee(root() == NULL || root()->parent() == NULL, "_root shouldn't have parent");
duke@435 1141 verifyTreeHelper(root());
duke@435 1142 }
duke@435 1143
duke@435 1144 size_t BinaryTreeDictionary::verifyPrevFreePtrs(TreeList* tl) {
duke@435 1145 size_t ct = 0;
duke@435 1146 for (FreeChunk* curFC = tl->head(); curFC != NULL; curFC = curFC->next()) {
duke@435 1147 ct++;
duke@435 1148 assert(curFC->prev() == NULL || curFC->prev()->isFree(),
duke@435 1149 "Chunk should be free");
duke@435 1150 }
duke@435 1151 return ct;
duke@435 1152 }
duke@435 1153
duke@435 1154 // Note: this helper is recursive rather than iterative, so use with
duke@435 1155 // caution on very deep trees; and watch out for stack overflow errors;
duke@435 1156 // In general, to be used only for debugging.
duke@435 1157 void BinaryTreeDictionary::verifyTreeHelper(TreeList* tl) const {
duke@435 1158 if (tl == NULL)
duke@435 1159 return;
duke@435 1160 guarantee(tl->size() != 0, "A list must has a size");
duke@435 1161 guarantee(tl->left() == NULL || tl->left()->parent() == tl,
duke@435 1162 "parent<-/->left");
duke@435 1163 guarantee(tl->right() == NULL || tl->right()->parent() == tl,
duke@435 1164 "parent<-/->right");;
duke@435 1165 guarantee(tl->left() == NULL || tl->left()->size() < tl->size(),
duke@435 1166 "parent !> left");
duke@435 1167 guarantee(tl->right() == NULL || tl->right()->size() > tl->size(),
duke@435 1168 "parent !< left");
duke@435 1169 guarantee(tl->head() == NULL || tl->head()->isFree(), "!Free");
duke@435 1170 guarantee(tl->head() == NULL || tl->head_as_TreeChunk()->list() == tl,
duke@435 1171 "list inconsistency");
duke@435 1172 guarantee(tl->count() > 0 || (tl->head() == NULL && tl->tail() == NULL),
duke@435 1173 "list count is inconsistent");
duke@435 1174 guarantee(tl->count() > 1 || tl->head() == tl->tail(),
duke@435 1175 "list is incorrectly constructed");
duke@435 1176 size_t count = verifyPrevFreePtrs(tl);
duke@435 1177 guarantee(count == (size_t)tl->count(), "Node count is incorrect");
duke@435 1178 if (tl->head() != NULL) {
duke@435 1179 tl->head_as_TreeChunk()->verifyTreeChunkList();
duke@435 1180 }
duke@435 1181 verifyTreeHelper(tl->left());
duke@435 1182 verifyTreeHelper(tl->right());
duke@435 1183 }
duke@435 1184
duke@435 1185 void BinaryTreeDictionary::verify() const {
duke@435 1186 verifyTree();
duke@435 1187 guarantee(totalSize() == totalSizeInTree(root()), "Total Size inconsistency");
duke@435 1188 }

mercurial