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_MEMORYMANAGER_HPP 26 #define SHARE_VM_SERVICES_MEMORYMANAGER_HPP 27 28 #include "gc/shared/gcCause.hpp" 29 #include "memory/allocation.hpp" 30 #include "oops/oopsHierarchy.hpp" 31 #include "runtime/handles.hpp" 32 #include "runtime/timer.hpp" 33 #include "services/memoryUsage.hpp" 34 35 // A memory manager is responsible for managing one or more memory pools. 36 // The garbage collector is one type of memory managers responsible 37 // for reclaiming memory occupied by unreachable objects. A Java virtual 38 // machine may have one or more memory managers. It may 39 // add or remove memory managers during execution. 40 // A memory pool can be managed by more than one memory managers. 41 42 class MemoryPool; 43 class GCMemoryManager; 44 class OopClosure; 45 46 class MemoryManager : public CHeapObj<mtInternal> { 47 private: 48 enum { 49 max_num_pools = 10 50 }; 51 52 MemoryPool* _pools[max_num_pools]; 53 int _num_pools; 54 55 protected: 56 volatile instanceOop _memory_mgr_obj; 57 58 public: 59 MemoryManager(); 60 61 int num_memory_pools() const { return _num_pools; } 62 MemoryPool* get_memory_pool(int index) { 63 assert(index >= 0 && index < _num_pools, "Invalid index"); 64 return _pools[index]; 65 } 66 67 void add_pool(MemoryPool* pool); 68 69 bool is_manager(instanceHandle mh) { return mh() == _memory_mgr_obj; } 70 71 virtual instanceOop get_memory_manager_instance(TRAPS); 72 virtual bool is_gc_memory_manager() { return false; } 73 virtual const char* name() = 0; 74 75 // GC support 76 void oops_do(OopClosure* f); 77 78 // Static factory methods to get a memory manager of a specific type 79 static MemoryManager* get_code_cache_memory_manager(); 80 static MemoryManager* get_metaspace_memory_manager(); 81 static GCMemoryManager* get_copy_memory_manager(); 82 static GCMemoryManager* get_msc_memory_manager(); 83 static GCMemoryManager* get_parnew_memory_manager(); 84 static GCMemoryManager* get_cms_memory_manager(); 85 static GCMemoryManager* get_psScavenge_memory_manager(); 86 static GCMemoryManager* get_psMarkSweep_memory_manager(); 87 static GCMemoryManager* get_g1YoungGen_memory_manager(); 88 static GCMemoryManager* get_g1OldGen_memory_manager(); 89 }; 90 91 class CodeCacheMemoryManager : public MemoryManager { 92 private: 93 public: 94 CodeCacheMemoryManager() : MemoryManager() {} 95 96 const char* name() { return "CodeCacheManager"; } 97 }; 98 99 class MetaspaceMemoryManager : public MemoryManager { 100 public: 101 MetaspaceMemoryManager() : MemoryManager() {} 102 103 const char* name() { return "Metaspace Manager"; } 104 }; 105 106 class GCStatInfo : public ResourceObj { 107 private: 108 size_t _index; 109 jlong _start_time; 110 jlong _end_time; 111 112 // We keep memory usage of all memory pools 113 MemoryUsage* _before_gc_usage_array; 114 MemoryUsage* _after_gc_usage_array; 115 int _usage_array_size; 116 117 void set_gc_usage(int pool_index, MemoryUsage, bool before_gc); 118 119 public: 120 GCStatInfo(int num_pools); 121 ~GCStatInfo(); 122 123 size_t gc_index() { return _index; } 124 jlong start_time() { return _start_time; } 125 jlong end_time() { return _end_time; } 126 int usage_array_size() { return _usage_array_size; } 127 MemoryUsage before_gc_usage_for_pool(int pool_index) { 128 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking"); 129 return _before_gc_usage_array[pool_index]; 130 } 131 MemoryUsage after_gc_usage_for_pool(int pool_index) { 132 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking"); 133 return _after_gc_usage_array[pool_index]; 134 } 135 136 MemoryUsage* before_gc_usage_array() { return _before_gc_usage_array; } 137 MemoryUsage* after_gc_usage_array() { return _after_gc_usage_array; } 138 139 void set_index(size_t index) { _index = index; } 140 void set_start_time(jlong time) { _start_time = time; } 141 void set_end_time(jlong time) { _end_time = time; } 142 void set_before_gc_usage(int pool_index, MemoryUsage usage) { 143 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking"); 144 set_gc_usage(pool_index, usage, true /* before gc */); 145 } 146 void set_after_gc_usage(int pool_index, MemoryUsage usage) { 147 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking"); 148 set_gc_usage(pool_index, usage, false /* after gc */); 149 } 150 151 void clear(); 152 }; 153 154 class GCMemoryManager : public MemoryManager { 155 private: 156 // TODO: We should unify the GCCounter and GCMemoryManager statistic 157 size_t _num_collections; 158 elapsedTimer _accumulated_timer; 159 elapsedTimer _gc_timer; // for measuring every GC duration 160 GCStatInfo* _last_gc_stat; 161 Mutex* _last_gc_lock; 162 GCStatInfo* _current_gc_stat; 163 int _num_gc_threads; 164 volatile bool _notification_enabled; 165 public: 166 GCMemoryManager(); 167 ~GCMemoryManager(); 168 169 void initialize_gc_stat_info(); 170 171 bool is_gc_memory_manager() { return true; } 172 jlong gc_time_ms() { return _accumulated_timer.milliseconds(); } 173 size_t gc_count() { return _num_collections; } 174 int num_gc_threads() { return _num_gc_threads; } 175 void set_num_gc_threads(int count) { _num_gc_threads = count; } 176 177 void gc_begin(bool recordGCBeginTime, bool recordPreGCUsage, 178 bool recordAccumulatedGCTime); 179 void gc_end(bool recordPostGCUsage, bool recordAccumulatedGCTime, 180 bool recordGCEndTime, bool countCollection, GCCause::Cause cause); 181 182 void reset_gc_stat() { _num_collections = 0; _accumulated_timer.reset(); } 183 184 // Copy out _last_gc_stat to the given destination, returning 185 // the collection count. Zero signifies no gc has taken place. 186 size_t get_last_gc_stat(GCStatInfo* dest); 187 188 void set_notification_enabled(bool enabled) { _notification_enabled = enabled; } 189 bool is_notification_enabled() { return _notification_enabled; } 190 }; 191 192 // These subclasses of GCMemoryManager are defined to include 193 // GC-specific information. 194 // TODO: Add GC-specific information 195 class CopyMemoryManager : public GCMemoryManager { 196 private: 197 public: 198 CopyMemoryManager() : GCMemoryManager() {} 199 200 const char* name() { return "Copy"; } 201 }; 202 203 class MSCMemoryManager : public GCMemoryManager { 204 private: 205 public: 206 MSCMemoryManager() : GCMemoryManager() {} 207 208 const char* name() { return "MarkSweepCompact"; } 209 }; 210 211 class ParNewMemoryManager : public GCMemoryManager { 212 private: 213 public: 214 ParNewMemoryManager() : GCMemoryManager() {} 215 216 const char* name() { return "ParNew"; } 217 }; 218 219 class CMSMemoryManager : public GCMemoryManager { 220 private: 221 public: 222 CMSMemoryManager() : GCMemoryManager() {} 223 224 const char* name() { return "ConcurrentMarkSweep";} 225 }; 226 227 class PSScavengeMemoryManager : public GCMemoryManager { 228 private: 229 public: 230 PSScavengeMemoryManager() : GCMemoryManager() {} 231 232 const char* name() { return "PS Scavenge"; } 233 }; 234 235 class PSMarkSweepMemoryManager : public GCMemoryManager { 236 private: 237 public: 238 PSMarkSweepMemoryManager() : GCMemoryManager() {} 239 240 const char* name() { return "PS MarkSweep"; } 241 }; 242 243 class G1YoungGenMemoryManager : public GCMemoryManager { 244 private: 245 public: 246 G1YoungGenMemoryManager() : GCMemoryManager() {} 247 248 const char* name() { return "G1 Young Generation"; } 249 }; 250 251 class G1OldGenMemoryManager : public GCMemoryManager { 252 private: 253 public: 254 G1OldGenMemoryManager() : GCMemoryManager() {} 255 256 const char* name() { return "G1 Old Generation"; } 257 }; 258 259 #endif // SHARE_VM_SERVICES_MEMORYMANAGER_HPP