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