src/hotspot/share/memory/metaspace/chunkManager.hpp
changeset 50193 49c3e91c424f
child 50380 bec342339138
equal deleted inserted replaced
50192:8bc79d2d1568 50193:49c3e91c424f
       
     1 /*
       
     2  * Copyright (c) 2018, 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.inline.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 TestVirtualSpaceNodeTest;
       
    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 ::TestVirtualSpaceNodeTest;
       
    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() {
       
    67     return &_humongous_dictionary;
       
    68   }
       
    69 
       
    70   // Size, in metaspace words, of all chunks managed by this ChunkManager
       
    71   size_t _free_chunks_total;
       
    72   // Number of chunks in this ChunkManager
       
    73   size_t _free_chunks_count;
       
    74 
       
    75   // Update counters after a chunk was added or removed removed.
       
    76   void account_for_added_chunk(const Metachunk* c);
       
    77   void account_for_removed_chunk(const Metachunk* c);
       
    78 
       
    79   size_t sum_free_chunks();
       
    80   size_t sum_free_chunks_count();
       
    81 
       
    82   void locked_verify_free_chunks_total();
       
    83   void slow_locked_verify_free_chunks_total() {
       
    84     if (VerifyMetaspace) {
       
    85       locked_verify_free_chunks_total();
       
    86     }
       
    87   }
       
    88   void locked_verify_free_chunks_count();
       
    89   void slow_locked_verify_free_chunks_count() {
       
    90     if (VerifyMetaspace) {
       
    91       locked_verify_free_chunks_count();
       
    92     }
       
    93   }
       
    94 
       
    95   // Given a pointer to a chunk, attempts to merge it with neighboring
       
    96   // free chunks to form a bigger chunk. Returns true if successful.
       
    97   bool attempt_to_coalesce_around_chunk(Metachunk* chunk, ChunkIndex target_chunk_type);
       
    98 
       
    99   // Helper for chunk merging:
       
   100   //  Given an address range with 1-n chunks which are all supposed to be
       
   101   //  free and hence currently managed by this ChunkManager, remove them
       
   102   //  from this ChunkManager and mark them as invalid.
       
   103   // - This does not correct the occupancy map.
       
   104   // - This does not adjust the counters in ChunkManager.
       
   105   // - Does not adjust container count counter in containing VirtualSpaceNode.
       
   106   // Returns number of chunks removed.
       
   107   int remove_chunks_in_area(MetaWord* p, size_t word_size);
       
   108 
       
   109   // Helper for chunk splitting: given a target chunk size and a larger free chunk,
       
   110   // split up the larger chunk into n smaller chunks, at least one of which should be
       
   111   // the target chunk of target chunk size. The smaller chunks, including the target
       
   112   // chunk, are returned to the freelist. The pointer to the target chunk is returned.
       
   113   // Note that this chunk is supposed to be removed from the freelist right away.
       
   114   Metachunk* split_chunk(size_t target_chunk_word_size, Metachunk* chunk);
       
   115 
       
   116  public:
       
   117 
       
   118   ChunkManager(bool is_class)
       
   119       : _is_class(is_class), _free_chunks_total(0), _free_chunks_count(0) {
       
   120     _free_chunks[SpecializedIndex].set_size(get_size_for_nonhumongous_chunktype(SpecializedIndex, is_class));
       
   121     _free_chunks[SmallIndex].set_size(get_size_for_nonhumongous_chunktype(SmallIndex, is_class));
       
   122     _free_chunks[MediumIndex].set_size(get_size_for_nonhumongous_chunktype(MediumIndex, is_class));
       
   123   }
       
   124 
       
   125   // Add or delete (return) a chunk to the global freelist.
       
   126   Metachunk* chunk_freelist_allocate(size_t word_size);
       
   127 
       
   128   // Map a size to a list index assuming that there are lists
       
   129   // for special, small, medium, and humongous chunks.
       
   130   ChunkIndex list_index(size_t size);
       
   131 
       
   132   // Map a given index to the chunk size.
       
   133   size_t size_by_index(ChunkIndex index) const;
       
   134 
       
   135   bool is_class() const { return _is_class; }
       
   136 
       
   137   // Convenience accessors.
       
   138   size_t medium_chunk_word_size() const { return size_by_index(MediumIndex); }
       
   139   size_t small_chunk_word_size() const { return size_by_index(SmallIndex); }
       
   140   size_t specialized_chunk_word_size() const { return size_by_index(SpecializedIndex); }
       
   141 
       
   142   // Take a chunk from the ChunkManager. The chunk is expected to be in
       
   143   // the chunk manager (the freelist if non-humongous, the dictionary if
       
   144   // humongous).
       
   145   void remove_chunk(Metachunk* chunk);
       
   146 
       
   147   // Return a single chunk of type index to the ChunkManager.
       
   148   void return_single_chunk(Metachunk* chunk);
       
   149 
       
   150   // Add the simple linked list of chunks to the freelist of chunks
       
   151   // of type index.
       
   152   void return_chunk_list(Metachunk* chunk);
       
   153 
       
   154   // Total of the space in the free chunks list
       
   155   size_t free_chunks_total_words();
       
   156   size_t free_chunks_total_bytes();
       
   157 
       
   158   // Number of chunks in the free chunks list
       
   159   size_t free_chunks_count();
       
   160 
       
   161   // Remove from a list by size.  Selects list based on size of chunk.
       
   162   Metachunk* free_chunks_get(size_t chunk_word_size);
       
   163 
       
   164 #define index_bounds_check(index)                                         \
       
   165   assert(is_valid_chunktype(index), "Bad index: %d", (int) index)
       
   166 
       
   167   size_t num_free_chunks(ChunkIndex index) const {
       
   168     index_bounds_check(index);
       
   169 
       
   170     if (index == HumongousIndex) {
       
   171       return _humongous_dictionary.total_free_blocks();
       
   172     }
       
   173 
       
   174     ssize_t count = _free_chunks[index].count();
       
   175     return count == -1 ? 0 : (size_t) count;
       
   176   }
       
   177 
       
   178   size_t size_free_chunks_in_bytes(ChunkIndex index) const {
       
   179     index_bounds_check(index);
       
   180 
       
   181     size_t word_size = 0;
       
   182     if (index == HumongousIndex) {
       
   183       word_size = _humongous_dictionary.total_size();
       
   184     } else {
       
   185       const size_t size_per_chunk_in_words = _free_chunks[index].size();
       
   186       word_size = size_per_chunk_in_words * num_free_chunks(index);
       
   187     }
       
   188 
       
   189     return word_size * BytesPerWord;
       
   190   }
       
   191 
       
   192   MetaspaceChunkFreeListSummary chunk_free_list_summary() const {
       
   193     return MetaspaceChunkFreeListSummary(num_free_chunks(SpecializedIndex),
       
   194                                          num_free_chunks(SmallIndex),
       
   195                                          num_free_chunks(MediumIndex),
       
   196                                          num_free_chunks(HumongousIndex),
       
   197                                          size_free_chunks_in_bytes(SpecializedIndex),
       
   198                                          size_free_chunks_in_bytes(SmallIndex),
       
   199                                          size_free_chunks_in_bytes(MediumIndex),
       
   200                                          size_free_chunks_in_bytes(HumongousIndex));
       
   201   }
       
   202 
       
   203   // Debug support
       
   204   void verify();
       
   205   void slow_verify() {
       
   206     if (VerifyMetaspace) {
       
   207       verify();
       
   208     }
       
   209   }
       
   210   void locked_verify();
       
   211   void slow_locked_verify() {
       
   212     if (VerifyMetaspace) {
       
   213       locked_verify();
       
   214     }
       
   215   }
       
   216 
       
   217   void locked_print_free_chunks(outputStream* st);
       
   218   void locked_print_sum_free_chunks(outputStream* st);
       
   219 
       
   220   // Fill in current statistic values to the given statistics object.
       
   221   void collect_statistics(ChunkManagerStatistics* out) const;
       
   222 
       
   223 };
       
   224 
       
   225 } // namespace metaspace
       
   226 
       
   227 
       
   228 #endif /* SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP_ */
       
   229