Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/src/share/vm/memory/binaryTreeDictionary.hpp
48693 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"2930/*31* A binary tree based search structure for free blocks.32* This is currently used in the Concurrent Mark&Sweep implementation, but33* will be used for free block management for metadata.34*/3536// A TreeList is a FreeList which can be used to maintain a37// binary tree of free lists.3839template <class Chunk_t, class FreeList_t> class TreeChunk;40template <class Chunk_t, class FreeList_t> class BinaryTreeDictionary;41template <class Chunk_t, class FreeList_t> class AscendTreeCensusClosure;42template <class Chunk_t, class FreeList_t> class DescendTreeCensusClosure;43template <class Chunk_t, class FreeList_t> class DescendTreeSearchClosure;4445class FreeChunk;46template <class> class AdaptiveFreeList;47typedef BinaryTreeDictionary<FreeChunk, AdaptiveFreeList<FreeChunk> > AFLBinaryTreeDictionary;4849template <class Chunk_t, class FreeList_t>50class TreeList : public FreeList_t {51friend class TreeChunk<Chunk_t, FreeList_t>;52friend class BinaryTreeDictionary<Chunk_t, FreeList_t>;53friend class AscendTreeCensusClosure<Chunk_t, FreeList_t>;54friend class DescendTreeCensusClosure<Chunk_t, FreeList_t>;55friend class DescendTreeSearchClosure<Chunk_t, FreeList_t>;5657TreeList<Chunk_t, FreeList_t>* _parent;58TreeList<Chunk_t, FreeList_t>* _left;59TreeList<Chunk_t, FreeList_t>* _right;6061protected:6263TreeList<Chunk_t, FreeList_t>* parent() const { return _parent; }64TreeList<Chunk_t, FreeList_t>* left() const { return _left; }65TreeList<Chunk_t, FreeList_t>* right() const { return _right; }6667// Wrapper on call to base class, to get the template to compile.68Chunk_t* head() const { return FreeList_t::head(); }69Chunk_t* tail() const { return FreeList_t::tail(); }70void set_head(Chunk_t* head) { FreeList_t::set_head(head); }71void set_tail(Chunk_t* tail) { FreeList_t::set_tail(tail); }7273size_t size() const { return FreeList_t::size(); }7475// Accessors for links in tree.7677void set_left(TreeList<Chunk_t, FreeList_t>* tl) {78_left = tl;79if (tl != NULL)80tl->set_parent(this);81}82void set_right(TreeList<Chunk_t, FreeList_t>* tl) {83_right = tl;84if (tl != NULL)85tl->set_parent(this);86}87void set_parent(TreeList<Chunk_t, FreeList_t>* tl) { _parent = tl; }8889void clear_left() { _left = NULL; }90void clear_right() { _right = NULL; }91void clear_parent() { _parent = NULL; }92void initialize() { clear_left(); clear_right(), clear_parent(); FreeList_t::initialize(); }9394// For constructing a TreeList from a Tree chunk or95// address and size.96TreeList();97static TreeList<Chunk_t, FreeList_t>*98as_TreeList(TreeChunk<Chunk_t, FreeList_t>* tc);99static TreeList<Chunk_t, FreeList_t>* as_TreeList(HeapWord* addr, size_t size);100101// Returns the head of the free list as a pointer to a TreeChunk.102TreeChunk<Chunk_t, FreeList_t>* head_as_TreeChunk();103104// Returns the first available chunk in the free list as a pointer105// to a TreeChunk.106TreeChunk<Chunk_t, FreeList_t>* first_available();107108// Returns the block with the largest heap address amongst109// those in the list for this size; potentially slow and expensive,110// use with caution!111TreeChunk<Chunk_t, FreeList_t>* largest_address();112113TreeList<Chunk_t, FreeList_t>* get_better_list(114BinaryTreeDictionary<Chunk_t, FreeList_t>* dictionary);115116// remove_chunk_replace_if_needed() removes the given "tc" from the TreeList.117// If "tc" is the first chunk in the list, it is also the118// TreeList that is the node in the tree. remove_chunk_replace_if_needed()119// returns the possibly replaced TreeList* for the node in120// the tree. It also updates the parent of the original121// node to point to the new node.122TreeList<Chunk_t, FreeList_t>* remove_chunk_replace_if_needed(TreeChunk<Chunk_t, FreeList_t>* tc);123// See FreeList.124void return_chunk_at_head(TreeChunk<Chunk_t, FreeList_t>* tc);125void return_chunk_at_tail(TreeChunk<Chunk_t, FreeList_t>* tc);126};127128// A TreeChunk is a subclass of a Chunk that additionally129// maintains a pointer to the free list on which it is currently130// linked.131// A TreeChunk is also used as a node in the binary tree. This132// allows the binary tree to be maintained without any additional133// storage (the free chunks are used). In a binary tree the first134// chunk in the free list is also the tree node. Note that the135// TreeChunk has an embedded TreeList for this purpose. Because136// the first chunk in the list is distinguished in this fashion137// (also is the node in the tree), it is the last chunk to be found138// on the free list for a node in the tree and is only removed if139// it is the last chunk on the free list.140141template <class Chunk_t, class FreeList_t>142class TreeChunk : public Chunk_t {143friend class TreeList<Chunk_t, FreeList_t>;144TreeList<Chunk_t, FreeList_t>* _list;145TreeList<Chunk_t, FreeList_t> _embedded_list; // if non-null, this chunk is on _list146147static size_t _min_tree_chunk_size;148149protected:150TreeList<Chunk_t, FreeList_t>* embedded_list() const { return (TreeList<Chunk_t, FreeList_t>*) &_embedded_list; }151void set_embedded_list(TreeList<Chunk_t, FreeList_t>* v) { _embedded_list = *v; }152public:153TreeList<Chunk_t, FreeList_t>* list() { return _list; }154void set_list(TreeList<Chunk_t, FreeList_t>* v) { _list = v; }155static TreeChunk<Chunk_t, FreeList_t>* as_TreeChunk(Chunk_t* fc);156// Initialize fields in a TreeChunk that should be157// initialized when the TreeChunk is being added to158// a free list in the tree.159void initialize() { embedded_list()->initialize(); }160161Chunk_t* next() const { return Chunk_t::next(); }162Chunk_t* prev() const { return Chunk_t::prev(); }163size_t size() const volatile { return Chunk_t::size(); }164165static size_t min_size() {166return _min_tree_chunk_size;167}168169// debugging170void verify_tree_chunk_list() const;171void assert_is_mangled() const;172};173174175template <class Chunk_t, class FreeList_t>176class BinaryTreeDictionary: public FreeBlockDictionary<Chunk_t> {177friend class VMStructs;178size_t _total_size;179size_t _total_free_blocks;180TreeList<Chunk_t, FreeList_t>* _root;181182// private accessors183void set_total_size(size_t v) { _total_size = v; }184virtual void inc_total_size(size_t v);185virtual void dec_total_size(size_t v);186void set_total_free_blocks(size_t v) { _total_free_blocks = v; }187TreeList<Chunk_t, FreeList_t>* root() const { return _root; }188void set_root(TreeList<Chunk_t, FreeList_t>* v) { _root = v; }189190// This field is added and can be set to point to the191// the Mutex used to synchronize access to the192// dictionary so that assertion checking can be done.193// For example it is set to point to _parDictionaryAllocLock.194NOT_PRODUCT(Mutex* _lock;)195196// Remove a chunk of size "size" or larger from the tree and197// return it. If the chunk198// is the last chunk of that size, remove the node for that size199// from the tree.200TreeChunk<Chunk_t, FreeList_t>* get_chunk_from_tree(size_t size, enum FreeBlockDictionary<Chunk_t>::Dither dither);201// Remove this chunk from the tree. If the removal results202// in an empty list in the tree, remove the empty list.203TreeChunk<Chunk_t, FreeList_t>* remove_chunk_from_tree(TreeChunk<Chunk_t, FreeList_t>* tc);204// Remove the node in the trees starting at tl that has the205// minimum value and return it. Repair the tree as needed.206TreeList<Chunk_t, FreeList_t>* remove_tree_minimum(TreeList<Chunk_t, FreeList_t>* tl);207// Add this free chunk to the tree.208void insert_chunk_in_tree(Chunk_t* freeChunk);209public:210211// Return a list of the specified size or NULL from the tree.212// The list is not removed from the tree.213TreeList<Chunk_t, FreeList_t>* find_list (size_t size) const;214215void verify_tree() const;216// verify that the given chunk is in the tree.217bool verify_chunk_in_free_list(Chunk_t* tc) const;218private:219void verify_tree_helper(TreeList<Chunk_t, FreeList_t>* tl) const;220static size_t verify_prev_free_ptrs(TreeList<Chunk_t, FreeList_t>* tl);221222// Returns the total number of chunks in the list.223size_t total_list_length(TreeList<Chunk_t, FreeList_t>* tl) const;224// Returns the total number of words in the chunks in the tree225// starting at "tl".226size_t total_size_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const;227// Returns the sum of the square of the size of each block228// in the tree starting at "tl".229double sum_of_squared_block_sizes(TreeList<Chunk_t, FreeList_t>* const tl) const;230// Returns the total number of free blocks in the tree starting231// at "tl".232size_t total_free_blocks_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const;233size_t num_free_blocks() const;234size_t tree_height() const;235size_t tree_height_helper(TreeList<Chunk_t, FreeList_t>* tl) const;236size_t total_nodes_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const;237size_t total_nodes_helper(TreeList<Chunk_t, FreeList_t>* tl) const;238239public:240// Constructor241BinaryTreeDictionary() :242_total_size(0), _total_free_blocks(0), _root(0) {}243244BinaryTreeDictionary(MemRegion mr);245246// Public accessors247size_t total_size() const { return _total_size; }248size_t total_free_blocks() const { return _total_free_blocks; }249250// Reset the dictionary to the initial conditions with251// a single free chunk.252void reset(MemRegion mr);253void reset(HeapWord* addr, size_t size);254// Reset the dictionary to be empty.255void reset();256257// Return a chunk of size "size" or greater from258// the tree.259Chunk_t* get_chunk(size_t size, enum FreeBlockDictionary<Chunk_t>::Dither dither) {260FreeBlockDictionary<Chunk_t>::verify_par_locked();261Chunk_t* res = get_chunk_from_tree(size, dither);262assert(res == NULL || res->is_free(),263"Should be returning a free chunk");264assert(dither != FreeBlockDictionary<Chunk_t>::exactly ||265res == NULL || res->size() == size, "Not correct size");266return res;267}268269void return_chunk(Chunk_t* chunk) {270FreeBlockDictionary<Chunk_t>::verify_par_locked();271insert_chunk_in_tree(chunk);272}273274void remove_chunk(Chunk_t* chunk) {275FreeBlockDictionary<Chunk_t>::verify_par_locked();276remove_chunk_from_tree((TreeChunk<Chunk_t, FreeList_t>*)chunk);277assert(chunk->is_free(), "Should still be a free chunk");278}279280size_t max_chunk_size() const;281size_t total_chunk_size(debug_only(const Mutex* lock)) const {282debug_only(283if (lock != NULL && lock->owned_by_self()) {284assert(total_size_in_tree(root()) == total_size(),285"_total_size inconsistency");286}287)288return total_size();289}290291size_t min_size() const {292return TreeChunk<Chunk_t, FreeList_t>::min_size();293}294295double sum_of_squared_block_sizes() const {296return sum_of_squared_block_sizes(root());297}298299Chunk_t* find_chunk_ends_at(HeapWord* target) const;300301// Find the list with size "size" in the binary tree and update302// the statistics in the list according to "split" (chunk was303// split or coalesce) and "birth" (chunk was added or removed).304void dict_census_update(size_t size, bool split, bool birth);305// Return true if the dictionary is overpopulated (more chunks of306// this size than desired) for size "size".307bool coal_dict_over_populated(size_t size);308// Methods called at the beginning of a sweep to prepare the309// statistics for the sweep.310void begin_sweep_dict_census(double coalSurplusPercent,311float inter_sweep_current,312float inter_sweep_estimate,313float intra_sweep_estimate);314// Methods called after the end of a sweep to modify the315// statistics for the sweep.316void end_sweep_dict_census(double splitSurplusPercent);317// Return the largest free chunk in the tree.318Chunk_t* find_largest_dict() const;319// Accessors for statistics320void set_tree_surplus(double splitSurplusPercent);321void set_tree_hints(void);322// Reset statistics for all the lists in the tree.323void clear_tree_census(void);324// Print the statistcis for all the lists in the tree. Also may325// print out summaries.326void print_dict_census(void) const;327void print_free_lists(outputStream* st) const;328329// For debugging. Returns the sum of the _returned_bytes for330// all lists in the tree.331size_t sum_dict_returned_bytes() PRODUCT_RETURN0;332// Sets the _returned_bytes for all the lists in the tree to zero.333void initialize_dict_returned_bytes() PRODUCT_RETURN;334// For debugging. Return the total number of chunks in the dictionary.335size_t total_count() PRODUCT_RETURN0;336337void report_statistics() const;338339void verify() const;340};341342#endif // SHARE_VM_MEMORY_BINARYTREEDICTIONARY_HPP343344345