Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/binaryTreeDictionary.hpp
32285 views
/*1* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_MEMORY_BINARYTREEDICTIONARY_HPP25#define SHARE_VM_MEMORY_BINARYTREEDICTIONARY_HPP2627#include "memory/freeBlockDictionary.hpp"28#include "memory/freeList.hpp"29#include "memory/memRegion.hpp"3031/*32* A binary tree based search structure for free blocks.33* This is currently used in the Concurrent Mark&Sweep implementation, but34* will be used for free block management for metadata.35*/3637// A TreeList is a FreeList which can be used to maintain a38// binary tree of free lists.3940template <class Chunk_t, class FreeList_t> class TreeChunk;41template <class Chunk_t, class FreeList_t> class BinaryTreeDictionary;42template <class Chunk_t, class FreeList_t> class AscendTreeCensusClosure;43template <class Chunk_t, class FreeList_t> class DescendTreeCensusClosure;44template <class Chunk_t, class FreeList_t> class DescendTreeSearchClosure;4546class FreeChunk;47template <class> class AdaptiveFreeList;48typedef BinaryTreeDictionary<FreeChunk, AdaptiveFreeList<FreeChunk> > AFLBinaryTreeDictionary;4950template <class Chunk_t, class FreeList_t>51class TreeList : public FreeList_t {52friend class TreeChunk<Chunk_t, FreeList_t>;53friend class BinaryTreeDictionary<Chunk_t, FreeList_t>;54friend class AscendTreeCensusClosure<Chunk_t, FreeList_t>;55friend class DescendTreeCensusClosure<Chunk_t, FreeList_t>;56friend class DescendTreeSearchClosure<Chunk_t, FreeList_t>;5758TreeList<Chunk_t, FreeList_t>* _parent;59TreeList<Chunk_t, FreeList_t>* _left;60TreeList<Chunk_t, FreeList_t>* _right;6162protected:6364TreeList<Chunk_t, FreeList_t>* parent() const { return _parent; }65TreeList<Chunk_t, FreeList_t>* left() const { return _left; }66TreeList<Chunk_t, FreeList_t>* right() const { return _right; }6768// Wrapper on call to base class, to get the template to compile.69Chunk_t* head() const { return FreeList_t::head(); }70Chunk_t* tail() const { return FreeList_t::tail(); }71void set_head(Chunk_t* head) { FreeList_t::set_head(head); }72void set_tail(Chunk_t* tail) { FreeList_t::set_tail(tail); }7374size_t size() const { return FreeList_t::size(); }7576// Accessors for links in tree.7778void set_left(TreeList<Chunk_t, FreeList_t>* tl) {79_left = tl;80if (tl != NULL)81tl->set_parent(this);82}83void set_right(TreeList<Chunk_t, FreeList_t>* tl) {84_right = tl;85if (tl != NULL)86tl->set_parent(this);87}88void set_parent(TreeList<Chunk_t, FreeList_t>* tl) { _parent = tl; }8990void clear_left() { _left = NULL; }91void clear_right() { _right = NULL; }92void clear_parent() { _parent = NULL; }93void initialize() { clear_left(); clear_right(), clear_parent(); FreeList_t::initialize(); }9495// For constructing a TreeList from a Tree chunk or96// address and size.97TreeList();98static TreeList<Chunk_t, FreeList_t>*99as_TreeList(TreeChunk<Chunk_t, FreeList_t>* tc);100static TreeList<Chunk_t, FreeList_t>* as_TreeList(HeapWord* addr, size_t size);101102// Returns the head of the free list as a pointer to a TreeChunk.103TreeChunk<Chunk_t, FreeList_t>* head_as_TreeChunk();104105// Returns the first available chunk in the free list as a pointer106// to a TreeChunk.107TreeChunk<Chunk_t, FreeList_t>* first_available();108109// Returns the block with the largest heap address amongst110// those in the list for this size; potentially slow and expensive,111// use with caution!112TreeChunk<Chunk_t, FreeList_t>* largest_address();113114TreeList<Chunk_t, FreeList_t>* get_better_list(115BinaryTreeDictionary<Chunk_t, FreeList_t>* dictionary);116117// remove_chunk_replace_if_needed() removes the given "tc" from the TreeList.118// If "tc" is the first chunk in the list, it is also the119// TreeList that is the node in the tree. remove_chunk_replace_if_needed()120// returns the possibly replaced TreeList* for the node in121// the tree. It also updates the parent of the original122// node to point to the new node.123TreeList<Chunk_t, FreeList_t>* remove_chunk_replace_if_needed(TreeChunk<Chunk_t, FreeList_t>* tc);124// See FreeList.125void return_chunk_at_head(TreeChunk<Chunk_t, FreeList_t>* tc);126void return_chunk_at_tail(TreeChunk<Chunk_t, FreeList_t>* tc);127};128129// A TreeChunk is a subclass of a Chunk that additionally130// maintains a pointer to the free list on which it is currently131// linked.132// A TreeChunk is also used as a node in the binary tree. This133// allows the binary tree to be maintained without any additional134// storage (the free chunks are used). In a binary tree the first135// chunk in the free list is also the tree node. Note that the136// TreeChunk has an embedded TreeList for this purpose. Because137// the first chunk in the list is distinguished in this fashion138// (also is the node in the tree), it is the last chunk to be found139// on the free list for a node in the tree and is only removed if140// it is the last chunk on the free list.141142template <class Chunk_t, class FreeList_t>143class TreeChunk : public Chunk_t {144friend class TreeList<Chunk_t, FreeList_t>;145TreeList<Chunk_t, FreeList_t>* _list;146TreeList<Chunk_t, FreeList_t> _embedded_list; // if non-null, this chunk is on _list147148static size_t _min_tree_chunk_size;149150protected:151TreeList<Chunk_t, FreeList_t>* embedded_list() const { return (TreeList<Chunk_t, FreeList_t>*) &_embedded_list; }152void set_embedded_list(TreeList<Chunk_t, FreeList_t>* v) { _embedded_list = *v; }153public:154TreeList<Chunk_t, FreeList_t>* list() { return _list; }155void set_list(TreeList<Chunk_t, FreeList_t>* v) { _list = v; }156static TreeChunk<Chunk_t, FreeList_t>* as_TreeChunk(Chunk_t* fc);157// Initialize fields in a TreeChunk that should be158// initialized when the TreeChunk is being added to159// a free list in the tree.160void initialize() { embedded_list()->initialize(); }161162Chunk_t* next() const { return Chunk_t::next(); }163Chunk_t* prev() const { return Chunk_t::prev(); }164size_t size() const volatile { return Chunk_t::size(); }165166static size_t min_size() {167return _min_tree_chunk_size;168}169170// debugging171void verify_tree_chunk_list() const;172void assert_is_mangled() const;173};174175176template <class Chunk_t, class FreeList_t>177class BinaryTreeDictionary: public FreeBlockDictionary<Chunk_t> {178friend class VMStructs;179size_t _total_size;180size_t _total_free_blocks;181TreeList<Chunk_t, FreeList_t>* _root;182183// private accessors184void set_total_size(size_t v) { _total_size = v; }185virtual void inc_total_size(size_t v);186virtual void dec_total_size(size_t v);187void set_total_free_blocks(size_t v) { _total_free_blocks = v; }188TreeList<Chunk_t, FreeList_t>* root() const { return _root; }189void set_root(TreeList<Chunk_t, FreeList_t>* v) { _root = v; }190191// This field is added and can be set to point to the192// the Mutex used to synchronize access to the193// dictionary so that assertion checking can be done.194// For example it is set to point to _parDictionaryAllocLock.195NOT_PRODUCT(Mutex* _lock;)196197// Remove a chunk of size "size" or larger from the tree and198// return it. If the chunk199// is the last chunk of that size, remove the node for that size200// from the tree.201TreeChunk<Chunk_t, FreeList_t>* get_chunk_from_tree(size_t size, enum FreeBlockDictionary<Chunk_t>::Dither dither);202// Remove this chunk from the tree. If the removal results203// in an empty list in the tree, remove the empty list.204TreeChunk<Chunk_t, FreeList_t>* remove_chunk_from_tree(TreeChunk<Chunk_t, FreeList_t>* tc);205// Remove the node in the trees starting at tl that has the206// minimum value and return it. Repair the tree as needed.207TreeList<Chunk_t, FreeList_t>* remove_tree_minimum(TreeList<Chunk_t, FreeList_t>* tl);208// Add this free chunk to the tree.209void insert_chunk_in_tree(Chunk_t* freeChunk);210public:211212// Return a list of the specified size or NULL from the tree.213// The list is not removed from the tree.214TreeList<Chunk_t, FreeList_t>* find_list (size_t size) const;215216void verify_tree() const;217// verify that the given chunk is in the tree.218bool verify_chunk_in_free_list(Chunk_t* tc) const;219private:220void verify_tree_helper(TreeList<Chunk_t, FreeList_t>* tl) const;221static size_t verify_prev_free_ptrs(TreeList<Chunk_t, FreeList_t>* tl);222223// Returns the total number of chunks in the list.224size_t total_list_length(TreeList<Chunk_t, FreeList_t>* tl) const;225// Returns the total number of words in the chunks in the tree226// starting at "tl".227size_t total_size_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const;228// Returns the sum of the square of the size of each block229// in the tree starting at "tl".230double sum_of_squared_block_sizes(TreeList<Chunk_t, FreeList_t>* const tl) const;231// Returns the total number of free blocks in the tree starting232// at "tl".233size_t total_free_blocks_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const;234size_t num_free_blocks() const;235size_t tree_height() const;236size_t tree_height_helper(TreeList<Chunk_t, FreeList_t>* tl) const;237size_t total_nodes_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const;238size_t total_nodes_helper(TreeList<Chunk_t, FreeList_t>* tl) const;239240public:241// Constructor242BinaryTreeDictionary() :243_total_size(0), _total_free_blocks(0), _root(0) {}244245BinaryTreeDictionary(MemRegion mr);246247// Public accessors248size_t total_size() const { return _total_size; }249size_t total_free_blocks() const { return _total_free_blocks; }250251// Reset the dictionary to the initial conditions with252// a single free chunk.253void reset(MemRegion mr);254void reset(HeapWord* addr, size_t size);255// Reset the dictionary to be empty.256void reset();257258// Return a chunk of size "size" or greater from259// the tree.260Chunk_t* get_chunk(size_t size, enum FreeBlockDictionary<Chunk_t>::Dither dither) {261FreeBlockDictionary<Chunk_t>::verify_par_locked();262Chunk_t* res = get_chunk_from_tree(size, dither);263assert(res == NULL || res->is_free(),264"Should be returning a free chunk");265assert(dither != FreeBlockDictionary<Chunk_t>::exactly ||266res == NULL || res->size() == size, "Not correct size");267return res;268}269270void return_chunk(Chunk_t* chunk) {271FreeBlockDictionary<Chunk_t>::verify_par_locked();272insert_chunk_in_tree(chunk);273}274275void remove_chunk(Chunk_t* chunk) {276FreeBlockDictionary<Chunk_t>::verify_par_locked();277remove_chunk_from_tree((TreeChunk<Chunk_t, FreeList_t>*)chunk);278assert(chunk->is_free(), "Should still be a free chunk");279}280281size_t max_chunk_size() const;282size_t total_chunk_size(debug_only(const Mutex* lock)) const {283debug_only(284if (lock != NULL && lock->owned_by_self()) {285assert(total_size_in_tree(root()) == total_size(),286"_total_size inconsistency");287}288)289return total_size();290}291292size_t min_size() const {293return TreeChunk<Chunk_t, FreeList_t>::min_size();294}295296double sum_of_squared_block_sizes() const {297return sum_of_squared_block_sizes(root());298}299300Chunk_t* find_chunk_ends_at(HeapWord* target) const;301302// Find the list with size "size" in the binary tree and update303// the statistics in the list according to "split" (chunk was304// split or coalesce) and "birth" (chunk was added or removed).305void dict_census_update(size_t size, bool split, bool birth);306// Return true if the dictionary is overpopulated (more chunks of307// this size than desired) for size "size".308bool coal_dict_over_populated(size_t size);309// Methods called at the beginning of a sweep to prepare the310// statistics for the sweep.311void begin_sweep_dict_census(double coalSurplusPercent,312float inter_sweep_current,313float inter_sweep_estimate,314float intra_sweep_estimate);315// Methods called after the end of a sweep to modify the316// statistics for the sweep.317void end_sweep_dict_census(double splitSurplusPercent);318// Return the largest free chunk in the tree.319Chunk_t* find_largest_dict() const;320// Accessors for statistics321void set_tree_surplus(double splitSurplusPercent);322void set_tree_hints(void);323// Reset statistics for all the lists in the tree.324void clear_tree_census(void);325// Print the statistcis for all the lists in the tree. Also may326// print out summaries.327void print_dict_census(void) const;328void print_free_lists(outputStream* st) const;329330// For debugging. Returns the sum of the _returned_bytes for331// all lists in the tree.332size_t sum_dict_returned_bytes() PRODUCT_RETURN0;333// Sets the _returned_bytes for all the lists in the tree to zero.334void initialize_dict_returned_bytes() PRODUCT_RETURN;335// For debugging. Return the total number of chunks in the dictionary.336size_t total_count() PRODUCT_RETURN0;337338void report_statistics() const;339340void verify() const;341};342343#endif // SHARE_VM_MEMORY_BINARYTREEDICTIONARY_HPP344345346