annotate src/hotspot/share/memory/metaspace.cpp @ 53554:74109912c738

8213751: ClassLoaderDataGraph::cld_do() should sometimes require CLDG_lock Summary: Add version of loaded_cld_do for runtime calls. Reviewed-by: eosterlund, rehn
author coleenp
date Fri, 16 Nov 2018 07:30:40 -0500
parents e0fd97beab7e
children 6ed72482de52
rev   line source
coleenp@13728 1 /*
iklam@49178 2 * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
coleenp@13728 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
coleenp@13728 4 *
coleenp@13728 5 * This code is free software; you can redistribute it and/or modify it
coleenp@13728 6 * under the terms of the GNU General Public License version 2 only, as
coleenp@13728 7 * published by the Free Software Foundation.
coleenp@13728 8 *
coleenp@13728 9 * This code is distributed in the hope that it will be useful, but WITHOUT
coleenp@13728 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
coleenp@13728 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
coleenp@13728 12 * version 2 for more details (a copy is included in the LICENSE file that
coleenp@13728 13 * accompanied this code).
coleenp@13728 14 *
coleenp@13728 15 * You should have received a copy of the GNU General Public License version
coleenp@13728 16 * 2 along with this work; if not, write to the Free Software Foundation,
coleenp@13728 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
coleenp@13728 18 *
coleenp@13728 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
coleenp@13728 20 * or visit www.oracle.com if you need additional information or have any
coleenp@13728 21 * questions.
coleenp@13728 22 *
coleenp@13728 23 */
stuefe@50777 24
coleenp@13728 25 #include "precompiled.hpp"
kvn@42650 26 #include "aot/aotLoader.hpp"
coleenp@52788 27 #include "classfile/classLoaderDataGraph.hpp"
coleenp@50984 28 #include "gc/shared/collectedHeap.hpp"
brutisso@35061 29 #include "logging/log.hpp"
stuefe@46701 30 #include "logging/logStream.hpp"
coleenp@13728 31 #include "memory/filemap.hpp"
coleenp@13728 32 #include "memory/metaspace.hpp"
stuefe@50777 33 #include "memory/metaspace/chunkManager.hpp"
stuefe@50777 34 #include "memory/metaspace/metachunk.hpp"
stuefe@50516 35 #include "memory/metaspace/metaspaceCommon.hpp"
stuefe@50777 36 #include "memory/metaspace/printCLDMetaspaceInfoClosure.hpp"
stuefe@50777 37 #include "memory/metaspace/spaceManager.hpp"
stuefe@50777 38 #include "memory/metaspace/virtualSpaceList.hpp"
coleenp@13728 39 #include "memory/metaspaceShared.hpp"
ehelin@23465 40 #include "memory/metaspaceTracer.hpp"
coleenp@50984 41 #include "memory/universe.hpp"
stefank@20406 42 #include "runtime/init.hpp"
coleenp@51033 43 #include "runtime/orderAccess.hpp"
coleenp@13728 44 #include "services/memTracker.hpp"
coleenp@13728 45 #include "utilities/copy.hpp"
coleenp@13728 46 #include "utilities/debug.hpp"
coleenp@50984 47 #include "utilities/formatBuffer.hpp"
stuefe@50516 48 #include "utilities/globalDefinitions.hpp"
coleenp@13728 49
stuefe@50516 50
stuefe@50777 51 using namespace metaspace;
coleenp@13728 52
coleenp@13728 53 MetaWord* last_allocated = 0;
coleenp@13728 54
coleenp@21188 55 size_t Metaspace::_compressed_class_space_size;
ehelin@23465 56 const MetaspaceTracer* Metaspace::_tracer = NULL;
hseigel@19319 57
iklam@46746 58 DEBUG_ONLY(bool Metaspace::_frozen = false;)
iklam@46746 59
stuefe@50516 60 static const char* space_type_name(Metaspace::MetaspaceType t) {
stuefe@50516 61 const char* s = NULL;
stuefe@50516 62 switch (t) {
stuefe@50516 63 case Metaspace::StandardMetaspaceType: s = "Standard"; break;
stuefe@50516 64 case Metaspace::BootMetaspaceType: s = "Boot"; break;
lfoltan@52170 65 case Metaspace::UnsafeAnonymousMetaspaceType: s = "UnsafeAnonymous"; break;
stuefe@50516 66 case Metaspace::ReflectionMetaspaceType: s = "Reflection"; break;
stuefe@50516 67 default: ShouldNotReachHere();
stuefe@50516 68 }
stuefe@50516 69 return s;
zgu@47983 70 }
zgu@47983 71
aharlap@50668 72 volatile size_t MetaspaceGC::_capacity_until_GC = 0;
coleenp@13728 73 uint MetaspaceGC::_shrink_factor = 0;
coleenp@13728 74 bool MetaspaceGC::_should_concurrent_collect = false;
coleenp@13728 75
coleenp@13728 76 // BlockFreelist methods
coleenp@13728 77
coleenp@13728 78 // VirtualSpaceNode methods
coleenp@13728 79
coleenp@13728 80 // MetaspaceGC methods
coleenp@13728 81
coleenp@13728 82 // VM_CollectForMetadataAllocation is the vm operation used to GC.
coleenp@13728 83 // Within the VM operation after the GC the attempt to allocate the metadata
coleenp@13728 84 // should succeed. If the GC did not free enough space for the metaspace
coleenp@13728 85 // allocation, the HWM is increased so that another virtualspace will be
coleenp@13728 86 // allocated for the metadata. With perm gen the increase in the perm
coleenp@13728 87 // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion. The
coleenp@13728 88 // metaspace policy uses those as the small and large steps for the HWM.
coleenp@13728 89 //
coleenp@13728 90 // After the GC the compute_new_size() for MetaspaceGC is called to
coleenp@13728 91 // resize the capacity of the metaspaces. The current implementation
jmasa@17109 92 // is based on the flags MinMetaspaceFreeRatio and MaxMetaspaceFreeRatio used
coleenp@13728 93 // to resize the Java heap by some GC's. New flags can be implemented
jmasa@17109 94 // if really needed. MinMetaspaceFreeRatio is used to calculate how much
coleenp@13728 95 // free space is desirable in the metaspace capacity to decide how much
jmasa@15610 96 // to increase the HWM. MaxMetaspaceFreeRatio is used to decide how much
coleenp@13728 97 // free space is desirable in the metaspace capacity before decreasing
coleenp@13728 98 // the HWM.
coleenp@13728 99
coleenp@13728 100 // Calculate the amount to increase the high water mark (HWM).
coleenp@13728 101 // Increase by a minimum amount (MinMetaspaceExpansion) so that
coleenp@13728 102 // another expansion is not requested too soon. If that is not
stefank@20406 103 // enough to satisfy the allocation, increase by MaxMetaspaceExpansion.
stefank@20406 104 // If that is still not enough, expand by the size of the allocation
stefank@20406 105 // plus some.
stefank@20406 106 size_t MetaspaceGC::delta_capacity_until_GC(size_t bytes) {
stefank@20406 107 size_t min_delta = MinMetaspaceExpansion;
stefank@20406 108 size_t max_delta = MaxMetaspaceExpansion;
stefank@46619 109 size_t delta = align_up(bytes, Metaspace::commit_alignment());
stefank@20406 110
stefank@20406 111 if (delta <= min_delta) {
stefank@20406 112 delta = min_delta;
stefank@20406 113 } else if (delta <= max_delta) {
coleenp@13728 114 // Don't want to hit the high water mark on the next
coleenp@13728 115 // allocation so make the delta greater than just enough
coleenp@13728 116 // for this allocation.
stefank@20406 117 delta = max_delta;
stefank@20406 118 } else {
stefank@20406 119 // This allocation is large but the next ones are probably not
stefank@20406 120 // so increase by the minimum.
stefank@20406 121 delta = delta + min_delta;
coleenp@13728 122 }
stefank@20406 123
stefank@46619 124 assert_is_aligned(delta, Metaspace::commit_alignment());
stefank@20406 125
stefank@20406 126 return delta;
coleenp@13728 127 }
coleenp@13728 128
stefank@20406 129 size_t MetaspaceGC::capacity_until_GC() {
coleenp@47789 130 size_t value = OrderAccess::load_acquire(&_capacity_until_GC);
aharlap@27693 131 assert(value >= MetaspaceSize, "Not initialized properly?");
stefank@20406 132 return value;
stefank@20406 133 }
stefank@20406 134
ehelin@26938 135 bool MetaspaceGC::inc_capacity_until_GC(size_t v, size_t* new_cap_until_GC, size_t* old_cap_until_GC) {
stefank@46619 136 assert_is_aligned(v, Metaspace::commit_alignment());
stefank@20406 137
tschatzl@50867 138 size_t old_capacity_until_GC = _capacity_until_GC;
tschatzl@50867 139 size_t new_value = old_capacity_until_GC + v;
ehelin@26938 140
tschatzl@50867 141 if (new_value < old_capacity_until_GC) {
ehelin@26938 142 // The addition wrapped around, set new_value to aligned max value.
stefank@46619 143 new_value = align_down(max_uintx, Metaspace::commit_alignment());
ehelin@26938 144 }
ehelin@26938 145
tschatzl@50867 146 size_t prev_value = Atomic::cmpxchg(new_value, &_capacity_until_GC, old_capacity_until_GC);
ehelin@26938 147
tschatzl@50867 148 if (old_capacity_until_GC != prev_value) {
ehelin@26938 149 return false;
ehelin@26938 150 }
ehelin@26938 151
ehelin@26938 152 if (new_cap_until_GC != NULL) {
ehelin@26938 153 *new_cap_until_GC = new_value;
ehelin@26938 154 }
ehelin@26938 155 if (old_cap_until_GC != NULL) {
tschatzl@50867 156 *old_cap_until_GC = old_capacity_until_GC;
ehelin@26938 157 }
ehelin@26938 158 return true;
stefank@20406 159 }
stefank@20406 160
stefank@20406 161 size_t MetaspaceGC::dec_capacity_until_GC(size_t v) {
stefank@46619 162 assert_is_aligned(v, Metaspace::commit_alignment());
stefank@20406 163
aharlap@50668 164 return Atomic::sub(v, &_capacity_until_GC);
stefank@20406 165 }
stefank@20406 166
ehelin@24847 167 void MetaspaceGC::initialize() {
ehelin@24847 168 // Set the high-water mark to MaxMetapaceSize during VM initializaton since
ehelin@24847 169 // we can't do a GC during initialization.
ehelin@24847 170 _capacity_until_GC = MaxMetaspaceSize;
ehelin@24847 171 }
ehelin@24847 172
ehelin@24847 173 void MetaspaceGC::post_initialize() {
ehelin@24847 174 // Reset the high-water mark once the VM initialization is done.
stuefe@49854 175 _capacity_until_GC = MAX2(MetaspaceUtils::committed_bytes(), MetaspaceSize);
ehelin@24847 176 }
ehelin@24847 177
stefank@20406 178 bool MetaspaceGC::can_expand(size_t word_size, bool is_class) {
stefank@20406 179 // Check if the compressed class space is full.
stefank@20406 180 if (is_class && Metaspace::using_class_space()) {
stuefe@49854 181 size_t class_committed = MetaspaceUtils::committed_bytes(Metaspace::ClassType);
stefank@20406 182 if (class_committed + word_size * BytesPerWord > CompressedClassSpaceSize) {
stuefe@49830 183 log_trace(gc, metaspace, freelist)("Cannot expand %s metaspace by " SIZE_FORMAT " words (CompressedClassSpaceSize = " SIZE_FORMAT " words)",
stuefe@49830 184 (is_class ? "class" : "non-class"), word_size, CompressedClassSpaceSize / sizeof(MetaWord));
coleenp@18491 185 return false;
coleenp@18491 186 }
mgerdin@16391 187 }
coleenp@13728 188
stefank@20406 189 // Check if the user has imposed a limit on the metaspace memory.
stuefe@49854 190 size_t committed_bytes = MetaspaceUtils::committed_bytes();
stefank@20406 191 if (committed_bytes + word_size * BytesPerWord > MaxMetaspaceSize) {
stuefe@49830 192 log_trace(gc, metaspace, freelist)("Cannot expand %s metaspace by " SIZE_FORMAT " words (MaxMetaspaceSize = " SIZE_FORMAT " words)",
stuefe@49830 193 (is_class ? "class" : "non-class"), word_size, MaxMetaspaceSize / sizeof(MetaWord));
stefank@20406 194 return false;
coleenp@13728 195 }
coleenp@13728 196
stefank@20406 197 return true;
stefank@20406 198 }
stefank@20406 199
stefank@20406 200 size_t MetaspaceGC::allowed_expansion() {
stuefe@49854 201 size_t committed_bytes = MetaspaceUtils::committed_bytes();
ehelin@24847 202 size_t capacity_until_gc = capacity_until_GC();
ehelin@24847 203
ehelin@24847 204 assert(capacity_until_gc >= committed_bytes,
david@33105 205 "capacity_until_gc: " SIZE_FORMAT " < committed_bytes: " SIZE_FORMAT,
david@33105 206 capacity_until_gc, committed_bytes);
stefank@20406 207
stefank@20406 208 size_t left_until_max = MaxMetaspaceSize - committed_bytes;
stefank@20406 209 size_t left_until_GC = capacity_until_gc - committed_bytes;
stefank@20406 210 size_t left_to_commit = MIN2(left_until_GC, left_until_max);
stuefe@49830 211 log_trace(gc, metaspace, freelist)("allowed expansion words: " SIZE_FORMAT
stuefe@49830 212 " (left_until_max: " SIZE_FORMAT ", left_until_GC: " SIZE_FORMAT ".",
stuefe@49830 213 left_to_commit / BytesPerWord, left_until_max / BytesPerWord, left_until_GC / BytesPerWord);
stefank@20406 214
stefank@20406 215 return left_to_commit / BytesPerWord;
coleenp@13728 216 }
coleenp@13728 217
coleenp@13728 218 void MetaspaceGC::compute_new_size() {
coleenp@13728 219 assert(_shrink_factor <= 100, "invalid shrink factor");
coleenp@13728 220 uint current_shrink_factor = _shrink_factor;
coleenp@13728 221 _shrink_factor = 0;
coleenp@13728 222
ehelin@24847 223 // Using committed_bytes() for used_after_gc is an overestimation, since the
ehelin@24847 224 // chunk free lists are included in committed_bytes() and the memory in an
ehelin@24847 225 // un-fragmented chunk free list is available for future allocations.
ehelin@24847 226 // However, if the chunk free lists becomes fragmented, then the memory may
ehelin@24847 227 // not be available for future allocations and the memory is therefore "in use".
ehelin@24847 228 // Including the chunk free lists in the definition of "in use" is therefore
ehelin@24847 229 // necessary. Not including the chunk free lists can cause capacity_until_GC to
ehelin@24847 230 // shrink below committed_bytes() and this has caused serious bugs in the past.
stuefe@49854 231 const size_t used_after_gc = MetaspaceUtils::committed_bytes();
jmasa@17109 232 const size_t capacity_until_GC = MetaspaceGC::capacity_until_GC();
coleenp@13728 233
jmasa@15610 234 const double minimum_free_percentage = MinMetaspaceFreeRatio / 100.0;
coleenp@13728 235 const double maximum_used_percentage = 1.0 - minimum_free_percentage;
coleenp@13728 236
coleenp@13728 237 const double min_tmp = used_after_gc / maximum_used_percentage;
coleenp@13728 238 size_t minimum_desired_capacity =
coleenp@13728 239 (size_t)MIN2(min_tmp, double(max_uintx));
coleenp@13728 240 // Don't shrink less than the initial generation size
coleenp@13728 241 minimum_desired_capacity = MAX2(minimum_desired_capacity,
coleenp@13728 242 MetaspaceSize);
coleenp@13728 243
brutisso@35061 244 log_trace(gc, metaspace)("MetaspaceGC::compute_new_size: ");
brutisso@35061 245 log_trace(gc, metaspace)(" minimum_free_percentage: %6.2f maximum_used_percentage: %6.2f",
brutisso@35061 246 minimum_free_percentage, maximum_used_percentage);
brutisso@35061 247 log_trace(gc, metaspace)(" used_after_gc : %6.1fKB", used_after_gc / (double) K);
coleenp@13728 248
coleenp@13728 249
jmasa@17109 250 size_t shrink_bytes = 0;
coleenp@13728 251 if (capacity_until_GC < minimum_desired_capacity) {
coleenp@13728 252 // If we have less capacity below the metaspace HWM, then
coleenp@13728 253 // increment the HWM.
coleenp@13728 254 size_t expand_bytes = minimum_desired_capacity - capacity_until_GC;
stefank@46619 255 expand_bytes = align_up(expand_bytes, Metaspace::commit_alignment());
coleenp@13728 256 // Don't expand unless it's significant
coleenp@13728 257 if (expand_bytes >= MinMetaspaceExpansion) {
ehelin@26938 258 size_t new_capacity_until_GC = 0;
ehelin@26938 259 bool succeeded = MetaspaceGC::inc_capacity_until_GC(expand_bytes, &new_capacity_until_GC);
ehelin@26938 260 assert(succeeded, "Should always succesfully increment HWM when at safepoint");
ehelin@26938 261
ehelin@23465 262 Metaspace::tracer()->report_gc_threshold(capacity_until_GC,
ehelin@23465 263 new_capacity_until_GC,
ehelin@23465 264 MetaspaceGCThresholdUpdater::ComputeNewSize);
brutisso@35061 265 log_trace(gc, metaspace)(" expanding: minimum_desired_capacity: %6.1fKB expand_bytes: %6.1fKB MinMetaspaceExpansion: %6.1fKB new metaspace HWM: %6.1fKB",
brutisso@35061 266 minimum_desired_capacity / (double) K,
brutisso@35061 267 expand_bytes / (double) K,
brutisso@35061 268 MinMetaspaceExpansion / (double) K,
brutisso@35061 269 new_capacity_until_GC / (double) K);
coleenp@13728 270 }
coleenp@13728 271 return;
coleenp@13728 272 }
coleenp@13728 273
coleenp@13728 274 // No expansion, now see if we want to shrink
coleenp@13728 275 // We would never want to shrink more than this
ehelin@22499 276 assert(capacity_until_GC >= minimum_desired_capacity,
david@33105 277 SIZE_FORMAT " >= " SIZE_FORMAT,
david@33105 278 capacity_until_GC, minimum_desired_capacity);
jmasa@17109 279 size_t max_shrink_bytes = capacity_until_GC - minimum_desired_capacity;
coleenp@13728 280
coleenp@13728 281 // Should shrinking be considered?
jmasa@15610 282 if (MaxMetaspaceFreeRatio < 100) {
jmasa@15610 283 const double maximum_free_percentage = MaxMetaspaceFreeRatio / 100.0;
coleenp@13728 284 const double minimum_used_percentage = 1.0 - maximum_free_percentage;
coleenp@13728 285 const double max_tmp = used_after_gc / minimum_used_percentage;
coleenp@13728 286 size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx));
coleenp@13728 287 maximum_desired_capacity = MAX2(maximum_desired_capacity,
coleenp@13728 288 MetaspaceSize);
brutisso@35061 289 log_trace(gc, metaspace)(" maximum_free_percentage: %6.2f minimum_used_percentage: %6.2f",
brutisso@35061 290 maximum_free_percentage, minimum_used_percentage);
brutisso@35061 291 log_trace(gc, metaspace)(" minimum_desired_capacity: %6.1fKB maximum_desired_capacity: %6.1fKB",
brutisso@35061 292 minimum_desired_capacity / (double) K, maximum_desired_capacity / (double) K);
coleenp@13728 293
coleenp@13728 294 assert(minimum_desired_capacity <= maximum_desired_capacity,
coleenp@13728 295 "sanity check");
coleenp@13728 296
coleenp@13728 297 if (capacity_until_GC > maximum_desired_capacity) {
coleenp@13728 298 // Capacity too large, compute shrinking size
jmasa@17109 299 shrink_bytes = capacity_until_GC - maximum_desired_capacity;
coleenp@13728 300 // We don't want shrink all the way back to initSize if people call
coleenp@13728 301 // System.gc(), because some programs do that between "phases" and then
coleenp@13728 302 // we'd just have to grow the heap up again for the next phase. So we
coleenp@13728 303 // damp the shrinking: 0% on the first call, 10% on the second call, 40%
coleenp@13728 304 // on the third call, and 100% by the fourth call. But if we recompute
coleenp@13728 305 // size without shrinking, it goes back to 0%.
jmasa@17109 306 shrink_bytes = shrink_bytes / 100 * current_shrink_factor;
stefank@20406 307
stefank@46619 308 shrink_bytes = align_down(shrink_bytes, Metaspace::commit_alignment());
stefank@20406 309
jmasa@17109 310 assert(shrink_bytes <= max_shrink_bytes,
david@33105 311 "invalid shrink size " SIZE_FORMAT " not <= " SIZE_FORMAT,
david@33105 312 shrink_bytes, max_shrink_bytes);
coleenp@13728 313 if (current_shrink_factor == 0) {
coleenp@13728 314 _shrink_factor = 10;
coleenp@13728 315 } else {
coleenp@13728 316 _shrink_factor = MIN2(current_shrink_factor * 4, (uint) 100);
coleenp@13728 317 }
jmasa@37151 318 log_trace(gc, metaspace)(" shrinking: initThreshold: %.1fK maximum_desired_capacity: %.1fK",
brutisso@35061 319 MetaspaceSize / (double) K, maximum_desired_capacity / (double) K);
brutisso@35061 320 log_trace(gc, metaspace)(" shrink_bytes: %.1fK current_shrink_factor: %d new shrink factor: %d MinMetaspaceExpansion: %.1fK",
brutisso@35061 321 shrink_bytes / (double) K, current_shrink_factor, _shrink_factor, MinMetaspaceExpansion / (double) K);
coleenp@13728 322 }
coleenp@13728 323 }
coleenp@13728 324
coleenp@13728 325 // Don't shrink unless it's significant
jmasa@17109 326 if (shrink_bytes >= MinMetaspaceExpansion &&
jmasa@17109 327 ((capacity_until_GC - shrink_bytes) >= MetaspaceSize)) {
ehelin@23465 328 size_t new_capacity_until_GC = MetaspaceGC::dec_capacity_until_GC(shrink_bytes);
ehelin@23465 329 Metaspace::tracer()->report_gc_threshold(capacity_until_GC,
ehelin@23465 330 new_capacity_until_GC,
ehelin@23465 331 MetaspaceGCThresholdUpdater::ComputeNewSize);
coleenp@13728 332 }
coleenp@13728 333 }
coleenp@13728 334
stuefe@49854 335 // MetaspaceUtils
stuefe@50516 336 size_t MetaspaceUtils::_capacity_words [Metaspace:: MetadataTypeCount] = {0, 0};
stuefe@50516 337 size_t MetaspaceUtils::_overhead_words [Metaspace:: MetadataTypeCount] = {0, 0};
stuefe@50516 338 volatile size_t MetaspaceUtils::_used_words [Metaspace:: MetadataTypeCount] = {0, 0};
stuefe@50516 339
stuefe@50516 340 // Collect used metaspace statistics. This involves walking the CLDG. The resulting
stuefe@50516 341 // output will be the accumulated values for all live metaspaces.
stuefe@50516 342 // Note: method does not do any locking.
stuefe@50516 343 void MetaspaceUtils::collect_statistics(ClassLoaderMetaspaceStatistics* out) {
stuefe@50516 344 out->reset();
stuefe@50516 345 ClassLoaderDataGraphMetaspaceIterator iter;
stuefe@50516 346 while (iter.repeat()) {
stuefe@50516 347 ClassLoaderMetaspace* msp = iter.get_next();
stuefe@50516 348 if (msp != NULL) {
stuefe@50516 349 msp->add_to_statistics(out);
stuefe@50516 350 }
stuefe@50516 351 }
stuefe@50516 352 }
stuefe@50516 353
stuefe@50516 354 size_t MetaspaceUtils::free_in_vs_bytes(Metaspace::MetadataType mdtype) {
ehelin@19322 355 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
ehelin@19322 356 return list == NULL ? 0 : list->free_bytes();
ehelin@19322 357 }
ehelin@19322 358
stuefe@50516 359 size_t MetaspaceUtils::free_in_vs_bytes() {
stuefe@50516 360 return free_in_vs_bytes(Metaspace::ClassType) + free_in_vs_bytes(Metaspace::NonClassType);
jmasa@17109 361 }
jmasa@17109 362
stuefe@50516 363 static void inc_stat_nonatomically(size_t* pstat, size_t words) {
stuefe@50516 364 assert_lock_strong(MetaspaceExpand_lock);
stuefe@50516 365 (*pstat) += words;
stuefe@50516 366 }
stuefe@50516 367
stuefe@50516 368 static void dec_stat_nonatomically(size_t* pstat, size_t words) {
stuefe@50516 369 assert_lock_strong(MetaspaceExpand_lock);
stuefe@50516 370 const size_t size_now = *pstat;
stuefe@50516 371 assert(size_now >= words, "About to decrement counter below zero "
stuefe@50516 372 "(current value: " SIZE_FORMAT ", decrement value: " SIZE_FORMAT ".",
stuefe@50516 373 size_now, words);
stuefe@50516 374 *pstat = size_now - words;
stuefe@50516 375 }
stuefe@50516 376
stuefe@50516 377 static void inc_stat_atomically(volatile size_t* pstat, size_t words) {
stuefe@50516 378 Atomic::add(words, pstat);
stuefe@50516 379 }
stuefe@50516 380
stuefe@50516 381 static void dec_stat_atomically(volatile size_t* pstat, size_t words) {
stuefe@50516 382 const size_t size_now = *pstat;
stuefe@50516 383 assert(size_now >= words, "About to decrement counter below zero "
stuefe@50516 384 "(current value: " SIZE_FORMAT ", decrement value: " SIZE_FORMAT ".",
stuefe@50516 385 size_now, words);
stuefe@50516 386 Atomic::sub(words, pstat);
stuefe@50516 387 }
stuefe@50516 388
stuefe@49854 389 void MetaspaceUtils::dec_capacity(Metaspace::MetadataType mdtype, size_t words) {
stuefe@50516 390 dec_stat_nonatomically(&_capacity_words[mdtype], words);
jmasa@17109 391 }
stuefe@49854 392 void MetaspaceUtils::inc_capacity(Metaspace::MetadataType mdtype, size_t words) {
stuefe@50516 393 inc_stat_nonatomically(&_capacity_words[mdtype], words);
jmasa@17109 394 }
stuefe@49854 395 void MetaspaceUtils::dec_used(Metaspace::MetadataType mdtype, size_t words) {
stuefe@50516 396 dec_stat_atomically(&_used_words[mdtype], words);
jmasa@17109 397 }
stuefe@49854 398 void MetaspaceUtils::inc_used(Metaspace::MetadataType mdtype, size_t words) {
stuefe@50516 399 inc_stat_atomically(&_used_words[mdtype], words);
jmasa@17109 400 }
stuefe@50516 401 void MetaspaceUtils::dec_overhead(Metaspace::MetadataType mdtype, size_t words) {
stuefe@50516 402 dec_stat_nonatomically(&_overhead_words[mdtype], words);
jmasa@13733 403 }
stuefe@50516 404 void MetaspaceUtils::inc_overhead(Metaspace::MetadataType mdtype, size_t words) {
stuefe@50516 405 inc_stat_nonatomically(&_overhead_words[mdtype], words);
ehelin@19988 406 }
ehelin@19988 407
stuefe@49854 408 size_t MetaspaceUtils::reserved_bytes(Metaspace::MetadataType mdtype) {
ehelin@19322 409 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
stefank@19989 410 return list == NULL ? 0 : list->reserved_bytes();
stefank@19989 411 }
stefank@19989 412
stuefe@49854 413 size_t MetaspaceUtils::committed_bytes(Metaspace::MetadataType mdtype) {
stefank@19989 414 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
stefank@19989 415 return list == NULL ? 0 : list->committed_bytes();
coleenp@13728 416 }
coleenp@13728 417
stuefe@49854 418 size_t MetaspaceUtils::min_chunk_size_words() { return Metaspace::first_chunk_word_size(); }
stuefe@49854 419
stuefe@49854 420 size_t MetaspaceUtils::free_chunks_total_words(Metaspace::MetadataType mdtype) {
stefank@20081 421 ChunkManager* chunk_manager = Metaspace::get_chunk_manager(mdtype);
stefank@20081 422 if (chunk_manager == NULL) {
hseigel@19319 423 return 0;
hseigel@19319 424 }
stefank@20081 425 chunk_manager->slow_verify();
stefank@20081 426 return chunk_manager->free_chunks_total_words();
coleenp@13728 427 }
coleenp@13728 428
stuefe@49854 429 size_t MetaspaceUtils::free_chunks_total_bytes(Metaspace::MetadataType mdtype) {
ehelin@19988 430 return free_chunks_total_words(mdtype) * BytesPerWord;
coleenp@13728 431 }
coleenp@13728 432
stuefe@49854 433 size_t MetaspaceUtils::free_chunks_total_words() {
ehelin@19988 434 return free_chunks_total_words(Metaspace::ClassType) +
ehelin@19988 435 free_chunks_total_words(Metaspace::NonClassType);
jmasa@17109 436 }
jmasa@17109 437
stuefe@49854 438 size_t MetaspaceUtils::free_chunks_total_bytes() {
ehelin@19988 439 return free_chunks_total_words() * BytesPerWord;
jmasa@17109 440 }
jmasa@17109 441
stuefe@49854 442 bool MetaspaceUtils::has_chunk_free_list(Metaspace::MetadataType mdtype) {
ehelin@23470 443 return Metaspace::get_chunk_manager(mdtype) != NULL;
ehelin@23470 444 }
ehelin@23470 445
stuefe@49854 446 MetaspaceChunkFreeListSummary MetaspaceUtils::chunk_free_list_summary(Metaspace::MetadataType mdtype) {
ehelin@23470 447 if (!has_chunk_free_list(mdtype)) {
ehelin@23470 448 return MetaspaceChunkFreeListSummary();
ehelin@23470 449 }
ehelin@23470 450
ehelin@23470 451 const ChunkManager* cm = Metaspace::get_chunk_manager(mdtype);
ehelin@23470 452 return cm->chunk_free_list_summary();
ehelin@23470 453 }
ehelin@23470 454
stuefe@49854 455 void MetaspaceUtils::print_metaspace_change(size_t prev_metadata_used) {
brutisso@35061 456 log_info(gc, metaspace)("Metaspace: " SIZE_FORMAT "K->" SIZE_FORMAT "K(" SIZE_FORMAT "K)",
brutisso@35061 457 prev_metadata_used/K, used_bytes()/K, reserved_bytes()/K);
coleenp@13728 458 }
coleenp@13728 459
stuefe@49854 460 void MetaspaceUtils::print_on(outputStream* out) {
coleenp@13728 461 Metaspace::MetadataType nct = Metaspace::NonClassType;
coleenp@13728 462
stefank@20406 463 out->print_cr(" Metaspace "
stefank@20406 464 "used " SIZE_FORMAT "K, "
stefank@20406 465 "capacity " SIZE_FORMAT "K, "
stefank@20406 466 "committed " SIZE_FORMAT "K, "
stefank@20406 467 "reserved " SIZE_FORMAT "K",
ehelin@23854 468 used_bytes()/K,
ehelin@23854 469 capacity_bytes()/K,
stefank@20406 470 committed_bytes()/K,
stefank@20406 471 reserved_bytes()/K);
stefank@20406 472
hseigel@19319 473 if (Metaspace::using_class_space()) {
hseigel@19319 474 Metaspace::MetadataType ct = Metaspace::ClassType;
hseigel@19319 475 out->print_cr(" class space "
stefank@20406 476 "used " SIZE_FORMAT "K, "
stefank@20406 477 "capacity " SIZE_FORMAT "K, "
stefank@20406 478 "committed " SIZE_FORMAT "K, "
stefank@20406 479 "reserved " SIZE_FORMAT "K",
ehelin@23854 480 used_bytes(ct)/K,
ehelin@23854 481 capacity_bytes(ct)/K,
stefank@20406 482 committed_bytes(ct)/K,
ehelin@19988 483 reserved_bytes(ct)/K);
hseigel@19319 484 }
coleenp@13728 485 }
coleenp@13728 486
zgu@47983 487
stuefe@50516 488 void MetaspaceUtils::print_vs(outputStream* out, size_t scale) {
stuefe@50516 489 const size_t reserved_nonclass_words = reserved_bytes(Metaspace::NonClassType) / sizeof(MetaWord);
stuefe@50516 490 const size_t committed_nonclass_words = committed_bytes(Metaspace::NonClassType) / sizeof(MetaWord);
stuefe@50516 491 {
stuefe@50516 492 if (Metaspace::using_class_space()) {
stuefe@50516 493 out->print(" Non-class space: ");
stuefe@50516 494 }
stuefe@50516 495 print_scaled_words(out, reserved_nonclass_words, scale, 7);
stuefe@50516 496 out->print(" reserved, ");
stuefe@50516 497 print_scaled_words_and_percentage(out, committed_nonclass_words, reserved_nonclass_words, scale, 7);
stuefe@50516 498 out->print_cr(" committed ");
stuefe@50516 499
stuefe@50516 500 if (Metaspace::using_class_space()) {
stuefe@50516 501 const size_t reserved_class_words = reserved_bytes(Metaspace::ClassType) / sizeof(MetaWord);
stuefe@50516 502 const size_t committed_class_words = committed_bytes(Metaspace::ClassType) / sizeof(MetaWord);
stuefe@50516 503 out->print(" Class space: ");
stuefe@50516 504 print_scaled_words(out, reserved_class_words, scale, 7);
stuefe@50516 505 out->print(" reserved, ");
stuefe@50516 506 print_scaled_words_and_percentage(out, committed_class_words, reserved_class_words, scale, 7);
stuefe@50516 507 out->print_cr(" committed ");
stuefe@50516 508
stuefe@50516 509 const size_t reserved_words = reserved_nonclass_words + reserved_class_words;
stuefe@50516 510 const size_t committed_words = committed_nonclass_words + committed_class_words;
stuefe@50516 511 out->print(" Both: ");
stuefe@50516 512 print_scaled_words(out, reserved_words, scale, 7);
stuefe@50516 513 out->print(" reserved, ");
stuefe@50516 514 print_scaled_words_and_percentage(out, committed_words, reserved_words, scale, 7);
stuefe@50516 515 out->print_cr(" committed ");
stuefe@50516 516 }
stuefe@50516 517 }
stuefe@50516 518 }
stuefe@50516 519
stuefe@50516 520 // This will print out a basic metaspace usage report but
stuefe@50516 521 // unlike print_report() is guaranteed not to lock or to walk the CLDG.
stuefe@50516 522 void MetaspaceUtils::print_basic_report(outputStream* out, size_t scale) {
stuefe@50516 523
stuefe@50516 524 out->cr();
stuefe@50516 525 out->print_cr("Usage:");
zgu@47983 526
zgu@47983 527 if (Metaspace::using_class_space()) {
stuefe@50516 528 out->print(" Non-class: ");
stuefe@50516 529 }
stuefe@50516 530
stuefe@50516 531 // In its most basic form, we do not require walking the CLDG. Instead, just print the running totals from
stuefe@50516 532 // MetaspaceUtils.
stuefe@50516 533 const size_t cap_nc = MetaspaceUtils::capacity_words(Metaspace::NonClassType);
stuefe@50516 534 const size_t overhead_nc = MetaspaceUtils::overhead_words(Metaspace::NonClassType);
stuefe@50516 535 const size_t used_nc = MetaspaceUtils::used_words(Metaspace::NonClassType);
stuefe@50516 536 const size_t free_and_waste_nc = cap_nc - overhead_nc - used_nc;
stuefe@50516 537
stuefe@50516 538 print_scaled_words(out, cap_nc, scale, 5);
stuefe@50516 539 out->print(" capacity, ");
stuefe@50516 540 print_scaled_words_and_percentage(out, used_nc, cap_nc, scale, 5);
stuefe@50516 541 out->print(" used, ");
stuefe@50516 542 print_scaled_words_and_percentage(out, free_and_waste_nc, cap_nc, scale, 5);
stuefe@50516 543 out->print(" free+waste, ");
stuefe@50516 544 print_scaled_words_and_percentage(out, overhead_nc, cap_nc, scale, 5);
stuefe@50516 545 out->print(" overhead. ");
stuefe@50516 546 out->cr();
stuefe@50516 547
stuefe@50516 548 if (Metaspace::using_class_space()) {
stuefe@50516 549 const size_t cap_c = MetaspaceUtils::capacity_words(Metaspace::ClassType);
stuefe@50516 550 const size_t overhead_c = MetaspaceUtils::overhead_words(Metaspace::ClassType);
stuefe@50516 551 const size_t used_c = MetaspaceUtils::used_words(Metaspace::ClassType);
stuefe@50516 552 const size_t free_and_waste_c = cap_c - overhead_c - used_c;
stuefe@50516 553 out->print(" Class: ");
stuefe@50516 554 print_scaled_words(out, cap_c, scale, 5);
stuefe@50516 555 out->print(" capacity, ");
stuefe@50516 556 print_scaled_words_and_percentage(out, used_c, cap_c, scale, 5);
stuefe@50516 557 out->print(" used, ");
stuefe@50516 558 print_scaled_words_and_percentage(out, free_and_waste_c, cap_c, scale, 5);
stuefe@50516 559 out->print(" free+waste, ");
stuefe@50516 560 print_scaled_words_and_percentage(out, overhead_c, cap_c, scale, 5);
stuefe@50516 561 out->print(" overhead. ");
stuefe@50516 562 out->cr();
stuefe@50516 563
stuefe@50516 564 out->print(" Both: ");
stuefe@50516 565 const size_t cap = cap_nc + cap_c;
stuefe@50516 566
stuefe@50516 567 print_scaled_words(out, cap, scale, 5);
stuefe@50516 568 out->print(" capacity, ");
stuefe@50516 569 print_scaled_words_and_percentage(out, used_nc + used_c, cap, scale, 5);
stuefe@50516 570 out->print(" used, ");
stuefe@50516 571 print_scaled_words_and_percentage(out, free_and_waste_nc + free_and_waste_c, cap, scale, 5);
stuefe@50516 572 out->print(" free+waste, ");
stuefe@50516 573 print_scaled_words_and_percentage(out, overhead_nc + overhead_c, cap, scale, 5);
stuefe@50516 574 out->print(" overhead. ");
stuefe@50516 575 out->cr();
stuefe@50516 576 }
stuefe@50516 577
stuefe@50516 578 out->cr();
stuefe@50516 579 out->print_cr("Virtual space:");
stuefe@50516 580
stuefe@50516 581 print_vs(out, scale);
stuefe@50516 582
stuefe@50516 583 out->cr();
stuefe@50516 584 out->print_cr("Chunk freelists:");
stuefe@50516 585
stuefe@50516 586 if (Metaspace::using_class_space()) {
stuefe@50516 587 out->print(" Non-Class: ");
stuefe@50516 588 }
stuefe@50516 589 print_human_readable_size(out, Metaspace::chunk_manager_metadata()->free_chunks_total_words(), scale);
stuefe@50516 590 out->cr();
stuefe@50516 591 if (Metaspace::using_class_space()) {
stuefe@50516 592 out->print(" Class: ");
stuefe@50516 593 print_human_readable_size(out, Metaspace::chunk_manager_class()->free_chunks_total_words(), scale);
stuefe@50516 594 out->cr();
stuefe@50516 595 out->print(" Both: ");
stuefe@50516 596 print_human_readable_size(out, Metaspace::chunk_manager_class()->free_chunks_total_words() +
stuefe@50516 597 Metaspace::chunk_manager_metadata()->free_chunks_total_words(), scale);
stuefe@50516 598 out->cr();
stuefe@50516 599 }
stuefe@50516 600 out->cr();
stuefe@50516 601
stuefe@50516 602 }
stuefe@50516 603
stuefe@50516 604 void MetaspaceUtils::print_report(outputStream* out, size_t scale, int flags) {
stuefe@50516 605
stuefe@50516 606 const bool print_loaders = (flags & rf_show_loaders) > 0;
stuefe@50893 607 const bool print_classes = (flags & rf_show_classes) > 0;
stuefe@50516 608 const bool print_by_chunktype = (flags & rf_break_down_by_chunktype) > 0;
stuefe@50516 609 const bool print_by_spacetype = (flags & rf_break_down_by_spacetype) > 0;
stuefe@50516 610
stuefe@50516 611 // Some report options require walking the class loader data graph.
stuefe@50893 612 PrintCLDMetaspaceInfoClosure cl(out, scale, print_loaders, print_classes, print_by_chunktype);
stuefe@50516 613 if (print_loaders) {
stuefe@50516 614 out->cr();
stuefe@50516 615 out->print_cr("Usage per loader:");
stuefe@50516 616 out->cr();
stuefe@50516 617 }
stuefe@50516 618
coleenp@53554 619 ClassLoaderDataGraph::loaded_cld_do(&cl); // collect data and optionally print
stuefe@50516 620
stuefe@50516 621 // Print totals, broken up by space type.
stuefe@50516 622 if (print_by_spacetype) {
stuefe@50516 623 out->cr();
stuefe@50516 624 out->print_cr("Usage per space type:");
stuefe@50516 625 out->cr();
stuefe@50516 626 for (int space_type = (int)Metaspace::ZeroMetaspaceType;
stuefe@50516 627 space_type < (int)Metaspace::MetaspaceTypeCount; space_type ++)
stuefe@50516 628 {
stuefe@50516 629 uintx num = cl._num_loaders_by_spacetype[space_type];
stuefe@50516 630 out->print("%s (" UINTX_FORMAT " loader%s)%c",
stuefe@50516 631 space_type_name((Metaspace::MetaspaceType)space_type),
stuefe@50516 632 num, (num == 1 ? "" : "s"), (num > 0 ? ':' : '.'));
stuefe@50516 633 if (num > 0) {
stuefe@50516 634 cl._stats_by_spacetype[space_type].print_on(out, scale, print_by_chunktype);
stuefe@50516 635 }
stuefe@50516 636 out->cr();
zgu@47983 637 }
stuefe@50516 638 }
stuefe@50516 639
stuefe@50516 640 // Print totals for in-use data:
stuefe@50516 641 out->cr();
stuefe@50516 642 out->print_cr("Total Usage ( " UINTX_FORMAT " loader%s)%c",
stuefe@50516 643 cl._num_loaders, (cl._num_loaders == 1 ? "" : "s"), (cl._num_loaders > 0 ? ':' : '.'));
stuefe@50516 644
stuefe@50516 645 cl._stats_total.print_on(out, scale, print_by_chunktype);
stuefe@50516 646
stuefe@50516 647 // -- Print Virtual space.
stuefe@50516 648 out->cr();
stuefe@50516 649 out->print_cr("Virtual space:");
stuefe@50516 650
stuefe@50516 651 print_vs(out, scale);
stuefe@50516 652
stuefe@50516 653 // -- Print VirtualSpaceList details.
stuefe@50516 654 if ((flags & rf_show_vslist) > 0) {
stuefe@50516 655 out->cr();
stuefe@50516 656 out->print_cr("Virtual space list%s:", Metaspace::using_class_space() ? "s" : "");
stuefe@50516 657
stuefe@50516 658 if (Metaspace::using_class_space()) {
stuefe@50516 659 out->print_cr(" Non-Class:");
stuefe@50516 660 }
stuefe@50516 661 Metaspace::space_list()->print_on(out, scale);
stuefe@50516 662 if (Metaspace::using_class_space()) {
stuefe@50516 663 out->print_cr(" Class:");
stuefe@50516 664 Metaspace::class_space_list()->print_on(out, scale);
stuefe@50516 665 }
stuefe@50516 666 }
stuefe@50516 667 out->cr();
stuefe@50516 668
stuefe@50516 669 // -- Print VirtualSpaceList map.
stuefe@50516 670 if ((flags & rf_show_vsmap) > 0) {
stuefe@50516 671 out->cr();
stuefe@50516 672 out->print_cr("Virtual space map:");
stuefe@50516 673
stuefe@50516 674 if (Metaspace::using_class_space()) {
stuefe@50516 675 out->print_cr(" Non-Class:");
stuefe@50516 676 }
stuefe@50516 677 Metaspace::space_list()->print_map(out);
stuefe@50516 678 if (Metaspace::using_class_space()) {
stuefe@50516 679 out->print_cr(" Class:");
stuefe@50516 680 Metaspace::class_space_list()->print_map(out);
stuefe@50516 681 }
stuefe@50516 682 }
stuefe@50516 683 out->cr();
stuefe@50516 684
stuefe@50516 685 // -- Print Freelists (ChunkManager) details
stuefe@50516 686 out->cr();
stuefe@50516 687 out->print_cr("Chunk freelist%s:", Metaspace::using_class_space() ? "s" : "");
stuefe@50516 688
stuefe@50516 689 ChunkManagerStatistics non_class_cm_stat;
stuefe@50516 690 Metaspace::chunk_manager_metadata()->collect_statistics(&non_class_cm_stat);
zgu@47983 691
zgu@47983 692 if (Metaspace::using_class_space()) {
stuefe@50516 693 out->print_cr(" Non-Class:");
stuefe@50516 694 }
stuefe@50516 695 non_class_cm_stat.print_on(out, scale);
zgu@47983 696
zgu@47983 697 if (Metaspace::using_class_space()) {
stuefe@50516 698 ChunkManagerStatistics class_cm_stat;
stuefe@50516 699 Metaspace::chunk_manager_class()->collect_statistics(&class_cm_stat);
stuefe@50516 700 out->print_cr(" Class:");
stuefe@50516 701 class_cm_stat.print_on(out, scale);
stuefe@50516 702 }
stuefe@50516 703
stuefe@50516 704 // As a convenience, print a summary of common waste.
stuefe@50516 705 out->cr();
stuefe@50516 706 out->print("Waste ");
stuefe@50516 707 // For all wastages, print percentages from total. As total use the total size of memory committed for metaspace.
stuefe@50516 708 const size_t committed_words = committed_bytes() / BytesPerWord;
stuefe@50516 709
stuefe@50516 710 out->print("(percentages refer to total committed size ");
stuefe@50516 711 print_scaled_words(out, committed_words, scale);
stuefe@50516 712 out->print_cr("):");
stuefe@50516 713
stuefe@50516 714 // Print space committed but not yet used by any class loader
stuefe@50516 715 const size_t unused_words_in_vs = MetaspaceUtils::free_in_vs_bytes() / BytesPerWord;
stuefe@50516 716 out->print(" Committed unused: ");
stuefe@50516 717 print_scaled_words_and_percentage(out, unused_words_in_vs, committed_words, scale, 6);
stuefe@50516 718 out->cr();
stuefe@50516 719
stuefe@50516 720 // Print waste for in-use chunks.
stuefe@50516 721 UsedChunksStatistics ucs_nonclass = cl._stats_total.nonclass_sm_stats().totals();
stuefe@50516 722 UsedChunksStatistics ucs_class = cl._stats_total.class_sm_stats().totals();
stuefe@50516 723 UsedChunksStatistics ucs_all;
stuefe@50516 724 ucs_all.add(ucs_nonclass);
stuefe@50516 725 ucs_all.add(ucs_class);
stuefe@50516 726
stuefe@50516 727 out->print(" Waste in chunks in use: ");
stuefe@50516 728 print_scaled_words_and_percentage(out, ucs_all.waste(), committed_words, scale, 6);
stuefe@50516 729 out->cr();
stuefe@50516 730 out->print(" Free in chunks in use: ");
stuefe@50516 731 print_scaled_words_and_percentage(out, ucs_all.free(), committed_words, scale, 6);
stuefe@50516 732 out->cr();
stuefe@50516 733 out->print(" Overhead in chunks in use: ");
stuefe@50516 734 print_scaled_words_and_percentage(out, ucs_all.overhead(), committed_words, scale, 6);
stuefe@50516 735 out->cr();
stuefe@50516 736
stuefe@50516 737 // Print waste in free chunks.
stuefe@50516 738 const size_t total_capacity_in_free_chunks =
stuefe@50516 739 Metaspace::chunk_manager_metadata()->free_chunks_total_words() +
stuefe@50516 740 (Metaspace::using_class_space() ? Metaspace::chunk_manager_class()->free_chunks_total_words() : 0);
stuefe@50516 741 out->print(" In free chunks: ");
stuefe@50516 742 print_scaled_words_and_percentage(out, total_capacity_in_free_chunks, committed_words, scale, 6);
stuefe@50516 743 out->cr();
stuefe@50516 744
stuefe@50516 745 // Print waste in deallocated blocks.
stuefe@50516 746 const uintx free_blocks_num =
stuefe@50516 747 cl._stats_total.nonclass_sm_stats().free_blocks_num() +
stuefe@50516 748 cl._stats_total.class_sm_stats().free_blocks_num();
stuefe@50516 749 const size_t free_blocks_cap_words =
stuefe@50516 750 cl._stats_total.nonclass_sm_stats().free_blocks_cap_words() +
stuefe@50516 751 cl._stats_total.class_sm_stats().free_blocks_cap_words();
stuefe@50516 752 out->print("Deallocated from chunks in use: ");
stuefe@50516 753 print_scaled_words_and_percentage(out, free_blocks_cap_words, committed_words, scale, 6);
kbarrett@50556 754 out->print(" (" UINTX_FORMAT " blocks)", free_blocks_num);
stuefe@50516 755 out->cr();
stuefe@50516 756
stuefe@50516 757 // Print total waste.
stuefe@50516 758 const size_t total_waste = ucs_all.waste() + ucs_all.free() + ucs_all.overhead() + total_capacity_in_free_chunks
stuefe@50516 759 + free_blocks_cap_words + unused_words_in_vs;
stuefe@50516 760 out->print(" -total-: ");
stuefe@50516 761 print_scaled_words_and_percentage(out, total_waste, committed_words, scale, 6);
stuefe@50516 762 out->cr();
stuefe@50516 763
stuefe@50516 764 // Print internal statistics
stuefe@50516 765 #ifdef ASSERT
stuefe@50516 766 out->cr();
stuefe@50516 767 out->cr();
stuefe@50516 768 out->print_cr("Internal statistics:");
stuefe@50516 769 out->cr();
stuefe@50516 770 out->print_cr("Number of allocations: " UINTX_FORMAT ".", g_internal_statistics.num_allocs);
stuefe@50516 771 out->print_cr("Number of space births: " UINTX_FORMAT ".", g_internal_statistics.num_metaspace_births);
stuefe@50516 772 out->print_cr("Number of space deaths: " UINTX_FORMAT ".", g_internal_statistics.num_metaspace_deaths);
stuefe@50516 773 out->print_cr("Number of virtual space node births: " UINTX_FORMAT ".", g_internal_statistics.num_vsnodes_created);
stuefe@50516 774 out->print_cr("Number of virtual space node deaths: " UINTX_FORMAT ".", g_internal_statistics.num_vsnodes_purged);
stuefe@50516 775 out->print_cr("Number of times virtual space nodes were expanded: " UINTX_FORMAT ".", g_internal_statistics.num_committed_space_expanded);
stuefe@50516 776 out->print_cr("Number of deallocations: " UINTX_FORMAT " (" UINTX_FORMAT " external).", g_internal_statistics.num_deallocs, g_internal_statistics.num_external_deallocs);
stuefe@50516 777 out->print_cr("Allocations from deallocated blocks: " UINTX_FORMAT ".", g_internal_statistics.num_allocs_from_deallocated_blocks);
stuefe@50516 778 out->cr();
stuefe@50516 779 #endif
stuefe@50516 780
stuefe@50516 781 // Print some interesting settings
stuefe@50516 782 out->cr();
stuefe@50516 783 out->cr();
stuefe@50516 784 out->print("MaxMetaspaceSize: ");
stuefe@50516 785 print_human_readable_size(out, MaxMetaspaceSize, scale);
stuefe@50516 786 out->cr();
stuefe@50516 787 out->print("InitialBootClassLoaderMetaspaceSize: ");
stuefe@50516 788 print_human_readable_size(out, InitialBootClassLoaderMetaspaceSize, scale);
stuefe@50516 789 out->cr();
stuefe@50516 790
stuefe@50516 791 out->print("UseCompressedClassPointers: %s", UseCompressedClassPointers ? "true" : "false");
stuefe@50516 792 out->cr();
zgu@47983 793 if (Metaspace::using_class_space()) {
stuefe@50516 794 out->print("CompressedClassSpaceSize: ");
stuefe@50516 795 print_human_readable_size(out, CompressedClassSpaceSize, scale);
zgu@47983 796 }
zgu@47983 797
zgu@47983 798 out->cr();
zgu@47983 799 out->cr();
stuefe@50516 800
stuefe@50516 801 } // MetaspaceUtils::print_report()
coleenp@13728 802
stuefe@47989 803 // Prints an ASCII representation of the given space.
stuefe@49854 804 void MetaspaceUtils::print_metaspace_map(outputStream* out, Metaspace::MetadataType mdtype) {
coleenp@50096 805 MutexLockerEx cl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
stuefe@47989 806 const bool for_class = mdtype == Metaspace::ClassType ? true : false;
stuefe@47989 807 VirtualSpaceList* const vsl = for_class ? Metaspace::class_space_list() : Metaspace::space_list();
stuefe@47989 808 if (vsl != NULL) {
stuefe@47989 809 if (for_class) {
stuefe@47989 810 if (!Metaspace::using_class_space()) {
stuefe@47989 811 out->print_cr("No Class Space.");
stuefe@47989 812 return;
stuefe@47989 813 }
stuefe@47989 814 out->print_raw("---- Metaspace Map (Class Space) ----");
stuefe@47989 815 } else {
stuefe@47989 816 out->print_raw("---- Metaspace Map (Non-Class Space) ----");
stuefe@47989 817 }
stuefe@47989 818 // Print legend:
stuefe@47989 819 out->cr();
stuefe@47989 820 out->print_cr("Chunk Types (uppercase chunks are in use): x-specialized, s-small, m-medium, h-humongous.");
stuefe@47989 821 out->cr();
stuefe@47989 822 VirtualSpaceList* const vsl = for_class ? Metaspace::class_space_list() : Metaspace::space_list();
stuefe@47989 823 vsl->print_map(out);
stuefe@47989 824 out->cr();
stuefe@47989 825 }
stuefe@47989 826 }
stuefe@47989 827
stuefe@49854 828 void MetaspaceUtils::verify_free_chunks() {
stefank@20081 829 Metaspace::chunk_manager_metadata()->verify();
hseigel@19319 830 if (Metaspace::using_class_space()) {
stefank@20081 831 Metaspace::chunk_manager_class()->verify();
hseigel@19319 832 }
mgerdin@14474 833 }
mgerdin@14474 834
stuefe@50516 835 void MetaspaceUtils::verify_metrics() {
jmasa@17109 836 #ifdef ASSERT
stuefe@50516 837 // Please note: there are time windows where the internal counters are out of sync with
stuefe@50516 838 // reality. For example, when a newly created ClassLoaderMetaspace creates its first chunk -
stuefe@50516 839 // the ClassLoaderMetaspace is not yet attached to its ClassLoaderData object and hence will
stuefe@50516 840 // not be counted when iterating the CLDG. So be careful when you call this method.
stuefe@50516 841 ClassLoaderMetaspaceStatistics total_stat;
stuefe@50516 842 collect_statistics(&total_stat);
stuefe@50516 843 UsedChunksStatistics nonclass_chunk_stat = total_stat.nonclass_sm_stats().totals();
stuefe@50516 844 UsedChunksStatistics class_chunk_stat = total_stat.class_sm_stats().totals();
stuefe@50516 845
stuefe@50516 846 bool mismatch = false;
stuefe@50516 847 for (int i = 0; i < Metaspace::MetadataTypeCount; i ++) {
stuefe@50516 848 Metaspace::MetadataType mdtype = (Metaspace::MetadataType)i;
stuefe@50516 849 UsedChunksStatistics chunk_stat = total_stat.sm_stats(mdtype).totals();
stuefe@50516 850 if (capacity_words(mdtype) != chunk_stat.cap() ||
stuefe@50516 851 used_words(mdtype) != chunk_stat.used() ||
stuefe@50516 852 overhead_words(mdtype) != chunk_stat.overhead()) {
stuefe@50516 853 mismatch = true;
stuefe@50516 854 tty->print_cr("MetaspaceUtils::verify_metrics: counter mismatch for mdtype=%u:", mdtype);
stuefe@50516 855 tty->print_cr("Expected cap " SIZE_FORMAT ", used " SIZE_FORMAT ", overhead " SIZE_FORMAT ".",
stuefe@50516 856 capacity_words(mdtype), used_words(mdtype), overhead_words(mdtype));
stuefe@50516 857 tty->print_cr("Got cap " SIZE_FORMAT ", used " SIZE_FORMAT ", overhead " SIZE_FORMAT ".",
stuefe@50516 858 chunk_stat.cap(), chunk_stat.used(), chunk_stat.overhead());
stuefe@50516 859 tty->flush();
stuefe@50516 860 }
stuefe@50516 861 }
stuefe@50516 862 assert(mismatch == false, "MetaspaceUtils::verify_metrics: counter mismatch.");
jmasa@17109 863 #endif
jmasa@17109 864 }
jmasa@17109 865
mdoerr@52843 866 // Utils to check if a pointer or range is part of a committed metaspace region.
mdoerr@52843 867 metaspace::VirtualSpaceNode* MetaspaceUtils::find_enclosing_virtual_space(const void* p) {
mdoerr@52843 868 VirtualSpaceNode* vsn = Metaspace::space_list()->find_enclosing_space(p);
mdoerr@52843 869 if (Metaspace::using_class_space() && vsn == NULL) {
mdoerr@52843 870 vsn = Metaspace::class_space_list()->find_enclosing_space(p);
mdoerr@52843 871 }
mdoerr@52843 872 return vsn;
mdoerr@52843 873 }
mdoerr@52843 874
mdoerr@52843 875 bool MetaspaceUtils::is_in_committed(const void* p) {
mdoerr@52843 876 #if INCLUDE_CDS
mdoerr@52843 877 if (UseSharedSpaces) {
mdoerr@52843 878 for (int idx = MetaspaceShared::ro; idx <= MetaspaceShared::mc; idx++) {
mdoerr@52843 879 if (FileMapInfo::current_info()->is_in_shared_region(p, idx)) {
mdoerr@52843 880 return true;
mdoerr@52843 881 }
mdoerr@52843 882 }
mdoerr@52843 883 }
mdoerr@52843 884 #endif
mdoerr@52843 885 return find_enclosing_virtual_space(p) != NULL;
mdoerr@52843 886 }
mdoerr@52843 887
mdoerr@52843 888 bool MetaspaceUtils::is_range_in_committed(const void* from, const void* to) {
mdoerr@52843 889 #if INCLUDE_CDS
mdoerr@52843 890 if (UseSharedSpaces) {
mdoerr@52843 891 for (int idx = MetaspaceShared::ro; idx <= MetaspaceShared::mc; idx++) {
mdoerr@52843 892 if (FileMapInfo::current_info()->is_in_shared_region(from, idx)) {
mdoerr@52843 893 return FileMapInfo::current_info()->is_in_shared_region(to, idx);
mdoerr@52843 894 }
mdoerr@52843 895 }
mdoerr@52843 896 }
mdoerr@52843 897 #endif
mdoerr@52843 898 VirtualSpaceNode* vsn = find_enclosing_virtual_space(from);
mdoerr@52843 899 return (vsn != NULL) && vsn->contains(to);
mdoerr@52843 900 }
mdoerr@52843 901
jmasa@17109 902
coleenp@13728 903 // Metaspace methods
coleenp@13728 904
coleenp@13728 905 size_t Metaspace::_first_chunk_word_size = 0;
jmasa@15086 906 size_t Metaspace::_first_class_chunk_word_size = 0;
jmasa@15086 907
stefank@20406 908 size_t Metaspace::_commit_alignment = 0;
stefank@20406 909 size_t Metaspace::_reserve_alignment = 0;
stefank@20406 910
coleenp@13728 911 VirtualSpaceList* Metaspace::_space_list = NULL;
coleenp@13728 912 VirtualSpaceList* Metaspace::_class_space_list = NULL;
coleenp@13728 913
stefank@20081 914 ChunkManager* Metaspace::_chunk_manager_metadata = NULL;
stefank@20081 915 ChunkManager* Metaspace::_chunk_manager_class = NULL;
stefank@20081 916
coleenp@13728 917 #define VIRTUALSPACEMULTIPLIER 2
coleenp@13728 918
hseigel@19319 919 #ifdef _LP64
coleenp@21188 920 static const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
coleenp@21188 921
hseigel@19319 922 void Metaspace::set_narrow_klass_base_and_shift(address metaspace_base, address cds_base) {
iklam@46746 923 assert(!DumpSharedSpaces, "narrow_klass is set by MetaspaceShared class.");
hseigel@19319 924 // Figure out the narrow_klass_base and the narrow_klass_shift. The
hseigel@19319 925 // narrow_klass_base is the lower of the metaspace base and the cds base
hseigel@19319 926 // (if cds is enabled). The narrow_klass_shift depends on the distance
hseigel@19319 927 // between the lower base and higher address.
hseigel@19319 928 address lower_base;
hseigel@19319 929 address higher_address;
iklam@26135 930 #if INCLUDE_CDS
hseigel@19319 931 if (UseSharedSpaces) {
iklam@46746 932 higher_address = MAX2((address)(cds_base + MetaspaceShared::core_spaces_size()),
coleenp@21188 933 (address)(metaspace_base + compressed_class_space_size()));
hseigel@19319 934 lower_base = MIN2(metaspace_base, cds_base);
iklam@26135 935 } else
iklam@26135 936 #endif
iklam@26135 937 {
coleenp@21188 938 higher_address = metaspace_base + compressed_class_space_size();
hseigel@19319 939 lower_base = metaspace_base;
coleenp@21188 940
coleenp@21188 941 uint64_t klass_encoding_max = UnscaledClassSpaceMax << LogKlassAlignmentInBytes;
coleenp@21188 942 // If compressed class space fits in lower 32G, we don't need a base.
coleenp@21188 943 if (higher_address <= (address)klass_encoding_max) {
jwilhelm@22551 944 lower_base = 0; // Effectively lower base is zero.
coleenp@21188 945 }
hseigel@19319 946 }
coleenp@21188 947
hseigel@19319 948 Universe::set_narrow_klass_base(lower_base);
coleenp@21188 949
jiangli@47754 950 // CDS uses LogKlassAlignmentInBytes for narrow_klass_shift. See
jiangli@47754 951 // MetaspaceShared::initialize_dumptime_shared_and_meta_spaces() for
jiangli@47754 952 // how dump time narrow_klass_shift is set. Although, CDS can work
jiangli@47754 953 // with zero-shift mode also, to be consistent with AOT it uses
jiangli@47754 954 // LogKlassAlignmentInBytes for klass shift so archived java heap objects
jiangli@47754 955 // can be used at same time as AOT code.
jiangli@47754 956 if (!UseSharedSpaces
jiangli@47754 957 && (uint64_t)(higher_address - lower_base) <= UnscaledClassSpaceMax) {
hseigel@19319 958 Universe::set_narrow_klass_shift(0);
hseigel@19319 959 } else {
hseigel@19319 960 Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes);
hseigel@19319 961 }
kvn@42650 962 AOTLoader::set_narrow_klass_shift();
hseigel@19319 963 }
hseigel@19319 964
iklam@26135 965 #if INCLUDE_CDS
hseigel@19319 966 // Return TRUE if the specified metaspace_base and cds_base are close enough
hseigel@19319 967 // to work with compressed klass pointers.
hseigel@19319 968 bool Metaspace::can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base) {
hseigel@19319 969 assert(cds_base != 0 && UseSharedSpaces, "Only use with CDS");
ehelin@19979 970 assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
hseigel@19319 971 address lower_base = MIN2((address)metaspace_base, cds_base);
iklam@46746 972 address higher_address = MAX2((address)(cds_base + MetaspaceShared::core_spaces_size()),
coleenp@21188 973 (address)(metaspace_base + compressed_class_space_size()));
coleenp@21518 974 return ((uint64_t)(higher_address - lower_base) <= UnscaledClassSpaceMax);
hseigel@19319 975 }
iklam@26135 976 #endif
hseigel@19319 977
hseigel@19319 978 // Try to allocate the metaspace at the requested addr.
hseigel@19319 979 void Metaspace::allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base) {
iklam@46746 980 assert(!DumpSharedSpaces, "compress klass space is allocated by MetaspaceShared class.");
hseigel@19319 981 assert(using_class_space(), "called improperly");
ehelin@19979 982 assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
coleenp@21188 983 assert(compressed_class_space_size() < KlassEncodingMetaspaceMax,
hseigel@19319 984 "Metaspace size is too big");
stefank@46619 985 assert_is_aligned(requested_addr, _reserve_alignment);
stefank@46619 986 assert_is_aligned(cds_base, _reserve_alignment);
stefank@46619 987 assert_is_aligned(compressed_class_space_size(), _reserve_alignment);
stefank@20406 988
stefank@20406 989 // Don't use large pages for the class space.
stefank@20406 990 bool large_pages = false;
hseigel@19319 991
goetz@39229 992 #if !(defined(AARCH64) || defined(AIX))
coleenp@21188 993 ReservedSpace metaspace_rs = ReservedSpace(compressed_class_space_size(),
stefank@20406 994 _reserve_alignment,
stefank@20406 995 large_pages,
goetz@28372 996 requested_addr);
aph@29180 997 #else // AARCH64
aph@29180 998 ReservedSpace metaspace_rs;
aph@29180 999
aph@29180 1000 // Our compressed klass pointers may fit nicely into the lower 32
aph@29180 1001 // bits.
aph@29180 1002 if ((uint64_t)requested_addr + compressed_class_space_size() < 4*G) {
aph@29180 1003 metaspace_rs = ReservedSpace(compressed_class_space_size(),
goetz@39229 1004 _reserve_alignment,
goetz@39229 1005 large_pages,
goetz@39229 1006 requested_addr);
aph@29180 1007 }
aph@29180 1008
aph@29180 1009 if (! metaspace_rs.is_reserved()) {
goetz@39229 1010 // Aarch64: Try to align metaspace so that we can decode a compressed
goetz@39229 1011 // klass with a single MOVK instruction. We can do this iff the
aph@29180 1012 // compressed class base is a multiple of 4G.
goetz@39229 1013 // Aix: Search for a place where we can find memory. If we need to load
goetz@39229 1014 // the base, 4G alignment is helpful, too.
goetz@39229 1015 size_t increment = AARCH64_ONLY(4*)G;
stefank@46619 1016 for (char *a = align_up(requested_addr, increment);
aph@29180 1017 a < (char*)(1024*G);
goetz@39229 1018 a += increment) {
goetz@39229 1019 if (a == (char *)(32*G)) {
goetz@39229 1020 // Go faster from here on. Zero-based is no longer possible.
goetz@39229 1021 increment = 4*G;
goetz@39229 1022 }
aph@29180 1023
aph@29180 1024 #if INCLUDE_CDS
aph@29180 1025 if (UseSharedSpaces
aph@29180 1026 && ! can_use_cds_with_metaspace_addr(a, cds_base)) {
aph@29180 1027 // We failed to find an aligned base that will reach. Fall
aph@29180 1028 // back to using our requested addr.
aph@29180 1029 metaspace_rs = ReservedSpace(compressed_class_space_size(),
aph@29180 1030 _reserve_alignment,
aph@29180 1031 large_pages,
enevill@29187 1032 requested_addr);
aph@29180 1033 break;
aph@29180 1034 }
aph@29180 1035 #endif
aph@29180 1036
aph@29180 1037 metaspace_rs = ReservedSpace(compressed_class_space_size(),
aph@29180 1038 _reserve_alignment,
aph@29180 1039 large_pages,
enevill@29187 1040 a);
aph@29180 1041 if (metaspace_rs.is_reserved())
aph@29180 1042 break;
aph@29180 1043 }
aph@29180 1044 }
aph@29180 1045
aph@29180 1046 #endif // AARCH64
aph@29180 1047
hseigel@19319 1048 if (!metaspace_rs.is_reserved()) {
iklam@26135 1049 #if INCLUDE_CDS
hseigel@19319 1050 if (UseSharedSpaces) {
stefank@46619 1051 size_t increment = align_up(1*G, _reserve_alignment);
stefank@20406 1052
hseigel@19319 1053 // Keep trying to allocate the metaspace, increasing the requested_addr
hseigel@19319 1054 // by 1GB each time, until we reach an address that will no longer allow
hseigel@19319 1055 // use of CDS with compressed klass pointers.
hseigel@19319 1056 char *addr = requested_addr;
stefank@20406 1057 while (!metaspace_rs.is_reserved() && (addr + increment > addr) &&
stefank@20406 1058 can_use_cds_with_metaspace_addr(addr + increment, cds_base)) {
stefank@20406 1059 addr = addr + increment;
coleenp@21188 1060 metaspace_rs = ReservedSpace(compressed_class_space_size(),
goetz@28372 1061 _reserve_alignment, large_pages, addr);
hseigel@19319 1062 }
hseigel@19319 1063 }
iklam@26135 1064 #endif
hseigel@19319 1065 // If no successful allocation then try to allocate the space anywhere. If
hseigel@19319 1066 // that fails then OOM doom. At this point we cannot try allocating the
ehelin@19979 1067 // metaspace as if UseCompressedClassPointers is off because too much
ehelin@19979 1068 // initialization has happened that depends on UseCompressedClassPointers.
ehelin@19979 1069 // So, UseCompressedClassPointers cannot be turned off at this point.
hseigel@19319 1070 if (!metaspace_rs.is_reserved()) {
coleenp@21188 1071 metaspace_rs = ReservedSpace(compressed_class_space_size(),
stefank@20406 1072 _reserve_alignment, large_pages);
hseigel@19319 1073 if (!metaspace_rs.is_reserved()) {
david@29800 1074 vm_exit_during_initialization(err_msg("Could not allocate metaspace: " SIZE_FORMAT " bytes",
coleenp@21188 1075 compressed_class_space_size()));
hseigel@19319 1076 }
hseigel@19319 1077 }
hseigel@19319 1078 }
hseigel@19319 1079
hseigel@19319 1080 // If we got here then the metaspace got allocated.
hseigel@19319 1081 MemTracker::record_virtual_memory_type((address)metaspace_rs.base(), mtClass);
hseigel@19319 1082
iklam@26135 1083 #if INCLUDE_CDS
hseigel@19319 1084 // Verify that we can use shared spaces. Otherwise, turn off CDS.
hseigel@19319 1085 if (UseSharedSpaces && !can_use_cds_with_metaspace_addr(metaspace_rs.base(), cds_base)) {
hseigel@19319 1086 FileMapInfo::stop_sharing_and_unmap(
hseigel@19319 1087 "Could not allocate metaspace at a compatible address");
hseigel@19319 1088 }
iklam@26135 1089 #endif
hseigel@19319 1090 set_narrow_klass_base_and_shift((address)metaspace_rs.base(),
hseigel@19319 1091 UseSharedSpaces ? (address)cds_base : 0);
hseigel@19319 1092
hseigel@19319 1093 initialize_class_space(metaspace_rs);
hseigel@19319 1094
stuefe@46701 1095 LogTarget(Trace, gc, metaspace) lt;
stuefe@46701 1096 if (lt.is_enabled()) {
brutisso@35061 1097 ResourceMark rm;
stuefe@46701 1098 LogStream ls(lt);
stuefe@46701 1099 print_compressed_class_space(&ls, requested_addr);
coleenp@31335 1100 }
coleenp@31335 1101 }
coleenp@31335 1102
coleenp@31335 1103 void Metaspace::print_compressed_class_space(outputStream* st, const char* requested_addr) {
coleenp@31335 1104 st->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: %d",
coleenp@31335 1105 p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift());
coleenp@31335 1106 if (_class_space_list != NULL) {
coleenp@31335 1107 address base = (address)_class_space_list->current_virtual_space()->bottom();
coleenp@31335 1108 st->print("Compressed class space size: " SIZE_FORMAT " Address: " PTR_FORMAT,
coleenp@31335 1109 compressed_class_space_size(), p2i(base));
coleenp@31335 1110 if (requested_addr != 0) {
coleenp@31335 1111 st->print(" Req Addr: " PTR_FORMAT, p2i(requested_addr));
coleenp@31335 1112 }
coleenp@31335 1113 st->cr();
hseigel@19319 1114 }
hseigel@19319 1115 }
hseigel@19319 1116
ehelin@19979 1117 // For UseCompressedClassPointers the class space is reserved above the top of
hseigel@19319 1118 // the Java heap. The argument passed in is at the base of the compressed space.
hseigel@19319 1119 void Metaspace::initialize_class_space(ReservedSpace rs) {
hseigel@19319 1120 // The reserved space size may be bigger because of alignment, esp with UseLargePages
ehelin@19979 1121 assert(rs.size() >= CompressedClassSpaceSize,
david@33105 1122 SIZE_FORMAT " != " SIZE_FORMAT, rs.size(), CompressedClassSpaceSize);
hseigel@19319 1123 assert(using_class_space(), "Must be using class space");
hseigel@19319 1124 _class_space_list = new VirtualSpaceList(rs);
stuefe@49830 1125 _chunk_manager_class = new ChunkManager(true/*is_class*/);
stefank@20406 1126
stefank@20406 1127 if (!_class_space_list->initialization_succeeded()) {
stefank@20406 1128 vm_exit_during_initialization("Failed to setup compressed class space virtual space list.");
stefank@20406 1129 }
hseigel@19319 1130 }
hseigel@19319 1131
hseigel@19319 1132 #endif
hseigel@19319 1133
stefank@20406 1134 void Metaspace::ergo_initialize() {
stefank@20406 1135 if (DumpSharedSpaces) {
stefank@20406 1136 // Using large pages when dumping the shared archive is currently not implemented.
stefank@20406 1137 FLAG_SET_ERGO(bool, UseLargePagesInMetaspace, false);
stefank@20406 1138 }
stefank@20406 1139
stefank@20406 1140 size_t page_size = os::vm_page_size();
stefank@20406 1141 if (UseLargePages && UseLargePagesInMetaspace) {
stefank@20406 1142 page_size = os::large_page_size();
stefank@20406 1143 }
stefank@20406 1144
stefank@20406 1145 _commit_alignment = page_size;
stefank@20406 1146 _reserve_alignment = MAX2(page_size, (size_t)os::vm_allocation_granularity());
stefank@20406 1147
stefank@20406 1148 // Do not use FLAG_SET_ERGO to update MaxMetaspaceSize, since this will
stefank@20406 1149 // override if MaxMetaspaceSize was set on the command line or not.
stefank@20406 1150 // This information is needed later to conform to the specification of the
stefank@20406 1151 // java.lang.management.MemoryUsage API.
stefank@20406 1152 //
stefank@20406 1153 // Ideally, we would be able to set the default value of MaxMetaspaceSize in
stefank@20406 1154 // globals.hpp to the aligned value, but this is not possible, since the
stefank@20406 1155 // alignment depends on other flags being parsed.
stefank@46619 1156 MaxMetaspaceSize = align_down_bounded(MaxMetaspaceSize, _reserve_alignment);
stefank@20406 1157
stefank@20406 1158 if (MetaspaceSize > MaxMetaspaceSize) {
stefank@20406 1159 MetaspaceSize = MaxMetaspaceSize;
stefank@20406 1160 }
stefank@20406 1161
stefank@46619 1162 MetaspaceSize = align_down_bounded(MetaspaceSize, _commit_alignment);
stefank@20406 1163
stefank@20406 1164 assert(MetaspaceSize <= MaxMetaspaceSize, "MetaspaceSize should be limited by MaxMetaspaceSize");
stefank@20406 1165
stefank@46619 1166 MinMetaspaceExpansion = align_down_bounded(MinMetaspaceExpansion, _commit_alignment);
stefank@46619 1167 MaxMetaspaceExpansion = align_down_bounded(MaxMetaspaceExpansion, _commit_alignment);
stefank@46619 1168
stefank@46619 1169 CompressedClassSpaceSize = align_down_bounded(CompressedClassSpaceSize, _reserve_alignment);
ysuenaga@47809 1170
ysuenaga@47809 1171 // Initial virtual space size will be calculated at global_initialize()
ysuenaga@47809 1172 size_t min_metaspace_sz =
ysuenaga@47809 1173 VIRTUALSPACEMULTIPLIER * InitialBootClassLoaderMetaspaceSize;
ysuenaga@47809 1174 if (UseCompressedClassPointers) {
ysuenaga@47809 1175 if ((min_metaspace_sz + CompressedClassSpaceSize) > MaxMetaspaceSize) {
ysuenaga@47809 1176 if (min_metaspace_sz >= MaxMetaspaceSize) {
ysuenaga@47809 1177 vm_exit_during_initialization("MaxMetaspaceSize is too small.");
ysuenaga@47809 1178 } else {
ysuenaga@47809 1179 FLAG_SET_ERGO(size_t, CompressedClassSpaceSize,
ysuenaga@47809 1180 MaxMetaspaceSize - min_metaspace_sz);
ysuenaga@47809 1181 }
ysuenaga@47809 1182 }
ysuenaga@47809 1183 } else if (min_metaspace_sz >= MaxMetaspaceSize) {
ysuenaga@47809 1184 FLAG_SET_ERGO(size_t, InitialBootClassLoaderMetaspaceSize,
ysuenaga@47809 1185 min_metaspace_sz);
ysuenaga@47809 1186 }
ysuenaga@47809 1187
coleenp@48658 1188 set_compressed_class_space_size(CompressedClassSpaceSize);
stefank@20406 1189 }
stefank@20406 1190
coleenp@13728 1191 void Metaspace::global_initialize() {
ehelin@24847 1192 MetaspaceGC::initialize();
ehelin@24847 1193
iklam@46746 1194 #if INCLUDE_CDS
coleenp@13728 1195 if (DumpSharedSpaces) {
jiangli@47754 1196 MetaspaceShared::initialize_dumptime_shared_and_meta_spaces();
iklam@46746 1197 } else if (UseSharedSpaces) {
jiangli@47754 1198 // If any of the archived space fails to map, UseSharedSpaces
jiangli@47754 1199 // is reset to false. Fall through to the
jiangli@47754 1200 // (!DumpSharedSpaces && !UseSharedSpaces) case to set up class
jiangli@47754 1201 // metaspace.
jiangli@47754 1202 MetaspaceShared::initialize_runtime_shared_and_meta_spaces();
jiangli@47754 1203 }
jiangli@47754 1204
jiangli@47754 1205 if (!DumpSharedSpaces && !UseSharedSpaces)
jiangli@47754 1206 #endif // INCLUDE_CDS
jiangli@47754 1207 {
iklam@46746 1208 #ifdef _LP64
jiangli@47754 1209 if (using_class_space()) {
stefank@46619 1210 char* base = (char*)align_up(Universe::heap()->reserved_region().end(), _reserve_alignment);
coleenp@31362 1211 allocate_metaspace_compressed_klass_ptrs(base, 0);
hseigel@19319 1212 }
jiangli@47754 1213 #endif // _LP64
iklam@46746 1214 }
hseigel@19319 1215
iklam@46746 1216 // Initialize these before initializing the VirtualSpaceList
iklam@46746 1217 _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord;
iklam@46746 1218 _first_chunk_word_size = align_word_size_up(_first_chunk_word_size);
iklam@46746 1219 // Make the first class chunk bigger than a medium chunk so it's not put
iklam@46746 1220 // on the medium chunk list. The next chunk will be small and progress
iklam@46746 1221 // from there. This size calculated by -version.
iklam@46746 1222 _first_class_chunk_word_size = MIN2((size_t)MediumChunk*6,
iklam@46746 1223 (CompressedClassSpaceSize/BytesPerWord)*2);
iklam@46746 1224 _first_class_chunk_word_size = align_word_size_up(_first_class_chunk_word_size);
iklam@46746 1225 // Arbitrarily set the initial virtual space to a multiple
iklam@46746 1226 // of the boot class loader size.
iklam@46746 1227 size_t word_size = VIRTUALSPACEMULTIPLIER * _first_chunk_word_size;
iklam@46746 1228 word_size = align_up(word_size, Metaspace::reserve_alignment_words());
iklam@46746 1229
iklam@46746 1230 // Initialize the list of virtual spaces.
iklam@46746 1231 _space_list = new VirtualSpaceList(word_size);
stuefe@49830 1232 _chunk_manager_metadata = new ChunkManager(false/*metaspace*/);
iklam@46746 1233
iklam@46746 1234 if (!_space_list->initialization_succeeded()) {
iklam@46746 1235 vm_exit_during_initialization("Unable to setup metadata virtual space list.", NULL);
coleenp@13728 1236 }
stefank@20406 1237
ehelin@23465 1238 _tracer = new MetaspaceTracer();
coleenp@13728 1239 }
coleenp@13728 1240
ehelin@24847 1241 void Metaspace::post_initialize() {
ehelin@24847 1242 MetaspaceGC::post_initialize();
ehelin@24847 1243 }
ehelin@24847 1244
stefank@42615 1245 void Metaspace::verify_global_initialization() {
stefank@42615 1246 assert(space_list() != NULL, "Metadata VirtualSpaceList has not been initialized");
stefank@42615 1247 assert(chunk_manager_metadata() != NULL, "Metadata ChunkManager has not been initialized");
stefank@42615 1248
stefank@42615 1249 if (using_class_space()) {
stefank@42615 1250 assert(class_space_list() != NULL, "Class VirtualSpaceList has not been initialized");
stefank@42615 1251 assert(chunk_manager_class() != NULL, "Class ChunkManager has not been initialized");
stefank@42615 1252 }
stefank@42615 1253 }
stefank@42615 1254
jmasa@15086 1255 size_t Metaspace::align_word_size_up(size_t word_size) {
jmasa@15086 1256 size_t byte_size = word_size * wordSize;
jmasa@15086 1257 return ReservedSpace::allocation_align_size_up(byte_size) / wordSize;
jmasa@15086 1258 }
jmasa@15086 1259
stefank@20729 1260 MetaWord* Metaspace::allocate(ClassLoaderData* loader_data, size_t word_size,
iklam@46746 1261 MetaspaceObj::Type type, TRAPS) {
iklam@46746 1262 assert(!_frozen, "sanity");
iklam@53416 1263 assert(!(DumpSharedSpaces && THREAD->is_VM_thread()), "sanity");
iklam@53416 1264
coleenp@13728 1265 if (HAS_PENDING_EXCEPTION) {
coleenp@13728 1266 assert(false, "Should not allocate with exception pending");
coleenp@13728 1267 return NULL; // caller does a CHECK_NULL too
coleenp@13728 1268 }
coleenp@13728 1269
coleenp@13728 1270 assert(loader_data != NULL, "Should never pass around a NULL loader_data. "
coleenp@13728 1271 "ClassLoaderData::the_null_class_loader_data() should have been used.");
stefank@20406 1272
stefank@20406 1273 MetadataType mdtype = (type == MetaspaceObj::ClassType) ? ClassType : NonClassType;
stefank@20406 1274
stefank@20406 1275 // Try to allocate metadata.
stefank@20406 1276 MetaWord* result = loader_data->metaspace_non_null()->allocate(word_size, mdtype);
coleenp@13728 1277
coleenp@13728 1278 if (result == NULL) {
ehelin@23468 1279 tracer()->report_metaspace_allocation_failure(loader_data, word_size, type, mdtype);
ehelin@23468 1280
stefank@20406 1281 // Allocation failed.
iklam@53416 1282 if (is_init_completed()) {
stefank@20406 1283 // Only start a GC if the bootstrapping has completed.
iklam@53416 1284 // Try to clean out some heap memory and retry. This can prevent premature
iklam@53416 1285 // expansion of the metaspace.
stefank@49476 1286 result = Universe::heap()->satisfy_failed_metadata_allocation(loader_data, word_size, mdtype);
coleenp@13728 1287 }
coleenp@13728 1288 }
stefank@20406 1289
stefank@20406 1290 if (result == NULL) {
ccheung@50147 1291 if (DumpSharedSpaces) {
ccheung@50147 1292 // CDS dumping keeps loading classes, so if we hit an OOM we probably will keep hitting OOM.
ccheung@50147 1293 // We should abort to avoid generating a potentially bad archive.
ccheung@53229 1294 vm_exit_during_cds_dumping(err_msg("Failed allocating metaspace object type %s of size " SIZE_FORMAT ". CDS dump aborted.",
ccheung@53229 1295 MetaspaceObj::type_name(type), word_size * BytesPerWord),
ccheung@53229 1296 err_msg("Please increase MaxMetaspaceSize (currently " SIZE_FORMAT " bytes).", MaxMetaspaceSize));
ccheung@50147 1297 }
goetz@51744 1298 report_metadata_oome(loader_data, word_size, type, mdtype, THREAD);
goetz@51744 1299 assert(HAS_PENDING_EXCEPTION, "sanity");
goetz@51744 1300 return NULL;
stefank@20406 1301 }
stefank@20406 1302
stefank@20729 1303 // Zero initialize.
coleenp@35898 1304 Copy::fill_to_words((HeapWord*)result, word_size, 0);
stefank@20729 1305
stefank@20729 1306 return result;
coleenp@13728 1307 }
coleenp@13728 1308
ehelin@23469 1309 void Metaspace::report_metadata_oome(ClassLoaderData* loader_data, size_t word_size, MetaspaceObj::Type type, MetadataType mdtype, TRAPS) {
ehelin@23469 1310 tracer()->report_metadata_oom(loader_data, word_size, type, mdtype);
ehelin@23469 1311
stefank@20406 1312 // If result is still null, we are out of memory.
stuefe@50976 1313 Log(gc, metaspace, freelist, oom) log;
coleenp@41727 1314 if (log.is_info()) {
coleenp@41727 1315 log.info("Metaspace (%s) allocation failed for size " SIZE_FORMAT,
coleenp@41727 1316 is_class_space_allocation(mdtype) ? "class" : "data", word_size);
brutisso@35061 1317 ResourceMark rm;
stuefe@46723 1318 if (log.is_debug()) {
stuefe@46723 1319 if (loader_data->metaspace_or_null() != NULL) {
stuefe@46723 1320 LogStream ls(log.debug());
coleenp@49813 1321 loader_data->print_value_on(&ls);
stuefe@46723 1322 }
stuefe@46723 1323 }
stuefe@46701 1324 LogStream ls(log.info());
stuefe@50516 1325 // In case of an OOM, log out a short but still useful report.
stuefe@50516 1326 MetaspaceUtils::print_basic_report(&ls, 0);
stefank@20406 1327 }
stefank@20406 1328
hseigel@21186 1329 bool out_of_compressed_class_space = false;
hseigel@21186 1330 if (is_class_space_allocation(mdtype)) {
stuefe@49866 1331 ClassLoaderMetaspace* metaspace = loader_data->metaspace_non_null();
hseigel@21186 1332 out_of_compressed_class_space =
stuefe@49854 1333 MetaspaceUtils::committed_bytes(Metaspace::ClassType) +
hseigel@21186 1334 (metaspace->class_chunk_size(word_size) * BytesPerWord) >
hseigel@21186 1335 CompressedClassSpaceSize;
hseigel@21186 1336 }
hseigel@21186 1337
stefank@20406 1338 // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
hseigel@21186 1339 const char* space_string = out_of_compressed_class_space ?
hseigel@21186 1340 "Compressed class space" : "Metaspace";
hseigel@21186 1341
stefank@20406 1342 report_java_out_of_memory(space_string);
stefank@20406 1343
stefank@20406 1344 if (JvmtiExport::should_post_resource_exhausted()) {
stefank@20406 1345 JvmtiExport::post_resource_exhausted(
stefank@20406 1346 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,
stefank@20406 1347 space_string);
stefank@20406 1348 }
stefank@20406 1349
stefank@20406 1350 if (!is_init_completed()) {
stefank@20406 1351 vm_exit_during_initialization("OutOfMemoryError", space_string);
stefank@20406 1352 }
stefank@20406 1353
hseigel@21186 1354 if (out_of_compressed_class_space) {
stefank@20406 1355 THROW_OOP(Universe::out_of_memory_error_class_metaspace());
stefank@20406 1356 } else {
stefank@20406 1357 THROW_OOP(Universe::out_of_memory_error_metaspace());
stefank@20406 1358 }
stefank@20406 1359 }
stefank@20406 1360
ehelin@23468 1361 const char* Metaspace::metadata_type_name(Metaspace::MetadataType mdtype) {
ehelin@23468 1362 switch (mdtype) {
ehelin@23468 1363 case Metaspace::ClassType: return "Class";
ehelin@23468 1364 case Metaspace::NonClassType: return "Metadata";
ehelin@23468 1365 default:
david@33105 1366 assert(false, "Got bad mdtype: %d", (int) mdtype);
ehelin@23468 1367 return NULL;
ehelin@23468 1368 }
ehelin@23468 1369 }
ehelin@23468 1370
stefank@20081 1371 void Metaspace::purge(MetadataType mdtype) {
stefank@20081 1372 get_space_list(mdtype)->purge(get_chunk_manager(mdtype));
stefank@20081 1373 }
stefank@20081 1374
jmasa@17101 1375 void Metaspace::purge() {
coleenp@50096 1376 MutexLockerEx cl(MetaspaceExpand_lock,
jmasa@17101 1377 Mutex::_no_safepoint_check_flag);
stefank@20081 1378 purge(NonClassType);
hseigel@19319 1379 if (using_class_space()) {
stefank@20081 1380 purge(ClassType);
hseigel@19319 1381 }
jmasa@17101 1382 }
jmasa@17101 1383
stuefe@49866 1384 bool Metaspace::contains(const void* ptr) {
stuefe@49866 1385 if (MetaspaceShared::is_in_shared_metaspace(ptr)) {
stuefe@49866 1386 return true;
stuefe@49866 1387 }
stuefe@49866 1388 return contains_non_shared(ptr);
stuefe@49866 1389 }
stuefe@49866 1390
stuefe@49866 1391 bool Metaspace::contains_non_shared(const void* ptr) {
stuefe@49866 1392 if (using_class_space() && get_space_list(ClassType)->contains(ptr)) {
stuefe@49866 1393 return true;
stuefe@49866 1394 }
stuefe@49866 1395
stuefe@49866 1396 return get_space_list(NonClassType)->contains(ptr);
stuefe@49866 1397 }
stuefe@49866 1398
stuefe@49866 1399 // ClassLoaderMetaspace
stuefe@49866 1400
stuefe@50516 1401 ClassLoaderMetaspace::ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType type)
tschatzl@52035 1402 : _space_type(type)
tschatzl@52035 1403 , _lock(lock)
stuefe@50516 1404 , _vsm(NULL)
stuefe@50516 1405 , _class_vsm(NULL)
stuefe@50516 1406 {
stuefe@49866 1407 initialize(lock, type);
stuefe@49866 1408 }
stuefe@49866 1409
stuefe@49866 1410 ClassLoaderMetaspace::~ClassLoaderMetaspace() {
iklam@53416 1411 Metaspace::assert_not_frozen();
stuefe@50516 1412 DEBUG_ONLY(Atomic::inc(&g_internal_statistics.num_metaspace_deaths));
stuefe@49866 1413 delete _vsm;
stuefe@49866 1414 if (Metaspace::using_class_space()) {
stuefe@49866 1415 delete _class_vsm;
stuefe@49866 1416 }
stuefe@49866 1417 }
stuefe@50516 1418
stuefe@49866 1419 void ClassLoaderMetaspace::initialize_first_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype) {
stuefe@49866 1420 Metachunk* chunk = get_initialization_chunk(type, mdtype);
stuefe@49866 1421 if (chunk != NULL) {
stuefe@50380 1422 // Add to this manager's list of chunks in use and make it the current_chunk().
stuefe@49866 1423 get_space_manager(mdtype)->add_chunk(chunk, true);
stuefe@49866 1424 }
stuefe@49866 1425 }
stuefe@49866 1426
stuefe@49866 1427 Metachunk* ClassLoaderMetaspace::get_initialization_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype) {
stuefe@49866 1428 size_t chunk_word_size = get_space_manager(mdtype)->get_initial_chunk_size(type);
stuefe@49866 1429
stuefe@49866 1430 // Get a chunk from the chunk freelist
stuefe@49866 1431 Metachunk* chunk = Metaspace::get_chunk_manager(mdtype)->chunk_freelist_allocate(chunk_word_size);
stuefe@49866 1432
stuefe@49866 1433 if (chunk == NULL) {
stuefe@49866 1434 chunk = Metaspace::get_space_list(mdtype)->get_new_chunk(chunk_word_size,
stuefe@49866 1435 get_space_manager(mdtype)->medium_chunk_bunch());
stuefe@49866 1436 }
stuefe@49866 1437
stuefe@49866 1438 return chunk;
stuefe@49866 1439 }
stuefe@49866 1440
stuefe@49866 1441 void ClassLoaderMetaspace::initialize(Mutex* lock, Metaspace::MetaspaceType type) {
stuefe@49866 1442 Metaspace::verify_global_initialization();
stuefe@49866 1443
stuefe@50516 1444 DEBUG_ONLY(Atomic::inc(&g_internal_statistics.num_metaspace_births));
stuefe@50516 1445
stuefe@49866 1446 // Allocate SpaceManager for metadata objects.
stuefe@49866 1447 _vsm = new SpaceManager(Metaspace::NonClassType, type, lock);
stuefe@49866 1448
stuefe@49866 1449 if (Metaspace::using_class_space()) {
stuefe@49866 1450 // Allocate SpaceManager for classes.
stuefe@49866 1451 _class_vsm = new SpaceManager(Metaspace::ClassType, type, lock);
stuefe@49866 1452 }
stuefe@49866 1453
coleenp@50096 1454 MutexLockerEx cl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
stuefe@49866 1455
stuefe@49866 1456 // Allocate chunk for metadata objects
stuefe@49866 1457 initialize_first_chunk(type, Metaspace::NonClassType);
stuefe@49866 1458
stuefe@49866 1459 // Allocate chunk for class metadata objects
stuefe@49866 1460 if (Metaspace::using_class_space()) {
stuefe@49866 1461 initialize_first_chunk(type, Metaspace::ClassType);
stuefe@49866 1462 }
stuefe@49866 1463 }
stuefe@49866 1464
stuefe@49866 1465 MetaWord* ClassLoaderMetaspace::allocate(size_t word_size, Metaspace::MetadataType mdtype) {
stuefe@49866 1466 Metaspace::assert_not_frozen();
stuefe@50516 1467
stuefe@50516 1468 DEBUG_ONLY(Atomic::inc(&g_internal_statistics.num_allocs));
stuefe@50516 1469
stuefe@49866 1470 // Don't use class_vsm() unless UseCompressedClassPointers is true.
stuefe@49866 1471 if (Metaspace::is_class_space_allocation(mdtype)) {
stuefe@49866 1472 return class_vsm()->allocate(word_size);
stuefe@49866 1473 } else {
stuefe@49866 1474 return vsm()->allocate(word_size);
stuefe@49866 1475 }
stuefe@49866 1476 }
stuefe@49866 1477
stuefe@49866 1478 MetaWord* ClassLoaderMetaspace::expand_and_allocate(size_t word_size, Metaspace::MetadataType mdtype) {
stuefe@49866 1479 Metaspace::assert_not_frozen();
stuefe@49866 1480 size_t delta_bytes = MetaspaceGC::delta_capacity_until_GC(word_size * BytesPerWord);
stuefe@49866 1481 assert(delta_bytes > 0, "Must be");
stuefe@49866 1482
stuefe@49866 1483 size_t before = 0;
stuefe@49866 1484 size_t after = 0;
stuefe@49866 1485 MetaWord* res;
stuefe@49866 1486 bool incremented;
stuefe@49866 1487
stuefe@49866 1488 // Each thread increments the HWM at most once. Even if the thread fails to increment
stuefe@49866 1489 // the HWM, an allocation is still attempted. This is because another thread must then
stuefe@49866 1490 // have incremented the HWM and therefore the allocation might still succeed.
stuefe@49866 1491 do {
stuefe@49866 1492 incremented = MetaspaceGC::inc_capacity_until_GC(delta_bytes, &after, &before);
stuefe@49866 1493 res = allocate(word_size, mdtype);
stuefe@49866 1494 } while (!incremented && res == NULL);
stuefe@49866 1495
stuefe@49866 1496 if (incremented) {
stuefe@49866 1497 Metaspace::tracer()->report_gc_threshold(before, after,
stuefe@49866 1498 MetaspaceGCThresholdUpdater::ExpandAndAllocate);
stuefe@49866 1499 log_trace(gc, metaspace)("Increase capacity to GC from " SIZE_FORMAT " to " SIZE_FORMAT, before, after);
stuefe@49866 1500 }
stuefe@49866 1501
stuefe@49866 1502 return res;
stuefe@49866 1503 }
stuefe@49866 1504
stuefe@50516 1505 size_t ClassLoaderMetaspace::allocated_blocks_bytes() const {
stuefe@50516 1506 return (vsm()->used_words() +
stuefe@50516 1507 (Metaspace::using_class_space() ? class_vsm()->used_words() : 0)) * BytesPerWord;
stuefe@49866 1508 }
stuefe@49866 1509
stuefe@49866 1510 size_t ClassLoaderMetaspace::allocated_chunks_bytes() const {
stuefe@50516 1511 return (vsm()->capacity_words() +
stuefe@50516 1512 (Metaspace::using_class_space() ? class_vsm()->capacity_words() : 0)) * BytesPerWord;
stuefe@49866 1513 }
stuefe@49866 1514
stuefe@49866 1515 void ClassLoaderMetaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) {
stuefe@49866 1516 Metaspace::assert_not_frozen();
stuefe@49866 1517 assert(!SafepointSynchronize::is_at_safepoint()
stuefe@49866 1518 || Thread::current()->is_VM_thread(), "should be the VM thread");
stuefe@49866 1519
stuefe@50516 1520 DEBUG_ONLY(Atomic::inc(&g_internal_statistics.num_external_deallocs));
stuefe@50516 1521
stuefe@49866 1522 MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag);
stuefe@49866 1523
stuefe@49866 1524 if (is_class && Metaspace::using_class_space()) {
stuefe@49866 1525 class_vsm()->deallocate(ptr, word_size);
stuefe@49866 1526 } else {
stuefe@49866 1527 vsm()->deallocate(ptr, word_size);
stuefe@49866 1528 }
stuefe@49866 1529 }
stuefe@49866 1530
stuefe@49866 1531 size_t ClassLoaderMetaspace::class_chunk_size(size_t word_size) {
stuefe@49866 1532 assert(Metaspace::using_class_space(), "Has to use class space");
stuefe@49866 1533 return class_vsm()->calc_chunk_size(word_size);
stuefe@49866 1534 }
stuefe@49866 1535
stuefe@49866 1536 void ClassLoaderMetaspace::print_on(outputStream* out) const {
coleenp@13728 1537 // Print both class virtual space counts and metaspace.
coleenp@13728 1538 if (Verbose) {
hseigel@19319 1539 vsm()->print_on(out);
stuefe@49866 1540 if (Metaspace::using_class_space()) {
coleenp@13728 1541 class_vsm()->print_on(out);
hseigel@19319 1542 }
coleenp@13728 1543 }
coleenp@13728 1544 }
coleenp@13728 1545
stuefe@49866 1546 void ClassLoaderMetaspace::verify() {
coleenp@13728 1547 vsm()->verify();
stuefe@49866 1548 if (Metaspace::using_class_space()) {
hseigel@19319 1549 class_vsm()->verify();
hseigel@19319 1550 }
coleenp@13728 1551 }
coleenp@13728 1552
stuefe@50516 1553 void ClassLoaderMetaspace::add_to_statistics_locked(ClassLoaderMetaspaceStatistics* out) const {
stuefe@50516 1554 assert_lock_strong(lock());
stuefe@50516 1555 vsm()->add_to_statistics_locked(&out->nonclass_sm_stats());
stuefe@49866 1556 if (Metaspace::using_class_space()) {
stuefe@50516 1557 class_vsm()->add_to_statistics_locked(&out->class_sm_stats());
hseigel@19319 1558 }
coleenp@13728 1559 }
stefank@19989 1560
stuefe@50516 1561 void ClassLoaderMetaspace::add_to_statistics(ClassLoaderMetaspaceStatistics* out) const {
stuefe@50516 1562 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
stuefe@50516 1563 add_to_statistics_locked(out);
stuefe@50516 1564 }
stuefe@49866 1565
stefank@19989 1566 /////////////// Unit tests ///////////////
stefank@19989 1567
stefank@19989 1568 #ifndef PRODUCT
stefank@19989 1569
stuefe@49854 1570 class TestMetaspaceUtilsTest : AllStatic {
stefank@19989 1571 public:
stefank@19989 1572 static void test_reserved() {
stuefe@49854 1573 size_t reserved = MetaspaceUtils::reserved_bytes();
stefank@19989 1574
stefank@19989 1575 assert(reserved > 0, "assert");
stefank@19989 1576
stuefe@49854 1577 size_t committed = MetaspaceUtils::committed_bytes();
stefank@19989 1578 assert(committed <= reserved, "assert");
stefank@19989 1579
stuefe@49854 1580 size_t reserved_metadata = MetaspaceUtils::reserved_bytes(Metaspace::NonClassType);
stefank@19989 1581 assert(reserved_metadata > 0, "assert");
stefank@19989 1582 assert(reserved_metadata <= reserved, "assert");
stefank@19989 1583
stefank@19989 1584 if (UseCompressedClassPointers) {
stuefe@49854 1585 size_t reserved_class = MetaspaceUtils::reserved_bytes(Metaspace::ClassType);
stefank@19989 1586 assert(reserved_class > 0, "assert");
stefank@19989 1587 assert(reserved_class < reserved, "assert");
stefank@19989 1588 }
stefank@19989 1589 }
stefank@19989 1590
stefank@19989 1591 static void test_committed() {
stuefe@49854 1592 size_t committed = MetaspaceUtils::committed_bytes();
stefank@19989 1593
stefank@19989 1594 assert(committed > 0, "assert");
stefank@19989 1595
stuefe@49854 1596 size_t reserved = MetaspaceUtils::reserved_bytes();
stefank@19989 1597 assert(committed <= reserved, "assert");
stefank@19989 1598
stuefe@49854 1599 size_t committed_metadata = MetaspaceUtils::committed_bytes(Metaspace::NonClassType);
stefank@19989 1600 assert(committed_metadata > 0, "assert");
stefank@19989 1601 assert(committed_metadata <= committed, "assert");
stefank@19989 1602
stefank@19989 1603 if (UseCompressedClassPointers) {
stuefe@49854 1604 size_t committed_class = MetaspaceUtils::committed_bytes(Metaspace::ClassType);
stefank@19989 1605 assert(committed_class > 0, "assert");
stefank@19989 1606 assert(committed_class < committed, "assert");
stefank@19989 1607 }
stefank@19989 1608 }
stefank@19989 1609
brutisso@20084 1610 static void test_virtual_space_list_large_chunk() {
brutisso@20084 1611 VirtualSpaceList* vs_list = new VirtualSpaceList(os::vm_allocation_granularity());
coleenp@50096 1612 MutexLockerEx cl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
brutisso@20084 1613 // A size larger than VirtualSpaceSize (256k) and add one page to make it _not_ be
brutisso@20084 1614 // vm_allocation_granularity aligned on Windows.
brutisso@20084 1615 size_t large_size = (size_t)(2*256*K + (os::vm_page_size()/BytesPerWord));
brutisso@20084 1616 large_size += (os::vm_page_size()/BytesPerWord);
stefank@42615 1617 vs_list->get_new_chunk(large_size, 0);
brutisso@20084 1618 }
brutisso@20084 1619
stefank@19989 1620 static void test() {
stefank@19989 1621 test_reserved();
stefank@19989 1622 test_committed();
brutisso@20084 1623 test_virtual_space_list_large_chunk();
stefank@19989 1624 }
stefank@19989 1625 };
stefank@19989 1626
stuefe@49854 1627 void TestMetaspaceUtils_test() {
stuefe@49854 1628 TestMetaspaceUtilsTest::test();
stefank@19989 1629 }
stefank@19989 1630
stuefe@46602 1631 #endif // !PRODUCT
stuefe@46602 1632
stuefe@49830 1633 struct chunkmanager_statistics_t {
stuefe@49830 1634 int num_specialized_chunks;
stuefe@49830 1635 int num_small_chunks;
stuefe@49830 1636 int num_medium_chunks;
stuefe@49830 1637 int num_humongous_chunks;
stuefe@49830 1638 };
stuefe@49830 1639
stuefe@49830 1640 extern void test_metaspace_retrieve_chunkmanager_statistics(Metaspace::MetadataType mdType, chunkmanager_statistics_t* out) {
stuefe@49830 1641 ChunkManager* const chunk_manager = Metaspace::get_chunk_manager(mdType);
stuefe@50516 1642 ChunkManagerStatistics stat;
stuefe@50516 1643 chunk_manager->collect_statistics(&stat);
stuefe@50516 1644 out->num_specialized_chunks = (int)stat.chunk_stats(SpecializedIndex).num();
stuefe@50516 1645 out->num_small_chunks = (int)stat.chunk_stats(SmallIndex).num();
stuefe@50516 1646 out->num_medium_chunks = (int)stat.chunk_stats(MediumIndex).num();
stuefe@50516 1647 out->num_humongous_chunks = (int)stat.chunk_stats(HumongousIndex).num();
stuefe@49830 1648 }
stuefe@49830 1649
stuefe@49830 1650 struct chunk_geometry_t {
stuefe@49830 1651 size_t specialized_chunk_word_size;
stuefe@49830 1652 size_t small_chunk_word_size;
stuefe@49830 1653 size_t medium_chunk_word_size;
stuefe@49830 1654 };
stuefe@49830 1655
stuefe@49830 1656 extern void test_metaspace_retrieve_chunk_geometry(Metaspace::MetadataType mdType, chunk_geometry_t* out) {
stuefe@49830 1657 if (mdType == Metaspace::NonClassType) {
stuefe@49830 1658 out->specialized_chunk_word_size = SpecializedChunk;
stuefe@49830 1659 out->small_chunk_word_size = SmallChunk;
stuefe@49830 1660 out->medium_chunk_word_size = MediumChunk;
stuefe@49830 1661 } else {
stuefe@49830 1662 out->specialized_chunk_word_size = ClassSpecializedChunk;
stuefe@49830 1663 out->small_chunk_word_size = ClassSmallChunk;
stuefe@49830 1664 out->medium_chunk_word_size = ClassMediumChunk;
stuefe@49830 1665 }
stuefe@49830 1666 }