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