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 #include "precompiled.hpp" 27 #include "logging/log.hpp" 28 #include "memory/classLoaderMetaspace.hpp" 29 #include "memory/metaspace.hpp" 30 #include "memory/metaspaceUtils.hpp" 31 #include "memory/metaspace/chunkManager.hpp" 32 #include "memory/metaspace/internalStats.hpp" 33 #include "memory/metaspace/metaspaceArena.hpp" 34 #include "memory/metaspace/metaspaceArenaGrowthPolicy.hpp" 35 #include "memory/metaspace/metaspaceSettings.hpp" 36 #include "memory/metaspace/metaspaceStatistics.hpp" 37 #include "memory/metaspace/runningCounters.hpp" 38 #include "memory/metaspaceTracer.hpp" 39 #include "runtime/mutexLocker.hpp" 40 #include "utilities/debug.hpp" 41 42 using metaspace::ChunkManager; 43 using metaspace::MetaspaceArena; 44 using metaspace::ArenaGrowthPolicy; 45 using metaspace::RunningCounters; 46 using metaspace::InternalStats; 47 48 #define LOGFMT "CLMS @" PTR_FORMAT " " 49 #define LOGFMT_ARGS p2i(this) 50 51 ClassLoaderMetaspace::ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType space_type) : 52 _lock(lock), 53 _space_type(space_type), 54 _non_class_space_arena(nullptr), 55 _class_space_arena(nullptr) 56 { 57 ChunkManager* const non_class_cm = 58 ChunkManager::chunkmanager_nonclass(); 59 60 // Initialize non-class Arena 61 _non_class_space_arena = new MetaspaceArena( 62 non_class_cm, 63 ArenaGrowthPolicy::policy_for_space_type(space_type, false), 64 RunningCounters::used_nonclass_counter(), 65 "non-class sm"); 66 67 // If needed, initialize class arena 68 if (Metaspace::using_class_space()) { 69 ChunkManager* const class_cm = 70 ChunkManager::chunkmanager_class(); 71 _class_space_arena = new MetaspaceArena( 72 class_cm, 73 ArenaGrowthPolicy::policy_for_space_type(space_type, true), 74 RunningCounters::used_class_counter(), 75 "class sm"); 76 } 77 78 UL2(debug, "born (nonclass arena: " PTR_FORMAT ", class arena: " PTR_FORMAT ".", 79 p2i(_non_class_space_arena), p2i(_class_space_arena)); 80 } 81 82 ClassLoaderMetaspace::~ClassLoaderMetaspace() { 83 UL(debug, "dies."); 84 MutexLocker fcl(lock(), Mutex::_no_safepoint_check_flag); 85 delete _non_class_space_arena; 86 delete _class_space_arena; 87 88 } 89 90 // Allocate word_size words from Metaspace. 91 MetaWord* ClassLoaderMetaspace::allocate(size_t word_size, Metaspace::MetadataType mdType) { 92 MutexLocker fcl(lock(), Mutex::_no_safepoint_check_flag); 93 if (Metaspace::is_class_space_allocation(mdType)) { 94 return class_space_arena()->allocate(word_size); 95 } else { 96 return non_class_space_arena()->allocate(word_size); 97 } 98 } 99 100 // Attempt to expand the GC threshold to be good for at least another word_size words 101 // and allocate. Returns null if failure. Used during Metaspace GC. 102 MetaWord* ClassLoaderMetaspace::expand_and_allocate(size_t word_size, Metaspace::MetadataType mdType) { 103 size_t delta_bytes = MetaspaceGC::delta_capacity_until_GC(word_size * BytesPerWord); 104 assert(delta_bytes > 0, "Must be"); 105 106 size_t before = 0; 107 size_t after = 0; 108 bool can_retry = true; 109 MetaWord* res; 110 bool incremented; 111 112 // Each thread increments the HWM at most once. Even if the thread fails to increment 113 // the HWM, an allocation is still attempted. This is because another thread must then 114 // have incremented the HWM and therefore the allocation might still succeed. 115 do { 116 incremented = MetaspaceGC::inc_capacity_until_GC(delta_bytes, &after, &before, &can_retry); 117 res = allocate(word_size, mdType); 118 } while (!incremented && res == nullptr && can_retry); 119 120 if (incremented) { 121 Metaspace::tracer()->report_gc_threshold(before, after, 122 MetaspaceGCThresholdUpdater::ExpandAndAllocate); 123 // Keeping both for now until I am sure the old variant (gc + metaspace) is not needed anymore 124 log_trace(gc, metaspace)("Increase capacity to GC from " SIZE_FORMAT " to " SIZE_FORMAT, before, after); 125 UL2(info, "GC threshold increased: " SIZE_FORMAT "->" SIZE_FORMAT ".", before, after); 126 } 127 128 return res; 129 } 130 131 // Prematurely returns a metaspace allocation to the _block_freelists 132 // because it is not needed anymore. 133 void ClassLoaderMetaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) { 134 MutexLocker fcl(lock(), Mutex::_no_safepoint_check_flag); 135 if (Metaspace::using_class_space() && is_class) { 136 class_space_arena()->deallocate(ptr, word_size); 137 } else { 138 non_class_space_arena()->deallocate(ptr, word_size); 139 } 140 DEBUG_ONLY(InternalStats::inc_num_deallocs();) 141 } 142 143 // Update statistics. This walks all in-use chunks. 144 void ClassLoaderMetaspace::add_to_statistics(metaspace::ClmsStats* out) const { 145 MutexLocker fcl(lock(), Mutex::_no_safepoint_check_flag); 146 if (non_class_space_arena() != nullptr) { 147 non_class_space_arena()->add_to_statistics(&out->_arena_stats_nonclass); 148 } 149 if (class_space_arena() != nullptr) { 150 class_space_arena()->add_to_statistics(&out->_arena_stats_class); 151 } 152 } 153 154 #ifdef ASSERT 155 void ClassLoaderMetaspace::verify() const { 156 MutexLocker fcl(lock(), Mutex::_no_safepoint_check_flag); 157 if (non_class_space_arena() != nullptr) { 158 non_class_space_arena()->verify(); 159 } 160 if (class_space_arena() != nullptr) { 161 class_space_arena()->verify(); 162 } 163 } 164 #endif // ASSERT 165 166 // Convenience method to get the most important usage statistics. 167 void ClassLoaderMetaspace::usage_numbers(Metaspace::MetadataType mdType, size_t* p_used_words, 168 size_t* p_committed_words, size_t* p_capacity_words) const { 169 const MetaspaceArena* arena = (mdType == Metaspace::MetadataType::ClassType) ? 170 class_space_arena() : non_class_space_arena(); 171 arena->usage_numbers(p_used_words, p_committed_words, p_capacity_words); 172 } 173 174 // Convenience method to get total usage numbers 175 void ClassLoaderMetaspace::usage_numbers(size_t* p_used_words, size_t* p_committed_words, 176 size_t* p_capacity_words) const { 177 size_t used_nc, comm_nc, cap_nc; 178 size_t used_c = 0, comm_c = 0, cap_c = 0; 179 { 180 MutexLocker fcl(lock(), Mutex::_no_safepoint_check_flag); 181 usage_numbers(Metaspace::MetadataType::NonClassType, &used_nc, &comm_nc, &cap_nc); 182 if (Metaspace::using_class_space()) { 183 usage_numbers(Metaspace::MetadataType::ClassType, &used_c, &comm_c, &cap_c); 184 } 185 } 186 if (p_used_words != nullptr) { 187 (*p_used_words) = used_nc + used_c; 188 } 189 if (p_committed_words != nullptr) { 190 (*p_committed_words) = comm_nc + comm_c; 191 } 192 if (p_capacity_words != nullptr) { 193 (*p_capacity_words) = cap_nc + cap_c; 194 } 195 }