src/share/vm/services/memoryManager.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6198
55fb97c4c58d
parent 0
f90c822e73f8
child 9637
eef07cd490d4
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_SERVICES_MEMORYMANAGER_HPP
aoqi@0 26 #define SHARE_VM_SERVICES_MEMORYMANAGER_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "runtime/timer.hpp"
aoqi@0 30 #include "services/memoryUsage.hpp"
aoqi@0 31
aoqi@0 32 // A memory manager is responsible for managing one or more memory pools.
aoqi@0 33 // The garbage collector is one type of memory managers responsible
aoqi@0 34 // for reclaiming memory occupied by unreachable objects. A Java virtual
aoqi@0 35 // machine may have one or more memory managers. It may
aoqi@0 36 // add or remove memory managers during execution.
aoqi@0 37 // A memory pool can be managed by more than one memory managers.
aoqi@0 38
aoqi@0 39 class MemoryPool;
aoqi@0 40 class GCMemoryManager;
aoqi@0 41 class OopClosure;
aoqi@0 42
aoqi@0 43 class MemoryManager : public CHeapObj<mtInternal> {
aoqi@0 44 private:
aoqi@0 45 enum {
aoqi@0 46 max_num_pools = 10
aoqi@0 47 };
aoqi@0 48
aoqi@0 49 MemoryPool* _pools[max_num_pools];
aoqi@0 50 int _num_pools;
aoqi@0 51
aoqi@0 52 protected:
aoqi@0 53 volatile instanceOop _memory_mgr_obj;
aoqi@0 54
aoqi@0 55 public:
aoqi@0 56 enum Name {
aoqi@0 57 Abstract,
aoqi@0 58 CodeCache,
aoqi@0 59 Metaspace,
aoqi@0 60 Copy,
aoqi@0 61 MarkSweepCompact,
aoqi@0 62 ParNew,
aoqi@0 63 ConcurrentMarkSweep,
aoqi@0 64 PSScavenge,
aoqi@0 65 PSMarkSweep,
aoqi@0 66 G1YoungGen,
aoqi@0 67 G1OldGen
aoqi@0 68 };
aoqi@0 69
aoqi@0 70 MemoryManager();
aoqi@0 71
aoqi@0 72 int num_memory_pools() const { return _num_pools; }
aoqi@0 73 MemoryPool* get_memory_pool(int index) {
aoqi@0 74 assert(index >= 0 && index < _num_pools, "Invalid index");
aoqi@0 75 return _pools[index];
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 void add_pool(MemoryPool* pool);
aoqi@0 79
aoqi@0 80 bool is_manager(instanceHandle mh) { return mh() == _memory_mgr_obj; }
aoqi@0 81
aoqi@0 82 virtual instanceOop get_memory_manager_instance(TRAPS);
aoqi@0 83 virtual MemoryManager::Name kind() { return MemoryManager::Abstract; }
aoqi@0 84 virtual bool is_gc_memory_manager() { return false; }
aoqi@0 85 virtual const char* name() = 0;
aoqi@0 86
aoqi@0 87 // GC support
aoqi@0 88 void oops_do(OopClosure* f);
aoqi@0 89
aoqi@0 90 // Static factory methods to get a memory manager of a specific type
aoqi@0 91 static MemoryManager* get_code_cache_memory_manager();
aoqi@0 92 static MemoryManager* get_metaspace_memory_manager();
aoqi@0 93 static GCMemoryManager* get_copy_memory_manager();
aoqi@0 94 static GCMemoryManager* get_msc_memory_manager();
aoqi@0 95 static GCMemoryManager* get_parnew_memory_manager();
aoqi@0 96 static GCMemoryManager* get_cms_memory_manager();
aoqi@0 97 static GCMemoryManager* get_psScavenge_memory_manager();
aoqi@0 98 static GCMemoryManager* get_psMarkSweep_memory_manager();
aoqi@0 99 static GCMemoryManager* get_g1YoungGen_memory_manager();
aoqi@0 100 static GCMemoryManager* get_g1OldGen_memory_manager();
aoqi@0 101
aoqi@0 102 };
aoqi@0 103
aoqi@0 104 class CodeCacheMemoryManager : public MemoryManager {
aoqi@0 105 private:
aoqi@0 106 public:
aoqi@0 107 CodeCacheMemoryManager() : MemoryManager() {}
aoqi@0 108
aoqi@0 109 MemoryManager::Name kind() { return MemoryManager::CodeCache; }
aoqi@0 110 const char* name() { return "CodeCacheManager"; }
aoqi@0 111 };
aoqi@0 112
aoqi@0 113 class MetaspaceMemoryManager : public MemoryManager {
aoqi@0 114 public:
aoqi@0 115 MetaspaceMemoryManager() : MemoryManager() {}
aoqi@0 116
aoqi@0 117 MemoryManager::Name kind() { return MemoryManager::Metaspace; }
aoqi@0 118 const char *name() { return "Metaspace Manager"; }
aoqi@0 119 };
aoqi@0 120
aoqi@0 121 class GCStatInfo : public ResourceObj {
aoqi@0 122 private:
aoqi@0 123 size_t _index;
aoqi@0 124 jlong _start_time;
aoqi@0 125 jlong _end_time;
aoqi@0 126
aoqi@0 127 // We keep memory usage of all memory pools
aoqi@0 128 MemoryUsage* _before_gc_usage_array;
aoqi@0 129 MemoryUsage* _after_gc_usage_array;
aoqi@0 130 int _usage_array_size;
aoqi@0 131
aoqi@0 132 void set_gc_usage(int pool_index, MemoryUsage, bool before_gc);
aoqi@0 133
aoqi@0 134 public:
aoqi@0 135 GCStatInfo(int num_pools);
aoqi@0 136 ~GCStatInfo();
aoqi@0 137
aoqi@0 138 size_t gc_index() { return _index; }
aoqi@0 139 jlong start_time() { return _start_time; }
aoqi@0 140 jlong end_time() { return _end_time; }
aoqi@0 141 int usage_array_size() { return _usage_array_size; }
aoqi@0 142 MemoryUsage before_gc_usage_for_pool(int pool_index) {
aoqi@0 143 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");
aoqi@0 144 return _before_gc_usage_array[pool_index];
aoqi@0 145 }
aoqi@0 146 MemoryUsage after_gc_usage_for_pool(int pool_index) {
aoqi@0 147 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");
aoqi@0 148 return _after_gc_usage_array[pool_index];
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 MemoryUsage* before_gc_usage_array() { return _before_gc_usage_array; }
aoqi@0 152 MemoryUsage* after_gc_usage_array() { return _after_gc_usage_array; }
aoqi@0 153
aoqi@0 154 void set_index(size_t index) { _index = index; }
aoqi@0 155 void set_start_time(jlong time) { _start_time = time; }
aoqi@0 156 void set_end_time(jlong time) { _end_time = time; }
aoqi@0 157 void set_before_gc_usage(int pool_index, MemoryUsage usage) {
aoqi@0 158 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");
aoqi@0 159 set_gc_usage(pool_index, usage, true /* before gc */);
aoqi@0 160 }
aoqi@0 161 void set_after_gc_usage(int pool_index, MemoryUsage usage) {
aoqi@0 162 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");
aoqi@0 163 set_gc_usage(pool_index, usage, false /* after gc */);
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 void clear();
aoqi@0 167 };
aoqi@0 168
aoqi@0 169 class GCMemoryManager : public MemoryManager {
aoqi@0 170 private:
aoqi@0 171 // TODO: We should unify the GCCounter and GCMemoryManager statistic
aoqi@0 172 size_t _num_collections;
aoqi@0 173 elapsedTimer _accumulated_timer;
aoqi@0 174 elapsedTimer _gc_timer; // for measuring every GC duration
aoqi@0 175 GCStatInfo* _last_gc_stat;
aoqi@0 176 Mutex* _last_gc_lock;
aoqi@0 177 GCStatInfo* _current_gc_stat;
aoqi@0 178 int _num_gc_threads;
aoqi@0 179 volatile bool _notification_enabled;
aoqi@0 180 public:
aoqi@0 181 GCMemoryManager();
aoqi@0 182 ~GCMemoryManager();
aoqi@0 183
aoqi@0 184 void initialize_gc_stat_info();
aoqi@0 185
aoqi@0 186 bool is_gc_memory_manager() { return true; }
aoqi@0 187 jlong gc_time_ms() { return _accumulated_timer.milliseconds(); }
aoqi@0 188 size_t gc_count() { return _num_collections; }
aoqi@0 189 int num_gc_threads() { return _num_gc_threads; }
aoqi@0 190 void set_num_gc_threads(int count) { _num_gc_threads = count; }
aoqi@0 191
aoqi@0 192 void gc_begin(bool recordGCBeginTime, bool recordPreGCUsage,
aoqi@0 193 bool recordAccumulatedGCTime);
aoqi@0 194 void gc_end(bool recordPostGCUsage, bool recordAccumulatedGCTime,
aoqi@0 195 bool recordGCEndTime, bool countCollection, GCCause::Cause cause);
aoqi@0 196
aoqi@0 197 void reset_gc_stat() { _num_collections = 0; _accumulated_timer.reset(); }
aoqi@0 198
aoqi@0 199 // Copy out _last_gc_stat to the given destination, returning
aoqi@0 200 // the collection count. Zero signifies no gc has taken place.
aoqi@0 201 size_t get_last_gc_stat(GCStatInfo* dest);
aoqi@0 202
aoqi@0 203 void set_notification_enabled(bool enabled) { _notification_enabled = enabled; }
aoqi@0 204 bool is_notification_enabled() { return _notification_enabled; }
aoqi@0 205 virtual MemoryManager::Name kind() = 0;
aoqi@0 206 };
aoqi@0 207
aoqi@0 208 // These subclasses of GCMemoryManager are defined to include
aoqi@0 209 // GC-specific information.
aoqi@0 210 // TODO: Add GC-specific information
aoqi@0 211 class CopyMemoryManager : public GCMemoryManager {
aoqi@0 212 private:
aoqi@0 213 public:
aoqi@0 214 CopyMemoryManager() : GCMemoryManager() {}
aoqi@0 215
aoqi@0 216 MemoryManager::Name kind() { return MemoryManager::Copy; }
aoqi@0 217 const char* name() { return "Copy"; }
aoqi@0 218 };
aoqi@0 219
aoqi@0 220 class MSCMemoryManager : public GCMemoryManager {
aoqi@0 221 private:
aoqi@0 222 public:
aoqi@0 223 MSCMemoryManager() : GCMemoryManager() {}
aoqi@0 224
aoqi@0 225 MemoryManager::Name kind() { return MemoryManager::MarkSweepCompact; }
aoqi@0 226 const char* name() { return "MarkSweepCompact"; }
aoqi@0 227
aoqi@0 228 };
aoqi@0 229
aoqi@0 230 class ParNewMemoryManager : public GCMemoryManager {
aoqi@0 231 private:
aoqi@0 232 public:
aoqi@0 233 ParNewMemoryManager() : GCMemoryManager() {}
aoqi@0 234
aoqi@0 235 MemoryManager::Name kind() { return MemoryManager::ParNew; }
aoqi@0 236 const char* name() { return "ParNew"; }
aoqi@0 237
aoqi@0 238 };
aoqi@0 239
aoqi@0 240 class CMSMemoryManager : public GCMemoryManager {
aoqi@0 241 private:
aoqi@0 242 public:
aoqi@0 243 CMSMemoryManager() : GCMemoryManager() {}
aoqi@0 244
aoqi@0 245 MemoryManager::Name kind() { return MemoryManager::ConcurrentMarkSweep; }
aoqi@0 246 const char* name() { return "ConcurrentMarkSweep";}
aoqi@0 247
aoqi@0 248 };
aoqi@0 249
aoqi@0 250 class PSScavengeMemoryManager : public GCMemoryManager {
aoqi@0 251 private:
aoqi@0 252 public:
aoqi@0 253 PSScavengeMemoryManager() : GCMemoryManager() {}
aoqi@0 254
aoqi@0 255 MemoryManager::Name kind() { return MemoryManager::PSScavenge; }
aoqi@0 256 const char* name() { return "PS Scavenge"; }
aoqi@0 257
aoqi@0 258 };
aoqi@0 259
aoqi@0 260 class PSMarkSweepMemoryManager : public GCMemoryManager {
aoqi@0 261 private:
aoqi@0 262 public:
aoqi@0 263 PSMarkSweepMemoryManager() : GCMemoryManager() {}
aoqi@0 264
aoqi@0 265 MemoryManager::Name kind() { return MemoryManager::PSMarkSweep; }
aoqi@0 266 const char* name() { return "PS MarkSweep"; }
aoqi@0 267 };
aoqi@0 268
aoqi@0 269 class G1YoungGenMemoryManager : public GCMemoryManager {
aoqi@0 270 private:
aoqi@0 271 public:
aoqi@0 272 G1YoungGenMemoryManager() : GCMemoryManager() {}
aoqi@0 273
aoqi@0 274 MemoryManager::Name kind() { return MemoryManager::G1YoungGen; }
aoqi@0 275 const char* name() { return "G1 Young Generation"; }
aoqi@0 276 };
aoqi@0 277
aoqi@0 278 class G1OldGenMemoryManager : public GCMemoryManager {
aoqi@0 279 private:
aoqi@0 280 public:
aoqi@0 281 G1OldGenMemoryManager() : GCMemoryManager() {}
aoqi@0 282
aoqi@0 283 MemoryManager::Name kind() { return MemoryManager::G1OldGen; }
aoqi@0 284 const char* name() { return "G1 Old Generation"; }
aoqi@0 285 };
aoqi@0 286
aoqi@0 287 #endif // SHARE_VM_SERVICES_MEMORYMANAGER_HPP

mercurial