src/share/vm/memory/metaspace.cpp

Fri, 16 Nov 2012 09:19:12 -0500

author
coleenp
date
Fri, 16 Nov 2012 09:19:12 -0500
changeset 4280
80e866b1d053
parent 4264
6bc207d87e5d
child 4295
59c790074993
child 4304
90273fc0a981
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright (c) 2011, 2012, 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/binaryTreeDictionary.hpp"
    27 #include "memory/freeList.hpp"
    28 #include "memory/collectorPolicy.hpp"
    29 #include "memory/filemap.hpp"
    30 #include "memory/freeList.hpp"
    31 #include "memory/metablock.hpp"
    32 #include "memory/metachunk.hpp"
    33 #include "memory/metaspace.hpp"
    34 #include "memory/metaspaceShared.hpp"
    35 #include "memory/resourceArea.hpp"
    36 #include "memory/universe.hpp"
    37 #include "runtime/globals.hpp"
    38 #include "runtime/mutex.hpp"
    39 #include "services/memTracker.hpp"
    40 #include "utilities/copy.hpp"
    41 #include "utilities/debug.hpp"
    43 typedef BinaryTreeDictionary<Metablock, FreeList> BlockTreeDictionary;
    44 typedef BinaryTreeDictionary<Metachunk, FreeList> ChunkTreeDictionary;
    45 // Define this macro to enable slow integrity checking of
    46 // the free chunk lists
    47 const bool metaspace_slow_verify = false;
    50 // Parameters for stress mode testing
    51 const uint metadata_deallocate_a_lot_block = 10;
    52 const uint metadata_deallocate_a_lock_chunk = 3;
    53 size_t const allocation_from_dictionary_limit = 64 * K;
    54 const size_t metadata_chunk_initialize = 0xf7f7f7f7;
    55 const size_t metadata_deallocate = 0xf5f5f5f5;
    57 MetaWord* last_allocated = 0;
    59 // Used in declarations in SpaceManager and ChunkManager
    60 enum ChunkIndex {
    61   SmallIndex = 0,
    62   MediumIndex = 1,
    63   HumongousIndex = 2,
    64   NumberOfFreeLists = 2,
    65   NumberOfInUseLists = 3
    66 };
    68 static ChunkIndex next_chunk_index(ChunkIndex i) {
    69   assert(i < NumberOfInUseLists, "Out of bound");
    70   return (ChunkIndex) (i+1);
    71 }
    73 // Originally _capacity_until_GC was set to MetaspaceSize here but
    74 // the default MetaspaceSize before argument processing was being
    75 // used which was not the desired value.  See the code
    76 // in should_expand() to see how the initialization is handled
    77 // now.
    78 size_t MetaspaceGC::_capacity_until_GC = 0;
    79 bool MetaspaceGC::_expand_after_GC = false;
    80 uint MetaspaceGC::_shrink_factor = 0;
    81 bool MetaspaceGC::_should_concurrent_collect = false;
    83 // Blocks of space for metadata are allocated out of Metachunks.
    84 //
    85 // Metachunk are allocated out of MetadataVirtualspaces and once
    86 // allocated there is no explicit link between a Metachunk and
    87 // the MetadataVirtualspaces from which it was allocated.
    88 //
    89 // Each SpaceManager maintains a
    90 // list of the chunks it is using and the current chunk.  The current
    91 // chunk is the chunk from which allocations are done.  Space freed in
    92 // a chunk is placed on the free list of blocks (BlockFreelist) and
    93 // reused from there.
    94 //
    95 // Future modification
    96 //
    97 // The Metachunk can conceivable be replaced by the Chunk in
    98 // allocation.hpp.  Note that the latter Chunk is the space for
    99 // allocation (allocations from the chunk are out of the space in
   100 // the Chunk after the header for the Chunk) where as Metachunks
   101 // point to space in a VirtualSpace.  To replace Metachunks with
   102 // Chunks, change Chunks so that they can be allocated out of a VirtualSpace.
   103 size_t Metablock::_min_block_byte_size = sizeof(Metablock);
   104 #ifdef ASSERT
   105   size_t Metablock::_overhead =
   106     Chunk::aligned_overhead_size(sizeof(Metablock)) / BytesPerWord;
   107 #else
   108   size_t Metablock::_overhead = 0;
   109 #endif
   112 // Pointer to list of Metachunks.
   113 class ChunkList VALUE_OBJ_CLASS_SPEC {
   114   // List of free chunks
   115   Metachunk* _head;
   117  public:
   118   // Constructor
   119   ChunkList() : _head(NULL) {}
   121   // Accessors
   122   Metachunk* head() { return _head; }
   123   void set_head(Metachunk* v) { _head = v; }
   125   // Link at head of the list
   126   void add_at_head(Metachunk* head, Metachunk* tail);
   127   void add_at_head(Metachunk* head);
   129   size_t sum_list_size();
   130   size_t sum_list_count();
   131   size_t sum_list_capacity();
   132 };
   134 // Manages the global free lists of chunks.
   135 // Has three lists of free chunks, and a total size and
   136 // count that includes all three
   138 class ChunkManager VALUE_OBJ_CLASS_SPEC {
   140   // Free list of chunks of different sizes.
   141   //   SmallChunk
   142   //   MediumChunk
   143   //   HumongousChunk
   144   ChunkList _free_chunks[NumberOfFreeLists];
   146   //   HumongousChunk
   147   ChunkTreeDictionary _humongous_dictionary;
   149   // ChunkManager in all lists of this type
   150   size_t _free_chunks_total;
   151   size_t _free_chunks_count;
   153   void dec_free_chunks_total(size_t v) {
   154     assert(_free_chunks_count > 0 &&
   155              _free_chunks_total > 0,
   156              "About to go negative");
   157     Atomic::add_ptr(-1, &_free_chunks_count);
   158     jlong minus_v = (jlong) - (jlong) v;
   159     Atomic::add_ptr(minus_v, &_free_chunks_total);
   160   }
   162   // Debug support
   164   size_t sum_free_chunks();
   165   size_t sum_free_chunks_count();
   167   void locked_verify_free_chunks_total();
   168   void slow_locked_verify_free_chunks_total() {
   169     if (metaspace_slow_verify) {
   170       locked_verify_free_chunks_total();
   171     }
   172   }
   173   void locked_verify_free_chunks_count();
   174   void slow_locked_verify_free_chunks_count() {
   175     if (metaspace_slow_verify) {
   176       locked_verify_free_chunks_count();
   177     }
   178   }
   179   void verify_free_chunks_count();
   181  public:
   183   ChunkManager() : _free_chunks_total(0), _free_chunks_count(0) {}
   185   // add or delete (return) a chunk to the global freelist.
   186   Metachunk* chunk_freelist_allocate(size_t word_size);
   187   void chunk_freelist_deallocate(Metachunk* chunk);
   189   // Total of the space in the free chunks list
   190   size_t free_chunks_total();
   191   size_t free_chunks_total_in_bytes();
   193   // Number of chunks in the free chunks list
   194   size_t free_chunks_count();
   196   void inc_free_chunks_total(size_t v, size_t count = 1) {
   197     Atomic::add_ptr(count, &_free_chunks_count);
   198     Atomic::add_ptr(v, &_free_chunks_total);
   199   }
   200   ChunkList* free_medium_chunks() { return &_free_chunks[1]; }
   201   ChunkList* free_small_chunks() { return &_free_chunks[0]; }
   202   ChunkTreeDictionary* humongous_dictionary() {
   203     return &_humongous_dictionary;
   204   }
   206   ChunkList* free_chunks(ChunkIndex index);
   208   // Returns the list for the given chunk word size.
   209   ChunkList* find_free_chunks_list(size_t word_size);
   211   // Add and remove from a list by size.  Selects
   212   // list based on size of chunk.
   213   void free_chunks_put(Metachunk* chuck);
   214   Metachunk* free_chunks_get(size_t chunk_word_size);
   216   // Debug support
   217   void verify();
   218   void slow_verify() {
   219     if (metaspace_slow_verify) {
   220       verify();
   221     }
   222   }
   223   void locked_verify();
   224   void slow_locked_verify() {
   225     if (metaspace_slow_verify) {
   226       locked_verify();
   227     }
   228   }
   229   void verify_free_chunks_total();
   231   void locked_print_free_chunks(outputStream* st);
   232   void locked_print_sum_free_chunks(outputStream* st);
   234   void print_on(outputStream* st);
   235 };
   238 // Used to manage the free list of Metablocks (a block corresponds
   239 // to the allocation of a quantum of metadata).
   240 class BlockFreelist VALUE_OBJ_CLASS_SPEC {
   241   BlockTreeDictionary* _dictionary;
   242   static Metablock* initialize_free_chunk(MetaWord* p, size_t word_size);
   244   // Accessors
   245   BlockTreeDictionary* dictionary() const { return _dictionary; }
   247  public:
   248   BlockFreelist();
   249   ~BlockFreelist();
   251   // Get and return a block to the free list
   252   MetaWord* get_block(size_t word_size);
   253   void return_block(MetaWord* p, size_t word_size);
   255   size_t total_size() {
   256   if (dictionary() == NULL) {
   257     return 0;
   258   } else {
   259     return dictionary()->total_size();
   260   }
   261 }
   263   void print_on(outputStream* st) const;
   264 };
   266 class VirtualSpaceNode : public CHeapObj<mtClass> {
   267   friend class VirtualSpaceList;
   269   // Link to next VirtualSpaceNode
   270   VirtualSpaceNode* _next;
   272   // total in the VirtualSpace
   273   MemRegion _reserved;
   274   ReservedSpace _rs;
   275   VirtualSpace _virtual_space;
   276   MetaWord* _top;
   278   // Convenience functions for logical bottom and end
   279   MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
   280   MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
   282   // Convenience functions to access the _virtual_space
   283   char* low()  const { return virtual_space()->low(); }
   284   char* high() const { return virtual_space()->high(); }
   286  public:
   288   VirtualSpaceNode(size_t byte_size);
   289   VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs) {}
   290   ~VirtualSpaceNode();
   292   // address of next available space in _virtual_space;
   293   // Accessors
   294   VirtualSpaceNode* next() { return _next; }
   295   void set_next(VirtualSpaceNode* v) { _next = v; }
   297   void set_reserved(MemRegion const v) { _reserved = v; }
   298   void set_top(MetaWord* v) { _top = v; }
   300   // Accessors
   301   MemRegion* reserved() { return &_reserved; }
   302   VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; }
   304   // Returns true if "word_size" is available in the virtual space
   305   bool is_available(size_t word_size) { return _top + word_size <= end(); }
   307   MetaWord* top() const { return _top; }
   308   void inc_top(size_t word_size) { _top += word_size; }
   310   // used and capacity in this single entry in the list
   311   size_t used_words_in_vs() const;
   312   size_t capacity_words_in_vs() const;
   314   bool initialize();
   316   // get space from the virtual space
   317   Metachunk* take_from_committed(size_t chunk_word_size);
   319   // Allocate a chunk from the virtual space and return it.
   320   Metachunk* get_chunk_vs(size_t chunk_word_size);
   321   Metachunk* get_chunk_vs_with_expand(size_t chunk_word_size);
   323   // Expands/shrinks the committed space in a virtual space.  Delegates
   324   // to Virtualspace
   325   bool expand_by(size_t words, bool pre_touch = false);
   326   bool shrink_by(size_t words);
   328   // Debug support
   329   static void verify_virtual_space_total();
   330   static void verify_virtual_space_count();
   331   void mangle();
   333   void print_on(outputStream* st) const;
   334 };
   336   // byte_size is the size of the associated virtualspace.
   337 VirtualSpaceNode::VirtualSpaceNode(size_t byte_size) : _top(NULL), _next(NULL), _rs(0) {
   338   // This allocates memory with mmap.  For DumpSharedspaces, allocate the
   339   // space at low memory so that other shared images don't conflict.
   340   // This is the same address as memory needed for UseCompressedOops but
   341   // compressed oops don't work with CDS (offsets in metadata are wrong), so
   342   // borrow the same address.
   343   if (DumpSharedSpaces) {
   344     char* shared_base = (char*)HeapBaseMinAddress;
   345     _rs = ReservedSpace(byte_size, 0, false, shared_base, 0);
   346     if (_rs.is_reserved()) {
   347       assert(_rs.base() == shared_base, "should match");
   348     } else {
   349       // If we are dumping the heap, then allocate a wasted block of address
   350       // space in order to push the heap to a lower address.  This extra
   351       // address range allows for other (or larger) libraries to be loaded
   352       // without them occupying the space required for the shared spaces.
   353       uintx reserved = 0;
   354       uintx block_size = 64*1024*1024;
   355       while (reserved < SharedDummyBlockSize) {
   356         char* dummy = os::reserve_memory(block_size);
   357         reserved += block_size;
   358       }
   359       _rs = ReservedSpace(byte_size);
   360     }
   361     MetaspaceShared::set_shared_rs(&_rs);
   362   } else {
   363     _rs = ReservedSpace(byte_size);
   364   }
   366   MemTracker::record_virtual_memory_type((address)_rs.base(), mtClass);
   367 }
   369 // List of VirtualSpaces for metadata allocation.
   370 // It has a  _next link for singly linked list and a MemRegion
   371 // for total space in the VirtualSpace.
   372 class VirtualSpaceList : public CHeapObj<mtClass> {
   373   friend class VirtualSpaceNode;
   375   enum VirtualSpaceSizes {
   376     VirtualSpaceSize = 256 * K
   377   };
   379   // Global list of virtual spaces
   380   // Head of the list
   381   VirtualSpaceNode* _virtual_space_list;
   382   // virtual space currently being used for allocations
   383   VirtualSpaceNode* _current_virtual_space;
   384   // Free chunk list for all other metadata
   385   ChunkManager      _chunk_manager;
   387   // Can this virtual list allocate >1 spaces?  Also, used to determine
   388   // whether to allocate unlimited small chunks in this virtual space
   389   bool _is_class;
   390   bool can_grow() const { return !is_class() || !UseCompressedKlassPointers; }
   392   // Sum of space in all virtual spaces and number of virtual spaces
   393   size_t _virtual_space_total;
   394   size_t _virtual_space_count;
   396   ~VirtualSpaceList();
   398   VirtualSpaceNode* virtual_space_list() const { return _virtual_space_list; }
   400   void set_virtual_space_list(VirtualSpaceNode* v) {
   401     _virtual_space_list = v;
   402   }
   403   void set_current_virtual_space(VirtualSpaceNode* v) {
   404     _current_virtual_space = v;
   405   }
   407   void link_vs(VirtualSpaceNode* new_entry, size_t vs_word_size);
   409   // Get another virtual space and add it to the list.  This
   410   // is typically prompted by a failed attempt to allocate a chunk
   411   // and is typically followed by the allocation of a chunk.
   412   bool grow_vs(size_t vs_word_size);
   414  public:
   415   VirtualSpaceList(size_t word_size);
   416   VirtualSpaceList(ReservedSpace rs);
   418   Metachunk* get_new_chunk(size_t word_size, size_t grow_chunks_by_words);
   420   VirtualSpaceNode* current_virtual_space() {
   421     return _current_virtual_space;
   422   }
   424   ChunkManager* chunk_manager() { return &_chunk_manager; }
   425   bool is_class() const { return _is_class; }
   427   // Allocate the first virtualspace.
   428   void initialize(size_t word_size);
   430   size_t virtual_space_total() { return _virtual_space_total; }
   431   void inc_virtual_space_total(size_t v) {
   432     Atomic::add_ptr(v, &_virtual_space_total);
   433   }
   435   size_t virtual_space_count() { return _virtual_space_count; }
   436   void inc_virtual_space_count() {
   437     Atomic::inc_ptr(&_virtual_space_count);
   438   }
   440   // Used and capacity in the entire list of virtual spaces.
   441   // These are global values shared by all Metaspaces
   442   size_t capacity_words_sum();
   443   size_t capacity_bytes_sum() { return capacity_words_sum() * BytesPerWord; }
   444   size_t used_words_sum();
   445   size_t used_bytes_sum() { return used_words_sum() * BytesPerWord; }
   447   bool contains(const void *ptr);
   449   void print_on(outputStream* st) const;
   451   class VirtualSpaceListIterator : public StackObj {
   452     VirtualSpaceNode* _virtual_spaces;
   453    public:
   454     VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) :
   455       _virtual_spaces(virtual_spaces) {}
   457     bool repeat() {
   458       return _virtual_spaces != NULL;
   459     }
   461     VirtualSpaceNode* get_next() {
   462       VirtualSpaceNode* result = _virtual_spaces;
   463       if (_virtual_spaces != NULL) {
   464         _virtual_spaces = _virtual_spaces->next();
   465       }
   466       return result;
   467     }
   468   };
   469 };
   471 class Metadebug : AllStatic {
   472   // Debugging support for Metaspaces
   473   static int _deallocate_block_a_lot_count;
   474   static int _deallocate_chunk_a_lot_count;
   475   static int _allocation_fail_alot_count;
   477  public:
   478   static int deallocate_block_a_lot_count() {
   479     return _deallocate_block_a_lot_count;
   480   }
   481   static void set_deallocate_block_a_lot_count(int v) {
   482     _deallocate_block_a_lot_count = v;
   483   }
   484   static void inc_deallocate_block_a_lot_count() {
   485     _deallocate_block_a_lot_count++;
   486   }
   487   static int deallocate_chunk_a_lot_count() {
   488     return _deallocate_chunk_a_lot_count;
   489   }
   490   static void reset_deallocate_chunk_a_lot_count() {
   491     _deallocate_chunk_a_lot_count = 1;
   492   }
   493   static void inc_deallocate_chunk_a_lot_count() {
   494     _deallocate_chunk_a_lot_count++;
   495   }
   497   static void init_allocation_fail_alot_count();
   498 #ifdef ASSERT
   499   static bool test_metadata_failure();
   500 #endif
   502   static void deallocate_chunk_a_lot(SpaceManager* sm,
   503                                      size_t chunk_word_size);
   504   static void deallocate_block_a_lot(SpaceManager* sm,
   505                                      size_t chunk_word_size);
   507 };
   509 int Metadebug::_deallocate_block_a_lot_count = 0;
   510 int Metadebug::_deallocate_chunk_a_lot_count = 0;
   511 int Metadebug::_allocation_fail_alot_count = 0;
   513 //  SpaceManager - used by Metaspace to handle allocations
   514 class SpaceManager : public CHeapObj<mtClass> {
   515   friend class Metaspace;
   516   friend class Metadebug;
   518  private:
   519   // protects allocations and contains.
   520   Mutex* const _lock;
   522   // List of chunks in use by this SpaceManager.  Allocations
   523   // are done from the current chunk.  The list is used for deallocating
   524   // chunks when the SpaceManager is freed.
   525   Metachunk* _chunks_in_use[NumberOfInUseLists];
   526   Metachunk* _current_chunk;
   528   // Virtual space where allocation comes from.
   529   VirtualSpaceList* _vs_list;
   531   // Number of small chunks to allocate to a manager
   532   // If class space manager, small chunks are unlimited
   533   static uint const _small_chunk_limit;
   534   bool has_small_chunk_limit() { return !vs_list()->is_class(); }
   536   // Sum of all space in allocated chunks
   537   size_t _allocation_total;
   539   // Free lists of blocks are per SpaceManager since they
   540   // are assumed to be in chunks in use by the SpaceManager
   541   // and all chunks in use by a SpaceManager are freed when
   542   // the class loader using the SpaceManager is collected.
   543   BlockFreelist _block_freelists;
   545   // protects virtualspace and chunk expansions
   546   static const char*  _expand_lock_name;
   547   static const int    _expand_lock_rank;
   548   static Mutex* const _expand_lock;
   550   // Accessors
   551   Metachunk* chunks_in_use(ChunkIndex index) const { return _chunks_in_use[index]; }
   552   void set_chunks_in_use(ChunkIndex index, Metachunk* v) { _chunks_in_use[index] = v; }
   554   BlockFreelist* block_freelists() const {
   555     return (BlockFreelist*) &_block_freelists;
   556   }
   558   VirtualSpaceList* vs_list() const    { return _vs_list; }
   560   Metachunk* current_chunk() const { return _current_chunk; }
   561   void set_current_chunk(Metachunk* v) {
   562     _current_chunk = v;
   563   }
   565   Metachunk* find_current_chunk(size_t word_size);
   567   // Add chunk to the list of chunks in use
   568   void add_chunk(Metachunk* v, bool make_current);
   570   Mutex* lock() const { return _lock; }
   572  public:
   573   SpaceManager(Mutex* lock, VirtualSpaceList* vs_list);
   574   ~SpaceManager();
   576   enum ChunkSizes {    // in words.
   577     SmallChunk = 512,
   578     MediumChunk = 8 * K,
   579     MediumChunkBunch = 4 * MediumChunk
   580   };
   582   // Accessors
   583   size_t allocation_total() const { return _allocation_total; }
   584   void inc_allocation_total(size_t v) { Atomic::add_ptr(v, &_allocation_total); }
   585   static bool is_humongous(size_t word_size) { return word_size > MediumChunk; }
   587   static Mutex* expand_lock() { return _expand_lock; }
   589   size_t sum_capacity_in_chunks_in_use() const;
   590   size_t sum_used_in_chunks_in_use() const;
   591   size_t sum_free_in_chunks_in_use() const;
   592   size_t sum_waste_in_chunks_in_use() const;
   593   size_t sum_waste_in_chunks_in_use(ChunkIndex index ) const;
   595   size_t sum_count_in_chunks_in_use();
   596   size_t sum_count_in_chunks_in_use(ChunkIndex i);
   598   // Block allocation and deallocation.
   599   // Allocates a block from the current chunk
   600   MetaWord* allocate(size_t word_size);
   602   // Helper for allocations
   603   MetaWord* allocate_work(size_t word_size);
   605   // Returns a block to the per manager freelist
   606   void deallocate(MetaWord* p, size_t word_size);
   608   // Based on the allocation size and a minimum chunk size,
   609   // returned chunk size (for expanding space for chunk allocation).
   610   size_t calc_chunk_size(size_t allocation_word_size);
   612   // Called when an allocation from the current chunk fails.
   613   // Gets a new chunk (may require getting a new virtual space),
   614   // and allocates from that chunk.
   615   MetaWord* grow_and_allocate(size_t word_size);
   617   // debugging support.
   619   void dump(outputStream* const out) const;
   620   void print_on(outputStream* st) const;
   621   void locked_print_chunks_in_use_on(outputStream* st) const;
   623   void verify();
   624 #ifdef ASSERT
   625   void mangle_freed_chunks();
   626   void verify_allocation_total();
   627 #endif
   628 };
   630 uint const SpaceManager::_small_chunk_limit = 4;
   634 const char* SpaceManager::_expand_lock_name =
   635   "SpaceManager chunk allocation lock";
   636 const int SpaceManager::_expand_lock_rank = Monitor::leaf - 1;
   637 Mutex* const SpaceManager::_expand_lock =
   638   new Mutex(SpaceManager::_expand_lock_rank,
   639             SpaceManager::_expand_lock_name,
   640             Mutex::_allow_vm_block_flag);
   642 size_t Metachunk::_overhead =
   643   Chunk::aligned_overhead_size(sizeof(Metachunk)) / BytesPerWord;
   645 // New blocks returned by the Metaspace are zero initialized.
   646 // We should fix the constructors to not assume this instead.
   647 Metablock* Metablock::initialize(MetaWord* p, size_t word_size) {
   648   if (p == NULL) {
   649     return NULL;
   650   }
   652   Metablock* result = (Metablock*) p;
   654   // Clear the memory
   655   Copy::fill_to_aligned_words((HeapWord*)result, word_size);
   656 #ifdef ASSERT
   657   result->set_word_size(word_size);
   658 #endif
   659   return result;
   660 }
   662 // Metachunk methods
   664 Metachunk* Metachunk::initialize(MetaWord* ptr, size_t word_size) {
   665   // Set bottom, top, and end.  Allow space for the Metachunk itself
   666   Metachunk* chunk = (Metachunk*) ptr;
   668   MetaWord* chunk_bottom = ptr + _overhead;
   669   chunk->set_bottom(ptr);
   670   chunk->set_top(chunk_bottom);
   671   MetaWord* chunk_end = ptr + word_size;
   672   assert(chunk_end > chunk_bottom, "Chunk must be too small");
   673   chunk->set_end(chunk_end);
   674   chunk->set_next(NULL);
   675   chunk->set_word_size(word_size);
   676 #ifdef ASSERT
   677   size_t data_word_size = pointer_delta(chunk_end, chunk_bottom, sizeof(MetaWord));
   678   Copy::fill_to_words((HeapWord*) chunk_bottom, data_word_size, metadata_chunk_initialize);
   679 #endif
   680   return chunk;
   681 }
   684 MetaWord* Metachunk::allocate(size_t word_size) {
   685   MetaWord* result = NULL;
   686   // If available, bump the pointer to allocate.
   687   if (free_word_size() >= word_size) {
   688     result = _top;
   689     _top = _top + word_size;
   690   }
   691   return result;
   692 }
   694 // _bottom points to the start of the chunk including the overhead.
   695 size_t Metachunk::used_word_size() {
   696   return pointer_delta(_top, _bottom, sizeof(MetaWord));
   697 }
   699 size_t Metachunk::free_word_size() {
   700   return pointer_delta(_end, _top, sizeof(MetaWord));
   701 }
   703 size_t Metachunk::capacity_word_size() {
   704   return pointer_delta(_end, _bottom, sizeof(MetaWord));
   705 }
   707 void Metachunk::print_on(outputStream* st) const {
   708   st->print_cr("Metachunk:"
   709                " bottom " PTR_FORMAT " top " PTR_FORMAT
   710                " end " PTR_FORMAT " size " SIZE_FORMAT,
   711                bottom(), top(), end(), word_size());
   712 }
   714 #ifdef ASSERT
   715 void Metachunk::mangle() {
   716   // Mangle the payload of the chunk and not the links that
   717   // maintain list of chunks.
   718   HeapWord* start = (HeapWord*)(bottom() + overhead());
   719   size_t word_size = capacity_word_size() - overhead();
   720   Copy::fill_to_words(start, word_size, metadata_chunk_initialize);
   721 }
   722 #endif // ASSERT
   724 void Metachunk::verify() {
   725 #ifdef ASSERT
   726   // Cannot walk through the blocks unless the blocks have
   727   // headers with sizes.
   728   assert(_bottom <= _top &&
   729          _top <= _end,
   730          "Chunk has been smashed");
   731   assert(SpaceManager::is_humongous(_word_size) ||
   732          _word_size == SpaceManager::MediumChunk ||
   733          _word_size == SpaceManager::SmallChunk,
   734          "Chunk size is wrong");
   735 #endif
   736   return;
   737 }
   739 // BlockFreelist methods
   741 BlockFreelist::BlockFreelist() : _dictionary(NULL) {}
   743 BlockFreelist::~BlockFreelist() {
   744   if (_dictionary != NULL) {
   745     if (Verbose && TraceMetadataChunkAllocation) {
   746       _dictionary->print_free_lists(gclog_or_tty);
   747     }
   748     delete _dictionary;
   749   }
   750 }
   752 Metablock* BlockFreelist::initialize_free_chunk(MetaWord* p, size_t word_size) {
   753   Metablock* block = (Metablock*) p;
   754   block->set_word_size(word_size);
   755   block->set_prev(NULL);
   756   block->set_next(NULL);
   758   return block;
   759 }
   761 void BlockFreelist::return_block(MetaWord* p, size_t word_size) {
   762   Metablock* free_chunk = initialize_free_chunk(p, word_size);
   763   if (dictionary() == NULL) {
   764    _dictionary = new BlockTreeDictionary();
   765   }
   766   dictionary()->return_chunk(free_chunk);
   767 }
   769 MetaWord* BlockFreelist::get_block(size_t word_size) {
   770   if (dictionary() == NULL) {
   771     return NULL;
   772   }
   774   if (word_size < TreeChunk<Metablock, FreeList>::min_size()) {
   775     // Dark matter.  Too small for dictionary.
   776     return NULL;
   777   }
   779   Metablock* free_block =
   780     dictionary()->get_chunk(word_size, FreeBlockDictionary<Metablock>::exactly);
   781   if (free_block == NULL) {
   782     return NULL;
   783   }
   785   return (MetaWord*) free_block;
   786 }
   788 void BlockFreelist::print_on(outputStream* st) const {
   789   if (dictionary() == NULL) {
   790     return;
   791   }
   792   dictionary()->print_free_lists(st);
   793 }
   795 // VirtualSpaceNode methods
   797 VirtualSpaceNode::~VirtualSpaceNode() {
   798   _rs.release();
   799 }
   801 size_t VirtualSpaceNode::used_words_in_vs() const {
   802   return pointer_delta(top(), bottom(), sizeof(MetaWord));
   803 }
   805 // Space committed in the VirtualSpace
   806 size_t VirtualSpaceNode::capacity_words_in_vs() const {
   807   return pointer_delta(end(), bottom(), sizeof(MetaWord));
   808 }
   811 // Allocates the chunk from the virtual space only.
   812 // This interface is also used internally for debugging.  Not all
   813 // chunks removed here are necessarily used for allocation.
   814 Metachunk* VirtualSpaceNode::take_from_committed(size_t chunk_word_size) {
   815   // Bottom of the new chunk
   816   MetaWord* chunk_limit = top();
   817   assert(chunk_limit != NULL, "Not safe to call this method");
   819   if (!is_available(chunk_word_size)) {
   820     if (TraceMetadataChunkAllocation) {
   821       tty->print("VirtualSpaceNode::take_from_committed() not available %d words ", chunk_word_size);
   822       // Dump some information about the virtual space that is nearly full
   823       print_on(tty);
   824     }
   825     return NULL;
   826   }
   828   // Take the space  (bump top on the current virtual space).
   829   inc_top(chunk_word_size);
   831   // Point the chunk at the space
   832   Metachunk* result = Metachunk::initialize(chunk_limit, chunk_word_size);
   833   return result;
   834 }
   837 // Expand the virtual space (commit more of the reserved space)
   838 bool VirtualSpaceNode::expand_by(size_t words, bool pre_touch) {
   839   size_t bytes = words * BytesPerWord;
   840   bool result =  virtual_space()->expand_by(bytes, pre_touch);
   841   if (TraceMetavirtualspaceAllocation && !result) {
   842     gclog_or_tty->print_cr("VirtualSpaceNode::expand_by() failed "
   843                            "for byte size " SIZE_FORMAT, bytes);
   844     virtual_space()->print();
   845   }
   846   return result;
   847 }
   849 // Shrink the virtual space (commit more of the reserved space)
   850 bool VirtualSpaceNode::shrink_by(size_t words) {
   851   size_t bytes = words * BytesPerWord;
   852   virtual_space()->shrink_by(bytes);
   853   return true;
   854 }
   856 // Add another chunk to the chunk list.
   858 Metachunk* VirtualSpaceNode::get_chunk_vs(size_t chunk_word_size) {
   859   assert_lock_strong(SpaceManager::expand_lock());
   860   Metachunk* result = NULL;
   862   return take_from_committed(chunk_word_size);
   863 }
   865 Metachunk* VirtualSpaceNode::get_chunk_vs_with_expand(size_t chunk_word_size) {
   866   assert_lock_strong(SpaceManager::expand_lock());
   868   Metachunk* new_chunk = get_chunk_vs(chunk_word_size);
   870   if (new_chunk == NULL) {
   871     // Only a small part of the virtualspace is committed when first
   872     // allocated so committing more here can be expected.
   873     size_t page_size_words = os::vm_page_size() / BytesPerWord;
   874     size_t aligned_expand_vs_by_words = align_size_up(chunk_word_size,
   875                                                     page_size_words);
   876     expand_by(aligned_expand_vs_by_words, false);
   877     new_chunk = get_chunk_vs(chunk_word_size);
   878   }
   879   return new_chunk;
   880 }
   882 bool VirtualSpaceNode::initialize() {
   884   if (!_rs.is_reserved()) {
   885     return false;
   886   }
   888   // Commit only 1 page instead of the whole reserved space _rs.size()
   889   size_t committed_byte_size = os::vm_page_size();
   890   bool result = virtual_space()->initialize(_rs, committed_byte_size);
   891   if (result) {
   892     set_top((MetaWord*)virtual_space()->low());
   893     set_reserved(MemRegion((HeapWord*)_rs.base(),
   894                  (HeapWord*)(_rs.base() + _rs.size())));
   896     assert(reserved()->start() == (HeapWord*) _rs.base(),
   897       err_msg("Reserved start was not set properly " PTR_FORMAT
   898         " != " PTR_FORMAT, reserved()->start(), _rs.base()));
   899     assert(reserved()->word_size() == _rs.size() / BytesPerWord,
   900       err_msg("Reserved size was not set properly " SIZE_FORMAT
   901         " != " SIZE_FORMAT, reserved()->word_size(),
   902         _rs.size() / BytesPerWord));
   903   }
   905   return result;
   906 }
   908 void VirtualSpaceNode::print_on(outputStream* st) const {
   909   size_t used = used_words_in_vs();
   910   size_t capacity = capacity_words_in_vs();
   911   VirtualSpace* vs = virtual_space();
   912   st->print_cr("   space @ " PTR_FORMAT " " SIZE_FORMAT "K, %3d%% used "
   913            "[" PTR_FORMAT ", " PTR_FORMAT ", "
   914            PTR_FORMAT ", " PTR_FORMAT ")",
   915            vs, capacity / K, used * 100 / capacity,
   916            bottom(), top(), end(),
   917            vs->high_boundary());
   918 }
   920 void VirtualSpaceNode::mangle() {
   921   size_t word_size = capacity_words_in_vs();
   922   Copy::fill_to_words((HeapWord*) low(), word_size, 0xf1f1f1f1);
   923 }
   925 // VirtualSpaceList methods
   926 // Space allocated from the VirtualSpace
   928 VirtualSpaceList::~VirtualSpaceList() {
   929   VirtualSpaceListIterator iter(virtual_space_list());
   930   while (iter.repeat()) {
   931     VirtualSpaceNode* vsl = iter.get_next();
   932     delete vsl;
   933   }
   934 }
   936 size_t VirtualSpaceList::used_words_sum() {
   937   size_t allocated_by_vs = 0;
   938   VirtualSpaceListIterator iter(virtual_space_list());
   939   while (iter.repeat()) {
   940     VirtualSpaceNode* vsl = iter.get_next();
   941     // Sum used region [bottom, top) in each virtualspace
   942     allocated_by_vs += vsl->used_words_in_vs();
   943   }
   944   assert(allocated_by_vs >= chunk_manager()->free_chunks_total(),
   945     err_msg("Total in free chunks " SIZE_FORMAT
   946             " greater than total from virtual_spaces " SIZE_FORMAT,
   947             allocated_by_vs, chunk_manager()->free_chunks_total()));
   948   size_t used =
   949     allocated_by_vs - chunk_manager()->free_chunks_total();
   950   return used;
   951 }
   953 // Space available in all MetadataVirtualspaces allocated
   954 // for metadata.  This is the upper limit on the capacity
   955 // of chunks allocated out of all the MetadataVirtualspaces.
   956 size_t VirtualSpaceList::capacity_words_sum() {
   957   size_t capacity = 0;
   958   VirtualSpaceListIterator iter(virtual_space_list());
   959   while (iter.repeat()) {
   960     VirtualSpaceNode* vsl = iter.get_next();
   961     capacity += vsl->capacity_words_in_vs();
   962   }
   963   return capacity;
   964 }
   966 VirtualSpaceList::VirtualSpaceList(size_t word_size ) :
   967                                    _is_class(false),
   968                                    _virtual_space_list(NULL),
   969                                    _current_virtual_space(NULL),
   970                                    _virtual_space_total(0),
   971                                    _virtual_space_count(0) {
   972   MutexLockerEx cl(SpaceManager::expand_lock(),
   973                    Mutex::_no_safepoint_check_flag);
   974   bool initialization_succeeded = grow_vs(word_size);
   976   assert(initialization_succeeded,
   977     " VirtualSpaceList initialization should not fail");
   978 }
   980 VirtualSpaceList::VirtualSpaceList(ReservedSpace rs) :
   981                                    _is_class(true),
   982                                    _virtual_space_list(NULL),
   983                                    _current_virtual_space(NULL),
   984                                    _virtual_space_total(0),
   985                                    _virtual_space_count(0) {
   986   MutexLockerEx cl(SpaceManager::expand_lock(),
   987                    Mutex::_no_safepoint_check_flag);
   988   VirtualSpaceNode* class_entry = new VirtualSpaceNode(rs);
   989   bool succeeded = class_entry->initialize();
   990   assert(succeeded, " VirtualSpaceList initialization should not fail");
   991   link_vs(class_entry, rs.size()/BytesPerWord);
   992 }
   994 // Allocate another meta virtual space and add it to the list.
   995 bool VirtualSpaceList::grow_vs(size_t vs_word_size) {
   996   assert_lock_strong(SpaceManager::expand_lock());
   997   if (vs_word_size == 0) {
   998     return false;
   999   }
  1000   // Reserve the space
  1001   size_t vs_byte_size = vs_word_size * BytesPerWord;
  1002   assert(vs_byte_size % os::vm_page_size() == 0, "Not aligned");
  1004   // Allocate the meta virtual space and initialize it.
  1005   VirtualSpaceNode* new_entry = new VirtualSpaceNode(vs_byte_size);
  1006   if (!new_entry->initialize()) {
  1007     delete new_entry;
  1008     return false;
  1009   } else {
  1010     link_vs(new_entry, vs_word_size);
  1011     return true;
  1015 void VirtualSpaceList::link_vs(VirtualSpaceNode* new_entry, size_t vs_word_size) {
  1016   if (virtual_space_list() == NULL) {
  1017       set_virtual_space_list(new_entry);
  1018   } else {
  1019     current_virtual_space()->set_next(new_entry);
  1021   set_current_virtual_space(new_entry);
  1022   inc_virtual_space_total(vs_word_size);
  1023   inc_virtual_space_count();
  1024 #ifdef ASSERT
  1025   new_entry->mangle();
  1026 #endif
  1027   if (TraceMetavirtualspaceAllocation && Verbose) {
  1028     VirtualSpaceNode* vsl = current_virtual_space();
  1029     vsl->print_on(tty);
  1033 Metachunk* VirtualSpaceList::get_new_chunk(size_t word_size,
  1034                                            size_t grow_chunks_by_words) {
  1036   // Get a chunk from the chunk freelist
  1037   Metachunk* next = chunk_manager()->chunk_freelist_allocate(grow_chunks_by_words);
  1039   // Allocate a chunk out of the current virtual space.
  1040   if (next == NULL) {
  1041     next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
  1044   if (next == NULL) {
  1045     // Not enough room in current virtual space.  Try to commit
  1046     // more space.
  1047     size_t expand_vs_by_words = MAX2((size_t)SpaceManager::MediumChunkBunch,
  1048                                        grow_chunks_by_words);
  1049     size_t page_size_words = os::vm_page_size() / BytesPerWord;
  1050     size_t aligned_expand_vs_by_words = align_size_up(expand_vs_by_words,
  1051                                                         page_size_words);
  1052     bool vs_expanded =
  1053       current_virtual_space()->expand_by(aligned_expand_vs_by_words, false);
  1054     if (!vs_expanded) {
  1055       // Should the capacity of the metaspaces be expanded for
  1056       // this allocation?  If it's the virtual space for classes and is
  1057       // being used for CompressedHeaders, don't allocate a new virtualspace.
  1058       if (can_grow() && MetaspaceGC::should_expand(this, word_size)) {
  1059         // Get another virtual space.
  1060           size_t grow_vs_words =
  1061             MAX2((size_t)VirtualSpaceSize, aligned_expand_vs_by_words);
  1062         if (grow_vs(grow_vs_words)) {
  1063           // Got it.  It's on the list now.  Get a chunk from it.
  1064           next = current_virtual_space()->get_chunk_vs_with_expand(grow_chunks_by_words);
  1066         if (TraceMetadataHumongousAllocation && SpaceManager::is_humongous(word_size)) {
  1067           gclog_or_tty->print_cr("  aligned_expand_vs_by_words " PTR_FORMAT,
  1068                                  aligned_expand_vs_by_words);
  1069           gclog_or_tty->print_cr("  grow_vs_words " PTR_FORMAT,
  1070                                  grow_vs_words);
  1072       } else {
  1073         // Allocation will fail and induce a GC
  1074         if (TraceMetadataChunkAllocation && Verbose) {
  1075           gclog_or_tty->print_cr("VirtualSpaceList::get_new_chunk():"
  1076             " Fail instead of expand the metaspace");
  1079     } else {
  1080       // The virtual space expanded, get a new chunk
  1081       next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
  1082       assert(next != NULL, "Just expanded, should succeed");
  1086   return next;
  1089 void VirtualSpaceList::print_on(outputStream* st) const {
  1090   if (TraceMetadataChunkAllocation && Verbose) {
  1091     VirtualSpaceListIterator iter(virtual_space_list());
  1092     while (iter.repeat()) {
  1093       VirtualSpaceNode* node = iter.get_next();
  1094       node->print_on(st);
  1099 #ifndef PRODUCT
  1100 bool VirtualSpaceList::contains(const void *ptr) {
  1101   VirtualSpaceNode* list = virtual_space_list();
  1102   VirtualSpaceListIterator iter(list);
  1103   while (iter.repeat()) {
  1104     VirtualSpaceNode* node = iter.get_next();
  1105     if (node->reserved()->contains(ptr)) {
  1106       return true;
  1109   return false;
  1111 #endif // PRODUCT
  1114 // MetaspaceGC methods
  1116 // VM_CollectForMetadataAllocation is the vm operation used to GC.
  1117 // Within the VM operation after the GC the attempt to allocate the metadata
  1118 // should succeed.  If the GC did not free enough space for the metaspace
  1119 // allocation, the HWM is increased so that another virtualspace will be
  1120 // allocated for the metadata.  With perm gen the increase in the perm
  1121 // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion.  The
  1122 // metaspace policy uses those as the small and large steps for the HWM.
  1123 //
  1124 // After the GC the compute_new_size() for MetaspaceGC is called to
  1125 // resize the capacity of the metaspaces.  The current implementation
  1126 // is based on the flags MinHeapFreeRatio and MaxHeapFreeRatio used
  1127 // to resize the Java heap by some GC's.  New flags can be implemented
  1128 // if really needed.  MinHeapFreeRatio is used to calculate how much
  1129 // free space is desirable in the metaspace capacity to decide how much
  1130 // to increase the HWM.  MaxHeapFreeRatio is used to decide how much
  1131 // free space is desirable in the metaspace capacity before decreasing
  1132 // the HWM.
  1134 // Calculate the amount to increase the high water mark (HWM).
  1135 // Increase by a minimum amount (MinMetaspaceExpansion) so that
  1136 // another expansion is not requested too soon.  If that is not
  1137 // enough to satisfy the allocation (i.e. big enough for a word_size
  1138 // allocation), increase by MaxMetaspaceExpansion.  If that is still
  1139 // not enough, expand by the size of the allocation (word_size) plus
  1140 // some.
  1141 size_t MetaspaceGC::delta_capacity_until_GC(size_t word_size) {
  1142   size_t before_inc = MetaspaceGC::capacity_until_GC();
  1143   size_t min_delta_words = MinMetaspaceExpansion / BytesPerWord;
  1144   size_t max_delta_words = MaxMetaspaceExpansion / BytesPerWord;
  1145   size_t page_size_words = os::vm_page_size() / BytesPerWord;
  1146   size_t size_delta_words = align_size_up(word_size, page_size_words);
  1147   size_t delta_words = MAX2(size_delta_words, min_delta_words);
  1148   if (delta_words > min_delta_words) {
  1149     // Don't want to hit the high water mark on the next
  1150     // allocation so make the delta greater than just enough
  1151     // for this allocation.
  1152     delta_words = MAX2(delta_words, max_delta_words);
  1153     if (delta_words > max_delta_words) {
  1154       // This allocation is large but the next ones are probably not
  1155       // so increase by the minimum.
  1156       delta_words = delta_words + min_delta_words;
  1159   return delta_words;
  1162 bool MetaspaceGC::should_expand(VirtualSpaceList* vsl, size_t word_size) {
  1164   // Class virtual space should always be expanded.  Call GC for the other
  1165   // metadata virtual space.
  1166   if (vsl == Metaspace::class_space_list()) return true;
  1168   // If the user wants a limit, impose one.
  1169   size_t max_metaspace_size_words = MaxMetaspaceSize / BytesPerWord;
  1170   size_t metaspace_size_words = MetaspaceSize / BytesPerWord;
  1171   if (!FLAG_IS_DEFAULT(MaxMetaspaceSize) &&
  1172       vsl->capacity_words_sum() >= max_metaspace_size_words) {
  1173     return false;
  1176   // If this is part of an allocation after a GC, expand
  1177   // unconditionally.
  1178   if(MetaspaceGC::expand_after_GC()) {
  1179     return true;
  1182   // If the capacity is below the minimum capacity, allow the
  1183   // expansion.  Also set the high-water-mark (capacity_until_GC)
  1184   // to that minimum capacity so that a GC will not be induced
  1185   // until that minimum capacity is exceeded.
  1186   if (vsl->capacity_words_sum() < metaspace_size_words ||
  1187       capacity_until_GC() == 0) {
  1188     set_capacity_until_GC(metaspace_size_words);
  1189     return true;
  1190   } else {
  1191     if (vsl->capacity_words_sum() < capacity_until_GC()) {
  1192       return true;
  1193     } else {
  1194       if (TraceMetadataChunkAllocation && Verbose) {
  1195         gclog_or_tty->print_cr("  allocation request size " SIZE_FORMAT
  1196                         "  capacity_until_GC " SIZE_FORMAT
  1197                         "  capacity_words_sum " SIZE_FORMAT
  1198                         "  used_words_sum " SIZE_FORMAT
  1199                         "  free chunks " SIZE_FORMAT
  1200                         "  free chunks count %d",
  1201                         word_size,
  1202                         capacity_until_GC(),
  1203                         vsl->capacity_words_sum(),
  1204                         vsl->used_words_sum(),
  1205                         vsl->chunk_manager()->free_chunks_total(),
  1206                         vsl->chunk_manager()->free_chunks_count());
  1208       return false;
  1213 // Variables are in bytes
  1215 void MetaspaceGC::compute_new_size() {
  1216   assert(_shrink_factor <= 100, "invalid shrink factor");
  1217   uint current_shrink_factor = _shrink_factor;
  1218   _shrink_factor = 0;
  1220   VirtualSpaceList *vsl = Metaspace::space_list();
  1222   size_t capacity_after_gc = vsl->capacity_bytes_sum();
  1223   // Check to see if these two can be calculated without walking the CLDG
  1224   size_t used_after_gc = vsl->used_bytes_sum();
  1225   size_t capacity_until_GC = vsl->capacity_bytes_sum();
  1226   size_t free_after_gc = capacity_until_GC - used_after_gc;
  1228   const double minimum_free_percentage = MinHeapFreeRatio / 100.0;
  1229   const double maximum_used_percentage = 1.0 - minimum_free_percentage;
  1231   const double min_tmp = used_after_gc / maximum_used_percentage;
  1232   size_t minimum_desired_capacity =
  1233     (size_t)MIN2(min_tmp, double(max_uintx));
  1234   // Don't shrink less than the initial generation size
  1235   minimum_desired_capacity = MAX2(minimum_desired_capacity,
  1236                                   MetaspaceSize);
  1238   if (PrintGCDetails && Verbose) {
  1239     const double free_percentage = ((double)free_after_gc) / capacity_until_GC;
  1240     gclog_or_tty->print_cr("\nMetaspaceGC::compute_new_size: ");
  1241     gclog_or_tty->print_cr("  "
  1242                   "  minimum_free_percentage: %6.2f"
  1243                   "  maximum_used_percentage: %6.2f",
  1244                   minimum_free_percentage,
  1245                   maximum_used_percentage);
  1246     double d_free_after_gc = free_after_gc / (double) K;
  1247     gclog_or_tty->print_cr("  "
  1248                   "   free_after_gc       : %6.1fK"
  1249                   "   used_after_gc       : %6.1fK"
  1250                   "   capacity_after_gc   : %6.1fK"
  1251                   "   metaspace HWM     : %6.1fK",
  1252                   free_after_gc / (double) K,
  1253                   used_after_gc / (double) K,
  1254                   capacity_after_gc / (double) K,
  1255                   capacity_until_GC / (double) K);
  1256     gclog_or_tty->print_cr("  "
  1257                   "   free_percentage: %6.2f",
  1258                   free_percentage);
  1262   if (capacity_until_GC < minimum_desired_capacity) {
  1263     // If we have less capacity below the metaspace HWM, then
  1264     // increment the HWM.
  1265     size_t expand_bytes = minimum_desired_capacity - capacity_until_GC;
  1266     // Don't expand unless it's significant
  1267     if (expand_bytes >= MinMetaspaceExpansion) {
  1268       size_t expand_words = expand_bytes / BytesPerWord;
  1269       MetaspaceGC::inc_capacity_until_GC(expand_words);
  1271     if (PrintGCDetails && Verbose) {
  1272       size_t new_capacity_until_GC = MetaspaceGC::capacity_until_GC_in_bytes();
  1273       gclog_or_tty->print_cr("    expanding:"
  1274                     "  minimum_desired_capacity: %6.1fK"
  1275                     "  expand_words: %6.1fK"
  1276                     "  MinMetaspaceExpansion: %6.1fK"
  1277                     "  new metaspace HWM:  %6.1fK",
  1278                     minimum_desired_capacity / (double) K,
  1279                     expand_bytes / (double) K,
  1280                     MinMetaspaceExpansion / (double) K,
  1281                     new_capacity_until_GC / (double) K);
  1283     return;
  1286   // No expansion, now see if we want to shrink
  1287   size_t shrink_words = 0;
  1288   // We would never want to shrink more than this
  1289   size_t max_shrink_words = capacity_until_GC - minimum_desired_capacity;
  1290   assert(max_shrink_words >= 0, err_msg("max_shrink_words " SIZE_FORMAT,
  1291     max_shrink_words));
  1293   // Should shrinking be considered?
  1294   if (MaxHeapFreeRatio < 100) {
  1295     const double maximum_free_percentage = MaxHeapFreeRatio / 100.0;
  1296     const double minimum_used_percentage = 1.0 - maximum_free_percentage;
  1297     const double max_tmp = used_after_gc / minimum_used_percentage;
  1298     size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx));
  1299     maximum_desired_capacity = MAX2(maximum_desired_capacity,
  1300                                     MetaspaceSize);
  1301     if (PrintGC && Verbose) {
  1302       gclog_or_tty->print_cr("  "
  1303                              "  maximum_free_percentage: %6.2f"
  1304                              "  minimum_used_percentage: %6.2f",
  1305                              maximum_free_percentage,
  1306                              minimum_used_percentage);
  1307       gclog_or_tty->print_cr("  "
  1308                              "  capacity_until_GC: %6.1fK"
  1309                              "  minimum_desired_capacity: %6.1fK"
  1310                              "  maximum_desired_capacity: %6.1fK",
  1311                              capacity_until_GC / (double) K,
  1312                              minimum_desired_capacity / (double) K,
  1313                              maximum_desired_capacity / (double) K);
  1316     assert(minimum_desired_capacity <= maximum_desired_capacity,
  1317            "sanity check");
  1319     if (capacity_until_GC > maximum_desired_capacity) {
  1320       // Capacity too large, compute shrinking size
  1321       shrink_words = capacity_until_GC - maximum_desired_capacity;
  1322       // We don't want shrink all the way back to initSize if people call
  1323       // System.gc(), because some programs do that between "phases" and then
  1324       // we'd just have to grow the heap up again for the next phase.  So we
  1325       // damp the shrinking: 0% on the first call, 10% on the second call, 40%
  1326       // on the third call, and 100% by the fourth call.  But if we recompute
  1327       // size without shrinking, it goes back to 0%.
  1328       shrink_words = shrink_words / 100 * current_shrink_factor;
  1329       assert(shrink_words <= max_shrink_words,
  1330         err_msg("invalid shrink size " SIZE_FORMAT " not <= " SIZE_FORMAT,
  1331           shrink_words, max_shrink_words));
  1332       if (current_shrink_factor == 0) {
  1333         _shrink_factor = 10;
  1334       } else {
  1335         _shrink_factor = MIN2(current_shrink_factor * 4, (uint) 100);
  1337       if (PrintGCDetails && Verbose) {
  1338         gclog_or_tty->print_cr("  "
  1339                       "  shrinking:"
  1340                       "  initSize: %.1fK"
  1341                       "  maximum_desired_capacity: %.1fK",
  1342                       MetaspaceSize / (double) K,
  1343                       maximum_desired_capacity / (double) K);
  1344         gclog_or_tty->print_cr("  "
  1345                       "  shrink_words: %.1fK"
  1346                       "  current_shrink_factor: %d"
  1347                       "  new shrink factor: %d"
  1348                       "  MinMetaspaceExpansion: %.1fK",
  1349                       shrink_words / (double) K,
  1350                       current_shrink_factor,
  1351                       _shrink_factor,
  1352                       MinMetaspaceExpansion / (double) K);
  1358   // Don't shrink unless it's significant
  1359   if (shrink_words >= MinMetaspaceExpansion) {
  1360     VirtualSpaceNode* csp = vsl->current_virtual_space();
  1361     size_t available_to_shrink = csp->capacity_words_in_vs() -
  1362       csp->used_words_in_vs();
  1363     shrink_words = MIN2(shrink_words, available_to_shrink);
  1364     csp->shrink_by(shrink_words);
  1365     MetaspaceGC::dec_capacity_until_GC(shrink_words);
  1366     if (PrintGCDetails && Verbose) {
  1367       size_t new_capacity_until_GC = MetaspaceGC::capacity_until_GC_in_bytes();
  1368       gclog_or_tty->print_cr("  metaspace HWM: %.1fK", new_capacity_until_GC / (double) K);
  1371   assert(vsl->used_bytes_sum() == used_after_gc &&
  1372          used_after_gc <= vsl->capacity_bytes_sum(),
  1373          "sanity check");
  1377 // Metadebug methods
  1379 void Metadebug::deallocate_chunk_a_lot(SpaceManager* sm,
  1380                                        size_t chunk_word_size){
  1381 #ifdef ASSERT
  1382   VirtualSpaceList* vsl = sm->vs_list();
  1383   if (MetaDataDeallocateALot &&
  1384       Metadebug::deallocate_chunk_a_lot_count() % MetaDataDeallocateALotInterval == 0 ) {
  1385     Metadebug::reset_deallocate_chunk_a_lot_count();
  1386     for (uint i = 0; i < metadata_deallocate_a_lock_chunk; i++) {
  1387       Metachunk* dummy_chunk = vsl->current_virtual_space()->take_from_committed(chunk_word_size);
  1388       if (dummy_chunk == NULL) {
  1389         break;
  1391       vsl->chunk_manager()->chunk_freelist_deallocate(dummy_chunk);
  1393       if (TraceMetadataChunkAllocation && Verbose) {
  1394         gclog_or_tty->print("Metadebug::deallocate_chunk_a_lot: %d) ",
  1395                                sm->sum_count_in_chunks_in_use());
  1396         dummy_chunk->print_on(gclog_or_tty);
  1397         gclog_or_tty->print_cr("  Free chunks total %d  count %d",
  1398                                vsl->chunk_manager()->free_chunks_total(),
  1399                                vsl->chunk_manager()->free_chunks_count());
  1402   } else {
  1403     Metadebug::inc_deallocate_chunk_a_lot_count();
  1405 #endif
  1408 void Metadebug::deallocate_block_a_lot(SpaceManager* sm,
  1409                                        size_t raw_word_size){
  1410 #ifdef ASSERT
  1411   if (MetaDataDeallocateALot &&
  1412         Metadebug::deallocate_block_a_lot_count() % MetaDataDeallocateALotInterval == 0 ) {
  1413     Metadebug::set_deallocate_block_a_lot_count(0);
  1414     for (uint i = 0; i < metadata_deallocate_a_lot_block; i++) {
  1415       MetaWord* dummy_block = sm->allocate_work(raw_word_size);
  1416       if (dummy_block == 0) {
  1417         break;
  1419       sm->deallocate(dummy_block, raw_word_size);
  1421   } else {
  1422     Metadebug::inc_deallocate_block_a_lot_count();
  1424 #endif
  1427 void Metadebug::init_allocation_fail_alot_count() {
  1428   if (MetadataAllocationFailALot) {
  1429     _allocation_fail_alot_count =
  1430       1+(long)((double)MetadataAllocationFailALotInterval*os::random()/(max_jint+1.0));
  1434 #ifdef ASSERT
  1435 bool Metadebug::test_metadata_failure() {
  1436   if (MetadataAllocationFailALot &&
  1437       Threads::is_vm_complete()) {
  1438     if (_allocation_fail_alot_count > 0) {
  1439       _allocation_fail_alot_count--;
  1440     } else {
  1441       if (TraceMetadataChunkAllocation && Verbose) {
  1442         gclog_or_tty->print_cr("Metadata allocation failing for "
  1443                                "MetadataAllocationFailALot");
  1445       init_allocation_fail_alot_count();
  1446       return true;
  1449   return false;
  1451 #endif
  1453 // ChunkList methods
  1455 size_t ChunkList::sum_list_size() {
  1456   size_t result = 0;
  1457   Metachunk* cur = head();
  1458   while (cur != NULL) {
  1459     result += cur->word_size();
  1460     cur = cur->next();
  1462   return result;
  1465 size_t ChunkList::sum_list_count() {
  1466   size_t result = 0;
  1467   Metachunk* cur = head();
  1468   while (cur != NULL) {
  1469     result++;
  1470     cur = cur->next();
  1472   return result;
  1475 size_t ChunkList::sum_list_capacity() {
  1476   size_t result = 0;
  1477   Metachunk* cur = head();
  1478   while (cur != NULL) {
  1479     result += cur->capacity_word_size();
  1480     cur = cur->next();
  1482   return result;
  1485 void ChunkList::add_at_head(Metachunk* head, Metachunk* tail) {
  1486   assert_lock_strong(SpaceManager::expand_lock());
  1487   assert(tail->next() == NULL, "Not the tail");
  1489   if (TraceMetadataChunkAllocation && Verbose) {
  1490     tty->print("ChunkList::add_at_head: ");
  1491     Metachunk* cur = head;
  1492     while (cur != NULL) {
  1493     tty->print(PTR_FORMAT " (" SIZE_FORMAT ") ", cur, cur->word_size());
  1494       cur = cur->next();
  1496     tty->print_cr("");
  1499   if (tail != NULL) {
  1500     tail->set_next(_head);
  1502   set_head(head);
  1505 void ChunkList::add_at_head(Metachunk* list) {
  1506   if (list == NULL) {
  1507     // Nothing to add
  1508     return;
  1510   assert_lock_strong(SpaceManager::expand_lock());
  1511   Metachunk* head = list;
  1512   Metachunk* tail = list;
  1513   Metachunk* cur = head->next();
  1514   // Search for the tail since it is not passed.
  1515   while (cur != NULL) {
  1516     tail = cur;
  1517     cur = cur->next();
  1519   add_at_head(head, tail);
  1522 // ChunkManager methods
  1524 // Verification of _free_chunks_total and _free_chunks_count does not
  1525 // work with the CMS collector because its use of additional locks
  1526 // complicate the mutex deadlock detection but it can still be useful
  1527 // for detecting errors in the chunk accounting with other collectors.
  1529 size_t ChunkManager::free_chunks_total() {
  1530 #ifdef ASSERT
  1531   if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) {
  1532     MutexLockerEx cl(SpaceManager::expand_lock(),
  1533                      Mutex::_no_safepoint_check_flag);
  1534     slow_locked_verify_free_chunks_total();
  1536 #endif
  1537   return _free_chunks_total;
  1540 size_t ChunkManager::free_chunks_total_in_bytes() {
  1541   return free_chunks_total() * BytesPerWord;
  1544 size_t ChunkManager::free_chunks_count() {
  1545 #ifdef ASSERT
  1546   if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) {
  1547     MutexLockerEx cl(SpaceManager::expand_lock(),
  1548                      Mutex::_no_safepoint_check_flag);
  1549     // This lock is only needed in debug because the verification
  1550     // of the _free_chunks_totals walks the list of free chunks
  1551     slow_locked_verify_free_chunks_count();
  1553 #endif
  1554   return _free_chunks_count;
  1557 void ChunkManager::locked_verify_free_chunks_total() {
  1558   assert_lock_strong(SpaceManager::expand_lock());
  1559   assert(sum_free_chunks() == _free_chunks_total,
  1560     err_msg("_free_chunks_total " SIZE_FORMAT " is not the"
  1561            " same as sum " SIZE_FORMAT, _free_chunks_total,
  1562            sum_free_chunks()));
  1565 void ChunkManager::verify_free_chunks_total() {
  1566   MutexLockerEx cl(SpaceManager::expand_lock(),
  1567                      Mutex::_no_safepoint_check_flag);
  1568   locked_verify_free_chunks_total();
  1571 void ChunkManager::locked_verify_free_chunks_count() {
  1572   assert_lock_strong(SpaceManager::expand_lock());
  1573   assert(sum_free_chunks_count() == _free_chunks_count,
  1574     err_msg("_free_chunks_count " SIZE_FORMAT " is not the"
  1575            " same as sum " SIZE_FORMAT, _free_chunks_count,
  1576            sum_free_chunks_count()));
  1579 void ChunkManager::verify_free_chunks_count() {
  1580 #ifdef ASSERT
  1581   MutexLockerEx cl(SpaceManager::expand_lock(),
  1582                      Mutex::_no_safepoint_check_flag);
  1583   locked_verify_free_chunks_count();
  1584 #endif
  1587 void ChunkManager::verify() {
  1588   MutexLockerEx cl(SpaceManager::expand_lock(),
  1589                      Mutex::_no_safepoint_check_flag);
  1590   locked_verify();
  1593 void ChunkManager::locked_verify() {
  1594   locked_verify_free_chunks_count();
  1595   locked_verify_free_chunks_total();
  1598 void ChunkManager::locked_print_free_chunks(outputStream* st) {
  1599   assert_lock_strong(SpaceManager::expand_lock());
  1600   st->print_cr("Free chunk total 0x%x  count 0x%x",
  1601                 _free_chunks_total, _free_chunks_count);
  1604 void ChunkManager::locked_print_sum_free_chunks(outputStream* st) {
  1605   assert_lock_strong(SpaceManager::expand_lock());
  1606   st->print_cr("Sum free chunk total 0x%x  count 0x%x",
  1607                 sum_free_chunks(), sum_free_chunks_count());
  1609 ChunkList* ChunkManager::free_chunks(ChunkIndex index) {
  1610   return &_free_chunks[index];
  1613 // These methods that sum the free chunk lists are used in printing
  1614 // methods that are used in product builds.
  1615 size_t ChunkManager::sum_free_chunks() {
  1616   assert_lock_strong(SpaceManager::expand_lock());
  1617   size_t result = 0;
  1618   for (ChunkIndex i = SmallIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
  1619     ChunkList* list = free_chunks(i);
  1621     if (list == NULL) {
  1622       continue;
  1625     result = result + list->sum_list_capacity();
  1627   result = result + humongous_dictionary()->total_size();
  1628   return result;
  1631 size_t ChunkManager::sum_free_chunks_count() {
  1632   assert_lock_strong(SpaceManager::expand_lock());
  1633   size_t count = 0;
  1634   for (ChunkIndex i = SmallIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
  1635     ChunkList* list = free_chunks(i);
  1636     if (list == NULL) {
  1637       continue;
  1639     count = count + list->sum_list_count();
  1641   count = count + humongous_dictionary()->total_free_blocks();
  1642   return count;
  1645 ChunkList* ChunkManager::find_free_chunks_list(size_t word_size) {
  1646   switch (word_size) {
  1647   case SpaceManager::SmallChunk :
  1648       return &_free_chunks[0];
  1649   case SpaceManager::MediumChunk :
  1650       return &_free_chunks[1];
  1651   default:
  1652     assert(word_size > SpaceManager::MediumChunk, "List inconsistency");
  1653     return &_free_chunks[2];
  1657 void ChunkManager::free_chunks_put(Metachunk* chunk) {
  1658   assert_lock_strong(SpaceManager::expand_lock());
  1659   ChunkList* free_list = find_free_chunks_list(chunk->word_size());
  1660   chunk->set_next(free_list->head());
  1661   free_list->set_head(chunk);
  1662   // chunk is being returned to the chunk free list
  1663   inc_free_chunks_total(chunk->capacity_word_size());
  1664   slow_locked_verify();
  1667 void ChunkManager::chunk_freelist_deallocate(Metachunk* chunk) {
  1668   // The deallocation of a chunk originates in the freelist
  1669   // manangement code for a Metaspace and does not hold the
  1670   // lock.
  1671   assert(chunk != NULL, "Deallocating NULL");
  1672   assert_lock_strong(SpaceManager::expand_lock());
  1673   slow_locked_verify();
  1674   if (TraceMetadataChunkAllocation) {
  1675     tty->print_cr("ChunkManager::chunk_freelist_deallocate: chunk "
  1676                   PTR_FORMAT "  size " SIZE_FORMAT,
  1677                   chunk, chunk->word_size());
  1679   free_chunks_put(chunk);
  1682 Metachunk* ChunkManager::free_chunks_get(size_t word_size) {
  1683   assert_lock_strong(SpaceManager::expand_lock());
  1685   slow_locked_verify();
  1687   Metachunk* chunk = NULL;
  1688   if (!SpaceManager::is_humongous(word_size)) {
  1689     ChunkList* free_list = find_free_chunks_list(word_size);
  1690     assert(free_list != NULL, "Sanity check");
  1692     chunk = free_list->head();
  1693     debug_only(Metachunk* debug_head = chunk;)
  1695     if (chunk == NULL) {
  1696       return NULL;
  1699     // Remove the chunk as the head of the list.
  1700     free_list->set_head(chunk->next());
  1701     chunk->set_next(NULL);
  1702     // Chunk has been removed from the chunks free list.
  1703     dec_free_chunks_total(chunk->capacity_word_size());
  1705     if (TraceMetadataChunkAllocation && Verbose) {
  1706       tty->print_cr("ChunkManager::free_chunks_get: free_list "
  1707                     PTR_FORMAT " head " PTR_FORMAT " size " SIZE_FORMAT,
  1708                     free_list, chunk, chunk->word_size());
  1710   } else {
  1711     chunk = humongous_dictionary()->get_chunk(
  1712       word_size,
  1713       FreeBlockDictionary<Metachunk>::atLeast);
  1715     if (chunk != NULL) {
  1716       if (TraceMetadataHumongousAllocation) {
  1717         size_t waste = chunk->word_size() - word_size;
  1718         tty->print_cr("Free list allocate humongous chunk size " SIZE_FORMAT
  1719                       " for requested size " SIZE_FORMAT
  1720                       " waste " SIZE_FORMAT,
  1721                       chunk->word_size(), word_size, waste);
  1723       // Chunk is being removed from the chunks free list.
  1724       dec_free_chunks_total(chunk->capacity_word_size());
  1725 #ifdef ASSERT
  1726       chunk->set_is_free(false);
  1727 #endif
  1730   slow_locked_verify();
  1731   return chunk;
  1734 Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) {
  1735   assert_lock_strong(SpaceManager::expand_lock());
  1736   slow_locked_verify();
  1738   // Take from the beginning of the list
  1739   Metachunk* chunk = free_chunks_get(word_size);
  1740   if (chunk == NULL) {
  1741     return NULL;
  1744   assert(word_size <= chunk->word_size() ||
  1745            SpaceManager::is_humongous(chunk->word_size()),
  1746            "Non-humongous variable sized chunk");
  1747   if (TraceMetadataChunkAllocation) {
  1748     tty->print("ChunkManager::chunk_freelist_allocate: chunk "
  1749                PTR_FORMAT "  size " SIZE_FORMAT " ",
  1750                chunk, chunk->word_size());
  1751     locked_print_free_chunks(tty);
  1754   return chunk;
  1757 void ChunkManager::print_on(outputStream* out) {
  1758   if (PrintFLSStatistics != 0) {
  1759     humongous_dictionary()->report_statistics();
  1763 // SpaceManager methods
  1765 size_t SpaceManager::sum_free_in_chunks_in_use() const {
  1766   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  1767   size_t free = 0;
  1768   for (ChunkIndex i = SmallIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1769     Metachunk* chunk = chunks_in_use(i);
  1770     while (chunk != NULL) {
  1771       free += chunk->free_word_size();
  1772       chunk = chunk->next();
  1775   return free;
  1778 size_t SpaceManager::sum_waste_in_chunks_in_use() const {
  1779   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  1780   size_t result = 0;
  1781   for (ChunkIndex i = SmallIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1784    result += sum_waste_in_chunks_in_use(i);
  1787   return result;
  1790 size_t SpaceManager::sum_waste_in_chunks_in_use(ChunkIndex index) const {
  1791   size_t result = 0;
  1792   size_t count = 0;
  1793   Metachunk* chunk = chunks_in_use(index);
  1794   // Count the free space in all the chunk but not the
  1795   // current chunk from which allocations are still being done.
  1796   if (chunk != NULL) {
  1797     Metachunk* prev = chunk;
  1798     while (chunk != NULL && chunk != current_chunk()) {
  1799       result += chunk->free_word_size();
  1800       prev = chunk;
  1801       chunk = chunk->next();
  1802       count++;
  1805   return result;
  1808 size_t SpaceManager::sum_capacity_in_chunks_in_use() const {
  1809   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  1810   size_t sum = 0;
  1811   for (ChunkIndex i = SmallIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1812     Metachunk* chunk = chunks_in_use(i);
  1813     while (chunk != NULL) {
  1814       // Just changed this sum += chunk->capacity_word_size();
  1815       // sum += chunk->word_size() - Metachunk::overhead();
  1816       sum += chunk->capacity_word_size();
  1817       chunk = chunk->next();
  1820   return sum;
  1823 size_t SpaceManager::sum_count_in_chunks_in_use() {
  1824   size_t count = 0;
  1825   for (ChunkIndex i = SmallIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1826     count = count + sum_count_in_chunks_in_use(i);
  1829   return count;
  1832 size_t SpaceManager::sum_count_in_chunks_in_use(ChunkIndex i) {
  1833   size_t count = 0;
  1834   Metachunk* chunk = chunks_in_use(i);
  1835   while (chunk != NULL) {
  1836     count++;
  1837     chunk = chunk->next();
  1839   return count;
  1843 size_t SpaceManager::sum_used_in_chunks_in_use() const {
  1844   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  1845   size_t used = 0;
  1846   for (ChunkIndex i = SmallIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1847     Metachunk* chunk = chunks_in_use(i);
  1848     while (chunk != NULL) {
  1849       used += chunk->used_word_size();
  1850       chunk = chunk->next();
  1853   return used;
  1856 void SpaceManager::locked_print_chunks_in_use_on(outputStream* st) const {
  1858   Metachunk* small_chunk = chunks_in_use(SmallIndex);
  1859   st->print_cr("SpaceManager: small chunk " PTR_FORMAT
  1860                " free " SIZE_FORMAT,
  1861                small_chunk,
  1862                small_chunk->free_word_size());
  1864   Metachunk* medium_chunk = chunks_in_use(MediumIndex);
  1865   st->print("medium chunk " PTR_FORMAT, medium_chunk);
  1866   Metachunk* tail = current_chunk();
  1867   st->print_cr(" current chunk " PTR_FORMAT, tail);
  1869   Metachunk* head = chunks_in_use(HumongousIndex);
  1870   st->print_cr("humongous chunk " PTR_FORMAT, head);
  1872   vs_list()->chunk_manager()->locked_print_free_chunks(st);
  1873   vs_list()->chunk_manager()->locked_print_sum_free_chunks(st);
  1876 size_t SpaceManager::calc_chunk_size(size_t word_size) {
  1878   // Decide between a small chunk and a medium chunk.  Up to
  1879   // _small_chunk_limit small chunks can be allocated but
  1880   // once a medium chunk has been allocated, no more small
  1881   // chunks will be allocated.
  1882   size_t chunk_word_size;
  1883   if (chunks_in_use(MediumIndex) == NULL &&
  1884       (!has_small_chunk_limit() ||
  1885        sum_count_in_chunks_in_use(SmallIndex) < _small_chunk_limit)) {
  1886     chunk_word_size = (size_t) SpaceManager::SmallChunk;
  1887     if (word_size + Metachunk::overhead() > SpaceManager::SmallChunk) {
  1888       chunk_word_size = MediumChunk;
  1890   } else {
  1891     chunk_word_size = MediumChunk;
  1894   // Might still need a humongous chunk
  1895   chunk_word_size =
  1896     MAX2((size_t) chunk_word_size, word_size + Metachunk::overhead());
  1898   if (TraceMetadataHumongousAllocation &&
  1899       SpaceManager::is_humongous(word_size)) {
  1900     gclog_or_tty->print_cr("Metadata humongous allocation:");
  1901     gclog_or_tty->print_cr("  word_size " PTR_FORMAT, word_size);
  1902     gclog_or_tty->print_cr("  chunk_word_size " PTR_FORMAT,
  1903                            chunk_word_size);
  1904     gclog_or_tty->print_cr("    chunk overhead " PTR_FORMAT,
  1905                            Metachunk::overhead());
  1907   return chunk_word_size;
  1910 MetaWord* SpaceManager::grow_and_allocate(size_t word_size) {
  1911   assert(vs_list()->current_virtual_space() != NULL,
  1912          "Should have been set");
  1913   assert(current_chunk() == NULL ||
  1914          current_chunk()->allocate(word_size) == NULL,
  1915          "Don't need to expand");
  1916   MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
  1918   if (TraceMetadataChunkAllocation && Verbose) {
  1919     gclog_or_tty->print_cr("SpaceManager::grow_and_allocate for " SIZE_FORMAT
  1920                            " words " SIZE_FORMAT " space left",
  1921                             word_size, current_chunk() != NULL ?
  1922                               current_chunk()->free_word_size() : 0);
  1925   // Get another chunk out of the virtual space
  1926   size_t grow_chunks_by_words = calc_chunk_size(word_size);
  1927   Metachunk* next = vs_list()->get_new_chunk(word_size, grow_chunks_by_words);
  1929   // If a chunk was available, add it to the in-use chunk list
  1930   // and do an allocation from it.
  1931   if (next != NULL) {
  1932     Metadebug::deallocate_chunk_a_lot(this, grow_chunks_by_words);
  1933     // Add to this manager's list of chunks in use.
  1934     add_chunk(next, false);
  1935     return next->allocate(word_size);
  1937   return NULL;
  1940 void SpaceManager::print_on(outputStream* st) const {
  1942   for (ChunkIndex i = SmallIndex;
  1943        i < NumberOfInUseLists ;
  1944        i = next_chunk_index(i) ) {
  1945     st->print_cr("  chunks_in_use " PTR_FORMAT " chunk size " PTR_FORMAT,
  1946                  chunks_in_use(i),
  1947                  chunks_in_use(i) == NULL ? 0 : chunks_in_use(i)->word_size());
  1949   st->print_cr("    waste:  Small " SIZE_FORMAT " Medium " SIZE_FORMAT
  1950                " Humongous " SIZE_FORMAT,
  1951                sum_waste_in_chunks_in_use(SmallIndex),
  1952                sum_waste_in_chunks_in_use(MediumIndex),
  1953                sum_waste_in_chunks_in_use(HumongousIndex));
  1954   // block free lists
  1955   if (block_freelists() != NULL) {
  1956     st->print_cr("total in block free lists " SIZE_FORMAT,
  1957       block_freelists()->total_size());
  1961 SpaceManager::SpaceManager(Mutex* lock, VirtualSpaceList* vs_list) :
  1962   _vs_list(vs_list),
  1963   _allocation_total(0),
  1964   _lock(lock) {
  1965   Metadebug::init_allocation_fail_alot_count();
  1966   for (ChunkIndex i = SmallIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1967     _chunks_in_use[i] = NULL;
  1969   _current_chunk = NULL;
  1970   if (TraceMetadataChunkAllocation && Verbose) {
  1971     gclog_or_tty->print_cr("SpaceManager(): " PTR_FORMAT, this);
  1975 SpaceManager::~SpaceManager() {
  1976   MutexLockerEx fcl(SpaceManager::expand_lock(),
  1977                     Mutex::_no_safepoint_check_flag);
  1979   ChunkManager* chunk_manager = vs_list()->chunk_manager();
  1981   chunk_manager->slow_locked_verify();
  1983   if (TraceMetadataChunkAllocation && Verbose) {
  1984     gclog_or_tty->print_cr("~SpaceManager(): " PTR_FORMAT, this);
  1985     locked_print_chunks_in_use_on(gclog_or_tty);
  1988   // Have to update before the chunks_in_use lists are emptied
  1989   // below.
  1990   chunk_manager->inc_free_chunks_total(sum_capacity_in_chunks_in_use(),
  1991                                        sum_count_in_chunks_in_use());
  1993 #ifdef ASSERT
  1994   // Mangle freed memory.
  1995   mangle_freed_chunks();
  1996 #endif // ASSERT
  1998   // Add all the chunks in use by this space manager
  1999   // to the global list of free chunks.
  2001   // Small chunks.  There is one _current_chunk for each
  2002   // Metaspace.  It could point to a small or medium chunk.
  2003   // Rather than determine which it is, follow the list of
  2004   // small chunks to add them to the free list
  2005   Metachunk* small_chunk = chunks_in_use(SmallIndex);
  2006   chunk_manager->free_small_chunks()->add_at_head(small_chunk);
  2007   set_chunks_in_use(SmallIndex, NULL);
  2009   // After the small chunk are the medium chunks
  2010   Metachunk* medium_chunk = chunks_in_use(MediumIndex);
  2011   assert(medium_chunk == NULL ||
  2012          medium_chunk->word_size() == MediumChunk,
  2013          "Chunk is on the wrong list");
  2015   if (medium_chunk != NULL) {
  2016     Metachunk* head = medium_chunk;
  2017     // If there is a medium chunk then the _current_chunk can only
  2018     // point to the last medium chunk.
  2019     Metachunk* tail = current_chunk();
  2020     chunk_manager->free_medium_chunks()->add_at_head(head, tail);
  2021     set_chunks_in_use(MediumIndex, NULL);
  2024   // Humongous chunks
  2025   // Humongous chunks are never the current chunk.
  2026   Metachunk* humongous_chunks = chunks_in_use(HumongousIndex);
  2028   while (humongous_chunks != NULL) {
  2029 #ifdef ASSERT
  2030     humongous_chunks->set_is_free(true);
  2031 #endif
  2032     Metachunk* next_humongous_chunks = humongous_chunks->next();
  2033     chunk_manager->humongous_dictionary()->return_chunk(humongous_chunks);
  2034     humongous_chunks = next_humongous_chunks;
  2036   set_chunks_in_use(HumongousIndex, NULL);
  2037   chunk_manager->slow_locked_verify();
  2040 void SpaceManager::deallocate(MetaWord* p, size_t word_size) {
  2041   assert_lock_strong(_lock);
  2042   size_t min_size = TreeChunk<Metablock, FreeList>::min_size();
  2043   assert(word_size >= min_size,
  2044     err_msg("Should not deallocate dark matter " SIZE_FORMAT, word_size));
  2045   block_freelists()->return_block(p, word_size);
  2048 // Adds a chunk to the list of chunks in use.
  2049 void SpaceManager::add_chunk(Metachunk* new_chunk, bool make_current) {
  2051   assert(new_chunk != NULL, "Should not be NULL");
  2052   assert(new_chunk->next() == NULL, "Should not be on a list");
  2054   new_chunk->reset_empty();
  2056   // Find the correct list and and set the current
  2057   // chunk for that list.
  2058   switch (new_chunk->word_size()) {
  2059   case SpaceManager::SmallChunk :
  2060     if (chunks_in_use(SmallIndex) == NULL) {
  2061       // First chunk to add to the list
  2062       set_chunks_in_use(SmallIndex, new_chunk);
  2063     } else {
  2064       assert(current_chunk()->word_size() == SpaceManager::SmallChunk,
  2065         err_msg( "Incorrect mix of sizes in chunk list "
  2066         SIZE_FORMAT " new chunk " SIZE_FORMAT,
  2067         current_chunk()->word_size(), new_chunk->word_size()));
  2068       current_chunk()->set_next(new_chunk);
  2070     // Make current chunk
  2071     set_current_chunk(new_chunk);
  2072     break;
  2073   case SpaceManager::MediumChunk :
  2074     if (chunks_in_use(MediumIndex) == NULL) {
  2075       // About to add the first medium chunk so teminate the
  2076       // small chunk list.  In general once medium chunks are
  2077       // being added, we're past the need for small chunks.
  2078       if (current_chunk() != NULL) {
  2079         // Only a small chunk or the initial chunk could be
  2080         // the current chunk if this is the first medium chunk.
  2081         assert(current_chunk()->word_size() == SpaceManager::SmallChunk ||
  2082           chunks_in_use(SmallIndex) == NULL,
  2083           err_msg("Should be a small chunk or initial chunk, current chunk "
  2084           SIZE_FORMAT " new chunk " SIZE_FORMAT,
  2085           current_chunk()->word_size(), new_chunk->word_size()));
  2086         current_chunk()->set_next(NULL);
  2088       // First chunk to add to the list
  2089       set_chunks_in_use(MediumIndex, new_chunk);
  2091     } else {
  2092       // As a minimum the first medium chunk added would
  2093       // have become the _current_chunk
  2094       // so the _current_chunk has to be non-NULL here
  2095       // (although not necessarily still the first medium chunk).
  2096       assert(current_chunk()->word_size() == SpaceManager::MediumChunk,
  2097              "A medium chunk should the current chunk");
  2098       current_chunk()->set_next(new_chunk);
  2100     // Make current chunk
  2101     set_current_chunk(new_chunk);
  2102     break;
  2103   default: {
  2104     // For null class loader data and DumpSharedSpaces, the first chunk isn't
  2105     // small, so small will be null.  Link this first chunk as the current
  2106     // chunk.
  2107     if (make_current) {
  2108       // Set as the current chunk but otherwise treat as a humongous chunk.
  2109       set_current_chunk(new_chunk);
  2111     // Link at head.  The _current_chunk only points to a humongous chunk for
  2112     // the null class loader metaspace (class and data virtual space managers)
  2113     // any humongous chunks so will not point to the tail
  2114     // of the humongous chunks list.
  2115     new_chunk->set_next(chunks_in_use(HumongousIndex));
  2116     set_chunks_in_use(HumongousIndex, new_chunk);
  2118     assert(new_chunk->word_size() > MediumChunk, "List inconsistency");
  2122   assert(new_chunk->is_empty(), "Not ready for reuse");
  2123   if (TraceMetadataChunkAllocation && Verbose) {
  2124     gclog_or_tty->print("SpaceManager::add_chunk: %d) ",
  2125                         sum_count_in_chunks_in_use());
  2126     new_chunk->print_on(gclog_or_tty);
  2127     vs_list()->chunk_manager()->locked_print_free_chunks(tty);
  2131 MetaWord* SpaceManager::allocate(size_t word_size) {
  2132   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  2134   // If only the dictionary is going to be used (i.e., no
  2135   // indexed free list), then there is a minimum size requirement.
  2136   // MinChunkSize is a placeholder for the real minimum size JJJ
  2137   size_t byte_size = word_size * BytesPerWord;
  2139   size_t byte_size_with_overhead = byte_size + Metablock::overhead();
  2141   size_t raw_bytes_size = MAX2(byte_size_with_overhead,
  2142                                Metablock::min_block_byte_size());
  2143   raw_bytes_size = ARENA_ALIGN(raw_bytes_size);
  2144   size_t raw_word_size = raw_bytes_size / BytesPerWord;
  2145   assert(raw_word_size * BytesPerWord == raw_bytes_size, "Size problem");
  2147   BlockFreelist* fl =  block_freelists();
  2148   MetaWord* p = NULL;
  2149   // Allocation from the dictionary is expensive in the sense that
  2150   // the dictionary has to be searched for a size.  Don't allocate
  2151   // from the dictionary until it starts to get fat.  Is this
  2152   // a reasonable policy?  Maybe an skinny dictionary is fast enough
  2153   // for allocations.  Do some profiling.  JJJ
  2154   if (fl->total_size() > allocation_from_dictionary_limit) {
  2155     p = fl->get_block(raw_word_size);
  2157   if (p == NULL) {
  2158     p = allocate_work(raw_word_size);
  2160   Metadebug::deallocate_block_a_lot(this, raw_word_size);
  2162   return p;
  2165 // Returns the address of spaced allocated for "word_size".
  2166 // This methods does not know about blocks (Metablocks)
  2167 MetaWord* SpaceManager::allocate_work(size_t word_size) {
  2168   assert_lock_strong(_lock);
  2169 #ifdef ASSERT
  2170   if (Metadebug::test_metadata_failure()) {
  2171     return NULL;
  2173 #endif
  2174   // Is there space in the current chunk?
  2175   MetaWord* result = NULL;
  2177   // For DumpSharedSpaces, only allocate out of the current chunk which is
  2178   // never null because we gave it the size we wanted.   Caller reports out
  2179   // of memory if this returns null.
  2180   if (DumpSharedSpaces) {
  2181     assert(current_chunk() != NULL, "should never happen");
  2182     inc_allocation_total(word_size);
  2183     return current_chunk()->allocate(word_size); // caller handles null result
  2185   if (current_chunk() != NULL) {
  2186     result = current_chunk()->allocate(word_size);
  2189   if (result == NULL) {
  2190     result = grow_and_allocate(word_size);
  2192   if (result > 0) {
  2193     inc_allocation_total(word_size);
  2194     assert(result != (MetaWord*) chunks_in_use(MediumIndex),
  2195            "Head of the list is being allocated");
  2198   return result;
  2201 void SpaceManager::verify() {
  2202   // If there are blocks in the dictionary, then
  2203   // verfication of chunks does not work since
  2204   // being in the dictionary alters a chunk.
  2205   if (block_freelists()->total_size() == 0) {
  2206     // Skip the small chunks because their next link points to
  2207     // medium chunks.  This is because the small chunk is the
  2208     // current chunk (for allocations) until it is full and the
  2209     // the addition of the next chunk does not NULL the next
  2210     // like of the small chunk.
  2211     for (ChunkIndex i = MediumIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  2212       Metachunk* curr = chunks_in_use(i);
  2213       while (curr != NULL) {
  2214         curr->verify();
  2215         curr = curr->next();
  2221 #ifdef ASSERT
  2222 void SpaceManager::verify_allocation_total() {
  2223 #if 0
  2224   // Verification is only guaranteed at a safepoint.
  2225   if (SafepointSynchronize::is_at_safepoint()) {
  2226     gclog_or_tty->print_cr("Chunk " PTR_FORMAT " allocation_total " SIZE_FORMAT
  2227                            " sum_used_in_chunks_in_use " SIZE_FORMAT,
  2228                            this,
  2229                            allocation_total(),
  2230                            sum_used_in_chunks_in_use());
  2232   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  2233   assert(allocation_total() == sum_used_in_chunks_in_use(),
  2234     err_msg("allocation total is not consistent %d vs %d",
  2235             allocation_total(), sum_used_in_chunks_in_use()));
  2236 #endif
  2239 #endif
  2241 void SpaceManager::dump(outputStream* const out) const {
  2242   size_t curr_total = 0;
  2243   size_t waste = 0;
  2244   uint i = 0;
  2245   size_t used = 0;
  2246   size_t capacity = 0;
  2248   // Add up statistics for all chunks in this SpaceManager.
  2249   for (ChunkIndex index = SmallIndex;
  2250        index < NumberOfInUseLists;
  2251        index = next_chunk_index(index)) {
  2252     for (Metachunk* curr = chunks_in_use(index);
  2253          curr != NULL;
  2254          curr = curr->next()) {
  2255       out->print("%d) ", i++);
  2256       curr->print_on(out);
  2257       if (TraceMetadataChunkAllocation && Verbose) {
  2258         block_freelists()->print_on(out);
  2260       curr_total += curr->word_size();
  2261       used += curr->used_word_size();
  2262       capacity += curr->capacity_word_size();
  2263       waste += curr->free_word_size() + curr->overhead();;
  2267   size_t free = current_chunk()->free_word_size();
  2268   // Free space isn't wasted.
  2269   waste -= free;
  2271   out->print_cr("total of all chunks "  SIZE_FORMAT " used " SIZE_FORMAT
  2272                 " free " SIZE_FORMAT " capacity " SIZE_FORMAT
  2273                 " waste " SIZE_FORMAT, curr_total, used, free, capacity, waste);
  2276 #ifdef ASSERT
  2277 void SpaceManager::mangle_freed_chunks() {
  2278   for (ChunkIndex index = SmallIndex;
  2279        index < NumberOfInUseLists;
  2280        index = next_chunk_index(index)) {
  2281     for (Metachunk* curr = chunks_in_use(index);
  2282          curr != NULL;
  2283          curr = curr->next()) {
  2284       // Try to detect incorrectly terminated small chunk
  2285       // list.
  2286       assert(index == MediumIndex || curr != chunks_in_use(MediumIndex),
  2287              err_msg("Mangling medium chunks in small chunks? "
  2288                      "curr " PTR_FORMAT " medium list " PTR_FORMAT,
  2289                      curr, chunks_in_use(MediumIndex)));
  2290       curr->mangle();
  2294 #endif // ASSERT
  2297 // MetaspaceAux
  2299 size_t MetaspaceAux::used_in_bytes(Metaspace::MetadataType mdtype) {
  2300   size_t used = 0;
  2301   ClassLoaderDataGraphMetaspaceIterator iter;
  2302   while (iter.repeat()) {
  2303     Metaspace* msp = iter.get_next();
  2304     // Sum allocation_total for each metaspace
  2305     if (msp != NULL) {
  2306       used += msp->used_words(mdtype);
  2309   return used * BytesPerWord;
  2312 size_t MetaspaceAux::free_in_bytes(Metaspace::MetadataType mdtype) {
  2313   size_t free = 0;
  2314   ClassLoaderDataGraphMetaspaceIterator iter;
  2315   while (iter.repeat()) {
  2316     Metaspace* msp = iter.get_next();
  2317     if (msp != NULL) {
  2318       free += msp->free_words(mdtype);
  2321   return free * BytesPerWord;
  2324 // The total words available for metadata allocation.  This
  2325 // uses Metaspace capacity_words() which is the total words
  2326 // in chunks allocated for a Metaspace.
  2327 size_t MetaspaceAux::capacity_in_bytes(Metaspace::MetadataType mdtype) {
  2328   size_t capacity = free_chunks_total(mdtype);
  2329   ClassLoaderDataGraphMetaspaceIterator iter;
  2330   while (iter.repeat()) {
  2331     Metaspace* msp = iter.get_next();
  2332     if (msp != NULL) {
  2333       capacity += msp->capacity_words(mdtype);
  2336   return capacity * BytesPerWord;
  2339 size_t MetaspaceAux::reserved_in_bytes(Metaspace::MetadataType mdtype) {
  2340   size_t reserved = (mdtype == Metaspace::ClassType) ?
  2341                        Metaspace::class_space_list()->virtual_space_total() :
  2342                        Metaspace::space_list()->virtual_space_total();
  2343   return reserved * BytesPerWord;
  2346 size_t MetaspaceAux::min_chunk_size() { return SpaceManager::MediumChunk; }
  2348 size_t MetaspaceAux::free_chunks_total(Metaspace::MetadataType mdtype) {
  2349   ChunkManager* chunk = (mdtype == Metaspace::ClassType) ?
  2350                             Metaspace::class_space_list()->chunk_manager() :
  2351                             Metaspace::space_list()->chunk_manager();
  2352   chunk->slow_verify();
  2353   return chunk->free_chunks_total();
  2356 size_t MetaspaceAux::free_chunks_total_in_bytes(Metaspace::MetadataType mdtype) {
  2357   return free_chunks_total(mdtype) * BytesPerWord;
  2360 void MetaspaceAux::print_metaspace_change(size_t prev_metadata_used) {
  2361   gclog_or_tty->print(", [Metaspace:");
  2362   if (PrintGCDetails && Verbose) {
  2363     gclog_or_tty->print(" "  SIZE_FORMAT
  2364                         "->" SIZE_FORMAT
  2365                         "("  SIZE_FORMAT "/" SIZE_FORMAT ")",
  2366                         prev_metadata_used,
  2367                         used_in_bytes(),
  2368                         capacity_in_bytes(),
  2369                         reserved_in_bytes());
  2370   } else {
  2371     gclog_or_tty->print(" "  SIZE_FORMAT "K"
  2372                         "->" SIZE_FORMAT "K"
  2373                         "("  SIZE_FORMAT "K/" SIZE_FORMAT "K)",
  2374                         prev_metadata_used / K,
  2375                         used_in_bytes()/ K,
  2376                         capacity_in_bytes()/K,
  2377                         reserved_in_bytes()/ K);
  2380   gclog_or_tty->print("]");
  2383 // This is printed when PrintGCDetails
  2384 void MetaspaceAux::print_on(outputStream* out) {
  2385   Metaspace::MetadataType ct = Metaspace::ClassType;
  2386   Metaspace::MetadataType nct = Metaspace::NonClassType;
  2388   out->print_cr(" Metaspace total "
  2389                 SIZE_FORMAT "K, used " SIZE_FORMAT "K,"
  2390                 " reserved " SIZE_FORMAT "K",
  2391                 capacity_in_bytes()/K, used_in_bytes()/K, reserved_in_bytes()/K);
  2392   out->print_cr("  data space     "
  2393                 SIZE_FORMAT "K, used " SIZE_FORMAT "K,"
  2394                 " reserved " SIZE_FORMAT "K",
  2395                 capacity_in_bytes(nct)/K, used_in_bytes(nct)/K, reserved_in_bytes(nct)/K);
  2396   out->print_cr("  class space    "
  2397                 SIZE_FORMAT "K, used " SIZE_FORMAT "K,"
  2398                 " reserved " SIZE_FORMAT "K",
  2399                 capacity_in_bytes(ct)/K, used_in_bytes(ct)/K, reserved_in_bytes(ct)/K);
  2402 // Print information for class space and data space separately.
  2403 // This is almost the same as above.
  2404 void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) {
  2405   size_t free_chunks_capacity_bytes = free_chunks_total_in_bytes(mdtype);
  2406   size_t capacity_bytes = capacity_in_bytes(mdtype);
  2407   size_t used_bytes = used_in_bytes(mdtype);
  2408   size_t free_bytes = free_in_bytes(mdtype);
  2409   size_t used_and_free = used_bytes + free_bytes +
  2410                            free_chunks_capacity_bytes;
  2411   out->print_cr("  Chunk accounting: used in chunks " SIZE_FORMAT
  2412              "K + unused in chunks " SIZE_FORMAT "K  + "
  2413              " capacity in free chunks " SIZE_FORMAT "K = " SIZE_FORMAT
  2414              "K  capacity in allocated chunks " SIZE_FORMAT "K",
  2415              used_bytes / K,
  2416              free_bytes / K,
  2417              free_chunks_capacity_bytes / K,
  2418              used_and_free / K,
  2419              capacity_bytes / K);
  2420   assert(used_and_free == capacity_bytes, "Accounting is wrong");
  2423 // Print total fragmentation for class and data metaspaces separately
  2424 void MetaspaceAux::print_waste(outputStream* out) {
  2426   size_t small_waste = 0, medium_waste = 0, large_waste = 0;
  2427   size_t cls_small_waste = 0, cls_medium_waste = 0, cls_large_waste = 0;
  2429   ClassLoaderDataGraphMetaspaceIterator iter;
  2430   while (iter.repeat()) {
  2431     Metaspace* msp = iter.get_next();
  2432     if (msp != NULL) {
  2433       small_waste += msp->vsm()->sum_waste_in_chunks_in_use(SmallIndex);
  2434       medium_waste += msp->vsm()->sum_waste_in_chunks_in_use(MediumIndex);
  2435       large_waste += msp->vsm()->sum_waste_in_chunks_in_use(HumongousIndex);
  2437       cls_small_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SmallIndex);
  2438       cls_medium_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(MediumIndex);
  2439       cls_large_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(HumongousIndex);
  2442   out->print_cr("Total fragmentation waste (words) doesn't count free space");
  2443   out->print("  data: small " SIZE_FORMAT " medium " SIZE_FORMAT,
  2444              small_waste, medium_waste);
  2445   out->print_cr(" class: small " SIZE_FORMAT, cls_small_waste);
  2448 // Dump global metaspace things from the end of ClassLoaderDataGraph
  2449 void MetaspaceAux::dump(outputStream* out) {
  2450   out->print_cr("All Metaspace:");
  2451   out->print("data space: "); print_on(out, Metaspace::NonClassType);
  2452   out->print("class space: "); print_on(out, Metaspace::ClassType);
  2453   print_waste(out);
  2456 void MetaspaceAux::verify_free_chunks() {
  2457   Metaspace::space_list()->chunk_manager()->verify();
  2458   Metaspace::class_space_list()->chunk_manager()->verify();
  2461 // Metaspace methods
  2463 size_t Metaspace::_first_chunk_word_size = 0;
  2465 Metaspace::Metaspace(Mutex* lock, size_t word_size) {
  2466   initialize(lock, word_size);
  2469 Metaspace::Metaspace(Mutex* lock) {
  2470   initialize(lock);
  2473 Metaspace::~Metaspace() {
  2474   delete _vsm;
  2475   delete _class_vsm;
  2478 VirtualSpaceList* Metaspace::_space_list = NULL;
  2479 VirtualSpaceList* Metaspace::_class_space_list = NULL;
  2481 #define VIRTUALSPACEMULTIPLIER 2
  2483 void Metaspace::global_initialize() {
  2484   // Initialize the alignment for shared spaces.
  2485   int max_alignment = os::vm_page_size();
  2486   MetaspaceShared::set_max_alignment(max_alignment);
  2488   if (DumpSharedSpaces) {
  2489     SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment);
  2490     SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment);
  2491     SharedMiscDataSize  = align_size_up(SharedMiscDataSize, max_alignment);
  2492     SharedMiscCodeSize  = align_size_up(SharedMiscCodeSize, max_alignment);
  2494     // Initialize with the sum of the shared space sizes.  The read-only
  2495     // and read write metaspace chunks will be allocated out of this and the
  2496     // remainder is the misc code and data chunks.
  2497     size_t total = align_size_up(SharedReadOnlySize + SharedReadWriteSize +
  2498                                  SharedMiscDataSize + SharedMiscCodeSize,
  2499                                  os::vm_allocation_granularity());
  2500     size_t word_size = total/wordSize;
  2501     _space_list = new VirtualSpaceList(word_size);
  2502   } else {
  2503     // If using shared space, open the file that contains the shared space
  2504     // and map in the memory before initializing the rest of metaspace (so
  2505     // the addresses don't conflict)
  2506     if (UseSharedSpaces) {
  2507       FileMapInfo* mapinfo = new FileMapInfo();
  2508       memset(mapinfo, 0, sizeof(FileMapInfo));
  2510       // Open the shared archive file, read and validate the header. If
  2511       // initialization fails, shared spaces [UseSharedSpaces] are
  2512       // disabled and the file is closed.
  2513       // Map in spaces now also
  2514       if (mapinfo->initialize() && MetaspaceShared::map_shared_spaces(mapinfo)) {
  2515         FileMapInfo::set_current_info(mapinfo);
  2516       } else {
  2517         assert(!mapinfo->is_open() && !UseSharedSpaces,
  2518                "archive file not closed or shared spaces not disabled.");
  2522     // Initialize this before initializing the VirtualSpaceList
  2523     _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord;
  2524     // Arbitrarily set the initial virtual space to a multiple
  2525     // of the boot class loader size.
  2526     size_t word_size = VIRTUALSPACEMULTIPLIER * Metaspace::first_chunk_word_size();
  2527     // Initialize the list of virtual spaces.
  2528     _space_list = new VirtualSpaceList(word_size);
  2532 // For UseCompressedKlassPointers the class space is reserved as a piece of the
  2533 // Java heap because the compression algorithm is the same for each.  The
  2534 // argument passed in is at the top of the compressed space
  2535 void Metaspace::initialize_class_space(ReservedSpace rs) {
  2536   // The reserved space size may be bigger because of alignment, esp with UseLargePages
  2537   assert(rs.size() >= ClassMetaspaceSize, err_msg("%d != %d", rs.size(), ClassMetaspaceSize));
  2538   _class_space_list = new VirtualSpaceList(rs);
  2542 void Metaspace::initialize(Mutex* lock, size_t initial_size) {
  2543   // Use SmallChunk size if not specified.   If specified, use this size for
  2544   // the data metaspace.
  2545   size_t word_size;
  2546   size_t class_word_size;
  2547   if (initial_size == 0) {
  2548     word_size = (size_t) SpaceManager::SmallChunk;
  2549     class_word_size = (size_t) SpaceManager::SmallChunk;
  2550   } else {
  2551     word_size = initial_size;
  2552     // Make the first class chunk bigger than a medium chunk so it's not put
  2553     // on the medium chunk list.   The next chunk will be small and progress
  2554     // from there.  This size calculated by -version.
  2555     class_word_size = MIN2((size_t)SpaceManager::MediumChunk*5,
  2556                            (ClassMetaspaceSize/BytesPerWord)*2);
  2559   assert(space_list() != NULL,
  2560     "Metadata VirtualSpaceList has not been initialized");
  2562   _vsm = new SpaceManager(lock, space_list());
  2563   if (_vsm == NULL) {
  2564     return;
  2567   assert(class_space_list() != NULL,
  2568     "Class VirtualSpaceList has not been initialized");
  2570   // Allocate SpaceManager for classes.
  2571   _class_vsm = new SpaceManager(lock, class_space_list());
  2572   if (_class_vsm == NULL) {
  2573     return;
  2576   MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
  2578   // Allocate chunk for metadata objects
  2579   Metachunk* new_chunk =
  2580      space_list()->current_virtual_space()->get_chunk_vs_with_expand(word_size);
  2581   assert(!DumpSharedSpaces || new_chunk != NULL, "should have enough space for both chunks");
  2582   if (new_chunk != NULL) {
  2583     // Add to this manager's list of chunks in use and current_chunk().
  2584     vsm()->add_chunk(new_chunk, true);
  2587   // Allocate chunk for class metadata objects
  2588   Metachunk* class_chunk =
  2589      class_space_list()->current_virtual_space()->get_chunk_vs_with_expand(class_word_size);
  2590   if (class_chunk != NULL) {
  2591     class_vsm()->add_chunk(class_chunk, true);
  2595 MetaWord* Metaspace::allocate(size_t word_size, MetadataType mdtype) {
  2596   // DumpSharedSpaces doesn't use class metadata area (yet)
  2597   if (mdtype == ClassType && !DumpSharedSpaces) {
  2598     return  class_vsm()->allocate(word_size);
  2599   } else {
  2600     return  vsm()->allocate(word_size);
  2604 MetaWord* Metaspace::expand_and_allocate(size_t word_size, MetadataType mdtype) {
  2605   MetaWord* result;
  2606   MetaspaceGC::set_expand_after_GC(true);
  2607   size_t before_inc = MetaspaceGC::capacity_until_GC();
  2608   size_t delta_words = MetaspaceGC::delta_capacity_until_GC(word_size);
  2609   MetaspaceGC::inc_capacity_until_GC(delta_words);
  2610   if (PrintGCDetails && Verbose) {
  2611     gclog_or_tty->print_cr("Increase capacity to GC from " SIZE_FORMAT
  2612       " to " SIZE_FORMAT, before_inc, MetaspaceGC::capacity_until_GC());
  2615   result = allocate(word_size, mdtype);
  2617   return result;
  2620 // Space allocated in the Metaspace.  This may
  2621 // be across several metadata virtual spaces.
  2622 char* Metaspace::bottom() const {
  2623   assert(DumpSharedSpaces, "only useful and valid for dumping shared spaces");
  2624   return (char*)vsm()->current_chunk()->bottom();
  2627 size_t Metaspace::used_words(MetadataType mdtype) const {
  2628   // return vsm()->allocation_total();
  2629   return mdtype == ClassType ? class_vsm()->sum_used_in_chunks_in_use() :
  2630                                vsm()->sum_used_in_chunks_in_use();  // includes overhead!
  2633 size_t Metaspace::free_words(MetadataType mdtype) const {
  2634   return mdtype == ClassType ? class_vsm()->sum_free_in_chunks_in_use() :
  2635                                vsm()->sum_free_in_chunks_in_use();
  2638 // Space capacity in the Metaspace.  It includes
  2639 // space in the list of chunks from which allocations
  2640 // have been made. Don't include space in the global freelist and
  2641 // in the space available in the dictionary which
  2642 // is already counted in some chunk.
  2643 size_t Metaspace::capacity_words(MetadataType mdtype) const {
  2644   return mdtype == ClassType ? class_vsm()->sum_capacity_in_chunks_in_use() :
  2645                                vsm()->sum_capacity_in_chunks_in_use();
  2648 void Metaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) {
  2649   if (SafepointSynchronize::is_at_safepoint()) {
  2650     assert(Thread::current()->is_VM_thread(), "should be the VM thread");
  2651     // Don't take Heap_lock
  2652     MutexLocker ml(vsm()->lock());
  2653     if (word_size < TreeChunk<Metablock, FreeList>::min_size()) {
  2654       // Dark matter.  Too small for dictionary.
  2655 #ifdef ASSERT
  2656       Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5);
  2657 #endif
  2658       return;
  2660     if (is_class) {
  2661        class_vsm()->deallocate(ptr, word_size);
  2662     } else {
  2663       vsm()->deallocate(ptr, word_size);
  2665   } else {
  2666     MutexLocker ml(vsm()->lock());
  2668     if (word_size < TreeChunk<Metablock, FreeList>::min_size()) {
  2669       // Dark matter.  Too small for dictionary.
  2670 #ifdef ASSERT
  2671       Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5);
  2672 #endif
  2673       return;
  2675     if (is_class) {
  2676       class_vsm()->deallocate(ptr, word_size);
  2677     } else {
  2678       vsm()->deallocate(ptr, word_size);
  2683 Metablock* Metaspace::allocate(ClassLoaderData* loader_data, size_t word_size,
  2684                               bool read_only, MetadataType mdtype, TRAPS) {
  2685   if (HAS_PENDING_EXCEPTION) {
  2686     assert(false, "Should not allocate with exception pending");
  2687     return NULL;  // caller does a CHECK_NULL too
  2690   // SSS: Should we align the allocations and make sure the sizes are aligned.
  2691   MetaWord* result = NULL;
  2693   assert(loader_data != NULL, "Should never pass around a NULL loader_data. "
  2694         "ClassLoaderData::the_null_class_loader_data() should have been used.");
  2695   // Allocate in metaspaces without taking out a lock, because it deadlocks
  2696   // with the SymbolTable_lock.  Dumping is single threaded for now.  We'll have
  2697   // to revisit this for application class data sharing.
  2698   if (DumpSharedSpaces) {
  2699     if (read_only) {
  2700       result = loader_data->ro_metaspace()->allocate(word_size, NonClassType);
  2701     } else {
  2702       result = loader_data->rw_metaspace()->allocate(word_size, NonClassType);
  2704     if (result == NULL) {
  2705       report_out_of_shared_space(read_only ? SharedReadOnly : SharedReadWrite);
  2707     return Metablock::initialize(result, word_size);
  2710   result = loader_data->metaspace_non_null()->allocate(word_size, mdtype);
  2712   if (result == NULL) {
  2713     // Try to clean out some memory and retry.
  2714     result =
  2715       Universe::heap()->collector_policy()->satisfy_failed_metadata_allocation(
  2716         loader_data, word_size, mdtype);
  2718     // If result is still null, we are out of memory.
  2719     if (result == NULL) {
  2720       // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
  2721       report_java_out_of_memory("Metadata space");
  2723       if (JvmtiExport::should_post_resource_exhausted()) {
  2724         JvmtiExport::post_resource_exhausted(
  2725             JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,
  2726             "Metadata space");
  2728       THROW_OOP_0(Universe::out_of_memory_error_perm_gen());
  2731   return Metablock::initialize(result, word_size);
  2734 void Metaspace::print_on(outputStream* out) const {
  2735   // Print both class virtual space counts and metaspace.
  2736   if (Verbose) {
  2737       vsm()->print_on(out);
  2738       class_vsm()->print_on(out);
  2742 #ifndef PRODUCT
  2743 bool Metaspace::contains(const void * ptr) const {
  2744   if (MetaspaceShared::is_in_shared_space(ptr)) {
  2745     return true;
  2747   MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
  2748   return space_list()->contains(ptr) || class_space_list()->contains(ptr);
  2750 #endif
  2752 void Metaspace::verify() {
  2753   vsm()->verify();
  2754   class_vsm()->verify();
  2757 void Metaspace::dump(outputStream* const out) const {
  2758   if (UseMallocOnly) {
  2759     // Just print usage for now
  2760     out->print_cr("usage %d", used_words(Metaspace::NonClassType));
  2762   out->print_cr("\nVirtual space manager: " INTPTR_FORMAT, vsm());
  2763   vsm()->dump(out);
  2764   out->print_cr("\nClass space manager: " INTPTR_FORMAT, class_vsm());
  2765   class_vsm()->dump(out);

mercurial