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