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