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