1 /* 2 * Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2020 SAP SE. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #ifndef SHARE_MEMORY_METASPACE_METASPACECONTEXT_HPP 27 #define SHARE_MEMORY_METASPACE_METASPACECONTEXT_HPP 28 29 #include "memory/allocation.hpp" 30 #include "memory/metaspace/counters.hpp" 31 #include "memory/virtualspace.hpp" 32 #include "utilities/debug.hpp" 33 34 class outputStream; 35 36 namespace metaspace { 37 38 class ChunkManager; 39 class VirtualSpaceList; 40 class CommitLimiter; 41 42 // MetaspaceContext is a convenience bracket around: 43 // 44 // - a VirtualSpaceList managing a memory area used for Metaspace 45 // - a ChunkManager sitting atop of that which manages chunk freelists 46 // 47 // In a normal VM only one or two of these contexts ever exist: one for the metaspace, and 48 // optionally another one for the compressed class space. 49 // 50 // For tests more contexts may be created, and this would also be a way to use Metaspace 51 // for things other than class metadata. We would have to work on the naming then. 52 // 53 // - (Future TODO): Context should own a lock to guard it. Currently this stuff is guarded 54 // by one global lock, the slightly misnamed Metaspace_expandlock, but that one 55 // should be split into one per context. 56 // - (Future TODO): Context can/should have its own allocation alignment. That way we 57 // can have different alignment between class space and non-class metaspace. That could 58 // help optimize compressed class pointer encoding, see discussion for JDK-8244943). 59 60 class MetaspaceContext : public CHeapObj<mtMetaspace> { 61 62 const char* const _name; 63 VirtualSpaceList* const _vslist; 64 ChunkManager* const _cm; 65 SizeAtomicCounter _used_words_counter; 66 67 MetaspaceContext(const char* name, VirtualSpaceList* vslist, ChunkManager* cm) : 68 _name(name), 69 _vslist(vslist), 70 _cm(cm) 71 {} 72 73 static MetaspaceContext* _nonclass_space_context; 74 static MetaspaceContext* _class_space_context; 75 76 public: 77 78 // Destroys the context: deletes chunkmanager and virtualspacelist. 79 // If this is a non-expandable context over an existing space, that space remains 80 // untouched, otherwise all memory is unmapped. 81 ~MetaspaceContext(); 82 83 VirtualSpaceList* vslist() { return _vslist; } 84 ChunkManager* cm() { return _cm; } 85 SizeAtomicCounter* used_words_counter() { return &_used_words_counter; } 86 87 // Create a new, empty, expandable metaspace context. 88 static MetaspaceContext* create_expandable_context(const char* name, CommitLimiter* commit_limiter); 89 90 // Create a new, empty, non-expandable metaspace context atop of an externally provided space. 91 static MetaspaceContext* create_nonexpandable_context(const char* name, ReservedSpace rs, CommitLimiter* commit_limiter); 92 93 void print_on(outputStream* st) const; 94 95 DEBUG_ONLY(void verify() const;) 96 97 static void initialize_class_space_context(ReservedSpace rs); 98 static void initialize_nonclass_space_context(); 99 100 // Returns pointer to the global metaspace context. 101 // If compressed class space is active, this contains the non-class-space allocations. 102 // If compressed class space is inactive, this contains all metaspace allocations. 103 static MetaspaceContext* context_nonclass() { return _nonclass_space_context; } 104 105 // Returns pointer to the global class space context, if compressed class space is active, 106 // null otherwise. 107 static MetaspaceContext* context_class() { return _class_space_context; } 108 109 size_t used_words() const; 110 size_t committed_words() const; 111 size_t reserved_words() const; 112 }; 113 114 } // end namespace 115 116 #endif // SHARE_MEMORY_METASPACE_METASPACECONTEXT_HPP 117