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