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 "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 class ShenandoahHeap; 51 52 // VM Monitoring and Management Support 53 54 class MemoryService : public AllStatic { 55 private: 56 enum { 57 init_pools_list_size = 10, 58 init_managers_list_size = 5, 59 init_code_heap_pools_size = 9 60 }; 61 62 static GrowableArray<MemoryPool*>* _pools_list; 63 static GrowableArray<MemoryManager*>* _managers_list; 64 65 // memory managers for minor and major GC statistics 66 static GCMemoryManager* _major_gc_manager; 67 static GCMemoryManager* _minor_gc_manager; 68 69 // memory manager and code heap pools for the CodeCache 70 static MemoryManager* _code_cache_manager; 71 static GrowableArray<MemoryPool*>* _code_heap_pools; 72 73 static MemoryPool* _metaspace_pool; 74 static MemoryPool* _compressed_class_pool; 75 76 static void add_generation_memory_pool(Generation* gen, 77 MemoryManager* major_mgr, 78 MemoryManager* minor_mgr); 79 static void add_generation_memory_pool(Generation* gen, 80 MemoryManager* major_mgr) { 81 add_generation_memory_pool(gen, major_mgr, NULL); 82 } 83 84 85 static void add_psYoung_memory_pool(PSYoungGen* young_gen, 86 MemoryManager* major_mgr, 87 MemoryManager* minor_mgr); 88 static void add_psOld_memory_pool(PSOldGen* old_gen, 89 MemoryManager* mgr); 90 91 static void add_g1YoungGen_memory_pool(G1CollectedHeap* g1h, 92 MemoryManager* major_mgr, 93 MemoryManager* minor_mgr); 94 static void add_g1OldGen_memory_pool(G1CollectedHeap* g1h, 95 MemoryManager* mgr); 96 97 static void add_shenandoah_memory_pool(ShenandoahHeap* pgc, 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* young_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 static void add_shenandoah_heap_info(ShenandoahHeap* heap); 124 125 public: 126 static void set_universe_heap(CollectedHeap* heap); 127 static void add_code_heap_memory_pool(CodeHeap* heap, const char* name); 128 static void add_metaspace_memory_pools(); 129 130 static MemoryPool* get_memory_pool(instanceHandle pool); 131 static MemoryManager* get_memory_manager(instanceHandle mgr); 132 133 static const int num_memory_pools() { 134 return _pools_list->length(); 135 } 136 static const int num_memory_managers() { 137 return _managers_list->length(); 138 } 139 140 static MemoryPool* get_memory_pool(int index) { 141 return _pools_list->at(index); 142 } 143 144 static MemoryManager* get_memory_manager(int index) { 145 return _managers_list->at(index); 146 } 147 148 static void track_memory_usage(); 149 static void track_code_cache_memory_usage() { 150 // Track memory pool usage of all CodeCache memory pools 151 for (int i = 0; i < _code_heap_pools->length(); ++i) { 152 track_memory_pool_usage(_code_heap_pools->at(i)); 153 } 154 } 155 static void track_metaspace_memory_usage() { 156 track_memory_pool_usage(_metaspace_pool); 157 } 158 static void track_compressed_class_memory_usage() { 159 track_memory_pool_usage(_compressed_class_pool); 160 } 161 static void track_memory_pool_usage(MemoryPool* pool); 162 163 static void gc_begin(bool fullGC, bool recordGCBeginTime, 164 bool recordAccumulatedGCTime, 165 bool recordPreGCUsage, bool recordPeakUsage); 166 static void gc_end(bool fullGC, bool recordPostGCUsage, 167 bool recordAccumulatedGCTime, 168 bool recordGCEndTime, bool countCollection, 169 GCCause::Cause cause); 170 171 static void oops_do(OopClosure* f); 172 173 static bool get_verbose() { return log_is_enabled(Info, gc); } 174 static bool set_verbose(bool verbose); 175 176 // Create an instance of java/lang/management/MemoryUsage 177 static Handle create_MemoryUsage_obj(MemoryUsage usage, TRAPS); 178 179 static const GCMemoryManager* get_minor_gc_manager() { 180 return _minor_gc_manager; 181 } 182 183 static const GCMemoryManager* get_major_gc_manager() { 184 return _major_gc_manager; 185 } 186 }; 187 188 class TraceMemoryManagerStats : public StackObj { 189 private: 190 bool _fullGC; 191 bool _recordGCBeginTime; 192 bool _recordPreGCUsage; 193 bool _recordPeakUsage; 194 bool _recordPostGCUsage; 195 bool _recordAccumulatedGCTime; 196 bool _recordGCEndTime; 197 bool _countCollection; 198 GCCause::Cause _cause; 199 public: 200 TraceMemoryManagerStats() {} 201 TraceMemoryManagerStats(bool fullGC, 202 GCCause::Cause cause, 203 bool recordGCBeginTime = true, 204 bool recordPreGCUsage = true, 205 bool recordPeakUsage = true, 206 bool recordPostGCUsage = true, 207 bool recordAccumulatedGCTime = true, 208 bool recordGCEndTime = true, 209 bool countCollection = true); 210 211 void initialize(bool fullGC, 212 GCCause::Cause cause, 213 bool recordGCBeginTime, 214 bool recordPreGCUsage, 215 bool recordPeakUsage, 216 bool recordPostGCUsage, 217 bool recordAccumulatedGCTime, 218 bool recordGCEndTime, 219 bool countCollection); 220 221 TraceMemoryManagerStats(Generation::Name kind, GCCause::Cause cause); 222 ~TraceMemoryManagerStats(); 223 }; 224 225 #endif // SHARE_VM_SERVICES_MEMORYSERVICE_HPP