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