src/share/vm/memory/binaryTreeDictionary.cpp

Tue, 15 Oct 2013 14:28:51 +0200

author
stefank
date
Tue, 15 Oct 2013 14:28:51 +0200
changeset 5941
bdfbb1fb19ca
parent 5689
de88570fabfc
child 6337
ab36007d6358
permissions
-rw-r--r--

8026391: The Metachunk header wastes memory
Reviewed-by: coleenp, jmasa

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

mercurial