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 #include "precompiled.hpp"
  25 #include "aot/aotLoader.hpp"
  26 #include "classfile/classLoaderData.hpp"
  27 #include "gc/shared/collectedHeap.hpp"
  28 #include "gc/shared/gcLocker.inline.hpp"
  29 #include "gc/shared/vmGCOperations.hpp"
  30 #include "logging/log.hpp"
  31 #include "logging/logStream.hpp"
  32 #include "memory/allocation.hpp"
  33 #include "memory/binaryTreeDictionary.hpp"
  34 #include "memory/filemap.hpp"
  35 #include "memory/freeList.hpp"
  36 #include "memory/metachunk.hpp"
  37 #include "memory/metaspace.hpp"
  38 #include "memory/metaspaceGCThresholdUpdater.hpp"
  39 #include "memory/metaspaceShared.hpp"
  40 #include "memory/metaspaceTracer.hpp"
  41 #include "memory/resourceArea.hpp"
  42 #include "memory/universe.hpp"
  43 #include "runtime/atomic.hpp"
  44 #include "runtime/globals.hpp"
  45 #include "runtime/init.hpp"
  46 #include "runtime/java.hpp"
  47 #include "runtime/mutex.hpp"
  48 #include "runtime/orderAccess.inline.hpp"
  49 #include "runtime/vmThread.hpp"
  50 #include "services/memTracker.hpp"
  51 #include "services/memoryService.hpp"
  52 #include "utilities/align.hpp"
  53 #include "utilities/copy.hpp"
  54 #include "utilities/debug.hpp"
  55 #include "utilities/macros.hpp"
  56 
  57 typedef BinaryTreeDictionary<Metablock, FreeList<Metablock> > BlockTreeDictionary;
  58 typedef BinaryTreeDictionary<Metachunk, FreeList<Metachunk> > ChunkTreeDictionary;
  59 
  60 // Set this constant to enable slow integrity checking of the free chunk lists
  61 const bool metaspace_slow_verify = false;
  62 
  63 size_t const allocation_from_dictionary_limit = 4 * K;
  64 
  65 MetaWord* last_allocated = 0;
  66 
  67 size_t Metaspace::_compressed_class_space_size;
  68 const MetaspaceTracer* Metaspace::_tracer = NULL;
  69 
  70 DEBUG_ONLY(bool Metaspace::_frozen = false;)
  71 
  72 // Used in declarations in SpaceManager and ChunkManager
  73 enum ChunkIndex {
  74   ZeroIndex = 0,
  75   SpecializedIndex = ZeroIndex,
  76   SmallIndex = SpecializedIndex + 1,
  77   MediumIndex = SmallIndex + 1,
  78   HumongousIndex = MediumIndex + 1,
  79   NumberOfFreeLists = 3,
  80   NumberOfInUseLists = 4
  81 };
  82 
  83 // Helper, returns a descriptive name for the given index.
  84 static const char* chunk_size_name(ChunkIndex index) {
  85   switch (index) {
  86     case SpecializedIndex:
  87       return "specialized";
  88     case SmallIndex:
  89       return "small";
  90     case MediumIndex:
  91       return "medium";
  92     case HumongousIndex:
  93       return "humongous";
  94     default:
  95       return "Invalid index";
  96   }
  97 }
  98 
  99 enum ChunkSizes {    // in words.
 100   ClassSpecializedChunk = 128,
 101   SpecializedChunk = 128,
 102   ClassSmallChunk = 256,
 103   SmallChunk = 512,
 104   ClassMediumChunk = 4 * K,
 105   MediumChunk = 8 * K
 106 };
 107 
 108 static ChunkIndex next_chunk_index(ChunkIndex i) {
 109   assert(i < NumberOfInUseLists, "Out of bound");
 110   return (ChunkIndex) (i+1);
 111 }
 112 
 113 static const char* scale_unit(size_t scale) {
 114   switch(scale) {
 115     case 1: return "BYTES";
 116     case K: return "KB";
 117     case M: return "MB";
 118     case G: return "GB";
 119     default:
 120       ShouldNotReachHere();
 121       return NULL;
 122   }
 123 }
 124 
 125 volatile intptr_t MetaspaceGC::_capacity_until_GC = 0;
 126 uint MetaspaceGC::_shrink_factor = 0;
 127 bool MetaspaceGC::_should_concurrent_collect = false;
 128 
 129 typedef class FreeList<Metachunk> ChunkList;
 130 
 131 // Manages the global free lists of chunks.
 132 class ChunkManager : public CHeapObj<mtInternal> {
 133   friend class TestVirtualSpaceNodeTest;
 134 
 135   // Free list of chunks of different sizes.
 136   //   SpecializedChunk
 137   //   SmallChunk
 138   //   MediumChunk
 139   ChunkList _free_chunks[NumberOfFreeLists];
 140 
 141   // Return non-humongous chunk list by its index.
 142   ChunkList* free_chunks(ChunkIndex index);
 143 
 144   // Returns non-humongous chunk list for the given chunk word size.
 145   ChunkList* find_free_chunks_list(size_t word_size);
 146 
 147   //   HumongousChunk
 148   ChunkTreeDictionary _humongous_dictionary;
 149 
 150   // Returns the humongous chunk dictionary.
 151   ChunkTreeDictionary* humongous_dictionary() {
 152     return &_humongous_dictionary;
 153   }
 154 
 155   // Size, in metaspace words, of all chunks managed by this ChunkManager
 156   size_t _free_chunks_total;
 157   // Number of chunks in this ChunkManager
 158   size_t _free_chunks_count;
 159 
 160   // Update counters after a chunk was added or removed removed.
 161   void account_for_added_chunk(const Metachunk* c);
 162   void account_for_removed_chunk(const Metachunk* c);
 163 
 164   // Debug support
 165 
 166   size_t sum_free_chunks();
 167   size_t sum_free_chunks_count();
 168 
 169   void locked_verify_free_chunks_total();
 170   void slow_locked_verify_free_chunks_total() {
 171     if (metaspace_slow_verify) {
 172       locked_verify_free_chunks_total();
 173     }
 174   }
 175   void locked_verify_free_chunks_count();
 176   void slow_locked_verify_free_chunks_count() {
 177     if (metaspace_slow_verify) {
 178       locked_verify_free_chunks_count();
 179     }
 180   }
 181   void verify_free_chunks_count();
 182 
 183   struct ChunkManagerStatistics {
 184     size_t num_by_type[NumberOfFreeLists];
 185     size_t single_size_by_type[NumberOfFreeLists];
 186     size_t total_size_by_type[NumberOfFreeLists];
 187     size_t num_humongous_chunks;
 188     size_t total_size_humongous_chunks;
 189   };
 190 
 191   void locked_get_statistics(ChunkManagerStatistics* stat) const;
 192   void get_statistics(ChunkManagerStatistics* stat) const;
 193   static void print_statistics(const ChunkManagerStatistics* stat, outputStream* out, size_t scale);
 194 
 195  public:
 196 
 197   ChunkManager(size_t specialized_size, size_t small_size, size_t medium_size)
 198       : _free_chunks_total(0), _free_chunks_count(0) {
 199     _free_chunks[SpecializedIndex].set_size(specialized_size);
 200     _free_chunks[SmallIndex].set_size(small_size);
 201     _free_chunks[MediumIndex].set_size(medium_size);
 202   }
 203 
 204   // add or delete (return) a chunk to the global freelist.
 205   Metachunk* chunk_freelist_allocate(size_t word_size);
 206 
 207   // Map a size to a list index assuming that there are lists
 208   // for special, small, medium, and humongous chunks.
 209   ChunkIndex list_index(size_t size);
 210 
 211   // Map a given index to the chunk size.
 212   size_t size_by_index(ChunkIndex index) const;
 213 
 214   // Take a chunk from the ChunkManager. The chunk is expected to be in
 215   // the chunk manager (the freelist if non-humongous, the dictionary if
 216   // humongous).
 217   void remove_chunk(Metachunk* chunk);
 218 
 219   // Return a single chunk of type index to the ChunkManager.
 220   void return_single_chunk(ChunkIndex index, Metachunk* chunk);
 221 
 222   // Add the simple linked list of chunks to the freelist of chunks
 223   // of type index.
 224   void return_chunk_list(ChunkIndex index, Metachunk* chunk);
 225 
 226   // Total of the space in the free chunks list
 227   size_t free_chunks_total_words();
 228   size_t free_chunks_total_bytes();
 229 
 230   // Number of chunks in the free chunks list
 231   size_t free_chunks_count();
 232 
 233   // Remove from a list by size.  Selects list based on size of chunk.
 234   Metachunk* free_chunks_get(size_t chunk_word_size);
 235 
 236 #define index_bounds_check(index)                                         \
 237   assert(index == SpecializedIndex ||                                     \
 238          index == SmallIndex ||                                           \
 239          index == MediumIndex ||                                          \
 240          index == HumongousIndex, "Bad index: %d", (int) index)
 241 
 242   size_t num_free_chunks(ChunkIndex index) const {
 243     index_bounds_check(index);
 244 
 245     if (index == HumongousIndex) {
 246       return _humongous_dictionary.total_free_blocks();
 247     }
 248 
 249     ssize_t count = _free_chunks[index].count();
 250     return count == -1 ? 0 : (size_t) count;
 251   }
 252 
 253   size_t size_free_chunks_in_bytes(ChunkIndex index) const {
 254     index_bounds_check(index);
 255 
 256     size_t word_size = 0;
 257     if (index == HumongousIndex) {
 258       word_size = _humongous_dictionary.total_size();
 259     } else {
 260       const size_t size_per_chunk_in_words = _free_chunks[index].size();
 261       word_size = size_per_chunk_in_words * num_free_chunks(index);
 262     }
 263 
 264     return word_size * BytesPerWord;
 265   }
 266 
 267   MetaspaceChunkFreeListSummary chunk_free_list_summary() const {
 268     return MetaspaceChunkFreeListSummary(num_free_chunks(SpecializedIndex),
 269                                          num_free_chunks(SmallIndex),
 270                                          num_free_chunks(MediumIndex),
 271                                          num_free_chunks(HumongousIndex),
 272                                          size_free_chunks_in_bytes(SpecializedIndex),
 273                                          size_free_chunks_in_bytes(SmallIndex),
 274                                          size_free_chunks_in_bytes(MediumIndex),
 275                                          size_free_chunks_in_bytes(HumongousIndex));
 276   }
 277 
 278   // Debug support
 279   void verify();
 280   void slow_verify() {
 281     if (metaspace_slow_verify) {
 282       verify();
 283     }
 284   }
 285   void locked_verify();
 286   void slow_locked_verify() {
 287     if (metaspace_slow_verify) {
 288       locked_verify();
 289     }
 290   }
 291   void verify_free_chunks_total();
 292 
 293   void locked_print_free_chunks(outputStream* st);
 294   void locked_print_sum_free_chunks(outputStream* st);
 295 
 296   void print_on(outputStream* st) const;
 297 
 298   // Prints composition for both non-class and (if available)
 299   // class chunk manager.
 300   static void print_all_chunkmanagers(outputStream* out, size_t scale = 1);
 301 };
 302 
 303 class SmallBlocks : public CHeapObj<mtClass> {
 304   const static uint _small_block_max_size = sizeof(TreeChunk<Metablock,  FreeList<Metablock> >)/HeapWordSize;
 305   const static uint _small_block_min_size = sizeof(Metablock)/HeapWordSize;
 306 
 307  private:
 308   FreeList<Metablock> _small_lists[_small_block_max_size - _small_block_min_size];
 309 
 310   FreeList<Metablock>& list_at(size_t word_size) {
 311     assert(word_size >= _small_block_min_size, "There are no metaspace objects less than %u words", _small_block_min_size);
 312     return _small_lists[word_size - _small_block_min_size];
 313   }
 314 
 315  public:
 316   SmallBlocks() {
 317     for (uint i = _small_block_min_size; i < _small_block_max_size; i++) {
 318       uint k = i - _small_block_min_size;
 319       _small_lists[k].set_size(i);
 320     }
 321   }
 322 
 323   size_t total_size() const {
 324     size_t result = 0;
 325     for (uint i = _small_block_min_size; i < _small_block_max_size; i++) {
 326       uint k = i - _small_block_min_size;
 327       result = result + _small_lists[k].count() * _small_lists[k].size();
 328     }
 329     return result;
 330   }
 331 
 332   static uint small_block_max_size() { return _small_block_max_size; }
 333   static uint small_block_min_size() { return _small_block_min_size; }
 334 
 335   MetaWord* get_block(size_t word_size) {
 336     if (list_at(word_size).count() > 0) {
 337       MetaWord* new_block = (MetaWord*) list_at(word_size).get_chunk_at_head();
 338       return new_block;
 339     } else {
 340       return NULL;
 341     }
 342   }
 343   void return_block(Metablock* free_chunk, size_t word_size) {
 344     list_at(word_size).return_chunk_at_head(free_chunk, false);
 345     assert(list_at(word_size).count() > 0, "Should have a chunk");
 346   }
 347 
 348   void print_on(outputStream* st) const {
 349     st->print_cr("SmallBlocks:");
 350     for (uint i = _small_block_min_size; i < _small_block_max_size; i++) {
 351       uint k = i - _small_block_min_size;
 352       st->print_cr("small_lists size " SIZE_FORMAT " count " SIZE_FORMAT, _small_lists[k].size(), _small_lists[k].count());
 353     }
 354   }
 355 };
 356 
 357 // Used to manage the free list of Metablocks (a block corresponds
 358 // to the allocation of a quantum of metadata).
 359 class BlockFreelist : public CHeapObj<mtClass> {
 360   BlockTreeDictionary* const _dictionary;
 361   SmallBlocks* _small_blocks;
 362 
 363   // Only allocate and split from freelist if the size of the allocation
 364   // is at least 1/4th the size of the available block.
 365   const static int WasteMultiplier = 4;
 366 
 367   // Accessors
 368   BlockTreeDictionary* dictionary() const { return _dictionary; }
 369   SmallBlocks* small_blocks() {
 370     if (_small_blocks == NULL) {
 371       _small_blocks = new SmallBlocks();
 372     }
 373     return _small_blocks;
 374   }
 375 
 376  public:
 377   BlockFreelist();
 378   ~BlockFreelist();
 379 
 380   // Get and return a block to the free list
 381   MetaWord* get_block(size_t word_size);
 382   void return_block(MetaWord* p, size_t word_size);
 383 
 384   size_t total_size() const  {
 385     size_t result = dictionary()->total_size();
 386     if (_small_blocks != NULL) {
 387       result = result + _small_blocks->total_size();
 388     }
 389     return result;
 390   }
 391 
 392   static size_t min_dictionary_size()   { return TreeChunk<Metablock, FreeList<Metablock> >::min_size(); }
 393   void print_on(outputStream* st) const;
 394 };
 395 
 396 // A VirtualSpaceList node.
 397 class VirtualSpaceNode : public CHeapObj<mtClass> {
 398   friend class VirtualSpaceList;
 399 
 400   // Link to next VirtualSpaceNode
 401   VirtualSpaceNode* _next;
 402 
 403   // total in the VirtualSpace
 404   MemRegion _reserved;
 405   ReservedSpace _rs;
 406   VirtualSpace _virtual_space;
 407   MetaWord* _top;
 408   // count of chunks contained in this VirtualSpace
 409   uintx _container_count;
 410 
 411   // Convenience functions to access the _virtual_space
 412   char* low()  const { return virtual_space()->low(); }
 413   char* high() const { return virtual_space()->high(); }
 414 
 415   // The first Metachunk will be allocated at the bottom of the
 416   // VirtualSpace
 417   Metachunk* first_chunk() { return (Metachunk*) bottom(); }
 418 
 419   // Committed but unused space in the virtual space
 420   size_t free_words_in_vs() const;
 421  public:
 422 
 423   VirtualSpaceNode(size_t byte_size);
 424   VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs), _container_count(0) {}
 425   ~VirtualSpaceNode();
 426 
 427   // Convenience functions for logical bottom and end
 428   MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
 429   MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
 430 
 431   bool contains(const void* ptr) { return ptr >= low() && ptr < high(); }
 432 
 433   size_t reserved_words() const  { return _virtual_space.reserved_size() / BytesPerWord; }
 434   size_t committed_words() const { return _virtual_space.actual_committed_size() / BytesPerWord; }
 435 
 436   bool is_pre_committed() const { return _virtual_space.special(); }
 437 
 438   // address of next available space in _virtual_space;
 439   // Accessors
 440   VirtualSpaceNode* next() { return _next; }
 441   void set_next(VirtualSpaceNode* v) { _next = v; }
 442 
 443   void set_reserved(MemRegion const v) { _reserved = v; }
 444   void set_top(MetaWord* v) { _top = v; }
 445 
 446   // Accessors
 447   MemRegion* reserved() { return &_reserved; }
 448   VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; }
 449 
 450   // Returns true if "word_size" is available in the VirtualSpace
 451   bool is_available(size_t word_size) { return word_size <= pointer_delta(end(), _top, sizeof(MetaWord)); }
 452 
 453   MetaWord* top() const { return _top; }
 454   void inc_top(size_t word_size) { _top += word_size; }
 455 
 456   uintx container_count() { return _container_count; }
 457   void inc_container_count();
 458   void dec_container_count();
 459 #ifdef ASSERT
 460   uintx container_count_slow();
 461   void verify_container_count();
 462 #endif
 463 
 464   // used and capacity in this single entry in the list
 465   size_t used_words_in_vs() const;
 466   size_t capacity_words_in_vs() const;
 467 
 468   bool initialize();
 469 
 470   // get space from the virtual space
 471   Metachunk* take_from_committed(size_t chunk_word_size);
 472 
 473   // Allocate a chunk from the virtual space and return it.
 474   Metachunk* get_chunk_vs(size_t chunk_word_size);
 475 
 476   // Expands/shrinks the committed space in a virtual space.  Delegates
 477   // to Virtualspace
 478   bool expand_by(size_t min_words, size_t preferred_words);
 479 
 480   // In preparation for deleting this node, remove all the chunks
 481   // in the node from any freelist.
 482   void purge(ChunkManager* chunk_manager);
 483 
 484   // If an allocation doesn't fit in the current node a new node is created.
 485   // Allocate chunks out of the remaining committed space in this node
 486   // to avoid wasting that memory.
 487   // This always adds up because all the chunk sizes are multiples of
 488   // the smallest chunk size.
 489   void retire(ChunkManager* chunk_manager);
 490 
 491 #ifdef ASSERT
 492   // Debug support
 493   void mangle();
 494 #endif
 495 
 496   void print_on(outputStream* st) const;
 497   void print_map(outputStream* st, bool is_class) const;
 498 };
 499 
 500 #define assert_is_aligned(value, alignment)                  \
 501   assert(is_aligned((value), (alignment)),                   \
 502          SIZE_FORMAT_HEX " is not aligned to "               \
 503          SIZE_FORMAT, (size_t)(uintptr_t)value, (alignment))
 504 
 505 // Decide if large pages should be committed when the memory is reserved.
 506 static bool should_commit_large_pages_when_reserving(size_t bytes) {
 507   if (UseLargePages && UseLargePagesInMetaspace && !os::can_commit_large_page_memory()) {
 508     size_t words = bytes / BytesPerWord;
 509     bool is_class = false; // We never reserve large pages for the class space.
 510     if (MetaspaceGC::can_expand(words, is_class) &&
 511         MetaspaceGC::allowed_expansion() >= words) {
 512       return true;
 513     }
 514   }
 515 
 516   return false;
 517 }
 518 
 519   // byte_size is the size of the associated virtualspace.
 520 VirtualSpaceNode::VirtualSpaceNode(size_t bytes) : _top(NULL), _next(NULL), _rs(), _container_count(0) {
 521   assert_is_aligned(bytes, Metaspace::reserve_alignment());
 522   bool large_pages = should_commit_large_pages_when_reserving(bytes);
 523   _rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages);
 524 
 525   if (_rs.is_reserved()) {
 526     assert(_rs.base() != NULL, "Catch if we get a NULL address");
 527     assert(_rs.size() != 0, "Catch if we get a 0 size");
 528     assert_is_aligned(_rs.base(), Metaspace::reserve_alignment());
 529     assert_is_aligned(_rs.size(), Metaspace::reserve_alignment());
 530 
 531     MemTracker::record_virtual_memory_type((address)_rs.base(), mtClass);
 532   }
 533 }
 534 
 535 void VirtualSpaceNode::purge(ChunkManager* chunk_manager) {
 536   Metachunk* chunk = first_chunk();
 537   Metachunk* invalid_chunk = (Metachunk*) top();
 538   while (chunk < invalid_chunk ) {
 539     assert(chunk->is_tagged_free(), "Should be tagged free");
 540     MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
 541     chunk_manager->remove_chunk(chunk);
 542     assert(chunk->next() == NULL &&
 543            chunk->prev() == NULL,
 544            "Was not removed from its list");
 545     chunk = (Metachunk*) next;
 546   }
 547 }
 548 
 549 void VirtualSpaceNode::print_map(outputStream* st, bool is_class) const {
 550 
 551   // Format:
 552   // <ptr>
 553   // <ptr>  . .. .               .  ..
 554   //        SSxSSMMMMMMMMMMMMMMMMsssXX
 555   //        112114444444444444444
 556   // <ptr>  . .. .               .  ..
 557   //        SSxSSMMMMMMMMMMMMMMMMsssXX
 558   //        112114444444444444444
 559 
 560   if (bottom() == top()) {
 561     return;
 562   }
 563 
 564   // First line: dividers for every med-chunk-sized interval
 565   // Second line: a dot for the start of a chunk
 566   // Third line: a letter per chunk type (x,s,m,h), uppercase if in use.
 567 
 568   const size_t spec_chunk_size = is_class ? ClassSpecializedChunk : SpecializedChunk;
 569   const size_t small_chunk_size = is_class ? ClassSmallChunk : SmallChunk;
 570   const size_t med_chunk_size = is_class ? ClassMediumChunk : MediumChunk;
 571 
 572   int line_len = 100;
 573   const size_t section_len = align_up(spec_chunk_size * line_len, med_chunk_size);
 574   line_len = (int)(section_len / spec_chunk_size);
 575 
 576   char* line1 = (char*)os::malloc(line_len, mtInternal);
 577   char* line2 = (char*)os::malloc(line_len, mtInternal);
 578   char* line3 = (char*)os::malloc(line_len, mtInternal);
 579   int pos = 0;
 580   const MetaWord* p = bottom();
 581   const Metachunk* chunk = (const Metachunk*)p;
 582   const MetaWord* chunk_end = p + chunk->word_size();
 583   while (p < top()) {
 584     if (pos == line_len) {
 585       pos = 0;
 586       st->fill_to(22);
 587       st->print_raw(line1, line_len);
 588       st->cr();
 589       st->fill_to(22);
 590       st->print_raw(line2, line_len);
 591       st->cr();
 592     }
 593     if (pos == 0) {
 594       st->print(PTR_FORMAT ":", p2i(p));
 595     }
 596     if (p == chunk_end) {
 597       chunk = (Metachunk*)p;
 598       chunk_end = p + chunk->word_size();
 599     }
 600     if (p == (const MetaWord*)chunk) {
 601       // chunk starts.
 602       line1[pos] = '.';
 603     } else {
 604       line1[pos] = ' ';
 605     }
 606     // Line 2: chunk type (x=spec, s=small, m=medium, h=humongous), uppercase if
 607     // chunk is in use.
 608     const bool chunk_is_free = ((Metachunk*)chunk)->is_tagged_free();
 609     if (chunk->word_size() == spec_chunk_size) {
 610       line2[pos] = chunk_is_free ? 'x' : 'X';
 611     } else if (chunk->word_size() == small_chunk_size) {
 612       line2[pos] = chunk_is_free ? 's' : 'S';
 613     } else if (chunk->word_size() == med_chunk_size) {
 614       line2[pos] = chunk_is_free ? 'm' : 'M';
 615    } else if (chunk->word_size() > med_chunk_size) {
 616       line2[pos] = chunk_is_free ? 'h' : 'H';
 617     } else {
 618       ShouldNotReachHere();
 619     }
 620     p += spec_chunk_size;
 621     pos ++;
 622   }
 623   if (pos > 0) {
 624     st->fill_to(22);
 625     st->print_raw(line1, pos);
 626     st->cr();
 627     st->fill_to(22);
 628     st->print_raw(line2, pos);
 629     st->cr();
 630   }
 631   os::free(line1);
 632   os::free(line2);
 633   os::free(line3);
 634 }
 635 
 636 
 637 #ifdef ASSERT
 638 uintx VirtualSpaceNode::container_count_slow() {
 639   uintx count = 0;
 640   Metachunk* chunk = first_chunk();
 641   Metachunk* invalid_chunk = (Metachunk*) top();
 642   while (chunk < invalid_chunk ) {
 643     MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
 644     // Don't count the chunks on the free lists.  Those are
 645     // still part of the VirtualSpaceNode but not currently
 646     // counted.
 647     if (!chunk->is_tagged_free()) {
 648       count++;
 649     }
 650     chunk = (Metachunk*) next;
 651   }
 652   return count;
 653 }
 654 #endif
 655 
 656 // List of VirtualSpaces for metadata allocation.
 657 class VirtualSpaceList : public CHeapObj<mtClass> {
 658   friend class VirtualSpaceNode;
 659 
 660   enum VirtualSpaceSizes {
 661     VirtualSpaceSize = 256 * K
 662   };
 663 
 664   // Head of the list
 665   VirtualSpaceNode* _virtual_space_list;
 666   // virtual space currently being used for allocations
 667   VirtualSpaceNode* _current_virtual_space;
 668 
 669   // Is this VirtualSpaceList used for the compressed class space
 670   bool _is_class;
 671 
 672   // Sum of reserved and committed memory in the virtual spaces
 673   size_t _reserved_words;
 674   size_t _committed_words;
 675 
 676   // Number of virtual spaces
 677   size_t _virtual_space_count;
 678 
 679   ~VirtualSpaceList();
 680 
 681   VirtualSpaceNode* virtual_space_list() const { return _virtual_space_list; }
 682 
 683   void set_virtual_space_list(VirtualSpaceNode* v) {
 684     _virtual_space_list = v;
 685   }
 686   void set_current_virtual_space(VirtualSpaceNode* v) {
 687     _current_virtual_space = v;
 688   }
 689 
 690   void link_vs(VirtualSpaceNode* new_entry);
 691 
 692   // Get another virtual space and add it to the list.  This
 693   // is typically prompted by a failed attempt to allocate a chunk
 694   // and is typically followed by the allocation of a chunk.
 695   bool create_new_virtual_space(size_t vs_word_size);
 696 
 697   // Chunk up the unused committed space in the current
 698   // virtual space and add the chunks to the free list.
 699   void retire_current_virtual_space();
 700 
 701  public:
 702   VirtualSpaceList(size_t word_size);
 703   VirtualSpaceList(ReservedSpace rs);
 704 
 705   size_t free_bytes();
 706 
 707   Metachunk* get_new_chunk(size_t chunk_word_size,
 708                            size_t suggested_commit_granularity);
 709 
 710   bool expand_node_by(VirtualSpaceNode* node,
 711                       size_t min_words,
 712                       size_t preferred_words);
 713 
 714   bool expand_by(size_t min_words,
 715                  size_t preferred_words);
 716 
 717   VirtualSpaceNode* current_virtual_space() {
 718     return _current_virtual_space;
 719   }
 720 
 721   bool is_class() const { return _is_class; }
 722 
 723   bool initialization_succeeded() { return _virtual_space_list != NULL; }
 724 
 725   size_t reserved_words()  { return _reserved_words; }
 726   size_t reserved_bytes()  { return reserved_words() * BytesPerWord; }
 727   size_t committed_words() { return _committed_words; }
 728   size_t committed_bytes() { return committed_words() * BytesPerWord; }
 729 
 730   void inc_reserved_words(size_t v);
 731   void dec_reserved_words(size_t v);
 732   void inc_committed_words(size_t v);
 733   void dec_committed_words(size_t v);
 734   void inc_virtual_space_count();
 735   void dec_virtual_space_count();
 736 
 737   bool contains(const void* ptr);
 738 
 739   // Unlink empty VirtualSpaceNodes and free it.
 740   void purge(ChunkManager* chunk_manager);
 741 
 742   void print_on(outputStream* st) const;
 743   void print_map(outputStream* st) const;
 744 
 745   class VirtualSpaceListIterator : public StackObj {
 746     VirtualSpaceNode* _virtual_spaces;
 747    public:
 748     VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) :
 749       _virtual_spaces(virtual_spaces) {}
 750 
 751     bool repeat() {
 752       return _virtual_spaces != NULL;
 753     }
 754 
 755     VirtualSpaceNode* get_next() {
 756       VirtualSpaceNode* result = _virtual_spaces;
 757       if (_virtual_spaces != NULL) {
 758         _virtual_spaces = _virtual_spaces->next();
 759       }
 760       return result;
 761     }
 762   };
 763 };
 764 
 765 class Metadebug : AllStatic {
 766   // Debugging support for Metaspaces
 767   static int _allocation_fail_alot_count;
 768 
 769  public:
 770 
 771   static void init_allocation_fail_alot_count();
 772 #ifdef ASSERT
 773   static bool test_metadata_failure();
 774 #endif
 775 };
 776 
 777 int Metadebug::_allocation_fail_alot_count = 0;
 778 
 779 //  SpaceManager - used by Metaspace to handle allocations
 780 class SpaceManager : public CHeapObj<mtClass> {
 781   friend class Metaspace;
 782   friend class Metadebug;
 783 
 784  private:
 785 
 786   // protects allocations
 787   Mutex* const _lock;
 788 
 789   // Type of metadata allocated.
 790   const Metaspace::MetadataType   _mdtype;
 791 
 792   // Type of metaspace
 793   const Metaspace::MetaspaceType  _space_type;
 794 
 795   // List of chunks in use by this SpaceManager.  Allocations
 796   // are done from the current chunk.  The list is used for deallocating
 797   // chunks when the SpaceManager is freed.
 798   Metachunk* _chunks_in_use[NumberOfInUseLists];
 799   Metachunk* _current_chunk;
 800 
 801   // Maximum number of small chunks to allocate to a SpaceManager
 802   static uint const _small_chunk_limit;
 803 
 804   // Maximum number of specialize chunks to allocate for anonymous
 805   // metadata space to a SpaceManager
 806   static uint const _anon_metadata_specialize_chunk_limit;
 807 
 808   // Sum of all space in allocated chunks
 809   size_t _allocated_blocks_words;
 810 
 811   // Sum of all allocated chunks
 812   size_t _allocated_chunks_words;
 813   size_t _allocated_chunks_count;
 814 
 815   // Free lists of blocks are per SpaceManager since they
 816   // are assumed to be in chunks in use by the SpaceManager
 817   // and all chunks in use by a SpaceManager are freed when
 818   // the class loader using the SpaceManager is collected.
 819   BlockFreelist* _block_freelists;
 820 
 821   // protects virtualspace and chunk expansions
 822   static const char*  _expand_lock_name;
 823   static const int    _expand_lock_rank;
 824   static Mutex* const _expand_lock;
 825 
 826  private:
 827   // Accessors
 828   Metachunk* chunks_in_use(ChunkIndex index) const { return _chunks_in_use[index]; }
 829   void set_chunks_in_use(ChunkIndex index, Metachunk* v) {
 830     _chunks_in_use[index] = v;
 831   }
 832 
 833   BlockFreelist* block_freelists() const { return _block_freelists; }
 834 
 835   Metaspace::MetadataType mdtype() { return _mdtype; }
 836 
 837   VirtualSpaceList* vs_list()   const { return Metaspace::get_space_list(_mdtype); }
 838   ChunkManager* chunk_manager() const { return Metaspace::get_chunk_manager(_mdtype); }
 839 
 840   Metachunk* current_chunk() const { return _current_chunk; }
 841   void set_current_chunk(Metachunk* v) {
 842     _current_chunk = v;
 843   }
 844 
 845   Metachunk* find_current_chunk(size_t word_size);
 846 
 847   // Add chunk to the list of chunks in use
 848   void add_chunk(Metachunk* v, bool make_current);
 849   void retire_current_chunk();
 850 
 851   Mutex* lock() const { return _lock; }
 852 
 853  protected:
 854   void initialize();
 855 
 856  public:
 857   SpaceManager(Metaspace::MetadataType mdtype,
 858                Metaspace::MetaspaceType space_type,
 859                Mutex* lock);
 860   ~SpaceManager();
 861 
 862   enum ChunkMultiples {
 863     MediumChunkMultiple = 4
 864   };
 865 
 866   static size_t specialized_chunk_size(bool is_class) { return is_class ? ClassSpecializedChunk : SpecializedChunk; }
 867   static size_t small_chunk_size(bool is_class)       { return is_class ? ClassSmallChunk : SmallChunk; }
 868   static size_t medium_chunk_size(bool is_class)      { return is_class ? ClassMediumChunk : MediumChunk; }
 869 
 870   static size_t smallest_chunk_size(bool is_class)    { return specialized_chunk_size(is_class); }
 871 
 872   // Accessors
 873   bool is_class() const { return _mdtype == Metaspace::ClassType; }
 874 
 875   size_t specialized_chunk_size() const { return specialized_chunk_size(is_class()); }
 876   size_t small_chunk_size()       const { return small_chunk_size(is_class()); }
 877   size_t medium_chunk_size()      const { return medium_chunk_size(is_class()); }
 878 
 879   size_t smallest_chunk_size()    const { return smallest_chunk_size(is_class()); }
 880 
 881   size_t medium_chunk_bunch()     const { return medium_chunk_size() * MediumChunkMultiple; }
 882 
 883   size_t allocated_blocks_words() const { return _allocated_blocks_words; }
 884   size_t allocated_blocks_bytes() const { return _allocated_blocks_words * BytesPerWord; }
 885   size_t allocated_chunks_words() const { return _allocated_chunks_words; }
 886   size_t allocated_chunks_bytes() const { return _allocated_chunks_words * BytesPerWord; }
 887   size_t allocated_chunks_count() const { return _allocated_chunks_count; }
 888 
 889   bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); }
 890 
 891   static Mutex* expand_lock() { return _expand_lock; }
 892 
 893   // Increment the per Metaspace and global running sums for Metachunks
 894   // by the given size.  This is used when a Metachunk to added to
 895   // the in-use list.
 896   void inc_size_metrics(size_t words);
 897   // Increment the per Metaspace and global running sums Metablocks by the given
 898   // size.  This is used when a Metablock is allocated.
 899   void inc_used_metrics(size_t words);
 900   // Delete the portion of the running sums for this SpaceManager. That is,
 901   // the globals running sums for the Metachunks and Metablocks are
 902   // decremented for all the Metachunks in-use by this SpaceManager.
 903   void dec_total_from_size_metrics();
 904 
 905   // Adjust the initial chunk size to match one of the fixed chunk list sizes,
 906   // or return the unadjusted size if the requested size is humongous.
 907   static size_t adjust_initial_chunk_size(size_t requested, bool is_class_space);
 908   size_t adjust_initial_chunk_size(size_t requested) const;
 909 
 910   // Get the initial chunks size for this metaspace type.
 911   size_t get_initial_chunk_size(Metaspace::MetaspaceType type) const;
 912 
 913   size_t sum_capacity_in_chunks_in_use() const;
 914   size_t sum_used_in_chunks_in_use() const;
 915   size_t sum_free_in_chunks_in_use() const;
 916   size_t sum_waste_in_chunks_in_use() const;
 917   size_t sum_waste_in_chunks_in_use(ChunkIndex index ) const;
 918 
 919   size_t sum_count_in_chunks_in_use();
 920   size_t sum_count_in_chunks_in_use(ChunkIndex i);
 921 
 922   Metachunk* get_new_chunk(size_t chunk_word_size);
 923 
 924   // Block allocation and deallocation.
 925   // Allocates a block from the current chunk
 926   MetaWord* allocate(size_t word_size);
 927   // Allocates a block from a small chunk
 928   MetaWord* get_small_chunk_and_allocate(size_t word_size);
 929 
 930   // Helper for allocations
 931   MetaWord* allocate_work(size_t word_size);
 932 
 933   // Returns a block to the per manager freelist
 934   void deallocate(MetaWord* p, size_t word_size);
 935 
 936   // Based on the allocation size and a minimum chunk size,
 937   // returned chunk size (for expanding space for chunk allocation).
 938   size_t calc_chunk_size(size_t allocation_word_size);
 939 
 940   // Called when an allocation from the current chunk fails.
 941   // Gets a new chunk (may require getting a new virtual space),
 942   // and allocates from that chunk.
 943   MetaWord* grow_and_allocate(size_t word_size);
 944 
 945   // Notify memory usage to MemoryService.
 946   void track_metaspace_memory_usage();
 947 
 948   // debugging support.
 949 
 950   void dump(outputStream* const out) const;
 951   void print_on(outputStream* st) const;
 952   void locked_print_chunks_in_use_on(outputStream* st) const;
 953 
 954   void verify();
 955   void verify_chunk_size(Metachunk* chunk);
 956 #ifdef ASSERT
 957   void verify_allocated_blocks_words();
 958 #endif
 959 
 960   // This adjusts the size given to be greater than the minimum allocation size in
 961   // words for data in metaspace.  Esentially the minimum size is currently 3 words.
 962   size_t get_allocation_word_size(size_t word_size) {
 963     size_t byte_size = word_size * BytesPerWord;
 964 
 965     size_t raw_bytes_size = MAX2(byte_size, sizeof(Metablock));
 966     raw_bytes_size = align_up(raw_bytes_size, Metachunk::object_alignment());
 967 
 968     size_t raw_word_size = raw_bytes_size / BytesPerWord;
 969     assert(raw_word_size * BytesPerWord == raw_bytes_size, "Size problem");
 970 
 971     return raw_word_size;
 972   }
 973 };
 974 
 975 uint const SpaceManager::_small_chunk_limit = 4;
 976 uint const SpaceManager::_anon_metadata_specialize_chunk_limit = 4;
 977 
 978 const char* SpaceManager::_expand_lock_name =
 979   "SpaceManager chunk allocation lock";
 980 const int SpaceManager::_expand_lock_rank = Monitor::leaf - 1;
 981 Mutex* const SpaceManager::_expand_lock =
 982   new Mutex(SpaceManager::_expand_lock_rank,
 983             SpaceManager::_expand_lock_name,
 984             Mutex::_allow_vm_block_flag,
 985             Monitor::_safepoint_check_never);
 986 
 987 void VirtualSpaceNode::inc_container_count() {
 988   assert_lock_strong(SpaceManager::expand_lock());
 989   _container_count++;
 990 }
 991 
 992 void VirtualSpaceNode::dec_container_count() {
 993   assert_lock_strong(SpaceManager::expand_lock());
 994   _container_count--;
 995 }
 996 
 997 #ifdef ASSERT
 998 void VirtualSpaceNode::verify_container_count() {
 999   assert(_container_count == container_count_slow(),
1000          "Inconsistency in container_count _container_count " UINTX_FORMAT
1001          " container_count_slow() " UINTX_FORMAT, _container_count, container_count_slow());
1002 }
1003 #endif
1004 
1005 // BlockFreelist methods
1006 
1007 BlockFreelist::BlockFreelist() : _dictionary(new BlockTreeDictionary()), _small_blocks(NULL) {}
1008 
1009 BlockFreelist::~BlockFreelist() {
1010   delete _dictionary;
1011   if (_small_blocks != NULL) {
1012     delete _small_blocks;
1013   }
1014 }
1015 
1016 void BlockFreelist::return_block(MetaWord* p, size_t word_size) {
1017   assert(word_size >= SmallBlocks::small_block_min_size(), "never return dark matter");
1018 
1019   Metablock* free_chunk = ::new (p) Metablock(word_size);
1020   if (word_size < SmallBlocks::small_block_max_size()) {
1021     small_blocks()->return_block(free_chunk, word_size);
1022   } else {
1023   dictionary()->return_chunk(free_chunk);
1024 }
1025   log_trace(gc, metaspace, freelist, blocks)("returning block at " INTPTR_FORMAT " size = "
1026             SIZE_FORMAT, p2i(free_chunk), word_size);
1027 }
1028 
1029 MetaWord* BlockFreelist::get_block(size_t word_size) {
1030   assert(word_size >= SmallBlocks::small_block_min_size(), "never get dark matter");
1031 
1032   // Try small_blocks first.
1033   if (word_size < SmallBlocks::small_block_max_size()) {
1034     // Don't create small_blocks() until needed.  small_blocks() allocates the small block list for
1035     // this space manager.
1036     MetaWord* new_block = (MetaWord*) small_blocks()->get_block(word_size);
1037     if (new_block != NULL) {
1038       log_trace(gc, metaspace, freelist, blocks)("getting block at " INTPTR_FORMAT " size = " SIZE_FORMAT,
1039               p2i(new_block), word_size);
1040       return new_block;
1041     }
1042   }
1043 
1044   if (word_size < BlockFreelist::min_dictionary_size()) {
1045     // If allocation in small blocks fails, this is Dark Matter.  Too small for dictionary.
1046     return NULL;
1047   }
1048 
1049   Metablock* free_block = dictionary()->get_chunk(word_size);
1050   if (free_block == NULL) {
1051     return NULL;
1052   }
1053 
1054   const size_t block_size = free_block->size();
1055   if (block_size > WasteMultiplier * word_size) {
1056     return_block((MetaWord*)free_block, block_size);
1057     return NULL;
1058   }
1059 
1060   MetaWord* new_block = (MetaWord*)free_block;
1061   assert(block_size >= word_size, "Incorrect size of block from freelist");
1062   const size_t unused = block_size - word_size;
1063   if (unused >= SmallBlocks::small_block_min_size()) {
1064     return_block(new_block + word_size, unused);
1065   }
1066 
1067   log_trace(gc, metaspace, freelist, blocks)("getting block at " INTPTR_FORMAT " size = " SIZE_FORMAT,
1068             p2i(new_block), word_size);
1069   return new_block;
1070 }
1071 
1072 void BlockFreelist::print_on(outputStream* st) const {
1073   dictionary()->print_free_lists(st);
1074   if (_small_blocks != NULL) {
1075     _small_blocks->print_on(st);
1076   }
1077 }
1078 
1079 // VirtualSpaceNode methods
1080 
1081 VirtualSpaceNode::~VirtualSpaceNode() {
1082   _rs.release();
1083 #ifdef ASSERT
1084   size_t word_size = sizeof(*this) / BytesPerWord;
1085   Copy::fill_to_words((HeapWord*) this, word_size, 0xf1f1f1f1);
1086 #endif
1087 }
1088 
1089 size_t VirtualSpaceNode::used_words_in_vs() const {
1090   return pointer_delta(top(), bottom(), sizeof(MetaWord));
1091 }
1092 
1093 // Space committed in the VirtualSpace
1094 size_t VirtualSpaceNode::capacity_words_in_vs() const {
1095   return pointer_delta(end(), bottom(), sizeof(MetaWord));
1096 }
1097 
1098 size_t VirtualSpaceNode::free_words_in_vs() const {
1099   return pointer_delta(end(), top(), sizeof(MetaWord));
1100 }
1101 
1102 // Allocates the chunk from the virtual space only.
1103 // This interface is also used internally for debugging.  Not all
1104 // chunks removed here are necessarily used for allocation.
1105 Metachunk* VirtualSpaceNode::take_from_committed(size_t chunk_word_size) {
1106   // Bottom of the new chunk
1107   MetaWord* chunk_limit = top();
1108   assert(chunk_limit != NULL, "Not safe to call this method");
1109 
1110   // The virtual spaces are always expanded by the
1111   // commit granularity to enforce the following condition.
1112   // Without this the is_available check will not work correctly.
1113   assert(_virtual_space.committed_size() == _virtual_space.actual_committed_size(),
1114       "The committed memory doesn't match the expanded memory.");
1115 
1116   if (!is_available(chunk_word_size)) {
1117     LogTarget(Debug, gc, metaspace, freelist) lt;
1118     if (lt.is_enabled()) {
1119       LogStream ls(lt);
1120       ls.print("VirtualSpaceNode::take_from_committed() not available " SIZE_FORMAT " words ", chunk_word_size);
1121       // Dump some information about the virtual space that is nearly full
1122       print_on(&ls);
1123     }
1124     return NULL;
1125   }
1126 
1127   // Take the space  (bump top on the current virtual space).
1128   inc_top(chunk_word_size);
1129 
1130   // Initialize the chunk
1131   Metachunk* result = ::new (chunk_limit) Metachunk(chunk_word_size, this);
1132   return result;
1133 }
1134 
1135 
1136 // Expand the virtual space (commit more of the reserved space)
1137 bool VirtualSpaceNode::expand_by(size_t min_words, size_t preferred_words) {
1138   size_t min_bytes = min_words * BytesPerWord;
1139   size_t preferred_bytes = preferred_words * BytesPerWord;
1140 
1141   size_t uncommitted = virtual_space()->reserved_size() - virtual_space()->actual_committed_size();
1142 
1143   if (uncommitted < min_bytes) {
1144     return false;
1145   }
1146 
1147   size_t commit = MIN2(preferred_bytes, uncommitted);
1148   bool result = virtual_space()->expand_by(commit, false);
1149 
1150   assert(result, "Failed to commit memory");
1151 
1152   return result;
1153 }
1154 
1155 Metachunk* VirtualSpaceNode::get_chunk_vs(size_t chunk_word_size) {
1156   assert_lock_strong(SpaceManager::expand_lock());
1157   Metachunk* result = take_from_committed(chunk_word_size);
1158   if (result != NULL) {
1159     inc_container_count();
1160   }
1161   return result;
1162 }
1163 
1164 bool VirtualSpaceNode::initialize() {
1165 
1166   if (!_rs.is_reserved()) {
1167     return false;
1168   }
1169 
1170   // These are necessary restriction to make sure that the virtual space always
1171   // grows in steps of Metaspace::commit_alignment(). If both base and size are
1172   // aligned only the middle alignment of the VirtualSpace is used.
1173   assert_is_aligned(_rs.base(), Metaspace::commit_alignment());
1174   assert_is_aligned(_rs.size(), Metaspace::commit_alignment());
1175 
1176   // ReservedSpaces marked as special will have the entire memory
1177   // pre-committed. Setting a committed size will make sure that
1178   // committed_size and actual_committed_size agrees.
1179   size_t pre_committed_size = _rs.special() ? _rs.size() : 0;
1180 
1181   bool result = virtual_space()->initialize_with_granularity(_rs, pre_committed_size,
1182                                             Metaspace::commit_alignment());
1183   if (result) {
1184     assert(virtual_space()->committed_size() == virtual_space()->actual_committed_size(),
1185         "Checking that the pre-committed memory was registered by the VirtualSpace");
1186 
1187     set_top((MetaWord*)virtual_space()->low());
1188     set_reserved(MemRegion((HeapWord*)_rs.base(),
1189                  (HeapWord*)(_rs.base() + _rs.size())));
1190 
1191     assert(reserved()->start() == (HeapWord*) _rs.base(),
1192            "Reserved start was not set properly " PTR_FORMAT
1193            " != " PTR_FORMAT, p2i(reserved()->start()), p2i(_rs.base()));
1194     assert(reserved()->word_size() == _rs.size() / BytesPerWord,
1195            "Reserved size was not set properly " SIZE_FORMAT
1196            " != " SIZE_FORMAT, reserved()->word_size(),
1197            _rs.size() / BytesPerWord);
1198   }
1199 
1200   return result;
1201 }
1202 
1203 void VirtualSpaceNode::print_on(outputStream* st) const {
1204   size_t used = used_words_in_vs();
1205   size_t capacity = capacity_words_in_vs();
1206   VirtualSpace* vs = virtual_space();
1207   st->print_cr("   space @ " PTR_FORMAT " " SIZE_FORMAT "K, " SIZE_FORMAT_W(3) "%% used "
1208            "[" PTR_FORMAT ", " PTR_FORMAT ", "
1209            PTR_FORMAT ", " PTR_FORMAT ")",
1210            p2i(vs), capacity / K,
1211            capacity == 0 ? 0 : used * 100 / capacity,
1212            p2i(bottom()), p2i(top()), p2i(end()),
1213            p2i(vs->high_boundary()));
1214 }
1215 
1216 #ifdef ASSERT
1217 void VirtualSpaceNode::mangle() {
1218   size_t word_size = capacity_words_in_vs();
1219   Copy::fill_to_words((HeapWord*) low(), word_size, 0xf1f1f1f1);
1220 }
1221 #endif // ASSERT
1222 
1223 // VirtualSpaceList methods
1224 // Space allocated from the VirtualSpace
1225 
1226 VirtualSpaceList::~VirtualSpaceList() {
1227   VirtualSpaceListIterator iter(virtual_space_list());
1228   while (iter.repeat()) {
1229     VirtualSpaceNode* vsl = iter.get_next();
1230     delete vsl;
1231   }
1232 }
1233 
1234 void VirtualSpaceList::inc_reserved_words(size_t v) {
1235   assert_lock_strong(SpaceManager::expand_lock());
1236   _reserved_words = _reserved_words + v;
1237 }
1238 void VirtualSpaceList::dec_reserved_words(size_t v) {
1239   assert_lock_strong(SpaceManager::expand_lock());
1240   _reserved_words = _reserved_words - v;
1241 }
1242 
1243 #define assert_committed_below_limit()                        \
1244   assert(MetaspaceAux::committed_bytes() <= MaxMetaspaceSize, \
1245          "Too much committed memory. Committed: " SIZE_FORMAT \
1246          " limit (MaxMetaspaceSize): " SIZE_FORMAT,           \
1247          MetaspaceAux::committed_bytes(), MaxMetaspaceSize);
1248 
1249 void VirtualSpaceList::inc_committed_words(size_t v) {
1250   assert_lock_strong(SpaceManager::expand_lock());
1251   _committed_words = _committed_words + v;
1252 
1253   assert_committed_below_limit();
1254 }
1255 void VirtualSpaceList::dec_committed_words(size_t v) {
1256   assert_lock_strong(SpaceManager::expand_lock());
1257   _committed_words = _committed_words - v;
1258 
1259   assert_committed_below_limit();
1260 }
1261 
1262 void VirtualSpaceList::inc_virtual_space_count() {
1263   assert_lock_strong(SpaceManager::expand_lock());
1264   _virtual_space_count++;
1265 }
1266 void VirtualSpaceList::dec_virtual_space_count() {
1267   assert_lock_strong(SpaceManager::expand_lock());
1268   _virtual_space_count--;
1269 }
1270 
1271 void ChunkManager::remove_chunk(Metachunk* chunk) {
1272   size_t word_size = chunk->word_size();
1273   ChunkIndex index = list_index(word_size);
1274   if (index != HumongousIndex) {
1275     free_chunks(index)->remove_chunk(chunk);
1276   } else {
1277     humongous_dictionary()->remove_chunk(chunk);
1278   }
1279 
1280   // Chunk has been removed from the chunks free list, update counters.
1281   account_for_removed_chunk(chunk);
1282 }
1283 
1284 // Walk the list of VirtualSpaceNodes and delete
1285 // nodes with a 0 container_count.  Remove Metachunks in
1286 // the node from their respective freelists.
1287 void VirtualSpaceList::purge(ChunkManager* chunk_manager) {
1288   assert(SafepointSynchronize::is_at_safepoint(), "must be called at safepoint for contains to work");
1289   assert_lock_strong(SpaceManager::expand_lock());
1290   // Don't use a VirtualSpaceListIterator because this
1291   // list is being changed and a straightforward use of an iterator is not safe.
1292   VirtualSpaceNode* purged_vsl = NULL;
1293   VirtualSpaceNode* prev_vsl = virtual_space_list();
1294   VirtualSpaceNode* next_vsl = prev_vsl;
1295   while (next_vsl != NULL) {
1296     VirtualSpaceNode* vsl = next_vsl;
1297     DEBUG_ONLY(vsl->verify_container_count();)
1298     next_vsl = vsl->next();
1299     // Don't free the current virtual space since it will likely
1300     // be needed soon.
1301     if (vsl->container_count() == 0 && vsl != current_virtual_space()) {
1302       // Unlink it from the list
1303       if (prev_vsl == vsl) {
1304         // This is the case of the current node being the first node.
1305         assert(vsl == virtual_space_list(), "Expected to be the first node");
1306         set_virtual_space_list(vsl->next());
1307       } else {
1308         prev_vsl->set_next(vsl->next());
1309       }
1310 
1311       vsl->purge(chunk_manager);
1312       dec_reserved_words(vsl->reserved_words());
1313       dec_committed_words(vsl->committed_words());
1314       dec_virtual_space_count();
1315       purged_vsl = vsl;
1316       delete vsl;
1317     } else {
1318       prev_vsl = vsl;
1319     }
1320   }
1321 #ifdef ASSERT
1322   if (purged_vsl != NULL) {
1323     // List should be stable enough to use an iterator here.
1324     VirtualSpaceListIterator iter(virtual_space_list());
1325     while (iter.repeat()) {
1326       VirtualSpaceNode* vsl = iter.get_next();
1327       assert(vsl != purged_vsl, "Purge of vsl failed");
1328     }
1329   }
1330 #endif
1331 }
1332 
1333 
1334 // This function looks at the mmap regions in the metaspace without locking.
1335 // The chunks are added with store ordering and not deleted except for at
1336 // unloading time during a safepoint.
1337 bool VirtualSpaceList::contains(const void* ptr) {
1338   // List should be stable enough to use an iterator here because removing virtual
1339   // space nodes is only allowed at a safepoint.
1340   VirtualSpaceListIterator iter(virtual_space_list());
1341   while (iter.repeat()) {
1342     VirtualSpaceNode* vsn = iter.get_next();
1343     if (vsn->contains(ptr)) {
1344       return true;
1345     }
1346   }
1347   return false;
1348 }
1349 
1350 void VirtualSpaceList::retire_current_virtual_space() {
1351   assert_lock_strong(SpaceManager::expand_lock());
1352 
1353   VirtualSpaceNode* vsn = current_virtual_space();
1354 
1355   ChunkManager* cm = is_class() ? Metaspace::chunk_manager_class() :
1356                                   Metaspace::chunk_manager_metadata();
1357 
1358   vsn->retire(cm);
1359 }
1360 
1361 void VirtualSpaceNode::retire(ChunkManager* chunk_manager) {
1362   DEBUG_ONLY(verify_container_count();)
1363   for (int i = (int)MediumIndex; i >= (int)ZeroIndex; --i) {
1364     ChunkIndex index = (ChunkIndex)i;
1365     size_t chunk_size = chunk_manager->size_by_index(index);
1366 
1367     while (free_words_in_vs() >= chunk_size) {
1368       Metachunk* chunk = get_chunk_vs(chunk_size);
1369       assert(chunk != NULL, "allocation should have been successful");
1370 
1371       chunk_manager->return_single_chunk(index, chunk);
1372     }
1373     DEBUG_ONLY(verify_container_count();)
1374   }
1375   assert(free_words_in_vs() == 0, "should be empty now");
1376 }
1377 
1378 VirtualSpaceList::VirtualSpaceList(size_t word_size) :
1379                                    _is_class(false),
1380                                    _virtual_space_list(NULL),
1381                                    _current_virtual_space(NULL),
1382                                    _reserved_words(0),
1383                                    _committed_words(0),
1384                                    _virtual_space_count(0) {
1385   MutexLockerEx cl(SpaceManager::expand_lock(),
1386                    Mutex::_no_safepoint_check_flag);
1387   create_new_virtual_space(word_size);
1388 }
1389 
1390 VirtualSpaceList::VirtualSpaceList(ReservedSpace rs) :
1391                                    _is_class(true),
1392                                    _virtual_space_list(NULL),
1393                                    _current_virtual_space(NULL),
1394                                    _reserved_words(0),
1395                                    _committed_words(0),
1396                                    _virtual_space_count(0) {
1397   MutexLockerEx cl(SpaceManager::expand_lock(),
1398                    Mutex::_no_safepoint_check_flag);
1399   VirtualSpaceNode* class_entry = new VirtualSpaceNode(rs);
1400   bool succeeded = class_entry->initialize();
1401   if (succeeded) {
1402     link_vs(class_entry);
1403   }
1404 }
1405 
1406 size_t VirtualSpaceList::free_bytes() {
1407   return current_virtual_space()->free_words_in_vs() * BytesPerWord;
1408 }
1409 
1410 // Allocate another meta virtual space and add it to the list.
1411 bool VirtualSpaceList::create_new_virtual_space(size_t vs_word_size) {
1412   assert_lock_strong(SpaceManager::expand_lock());
1413 
1414   if (is_class()) {
1415     assert(false, "We currently don't support more than one VirtualSpace for"
1416                   " the compressed class space. The initialization of the"
1417                   " CCS uses another code path and should not hit this path.");
1418     return false;
1419   }
1420 
1421   if (vs_word_size == 0) {
1422     assert(false, "vs_word_size should always be at least _reserve_alignment large.");
1423     return false;
1424   }
1425 
1426   // Reserve the space
1427   size_t vs_byte_size = vs_word_size * BytesPerWord;
1428   assert_is_aligned(vs_byte_size, Metaspace::reserve_alignment());
1429 
1430   // Allocate the meta virtual space and initialize it.
1431   VirtualSpaceNode* new_entry = new VirtualSpaceNode(vs_byte_size);
1432   if (!new_entry->initialize()) {
1433     delete new_entry;
1434     return false;
1435   } else {
1436     assert(new_entry->reserved_words() == vs_word_size,
1437         "Reserved memory size differs from requested memory size");
1438     // ensure lock-free iteration sees fully initialized node
1439     OrderAccess::storestore();
1440     link_vs(new_entry);
1441     return true;
1442   }
1443 }
1444 
1445 void VirtualSpaceList::link_vs(VirtualSpaceNode* new_entry) {
1446   if (virtual_space_list() == NULL) {
1447       set_virtual_space_list(new_entry);
1448   } else {
1449     current_virtual_space()->set_next(new_entry);
1450   }
1451   set_current_virtual_space(new_entry);
1452   inc_reserved_words(new_entry->reserved_words());
1453   inc_committed_words(new_entry->committed_words());
1454   inc_virtual_space_count();
1455 #ifdef ASSERT
1456   new_entry->mangle();
1457 #endif
1458   LogTarget(Trace, gc, metaspace) lt;
1459   if (lt.is_enabled()) {
1460     LogStream ls(lt);
1461     VirtualSpaceNode* vsl = current_virtual_space();
1462     ResourceMark rm;
1463     vsl->print_on(&ls);
1464   }
1465 }
1466 
1467 bool VirtualSpaceList::expand_node_by(VirtualSpaceNode* node,
1468                                       size_t min_words,
1469                                       size_t preferred_words) {
1470   size_t before = node->committed_words();
1471 
1472   bool result = node->expand_by(min_words, preferred_words);
1473 
1474   size_t after = node->committed_words();
1475 
1476   // after and before can be the same if the memory was pre-committed.
1477   assert(after >= before, "Inconsistency");
1478   inc_committed_words(after - before);
1479 
1480   return result;
1481 }
1482 
1483 bool VirtualSpaceList::expand_by(size_t min_words, size_t preferred_words) {
1484   assert_is_aligned(min_words,       Metaspace::commit_alignment_words());
1485   assert_is_aligned(preferred_words, Metaspace::commit_alignment_words());
1486   assert(min_words <= preferred_words, "Invalid arguments");
1487 
1488   if (!MetaspaceGC::can_expand(min_words, this->is_class())) {
1489     return  false;
1490   }
1491 
1492   size_t allowed_expansion_words = MetaspaceGC::allowed_expansion();
1493   if (allowed_expansion_words < min_words) {
1494     return false;
1495   }
1496 
1497   size_t max_expansion_words = MIN2(preferred_words, allowed_expansion_words);
1498 
1499   // Commit more memory from the the current virtual space.
1500   bool vs_expanded = expand_node_by(current_virtual_space(),
1501                                     min_words,
1502                                     max_expansion_words);
1503   if (vs_expanded) {
1504     return true;
1505   }
1506   retire_current_virtual_space();
1507 
1508   // Get another virtual space.
1509   size_t grow_vs_words = MAX2((size_t)VirtualSpaceSize, preferred_words);
1510   grow_vs_words = align_up(grow_vs_words, Metaspace::reserve_alignment_words());
1511 
1512   if (create_new_virtual_space(grow_vs_words)) {
1513     if (current_virtual_space()->is_pre_committed()) {
1514       // The memory was pre-committed, so we are done here.
1515       assert(min_words <= current_virtual_space()->committed_words(),
1516           "The new VirtualSpace was pre-committed, so it"
1517           "should be large enough to fit the alloc request.");
1518       return true;
1519     }
1520 
1521     return expand_node_by(current_virtual_space(),
1522                           min_words,
1523                           max_expansion_words);
1524   }
1525 
1526   return false;
1527 }
1528 
1529 Metachunk* VirtualSpaceList::get_new_chunk(size_t chunk_word_size, size_t suggested_commit_granularity) {
1530 
1531   // Allocate a chunk out of the current virtual space.
1532   Metachunk* next = current_virtual_space()->get_chunk_vs(chunk_word_size);
1533 
1534   if (next != NULL) {
1535     return next;
1536   }
1537 
1538   // The expand amount is currently only determined by the requested sizes
1539   // and not how much committed memory is left in the current virtual space.
1540 
1541   size_t min_word_size       = align_up(chunk_word_size,              Metaspace::commit_alignment_words());
1542   size_t preferred_word_size = align_up(suggested_commit_granularity, Metaspace::commit_alignment_words());
1543   if (min_word_size >= preferred_word_size) {
1544     // Can happen when humongous chunks are allocated.
1545     preferred_word_size = min_word_size;
1546   }
1547 
1548   bool expanded = expand_by(min_word_size, preferred_word_size);
1549   if (expanded) {
1550     next = current_virtual_space()->get_chunk_vs(chunk_word_size);
1551     assert(next != NULL, "The allocation was expected to succeed after the expansion");
1552   }
1553 
1554    return next;
1555 }
1556 
1557 void VirtualSpaceList::print_on(outputStream* st) const {
1558   VirtualSpaceListIterator iter(virtual_space_list());
1559   while (iter.repeat()) {
1560     VirtualSpaceNode* node = iter.get_next();
1561     node->print_on(st);
1562   }
1563 }
1564 
1565 void VirtualSpaceList::print_map(outputStream* st) const {
1566   VirtualSpaceNode* list = virtual_space_list();
1567   VirtualSpaceListIterator iter(list);
1568   unsigned i = 0;
1569   while (iter.repeat()) {
1570     st->print_cr("Node %u:", i);
1571     VirtualSpaceNode* node = iter.get_next();
1572     node->print_map(st, this->is_class());
1573     i ++;
1574   }
1575 }
1576 
1577 // MetaspaceGC methods
1578 
1579 // VM_CollectForMetadataAllocation is the vm operation used to GC.
1580 // Within the VM operation after the GC the attempt to allocate the metadata
1581 // should succeed.  If the GC did not free enough space for the metaspace
1582 // allocation, the HWM is increased so that another virtualspace will be
1583 // allocated for the metadata.  With perm gen the increase in the perm
1584 // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion.  The
1585 // metaspace policy uses those as the small and large steps for the HWM.
1586 //
1587 // After the GC the compute_new_size() for MetaspaceGC is called to
1588 // resize the capacity of the metaspaces.  The current implementation
1589 // is based on the flags MinMetaspaceFreeRatio and MaxMetaspaceFreeRatio used
1590 // to resize the Java heap by some GC's.  New flags can be implemented
1591 // if really needed.  MinMetaspaceFreeRatio is used to calculate how much
1592 // free space is desirable in the metaspace capacity to decide how much
1593 // to increase the HWM.  MaxMetaspaceFreeRatio is used to decide how much
1594 // free space is desirable in the metaspace capacity before decreasing
1595 // the HWM.
1596 
1597 // Calculate the amount to increase the high water mark (HWM).
1598 // Increase by a minimum amount (MinMetaspaceExpansion) so that
1599 // another expansion is not requested too soon.  If that is not
1600 // enough to satisfy the allocation, increase by MaxMetaspaceExpansion.
1601 // If that is still not enough, expand by the size of the allocation
1602 // plus some.
1603 size_t MetaspaceGC::delta_capacity_until_GC(size_t bytes) {
1604   size_t min_delta = MinMetaspaceExpansion;
1605   size_t max_delta = MaxMetaspaceExpansion;
1606   size_t delta = align_up(bytes, Metaspace::commit_alignment());
1607 
1608   if (delta <= min_delta) {
1609     delta = min_delta;
1610   } else if (delta <= max_delta) {
1611     // Don't want to hit the high water mark on the next
1612     // allocation so make the delta greater than just enough
1613     // for this allocation.
1614     delta = max_delta;
1615   } else {
1616     // This allocation is large but the next ones are probably not
1617     // so increase by the minimum.
1618     delta = delta + min_delta;
1619   }
1620 
1621   assert_is_aligned(delta, Metaspace::commit_alignment());
1622 
1623   return delta;
1624 }
1625 
1626 size_t MetaspaceGC::capacity_until_GC() {
1627   size_t value = OrderAccess::load_acquire(&_capacity_until_GC);
1628   assert(value >= MetaspaceSize, "Not initialized properly?");
1629   return value;
1630 }
1631 
1632 bool MetaspaceGC::inc_capacity_until_GC(size_t v, size_t* new_cap_until_GC, size_t* old_cap_until_GC) {
1633   assert_is_aligned(v, Metaspace::commit_alignment());
1634 
1635   intptr_t capacity_until_GC = _capacity_until_GC;
1636   intptr_t new_value = capacity_until_GC + v;
1637 
1638   if (new_value < capacity_until_GC) {
1639     // The addition wrapped around, set new_value to aligned max value.
1640     new_value = align_down(max_uintx, Metaspace::commit_alignment());
1641   }
1642 
1643   intptr_t expected = _capacity_until_GC;
1644   intptr_t actual = Atomic::cmpxchg(new_value, &_capacity_until_GC, expected);
1645 
1646   if (expected != actual) {
1647     return false;
1648   }
1649 
1650   if (new_cap_until_GC != NULL) {
1651     *new_cap_until_GC = new_value;
1652   }
1653   if (old_cap_until_GC != NULL) {
1654     *old_cap_until_GC = capacity_until_GC;
1655   }
1656   return true;
1657 }
1658 
1659 size_t MetaspaceGC::dec_capacity_until_GC(size_t v) {
1660   assert_is_aligned(v, Metaspace::commit_alignment());
1661 
1662   return (size_t)Atomic::sub((intptr_t)v, &_capacity_until_GC);
1663 }
1664 
1665 void MetaspaceGC::initialize() {
1666   // Set the high-water mark to MaxMetapaceSize during VM initializaton since
1667   // we can't do a GC during initialization.
1668   _capacity_until_GC = MaxMetaspaceSize;
1669 }
1670 
1671 void MetaspaceGC::post_initialize() {
1672   // Reset the high-water mark once the VM initialization is done.
1673   _capacity_until_GC = MAX2(MetaspaceAux::committed_bytes(), MetaspaceSize);
1674 }
1675 
1676 bool MetaspaceGC::can_expand(size_t word_size, bool is_class) {
1677   // Check if the compressed class space is full.
1678   if (is_class && Metaspace::using_class_space()) {
1679     size_t class_committed = MetaspaceAux::committed_bytes(Metaspace::ClassType);
1680     if (class_committed + word_size * BytesPerWord > CompressedClassSpaceSize) {
1681       return false;
1682     }
1683   }
1684 
1685   // Check if the user has imposed a limit on the metaspace memory.
1686   size_t committed_bytes = MetaspaceAux::committed_bytes();
1687   if (committed_bytes + word_size * BytesPerWord > MaxMetaspaceSize) {
1688     return false;
1689   }
1690 
1691   return true;
1692 }
1693 
1694 size_t MetaspaceGC::allowed_expansion() {
1695   size_t committed_bytes = MetaspaceAux::committed_bytes();
1696   size_t capacity_until_gc = capacity_until_GC();
1697 
1698   assert(capacity_until_gc >= committed_bytes,
1699          "capacity_until_gc: " SIZE_FORMAT " < committed_bytes: " SIZE_FORMAT,
1700          capacity_until_gc, committed_bytes);
1701 
1702   size_t left_until_max  = MaxMetaspaceSize - committed_bytes;
1703   size_t left_until_GC = capacity_until_gc - committed_bytes;
1704   size_t left_to_commit = MIN2(left_until_GC, left_until_max);
1705 
1706   return left_to_commit / BytesPerWord;
1707 }
1708 
1709 void MetaspaceGC::compute_new_size() {
1710   assert(_shrink_factor <= 100, "invalid shrink factor");
1711   uint current_shrink_factor = _shrink_factor;
1712   _shrink_factor = 0;
1713 
1714   // Using committed_bytes() for used_after_gc is an overestimation, since the
1715   // chunk free lists are included in committed_bytes() and the memory in an
1716   // un-fragmented chunk free list is available for future allocations.
1717   // However, if the chunk free lists becomes fragmented, then the memory may
1718   // not be available for future allocations and the memory is therefore "in use".
1719   // Including the chunk free lists in the definition of "in use" is therefore
1720   // necessary. Not including the chunk free lists can cause capacity_until_GC to
1721   // shrink below committed_bytes() and this has caused serious bugs in the past.
1722   const size_t used_after_gc = MetaspaceAux::committed_bytes();
1723   const size_t capacity_until_GC = MetaspaceGC::capacity_until_GC();
1724 
1725   const double minimum_free_percentage = MinMetaspaceFreeRatio / 100.0;
1726   const double maximum_used_percentage = 1.0 - minimum_free_percentage;
1727 
1728   const double min_tmp = used_after_gc / maximum_used_percentage;
1729   size_t minimum_desired_capacity =
1730     (size_t)MIN2(min_tmp, double(max_uintx));
1731   // Don't shrink less than the initial generation size
1732   minimum_desired_capacity = MAX2(minimum_desired_capacity,
1733                                   MetaspaceSize);
1734 
1735   log_trace(gc, metaspace)("MetaspaceGC::compute_new_size: ");
1736   log_trace(gc, metaspace)("    minimum_free_percentage: %6.2f  maximum_used_percentage: %6.2f",
1737                            minimum_free_percentage, maximum_used_percentage);
1738   log_trace(gc, metaspace)("     used_after_gc       : %6.1fKB", used_after_gc / (double) K);
1739 
1740 
1741   size_t shrink_bytes = 0;
1742   if (capacity_until_GC < minimum_desired_capacity) {
1743     // If we have less capacity below the metaspace HWM, then
1744     // increment the HWM.
1745     size_t expand_bytes = minimum_desired_capacity - capacity_until_GC;
1746     expand_bytes = align_up(expand_bytes, Metaspace::commit_alignment());
1747     // Don't expand unless it's significant
1748     if (expand_bytes >= MinMetaspaceExpansion) {
1749       size_t new_capacity_until_GC = 0;
1750       bool succeeded = MetaspaceGC::inc_capacity_until_GC(expand_bytes, &new_capacity_until_GC);
1751       assert(succeeded, "Should always succesfully increment HWM when at safepoint");
1752 
1753       Metaspace::tracer()->report_gc_threshold(capacity_until_GC,
1754                                                new_capacity_until_GC,
1755                                                MetaspaceGCThresholdUpdater::ComputeNewSize);
1756       log_trace(gc, metaspace)("    expanding:  minimum_desired_capacity: %6.1fKB  expand_bytes: %6.1fKB  MinMetaspaceExpansion: %6.1fKB  new metaspace HWM:  %6.1fKB",
1757                                minimum_desired_capacity / (double) K,
1758                                expand_bytes / (double) K,
1759                                MinMetaspaceExpansion / (double) K,
1760                                new_capacity_until_GC / (double) K);
1761     }
1762     return;
1763   }
1764 
1765   // No expansion, now see if we want to shrink
1766   // We would never want to shrink more than this
1767   assert(capacity_until_GC >= minimum_desired_capacity,
1768          SIZE_FORMAT " >= " SIZE_FORMAT,
1769          capacity_until_GC, minimum_desired_capacity);
1770   size_t max_shrink_bytes = capacity_until_GC - minimum_desired_capacity;
1771 
1772   // Should shrinking be considered?
1773   if (MaxMetaspaceFreeRatio < 100) {
1774     const double maximum_free_percentage = MaxMetaspaceFreeRatio / 100.0;
1775     const double minimum_used_percentage = 1.0 - maximum_free_percentage;
1776     const double max_tmp = used_after_gc / minimum_used_percentage;
1777     size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx));
1778     maximum_desired_capacity = MAX2(maximum_desired_capacity,
1779                                     MetaspaceSize);
1780     log_trace(gc, metaspace)("    maximum_free_percentage: %6.2f  minimum_used_percentage: %6.2f",
1781                              maximum_free_percentage, minimum_used_percentage);
1782     log_trace(gc, metaspace)("    minimum_desired_capacity: %6.1fKB  maximum_desired_capacity: %6.1fKB",
1783                              minimum_desired_capacity / (double) K, maximum_desired_capacity / (double) K);
1784 
1785     assert(minimum_desired_capacity <= maximum_desired_capacity,
1786            "sanity check");
1787 
1788     if (capacity_until_GC > maximum_desired_capacity) {
1789       // Capacity too large, compute shrinking size
1790       shrink_bytes = capacity_until_GC - maximum_desired_capacity;
1791       // We don't want shrink all the way back to initSize if people call
1792       // System.gc(), because some programs do that between "phases" and then
1793       // we'd just have to grow the heap up again for the next phase.  So we
1794       // damp the shrinking: 0% on the first call, 10% on the second call, 40%
1795       // on the third call, and 100% by the fourth call.  But if we recompute
1796       // size without shrinking, it goes back to 0%.
1797       shrink_bytes = shrink_bytes / 100 * current_shrink_factor;
1798 
1799       shrink_bytes = align_down(shrink_bytes, Metaspace::commit_alignment());
1800 
1801       assert(shrink_bytes <= max_shrink_bytes,
1802              "invalid shrink size " SIZE_FORMAT " not <= " SIZE_FORMAT,
1803              shrink_bytes, max_shrink_bytes);
1804       if (current_shrink_factor == 0) {
1805         _shrink_factor = 10;
1806       } else {
1807         _shrink_factor = MIN2(current_shrink_factor * 4, (uint) 100);
1808       }
1809       log_trace(gc, metaspace)("    shrinking:  initThreshold: %.1fK  maximum_desired_capacity: %.1fK",
1810                                MetaspaceSize / (double) K, maximum_desired_capacity / (double) K);
1811       log_trace(gc, metaspace)("    shrink_bytes: %.1fK  current_shrink_factor: %d  new shrink factor: %d  MinMetaspaceExpansion: %.1fK",
1812                                shrink_bytes / (double) K, current_shrink_factor, _shrink_factor, MinMetaspaceExpansion / (double) K);
1813     }
1814   }
1815 
1816   // Don't shrink unless it's significant
1817   if (shrink_bytes >= MinMetaspaceExpansion &&
1818       ((capacity_until_GC - shrink_bytes) >= MetaspaceSize)) {
1819     size_t new_capacity_until_GC = MetaspaceGC::dec_capacity_until_GC(shrink_bytes);
1820     Metaspace::tracer()->report_gc_threshold(capacity_until_GC,
1821                                              new_capacity_until_GC,
1822                                              MetaspaceGCThresholdUpdater::ComputeNewSize);
1823   }
1824 }
1825 
1826 MetaWord* MetaspaceGC::satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
1827                                                           size_t word_size,
1828                                                           Metaspace::MetadataType mdtype) {
1829   uint loop_count = 0;
1830   uint gc_count = 0;
1831   uint full_gc_count = 0;
1832 
1833   assert(!Heap_lock->owned_by_self(), "Should not be holding the Heap_lock");
1834 
1835   do {
1836     MetaWord* result = loader_data->metaspace_non_null()->allocate(word_size, mdtype);
1837     if (result != NULL) {
1838       return result;
1839     }
1840 
1841     if (GCLocker::is_active_and_needs_gc()) {
1842       // If the GCLocker is active, just expand and allocate.
1843       // If that does not succeed, wait if this thread is not
1844       // in a critical section itself.
1845       result = loader_data->metaspace_non_null()->expand_and_allocate(word_size, mdtype);
1846       if (result != NULL) {
1847         return result;
1848       }
1849       JavaThread* jthr = JavaThread::current();
1850       if (!jthr->in_critical()) {
1851         // Wait for JNI critical section to be exited
1852         GCLocker::stall_until_clear();
1853         // The GC invoked by the last thread leaving the critical
1854         // section will be a young collection and a full collection
1855         // is (currently) needed for unloading classes so continue
1856         // to the next iteration to get a full GC.
1857         continue;
1858       } else {
1859         if (CheckJNICalls) {
1860           fatal("Possible deadlock due to allocating while"
1861                 " in jni critical section");
1862         }
1863         return NULL;
1864       }
1865     }
1866 
1867     {  // Need lock to get self consistent gc_count's
1868       MutexLocker ml(Heap_lock);
1869       gc_count      = Universe::heap()->total_collections();
1870       full_gc_count = Universe::heap()->total_full_collections();
1871     }
1872 
1873     // Generate a VM operation
1874     VM_CollectForMetadataAllocation op(loader_data,
1875                                        word_size,
1876                                        mdtype,
1877                                        gc_count,
1878                                        full_gc_count,
1879                                        GCCause::_metadata_GC_threshold);
1880     VMThread::execute(&op);
1881 
1882     // If GC was locked out, try again. Check before checking success because the
1883     // prologue could have succeeded and the GC still have been locked out.
1884     if (op.gc_locked()) {
1885       continue;
1886     }
1887 
1888     if (op.prologue_succeeded()) {
1889       return op.result();
1890     }
1891     loop_count++;
1892     if ((QueuedAllocationWarningCount > 0) &&
1893         (loop_count % QueuedAllocationWarningCount == 0)) {
1894       log_warning(gc, ergo)("satisfy_failed_metadata_allocation() retries %d times,"
1895                             " size=" SIZE_FORMAT, loop_count, word_size);
1896     }
1897   } while (true);  // Until a GC is done
1898 }
1899 
1900 // Metadebug methods
1901 
1902 void Metadebug::init_allocation_fail_alot_count() {
1903   if (MetadataAllocationFailALot) {
1904     _allocation_fail_alot_count =
1905       1+(long)((double)MetadataAllocationFailALotInterval*os::random()/(max_jint+1.0));
1906   }
1907 }
1908 
1909 #ifdef ASSERT
1910 bool Metadebug::test_metadata_failure() {
1911   if (MetadataAllocationFailALot &&
1912       Threads::is_vm_complete()) {
1913     if (_allocation_fail_alot_count > 0) {
1914       _allocation_fail_alot_count--;
1915     } else {
1916       log_trace(gc, metaspace, freelist)("Metadata allocation failing for MetadataAllocationFailALot");
1917       init_allocation_fail_alot_count();
1918       return true;
1919     }
1920   }
1921   return false;
1922 }
1923 #endif
1924 
1925 // ChunkManager methods
1926 size_t ChunkManager::free_chunks_total_words() {
1927   return _free_chunks_total;
1928 }
1929 
1930 size_t ChunkManager::free_chunks_total_bytes() {
1931   return free_chunks_total_words() * BytesPerWord;
1932 }
1933 
1934 // Update internal accounting after a chunk was added
1935 void ChunkManager::account_for_added_chunk(const Metachunk* c) {
1936   assert_lock_strong(SpaceManager::expand_lock());
1937   _free_chunks_count ++;
1938   _free_chunks_total += c->word_size();
1939 }
1940 
1941 // Update internal accounting after a chunk was removed
1942 void ChunkManager::account_for_removed_chunk(const Metachunk* c) {
1943   assert_lock_strong(SpaceManager::expand_lock());
1944   assert(_free_chunks_count >= 1,
1945     "ChunkManager::_free_chunks_count: about to go negative (" SIZE_FORMAT ").", _free_chunks_count);
1946   assert(_free_chunks_total >= c->word_size(),
1947     "ChunkManager::_free_chunks_total: about to go negative"
1948      "(now: " SIZE_FORMAT ", decrement value: " SIZE_FORMAT ").", _free_chunks_total, c->word_size());
1949   _free_chunks_count --;
1950   _free_chunks_total -= c->word_size();
1951 }
1952 
1953 size_t ChunkManager::free_chunks_count() {
1954 #ifdef ASSERT
1955   if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) {
1956     MutexLockerEx cl(SpaceManager::expand_lock(),
1957                      Mutex::_no_safepoint_check_flag);
1958     // This lock is only needed in debug because the verification
1959     // of the _free_chunks_totals walks the list of free chunks
1960     slow_locked_verify_free_chunks_count();
1961   }
1962 #endif
1963   return _free_chunks_count;
1964 }
1965 
1966 ChunkIndex ChunkManager::list_index(size_t size) {
1967   if (size_by_index(SpecializedIndex) == size) {
1968     return SpecializedIndex;
1969   }
1970   if (size_by_index(SmallIndex) == size) {
1971     return SmallIndex;
1972   }
1973   const size_t med_size = size_by_index(MediumIndex);
1974   if (med_size == size) {
1975     return MediumIndex;
1976   }
1977 
1978   assert(size > med_size, "Not a humongous chunk");
1979   return HumongousIndex;
1980 }
1981 
1982 size_t ChunkManager::size_by_index(ChunkIndex index) const {
1983   index_bounds_check(index);
1984   assert(index != HumongousIndex, "Do not call for humongous chunks.");
1985   return _free_chunks[index].size();
1986 }
1987 
1988 void ChunkManager::locked_verify_free_chunks_total() {
1989   assert_lock_strong(SpaceManager::expand_lock());
1990   assert(sum_free_chunks() == _free_chunks_total,
1991          "_free_chunks_total " SIZE_FORMAT " is not the"
1992          " same as sum " SIZE_FORMAT, _free_chunks_total,
1993          sum_free_chunks());
1994 }
1995 
1996 void ChunkManager::verify_free_chunks_total() {
1997   MutexLockerEx cl(SpaceManager::expand_lock(),
1998                      Mutex::_no_safepoint_check_flag);
1999   locked_verify_free_chunks_total();
2000 }
2001 
2002 void ChunkManager::locked_verify_free_chunks_count() {
2003   assert_lock_strong(SpaceManager::expand_lock());
2004   assert(sum_free_chunks_count() == _free_chunks_count,
2005          "_free_chunks_count " SIZE_FORMAT " is not the"
2006          " same as sum " SIZE_FORMAT, _free_chunks_count,
2007          sum_free_chunks_count());
2008 }
2009 
2010 void ChunkManager::verify_free_chunks_count() {
2011 #ifdef ASSERT
2012   MutexLockerEx cl(SpaceManager::expand_lock(),
2013                      Mutex::_no_safepoint_check_flag);
2014   locked_verify_free_chunks_count();
2015 #endif
2016 }
2017 
2018 void ChunkManager::verify() {
2019   MutexLockerEx cl(SpaceManager::expand_lock(),
2020                      Mutex::_no_safepoint_check_flag);
2021   locked_verify();
2022 }
2023 
2024 void ChunkManager::locked_verify() {
2025   locked_verify_free_chunks_count();
2026   locked_verify_free_chunks_total();
2027 }
2028 
2029 void ChunkManager::locked_print_free_chunks(outputStream* st) {
2030   assert_lock_strong(SpaceManager::expand_lock());
2031   st->print_cr("Free chunk total " SIZE_FORMAT "  count " SIZE_FORMAT,
2032                 _free_chunks_total, _free_chunks_count);
2033 }
2034 
2035 void ChunkManager::locked_print_sum_free_chunks(outputStream* st) {
2036   assert_lock_strong(SpaceManager::expand_lock());
2037   st->print_cr("Sum free chunk total " SIZE_FORMAT "  count " SIZE_FORMAT,
2038                 sum_free_chunks(), sum_free_chunks_count());
2039 }
2040 
2041 ChunkList* ChunkManager::free_chunks(ChunkIndex index) {
2042   assert(index == SpecializedIndex || index == SmallIndex || index == MediumIndex,
2043          "Bad index: %d", (int)index);
2044 
2045   return &_free_chunks[index];
2046 }
2047 
2048 // These methods that sum the free chunk lists are used in printing
2049 // methods that are used in product builds.
2050 size_t ChunkManager::sum_free_chunks() {
2051   assert_lock_strong(SpaceManager::expand_lock());
2052   size_t result = 0;
2053   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
2054     ChunkList* list = free_chunks(i);
2055 
2056     if (list == NULL) {
2057       continue;
2058     }
2059 
2060     result = result + list->count() * list->size();
2061   }
2062   result = result + humongous_dictionary()->total_size();
2063   return result;
2064 }
2065 
2066 size_t ChunkManager::sum_free_chunks_count() {
2067   assert_lock_strong(SpaceManager::expand_lock());
2068   size_t count = 0;
2069   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
2070     ChunkList* list = free_chunks(i);
2071     if (list == NULL) {
2072       continue;
2073     }
2074     count = count + list->count();
2075   }
2076   count = count + humongous_dictionary()->total_free_blocks();
2077   return count;
2078 }
2079 
2080 ChunkList* ChunkManager::find_free_chunks_list(size_t word_size) {
2081   ChunkIndex index = list_index(word_size);
2082   assert(index < HumongousIndex, "No humongous list");
2083   return free_chunks(index);
2084 }
2085 
2086 Metachunk* ChunkManager::free_chunks_get(size_t word_size) {
2087   assert_lock_strong(SpaceManager::expand_lock());
2088 
2089   slow_locked_verify();
2090 
2091   Metachunk* chunk = NULL;
2092   if (list_index(word_size) != HumongousIndex) {
2093     ChunkList* free_list = find_free_chunks_list(word_size);
2094     assert(free_list != NULL, "Sanity check");
2095 
2096     chunk = free_list->head();
2097 
2098     if (chunk == NULL) {
2099       return NULL;
2100     }
2101 
2102     // Remove the chunk as the head of the list.
2103     free_list->remove_chunk(chunk);
2104 
2105     log_trace(gc, metaspace, freelist)("ChunkManager::free_chunks_get: free_list " PTR_FORMAT " head " PTR_FORMAT " size " SIZE_FORMAT,
2106                                        p2i(free_list), p2i(chunk), chunk->word_size());
2107   } else {
2108     chunk = humongous_dictionary()->get_chunk(word_size);
2109 
2110     if (chunk == NULL) {
2111       return NULL;
2112     }
2113 
2114     log_debug(gc, metaspace, alloc)("Free list allocate humongous chunk size " SIZE_FORMAT " for requested size " SIZE_FORMAT " waste " SIZE_FORMAT,
2115                                     chunk->word_size(), word_size, chunk->word_size() - word_size);
2116   }
2117 
2118   // Chunk has been removed from the chunk manager; update counters.
2119   account_for_removed_chunk(chunk);
2120 
2121   // Remove it from the links to this freelist
2122   chunk->set_next(NULL);
2123   chunk->set_prev(NULL);
2124 
2125   // Chunk is no longer on any freelist. Setting to false make container_count_slow()
2126   // work.
2127   chunk->set_is_tagged_free(false);
2128   chunk->container()->inc_container_count();
2129 
2130   slow_locked_verify();
2131   return chunk;
2132 }
2133 
2134 Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) {
2135   assert_lock_strong(SpaceManager::expand_lock());
2136   slow_locked_verify();
2137 
2138   // Take from the beginning of the list
2139   Metachunk* chunk = free_chunks_get(word_size);
2140   if (chunk == NULL) {
2141     return NULL;
2142   }
2143 
2144   assert((word_size <= chunk->word_size()) ||
2145          (list_index(chunk->word_size()) == HumongousIndex),
2146          "Non-humongous variable sized chunk");
2147   LogTarget(Debug, gc, metaspace, freelist) lt;
2148   if (lt.is_enabled()) {
2149     size_t list_count;
2150     if (list_index(word_size) < HumongousIndex) {
2151       ChunkList* list = find_free_chunks_list(word_size);
2152       list_count = list->count();
2153     } else {
2154       list_count = humongous_dictionary()->total_count();
2155     }
2156     LogStream ls(lt);
2157     ls.print("ChunkManager::chunk_freelist_allocate: " PTR_FORMAT " chunk " PTR_FORMAT "  size " SIZE_FORMAT " count " SIZE_FORMAT " ",
2158              p2i(this), p2i(chunk), chunk->word_size(), list_count);
2159     ResourceMark rm;
2160     locked_print_free_chunks(&ls);
2161   }
2162 
2163   return chunk;
2164 }
2165 
2166 void ChunkManager::return_single_chunk(ChunkIndex index, Metachunk* chunk) {
2167   assert_lock_strong(SpaceManager::expand_lock());
2168   assert(chunk != NULL, "Expected chunk.");
2169   assert(chunk->container() != NULL, "Container should have been set.");
2170   assert(chunk->is_tagged_free() == false, "Chunk should be in use.");
2171   index_bounds_check(index);
2172 
2173   // Note: mangle *before* returning the chunk to the freelist or dictionary. It does not
2174   // matter for the freelist (non-humongous chunks), but the humongous chunk dictionary
2175   // keeps tree node pointers in the chunk payload area which mangle will overwrite.
2176   NOT_PRODUCT(chunk->mangle(badMetaWordVal);)
2177 
2178   if (index != HumongousIndex) {
2179     // Return non-humongous chunk to freelist.
2180     ChunkList* list = free_chunks(index);
2181     assert(list->size() == chunk->word_size(), "Wrong chunk type.");
2182     list->return_chunk_at_head(chunk);
2183     log_trace(gc, metaspace, freelist)("returned one %s chunk at " PTR_FORMAT " to freelist.",
2184         chunk_size_name(index), p2i(chunk));
2185   } else {
2186     // Return humongous chunk to dictionary.
2187     assert(chunk->word_size() > free_chunks(MediumIndex)->size(), "Wrong chunk type.");
2188     assert(chunk->word_size() % free_chunks(SpecializedIndex)->size() == 0,
2189            "Humongous chunk has wrong alignment.");
2190     _humongous_dictionary.return_chunk(chunk);
2191     log_trace(gc, metaspace, freelist)("returned one %s chunk at " PTR_FORMAT " (word size " SIZE_FORMAT ") to freelist.",
2192         chunk_size_name(index), p2i(chunk), chunk->word_size());
2193   }
2194   chunk->container()->dec_container_count();
2195   chunk->set_is_tagged_free(true);
2196 
2197   // Chunk has been added; update counters.
2198   account_for_added_chunk(chunk);
2199 
2200 }
2201 
2202 void ChunkManager::return_chunk_list(ChunkIndex index, Metachunk* chunks) {
2203   index_bounds_check(index);
2204   if (chunks == NULL) {
2205     return;
2206   }
2207   LogTarget(Trace, gc, metaspace, freelist) log;
2208   if (log.is_enabled()) { // tracing
2209     log.print("returning list of %s chunks...", chunk_size_name(index));
2210   }
2211   unsigned num_chunks_returned = 0;
2212   size_t size_chunks_returned = 0;
2213   Metachunk* cur = chunks;
2214   while (cur != NULL) {
2215     // Capture the next link before it is changed
2216     // by the call to return_chunk_at_head();
2217     Metachunk* next = cur->next();
2218     if (log.is_enabled()) { // tracing
2219       num_chunks_returned ++;
2220       size_chunks_returned += cur->word_size();
2221     }
2222     return_single_chunk(index, cur);
2223     cur = next;
2224   }
2225   if (log.is_enabled()) { // tracing
2226     log.print("returned %u %s chunks to freelist, total word size " SIZE_FORMAT ".",
2227         num_chunks_returned, chunk_size_name(index), size_chunks_returned);
2228     if (index != HumongousIndex) {
2229       log.print("updated freelist count: " SIZE_FORMAT ".", free_chunks(index)->size());
2230     } else {
2231       log.print("updated dictionary count " SIZE_FORMAT ".", _humongous_dictionary.total_count());
2232     }
2233   }
2234 }
2235 
2236 void ChunkManager::print_on(outputStream* out) const {
2237   _humongous_dictionary.report_statistics(out);
2238 }
2239 
2240 void ChunkManager::locked_get_statistics(ChunkManagerStatistics* stat) const {
2241   assert_lock_strong(SpaceManager::expand_lock());
2242   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
2243     stat->num_by_type[i] = num_free_chunks(i);
2244     stat->single_size_by_type[i] = size_by_index(i);
2245     stat->total_size_by_type[i] = size_free_chunks_in_bytes(i);
2246   }
2247   stat->num_humongous_chunks = num_free_chunks(HumongousIndex);
2248   stat->total_size_humongous_chunks = size_free_chunks_in_bytes(HumongousIndex);
2249 }
2250 
2251 void ChunkManager::get_statistics(ChunkManagerStatistics* stat) const {
2252   MutexLockerEx cl(SpaceManager::expand_lock(),
2253                    Mutex::_no_safepoint_check_flag);
2254   locked_get_statistics(stat);
2255 }
2256 
2257 void ChunkManager::print_statistics(const ChunkManagerStatistics* stat, outputStream* out, size_t scale) {
2258   size_t total = 0;
2259   assert(scale == 1 || scale == K || scale == M || scale == G, "Invalid scale");
2260 
2261   const char* unit = scale_unit(scale);
2262   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
2263     out->print("  " SIZE_FORMAT " %s (" SIZE_FORMAT " bytes) chunks, total ",
2264                    stat->num_by_type[i], chunk_size_name(i),
2265                    stat->single_size_by_type[i]);
2266     if (scale == 1) {
2267       out->print_cr(SIZE_FORMAT " bytes", stat->total_size_by_type[i]);
2268     } else {
2269       out->print_cr("%.2f%s", (float)stat->total_size_by_type[i] / scale, unit);
2270     }
2271 
2272     total += stat->total_size_by_type[i];
2273   }
2274 
2275 
2276   total += stat->total_size_humongous_chunks;
2277 
2278   if (scale == 1) {
2279     out->print_cr("  " SIZE_FORMAT " humongous chunks, total " SIZE_FORMAT " bytes",
2280     stat->num_humongous_chunks, stat->total_size_humongous_chunks);
2281 
2282     out->print_cr("  total size: " SIZE_FORMAT " bytes.", total);
2283   } else {
2284     out->print_cr("  " SIZE_FORMAT " humongous chunks, total %.2f%s",
2285     stat->num_humongous_chunks,
2286     (float)stat->total_size_humongous_chunks / scale, unit);
2287 
2288     out->print_cr("  total size: %.2f%s.", (float)total / scale, unit);
2289   }
2290 
2291 }
2292 
2293 void ChunkManager::print_all_chunkmanagers(outputStream* out, size_t scale) {
2294   assert(scale == 1 || scale == K || scale == M || scale == G, "Invalid scale");
2295 
2296   // Note: keep lock protection only to retrieving statistics; keep printing
2297   // out of lock protection
2298   ChunkManagerStatistics stat;
2299   out->print_cr("Chunkmanager (non-class):");
2300   const ChunkManager* const non_class_cm = Metaspace::chunk_manager_metadata();
2301   if (non_class_cm != NULL) {
2302     non_class_cm->get_statistics(&stat);
2303     ChunkManager::print_statistics(&stat, out, scale);
2304   } else {
2305     out->print_cr("unavailable.");
2306   }
2307   out->print_cr("Chunkmanager (class):");
2308   const ChunkManager* const class_cm = Metaspace::chunk_manager_class();
2309   if (class_cm != NULL) {
2310     class_cm->get_statistics(&stat);
2311     ChunkManager::print_statistics(&stat, out, scale);
2312   } else {
2313     out->print_cr("unavailable.");
2314   }
2315 }
2316 
2317 // SpaceManager methods
2318 
2319 size_t SpaceManager::adjust_initial_chunk_size(size_t requested, bool is_class_space) {
2320   size_t chunk_sizes[] = {
2321       specialized_chunk_size(is_class_space),
2322       small_chunk_size(is_class_space),
2323       medium_chunk_size(is_class_space)
2324   };
2325 
2326   // Adjust up to one of the fixed chunk sizes ...
2327   for (size_t i = 0; i < ARRAY_SIZE(chunk_sizes); i++) {
2328     if (requested <= chunk_sizes[i]) {
2329       return chunk_sizes[i];
2330     }
2331   }
2332 
2333   // ... or return the size as a humongous chunk.
2334   return requested;
2335 }
2336 
2337 size_t SpaceManager::adjust_initial_chunk_size(size_t requested) const {
2338   return adjust_initial_chunk_size(requested, is_class());
2339 }
2340 
2341 size_t SpaceManager::get_initial_chunk_size(Metaspace::MetaspaceType type) const {
2342   size_t requested;
2343 
2344   if (is_class()) {
2345     switch (type) {
2346     case Metaspace::BootMetaspaceType:       requested = Metaspace::first_class_chunk_word_size(); break;
2347     case Metaspace::AnonymousMetaspaceType:  requested = ClassSpecializedChunk; break;
2348     case Metaspace::ReflectionMetaspaceType: requested = ClassSpecializedChunk; break;
2349     default:                                 requested = ClassSmallChunk; break;
2350     }
2351   } else {
2352     switch (type) {
2353     case Metaspace::BootMetaspaceType:       requested = Metaspace::first_chunk_word_size(); break;
2354     case Metaspace::AnonymousMetaspaceType:  requested = SpecializedChunk; break;
2355     case Metaspace::ReflectionMetaspaceType: requested = SpecializedChunk; break;
2356     default:                                 requested = SmallChunk; break;
2357     }
2358   }
2359 
2360   // Adjust to one of the fixed chunk sizes (unless humongous)
2361   const size_t adjusted = adjust_initial_chunk_size(requested);
2362 
2363   assert(adjusted != 0, "Incorrect initial chunk size. Requested: "
2364          SIZE_FORMAT " adjusted: " SIZE_FORMAT, requested, adjusted);
2365 
2366   return adjusted;
2367 }
2368 
2369 size_t SpaceManager::sum_free_in_chunks_in_use() const {
2370   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
2371   size_t free = 0;
2372   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
2373     Metachunk* chunk = chunks_in_use(i);
2374     while (chunk != NULL) {
2375       free += chunk->free_word_size();
2376       chunk = chunk->next();
2377     }
2378   }
2379   return free;
2380 }
2381 
2382 size_t SpaceManager::sum_waste_in_chunks_in_use() const {
2383   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
2384   size_t result = 0;
2385   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
2386    result += sum_waste_in_chunks_in_use(i);
2387   }
2388 
2389   return result;
2390 }
2391 
2392 size_t SpaceManager::sum_waste_in_chunks_in_use(ChunkIndex index) const {
2393   size_t result = 0;
2394   Metachunk* chunk = chunks_in_use(index);
2395   // Count the free space in all the chunk but not the
2396   // current chunk from which allocations are still being done.
2397   while (chunk != NULL) {
2398     if (chunk != current_chunk()) {
2399       result += chunk->free_word_size();
2400     }
2401     chunk = chunk->next();
2402   }
2403   return result;
2404 }
2405 
2406 size_t SpaceManager::sum_capacity_in_chunks_in_use() const {
2407   // For CMS use "allocated_chunks_words()" which does not need the
2408   // Metaspace lock.  For the other collectors sum over the
2409   // lists.  Use both methods as a check that "allocated_chunks_words()"
2410   // is correct.  That is, sum_capacity_in_chunks() is too expensive
2411   // to use in the product and allocated_chunks_words() should be used
2412   // but allow for  checking that allocated_chunks_words() returns the same
2413   // value as sum_capacity_in_chunks_in_use() which is the definitive
2414   // answer.
2415   if (UseConcMarkSweepGC) {
2416     return allocated_chunks_words();
2417   } else {
2418     MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
2419     size_t sum = 0;
2420     for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
2421       Metachunk* chunk = chunks_in_use(i);
2422       while (chunk != NULL) {
2423         sum += chunk->word_size();
2424         chunk = chunk->next();
2425       }
2426     }
2427   return sum;
2428   }
2429 }
2430 
2431 size_t SpaceManager::sum_count_in_chunks_in_use() {
2432   size_t count = 0;
2433   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
2434     count = count + sum_count_in_chunks_in_use(i);
2435   }
2436 
2437   return count;
2438 }
2439 
2440 size_t SpaceManager::sum_count_in_chunks_in_use(ChunkIndex i) {
2441   size_t count = 0;
2442   Metachunk* chunk = chunks_in_use(i);
2443   while (chunk != NULL) {
2444     count++;
2445     chunk = chunk->next();
2446   }
2447   return count;
2448 }
2449 
2450 
2451 size_t SpaceManager::sum_used_in_chunks_in_use() const {
2452   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
2453   size_t used = 0;
2454   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
2455     Metachunk* chunk = chunks_in_use(i);
2456     while (chunk != NULL) {
2457       used += chunk->used_word_size();
2458       chunk = chunk->next();
2459     }
2460   }
2461   return used;
2462 }
2463 
2464 void SpaceManager::locked_print_chunks_in_use_on(outputStream* st) const {
2465 
2466   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
2467     Metachunk* chunk = chunks_in_use(i);
2468     st->print("SpaceManager: %s " PTR_FORMAT,
2469                  chunk_size_name(i), p2i(chunk));
2470     if (chunk != NULL) {
2471       st->print_cr(" free " SIZE_FORMAT,
2472                    chunk->free_word_size());
2473     } else {
2474       st->cr();
2475     }
2476   }
2477 
2478   chunk_manager()->locked_print_free_chunks(st);
2479   chunk_manager()->locked_print_sum_free_chunks(st);
2480 }
2481 
2482 size_t SpaceManager::calc_chunk_size(size_t word_size) {
2483 
2484   // Decide between a small chunk and a medium chunk.  Up to
2485   // _small_chunk_limit small chunks can be allocated.
2486   // After that a medium chunk is preferred.
2487   size_t chunk_word_size;
2488 
2489   // Special case for anonymous metadata space.
2490   // Anonymous metadata space is usually small, with majority within 1K - 2K range and
2491   // rarely about 4K (64-bits JVM).
2492   // Instead of jumping to SmallChunk after initial chunk exhausted, keeping allocation
2493   // from SpecializeChunk up to _anon_metadata_specialize_chunk_limit (4) reduces space waste
2494   // from 60+% to around 30%.
2495   if (_space_type == Metaspace::AnonymousMetaspaceType &&
2496       _mdtype == Metaspace::NonClassType &&
2497       sum_count_in_chunks_in_use(SpecializedIndex) < _anon_metadata_specialize_chunk_limit &&
2498       word_size + Metachunk::overhead() <= SpecializedChunk) {
2499     return SpecializedChunk;
2500   }
2501 
2502   if (chunks_in_use(MediumIndex) == NULL &&
2503       sum_count_in_chunks_in_use(SmallIndex) < _small_chunk_limit) {
2504     chunk_word_size = (size_t) small_chunk_size();
2505     if (word_size + Metachunk::overhead() > small_chunk_size()) {
2506       chunk_word_size = medium_chunk_size();
2507     }
2508   } else {
2509     chunk_word_size = medium_chunk_size();
2510   }
2511 
2512   // Might still need a humongous chunk.  Enforce
2513   // humongous allocations sizes to be aligned up to
2514   // the smallest chunk size.
2515   size_t if_humongous_sized_chunk =
2516     align_up(word_size + Metachunk::overhead(),
2517                   smallest_chunk_size());
2518   chunk_word_size =
2519     MAX2((size_t) chunk_word_size, if_humongous_sized_chunk);
2520 
2521   assert(!SpaceManager::is_humongous(word_size) ||
2522          chunk_word_size == if_humongous_sized_chunk,
2523          "Size calculation is wrong, word_size " SIZE_FORMAT
2524          " chunk_word_size " SIZE_FORMAT,
2525          word_size, chunk_word_size);
2526   Log(gc, metaspace, alloc) log;
2527   if (log.is_debug() && SpaceManager::is_humongous(word_size)) {
2528     log.debug("Metadata humongous allocation:");
2529     log.debug("  word_size " PTR_FORMAT, word_size);
2530     log.debug("  chunk_word_size " PTR_FORMAT, chunk_word_size);
2531     log.debug("    chunk overhead " PTR_FORMAT, Metachunk::overhead());
2532   }
2533   return chunk_word_size;
2534 }
2535 
2536 void SpaceManager::track_metaspace_memory_usage() {
2537   if (is_init_completed()) {
2538     if (is_class()) {
2539       MemoryService::track_compressed_class_memory_usage();
2540     }
2541     MemoryService::track_metaspace_memory_usage();
2542   }
2543 }
2544 
2545 MetaWord* SpaceManager::grow_and_allocate(size_t word_size) {
2546   assert(vs_list()->current_virtual_space() != NULL,
2547          "Should have been set");
2548   assert(current_chunk() == NULL ||
2549          current_chunk()->allocate(word_size) == NULL,
2550          "Don't need to expand");
2551   MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
2552 
2553   if (log_is_enabled(Trace, gc, metaspace, freelist)) {
2554     size_t words_left = 0;
2555     size_t words_used = 0;
2556     if (current_chunk() != NULL) {
2557       words_left = current_chunk()->free_word_size();
2558       words_used = current_chunk()->used_word_size();
2559     }
2560     log_trace(gc, metaspace, freelist)("SpaceManager::grow_and_allocate for " SIZE_FORMAT " words " SIZE_FORMAT " words used " SIZE_FORMAT " words left",
2561                                        word_size, words_used, words_left);
2562   }
2563 
2564   // Get another chunk
2565   size_t chunk_word_size = calc_chunk_size(word_size);
2566   Metachunk* next = get_new_chunk(chunk_word_size);
2567 
2568   MetaWord* mem = NULL;
2569 
2570   // If a chunk was available, add it to the in-use chunk list
2571   // and do an allocation from it.
2572   if (next != NULL) {
2573     // Add to this manager's list of chunks in use.
2574     add_chunk(next, false);
2575     mem = next->allocate(word_size);
2576   }
2577 
2578   // Track metaspace memory usage statistic.
2579   track_metaspace_memory_usage();
2580 
2581   return mem;
2582 }
2583 
2584 void SpaceManager::print_on(outputStream* st) const {
2585 
2586   for (ChunkIndex i = ZeroIndex;
2587        i < NumberOfInUseLists ;
2588        i = next_chunk_index(i) ) {
2589     st->print_cr("  chunks_in_use " PTR_FORMAT " chunk size " SIZE_FORMAT,
2590                  p2i(chunks_in_use(i)),
2591                  chunks_in_use(i) == NULL ? 0 : chunks_in_use(i)->word_size());
2592   }
2593   st->print_cr("    waste:  Small " SIZE_FORMAT " Medium " SIZE_FORMAT
2594                " Humongous " SIZE_FORMAT,
2595                sum_waste_in_chunks_in_use(SmallIndex),
2596                sum_waste_in_chunks_in_use(MediumIndex),
2597                sum_waste_in_chunks_in_use(HumongousIndex));
2598   // block free lists
2599   if (block_freelists() != NULL) {
2600     st->print_cr("total in block free lists " SIZE_FORMAT,
2601       block_freelists()->total_size());
2602   }
2603 }
2604 
2605 SpaceManager::SpaceManager(Metaspace::MetadataType mdtype,
2606                            Metaspace::MetaspaceType space_type,
2607                            Mutex* lock) :
2608   _mdtype(mdtype),
2609   _space_type(space_type),
2610   _allocated_blocks_words(0),
2611   _allocated_chunks_words(0),
2612   _allocated_chunks_count(0),
2613   _block_freelists(NULL),
2614   _lock(lock)
2615 {
2616   initialize();
2617 }
2618 
2619 void SpaceManager::inc_size_metrics(size_t words) {
2620   assert_lock_strong(SpaceManager::expand_lock());
2621   // Total of allocated Metachunks and allocated Metachunks count
2622   // for each SpaceManager
2623   _allocated_chunks_words = _allocated_chunks_words + words;
2624   _allocated_chunks_count++;
2625   // Global total of capacity in allocated Metachunks
2626   MetaspaceAux::inc_capacity(mdtype(), words);
2627   // Global total of allocated Metablocks.
2628   // used_words_slow() includes the overhead in each
2629   // Metachunk so include it in the used when the
2630   // Metachunk is first added (so only added once per
2631   // Metachunk).
2632   MetaspaceAux::inc_used(mdtype(), Metachunk::overhead());
2633 }
2634 
2635 void SpaceManager::inc_used_metrics(size_t words) {
2636   // Add to the per SpaceManager total
2637   Atomic::add(words, &_allocated_blocks_words);
2638   // Add to the global total
2639   MetaspaceAux::inc_used(mdtype(), words);
2640 }
2641 
2642 void SpaceManager::dec_total_from_size_metrics() {
2643   MetaspaceAux::dec_capacity(mdtype(), allocated_chunks_words());
2644   MetaspaceAux::dec_used(mdtype(), allocated_blocks_words());
2645   // Also deduct the overhead per Metachunk
2646   MetaspaceAux::dec_used(mdtype(), allocated_chunks_count() * Metachunk::overhead());
2647 }
2648 
2649 void SpaceManager::initialize() {
2650   Metadebug::init_allocation_fail_alot_count();
2651   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
2652     _chunks_in_use[i] = NULL;
2653   }
2654   _current_chunk = NULL;
2655   log_trace(gc, metaspace, freelist)("SpaceManager(): " PTR_FORMAT, p2i(this));
2656 }
2657 
2658 SpaceManager::~SpaceManager() {
2659   // This call this->_lock which can't be done while holding expand_lock()
2660   assert(sum_capacity_in_chunks_in_use() == allocated_chunks_words(),
2661          "sum_capacity_in_chunks_in_use() " SIZE_FORMAT
2662          " allocated_chunks_words() " SIZE_FORMAT,
2663          sum_capacity_in_chunks_in_use(), allocated_chunks_words());
2664 
2665   MutexLockerEx fcl(SpaceManager::expand_lock(),
2666                     Mutex::_no_safepoint_check_flag);
2667 
2668   chunk_manager()->slow_locked_verify();
2669 
2670   dec_total_from_size_metrics();
2671 
2672   Log(gc, metaspace, freelist) log;
2673   if (log.is_trace()) {
2674     log.trace("~SpaceManager(): " PTR_FORMAT, p2i(this));
2675     ResourceMark rm;
2676     LogStream ls(log.trace());
2677     locked_print_chunks_in_use_on(&ls);
2678     if (block_freelists() != NULL) {
2679       block_freelists()->print_on(&ls);
2680     }
2681   }
2682 
2683   // Add all the chunks in use by this space manager
2684   // to the global list of free chunks.
2685 
2686   // Follow each list of chunks-in-use and add them to the
2687   // free lists.  Each list is NULL terminated.
2688 
2689   for (ChunkIndex i = ZeroIndex; i <= HumongousIndex; i = next_chunk_index(i)) {
2690     Metachunk* chunks = chunks_in_use(i);
2691     chunk_manager()->return_chunk_list(i, chunks);
2692     set_chunks_in_use(i, NULL);
2693   }
2694 
2695   chunk_manager()->slow_locked_verify();
2696 
2697   if (_block_freelists != NULL) {
2698     delete _block_freelists;
2699   }
2700 }
2701 
2702 void SpaceManager::deallocate(MetaWord* p, size_t word_size) {
2703   assert_lock_strong(_lock);
2704   // Allocations and deallocations are in raw_word_size
2705   size_t raw_word_size = get_allocation_word_size(word_size);
2706   // Lazily create a block_freelist
2707   if (block_freelists() == NULL) {
2708     _block_freelists = new BlockFreelist();
2709   }
2710   block_freelists()->return_block(p, raw_word_size);
2711 }
2712 
2713 // Adds a chunk to the list of chunks in use.
2714 void SpaceManager::add_chunk(Metachunk* new_chunk, bool make_current) {
2715 
2716   assert(new_chunk != NULL, "Should not be NULL");
2717   assert(new_chunk->next() == NULL, "Should not be on a list");
2718 
2719   new_chunk->reset_empty();
2720 
2721   // Find the correct list and and set the current
2722   // chunk for that list.
2723   ChunkIndex index = chunk_manager()->list_index(new_chunk->word_size());
2724 
2725   if (index != HumongousIndex) {
2726     retire_current_chunk();
2727     set_current_chunk(new_chunk);
2728     new_chunk->set_next(chunks_in_use(index));
2729     set_chunks_in_use(index, new_chunk);
2730   } else {
2731     // For null class loader data and DumpSharedSpaces, the first chunk isn't
2732     // small, so small will be null.  Link this first chunk as the current
2733     // chunk.
2734     if (make_current) {
2735       // Set as the current chunk but otherwise treat as a humongous chunk.
2736       set_current_chunk(new_chunk);
2737     }
2738     // Link at head.  The _current_chunk only points to a humongous chunk for
2739     // the null class loader metaspace (class and data virtual space managers)
2740     // any humongous chunks so will not point to the tail
2741     // of the humongous chunks list.
2742     new_chunk->set_next(chunks_in_use(HumongousIndex));
2743     set_chunks_in_use(HumongousIndex, new_chunk);
2744 
2745     assert(new_chunk->word_size() > medium_chunk_size(), "List inconsistency");
2746   }
2747 
2748   // Add to the running sum of capacity
2749   inc_size_metrics(new_chunk->word_size());
2750 
2751   assert(new_chunk->is_empty(), "Not ready for reuse");
2752   Log(gc, metaspace, freelist) log;
2753   if (log.is_trace()) {
2754     log.trace("SpaceManager::add_chunk: " SIZE_FORMAT ") ", sum_count_in_chunks_in_use());
2755     ResourceMark rm;
2756     LogStream ls(log.trace());
2757     new_chunk->print_on(&ls);
2758     chunk_manager()->locked_print_free_chunks(&ls);
2759   }
2760 }
2761 
2762 void SpaceManager::retire_current_chunk() {
2763   if (current_chunk() != NULL) {
2764     size_t remaining_words = current_chunk()->free_word_size();
2765     if (remaining_words >= BlockFreelist::min_dictionary_size()) {
2766       MetaWord* ptr = current_chunk()->allocate(remaining_words);
2767       deallocate(ptr, remaining_words);
2768       inc_used_metrics(remaining_words);
2769     }
2770   }
2771 }
2772 
2773 Metachunk* SpaceManager::get_new_chunk(size_t chunk_word_size) {
2774   // Get a chunk from the chunk freelist
2775   Metachunk* next = chunk_manager()->chunk_freelist_allocate(chunk_word_size);
2776 
2777   if (next == NULL) {
2778     next = vs_list()->get_new_chunk(chunk_word_size,
2779                                     medium_chunk_bunch());
2780   }
2781 
2782   Log(gc, metaspace, alloc) log;
2783   if (log.is_debug() && next != NULL &&
2784       SpaceManager::is_humongous(next->word_size())) {
2785     log.debug("  new humongous chunk word size " PTR_FORMAT, next->word_size());
2786   }
2787 
2788   return next;
2789 }
2790 
2791 /*
2792  * The policy is to allocate up to _small_chunk_limit small chunks
2793  * after which only medium chunks are allocated.  This is done to
2794  * reduce fragmentation.  In some cases, this can result in a lot
2795  * of small chunks being allocated to the point where it's not
2796  * possible to expand.  If this happens, there may be no medium chunks
2797  * available and OOME would be thrown.  Instead of doing that,
2798  * if the allocation request size fits in a small chunk, an attempt
2799  * will be made to allocate a small chunk.
2800  */
2801 MetaWord* SpaceManager::get_small_chunk_and_allocate(size_t word_size) {
2802   size_t raw_word_size = get_allocation_word_size(word_size);
2803 
2804   if (raw_word_size + Metachunk::overhead() > small_chunk_size()) {
2805     return NULL;
2806   }
2807 
2808   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
2809   MutexLockerEx cl1(expand_lock(), Mutex::_no_safepoint_check_flag);
2810 
2811   Metachunk* chunk = chunk_manager()->chunk_freelist_allocate(small_chunk_size());
2812 
2813   MetaWord* mem = NULL;
2814 
2815   if (chunk != NULL) {
2816     // Add chunk to the in-use chunk list and do an allocation from it.
2817     // Add to this manager's list of chunks in use.
2818     add_chunk(chunk, false);
2819     mem = chunk->allocate(raw_word_size);
2820 
2821     inc_used_metrics(raw_word_size);
2822 
2823     // Track metaspace memory usage statistic.
2824     track_metaspace_memory_usage();
2825   }
2826 
2827   return mem;
2828 }
2829 
2830 MetaWord* SpaceManager::allocate(size_t word_size) {
2831   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
2832   size_t raw_word_size = get_allocation_word_size(word_size);
2833   BlockFreelist* fl =  block_freelists();
2834   MetaWord* p = NULL;
2835   // Allocation from the dictionary is expensive in the sense that
2836   // the dictionary has to be searched for a size.  Don't allocate
2837   // from the dictionary until it starts to get fat.  Is this
2838   // a reasonable policy?  Maybe an skinny dictionary is fast enough
2839   // for allocations.  Do some profiling.  JJJ
2840   if (fl != NULL && fl->total_size() > allocation_from_dictionary_limit) {
2841     p = fl->get_block(raw_word_size);
2842   }
2843   if (p == NULL) {
2844     p = allocate_work(raw_word_size);
2845   }
2846 
2847   return p;
2848 }
2849 
2850 // Returns the address of spaced allocated for "word_size".
2851 // This methods does not know about blocks (Metablocks)
2852 MetaWord* SpaceManager::allocate_work(size_t word_size) {
2853   assert_lock_strong(_lock);
2854 #ifdef ASSERT
2855   if (Metadebug::test_metadata_failure()) {
2856     return NULL;
2857   }
2858 #endif
2859   // Is there space in the current chunk?
2860   MetaWord* result = NULL;
2861 
2862   if (current_chunk() != NULL) {
2863     result = current_chunk()->allocate(word_size);
2864   }
2865 
2866   if (result == NULL) {
2867     result = grow_and_allocate(word_size);
2868   }
2869 
2870   if (result != NULL) {
2871     inc_used_metrics(word_size);
2872     assert(result != (MetaWord*) chunks_in_use(MediumIndex),
2873            "Head of the list is being allocated");
2874   }
2875 
2876   return result;
2877 }
2878 
2879 void SpaceManager::verify() {
2880   // If there are blocks in the dictionary, then
2881   // verification of chunks does not work since
2882   // being in the dictionary alters a chunk.
2883   if (block_freelists() != NULL && block_freelists()->total_size() == 0) {
2884     for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
2885       Metachunk* curr = chunks_in_use(i);
2886       while (curr != NULL) {
2887         curr->verify();
2888         verify_chunk_size(curr);
2889         curr = curr->next();
2890       }
2891     }
2892   }
2893 }
2894 
2895 void SpaceManager::verify_chunk_size(Metachunk* chunk) {
2896   assert(is_humongous(chunk->word_size()) ||
2897          chunk->word_size() == medium_chunk_size() ||
2898          chunk->word_size() == small_chunk_size() ||
2899          chunk->word_size() == specialized_chunk_size(),
2900          "Chunk size is wrong");
2901   return;
2902 }
2903 
2904 #ifdef ASSERT
2905 void SpaceManager::verify_allocated_blocks_words() {
2906   // Verification is only guaranteed at a safepoint.
2907   assert(SafepointSynchronize::is_at_safepoint() || !Universe::is_fully_initialized(),
2908     "Verification can fail if the applications is running");
2909   assert(allocated_blocks_words() == sum_used_in_chunks_in_use(),
2910          "allocation total is not consistent " SIZE_FORMAT
2911          " vs " SIZE_FORMAT,
2912          allocated_blocks_words(), sum_used_in_chunks_in_use());
2913 }
2914 
2915 #endif
2916 
2917 void SpaceManager::dump(outputStream* const out) const {
2918   size_t curr_total = 0;
2919   size_t waste = 0;
2920   uint i = 0;
2921   size_t used = 0;
2922   size_t capacity = 0;
2923 
2924   // Add up statistics for all chunks in this SpaceManager.
2925   for (ChunkIndex index = ZeroIndex;
2926        index < NumberOfInUseLists;
2927        index = next_chunk_index(index)) {
2928     for (Metachunk* curr = chunks_in_use(index);
2929          curr != NULL;
2930          curr = curr->next()) {
2931       out->print("%d) ", i++);
2932       curr->print_on(out);
2933       curr_total += curr->word_size();
2934       used += curr->used_word_size();
2935       capacity += curr->word_size();
2936       waste += curr->free_word_size() + curr->overhead();;
2937     }
2938   }
2939 
2940   if (log_is_enabled(Trace, gc, metaspace, freelist)) {
2941     if (block_freelists() != NULL) block_freelists()->print_on(out);
2942   }
2943 
2944   size_t free = current_chunk() == NULL ? 0 : current_chunk()->free_word_size();
2945   // Free space isn't wasted.
2946   waste -= free;
2947 
2948   out->print_cr("total of all chunks "  SIZE_FORMAT " used " SIZE_FORMAT
2949                 " free " SIZE_FORMAT " capacity " SIZE_FORMAT
2950                 " waste " SIZE_FORMAT, curr_total, used, free, capacity, waste);
2951 }
2952 
2953 // MetaspaceAux
2954 
2955 
2956 size_t MetaspaceAux::_capacity_words[] = {0, 0};
2957 volatile size_t MetaspaceAux::_used_words[] = {0, 0};
2958 
2959 size_t MetaspaceAux::free_bytes(Metaspace::MetadataType mdtype) {
2960   VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
2961   return list == NULL ? 0 : list->free_bytes();
2962 }
2963 
2964 size_t MetaspaceAux::free_bytes() {
2965   return free_bytes(Metaspace::ClassType) + free_bytes(Metaspace::NonClassType);
2966 }
2967 
2968 void MetaspaceAux::dec_capacity(Metaspace::MetadataType mdtype, size_t words) {
2969   assert_lock_strong(SpaceManager::expand_lock());
2970   assert(words <= capacity_words(mdtype),
2971          "About to decrement below 0: words " SIZE_FORMAT
2972          " is greater than _capacity_words[%u] " SIZE_FORMAT,
2973          words, mdtype, capacity_words(mdtype));
2974   _capacity_words[mdtype] -= words;
2975 }
2976 
2977 void MetaspaceAux::inc_capacity(Metaspace::MetadataType mdtype, size_t words) {
2978   assert_lock_strong(SpaceManager::expand_lock());
2979   // Needs to be atomic
2980   _capacity_words[mdtype] += words;
2981 }
2982 
2983 void MetaspaceAux::dec_used(Metaspace::MetadataType mdtype, size_t words) {
2984   assert(words <= used_words(mdtype),
2985          "About to decrement below 0: words " SIZE_FORMAT
2986          " is greater than _used_words[%u] " SIZE_FORMAT,
2987          words, mdtype, used_words(mdtype));
2988   // For CMS deallocation of the Metaspaces occurs during the
2989   // sweep which is a concurrent phase.  Protection by the expand_lock()
2990   // is not enough since allocation is on a per Metaspace basis
2991   // and protected by the Metaspace lock.
2992   Atomic::sub(words, &_used_words[mdtype]);
2993 }
2994 
2995 void MetaspaceAux::inc_used(Metaspace::MetadataType mdtype, size_t words) {
2996   // _used_words tracks allocations for
2997   // each piece of metadata.  Those allocations are
2998   // generally done concurrently by different application
2999   // threads so must be done atomically.
3000   Atomic::add(words, &_used_words[mdtype]);
3001 }
3002 
3003 size_t MetaspaceAux::used_bytes_slow(Metaspace::MetadataType mdtype) {
3004   size_t used = 0;
3005   ClassLoaderDataGraphMetaspaceIterator iter;
3006   while (iter.repeat()) {
3007     Metaspace* msp = iter.get_next();
3008     // Sum allocated_blocks_words for each metaspace
3009     if (msp != NULL) {
3010       used += msp->used_words_slow(mdtype);
3011     }
3012   }
3013   return used * BytesPerWord;
3014 }
3015 
3016 size_t MetaspaceAux::free_bytes_slow(Metaspace::MetadataType mdtype) {
3017   size_t free = 0;
3018   ClassLoaderDataGraphMetaspaceIterator iter;
3019   while (iter.repeat()) {
3020     Metaspace* msp = iter.get_next();
3021     if (msp != NULL) {
3022       free += msp->free_words_slow(mdtype);
3023     }
3024   }
3025   return free * BytesPerWord;
3026 }
3027 
3028 size_t MetaspaceAux::capacity_bytes_slow(Metaspace::MetadataType mdtype) {
3029   if ((mdtype == Metaspace::ClassType) && !Metaspace::using_class_space()) {
3030     return 0;
3031   }
3032   // Don't count the space in the freelists.  That space will be
3033   // added to the capacity calculation as needed.
3034   size_t capacity = 0;
3035   ClassLoaderDataGraphMetaspaceIterator iter;
3036   while (iter.repeat()) {
3037     Metaspace* msp = iter.get_next();
3038     if (msp != NULL) {
3039       capacity += msp->capacity_words_slow(mdtype);
3040     }
3041   }
3042   return capacity * BytesPerWord;
3043 }
3044 
3045 size_t MetaspaceAux::capacity_bytes_slow() {
3046 #ifdef PRODUCT
3047   // Use capacity_bytes() in PRODUCT instead of this function.
3048   guarantee(false, "Should not call capacity_bytes_slow() in the PRODUCT");
3049 #endif
3050   size_t class_capacity = capacity_bytes_slow(Metaspace::ClassType);
3051   size_t non_class_capacity = capacity_bytes_slow(Metaspace::NonClassType);
3052   assert(capacity_bytes() == class_capacity + non_class_capacity,
3053          "bad accounting: capacity_bytes() " SIZE_FORMAT
3054          " class_capacity + non_class_capacity " SIZE_FORMAT
3055          " class_capacity " SIZE_FORMAT " non_class_capacity " SIZE_FORMAT,
3056          capacity_bytes(), class_capacity + non_class_capacity,
3057          class_capacity, non_class_capacity);
3058 
3059   return class_capacity + non_class_capacity;
3060 }
3061 
3062 size_t MetaspaceAux::reserved_bytes(Metaspace::MetadataType mdtype) {
3063   VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
3064   return list == NULL ? 0 : list->reserved_bytes();
3065 }
3066 
3067 size_t MetaspaceAux::committed_bytes(Metaspace::MetadataType mdtype) {
3068   VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
3069   return list == NULL ? 0 : list->committed_bytes();
3070 }
3071 
3072 size_t MetaspaceAux::min_chunk_size_words() { return Metaspace::first_chunk_word_size(); }
3073 
3074 size_t MetaspaceAux::free_chunks_total_words(Metaspace::MetadataType mdtype) {
3075   ChunkManager* chunk_manager = Metaspace::get_chunk_manager(mdtype);
3076   if (chunk_manager == NULL) {
3077     return 0;
3078   }
3079   chunk_manager->slow_verify();
3080   return chunk_manager->free_chunks_total_words();
3081 }
3082 
3083 size_t MetaspaceAux::free_chunks_total_bytes(Metaspace::MetadataType mdtype) {
3084   return free_chunks_total_words(mdtype) * BytesPerWord;
3085 }
3086 
3087 size_t MetaspaceAux::free_chunks_total_words() {
3088   return free_chunks_total_words(Metaspace::ClassType) +
3089          free_chunks_total_words(Metaspace::NonClassType);
3090 }
3091 
3092 size_t MetaspaceAux::free_chunks_total_bytes() {
3093   return free_chunks_total_words() * BytesPerWord;
3094 }
3095 
3096 bool MetaspaceAux::has_chunk_free_list(Metaspace::MetadataType mdtype) {
3097   return Metaspace::get_chunk_manager(mdtype) != NULL;
3098 }
3099 
3100 MetaspaceChunkFreeListSummary MetaspaceAux::chunk_free_list_summary(Metaspace::MetadataType mdtype) {
3101   if (!has_chunk_free_list(mdtype)) {
3102     return MetaspaceChunkFreeListSummary();
3103   }
3104 
3105   const ChunkManager* cm = Metaspace::get_chunk_manager(mdtype);
3106   return cm->chunk_free_list_summary();
3107 }
3108 
3109 void MetaspaceAux::print_metaspace_change(size_t prev_metadata_used) {
3110   log_info(gc, metaspace)("Metaspace: "  SIZE_FORMAT "K->" SIZE_FORMAT "K("  SIZE_FORMAT "K)",
3111                           prev_metadata_used/K, used_bytes()/K, reserved_bytes()/K);
3112 }
3113 
3114 void MetaspaceAux::print_on(outputStream* out) {
3115   Metaspace::MetadataType nct = Metaspace::NonClassType;
3116 
3117   out->print_cr(" Metaspace       "
3118                 "used "      SIZE_FORMAT "K, "
3119                 "capacity "  SIZE_FORMAT "K, "
3120                 "committed " SIZE_FORMAT "K, "
3121                 "reserved "  SIZE_FORMAT "K",
3122                 used_bytes()/K,
3123                 capacity_bytes()/K,
3124                 committed_bytes()/K,
3125                 reserved_bytes()/K);
3126 
3127   if (Metaspace::using_class_space()) {
3128     Metaspace::MetadataType ct = Metaspace::ClassType;
3129     out->print_cr("  class space    "
3130                   "used "      SIZE_FORMAT "K, "
3131                   "capacity "  SIZE_FORMAT "K, "
3132                   "committed " SIZE_FORMAT "K, "
3133                   "reserved "  SIZE_FORMAT "K",
3134                   used_bytes(ct)/K,
3135                   capacity_bytes(ct)/K,
3136                   committed_bytes(ct)/K,
3137                   reserved_bytes(ct)/K);
3138   }
3139 }
3140 
3141 // Print information for class space and data space separately.
3142 // This is almost the same as above.
3143 void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) {
3144   size_t free_chunks_capacity_bytes = free_chunks_total_bytes(mdtype);
3145   size_t capacity_bytes = capacity_bytes_slow(mdtype);
3146   size_t used_bytes = used_bytes_slow(mdtype);
3147   size_t free_bytes = free_bytes_slow(mdtype);
3148   size_t used_and_free = used_bytes + free_bytes +
3149                            free_chunks_capacity_bytes;
3150   out->print_cr("  Chunk accounting: (used in chunks " SIZE_FORMAT
3151              "K + unused in chunks " SIZE_FORMAT "K  + "
3152              " capacity in free chunks " SIZE_FORMAT "K) = " SIZE_FORMAT
3153              "K  capacity in allocated chunks " SIZE_FORMAT "K",
3154              used_bytes / K,
3155              free_bytes / K,
3156              free_chunks_capacity_bytes / K,
3157              used_and_free / K,
3158              capacity_bytes / K);
3159   // Accounting can only be correct if we got the values during a safepoint
3160   assert(!SafepointSynchronize::is_at_safepoint() || used_and_free == capacity_bytes, "Accounting is wrong");
3161 }
3162 
3163 // Print total fragmentation for class metaspaces
3164 void MetaspaceAux::print_class_waste(outputStream* out) {
3165   assert(Metaspace::using_class_space(), "class metaspace not used");
3166   size_t cls_specialized_waste = 0, cls_small_waste = 0, cls_medium_waste = 0;
3167   size_t cls_specialized_count = 0, cls_small_count = 0, cls_medium_count = 0, cls_humongous_count = 0;
3168   ClassLoaderDataGraphMetaspaceIterator iter;
3169   while (iter.repeat()) {
3170     Metaspace* msp = iter.get_next();
3171     if (msp != NULL) {
3172       cls_specialized_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
3173       cls_specialized_count += msp->class_vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
3174       cls_small_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SmallIndex);
3175       cls_small_count += msp->class_vsm()->sum_count_in_chunks_in_use(SmallIndex);
3176       cls_medium_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(MediumIndex);
3177       cls_medium_count += msp->class_vsm()->sum_count_in_chunks_in_use(MediumIndex);
3178       cls_humongous_count += msp->class_vsm()->sum_count_in_chunks_in_use(HumongousIndex);
3179     }
3180   }
3181   out->print_cr(" class: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
3182                 SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
3183                 SIZE_FORMAT " medium(s) " SIZE_FORMAT ", "
3184                 "large count " SIZE_FORMAT,
3185                 cls_specialized_count, cls_specialized_waste,
3186                 cls_small_count, cls_small_waste,
3187                 cls_medium_count, cls_medium_waste, cls_humongous_count);
3188 }
3189 
3190 // Print total fragmentation for data and class metaspaces separately
3191 void MetaspaceAux::print_waste(outputStream* out) {
3192   size_t specialized_waste = 0, small_waste = 0, medium_waste = 0;
3193   size_t specialized_count = 0, small_count = 0, medium_count = 0, humongous_count = 0;
3194 
3195   ClassLoaderDataGraphMetaspaceIterator iter;
3196   while (iter.repeat()) {
3197     Metaspace* msp = iter.get_next();
3198     if (msp != NULL) {
3199       specialized_waste += msp->vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
3200       specialized_count += msp->vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
3201       small_waste += msp->vsm()->sum_waste_in_chunks_in_use(SmallIndex);
3202       small_count += msp->vsm()->sum_count_in_chunks_in_use(SmallIndex);
3203       medium_waste += msp->vsm()->sum_waste_in_chunks_in_use(MediumIndex);
3204       medium_count += msp->vsm()->sum_count_in_chunks_in_use(MediumIndex);
3205       humongous_count += msp->vsm()->sum_count_in_chunks_in_use(HumongousIndex);
3206     }
3207   }
3208   out->print_cr("Total fragmentation waste (words) doesn't count free space");
3209   out->print_cr("  data: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
3210                         SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
3211                         SIZE_FORMAT " medium(s) " SIZE_FORMAT ", "
3212                         "large count " SIZE_FORMAT,
3213              specialized_count, specialized_waste, small_count,
3214              small_waste, medium_count, medium_waste, humongous_count);
3215   if (Metaspace::using_class_space()) {
3216     print_class_waste(out);
3217   }
3218 }
3219 
3220 class MetadataStats VALUE_OBJ_CLASS_SPEC {
3221 private:
3222   size_t _capacity;
3223   size_t _used;
3224   size_t _free;
3225   size_t _waste;
3226 
3227 public:
3228   MetadataStats() : _capacity(0), _used(0), _free(0), _waste(0) { }
3229   MetadataStats(size_t capacity, size_t used, size_t free, size_t waste)
3230   : _capacity(capacity), _used(used), _free(free), _waste(waste) { }
3231 
3232   void add(const MetadataStats& stats) {
3233     _capacity += stats.capacity();
3234     _used += stats.used();
3235     _free += stats.free();
3236     _waste += stats.waste();
3237   }
3238 
3239   size_t capacity() const { return _capacity; }
3240   size_t used() const     { return _used; }
3241   size_t free() const     { return _free; }
3242   size_t waste() const    { return _waste; }
3243 
3244   void print_on(outputStream* out, size_t scale) const;
3245 };
3246 
3247 
3248 void MetadataStats::print_on(outputStream* out, size_t scale) const {
3249   const char* unit = scale_unit(scale);
3250   out->print_cr("capacity=%10.2f%s used=%10.2f%s free=%10.2f%s waste=%10.2f%s",
3251     (float)capacity() / scale, unit,
3252     (float)used() / scale, unit,
3253     (float)free() / scale, unit,
3254     (float)waste() / scale, unit);
3255 }
3256 
3257 class PrintCLDMetaspaceInfoClosure : public CLDClosure {
3258 private:
3259   outputStream*  _out;
3260   size_t         _scale;
3261 
3262   size_t         _total_count;
3263   MetadataStats  _total_metadata;
3264   MetadataStats  _total_class;
3265 
3266   size_t         _total_anon_count;
3267   MetadataStats  _total_anon_metadata;
3268   MetadataStats  _total_anon_class;
3269 
3270 public:
3271   PrintCLDMetaspaceInfoClosure(outputStream* out, size_t scale = K)
3272   : _out(out), _scale(scale), _total_count(0), _total_anon_count(0) { }
3273 
3274   ~PrintCLDMetaspaceInfoClosure() {
3275     print_summary();
3276   }
3277 
3278   void do_cld(ClassLoaderData* cld) {
3279     assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
3280 
3281     if (cld->is_unloading()) return;
3282     Metaspace* msp = cld->metaspace_or_null();
3283     if (msp == NULL) {
3284       return;
3285     }
3286 
3287     bool anonymous = false;
3288     if (cld->is_anonymous()) {
3289       _out->print_cr("ClassLoader: for anonymous class");
3290       anonymous = true;
3291     } else {
3292       ResourceMark rm;
3293       _out->print_cr("ClassLoader: %s", cld->loader_name());
3294     }
3295 
3296     print_metaspace(msp, anonymous);
3297     _out->cr();
3298   }
3299 
3300 private:
3301   void print_metaspace(Metaspace* msp, bool anonymous);
3302   void print_summary() const;
3303 };
3304 
3305 void PrintCLDMetaspaceInfoClosure::print_metaspace(Metaspace* msp, bool anonymous){
3306   assert(msp != NULL, "Sanity");
3307   SpaceManager* vsm = msp->vsm();
3308   const char* unit = scale_unit(_scale);
3309 
3310   size_t capacity = vsm->sum_capacity_in_chunks_in_use() * BytesPerWord;
3311   size_t used = vsm->sum_used_in_chunks_in_use() * BytesPerWord;
3312   size_t free = vsm->sum_free_in_chunks_in_use() * BytesPerWord;
3313   size_t waste = vsm->sum_waste_in_chunks_in_use() * BytesPerWord;
3314 
3315   _total_count ++;
3316   MetadataStats metadata_stats(capacity, used, free, waste);
3317   _total_metadata.add(metadata_stats);
3318 
3319   if (anonymous) {
3320     _total_anon_count ++;
3321     _total_anon_metadata.add(metadata_stats);
3322   }
3323 
3324   _out->print("  Metadata   ");
3325   metadata_stats.print_on(_out, _scale);
3326 
3327   if (Metaspace::using_class_space()) {
3328     vsm = msp->class_vsm();
3329 
3330     capacity = vsm->sum_capacity_in_chunks_in_use() * BytesPerWord;
3331     used = vsm->sum_used_in_chunks_in_use() * BytesPerWord;
3332     free = vsm->sum_free_in_chunks_in_use() * BytesPerWord;
3333     waste = vsm->sum_waste_in_chunks_in_use() * BytesPerWord;
3334 
3335     MetadataStats class_stats(capacity, used, free, waste);
3336     _total_class.add(class_stats);
3337 
3338     if (anonymous) {
3339       _total_anon_class.add(class_stats);
3340     }
3341 
3342     _out->print("  Class data ");
3343     class_stats.print_on(_out, _scale);
3344   }
3345 }
3346 
3347 void PrintCLDMetaspaceInfoClosure::print_summary() const {
3348   const char* unit = scale_unit(_scale);
3349   _out->cr();
3350   _out->print_cr("Summary:");
3351 
3352   MetadataStats total;
3353   total.add(_total_metadata);
3354   total.add(_total_class);
3355 
3356   _out->print("  Total class loaders=" SIZE_FORMAT_W(6) " ", _total_count);
3357   total.print_on(_out, _scale);
3358 
3359   _out->print("                    Metadata ");
3360   _total_metadata.print_on(_out, _scale);
3361 
3362   if (Metaspace::using_class_space()) {
3363     _out->print("                  Class data ");
3364     _total_class.print_on(_out, _scale);
3365   }
3366   _out->cr();
3367 
3368   MetadataStats total_anon;
3369   total_anon.add(_total_anon_metadata);
3370   total_anon.add(_total_anon_class);
3371 
3372   _out->print("For anonymous classes=" SIZE_FORMAT_W(6) " ", _total_anon_count);
3373   total_anon.print_on(_out, _scale);
3374 
3375   _out->print("                    Metadata ");
3376   _total_anon_metadata.print_on(_out, _scale);
3377 
3378   if (Metaspace::using_class_space()) {
3379     _out->print("                  Class data ");
3380     _total_anon_class.print_on(_out, _scale);
3381   }
3382 }
3383 
3384 void MetaspaceAux::print_metadata_for_nmt(outputStream* out, size_t scale) {
3385   const char* unit = scale_unit(scale);
3386   out->print_cr("Metaspaces:");
3387   out->print_cr("  Metadata space: reserved=" SIZE_FORMAT_W(10) "%s committed=" SIZE_FORMAT_W(10) "%s",
3388     reserved_bytes(Metaspace::NonClassType) / scale, unit,
3389     committed_bytes(Metaspace::NonClassType) / scale, unit);
3390   if (Metaspace::using_class_space()) {
3391     out->print_cr("  Class    space: reserved=" SIZE_FORMAT_W(10) "%s committed=" SIZE_FORMAT_W(10) "%s",
3392     reserved_bytes(Metaspace::ClassType) / scale, unit,
3393     committed_bytes(Metaspace::ClassType) / scale, unit);
3394   }
3395 
3396   out->cr();
3397   ChunkManager::print_all_chunkmanagers(out, scale);
3398 
3399   out->cr();
3400   out->print_cr("Per-classloader metadata:");
3401   out->cr();
3402 
3403   PrintCLDMetaspaceInfoClosure cl(out, scale);
3404   ClassLoaderDataGraph::cld_do(&cl);
3405 }
3406 
3407 
3408 // Dump global metaspace things from the end of ClassLoaderDataGraph
3409 void MetaspaceAux::dump(outputStream* out) {
3410   out->print_cr("All Metaspace:");
3411   out->print("data space: "); print_on(out, Metaspace::NonClassType);
3412   out->print("class space: "); print_on(out, Metaspace::ClassType);
3413   print_waste(out);
3414 }
3415 
3416 // Prints an ASCII representation of the given space.
3417 void MetaspaceAux::print_metaspace_map(outputStream* out, Metaspace::MetadataType mdtype) {
3418   MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
3419   const bool for_class = mdtype == Metaspace::ClassType ? true : false;
3420   VirtualSpaceList* const vsl = for_class ? Metaspace::class_space_list() : Metaspace::space_list();
3421   if (vsl != NULL) {
3422     if (for_class) {
3423       if (!Metaspace::using_class_space()) {
3424         out->print_cr("No Class Space.");
3425         return;
3426       }
3427       out->print_raw("---- Metaspace Map (Class Space) ----");
3428     } else {
3429       out->print_raw("---- Metaspace Map (Non-Class Space) ----");
3430     }
3431     // Print legend:
3432     out->cr();
3433     out->print_cr("Chunk Types (uppercase chunks are in use): x-specialized, s-small, m-medium, h-humongous.");
3434     out->cr();
3435     VirtualSpaceList* const vsl = for_class ? Metaspace::class_space_list() : Metaspace::space_list();
3436     vsl->print_map(out);
3437     out->cr();
3438   }
3439 }
3440 
3441 void MetaspaceAux::verify_free_chunks() {
3442   Metaspace::chunk_manager_metadata()->verify();
3443   if (Metaspace::using_class_space()) {
3444     Metaspace::chunk_manager_class()->verify();
3445   }
3446 }
3447 
3448 void MetaspaceAux::verify_capacity() {
3449 #ifdef ASSERT
3450   size_t running_sum_capacity_bytes = capacity_bytes();
3451   // For purposes of the running sum of capacity, verify against capacity
3452   size_t capacity_in_use_bytes = capacity_bytes_slow();
3453   assert(running_sum_capacity_bytes == capacity_in_use_bytes,
3454          "capacity_words() * BytesPerWord " SIZE_FORMAT
3455          " capacity_bytes_slow()" SIZE_FORMAT,
3456          running_sum_capacity_bytes, capacity_in_use_bytes);
3457   for (Metaspace::MetadataType i = Metaspace::ClassType;
3458        i < Metaspace:: MetadataTypeCount;
3459        i = (Metaspace::MetadataType)(i + 1)) {
3460     size_t capacity_in_use_bytes = capacity_bytes_slow(i);
3461     assert(capacity_bytes(i) == capacity_in_use_bytes,
3462            "capacity_bytes(%u) " SIZE_FORMAT
3463            " capacity_bytes_slow(%u)" SIZE_FORMAT,
3464            i, capacity_bytes(i), i, capacity_in_use_bytes);
3465   }
3466 #endif
3467 }
3468 
3469 void MetaspaceAux::verify_used() {
3470 #ifdef ASSERT
3471   size_t running_sum_used_bytes = used_bytes();
3472   // For purposes of the running sum of used, verify against used
3473   size_t used_in_use_bytes = used_bytes_slow();
3474   assert(used_bytes() == used_in_use_bytes,
3475          "used_bytes() " SIZE_FORMAT
3476          " used_bytes_slow()" SIZE_FORMAT,
3477          used_bytes(), used_in_use_bytes);
3478   for (Metaspace::MetadataType i = Metaspace::ClassType;
3479        i < Metaspace:: MetadataTypeCount;
3480        i = (Metaspace::MetadataType)(i + 1)) {
3481     size_t used_in_use_bytes = used_bytes_slow(i);
3482     assert(used_bytes(i) == used_in_use_bytes,
3483            "used_bytes(%u) " SIZE_FORMAT
3484            " used_bytes_slow(%u)" SIZE_FORMAT,
3485            i, used_bytes(i), i, used_in_use_bytes);
3486   }
3487 #endif
3488 }
3489 
3490 void MetaspaceAux::verify_metrics() {
3491   verify_capacity();
3492   verify_used();
3493 }
3494 
3495 
3496 // Metaspace methods
3497 
3498 size_t Metaspace::_first_chunk_word_size = 0;
3499 size_t Metaspace::_first_class_chunk_word_size = 0;
3500 
3501 size_t Metaspace::_commit_alignment = 0;
3502 size_t Metaspace::_reserve_alignment = 0;
3503 
3504 Metaspace::Metaspace(Mutex* lock, MetaspaceType type) {
3505   initialize(lock, type);
3506 }
3507 
3508 Metaspace::~Metaspace() {
3509   delete _vsm;
3510   if (using_class_space()) {
3511     delete _class_vsm;
3512   }
3513 }
3514 
3515 VirtualSpaceList* Metaspace::_space_list = NULL;
3516 VirtualSpaceList* Metaspace::_class_space_list = NULL;
3517 
3518 ChunkManager* Metaspace::_chunk_manager_metadata = NULL;
3519 ChunkManager* Metaspace::_chunk_manager_class = NULL;
3520 
3521 #define VIRTUALSPACEMULTIPLIER 2
3522 
3523 #ifdef _LP64
3524 static const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
3525 
3526 void Metaspace::set_narrow_klass_base_and_shift(address metaspace_base, address cds_base) {
3527   assert(!DumpSharedSpaces, "narrow_klass is set by MetaspaceShared class.");
3528   // Figure out the narrow_klass_base and the narrow_klass_shift.  The
3529   // narrow_klass_base is the lower of the metaspace base and the cds base
3530   // (if cds is enabled).  The narrow_klass_shift depends on the distance
3531   // between the lower base and higher address.
3532   address lower_base;
3533   address higher_address;
3534 #if INCLUDE_CDS
3535   if (UseSharedSpaces) {
3536     higher_address = MAX2((address)(cds_base + MetaspaceShared::core_spaces_size()),
3537                           (address)(metaspace_base + compressed_class_space_size()));
3538     lower_base = MIN2(metaspace_base, cds_base);
3539   } else
3540 #endif
3541   {
3542     higher_address = metaspace_base + compressed_class_space_size();
3543     lower_base = metaspace_base;
3544 
3545     uint64_t klass_encoding_max = UnscaledClassSpaceMax << LogKlassAlignmentInBytes;
3546     // If compressed class space fits in lower 32G, we don't need a base.
3547     if (higher_address <= (address)klass_encoding_max) {
3548       lower_base = 0; // Effectively lower base is zero.
3549     }
3550   }
3551 
3552   Universe::set_narrow_klass_base(lower_base);
3553 
3554   // CDS uses LogKlassAlignmentInBytes for narrow_klass_shift. See
3555   // MetaspaceShared::initialize_dumptime_shared_and_meta_spaces() for
3556   // how dump time narrow_klass_shift is set. Although, CDS can work
3557   // with zero-shift mode also, to be consistent with AOT it uses
3558   // LogKlassAlignmentInBytes for klass shift so archived java heap objects
3559   // can be used at same time as AOT code.
3560   if (!UseSharedSpaces
3561       && (uint64_t)(higher_address - lower_base) <= UnscaledClassSpaceMax) {
3562     Universe::set_narrow_klass_shift(0);
3563   } else {
3564     Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes);
3565   }
3566   AOTLoader::set_narrow_klass_shift();
3567 }
3568 
3569 #if INCLUDE_CDS
3570 // Return TRUE if the specified metaspace_base and cds_base are close enough
3571 // to work with compressed klass pointers.
3572 bool Metaspace::can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base) {
3573   assert(cds_base != 0 && UseSharedSpaces, "Only use with CDS");
3574   assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
3575   address lower_base = MIN2((address)metaspace_base, cds_base);
3576   address higher_address = MAX2((address)(cds_base + MetaspaceShared::core_spaces_size()),
3577                                 (address)(metaspace_base + compressed_class_space_size()));
3578   return ((uint64_t)(higher_address - lower_base) <= UnscaledClassSpaceMax);
3579 }
3580 #endif
3581 
3582 // Try to allocate the metaspace at the requested addr.
3583 void Metaspace::allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base) {
3584   assert(!DumpSharedSpaces, "compress klass space is allocated by MetaspaceShared class.");
3585   assert(using_class_space(), "called improperly");
3586   assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
3587   assert(compressed_class_space_size() < KlassEncodingMetaspaceMax,
3588          "Metaspace size is too big");
3589   assert_is_aligned(requested_addr, _reserve_alignment);
3590   assert_is_aligned(cds_base, _reserve_alignment);
3591   assert_is_aligned(compressed_class_space_size(), _reserve_alignment);
3592 
3593   // Don't use large pages for the class space.
3594   bool large_pages = false;
3595 
3596 #if !(defined(AARCH64) || defined(AIX))
3597   ReservedSpace metaspace_rs = ReservedSpace(compressed_class_space_size(),
3598                                              _reserve_alignment,
3599                                              large_pages,
3600                                              requested_addr);
3601 #else // AARCH64
3602   ReservedSpace metaspace_rs;
3603 
3604   // Our compressed klass pointers may fit nicely into the lower 32
3605   // bits.
3606   if ((uint64_t)requested_addr + compressed_class_space_size() < 4*G) {
3607     metaspace_rs = ReservedSpace(compressed_class_space_size(),
3608                                  _reserve_alignment,
3609                                  large_pages,
3610                                  requested_addr);
3611   }
3612 
3613   if (! metaspace_rs.is_reserved()) {
3614     // Aarch64: Try to align metaspace so that we can decode a compressed
3615     // klass with a single MOVK instruction.  We can do this iff the
3616     // compressed class base is a multiple of 4G.
3617     // Aix: Search for a place where we can find memory. If we need to load
3618     // the base, 4G alignment is helpful, too.
3619     size_t increment = AARCH64_ONLY(4*)G;
3620     for (char *a = align_up(requested_addr, increment);
3621          a < (char*)(1024*G);
3622          a += increment) {
3623       if (a == (char *)(32*G)) {
3624         // Go faster from here on. Zero-based is no longer possible.
3625         increment = 4*G;
3626       }
3627 
3628 #if INCLUDE_CDS
3629       if (UseSharedSpaces
3630           && ! can_use_cds_with_metaspace_addr(a, cds_base)) {
3631         // We failed to find an aligned base that will reach.  Fall
3632         // back to using our requested addr.
3633         metaspace_rs = ReservedSpace(compressed_class_space_size(),
3634                                      _reserve_alignment,
3635                                      large_pages,
3636                                      requested_addr);
3637         break;
3638       }
3639 #endif
3640 
3641       metaspace_rs = ReservedSpace(compressed_class_space_size(),
3642                                    _reserve_alignment,
3643                                    large_pages,
3644                                    a);
3645       if (metaspace_rs.is_reserved())
3646         break;
3647     }
3648   }
3649 
3650 #endif // AARCH64
3651 
3652   if (!metaspace_rs.is_reserved()) {
3653 #if INCLUDE_CDS
3654     if (UseSharedSpaces) {
3655       size_t increment = align_up(1*G, _reserve_alignment);
3656 
3657       // Keep trying to allocate the metaspace, increasing the requested_addr
3658       // by 1GB each time, until we reach an address that will no longer allow
3659       // use of CDS with compressed klass pointers.
3660       char *addr = requested_addr;
3661       while (!metaspace_rs.is_reserved() && (addr + increment > addr) &&
3662              can_use_cds_with_metaspace_addr(addr + increment, cds_base)) {
3663         addr = addr + increment;
3664         metaspace_rs = ReservedSpace(compressed_class_space_size(),
3665                                      _reserve_alignment, large_pages, addr);
3666       }
3667     }
3668 #endif
3669     // If no successful allocation then try to allocate the space anywhere.  If
3670     // that fails then OOM doom.  At this point we cannot try allocating the
3671     // metaspace as if UseCompressedClassPointers is off because too much
3672     // initialization has happened that depends on UseCompressedClassPointers.
3673     // So, UseCompressedClassPointers cannot be turned off at this point.
3674     if (!metaspace_rs.is_reserved()) {
3675       metaspace_rs = ReservedSpace(compressed_class_space_size(),
3676                                    _reserve_alignment, large_pages);
3677       if (!metaspace_rs.is_reserved()) {
3678         vm_exit_during_initialization(err_msg("Could not allocate metaspace: " SIZE_FORMAT " bytes",
3679                                               compressed_class_space_size()));
3680       }
3681     }
3682   }
3683 
3684   // If we got here then the metaspace got allocated.
3685   MemTracker::record_virtual_memory_type((address)metaspace_rs.base(), mtClass);
3686 
3687 #if INCLUDE_CDS
3688   // Verify that we can use shared spaces.  Otherwise, turn off CDS.
3689   if (UseSharedSpaces && !can_use_cds_with_metaspace_addr(metaspace_rs.base(), cds_base)) {
3690     FileMapInfo::stop_sharing_and_unmap(
3691         "Could not allocate metaspace at a compatible address");
3692   }
3693 #endif
3694   set_narrow_klass_base_and_shift((address)metaspace_rs.base(),
3695                                   UseSharedSpaces ? (address)cds_base : 0);
3696 
3697   initialize_class_space(metaspace_rs);
3698 
3699   LogTarget(Trace, gc, metaspace) lt;
3700   if (lt.is_enabled()) {
3701     ResourceMark rm;
3702     LogStream ls(lt);
3703     print_compressed_class_space(&ls, requested_addr);
3704   }
3705 }
3706 
3707 void Metaspace::print_compressed_class_space(outputStream* st, const char* requested_addr) {
3708   st->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: %d",
3709                p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift());
3710   if (_class_space_list != NULL) {
3711     address base = (address)_class_space_list->current_virtual_space()->bottom();
3712     st->print("Compressed class space size: " SIZE_FORMAT " Address: " PTR_FORMAT,
3713                  compressed_class_space_size(), p2i(base));
3714     if (requested_addr != 0) {
3715       st->print(" Req Addr: " PTR_FORMAT, p2i(requested_addr));
3716     }
3717     st->cr();
3718   }
3719 }
3720 
3721 // For UseCompressedClassPointers the class space is reserved above the top of
3722 // the Java heap.  The argument passed in is at the base of the compressed space.
3723 void Metaspace::initialize_class_space(ReservedSpace rs) {
3724   // The reserved space size may be bigger because of alignment, esp with UseLargePages
3725   assert(rs.size() >= CompressedClassSpaceSize,
3726          SIZE_FORMAT " != " SIZE_FORMAT, rs.size(), CompressedClassSpaceSize);
3727   assert(using_class_space(), "Must be using class space");
3728   _class_space_list = new VirtualSpaceList(rs);
3729   _chunk_manager_class = new ChunkManager(ClassSpecializedChunk, ClassSmallChunk, ClassMediumChunk);
3730 
3731   if (!_class_space_list->initialization_succeeded()) {
3732     vm_exit_during_initialization("Failed to setup compressed class space virtual space list.");
3733   }
3734 }
3735 
3736 #endif
3737 
3738 void Metaspace::ergo_initialize() {
3739   if (DumpSharedSpaces) {
3740     // Using large pages when dumping the shared archive is currently not implemented.
3741     FLAG_SET_ERGO(bool, UseLargePagesInMetaspace, false);
3742   }
3743 
3744   size_t page_size = os::vm_page_size();
3745   if (UseLargePages && UseLargePagesInMetaspace) {
3746     page_size = os::large_page_size();
3747   }
3748 
3749   _commit_alignment  = page_size;
3750   _reserve_alignment = MAX2(page_size, (size_t)os::vm_allocation_granularity());
3751 
3752   // Do not use FLAG_SET_ERGO to update MaxMetaspaceSize, since this will
3753   // override if MaxMetaspaceSize was set on the command line or not.
3754   // This information is needed later to conform to the specification of the
3755   // java.lang.management.MemoryUsage API.
3756   //
3757   // Ideally, we would be able to set the default value of MaxMetaspaceSize in
3758   // globals.hpp to the aligned value, but this is not possible, since the
3759   // alignment depends on other flags being parsed.
3760   MaxMetaspaceSize = align_down_bounded(MaxMetaspaceSize, _reserve_alignment);
3761 
3762   if (MetaspaceSize > MaxMetaspaceSize) {
3763     MetaspaceSize = MaxMetaspaceSize;
3764   }
3765 
3766   MetaspaceSize = align_down_bounded(MetaspaceSize, _commit_alignment);
3767 
3768   assert(MetaspaceSize <= MaxMetaspaceSize, "MetaspaceSize should be limited by MaxMetaspaceSize");
3769 
3770   MinMetaspaceExpansion = align_down_bounded(MinMetaspaceExpansion, _commit_alignment);
3771   MaxMetaspaceExpansion = align_down_bounded(MaxMetaspaceExpansion, _commit_alignment);
3772 
3773   CompressedClassSpaceSize = align_down_bounded(CompressedClassSpaceSize, _reserve_alignment);
3774 
3775   // Initial virtual space size will be calculated at global_initialize()
3776   size_t min_metaspace_sz =
3777       VIRTUALSPACEMULTIPLIER * InitialBootClassLoaderMetaspaceSize;
3778   if (UseCompressedClassPointers) {
3779     if ((min_metaspace_sz + CompressedClassSpaceSize) >  MaxMetaspaceSize) {
3780       if (min_metaspace_sz >= MaxMetaspaceSize) {
3781         vm_exit_during_initialization("MaxMetaspaceSize is too small.");
3782       } else {
3783         FLAG_SET_ERGO(size_t, CompressedClassSpaceSize,
3784                       MaxMetaspaceSize - min_metaspace_sz);
3785       }
3786     }
3787   } else if (min_metaspace_sz >= MaxMetaspaceSize) {
3788     FLAG_SET_ERGO(size_t, InitialBootClassLoaderMetaspaceSize,
3789                   min_metaspace_sz);
3790   }
3791 
3792   set_compressed_class_space_size(CompressedClassSpaceSize);
3793 }
3794 
3795 void Metaspace::global_initialize() {
3796   MetaspaceGC::initialize();
3797 
3798 #if INCLUDE_CDS
3799   if (DumpSharedSpaces) {
3800     MetaspaceShared::initialize_dumptime_shared_and_meta_spaces();
3801   } else if (UseSharedSpaces) {
3802     // If any of the archived space fails to map, UseSharedSpaces
3803     // is reset to false. Fall through to the
3804     // (!DumpSharedSpaces && !UseSharedSpaces) case to set up class
3805     // metaspace.
3806     MetaspaceShared::initialize_runtime_shared_and_meta_spaces();
3807   }
3808 
3809   if (!DumpSharedSpaces && !UseSharedSpaces)
3810 #endif // INCLUDE_CDS
3811   {
3812 #ifdef _LP64
3813     if (using_class_space()) {
3814       char* base = (char*)align_up(Universe::heap()->reserved_region().end(), _reserve_alignment);
3815       allocate_metaspace_compressed_klass_ptrs(base, 0);
3816     }
3817 #endif // _LP64
3818   }
3819 
3820   // Initialize these before initializing the VirtualSpaceList
3821   _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord;
3822   _first_chunk_word_size = align_word_size_up(_first_chunk_word_size);
3823   // Make the first class chunk bigger than a medium chunk so it's not put
3824   // on the medium chunk list.   The next chunk will be small and progress
3825   // from there.  This size calculated by -version.
3826   _first_class_chunk_word_size = MIN2((size_t)MediumChunk*6,
3827                                      (CompressedClassSpaceSize/BytesPerWord)*2);
3828   _first_class_chunk_word_size = align_word_size_up(_first_class_chunk_word_size);
3829   // Arbitrarily set the initial virtual space to a multiple
3830   // of the boot class loader size.
3831   size_t word_size = VIRTUALSPACEMULTIPLIER * _first_chunk_word_size;
3832   word_size = align_up(word_size, Metaspace::reserve_alignment_words());
3833 
3834   // Initialize the list of virtual spaces.
3835   _space_list = new VirtualSpaceList(word_size);
3836   _chunk_manager_metadata = new ChunkManager(SpecializedChunk, SmallChunk, MediumChunk);
3837 
3838   if (!_space_list->initialization_succeeded()) {
3839     vm_exit_during_initialization("Unable to setup metadata virtual space list.", NULL);
3840   }
3841 
3842   _tracer = new MetaspaceTracer();
3843 }
3844 
3845 void Metaspace::post_initialize() {
3846   MetaspaceGC::post_initialize();
3847 }
3848 
3849 void Metaspace::initialize_first_chunk(MetaspaceType type, MetadataType mdtype) {
3850   Metachunk* chunk = get_initialization_chunk(type, mdtype);
3851   if (chunk != NULL) {
3852     // Add to this manager's list of chunks in use and current_chunk().
3853     get_space_manager(mdtype)->add_chunk(chunk, true);
3854   }
3855 }
3856 
3857 Metachunk* Metaspace::get_initialization_chunk(MetaspaceType type, MetadataType mdtype) {
3858   size_t chunk_word_size = get_space_manager(mdtype)->get_initial_chunk_size(type);
3859 
3860   // Get a chunk from the chunk freelist
3861   Metachunk* chunk = get_chunk_manager(mdtype)->chunk_freelist_allocate(chunk_word_size);
3862 
3863   if (chunk == NULL) {
3864     chunk = get_space_list(mdtype)->get_new_chunk(chunk_word_size,
3865                                                   get_space_manager(mdtype)->medium_chunk_bunch());
3866   }
3867 
3868   return chunk;
3869 }
3870 
3871 void Metaspace::verify_global_initialization() {
3872   assert(space_list() != NULL, "Metadata VirtualSpaceList has not been initialized");
3873   assert(chunk_manager_metadata() != NULL, "Metadata ChunkManager has not been initialized");
3874 
3875   if (using_class_space()) {
3876     assert(class_space_list() != NULL, "Class VirtualSpaceList has not been initialized");
3877     assert(chunk_manager_class() != NULL, "Class ChunkManager has not been initialized");
3878   }
3879 }
3880 
3881 void Metaspace::initialize(Mutex* lock, MetaspaceType type) {
3882   verify_global_initialization();
3883 
3884   // Allocate SpaceManager for metadata objects.
3885   _vsm = new SpaceManager(NonClassType, type, lock);
3886 
3887   if (using_class_space()) {
3888     // Allocate SpaceManager for classes.
3889     _class_vsm = new SpaceManager(ClassType, type, lock);
3890   }
3891 
3892   MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
3893 
3894   // Allocate chunk for metadata objects
3895   initialize_first_chunk(type, NonClassType);
3896 
3897   // Allocate chunk for class metadata objects
3898   if (using_class_space()) {
3899     initialize_first_chunk(type, ClassType);
3900   }
3901 }
3902 
3903 size_t Metaspace::align_word_size_up(size_t word_size) {
3904   size_t byte_size = word_size * wordSize;
3905   return ReservedSpace::allocation_align_size_up(byte_size) / wordSize;
3906 }
3907 
3908 MetaWord* Metaspace::allocate(size_t word_size, MetadataType mdtype) {
3909   assert(!_frozen, "sanity");
3910   // Don't use class_vsm() unless UseCompressedClassPointers is true.
3911   if (is_class_space_allocation(mdtype)) {
3912     return  class_vsm()->allocate(word_size);
3913   } else {
3914     return  vsm()->allocate(word_size);
3915   }
3916 }
3917 
3918 MetaWord* Metaspace::expand_and_allocate(size_t word_size, MetadataType mdtype) {
3919   assert(!_frozen, "sanity");
3920   size_t delta_bytes = MetaspaceGC::delta_capacity_until_GC(word_size * BytesPerWord);
3921   assert(delta_bytes > 0, "Must be");
3922 
3923   size_t before = 0;
3924   size_t after = 0;
3925   MetaWord* res;
3926   bool incremented;
3927 
3928   // Each thread increments the HWM at most once. Even if the thread fails to increment
3929   // the HWM, an allocation is still attempted. This is because another thread must then
3930   // have incremented the HWM and therefore the allocation might still succeed.
3931   do {
3932     incremented = MetaspaceGC::inc_capacity_until_GC(delta_bytes, &after, &before);
3933     res = allocate(word_size, mdtype);
3934   } while (!incremented && res == NULL);
3935 
3936   if (incremented) {
3937     tracer()->report_gc_threshold(before, after,
3938                                   MetaspaceGCThresholdUpdater::ExpandAndAllocate);
3939     log_trace(gc, metaspace)("Increase capacity to GC from " SIZE_FORMAT " to " SIZE_FORMAT, before, after);
3940   }
3941 
3942   return res;
3943 }
3944 
3945 size_t Metaspace::used_words_slow(MetadataType mdtype) const {
3946   if (mdtype == ClassType) {
3947     return using_class_space() ? class_vsm()->sum_used_in_chunks_in_use() : 0;
3948   } else {
3949     return vsm()->sum_used_in_chunks_in_use();  // includes overhead!
3950   }
3951 }
3952 
3953 size_t Metaspace::free_words_slow(MetadataType mdtype) const {
3954   assert(!_frozen, "sanity");
3955   if (mdtype == ClassType) {
3956     return using_class_space() ? class_vsm()->sum_free_in_chunks_in_use() : 0;
3957   } else {
3958     return vsm()->sum_free_in_chunks_in_use();
3959   }
3960 }
3961 
3962 // Space capacity in the Metaspace.  It includes
3963 // space in the list of chunks from which allocations
3964 // have been made. Don't include space in the global freelist and
3965 // in the space available in the dictionary which
3966 // is already counted in some chunk.
3967 size_t Metaspace::capacity_words_slow(MetadataType mdtype) const {
3968   if (mdtype == ClassType) {
3969     return using_class_space() ? class_vsm()->sum_capacity_in_chunks_in_use() : 0;
3970   } else {
3971     return vsm()->sum_capacity_in_chunks_in_use();
3972   }
3973 }
3974 
3975 size_t Metaspace::used_bytes_slow(MetadataType mdtype) const {
3976   return used_words_slow(mdtype) * BytesPerWord;
3977 }
3978 
3979 size_t Metaspace::capacity_bytes_slow(MetadataType mdtype) const {
3980   return capacity_words_slow(mdtype) * BytesPerWord;
3981 }
3982 
3983 size_t Metaspace::allocated_blocks_bytes() const {
3984   return vsm()->allocated_blocks_bytes() +
3985       (using_class_space() ? class_vsm()->allocated_blocks_bytes() : 0);
3986 }
3987 
3988 size_t Metaspace::allocated_chunks_bytes() const {
3989   return vsm()->allocated_chunks_bytes() +
3990       (using_class_space() ? class_vsm()->allocated_chunks_bytes() : 0);
3991 }
3992 
3993 void Metaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) {
3994   assert(!_frozen, "sanity");
3995   assert(!SafepointSynchronize::is_at_safepoint()
3996          || Thread::current()->is_VM_thread(), "should be the VM thread");
3997 
3998   MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag);
3999 
4000   if (is_class && using_class_space()) {
4001     class_vsm()->deallocate(ptr, word_size);
4002   } else {
4003     vsm()->deallocate(ptr, word_size);
4004   }
4005 }
4006 
4007 MetaWord* Metaspace::allocate(ClassLoaderData* loader_data, size_t word_size,
4008                               MetaspaceObj::Type type, TRAPS) {
4009   assert(!_frozen, "sanity");
4010   if (HAS_PENDING_EXCEPTION) {
4011     assert(false, "Should not allocate with exception pending");
4012     return NULL;  // caller does a CHECK_NULL too
4013   }
4014 
4015   assert(loader_data != NULL, "Should never pass around a NULL loader_data. "
4016         "ClassLoaderData::the_null_class_loader_data() should have been used.");
4017 
4018   MetadataType mdtype = (type == MetaspaceObj::ClassType) ? ClassType : NonClassType;
4019 
4020   // Try to allocate metadata.
4021   MetaWord* result = loader_data->metaspace_non_null()->allocate(word_size, mdtype);
4022 
4023   if (result == NULL) {
4024     tracer()->report_metaspace_allocation_failure(loader_data, word_size, type, mdtype);
4025 
4026     // Allocation failed.
4027     if (is_init_completed()) {
4028       // Only start a GC if the bootstrapping has completed.
4029 
4030       // Try to clean out some memory and retry.
4031       result = Universe::heap()->satisfy_failed_metadata_allocation(loader_data, word_size, mdtype);
4032     }
4033   }
4034 
4035   if (result == NULL) {
4036     SpaceManager* sm;
4037     if (is_class_space_allocation(mdtype)) {
4038       sm = loader_data->metaspace_non_null()->class_vsm();
4039     } else {
4040       sm = loader_data->metaspace_non_null()->vsm();
4041     }
4042 
4043     result = sm->get_small_chunk_and_allocate(word_size);
4044 
4045     if (result == NULL) {
4046       report_metadata_oome(loader_data, word_size, type, mdtype, CHECK_NULL);
4047     }
4048   }
4049 
4050   // Zero initialize.
4051   Copy::fill_to_words((HeapWord*)result, word_size, 0);
4052 
4053   return result;
4054 }
4055 
4056 size_t Metaspace::class_chunk_size(size_t word_size) {
4057   assert(using_class_space(), "Has to use class space");
4058   return class_vsm()->calc_chunk_size(word_size);
4059 }
4060 
4061 void Metaspace::report_metadata_oome(ClassLoaderData* loader_data, size_t word_size, MetaspaceObj::Type type, MetadataType mdtype, TRAPS) {
4062   tracer()->report_metadata_oom(loader_data, word_size, type, mdtype);
4063 
4064   // If result is still null, we are out of memory.
4065   Log(gc, metaspace, freelist) log;
4066   if (log.is_info()) {
4067     log.info("Metaspace (%s) allocation failed for size " SIZE_FORMAT,
4068              is_class_space_allocation(mdtype) ? "class" : "data", word_size);
4069     ResourceMark rm;
4070     if (log.is_debug()) {
4071       if (loader_data->metaspace_or_null() != NULL) {
4072         LogStream ls(log.debug());
4073         loader_data->dump(&ls);
4074       }
4075     }
4076     LogStream ls(log.info());
4077     MetaspaceAux::dump(&ls);
4078     MetaspaceAux::print_metaspace_map(&ls, mdtype);
4079     ChunkManager::print_all_chunkmanagers(&ls);
4080   }
4081 
4082   bool out_of_compressed_class_space = false;
4083   if (is_class_space_allocation(mdtype)) {
4084     Metaspace* metaspace = loader_data->metaspace_non_null();
4085     out_of_compressed_class_space =
4086       MetaspaceAux::committed_bytes(Metaspace::ClassType) +
4087       (metaspace->class_chunk_size(word_size) * BytesPerWord) >
4088       CompressedClassSpaceSize;
4089   }
4090 
4091   // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
4092   const char* space_string = out_of_compressed_class_space ?
4093     "Compressed class space" : "Metaspace";
4094 
4095   report_java_out_of_memory(space_string);
4096 
4097   if (JvmtiExport::should_post_resource_exhausted()) {
4098     JvmtiExport::post_resource_exhausted(
4099         JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,
4100         space_string);
4101   }
4102 
4103   if (!is_init_completed()) {
4104     vm_exit_during_initialization("OutOfMemoryError", space_string);
4105   }
4106 
4107   if (out_of_compressed_class_space) {
4108     THROW_OOP(Universe::out_of_memory_error_class_metaspace());
4109   } else {
4110     THROW_OOP(Universe::out_of_memory_error_metaspace());
4111   }
4112 }
4113 
4114 const char* Metaspace::metadata_type_name(Metaspace::MetadataType mdtype) {
4115   switch (mdtype) {
4116     case Metaspace::ClassType: return "Class";
4117     case Metaspace::NonClassType: return "Metadata";
4118     default:
4119       assert(false, "Got bad mdtype: %d", (int) mdtype);
4120       return NULL;
4121   }
4122 }
4123 
4124 void Metaspace::purge(MetadataType mdtype) {
4125   get_space_list(mdtype)->purge(get_chunk_manager(mdtype));
4126 }
4127 
4128 void Metaspace::purge() {
4129   MutexLockerEx cl(SpaceManager::expand_lock(),
4130                    Mutex::_no_safepoint_check_flag);
4131   purge(NonClassType);
4132   if (using_class_space()) {
4133     purge(ClassType);
4134   }
4135 }
4136 
4137 void Metaspace::print_on(outputStream* out) const {
4138   // Print both class virtual space counts and metaspace.
4139   if (Verbose) {
4140     vsm()->print_on(out);
4141     if (using_class_space()) {
4142       class_vsm()->print_on(out);
4143     }
4144   }
4145 }
4146 
4147 bool Metaspace::contains(const void* ptr) {
4148   if (MetaspaceShared::is_in_shared_metaspace(ptr)) {
4149     return true;
4150   }
4151   return contains_non_shared(ptr);
4152 }
4153 
4154 bool Metaspace::contains_non_shared(const void* ptr) {
4155   if (using_class_space() && get_space_list(ClassType)->contains(ptr)) {
4156      return true;
4157   }
4158 
4159   return get_space_list(NonClassType)->contains(ptr);
4160 }
4161 
4162 void Metaspace::verify() {
4163   vsm()->verify();
4164   if (using_class_space()) {
4165     class_vsm()->verify();
4166   }
4167 }
4168 
4169 void Metaspace::dump(outputStream* const out) const {
4170   out->print_cr("\nVirtual space manager: " INTPTR_FORMAT, p2i(vsm()));
4171   vsm()->dump(out);
4172   if (using_class_space()) {
4173     out->print_cr("\nClass space manager: " INTPTR_FORMAT, p2i(class_vsm()));
4174     class_vsm()->dump(out);
4175   }
4176 }
4177 
4178 /////////////// Unit tests ///////////////
4179 
4180 #ifndef PRODUCT
4181 
4182 class TestMetaspaceAuxTest : AllStatic {
4183  public:
4184   static void test_reserved() {
4185     size_t reserved = MetaspaceAux::reserved_bytes();
4186 
4187     assert(reserved > 0, "assert");
4188 
4189     size_t committed  = MetaspaceAux::committed_bytes();
4190     assert(committed <= reserved, "assert");
4191 
4192     size_t reserved_metadata = MetaspaceAux::reserved_bytes(Metaspace::NonClassType);
4193     assert(reserved_metadata > 0, "assert");
4194     assert(reserved_metadata <= reserved, "assert");
4195 
4196     if (UseCompressedClassPointers) {
4197       size_t reserved_class    = MetaspaceAux::reserved_bytes(Metaspace::ClassType);
4198       assert(reserved_class > 0, "assert");
4199       assert(reserved_class < reserved, "assert");
4200     }
4201   }
4202 
4203   static void test_committed() {
4204     size_t committed = MetaspaceAux::committed_bytes();
4205 
4206     assert(committed > 0, "assert");
4207 
4208     size_t reserved  = MetaspaceAux::reserved_bytes();
4209     assert(committed <= reserved, "assert");
4210 
4211     size_t committed_metadata = MetaspaceAux::committed_bytes(Metaspace::NonClassType);
4212     assert(committed_metadata > 0, "assert");
4213     assert(committed_metadata <= committed, "assert");
4214 
4215     if (UseCompressedClassPointers) {
4216       size_t committed_class    = MetaspaceAux::committed_bytes(Metaspace::ClassType);
4217       assert(committed_class > 0, "assert");
4218       assert(committed_class < committed, "assert");
4219     }
4220   }
4221 
4222   static void test_virtual_space_list_large_chunk() {
4223     VirtualSpaceList* vs_list = new VirtualSpaceList(os::vm_allocation_granularity());
4224     MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
4225     // A size larger than VirtualSpaceSize (256k) and add one page to make it _not_ be
4226     // vm_allocation_granularity aligned on Windows.
4227     size_t large_size = (size_t)(2*256*K + (os::vm_page_size()/BytesPerWord));
4228     large_size += (os::vm_page_size()/BytesPerWord);
4229     vs_list->get_new_chunk(large_size, 0);
4230   }
4231 
4232   static void test() {
4233     test_reserved();
4234     test_committed();
4235     test_virtual_space_list_large_chunk();
4236   }
4237 };
4238 
4239 void TestMetaspaceAux_test() {
4240   TestMetaspaceAuxTest::test();
4241 }
4242 
4243 class TestVirtualSpaceNodeTest {
4244   static void chunk_up(size_t words_left, size_t& num_medium_chunks,
4245                                           size_t& num_small_chunks,
4246                                           size_t& num_specialized_chunks) {
4247     num_medium_chunks = words_left / MediumChunk;
4248     words_left = words_left % MediumChunk;
4249 
4250     num_small_chunks = words_left / SmallChunk;
4251     words_left = words_left % SmallChunk;
4252     // how many specialized chunks can we get?
4253     num_specialized_chunks = words_left / SpecializedChunk;
4254     assert(words_left % SpecializedChunk == 0, "should be nothing left");
4255   }
4256 
4257  public:
4258   static void test() {
4259     MutexLockerEx ml(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
4260     const size_t vsn_test_size_words = MediumChunk  * 4;
4261     const size_t vsn_test_size_bytes = vsn_test_size_words * BytesPerWord;
4262 
4263     // The chunk sizes must be multiples of eachother, or this will fail
4264     STATIC_ASSERT(MediumChunk % SmallChunk == 0);
4265     STATIC_ASSERT(SmallChunk % SpecializedChunk == 0);
4266 
4267     { // No committed memory in VSN
4268       ChunkManager cm(SpecializedChunk, SmallChunk, MediumChunk);
4269       VirtualSpaceNode vsn(vsn_test_size_bytes);
4270       vsn.initialize();
4271       vsn.retire(&cm);
4272       assert(cm.sum_free_chunks_count() == 0, "did not commit any memory in the VSN");
4273     }
4274 
4275     { // All of VSN is committed, half is used by chunks
4276       ChunkManager cm(SpecializedChunk, SmallChunk, MediumChunk);
4277       VirtualSpaceNode vsn(vsn_test_size_bytes);
4278       vsn.initialize();
4279       vsn.expand_by(vsn_test_size_words, vsn_test_size_words);
4280       vsn.get_chunk_vs(MediumChunk);
4281       vsn.get_chunk_vs(MediumChunk);
4282       vsn.retire(&cm);
4283       assert(cm.sum_free_chunks_count() == 2, "should have been memory left for 2 medium chunks");
4284       assert(cm.sum_free_chunks() == 2*MediumChunk, "sizes should add up");
4285     }
4286 
4287     const size_t page_chunks = 4 * (size_t)os::vm_page_size() / BytesPerWord;
4288     // This doesn't work for systems with vm_page_size >= 16K.
4289     if (page_chunks < MediumChunk) {
4290       // 4 pages of VSN is committed, some is used by chunks
4291       ChunkManager cm(SpecializedChunk, SmallChunk, MediumChunk);
4292       VirtualSpaceNode vsn(vsn_test_size_bytes);
4293 
4294       vsn.initialize();
4295       vsn.expand_by(page_chunks, page_chunks);
4296       vsn.get_chunk_vs(SmallChunk);
4297       vsn.get_chunk_vs(SpecializedChunk);
4298       vsn.retire(&cm);
4299 
4300       // committed - used = words left to retire
4301       const size_t words_left = page_chunks - SmallChunk - SpecializedChunk;
4302 
4303       size_t num_medium_chunks, num_small_chunks, num_spec_chunks;
4304       chunk_up(words_left, num_medium_chunks, num_small_chunks, num_spec_chunks);
4305 
4306       assert(num_medium_chunks == 0, "should not get any medium chunks");
4307       assert(cm.sum_free_chunks_count() == (num_small_chunks + num_spec_chunks), "should be space for 3 chunks");
4308       assert(cm.sum_free_chunks() == words_left, "sizes should add up");
4309     }
4310 
4311     { // Half of VSN is committed, a humongous chunk is used
4312       ChunkManager cm(SpecializedChunk, SmallChunk, MediumChunk);
4313       VirtualSpaceNode vsn(vsn_test_size_bytes);
4314       vsn.initialize();
4315       vsn.expand_by(MediumChunk * 2, MediumChunk * 2);
4316       vsn.get_chunk_vs(MediumChunk + SpecializedChunk); // Humongous chunks will be aligned up to MediumChunk + SpecializedChunk
4317       vsn.retire(&cm);
4318 
4319       const size_t words_left = MediumChunk * 2 - (MediumChunk + SpecializedChunk);
4320       size_t num_medium_chunks, num_small_chunks, num_spec_chunks;
4321       chunk_up(words_left, num_medium_chunks, num_small_chunks, num_spec_chunks);
4322 
4323       assert(num_medium_chunks == 0, "should not get any medium chunks");
4324       assert(cm.sum_free_chunks_count() == (num_small_chunks + num_spec_chunks), "should be space for 3 chunks");
4325       assert(cm.sum_free_chunks() == words_left, "sizes should add up");
4326     }
4327 
4328   }
4329 
4330 #define assert_is_available_positive(word_size) \
4331   assert(vsn.is_available(word_size), \
4332          #word_size ": " PTR_FORMAT " bytes were not available in " \
4333          "VirtualSpaceNode [" PTR_FORMAT ", " PTR_FORMAT ")", \
4334          (uintptr_t)(word_size * BytesPerWord), p2i(vsn.bottom()), p2i(vsn.end()));
4335 
4336 #define assert_is_available_negative(word_size) \
4337   assert(!vsn.is_available(word_size), \
4338          #word_size ": " PTR_FORMAT " bytes should not be available in " \
4339          "VirtualSpaceNode [" PTR_FORMAT ", " PTR_FORMAT ")", \
4340          (uintptr_t)(word_size * BytesPerWord), p2i(vsn.bottom()), p2i(vsn.end()));
4341 
4342   static void test_is_available_positive() {
4343     // Reserve some memory.
4344     VirtualSpaceNode vsn(os::vm_allocation_granularity());
4345     assert(vsn.initialize(), "Failed to setup VirtualSpaceNode");
4346 
4347     // Commit some memory.
4348     size_t commit_word_size = os::vm_allocation_granularity() / BytesPerWord;
4349     bool expanded = vsn.expand_by(commit_word_size, commit_word_size);
4350     assert(expanded, "Failed to commit");
4351 
4352     // Check that is_available accepts the committed size.
4353     assert_is_available_positive(commit_word_size);
4354 
4355     // Check that is_available accepts half the committed size.
4356     size_t expand_word_size = commit_word_size / 2;
4357     assert_is_available_positive(expand_word_size);
4358   }
4359 
4360   static void test_is_available_negative() {
4361     // Reserve some memory.
4362     VirtualSpaceNode vsn(os::vm_allocation_granularity());
4363     assert(vsn.initialize(), "Failed to setup VirtualSpaceNode");
4364 
4365     // Commit some memory.
4366     size_t commit_word_size = os::vm_allocation_granularity() / BytesPerWord;
4367     bool expanded = vsn.expand_by(commit_word_size, commit_word_size);
4368     assert(expanded, "Failed to commit");
4369 
4370     // Check that is_available doesn't accept a too large size.
4371     size_t two_times_commit_word_size = commit_word_size * 2;
4372     assert_is_available_negative(two_times_commit_word_size);
4373   }
4374 
4375   static void test_is_available_overflow() {
4376     // Reserve some memory.
4377     VirtualSpaceNode vsn(os::vm_allocation_granularity());
4378     assert(vsn.initialize(), "Failed to setup VirtualSpaceNode");
4379 
4380     // Commit some memory.
4381     size_t commit_word_size = os::vm_allocation_granularity() / BytesPerWord;
4382     bool expanded = vsn.expand_by(commit_word_size, commit_word_size);
4383     assert(expanded, "Failed to commit");
4384 
4385     // Calculate a size that will overflow the virtual space size.
4386     void* virtual_space_max = (void*)(uintptr_t)-1;
4387     size_t bottom_to_max = pointer_delta(virtual_space_max, vsn.bottom(), 1);
4388     size_t overflow_size = bottom_to_max + BytesPerWord;
4389     size_t overflow_word_size = overflow_size / BytesPerWord;
4390 
4391     // Check that is_available can handle the overflow.
4392     assert_is_available_negative(overflow_word_size);
4393   }
4394 
4395   static void test_is_available() {
4396     TestVirtualSpaceNodeTest::test_is_available_positive();
4397     TestVirtualSpaceNodeTest::test_is_available_negative();
4398     TestVirtualSpaceNodeTest::test_is_available_overflow();
4399   }
4400 };
4401 
4402 void TestVirtualSpaceNode_test() {
4403   TestVirtualSpaceNodeTest::test();
4404   TestVirtualSpaceNodeTest::test_is_available();
4405 }
4406 
4407 // The following test is placed here instead of a gtest / unittest file
4408 // because the ChunkManager class is only available in this file.
4409 void ChunkManager_test_list_index() {
4410   ChunkManager manager(ClassSpecializedChunk, ClassSmallChunk, ClassMediumChunk);
4411 
4412   // Test previous bug where a query for a humongous class metachunk,
4413   // incorrectly matched the non-class medium metachunk size.
4414   {
4415     assert(MediumChunk > ClassMediumChunk, "Precondition for test");
4416 
4417     ChunkIndex index = manager.list_index(MediumChunk);
4418 
4419     assert(index == HumongousIndex,
4420            "Requested size is larger than ClassMediumChunk,"
4421            " so should return HumongousIndex. Got index: %d", (int)index);
4422   }
4423 
4424   // Check the specified sizes as well.
4425   {
4426     ChunkIndex index = manager.list_index(ClassSpecializedChunk);
4427     assert(index == SpecializedIndex, "Wrong index returned. Got index: %d", (int)index);
4428   }
4429   {
4430     ChunkIndex index = manager.list_index(ClassSmallChunk);
4431     assert(index == SmallIndex, "Wrong index returned. Got index: %d", (int)index);
4432   }
4433   {
4434     ChunkIndex index = manager.list_index(ClassMediumChunk);
4435     assert(index == MediumIndex, "Wrong index returned. Got index: %d", (int)index);
4436   }
4437   {
4438     ChunkIndex index = manager.list_index(ClassMediumChunk + 1);
4439     assert(index == HumongousIndex, "Wrong index returned. Got index: %d", (int)index);
4440   }
4441 }
4442 
4443 #endif // !PRODUCT
4444 
4445 #ifdef ASSERT
4446 
4447 // ChunkManagerReturnTest stresses taking/returning chunks from the ChunkManager. It takes and
4448 // returns chunks from/to the ChunkManager while keeping track of the expected ChunkManager
4449 // content.
4450 class ChunkManagerReturnTestImpl : public CHeapObj<mtClass> {
4451 
4452   VirtualSpaceNode _vsn;
4453   ChunkManager _cm;
4454 
4455   // The expected content of the chunk manager.
4456   unsigned _chunks_in_chunkmanager;
4457   size_t _words_in_chunkmanager;
4458 
4459   // A fixed size pool of chunks. Chunks may be in the chunk manager (free) or not (in use).
4460   static const int num_chunks = 256;
4461   Metachunk* _pool[num_chunks];
4462 
4463   // Helper, return a random position into the chunk pool.
4464   static int get_random_position() {
4465     return os::random() % num_chunks;
4466   }
4467 
4468   // Asserts that ChunkManager counters match expectations.
4469   void assert_counters() {
4470     assert(_vsn.container_count() == num_chunks - _chunks_in_chunkmanager, "vsn counter mismatch.");
4471     assert(_cm.free_chunks_count() == _chunks_in_chunkmanager, "cm counter mismatch.");
4472     assert(_cm.free_chunks_total_words() == _words_in_chunkmanager, "cm counter mismatch.");
4473   }
4474 
4475   // Get a random chunk size. Equal chance to get spec/med/small chunk size or
4476   // a humongous chunk size. The latter itself is random in the range of [med+spec..4*med).
4477   size_t get_random_chunk_size() {
4478     const size_t sizes [] = { SpecializedChunk, SmallChunk, MediumChunk };
4479     const int rand = os::random() % 4;
4480     if (rand < 3) {
4481       return sizes[rand];
4482     } else {
4483       // Note: this affects the max. size of space (see _vsn initialization in ctor).
4484       return align_up(MediumChunk + 1 + (os::random() % (MediumChunk * 4)), SpecializedChunk);
4485     }
4486   }
4487 
4488   // Starting at pool index <start>+1, find the next chunk tagged as either free or in use, depending
4489   // on <is_free>. Search wraps. Returns its position, or -1 if no matching chunk was found.
4490   int next_matching_chunk(int start, bool is_free) const {
4491     assert(start >= 0 && start < num_chunks, "invalid parameter");
4492     int pos = start;
4493     do {
4494       if (++pos == num_chunks) {
4495         pos = 0;
4496       }
4497       if (_pool[pos]->is_tagged_free() == is_free) {
4498         return pos;
4499       }
4500     } while (pos != start);
4501     return -1;
4502   }
4503 
4504   // A structure to keep information about a chunk list including which
4505   // chunks are part of this list. This is needed to keep information about a chunk list
4506   // we will to return to the ChunkManager, because the original list will be destroyed.
4507   struct AChunkList {
4508     Metachunk* head;
4509     Metachunk* all[num_chunks];
4510     size_t size;
4511     int num;
4512     ChunkIndex index;
4513   };
4514 
4515   // Assemble, from the in-use chunks (not in the chunk manager) in the pool,
4516   // a random chunk list of max. length <list_size> of chunks with the same
4517   // ChunkIndex (chunk size).
4518   // Returns false if list cannot be assembled. List is returned in the <out>
4519   // structure. Returned list may be smaller than <list_size>.
4520   bool assemble_random_chunklist(AChunkList* out, int list_size) {
4521     // Choose a random in-use chunk from the pool...
4522     const int headpos = next_matching_chunk(get_random_position(), false);
4523     if (headpos == -1) {
4524       return false;
4525     }
4526     Metachunk* const head = _pool[headpos];
4527     out->all[0] = head;
4528     assert(head->is_tagged_free() == false, "Chunk state mismatch");
4529     // ..then go from there, chain it up with up to list_size - 1 number of other
4530     // in-use chunks of the same index.
4531     const ChunkIndex index = _cm.list_index(head->word_size());
4532     int num_added = 1;
4533     size_t size_added = head->word_size();
4534     int pos = headpos;
4535     Metachunk* tail = head;
4536     do {
4537       pos = next_matching_chunk(pos, false);
4538       if (pos != headpos) {
4539         Metachunk* c = _pool[pos];
4540         assert(c->is_tagged_free() == false, "Chunk state mismatch");
4541         if (index == _cm.list_index(c->word_size())) {
4542           tail->set_next(c);
4543           c->set_prev(tail);
4544           tail = c;
4545           out->all[num_added] = c;
4546           num_added ++;
4547           size_added += c->word_size();
4548         }
4549       }
4550     } while (num_added < list_size && pos != headpos);
4551     out->head = head;
4552     out->index = index;
4553     out->size = size_added;
4554     out->num = num_added;
4555     return true;
4556   }
4557 
4558   // Take a single random chunk from the ChunkManager.
4559   bool take_single_random_chunk_from_chunkmanager() {
4560     assert_counters();
4561     _cm.locked_verify();
4562     int pos = next_matching_chunk(get_random_position(), true);
4563     if (pos == -1) {
4564       return false;
4565     }
4566     Metachunk* c = _pool[pos];
4567     assert(c->is_tagged_free(), "Chunk state mismatch");
4568     // Note: instead of using ChunkManager::remove_chunk on this one chunk, we call
4569     // ChunkManager::free_chunks_get() with this chunk's word size. We really want
4570     // to exercise ChunkManager::free_chunks_get() because that one gets called for
4571     // normal chunk allocation.
4572     Metachunk* c2 = _cm.free_chunks_get(c->word_size());
4573     assert(c2 != NULL, "Unexpected.");
4574     assert(!c2->is_tagged_free(), "Chunk state mismatch");
4575     assert(c2->next() == NULL && c2->prev() == NULL, "Chunk should be outside of a list.");
4576     _chunks_in_chunkmanager --;
4577     _words_in_chunkmanager -= c->word_size();
4578     assert_counters();
4579     _cm.locked_verify();
4580     return true;
4581   }
4582 
4583   // Returns a single random chunk to the chunk manager. Returns false if that
4584   // was not possible (all chunks are already in the chunk manager).
4585   bool return_single_random_chunk_to_chunkmanager() {
4586     assert_counters();
4587     _cm.locked_verify();
4588     int pos = next_matching_chunk(get_random_position(), false);
4589     if (pos == -1) {
4590       return false;
4591     }
4592     Metachunk* c = _pool[pos];
4593     assert(c->is_tagged_free() == false, "wrong chunk information");
4594     _cm.return_single_chunk(_cm.list_index(c->word_size()), c);
4595     _chunks_in_chunkmanager ++;
4596     _words_in_chunkmanager += c->word_size();
4597     assert(c->is_tagged_free() == true, "wrong chunk information");
4598     assert_counters();
4599     _cm.locked_verify();
4600     return true;
4601   }
4602 
4603   // Return a random chunk list to the chunk manager. Returns the length of the
4604   // returned list.
4605   int return_random_chunk_list_to_chunkmanager(int list_size) {
4606     assert_counters();
4607     _cm.locked_verify();
4608     AChunkList aChunkList;
4609     if (!assemble_random_chunklist(&aChunkList, list_size)) {
4610       return 0;
4611     }
4612     // Before returning chunks are returned, they should be tagged in use.
4613     for (int i = 0; i < aChunkList.num; i ++) {
4614       assert(!aChunkList.all[i]->is_tagged_free(), "chunk state mismatch.");
4615     }
4616     _cm.return_chunk_list(aChunkList.index, aChunkList.head);
4617     _chunks_in_chunkmanager += aChunkList.num;
4618     _words_in_chunkmanager += aChunkList.size;
4619     // After all chunks are returned, check that they are now tagged free.
4620     for (int i = 0; i < aChunkList.num; i ++) {
4621       assert(aChunkList.all[i]->is_tagged_free(), "chunk state mismatch.");
4622     }
4623     assert_counters();
4624     _cm.locked_verify();
4625     return aChunkList.num;
4626   }
4627 
4628 public:
4629 
4630   ChunkManagerReturnTestImpl()
4631     : _vsn(align_up(MediumChunk * num_chunks * 5 * sizeof(MetaWord), Metaspace::reserve_alignment()))
4632     , _cm(SpecializedChunk, SmallChunk, MediumChunk)
4633     , _chunks_in_chunkmanager(0)
4634     , _words_in_chunkmanager(0)
4635   {
4636     MutexLockerEx ml(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
4637     // Allocate virtual space and allocate random chunks. Keep these chunks in the _pool. These chunks are
4638     // "in use", because not yet added to any chunk manager.
4639     _vsn.initialize();
4640     _vsn.expand_by(_vsn.reserved_words(), _vsn.reserved_words());
4641     for (int i = 0; i < num_chunks; i ++) {
4642       const size_t size = get_random_chunk_size();
4643       _pool[i] = _vsn.get_chunk_vs(size);
4644       assert(_pool[i] != NULL, "allocation failed");
4645     }
4646     assert_counters();
4647     _cm.locked_verify();
4648   }
4649 
4650   // Test entry point.
4651   // Return some chunks to the chunk manager (return phase). Take some chunks out (take phase). Repeat.
4652   // Chunks are choosen randomly. Number of chunks to return or taken are choosen randomly, but affected
4653   // by the <phase_length_factor> argument: a factor of 0.0 will cause the test to quickly alternate between
4654   // returning and taking, whereas a factor of 1.0 will take/return all chunks from/to the
4655   // chunks manager, thereby emptying or filling it completely.
4656   void do_test(float phase_length_factor) {
4657     MutexLockerEx ml(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
4658     assert_counters();
4659     // Execute n operations, and operation being the move of a single chunk to/from the chunk manager.
4660     const int num_max_ops = num_chunks * 100;
4661     int num_ops = num_max_ops;
4662     const int average_phase_length = (int)(phase_length_factor * num_chunks);
4663     int num_ops_until_switch = MAX2(1, (average_phase_length + os::random() % 8 - 4));
4664     bool return_phase = true;
4665     while (num_ops > 0) {
4666       int chunks_moved = 0;
4667       if (return_phase) {
4668         // Randomly switch between returning a single chunk or a random length chunk list.
4669         if (os::random() % 2 == 0) {
4670           if (return_single_random_chunk_to_chunkmanager()) {
4671             chunks_moved = 1;
4672           }
4673         } else {
4674           const int list_length = MAX2(1, (os::random() % num_ops_until_switch));
4675           chunks_moved = return_random_chunk_list_to_chunkmanager(list_length);
4676         }
4677       } else {
4678         // Breath out.
4679         if (take_single_random_chunk_from_chunkmanager()) {
4680           chunks_moved = 1;
4681         }
4682       }
4683       num_ops -= chunks_moved;
4684       num_ops_until_switch -= chunks_moved;
4685       if (chunks_moved == 0 || num_ops_until_switch <= 0) {
4686         return_phase = !return_phase;
4687         num_ops_until_switch = MAX2(1, (average_phase_length + os::random() % 8 - 4));
4688       }
4689     }
4690   }
4691 };
4692 
4693 void* setup_chunkmanager_returntests() {
4694   ChunkManagerReturnTestImpl* p = new ChunkManagerReturnTestImpl();
4695   return p;
4696 }
4697 
4698 void teardown_chunkmanager_returntests(void* p) {
4699   delete (ChunkManagerReturnTestImpl*) p;
4700 }
4701 
4702 void run_chunkmanager_returntests(void* p, float phase_length) {
4703   ChunkManagerReturnTestImpl* test = (ChunkManagerReturnTestImpl*) p;
4704   test->do_test(phase_length);
4705 }
4706 
4707 // The following test is placed here instead of a gtest / unittest file
4708 // because the ChunkManager class is only available in this file.
4709 class SpaceManagerTest : AllStatic {
4710   friend void SpaceManager_test_adjust_initial_chunk_size();
4711 
4712   static void test_adjust_initial_chunk_size(bool is_class) {
4713     const size_t smallest = SpaceManager::smallest_chunk_size(is_class);
4714     const size_t normal   = SpaceManager::small_chunk_size(is_class);
4715     const size_t medium   = SpaceManager::medium_chunk_size(is_class);
4716 
4717 #define test_adjust_initial_chunk_size(value, expected, is_class_value)          \
4718     do {                                                                         \
4719       size_t v = value;                                                          \
4720       size_t e = expected;                                                       \
4721       assert(SpaceManager::adjust_initial_chunk_size(v, (is_class_value)) == e,  \
4722              "Expected: " SIZE_FORMAT " got: " SIZE_FORMAT, e, v);               \
4723     } while (0)
4724 
4725     // Smallest (specialized)
4726     test_adjust_initial_chunk_size(1,            smallest, is_class);
4727     test_adjust_initial_chunk_size(smallest - 1, smallest, is_class);
4728     test_adjust_initial_chunk_size(smallest,     smallest, is_class);
4729 
4730     // Small
4731     test_adjust_initial_chunk_size(smallest + 1, normal, is_class);
4732     test_adjust_initial_chunk_size(normal - 1,   normal, is_class);
4733     test_adjust_initial_chunk_size(normal,       normal, is_class);
4734 
4735     // Medium
4736     test_adjust_initial_chunk_size(normal + 1, medium, is_class);
4737     test_adjust_initial_chunk_size(medium - 1, medium, is_class);
4738     test_adjust_initial_chunk_size(medium,     medium, is_class);
4739 
4740     // Humongous
4741     test_adjust_initial_chunk_size(medium + 1, medium + 1, is_class);
4742 
4743 #undef test_adjust_initial_chunk_size
4744   }
4745 
4746   static void test_adjust_initial_chunk_size() {
4747     test_adjust_initial_chunk_size(false);
4748     test_adjust_initial_chunk_size(true);
4749   }
4750 };
4751 
4752 void SpaceManager_test_adjust_initial_chunk_size() {
4753   SpaceManagerTest::test_adjust_initial_chunk_size();
4754 }
4755 
4756 #endif // ASSERT