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