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/metaspace/chunkLevel.hpp"
  30 
  31 namespace metaspace {
  32 
  33 class VirtualSpaceList;
  34 class Metachunk;
  35 
  36 
  37 // class ChunkManager
  38 //
  39 // The ChunkManager has a central role. Callers request chunks from it.
  40 // It keeps the freelists for chunks. If the freelist is exhausted it
  41 // allocates new chunks from a connected VirtualSpaceList.
  42 //
  43 class ChunkManager : public CHeapObj<mtInternal> {
  44 
  45   // A chunk manager is connected to a virtual space list which is used
  46   // to allocate new root chunks when no free chunks are found.
  47   VirtualSpaceList* const _vs_list;
  48 
  49   // Name
  50   const char* const _name;
  51 
  52   // Freelist
  53   Metachunk* _chunks [chklvl::NUM_CHUNK_LEVELS];
  54 
  55   // Statistics.
  56   // Number of counters per level.
  57   int        _num_chunks [chklvl::NUM_CHUNK_LEVELS];
  58 
  59   // Total size in all chunks, and total number of chunks.
  60   size_t     _total_word_size;
  61   int        _total_num_chunks;
  62 
  63   // Remove a chunk of the given level from its freelist, and adjust accounting.
  64   // If no chunk of this given level is free, return NULL.
  65   Metachunk* get_chunk_simple(chklvl_t level);
  66 
  67   // Update counters after a chunk has been added or removed.
  68   void account_for_added_chunk(const Metachunk* c);
  69   void account_for_removed_chunk(const Metachunk* c);
  70 
  71   // Returns true if this manager contains the given chunk. Slow (walks free list) and
  72   // only needed for verifications.
  73   DEBUG_ONLY(bool contains_chunk(Metachunk* metachunk) const;)
  74 
  75 public:
  76 
  77   // Creates a chunk manager with a given name (which is for debug purposes only)
  78   // and an associated space list which will be used to request new chunks from
  79   // (see get_chunk())
  80   ChunkManager(const char* name, VirtualSpaceList* space_list);
  81 
  82   // Get a chunk and be smart about it.
  83   // - Attempt to find a free chunk of exactly the pref_level level
  84   // - Failing that, attempt to find a chunk smaller or equal the minimal level.
  85   // - Failing that, attempt to find a free chunk of larger size and split it.
  86   // - Failing that, attempt to allocate a new chunk from the connected virtual space.
  87   //    This may fail if we hit GC threshold or metaspace limit.
  88   // - Failing that, give up and return NULL.
  89   Metachunk* get_chunk(chklvl_t min_level, chklvl_t pref_level);
  90 
  91   // Remove the given chunk from its free list and adjust accounting.
  92   // (Called during VirtualSpaceNode purging which happens during a Metaspace GC.)
  93   void remove_chunk(Metachunk* chunk);
  94 
  95   // Return a chunk to the ChunkManager and adjust accounting. May merge chunk
  96   //  with neighbors.
  97   // Happens after a Classloader was unloaded and releases its metaspace chunks.
  98   // !! Note: this may invalidate the chunk. Do not access the chunk after
  99   //    this function returns !!
 100   void return_chunk(Metachunk* chunk);
 101 
 102   // Uncommit payload area of free chunks. Will be called during Metaspace GC.
 103   void uncommit_free_chunks();
 104 
 105   // Run verifications. slow=true: verify chunk-internal integrity too.
 106   DEBUG_ONLY(void verify(bool slow) const;)
 107   DEBUG_ONLY(void locked_verify(bool slow) const;)
 108 
 109   // Returns the name of this chunk manager.
 110   const char* name() const { return _name; }
 111 
 112 };
 113 
 114 } // namespace metaspace
 115 
 116 #endif // SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP