1 /*
   2  * Copyright (c) 2003, 2006, 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 // Forward declaration
  26 class MemoryPool;
  27 class MemoryManager;
  28 class GCMemoryManager;
  29 class CollectedHeap;
  30 class Generation;
  31 class DefNewGeneration;
  32 class PSYoungGen;
  33 class PSOldGen;
  34 class PSPermGen;
  35 class CodeHeap;
  36 class ContiguousSpace;
  37 class CompactibleFreeListSpace;
  38 class PermanentGenerationSpec;
  39 class GenCollectedHeap;
  40 class ParallelScavengeHeap;
  41 class CompactingPermGenGen;
  42 class CMSPermGenGen;
  43 class G1CollectedHeap;
  44 
  45 // VM Monitoring and Management Support
  46 
  47 class MemoryService : public AllStatic {
  48 private:
  49   enum {
  50     init_pools_list_size = 10,
  51     init_managers_list_size = 5
  52   };
  53 
  54   // index for minor and major generations
  55   enum {
  56     minor = 0,
  57     major = 1,
  58     n_gens = 2
  59   };
  60 
  61   static GrowableArray<MemoryPool*>*    _pools_list;
  62   static GrowableArray<MemoryManager*>* _managers_list;
  63 
  64   // memory managers for minor and major GC statistics
  65   static GCMemoryManager*               _major_gc_manager;
  66   static GCMemoryManager*               _minor_gc_manager;
  67 
  68   // Code heap memory pool
  69   static MemoryPool*                    _code_heap_pool;
  70 
  71   static void add_generation_memory_pool(Generation* gen,
  72                                          MemoryManager* major_mgr,
  73                                          MemoryManager* minor_mgr);
  74   static void add_generation_memory_pool(Generation* gen,
  75                                          MemoryManager* major_mgr) {
  76     add_generation_memory_pool(gen, major_mgr, NULL);
  77   }
  78 
  79   static void add_compact_perm_gen_memory_pool(CompactingPermGenGen* perm_gen,
  80                                                MemoryManager* mgr);
  81   static void add_cms_perm_gen_memory_pool(CMSPermGenGen* perm_gen,
  82                                            MemoryManager* mgr);
  83 
  84   static void add_psYoung_memory_pool(PSYoungGen* gen,
  85                                       MemoryManager* major_mgr,
  86                                       MemoryManager* minor_mgr);
  87   static void add_psOld_memory_pool(PSOldGen* gen,
  88                                     MemoryManager* mgr);
  89   static void add_psPerm_memory_pool(PSPermGen* perm,
  90                                      MemoryManager* mgr);
  91 
  92   static void add_g1YoungGen_memory_pool(G1CollectedHeap* g1h,
  93                                          MemoryManager* major_mgr,
  94                                          MemoryManager* minor_mgr);
  95   static void add_g1OldGen_memory_pool(G1CollectedHeap* g1h,
  96                                        MemoryManager* mgr);
  97   static void add_g1PermGen_memory_pool(G1CollectedHeap* g1h,
  98                                         MemoryManager* mgr);
  99 
 100   static MemoryPool* add_space(ContiguousSpace* space,
 101                                const char* name,
 102                                bool is_heap,
 103                                size_t max_size,
 104                                bool support_usage_threshold);
 105   static MemoryPool* add_survivor_spaces(DefNewGeneration* gen,
 106                                          const char* name,
 107                                          bool is_heap,
 108                                          size_t max_size,
 109                                          bool support_usage_threshold);
 110   static MemoryPool* add_gen(Generation* gen,
 111                              const char* name,
 112                              bool is_heap,
 113                              bool support_usage_threshold);
 114   static MemoryPool* add_cms_space(CompactibleFreeListSpace* space,
 115                                    const char* name,
 116                                    bool is_heap,
 117                                    size_t max_size,
 118                                    bool support_usage_threshold);
 119 
 120   static void add_gen_collected_heap_info(GenCollectedHeap* heap);
 121   static void add_parallel_scavenge_heap_info(ParallelScavengeHeap* heap);
 122   static void add_g1_heap_info(G1CollectedHeap* g1h);
 123 
 124 public:
 125   static void set_universe_heap(CollectedHeap* heap);
 126   static void add_code_heap_memory_pool(CodeHeap* heap);
 127 
 128   static MemoryPool*    get_memory_pool(instanceHandle pool);
 129   static MemoryManager* get_memory_manager(instanceHandle mgr);
 130 
 131   static const int num_memory_pools() {
 132     return _pools_list->length();
 133   }
 134   static const int num_memory_managers() {
 135     return _managers_list->length();
 136   }
 137 
 138   static MemoryPool* get_memory_pool(int index) {
 139     return _pools_list->at(index);
 140   }
 141 
 142   static MemoryManager* get_memory_manager(int index) {
 143     return _managers_list->at(index);
 144   }
 145 
 146   static void track_memory_usage();
 147   static void track_code_cache_memory_usage() {
 148     track_memory_pool_usage(_code_heap_pool);
 149   }
 150   static void track_memory_pool_usage(MemoryPool* pool);
 151 
 152   static void gc_begin(bool fullGC, bool recordGCBeginTime,
 153                        bool recordAccumulatedGCTime, 
 154                        bool recordPreGCUsage, bool recordPeakUsage);
 155   static void gc_end(bool fullGC, bool recordPostGCUsage,
 156                      bool recordAccumulatedGCTime,
 157                      bool recordGCEndTime, bool countCollection);
 158 
 159 
 160   static void oops_do(OopClosure* f);
 161 
 162   static bool get_verbose() { return PrintGC; }
 163   static bool set_verbose(bool verbose);
 164 
 165   // Create an instance of java/lang/management/MemoryUsage
 166   static Handle create_MemoryUsage_obj(MemoryUsage usage, TRAPS);
 167 };
 168 
 169 class TraceMemoryManagerStats : public StackObj {
 170 private:
 171   bool         _fullGC;
 172   bool         _recordGCBeginTime;
 173   bool         _recordPreGCUsage;
 174   bool         _recordPeakUsage;
 175   bool         _recordPostGCUsage;
 176   bool         _recordAccumulatedGCTime;
 177   bool         _recordGCEndTime;
 178   bool         _countCollection;
 179 
 180 public:
 181   TraceMemoryManagerStats() {}
 182   TraceMemoryManagerStats(bool fullGC,
 183                           bool recordGCBeginTime = true,
 184                           bool recordPreGCUsage = true,
 185                           bool recordPeakUsage = true,
 186                           bool recordPostGCUsage = true,
 187                           bool recordAccumulatedGCTime = true,
 188                           bool recordGCEndTime = true,
 189                           bool countCollection = true);
 190 
 191   void initialize(bool fullGC,
 192                   bool recordGCBeginTime,
 193                   bool recordPreGCUsage,
 194                   bool recordPeakUsage,
 195                   bool recordPostGCUsage,
 196                   bool recordAccumulatedGCTime,
 197                   bool recordGCEndTime,
 198                   bool countCollection);
 199 
 200   TraceMemoryManagerStats(Generation::Name kind);
 201   ~TraceMemoryManagerStats();
 202 };