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