< prev index next >

src/hotspot/share/memory/classLoaderMetaspace.cpp

Print this page

 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) {

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 }

 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) {

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 }
< prev index next >