src/share/vm/memory/metaspace.cpp

Fri, 25 Oct 2013 11:05:32 -0400

author
hseigel
date
Fri, 25 Oct 2013 11:05:32 -0400
changeset 6027
a6177f601c64
parent 5945
94c0343b1887
child 6029
209aa13ab8c0
permissions
-rw-r--r--

8026822: metaspace/flags/maxMetaspaceSize throws OOM of unexpected type.java.lang.OutOfMemoryError: Compressed class space
Summary: Incorporate chunk size when seeing if OutOfMemoryError was caused by Metaspace or Compressed class space.
Reviewed-by: stefank, coleenp

     1 /*
     2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    24 #include "precompiled.hpp"
    25 #include "gc_interface/collectedHeap.hpp"
    26 #include "memory/allocation.hpp"
    27 #include "memory/binaryTreeDictionary.hpp"
    28 #include "memory/freeList.hpp"
    29 #include "memory/collectorPolicy.hpp"
    30 #include "memory/filemap.hpp"
    31 #include "memory/freeList.hpp"
    32 #include "memory/gcLocker.hpp"
    33 #include "memory/metachunk.hpp"
    34 #include "memory/metaspace.hpp"
    35 #include "memory/metaspaceShared.hpp"
    36 #include "memory/resourceArea.hpp"
    37 #include "memory/universe.hpp"
    38 #include "runtime/atomic.inline.hpp"
    39 #include "runtime/globals.hpp"
    40 #include "runtime/init.hpp"
    41 #include "runtime/java.hpp"
    42 #include "runtime/mutex.hpp"
    43 #include "runtime/orderAccess.hpp"
    44 #include "services/memTracker.hpp"
    45 #include "services/memoryService.hpp"
    46 #include "utilities/copy.hpp"
    47 #include "utilities/debug.hpp"
    49 typedef BinaryTreeDictionary<Metablock, FreeList> BlockTreeDictionary;
    50 typedef BinaryTreeDictionary<Metachunk, FreeList> ChunkTreeDictionary;
    52 // Set this constant to enable slow integrity checking of the free chunk lists
    53 const bool metaspace_slow_verify = false;
    55 size_t const allocation_from_dictionary_limit = 4 * K;
    57 MetaWord* last_allocated = 0;
    59 size_t Metaspace::_class_metaspace_size;
    61 // Used in declarations in SpaceManager and ChunkManager
    62 enum ChunkIndex {
    63   ZeroIndex = 0,
    64   SpecializedIndex = ZeroIndex,
    65   SmallIndex = SpecializedIndex + 1,
    66   MediumIndex = SmallIndex + 1,
    67   HumongousIndex = MediumIndex + 1,
    68   NumberOfFreeLists = 3,
    69   NumberOfInUseLists = 4
    70 };
    72 enum ChunkSizes {    // in words.
    73   ClassSpecializedChunk = 128,
    74   SpecializedChunk = 128,
    75   ClassSmallChunk = 256,
    76   SmallChunk = 512,
    77   ClassMediumChunk = 4 * K,
    78   MediumChunk = 8 * K,
    79   HumongousChunkGranularity = 8
    80 };
    82 static ChunkIndex next_chunk_index(ChunkIndex i) {
    83   assert(i < NumberOfInUseLists, "Out of bound");
    84   return (ChunkIndex) (i+1);
    85 }
    87 volatile intptr_t MetaspaceGC::_capacity_until_GC = 0;
    88 uint MetaspaceGC::_shrink_factor = 0;
    89 bool MetaspaceGC::_should_concurrent_collect = false;
    91 typedef class FreeList<Metachunk> ChunkList;
    93 // Manages the global free lists of chunks.
    94 class ChunkManager : public CHeapObj<mtInternal> {
    96   // Free list of chunks of different sizes.
    97   //   SpecializedChunk
    98   //   SmallChunk
    99   //   MediumChunk
   100   //   HumongousChunk
   101   ChunkList _free_chunks[NumberOfFreeLists];
   103   //   HumongousChunk
   104   ChunkTreeDictionary _humongous_dictionary;
   106   // ChunkManager in all lists of this type
   107   size_t _free_chunks_total;
   108   size_t _free_chunks_count;
   110   void dec_free_chunks_total(size_t v) {
   111     assert(_free_chunks_count > 0 &&
   112              _free_chunks_total > 0,
   113              "About to go negative");
   114     Atomic::add_ptr(-1, &_free_chunks_count);
   115     jlong minus_v = (jlong) - (jlong) v;
   116     Atomic::add_ptr(minus_v, &_free_chunks_total);
   117   }
   119   // Debug support
   121   size_t sum_free_chunks();
   122   size_t sum_free_chunks_count();
   124   void locked_verify_free_chunks_total();
   125   void slow_locked_verify_free_chunks_total() {
   126     if (metaspace_slow_verify) {
   127       locked_verify_free_chunks_total();
   128     }
   129   }
   130   void locked_verify_free_chunks_count();
   131   void slow_locked_verify_free_chunks_count() {
   132     if (metaspace_slow_verify) {
   133       locked_verify_free_chunks_count();
   134     }
   135   }
   136   void verify_free_chunks_count();
   138  public:
   140   ChunkManager(size_t specialized_size, size_t small_size, size_t medium_size)
   141       : _free_chunks_total(0), _free_chunks_count(0) {
   142     _free_chunks[SpecializedIndex].set_size(specialized_size);
   143     _free_chunks[SmallIndex].set_size(small_size);
   144     _free_chunks[MediumIndex].set_size(medium_size);
   145   }
   147   // add or delete (return) a chunk to the global freelist.
   148   Metachunk* chunk_freelist_allocate(size_t word_size);
   150   // Map a size to a list index assuming that there are lists
   151   // for special, small, medium, and humongous chunks.
   152   static ChunkIndex list_index(size_t size);
   154   // Remove the chunk from its freelist.  It is
   155   // expected to be on one of the _free_chunks[] lists.
   156   void remove_chunk(Metachunk* chunk);
   158   // Add the simple linked list of chunks to the freelist of chunks
   159   // of type index.
   160   void return_chunks(ChunkIndex index, Metachunk* chunks);
   162   // Total of the space in the free chunks list
   163   size_t free_chunks_total_words();
   164   size_t free_chunks_total_bytes();
   166   // Number of chunks in the free chunks list
   167   size_t free_chunks_count();
   169   void inc_free_chunks_total(size_t v, size_t count = 1) {
   170     Atomic::add_ptr(count, &_free_chunks_count);
   171     Atomic::add_ptr(v, &_free_chunks_total);
   172   }
   173   ChunkTreeDictionary* humongous_dictionary() {
   174     return &_humongous_dictionary;
   175   }
   177   ChunkList* free_chunks(ChunkIndex index);
   179   // Returns the list for the given chunk word size.
   180   ChunkList* find_free_chunks_list(size_t word_size);
   182   // Remove from a list by size.  Selects list based on size of chunk.
   183   Metachunk* free_chunks_get(size_t chunk_word_size);
   185   // Debug support
   186   void verify();
   187   void slow_verify() {
   188     if (metaspace_slow_verify) {
   189       verify();
   190     }
   191   }
   192   void locked_verify();
   193   void slow_locked_verify() {
   194     if (metaspace_slow_verify) {
   195       locked_verify();
   196     }
   197   }
   198   void verify_free_chunks_total();
   200   void locked_print_free_chunks(outputStream* st);
   201   void locked_print_sum_free_chunks(outputStream* st);
   203   void print_on(outputStream* st) const;
   204 };
   206 // Used to manage the free list of Metablocks (a block corresponds
   207 // to the allocation of a quantum of metadata).
   208 class BlockFreelist VALUE_OBJ_CLASS_SPEC {
   209   BlockTreeDictionary* _dictionary;
   211   // Only allocate and split from freelist if the size of the allocation
   212   // is at least 1/4th the size of the available block.
   213   const static int WasteMultiplier = 4;
   215   // Accessors
   216   BlockTreeDictionary* dictionary() const { return _dictionary; }
   218  public:
   219   BlockFreelist();
   220   ~BlockFreelist();
   222   // Get and return a block to the free list
   223   MetaWord* get_block(size_t word_size);
   224   void return_block(MetaWord* p, size_t word_size);
   226   size_t total_size() {
   227   if (dictionary() == NULL) {
   228     return 0;
   229   } else {
   230     return dictionary()->total_size();
   231   }
   232 }
   234   void print_on(outputStream* st) const;
   235 };
   237 // A VirtualSpaceList node.
   238 class VirtualSpaceNode : public CHeapObj<mtClass> {
   239   friend class VirtualSpaceList;
   241   // Link to next VirtualSpaceNode
   242   VirtualSpaceNode* _next;
   244   // total in the VirtualSpace
   245   MemRegion _reserved;
   246   ReservedSpace _rs;
   247   VirtualSpace _virtual_space;
   248   MetaWord* _top;
   249   // count of chunks contained in this VirtualSpace
   250   uintx _container_count;
   252   // Convenience functions to access the _virtual_space
   253   char* low()  const { return virtual_space()->low(); }
   254   char* high() const { return virtual_space()->high(); }
   256   // The first Metachunk will be allocated at the bottom of the
   257   // VirtualSpace
   258   Metachunk* first_chunk() { return (Metachunk*) bottom(); }
   260  public:
   262   VirtualSpaceNode(size_t byte_size);
   263   VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs), _container_count(0) {}
   264   ~VirtualSpaceNode();
   266   // Convenience functions for logical bottom and end
   267   MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
   268   MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
   270   size_t reserved_words() const  { return _virtual_space.reserved_size() / BytesPerWord; }
   271   size_t committed_words() const { return _virtual_space.actual_committed_size() / BytesPerWord; }
   273   bool is_pre_committed() const { return _virtual_space.special(); }
   275   // address of next available space in _virtual_space;
   276   // Accessors
   277   VirtualSpaceNode* next() { return _next; }
   278   void set_next(VirtualSpaceNode* v) { _next = v; }
   280   void set_reserved(MemRegion const v) { _reserved = v; }
   281   void set_top(MetaWord* v) { _top = v; }
   283   // Accessors
   284   MemRegion* reserved() { return &_reserved; }
   285   VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; }
   287   // Returns true if "word_size" is available in the VirtualSpace
   288   bool is_available(size_t word_size) { return _top + word_size <= end(); }
   290   MetaWord* top() const { return _top; }
   291   void inc_top(size_t word_size) { _top += word_size; }
   293   uintx container_count() { return _container_count; }
   294   void inc_container_count();
   295   void dec_container_count();
   296 #ifdef ASSERT
   297   uint container_count_slow();
   298   void verify_container_count();
   299 #endif
   301   // used and capacity in this single entry in the list
   302   size_t used_words_in_vs() const;
   303   size_t capacity_words_in_vs() const;
   304   size_t free_words_in_vs() const;
   306   bool initialize();
   308   // get space from the virtual space
   309   Metachunk* take_from_committed(size_t chunk_word_size);
   311   // Allocate a chunk from the virtual space and return it.
   312   Metachunk* get_chunk_vs(size_t chunk_word_size);
   314   // Expands/shrinks the committed space in a virtual space.  Delegates
   315   // to Virtualspace
   316   bool expand_by(size_t min_words, size_t preferred_words);
   318   // In preparation for deleting this node, remove all the chunks
   319   // in the node from any freelist.
   320   void purge(ChunkManager* chunk_manager);
   322 #ifdef ASSERT
   323   // Debug support
   324   void mangle();
   325 #endif
   327   void print_on(outputStream* st) const;
   328 };
   330 #define assert_is_ptr_aligned(ptr, alignment) \
   331   assert(is_ptr_aligned(ptr, alignment),      \
   332     err_msg(PTR_FORMAT " is not aligned to "  \
   333       SIZE_FORMAT, ptr, alignment))
   335 #define assert_is_size_aligned(size, alignment) \
   336   assert(is_size_aligned(size, alignment),      \
   337     err_msg(SIZE_FORMAT " is not aligned to "   \
   338        SIZE_FORMAT, size, alignment))
   341 // Decide if large pages should be committed when the memory is reserved.
   342 static bool should_commit_large_pages_when_reserving(size_t bytes) {
   343   if (UseLargePages && UseLargePagesInMetaspace && !os::can_commit_large_page_memory()) {
   344     size_t words = bytes / BytesPerWord;
   345     bool is_class = false; // We never reserve large pages for the class space.
   346     if (MetaspaceGC::can_expand(words, is_class) &&
   347         MetaspaceGC::allowed_expansion() >= words) {
   348       return true;
   349     }
   350   }
   352   return false;
   353 }
   355   // byte_size is the size of the associated virtualspace.
   356 VirtualSpaceNode::VirtualSpaceNode(size_t bytes) : _top(NULL), _next(NULL), _rs(), _container_count(0) {
   357   assert_is_size_aligned(bytes, Metaspace::reserve_alignment());
   359   // This allocates memory with mmap.  For DumpSharedspaces, try to reserve
   360   // configurable address, generally at the top of the Java heap so other
   361   // memory addresses don't conflict.
   362   if (DumpSharedSpaces) {
   363     bool large_pages = false; // No large pages when dumping the CDS archive.
   364     char* shared_base = (char*)align_ptr_up((char*)SharedBaseAddress, Metaspace::reserve_alignment());
   366     _rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages, shared_base, 0);
   367     if (_rs.is_reserved()) {
   368       assert(shared_base == 0 || _rs.base() == shared_base, "should match");
   369     } else {
   370       // Get a mmap region anywhere if the SharedBaseAddress fails.
   371       _rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages);
   372     }
   373     MetaspaceShared::set_shared_rs(&_rs);
   374   } else {
   375     bool large_pages = should_commit_large_pages_when_reserving(bytes);
   377     _rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages);
   378   }
   380   if (_rs.is_reserved()) {
   381     assert(_rs.base() != NULL, "Catch if we get a NULL address");
   382     assert(_rs.size() != 0, "Catch if we get a 0 size");
   383     assert_is_ptr_aligned(_rs.base(), Metaspace::reserve_alignment());
   384     assert_is_size_aligned(_rs.size(), Metaspace::reserve_alignment());
   386     MemTracker::record_virtual_memory_type((address)_rs.base(), mtClass);
   387   }
   388 }
   390 void VirtualSpaceNode::purge(ChunkManager* chunk_manager) {
   391   Metachunk* chunk = first_chunk();
   392   Metachunk* invalid_chunk = (Metachunk*) top();
   393   while (chunk < invalid_chunk ) {
   394     assert(chunk->is_tagged_free(), "Should be tagged free");
   395     MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
   396     chunk_manager->remove_chunk(chunk);
   397     assert(chunk->next() == NULL &&
   398            chunk->prev() == NULL,
   399            "Was not removed from its list");
   400     chunk = (Metachunk*) next;
   401   }
   402 }
   404 #ifdef ASSERT
   405 uint VirtualSpaceNode::container_count_slow() {
   406   uint count = 0;
   407   Metachunk* chunk = first_chunk();
   408   Metachunk* invalid_chunk = (Metachunk*) top();
   409   while (chunk < invalid_chunk ) {
   410     MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
   411     // Don't count the chunks on the free lists.  Those are
   412     // still part of the VirtualSpaceNode but not currently
   413     // counted.
   414     if (!chunk->is_tagged_free()) {
   415       count++;
   416     }
   417     chunk = (Metachunk*) next;
   418   }
   419   return count;
   420 }
   421 #endif
   423 // List of VirtualSpaces for metadata allocation.
   424 class VirtualSpaceList : public CHeapObj<mtClass> {
   425   friend class VirtualSpaceNode;
   427   enum VirtualSpaceSizes {
   428     VirtualSpaceSize = 256 * K
   429   };
   431   // Head of the list
   432   VirtualSpaceNode* _virtual_space_list;
   433   // virtual space currently being used for allocations
   434   VirtualSpaceNode* _current_virtual_space;
   436   // Is this VirtualSpaceList used for the compressed class space
   437   bool _is_class;
   439   // Sum of reserved and committed memory in the virtual spaces
   440   size_t _reserved_words;
   441   size_t _committed_words;
   443   // Number of virtual spaces
   444   size_t _virtual_space_count;
   446   ~VirtualSpaceList();
   448   VirtualSpaceNode* virtual_space_list() const { return _virtual_space_list; }
   450   void set_virtual_space_list(VirtualSpaceNode* v) {
   451     _virtual_space_list = v;
   452   }
   453   void set_current_virtual_space(VirtualSpaceNode* v) {
   454     _current_virtual_space = v;
   455   }
   457   void link_vs(VirtualSpaceNode* new_entry);
   459   // Get another virtual space and add it to the list.  This
   460   // is typically prompted by a failed attempt to allocate a chunk
   461   // and is typically followed by the allocation of a chunk.
   462   bool create_new_virtual_space(size_t vs_word_size);
   464  public:
   465   VirtualSpaceList(size_t word_size);
   466   VirtualSpaceList(ReservedSpace rs);
   468   size_t free_bytes();
   470   Metachunk* get_new_chunk(size_t word_size,
   471                            size_t grow_chunks_by_words,
   472                            size_t medium_chunk_bunch);
   474   bool expand_node_by(VirtualSpaceNode* node,
   475                       size_t min_words,
   476                       size_t preferred_words);
   478   bool expand_by(size_t min_words,
   479                  size_t preferred_words);
   481   VirtualSpaceNode* current_virtual_space() {
   482     return _current_virtual_space;
   483   }
   485   bool is_class() const { return _is_class; }
   487   bool initialization_succeeded() { return _virtual_space_list != NULL; }
   489   size_t reserved_words()  { return _reserved_words; }
   490   size_t reserved_bytes()  { return reserved_words() * BytesPerWord; }
   491   size_t committed_words() { return _committed_words; }
   492   size_t committed_bytes() { return committed_words() * BytesPerWord; }
   494   void inc_reserved_words(size_t v);
   495   void dec_reserved_words(size_t v);
   496   void inc_committed_words(size_t v);
   497   void dec_committed_words(size_t v);
   498   void inc_virtual_space_count();
   499   void dec_virtual_space_count();
   501   // Unlink empty VirtualSpaceNodes and free it.
   502   void purge(ChunkManager* chunk_manager);
   504   bool contains(const void *ptr);
   506   void print_on(outputStream* st) const;
   508   class VirtualSpaceListIterator : public StackObj {
   509     VirtualSpaceNode* _virtual_spaces;
   510    public:
   511     VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) :
   512       _virtual_spaces(virtual_spaces) {}
   514     bool repeat() {
   515       return _virtual_spaces != NULL;
   516     }
   518     VirtualSpaceNode* get_next() {
   519       VirtualSpaceNode* result = _virtual_spaces;
   520       if (_virtual_spaces != NULL) {
   521         _virtual_spaces = _virtual_spaces->next();
   522       }
   523       return result;
   524     }
   525   };
   526 };
   528 class Metadebug : AllStatic {
   529   // Debugging support for Metaspaces
   530   static int _allocation_fail_alot_count;
   532  public:
   534   static void init_allocation_fail_alot_count();
   535 #ifdef ASSERT
   536   static bool test_metadata_failure();
   537 #endif
   538 };
   540 int Metadebug::_allocation_fail_alot_count = 0;
   542 //  SpaceManager - used by Metaspace to handle allocations
   543 class SpaceManager : public CHeapObj<mtClass> {
   544   friend class Metaspace;
   545   friend class Metadebug;
   547  private:
   549   // protects allocations and contains.
   550   Mutex* const _lock;
   552   // Type of metadata allocated.
   553   Metaspace::MetadataType _mdtype;
   555   // List of chunks in use by this SpaceManager.  Allocations
   556   // are done from the current chunk.  The list is used for deallocating
   557   // chunks when the SpaceManager is freed.
   558   Metachunk* _chunks_in_use[NumberOfInUseLists];
   559   Metachunk* _current_chunk;
   561   // Number of small chunks to allocate to a manager
   562   // If class space manager, small chunks are unlimited
   563   static uint const _small_chunk_limit;
   565   // Sum of all space in allocated chunks
   566   size_t _allocated_blocks_words;
   568   // Sum of all allocated chunks
   569   size_t _allocated_chunks_words;
   570   size_t _allocated_chunks_count;
   572   // Free lists of blocks are per SpaceManager since they
   573   // are assumed to be in chunks in use by the SpaceManager
   574   // and all chunks in use by a SpaceManager are freed when
   575   // the class loader using the SpaceManager is collected.
   576   BlockFreelist _block_freelists;
   578   // protects virtualspace and chunk expansions
   579   static const char*  _expand_lock_name;
   580   static const int    _expand_lock_rank;
   581   static Mutex* const _expand_lock;
   583  private:
   584   // Accessors
   585   Metachunk* chunks_in_use(ChunkIndex index) const { return _chunks_in_use[index]; }
   586   void set_chunks_in_use(ChunkIndex index, Metachunk* v) { _chunks_in_use[index] = v; }
   588   BlockFreelist* block_freelists() const {
   589     return (BlockFreelist*) &_block_freelists;
   590   }
   592   Metaspace::MetadataType mdtype() { return _mdtype; }
   594   VirtualSpaceList* vs_list()   const { return Metaspace::get_space_list(_mdtype); }
   595   ChunkManager* chunk_manager() const { return Metaspace::get_chunk_manager(_mdtype); }
   597   Metachunk* current_chunk() const { return _current_chunk; }
   598   void set_current_chunk(Metachunk* v) {
   599     _current_chunk = v;
   600   }
   602   Metachunk* find_current_chunk(size_t word_size);
   604   // Add chunk to the list of chunks in use
   605   void add_chunk(Metachunk* v, bool make_current);
   606   void retire_current_chunk();
   608   Mutex* lock() const { return _lock; }
   610   const char* chunk_size_name(ChunkIndex index) const;
   612  protected:
   613   void initialize();
   615  public:
   616   SpaceManager(Metaspace::MetadataType mdtype,
   617                Mutex* lock);
   618   ~SpaceManager();
   620   enum ChunkMultiples {
   621     MediumChunkMultiple = 4
   622   };
   624   bool is_class() { return _mdtype == Metaspace::ClassType; }
   626   // Accessors
   627   size_t specialized_chunk_size() { return SpecializedChunk; }
   628   size_t small_chunk_size() { return (size_t) is_class() ? ClassSmallChunk : SmallChunk; }
   629   size_t medium_chunk_size() { return (size_t) is_class() ? ClassMediumChunk : MediumChunk; }
   630   size_t medium_chunk_bunch() { return medium_chunk_size() * MediumChunkMultiple; }
   632   size_t allocated_blocks_words() const { return _allocated_blocks_words; }
   633   size_t allocated_blocks_bytes() const { return _allocated_blocks_words * BytesPerWord; }
   634   size_t allocated_chunks_words() const { return _allocated_chunks_words; }
   635   size_t allocated_chunks_count() const { return _allocated_chunks_count; }
   637   bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); }
   639   static Mutex* expand_lock() { return _expand_lock; }
   641   // Increment the per Metaspace and global running sums for Metachunks
   642   // by the given size.  This is used when a Metachunk to added to
   643   // the in-use list.
   644   void inc_size_metrics(size_t words);
   645   // Increment the per Metaspace and global running sums Metablocks by the given
   646   // size.  This is used when a Metablock is allocated.
   647   void inc_used_metrics(size_t words);
   648   // Delete the portion of the running sums for this SpaceManager. That is,
   649   // the globals running sums for the Metachunks and Metablocks are
   650   // decremented for all the Metachunks in-use by this SpaceManager.
   651   void dec_total_from_size_metrics();
   653   // Set the sizes for the initial chunks.
   654   void get_initial_chunk_sizes(Metaspace::MetaspaceType type,
   655                                size_t* chunk_word_size,
   656                                size_t* class_chunk_word_size);
   658   size_t sum_capacity_in_chunks_in_use() const;
   659   size_t sum_used_in_chunks_in_use() const;
   660   size_t sum_free_in_chunks_in_use() const;
   661   size_t sum_waste_in_chunks_in_use() const;
   662   size_t sum_waste_in_chunks_in_use(ChunkIndex index ) const;
   664   size_t sum_count_in_chunks_in_use();
   665   size_t sum_count_in_chunks_in_use(ChunkIndex i);
   667   Metachunk* get_new_chunk(size_t word_size, size_t grow_chunks_by_words);
   669   // Block allocation and deallocation.
   670   // Allocates a block from the current chunk
   671   MetaWord* allocate(size_t word_size);
   673   // Helper for allocations
   674   MetaWord* allocate_work(size_t word_size);
   676   // Returns a block to the per manager freelist
   677   void deallocate(MetaWord* p, size_t word_size);
   679   // Based on the allocation size and a minimum chunk size,
   680   // returned chunk size (for expanding space for chunk allocation).
   681   size_t calc_chunk_size(size_t allocation_word_size);
   683   // Called when an allocation from the current chunk fails.
   684   // Gets a new chunk (may require getting a new virtual space),
   685   // and allocates from that chunk.
   686   MetaWord* grow_and_allocate(size_t word_size);
   688   // Notify memory usage to MemoryService.
   689   void track_metaspace_memory_usage();
   691   // debugging support.
   693   void dump(outputStream* const out) const;
   694   void print_on(outputStream* st) const;
   695   void locked_print_chunks_in_use_on(outputStream* st) const;
   697   void verify();
   698   void verify_chunk_size(Metachunk* chunk);
   699   NOT_PRODUCT(void mangle_freed_chunks();)
   700 #ifdef ASSERT
   701   void verify_allocated_blocks_words();
   702 #endif
   704   size_t get_raw_word_size(size_t word_size) {
   705     size_t byte_size = word_size * BytesPerWord;
   707     size_t raw_bytes_size = MAX2(byte_size, sizeof(Metablock));
   708     raw_bytes_size = align_size_up(raw_bytes_size, Metachunk::object_alignment());
   710     size_t raw_word_size = raw_bytes_size / BytesPerWord;
   711     assert(raw_word_size * BytesPerWord == raw_bytes_size, "Size problem");
   713     return raw_word_size;
   714   }
   715 };
   717 uint const SpaceManager::_small_chunk_limit = 4;
   719 const char* SpaceManager::_expand_lock_name =
   720   "SpaceManager chunk allocation lock";
   721 const int SpaceManager::_expand_lock_rank = Monitor::leaf - 1;
   722 Mutex* const SpaceManager::_expand_lock =
   723   new Mutex(SpaceManager::_expand_lock_rank,
   724             SpaceManager::_expand_lock_name,
   725             Mutex::_allow_vm_block_flag);
   727 void VirtualSpaceNode::inc_container_count() {
   728   assert_lock_strong(SpaceManager::expand_lock());
   729   _container_count++;
   730   assert(_container_count == container_count_slow(),
   731          err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
   732                  " container_count_slow() " SIZE_FORMAT,
   733                  _container_count, container_count_slow()));
   734 }
   736 void VirtualSpaceNode::dec_container_count() {
   737   assert_lock_strong(SpaceManager::expand_lock());
   738   _container_count--;
   739 }
   741 #ifdef ASSERT
   742 void VirtualSpaceNode::verify_container_count() {
   743   assert(_container_count == container_count_slow(),
   744     err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
   745             " container_count_slow() " SIZE_FORMAT, _container_count, container_count_slow()));
   746 }
   747 #endif
   749 // BlockFreelist methods
   751 BlockFreelist::BlockFreelist() : _dictionary(NULL) {}
   753 BlockFreelist::~BlockFreelist() {
   754   if (_dictionary != NULL) {
   755     if (Verbose && TraceMetadataChunkAllocation) {
   756       _dictionary->print_free_lists(gclog_or_tty);
   757     }
   758     delete _dictionary;
   759   }
   760 }
   762 void BlockFreelist::return_block(MetaWord* p, size_t word_size) {
   763   Metablock* free_chunk = ::new (p) Metablock(word_size);
   764   if (dictionary() == NULL) {
   765    _dictionary = new BlockTreeDictionary();
   766   }
   767   dictionary()->return_chunk(free_chunk);
   768 }
   770 MetaWord* BlockFreelist::get_block(size_t word_size) {
   771   if (dictionary() == NULL) {
   772     return NULL;
   773   }
   775   if (word_size < TreeChunk<Metablock, FreeList>::min_size()) {
   776     // Dark matter.  Too small for dictionary.
   777     return NULL;
   778   }
   780   Metablock* free_block =
   781     dictionary()->get_chunk(word_size, FreeBlockDictionary<Metablock>::atLeast);
   782   if (free_block == NULL) {
   783     return NULL;
   784   }
   786   const size_t block_size = free_block->size();
   787   if (block_size > WasteMultiplier * word_size) {
   788     return_block((MetaWord*)free_block, block_size);
   789     return NULL;
   790   }
   792   MetaWord* new_block = (MetaWord*)free_block;
   793   assert(block_size >= word_size, "Incorrect size of block from freelist");
   794   const size_t unused = block_size - word_size;
   795   if (unused >= TreeChunk<Metablock, FreeList>::min_size()) {
   796     return_block(new_block + word_size, unused);
   797   }
   799   return new_block;
   800 }
   802 void BlockFreelist::print_on(outputStream* st) const {
   803   if (dictionary() == NULL) {
   804     return;
   805   }
   806   dictionary()->print_free_lists(st);
   807 }
   809 // VirtualSpaceNode methods
   811 VirtualSpaceNode::~VirtualSpaceNode() {
   812   _rs.release();
   813 #ifdef ASSERT
   814   size_t word_size = sizeof(*this) / BytesPerWord;
   815   Copy::fill_to_words((HeapWord*) this, word_size, 0xf1f1f1f1);
   816 #endif
   817 }
   819 size_t VirtualSpaceNode::used_words_in_vs() const {
   820   return pointer_delta(top(), bottom(), sizeof(MetaWord));
   821 }
   823 // Space committed in the VirtualSpace
   824 size_t VirtualSpaceNode::capacity_words_in_vs() const {
   825   return pointer_delta(end(), bottom(), sizeof(MetaWord));
   826 }
   828 size_t VirtualSpaceNode::free_words_in_vs() const {
   829   return pointer_delta(end(), top(), sizeof(MetaWord));
   830 }
   832 // Allocates the chunk from the virtual space only.
   833 // This interface is also used internally for debugging.  Not all
   834 // chunks removed here are necessarily used for allocation.
   835 Metachunk* VirtualSpaceNode::take_from_committed(size_t chunk_word_size) {
   836   // Bottom of the new chunk
   837   MetaWord* chunk_limit = top();
   838   assert(chunk_limit != NULL, "Not safe to call this method");
   840   // The virtual spaces are always expanded by the
   841   // commit granularity to enforce the following condition.
   842   // Without this the is_available check will not work correctly.
   843   assert(_virtual_space.committed_size() == _virtual_space.actual_committed_size(),
   844       "The committed memory doesn't match the expanded memory.");
   846   if (!is_available(chunk_word_size)) {
   847     if (TraceMetadataChunkAllocation) {
   848       gclog_or_tty->print("VirtualSpaceNode::take_from_committed() not available %d words ", chunk_word_size);
   849       // Dump some information about the virtual space that is nearly full
   850       print_on(gclog_or_tty);
   851     }
   852     return NULL;
   853   }
   855   // Take the space  (bump top on the current virtual space).
   856   inc_top(chunk_word_size);
   858   // Initialize the chunk
   859   Metachunk* result = ::new (chunk_limit) Metachunk(chunk_word_size, this);
   860   return result;
   861 }
   864 // Expand the virtual space (commit more of the reserved space)
   865 bool VirtualSpaceNode::expand_by(size_t min_words, size_t preferred_words) {
   866   size_t min_bytes = min_words * BytesPerWord;
   867   size_t preferred_bytes = preferred_words * BytesPerWord;
   869   size_t uncommitted = virtual_space()->reserved_size() - virtual_space()->actual_committed_size();
   871   if (uncommitted < min_bytes) {
   872     return false;
   873   }
   875   size_t commit = MIN2(preferred_bytes, uncommitted);
   876   bool result = virtual_space()->expand_by(commit, false);
   878   assert(result, "Failed to commit memory");
   880   return result;
   881 }
   883 Metachunk* VirtualSpaceNode::get_chunk_vs(size_t chunk_word_size) {
   884   assert_lock_strong(SpaceManager::expand_lock());
   885   Metachunk* result = take_from_committed(chunk_word_size);
   886   if (result != NULL) {
   887     inc_container_count();
   888   }
   889   return result;
   890 }
   892 bool VirtualSpaceNode::initialize() {
   894   if (!_rs.is_reserved()) {
   895     return false;
   896   }
   898   // These are necessary restriction to make sure that the virtual space always
   899   // grows in steps of Metaspace::commit_alignment(). If both base and size are
   900   // aligned only the middle alignment of the VirtualSpace is used.
   901   assert_is_ptr_aligned(_rs.base(), Metaspace::commit_alignment());
   902   assert_is_size_aligned(_rs.size(), Metaspace::commit_alignment());
   904   // ReservedSpaces marked as special will have the entire memory
   905   // pre-committed. Setting a committed size will make sure that
   906   // committed_size and actual_committed_size agrees.
   907   size_t pre_committed_size = _rs.special() ? _rs.size() : 0;
   909   bool result = virtual_space()->initialize_with_granularity(_rs, pre_committed_size,
   910                                             Metaspace::commit_alignment());
   911   if (result) {
   912     assert(virtual_space()->committed_size() == virtual_space()->actual_committed_size(),
   913         "Checking that the pre-committed memory was registered by the VirtualSpace");
   915     set_top((MetaWord*)virtual_space()->low());
   916     set_reserved(MemRegion((HeapWord*)_rs.base(),
   917                  (HeapWord*)(_rs.base() + _rs.size())));
   919     assert(reserved()->start() == (HeapWord*) _rs.base(),
   920       err_msg("Reserved start was not set properly " PTR_FORMAT
   921         " != " PTR_FORMAT, reserved()->start(), _rs.base()));
   922     assert(reserved()->word_size() == _rs.size() / BytesPerWord,
   923       err_msg("Reserved size was not set properly " SIZE_FORMAT
   924         " != " SIZE_FORMAT, reserved()->word_size(),
   925         _rs.size() / BytesPerWord));
   926   }
   928   return result;
   929 }
   931 void VirtualSpaceNode::print_on(outputStream* st) const {
   932   size_t used = used_words_in_vs();
   933   size_t capacity = capacity_words_in_vs();
   934   VirtualSpace* vs = virtual_space();
   935   st->print_cr("   space @ " PTR_FORMAT " " SIZE_FORMAT "K, %3d%% used "
   936            "[" PTR_FORMAT ", " PTR_FORMAT ", "
   937            PTR_FORMAT ", " PTR_FORMAT ")",
   938            vs, capacity / K,
   939            capacity == 0 ? 0 : used * 100 / capacity,
   940            bottom(), top(), end(),
   941            vs->high_boundary());
   942 }
   944 #ifdef ASSERT
   945 void VirtualSpaceNode::mangle() {
   946   size_t word_size = capacity_words_in_vs();
   947   Copy::fill_to_words((HeapWord*) low(), word_size, 0xf1f1f1f1);
   948 }
   949 #endif // ASSERT
   951 // VirtualSpaceList methods
   952 // Space allocated from the VirtualSpace
   954 VirtualSpaceList::~VirtualSpaceList() {
   955   VirtualSpaceListIterator iter(virtual_space_list());
   956   while (iter.repeat()) {
   957     VirtualSpaceNode* vsl = iter.get_next();
   958     delete vsl;
   959   }
   960 }
   962 void VirtualSpaceList::inc_reserved_words(size_t v) {
   963   assert_lock_strong(SpaceManager::expand_lock());
   964   _reserved_words = _reserved_words + v;
   965 }
   966 void VirtualSpaceList::dec_reserved_words(size_t v) {
   967   assert_lock_strong(SpaceManager::expand_lock());
   968   _reserved_words = _reserved_words - v;
   969 }
   971 #define assert_committed_below_limit()                             \
   972   assert(MetaspaceAux::committed_bytes() <= MaxMetaspaceSize,      \
   973       err_msg("Too much committed memory. Committed: " SIZE_FORMAT \
   974               " limit (MaxMetaspaceSize): " SIZE_FORMAT,           \
   975           MetaspaceAux::committed_bytes(), MaxMetaspaceSize));
   977 void VirtualSpaceList::inc_committed_words(size_t v) {
   978   assert_lock_strong(SpaceManager::expand_lock());
   979   _committed_words = _committed_words + v;
   981   assert_committed_below_limit();
   982 }
   983 void VirtualSpaceList::dec_committed_words(size_t v) {
   984   assert_lock_strong(SpaceManager::expand_lock());
   985   _committed_words = _committed_words - v;
   987   assert_committed_below_limit();
   988 }
   990 void VirtualSpaceList::inc_virtual_space_count() {
   991   assert_lock_strong(SpaceManager::expand_lock());
   992   _virtual_space_count++;
   993 }
   994 void VirtualSpaceList::dec_virtual_space_count() {
   995   assert_lock_strong(SpaceManager::expand_lock());
   996   _virtual_space_count--;
   997 }
   999 void ChunkManager::remove_chunk(Metachunk* chunk) {
  1000   size_t word_size = chunk->word_size();
  1001   ChunkIndex index = list_index(word_size);
  1002   if (index != HumongousIndex) {
  1003     free_chunks(index)->remove_chunk(chunk);
  1004   } else {
  1005     humongous_dictionary()->remove_chunk(chunk);
  1008   // Chunk is being removed from the chunks free list.
  1009   dec_free_chunks_total(chunk->word_size());
  1012 // Walk the list of VirtualSpaceNodes and delete
  1013 // nodes with a 0 container_count.  Remove Metachunks in
  1014 // the node from their respective freelists.
  1015 void VirtualSpaceList::purge(ChunkManager* chunk_manager) {
  1016   assert_lock_strong(SpaceManager::expand_lock());
  1017   // Don't use a VirtualSpaceListIterator because this
  1018   // list is being changed and a straightforward use of an iterator is not safe.
  1019   VirtualSpaceNode* purged_vsl = NULL;
  1020   VirtualSpaceNode* prev_vsl = virtual_space_list();
  1021   VirtualSpaceNode* next_vsl = prev_vsl;
  1022   while (next_vsl != NULL) {
  1023     VirtualSpaceNode* vsl = next_vsl;
  1024     next_vsl = vsl->next();
  1025     // Don't free the current virtual space since it will likely
  1026     // be needed soon.
  1027     if (vsl->container_count() == 0 && vsl != current_virtual_space()) {
  1028       // Unlink it from the list
  1029       if (prev_vsl == vsl) {
  1030         // This is the case of the current node being the first node.
  1031         assert(vsl == virtual_space_list(), "Expected to be the first node");
  1032         set_virtual_space_list(vsl->next());
  1033       } else {
  1034         prev_vsl->set_next(vsl->next());
  1037       vsl->purge(chunk_manager);
  1038       dec_reserved_words(vsl->reserved_words());
  1039       dec_committed_words(vsl->committed_words());
  1040       dec_virtual_space_count();
  1041       purged_vsl = vsl;
  1042       delete vsl;
  1043     } else {
  1044       prev_vsl = vsl;
  1047 #ifdef ASSERT
  1048   if (purged_vsl != NULL) {
  1049   // List should be stable enough to use an iterator here.
  1050   VirtualSpaceListIterator iter(virtual_space_list());
  1051     while (iter.repeat()) {
  1052       VirtualSpaceNode* vsl = iter.get_next();
  1053       assert(vsl != purged_vsl, "Purge of vsl failed");
  1056 #endif
  1059 VirtualSpaceList::VirtualSpaceList(size_t word_size) :
  1060                                    _is_class(false),
  1061                                    _virtual_space_list(NULL),
  1062                                    _current_virtual_space(NULL),
  1063                                    _reserved_words(0),
  1064                                    _committed_words(0),
  1065                                    _virtual_space_count(0) {
  1066   MutexLockerEx cl(SpaceManager::expand_lock(),
  1067                    Mutex::_no_safepoint_check_flag);
  1068   create_new_virtual_space(word_size);
  1071 VirtualSpaceList::VirtualSpaceList(ReservedSpace rs) :
  1072                                    _is_class(true),
  1073                                    _virtual_space_list(NULL),
  1074                                    _current_virtual_space(NULL),
  1075                                    _reserved_words(0),
  1076                                    _committed_words(0),
  1077                                    _virtual_space_count(0) {
  1078   MutexLockerEx cl(SpaceManager::expand_lock(),
  1079                    Mutex::_no_safepoint_check_flag);
  1080   VirtualSpaceNode* class_entry = new VirtualSpaceNode(rs);
  1081   bool succeeded = class_entry->initialize();
  1082   if (succeeded) {
  1083     link_vs(class_entry);
  1087 size_t VirtualSpaceList::free_bytes() {
  1088   return virtual_space_list()->free_words_in_vs() * BytesPerWord;
  1091 // Allocate another meta virtual space and add it to the list.
  1092 bool VirtualSpaceList::create_new_virtual_space(size_t vs_word_size) {
  1093   assert_lock_strong(SpaceManager::expand_lock());
  1095   if (is_class()) {
  1096     assert(false, "We currently don't support more than one VirtualSpace for"
  1097                   " the compressed class space. The initialization of the"
  1098                   " CCS uses another code path and should not hit this path.");
  1099     return false;
  1102   if (vs_word_size == 0) {
  1103     assert(false, "vs_word_size should always be at least _reserve_alignment large.");
  1104     return false;
  1107   // Reserve the space
  1108   size_t vs_byte_size = vs_word_size * BytesPerWord;
  1109   assert_is_size_aligned(vs_byte_size, Metaspace::reserve_alignment());
  1111   // Allocate the meta virtual space and initialize it.
  1112   VirtualSpaceNode* new_entry = new VirtualSpaceNode(vs_byte_size);
  1113   if (!new_entry->initialize()) {
  1114     delete new_entry;
  1115     return false;
  1116   } else {
  1117     assert(new_entry->reserved_words() == vs_word_size,
  1118         "Reserved memory size differs from requested memory size");
  1119     // ensure lock-free iteration sees fully initialized node
  1120     OrderAccess::storestore();
  1121     link_vs(new_entry);
  1122     return true;
  1126 void VirtualSpaceList::link_vs(VirtualSpaceNode* new_entry) {
  1127   if (virtual_space_list() == NULL) {
  1128       set_virtual_space_list(new_entry);
  1129   } else {
  1130     current_virtual_space()->set_next(new_entry);
  1132   set_current_virtual_space(new_entry);
  1133   inc_reserved_words(new_entry->reserved_words());
  1134   inc_committed_words(new_entry->committed_words());
  1135   inc_virtual_space_count();
  1136 #ifdef ASSERT
  1137   new_entry->mangle();
  1138 #endif
  1139   if (TraceMetavirtualspaceAllocation && Verbose) {
  1140     VirtualSpaceNode* vsl = current_virtual_space();
  1141     vsl->print_on(gclog_or_tty);
  1145 bool VirtualSpaceList::expand_node_by(VirtualSpaceNode* node,
  1146                                       size_t min_words,
  1147                                       size_t preferred_words) {
  1148   size_t before = node->committed_words();
  1150   bool result = node->expand_by(min_words, preferred_words);
  1152   size_t after = node->committed_words();
  1154   // after and before can be the same if the memory was pre-committed.
  1155   assert(after >= before, "Inconsistency");
  1156   inc_committed_words(after - before);
  1158   return result;
  1161 bool VirtualSpaceList::expand_by(size_t min_words, size_t preferred_words) {
  1162   assert_is_size_aligned(min_words,       Metaspace::commit_alignment_words());
  1163   assert_is_size_aligned(preferred_words, Metaspace::commit_alignment_words());
  1164   assert(min_words <= preferred_words, "Invalid arguments");
  1166   if (!MetaspaceGC::can_expand(min_words, this->is_class())) {
  1167     return  false;
  1170   size_t allowed_expansion_words = MetaspaceGC::allowed_expansion();
  1171   if (allowed_expansion_words < min_words) {
  1172     return false;
  1175   size_t max_expansion_words = MIN2(preferred_words, allowed_expansion_words);
  1177   // Commit more memory from the the current virtual space.
  1178   bool vs_expanded = expand_node_by(current_virtual_space(),
  1179                                     min_words,
  1180                                     max_expansion_words);
  1181   if (vs_expanded) {
  1182     return true;
  1185   // Get another virtual space.
  1186   size_t grow_vs_words = MAX2((size_t)VirtualSpaceSize, preferred_words);
  1187   grow_vs_words = align_size_up(grow_vs_words, Metaspace::reserve_alignment_words());
  1189   if (create_new_virtual_space(grow_vs_words)) {
  1190     if (current_virtual_space()->is_pre_committed()) {
  1191       // The memory was pre-committed, so we are done here.
  1192       assert(min_words <= current_virtual_space()->committed_words(),
  1193           "The new VirtualSpace was pre-committed, so it"
  1194           "should be large enough to fit the alloc request.");
  1195       return true;
  1198     return expand_node_by(current_virtual_space(),
  1199                           min_words,
  1200                           max_expansion_words);
  1203   return false;
  1206 Metachunk* VirtualSpaceList::get_new_chunk(size_t word_size,
  1207                                            size_t grow_chunks_by_words,
  1208                                            size_t medium_chunk_bunch) {
  1210   // Allocate a chunk out of the current virtual space.
  1211   Metachunk* next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
  1213   if (next != NULL) {
  1214     return next;
  1217   // The expand amount is currently only determined by the requested sizes
  1218   // and not how much committed memory is left in the current virtual space.
  1220   size_t min_word_size       = align_size_up(grow_chunks_by_words, Metaspace::commit_alignment_words());
  1221   size_t preferred_word_size = align_size_up(medium_chunk_bunch,   Metaspace::commit_alignment_words());
  1222   if (min_word_size >= preferred_word_size) {
  1223     // Can happen when humongous chunks are allocated.
  1224     preferred_word_size = min_word_size;
  1227   bool expanded = expand_by(min_word_size, preferred_word_size);
  1228   if (expanded) {
  1229     next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
  1230     assert(next != NULL, "The allocation was expected to succeed after the expansion");
  1233    return next;
  1236 void VirtualSpaceList::print_on(outputStream* st) const {
  1237   if (TraceMetadataChunkAllocation && Verbose) {
  1238     VirtualSpaceListIterator iter(virtual_space_list());
  1239     while (iter.repeat()) {
  1240       VirtualSpaceNode* node = iter.get_next();
  1241       node->print_on(st);
  1246 bool VirtualSpaceList::contains(const void *ptr) {
  1247   VirtualSpaceNode* list = virtual_space_list();
  1248   VirtualSpaceListIterator iter(list);
  1249   while (iter.repeat()) {
  1250     VirtualSpaceNode* node = iter.get_next();
  1251     if (node->reserved()->contains(ptr)) {
  1252       return true;
  1255   return false;
  1259 // MetaspaceGC methods
  1261 // VM_CollectForMetadataAllocation is the vm operation used to GC.
  1262 // Within the VM operation after the GC the attempt to allocate the metadata
  1263 // should succeed.  If the GC did not free enough space for the metaspace
  1264 // allocation, the HWM is increased so that another virtualspace will be
  1265 // allocated for the metadata.  With perm gen the increase in the perm
  1266 // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion.  The
  1267 // metaspace policy uses those as the small and large steps for the HWM.
  1268 //
  1269 // After the GC the compute_new_size() for MetaspaceGC is called to
  1270 // resize the capacity of the metaspaces.  The current implementation
  1271 // is based on the flags MinMetaspaceFreeRatio and MaxMetaspaceFreeRatio used
  1272 // to resize the Java heap by some GC's.  New flags can be implemented
  1273 // if really needed.  MinMetaspaceFreeRatio is used to calculate how much
  1274 // free space is desirable in the metaspace capacity to decide how much
  1275 // to increase the HWM.  MaxMetaspaceFreeRatio is used to decide how much
  1276 // free space is desirable in the metaspace capacity before decreasing
  1277 // the HWM.
  1279 // Calculate the amount to increase the high water mark (HWM).
  1280 // Increase by a minimum amount (MinMetaspaceExpansion) so that
  1281 // another expansion is not requested too soon.  If that is not
  1282 // enough to satisfy the allocation, increase by MaxMetaspaceExpansion.
  1283 // If that is still not enough, expand by the size of the allocation
  1284 // plus some.
  1285 size_t MetaspaceGC::delta_capacity_until_GC(size_t bytes) {
  1286   size_t min_delta = MinMetaspaceExpansion;
  1287   size_t max_delta = MaxMetaspaceExpansion;
  1288   size_t delta = align_size_up(bytes, Metaspace::commit_alignment());
  1290   if (delta <= min_delta) {
  1291     delta = min_delta;
  1292   } else if (delta <= max_delta) {
  1293     // Don't want to hit the high water mark on the next
  1294     // allocation so make the delta greater than just enough
  1295     // for this allocation.
  1296     delta = max_delta;
  1297   } else {
  1298     // This allocation is large but the next ones are probably not
  1299     // so increase by the minimum.
  1300     delta = delta + min_delta;
  1303   assert_is_size_aligned(delta, Metaspace::commit_alignment());
  1305   return delta;
  1308 size_t MetaspaceGC::capacity_until_GC() {
  1309   size_t value = (size_t)OrderAccess::load_ptr_acquire(&_capacity_until_GC);
  1310   assert(value >= MetaspaceSize, "Not initialied properly?");
  1311   return value;
  1314 size_t MetaspaceGC::inc_capacity_until_GC(size_t v) {
  1315   assert_is_size_aligned(v, Metaspace::commit_alignment());
  1317   return (size_t)Atomic::add_ptr(v, &_capacity_until_GC);
  1320 size_t MetaspaceGC::dec_capacity_until_GC(size_t v) {
  1321   assert_is_size_aligned(v, Metaspace::commit_alignment());
  1323   return (size_t)Atomic::add_ptr(-(intptr_t)v, &_capacity_until_GC);
  1326 bool MetaspaceGC::can_expand(size_t word_size, bool is_class) {
  1327   // Check if the compressed class space is full.
  1328   if (is_class && Metaspace::using_class_space()) {
  1329     size_t class_committed = MetaspaceAux::committed_bytes(Metaspace::ClassType);
  1330     if (class_committed + word_size * BytesPerWord > CompressedClassSpaceSize) {
  1331       return false;
  1335   // Check if the user has imposed a limit on the metaspace memory.
  1336   size_t committed_bytes = MetaspaceAux::committed_bytes();
  1337   if (committed_bytes + word_size * BytesPerWord > MaxMetaspaceSize) {
  1338     return false;
  1341   return true;
  1344 size_t MetaspaceGC::allowed_expansion() {
  1345   size_t committed_bytes = MetaspaceAux::committed_bytes();
  1347   size_t left_until_max  = MaxMetaspaceSize - committed_bytes;
  1349   // Always grant expansion if we are initiating the JVM,
  1350   // or if the GC_locker is preventing GCs.
  1351   if (!is_init_completed() || GC_locker::is_active_and_needs_gc()) {
  1352     return left_until_max / BytesPerWord;
  1355   size_t capacity_until_gc = capacity_until_GC();
  1357   if (capacity_until_gc <= committed_bytes) {
  1358     return 0;
  1361   size_t left_until_GC = capacity_until_gc - committed_bytes;
  1362   size_t left_to_commit = MIN2(left_until_GC, left_until_max);
  1364   return left_to_commit / BytesPerWord;
  1367 void MetaspaceGC::compute_new_size() {
  1368   assert(_shrink_factor <= 100, "invalid shrink factor");
  1369   uint current_shrink_factor = _shrink_factor;
  1370   _shrink_factor = 0;
  1372   const size_t used_after_gc = MetaspaceAux::allocated_capacity_bytes();
  1373   const size_t capacity_until_GC = MetaspaceGC::capacity_until_GC();
  1375   const double minimum_free_percentage = MinMetaspaceFreeRatio / 100.0;
  1376   const double maximum_used_percentage = 1.0 - minimum_free_percentage;
  1378   const double min_tmp = used_after_gc / maximum_used_percentage;
  1379   size_t minimum_desired_capacity =
  1380     (size_t)MIN2(min_tmp, double(max_uintx));
  1381   // Don't shrink less than the initial generation size
  1382   minimum_desired_capacity = MAX2(minimum_desired_capacity,
  1383                                   MetaspaceSize);
  1385   if (PrintGCDetails && Verbose) {
  1386     gclog_or_tty->print_cr("\nMetaspaceGC::compute_new_size: ");
  1387     gclog_or_tty->print_cr("  "
  1388                   "  minimum_free_percentage: %6.2f"
  1389                   "  maximum_used_percentage: %6.2f",
  1390                   minimum_free_percentage,
  1391                   maximum_used_percentage);
  1392     gclog_or_tty->print_cr("  "
  1393                   "   used_after_gc       : %6.1fKB",
  1394                   used_after_gc / (double) K);
  1398   size_t shrink_bytes = 0;
  1399   if (capacity_until_GC < minimum_desired_capacity) {
  1400     // If we have less capacity below the metaspace HWM, then
  1401     // increment the HWM.
  1402     size_t expand_bytes = minimum_desired_capacity - capacity_until_GC;
  1403     expand_bytes = align_size_up(expand_bytes, Metaspace::commit_alignment());
  1404     // Don't expand unless it's significant
  1405     if (expand_bytes >= MinMetaspaceExpansion) {
  1406       MetaspaceGC::inc_capacity_until_GC(expand_bytes);
  1408     if (PrintGCDetails && Verbose) {
  1409       size_t new_capacity_until_GC = capacity_until_GC;
  1410       gclog_or_tty->print_cr("    expanding:"
  1411                     "  minimum_desired_capacity: %6.1fKB"
  1412                     "  expand_bytes: %6.1fKB"
  1413                     "  MinMetaspaceExpansion: %6.1fKB"
  1414                     "  new metaspace HWM:  %6.1fKB",
  1415                     minimum_desired_capacity / (double) K,
  1416                     expand_bytes / (double) K,
  1417                     MinMetaspaceExpansion / (double) K,
  1418                     new_capacity_until_GC / (double) K);
  1420     return;
  1423   // No expansion, now see if we want to shrink
  1424   // We would never want to shrink more than this
  1425   size_t max_shrink_bytes = capacity_until_GC - minimum_desired_capacity;
  1426   assert(max_shrink_bytes >= 0, err_msg("max_shrink_bytes " SIZE_FORMAT,
  1427     max_shrink_bytes));
  1429   // Should shrinking be considered?
  1430   if (MaxMetaspaceFreeRatio < 100) {
  1431     const double maximum_free_percentage = MaxMetaspaceFreeRatio / 100.0;
  1432     const double minimum_used_percentage = 1.0 - maximum_free_percentage;
  1433     const double max_tmp = used_after_gc / minimum_used_percentage;
  1434     size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx));
  1435     maximum_desired_capacity = MAX2(maximum_desired_capacity,
  1436                                     MetaspaceSize);
  1437     if (PrintGCDetails && Verbose) {
  1438       gclog_or_tty->print_cr("  "
  1439                              "  maximum_free_percentage: %6.2f"
  1440                              "  minimum_used_percentage: %6.2f",
  1441                              maximum_free_percentage,
  1442                              minimum_used_percentage);
  1443       gclog_or_tty->print_cr("  "
  1444                              "  minimum_desired_capacity: %6.1fKB"
  1445                              "  maximum_desired_capacity: %6.1fKB",
  1446                              minimum_desired_capacity / (double) K,
  1447                              maximum_desired_capacity / (double) K);
  1450     assert(minimum_desired_capacity <= maximum_desired_capacity,
  1451            "sanity check");
  1453     if (capacity_until_GC > maximum_desired_capacity) {
  1454       // Capacity too large, compute shrinking size
  1455       shrink_bytes = capacity_until_GC - maximum_desired_capacity;
  1456       // We don't want shrink all the way back to initSize if people call
  1457       // System.gc(), because some programs do that between "phases" and then
  1458       // we'd just have to grow the heap up again for the next phase.  So we
  1459       // damp the shrinking: 0% on the first call, 10% on the second call, 40%
  1460       // on the third call, and 100% by the fourth call.  But if we recompute
  1461       // size without shrinking, it goes back to 0%.
  1462       shrink_bytes = shrink_bytes / 100 * current_shrink_factor;
  1464       shrink_bytes = align_size_down(shrink_bytes, Metaspace::commit_alignment());
  1466       assert(shrink_bytes <= max_shrink_bytes,
  1467         err_msg("invalid shrink size " SIZE_FORMAT " not <= " SIZE_FORMAT,
  1468           shrink_bytes, max_shrink_bytes));
  1469       if (current_shrink_factor == 0) {
  1470         _shrink_factor = 10;
  1471       } else {
  1472         _shrink_factor = MIN2(current_shrink_factor * 4, (uint) 100);
  1474       if (PrintGCDetails && Verbose) {
  1475         gclog_or_tty->print_cr("  "
  1476                       "  shrinking:"
  1477                       "  initSize: %.1fK"
  1478                       "  maximum_desired_capacity: %.1fK",
  1479                       MetaspaceSize / (double) K,
  1480                       maximum_desired_capacity / (double) K);
  1481         gclog_or_tty->print_cr("  "
  1482                       "  shrink_bytes: %.1fK"
  1483                       "  current_shrink_factor: %d"
  1484                       "  new shrink factor: %d"
  1485                       "  MinMetaspaceExpansion: %.1fK",
  1486                       shrink_bytes / (double) K,
  1487                       current_shrink_factor,
  1488                       _shrink_factor,
  1489                       MinMetaspaceExpansion / (double) K);
  1494   // Don't shrink unless it's significant
  1495   if (shrink_bytes >= MinMetaspaceExpansion &&
  1496       ((capacity_until_GC - shrink_bytes) >= MetaspaceSize)) {
  1497     MetaspaceGC::dec_capacity_until_GC(shrink_bytes);
  1501 // Metadebug methods
  1503 void Metadebug::init_allocation_fail_alot_count() {
  1504   if (MetadataAllocationFailALot) {
  1505     _allocation_fail_alot_count =
  1506       1+(long)((double)MetadataAllocationFailALotInterval*os::random()/(max_jint+1.0));
  1510 #ifdef ASSERT
  1511 bool Metadebug::test_metadata_failure() {
  1512   if (MetadataAllocationFailALot &&
  1513       Threads::is_vm_complete()) {
  1514     if (_allocation_fail_alot_count > 0) {
  1515       _allocation_fail_alot_count--;
  1516     } else {
  1517       if (TraceMetadataChunkAllocation && Verbose) {
  1518         gclog_or_tty->print_cr("Metadata allocation failing for "
  1519                                "MetadataAllocationFailALot");
  1521       init_allocation_fail_alot_count();
  1522       return true;
  1525   return false;
  1527 #endif
  1529 // ChunkManager methods
  1531 size_t ChunkManager::free_chunks_total_words() {
  1532   return _free_chunks_total;
  1535 size_t ChunkManager::free_chunks_total_bytes() {
  1536   return free_chunks_total_words() * BytesPerWord;
  1539 size_t ChunkManager::free_chunks_count() {
  1540 #ifdef ASSERT
  1541   if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) {
  1542     MutexLockerEx cl(SpaceManager::expand_lock(),
  1543                      Mutex::_no_safepoint_check_flag);
  1544     // This lock is only needed in debug because the verification
  1545     // of the _free_chunks_totals walks the list of free chunks
  1546     slow_locked_verify_free_chunks_count();
  1548 #endif
  1549   return _free_chunks_count;
  1552 void ChunkManager::locked_verify_free_chunks_total() {
  1553   assert_lock_strong(SpaceManager::expand_lock());
  1554   assert(sum_free_chunks() == _free_chunks_total,
  1555     err_msg("_free_chunks_total " SIZE_FORMAT " is not the"
  1556            " same as sum " SIZE_FORMAT, _free_chunks_total,
  1557            sum_free_chunks()));
  1560 void ChunkManager::verify_free_chunks_total() {
  1561   MutexLockerEx cl(SpaceManager::expand_lock(),
  1562                      Mutex::_no_safepoint_check_flag);
  1563   locked_verify_free_chunks_total();
  1566 void ChunkManager::locked_verify_free_chunks_count() {
  1567   assert_lock_strong(SpaceManager::expand_lock());
  1568   assert(sum_free_chunks_count() == _free_chunks_count,
  1569     err_msg("_free_chunks_count " SIZE_FORMAT " is not the"
  1570            " same as sum " SIZE_FORMAT, _free_chunks_count,
  1571            sum_free_chunks_count()));
  1574 void ChunkManager::verify_free_chunks_count() {
  1575 #ifdef ASSERT
  1576   MutexLockerEx cl(SpaceManager::expand_lock(),
  1577                      Mutex::_no_safepoint_check_flag);
  1578   locked_verify_free_chunks_count();
  1579 #endif
  1582 void ChunkManager::verify() {
  1583   MutexLockerEx cl(SpaceManager::expand_lock(),
  1584                      Mutex::_no_safepoint_check_flag);
  1585   locked_verify();
  1588 void ChunkManager::locked_verify() {
  1589   locked_verify_free_chunks_count();
  1590   locked_verify_free_chunks_total();
  1593 void ChunkManager::locked_print_free_chunks(outputStream* st) {
  1594   assert_lock_strong(SpaceManager::expand_lock());
  1595   st->print_cr("Free chunk total " SIZE_FORMAT "  count " SIZE_FORMAT,
  1596                 _free_chunks_total, _free_chunks_count);
  1599 void ChunkManager::locked_print_sum_free_chunks(outputStream* st) {
  1600   assert_lock_strong(SpaceManager::expand_lock());
  1601   st->print_cr("Sum free chunk total " SIZE_FORMAT "  count " SIZE_FORMAT,
  1602                 sum_free_chunks(), sum_free_chunks_count());
  1604 ChunkList* ChunkManager::free_chunks(ChunkIndex index) {
  1605   return &_free_chunks[index];
  1608 // These methods that sum the free chunk lists are used in printing
  1609 // methods that are used in product builds.
  1610 size_t ChunkManager::sum_free_chunks() {
  1611   assert_lock_strong(SpaceManager::expand_lock());
  1612   size_t result = 0;
  1613   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
  1614     ChunkList* list = free_chunks(i);
  1616     if (list == NULL) {
  1617       continue;
  1620     result = result + list->count() * list->size();
  1622   result = result + humongous_dictionary()->total_size();
  1623   return result;
  1626 size_t ChunkManager::sum_free_chunks_count() {
  1627   assert_lock_strong(SpaceManager::expand_lock());
  1628   size_t count = 0;
  1629   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
  1630     ChunkList* list = free_chunks(i);
  1631     if (list == NULL) {
  1632       continue;
  1634     count = count + list->count();
  1636   count = count + humongous_dictionary()->total_free_blocks();
  1637   return count;
  1640 ChunkList* ChunkManager::find_free_chunks_list(size_t word_size) {
  1641   ChunkIndex index = list_index(word_size);
  1642   assert(index < HumongousIndex, "No humongous list");
  1643   return free_chunks(index);
  1646 Metachunk* ChunkManager::free_chunks_get(size_t word_size) {
  1647   assert_lock_strong(SpaceManager::expand_lock());
  1649   slow_locked_verify();
  1651   Metachunk* chunk = NULL;
  1652   if (list_index(word_size) != HumongousIndex) {
  1653     ChunkList* free_list = find_free_chunks_list(word_size);
  1654     assert(free_list != NULL, "Sanity check");
  1656     chunk = free_list->head();
  1658     if (chunk == NULL) {
  1659       return NULL;
  1662     // Remove the chunk as the head of the list.
  1663     free_list->remove_chunk(chunk);
  1665     if (TraceMetadataChunkAllocation && Verbose) {
  1666       gclog_or_tty->print_cr("ChunkManager::free_chunks_get: free_list "
  1667                              PTR_FORMAT " head " PTR_FORMAT " size " SIZE_FORMAT,
  1668                              free_list, chunk, chunk->word_size());
  1670   } else {
  1671     chunk = humongous_dictionary()->get_chunk(
  1672       word_size,
  1673       FreeBlockDictionary<Metachunk>::atLeast);
  1675     if (chunk == NULL) {
  1676       return NULL;
  1679     if (TraceMetadataHumongousAllocation) {
  1680       size_t waste = chunk->word_size() - word_size;
  1681       gclog_or_tty->print_cr("Free list allocate humongous chunk size "
  1682                              SIZE_FORMAT " for requested size " SIZE_FORMAT
  1683                              " waste " SIZE_FORMAT,
  1684                              chunk->word_size(), word_size, waste);
  1688   // Chunk is being removed from the chunks free list.
  1689   dec_free_chunks_total(chunk->word_size());
  1691   // Remove it from the links to this freelist
  1692   chunk->set_next(NULL);
  1693   chunk->set_prev(NULL);
  1694 #ifdef ASSERT
  1695   // Chunk is no longer on any freelist. Setting to false make container_count_slow()
  1696   // work.
  1697   chunk->set_is_tagged_free(false);
  1698 #endif
  1699   chunk->container()->inc_container_count();
  1701   slow_locked_verify();
  1702   return chunk;
  1705 Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) {
  1706   assert_lock_strong(SpaceManager::expand_lock());
  1707   slow_locked_verify();
  1709   // Take from the beginning of the list
  1710   Metachunk* chunk = free_chunks_get(word_size);
  1711   if (chunk == NULL) {
  1712     return NULL;
  1715   assert((word_size <= chunk->word_size()) ||
  1716          list_index(chunk->word_size() == HumongousIndex),
  1717          "Non-humongous variable sized chunk");
  1718   if (TraceMetadataChunkAllocation) {
  1719     size_t list_count;
  1720     if (list_index(word_size) < HumongousIndex) {
  1721       ChunkList* list = find_free_chunks_list(word_size);
  1722       list_count = list->count();
  1723     } else {
  1724       list_count = humongous_dictionary()->total_count();
  1726     gclog_or_tty->print("ChunkManager::chunk_freelist_allocate: " PTR_FORMAT " chunk "
  1727                         PTR_FORMAT "  size " SIZE_FORMAT " count " SIZE_FORMAT " ",
  1728                         this, chunk, chunk->word_size(), list_count);
  1729     locked_print_free_chunks(gclog_or_tty);
  1732   return chunk;
  1735 void ChunkManager::print_on(outputStream* out) const {
  1736   if (PrintFLSStatistics != 0) {
  1737     const_cast<ChunkManager *>(this)->humongous_dictionary()->report_statistics();
  1741 // SpaceManager methods
  1743 void SpaceManager::get_initial_chunk_sizes(Metaspace::MetaspaceType type,
  1744                                            size_t* chunk_word_size,
  1745                                            size_t* class_chunk_word_size) {
  1746   switch (type) {
  1747   case Metaspace::BootMetaspaceType:
  1748     *chunk_word_size = Metaspace::first_chunk_word_size();
  1749     *class_chunk_word_size = Metaspace::first_class_chunk_word_size();
  1750     break;
  1751   case Metaspace::ROMetaspaceType:
  1752     *chunk_word_size = SharedReadOnlySize / wordSize;
  1753     *class_chunk_word_size = ClassSpecializedChunk;
  1754     break;
  1755   case Metaspace::ReadWriteMetaspaceType:
  1756     *chunk_word_size = SharedReadWriteSize / wordSize;
  1757     *class_chunk_word_size = ClassSpecializedChunk;
  1758     break;
  1759   case Metaspace::AnonymousMetaspaceType:
  1760   case Metaspace::ReflectionMetaspaceType:
  1761     *chunk_word_size = SpecializedChunk;
  1762     *class_chunk_word_size = ClassSpecializedChunk;
  1763     break;
  1764   default:
  1765     *chunk_word_size = SmallChunk;
  1766     *class_chunk_word_size = ClassSmallChunk;
  1767     break;
  1769   assert(*chunk_word_size != 0 && *class_chunk_word_size != 0,
  1770     err_msg("Initial chunks sizes bad: data  " SIZE_FORMAT
  1771             " class " SIZE_FORMAT,
  1772             *chunk_word_size, *class_chunk_word_size));
  1775 size_t SpaceManager::sum_free_in_chunks_in_use() const {
  1776   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  1777   size_t free = 0;
  1778   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1779     Metachunk* chunk = chunks_in_use(i);
  1780     while (chunk != NULL) {
  1781       free += chunk->free_word_size();
  1782       chunk = chunk->next();
  1785   return free;
  1788 size_t SpaceManager::sum_waste_in_chunks_in_use() const {
  1789   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  1790   size_t result = 0;
  1791   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1792    result += sum_waste_in_chunks_in_use(i);
  1795   return result;
  1798 size_t SpaceManager::sum_waste_in_chunks_in_use(ChunkIndex index) const {
  1799   size_t result = 0;
  1800   Metachunk* chunk = chunks_in_use(index);
  1801   // Count the free space in all the chunk but not the
  1802   // current chunk from which allocations are still being done.
  1803   while (chunk != NULL) {
  1804     if (chunk != current_chunk()) {
  1805       result += chunk->free_word_size();
  1807     chunk = chunk->next();
  1809   return result;
  1812 size_t SpaceManager::sum_capacity_in_chunks_in_use() const {
  1813   // For CMS use "allocated_chunks_words()" which does not need the
  1814   // Metaspace lock.  For the other collectors sum over the
  1815   // lists.  Use both methods as a check that "allocated_chunks_words()"
  1816   // is correct.  That is, sum_capacity_in_chunks() is too expensive
  1817   // to use in the product and allocated_chunks_words() should be used
  1818   // but allow for  checking that allocated_chunks_words() returns the same
  1819   // value as sum_capacity_in_chunks_in_use() which is the definitive
  1820   // answer.
  1821   if (UseConcMarkSweepGC) {
  1822     return allocated_chunks_words();
  1823   } else {
  1824     MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  1825     size_t sum = 0;
  1826     for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1827       Metachunk* chunk = chunks_in_use(i);
  1828       while (chunk != NULL) {
  1829         sum += chunk->word_size();
  1830         chunk = chunk->next();
  1833   return sum;
  1837 size_t SpaceManager::sum_count_in_chunks_in_use() {
  1838   size_t count = 0;
  1839   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1840     count = count + sum_count_in_chunks_in_use(i);
  1843   return count;
  1846 size_t SpaceManager::sum_count_in_chunks_in_use(ChunkIndex i) {
  1847   size_t count = 0;
  1848   Metachunk* chunk = chunks_in_use(i);
  1849   while (chunk != NULL) {
  1850     count++;
  1851     chunk = chunk->next();
  1853   return count;
  1857 size_t SpaceManager::sum_used_in_chunks_in_use() const {
  1858   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  1859   size_t used = 0;
  1860   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1861     Metachunk* chunk = chunks_in_use(i);
  1862     while (chunk != NULL) {
  1863       used += chunk->used_word_size();
  1864       chunk = chunk->next();
  1867   return used;
  1870 void SpaceManager::locked_print_chunks_in_use_on(outputStream* st) const {
  1872   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1873     Metachunk* chunk = chunks_in_use(i);
  1874     st->print("SpaceManager: %s " PTR_FORMAT,
  1875                  chunk_size_name(i), chunk);
  1876     if (chunk != NULL) {
  1877       st->print_cr(" free " SIZE_FORMAT,
  1878                    chunk->free_word_size());
  1879     } else {
  1880       st->print_cr("");
  1884   chunk_manager()->locked_print_free_chunks(st);
  1885   chunk_manager()->locked_print_sum_free_chunks(st);
  1888 size_t SpaceManager::calc_chunk_size(size_t word_size) {
  1890   // Decide between a small chunk and a medium chunk.  Up to
  1891   // _small_chunk_limit small chunks can be allocated but
  1892   // once a medium chunk has been allocated, no more small
  1893   // chunks will be allocated.
  1894   size_t chunk_word_size;
  1895   if (chunks_in_use(MediumIndex) == NULL &&
  1896       sum_count_in_chunks_in_use(SmallIndex) < _small_chunk_limit) {
  1897     chunk_word_size = (size_t) small_chunk_size();
  1898     if (word_size + Metachunk::overhead() > small_chunk_size()) {
  1899       chunk_word_size = medium_chunk_size();
  1901   } else {
  1902     chunk_word_size = medium_chunk_size();
  1905   // Might still need a humongous chunk.  Enforce an
  1906   // eight word granularity to facilitate reuse (some
  1907   // wastage but better chance of reuse).
  1908   size_t if_humongous_sized_chunk =
  1909     align_size_up(word_size + Metachunk::overhead(),
  1910                   HumongousChunkGranularity);
  1911   chunk_word_size =
  1912     MAX2((size_t) chunk_word_size, if_humongous_sized_chunk);
  1914   assert(!SpaceManager::is_humongous(word_size) ||
  1915          chunk_word_size == if_humongous_sized_chunk,
  1916          err_msg("Size calculation is wrong, word_size " SIZE_FORMAT
  1917                  " chunk_word_size " SIZE_FORMAT,
  1918                  word_size, chunk_word_size));
  1919   if (TraceMetadataHumongousAllocation &&
  1920       SpaceManager::is_humongous(word_size)) {
  1921     gclog_or_tty->print_cr("Metadata humongous allocation:");
  1922     gclog_or_tty->print_cr("  word_size " PTR_FORMAT, word_size);
  1923     gclog_or_tty->print_cr("  chunk_word_size " PTR_FORMAT,
  1924                            chunk_word_size);
  1925     gclog_or_tty->print_cr("    chunk overhead " PTR_FORMAT,
  1926                            Metachunk::overhead());
  1928   return chunk_word_size;
  1931 void SpaceManager::track_metaspace_memory_usage() {
  1932   if (is_init_completed()) {
  1933     if (is_class()) {
  1934       MemoryService::track_compressed_class_memory_usage();
  1936     MemoryService::track_metaspace_memory_usage();
  1940 MetaWord* SpaceManager::grow_and_allocate(size_t word_size) {
  1941   assert(vs_list()->current_virtual_space() != NULL,
  1942          "Should have been set");
  1943   assert(current_chunk() == NULL ||
  1944          current_chunk()->allocate(word_size) == NULL,
  1945          "Don't need to expand");
  1946   MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
  1948   if (TraceMetadataChunkAllocation && Verbose) {
  1949     size_t words_left = 0;
  1950     size_t words_used = 0;
  1951     if (current_chunk() != NULL) {
  1952       words_left = current_chunk()->free_word_size();
  1953       words_used = current_chunk()->used_word_size();
  1955     gclog_or_tty->print_cr("SpaceManager::grow_and_allocate for " SIZE_FORMAT
  1956                            " words " SIZE_FORMAT " words used " SIZE_FORMAT
  1957                            " words left",
  1958                             word_size, words_used, words_left);
  1961   // Get another chunk out of the virtual space
  1962   size_t grow_chunks_by_words = calc_chunk_size(word_size);
  1963   Metachunk* next = get_new_chunk(word_size, grow_chunks_by_words);
  1965   MetaWord* mem = NULL;
  1967   // If a chunk was available, add it to the in-use chunk list
  1968   // and do an allocation from it.
  1969   if (next != NULL) {
  1970     // Add to this manager's list of chunks in use.
  1971     add_chunk(next, false);
  1972     mem = next->allocate(word_size);
  1975   // Track metaspace memory usage statistic.
  1976   track_metaspace_memory_usage();
  1978   return mem;
  1981 void SpaceManager::print_on(outputStream* st) const {
  1983   for (ChunkIndex i = ZeroIndex;
  1984        i < NumberOfInUseLists ;
  1985        i = next_chunk_index(i) ) {
  1986     st->print_cr("  chunks_in_use " PTR_FORMAT " chunk size " PTR_FORMAT,
  1987                  chunks_in_use(i),
  1988                  chunks_in_use(i) == NULL ? 0 : chunks_in_use(i)->word_size());
  1990   st->print_cr("    waste:  Small " SIZE_FORMAT " Medium " SIZE_FORMAT
  1991                " Humongous " SIZE_FORMAT,
  1992                sum_waste_in_chunks_in_use(SmallIndex),
  1993                sum_waste_in_chunks_in_use(MediumIndex),
  1994                sum_waste_in_chunks_in_use(HumongousIndex));
  1995   // block free lists
  1996   if (block_freelists() != NULL) {
  1997     st->print_cr("total in block free lists " SIZE_FORMAT,
  1998       block_freelists()->total_size());
  2002 SpaceManager::SpaceManager(Metaspace::MetadataType mdtype,
  2003                            Mutex* lock) :
  2004   _mdtype(mdtype),
  2005   _allocated_blocks_words(0),
  2006   _allocated_chunks_words(0),
  2007   _allocated_chunks_count(0),
  2008   _lock(lock)
  2010   initialize();
  2013 void SpaceManager::inc_size_metrics(size_t words) {
  2014   assert_lock_strong(SpaceManager::expand_lock());
  2015   // Total of allocated Metachunks and allocated Metachunks count
  2016   // for each SpaceManager
  2017   _allocated_chunks_words = _allocated_chunks_words + words;
  2018   _allocated_chunks_count++;
  2019   // Global total of capacity in allocated Metachunks
  2020   MetaspaceAux::inc_capacity(mdtype(), words);
  2021   // Global total of allocated Metablocks.
  2022   // used_words_slow() includes the overhead in each
  2023   // Metachunk so include it in the used when the
  2024   // Metachunk is first added (so only added once per
  2025   // Metachunk).
  2026   MetaspaceAux::inc_used(mdtype(), Metachunk::overhead());
  2029 void SpaceManager::inc_used_metrics(size_t words) {
  2030   // Add to the per SpaceManager total
  2031   Atomic::add_ptr(words, &_allocated_blocks_words);
  2032   // Add to the global total
  2033   MetaspaceAux::inc_used(mdtype(), words);
  2036 void SpaceManager::dec_total_from_size_metrics() {
  2037   MetaspaceAux::dec_capacity(mdtype(), allocated_chunks_words());
  2038   MetaspaceAux::dec_used(mdtype(), allocated_blocks_words());
  2039   // Also deduct the overhead per Metachunk
  2040   MetaspaceAux::dec_used(mdtype(), allocated_chunks_count() * Metachunk::overhead());
  2043 void SpaceManager::initialize() {
  2044   Metadebug::init_allocation_fail_alot_count();
  2045   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  2046     _chunks_in_use[i] = NULL;
  2048   _current_chunk = NULL;
  2049   if (TraceMetadataChunkAllocation && Verbose) {
  2050     gclog_or_tty->print_cr("SpaceManager(): " PTR_FORMAT, this);
  2054 void ChunkManager::return_chunks(ChunkIndex index, Metachunk* chunks) {
  2055   if (chunks == NULL) {
  2056     return;
  2058   ChunkList* list = free_chunks(index);
  2059   assert(list->size() == chunks->word_size(), "Mismatch in chunk sizes");
  2060   assert_lock_strong(SpaceManager::expand_lock());
  2061   Metachunk* cur = chunks;
  2063   // This returns chunks one at a time.  If a new
  2064   // class List can be created that is a base class
  2065   // of FreeList then something like FreeList::prepend()
  2066   // can be used in place of this loop
  2067   while (cur != NULL) {
  2068     assert(cur->container() != NULL, "Container should have been set");
  2069     cur->container()->dec_container_count();
  2070     // Capture the next link before it is changed
  2071     // by the call to return_chunk_at_head();
  2072     Metachunk* next = cur->next();
  2073     DEBUG_ONLY(cur->set_is_tagged_free(true);)
  2074     list->return_chunk_at_head(cur);
  2075     cur = next;
  2079 SpaceManager::~SpaceManager() {
  2080   // This call this->_lock which can't be done while holding expand_lock()
  2081   assert(sum_capacity_in_chunks_in_use() == allocated_chunks_words(),
  2082     err_msg("sum_capacity_in_chunks_in_use() " SIZE_FORMAT
  2083             " allocated_chunks_words() " SIZE_FORMAT,
  2084             sum_capacity_in_chunks_in_use(), allocated_chunks_words()));
  2086   MutexLockerEx fcl(SpaceManager::expand_lock(),
  2087                     Mutex::_no_safepoint_check_flag);
  2089   chunk_manager()->slow_locked_verify();
  2091   dec_total_from_size_metrics();
  2093   if (TraceMetadataChunkAllocation && Verbose) {
  2094     gclog_or_tty->print_cr("~SpaceManager(): " PTR_FORMAT, this);
  2095     locked_print_chunks_in_use_on(gclog_or_tty);
  2098   // Do not mangle freed Metachunks.  The chunk size inside Metachunks
  2099   // is during the freeing of a VirtualSpaceNodes.
  2101   // Have to update before the chunks_in_use lists are emptied
  2102   // below.
  2103   chunk_manager()->inc_free_chunks_total(allocated_chunks_words(),
  2104                                          sum_count_in_chunks_in_use());
  2106   // Add all the chunks in use by this space manager
  2107   // to the global list of free chunks.
  2109   // Follow each list of chunks-in-use and add them to the
  2110   // free lists.  Each list is NULL terminated.
  2112   for (ChunkIndex i = ZeroIndex; i < HumongousIndex; i = next_chunk_index(i)) {
  2113     if (TraceMetadataChunkAllocation && Verbose) {
  2114       gclog_or_tty->print_cr("returned %d %s chunks to freelist",
  2115                              sum_count_in_chunks_in_use(i),
  2116                              chunk_size_name(i));
  2118     Metachunk* chunks = chunks_in_use(i);
  2119     chunk_manager()->return_chunks(i, chunks);
  2120     set_chunks_in_use(i, NULL);
  2121     if (TraceMetadataChunkAllocation && Verbose) {
  2122       gclog_or_tty->print_cr("updated freelist count %d %s",
  2123                              chunk_manager()->free_chunks(i)->count(),
  2124                              chunk_size_name(i));
  2126     assert(i != HumongousIndex, "Humongous chunks are handled explicitly later");
  2129   // The medium chunk case may be optimized by passing the head and
  2130   // tail of the medium chunk list to add_at_head().  The tail is often
  2131   // the current chunk but there are probably exceptions.
  2133   // Humongous chunks
  2134   if (TraceMetadataChunkAllocation && Verbose) {
  2135     gclog_or_tty->print_cr("returned %d %s humongous chunks to dictionary",
  2136                             sum_count_in_chunks_in_use(HumongousIndex),
  2137                             chunk_size_name(HumongousIndex));
  2138     gclog_or_tty->print("Humongous chunk dictionary: ");
  2140   // Humongous chunks are never the current chunk.
  2141   Metachunk* humongous_chunks = chunks_in_use(HumongousIndex);
  2143   while (humongous_chunks != NULL) {
  2144 #ifdef ASSERT
  2145     humongous_chunks->set_is_tagged_free(true);
  2146 #endif
  2147     if (TraceMetadataChunkAllocation && Verbose) {
  2148       gclog_or_tty->print(PTR_FORMAT " (" SIZE_FORMAT ") ",
  2149                           humongous_chunks,
  2150                           humongous_chunks->word_size());
  2152     assert(humongous_chunks->word_size() == (size_t)
  2153            align_size_up(humongous_chunks->word_size(),
  2154                              HumongousChunkGranularity),
  2155            err_msg("Humongous chunk size is wrong: word size " SIZE_FORMAT
  2156                    " granularity %d",
  2157                    humongous_chunks->word_size(), HumongousChunkGranularity));
  2158     Metachunk* next_humongous_chunks = humongous_chunks->next();
  2159     humongous_chunks->container()->dec_container_count();
  2160     chunk_manager()->humongous_dictionary()->return_chunk(humongous_chunks);
  2161     humongous_chunks = next_humongous_chunks;
  2163   if (TraceMetadataChunkAllocation && Verbose) {
  2164     gclog_or_tty->print_cr("");
  2165     gclog_or_tty->print_cr("updated dictionary count %d %s",
  2166                      chunk_manager()->humongous_dictionary()->total_count(),
  2167                      chunk_size_name(HumongousIndex));
  2169   chunk_manager()->slow_locked_verify();
  2172 const char* SpaceManager::chunk_size_name(ChunkIndex index) const {
  2173   switch (index) {
  2174     case SpecializedIndex:
  2175       return "Specialized";
  2176     case SmallIndex:
  2177       return "Small";
  2178     case MediumIndex:
  2179       return "Medium";
  2180     case HumongousIndex:
  2181       return "Humongous";
  2182     default:
  2183       return NULL;
  2187 ChunkIndex ChunkManager::list_index(size_t size) {
  2188   switch (size) {
  2189     case SpecializedChunk:
  2190       assert(SpecializedChunk == ClassSpecializedChunk,
  2191              "Need branch for ClassSpecializedChunk");
  2192       return SpecializedIndex;
  2193     case SmallChunk:
  2194     case ClassSmallChunk:
  2195       return SmallIndex;
  2196     case MediumChunk:
  2197     case ClassMediumChunk:
  2198       return MediumIndex;
  2199     default:
  2200       assert(size > MediumChunk || size > ClassMediumChunk,
  2201              "Not a humongous chunk");
  2202       return HumongousIndex;
  2206 void SpaceManager::deallocate(MetaWord* p, size_t word_size) {
  2207   assert_lock_strong(_lock);
  2208   size_t raw_word_size = get_raw_word_size(word_size);
  2209   size_t min_size = TreeChunk<Metablock, FreeList>::min_size();
  2210   assert(raw_word_size >= min_size,
  2211          err_msg("Should not deallocate dark matter " SIZE_FORMAT "<" SIZE_FORMAT, word_size, min_size));
  2212   block_freelists()->return_block(p, raw_word_size);
  2215 // Adds a chunk to the list of chunks in use.
  2216 void SpaceManager::add_chunk(Metachunk* new_chunk, bool make_current) {
  2218   assert(new_chunk != NULL, "Should not be NULL");
  2219   assert(new_chunk->next() == NULL, "Should not be on a list");
  2221   new_chunk->reset_empty();
  2223   // Find the correct list and and set the current
  2224   // chunk for that list.
  2225   ChunkIndex index = ChunkManager::list_index(new_chunk->word_size());
  2227   if (index != HumongousIndex) {
  2228     retire_current_chunk();
  2229     set_current_chunk(new_chunk);
  2230     new_chunk->set_next(chunks_in_use(index));
  2231     set_chunks_in_use(index, new_chunk);
  2232   } else {
  2233     // For null class loader data and DumpSharedSpaces, the first chunk isn't
  2234     // small, so small will be null.  Link this first chunk as the current
  2235     // chunk.
  2236     if (make_current) {
  2237       // Set as the current chunk but otherwise treat as a humongous chunk.
  2238       set_current_chunk(new_chunk);
  2240     // Link at head.  The _current_chunk only points to a humongous chunk for
  2241     // the null class loader metaspace (class and data virtual space managers)
  2242     // any humongous chunks so will not point to the tail
  2243     // of the humongous chunks list.
  2244     new_chunk->set_next(chunks_in_use(HumongousIndex));
  2245     set_chunks_in_use(HumongousIndex, new_chunk);
  2247     assert(new_chunk->word_size() > medium_chunk_size(), "List inconsistency");
  2250   // Add to the running sum of capacity
  2251   inc_size_metrics(new_chunk->word_size());
  2253   assert(new_chunk->is_empty(), "Not ready for reuse");
  2254   if (TraceMetadataChunkAllocation && Verbose) {
  2255     gclog_or_tty->print("SpaceManager::add_chunk: %d) ",
  2256                         sum_count_in_chunks_in_use());
  2257     new_chunk->print_on(gclog_or_tty);
  2258     chunk_manager()->locked_print_free_chunks(gclog_or_tty);
  2262 void SpaceManager::retire_current_chunk() {
  2263   if (current_chunk() != NULL) {
  2264     size_t remaining_words = current_chunk()->free_word_size();
  2265     if (remaining_words >= TreeChunk<Metablock, FreeList>::min_size()) {
  2266       block_freelists()->return_block(current_chunk()->allocate(remaining_words), remaining_words);
  2267       inc_used_metrics(remaining_words);
  2272 Metachunk* SpaceManager::get_new_chunk(size_t word_size,
  2273                                        size_t grow_chunks_by_words) {
  2274   // Get a chunk from the chunk freelist
  2275   Metachunk* next = chunk_manager()->chunk_freelist_allocate(grow_chunks_by_words);
  2277   if (next == NULL) {
  2278     next = vs_list()->get_new_chunk(word_size,
  2279                                     grow_chunks_by_words,
  2280                                     medium_chunk_bunch());
  2283   if (TraceMetadataHumongousAllocation && next != NULL &&
  2284       SpaceManager::is_humongous(next->word_size())) {
  2285     gclog_or_tty->print_cr("  new humongous chunk word size "
  2286                            PTR_FORMAT, next->word_size());
  2289   return next;
  2292 MetaWord* SpaceManager::allocate(size_t word_size) {
  2293   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  2295   size_t raw_word_size = get_raw_word_size(word_size);
  2296   BlockFreelist* fl =  block_freelists();
  2297   MetaWord* p = NULL;
  2298   // Allocation from the dictionary is expensive in the sense that
  2299   // the dictionary has to be searched for a size.  Don't allocate
  2300   // from the dictionary until it starts to get fat.  Is this
  2301   // a reasonable policy?  Maybe an skinny dictionary is fast enough
  2302   // for allocations.  Do some profiling.  JJJ
  2303   if (fl->total_size() > allocation_from_dictionary_limit) {
  2304     p = fl->get_block(raw_word_size);
  2306   if (p == NULL) {
  2307     p = allocate_work(raw_word_size);
  2310   return p;
  2313 // Returns the address of spaced allocated for "word_size".
  2314 // This methods does not know about blocks (Metablocks)
  2315 MetaWord* SpaceManager::allocate_work(size_t word_size) {
  2316   assert_lock_strong(_lock);
  2317 #ifdef ASSERT
  2318   if (Metadebug::test_metadata_failure()) {
  2319     return NULL;
  2321 #endif
  2322   // Is there space in the current chunk?
  2323   MetaWord* result = NULL;
  2325   // For DumpSharedSpaces, only allocate out of the current chunk which is
  2326   // never null because we gave it the size we wanted.   Caller reports out
  2327   // of memory if this returns null.
  2328   if (DumpSharedSpaces) {
  2329     assert(current_chunk() != NULL, "should never happen");
  2330     inc_used_metrics(word_size);
  2331     return current_chunk()->allocate(word_size); // caller handles null result
  2334   if (current_chunk() != NULL) {
  2335     result = current_chunk()->allocate(word_size);
  2338   if (result == NULL) {
  2339     result = grow_and_allocate(word_size);
  2342   if (result != NULL) {
  2343     inc_used_metrics(word_size);
  2344     assert(result != (MetaWord*) chunks_in_use(MediumIndex),
  2345            "Head of the list is being allocated");
  2348   return result;
  2351 void SpaceManager::verify() {
  2352   // If there are blocks in the dictionary, then
  2353   // verfication of chunks does not work since
  2354   // being in the dictionary alters a chunk.
  2355   if (block_freelists()->total_size() == 0) {
  2356     for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  2357       Metachunk* curr = chunks_in_use(i);
  2358       while (curr != NULL) {
  2359         curr->verify();
  2360         verify_chunk_size(curr);
  2361         curr = curr->next();
  2367 void SpaceManager::verify_chunk_size(Metachunk* chunk) {
  2368   assert(is_humongous(chunk->word_size()) ||
  2369          chunk->word_size() == medium_chunk_size() ||
  2370          chunk->word_size() == small_chunk_size() ||
  2371          chunk->word_size() == specialized_chunk_size(),
  2372          "Chunk size is wrong");
  2373   return;
  2376 #ifdef ASSERT
  2377 void SpaceManager::verify_allocated_blocks_words() {
  2378   // Verification is only guaranteed at a safepoint.
  2379   assert(SafepointSynchronize::is_at_safepoint() || !Universe::is_fully_initialized(),
  2380     "Verification can fail if the applications is running");
  2381   assert(allocated_blocks_words() == sum_used_in_chunks_in_use(),
  2382     err_msg("allocation total is not consistent " SIZE_FORMAT
  2383             " vs " SIZE_FORMAT,
  2384             allocated_blocks_words(), sum_used_in_chunks_in_use()));
  2387 #endif
  2389 void SpaceManager::dump(outputStream* const out) const {
  2390   size_t curr_total = 0;
  2391   size_t waste = 0;
  2392   uint i = 0;
  2393   size_t used = 0;
  2394   size_t capacity = 0;
  2396   // Add up statistics for all chunks in this SpaceManager.
  2397   for (ChunkIndex index = ZeroIndex;
  2398        index < NumberOfInUseLists;
  2399        index = next_chunk_index(index)) {
  2400     for (Metachunk* curr = chunks_in_use(index);
  2401          curr != NULL;
  2402          curr = curr->next()) {
  2403       out->print("%d) ", i++);
  2404       curr->print_on(out);
  2405       curr_total += curr->word_size();
  2406       used += curr->used_word_size();
  2407       capacity += curr->word_size();
  2408       waste += curr->free_word_size() + curr->overhead();;
  2412   if (TraceMetadataChunkAllocation && Verbose) {
  2413     block_freelists()->print_on(out);
  2416   size_t free = current_chunk() == NULL ? 0 : current_chunk()->free_word_size();
  2417   // Free space isn't wasted.
  2418   waste -= free;
  2420   out->print_cr("total of all chunks "  SIZE_FORMAT " used " SIZE_FORMAT
  2421                 " free " SIZE_FORMAT " capacity " SIZE_FORMAT
  2422                 " waste " SIZE_FORMAT, curr_total, used, free, capacity, waste);
  2425 #ifndef PRODUCT
  2426 void SpaceManager::mangle_freed_chunks() {
  2427   for (ChunkIndex index = ZeroIndex;
  2428        index < NumberOfInUseLists;
  2429        index = next_chunk_index(index)) {
  2430     for (Metachunk* curr = chunks_in_use(index);
  2431          curr != NULL;
  2432          curr = curr->next()) {
  2433       curr->mangle();
  2437 #endif // PRODUCT
  2439 // MetaspaceAux
  2442 size_t MetaspaceAux::_allocated_capacity_words[] = {0, 0};
  2443 size_t MetaspaceAux::_allocated_used_words[] = {0, 0};
  2445 size_t MetaspaceAux::free_bytes(Metaspace::MetadataType mdtype) {
  2446   VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
  2447   return list == NULL ? 0 : list->free_bytes();
  2450 size_t MetaspaceAux::free_bytes() {
  2451   return free_bytes(Metaspace::ClassType) + free_bytes(Metaspace::NonClassType);
  2454 void MetaspaceAux::dec_capacity(Metaspace::MetadataType mdtype, size_t words) {
  2455   assert_lock_strong(SpaceManager::expand_lock());
  2456   assert(words <= allocated_capacity_words(mdtype),
  2457     err_msg("About to decrement below 0: words " SIZE_FORMAT
  2458             " is greater than _allocated_capacity_words[%u] " SIZE_FORMAT,
  2459             words, mdtype, allocated_capacity_words(mdtype)));
  2460   _allocated_capacity_words[mdtype] -= words;
  2463 void MetaspaceAux::inc_capacity(Metaspace::MetadataType mdtype, size_t words) {
  2464   assert_lock_strong(SpaceManager::expand_lock());
  2465   // Needs to be atomic
  2466   _allocated_capacity_words[mdtype] += words;
  2469 void MetaspaceAux::dec_used(Metaspace::MetadataType mdtype, size_t words) {
  2470   assert(words <= allocated_used_words(mdtype),
  2471     err_msg("About to decrement below 0: words " SIZE_FORMAT
  2472             " is greater than _allocated_used_words[%u] " SIZE_FORMAT,
  2473             words, mdtype, allocated_used_words(mdtype)));
  2474   // For CMS deallocation of the Metaspaces occurs during the
  2475   // sweep which is a concurrent phase.  Protection by the expand_lock()
  2476   // is not enough since allocation is on a per Metaspace basis
  2477   // and protected by the Metaspace lock.
  2478   jlong minus_words = (jlong) - (jlong) words;
  2479   Atomic::add_ptr(minus_words, &_allocated_used_words[mdtype]);
  2482 void MetaspaceAux::inc_used(Metaspace::MetadataType mdtype, size_t words) {
  2483   // _allocated_used_words tracks allocations for
  2484   // each piece of metadata.  Those allocations are
  2485   // generally done concurrently by different application
  2486   // threads so must be done atomically.
  2487   Atomic::add_ptr(words, &_allocated_used_words[mdtype]);
  2490 size_t MetaspaceAux::used_bytes_slow(Metaspace::MetadataType mdtype) {
  2491   size_t used = 0;
  2492   ClassLoaderDataGraphMetaspaceIterator iter;
  2493   while (iter.repeat()) {
  2494     Metaspace* msp = iter.get_next();
  2495     // Sum allocated_blocks_words for each metaspace
  2496     if (msp != NULL) {
  2497       used += msp->used_words_slow(mdtype);
  2500   return used * BytesPerWord;
  2503 size_t MetaspaceAux::free_bytes_slow(Metaspace::MetadataType mdtype) {
  2504   size_t free = 0;
  2505   ClassLoaderDataGraphMetaspaceIterator iter;
  2506   while (iter.repeat()) {
  2507     Metaspace* msp = iter.get_next();
  2508     if (msp != NULL) {
  2509       free += msp->free_words_slow(mdtype);
  2512   return free * BytesPerWord;
  2515 size_t MetaspaceAux::capacity_bytes_slow(Metaspace::MetadataType mdtype) {
  2516   if ((mdtype == Metaspace::ClassType) && !Metaspace::using_class_space()) {
  2517     return 0;
  2519   // Don't count the space in the freelists.  That space will be
  2520   // added to the capacity calculation as needed.
  2521   size_t capacity = 0;
  2522   ClassLoaderDataGraphMetaspaceIterator iter;
  2523   while (iter.repeat()) {
  2524     Metaspace* msp = iter.get_next();
  2525     if (msp != NULL) {
  2526       capacity += msp->capacity_words_slow(mdtype);
  2529   return capacity * BytesPerWord;
  2532 size_t MetaspaceAux::capacity_bytes_slow() {
  2533 #ifdef PRODUCT
  2534   // Use allocated_capacity_bytes() in PRODUCT instead of this function.
  2535   guarantee(false, "Should not call capacity_bytes_slow() in the PRODUCT");
  2536 #endif
  2537   size_t class_capacity = capacity_bytes_slow(Metaspace::ClassType);
  2538   size_t non_class_capacity = capacity_bytes_slow(Metaspace::NonClassType);
  2539   assert(allocated_capacity_bytes() == class_capacity + non_class_capacity,
  2540       err_msg("bad accounting: allocated_capacity_bytes() " SIZE_FORMAT
  2541         " class_capacity + non_class_capacity " SIZE_FORMAT
  2542         " class_capacity " SIZE_FORMAT " non_class_capacity " SIZE_FORMAT,
  2543         allocated_capacity_bytes(), class_capacity + non_class_capacity,
  2544         class_capacity, non_class_capacity));
  2546   return class_capacity + non_class_capacity;
  2549 size_t MetaspaceAux::reserved_bytes(Metaspace::MetadataType mdtype) {
  2550   VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
  2551   return list == NULL ? 0 : list->reserved_bytes();
  2554 size_t MetaspaceAux::committed_bytes(Metaspace::MetadataType mdtype) {
  2555   VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
  2556   return list == NULL ? 0 : list->committed_bytes();
  2559 size_t MetaspaceAux::min_chunk_size_words() { return Metaspace::first_chunk_word_size(); }
  2561 size_t MetaspaceAux::free_chunks_total_words(Metaspace::MetadataType mdtype) {
  2562   ChunkManager* chunk_manager = Metaspace::get_chunk_manager(mdtype);
  2563   if (chunk_manager == NULL) {
  2564     return 0;
  2566   chunk_manager->slow_verify();
  2567   return chunk_manager->free_chunks_total_words();
  2570 size_t MetaspaceAux::free_chunks_total_bytes(Metaspace::MetadataType mdtype) {
  2571   return free_chunks_total_words(mdtype) * BytesPerWord;
  2574 size_t MetaspaceAux::free_chunks_total_words() {
  2575   return free_chunks_total_words(Metaspace::ClassType) +
  2576          free_chunks_total_words(Metaspace::NonClassType);
  2579 size_t MetaspaceAux::free_chunks_total_bytes() {
  2580   return free_chunks_total_words() * BytesPerWord;
  2583 void MetaspaceAux::print_metaspace_change(size_t prev_metadata_used) {
  2584   gclog_or_tty->print(", [Metaspace:");
  2585   if (PrintGCDetails && Verbose) {
  2586     gclog_or_tty->print(" "  SIZE_FORMAT
  2587                         "->" SIZE_FORMAT
  2588                         "("  SIZE_FORMAT ")",
  2589                         prev_metadata_used,
  2590                         allocated_used_bytes(),
  2591                         reserved_bytes());
  2592   } else {
  2593     gclog_or_tty->print(" "  SIZE_FORMAT "K"
  2594                         "->" SIZE_FORMAT "K"
  2595                         "("  SIZE_FORMAT "K)",
  2596                         prev_metadata_used/K,
  2597                         allocated_used_bytes()/K,
  2598                         reserved_bytes()/K);
  2601   gclog_or_tty->print("]");
  2604 // This is printed when PrintGCDetails
  2605 void MetaspaceAux::print_on(outputStream* out) {
  2606   Metaspace::MetadataType nct = Metaspace::NonClassType;
  2608   out->print_cr(" Metaspace       "
  2609                 "used "      SIZE_FORMAT "K, "
  2610                 "capacity "  SIZE_FORMAT "K, "
  2611                 "committed " SIZE_FORMAT "K, "
  2612                 "reserved "  SIZE_FORMAT "K",
  2613                 allocated_used_bytes()/K,
  2614                 allocated_capacity_bytes()/K,
  2615                 committed_bytes()/K,
  2616                 reserved_bytes()/K);
  2618   if (Metaspace::using_class_space()) {
  2619     Metaspace::MetadataType ct = Metaspace::ClassType;
  2620     out->print_cr("  class space    "
  2621                   "used "      SIZE_FORMAT "K, "
  2622                   "capacity "  SIZE_FORMAT "K, "
  2623                   "committed " SIZE_FORMAT "K, "
  2624                   "reserved "  SIZE_FORMAT "K",
  2625                   allocated_used_bytes(ct)/K,
  2626                   allocated_capacity_bytes(ct)/K,
  2627                   committed_bytes(ct)/K,
  2628                   reserved_bytes(ct)/K);
  2632 // Print information for class space and data space separately.
  2633 // This is almost the same as above.
  2634 void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) {
  2635   size_t free_chunks_capacity_bytes = free_chunks_total_bytes(mdtype);
  2636   size_t capacity_bytes = capacity_bytes_slow(mdtype);
  2637   size_t used_bytes = used_bytes_slow(mdtype);
  2638   size_t free_bytes = free_bytes_slow(mdtype);
  2639   size_t used_and_free = used_bytes + free_bytes +
  2640                            free_chunks_capacity_bytes;
  2641   out->print_cr("  Chunk accounting: used in chunks " SIZE_FORMAT
  2642              "K + unused in chunks " SIZE_FORMAT "K  + "
  2643              " capacity in free chunks " SIZE_FORMAT "K = " SIZE_FORMAT
  2644              "K  capacity in allocated chunks " SIZE_FORMAT "K",
  2645              used_bytes / K,
  2646              free_bytes / K,
  2647              free_chunks_capacity_bytes / K,
  2648              used_and_free / K,
  2649              capacity_bytes / K);
  2650   // Accounting can only be correct if we got the values during a safepoint
  2651   assert(!SafepointSynchronize::is_at_safepoint() || used_and_free == capacity_bytes, "Accounting is wrong");
  2654 // Print total fragmentation for class metaspaces
  2655 void MetaspaceAux::print_class_waste(outputStream* out) {
  2656   assert(Metaspace::using_class_space(), "class metaspace not used");
  2657   size_t cls_specialized_waste = 0, cls_small_waste = 0, cls_medium_waste = 0;
  2658   size_t cls_specialized_count = 0, cls_small_count = 0, cls_medium_count = 0, cls_humongous_count = 0;
  2659   ClassLoaderDataGraphMetaspaceIterator iter;
  2660   while (iter.repeat()) {
  2661     Metaspace* msp = iter.get_next();
  2662     if (msp != NULL) {
  2663       cls_specialized_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
  2664       cls_specialized_count += msp->class_vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
  2665       cls_small_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SmallIndex);
  2666       cls_small_count += msp->class_vsm()->sum_count_in_chunks_in_use(SmallIndex);
  2667       cls_medium_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(MediumIndex);
  2668       cls_medium_count += msp->class_vsm()->sum_count_in_chunks_in_use(MediumIndex);
  2669       cls_humongous_count += msp->class_vsm()->sum_count_in_chunks_in_use(HumongousIndex);
  2672   out->print_cr(" class: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
  2673                 SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
  2674                 SIZE_FORMAT " medium(s) " SIZE_FORMAT ", "
  2675                 "large count " SIZE_FORMAT,
  2676                 cls_specialized_count, cls_specialized_waste,
  2677                 cls_small_count, cls_small_waste,
  2678                 cls_medium_count, cls_medium_waste, cls_humongous_count);
  2681 // Print total fragmentation for data and class metaspaces separately
  2682 void MetaspaceAux::print_waste(outputStream* out) {
  2683   size_t specialized_waste = 0, small_waste = 0, medium_waste = 0;
  2684   size_t specialized_count = 0, small_count = 0, medium_count = 0, humongous_count = 0;
  2686   ClassLoaderDataGraphMetaspaceIterator iter;
  2687   while (iter.repeat()) {
  2688     Metaspace* msp = iter.get_next();
  2689     if (msp != NULL) {
  2690       specialized_waste += msp->vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
  2691       specialized_count += msp->vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
  2692       small_waste += msp->vsm()->sum_waste_in_chunks_in_use(SmallIndex);
  2693       small_count += msp->vsm()->sum_count_in_chunks_in_use(SmallIndex);
  2694       medium_waste += msp->vsm()->sum_waste_in_chunks_in_use(MediumIndex);
  2695       medium_count += msp->vsm()->sum_count_in_chunks_in_use(MediumIndex);
  2696       humongous_count += msp->vsm()->sum_count_in_chunks_in_use(HumongousIndex);
  2699   out->print_cr("Total fragmentation waste (words) doesn't count free space");
  2700   out->print_cr("  data: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
  2701                         SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
  2702                         SIZE_FORMAT " medium(s) " SIZE_FORMAT ", "
  2703                         "large count " SIZE_FORMAT,
  2704              specialized_count, specialized_waste, small_count,
  2705              small_waste, medium_count, medium_waste, humongous_count);
  2706   if (Metaspace::using_class_space()) {
  2707     print_class_waste(out);
  2711 // Dump global metaspace things from the end of ClassLoaderDataGraph
  2712 void MetaspaceAux::dump(outputStream* out) {
  2713   out->print_cr("All Metaspace:");
  2714   out->print("data space: "); print_on(out, Metaspace::NonClassType);
  2715   out->print("class space: "); print_on(out, Metaspace::ClassType);
  2716   print_waste(out);
  2719 void MetaspaceAux::verify_free_chunks() {
  2720   Metaspace::chunk_manager_metadata()->verify();
  2721   if (Metaspace::using_class_space()) {
  2722     Metaspace::chunk_manager_class()->verify();
  2726 void MetaspaceAux::verify_capacity() {
  2727 #ifdef ASSERT
  2728   size_t running_sum_capacity_bytes = allocated_capacity_bytes();
  2729   // For purposes of the running sum of capacity, verify against capacity
  2730   size_t capacity_in_use_bytes = capacity_bytes_slow();
  2731   assert(running_sum_capacity_bytes == capacity_in_use_bytes,
  2732     err_msg("allocated_capacity_words() * BytesPerWord " SIZE_FORMAT
  2733             " capacity_bytes_slow()" SIZE_FORMAT,
  2734             running_sum_capacity_bytes, capacity_in_use_bytes));
  2735   for (Metaspace::MetadataType i = Metaspace::ClassType;
  2736        i < Metaspace:: MetadataTypeCount;
  2737        i = (Metaspace::MetadataType)(i + 1)) {
  2738     size_t capacity_in_use_bytes = capacity_bytes_slow(i);
  2739     assert(allocated_capacity_bytes(i) == capacity_in_use_bytes,
  2740       err_msg("allocated_capacity_bytes(%u) " SIZE_FORMAT
  2741               " capacity_bytes_slow(%u)" SIZE_FORMAT,
  2742               i, allocated_capacity_bytes(i), i, capacity_in_use_bytes));
  2744 #endif
  2747 void MetaspaceAux::verify_used() {
  2748 #ifdef ASSERT
  2749   size_t running_sum_used_bytes = allocated_used_bytes();
  2750   // For purposes of the running sum of used, verify against used
  2751   size_t used_in_use_bytes = used_bytes_slow();
  2752   assert(allocated_used_bytes() == used_in_use_bytes,
  2753     err_msg("allocated_used_bytes() " SIZE_FORMAT
  2754             " used_bytes_slow()" SIZE_FORMAT,
  2755             allocated_used_bytes(), used_in_use_bytes));
  2756   for (Metaspace::MetadataType i = Metaspace::ClassType;
  2757        i < Metaspace:: MetadataTypeCount;
  2758        i = (Metaspace::MetadataType)(i + 1)) {
  2759     size_t used_in_use_bytes = used_bytes_slow(i);
  2760     assert(allocated_used_bytes(i) == used_in_use_bytes,
  2761       err_msg("allocated_used_bytes(%u) " SIZE_FORMAT
  2762               " used_bytes_slow(%u)" SIZE_FORMAT,
  2763               i, allocated_used_bytes(i), i, used_in_use_bytes));
  2765 #endif
  2768 void MetaspaceAux::verify_metrics() {
  2769   verify_capacity();
  2770   verify_used();
  2774 // Metaspace methods
  2776 size_t Metaspace::_first_chunk_word_size = 0;
  2777 size_t Metaspace::_first_class_chunk_word_size = 0;
  2779 size_t Metaspace::_commit_alignment = 0;
  2780 size_t Metaspace::_reserve_alignment = 0;
  2782 Metaspace::Metaspace(Mutex* lock, MetaspaceType type) {
  2783   initialize(lock, type);
  2786 Metaspace::~Metaspace() {
  2787   delete _vsm;
  2788   if (using_class_space()) {
  2789     delete _class_vsm;
  2793 VirtualSpaceList* Metaspace::_space_list = NULL;
  2794 VirtualSpaceList* Metaspace::_class_space_list = NULL;
  2796 ChunkManager* Metaspace::_chunk_manager_metadata = NULL;
  2797 ChunkManager* Metaspace::_chunk_manager_class = NULL;
  2799 #define VIRTUALSPACEMULTIPLIER 2
  2801 #ifdef _LP64
  2802 void Metaspace::set_narrow_klass_base_and_shift(address metaspace_base, address cds_base) {
  2803   // Figure out the narrow_klass_base and the narrow_klass_shift.  The
  2804   // narrow_klass_base is the lower of the metaspace base and the cds base
  2805   // (if cds is enabled).  The narrow_klass_shift depends on the distance
  2806   // between the lower base and higher address.
  2807   address lower_base;
  2808   address higher_address;
  2809   if (UseSharedSpaces) {
  2810     higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()),
  2811                           (address)(metaspace_base + class_metaspace_size()));
  2812     lower_base = MIN2(metaspace_base, cds_base);
  2813   } else {
  2814     higher_address = metaspace_base + class_metaspace_size();
  2815     lower_base = metaspace_base;
  2817   Universe::set_narrow_klass_base(lower_base);
  2818   if ((uint64_t)(higher_address - lower_base) < (uint64_t)max_juint) {
  2819     Universe::set_narrow_klass_shift(0);
  2820   } else {
  2821     assert(!UseSharedSpaces, "Cannot shift with UseSharedSpaces");
  2822     Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes);
  2826 // Return TRUE if the specified metaspace_base and cds_base are close enough
  2827 // to work with compressed klass pointers.
  2828 bool Metaspace::can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base) {
  2829   assert(cds_base != 0 && UseSharedSpaces, "Only use with CDS");
  2830   assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
  2831   address lower_base = MIN2((address)metaspace_base, cds_base);
  2832   address higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()),
  2833                                 (address)(metaspace_base + class_metaspace_size()));
  2834   return ((uint64_t)(higher_address - lower_base) < (uint64_t)max_juint);
  2837 // Try to allocate the metaspace at the requested addr.
  2838 void Metaspace::allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base) {
  2839   assert(using_class_space(), "called improperly");
  2840   assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
  2841   assert(class_metaspace_size() < KlassEncodingMetaspaceMax,
  2842          "Metaspace size is too big");
  2843   assert_is_ptr_aligned(requested_addr,          _reserve_alignment);
  2844   assert_is_ptr_aligned(cds_base,                _reserve_alignment);
  2845   assert_is_size_aligned(class_metaspace_size(), _reserve_alignment);
  2847   // Don't use large pages for the class space.
  2848   bool large_pages = false;
  2850   ReservedSpace metaspace_rs = ReservedSpace(class_metaspace_size(),
  2851                                              _reserve_alignment,
  2852                                              large_pages,
  2853                                              requested_addr, 0);
  2854   if (!metaspace_rs.is_reserved()) {
  2855     if (UseSharedSpaces) {
  2856       size_t increment = align_size_up(1*G, _reserve_alignment);
  2858       // Keep trying to allocate the metaspace, increasing the requested_addr
  2859       // by 1GB each time, until we reach an address that will no longer allow
  2860       // use of CDS with compressed klass pointers.
  2861       char *addr = requested_addr;
  2862       while (!metaspace_rs.is_reserved() && (addr + increment > addr) &&
  2863              can_use_cds_with_metaspace_addr(addr + increment, cds_base)) {
  2864         addr = addr + increment;
  2865         metaspace_rs = ReservedSpace(class_metaspace_size(),
  2866                                      _reserve_alignment, large_pages, addr, 0);
  2870     // If no successful allocation then try to allocate the space anywhere.  If
  2871     // that fails then OOM doom.  At this point we cannot try allocating the
  2872     // metaspace as if UseCompressedClassPointers is off because too much
  2873     // initialization has happened that depends on UseCompressedClassPointers.
  2874     // So, UseCompressedClassPointers cannot be turned off at this point.
  2875     if (!metaspace_rs.is_reserved()) {
  2876       metaspace_rs = ReservedSpace(class_metaspace_size(),
  2877                                    _reserve_alignment, large_pages);
  2878       if (!metaspace_rs.is_reserved()) {
  2879         vm_exit_during_initialization(err_msg("Could not allocate metaspace: %d bytes",
  2880                                               class_metaspace_size()));
  2885   // If we got here then the metaspace got allocated.
  2886   MemTracker::record_virtual_memory_type((address)metaspace_rs.base(), mtClass);
  2888   // Verify that we can use shared spaces.  Otherwise, turn off CDS.
  2889   if (UseSharedSpaces && !can_use_cds_with_metaspace_addr(metaspace_rs.base(), cds_base)) {
  2890     FileMapInfo::stop_sharing_and_unmap(
  2891         "Could not allocate metaspace at a compatible address");
  2894   set_narrow_klass_base_and_shift((address)metaspace_rs.base(),
  2895                                   UseSharedSpaces ? (address)cds_base : 0);
  2897   initialize_class_space(metaspace_rs);
  2899   if (PrintCompressedOopsMode || (PrintMiscellaneous && Verbose)) {
  2900     gclog_or_tty->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: " SIZE_FORMAT,
  2901                             Universe::narrow_klass_base(), Universe::narrow_klass_shift());
  2902     gclog_or_tty->print_cr("Metaspace Size: " SIZE_FORMAT " Address: " PTR_FORMAT " Req Addr: " PTR_FORMAT,
  2903                            class_metaspace_size(), metaspace_rs.base(), requested_addr);
  2907 // For UseCompressedClassPointers the class space is reserved above the top of
  2908 // the Java heap.  The argument passed in is at the base of the compressed space.
  2909 void Metaspace::initialize_class_space(ReservedSpace rs) {
  2910   // The reserved space size may be bigger because of alignment, esp with UseLargePages
  2911   assert(rs.size() >= CompressedClassSpaceSize,
  2912          err_msg(SIZE_FORMAT " != " UINTX_FORMAT, rs.size(), CompressedClassSpaceSize));
  2913   assert(using_class_space(), "Must be using class space");
  2914   _class_space_list = new VirtualSpaceList(rs);
  2915   _chunk_manager_class = new ChunkManager(SpecializedChunk, ClassSmallChunk, ClassMediumChunk);
  2917   if (!_class_space_list->initialization_succeeded()) {
  2918     vm_exit_during_initialization("Failed to setup compressed class space virtual space list.");
  2922 #endif
  2924 // Align down. If the aligning result in 0, return 'alignment'.
  2925 static size_t restricted_align_down(size_t size, size_t alignment) {
  2926   return MAX2(alignment, align_size_down_(size, alignment));
  2929 void Metaspace::ergo_initialize() {
  2930   if (DumpSharedSpaces) {
  2931     // Using large pages when dumping the shared archive is currently not implemented.
  2932     FLAG_SET_ERGO(bool, UseLargePagesInMetaspace, false);
  2935   size_t page_size = os::vm_page_size();
  2936   if (UseLargePages && UseLargePagesInMetaspace) {
  2937     page_size = os::large_page_size();
  2940   _commit_alignment  = page_size;
  2941   _reserve_alignment = MAX2(page_size, (size_t)os::vm_allocation_granularity());
  2943   // Do not use FLAG_SET_ERGO to update MaxMetaspaceSize, since this will
  2944   // override if MaxMetaspaceSize was set on the command line or not.
  2945   // This information is needed later to conform to the specification of the
  2946   // java.lang.management.MemoryUsage API.
  2947   //
  2948   // Ideally, we would be able to set the default value of MaxMetaspaceSize in
  2949   // globals.hpp to the aligned value, but this is not possible, since the
  2950   // alignment depends on other flags being parsed.
  2951   MaxMetaspaceSize = restricted_align_down(MaxMetaspaceSize, _reserve_alignment);
  2953   if (MetaspaceSize > MaxMetaspaceSize) {
  2954     MetaspaceSize = MaxMetaspaceSize;
  2957   MetaspaceSize = restricted_align_down(MetaspaceSize, _commit_alignment);
  2959   assert(MetaspaceSize <= MaxMetaspaceSize, "MetaspaceSize should be limited by MaxMetaspaceSize");
  2961   if (MetaspaceSize < 256*K) {
  2962     vm_exit_during_initialization("Too small initial Metaspace size");
  2965   MinMetaspaceExpansion = restricted_align_down(MinMetaspaceExpansion, _commit_alignment);
  2966   MaxMetaspaceExpansion = restricted_align_down(MaxMetaspaceExpansion, _commit_alignment);
  2968   CompressedClassSpaceSize = restricted_align_down(CompressedClassSpaceSize, _reserve_alignment);
  2969   set_class_metaspace_size(CompressedClassSpaceSize);
  2972 void Metaspace::global_initialize() {
  2973   // Initialize the alignment for shared spaces.
  2974   int max_alignment = os::vm_page_size();
  2975   size_t cds_total = 0;
  2977   MetaspaceShared::set_max_alignment(max_alignment);
  2979   if (DumpSharedSpaces) {
  2980     SharedReadOnlySize  = align_size_up(SharedReadOnlySize,  max_alignment);
  2981     SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment);
  2982     SharedMiscDataSize  = align_size_up(SharedMiscDataSize,  max_alignment);
  2983     SharedMiscCodeSize  = align_size_up(SharedMiscCodeSize,  max_alignment);
  2985     // Initialize with the sum of the shared space sizes.  The read-only
  2986     // and read write metaspace chunks will be allocated out of this and the
  2987     // remainder is the misc code and data chunks.
  2988     cds_total = FileMapInfo::shared_spaces_size();
  2989     cds_total = align_size_up(cds_total, _reserve_alignment);
  2990     _space_list = new VirtualSpaceList(cds_total/wordSize);
  2991     _chunk_manager_metadata = new ChunkManager(SpecializedChunk, SmallChunk, MediumChunk);
  2993     if (!_space_list->initialization_succeeded()) {
  2994       vm_exit_during_initialization("Unable to dump shared archive.", NULL);
  2997 #ifdef _LP64
  2998     if (cds_total + class_metaspace_size() > (uint64_t)max_juint) {
  2999       vm_exit_during_initialization("Unable to dump shared archive.",
  3000           err_msg("Size of archive (" SIZE_FORMAT ") + compressed class space ("
  3001                   SIZE_FORMAT ") == total (" SIZE_FORMAT ") is larger than compressed "
  3002                   "klass limit: " SIZE_FORMAT, cds_total, class_metaspace_size(),
  3003                   cds_total + class_metaspace_size(), (size_t)max_juint));
  3006     // Set the compressed klass pointer base so that decoding of these pointers works
  3007     // properly when creating the shared archive.
  3008     assert(UseCompressedOops && UseCompressedClassPointers,
  3009       "UseCompressedOops and UseCompressedClassPointers must be set");
  3010     Universe::set_narrow_klass_base((address)_space_list->current_virtual_space()->bottom());
  3011     if (TraceMetavirtualspaceAllocation && Verbose) {
  3012       gclog_or_tty->print_cr("Setting_narrow_klass_base to Address: " PTR_FORMAT,
  3013                              _space_list->current_virtual_space()->bottom());
  3016     Universe::set_narrow_klass_shift(0);
  3017 #endif
  3019   } else {
  3020     // If using shared space, open the file that contains the shared space
  3021     // and map in the memory before initializing the rest of metaspace (so
  3022     // the addresses don't conflict)
  3023     address cds_address = NULL;
  3024     if (UseSharedSpaces) {
  3025       FileMapInfo* mapinfo = new FileMapInfo();
  3026       memset(mapinfo, 0, sizeof(FileMapInfo));
  3028       // Open the shared archive file, read and validate the header. If
  3029       // initialization fails, shared spaces [UseSharedSpaces] are
  3030       // disabled and the file is closed.
  3031       // Map in spaces now also
  3032       if (mapinfo->initialize() && MetaspaceShared::map_shared_spaces(mapinfo)) {
  3033         FileMapInfo::set_current_info(mapinfo);
  3034         cds_total = FileMapInfo::shared_spaces_size();
  3035         cds_address = (address)mapinfo->region_base(0);
  3036       } else {
  3037         assert(!mapinfo->is_open() && !UseSharedSpaces,
  3038                "archive file not closed or shared spaces not disabled.");
  3042 #ifdef _LP64
  3043     // If UseCompressedClassPointers is set then allocate the metaspace area
  3044     // above the heap and above the CDS area (if it exists).
  3045     if (using_class_space()) {
  3046       if (UseSharedSpaces) {
  3047         char* cds_end = (char*)(cds_address + cds_total);
  3048         cds_end = (char *)align_ptr_up(cds_end, _reserve_alignment);
  3049         allocate_metaspace_compressed_klass_ptrs(cds_end, cds_address);
  3050       } else {
  3051         allocate_metaspace_compressed_klass_ptrs((char *)CompressedKlassPointersBase, 0);
  3054 #endif
  3056     // Initialize these before initializing the VirtualSpaceList
  3057     _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord;
  3058     _first_chunk_word_size = align_word_size_up(_first_chunk_word_size);
  3059     // Make the first class chunk bigger than a medium chunk so it's not put
  3060     // on the medium chunk list.   The next chunk will be small and progress
  3061     // from there.  This size calculated by -version.
  3062     _first_class_chunk_word_size = MIN2((size_t)MediumChunk*6,
  3063                                        (CompressedClassSpaceSize/BytesPerWord)*2);
  3064     _first_class_chunk_word_size = align_word_size_up(_first_class_chunk_word_size);
  3065     // Arbitrarily set the initial virtual space to a multiple
  3066     // of the boot class loader size.
  3067     size_t word_size = VIRTUALSPACEMULTIPLIER * _first_chunk_word_size;
  3068     word_size = align_size_up(word_size, Metaspace::reserve_alignment_words());
  3070     // Initialize the list of virtual spaces.
  3071     _space_list = new VirtualSpaceList(word_size);
  3072     _chunk_manager_metadata = new ChunkManager(SpecializedChunk, SmallChunk, MediumChunk);
  3074     if (!_space_list->initialization_succeeded()) {
  3075       vm_exit_during_initialization("Unable to setup metadata virtual space list.", NULL);
  3079   MetaspaceGC::initialize();
  3082 Metachunk* Metaspace::get_initialization_chunk(MetadataType mdtype,
  3083                                                size_t chunk_word_size,
  3084                                                size_t chunk_bunch) {
  3085   // Get a chunk from the chunk freelist
  3086   Metachunk* chunk = get_chunk_manager(mdtype)->chunk_freelist_allocate(chunk_word_size);
  3087   if (chunk != NULL) {
  3088     return chunk;
  3091   return get_space_list(mdtype)->get_new_chunk(chunk_word_size, chunk_word_size, chunk_bunch);
  3094 void Metaspace::initialize(Mutex* lock, MetaspaceType type) {
  3096   assert(space_list() != NULL,
  3097     "Metadata VirtualSpaceList has not been initialized");
  3098   assert(chunk_manager_metadata() != NULL,
  3099     "Metadata ChunkManager has not been initialized");
  3101   _vsm = new SpaceManager(NonClassType, lock);
  3102   if (_vsm == NULL) {
  3103     return;
  3105   size_t word_size;
  3106   size_t class_word_size;
  3107   vsm()->get_initial_chunk_sizes(type, &word_size, &class_word_size);
  3109   if (using_class_space()) {
  3110   assert(class_space_list() != NULL,
  3111     "Class VirtualSpaceList has not been initialized");
  3112   assert(chunk_manager_class() != NULL,
  3113     "Class ChunkManager has not been initialized");
  3115     // Allocate SpaceManager for classes.
  3116     _class_vsm = new SpaceManager(ClassType, lock);
  3117     if (_class_vsm == NULL) {
  3118       return;
  3122   MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
  3124   // Allocate chunk for metadata objects
  3125   Metachunk* new_chunk = get_initialization_chunk(NonClassType,
  3126                                                   word_size,
  3127                                                   vsm()->medium_chunk_bunch());
  3128   assert(!DumpSharedSpaces || new_chunk != NULL, "should have enough space for both chunks");
  3129   if (new_chunk != NULL) {
  3130     // Add to this manager's list of chunks in use and current_chunk().
  3131     vsm()->add_chunk(new_chunk, true);
  3134   // Allocate chunk for class metadata objects
  3135   if (using_class_space()) {
  3136     Metachunk* class_chunk = get_initialization_chunk(ClassType,
  3137                                                       class_word_size,
  3138                                                       class_vsm()->medium_chunk_bunch());
  3139     if (class_chunk != NULL) {
  3140       class_vsm()->add_chunk(class_chunk, true);
  3144   _alloc_record_head = NULL;
  3145   _alloc_record_tail = NULL;
  3148 size_t Metaspace::align_word_size_up(size_t word_size) {
  3149   size_t byte_size = word_size * wordSize;
  3150   return ReservedSpace::allocation_align_size_up(byte_size) / wordSize;
  3153 MetaWord* Metaspace::allocate(size_t word_size, MetadataType mdtype) {
  3154   // DumpSharedSpaces doesn't use class metadata area (yet)
  3155   // Also, don't use class_vsm() unless UseCompressedClassPointers is true.
  3156   if (is_class_space_allocation(mdtype)) {
  3157     return  class_vsm()->allocate(word_size);
  3158   } else {
  3159     return  vsm()->allocate(word_size);
  3163 MetaWord* Metaspace::expand_and_allocate(size_t word_size, MetadataType mdtype) {
  3164   size_t delta_bytes = MetaspaceGC::delta_capacity_until_GC(word_size * BytesPerWord);
  3165   assert(delta_bytes > 0, "Must be");
  3167   size_t after_inc = MetaspaceGC::inc_capacity_until_GC(delta_bytes);
  3168   size_t before_inc = after_inc - delta_bytes;
  3170   if (PrintGCDetails && Verbose) {
  3171     gclog_or_tty->print_cr("Increase capacity to GC from " SIZE_FORMAT
  3172         " to " SIZE_FORMAT, before_inc, after_inc);
  3175   return allocate(word_size, mdtype);
  3178 // Space allocated in the Metaspace.  This may
  3179 // be across several metadata virtual spaces.
  3180 char* Metaspace::bottom() const {
  3181   assert(DumpSharedSpaces, "only useful and valid for dumping shared spaces");
  3182   return (char*)vsm()->current_chunk()->bottom();
  3185 size_t Metaspace::used_words_slow(MetadataType mdtype) const {
  3186   if (mdtype == ClassType) {
  3187     return using_class_space() ? class_vsm()->sum_used_in_chunks_in_use() : 0;
  3188   } else {
  3189     return vsm()->sum_used_in_chunks_in_use();  // includes overhead!
  3193 size_t Metaspace::free_words_slow(MetadataType mdtype) const {
  3194   if (mdtype == ClassType) {
  3195     return using_class_space() ? class_vsm()->sum_free_in_chunks_in_use() : 0;
  3196   } else {
  3197     return vsm()->sum_free_in_chunks_in_use();
  3201 // Space capacity in the Metaspace.  It includes
  3202 // space in the list of chunks from which allocations
  3203 // have been made. Don't include space in the global freelist and
  3204 // in the space available in the dictionary which
  3205 // is already counted in some chunk.
  3206 size_t Metaspace::capacity_words_slow(MetadataType mdtype) const {
  3207   if (mdtype == ClassType) {
  3208     return using_class_space() ? class_vsm()->sum_capacity_in_chunks_in_use() : 0;
  3209   } else {
  3210     return vsm()->sum_capacity_in_chunks_in_use();
  3214 size_t Metaspace::used_bytes_slow(MetadataType mdtype) const {
  3215   return used_words_slow(mdtype) * BytesPerWord;
  3218 size_t Metaspace::capacity_bytes_slow(MetadataType mdtype) const {
  3219   return capacity_words_slow(mdtype) * BytesPerWord;
  3222 void Metaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) {
  3223   if (SafepointSynchronize::is_at_safepoint()) {
  3224     assert(Thread::current()->is_VM_thread(), "should be the VM thread");
  3225     // Don't take Heap_lock
  3226     MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag);
  3227     if (word_size < TreeChunk<Metablock, FreeList>::min_size()) {
  3228       // Dark matter.  Too small for dictionary.
  3229 #ifdef ASSERT
  3230       Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5);
  3231 #endif
  3232       return;
  3234     if (is_class && using_class_space()) {
  3235       class_vsm()->deallocate(ptr, word_size);
  3236     } else {
  3237       vsm()->deallocate(ptr, word_size);
  3239   } else {
  3240     MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag);
  3242     if (word_size < TreeChunk<Metablock, FreeList>::min_size()) {
  3243       // Dark matter.  Too small for dictionary.
  3244 #ifdef ASSERT
  3245       Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5);
  3246 #endif
  3247       return;
  3249     if (is_class && using_class_space()) {
  3250       class_vsm()->deallocate(ptr, word_size);
  3251     } else {
  3252       vsm()->deallocate(ptr, word_size);
  3258 MetaWord* Metaspace::allocate(ClassLoaderData* loader_data, size_t word_size,
  3259                               bool read_only, MetaspaceObj::Type type, TRAPS) {
  3260   if (HAS_PENDING_EXCEPTION) {
  3261     assert(false, "Should not allocate with exception pending");
  3262     return NULL;  // caller does a CHECK_NULL too
  3265   assert(loader_data != NULL, "Should never pass around a NULL loader_data. "
  3266         "ClassLoaderData::the_null_class_loader_data() should have been used.");
  3268   // Allocate in metaspaces without taking out a lock, because it deadlocks
  3269   // with the SymbolTable_lock.  Dumping is single threaded for now.  We'll have
  3270   // to revisit this for application class data sharing.
  3271   if (DumpSharedSpaces) {
  3272     assert(type > MetaspaceObj::UnknownType && type < MetaspaceObj::_number_of_types, "sanity");
  3273     Metaspace* space = read_only ? loader_data->ro_metaspace() : loader_data->rw_metaspace();
  3274     MetaWord* result = space->allocate(word_size, NonClassType);
  3275     if (result == NULL) {
  3276       report_out_of_shared_space(read_only ? SharedReadOnly : SharedReadWrite);
  3279     space->record_allocation(result, type, space->vsm()->get_raw_word_size(word_size));
  3281     // Zero initialize.
  3282     Copy::fill_to_aligned_words((HeapWord*)result, word_size, 0);
  3284     return result;
  3287   MetadataType mdtype = (type == MetaspaceObj::ClassType) ? ClassType : NonClassType;
  3289   // Try to allocate metadata.
  3290   MetaWord* result = loader_data->metaspace_non_null()->allocate(word_size, mdtype);
  3292   if (result == NULL) {
  3293     // Allocation failed.
  3294     if (is_init_completed()) {
  3295       // Only start a GC if the bootstrapping has completed.
  3297       // Try to clean out some memory and retry.
  3298       result = Universe::heap()->collector_policy()->satisfy_failed_metadata_allocation(
  3299           loader_data, word_size, mdtype);
  3303   if (result == NULL) {
  3304     report_metadata_oome(loader_data, word_size, mdtype, THREAD);
  3305     // Will not reach here.
  3306     return NULL;
  3309   // Zero initialize.
  3310   Copy::fill_to_aligned_words((HeapWord*)result, word_size, 0);
  3312   return result;
  3315 size_t Metaspace::class_chunk_size(size_t word_size) {
  3316   assert(using_class_space(), "Has to use class space");
  3317   return class_vsm()->calc_chunk_size(word_size);
  3320 void Metaspace::report_metadata_oome(ClassLoaderData* loader_data, size_t word_size, MetadataType mdtype, TRAPS) {
  3321   // If result is still null, we are out of memory.
  3322   if (Verbose && TraceMetadataChunkAllocation) {
  3323     gclog_or_tty->print_cr("Metaspace allocation failed for size "
  3324         SIZE_FORMAT, word_size);
  3325     if (loader_data->metaspace_or_null() != NULL) {
  3326       loader_data->dump(gclog_or_tty);
  3328     MetaspaceAux::dump(gclog_or_tty);
  3331   bool out_of_compressed_class_space = false;
  3332   if (is_class_space_allocation(mdtype)) {
  3333     Metaspace* metaspace = loader_data->metaspace_non_null();
  3334     out_of_compressed_class_space =
  3335       MetaspaceAux::committed_bytes(Metaspace::ClassType) +
  3336       (metaspace->class_chunk_size(word_size) * BytesPerWord) >
  3337       CompressedClassSpaceSize;
  3340   // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
  3341   const char* space_string = out_of_compressed_class_space ?
  3342     "Compressed class space" : "Metaspace";
  3344   report_java_out_of_memory(space_string);
  3346   if (JvmtiExport::should_post_resource_exhausted()) {
  3347     JvmtiExport::post_resource_exhausted(
  3348         JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,
  3349         space_string);
  3352   if (!is_init_completed()) {
  3353     vm_exit_during_initialization("OutOfMemoryError", space_string);
  3356   if (out_of_compressed_class_space) {
  3357     THROW_OOP(Universe::out_of_memory_error_class_metaspace());
  3358   } else {
  3359     THROW_OOP(Universe::out_of_memory_error_metaspace());
  3363 void Metaspace::record_allocation(void* ptr, MetaspaceObj::Type type, size_t word_size) {
  3364   assert(DumpSharedSpaces, "sanity");
  3366   AllocRecord *rec = new AllocRecord((address)ptr, type, (int)word_size * HeapWordSize);
  3367   if (_alloc_record_head == NULL) {
  3368     _alloc_record_head = _alloc_record_tail = rec;
  3369   } else {
  3370     _alloc_record_tail->_next = rec;
  3371     _alloc_record_tail = rec;
  3375 void Metaspace::iterate(Metaspace::AllocRecordClosure *closure) {
  3376   assert(DumpSharedSpaces, "unimplemented for !DumpSharedSpaces");
  3378   address last_addr = (address)bottom();
  3380   for (AllocRecord *rec = _alloc_record_head; rec; rec = rec->_next) {
  3381     address ptr = rec->_ptr;
  3382     if (last_addr < ptr) {
  3383       closure->doit(last_addr, MetaspaceObj::UnknownType, ptr - last_addr);
  3385     closure->doit(ptr, rec->_type, rec->_byte_size);
  3386     last_addr = ptr + rec->_byte_size;
  3389   address top = ((address)bottom()) + used_bytes_slow(Metaspace::NonClassType);
  3390   if (last_addr < top) {
  3391     closure->doit(last_addr, MetaspaceObj::UnknownType, top - last_addr);
  3395 void Metaspace::purge(MetadataType mdtype) {
  3396   get_space_list(mdtype)->purge(get_chunk_manager(mdtype));
  3399 void Metaspace::purge() {
  3400   MutexLockerEx cl(SpaceManager::expand_lock(),
  3401                    Mutex::_no_safepoint_check_flag);
  3402   purge(NonClassType);
  3403   if (using_class_space()) {
  3404     purge(ClassType);
  3408 void Metaspace::print_on(outputStream* out) const {
  3409   // Print both class virtual space counts and metaspace.
  3410   if (Verbose) {
  3411     vsm()->print_on(out);
  3412     if (using_class_space()) {
  3413       class_vsm()->print_on(out);
  3418 bool Metaspace::contains(const void * ptr) {
  3419   if (MetaspaceShared::is_in_shared_space(ptr)) {
  3420     return true;
  3422   // This is checked while unlocked.  As long as the virtualspaces are added
  3423   // at the end, the pointer will be in one of them.  The virtual spaces
  3424   // aren't deleted presently.  When they are, some sort of locking might
  3425   // be needed.  Note, locking this can cause inversion problems with the
  3426   // caller in MetaspaceObj::is_metadata() function.
  3427   return space_list()->contains(ptr) ||
  3428          (using_class_space() && class_space_list()->contains(ptr));
  3431 void Metaspace::verify() {
  3432   vsm()->verify();
  3433   if (using_class_space()) {
  3434     class_vsm()->verify();
  3438 void Metaspace::dump(outputStream* const out) const {
  3439   out->print_cr("\nVirtual space manager: " INTPTR_FORMAT, vsm());
  3440   vsm()->dump(out);
  3441   if (using_class_space()) {
  3442     out->print_cr("\nClass space manager: " INTPTR_FORMAT, class_vsm());
  3443     class_vsm()->dump(out);
  3447 /////////////// Unit tests ///////////////
  3449 #ifndef PRODUCT
  3451 class TestMetaspaceAuxTest : AllStatic {
  3452  public:
  3453   static void test_reserved() {
  3454     size_t reserved = MetaspaceAux::reserved_bytes();
  3456     assert(reserved > 0, "assert");
  3458     size_t committed  = MetaspaceAux::committed_bytes();
  3459     assert(committed <= reserved, "assert");
  3461     size_t reserved_metadata = MetaspaceAux::reserved_bytes(Metaspace::NonClassType);
  3462     assert(reserved_metadata > 0, "assert");
  3463     assert(reserved_metadata <= reserved, "assert");
  3465     if (UseCompressedClassPointers) {
  3466       size_t reserved_class    = MetaspaceAux::reserved_bytes(Metaspace::ClassType);
  3467       assert(reserved_class > 0, "assert");
  3468       assert(reserved_class < reserved, "assert");
  3472   static void test_committed() {
  3473     size_t committed = MetaspaceAux::committed_bytes();
  3475     assert(committed > 0, "assert");
  3477     size_t reserved  = MetaspaceAux::reserved_bytes();
  3478     assert(committed <= reserved, "assert");
  3480     size_t committed_metadata = MetaspaceAux::committed_bytes(Metaspace::NonClassType);
  3481     assert(committed_metadata > 0, "assert");
  3482     assert(committed_metadata <= committed, "assert");
  3484     if (UseCompressedClassPointers) {
  3485       size_t committed_class    = MetaspaceAux::committed_bytes(Metaspace::ClassType);
  3486       assert(committed_class > 0, "assert");
  3487       assert(committed_class < committed, "assert");
  3491   static void test_virtual_space_list_large_chunk() {
  3492     VirtualSpaceList* vs_list = new VirtualSpaceList(os::vm_allocation_granularity());
  3493     MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
  3494     // A size larger than VirtualSpaceSize (256k) and add one page to make it _not_ be
  3495     // vm_allocation_granularity aligned on Windows.
  3496     size_t large_size = (size_t)(2*256*K + (os::vm_page_size()/BytesPerWord));
  3497     large_size += (os::vm_page_size()/BytesPerWord);
  3498     vs_list->get_new_chunk(large_size, large_size, 0);
  3501   static void test() {
  3502     test_reserved();
  3503     test_committed();
  3504     test_virtual_space_list_large_chunk();
  3506 };
  3508 void TestMetaspaceAux_test() {
  3509   TestMetaspaceAuxTest::test();
  3512 #endif

mercurial