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_SPACEMANAGER_HPP_ 26 #define SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP_ 27 28 #include "memory/allocation.hpp" 29 #include "memory/metaspace.hpp" 30 #include "memory/metaspace/blockFreelist.hpp" 31 #include "memory/metaspace/metaspaceCommon.hpp" 32 #include "memory/metaspace/metachunk.hpp" 33 #include "memory/metaspace/metaspaceStatistics.hpp" 34 #include "utilities/debug.hpp" 35 #include "utilities/globalDefinitions.hpp" 36 37 class outputStream; 38 class Mutex; 39 40 namespace metaspace { 41 namespace internals { 42 43 // SpaceManager - used by Metaspace to handle allocations 44 class SpaceManager : public CHeapObj<mtClass> { 45 friend class ::ClassLoaderMetaspace; 46 friend class Metadebug; 47 48 private: 49 50 // protects allocations 51 Mutex* const _lock; 52 53 // Type of metadata allocated. 54 const Metaspace::MetadataType _mdtype; 55 56 // Type of metaspace 57 const Metaspace::MetaspaceType _space_type; 58 59 // List of chunks in use by this SpaceManager. Allocations 60 // are done from the current chunk. The list is used for deallocating 61 // chunks when the SpaceManager is freed. 62 Metachunk* _chunk_list; 63 Metachunk* _current_chunk; 64 65 enum { 66 67 // Maximum number of small chunks to allocate to a SpaceManager 68 small_chunk_limit = 4, 69 70 // Maximum number of specialize chunks to allocate for anonymous and delegating 71 // metadata space to a SpaceManager 72 anon_and_delegating_metadata_specialize_chunk_limit = 4, 73 74 allocation_from_dictionary_limit = 4 * K 75 76 }; 77 78 // Some running counters, but lets keep their number small to not add to much to 79 // the per-classloader footprint. 80 // Note: capacity = used + free + waste + overhead. We do not keep running counters for 81 // free and waste. Their sum can be deduced from the three other values. 82 size_t _overhead_words; 83 size_t _capacity_words; 84 size_t _used_words; 85 uintx _num_chunks_by_type[NumberOfInUseLists]; 86 87 // Free lists of blocks are per SpaceManager since they 88 // are assumed to be in chunks in use by the SpaceManager 89 // and all chunks in use by a SpaceManager are freed when 90 // the class loader using the SpaceManager is collected. 91 BlockFreelist* _block_freelists; 92 93 private: 94 // Accessors 95 Metachunk* chunk_list() const { return _chunk_list; } 96 97 BlockFreelist* block_freelists() const { return _block_freelists; } 98 99 Metaspace::MetadataType mdtype() { return _mdtype; } 100 101 VirtualSpaceList* vs_list() const { return Metaspace::get_space_list(_mdtype); } 102 ChunkManager* chunk_manager() const { return Metaspace::get_chunk_manager(_mdtype); } 103 104 Metachunk* current_chunk() const { return _current_chunk; } 105 void set_current_chunk(Metachunk* v) { 106 _current_chunk = v; 107 } 108 109 Metachunk* find_current_chunk(size_t word_size); 110 111 // Add chunk to the list of chunks in use 112 void add_chunk(Metachunk* v, bool make_current); 113 void retire_current_chunk(); 114 115 Mutex* lock() const { return _lock; } 116 117 // Adds to the given statistic object. Expects to be locked with lock(). 118 void add_to_statistics_locked(SpaceManagerStatistics* out) const; 119 120 // Verify internal counters against the current state. Expects to be locked with lock(). 121 DEBUG_ONLY(void verify_metrics_locked() const;) 122 123 public: 124 SpaceManager(Metaspace::MetadataType mdtype, 125 Metaspace::MetaspaceType space_type, 126 Mutex* lock); 127 ~SpaceManager(); 128 129 enum ChunkMultiples { 130 MediumChunkMultiple = 4 131 }; 132 133 static size_t specialized_chunk_size(bool is_class) { return is_class ? ClassSpecializedChunk : SpecializedChunk; } 134 static size_t small_chunk_size(bool is_class) { return is_class ? ClassSmallChunk : SmallChunk; } 135 static size_t medium_chunk_size(bool is_class) { return is_class ? ClassMediumChunk : MediumChunk; } 136 137 static size_t smallest_chunk_size(bool is_class) { return specialized_chunk_size(is_class); } 138 139 // Accessors 140 bool is_class() const { return _mdtype == Metaspace::ClassType; } 141 142 size_t specialized_chunk_size() const { return specialized_chunk_size(is_class()); } 143 size_t small_chunk_size() const { return small_chunk_size(is_class()); } 144 size_t medium_chunk_size() const { return medium_chunk_size(is_class()); } 145 146 size_t smallest_chunk_size() const { return smallest_chunk_size(is_class()); } 147 148 size_t medium_chunk_bunch() const { return medium_chunk_size() * MediumChunkMultiple; } 149 150 bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); } 151 152 size_t capacity_words() const { return _capacity_words; } 153 size_t used_words() const { return _used_words; } 154 size_t overhead_words() const { return _overhead_words; } 155 156 // Adjust local, global counters after a new chunk has been added. 157 void account_for_new_chunk(const Metachunk* new_chunk); 158 159 // Adjust local, global counters after space has been allocated from the current chunk. 160 void account_for_allocation(size_t words); 161 162 // Adjust global counters just before the SpaceManager dies, after all its chunks 163 // have been returned to the freelist. 164 void account_for_spacemanager_death(); 165 166 // Adjust the initial chunk size to match one of the fixed chunk list sizes, 167 // or return the unadjusted size if the requested size is humongous. 168 static size_t adjust_initial_chunk_size(size_t requested, bool is_class_space); 169 size_t adjust_initial_chunk_size(size_t requested) const; 170 171 // Get the initial chunks size for this metaspace type. 172 size_t get_initial_chunk_size(Metaspace::MetaspaceType type) const; 173 174 // Todo: remove this once we have counters by chunk type. 175 uintx num_chunks_by_type(ChunkIndex chunk_type) const { return _num_chunks_by_type[chunk_type]; } 176 177 Metachunk* get_new_chunk(size_t chunk_word_size); 178 179 // Block allocation and deallocation. 180 // Allocates a block from the current chunk 181 MetaWord* allocate(size_t word_size); 182 183 // Helper for allocations 184 MetaWord* allocate_work(size_t word_size); 185 186 // Returns a block to the per manager freelist 187 void deallocate(MetaWord* p, size_t word_size); 188 189 // Based on the allocation size and a minimum chunk size, 190 // returned chunk size (for expanding space for chunk allocation). 191 size_t calc_chunk_size(size_t allocation_word_size); 192 193 // Called when an allocation from the current chunk fails. 194 // Gets a new chunk (may require getting a new virtual space), 195 // and allocates from that chunk. 196 MetaWord* grow_and_allocate(size_t word_size); 197 198 // Notify memory usage to MemoryService. 199 void track_metaspace_memory_usage(); 200 201 // debugging support. 202 203 void print_on(outputStream* st) const; 204 void locked_print_chunks_in_use_on(outputStream* st) const; 205 206 void verify(); 207 void verify_chunk_size(Metachunk* chunk); 208 209 // This adjusts the size given to be greater than the minimum allocation size in 210 // words for data in metaspace. Esentially the minimum size is currently 3 words. 211 size_t get_allocation_word_size(size_t word_size) { 212 size_t byte_size = word_size * BytesPerWord; 213 214 size_t raw_bytes_size = MAX2(byte_size, sizeof(Metablock)); 215 raw_bytes_size = align_up(raw_bytes_size, Metachunk::object_alignment()); 216 217 size_t raw_word_size = raw_bytes_size / BytesPerWord; 218 assert(raw_word_size * BytesPerWord == raw_bytes_size, "Size problem"); 219 220 return raw_word_size; 221 } 222 223 // Adds to the given statistic object. 224 void add_to_statistics(SpaceManagerStatistics* out) const; 225 226 // Verify internal counters against the current state. 227 DEBUG_ONLY(void verify_metrics() const;) 228 229 }; 230 231 232 } // namespace metaspace 233 } // namespace internals 234 235 #endif /* SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP_ */