1 /* 2 * Copyright (c) 2003, 2020, 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_SERVICES_MEMORYPOOL_HPP 26 #define SHARE_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 OopHandle _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 ~MemoryPool()88 virtual ~MemoryPool() { } 89 name()90 const char* name() { return _name; } is_heap()91 bool is_heap() { return _type == Heap; } is_non_heap()92 bool is_non_heap() { return _type == NonHeap; } initial_size() const93 size_t initial_size() const { return _initial_size; } num_memory_managers() const94 int num_memory_managers() const { return _num_managers; } 95 // max size could be changed max_size() const96 virtual size_t max_size() const { return _max_size; } 97 98 bool is_pool(instanceHandle pool) const; 99 available_for_allocation()100 bool available_for_allocation() { return _available_for_allocation; } set_available_for_allocation(bool value)101 bool set_available_for_allocation(bool value) { 102 bool prev = _available_for_allocation; 103 _available_for_allocation = value; 104 return prev; 105 } 106 get_memory_manager(int index)107 MemoryManager* get_memory_manager(int index) { 108 assert(index >= 0 && index < _num_managers, "Invalid index"); 109 return _managers[index]; 110 } 111 112 // Records current memory usage if it's a peak usage 113 void record_peak_memory_usage(); 114 get_peak_memory_usage()115 MemoryUsage get_peak_memory_usage() { 116 // check current memory usage first and then return peak usage 117 record_peak_memory_usage(); 118 return _peak_usage; 119 } reset_peak_memory_usage()120 void reset_peak_memory_usage() { 121 _peak_usage = get_memory_usage(); 122 } 123 usage_threshold()124 ThresholdSupport* usage_threshold() { return _usage_threshold; } gc_usage_threshold()125 ThresholdSupport* gc_usage_threshold() { return _gc_usage_threshold; } 126 usage_sensor()127 SensorInfo* usage_sensor() { return _usage_sensor; } gc_usage_sensor()128 SensorInfo* gc_usage_sensor() { return _gc_usage_sensor; } 129 130 void set_usage_sensor_obj(instanceHandle s); 131 void set_gc_usage_sensor_obj(instanceHandle s); set_last_collection_usage(MemoryUsage u)132 void set_last_collection_usage(MemoryUsage u) { _after_gc_usage = u; } 133 134 virtual instanceOop get_memory_pool_instance(TRAPS); 135 virtual MemoryUsage get_memory_usage() = 0; 136 virtual size_t used_in_bytes() = 0; is_collected_pool()137 virtual bool is_collected_pool() { return false; } get_last_collection_usage()138 virtual MemoryUsage get_last_collection_usage() { return _after_gc_usage; } 139 }; 140 141 class CollectedMemoryPool : public MemoryPool { 142 public: CollectedMemoryPool(const char * name,size_t init_size,size_t max_size,bool support_usage_threshold)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) {}; is_collected_pool()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(); used_in_bytes()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_SERVICES_MEMORYPOOL_HPP 173