< prev index next >

src/hotspot/share/memory/metaspace/chunkManager.hpp

Print this page
rev 60538 : imported patch jep387-core.patch
   1 /*
   2  * Copyright (c) 2018, 2019, 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 
  25 #ifndef SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP
  26 #define SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/binaryTreeDictionary.hpp"
  30 #include "memory/freeList.hpp"

  31 #include "memory/metaspace/metachunk.hpp"
  32 #include "memory/metaspace/metaspaceStatistics.hpp"
  33 #include "memory/metaspaceChunkFreeListSummary.hpp"
  34 #include "utilities/globalDefinitions.hpp"
  35 
  36 class ChunkManagerTestAccessor;
  37 
  38 namespace metaspace {
  39 
  40 typedef class FreeList<Metachunk> ChunkList;
  41 typedef BinaryTreeDictionary<Metachunk, FreeList<Metachunk> > ChunkTreeDictionary;
  42 
  43 // Manages the global free lists of chunks.
  44 class ChunkManager : public CHeapObj<mtInternal> {
  45   friend class ::ChunkManagerTestAccessor;
  46 
  47   // Free list of chunks of different sizes.
  48   //   SpecializedChunk
  49   //   SmallChunk
  50   //   MediumChunk
  51   ChunkList _free_chunks[NumberOfFreeLists];
  52 
  53   // Whether or not this is the class chunkmanager.
  54   const bool _is_class;
  55 
  56   // Return non-humongous chunk list by its index.
  57   ChunkList* free_chunks(ChunkIndex index);
  58 
  59   // Returns non-humongous chunk list for the given chunk word size.
  60   ChunkList* find_free_chunks_list(size_t word_size);
  61 
  62   //   HumongousChunk
  63   ChunkTreeDictionary _humongous_dictionary;
  64 
  65   // Returns the humongous chunk dictionary.
  66   ChunkTreeDictionary* humongous_dictionary() { return &_humongous_dictionary; }
  67   const ChunkTreeDictionary* humongous_dictionary() const { return &_humongous_dictionary; }
  68 
  69   // Size, in metaspace words, of all chunks managed by this ChunkManager
  70   size_t _free_chunks_total;
  71   // Number of chunks in this ChunkManager
  72   size_t _free_chunks_count;
  73 
  74   // Update counters after a chunk was added or removed removed.
  75   void account_for_added_chunk(const Metachunk* c);
  76   void account_for_removed_chunk(const Metachunk* c);
  77 
  78   // Given a pointer to a chunk, attempts to merge it with neighboring
  79   // free chunks to form a bigger chunk. Returns true if successful.
  80   bool attempt_to_coalesce_around_chunk(Metachunk* chunk, ChunkIndex target_chunk_type);
  81 
  82   // Helper for chunk merging:
  83   //  Given an address range with 1-n chunks which are all supposed to be
  84   //  free and hence currently managed by this ChunkManager, remove them
  85   //  from this ChunkManager and mark them as invalid.
  86   // - This does not correct the occupancy map.
  87   // - This does not adjust the counters in ChunkManager.
  88   // - Does not adjust container count counter in containing VirtualSpaceNode.
  89   // Returns number of chunks removed.
  90   int remove_chunks_in_area(MetaWord* p, size_t word_size);
  91 
  92   // Helper for chunk splitting: given a target chunk size and a larger free chunk,
  93   // split up the larger chunk into n smaller chunks, at least one of which should be
  94   // the target chunk of target chunk size. The smaller chunks, including the target
  95   // chunk, are returned to the freelist. The pointer to the target chunk is returned.
  96   // Note that this chunk is supposed to be removed from the freelist right away.
  97   Metachunk* split_chunk(size_t target_chunk_word_size, Metachunk* chunk);
  98 
  99  public:
 100 
 101   ChunkManager(bool is_class);
 102 
 103   // Add or delete (return) a chunk to the global freelist.
 104   Metachunk* chunk_freelist_allocate(size_t word_size);
 105 
 106   // Map a size to a list index assuming that there are lists
 107   // for special, small, medium, and humongous chunks.
 108   ChunkIndex list_index(size_t size);
 109 
 110   // Map a given index to the chunk size.
 111   size_t size_by_index(ChunkIndex index) const;
 112 
 113   bool is_class() const { return _is_class; }
 114 
 115   // Convenience accessors.
 116   size_t medium_chunk_word_size() const { return size_by_index(MediumIndex); }
 117   size_t small_chunk_word_size() const { return size_by_index(SmallIndex); }
 118   size_t specialized_chunk_word_size() const { return size_by_index(SpecializedIndex); }
 119 
 120   // Take a chunk from the ChunkManager. The chunk is expected to be in
 121   // the chunk manager (the freelist if non-humongous, the dictionary if
 122   // humongous).
 123   void remove_chunk(Metachunk* chunk);
 124 
 125   // Return a single chunk of type index to the ChunkManager.
 126   void return_single_chunk(Metachunk* chunk);
 127 
 128   // Add the simple linked list of chunks to the freelist of chunks
 129   // of type index.
 130   void return_chunk_list(Metachunk* chunk);
 131 
 132   // Total of the space in the free chunks list
 133   size_t free_chunks_total_words() const { return _free_chunks_total; }
 134   size_t free_chunks_total_bytes() const { return free_chunks_total_words() * BytesPerWord; }
 135 
 136   // Number of chunks in the free chunks list
 137   size_t free_chunks_count() const { return _free_chunks_count; }
 138 
 139   // Remove from a list by size.  Selects list based on size of chunk.
 140   Metachunk* free_chunks_get(size_t chunk_word_size);
 141 
 142 #define index_bounds_check(index)                                         \
 143   assert(is_valid_chunktype(index), "Bad index: %d", (int) index)
 144 
 145   size_t num_free_chunks(ChunkIndex index) const {
 146     index_bounds_check(index);
 147 
 148     if (index == HumongousIndex) {
 149       return _humongous_dictionary.total_free_blocks();
 150     }
 151 
 152     ssize_t count = _free_chunks[index].count();
 153     return count == -1 ? 0 : (size_t) count;
 154   }
 155 
 156   size_t size_free_chunks_in_bytes(ChunkIndex index) const {
 157     index_bounds_check(index);
 158 
 159     size_t word_size = 0;
 160     if (index == HumongousIndex) {
 161       word_size = _humongous_dictionary.total_size();
 162     } else {
 163       const size_t size_per_chunk_in_words = _free_chunks[index].size();
 164       word_size = size_per_chunk_in_words * num_free_chunks(index);
 165     }
 166 
 167     return word_size * BytesPerWord;
 168   }
 169 
 170   MetaspaceChunkFreeListSummary chunk_free_list_summary() const {
 171     return MetaspaceChunkFreeListSummary(num_free_chunks(SpecializedIndex),
 172                                          num_free_chunks(SmallIndex),
 173                                          num_free_chunks(MediumIndex),
 174                                          num_free_chunks(HumongousIndex),
 175                                          size_free_chunks_in_bytes(SpecializedIndex),
 176                                          size_free_chunks_in_bytes(SmallIndex),
 177                                          size_free_chunks_in_bytes(MediumIndex),
 178                                          size_free_chunks_in_bytes(HumongousIndex));
 179   }
 180 
 181 #ifdef ASSERT
 182   // Debug support
 183   // Verify free list integrity. slow=true: verify chunk-internal integrity too.
 184   void verify(bool slow) const;
 185   void locked_verify(bool slow) const;
 186 #endif
 187 
 188   void locked_print_free_chunks(outputStream* st);















































































































































 189 
 190   // Fill in current statistic values to the given statistics object.
 191   void collect_statistics(ChunkManagerStatistics* out) const;
 192 
 193 };
 194 
 195 } // namespace metaspace
 196 
 197 
 198 #endif // SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP
   1 /*
   2  * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, 2020 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #ifndef SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP
  27 #define SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP
  28 
  29 #include "memory/allocation.hpp"
  30 #include "memory/metaspace/chunkLevel.hpp"
  31 #include "memory/metaspace/counter.hpp"
  32 #include "memory/metaspace/freeChunkList.hpp"
  33 #include "memory/metaspace/metachunk.hpp"





  34 
  35 namespace metaspace {
  36 
  37 class VirtualSpaceList;
  38 struct cm_stats_t;
  39 
  40 // ChunkManager has a somewhat central role.















































































































































  41 
  42 // Arenas request chunks from it and, on death, return chunks back to it.
  43 //  It keeps freelists for chunks, one per chunk level, sorted by chunk
  44 //  commit state.
  45 //  To feed the freelists, it allocates root chunks from the associated
  46 //  VirtualSpace below it.
  47 //
  48 // ChunkManager directs splitting chunks, if a chunk request cannot be
  49 //  fulfilled directly. It also takes care of merging when chunks are
  50 //  returned to it, before they are added to the freelist.
  51 //
  52 // The freelists are double linked double headed; fully committed chunks
  53 //  are added to the front, others to the back.
  54 //
  55 // Level
  56 //          +--------------------+   +--------------------+
  57 //  0  +----|  free root chunk   |---|  free root chunk   |---...
  58 //     |    +--------------------+   +--------------------+
  59 //     |
  60 //     |    +----------+   +----------+
  61 //  1  +----|          |---|          |---...
  62 //     |    +----------+   +----------+
  63 //     |
  64 //  .
  65 //  .
  66 //  .
  67 //
  68 //     |    +-+   +-+
  69 //  12 +----| |---| |---...
  70 //          +-+   +-+
  71 
  72 
  73 class ChunkManager : public CHeapObj<mtMetaspace> {
  74 
  75   // A chunk manager is connected to a virtual space list which is used
  76   // to allocate new root chunks when no free chunks are found.
  77   VirtualSpaceList* const _vslist;
  78 
  79   // Name
  80   const char* const _name;
  81 
  82   // Freelists
  83   FreeChunkListVector _chunks;
  84 
  85   // Returns true if this manager contains the given chunk. Slow (walks free lists) and
  86   // only needed for verifications.
  87   DEBUG_ONLY(bool contains_chunk(Metachunk* c) const;)
  88 
  89   // Given a chunk, split it into a target chunk of a smaller size (target level)
  90   //  at least one, possible more splinter chunks. Splinter chunks are added to the
  91   //  freelist.
  92   // The original chunk must be outside of the freelist and its state must be free.
  93   // The resulting target chunk will be located at the same address as the original
  94   //  chunk, but it will of course be smaller (of a higher level).
  95   // The committed areas within the original chunk carry over to the resulting
  96   //  chunks.
  97   void split_chunk_and_add_splinters(Metachunk* c, chunklevel_t target_level);
  98 
  99   // See get_chunk(s,s,s)
 100   Metachunk* get_chunk_locked(size_t preferred_word_size, size_t min_word_size, size_t min_committed_words);
 101 
 102   // Uncommit all chunks equal or below the given level.
 103   void uncommit_free_chunks(chunklevel_t max_level);
 104 
 105   // Return a single chunk to the freelist without doing any merging, and adjust accounting.
 106   void return_chunk_simple_locked(Metachunk* c);
 107 
 108   // See return_chunk().
 109   void return_chunk_locked(Metachunk* c);
 110 
 111 public:
 112 
 113   // Creates a chunk manager with a given name (which is for debug purposes only)
 114   // and an associated space list which will be used to request new chunks from
 115   // (see get_chunk())
 116   ChunkManager(const char* name, VirtualSpaceList* space_list);
 117 
 118   // On success, returns a chunk of level of <preferred_level>, but at most <max_level>.
 119   //  The first first <min_committed_words> of the chunk are guaranteed to be committed.
 120   // On error, will return NULL.
 121   //
 122   // This function may fail for two reasons:
 123   // - Either we are unable to reserve space for a new chunk (if the underlying VirtualSpaceList
 124   //   is non-expandable but needs expanding - aka out of compressed class space).
 125   // - Or, if the necessary space cannot be committed because we hit a commit limit.
 126   //   This may be either the GC threshold or MaxMetaspaceSize.
 127   Metachunk* get_chunk(chunklevel_t preferred_level, chunklevel_t max_level, size_t min_committed_words);
 128 
 129   // Convenience function - get a chunk of a given level, uncommitted.
 130   Metachunk* get_chunk(chunklevel_t lvl) { return get_chunk(lvl, lvl, 0); }
 131 
 132   // Return a single chunk to the ChunkManager and adjust accounting. May merge chunk
 133   //  with neighbors.
 134   // Happens after a Classloader was unloaded and releases its metaspace chunks.
 135   // !! Notes:
 136   //    1) After this method returns, c may not be valid anymore. ** Do not access c after this function returns **.
 137   //    2) This function will not remove c from its current chunk list. This has to be done by the caller prior to
 138   //       calling this method.
 139   void return_chunk(Metachunk* c);
 140 
 141   // Given a chunk c, which must be "in use" and must not be a root chunk, attempt to
 142   // enlarge it in place by claiming its trailing buddy.
 143   //
 144   // This will only work if c is the leader of the buddy pair and the trailing buddy is free.
 145   //
 146   // If successful, the follower chunk will be removed from the freelists, the leader chunk c will
 147   // double in size (level decreased by one).
 148   //
 149   // On success, true is returned, false otherwise.
 150   bool attempt_enlarge_chunk(Metachunk* c);
 151 
 152   // Attempt to reclaim free areas in metaspace wholesale:
 153   // - first, attempt to purge nodes of the backing virtual space list: nodes which are completely
 154   //   unused get unmapped and deleted completely.
 155   // - second, it will uncommit free chunks depending on commit granule size.
 156   void purge();
 157 
 158   // Run verifications. slow=true: verify chunk-internal integrity too.
 159   DEBUG_ONLY(void verify(bool slow) const;)
 160   DEBUG_ONLY(void verify_locked(bool slow) const;)
 161 
 162   // Returns the name of this chunk manager.
 163   const char* name() const                  { return _name; }
 164 
 165   // Returns total number of chunks
 166   int total_num_chunks() const              { return _chunks.num_chunks(); }
 167 
 168   // Returns number of words in all free chunks (regardless of commit state).
 169   size_t total_word_size() const            { return _chunks.word_size(); }
 170 
 171   // Returns number of committed words in all free chunks.
 172   size_t total_committed_word_size() const  { return _chunks.committed_word_size(); }
 173 
 174   // Update statistics.
 175   void add_to_statistics(cm_stats_t* out) const;
 176 
 177   void print_on(outputStream* st) const;
 178   void print_on_locked(outputStream* st) const;
 179 
 180 public:
 181 
 182   // Convenience methods to return the global class-space chunkmanager
 183   //  and non-class chunkmanager, respectively.
 184   static ChunkManager* chunkmanager_class();
 185   static ChunkManager* chunkmanager_nonclass();
 186 


 187 
 188 };
 189 
 190 } // namespace metaspace

 191 
 192 #endif // SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP
< prev index next >