| 1 | /* | 
|---|
| 2 | * Copyright (c) 2003, 2019, 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 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 | virtual ~MemoryPool() { } | 
|---|
| 89 |  | 
|---|
| 90 | const char* name()                       { return _name; } | 
|---|
| 91 | bool        is_heap()                    { return _type == Heap; } | 
|---|
| 92 | bool        is_non_heap()                { return _type == NonHeap; } | 
|---|
| 93 | size_t      initial_size()   const       { return _initial_size; } | 
|---|
| 94 | int         num_memory_managers() const  { return _num_managers; } | 
|---|
| 95 | // max size could be changed | 
|---|
| 96 | virtual size_t max_size()    const       { return _max_size; } | 
|---|
| 97 |  | 
|---|
| 98 | bool is_pool(instanceHandle pool) { return oopDesc::equals(pool(), _memory_pool_obj); } | 
|---|
| 99 |  | 
|---|
| 100 | bool available_for_allocation()   { return _available_for_allocation; } | 
|---|
| 101 | bool set_available_for_allocation(bool value) { | 
|---|
| 102 | bool prev = _available_for_allocation; | 
|---|
| 103 | _available_for_allocation = value; | 
|---|
| 104 | return prev; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 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 |  | 
|---|
| 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 | } | 
|---|
| 120 | void        reset_peak_memory_usage() { | 
|---|
| 121 | _peak_usage = get_memory_usage(); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | ThresholdSupport* usage_threshold()      { return _usage_threshold; } | 
|---|
| 125 | ThresholdSupport* gc_usage_threshold()   { return _gc_usage_threshold; } | 
|---|
| 126 |  | 
|---|
| 127 | SensorInfo*       usage_sensor()         {  return _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); | 
|---|
| 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; | 
|---|
| 137 | virtual bool        is_collected_pool()         { return false; } | 
|---|
| 138 | virtual MemoryUsage get_last_collection_usage() { return _after_gc_usage; } | 
|---|
| 139 |  | 
|---|
| 140 | // GC support | 
|---|
| 141 | void oops_do(OopClosure* f); | 
|---|
| 142 | }; | 
|---|
| 143 |  | 
|---|
| 144 | class CollectedMemoryPool : public MemoryPool { | 
|---|
| 145 | public: | 
|---|
| 146 | CollectedMemoryPool(const char* name, size_t init_size, size_t max_size, bool support_usage_threshold) : | 
|---|
| 147 | MemoryPool(name, MemoryPool::Heap, init_size, max_size, support_usage_threshold, true) {}; | 
|---|
| 148 | bool is_collected_pool()            { return true; } | 
|---|
| 149 | }; | 
|---|
| 150 |  | 
|---|
| 151 | class CodeHeapPool: public MemoryPool { | 
|---|
| 152 | private: | 
|---|
| 153 | CodeHeap* _codeHeap; | 
|---|
| 154 | public: | 
|---|
| 155 | CodeHeapPool(CodeHeap* codeHeap, const char* name, bool support_usage_threshold); | 
|---|
| 156 | MemoryUsage get_memory_usage(); | 
|---|
| 157 | size_t used_in_bytes()            { return _codeHeap->allocated_capacity(); } | 
|---|
| 158 | }; | 
|---|
| 159 |  | 
|---|
| 160 | class MetaspacePool : public MemoryPool { | 
|---|
| 161 | size_t calculate_max_size() const; | 
|---|
| 162 | public: | 
|---|
| 163 | MetaspacePool(); | 
|---|
| 164 | MemoryUsage get_memory_usage(); | 
|---|
| 165 | size_t used_in_bytes(); | 
|---|
| 166 | }; | 
|---|
| 167 |  | 
|---|
| 168 | class CompressedKlassSpacePool : public MemoryPool { | 
|---|
| 169 | public: | 
|---|
| 170 | CompressedKlassSpacePool(); | 
|---|
| 171 | MemoryUsage get_memory_usage(); | 
|---|
| 172 | size_t used_in_bytes(); | 
|---|
| 173 | }; | 
|---|
| 174 |  | 
|---|
| 175 | #endif // SHARE_SERVICES_MEMORYPOOL_HPP | 
|---|
| 176 |  | 
|---|