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