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