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

Fri, 10 Sep 2010 17:07:55 -0700

author
ysr
date
Fri, 10 Sep 2010 17:07:55 -0700
changeset 2132
179464550c7d
parent 1907
c18cbe5936b8
child 2314
f95d63e2154a
permissions
-rw-r--r--

6983930: CMS: Various small cleanups ca September 2010
Summary: Fixed comment/documentation typos; converted some guarantee()s to assert()s.
Reviewed-by: jmasa

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

mercurial