1 /* 2 * Copyright (c) 2020, 2022, 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_TESTHELPERS_HPP 27 #define SHARE_MEMORY_METASPACE_TESTHELPERS_HPP 28 29 #include "memory/allocation.hpp" 30 #include "memory/metaspace.hpp" 31 #include "memory/metaspace/commitLimiter.hpp" 32 #include "memory/metaspace/counters.hpp" 33 #include "memory/metaspace/metaspaceContext.hpp" 34 #include "memory/virtualspace.hpp" 35 #include "utilities/globalDefinitions.hpp" 36 37 // This is just convenience classes for metaspace-related tests 38 // (jtreg, via whitebox API, and gtests) 39 40 class ReservedSpace; 41 class Mutex; 42 class outputStream; 43 44 namespace metaspace { 45 46 struct ArenaStats; 47 class MetaspaceContext; 48 class MetaspaceArena; 49 50 // Wraps a MetaspaceTestArena with its own lock for testing purposes. 51 class MetaspaceTestArena : public CHeapObj<mtInternal> { 52 53 Mutex* const _lock; 54 MetaspaceArena* const _arena; 55 56 public: 57 58 const MetaspaceArena* arena() const { 59 return _arena; 60 } 61 62 MetaspaceTestArena(Mutex* lock, MetaspaceArena* arena); 63 ~MetaspaceTestArena(); 64 65 MetaWord* allocate(size_t word_size); 66 MetaWord* allocate_for_klass(size_t word_size); 67 MetaWord* allocate_from_freeblocks_only(size_t word_size); 68 void deallocate(MetaWord* p, size_t word_size); 69 void add_to_statistics(ArenaStats* out) const; 70 }; 71 72 // Wraps an instance of a MetaspaceContext together with some side objects for easy use in test beds (whitebox, gtests) 73 class MetaspaceTestContext : public CHeapObj<mtInternal> { 74 75 const char* const _name; 76 const size_t _reserve_limit; 77 const size_t _commit_limit; 78 79 MetaspaceContext* _context; 80 CommitLimiter _commit_limiter; 81 SizeAtomicCounter _used_words_counter; 82 83 // For non-expandable contexts we keep track of the space 84 // and delete it at destruction time. 85 ReservedSpace _rs; 86 87 public: 88 89 // Note: limit == 0 means unlimited 90 // Reserve limit > 0 simulates a non-expandable VirtualSpaceList (like CompressedClassSpace) 91 // Commit limit > 0 simulates a limit to max committable space (like MaxMetaspaceSize) 92 MetaspaceTestContext(const char* name, size_t commit_limit = 0, size_t reserve_limit = 0); 93 ~MetaspaceTestContext(); 94 95 // Create an arena, feeding off this area. 96 MetaspaceTestArena* create_arena(Metaspace::MetaspaceType type); 97 98 void purge_area(); 99 100 // Accessors 101 const CommitLimiter& commit_limiter() const { return _commit_limiter; } 102 const VirtualSpaceList& vslist() const { return *(_context->vslist()); } 103 ChunkManager& cm() { return *(_context->cm()); } 104 105 // Returns reserve- and commit limit we run the test with (in the real world, 106 // these would be equivalent to CompressedClassSpaceSize resp MaxMetaspaceSize) 107 size_t reserve_limit() const { return _reserve_limit == 0 ? max_uintx : 0; } 108 size_t commit_limit() const { return _commit_limit == 0 ? max_uintx : 0; } 109 110 // Convenience function to retrieve total committed/used words 111 size_t used_words() const { return _used_words_counter.get(); } 112 size_t committed_words() const { return _commit_limiter.committed_words(); } 113 114 DEBUG_ONLY(void verify() const;) 115 116 void print_on(outputStream* st) const; 117 118 }; 119 120 } // namespace metaspace 121 122 #endif // SHARE_MEMORY_METASPACE_TESTHELPERS_HPP