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