1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_MEMORY_METASPACE_CHUNKTREE_HPP
  26 #define SHARE_MEMORY_METASPACE_CHUNKTREE_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/metaspace/abstractPool.hpp"
  30 #include "memory/metaspace/chunkLevel.hpp"
  31 #include "memory/metaspace/metachunk.hpp"
  32 
  33 namespace metaspace {
  34 
  35 // Chunks live in a binary tree.
  36 //
  37 
  38 class ChunkClosure {
  39  public:
  40   // Return false to cancel traversal.
  41   virtual bool do_chunk(Metachunk* chunk) = 0;
  42 };
  43 
  44 
  45 class ChunkTree : public CHeapObj<mtClass> {
  46 
  47   typedef u2 ref_t;
  48 
  49   // Root is either a direct pointer to a Metachunk* (in that case, a root chunk of max. size)
  50   // or a pointer to a node.
  51   ref_t _root;
  52 
  53   struct btnode_t {
  54 
  55     ref_t parent;
  56     ref_t child[2];
  57 
  58   };
  59 
  60   typedef AbstractPool<btnode_t, ref_t> NodePoolType;
  61   typedef AbstractPool<Metachunk, ref_t> ChunkPoolType;
  62   NodePoolType _nodePool;
  63   ChunkPoolType _chunkPool;
  64 
  65   // The upper two bits of a reference encode information about it.
  66   // bit 0,1:  00 - reference is a btnode_t
  67   //           10 - reference is a free chunk
  68   //           11 - reference is a chunk in use.
  69   // This also means a reference has to get by with 14 bits. Which covers 16K, which is enough for both
  70   // chunk headers and nodes within one root chunk area.
  71   static const u2 highest_possible_index = (1 << 14) - 1;
  72   static const u2 node_marker = 0;
  73   static const u2 free_chunk_marker = 2;
  74   static const u2 used_chunk_marker = 3;
  75 
  76   static u2 get_raw_index_from_reference(ref_t ref)     { return 0x3FFF & ref; }
  77   static u2 get_info_from_reference(ref_t ref)          { return 0xc000 & ref; }
  78 
  79   static u2 encode_reference(u2 raw_idx, u2 info) {
  80     assert(raw_idx <= highest_possible_index, "invalid index");
  81     return (info << 14) | raw_idx;
  82   }
  83 
  84 #ifdef ASSERT
  85   static bool reference_is_node(ref_t ref)        { return get_info_from_reference(ref) == node_marker; }
  86   static bool reference_is_chunk(ref_t ref)       { u2 i = get_info_from_reference(ref); return i == free_chunk_marker || i == used_chunk_marker; }
  87   static bool reference_is_used_chunk(ref_t ref)  { return get_info_from_reference(ref) == used_chunk_marker; }
  88 
  89   void check_is_valid_node_ref(ref_t ref)  { assert(resolve_reference_to_node(ref) != NULL, "invalid node ref"); }
  90   void check_is_valid_chunk_ref(ref_t ref) { assert(resolve_reference_to_chunk(ref) != NULL, "invalid chunk ref"); }
  91   void check_is_valid_ref(ref_t ref);
  92 #endif
  93 
  94   static bool reference_is_free_chunk(ref_t ref)  { return get_info_from_reference(ref) == free_chunk_marker; }
  95 
  96   // Given a reference we know to be a node, resolve it to the node pointer.
  97   btnode_t* resolve_reference_to_node(ref_t ref) const {
  98     assert(reference_is_node(ref), "Not a node ref");
  99     return _nodePool.elem_at_index(get_raw_index_from_reference(ref));
 100   }
 101 
 102   // Allocate a new node. Node is uninitialized.
 103   // Returns pointer to node, and reference in ref.
 104   btnode_t* allocate_new_node() {
 105     return _nodePool.allocate_element();
 106   }
 107 
 108   // Given a node pointer, return its correctly encoded reference.
 109   ref_t encode_reference_for_node(const btnode_t* n) const {
 110     const u2 raw_idx = _nodePool.index_for_elem(n);
 111     return encode_reference(raw_idx, node_marker);
 112   }
 113 
 114   // Release a node to the pool.
 115   void release_node(btnode_t* n) {
 116     _nodePool.return_element(n);
 117   }
 118 
 119   // Given a reference we know to be a chunk, resolve it to the chunk pointer.
 120   Metachunk* resolve_reference_to_chunk(ref_t ref) const {
 121     assert(reference_is_chunk(ref), "Not a chunk ref");
 122     return _chunkPool.elem_at_index(get_raw_index_from_reference(ref));
 123   }
 124 
 125   // Allocate a new node. Node is uninitialized.
 126   // Returns pointer to node, and reference in ref.
 127   Metachunk* allocate_new_chunk() {
 128     return _chunkPool.allocate_element();
 129   }
 130 
 131   // Given a chunk pointer, return its correctly encoded reference.
 132   ref_t encode_reference_for_chunk(Metachunk* c, bool is_free) const {
 133     const u2 raw_idx = _chunkPool.index_for_elem(c);
 134     return encode_reference(raw_idx, is_free ? free_chunk_marker : used_chunk_marker);
 135   }
 136 
 137   // Release a chunk to the pool.
 138   void release_chunk(Metachunk* c) {
 139     _chunkPool.return_element(c);
 140   }
 141 
 142   //// Helpers for tree traversal ////
 143 
 144   class ConstChunkClosure;
 145   bool iterate_chunks_helper(ref_t ref, ChunkClosure* cc) const;
 146 
 147 #ifdef ASSERT
 148   // Verify a life node (one which lives in the tree).
 149   void verify_node(const btnode_t* n) const;
 150   // Helper for verify()
 151   void verify_helper(bool slow, ref_t ref, const MetaWord* p, int* num_chunks, int* num_nodes) const;
 152 #endif
 153 
 154   // Given a chunk c, split it once.
 155   //
 156   // The original chunk must not be part of a freelist.
 157   //
 158   // Returns pointer to the result chunk; updates the splinters array to return the splintered off chunk.
 159   //
 160   // Returns NULL if chunk cannot be split any further.
 161   Metachunk* split_once(Metachunk* c, Metachunk* splinters[chklvl::NUM_CHUNK_LEVELS]);
 162 
 163   // Given a chunk, attempt to merge it with its sibling if it is free.
 164   // Returns pointer to the result chunk if successful, NULL otherwise.
 165   //
 166   // Returns number of merged chunks, by chunk level, in num_merged array. These numbers
 167   // includes the original chunk.
 168   //
 169   // !!! Please note that if this method returns a non-NULL value, the
 170   // original chunk will be invalid and should not be accessed anymore! !!!
 171   Metachunk* merge_once(Metachunk* c, int num_merged[chklvl::NUM_CHUNK_LEVELS]);
 172 
 173 public:
 174 
 175   ChunkTree();
 176   virtual ~ChunkTree() {}
 177 
 178   // Initialize: allocate a root node and a root chunk header; return the
 179   // root chunk header. It will be partly initialized.
 180   // Note: this just allocates a memory-less header; memory itself is allocated inside VirtualSpaceNode.
 181   Metachunk* alloc_root_chunk_header();
 182 
 183   // Given a chunk c, split it recursively until you get a chunk of the given target_level.
 184   //
 185   // The original chunk must not be part of a freelist.
 186   //
 187   // Returns pointer to the result chunk; returns split off chunks in splinters array.
 188   //
 189   // Returns NULL if chunk cannot be split at least once.
 190   Metachunk* split(chklvl_t target_level, Metachunk* c, Metachunk* splinters[chklvl::NUM_CHUNK_LEVELS]);
 191 
 192   // Given a chunk, attempt to merge it recursively with its neighboring chunks.
 193   //
 194   // If successful (merged at least once), returns address of
 195   // the merged chunk; NULL otherwise.
 196   //
 197   // The merged chunks are removed from their freelist; the number of merged chunks is
 198   // returned, split by level, in num_merged array. Note that these numbers does not
 199   // include the original chunk.
 200   //
 201   // !!! Please note that if this method returns a non-NULL value, the
 202   // original chunk will be invalid and should not be accessed anymore! !!!
 203   Metachunk* merge(Metachunk* c, int num_merged[chklvl::NUM_CHUNK_LEVELS]);
 204 
 205   //// tree traversal ////
 206 
 207   // Iterate over all nodes in this tree. Returns true for complete traversal,
 208   // false if traversal was cancelled.
 209   bool iterate_chunks(ChunkClosure* cc) const;
 210 
 211 
 212   //// Debug stuff ////
 213 
 214   // Verify tree. If base != NULL, it should point to the location assumed
 215   // to be base of the first chunk.
 216   DEBUG_ONLY(void verify(bool slow, const MetaWord* base) const;)
 217 
 218   // Returns the footprint of this tree, in words.
 219   size_t memory_footprint_words() const;
 220 
 221 
 222 };
 223 
 224 
 225 ///////////////////////
 226 // An C-heap allocated array of chunk trees. Used to describe fragmentation over a range of multiple root chunks.
 227 class ChunkTreeArray {
 228 
 229   const MetaWord* const _base;
 230   const size_t _word_size;
 231 
 232   ChunkTree** _arr;
 233   int _num;
 234 
 235 #ifdef ASSERT
 236   void check_pointer(const MetaWord* p) const {
 237     assert(p >= _base && p < _base + _word_size, "Invalid pointer");
 238   }
 239 #endif
 240 
 241   int index_by_address(const MetaWord* p) const {
 242     DEBUG_ONLY(check_pointer(p);)
 243     return (p - _base) / chklvl::MAX_CHUNK_WORD_SIZE;
 244   }
 245 
 246 public:
 247 
 248   // Create an array of ChunkTree objects, all initialized to NULL, covering
 249   // a given memory range. Memory range must be aligned to size of root chunks.
 250   ChunkTreeArray(const MetaWord* base, size_t word_size);
 251 
 252   ~ChunkTreeArray();
 253 
 254   // Given a memory address into the range the trees cover, return the corresponding
 255   // tree. If none existed at this position, create it.
 256   ChunkTree* get_tree_by_address(const MetaWord* p) const {
 257     assert(p >= _base && p < _base + _word_size, "Invalid pointer");
 258     const int idx = index_by_address(p);
 259     assert(idx >= 0 && idx < _num, "Invalid index");
 260     if (_arr[idx] == NULL) {
 261       _arr[idx] = new ChunkTree();
 262     }
 263     return _arr[idx];
 264   }
 265 
 266   // Iterate over all nodes in all trees. Returns true for complete traversal,
 267   // false if traversal was cancelled.
 268   bool iterate_chunks(ChunkClosure* cc) const;
 269 
 270   DEBUG_ONLY(void verify(bool slow) const;)
 271 
 272   // Returns the footprint of all trees in this array, in words.
 273   size_t memory_footprint_words() const;
 274 
 275 };
 276 
 277 
 278 } // namespace metaspace
 279 
 280 #endif // SHARE_MEMORY_METASPACE_CHUNKTREE_HPP