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

Fri, 29 Feb 2008 14:42:56 -0800

author
ysr
date
Fri, 29 Feb 2008 14:42:56 -0800
changeset 447
6432c3bb6240
parent 435
a61af66fc99e
child 631
d1605aabd0a1
child 698
12eea04c8b06
permissions
-rw-r--r--

6668743: CMS: Consolidate block statistics reporting code
Summary: Reduce the amount of related code replication and improve pretty printing.
Reviewed-by: jmasa

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

mercurial