src/share/vm/memory/binaryTreeDictionary.hpp

Thu, 29 Mar 2012 19:46:24 -0700

author
jmasa
date
Thu, 29 Mar 2012 19:46:24 -0700
changeset 3730
9f059abe8cf2
parent 2314
src/share/vm/gc_implementation/concurrentMarkSweep/binaryTreeDictionary.hpp@f95d63e2154a
child 3732
f69a5d43dc19
permissions
-rw-r--r--

7131629: Generalize the CMS free list code
Summary: Make the FreeChunk, FreeList, TreeList, and BinaryTreeDictionary classes usable outside CMS.
Reviewed-by: brutisso, johnc, jwilhelm
Contributed-by: coleen.phillimore@oracle.com

duke@435 1 /*
jmasa@3730 2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
jmasa@3730 25 #ifndef SHARE_VM_MEMORY_BINARYTREEDICTIONARY_HPP
jmasa@3730 26 #define SHARE_VM_MEMORY_BINARYTREEDICTIONARY_HPP
stefank@2314 27
jmasa@3730 28 #include "memory/freeBlockDictionary.hpp"
jmasa@3730 29 #include "memory/freeList.hpp"
stefank@2314 30
duke@435 31 /*
duke@435 32 * A binary tree based search structure for free blocks.
jmasa@3730 33 * This is currently used in the Concurrent Mark&Sweep implementation, but
jmasa@3730 34 * will be used for free block management for metadata.
duke@435 35 */
duke@435 36
duke@435 37 // A TreeList is a FreeList which can be used to maintain a
duke@435 38 // binary tree of free lists.
duke@435 39
jmasa@3730 40 template <class Chunk> class TreeChunk;
jmasa@3730 41 template <class Chunk> class BinaryTreeDictionary;
jmasa@3730 42 template <class Chunk> class AscendTreeCensusClosure;
jmasa@3730 43 template <class Chunk> class DescendTreeCensusClosure;
jmasa@3730 44 template <class Chunk> class DescendTreeSearchClosure;
duke@435 45
jmasa@3730 46 template <class Chunk>
jmasa@3730 47 class TreeList: public FreeList<Chunk> {
jmasa@3730 48 friend class TreeChunk<Chunk>;
jmasa@3730 49 friend class BinaryTreeDictionary<Chunk>;
jmasa@3730 50 friend class AscendTreeCensusClosure<Chunk>;
jmasa@3730 51 friend class DescendTreeCensusClosure<Chunk>;
jmasa@3730 52 friend class DescendTreeSearchClosure<Chunk>;
jmasa@3730 53
jmasa@3730 54 TreeList<Chunk>* _parent;
jmasa@3730 55 TreeList<Chunk>* _left;
jmasa@3730 56 TreeList<Chunk>* _right;
duke@435 57
duke@435 58 protected:
jmasa@3730 59 TreeList<Chunk>* parent() const { return _parent; }
jmasa@3730 60 TreeList<Chunk>* left() const { return _left; }
jmasa@3730 61 TreeList<Chunk>* right() const { return _right; }
jmasa@3730 62
jmasa@3730 63 // Wrapper on call to base class, to get the template to compile.
jmasa@3730 64 Chunk* head() const { return FreeList<Chunk>::head(); }
jmasa@3730 65 Chunk* tail() const { return FreeList<Chunk>::tail(); }
jmasa@3730 66 void set_head(Chunk* head) { FreeList<Chunk>::set_head(head); }
jmasa@3730 67 void set_tail(Chunk* tail) { FreeList<Chunk>::set_tail(tail); }
jmasa@3730 68
jmasa@3730 69 size_t size() const { return FreeList<Chunk>::size(); }
duke@435 70
duke@435 71 // Accessors for links in tree.
duke@435 72
jmasa@3730 73 void setLeft(TreeList<Chunk>* tl) {
duke@435 74 _left = tl;
duke@435 75 if (tl != NULL)
duke@435 76 tl->setParent(this);
duke@435 77 }
jmasa@3730 78 void setRight(TreeList<Chunk>* tl) {
duke@435 79 _right = tl;
duke@435 80 if (tl != NULL)
duke@435 81 tl->setParent(this);
duke@435 82 }
jmasa@3730 83 void setParent(TreeList<Chunk>* tl) { _parent = tl; }
duke@435 84
duke@435 85 void clearLeft() { _left = NULL; }
duke@435 86 void clearRight() { _right = NULL; }
duke@435 87 void clearParent() { _parent = NULL; }
duke@435 88 void initialize() { clearLeft(); clearRight(), clearParent(); }
duke@435 89
duke@435 90 // For constructing a TreeList from a Tree chunk or
duke@435 91 // address and size.
jmasa@3730 92 static TreeList<Chunk>* as_TreeList(TreeChunk<Chunk>* tc);
jmasa@3730 93 static TreeList<Chunk>* as_TreeList(HeapWord* addr, size_t size);
duke@435 94
duke@435 95 // Returns the head of the free list as a pointer to a TreeChunk.
jmasa@3730 96 TreeChunk<Chunk>* head_as_TreeChunk();
duke@435 97
duke@435 98 // Returns the first available chunk in the free list as a pointer
duke@435 99 // to a TreeChunk.
jmasa@3730 100 TreeChunk<Chunk>* first_available();
duke@435 101
ysr@1580 102 // Returns the block with the largest heap address amongst
ysr@1580 103 // those in the list for this size; potentially slow and expensive,
ysr@1580 104 // use with caution!
jmasa@3730 105 TreeChunk<Chunk>* largest_address();
ysr@1580 106
duke@435 107 // removeChunkReplaceIfNeeded() removes the given "tc" from the TreeList.
duke@435 108 // If "tc" is the first chunk in the list, it is also the
duke@435 109 // TreeList that is the node in the tree. removeChunkReplaceIfNeeded()
duke@435 110 // returns the possibly replaced TreeList* for the node in
duke@435 111 // the tree. It also updates the parent of the original
duke@435 112 // node to point to the new node.
jmasa@3730 113 TreeList<Chunk>* removeChunkReplaceIfNeeded(TreeChunk<Chunk>* tc);
duke@435 114 // See FreeList.
jmasa@3730 115 void returnChunkAtHead(TreeChunk<Chunk>* tc);
jmasa@3730 116 void returnChunkAtTail(TreeChunk<Chunk>* tc);
duke@435 117 };
duke@435 118
jmasa@3730 119 // A TreeChunk is a subclass of a Chunk that additionally
duke@435 120 // maintains a pointer to the free list on which it is currently
duke@435 121 // linked.
duke@435 122 // A TreeChunk is also used as a node in the binary tree. This
duke@435 123 // allows the binary tree to be maintained without any additional
duke@435 124 // storage (the free chunks are used). In a binary tree the first
duke@435 125 // chunk in the free list is also the tree node. Note that the
duke@435 126 // TreeChunk has an embedded TreeList for this purpose. Because
duke@435 127 // the first chunk in the list is distinguished in this fashion
duke@435 128 // (also is the node in the tree), it is the last chunk to be found
duke@435 129 // on the free list for a node in the tree and is only removed if
duke@435 130 // it is the last chunk on the free list.
duke@435 131
jmasa@3730 132 template <class Chunk>
jmasa@3730 133 class TreeChunk : public Chunk {
jmasa@3730 134 friend class TreeList<Chunk>;
jmasa@3730 135 TreeList<Chunk>* _list;
jmasa@3730 136 TreeList<Chunk> _embedded_list; // if non-null, this chunk is on _list
duke@435 137 protected:
jmasa@3730 138 TreeList<Chunk>* embedded_list() const { return (TreeList<Chunk>*) &_embedded_list; }
jmasa@3730 139 void set_embedded_list(TreeList<Chunk>* v) { _embedded_list = *v; }
duke@435 140 public:
jmasa@3730 141 TreeList<Chunk>* list() { return _list; }
jmasa@3730 142 void set_list(TreeList<Chunk>* v) { _list = v; }
jmasa@3730 143 static TreeChunk<Chunk>* as_TreeChunk(Chunk* fc);
duke@435 144 // Initialize fields in a TreeChunk that should be
duke@435 145 // initialized when the TreeChunk is being added to
duke@435 146 // a free list in the tree.
duke@435 147 void initialize() { embedded_list()->initialize(); }
duke@435 148
jmasa@3730 149 Chunk* next() const { return Chunk::next(); }
jmasa@3730 150 Chunk* prev() const { return Chunk::prev(); }
jmasa@3730 151 size_t size() const volatile { return Chunk::size(); }
jmasa@3730 152
duke@435 153 // debugging
duke@435 154 void verifyTreeChunkList() const;
duke@435 155 };
duke@435 156
duke@435 157
jmasa@3730 158 template <class Chunk>
jmasa@3730 159 class BinaryTreeDictionary: public FreeBlockDictionary<Chunk> {
dcubed@587 160 friend class VMStructs;
duke@435 161 bool _splay;
duke@435 162 size_t _totalSize;
duke@435 163 size_t _totalFreeBlocks;
jmasa@3730 164 TreeList<Chunk>* _root;
jmasa@3730 165 bool _adaptive_freelists;
duke@435 166
duke@435 167 // private accessors
duke@435 168 bool splay() const { return _splay; }
duke@435 169 void set_splay(bool v) { _splay = v; }
duke@435 170 void set_totalSize(size_t v) { _totalSize = v; }
duke@435 171 virtual void inc_totalSize(size_t v);
duke@435 172 virtual void dec_totalSize(size_t v);
duke@435 173 size_t totalFreeBlocks() const { return _totalFreeBlocks; }
duke@435 174 void set_totalFreeBlocks(size_t v) { _totalFreeBlocks = v; }
jmasa@3730 175 TreeList<Chunk>* root() const { return _root; }
jmasa@3730 176 void set_root(TreeList<Chunk>* v) { _root = v; }
jmasa@3730 177 bool adaptive_freelists() { return _adaptive_freelists; }
jmasa@3730 178
jmasa@3730 179 // This field is added and can be set to point to the
jmasa@3730 180 // the Mutex used to synchronize access to the
jmasa@3730 181 // dictionary so that assertion checking can be done.
jmasa@3730 182 // For example it is set to point to _parDictionaryAllocLock.
jmasa@3730 183 NOT_PRODUCT(Mutex* _lock;)
duke@435 184
duke@435 185 // Remove a chunk of size "size" or larger from the tree and
duke@435 186 // return it. If the chunk
duke@435 187 // is the last chunk of that size, remove the node for that size
duke@435 188 // from the tree.
jmasa@3730 189 TreeChunk<Chunk>* getChunkFromTree(size_t size, enum FreeBlockDictionary<Chunk>::Dither dither, bool splay);
duke@435 190 // Return a list of the specified size or NULL from the tree.
duke@435 191 // The list is not removed from the tree.
jmasa@3730 192 TreeList<Chunk>* findList (size_t size) const;
duke@435 193 // Remove this chunk from the tree. If the removal results
duke@435 194 // in an empty list in the tree, remove the empty list.
jmasa@3730 195 TreeChunk<Chunk>* removeChunkFromTree(TreeChunk<Chunk>* tc);
duke@435 196 // Remove the node in the trees starting at tl that has the
duke@435 197 // minimum value and return it. Repair the tree as needed.
jmasa@3730 198 TreeList<Chunk>* removeTreeMinimum(TreeList<Chunk>* tl);
jmasa@3730 199 void semiSplayStep(TreeList<Chunk>* tl);
duke@435 200 // Add this free chunk to the tree.
jmasa@3730 201 void insertChunkInTree(Chunk* freeChunk);
duke@435 202 public:
jmasa@3730 203
jmasa@3730 204 static const size_t min_tree_chunk_size = sizeof(TreeChunk<Chunk>)/HeapWordSize;
jmasa@3730 205
duke@435 206 void verifyTree() const;
duke@435 207 // verify that the given chunk is in the tree.
jmasa@3730 208 bool verifyChunkInFreeLists(Chunk* tc) const;
duke@435 209 private:
jmasa@3730 210 void verifyTreeHelper(TreeList<Chunk>* tl) const;
jmasa@3730 211 static size_t verifyPrevFreePtrs(TreeList<Chunk>* tl);
duke@435 212
duke@435 213 // Returns the total number of chunks in the list.
jmasa@3730 214 size_t totalListLength(TreeList<Chunk>* tl) const;
duke@435 215 // Returns the total number of words in the chunks in the tree
duke@435 216 // starting at "tl".
jmasa@3730 217 size_t totalSizeInTree(TreeList<Chunk>* tl) const;
duke@435 218 // Returns the sum of the square of the size of each block
duke@435 219 // in the tree starting at "tl".
jmasa@3730 220 double sum_of_squared_block_sizes(TreeList<Chunk>* const tl) const;
duke@435 221 // Returns the total number of free blocks in the tree starting
duke@435 222 // at "tl".
jmasa@3730 223 size_t totalFreeBlocksInTree(TreeList<Chunk>* tl) const;
duke@435 224 size_t numFreeBlocks() const;
duke@435 225 size_t treeHeight() const;
jmasa@3730 226 size_t treeHeightHelper(TreeList<Chunk>* tl) const;
jmasa@3730 227 size_t totalNodesInTree(TreeList<Chunk>* tl) const;
jmasa@3730 228 size_t totalNodesHelper(TreeList<Chunk>* tl) const;
duke@435 229
duke@435 230 public:
duke@435 231 // Constructor
jmasa@3730 232 BinaryTreeDictionary(bool adaptive_freelists, bool splay = false);
jmasa@3730 233 BinaryTreeDictionary(MemRegion mr, bool adaptive_freelists, bool splay = false);
jmasa@3730 234
jmasa@3730 235 // Public accessors
jmasa@3730 236 size_t totalSize() const { return _totalSize; }
duke@435 237
duke@435 238 // Reset the dictionary to the initial conditions with
duke@435 239 // a single free chunk.
duke@435 240 void reset(MemRegion mr);
duke@435 241 void reset(HeapWord* addr, size_t size);
duke@435 242 // Reset the dictionary to be empty.
duke@435 243 void reset();
duke@435 244
duke@435 245 // Return a chunk of size "size" or greater from
duke@435 246 // the tree.
duke@435 247 // want a better dynamic splay strategy for the future.
jmasa@3730 248 Chunk* getChunk(size_t size, enum FreeBlockDictionary<Chunk>::Dither dither) {
jmasa@3730 249 FreeBlockDictionary<Chunk>::verify_par_locked();
jmasa@3730 250 Chunk* res = getChunkFromTree(size, dither, splay());
duke@435 251 assert(res == NULL || res->isFree(),
duke@435 252 "Should be returning a free chunk");
duke@435 253 return res;
duke@435 254 }
duke@435 255
jmasa@3730 256 void returnChunk(Chunk* chunk) {
jmasa@3730 257 FreeBlockDictionary<Chunk>::verify_par_locked();
duke@435 258 insertChunkInTree(chunk);
duke@435 259 }
duke@435 260
jmasa@3730 261 void removeChunk(Chunk* chunk) {
jmasa@3730 262 FreeBlockDictionary<Chunk>::verify_par_locked();
jmasa@3730 263 removeChunkFromTree((TreeChunk<Chunk>*)chunk);
duke@435 264 assert(chunk->isFree(), "Should still be a free chunk");
duke@435 265 }
duke@435 266
duke@435 267 size_t maxChunkSize() const;
duke@435 268 size_t totalChunkSize(debug_only(const Mutex* lock)) const {
duke@435 269 debug_only(
duke@435 270 if (lock != NULL && lock->owned_by_self()) {
duke@435 271 assert(totalSizeInTree(root()) == totalSize(),
duke@435 272 "_totalSize inconsistency");
duke@435 273 }
duke@435 274 )
duke@435 275 return totalSize();
duke@435 276 }
duke@435 277
duke@435 278 size_t minSize() const {
jmasa@3730 279 return min_tree_chunk_size;
duke@435 280 }
duke@435 281
duke@435 282 double sum_of_squared_block_sizes() const {
duke@435 283 return sum_of_squared_block_sizes(root());
duke@435 284 }
duke@435 285
jmasa@3730 286 Chunk* find_chunk_ends_at(HeapWord* target) const;
duke@435 287
duke@435 288 // Find the list with size "size" in the binary tree and update
duke@435 289 // the statistics in the list according to "split" (chunk was
duke@435 290 // split or coalesce) and "birth" (chunk was added or removed).
duke@435 291 void dictCensusUpdate(size_t size, bool split, bool birth);
duke@435 292 // Return true if the dictionary is overpopulated (more chunks of
duke@435 293 // this size than desired) for size "size".
duke@435 294 bool coalDictOverPopulated(size_t size);
duke@435 295 // Methods called at the beginning of a sweep to prepare the
duke@435 296 // statistics for the sweep.
duke@435 297 void beginSweepDictCensus(double coalSurplusPercent,
ysr@1580 298 float inter_sweep_current,
ysr@1580 299 float inter_sweep_estimate,
ysr@1580 300 float intra_sweep_estimate);
duke@435 301 // Methods called after the end of a sweep to modify the
duke@435 302 // statistics for the sweep.
duke@435 303 void endSweepDictCensus(double splitSurplusPercent);
duke@435 304 // Return the largest free chunk in the tree.
jmasa@3730 305 Chunk* findLargestDict() const;
duke@435 306 // Accessors for statistics
duke@435 307 void setTreeSurplus(double splitSurplusPercent);
duke@435 308 void setTreeHints(void);
duke@435 309 // Reset statistics for all the lists in the tree.
duke@435 310 void clearTreeCensus(void);
duke@435 311 // Print the statistcis for all the lists in the tree. Also may
duke@435 312 // print out summaries.
duke@435 313 void printDictCensus(void) const;
ysr@1580 314 void print_free_lists(outputStream* st) const;
duke@435 315
duke@435 316 // For debugging. Returns the sum of the _returnedBytes for
duke@435 317 // all lists in the tree.
duke@435 318 size_t sumDictReturnedBytes() PRODUCT_RETURN0;
duke@435 319 // Sets the _returnedBytes for all the lists in the tree to zero.
duke@435 320 void initializeDictReturnedBytes() PRODUCT_RETURN;
duke@435 321 // For debugging. Return the total number of chunks in the dictionary.
duke@435 322 size_t totalCount() PRODUCT_RETURN0;
duke@435 323
duke@435 324 void reportStatistics() const;
duke@435 325
duke@435 326 void verify() const;
duke@435 327 };
stefank@2314 328
jmasa@3730 329 #endif // SHARE_VM_MEMORY_BINARYTREEDICTIONARY_HPP

mercurial