src/share/vm/memory/metaspace.cpp

Wed, 01 May 2013 14:11:01 +0100

author
chegar
date
Wed, 01 May 2013 14:11:01 +0100
changeset 5246
4b52137b07c9
parent 4932
df254344edf1
child 5007
c23dbf0e8ab7
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    24 #include "precompiled.hpp"
    25 #include "gc_interface/collectedHeap.hpp"
    26 #include "memory/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 "runtime/orderAccess.hpp"
    40 #include "services/memTracker.hpp"
    41 #include "utilities/copy.hpp"
    42 #include "utilities/debug.hpp"
    44 typedef BinaryTreeDictionary<Metablock, FreeList> BlockTreeDictionary;
    45 typedef BinaryTreeDictionary<Metachunk, FreeList> ChunkTreeDictionary;
    46 // Define this macro to enable slow integrity checking of
    47 // the free chunk lists
    48 const bool metaspace_slow_verify = false;
    51 // Parameters for stress mode testing
    52 const uint metadata_deallocate_a_lot_block = 10;
    53 const uint metadata_deallocate_a_lock_chunk = 3;
    54 size_t const allocation_from_dictionary_limit = 64 * K;
    56 MetaWord* last_allocated = 0;
    58 // Used in declarations in SpaceManager and ChunkManager
    59 enum ChunkIndex {
    60   ZeroIndex = 0,
    61   SpecializedIndex = ZeroIndex,
    62   SmallIndex = SpecializedIndex + 1,
    63   MediumIndex = SmallIndex + 1,
    64   HumongousIndex = MediumIndex + 1,
    65   NumberOfFreeLists = 3,
    66   NumberOfInUseLists = 4
    67 };
    69 enum ChunkSizes {    // in words.
    70   ClassSpecializedChunk = 128,
    71   SpecializedChunk = 128,
    72   ClassSmallChunk = 256,
    73   SmallChunk = 512,
    74   ClassMediumChunk = 1 * K,
    75   MediumChunk = 8 * K,
    76   HumongousChunkGranularity = 8
    77 };
    79 static ChunkIndex next_chunk_index(ChunkIndex i) {
    80   assert(i < NumberOfInUseLists, "Out of bound");
    81   return (ChunkIndex) (i+1);
    82 }
    84 // Originally _capacity_until_GC was set to MetaspaceSize here but
    85 // the default MetaspaceSize before argument processing was being
    86 // used which was not the desired value.  See the code
    87 // in should_expand() to see how the initialization is handled
    88 // now.
    89 size_t MetaspaceGC::_capacity_until_GC = 0;
    90 bool MetaspaceGC::_expand_after_GC = false;
    91 uint MetaspaceGC::_shrink_factor = 0;
    92 bool MetaspaceGC::_should_concurrent_collect = false;
    94 // Blocks of space for metadata are allocated out of Metachunks.
    95 //
    96 // Metachunk are allocated out of MetadataVirtualspaces and once
    97 // allocated there is no explicit link between a Metachunk and
    98 // the MetadataVirtualspaces from which it was allocated.
    99 //
   100 // Each SpaceManager maintains a
   101 // list of the chunks it is using and the current chunk.  The current
   102 // chunk is the chunk from which allocations are done.  Space freed in
   103 // a chunk is placed on the free list of blocks (BlockFreelist) and
   104 // reused from there.
   106 typedef class FreeList<Metachunk> ChunkList;
   108 // Manages the global free lists of chunks.
   109 // Has three lists of free chunks, and a total size and
   110 // count that includes all three
   112 class ChunkManager VALUE_OBJ_CLASS_SPEC {
   114   // Free list of chunks of different sizes.
   115   //   SmallChunk
   116   //   MediumChunk
   117   //   HumongousChunk
   118   ChunkList _free_chunks[NumberOfFreeLists];
   121   //   HumongousChunk
   122   ChunkTreeDictionary _humongous_dictionary;
   124   // ChunkManager in all lists of this type
   125   size_t _free_chunks_total;
   126   size_t _free_chunks_count;
   128   void dec_free_chunks_total(size_t v) {
   129     assert(_free_chunks_count > 0 &&
   130              _free_chunks_total > 0,
   131              "About to go negative");
   132     Atomic::add_ptr(-1, &_free_chunks_count);
   133     jlong minus_v = (jlong) - (jlong) v;
   134     Atomic::add_ptr(minus_v, &_free_chunks_total);
   135   }
   137   // Debug support
   139   size_t sum_free_chunks();
   140   size_t sum_free_chunks_count();
   142   void locked_verify_free_chunks_total();
   143   void slow_locked_verify_free_chunks_total() {
   144     if (metaspace_slow_verify) {
   145       locked_verify_free_chunks_total();
   146     }
   147   }
   148   void locked_verify_free_chunks_count();
   149   void slow_locked_verify_free_chunks_count() {
   150     if (metaspace_slow_verify) {
   151       locked_verify_free_chunks_count();
   152     }
   153   }
   154   void verify_free_chunks_count();
   156  public:
   158   ChunkManager() : _free_chunks_total(0), _free_chunks_count(0) {}
   160   // add or delete (return) a chunk to the global freelist.
   161   Metachunk* chunk_freelist_allocate(size_t word_size);
   162   void chunk_freelist_deallocate(Metachunk* chunk);
   164   // Map a size to a list index assuming that there are lists
   165   // for special, small, medium, and humongous chunks.
   166   static ChunkIndex list_index(size_t size);
   168   // Add the simple linked list of chunks to the freelist of chunks
   169   // of type index.
   170   void return_chunks(ChunkIndex index, Metachunk* chunks);
   172   // Total of the space in the free chunks list
   173   size_t free_chunks_total();
   174   size_t free_chunks_total_in_bytes();
   176   // Number of chunks in the free chunks list
   177   size_t free_chunks_count();
   179   void inc_free_chunks_total(size_t v, size_t count = 1) {
   180     Atomic::add_ptr(count, &_free_chunks_count);
   181     Atomic::add_ptr(v, &_free_chunks_total);
   182   }
   183   ChunkTreeDictionary* humongous_dictionary() {
   184     return &_humongous_dictionary;
   185   }
   187   ChunkList* free_chunks(ChunkIndex index);
   189   // Returns the list for the given chunk word size.
   190   ChunkList* find_free_chunks_list(size_t word_size);
   192   // Add and remove from a list by size.  Selects
   193   // list based on size of chunk.
   194   void free_chunks_put(Metachunk* chuck);
   195   Metachunk* free_chunks_get(size_t chunk_word_size);
   197   // Debug support
   198   void verify();
   199   void slow_verify() {
   200     if (metaspace_slow_verify) {
   201       verify();
   202     }
   203   }
   204   void locked_verify();
   205   void slow_locked_verify() {
   206     if (metaspace_slow_verify) {
   207       locked_verify();
   208     }
   209   }
   210   void verify_free_chunks_total();
   212   void locked_print_free_chunks(outputStream* st);
   213   void locked_print_sum_free_chunks(outputStream* st);
   215   void print_on(outputStream* st);
   216 };
   219 // Used to manage the free list of Metablocks (a block corresponds
   220 // to the allocation of a quantum of metadata).
   221 class BlockFreelist VALUE_OBJ_CLASS_SPEC {
   222   BlockTreeDictionary* _dictionary;
   223   static Metablock* initialize_free_chunk(MetaWord* p, size_t word_size);
   225   // Accessors
   226   BlockTreeDictionary* dictionary() const { return _dictionary; }
   228  public:
   229   BlockFreelist();
   230   ~BlockFreelist();
   232   // Get and return a block to the free list
   233   MetaWord* get_block(size_t word_size);
   234   void return_block(MetaWord* p, size_t word_size);
   236   size_t total_size() {
   237   if (dictionary() == NULL) {
   238     return 0;
   239   } else {
   240     return dictionary()->total_size();
   241   }
   242 }
   244   void print_on(outputStream* st) const;
   245 };
   247 class VirtualSpaceNode : public CHeapObj<mtClass> {
   248   friend class VirtualSpaceList;
   250   // Link to next VirtualSpaceNode
   251   VirtualSpaceNode* _next;
   253   // total in the VirtualSpace
   254   MemRegion _reserved;
   255   ReservedSpace _rs;
   256   VirtualSpace _virtual_space;
   257   MetaWord* _top;
   259   // Convenience functions for logical bottom and end
   260   MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
   261   MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
   263   // Convenience functions to access the _virtual_space
   264   char* low()  const { return virtual_space()->low(); }
   265   char* high() const { return virtual_space()->high(); }
   267  public:
   269   VirtualSpaceNode(size_t byte_size);
   270   VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs) {}
   271   ~VirtualSpaceNode();
   273   // address of next available space in _virtual_space;
   274   // Accessors
   275   VirtualSpaceNode* next() { return _next; }
   276   void set_next(VirtualSpaceNode* v) { _next = v; }
   278   void set_reserved(MemRegion const v) { _reserved = v; }
   279   void set_top(MetaWord* v) { _top = v; }
   281   // Accessors
   282   MemRegion* reserved() { return &_reserved; }
   283   VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; }
   285   // Returns true if "word_size" is available in the virtual space
   286   bool is_available(size_t word_size) { return _top + word_size <= end(); }
   288   MetaWord* top() const { return _top; }
   289   void inc_top(size_t word_size) { _top += word_size; }
   291   // used and capacity in this single entry in the list
   292   size_t used_words_in_vs() const;
   293   size_t capacity_words_in_vs() const;
   295   bool initialize();
   297   // get space from the virtual space
   298   Metachunk* take_from_committed(size_t chunk_word_size);
   300   // Allocate a chunk from the virtual space and return it.
   301   Metachunk* get_chunk_vs(size_t chunk_word_size);
   302   Metachunk* get_chunk_vs_with_expand(size_t chunk_word_size);
   304   // Expands/shrinks the committed space in a virtual space.  Delegates
   305   // to Virtualspace
   306   bool expand_by(size_t words, bool pre_touch = false);
   307   bool shrink_by(size_t words);
   309 #ifdef ASSERT
   310   // Debug support
   311   static void verify_virtual_space_total();
   312   static void verify_virtual_space_count();
   313   void mangle();
   314 #endif
   316   void print_on(outputStream* st) const;
   317 };
   319   // byte_size is the size of the associated virtualspace.
   320 VirtualSpaceNode::VirtualSpaceNode(size_t byte_size) : _top(NULL), _next(NULL), _rs(0) {
   321   // align up to vm allocation granularity
   322   byte_size = align_size_up(byte_size, os::vm_allocation_granularity());
   324   // This allocates memory with mmap.  For DumpSharedspaces, try to reserve
   325   // configurable address, generally at the top of the Java heap so other
   326   // memory addresses don't conflict.
   327   if (DumpSharedSpaces) {
   328     char* shared_base = (char*)SharedBaseAddress;
   329     _rs = ReservedSpace(byte_size, 0, false, shared_base, 0);
   330     if (_rs.is_reserved()) {
   331       assert(shared_base == 0 || _rs.base() == shared_base, "should match");
   332     } else {
   333       // Get a mmap region anywhere if the SharedBaseAddress fails.
   334       _rs = ReservedSpace(byte_size);
   335     }
   336     MetaspaceShared::set_shared_rs(&_rs);
   337   } else {
   338     _rs = ReservedSpace(byte_size);
   339   }
   341   MemTracker::record_virtual_memory_type((address)_rs.base(), mtClass);
   342 }
   344 // List of VirtualSpaces for metadata allocation.
   345 // It has a  _next link for singly linked list and a MemRegion
   346 // for total space in the VirtualSpace.
   347 class VirtualSpaceList : public CHeapObj<mtClass> {
   348   friend class VirtualSpaceNode;
   350   enum VirtualSpaceSizes {
   351     VirtualSpaceSize = 256 * K
   352   };
   354   // Global list of virtual spaces
   355   // Head of the list
   356   VirtualSpaceNode* _virtual_space_list;
   357   // virtual space currently being used for allocations
   358   VirtualSpaceNode* _current_virtual_space;
   359   // Free chunk list for all other metadata
   360   ChunkManager      _chunk_manager;
   362   // Can this virtual list allocate >1 spaces?  Also, used to determine
   363   // whether to allocate unlimited small chunks in this virtual space
   364   bool _is_class;
   365   bool can_grow() const { return !is_class() || !UseCompressedKlassPointers; }
   367   // Sum of space in all virtual spaces and number of virtual spaces
   368   size_t _virtual_space_total;
   369   size_t _virtual_space_count;
   371   ~VirtualSpaceList();
   373   VirtualSpaceNode* virtual_space_list() const { return _virtual_space_list; }
   375   void set_virtual_space_list(VirtualSpaceNode* v) {
   376     _virtual_space_list = v;
   377   }
   378   void set_current_virtual_space(VirtualSpaceNode* v) {
   379     _current_virtual_space = v;
   380   }
   382   void link_vs(VirtualSpaceNode* new_entry, size_t vs_word_size);
   384   // Get another virtual space and add it to the list.  This
   385   // is typically prompted by a failed attempt to allocate a chunk
   386   // and is typically followed by the allocation of a chunk.
   387   bool grow_vs(size_t vs_word_size);
   389  public:
   390   VirtualSpaceList(size_t word_size);
   391   VirtualSpaceList(ReservedSpace rs);
   393   Metachunk* get_new_chunk(size_t word_size,
   394                            size_t grow_chunks_by_words,
   395                            size_t medium_chunk_bunch);
   397   // Get the first chunk for a Metaspace.  Used for
   398   // special cases such as the boot class loader, reflection
   399   // class loader and anonymous class loader.
   400   Metachunk* get_initialization_chunk(size_t word_size, size_t chunk_bunch);
   402   VirtualSpaceNode* current_virtual_space() {
   403     return _current_virtual_space;
   404   }
   406   ChunkManager* chunk_manager() { return &_chunk_manager; }
   407   bool is_class() const { return _is_class; }
   409   // Allocate the first virtualspace.
   410   void initialize(size_t word_size);
   412   size_t virtual_space_total() { return _virtual_space_total; }
   413   void inc_virtual_space_total(size_t v) {
   414     Atomic::add_ptr(v, &_virtual_space_total);
   415   }
   417   size_t virtual_space_count() { return _virtual_space_count; }
   418   void inc_virtual_space_count() {
   419     Atomic::inc_ptr(&_virtual_space_count);
   420   }
   422   // Used and capacity in the entire list of virtual spaces.
   423   // These are global values shared by all Metaspaces
   424   size_t capacity_words_sum();
   425   size_t capacity_bytes_sum() { return capacity_words_sum() * BytesPerWord; }
   426   size_t used_words_sum();
   427   size_t used_bytes_sum() { return used_words_sum() * BytesPerWord; }
   429   bool contains(const void *ptr);
   431   void print_on(outputStream* st) const;
   433   class VirtualSpaceListIterator : public StackObj {
   434     VirtualSpaceNode* _virtual_spaces;
   435    public:
   436     VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) :
   437       _virtual_spaces(virtual_spaces) {}
   439     bool repeat() {
   440       return _virtual_spaces != NULL;
   441     }
   443     VirtualSpaceNode* get_next() {
   444       VirtualSpaceNode* result = _virtual_spaces;
   445       if (_virtual_spaces != NULL) {
   446         _virtual_spaces = _virtual_spaces->next();
   447       }
   448       return result;
   449     }
   450   };
   451 };
   453 class Metadebug : AllStatic {
   454   // Debugging support for Metaspaces
   455   static int _deallocate_block_a_lot_count;
   456   static int _deallocate_chunk_a_lot_count;
   457   static int _allocation_fail_alot_count;
   459  public:
   460   static int deallocate_block_a_lot_count() {
   461     return _deallocate_block_a_lot_count;
   462   }
   463   static void set_deallocate_block_a_lot_count(int v) {
   464     _deallocate_block_a_lot_count = v;
   465   }
   466   static void inc_deallocate_block_a_lot_count() {
   467     _deallocate_block_a_lot_count++;
   468   }
   469   static int deallocate_chunk_a_lot_count() {
   470     return _deallocate_chunk_a_lot_count;
   471   }
   472   static void reset_deallocate_chunk_a_lot_count() {
   473     _deallocate_chunk_a_lot_count = 1;
   474   }
   475   static void inc_deallocate_chunk_a_lot_count() {
   476     _deallocate_chunk_a_lot_count++;
   477   }
   479   static void init_allocation_fail_alot_count();
   480 #ifdef ASSERT
   481   static bool test_metadata_failure();
   482 #endif
   484   static void deallocate_chunk_a_lot(SpaceManager* sm,
   485                                      size_t chunk_word_size);
   486   static void deallocate_block_a_lot(SpaceManager* sm,
   487                                      size_t chunk_word_size);
   489 };
   491 int Metadebug::_deallocate_block_a_lot_count = 0;
   492 int Metadebug::_deallocate_chunk_a_lot_count = 0;
   493 int Metadebug::_allocation_fail_alot_count = 0;
   495 //  SpaceManager - used by Metaspace to handle allocations
   496 class SpaceManager : public CHeapObj<mtClass> {
   497   friend class Metaspace;
   498   friend class Metadebug;
   500  private:
   502   // protects allocations and contains.
   503   Mutex* const _lock;
   505   // Chunk related size
   506   size_t _medium_chunk_bunch;
   508   // List of chunks in use by this SpaceManager.  Allocations
   509   // are done from the current chunk.  The list is used for deallocating
   510   // chunks when the SpaceManager is freed.
   511   Metachunk* _chunks_in_use[NumberOfInUseLists];
   512   Metachunk* _current_chunk;
   514   // Virtual space where allocation comes from.
   515   VirtualSpaceList* _vs_list;
   517   // Number of small chunks to allocate to a manager
   518   // If class space manager, small chunks are unlimited
   519   static uint const _small_chunk_limit;
   520   bool has_small_chunk_limit() { return !vs_list()->is_class(); }
   522   // Sum of all space in allocated chunks
   523   size_t _allocation_total;
   525   // Free lists of blocks are per SpaceManager since they
   526   // are assumed to be in chunks in use by the SpaceManager
   527   // and all chunks in use by a SpaceManager are freed when
   528   // the class loader using the SpaceManager is collected.
   529   BlockFreelist _block_freelists;
   531   // protects virtualspace and chunk expansions
   532   static const char*  _expand_lock_name;
   533   static const int    _expand_lock_rank;
   534   static Mutex* const _expand_lock;
   536  private:
   537   // Accessors
   538   Metachunk* chunks_in_use(ChunkIndex index) const { return _chunks_in_use[index]; }
   539   void set_chunks_in_use(ChunkIndex index, Metachunk* v) { _chunks_in_use[index] = v; }
   541   BlockFreelist* block_freelists() const {
   542     return (BlockFreelist*) &_block_freelists;
   543   }
   545   VirtualSpaceList* vs_list() const    { return _vs_list; }
   547   Metachunk* current_chunk() const { return _current_chunk; }
   548   void set_current_chunk(Metachunk* v) {
   549     _current_chunk = v;
   550   }
   552   Metachunk* find_current_chunk(size_t word_size);
   554   // Add chunk to the list of chunks in use
   555   void add_chunk(Metachunk* v, bool make_current);
   557   Mutex* lock() const { return _lock; }
   559   const char* chunk_size_name(ChunkIndex index) const;
   561  protected:
   562   void initialize();
   564  public:
   565   SpaceManager(Mutex* lock,
   566                VirtualSpaceList* vs_list);
   567   ~SpaceManager();
   569   enum ChunkMultiples {
   570     MediumChunkMultiple = 4
   571   };
   573   // Accessors
   574   size_t specialized_chunk_size() { return SpecializedChunk; }
   575   size_t small_chunk_size() { return (size_t) vs_list()->is_class() ? ClassSmallChunk : SmallChunk; }
   576   size_t medium_chunk_size() { return (size_t) vs_list()->is_class() ? ClassMediumChunk : MediumChunk; }
   577   size_t medium_chunk_bunch() { return medium_chunk_size() * MediumChunkMultiple; }
   579   size_t allocation_total() const { return _allocation_total; }
   580   void inc_allocation_total(size_t v) { Atomic::add_ptr(v, &_allocation_total); }
   581   bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); }
   583   static Mutex* expand_lock() { return _expand_lock; }
   585   // Set the sizes for the initial chunks.
   586   void get_initial_chunk_sizes(Metaspace::MetaspaceType type,
   587                                size_t* chunk_word_size,
   588                                size_t* class_chunk_word_size);
   590   size_t sum_capacity_in_chunks_in_use() const;
   591   size_t sum_used_in_chunks_in_use() const;
   592   size_t sum_free_in_chunks_in_use() const;
   593   size_t sum_waste_in_chunks_in_use() const;
   594   size_t sum_waste_in_chunks_in_use(ChunkIndex index ) const;
   596   size_t sum_count_in_chunks_in_use();
   597   size_t sum_count_in_chunks_in_use(ChunkIndex i);
   599   Metachunk* get_new_chunk(size_t word_size, size_t grow_chunks_by_words);
   601   // Block allocation and deallocation.
   602   // Allocates a block from the current chunk
   603   MetaWord* allocate(size_t word_size);
   605   // Helper for allocations
   606   MetaWord* allocate_work(size_t word_size);
   608   // Returns a block to the per manager freelist
   609   void deallocate(MetaWord* p, size_t word_size);
   611   // Based on the allocation size and a minimum chunk size,
   612   // returned chunk size (for expanding space for chunk allocation).
   613   size_t calc_chunk_size(size_t allocation_word_size);
   615   // Called when an allocation from the current chunk fails.
   616   // Gets a new chunk (may require getting a new virtual space),
   617   // and allocates from that chunk.
   618   MetaWord* grow_and_allocate(size_t word_size);
   620   // debugging support.
   622   void dump(outputStream* const out) const;
   623   void print_on(outputStream* st) const;
   624   void locked_print_chunks_in_use_on(outputStream* st) const;
   626   void verify();
   627   void verify_chunk_size(Metachunk* chunk);
   628   NOT_PRODUCT(void mangle_freed_chunks();)
   629 #ifdef ASSERT
   630   void verify_allocation_total();
   631 #endif
   632 };
   634 uint const SpaceManager::_small_chunk_limit = 4;
   636 const char* SpaceManager::_expand_lock_name =
   637   "SpaceManager chunk allocation lock";
   638 const int SpaceManager::_expand_lock_rank = Monitor::leaf - 1;
   639 Mutex* const SpaceManager::_expand_lock =
   640   new Mutex(SpaceManager::_expand_lock_rank,
   641             SpaceManager::_expand_lock_name,
   642             Mutex::_allow_vm_block_flag);
   644 // BlockFreelist methods
   646 BlockFreelist::BlockFreelist() : _dictionary(NULL) {}
   648 BlockFreelist::~BlockFreelist() {
   649   if (_dictionary != NULL) {
   650     if (Verbose && TraceMetadataChunkAllocation) {
   651       _dictionary->print_free_lists(gclog_or_tty);
   652     }
   653     delete _dictionary;
   654   }
   655 }
   657 Metablock* BlockFreelist::initialize_free_chunk(MetaWord* p, size_t word_size) {
   658   Metablock* block = (Metablock*) p;
   659   block->set_word_size(word_size);
   660   block->set_prev(NULL);
   661   block->set_next(NULL);
   663   return block;
   664 }
   666 void BlockFreelist::return_block(MetaWord* p, size_t word_size) {
   667   Metablock* free_chunk = initialize_free_chunk(p, word_size);
   668   if (dictionary() == NULL) {
   669    _dictionary = new BlockTreeDictionary();
   670   }
   671   dictionary()->return_chunk(free_chunk);
   672 }
   674 MetaWord* BlockFreelist::get_block(size_t word_size) {
   675   if (dictionary() == NULL) {
   676     return NULL;
   677   }
   679   if (word_size < TreeChunk<Metablock, FreeList>::min_size()) {
   680     // Dark matter.  Too small for dictionary.
   681     return NULL;
   682   }
   684   Metablock* free_block =
   685     dictionary()->get_chunk(word_size, FreeBlockDictionary<Metablock>::exactly);
   686   if (free_block == NULL) {
   687     return NULL;
   688   }
   690   return (MetaWord*) free_block;
   691 }
   693 void BlockFreelist::print_on(outputStream* st) const {
   694   if (dictionary() == NULL) {
   695     return;
   696   }
   697   dictionary()->print_free_lists(st);
   698 }
   700 // VirtualSpaceNode methods
   702 VirtualSpaceNode::~VirtualSpaceNode() {
   703   _rs.release();
   704 }
   706 size_t VirtualSpaceNode::used_words_in_vs() const {
   707   return pointer_delta(top(), bottom(), sizeof(MetaWord));
   708 }
   710 // Space committed in the VirtualSpace
   711 size_t VirtualSpaceNode::capacity_words_in_vs() const {
   712   return pointer_delta(end(), bottom(), sizeof(MetaWord));
   713 }
   716 // Allocates the chunk from the virtual space only.
   717 // This interface is also used internally for debugging.  Not all
   718 // chunks removed here are necessarily used for allocation.
   719 Metachunk* VirtualSpaceNode::take_from_committed(size_t chunk_word_size) {
   720   // Bottom of the new chunk
   721   MetaWord* chunk_limit = top();
   722   assert(chunk_limit != NULL, "Not safe to call this method");
   724   if (!is_available(chunk_word_size)) {
   725     if (TraceMetadataChunkAllocation) {
   726       tty->print("VirtualSpaceNode::take_from_committed() not available %d words ", chunk_word_size);
   727       // Dump some information about the virtual space that is nearly full
   728       print_on(tty);
   729     }
   730     return NULL;
   731   }
   733   // Take the space  (bump top on the current virtual space).
   734   inc_top(chunk_word_size);
   736   // Point the chunk at the space
   737   Metachunk* result = Metachunk::initialize(chunk_limit, chunk_word_size);
   738   return result;
   739 }
   742 // Expand the virtual space (commit more of the reserved space)
   743 bool VirtualSpaceNode::expand_by(size_t words, bool pre_touch) {
   744   size_t bytes = words * BytesPerWord;
   745   bool result =  virtual_space()->expand_by(bytes, pre_touch);
   746   if (TraceMetavirtualspaceAllocation && !result) {
   747     gclog_or_tty->print_cr("VirtualSpaceNode::expand_by() failed "
   748                            "for byte size " SIZE_FORMAT, bytes);
   749     virtual_space()->print();
   750   }
   751   return result;
   752 }
   754 // Shrink the virtual space (commit more of the reserved space)
   755 bool VirtualSpaceNode::shrink_by(size_t words) {
   756   size_t bytes = words * BytesPerWord;
   757   virtual_space()->shrink_by(bytes);
   758   return true;
   759 }
   761 // Add another chunk to the chunk list.
   763 Metachunk* VirtualSpaceNode::get_chunk_vs(size_t chunk_word_size) {
   764   assert_lock_strong(SpaceManager::expand_lock());
   765   Metachunk* result = NULL;
   767   return take_from_committed(chunk_word_size);
   768 }
   770 Metachunk* VirtualSpaceNode::get_chunk_vs_with_expand(size_t chunk_word_size) {
   771   assert_lock_strong(SpaceManager::expand_lock());
   773   Metachunk* new_chunk = get_chunk_vs(chunk_word_size);
   775   if (new_chunk == NULL) {
   776     // Only a small part of the virtualspace is committed when first
   777     // allocated so committing more here can be expected.
   778     size_t page_size_words = os::vm_page_size() / BytesPerWord;
   779     size_t aligned_expand_vs_by_words = align_size_up(chunk_word_size,
   780                                                     page_size_words);
   781     expand_by(aligned_expand_vs_by_words, false);
   782     new_chunk = get_chunk_vs(chunk_word_size);
   783   }
   784   return new_chunk;
   785 }
   787 bool VirtualSpaceNode::initialize() {
   789   if (!_rs.is_reserved()) {
   790     return false;
   791   }
   793   // An allocation out of this Virtualspace that is larger
   794   // than an initial commit size can waste that initial committed
   795   // space.
   796   size_t committed_byte_size = 0;
   797   bool result = virtual_space()->initialize(_rs, committed_byte_size);
   798   if (result) {
   799     set_top((MetaWord*)virtual_space()->low());
   800     set_reserved(MemRegion((HeapWord*)_rs.base(),
   801                  (HeapWord*)(_rs.base() + _rs.size())));
   803     assert(reserved()->start() == (HeapWord*) _rs.base(),
   804       err_msg("Reserved start was not set properly " PTR_FORMAT
   805         " != " PTR_FORMAT, reserved()->start(), _rs.base()));
   806     assert(reserved()->word_size() == _rs.size() / BytesPerWord,
   807       err_msg("Reserved size was not set properly " SIZE_FORMAT
   808         " != " SIZE_FORMAT, reserved()->word_size(),
   809         _rs.size() / BytesPerWord));
   810   }
   812   return result;
   813 }
   815 void VirtualSpaceNode::print_on(outputStream* st) const {
   816   size_t used = used_words_in_vs();
   817   size_t capacity = capacity_words_in_vs();
   818   VirtualSpace* vs = virtual_space();
   819   st->print_cr("   space @ " PTR_FORMAT " " SIZE_FORMAT "K, %3d%% used "
   820            "[" PTR_FORMAT ", " PTR_FORMAT ", "
   821            PTR_FORMAT ", " PTR_FORMAT ")",
   822            vs, capacity / K,
   823            capacity == 0 ? 0 : used * 100 / capacity,
   824            bottom(), top(), end(),
   825            vs->high_boundary());
   826 }
   828 #ifdef ASSERT
   829 void VirtualSpaceNode::mangle() {
   830   size_t word_size = capacity_words_in_vs();
   831   Copy::fill_to_words((HeapWord*) low(), word_size, 0xf1f1f1f1);
   832 }
   833 #endif // ASSERT
   835 // VirtualSpaceList methods
   836 // Space allocated from the VirtualSpace
   838 VirtualSpaceList::~VirtualSpaceList() {
   839   VirtualSpaceListIterator iter(virtual_space_list());
   840   while (iter.repeat()) {
   841     VirtualSpaceNode* vsl = iter.get_next();
   842     delete vsl;
   843   }
   844 }
   846 size_t VirtualSpaceList::used_words_sum() {
   847   size_t allocated_by_vs = 0;
   848   VirtualSpaceListIterator iter(virtual_space_list());
   849   while (iter.repeat()) {
   850     VirtualSpaceNode* vsl = iter.get_next();
   851     // Sum used region [bottom, top) in each virtualspace
   852     allocated_by_vs += vsl->used_words_in_vs();
   853   }
   854   assert(allocated_by_vs >= chunk_manager()->free_chunks_total(),
   855     err_msg("Total in free chunks " SIZE_FORMAT
   856             " greater than total from virtual_spaces " SIZE_FORMAT,
   857             allocated_by_vs, chunk_manager()->free_chunks_total()));
   858   size_t used =
   859     allocated_by_vs - chunk_manager()->free_chunks_total();
   860   return used;
   861 }
   863 // Space available in all MetadataVirtualspaces allocated
   864 // for metadata.  This is the upper limit on the capacity
   865 // of chunks allocated out of all the MetadataVirtualspaces.
   866 size_t VirtualSpaceList::capacity_words_sum() {
   867   size_t capacity = 0;
   868   VirtualSpaceListIterator iter(virtual_space_list());
   869   while (iter.repeat()) {
   870     VirtualSpaceNode* vsl = iter.get_next();
   871     capacity += vsl->capacity_words_in_vs();
   872   }
   873   return capacity;
   874 }
   876 VirtualSpaceList::VirtualSpaceList(size_t word_size ) :
   877                                    _is_class(false),
   878                                    _virtual_space_list(NULL),
   879                                    _current_virtual_space(NULL),
   880                                    _virtual_space_total(0),
   881                                    _virtual_space_count(0) {
   882   MutexLockerEx cl(SpaceManager::expand_lock(),
   883                    Mutex::_no_safepoint_check_flag);
   884   bool initialization_succeeded = grow_vs(word_size);
   886   _chunk_manager.free_chunks(SpecializedIndex)->set_size(SpecializedChunk);
   887   _chunk_manager.free_chunks(SmallIndex)->set_size(SmallChunk);
   888   _chunk_manager.free_chunks(MediumIndex)->set_size(MediumChunk);
   889   assert(initialization_succeeded,
   890     " VirtualSpaceList initialization should not fail");
   891 }
   893 VirtualSpaceList::VirtualSpaceList(ReservedSpace rs) :
   894                                    _is_class(true),
   895                                    _virtual_space_list(NULL),
   896                                    _current_virtual_space(NULL),
   897                                    _virtual_space_total(0),
   898                                    _virtual_space_count(0) {
   899   MutexLockerEx cl(SpaceManager::expand_lock(),
   900                    Mutex::_no_safepoint_check_flag);
   901   VirtualSpaceNode* class_entry = new VirtualSpaceNode(rs);
   902   bool succeeded = class_entry->initialize();
   903   _chunk_manager.free_chunks(SpecializedIndex)->set_size(SpecializedChunk);
   904   _chunk_manager.free_chunks(SmallIndex)->set_size(ClassSmallChunk);
   905   _chunk_manager.free_chunks(MediumIndex)->set_size(ClassMediumChunk);
   906   assert(succeeded, " VirtualSpaceList initialization should not fail");
   907   link_vs(class_entry, rs.size()/BytesPerWord);
   908 }
   910 // Allocate another meta virtual space and add it to the list.
   911 bool VirtualSpaceList::grow_vs(size_t vs_word_size) {
   912   assert_lock_strong(SpaceManager::expand_lock());
   913   if (vs_word_size == 0) {
   914     return false;
   915   }
   916   // Reserve the space
   917   size_t vs_byte_size = vs_word_size * BytesPerWord;
   918   assert(vs_byte_size % os::vm_page_size() == 0, "Not aligned");
   920   // Allocate the meta virtual space and initialize it.
   921   VirtualSpaceNode* new_entry = new VirtualSpaceNode(vs_byte_size);
   922   if (!new_entry->initialize()) {
   923     delete new_entry;
   924     return false;
   925   } else {
   926     // ensure lock-free iteration sees fully initialized node
   927     OrderAccess::storestore();
   928     link_vs(new_entry, vs_word_size);
   929     return true;
   930   }
   931 }
   933 void VirtualSpaceList::link_vs(VirtualSpaceNode* new_entry, size_t vs_word_size) {
   934   if (virtual_space_list() == NULL) {
   935       set_virtual_space_list(new_entry);
   936   } else {
   937     current_virtual_space()->set_next(new_entry);
   938   }
   939   set_current_virtual_space(new_entry);
   940   inc_virtual_space_total(vs_word_size);
   941   inc_virtual_space_count();
   942 #ifdef ASSERT
   943   new_entry->mangle();
   944 #endif
   945   if (TraceMetavirtualspaceAllocation && Verbose) {
   946     VirtualSpaceNode* vsl = current_virtual_space();
   947     vsl->print_on(tty);
   948   }
   949 }
   951 Metachunk* VirtualSpaceList::get_new_chunk(size_t word_size,
   952                                            size_t grow_chunks_by_words,
   953                                            size_t medium_chunk_bunch) {
   955   // Get a chunk from the chunk freelist
   956   Metachunk* next = chunk_manager()->chunk_freelist_allocate(grow_chunks_by_words);
   958   // Allocate a chunk out of the current virtual space.
   959   if (next == NULL) {
   960     next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
   961   }
   963   if (next == NULL) {
   964     // Not enough room in current virtual space.  Try to commit
   965     // more space.
   966     size_t expand_vs_by_words = MAX2(medium_chunk_bunch,
   967                                      grow_chunks_by_words);
   968     size_t page_size_words = os::vm_page_size() / BytesPerWord;
   969     size_t aligned_expand_vs_by_words = align_size_up(expand_vs_by_words,
   970                                                         page_size_words);
   971     bool vs_expanded =
   972       current_virtual_space()->expand_by(aligned_expand_vs_by_words, false);
   973     if (!vs_expanded) {
   974       // Should the capacity of the metaspaces be expanded for
   975       // this allocation?  If it's the virtual space for classes and is
   976       // being used for CompressedHeaders, don't allocate a new virtualspace.
   977       if (can_grow() && MetaspaceGC::should_expand(this, word_size)) {
   978         // Get another virtual space.
   979           size_t grow_vs_words =
   980             MAX2((size_t)VirtualSpaceSize, aligned_expand_vs_by_words);
   981         if (grow_vs(grow_vs_words)) {
   982           // Got it.  It's on the list now.  Get a chunk from it.
   983           next = current_virtual_space()->get_chunk_vs_with_expand(grow_chunks_by_words);
   984         }
   985       } else {
   986         // Allocation will fail and induce a GC
   987         if (TraceMetadataChunkAllocation && Verbose) {
   988           gclog_or_tty->print_cr("VirtualSpaceList::get_new_chunk():"
   989             " Fail instead of expand the metaspace");
   990         }
   991       }
   992     } else {
   993       // The virtual space expanded, get a new chunk
   994       next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
   995       assert(next != NULL, "Just expanded, should succeed");
   996     }
   997   }
   999   assert(next == NULL || (next->next() == NULL && next->prev() == NULL),
  1000          "New chunk is still on some list");
  1001   return next;
  1004 Metachunk* VirtualSpaceList::get_initialization_chunk(size_t chunk_word_size,
  1005                                                       size_t chunk_bunch) {
  1006   // Get a chunk from the chunk freelist
  1007   Metachunk* new_chunk = get_new_chunk(chunk_word_size,
  1008                                        chunk_word_size,
  1009                                        chunk_bunch);
  1010   return new_chunk;
  1013 void VirtualSpaceList::print_on(outputStream* st) const {
  1014   if (TraceMetadataChunkAllocation && Verbose) {
  1015     VirtualSpaceListIterator iter(virtual_space_list());
  1016     while (iter.repeat()) {
  1017       VirtualSpaceNode* node = iter.get_next();
  1018       node->print_on(st);
  1023 bool VirtualSpaceList::contains(const void *ptr) {
  1024   VirtualSpaceNode* list = virtual_space_list();
  1025   VirtualSpaceListIterator iter(list);
  1026   while (iter.repeat()) {
  1027     VirtualSpaceNode* node = iter.get_next();
  1028     if (node->reserved()->contains(ptr)) {
  1029       return true;
  1032   return false;
  1036 // MetaspaceGC methods
  1038 // VM_CollectForMetadataAllocation is the vm operation used to GC.
  1039 // Within the VM operation after the GC the attempt to allocate the metadata
  1040 // should succeed.  If the GC did not free enough space for the metaspace
  1041 // allocation, the HWM is increased so that another virtualspace will be
  1042 // allocated for the metadata.  With perm gen the increase in the perm
  1043 // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion.  The
  1044 // metaspace policy uses those as the small and large steps for the HWM.
  1045 //
  1046 // After the GC the compute_new_size() for MetaspaceGC is called to
  1047 // resize the capacity of the metaspaces.  The current implementation
  1048 // is based on the flags MinMetaspaceFreeRatio and MaxHeapFreeRatio used
  1049 // to resize the Java heap by some GC's.  New flags can be implemented
  1050 // if really needed.  MinHeapFreeRatio is used to calculate how much
  1051 // free space is desirable in the metaspace capacity to decide how much
  1052 // to increase the HWM.  MaxMetaspaceFreeRatio is used to decide how much
  1053 // free space is desirable in the metaspace capacity before decreasing
  1054 // the HWM.
  1056 // Calculate the amount to increase the high water mark (HWM).
  1057 // Increase by a minimum amount (MinMetaspaceExpansion) so that
  1058 // another expansion is not requested too soon.  If that is not
  1059 // enough to satisfy the allocation (i.e. big enough for a word_size
  1060 // allocation), increase by MaxMetaspaceExpansion.  If that is still
  1061 // not enough, expand by the size of the allocation (word_size) plus
  1062 // some.
  1063 size_t MetaspaceGC::delta_capacity_until_GC(size_t word_size) {
  1064   size_t before_inc = MetaspaceGC::capacity_until_GC();
  1065   size_t min_delta_words = MinMetaspaceExpansion / BytesPerWord;
  1066   size_t max_delta_words = MaxMetaspaceExpansion / BytesPerWord;
  1067   size_t page_size_words = os::vm_page_size() / BytesPerWord;
  1068   size_t size_delta_words = align_size_up(word_size, page_size_words);
  1069   size_t delta_words = MAX2(size_delta_words, min_delta_words);
  1070   if (delta_words > min_delta_words) {
  1071     // Don't want to hit the high water mark on the next
  1072     // allocation so make the delta greater than just enough
  1073     // for this allocation.
  1074     delta_words = MAX2(delta_words, max_delta_words);
  1075     if (delta_words > max_delta_words) {
  1076       // This allocation is large but the next ones are probably not
  1077       // so increase by the minimum.
  1078       delta_words = delta_words + min_delta_words;
  1081   return delta_words;
  1084 bool MetaspaceGC::should_expand(VirtualSpaceList* vsl, size_t word_size) {
  1085   // If the user wants a limit, impose one.
  1086   if (!FLAG_IS_DEFAULT(MaxMetaspaceSize) &&
  1087       MetaspaceAux::reserved_in_bytes() >= MaxMetaspaceSize) {
  1088     return false;
  1091   // Class virtual space should always be expanded.  Call GC for the other
  1092   // metadata virtual space.
  1093   if (vsl == Metaspace::class_space_list()) return true;
  1095   // If this is part of an allocation after a GC, expand
  1096   // unconditionally.
  1097   if(MetaspaceGC::expand_after_GC()) {
  1098     return true;
  1101   size_t metaspace_size_words = MetaspaceSize / BytesPerWord;
  1103   // If the capacity is below the minimum capacity, allow the
  1104   // expansion.  Also set the high-water-mark (capacity_until_GC)
  1105   // to that minimum capacity so that a GC will not be induced
  1106   // until that minimum capacity is exceeded.
  1107   if (vsl->capacity_words_sum() < metaspace_size_words ||
  1108       capacity_until_GC() == 0) {
  1109     set_capacity_until_GC(metaspace_size_words);
  1110     return true;
  1111   } else {
  1112     if (vsl->capacity_words_sum() < capacity_until_GC()) {
  1113       return true;
  1114     } else {
  1115       if (TraceMetadataChunkAllocation && Verbose) {
  1116         gclog_or_tty->print_cr("  allocation request size " SIZE_FORMAT
  1117                         "  capacity_until_GC " SIZE_FORMAT
  1118                         "  capacity_words_sum " SIZE_FORMAT
  1119                         "  used_words_sum " SIZE_FORMAT
  1120                         "  free chunks " SIZE_FORMAT
  1121                         "  free chunks count %d",
  1122                         word_size,
  1123                         capacity_until_GC(),
  1124                         vsl->capacity_words_sum(),
  1125                         vsl->used_words_sum(),
  1126                         vsl->chunk_manager()->free_chunks_total(),
  1127                         vsl->chunk_manager()->free_chunks_count());
  1129       return false;
  1134 // Variables are in bytes
  1136 void MetaspaceGC::compute_new_size() {
  1137   assert(_shrink_factor <= 100, "invalid shrink factor");
  1138   uint current_shrink_factor = _shrink_factor;
  1139   _shrink_factor = 0;
  1141   VirtualSpaceList *vsl = Metaspace::space_list();
  1143   size_t capacity_after_gc = vsl->capacity_bytes_sum();
  1144   // Check to see if these two can be calculated without walking the CLDG
  1145   size_t used_after_gc = vsl->used_bytes_sum();
  1146   size_t capacity_until_GC = vsl->capacity_bytes_sum();
  1147   size_t free_after_gc = capacity_until_GC - used_after_gc;
  1149   const double minimum_free_percentage = MinMetaspaceFreeRatio / 100.0;
  1150   const double maximum_used_percentage = 1.0 - minimum_free_percentage;
  1152   const double min_tmp = used_after_gc / maximum_used_percentage;
  1153   size_t minimum_desired_capacity =
  1154     (size_t)MIN2(min_tmp, double(max_uintx));
  1155   // Don't shrink less than the initial generation size
  1156   minimum_desired_capacity = MAX2(minimum_desired_capacity,
  1157                                   MetaspaceSize);
  1159   if (PrintGCDetails && Verbose) {
  1160     const double free_percentage = ((double)free_after_gc) / capacity_until_GC;
  1161     gclog_or_tty->print_cr("\nMetaspaceGC::compute_new_size: ");
  1162     gclog_or_tty->print_cr("  "
  1163                   "  minimum_free_percentage: %6.2f"
  1164                   "  maximum_used_percentage: %6.2f",
  1165                   minimum_free_percentage,
  1166                   maximum_used_percentage);
  1167     double d_free_after_gc = free_after_gc / (double) K;
  1168     gclog_or_tty->print_cr("  "
  1169                   "   free_after_gc       : %6.1fK"
  1170                   "   used_after_gc       : %6.1fK"
  1171                   "   capacity_after_gc   : %6.1fK"
  1172                   "   metaspace HWM     : %6.1fK",
  1173                   free_after_gc / (double) K,
  1174                   used_after_gc / (double) K,
  1175                   capacity_after_gc / (double) K,
  1176                   capacity_until_GC / (double) K);
  1177     gclog_or_tty->print_cr("  "
  1178                   "   free_percentage: %6.2f",
  1179                   free_percentage);
  1183   if (capacity_until_GC < minimum_desired_capacity) {
  1184     // If we have less capacity below the metaspace HWM, then
  1185     // increment the HWM.
  1186     size_t expand_bytes = minimum_desired_capacity - capacity_until_GC;
  1187     // Don't expand unless it's significant
  1188     if (expand_bytes >= MinMetaspaceExpansion) {
  1189       size_t expand_words = expand_bytes / BytesPerWord;
  1190       MetaspaceGC::inc_capacity_until_GC(expand_words);
  1192     if (PrintGCDetails && Verbose) {
  1193       size_t new_capacity_until_GC = MetaspaceGC::capacity_until_GC_in_bytes();
  1194       gclog_or_tty->print_cr("    expanding:"
  1195                     "  minimum_desired_capacity: %6.1fK"
  1196                     "  expand_words: %6.1fK"
  1197                     "  MinMetaspaceExpansion: %6.1fK"
  1198                     "  new metaspace HWM:  %6.1fK",
  1199                     minimum_desired_capacity / (double) K,
  1200                     expand_bytes / (double) K,
  1201                     MinMetaspaceExpansion / (double) K,
  1202                     new_capacity_until_GC / (double) K);
  1204     return;
  1207   // No expansion, now see if we want to shrink
  1208   size_t shrink_words = 0;
  1209   // We would never want to shrink more than this
  1210   size_t max_shrink_words = capacity_until_GC - minimum_desired_capacity;
  1211   assert(max_shrink_words >= 0, err_msg("max_shrink_words " SIZE_FORMAT,
  1212     max_shrink_words));
  1214   // Should shrinking be considered?
  1215   if (MaxMetaspaceFreeRatio < 100) {
  1216     const double maximum_free_percentage = MaxMetaspaceFreeRatio / 100.0;
  1217     const double minimum_used_percentage = 1.0 - maximum_free_percentage;
  1218     const double max_tmp = used_after_gc / minimum_used_percentage;
  1219     size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx));
  1220     maximum_desired_capacity = MAX2(maximum_desired_capacity,
  1221                                     MetaspaceSize);
  1222     if (PrintGC && Verbose) {
  1223       gclog_or_tty->print_cr("  "
  1224                              "  maximum_free_percentage: %6.2f"
  1225                              "  minimum_used_percentage: %6.2f",
  1226                              maximum_free_percentage,
  1227                              minimum_used_percentage);
  1228       gclog_or_tty->print_cr("  "
  1229                              "  capacity_until_GC: %6.1fK"
  1230                              "  minimum_desired_capacity: %6.1fK"
  1231                              "  maximum_desired_capacity: %6.1fK",
  1232                              capacity_until_GC / (double) K,
  1233                              minimum_desired_capacity / (double) K,
  1234                              maximum_desired_capacity / (double) K);
  1237     assert(minimum_desired_capacity <= maximum_desired_capacity,
  1238            "sanity check");
  1240     if (capacity_until_GC > maximum_desired_capacity) {
  1241       // Capacity too large, compute shrinking size
  1242       shrink_words = capacity_until_GC - maximum_desired_capacity;
  1243       // We don't want shrink all the way back to initSize if people call
  1244       // System.gc(), because some programs do that between "phases" and then
  1245       // we'd just have to grow the heap up again for the next phase.  So we
  1246       // damp the shrinking: 0% on the first call, 10% on the second call, 40%
  1247       // on the third call, and 100% by the fourth call.  But if we recompute
  1248       // size without shrinking, it goes back to 0%.
  1249       shrink_words = shrink_words / 100 * current_shrink_factor;
  1250       assert(shrink_words <= max_shrink_words,
  1251         err_msg("invalid shrink size " SIZE_FORMAT " not <= " SIZE_FORMAT,
  1252           shrink_words, max_shrink_words));
  1253       if (current_shrink_factor == 0) {
  1254         _shrink_factor = 10;
  1255       } else {
  1256         _shrink_factor = MIN2(current_shrink_factor * 4, (uint) 100);
  1258       if (PrintGCDetails && Verbose) {
  1259         gclog_or_tty->print_cr("  "
  1260                       "  shrinking:"
  1261                       "  initSize: %.1fK"
  1262                       "  maximum_desired_capacity: %.1fK",
  1263                       MetaspaceSize / (double) K,
  1264                       maximum_desired_capacity / (double) K);
  1265         gclog_or_tty->print_cr("  "
  1266                       "  shrink_words: %.1fK"
  1267                       "  current_shrink_factor: %d"
  1268                       "  new shrink factor: %d"
  1269                       "  MinMetaspaceExpansion: %.1fK",
  1270                       shrink_words / (double) K,
  1271                       current_shrink_factor,
  1272                       _shrink_factor,
  1273                       MinMetaspaceExpansion / (double) K);
  1279   // Don't shrink unless it's significant
  1280   if (shrink_words >= MinMetaspaceExpansion) {
  1281     VirtualSpaceNode* csp = vsl->current_virtual_space();
  1282     size_t available_to_shrink = csp->capacity_words_in_vs() -
  1283       csp->used_words_in_vs();
  1284     shrink_words = MIN2(shrink_words, available_to_shrink);
  1285     csp->shrink_by(shrink_words);
  1286     MetaspaceGC::dec_capacity_until_GC(shrink_words);
  1287     if (PrintGCDetails && Verbose) {
  1288       size_t new_capacity_until_GC = MetaspaceGC::capacity_until_GC_in_bytes();
  1289       gclog_or_tty->print_cr("  metaspace HWM: %.1fK", new_capacity_until_GC / (double) K);
  1292   assert(used_after_gc <= vsl->capacity_bytes_sum(),
  1293          "sanity check");
  1297 // Metadebug methods
  1299 void Metadebug::deallocate_chunk_a_lot(SpaceManager* sm,
  1300                                        size_t chunk_word_size){
  1301 #ifdef ASSERT
  1302   VirtualSpaceList* vsl = sm->vs_list();
  1303   if (MetaDataDeallocateALot &&
  1304       Metadebug::deallocate_chunk_a_lot_count() % MetaDataDeallocateALotInterval == 0 ) {
  1305     Metadebug::reset_deallocate_chunk_a_lot_count();
  1306     for (uint i = 0; i < metadata_deallocate_a_lock_chunk; i++) {
  1307       Metachunk* dummy_chunk = vsl->current_virtual_space()->take_from_committed(chunk_word_size);
  1308       if (dummy_chunk == NULL) {
  1309         break;
  1311       vsl->chunk_manager()->chunk_freelist_deallocate(dummy_chunk);
  1313       if (TraceMetadataChunkAllocation && Verbose) {
  1314         gclog_or_tty->print("Metadebug::deallocate_chunk_a_lot: %d) ",
  1315                                sm->sum_count_in_chunks_in_use());
  1316         dummy_chunk->print_on(gclog_or_tty);
  1317         gclog_or_tty->print_cr("  Free chunks total %d  count %d",
  1318                                vsl->chunk_manager()->free_chunks_total(),
  1319                                vsl->chunk_manager()->free_chunks_count());
  1322   } else {
  1323     Metadebug::inc_deallocate_chunk_a_lot_count();
  1325 #endif
  1328 void Metadebug::deallocate_block_a_lot(SpaceManager* sm,
  1329                                        size_t raw_word_size){
  1330 #ifdef ASSERT
  1331   if (MetaDataDeallocateALot &&
  1332         Metadebug::deallocate_block_a_lot_count() % MetaDataDeallocateALotInterval == 0 ) {
  1333     Metadebug::set_deallocate_block_a_lot_count(0);
  1334     for (uint i = 0; i < metadata_deallocate_a_lot_block; i++) {
  1335       MetaWord* dummy_block = sm->allocate_work(raw_word_size);
  1336       if (dummy_block == 0) {
  1337         break;
  1339       sm->deallocate(dummy_block, raw_word_size);
  1341   } else {
  1342     Metadebug::inc_deallocate_block_a_lot_count();
  1344 #endif
  1347 void Metadebug::init_allocation_fail_alot_count() {
  1348   if (MetadataAllocationFailALot) {
  1349     _allocation_fail_alot_count =
  1350       1+(long)((double)MetadataAllocationFailALotInterval*os::random()/(max_jint+1.0));
  1354 #ifdef ASSERT
  1355 bool Metadebug::test_metadata_failure() {
  1356   if (MetadataAllocationFailALot &&
  1357       Threads::is_vm_complete()) {
  1358     if (_allocation_fail_alot_count > 0) {
  1359       _allocation_fail_alot_count--;
  1360     } else {
  1361       if (TraceMetadataChunkAllocation && Verbose) {
  1362         gclog_or_tty->print_cr("Metadata allocation failing for "
  1363                                "MetadataAllocationFailALot");
  1365       init_allocation_fail_alot_count();
  1366       return true;
  1369   return false;
  1371 #endif
  1373 // ChunkManager methods
  1375 // Verification of _free_chunks_total and _free_chunks_count does not
  1376 // work with the CMS collector because its use of additional locks
  1377 // complicate the mutex deadlock detection but it can still be useful
  1378 // for detecting errors in the chunk accounting with other collectors.
  1380 size_t ChunkManager::free_chunks_total() {
  1381 #ifdef ASSERT
  1382   if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) {
  1383     MutexLockerEx cl(SpaceManager::expand_lock(),
  1384                      Mutex::_no_safepoint_check_flag);
  1385     slow_locked_verify_free_chunks_total();
  1387 #endif
  1388   return _free_chunks_total;
  1391 size_t ChunkManager::free_chunks_total_in_bytes() {
  1392   return free_chunks_total() * BytesPerWord;
  1395 size_t ChunkManager::free_chunks_count() {
  1396 #ifdef ASSERT
  1397   if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) {
  1398     MutexLockerEx cl(SpaceManager::expand_lock(),
  1399                      Mutex::_no_safepoint_check_flag);
  1400     // This lock is only needed in debug because the verification
  1401     // of the _free_chunks_totals walks the list of free chunks
  1402     slow_locked_verify_free_chunks_count();
  1404 #endif
  1405   return _free_chunks_count;
  1408 void ChunkManager::locked_verify_free_chunks_total() {
  1409   assert_lock_strong(SpaceManager::expand_lock());
  1410   assert(sum_free_chunks() == _free_chunks_total,
  1411     err_msg("_free_chunks_total " SIZE_FORMAT " is not the"
  1412            " same as sum " SIZE_FORMAT, _free_chunks_total,
  1413            sum_free_chunks()));
  1416 void ChunkManager::verify_free_chunks_total() {
  1417   MutexLockerEx cl(SpaceManager::expand_lock(),
  1418                      Mutex::_no_safepoint_check_flag);
  1419   locked_verify_free_chunks_total();
  1422 void ChunkManager::locked_verify_free_chunks_count() {
  1423   assert_lock_strong(SpaceManager::expand_lock());
  1424   assert(sum_free_chunks_count() == _free_chunks_count,
  1425     err_msg("_free_chunks_count " SIZE_FORMAT " is not the"
  1426            " same as sum " SIZE_FORMAT, _free_chunks_count,
  1427            sum_free_chunks_count()));
  1430 void ChunkManager::verify_free_chunks_count() {
  1431 #ifdef ASSERT
  1432   MutexLockerEx cl(SpaceManager::expand_lock(),
  1433                      Mutex::_no_safepoint_check_flag);
  1434   locked_verify_free_chunks_count();
  1435 #endif
  1438 void ChunkManager::verify() {
  1439   MutexLockerEx cl(SpaceManager::expand_lock(),
  1440                      Mutex::_no_safepoint_check_flag);
  1441   locked_verify();
  1444 void ChunkManager::locked_verify() {
  1445   locked_verify_free_chunks_count();
  1446   locked_verify_free_chunks_total();
  1449 void ChunkManager::locked_print_free_chunks(outputStream* st) {
  1450   assert_lock_strong(SpaceManager::expand_lock());
  1451   st->print_cr("Free chunk total " SIZE_FORMAT "  count " SIZE_FORMAT,
  1452                 _free_chunks_total, _free_chunks_count);
  1455 void ChunkManager::locked_print_sum_free_chunks(outputStream* st) {
  1456   assert_lock_strong(SpaceManager::expand_lock());
  1457   st->print_cr("Sum free chunk total " SIZE_FORMAT "  count " SIZE_FORMAT,
  1458                 sum_free_chunks(), sum_free_chunks_count());
  1460 ChunkList* ChunkManager::free_chunks(ChunkIndex index) {
  1461   return &_free_chunks[index];
  1464 // These methods that sum the free chunk lists are used in printing
  1465 // methods that are used in product builds.
  1466 size_t ChunkManager::sum_free_chunks() {
  1467   assert_lock_strong(SpaceManager::expand_lock());
  1468   size_t result = 0;
  1469   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
  1470     ChunkList* list = free_chunks(i);
  1472     if (list == NULL) {
  1473       continue;
  1476     result = result + list->count() * list->size();
  1478   result = result + humongous_dictionary()->total_size();
  1479   return result;
  1482 size_t ChunkManager::sum_free_chunks_count() {
  1483   assert_lock_strong(SpaceManager::expand_lock());
  1484   size_t count = 0;
  1485   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
  1486     ChunkList* list = free_chunks(i);
  1487     if (list == NULL) {
  1488       continue;
  1490     count = count + list->count();
  1492   count = count + humongous_dictionary()->total_free_blocks();
  1493   return count;
  1496 ChunkList* ChunkManager::find_free_chunks_list(size_t word_size) {
  1497   ChunkIndex index = list_index(word_size);
  1498   assert(index < HumongousIndex, "No humongous list");
  1499   return free_chunks(index);
  1502 void ChunkManager::free_chunks_put(Metachunk* chunk) {
  1503   assert_lock_strong(SpaceManager::expand_lock());
  1504   ChunkList* free_list = find_free_chunks_list(chunk->word_size());
  1505   chunk->set_next(free_list->head());
  1506   free_list->set_head(chunk);
  1507   // chunk is being returned to the chunk free list
  1508   inc_free_chunks_total(chunk->capacity_word_size());
  1509   slow_locked_verify();
  1512 void ChunkManager::chunk_freelist_deallocate(Metachunk* chunk) {
  1513   // The deallocation of a chunk originates in the freelist
  1514   // manangement code for a Metaspace and does not hold the
  1515   // lock.
  1516   assert(chunk != NULL, "Deallocating NULL");
  1517   assert_lock_strong(SpaceManager::expand_lock());
  1518   slow_locked_verify();
  1519   if (TraceMetadataChunkAllocation) {
  1520     tty->print_cr("ChunkManager::chunk_freelist_deallocate: chunk "
  1521                   PTR_FORMAT "  size " SIZE_FORMAT,
  1522                   chunk, chunk->word_size());
  1524   free_chunks_put(chunk);
  1527 Metachunk* ChunkManager::free_chunks_get(size_t word_size) {
  1528   assert_lock_strong(SpaceManager::expand_lock());
  1530   slow_locked_verify();
  1532   Metachunk* chunk = NULL;
  1533   if (list_index(word_size) != HumongousIndex) {
  1534     ChunkList* free_list = find_free_chunks_list(word_size);
  1535     assert(free_list != NULL, "Sanity check");
  1537     chunk = free_list->head();
  1538     debug_only(Metachunk* debug_head = chunk;)
  1540     if (chunk == NULL) {
  1541       return NULL;
  1544     // Remove the chunk as the head of the list.
  1545     free_list->remove_chunk(chunk);
  1547     // Chunk is being removed from the chunks free list.
  1548     dec_free_chunks_total(chunk->capacity_word_size());
  1550     if (TraceMetadataChunkAllocation && Verbose) {
  1551       tty->print_cr("ChunkManager::free_chunks_get: free_list "
  1552                     PTR_FORMAT " head " PTR_FORMAT " size " SIZE_FORMAT,
  1553                     free_list, chunk, chunk->word_size());
  1555   } else {
  1556     chunk = humongous_dictionary()->get_chunk(
  1557       word_size,
  1558       FreeBlockDictionary<Metachunk>::atLeast);
  1560     if (chunk != NULL) {
  1561       if (TraceMetadataHumongousAllocation) {
  1562         size_t waste = chunk->word_size() - word_size;
  1563         tty->print_cr("Free list allocate humongous chunk size " SIZE_FORMAT
  1564                       " for requested size " SIZE_FORMAT
  1565                       " waste " SIZE_FORMAT,
  1566                       chunk->word_size(), word_size, waste);
  1568       // Chunk is being removed from the chunks free list.
  1569       dec_free_chunks_total(chunk->capacity_word_size());
  1570 #ifdef ASSERT
  1571       chunk->set_is_free(false);
  1572 #endif
  1573     } else {
  1574       return NULL;
  1578   // Remove it from the links to this freelist
  1579   chunk->set_next(NULL);
  1580   chunk->set_prev(NULL);
  1581   slow_locked_verify();
  1582   return chunk;
  1585 Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) {
  1586   assert_lock_strong(SpaceManager::expand_lock());
  1587   slow_locked_verify();
  1589   // Take from the beginning of the list
  1590   Metachunk* chunk = free_chunks_get(word_size);
  1591   if (chunk == NULL) {
  1592     return NULL;
  1595   assert((word_size <= chunk->word_size()) ||
  1596          list_index(chunk->word_size() == HumongousIndex),
  1597          "Non-humongous variable sized chunk");
  1598   if (TraceMetadataChunkAllocation) {
  1599     size_t list_count;
  1600     if (list_index(word_size) < HumongousIndex) {
  1601       ChunkList* list = find_free_chunks_list(word_size);
  1602       list_count = list->count();
  1603     } else {
  1604       list_count = humongous_dictionary()->total_count();
  1606     tty->print("ChunkManager::chunk_freelist_allocate: " PTR_FORMAT " chunk "
  1607                PTR_FORMAT "  size " SIZE_FORMAT " count " SIZE_FORMAT " ",
  1608                this, chunk, chunk->word_size(), list_count);
  1609     locked_print_free_chunks(tty);
  1612   return chunk;
  1615 void ChunkManager::print_on(outputStream* out) {
  1616   if (PrintFLSStatistics != 0) {
  1617     humongous_dictionary()->report_statistics();
  1621 // SpaceManager methods
  1623 void SpaceManager::get_initial_chunk_sizes(Metaspace::MetaspaceType type,
  1624                                            size_t* chunk_word_size,
  1625                                            size_t* class_chunk_word_size) {
  1626   switch (type) {
  1627   case Metaspace::BootMetaspaceType:
  1628     *chunk_word_size = Metaspace::first_chunk_word_size();
  1629     *class_chunk_word_size = Metaspace::first_class_chunk_word_size();
  1630     break;
  1631   case Metaspace::ROMetaspaceType:
  1632     *chunk_word_size = SharedReadOnlySize / wordSize;
  1633     *class_chunk_word_size = ClassSpecializedChunk;
  1634     break;
  1635   case Metaspace::ReadWriteMetaspaceType:
  1636     *chunk_word_size = SharedReadWriteSize / wordSize;
  1637     *class_chunk_word_size = ClassSpecializedChunk;
  1638     break;
  1639   case Metaspace::AnonymousMetaspaceType:
  1640   case Metaspace::ReflectionMetaspaceType:
  1641     *chunk_word_size = SpecializedChunk;
  1642     *class_chunk_word_size = ClassSpecializedChunk;
  1643     break;
  1644   default:
  1645     *chunk_word_size = SmallChunk;
  1646     *class_chunk_word_size = ClassSmallChunk;
  1647     break;
  1649   assert(*chunk_word_size != 0 && *class_chunk_word_size != 0,
  1650     err_msg("Initial chunks sizes bad: data  " SIZE_FORMAT
  1651             " class " SIZE_FORMAT,
  1652             *chunk_word_size, *class_chunk_word_size));
  1655 size_t SpaceManager::sum_free_in_chunks_in_use() const {
  1656   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  1657   size_t free = 0;
  1658   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1659     Metachunk* chunk = chunks_in_use(i);
  1660     while (chunk != NULL) {
  1661       free += chunk->free_word_size();
  1662       chunk = chunk->next();
  1665   return free;
  1668 size_t SpaceManager::sum_waste_in_chunks_in_use() const {
  1669   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  1670   size_t result = 0;
  1671   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1672    result += sum_waste_in_chunks_in_use(i);
  1675   return result;
  1678 size_t SpaceManager::sum_waste_in_chunks_in_use(ChunkIndex index) const {
  1679   size_t result = 0;
  1680   Metachunk* chunk = chunks_in_use(index);
  1681   // Count the free space in all the chunk but not the
  1682   // current chunk from which allocations are still being done.
  1683   if (chunk != NULL) {
  1684     Metachunk* prev = chunk;
  1685     while (chunk != NULL && chunk != current_chunk()) {
  1686       result += chunk->free_word_size();
  1687       prev = chunk;
  1688       chunk = chunk->next();
  1691   return result;
  1694 size_t SpaceManager::sum_capacity_in_chunks_in_use() const {
  1695   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  1696   size_t sum = 0;
  1697   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1698     Metachunk* chunk = chunks_in_use(i);
  1699     while (chunk != NULL) {
  1700       // Just changed this sum += chunk->capacity_word_size();
  1701       // sum += chunk->word_size() - Metachunk::overhead();
  1702       sum += chunk->capacity_word_size();
  1703       chunk = chunk->next();
  1706   return sum;
  1709 size_t SpaceManager::sum_count_in_chunks_in_use() {
  1710   size_t count = 0;
  1711   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1712     count = count + sum_count_in_chunks_in_use(i);
  1715   return count;
  1718 size_t SpaceManager::sum_count_in_chunks_in_use(ChunkIndex i) {
  1719   size_t count = 0;
  1720   Metachunk* chunk = chunks_in_use(i);
  1721   while (chunk != NULL) {
  1722     count++;
  1723     chunk = chunk->next();
  1725   return count;
  1729 size_t SpaceManager::sum_used_in_chunks_in_use() const {
  1730   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  1731   size_t used = 0;
  1732   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1733     Metachunk* chunk = chunks_in_use(i);
  1734     while (chunk != NULL) {
  1735       used += chunk->used_word_size();
  1736       chunk = chunk->next();
  1739   return used;
  1742 void SpaceManager::locked_print_chunks_in_use_on(outputStream* st) const {
  1744   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1745     Metachunk* chunk = chunks_in_use(i);
  1746     st->print("SpaceManager: %s " PTR_FORMAT,
  1747                  chunk_size_name(i), chunk);
  1748     if (chunk != NULL) {
  1749       st->print_cr(" free " SIZE_FORMAT,
  1750                    chunk->free_word_size());
  1751     } else {
  1752       st->print_cr("");
  1756   vs_list()->chunk_manager()->locked_print_free_chunks(st);
  1757   vs_list()->chunk_manager()->locked_print_sum_free_chunks(st);
  1760 size_t SpaceManager::calc_chunk_size(size_t word_size) {
  1762   // Decide between a small chunk and a medium chunk.  Up to
  1763   // _small_chunk_limit small chunks can be allocated but
  1764   // once a medium chunk has been allocated, no more small
  1765   // chunks will be allocated.
  1766   size_t chunk_word_size;
  1767   if (chunks_in_use(MediumIndex) == NULL &&
  1768       (!has_small_chunk_limit() ||
  1769        sum_count_in_chunks_in_use(SmallIndex) < _small_chunk_limit)) {
  1770     chunk_word_size = (size_t) small_chunk_size();
  1771     if (word_size + Metachunk::overhead() > small_chunk_size()) {
  1772       chunk_word_size = medium_chunk_size();
  1774   } else {
  1775     chunk_word_size = medium_chunk_size();
  1778   // Might still need a humongous chunk.  Enforce an
  1779   // eight word granularity to facilitate reuse (some
  1780   // wastage but better chance of reuse).
  1781   size_t if_humongous_sized_chunk =
  1782     align_size_up(word_size + Metachunk::overhead(),
  1783                   HumongousChunkGranularity);
  1784   chunk_word_size =
  1785     MAX2((size_t) chunk_word_size, if_humongous_sized_chunk);
  1787   assert(!SpaceManager::is_humongous(word_size) ||
  1788          chunk_word_size == if_humongous_sized_chunk,
  1789          err_msg("Size calculation is wrong, word_size " SIZE_FORMAT
  1790                  " chunk_word_size " SIZE_FORMAT,
  1791                  word_size, chunk_word_size));
  1792   if (TraceMetadataHumongousAllocation &&
  1793       SpaceManager::is_humongous(word_size)) {
  1794     gclog_or_tty->print_cr("Metadata humongous allocation:");
  1795     gclog_or_tty->print_cr("  word_size " PTR_FORMAT, word_size);
  1796     gclog_or_tty->print_cr("  chunk_word_size " PTR_FORMAT,
  1797                            chunk_word_size);
  1798     gclog_or_tty->print_cr("    chunk overhead " PTR_FORMAT,
  1799                            Metachunk::overhead());
  1801   return chunk_word_size;
  1804 MetaWord* SpaceManager::grow_and_allocate(size_t word_size) {
  1805   assert(vs_list()->current_virtual_space() != NULL,
  1806          "Should have been set");
  1807   assert(current_chunk() == NULL ||
  1808          current_chunk()->allocate(word_size) == NULL,
  1809          "Don't need to expand");
  1810   MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
  1812   if (TraceMetadataChunkAllocation && Verbose) {
  1813     size_t words_left = 0;
  1814     size_t words_used = 0;
  1815     if (current_chunk() != NULL) {
  1816       words_left = current_chunk()->free_word_size();
  1817       words_used = current_chunk()->used_word_size();
  1819     gclog_or_tty->print_cr("SpaceManager::grow_and_allocate for " SIZE_FORMAT
  1820                            " words " SIZE_FORMAT " words used " SIZE_FORMAT
  1821                            " words left",
  1822                             word_size, words_used, words_left);
  1825   // Get another chunk out of the virtual space
  1826   size_t grow_chunks_by_words = calc_chunk_size(word_size);
  1827   Metachunk* next = get_new_chunk(word_size, grow_chunks_by_words);
  1829   // If a chunk was available, add it to the in-use chunk list
  1830   // and do an allocation from it.
  1831   if (next != NULL) {
  1832     Metadebug::deallocate_chunk_a_lot(this, grow_chunks_by_words);
  1833     // Add to this manager's list of chunks in use.
  1834     add_chunk(next, false);
  1835     return next->allocate(word_size);
  1837   return NULL;
  1840 void SpaceManager::print_on(outputStream* st) const {
  1842   for (ChunkIndex i = ZeroIndex;
  1843        i < NumberOfInUseLists ;
  1844        i = next_chunk_index(i) ) {
  1845     st->print_cr("  chunks_in_use " PTR_FORMAT " chunk size " PTR_FORMAT,
  1846                  chunks_in_use(i),
  1847                  chunks_in_use(i) == NULL ? 0 : chunks_in_use(i)->word_size());
  1849   st->print_cr("    waste:  Small " SIZE_FORMAT " Medium " SIZE_FORMAT
  1850                " Humongous " SIZE_FORMAT,
  1851                sum_waste_in_chunks_in_use(SmallIndex),
  1852                sum_waste_in_chunks_in_use(MediumIndex),
  1853                sum_waste_in_chunks_in_use(HumongousIndex));
  1854   // block free lists
  1855   if (block_freelists() != NULL) {
  1856     st->print_cr("total in block free lists " SIZE_FORMAT,
  1857       block_freelists()->total_size());
  1861 SpaceManager::SpaceManager(Mutex* lock,
  1862                            VirtualSpaceList* vs_list) :
  1863   _vs_list(vs_list),
  1864   _allocation_total(0),
  1865   _lock(lock)
  1867   initialize();
  1870 void SpaceManager::initialize() {
  1871   Metadebug::init_allocation_fail_alot_count();
  1872   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  1873     _chunks_in_use[i] = NULL;
  1875   _current_chunk = NULL;
  1876   if (TraceMetadataChunkAllocation && Verbose) {
  1877     gclog_or_tty->print_cr("SpaceManager(): " PTR_FORMAT, this);
  1881 void ChunkManager::return_chunks(ChunkIndex index, Metachunk* chunks) {
  1882   if (chunks == NULL) {
  1883     return;
  1885   ChunkList* list = free_chunks(index);
  1886   assert(list->size() == chunks->word_size(), "Mismatch in chunk sizes");
  1887   assert_lock_strong(SpaceManager::expand_lock());
  1888   Metachunk* cur = chunks;
  1890   // This return chunks one at a time.  If a new
  1891   // class List can be created that is a base class
  1892   // of FreeList then something like FreeList::prepend()
  1893   // can be used in place of this loop
  1894   while (cur != NULL) {
  1895     // Capture the next link before it is changed
  1896     // by the call to return_chunk_at_head();
  1897     Metachunk* next = cur->next();
  1898     cur->set_is_free(true);
  1899     list->return_chunk_at_head(cur);
  1900     cur = next;
  1904 SpaceManager::~SpaceManager() {
  1905   // This call this->_lock which can't be done while holding expand_lock()
  1906   const size_t in_use_before = sum_capacity_in_chunks_in_use();
  1908   MutexLockerEx fcl(SpaceManager::expand_lock(),
  1909                     Mutex::_no_safepoint_check_flag);
  1911   ChunkManager* chunk_manager = vs_list()->chunk_manager();
  1913   chunk_manager->slow_locked_verify();
  1915   if (TraceMetadataChunkAllocation && Verbose) {
  1916     gclog_or_tty->print_cr("~SpaceManager(): " PTR_FORMAT, this);
  1917     locked_print_chunks_in_use_on(gclog_or_tty);
  1920   // Mangle freed memory.
  1921   NOT_PRODUCT(mangle_freed_chunks();)
  1923   // Have to update before the chunks_in_use lists are emptied
  1924   // below.
  1925   chunk_manager->inc_free_chunks_total(in_use_before,
  1926                                        sum_count_in_chunks_in_use());
  1928   // Add all the chunks in use by this space manager
  1929   // to the global list of free chunks.
  1931   // Follow each list of chunks-in-use and add them to the
  1932   // free lists.  Each list is NULL terminated.
  1934   for (ChunkIndex i = ZeroIndex; i < HumongousIndex; i = next_chunk_index(i)) {
  1935     if (TraceMetadataChunkAllocation && Verbose) {
  1936       gclog_or_tty->print_cr("returned %d %s chunks to freelist",
  1937                              sum_count_in_chunks_in_use(i),
  1938                              chunk_size_name(i));
  1940     Metachunk* chunks = chunks_in_use(i);
  1941     chunk_manager->return_chunks(i, chunks);
  1942     set_chunks_in_use(i, NULL);
  1943     if (TraceMetadataChunkAllocation && Verbose) {
  1944       gclog_or_tty->print_cr("updated freelist count %d %s",
  1945                              chunk_manager->free_chunks(i)->count(),
  1946                              chunk_size_name(i));
  1948     assert(i != HumongousIndex, "Humongous chunks are handled explicitly later");
  1951   // The medium chunk case may be optimized by passing the head and
  1952   // tail of the medium chunk list to add_at_head().  The tail is often
  1953   // the current chunk but there are probably exceptions.
  1955   // Humongous chunks
  1956   if (TraceMetadataChunkAllocation && Verbose) {
  1957     gclog_or_tty->print_cr("returned %d %s humongous chunks to dictionary",
  1958                             sum_count_in_chunks_in_use(HumongousIndex),
  1959                             chunk_size_name(HumongousIndex));
  1960     gclog_or_tty->print("Humongous chunk dictionary: ");
  1962   // Humongous chunks are never the current chunk.
  1963   Metachunk* humongous_chunks = chunks_in_use(HumongousIndex);
  1965   while (humongous_chunks != NULL) {
  1966 #ifdef ASSERT
  1967     humongous_chunks->set_is_free(true);
  1968 #endif
  1969     if (TraceMetadataChunkAllocation && Verbose) {
  1970       gclog_or_tty->print(PTR_FORMAT " (" SIZE_FORMAT ") ",
  1971                           humongous_chunks,
  1972                           humongous_chunks->word_size());
  1974     assert(humongous_chunks->word_size() == (size_t)
  1975            align_size_up(humongous_chunks->word_size(),
  1976                              HumongousChunkGranularity),
  1977            err_msg("Humongous chunk size is wrong: word size " SIZE_FORMAT
  1978                    " granularity %d",
  1979                    humongous_chunks->word_size(), HumongousChunkGranularity));
  1980     Metachunk* next_humongous_chunks = humongous_chunks->next();
  1981     chunk_manager->humongous_dictionary()->return_chunk(humongous_chunks);
  1982     humongous_chunks = next_humongous_chunks;
  1984   if (TraceMetadataChunkAllocation && Verbose) {
  1985     gclog_or_tty->print_cr("");
  1986     gclog_or_tty->print_cr("updated dictionary count %d %s",
  1987                      chunk_manager->humongous_dictionary()->total_count(),
  1988                      chunk_size_name(HumongousIndex));
  1990   set_chunks_in_use(HumongousIndex, NULL);
  1991   chunk_manager->slow_locked_verify();
  1994 const char* SpaceManager::chunk_size_name(ChunkIndex index) const {
  1995   switch (index) {
  1996     case SpecializedIndex:
  1997       return "Specialized";
  1998     case SmallIndex:
  1999       return "Small";
  2000     case MediumIndex:
  2001       return "Medium";
  2002     case HumongousIndex:
  2003       return "Humongous";
  2004     default:
  2005       return NULL;
  2009 ChunkIndex ChunkManager::list_index(size_t size) {
  2010   switch (size) {
  2011     case SpecializedChunk:
  2012       assert(SpecializedChunk == ClassSpecializedChunk,
  2013              "Need branch for ClassSpecializedChunk");
  2014       return SpecializedIndex;
  2015     case SmallChunk:
  2016     case ClassSmallChunk:
  2017       return SmallIndex;
  2018     case MediumChunk:
  2019     case ClassMediumChunk:
  2020       return MediumIndex;
  2021     default:
  2022       assert(size > MediumChunk || size > ClassMediumChunk,
  2023              "Not a humongous chunk");
  2024       return HumongousIndex;
  2028 void SpaceManager::deallocate(MetaWord* p, size_t word_size) {
  2029   assert_lock_strong(_lock);
  2030   size_t min_size = TreeChunk<Metablock, FreeList>::min_size();
  2031   assert(word_size >= min_size,
  2032     err_msg("Should not deallocate dark matter " SIZE_FORMAT, word_size));
  2033   block_freelists()->return_block(p, word_size);
  2036 // Adds a chunk to the list of chunks in use.
  2037 void SpaceManager::add_chunk(Metachunk* new_chunk, bool make_current) {
  2039   assert(new_chunk != NULL, "Should not be NULL");
  2040   assert(new_chunk->next() == NULL, "Should not be on a list");
  2042   new_chunk->reset_empty();
  2044   // Find the correct list and and set the current
  2045   // chunk for that list.
  2046   ChunkIndex index = ChunkManager::list_index(new_chunk->word_size());
  2048   if (index != HumongousIndex) {
  2049     set_current_chunk(new_chunk);
  2050     new_chunk->set_next(chunks_in_use(index));
  2051     set_chunks_in_use(index, new_chunk);
  2052   } else {
  2053     // For null class loader data and DumpSharedSpaces, the first chunk isn't
  2054     // small, so small will be null.  Link this first chunk as the current
  2055     // chunk.
  2056     if (make_current) {
  2057       // Set as the current chunk but otherwise treat as a humongous chunk.
  2058       set_current_chunk(new_chunk);
  2060     // Link at head.  The _current_chunk only points to a humongous chunk for
  2061     // the null class loader metaspace (class and data virtual space managers)
  2062     // any humongous chunks so will not point to the tail
  2063     // of the humongous chunks list.
  2064     new_chunk->set_next(chunks_in_use(HumongousIndex));
  2065     set_chunks_in_use(HumongousIndex, new_chunk);
  2067     assert(new_chunk->word_size() > medium_chunk_size(), "List inconsistency");
  2070   assert(new_chunk->is_empty(), "Not ready for reuse");
  2071   if (TraceMetadataChunkAllocation && Verbose) {
  2072     gclog_or_tty->print("SpaceManager::add_chunk: %d) ",
  2073                         sum_count_in_chunks_in_use());
  2074     new_chunk->print_on(gclog_or_tty);
  2075     vs_list()->chunk_manager()->locked_print_free_chunks(tty);
  2079 Metachunk* SpaceManager::get_new_chunk(size_t word_size,
  2080                                        size_t grow_chunks_by_words) {
  2082   Metachunk* next = vs_list()->get_new_chunk(word_size,
  2083                                              grow_chunks_by_words,
  2084                                              medium_chunk_bunch());
  2086   if (TraceMetadataHumongousAllocation &&
  2087       SpaceManager::is_humongous(next->word_size())) {
  2088     gclog_or_tty->print_cr("  new humongous chunk word size " PTR_FORMAT,
  2089                            next->word_size());
  2092   return next;
  2095 MetaWord* SpaceManager::allocate(size_t word_size) {
  2096   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  2098   // If only the dictionary is going to be used (i.e., no
  2099   // indexed free list), then there is a minimum size requirement.
  2100   // MinChunkSize is a placeholder for the real minimum size JJJ
  2101   size_t byte_size = word_size * BytesPerWord;
  2103   size_t byte_size_with_overhead = byte_size + Metablock::overhead();
  2105   size_t raw_bytes_size = MAX2(byte_size_with_overhead,
  2106                                Metablock::min_block_byte_size());
  2107   raw_bytes_size = ARENA_ALIGN(raw_bytes_size);
  2108   size_t raw_word_size = raw_bytes_size / BytesPerWord;
  2109   assert(raw_word_size * BytesPerWord == raw_bytes_size, "Size problem");
  2111   BlockFreelist* fl =  block_freelists();
  2112   MetaWord* p = NULL;
  2113   // Allocation from the dictionary is expensive in the sense that
  2114   // the dictionary has to be searched for a size.  Don't allocate
  2115   // from the dictionary until it starts to get fat.  Is this
  2116   // a reasonable policy?  Maybe an skinny dictionary is fast enough
  2117   // for allocations.  Do some profiling.  JJJ
  2118   if (fl->total_size() > allocation_from_dictionary_limit) {
  2119     p = fl->get_block(raw_word_size);
  2121   if (p == NULL) {
  2122     p = allocate_work(raw_word_size);
  2124   Metadebug::deallocate_block_a_lot(this, raw_word_size);
  2126   return p;
  2129 // Returns the address of spaced allocated for "word_size".
  2130 // This methods does not know about blocks (Metablocks)
  2131 MetaWord* SpaceManager::allocate_work(size_t word_size) {
  2132   assert_lock_strong(_lock);
  2133 #ifdef ASSERT
  2134   if (Metadebug::test_metadata_failure()) {
  2135     return NULL;
  2137 #endif
  2138   // Is there space in the current chunk?
  2139   MetaWord* result = NULL;
  2141   // For DumpSharedSpaces, only allocate out of the current chunk which is
  2142   // never null because we gave it the size we wanted.   Caller reports out
  2143   // of memory if this returns null.
  2144   if (DumpSharedSpaces) {
  2145     assert(current_chunk() != NULL, "should never happen");
  2146     inc_allocation_total(word_size);
  2147     return current_chunk()->allocate(word_size); // caller handles null result
  2149   if (current_chunk() != NULL) {
  2150     result = current_chunk()->allocate(word_size);
  2153   if (result == NULL) {
  2154     result = grow_and_allocate(word_size);
  2156   if (result > 0) {
  2157     inc_allocation_total(word_size);
  2158     assert(result != (MetaWord*) chunks_in_use(MediumIndex),
  2159            "Head of the list is being allocated");
  2162   return result;
  2165 void SpaceManager::verify() {
  2166   // If there are blocks in the dictionary, then
  2167   // verfication of chunks does not work since
  2168   // being in the dictionary alters a chunk.
  2169   if (block_freelists()->total_size() == 0) {
  2170     for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
  2171       Metachunk* curr = chunks_in_use(i);
  2172       while (curr != NULL) {
  2173         curr->verify();
  2174         verify_chunk_size(curr);
  2175         curr = curr->next();
  2181 void SpaceManager::verify_chunk_size(Metachunk* chunk) {
  2182   assert(is_humongous(chunk->word_size()) ||
  2183          chunk->word_size() == medium_chunk_size() ||
  2184          chunk->word_size() == small_chunk_size() ||
  2185          chunk->word_size() == specialized_chunk_size(),
  2186          "Chunk size is wrong");
  2187   return;
  2190 #ifdef ASSERT
  2191 void SpaceManager::verify_allocation_total() {
  2192   // Verification is only guaranteed at a safepoint.
  2193   if (SafepointSynchronize::is_at_safepoint()) {
  2194     gclog_or_tty->print_cr("Chunk " PTR_FORMAT " allocation_total " SIZE_FORMAT
  2195                            " sum_used_in_chunks_in_use " SIZE_FORMAT,
  2196                            this,
  2197                            allocation_total(),
  2198                            sum_used_in_chunks_in_use());
  2200   MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
  2201   assert(allocation_total() == sum_used_in_chunks_in_use(),
  2202     err_msg("allocation total is not consistent " SIZE_FORMAT
  2203             " vs " SIZE_FORMAT,
  2204             allocation_total(), sum_used_in_chunks_in_use()));
  2207 #endif
  2209 void SpaceManager::dump(outputStream* const out) const {
  2210   size_t curr_total = 0;
  2211   size_t waste = 0;
  2212   uint i = 0;
  2213   size_t used = 0;
  2214   size_t capacity = 0;
  2216   // Add up statistics for all chunks in this SpaceManager.
  2217   for (ChunkIndex index = ZeroIndex;
  2218        index < NumberOfInUseLists;
  2219        index = next_chunk_index(index)) {
  2220     for (Metachunk* curr = chunks_in_use(index);
  2221          curr != NULL;
  2222          curr = curr->next()) {
  2223       out->print("%d) ", i++);
  2224       curr->print_on(out);
  2225       if (TraceMetadataChunkAllocation && Verbose) {
  2226         block_freelists()->print_on(out);
  2228       curr_total += curr->word_size();
  2229       used += curr->used_word_size();
  2230       capacity += curr->capacity_word_size();
  2231       waste += curr->free_word_size() + curr->overhead();;
  2235   size_t free = current_chunk() == NULL ? 0 : current_chunk()->free_word_size();
  2236   // Free space isn't wasted.
  2237   waste -= free;
  2239   out->print_cr("total of all chunks "  SIZE_FORMAT " used " SIZE_FORMAT
  2240                 " free " SIZE_FORMAT " capacity " SIZE_FORMAT
  2241                 " waste " SIZE_FORMAT, curr_total, used, free, capacity, waste);
  2244 #ifndef PRODUCT
  2245 void SpaceManager::mangle_freed_chunks() {
  2246   for (ChunkIndex index = ZeroIndex;
  2247        index < NumberOfInUseLists;
  2248        index = next_chunk_index(index)) {
  2249     for (Metachunk* curr = chunks_in_use(index);
  2250          curr != NULL;
  2251          curr = curr->next()) {
  2252       curr->mangle();
  2256 #endif // PRODUCT
  2258 // MetaspaceAux
  2260 size_t MetaspaceAux::used_in_bytes(Metaspace::MetadataType mdtype) {
  2261   size_t used = 0;
  2262   ClassLoaderDataGraphMetaspaceIterator iter;
  2263   while (iter.repeat()) {
  2264     Metaspace* msp = iter.get_next();
  2265     // Sum allocation_total for each metaspace
  2266     if (msp != NULL) {
  2267       used += msp->used_words(mdtype);
  2270   return used * BytesPerWord;
  2273 size_t MetaspaceAux::free_in_bytes(Metaspace::MetadataType mdtype) {
  2274   size_t free = 0;
  2275   ClassLoaderDataGraphMetaspaceIterator iter;
  2276   while (iter.repeat()) {
  2277     Metaspace* msp = iter.get_next();
  2278     if (msp != NULL) {
  2279       free += msp->free_words(mdtype);
  2282   return free * BytesPerWord;
  2285 size_t MetaspaceAux::capacity_in_bytes(Metaspace::MetadataType mdtype) {
  2286   size_t capacity = free_chunks_total(mdtype);
  2287   ClassLoaderDataGraphMetaspaceIterator iter;
  2288   while (iter.repeat()) {
  2289     Metaspace* msp = iter.get_next();
  2290     if (msp != NULL) {
  2291       capacity += msp->capacity_words(mdtype);
  2294   return capacity * BytesPerWord;
  2297 size_t MetaspaceAux::reserved_in_bytes(Metaspace::MetadataType mdtype) {
  2298   size_t reserved = (mdtype == Metaspace::ClassType) ?
  2299                        Metaspace::class_space_list()->virtual_space_total() :
  2300                        Metaspace::space_list()->virtual_space_total();
  2301   return reserved * BytesPerWord;
  2304 size_t MetaspaceAux::min_chunk_size() { return Metaspace::first_chunk_word_size(); }
  2306 size_t MetaspaceAux::free_chunks_total(Metaspace::MetadataType mdtype) {
  2307   ChunkManager* chunk = (mdtype == Metaspace::ClassType) ?
  2308                             Metaspace::class_space_list()->chunk_manager() :
  2309                             Metaspace::space_list()->chunk_manager();
  2310   chunk->slow_verify();
  2311   return chunk->free_chunks_total();
  2314 size_t MetaspaceAux::free_chunks_total_in_bytes(Metaspace::MetadataType mdtype) {
  2315   return free_chunks_total(mdtype) * BytesPerWord;
  2318 void MetaspaceAux::print_metaspace_change(size_t prev_metadata_used) {
  2319   gclog_or_tty->print(", [Metaspace:");
  2320   if (PrintGCDetails && Verbose) {
  2321     gclog_or_tty->print(" "  SIZE_FORMAT
  2322                         "->" SIZE_FORMAT
  2323                         "("  SIZE_FORMAT "/" SIZE_FORMAT ")",
  2324                         prev_metadata_used,
  2325                         used_in_bytes(),
  2326                         capacity_in_bytes(),
  2327                         reserved_in_bytes());
  2328   } else {
  2329     gclog_or_tty->print(" "  SIZE_FORMAT "K"
  2330                         "->" SIZE_FORMAT "K"
  2331                         "("  SIZE_FORMAT "K/" SIZE_FORMAT "K)",
  2332                         prev_metadata_used / K,
  2333                         used_in_bytes()/ K,
  2334                         capacity_in_bytes()/K,
  2335                         reserved_in_bytes()/ K);
  2338   gclog_or_tty->print("]");
  2341 // This is printed when PrintGCDetails
  2342 void MetaspaceAux::print_on(outputStream* out) {
  2343   Metaspace::MetadataType ct = Metaspace::ClassType;
  2344   Metaspace::MetadataType nct = Metaspace::NonClassType;
  2346   out->print_cr(" Metaspace total "
  2347                 SIZE_FORMAT "K, used " SIZE_FORMAT "K,"
  2348                 " reserved " SIZE_FORMAT "K",
  2349                 capacity_in_bytes()/K, used_in_bytes()/K, reserved_in_bytes()/K);
  2350   out->print_cr("  data space     "
  2351                 SIZE_FORMAT "K, used " SIZE_FORMAT "K,"
  2352                 " reserved " SIZE_FORMAT "K",
  2353                 capacity_in_bytes(nct)/K, used_in_bytes(nct)/K, reserved_in_bytes(nct)/K);
  2354   out->print_cr("  class space    "
  2355                 SIZE_FORMAT "K, used " SIZE_FORMAT "K,"
  2356                 " reserved " SIZE_FORMAT "K",
  2357                 capacity_in_bytes(ct)/K, used_in_bytes(ct)/K, reserved_in_bytes(ct)/K);
  2360 // Print information for class space and data space separately.
  2361 // This is almost the same as above.
  2362 void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) {
  2363   size_t free_chunks_capacity_bytes = free_chunks_total_in_bytes(mdtype);
  2364   size_t capacity_bytes = capacity_in_bytes(mdtype);
  2365   size_t used_bytes = used_in_bytes(mdtype);
  2366   size_t free_bytes = free_in_bytes(mdtype);
  2367   size_t used_and_free = used_bytes + free_bytes +
  2368                            free_chunks_capacity_bytes;
  2369   out->print_cr("  Chunk accounting: used in chunks " SIZE_FORMAT
  2370              "K + unused in chunks " SIZE_FORMAT "K  + "
  2371              " capacity in free chunks " SIZE_FORMAT "K = " SIZE_FORMAT
  2372              "K  capacity in allocated chunks " SIZE_FORMAT "K",
  2373              used_bytes / K,
  2374              free_bytes / K,
  2375              free_chunks_capacity_bytes / K,
  2376              used_and_free / K,
  2377              capacity_bytes / K);
  2378   // Accounting can only be correct if we got the values during a safepoint
  2379   assert(!SafepointSynchronize::is_at_safepoint() || used_and_free == capacity_bytes, "Accounting is wrong");
  2382 // Print total fragmentation for class and data metaspaces separately
  2383 void MetaspaceAux::print_waste(outputStream* out) {
  2385   size_t specialized_waste = 0, small_waste = 0, medium_waste = 0, large_waste = 0;
  2386   size_t specialized_count = 0, small_count = 0, medium_count = 0, large_count = 0;
  2387   size_t cls_specialized_waste = 0, cls_small_waste = 0, cls_medium_waste = 0, cls_large_waste = 0;
  2388   size_t cls_specialized_count = 0, cls_small_count = 0, cls_medium_count = 0, cls_large_count = 0;
  2390   ClassLoaderDataGraphMetaspaceIterator iter;
  2391   while (iter.repeat()) {
  2392     Metaspace* msp = iter.get_next();
  2393     if (msp != NULL) {
  2394       specialized_waste += msp->vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
  2395       specialized_count += msp->vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
  2396       small_waste += msp->vsm()->sum_waste_in_chunks_in_use(SmallIndex);
  2397       small_count += msp->vsm()->sum_count_in_chunks_in_use(SmallIndex);
  2398       medium_waste += msp->vsm()->sum_waste_in_chunks_in_use(MediumIndex);
  2399       medium_count += msp->vsm()->sum_count_in_chunks_in_use(MediumIndex);
  2400       large_waste += msp->vsm()->sum_waste_in_chunks_in_use(HumongousIndex);
  2401       large_count += msp->vsm()->sum_count_in_chunks_in_use(HumongousIndex);
  2403       cls_specialized_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
  2404       cls_specialized_count += msp->class_vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
  2405       cls_small_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SmallIndex);
  2406       cls_small_count += msp->class_vsm()->sum_count_in_chunks_in_use(SmallIndex);
  2407       cls_medium_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(MediumIndex);
  2408       cls_medium_count += msp->class_vsm()->sum_count_in_chunks_in_use(MediumIndex);
  2409       cls_large_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(HumongousIndex);
  2410       cls_large_count += msp->class_vsm()->sum_count_in_chunks_in_use(HumongousIndex);
  2413   out->print_cr("Total fragmentation waste (words) doesn't count free space");
  2414   out->print_cr("  data: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
  2415                         SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
  2416                         SIZE_FORMAT " medium(s) " SIZE_FORMAT,
  2417              specialized_count, specialized_waste, small_count,
  2418              small_waste, medium_count, medium_waste);
  2419   out->print_cr(" class: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
  2420                            SIZE_FORMAT " small(s) " SIZE_FORMAT,
  2421              cls_specialized_count, cls_specialized_waste,
  2422              cls_small_count, cls_small_waste);
  2425 // Dump global metaspace things from the end of ClassLoaderDataGraph
  2426 void MetaspaceAux::dump(outputStream* out) {
  2427   out->print_cr("All Metaspace:");
  2428   out->print("data space: "); print_on(out, Metaspace::NonClassType);
  2429   out->print("class space: "); print_on(out, Metaspace::ClassType);
  2430   print_waste(out);
  2433 void MetaspaceAux::verify_free_chunks() {
  2434   Metaspace::space_list()->chunk_manager()->verify();
  2435   Metaspace::class_space_list()->chunk_manager()->verify();
  2438 // Metaspace methods
  2440 size_t Metaspace::_first_chunk_word_size = 0;
  2441 size_t Metaspace::_first_class_chunk_word_size = 0;
  2443 Metaspace::Metaspace(Mutex* lock, MetaspaceType type) {
  2444   initialize(lock, type);
  2447 Metaspace::~Metaspace() {
  2448   delete _vsm;
  2449   delete _class_vsm;
  2452 VirtualSpaceList* Metaspace::_space_list = NULL;
  2453 VirtualSpaceList* Metaspace::_class_space_list = NULL;
  2455 #define VIRTUALSPACEMULTIPLIER 2
  2457 void Metaspace::global_initialize() {
  2458   // Initialize the alignment for shared spaces.
  2459   int max_alignment = os::vm_page_size();
  2460   MetaspaceShared::set_max_alignment(max_alignment);
  2462   if (DumpSharedSpaces) {
  2463     SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment);
  2464     SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment);
  2465     SharedMiscDataSize  = align_size_up(SharedMiscDataSize, max_alignment);
  2466     SharedMiscCodeSize  = align_size_up(SharedMiscCodeSize, max_alignment);
  2468     // Initialize with the sum of the shared space sizes.  The read-only
  2469     // and read write metaspace chunks will be allocated out of this and the
  2470     // remainder is the misc code and data chunks.
  2471     size_t total = align_size_up(SharedReadOnlySize + SharedReadWriteSize +
  2472                                  SharedMiscDataSize + SharedMiscCodeSize,
  2473                                  os::vm_allocation_granularity());
  2474     size_t word_size = total/wordSize;
  2475     _space_list = new VirtualSpaceList(word_size);
  2476   } else {
  2477     // If using shared space, open the file that contains the shared space
  2478     // and map in the memory before initializing the rest of metaspace (so
  2479     // the addresses don't conflict)
  2480     if (UseSharedSpaces) {
  2481       FileMapInfo* mapinfo = new FileMapInfo();
  2482       memset(mapinfo, 0, sizeof(FileMapInfo));
  2484       // Open the shared archive file, read and validate the header. If
  2485       // initialization fails, shared spaces [UseSharedSpaces] are
  2486       // disabled and the file is closed.
  2487       // Map in spaces now also
  2488       if (mapinfo->initialize() && MetaspaceShared::map_shared_spaces(mapinfo)) {
  2489         FileMapInfo::set_current_info(mapinfo);
  2490       } else {
  2491         assert(!mapinfo->is_open() && !UseSharedSpaces,
  2492                "archive file not closed or shared spaces not disabled.");
  2496     // Initialize these before initializing the VirtualSpaceList
  2497     _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord;
  2498     _first_chunk_word_size = align_word_size_up(_first_chunk_word_size);
  2499     // Make the first class chunk bigger than a medium chunk so it's not put
  2500     // on the medium chunk list.   The next chunk will be small and progress
  2501     // from there.  This size calculated by -version.
  2502     _first_class_chunk_word_size = MIN2((size_t)MediumChunk*6,
  2503                                        (ClassMetaspaceSize/BytesPerWord)*2);
  2504     _first_class_chunk_word_size = align_word_size_up(_first_class_chunk_word_size);
  2505     // Arbitrarily set the initial virtual space to a multiple
  2506     // of the boot class loader size.
  2507     size_t word_size = VIRTUALSPACEMULTIPLIER * first_chunk_word_size();
  2508     // Initialize the list of virtual spaces.
  2509     _space_list = new VirtualSpaceList(word_size);
  2513 // For UseCompressedKlassPointers the class space is reserved as a piece of the
  2514 // Java heap because the compression algorithm is the same for each.  The
  2515 // argument passed in is at the top of the compressed space
  2516 void Metaspace::initialize_class_space(ReservedSpace rs) {
  2517   // The reserved space size may be bigger because of alignment, esp with UseLargePages
  2518   assert(rs.size() >= ClassMetaspaceSize,
  2519          err_msg(SIZE_FORMAT " != " UINTX_FORMAT, rs.size(), ClassMetaspaceSize));
  2520   _class_space_list = new VirtualSpaceList(rs);
  2523 void Metaspace::initialize(Mutex* lock,
  2524                            MetaspaceType type) {
  2526   assert(space_list() != NULL,
  2527     "Metadata VirtualSpaceList has not been initialized");
  2529   _vsm = new SpaceManager(lock, space_list());
  2530   if (_vsm == NULL) {
  2531     return;
  2533   size_t word_size;
  2534   size_t class_word_size;
  2535   vsm()->get_initial_chunk_sizes(type,
  2536                                  &word_size,
  2537                                  &class_word_size);
  2539   assert(class_space_list() != NULL,
  2540     "Class VirtualSpaceList has not been initialized");
  2542   // Allocate SpaceManager for classes.
  2543   _class_vsm = new SpaceManager(lock, class_space_list());
  2544   if (_class_vsm == NULL) {
  2545     return;
  2548   MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
  2550   // Allocate chunk for metadata objects
  2551   Metachunk* new_chunk =
  2552      space_list()->get_initialization_chunk(word_size,
  2553                                             vsm()->medium_chunk_bunch());
  2554   assert(!DumpSharedSpaces || new_chunk != NULL, "should have enough space for both chunks");
  2555   if (new_chunk != NULL) {
  2556     // Add to this manager's list of chunks in use and current_chunk().
  2557     vsm()->add_chunk(new_chunk, true);
  2560   // Allocate chunk for class metadata objects
  2561   Metachunk* class_chunk =
  2562      class_space_list()->get_initialization_chunk(class_word_size,
  2563                                                   class_vsm()->medium_chunk_bunch());
  2564   if (class_chunk != NULL) {
  2565     class_vsm()->add_chunk(class_chunk, true);
  2569 size_t Metaspace::align_word_size_up(size_t word_size) {
  2570   size_t byte_size = word_size * wordSize;
  2571   return ReservedSpace::allocation_align_size_up(byte_size) / wordSize;
  2574 MetaWord* Metaspace::allocate(size_t word_size, MetadataType mdtype) {
  2575   // DumpSharedSpaces doesn't use class metadata area (yet)
  2576   if (mdtype == ClassType && !DumpSharedSpaces) {
  2577     return  class_vsm()->allocate(word_size);
  2578   } else {
  2579     return  vsm()->allocate(word_size);
  2583 MetaWord* Metaspace::expand_and_allocate(size_t word_size, MetadataType mdtype) {
  2584   MetaWord* result;
  2585   MetaspaceGC::set_expand_after_GC(true);
  2586   size_t before_inc = MetaspaceGC::capacity_until_GC();
  2587   size_t delta_words = MetaspaceGC::delta_capacity_until_GC(word_size);
  2588   MetaspaceGC::inc_capacity_until_GC(delta_words);
  2589   if (PrintGCDetails && Verbose) {
  2590     gclog_or_tty->print_cr("Increase capacity to GC from " SIZE_FORMAT
  2591       " to " SIZE_FORMAT, before_inc, MetaspaceGC::capacity_until_GC());
  2594   result = allocate(word_size, mdtype);
  2596   return result;
  2599 // Space allocated in the Metaspace.  This may
  2600 // be across several metadata virtual spaces.
  2601 char* Metaspace::bottom() const {
  2602   assert(DumpSharedSpaces, "only useful and valid for dumping shared spaces");
  2603   return (char*)vsm()->current_chunk()->bottom();
  2606 size_t Metaspace::used_words(MetadataType mdtype) const {
  2607   // return vsm()->allocation_total();
  2608   return mdtype == ClassType ? class_vsm()->sum_used_in_chunks_in_use() :
  2609                                vsm()->sum_used_in_chunks_in_use();  // includes overhead!
  2612 size_t Metaspace::free_words(MetadataType mdtype) const {
  2613   return mdtype == ClassType ? class_vsm()->sum_free_in_chunks_in_use() :
  2614                                vsm()->sum_free_in_chunks_in_use();
  2617 // Space capacity in the Metaspace.  It includes
  2618 // space in the list of chunks from which allocations
  2619 // have been made. Don't include space in the global freelist and
  2620 // in the space available in the dictionary which
  2621 // is already counted in some chunk.
  2622 size_t Metaspace::capacity_words(MetadataType mdtype) const {
  2623   return mdtype == ClassType ? class_vsm()->sum_capacity_in_chunks_in_use() :
  2624                                vsm()->sum_capacity_in_chunks_in_use();
  2627 void Metaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) {
  2628   if (SafepointSynchronize::is_at_safepoint()) {
  2629     assert(Thread::current()->is_VM_thread(), "should be the VM thread");
  2630     // Don't take Heap_lock
  2631     MutexLocker ml(vsm()->lock());
  2632     if (word_size < TreeChunk<Metablock, FreeList>::min_size()) {
  2633       // Dark matter.  Too small for dictionary.
  2634 #ifdef ASSERT
  2635       Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5);
  2636 #endif
  2637       return;
  2639     if (is_class) {
  2640        class_vsm()->deallocate(ptr, word_size);
  2641     } else {
  2642       vsm()->deallocate(ptr, word_size);
  2644   } else {
  2645     MutexLocker ml(vsm()->lock());
  2647     if (word_size < TreeChunk<Metablock, FreeList>::min_size()) {
  2648       // Dark matter.  Too small for dictionary.
  2649 #ifdef ASSERT
  2650       Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5);
  2651 #endif
  2652       return;
  2654     if (is_class) {
  2655       class_vsm()->deallocate(ptr, word_size);
  2656     } else {
  2657       vsm()->deallocate(ptr, word_size);
  2662 Metablock* Metaspace::allocate(ClassLoaderData* loader_data, size_t word_size,
  2663                               bool read_only, MetadataType mdtype, TRAPS) {
  2664   if (HAS_PENDING_EXCEPTION) {
  2665     assert(false, "Should not allocate with exception pending");
  2666     return NULL;  // caller does a CHECK_NULL too
  2669   // SSS: Should we align the allocations and make sure the sizes are aligned.
  2670   MetaWord* result = NULL;
  2672   assert(loader_data != NULL, "Should never pass around a NULL loader_data. "
  2673         "ClassLoaderData::the_null_class_loader_data() should have been used.");
  2674   // Allocate in metaspaces without taking out a lock, because it deadlocks
  2675   // with the SymbolTable_lock.  Dumping is single threaded for now.  We'll have
  2676   // to revisit this for application class data sharing.
  2677   if (DumpSharedSpaces) {
  2678     if (read_only) {
  2679       result = loader_data->ro_metaspace()->allocate(word_size, NonClassType);
  2680     } else {
  2681       result = loader_data->rw_metaspace()->allocate(word_size, NonClassType);
  2683     if (result == NULL) {
  2684       report_out_of_shared_space(read_only ? SharedReadOnly : SharedReadWrite);
  2686     return Metablock::initialize(result, word_size);
  2689   result = loader_data->metaspace_non_null()->allocate(word_size, mdtype);
  2691   if (result == NULL) {
  2692     // Try to clean out some memory and retry.
  2693     result =
  2694       Universe::heap()->collector_policy()->satisfy_failed_metadata_allocation(
  2695         loader_data, word_size, mdtype);
  2697     // If result is still null, we are out of memory.
  2698     if (result == NULL) {
  2699       if (Verbose && TraceMetadataChunkAllocation) {
  2700         gclog_or_tty->print_cr("Metaspace allocation failed for size "
  2701           SIZE_FORMAT, word_size);
  2702         if (loader_data->metaspace_or_null() != NULL) loader_data->metaspace_or_null()->dump(gclog_or_tty);
  2703         MetaspaceAux::dump(gclog_or_tty);
  2705       // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
  2706       report_java_out_of_memory("Metadata space");
  2708       if (JvmtiExport::should_post_resource_exhausted()) {
  2709         JvmtiExport::post_resource_exhausted(
  2710             JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,
  2711             "Metadata space");
  2713       THROW_OOP_0(Universe::out_of_memory_error_perm_gen());
  2716   return Metablock::initialize(result, word_size);
  2719 void Metaspace::print_on(outputStream* out) const {
  2720   // Print both class virtual space counts and metaspace.
  2721   if (Verbose) {
  2722       vsm()->print_on(out);
  2723       class_vsm()->print_on(out);
  2727 bool Metaspace::contains(const void * ptr) {
  2728   if (MetaspaceShared::is_in_shared_space(ptr)) {
  2729     return true;
  2731   // This is checked while unlocked.  As long as the virtualspaces are added
  2732   // at the end, the pointer will be in one of them.  The virtual spaces
  2733   // aren't deleted presently.  When they are, some sort of locking might
  2734   // be needed.  Note, locking this can cause inversion problems with the
  2735   // caller in MetaspaceObj::is_metadata() function.
  2736   return space_list()->contains(ptr) || class_space_list()->contains(ptr);
  2739 void Metaspace::verify() {
  2740   vsm()->verify();
  2741   class_vsm()->verify();
  2744 void Metaspace::dump(outputStream* const out) const {
  2745   if (UseMallocOnly) {
  2746     // Just print usage for now
  2747     out->print_cr("usage %d", used_words(Metaspace::NonClassType));
  2749   out->print_cr("\nVirtual space manager: " INTPTR_FORMAT, vsm());
  2750   vsm()->dump(out);
  2751   out->print_cr("\nClass space manager: " INTPTR_FORMAT, class_vsm());
  2752   class_vsm()->dump(out);

mercurial