src/share/vm/memory/binaryTreeDictionary.hpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

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

mercurial