src/share/vm/memory/binaryTreeDictionary.cpp

Mon, 03 Dec 2012 15:09:39 -0800

author
jmasa
date
Mon, 03 Dec 2012 15:09:39 -0800
changeset 4382
e51c9860cf66
parent 4297
19c1bd641922
child 4488
3c327c2b6782
child 4542
db9981fd3124
permissions
-rw-r--r--

8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
Reviewed-by: johnc, coleenp

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

mercurial