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_VIRTUALSPACENODE_HPP
  26 #define SHARE_MEMORY_METASPACE_VIRTUALSPACENODE_HPP
  27 
  28 #include "memory/metaspace/constants.hpp"
  29 #include "memory/metaspace/counter.hpp"
  30 #include "memory/metaspace/chunkTree.hpp"
  31 #include "memory/metaspace/commitMask.hpp"
  32 #include "memory/virtualspace.hpp"
  33 #include "memory/memRegion.hpp"
  34 #include "utilities/debug.hpp"
  35 #include "utilities/bitMap.hpp"
  36 #include "utilities/globalDefinitions.hpp"
  37 
  38 
  39 class outputStream;
  40 
  41 namespace metaspace {
  42 
  43 class CommitLimiter;
  44 
  45 // VirtualSpaceNode manage a single address range of the Metaspace.
  46 //
  47 // That address range may contain interleaved committed and uncommitted
  48 // regions. It keeps track of which regions have committed and offers
  49 // functions to commit and uncommit regions.
  50 //
  51 // It allocates and hands out memory ranges, starting at the bottom.
  52 //
  53 // Address range must be aligned to root chunk size.
  54 //
  55 class VirtualSpaceNode : public CHeapObj<mtClass> {
  56 
  57   // Link to next VirtualSpaceNode
  58   VirtualSpaceNode* _next;
  59 
  60   ReservedSpace _rs;
  61 
  62   // Start pointer of the area.
  63   MetaWord* const _base;
  64 
  65   // Size, in words, of the whole node
  66   const size_t _word_size;
  67 
  68   // Size, in words, of the range of this node which has been handed out in
  69   // the form of chunks.
  70   size_t _used_words;
  71 
  72   // The bitmap describing the commit state of the region:
  73   // Each bit covers a region of 64K (see constants::commit_granule_size).
  74   CommitMask _commit_mask;
  75 
  76   // An array of chunk trees. Each one describes fragmentation inside the associated root chunk.
  77   ChunkTreeArray _chunk_tree_array;
  78 
  79   // Limiter object to ask before expanding the committed size of this node.
  80   CommitLimiter* const _commit_limiter;
  81 
  82   // Points to outside size counters which we are to increase/decrease when we commit/uncommit
  83   // space from this node.
  84   SizeCounter* const _total_reserved_words_counter;
  85   SizeCounter* const _total_committed_words_counter;
  86 
  87   /// committing, uncommitting ///
  88 
  89   // Given a pointer into this node, calculate the start of the commit granule
  90   // the pointer points into.
  91   MetaWord* calc_start_of_granule(MetaWord* p) const {
  92     DEBUG_ONLY(check_pointer(p));
  93     return align_down(p, constants::commit_granule_bytes);
  94   }
  95 
  96   // Given an address range, ensure it is committed.
  97   //
  98   // The range has to be aligned to granule size.
  99   //
 100   // Function will:
 101   // - check how many granules in that region are uncommitted; If all are committed, it
 102   //    returns true immediately.
 103   // - check if committing those uncommitted granules would bring us over the commit limit
 104   //    (GC threshold, MaxMetaspaceSize). If true, it returns false.
 105   // - commit the memory.
 106   // - mark the range as committed in the commit mask
 107   //
 108   // Returns true if success, false if it did hit a commit limit.
 109   bool commit_range(MetaWord* p, size_t word_size);
 110 
 111   //// creation ////
 112 
 113   // Create a new empty node spanning the given reserved space.
 114   VirtualSpaceNode(ReservedSpace rs,
 115                    CommitLimiter* limiter,
 116                    SizeCounter* reserve_counter,
 117                    SizeCounter* commit_counter);
 118 
 119   MetaWord* base() const        { return _base; }
 120   MetaWord* word_size() const   { return _word_size; }
 121 
 122 public:
 123 
 124   // Create a node of a given size
 125   static VirtualSpaceNode* create_node(size_t word_size,
 126                                        CommitLimiter* limiter,
 127                                        SizeCounter* reserve_counter,
 128                                        SizeCounter* commit_counter);
 129 
 130   // Create a node over an existing space
 131   static VirtualSpaceNode* create_node(ReservedSpace rs,
 132                                        CommitLimiter* limiter,
 133                                        SizeCounter* reserve_counter,
 134                                        SizeCounter* commit_counter);
 135 
 136   ~VirtualSpaceNode();
 137 
 138   //// Chunk allocation, splitting, merging /////
 139 
 140   // Allocate a root chunk from this node. Will fail and return NULL
 141   // if the node is full.
 142   // Note: this just returns a chunk whose memory is reserved; no memory is committed yet.
 143   // Hence, before using this chunk, it must be committed.
 144   // Also, no limits are checked, since no committing takes place.
 145   Metachunk* allocate_root_chunk();
 146 
 147   // Given a chunk c, split it recursively until you get a chunk of the given target_level.
 148   //
 149   // The original chunk must not be part of a freelist.
 150   //
 151   // Returns pointer to the result chunk; returns split off chunks in splinters array.
 152   //
 153   // Returns NULL if chunk cannot be split at least once.
 154   Metachunk* split(chklvl_t target_level, Metachunk* c, Metachunk* splinters[chklvl::NUM_CHUNK_LEVELS]);
 155 
 156   // Given a chunk, attempt to merge it recursively with its neighboring chunks.
 157   //
 158   // If successful (merged at least once), returns address of
 159   // the merged chunk; NULL otherwise.
 160   //
 161   // The merged chunks are removed from their freelist; the number of merged chunks is
 162   // returned, split by level, in num_merged array. Note that these numbers does not
 163   // include the original chunk.
 164   //
 165   // !!! Please note that if this method returns a non-NULL value, the
 166   // original chunk will be invalid and should not be accessed anymore! !!!
 167   Metachunk* merge(Metachunk* c, int num_merged[chklvl::NUM_CHUNK_LEVELS]);
 168 
 169 
 170   /// misc /////
 171 
 172   // Returns size, in words, of the used space in this node alone.
 173   // (Notes:
 174   //  - This is the space handed out to the ChunkManager, so it is "used" from the viewpoint of this node,
 175   //    but not necessarily used for Metadata.
 176   //  - This may or may not be committed memory.
 177   size_t used_words() const             { return _used_words; }
 178 
 179   // Returns size, in words, of how much space is left in this node alone.
 180   size_t free_words() const             { return _word_size - _used_words; }
 181 
 182   // Returns size, in words, of committed space in this node alone.
 183   size_t committed_words() const;
 184 
 185   //// Committing/uncommitting memory /////
 186 
 187   // Given an address range, ensure it is committed.
 188   //
 189   // The range does not have to be aligned to granule size. However, the function will always commit
 190   // whole granules.
 191   //
 192   // Function will:
 193   // - check how many granules in that region are uncommitted; If all are committed, it
 194   //    returns true immediately.
 195   // - check if committing those uncommitted granules would bring us over the commit limit
 196   //    (GC threshold, MaxMetaspaceSize). If true, it returns false.
 197   // - commit the memory.
 198   // - mark the range as committed in the commit mask
 199   //
 200   // Returns true if success, false if it did hit a commit limit.
 201   bool ensure_range_is_committed(MetaWord* p, size_t word_size);
 202 
 203   // Given an address range (which has to be aligned to commit granule size):
 204   //  - uncommit it
 205   //  - mark it as uncommitted in the commit mask
 206   bool uncommit_range(MetaWord* p, size_t word_size);
 207 
 208   //// List stuff ////
 209   VirtualSpaceNode* next() const        { return _next; }
 210   void set_next(VirtualSpaceNode* vsn)  { _next = vsn; }
 211 
 212   /// Debug stuff ////
 213 
 214   // Verify counters and basic structure. Slow mode: verify all chunks in depth
 215   bool contains(const MetaWord* p) const {
 216     return p >= _base && p < _base + _used_words;
 217   }
 218 
 219 #ifdef ASSERT
 220   void check_pointer(const MetaWord* p) const {
 221     assert(contains(p), "invalid pointer");
 222   }
 223   void verify(bool slow) const;
 224 #endif
 225 
 226 };
 227 
 228 
 229 } // namespace metaspace
 230 
 231 #endif // SHARE_MEMORY_METASPACE_VIRTUALSPACENODE_HPP