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_SPACEMANAGER_HPP
  26 #define SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/metaspace.hpp"
  30 #include "memory/metaspace/blockFreelist.hpp"
  31 #include "memory/metaspace/chunkAllocSequence.hpp"
  32 #include "memory/metaspace/chunkManager.hpp"
  33 #include "memory/metaspace/metachunk.hpp"
  34 #include "memory/metaspace/metaspaceCommon.hpp"
  35 
  36 class outputStream;
  37 class Mutex;
  38 
  39 namespace metaspace {
  40 
  41 class SpaceManagerStatistics;
  42 
  43 // The SpaceManager:
  44 // - keeps a list of chunks-in-use by the class loader, as well as a current chunk used
  45 //   to allocate from
  46 // - keeps a dictionary of free MetaBlocks. Those can be remnants of a retired chunk or
  47 //   allocations which were not needed anymore for some reason (e.g. releasing half-allocated
  48 //   structures when class loading fails)
  49 
  50 class SpaceManager : public CHeapObj<mtClass> {
  51 
  52   // Lock handed down from the associated ClassLoaderData.
  53   //  Protects allocations from this space.
  54   Mutex* const _lock;
  55 
  56   // The chunk manager to allocate chunks from.
  57   ChunkManager* const _chunk_manager;
  58 
  59   // The chunk allocation strategy to use.
  60   const ChunkAllocSequence* const _chunk_alloc_sequence;
  61 
  62   // List of chunks in use by this SpaceManager.  Allocations
  63   // are done from the current chunk.  The list is used for deallocating
  64   // chunks when the SpaceManager is freed.
  65   MetachunkList _chunks;
  66   Metachunk* _current_chunk;
  67 
  68   // Prematurely released metablocks.
  69   BlockFreelist* _block_freelist;
  70 
  71 
  72   // Statistics
  73 
  74   // Running counters.
  75   // Note: capacity = used + free + waste + overhead. We do not keep running counters for
  76   // free and waste. Their sum can be deduced from the three other values.
  77  // size_t _overhead_words;
  78   // size_t _capacity_words;
  79   // size_t _used_words;
  80   uintx _num_chunks_by_level[chklvl::NUM_CHUNK_LEVELS];
  81   uintx _num_chunks_total;
  82 
  83 
  84   Mutex* lock() const                           { return _lock; }
  85   ChunkManager* chunk_manager() const           { return _chunk_manager; }
  86   const ChunkAllocSequence* chunk_alloc_sequence() const    { return _chunk_alloc_sequence; }
  87 
  88   BlockFreelist* block_freelist() const         { return _block_freelist; }
  89   void create_block_freelist();
  90 
  91   // The current chunk is unable to service a request. The remainder of the chunk is
  92   // chopped into blocks and fed into the _block_freelists, in the hope of later reuse.
  93   void retire_current_chunk();
  94 
  95   // Given a requested word size, will allocate a chunk large enough to at least fit that
  96   // size, but may be larger according to the rules in the ChunkAllocSequence.
  97   // Updates counters and adds the chunk to the head of the chunk list.
  98   Metachunk* allocate_chunk_to_fit(size_t requested_word_size);
  99 
 100 public:
 101 
 102   SpaceManager(ChunkManager* chunk_manager, const ChunkAllocSequence* alloc_sequence, Mutex* lock);
 103 
 104   ~SpaceManager();
 105 
 106   // Allocate memory from Metaspace. Will attempt to allocate from the _block_freelists,
 107   // failing that, from the current chunk; failing that, attempt to get a new chunk from
 108   // the associated ChunkManager.
 109   MetaWord* allocate(size_t word_size);
 110 
 111   // Prematurely returns a metaspace allocation to the _block_freelists because it is not
 112   // needed anymore.
 113   void deallocate(MetaWord* p, size_t word_size);
 114 
 115   // Update statistics. This walks all in-use chunks.
 116   void add_to_statistics(SpaceManagerStatistics* out) const;
 117 
 118   // Run verifications. slow=true: verify chunk-internal integrity too.
 119   DEBUG_ONLY(void locked_verify(bool slow) const;)
 120 
 121 };
 122 
 123 } // namespace metaspace
 124 
 125 #endif // SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP