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