src/share/vm/memory/binaryTreeDictionary.cpp

Tue, 24 Feb 2015 15:04:52 -0500

author
dlong
date
Tue, 24 Feb 2015 15:04:52 -0500
changeset 7598
ddce0b7cee93
parent 6680
78bbf4d43a14
child 6876
710a3c8b516e
permissions
-rw-r--r--

8072383: resolve conflicts between open and closed ports
Summary: refactor close to remove references to closed ports
Reviewed-by: kvn, simonis, sgehwolf, dholmes

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

mercurial