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_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/chunkAllocSequence.hpp" 32 #include "memory/metaspace/chunkManager.hpp" 33 #include "memory/metaspace/metachunk.hpp" 34 #include "memory/metaspace/metaspaceCommon.hpp" 35 36 class outputStream; 37 class Mutex; 38 39 namespace metaspace { 40 41 42 // The SpaceManager: 43 // - keeps a list of chunks-in-use by the class loader, as well as a current chunk used 44 // to allocate from 45 // - keeps a dictionary of free MetaBlocks. Those can be remnants of a retired chunk or 46 // allocations which were not needed anymore for some reason (e.g. releasing half-allocated 47 // structures when class loading fails) 48 49 class SpaceManager : public CHeapObj<mtClass> { 50 51 // Lock handed down from the associated ClassLoaderData. 52 // Protects allocations from this space. 53 Mutex* const _lock; 54 55 // The chunk manager to allocate chunks from. 56 ChunkManager* const _chunk_manager; 57 58 // The chunk allocation strategy to use. 59 const ChunkAllocSequence* const _chunk_alloc_sequence; 60 61 // List of chunks in use by this SpaceManager. Allocations 62 // are done from the current chunk. The list is used for deallocating 63 // chunks when the SpaceManager is freed. 64 MetachunkList _chunks; 65 Metachunk* _current_chunk; 66 67 // Prematurely released metablocks. 68 BlockFreelist* _block_freelist; 69 70 71 // Statistics 72 73 // Running counters. 74 // Note: capacity = used + free + waste + overhead. We do not keep running counters for 75 // free and waste. Their sum can be deduced from the three other values. 76 // size_t _overhead_words; 77 // size_t _capacity_words; 78 // size_t _used_words; 79 uintx _num_chunks_by_level[chklvl::NUM_CHUNK_LEVELS]; 80 uintx _num_chunks_total; 81 82 83 Mutex* lock() const { return _lock; } 84 ChunkManager* chunk_manager() const { return _chunk_manager; } 85 const ChunkAllocSequence* chunk_alloc_sequence() const { return _chunk_alloc_sequence; } 86 87 BlockFreelist* block_freelist() const { return _block_freelist; } 88 void create_block_freelist(); 89 90 // The current chunk is unable to service a request. The remainder of the chunk is 91 // chopped into blocks and fed into the _block_freelists, in the hope of later reuse. 92 void retire_current_chunk(); 93 94 // Given a requested word size, will allocate a chunk large enough to at least fit that 95 // size, but may be larger according to the rules in the ChunkAllocSequence. 96 // Updates counters and adds the chunk to the head of the chunk list. 97 Metachunk* allocate_chunk_to_fit(size_t requested_word_size); 98 99 public: 100 101 SpaceManager(ChunkManager* chunk_manager, const ChunkAllocSequence* alloc_sequence, Mutex* lock); 102 103 ~SpaceManager(); 104 105 // Allocate memory from Metaspace. Will attempt to allocate from the _block_freelists, 106 // failing that, from the current chunk; failing that, attempt to get a new chunk from 107 // the associated ChunkManager. 108 MetaWord* allocate(size_t word_size); 109 110 // Prematurely returns a metaspace allocation to the _block_freelists because it is not 111 // needed anymore. 112 void deallocate(MetaWord* p, size_t word_size); 113 114 // Run verifications. slow=true: verify chunk-internal integrity too. 115 DEBUG_ONLY(void locked_verify(bool slow) const;) 116 117 }; 118 119 } // namespace metaspace 120 121 #endif // SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP