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 manager and code heap pools for the CodeCache
  65   static MemoryManager*                 _code_cache_manager;
  66   static GrowableArray<MemoryPool*>*    _code_heap_pools;
  67 
  68   static MemoryPool*                    _metaspace_pool;
  69   static MemoryPool*                    _compressed_class_pool;
  70 
  71 public:
  72   static void set_universe_heap(CollectedHeap* heap);
  73   static void add_code_heap_memory_pool(CodeHeap* heap, const char* name);
  74   static void add_metaspace_memory_pools();
  75 
  76   static MemoryPool*    get_memory_pool(instanceHandle pool);
  77   static MemoryManager* get_memory_manager(instanceHandle mgr);
  78 
  79   static const int num_memory_pools() {
  80     return _pools_list->length();
  81   }
  82   static const int num_memory_managers() {
  83     return _managers_list->length();
  84   }
  85 
  86   static MemoryPool* get_memory_pool(int index) {
  87     return _pools_list->at(index);
  88   }
  89 
  90   static MemoryManager* get_memory_manager(int index) {
  91     return _managers_list->at(index);
  92   }
  93 
  94   static void track_memory_usage();
  95   static void track_code_cache_memory_usage() {
  96     // Track memory pool usage of all CodeCache memory pools
  97     for (int i = 0; i < _code_heap_pools->length(); ++i) {
  98       track_memory_pool_usage(_code_heap_pools->at(i));
  99     }
 100   }
 101   static void track_metaspace_memory_usage() {
 102     track_memory_pool_usage(_metaspace_pool);
 103   }
 104   static void track_compressed_class_memory_usage() {
 105     track_memory_pool_usage(_compressed_class_pool);
 106   }
 107   static void track_memory_pool_usage(MemoryPool* pool);
 108 
 109   static void gc_begin(GCMemoryManager* mgr, bool recordGCBeginTime,
 110                        bool recordAccumulatedGCTime,
 111                        bool recordPreGCUsage, bool recordPeakUsage);
 112   static void gc_end(GCMemoryManager* mgr, bool recordPostGCUsage,
 113                      bool recordAccumulatedGCTime,
 114                      bool recordGCEndTime, bool countCollection,
 115                      GCCause::Cause cause);
 116 
 117   static void oops_do(OopClosure* f);
 118 
 119   static bool get_verbose() { return log_is_enabled(Info, gc); }
 120   static bool set_verbose(bool verbose);
 121 
 122   // Create an instance of java/lang/management/MemoryUsage
 123   static Handle create_MemoryUsage_obj(MemoryUsage usage, TRAPS);
 124 };
 125 
 126 class TraceMemoryManagerStats : public StackObj {
 127 private:
 128   GCMemoryManager* _gc_mem_mgr;
 129   bool         _recordGCBeginTime;
 130   bool         _recordPreGCUsage;
 131   bool         _recordPeakUsage;
 132   bool         _recordPostGCUsage;
 133   bool         _recordAccumulatedGCTime;
 134   bool         _recordGCEndTime;
 135   bool         _countCollection;
 136   GCCause::Cause _cause;
 137 public:
 138   TraceMemoryManagerStats() {}
 139   TraceMemoryManagerStats(GCMemoryManager* gc_mem_mgr,
 140                           GCCause::Cause cause,
 141                           bool recordGCBeginTime = true,
 142                           bool recordPreGCUsage = true,
 143                           bool recordPeakUsage = true,
 144                           bool recordPostGCUsage = true,
 145                           bool recordAccumulatedGCTime = true,
 146                           bool recordGCEndTime = true,
 147                           bool countCollection = true);
 148 
 149   void initialize(GCMemoryManager* gc_mem_mgr,
 150                   GCCause::Cause cause,
 151                   bool recordGCBeginTime,
 152                   bool recordPreGCUsage,
 153                   bool recordPeakUsage,
 154                   bool recordPostGCUsage,
 155                   bool recordAccumulatedGCTime,
 156                   bool recordGCEndTime,
 157                   bool countCollection);
 158 
 159   ~TraceMemoryManagerStats();
 160 };
 161 
 162 #endif // SHARE_VM_SERVICES_MEMORYSERVICE_HPP