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