src/share/vm/memory/binaryTreeDictionary.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

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

mercurial