1 /*
   2  * Copyright (c) 2003, 2015, 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_VM_SERVICES_MEMORYPOOL_HPP
  26 #define SHARE_VM_SERVICES_MEMORYPOOL_HPP
  27 
  28 #include "memory/heap.hpp"
  29 #include "services/memoryUsage.hpp"
  30 #include "utilities/macros.hpp"
  31 
  32 // A memory pool represents the memory area that the VM manages.
  33 // The Java virtual machine has at least one memory pool
  34 // and it may create or remove memory pools during execution.
  35 // A memory pool can belong to the heap or the non-heap memory.
  36 // A Java virtual machine may also have memory pools belonging to
  37 // both heap and non-heap memory.
  38 
  39 // Forward declaration
  40 class CompactibleFreeListSpace;
  41 class ContiguousSpace;
  42 class MemoryManager;
  43 class SensorInfo;
  44 class Generation;
  45 class DefNewGeneration;
  46 class ThresholdSupport;
  47 
  48 class MemoryPool : public CHeapObj<mtInternal> {
  49   friend class MemoryManager;
  50  public:
  51   enum PoolType {
  52     Heap    = 1,
  53     NonHeap = 2
  54   };
  55 
  56  private:
  57   enum {
  58     max_num_managers = 5
  59   };
  60 
  61   // We could make some of the following as performance counters
  62   // for external monitoring.
  63   const char*      _name;
  64   PoolType         _type;
  65   size_t           _initial_size;
  66   size_t           _max_size;
  67   bool             _available_for_allocation; // Default is true
  68   MemoryManager*   _managers[max_num_managers];
  69   int              _num_managers;
  70   MemoryUsage      _peak_usage;               // Peak memory usage
  71   MemoryUsage      _after_gc_usage;           // After GC memory usage
  72 
  73   ThresholdSupport* _usage_threshold;
  74   ThresholdSupport* _gc_usage_threshold;
  75 
  76   SensorInfo*      _usage_sensor;
  77   SensorInfo*      _gc_usage_sensor;
  78 
  79   volatile instanceOop _memory_pool_obj;
  80 
  81   void add_manager(MemoryManager* mgr);
  82 
  83  public:
  84   MemoryPool(const char* name,
  85              PoolType type,
  86              size_t init_size,
  87              size_t max_size,
  88              bool support_usage_threshold,
  89              bool support_gc_threshold);
  90 
  91   const char* name()                       { return _name; }
  92   bool        is_heap()                    { return _type == Heap; }
  93   bool        is_non_heap()                { return _type == NonHeap; }
  94   size_t      initial_size()   const       { return _initial_size; }
  95   int         num_memory_managers() const  { return _num_managers; }
  96   // max size could be changed
  97   virtual size_t max_size()    const       { return _max_size; }
  98 
  99   bool is_pool(instanceHandle pool) { return (pool() == _memory_pool_obj); }
 100 
 101   bool available_for_allocation()   { return _available_for_allocation; }
 102   bool set_available_for_allocation(bool value) {
 103     bool prev = _available_for_allocation;
 104     _available_for_allocation = value;
 105     return prev;
 106   }
 107 
 108   MemoryManager* get_memory_manager(int index) {
 109     assert(index >= 0 && index < _num_managers, "Invalid index");
 110     return _managers[index];
 111   }
 112 
 113   // Records current memory usage if it's a peak usage
 114   void record_peak_memory_usage();
 115 
 116   MemoryUsage get_peak_memory_usage() {
 117     // check current memory usage first and then return peak usage
 118     record_peak_memory_usage();
 119     return _peak_usage;
 120   }
 121   void        reset_peak_memory_usage() {
 122     _peak_usage = get_memory_usage();
 123   }
 124 
 125   ThresholdSupport* usage_threshold()      { return _usage_threshold; }
 126   ThresholdSupport* gc_usage_threshold()   { return _gc_usage_threshold; }
 127 
 128   SensorInfo*       usage_sensor()         {  return _usage_sensor; }
 129   SensorInfo*       gc_usage_sensor()      { return _gc_usage_sensor; }
 130 
 131   void        set_usage_sensor_obj(instanceHandle s);
 132   void        set_gc_usage_sensor_obj(instanceHandle s);
 133   void        set_last_collection_usage(MemoryUsage u)  { _after_gc_usage = u; }
 134 
 135   virtual instanceOop get_memory_pool_instance(TRAPS);
 136   virtual MemoryUsage get_memory_usage() = 0;
 137   virtual size_t      used_in_bytes() = 0;
 138   virtual bool        is_collected_pool()         { return false; }
 139   virtual MemoryUsage get_last_collection_usage() { return _after_gc_usage; }
 140 
 141   // GC support
 142   void oops_do(OopClosure* f);
 143 };
 144 
 145 class CollectedMemoryPool : public MemoryPool {
 146 public:
 147   CollectedMemoryPool(const char* name, PoolType type, size_t init_size, size_t max_size, bool support_usage_threshold) :
 148     MemoryPool(name, type, init_size, max_size, support_usage_threshold, true) {};
 149   bool is_collected_pool()            { return true; }
 150 };
 151 
 152 class ContiguousSpacePool : public CollectedMemoryPool {
 153 private:
 154   ContiguousSpace* _space;
 155 
 156 public:
 157   ContiguousSpacePool(ContiguousSpace* space, const char* name, PoolType type, size_t max_size, bool support_usage_threshold);
 158 
 159   ContiguousSpace* space()              { return _space; }
 160   MemoryUsage get_memory_usage();
 161   size_t used_in_bytes();
 162 };
 163 
 164 class SurvivorContiguousSpacePool : public CollectedMemoryPool {
 165 private:
 166   DefNewGeneration* _young_gen;
 167 
 168 public:
 169   SurvivorContiguousSpacePool(DefNewGeneration* young_gen,
 170                               const char* name,
 171                               PoolType type,
 172                               size_t max_size,
 173                               bool support_usage_threshold);
 174 
 175   MemoryUsage get_memory_usage();
 176 
 177   size_t used_in_bytes();
 178   size_t committed_in_bytes();
 179 };
 180 
 181 #if INCLUDE_ALL_GCS
 182 class CompactibleFreeListSpacePool : public CollectedMemoryPool {
 183 private:
 184   CompactibleFreeListSpace* _space;
 185 public:
 186   CompactibleFreeListSpacePool(CompactibleFreeListSpace* space,
 187                                const char* name,
 188                                PoolType type,
 189                                size_t max_size,
 190                                bool support_usage_threshold);
 191 
 192   MemoryUsage get_memory_usage();
 193   size_t used_in_bytes();
 194 };
 195 #endif // INCLUDE_ALL_GCS
 196 
 197 
 198 class GenerationPool : public CollectedMemoryPool {
 199 private:
 200   Generation* _gen;
 201 public:
 202   GenerationPool(Generation* gen, const char* name, PoolType type, bool support_usage_threshold);
 203 
 204   MemoryUsage get_memory_usage();
 205   size_t used_in_bytes();
 206 };
 207 
 208 class CodeHeapPool: public MemoryPool {
 209 private:
 210   CodeHeap* _codeHeap;
 211 public:
 212   CodeHeapPool(CodeHeap* codeHeap, const char* name, bool support_usage_threshold);
 213   MemoryUsage get_memory_usage();
 214   size_t used_in_bytes()            { return _codeHeap->allocated_capacity(); }
 215 };
 216 
 217 class MetaspacePool : public MemoryPool {
 218   size_t calculate_max_size() const;
 219  public:
 220   MetaspacePool();
 221   MemoryUsage get_memory_usage();
 222   size_t used_in_bytes();
 223 };
 224 
 225 class CompressedKlassSpacePool : public MemoryPool {
 226  public:
 227   CompressedKlassSpacePool();
 228   MemoryUsage get_memory_usage();
 229   size_t used_in_bytes();
 230 };
 231 
 232 #endif // SHARE_VM_SERVICES_MEMORYPOOL_HPP