1 /* 2 * Copyright (c) 2018, 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_CHUNKMANAGER_HPP 26 #define SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP 27 28 #include "memory/allocation.hpp" 29 #include "memory/binaryTreeDictionary.hpp" 30 #include "memory/freeList.inline.hpp" 31 #include "memory/metaspace/metachunk.hpp" 32 #include "memory/metaspace/metaspaceStatistics.hpp" 33 #include "memory/metaspaceChunkFreeListSummary.hpp" 34 #include "utilities/globalDefinitions.hpp" 35 36 class TestVirtualSpaceNodeTest; 37 38 namespace metaspace { 39 namespace internals { 40 41 typedef class FreeList<Metachunk> ChunkList; 42 typedef BinaryTreeDictionary<Metachunk, FreeList<Metachunk> > ChunkTreeDictionary; 43 44 // Manages the global free lists of chunks. 45 class ChunkManager : public CHeapObj<mtInternal> { 46 friend class ::TestVirtualSpaceNodeTest; 47 48 // Free list of chunks of different sizes. 49 // SpecializedChunk 50 // SmallChunk 51 // MediumChunk 52 ChunkList _free_chunks[NumberOfFreeLists]; 53 54 // Whether or not this is the class chunkmanager. 55 const bool _is_class; 56 57 // Return non-humongous chunk list by its index. 58 ChunkList* free_chunks(ChunkIndex index); 59 60 // Returns non-humongous chunk list for the given chunk word size. 61 ChunkList* find_free_chunks_list(size_t word_size); 62 63 // HumongousChunk 64 ChunkTreeDictionary _humongous_dictionary; 65 66 // Returns the humongous chunk dictionary. 67 ChunkTreeDictionary* humongous_dictionary() { 68 return &_humongous_dictionary; 69 } 70 71 // Size, in metaspace words, of all chunks managed by this ChunkManager 72 size_t _free_chunks_total; 73 // Number of chunks in this ChunkManager 74 size_t _free_chunks_count; 75 76 // Update counters after a chunk was added or removed removed. 77 void account_for_added_chunk(const Metachunk* c); 78 void account_for_removed_chunk(const Metachunk* c); 79 80 size_t sum_free_chunks(); 81 size_t sum_free_chunks_count(); 82 83 void locked_verify_free_chunks_total(); 84 void slow_locked_verify_free_chunks_total() { 85 if (VerifyMetaspace) { 86 locked_verify_free_chunks_total(); 87 } 88 } 89 void locked_verify_free_chunks_count(); 90 void slow_locked_verify_free_chunks_count() { 91 if (VerifyMetaspace) { 92 locked_verify_free_chunks_count(); 93 } 94 } 95 96 // Given a pointer to a chunk, attempts to merge it with neighboring 97 // free chunks to form a bigger chunk. Returns true if successful. 98 bool attempt_to_coalesce_around_chunk(Metachunk* chunk, ChunkIndex target_chunk_type); 99 100 // Helper for chunk merging: 101 // Given an address range with 1-n chunks which are all supposed to be 102 // free and hence currently managed by this ChunkManager, remove them 103 // from this ChunkManager and mark them as invalid. 104 // - This does not correct the occupancy map. 105 // - This does not adjust the counters in ChunkManager. 106 // - Does not adjust container count counter in containing VirtualSpaceNode. 107 // Returns number of chunks removed. 108 int remove_chunks_in_area(MetaWord* p, size_t word_size); 109 110 // Helper for chunk splitting: given a target chunk size and a larger free chunk, 111 // split up the larger chunk into n smaller chunks, at least one of which should be 112 // the target chunk of target chunk size. The smaller chunks, including the target 113 // chunk, are returned to the freelist. The pointer to the target chunk is returned. 114 // Note that this chunk is supposed to be removed from the freelist right away. 115 Metachunk* split_chunk(size_t target_chunk_word_size, Metachunk* chunk); 116 117 public: 118 119 ChunkManager(bool is_class) 120 : _is_class(is_class), _free_chunks_total(0), _free_chunks_count(0) { 121 _free_chunks[SpecializedIndex].set_size(get_size_for_nonhumongous_chunktype(SpecializedIndex, is_class)); 122 _free_chunks[SmallIndex].set_size(get_size_for_nonhumongous_chunktype(SmallIndex, is_class)); 123 _free_chunks[MediumIndex].set_size(get_size_for_nonhumongous_chunktype(MediumIndex, is_class)); 124 } 125 126 // Add or delete (return) a chunk to the global freelist. 127 Metachunk* chunk_freelist_allocate(size_t word_size); 128 129 // Map a size to a list index assuming that there are lists 130 // for special, small, medium, and humongous chunks. 131 ChunkIndex list_index(size_t size); 132 133 // Map a given index to the chunk size. 134 size_t size_by_index(ChunkIndex index) const; 135 136 bool is_class() const { return _is_class; } 137 138 // Convenience accessors. 139 size_t medium_chunk_word_size() const { return size_by_index(MediumIndex); } 140 size_t small_chunk_word_size() const { return size_by_index(SmallIndex); } 141 size_t specialized_chunk_word_size() const { return size_by_index(SpecializedIndex); } 142 143 // Take a chunk from the ChunkManager. The chunk is expected to be in 144 // the chunk manager (the freelist if non-humongous, the dictionary if 145 // humongous). 146 void remove_chunk(Metachunk* chunk); 147 148 // Return a single chunk of type index to the ChunkManager. 149 void return_single_chunk(Metachunk* chunk); 150 151 // Add the simple linked list of chunks to the freelist of chunks 152 // of type index. 153 void return_chunk_list(Metachunk* chunk); 154 155 // Total of the space in the free chunks list 156 size_t free_chunks_total_words(); 157 size_t free_chunks_total_bytes(); 158 159 // Number of chunks in the free chunks list 160 size_t free_chunks_count(); 161 162 // Remove from a list by size. Selects list based on size of chunk. 163 Metachunk* free_chunks_get(size_t chunk_word_size); 164 165 #define index_bounds_check(index) \ 166 assert(is_valid_chunktype(index), "Bad index: %d", (int) index) 167 168 size_t num_free_chunks(ChunkIndex index) const { 169 index_bounds_check(index); 170 171 if (index == HumongousIndex) { 172 return _humongous_dictionary.total_free_blocks(); 173 } 174 175 ssize_t count = _free_chunks[index].count(); 176 return count == -1 ? 0 : (size_t) count; 177 } 178 179 size_t size_free_chunks_in_bytes(ChunkIndex index) const { 180 index_bounds_check(index); 181 182 size_t word_size = 0; 183 if (index == HumongousIndex) { 184 word_size = _humongous_dictionary.total_size(); 185 } else { 186 const size_t size_per_chunk_in_words = _free_chunks[index].size(); 187 word_size = size_per_chunk_in_words * num_free_chunks(index); 188 } 189 190 return word_size * BytesPerWord; 191 } 192 193 MetaspaceChunkFreeListSummary chunk_free_list_summary() const { 194 return MetaspaceChunkFreeListSummary(num_free_chunks(SpecializedIndex), 195 num_free_chunks(SmallIndex), 196 num_free_chunks(MediumIndex), 197 num_free_chunks(HumongousIndex), 198 size_free_chunks_in_bytes(SpecializedIndex), 199 size_free_chunks_in_bytes(SmallIndex), 200 size_free_chunks_in_bytes(MediumIndex), 201 size_free_chunks_in_bytes(HumongousIndex)); 202 } 203 204 // Debug support 205 void verify(); 206 void slow_verify() { 207 if (VerifyMetaspace) { 208 verify(); 209 } 210 } 211 void locked_verify(); 212 void slow_locked_verify() { 213 if (VerifyMetaspace) { 214 locked_verify(); 215 } 216 } 217 218 void locked_print_free_chunks(outputStream* st); 219 void locked_print_sum_free_chunks(outputStream* st); 220 221 // Fill in current statistic values to the given statistics object. 222 void collect_statistics(ChunkManagerStatistics* out) const; 223 224 }; 225 226 } // namespace metaspace 227 } // namespace internals 228 229 230 #endif /* SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP_ */ 231