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