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