src/hotspot/share/memory/metaspace/spaceManager.hpp
branchstuefe-new-metaspace-branch
changeset 58063 bdf136b8ae0e
parent 53244 9807daeb47c4
child 58085 a5f523f2ff91
equal deleted inserted replaced
58062:65cad575ace3 58063:bdf136b8ae0e
     1 /*
     1 /*
     2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * Copyright (c) 2018, 2019 SAP SE. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * 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  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  * published by the Free Software Foundation.
    26 #define SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP
    27 #define SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP
    27 
    28 
    28 #include "memory/allocation.hpp"
    29 #include "memory/allocation.hpp"
    29 #include "memory/metaspace.hpp"
    30 #include "memory/metaspace.hpp"
    30 #include "memory/metaspace/blockFreelist.hpp"
    31 #include "memory/metaspace/blockFreelist.hpp"
       
    32 #include "memory/metaspace/chunkAllocSequence.hpp"
       
    33 #include "memory/metaspace/chunkManager.hpp"
       
    34 #include "memory/metaspace/metachunk.hpp"
    31 #include "memory/metaspace/metaspaceCommon.hpp"
    35 #include "memory/metaspace/metaspaceCommon.hpp"
    32 #include "memory/metaspace/metachunk.hpp"
       
    33 #include "memory/metaspace/metaspaceStatistics.hpp"
       
    34 #include "utilities/debug.hpp"
       
    35 #include "utilities/globalDefinitions.hpp"
       
    36 
    36 
    37 class outputStream;
    37 class outputStream;
    38 class Mutex;
    38 class Mutex;
    39 
    39 
    40 namespace metaspace {
    40 namespace metaspace {
    41 
    41 
    42 //  SpaceManager - used by Metaspace to handle allocations
    42 class sm_stats_t;
       
    43 
       
    44 // The SpaceManager:
       
    45 // - keeps a list of chunks-in-use by the class loader, as well as a current chunk used
       
    46 //   to allocate from
       
    47 // - keeps a dictionary of free MetaBlocks. Those can be remnants of a retired chunk or
       
    48 //   allocations which were not needed anymore for some reason (e.g. releasing half-allocated
       
    49 //   structures when class loading fails)
       
    50 
    43 class SpaceManager : public CHeapObj<mtClass> {
    51 class SpaceManager : public CHeapObj<mtClass> {
    44   friend class ::ClassLoaderMetaspace;
       
    45   friend class Metadebug;
       
    46 
    52 
    47  private:
    53   // Lock handed down from the associated ClassLoaderData.
    48 
    54   //  Protects allocations from this space.
    49   // protects allocations
       
    50   Mutex* const _lock;
    55   Mutex* const _lock;
    51 
    56 
    52   // Type of metadata allocated.
    57   // The chunk manager to allocate chunks from.
    53   const Metaspace::MetadataType   _mdtype;
    58   ChunkManager* const _chunk_manager;
    54 
    59 
    55   // Type of metaspace
    60   // The chunk allocation strategy to use.
    56   const Metaspace::MetaspaceType  _space_type;
    61   const ChunkAllocSequence* const _chunk_alloc_sequence;
    57 
    62 
    58   // List of chunks in use by this SpaceManager.  Allocations
    63   // List of chunks in use by this SpaceManager.  Allocations
    59   // are done from the current chunk.  The list is used for deallocating
    64   // are done from the current chunk. The list is used for deallocating
    60   // chunks when the SpaceManager is freed.
    65   // chunks when the SpaceManager is freed.
    61   Metachunk* _chunk_list;
    66   MetachunkList _chunks;
    62   Metachunk* _current_chunk;
       
    63 
    67 
    64   enum {
    68   Metachunk* current_chunk()              { return _chunks.first(); }
       
    69   const Metachunk* current_chunk() const  { return _chunks.first(); }
    65 
    70 
    66     // Maximum number of small chunks to allocate to a SpaceManager
    71   // Prematurely released metablocks.
    67     small_chunk_limit = 4,
    72   BlockFreelist* _block_freelist;
    68 
    73 
    69     // Maximum number of specialize chunks to allocate for anonymous and delegating
    74   // Points to outside size counter which we are to increase/decrease when we allocate memory
    70     // metadata space to a SpaceManager
    75   // on behalf of a user or when we are destroyed.
    71     anon_and_delegating_metadata_specialize_chunk_limit = 4,
    76   SizeAtomicCounter* const _total_used_words_counter;
    72 
    77 
    73     allocation_from_dictionary_limit = 4 * K
    78   const char* const _name;
    74 
    79 
    75   };
    80   Mutex* lock() const                           { return _lock; }
       
    81   ChunkManager* chunk_manager() const           { return _chunk_manager; }
       
    82   const ChunkAllocSequence* chunk_alloc_sequence() const    { return _chunk_alloc_sequence; }
    76 
    83 
    77   // Some running counters, but lets keep their number small to not add to much to
    84   BlockFreelist* block_freelist() const         { return _block_freelist; }
    78   // the per-classloader footprint.
    85   void create_block_freelist();
    79   // Note: capacity = used + free + waste + overhead. We do not keep running counters for
    86   void add_allocation_to_block_freelist(MetaWord* p, size_t word_size);
    80   // free and waste. Their sum can be deduced from the three other values.
       
    81   size_t _overhead_words;
       
    82   size_t _capacity_words;
       
    83   size_t _used_words;
       
    84   uintx _num_chunks_by_type[NumberOfInUseLists];
       
    85 
    87 
    86   // Free lists of blocks are per SpaceManager since they
    88   // The current chunk is too small to service an allocation request, and we cannot enlarge
    87   // are assumed to be in chunks in use by the SpaceManager
    89   // it in-place. Before we allocate a new chunk, take care of the remaining space in the
    88   // and all chunks in use by a SpaceManager are freed when
    90   // current chunk by storing it in the deallocation freelist.
    89   // the class loader using the SpaceManager is collected.
       
    90   BlockFreelist* _block_freelists;
       
    91 
       
    92  private:
       
    93   // Accessors
       
    94   Metachunk* chunk_list() const { return _chunk_list; }
       
    95 
       
    96   BlockFreelist* block_freelists() const { return _block_freelists; }
       
    97 
       
    98   Metaspace::MetadataType mdtype() { return _mdtype; }
       
    99 
       
   100   VirtualSpaceList* vs_list()   const { return Metaspace::get_space_list(_mdtype); }
       
   101   ChunkManager* chunk_manager() const { return Metaspace::get_chunk_manager(_mdtype); }
       
   102 
       
   103   Metachunk* current_chunk() const { return _current_chunk; }
       
   104   void set_current_chunk(Metachunk* v) {
       
   105     _current_chunk = v;
       
   106   }
       
   107 
       
   108   Metachunk* find_current_chunk(size_t word_size);
       
   109 
       
   110   // Add chunk to the list of chunks in use
       
   111   void add_chunk(Metachunk* v, bool make_current);
       
   112   void retire_current_chunk();
    91   void retire_current_chunk();
   113 
    92 
   114   Mutex* lock() const { return _lock; }
    93   // Given a requested word size, will allocate a chunk large enough to at least fit that
       
    94   // size, but may be larger according to the rules in the ChunkAllocSequence.
       
    95   // Updates counters and adds the chunk to the head of the chunk list.
       
    96   Metachunk* allocate_chunk_to_fit(size_t requested_word_size);
   115 
    97 
   116   // Adds to the given statistic object. Expects to be locked with lock().
    98   // Prematurely returns a metaspace allocation to the _block_freelists
   117   void add_to_statistics_locked(SpaceManagerStatistics* out) const;
    99   // because it is not needed anymore (requires CLD lock to be active).
       
   100   void deallocate_locked(MetaWord* p, size_t word_size);
   118 
   101 
   119   // Verify internal counters against the current state. Expects to be locked with lock().
   102   // Returns true if the area indicated by pointer and size have actually been allocated
   120   DEBUG_ONLY(void verify_metrics_locked() const;)
   103   // from this space manager.
       
   104   DEBUG_ONLY(bool is_valid_area(MetaWord* p, size_t word_size) const;)
   121 
   105 
   122  public:
   106 public:
   123   SpaceManager(Metaspace::MetadataType mdtype,
   107 
   124                Metaspace::MetaspaceType space_type,
   108   SpaceManager(ChunkManager* chunk_manager,
   125                Mutex* lock);
   109                const ChunkAllocSequence* alloc_sequence,
       
   110                Mutex* lock,
       
   111                SizeAtomicCounter* total_used_words_counter,
       
   112                const char* name);
       
   113 
   126   ~SpaceManager();
   114   ~SpaceManager();
   127 
   115 
   128   enum ChunkMultiples {
   116   // Allocate memory from Metaspace.
   129     MediumChunkMultiple = 4
   117   // 1) Attempt to allocate from the dictionary of deallocated blocks.
   130   };
   118   // 2) Attempt to allocate from the current chunk.
   131 
   119   // 3) Attempt to enlarge the current chunk in place if it is too small.
   132   static size_t specialized_chunk_size(bool is_class) { return is_class ? ClassSpecializedChunk : SpecializedChunk; }
   120   // 4) Attempt to get a new chunk and allocate from that chunk.
   133   static size_t small_chunk_size(bool is_class)       { return is_class ? ClassSmallChunk : SmallChunk; }
   121   // At any point, if we hit a commit limit, we return NULL.
   134   static size_t medium_chunk_size(bool is_class)      { return is_class ? ClassMediumChunk : MediumChunk; }
       
   135 
       
   136   static size_t smallest_chunk_size(bool is_class)    { return specialized_chunk_size(is_class); }
       
   137 
       
   138   // Accessors
       
   139   bool is_class() const { return _mdtype == Metaspace::ClassType; }
       
   140 
       
   141   size_t specialized_chunk_size() const { return specialized_chunk_size(is_class()); }
       
   142   size_t small_chunk_size()       const { return small_chunk_size(is_class()); }
       
   143   size_t medium_chunk_size()      const { return medium_chunk_size(is_class()); }
       
   144 
       
   145   size_t smallest_chunk_size()    const { return smallest_chunk_size(is_class()); }
       
   146 
       
   147   size_t medium_chunk_bunch()     const { return medium_chunk_size() * MediumChunkMultiple; }
       
   148 
       
   149   bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); }
       
   150 
       
   151   size_t capacity_words() const     { return _capacity_words; }
       
   152   size_t used_words() const         { return _used_words; }
       
   153   size_t overhead_words() const     { return _overhead_words; }
       
   154 
       
   155   // Adjust local, global counters after a new chunk has been added.
       
   156   void account_for_new_chunk(const Metachunk* new_chunk);
       
   157 
       
   158   // Adjust local, global counters after space has been allocated from the current chunk.
       
   159   void account_for_allocation(size_t words);
       
   160 
       
   161   // Adjust global counters just before the SpaceManager dies, after all its chunks
       
   162   // have been returned to the freelist.
       
   163   void account_for_spacemanager_death();
       
   164 
       
   165   // Adjust the initial chunk size to match one of the fixed chunk list sizes,
       
   166   // or return the unadjusted size if the requested size is humongous.
       
   167   static size_t adjust_initial_chunk_size(size_t requested, bool is_class_space);
       
   168   size_t adjust_initial_chunk_size(size_t requested) const;
       
   169 
       
   170   // Get the initial chunks size for this metaspace type.
       
   171   size_t get_initial_chunk_size(Metaspace::MetaspaceType type) const;
       
   172 
       
   173   // Todo: remove this once we have counters by chunk type.
       
   174   uintx num_chunks_by_type(ChunkIndex chunk_type) const       { return _num_chunks_by_type[chunk_type]; }
       
   175 
       
   176   Metachunk* get_new_chunk(size_t chunk_word_size);
       
   177 
       
   178   // Block allocation and deallocation.
       
   179   // Allocates a block from the current chunk
       
   180   MetaWord* allocate(size_t word_size);
   122   MetaWord* allocate(size_t word_size);
   181 
   123 
   182   // Helper for allocations
   124   // Prematurely returns a metaspace allocation to the _block_freelists because it is not
   183   MetaWord* allocate_work(size_t word_size);
   125   // needed anymore.
   184 
       
   185   // Returns a block to the per manager freelist
       
   186   void deallocate(MetaWord* p, size_t word_size);
   126   void deallocate(MetaWord* p, size_t word_size);
   187 
   127 
   188   // Based on the allocation size and a minimum chunk size,
   128   // Update statistics. This walks all in-use chunks.
   189   // returned chunk size (for expanding space for chunk allocation).
   129   void add_to_statistics(sm_stats_t* out) const;
   190   size_t calc_chunk_size(size_t allocation_word_size);
       
   191 
   130 
   192   // Called when an allocation from the current chunk fails.
   131   // Run verifications. slow=true: verify chunk-internal integrity too.
   193   // Gets a new chunk (may require getting a new virtual space),
   132   DEBUG_ONLY(void verify(bool slow) const;)
   194   // and allocates from that chunk.
       
   195   MetaWord* grow_and_allocate(size_t word_size);
       
   196 
       
   197   // Notify memory usage to MemoryService.
       
   198   void track_metaspace_memory_usage();
       
   199 
       
   200   // debugging support.
       
   201 
       
   202   void print_on(outputStream* st) const;
       
   203   void locked_print_chunks_in_use_on(outputStream* st) const;
       
   204 
       
   205   void verify();
       
   206   void verify_chunk_size(Metachunk* chunk);
       
   207 
       
   208   // This adjusts the size given to be greater than the minimum allocation size in
       
   209   // words for data in metaspace.  Esentially the minimum size is currently 3 words.
       
   210   size_t get_allocation_word_size(size_t word_size) {
       
   211     size_t byte_size = word_size * BytesPerWord;
       
   212 
       
   213     size_t raw_bytes_size = MAX2(byte_size, sizeof(Metablock));
       
   214     raw_bytes_size = align_up(raw_bytes_size, Metachunk::object_alignment());
       
   215 
       
   216     size_t raw_word_size = raw_bytes_size / BytesPerWord;
       
   217     assert(raw_word_size * BytesPerWord == raw_bytes_size, "Size problem");
       
   218 
       
   219     return raw_word_size;
       
   220   }
       
   221 
       
   222   // Adds to the given statistic object.
       
   223   void add_to_statistics(SpaceManagerStatistics* out) const;
       
   224 
       
   225   // Verify internal counters against the current state.
       
   226   DEBUG_ONLY(void verify_metrics() const;)
       
   227 
   133 
   228 };
   134 };
   229 
       
   230 
   135 
   231 } // namespace metaspace
   136 } // namespace metaspace
   232 
   137 
   233 #endif // SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP
   138 #endif // SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP