1 /*
  2  * Copyright (c) 2003, 2017, 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 MemoryManager;
 41 class SensorInfo;
 42 class ThresholdSupport;
 43 
 44 class MemoryPool : public CHeapObj<mtInternal> {
 45   friend class MemoryManager;
 46  public:
 47   enum PoolType {
 48     Heap    = 1,
 49     NonHeap = 2
 50   };
 51 
 52  private:
 53   enum {
 54     max_num_managers = 5
 55   };
 56 
 57   // We could make some of the following as performance counters
 58   // for external monitoring.
 59   const char*      _name;
 60   PoolType         _type;
 61   size_t           _initial_size;
 62   size_t           _max_size;
 63   bool             _available_for_allocation; // Default is true
 64   MemoryManager*   _managers[max_num_managers];
 65   int              _num_managers;
 66   MemoryUsage      _peak_usage;               // Peak memory usage
 67   MemoryUsage      _after_gc_usage;           // After GC memory usage
 68 
 69   ThresholdSupport* _usage_threshold;
 70   ThresholdSupport* _gc_usage_threshold;
 71 
 72   SensorInfo*      _usage_sensor;
 73   SensorInfo*      _gc_usage_sensor;
 74 
 75   volatile instanceOop _memory_pool_obj;
 76 
 77   void add_manager(MemoryManager* mgr);
 78 
 79  public:
 80   MemoryPool(const char* name,
 81              PoolType type,
 82              size_t init_size,
 83              size_t max_size,
 84              bool support_usage_threshold,
 85              bool support_gc_threshold);
 86 
 87   const char* name()                       { return _name; }
 88   bool        is_heap()                    { return _type == Heap; }
 89   bool        is_non_heap()                { return _type == NonHeap; }
 90   size_t      initial_size()   const       { return _initial_size; }
 91   int         num_memory_managers() const  { return _num_managers; }
 92   // max size could be changed
 93   virtual size_t max_size()    const       { return _max_size; }
 94 
 95   bool is_pool(instanceHandle pool) { return (pool() == _memory_pool_obj); }
 96 
 97   bool available_for_allocation()   { return _available_for_allocation; }
 98   bool set_available_for_allocation(bool value) {
 99     bool prev = _available_for_allocation;
100     _available_for_allocation = value;
101     return prev;
102   }
103 
104   MemoryManager* get_memory_manager(int index) {
105     assert(index >= 0 && index < _num_managers, "Invalid index");
106     return _managers[index];
107   }
108 
109   // Records current memory usage if it's a peak usage
110   void record_peak_memory_usage();
111 
112   MemoryUsage get_peak_memory_usage() {
113     // check current memory usage first and then return peak usage
114     record_peak_memory_usage();
115     return _peak_usage;
116   }
117   void        reset_peak_memory_usage() {
118     _peak_usage = get_memory_usage();
119   }
120 
121   ThresholdSupport* usage_threshold()      { return _usage_threshold; }
122   ThresholdSupport* gc_usage_threshold()   { return _gc_usage_threshold; }
123 
124   SensorInfo*       usage_sensor()         {  return _usage_sensor; }
125   SensorInfo*       gc_usage_sensor()      { return _gc_usage_sensor; }
126 
127   void        set_usage_sensor_obj(instanceHandle s);
128   void        set_gc_usage_sensor_obj(instanceHandle s);
129   void        set_last_collection_usage(MemoryUsage u)  { _after_gc_usage = u; }
130 
131   virtual instanceOop get_memory_pool_instance(TRAPS);
132   virtual MemoryUsage get_memory_usage() = 0;
133   virtual size_t      used_in_bytes() = 0;
134   virtual bool        is_collected_pool()         { return false; }
135   virtual MemoryUsage get_last_collection_usage() { return _after_gc_usage; }
136 
137   // GC support
138   void oops_do(OopClosure* f);
139 };
140 
141 class CollectedMemoryPool : public MemoryPool {
142 public:
143   CollectedMemoryPool(const char* name, size_t init_size, size_t max_size, bool support_usage_threshold) :
144     MemoryPool(name, MemoryPool::Heap, init_size, max_size, support_usage_threshold, true) {};
145   bool is_collected_pool()            { return true; }
146 };
147 
148 class CodeHeapPool: public MemoryPool {
149 private:
150   CodeHeap* _codeHeap;
151 public:
152   CodeHeapPool(CodeHeap* codeHeap, const char* name, bool support_usage_threshold);
153   MemoryUsage get_memory_usage();
154   size_t used_in_bytes()            { return _codeHeap->allocated_capacity(); }
155 };
156 
157 class MetaspacePool : public MemoryPool {
158   size_t calculate_max_size() const;
159  public:
160   MetaspacePool();
161   MemoryUsage get_memory_usage();
162   size_t used_in_bytes();
163 };
164 
165 class CompressedKlassSpacePool : public MemoryPool {
166  public:
167   CompressedKlassSpacePool();
168   MemoryUsage get_memory_usage();
169   size_t used_in_bytes();
170 };
171 
172 #endif // SHARE_VM_SERVICES_MEMORYPOOL_HPP