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