1 /*
   2  * Copyright (c) 2003, 2017, 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 "gc/shared/gcCause.hpp"
  29 #include "gc/shared/generation.hpp"
  30 #include "logging/log.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "runtime/handles.hpp"
  33 #include "services/memoryUsage.hpp"
  34 
  35 // Forward declaration
  36 class MemoryPool;
  37 class MemoryManager;
  38 class GCMemoryManager;
  39 class CollectedHeap;
  40 class Generation;
  41 class DefNewGeneration;
  42 class PSYoungGen;
  43 class PSOldGen;
  44 class CodeHeap;
  45 class ContiguousSpace;
  46 class CompactibleFreeListSpace;
  47 class GenCollectedHeap;
  48 class ParallelScavengeHeap;
  49 class G1CollectedHeap;
  50 
  51 // VM Monitoring and Management Support
  52 
  53 class MemoryService : public AllStatic {
  54 private:
  55   enum {
  56     init_pools_list_size = 10,
  57     init_managers_list_size = 5,
  58     init_code_heap_pools_size = 9
  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   // memory manager and code heap pools for the CodeCache
  69   static MemoryManager*                 _code_cache_manager;
  70   static GrowableArray<MemoryPool*>*    _code_heap_pools;
  71 
  72   static MemoryPool*                    _metaspace_pool;
  73   static MemoryPool*                    _compressed_class_pool;
  74 
  75 public:
  76   static void set_universe_heap(CollectedHeap* heap);
  77   static void add_code_heap_memory_pool(CodeHeap* heap, const char* name);
  78   static void add_metaspace_memory_pools();
  79 
  80   static MemoryPool*    get_memory_pool(instanceHandle pool);
  81   static MemoryManager* get_memory_manager(instanceHandle mgr);
  82 
  83   static const int num_memory_pools() {
  84     return _pools_list->length();
  85   }
  86   static const int num_memory_managers() {
  87     return _managers_list->length();
  88   }
  89 
  90   static MemoryPool* get_memory_pool(int index) {
  91     return _pools_list->at(index);
  92   }
  93 
  94   static MemoryManager* get_memory_manager(int index) {
  95     return _managers_list->at(index);
  96   }
  97 
  98   static void track_memory_usage();
  99   static void track_code_cache_memory_usage() {
 100     // Track memory pool usage of all CodeCache memory pools
 101     for (int i = 0; i < _code_heap_pools->length(); ++i) {
 102       track_memory_pool_usage(_code_heap_pools->at(i));
 103     }
 104   }
 105   static void track_metaspace_memory_usage() {
 106     track_memory_pool_usage(_metaspace_pool);
 107   }
 108   static void track_compressed_class_memory_usage() {
 109     track_memory_pool_usage(_compressed_class_pool);
 110   }
 111   static void track_memory_pool_usage(MemoryPool* pool);
 112 
 113   static void gc_begin(bool fullGC, bool recordGCBeginTime,
 114                        bool recordAccumulatedGCTime,
 115                        bool recordPreGCUsage, bool recordPeakUsage);
 116   static void gc_end(bool fullGC, bool recordPostGCUsage,
 117                      bool recordAccumulatedGCTime,
 118                      bool recordGCEndTime, bool countCollection,
 119                      GCCause::Cause cause);
 120 
 121   static void oops_do(OopClosure* f);
 122 
 123   static bool get_verbose() { return log_is_enabled(Info, gc); }
 124   static bool set_verbose(bool verbose);
 125 
 126   // Create an instance of java/lang/management/MemoryUsage
 127   static Handle create_MemoryUsage_obj(MemoryUsage usage, TRAPS);
 128 
 129   static const GCMemoryManager* get_minor_gc_manager() {
 130       return _minor_gc_manager;
 131   }
 132 
 133   static const GCMemoryManager* get_major_gc_manager() {
 134       return _major_gc_manager;
 135   }
 136 };
 137 
 138 class TraceMemoryManagerStats : public StackObj {
 139 private:
 140   bool         _fullGC;
 141   bool         _recordGCBeginTime;
 142   bool         _recordPreGCUsage;
 143   bool         _recordPeakUsage;
 144   bool         _recordPostGCUsage;
 145   bool         _recordAccumulatedGCTime;
 146   bool         _recordGCEndTime;
 147   bool         _countCollection;
 148   GCCause::Cause _cause;
 149 public:
 150   TraceMemoryManagerStats() {}
 151   TraceMemoryManagerStats(bool fullGC,
 152                           GCCause::Cause cause,
 153                           bool recordGCBeginTime = true,
 154                           bool recordPreGCUsage = true,
 155                           bool recordPeakUsage = true,
 156                           bool recordPostGCUsage = true,
 157                           bool recordAccumulatedGCTime = true,
 158                           bool recordGCEndTime = true,
 159                           bool countCollection = true);
 160 
 161   void initialize(bool fullGC,
 162                   GCCause::Cause cause,
 163                   bool recordGCBeginTime,
 164                   bool recordPreGCUsage,
 165                   bool recordPeakUsage,
 166                   bool recordPostGCUsage,
 167                   bool recordAccumulatedGCTime,
 168                   bool recordGCEndTime,
 169                   bool countCollection);
 170 
 171   TraceMemoryManagerStats(Generation::Name kind, GCCause::Cause cause);
 172   ~TraceMemoryManagerStats();
 173 };
 174 
 175 #endif // SHARE_VM_SERVICES_MEMORYSERVICE_HPP