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 "memory/allocation.hpp" 31 #include "runtime/handles.hpp" 32 #include "services/memoryUsage.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 class ShenandoahHeap; 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 static void add_generation_memory_pool(Generation* gen, 76 MemoryManager* major_mgr, 77 MemoryManager* minor_mgr); 78 static void add_generation_memory_pool(Generation* gen, 79 MemoryManager* major_mgr) { 80 add_generation_memory_pool(gen, major_mgr, NULL); 81 } 82 83 84 static void add_psYoung_memory_pool(PSYoungGen* young_gen, 85 MemoryManager* major_mgr, 86 MemoryManager* minor_mgr); 87 static void add_psOld_memory_pool(PSOldGen* old_gen, 88 MemoryManager* mgr); 89 90 static void add_g1YoungGen_memory_pool(G1CollectedHeap* g1h, 91 MemoryManager* major_mgr, 92 MemoryManager* minor_mgr); 93 static void add_g1OldGen_memory_pool(G1CollectedHeap* g1h, 94 MemoryManager* mgr); 95 96 static void add_shenandoah_memory_pool(ShenandoahHeap* pgc, 97 MemoryManager* mgr); 98 99 static MemoryPool* add_space(ContiguousSpace* space, 100 const char* name, 101 bool is_heap, 102 size_t max_size, 103 bool support_usage_threshold); 104 static MemoryPool* add_survivor_spaces(DefNewGeneration* young_gen, 105 const char* name, 106 bool is_heap, 107 size_t max_size, 108 bool support_usage_threshold); 109 static MemoryPool* add_gen(Generation* gen, 110 const char* name, 111 bool is_heap, 112 bool support_usage_threshold); 113 static MemoryPool* add_cms_space(CompactibleFreeListSpace* space, 114 const char* name, 115 bool is_heap, 116 size_t max_size, 117 bool support_usage_threshold); 118 119 static void add_gen_collected_heap_info(GenCollectedHeap* heap); 120 static void add_parallel_scavenge_heap_info(ParallelScavengeHeap* heap); 121 static void add_g1_heap_info(G1CollectedHeap* g1h); 122 static void add_shenandoah_heap_info(ShenandoahHeap* heap); 123 124 public: 125 static void set_universe_heap(CollectedHeap* heap); 126 static void add_code_heap_memory_pool(CodeHeap* heap, const char* name); 127 static void add_metaspace_memory_pools(); 128 129 static MemoryPool* get_memory_pool(instanceHandle pool); 130 static MemoryManager* get_memory_manager(instanceHandle mgr); 131 132 static const int num_memory_pools() { 133 return _pools_list->length(); 134 } 135 static const int num_memory_managers() { 136 return _managers_list->length(); 137 } 138 139 static MemoryPool* get_memory_pool(int index) { 140 return _pools_list->at(index); 141 } 142 143 static MemoryManager* get_memory_manager(int index) { 144 return _managers_list->at(index); 145 } 146 147 static void track_memory_usage(); 148 static void track_code_cache_memory_usage() { 149 // Track memory pool usage of all CodeCache memory pools 150 for (int i = 0; i < _code_heap_pools->length(); ++i) { 151 track_memory_pool_usage(_code_heap_pools->at(i)); 152 } 153 } 154 static void track_metaspace_memory_usage() { 155 track_memory_pool_usage(_metaspace_pool); 156 } 157 static void track_compressed_class_memory_usage() { 158 track_memory_pool_usage(_compressed_class_pool); 159 } 160 static void track_memory_pool_usage(MemoryPool* pool); 161 162 static void gc_begin(bool fullGC, bool recordGCBeginTime, 163 bool recordAccumulatedGCTime, 164 bool recordPreGCUsage, bool recordPeakUsage); 165 static void gc_end(bool fullGC, bool recordPostGCUsage, 166 bool recordAccumulatedGCTime, 167 bool recordGCEndTime, bool countCollection, 168 GCCause::Cause cause); 169 170 static void oops_do(OopClosure* f); 171 172 static bool get_verbose() { return PrintGC; } 173 static bool set_verbose(bool verbose); 174 175 // Create an instance of java/lang/management/MemoryUsage 176 static Handle create_MemoryUsage_obj(MemoryUsage usage, TRAPS); 177 178 static const GCMemoryManager* get_minor_gc_manager() { 179 return _minor_gc_manager; 180 } 181 182 static const GCMemoryManager* get_major_gc_manager() { 183 return _major_gc_manager; 184 } 185 }; 186 187 class TraceMemoryManagerStats : public StackObj { 188 private: 189 bool _fullGC; 190 bool _recordGCBeginTime; 191 bool _recordPreGCUsage; 192 bool _recordPeakUsage; 193 bool _recordPostGCUsage; 194 bool _recordAccumulatedGCTime; 195 bool _recordGCEndTime; 196 bool _countCollection; 197 GCCause::Cause _cause; 198 public: 199 TraceMemoryManagerStats() {} 200 TraceMemoryManagerStats(bool fullGC, 201 GCCause::Cause cause, 202 bool recordGCBeginTime = true, 203 bool recordPreGCUsage = true, 204 bool recordPeakUsage = true, 205 bool recordPostGCUsage = true, 206 bool recordAccumulatedGCTime = true, 207 bool recordGCEndTime = true, 208 bool countCollection = true); 209 210 void initialize(bool fullGC, 211 GCCause::Cause cause, 212 bool recordGCBeginTime, 213 bool recordPreGCUsage, 214 bool recordPeakUsage, 215 bool recordPostGCUsage, 216 bool recordAccumulatedGCTime, 217 bool recordGCEndTime, 218 bool countCollection); 219 220 TraceMemoryManagerStats(Generation::Name kind, GCCause::Cause cause); 221 ~TraceMemoryManagerStats(); 222 }; 223 224 #endif // SHARE_VM_SERVICES_MEMORYSERVICE_HPP