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 #include "precompiled.hpp" 26 #include "classfile/systemDictionary.hpp" 27 #include "classfile/vmSymbols.hpp" 28 #include "gc/shared/collectedHeap.hpp" 29 #include "gc/shared/generation.hpp" 30 #include "logging/logConfiguration.hpp" 31 #include "memory/heap.hpp" 32 #include "memory/memRegion.hpp" 33 #include "oops/oop.inline.hpp" 34 #include "runtime/globals.hpp" 35 #include "runtime/javaCalls.hpp" 36 #include "services/classLoadingService.hpp" 37 #include "services/lowMemoryDetector.hpp" 38 #include "services/management.hpp" 39 #include "services/memoryManager.hpp" 40 #include "services/memoryPool.hpp" 41 #include "services/memoryService.hpp" 42 #include "utilities/growableArray.hpp" 43 #include "utilities/macros.hpp" 44 45 GrowableArray<MemoryPool*>* MemoryService::_pools_list = 46 new (ResourceObj::C_HEAP, mtInternal) GrowableArray<MemoryPool*>(init_pools_list_size, true); 47 GrowableArray<MemoryManager*>* MemoryService::_managers_list = 48 new (ResourceObj::C_HEAP, mtInternal) GrowableArray<MemoryManager*>(init_managers_list_size, true); 49 50 MemoryManager* MemoryService::_code_cache_manager = NULL; 51 GrowableArray<MemoryPool*>* MemoryService::_code_heap_pools = 52 new (ResourceObj::C_HEAP, mtInternal) GrowableArray<MemoryPool*>(init_code_heap_pools_size, true); 53 MemoryPool* MemoryService::_metaspace_pool = NULL; 54 MemoryPool* MemoryService::_compressed_class_pool = NULL; 55 56 class GcThreadCountClosure: public ThreadClosure { 57 private: 58 int _count; 59 public: 60 GcThreadCountClosure() : _count(0) {}; 61 void do_thread(Thread* thread); 62 int count() { return _count; } 63 }; 64 65 void GcThreadCountClosure::do_thread(Thread* thread) { 66 _count++; 67 } 68 69 void MemoryService::set_universe_heap(CollectedHeap* heap) { 70 ResourceMark rm; // For internal allocations in GrowableArray. 71 GrowableArray<MemoryManager*> gc_mem_mgrs = heap->memory_managers(); 72 _managers_list->appendAll(&gc_mem_mgrs); 73 74 GrowableArray<MemoryPool*> gc_mem_pools = heap->memory_pools(); 75 _pools_list->appendAll(&gc_mem_pools); 76 77 // set the GC thread count 78 GcThreadCountClosure gctcc; 79 heap->gc_threads_do(&gctcc); 80 int count = gctcc.count(); 81 82 int num_mgrs = gc_mem_mgrs.length(); 83 for (int i = 0; i < num_mgrs; i++) { 84 assert(gc_mem_mgrs.at(i)->is_gc_memory_manager(), "expect GC memory manager"); 85 GCMemoryManager* gc_mgr = (GCMemoryManager*) gc_mem_mgrs.at(i); 86 if (count > 0) { 87 gc_mgr->set_num_gc_threads(count); 88 } 89 // All memory pools and memory managers are initialized. 90 gc_mgr->initialize_gc_stat_info(); 91 } 92 } 93 94 void MemoryService::add_code_heap_memory_pool(CodeHeap* heap, const char* name) { 95 // Create new memory pool for this heap 96 MemoryPool* code_heap_pool = new CodeHeapPool(heap, name, true /* support_usage_threshold */); 97 98 // Append to lists 99 _code_heap_pools->append(code_heap_pool); 100 _pools_list->append(code_heap_pool); 101 102 if (_code_cache_manager == NULL) { 103 // Create CodeCache memory manager 104 _code_cache_manager = MemoryManager::get_code_cache_memory_manager(); 105 _managers_list->append(_code_cache_manager); 106 } 107 108 _code_cache_manager->add_pool(code_heap_pool); 109 } 110 111 void MemoryService::add_metaspace_memory_pools() { 112 MemoryManager* mgr = MemoryManager::get_metaspace_memory_manager(); 113 114 _metaspace_pool = new MetaspacePool(); 115 mgr->add_pool(_metaspace_pool); 116 _pools_list->append(_metaspace_pool); 117 118 if (UseCompressedClassPointers) { 119 _compressed_class_pool = new CompressedKlassSpacePool(); 120 mgr->add_pool(_compressed_class_pool); 121 _pools_list->append(_compressed_class_pool); 122 } 123 124 _managers_list->append(mgr); 125 } 126 127 MemoryManager* MemoryService::get_memory_manager(instanceHandle mh) { 128 for (int i = 0; i < _managers_list->length(); i++) { 129 MemoryManager* mgr = _managers_list->at(i); 130 if (mgr->is_manager(mh)) { 131 return mgr; 132 } 133 } 134 return NULL; 135 } 136 137 MemoryPool* MemoryService::get_memory_pool(instanceHandle ph) { 138 for (int i = 0; i < _pools_list->length(); i++) { 139 MemoryPool* pool = _pools_list->at(i); 140 if (pool->is_pool(ph)) { 141 return pool; 142 } 143 } 144 return NULL; 145 } 146 147 void MemoryService::track_memory_usage() { 148 // Track the peak memory usage 149 for (int i = 0; i < _pools_list->length(); i++) { 150 MemoryPool* pool = _pools_list->at(i); 151 pool->record_peak_memory_usage(); 152 } 153 154 // Detect low memory 155 LowMemoryDetector::detect_low_memory(); 156 } 157 158 void MemoryService::track_memory_pool_usage(MemoryPool* pool) { 159 // Track the peak memory usage 160 pool->record_peak_memory_usage(); 161 162 // Detect low memory 163 if (LowMemoryDetector::is_enabled(pool)) { 164 LowMemoryDetector::detect_low_memory(pool); 165 } 166 } 167 168 void MemoryService::gc_begin(GCMemoryManager* mgr, bool recordGCBeginTime, 169 bool recordAccumulatedGCTime, 170 bool recordPreGCUsage, bool recordPeakUsage) { 171 172 assert(mgr->is_gc_memory_manager(), "Sanity check"); 173 mgr->gc_begin(recordGCBeginTime, recordPreGCUsage, recordAccumulatedGCTime); 174 175 // Track the peak memory usage when GC begins 176 if (recordPeakUsage) { 177 for (int i = 0; i < _pools_list->length(); i++) { 178 MemoryPool* pool = _pools_list->at(i); 179 pool->record_peak_memory_usage(); 180 } 181 } 182 } 183 184 void MemoryService::gc_end(GCMemoryManager* mgr, bool recordPostGCUsage, 185 bool recordAccumulatedGCTime, 186 bool recordGCEndTime, bool countCollection, 187 GCCause::Cause cause) { 188 189 assert(mgr->is_gc_memory_manager(), "Sanity check"); 190 191 // register the GC end statistics and memory usage 192 mgr->gc_end(recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime, 193 countCollection, cause); 194 } 195 196 void MemoryService::oops_do(OopClosure* f) { 197 int i; 198 199 for (i = 0; i < _pools_list->length(); i++) { 200 MemoryPool* pool = _pools_list->at(i); 201 pool->oops_do(f); 202 } 203 for (i = 0; i < _managers_list->length(); i++) { 204 MemoryManager* mgr = _managers_list->at(i); 205 mgr->oops_do(f); 206 } 207 } 208 209 bool MemoryService::set_verbose(bool verbose) { 210 MutexLocker m(Management_lock); 211 // verbose will be set to the previous value 212 if (verbose) { 213 LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(gc)); 214 } else { 215 LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(gc)); 216 } 217 ClassLoadingService::reset_trace_class_unloading(); 218 219 return verbose; 220 } 221 222 Handle MemoryService::create_MemoryUsage_obj(MemoryUsage usage, TRAPS) { 223 InstanceKlass* ik = Management::java_lang_management_MemoryUsage_klass(CHECK_NH); 224 225 instanceHandle obj = ik->allocate_instance_handle(CHECK_NH); 226 227 JavaValue result(T_VOID); 228 JavaCallArguments args(10); 229 args.push_oop(obj); // receiver 230 args.push_long(usage.init_size_as_jlong()); // Argument 1 231 args.push_long(usage.used_as_jlong()); // Argument 2 232 args.push_long(usage.committed_as_jlong()); // Argument 3 233 args.push_long(usage.max_size_as_jlong()); // Argument 4 234 235 JavaCalls::call_special(&result, 236 ik, 237 vmSymbols::object_initializer_name(), 238 vmSymbols::long_long_long_long_void_signature(), 239 &args, 240 CHECK_NH); 241 return obj; 242 } 243 244 TraceMemoryManagerStats::TraceMemoryManagerStats(GCMemoryManager* gc_mem_mgr, 245 GCCause::Cause cause, 246 bool recordGCBeginTime, 247 bool recordPreGCUsage, 248 bool recordPeakUsage, 249 bool recordPostGCUsage, 250 bool recordAccumulatedGCTime, 251 bool recordGCEndTime, 252 bool countCollection) { 253 initialize(gc_mem_mgr, cause, recordGCBeginTime, recordPreGCUsage, recordPeakUsage, 254 recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime, 255 countCollection); 256 } 257 258 // for a subclass to create then initialize an instance before invoking 259 // the MemoryService 260 void TraceMemoryManagerStats::initialize(GCMemoryManager* gc_mem_mgr, 261 GCCause::Cause cause, 262 bool recordGCBeginTime, 263 bool recordPreGCUsage, 264 bool recordPeakUsage, 265 bool recordPostGCUsage, 266 bool recordAccumulatedGCTime, 267 bool recordGCEndTime, 268 bool countCollection) { 269 _gc_mem_mgr = gc_mem_mgr; 270 _recordGCBeginTime = recordGCBeginTime; 271 _recordPreGCUsage = recordPreGCUsage; 272 _recordPeakUsage = recordPeakUsage; 273 _recordPostGCUsage = recordPostGCUsage; 274 _recordAccumulatedGCTime = recordAccumulatedGCTime; 275 _recordGCEndTime = recordGCEndTime; 276 _countCollection = countCollection; 277 _cause = cause; 278 279 MemoryService::gc_begin(_gc_mem_mgr, _recordGCBeginTime, _recordAccumulatedGCTime, 280 _recordPreGCUsage, _recordPeakUsage); 281 } 282 283 TraceMemoryManagerStats::~TraceMemoryManagerStats() { 284 MemoryService::gc_end(_gc_mem_mgr, _recordPostGCUsage, _recordAccumulatedGCTime, 285 _recordGCEndTime, _countCollection, _cause); 286 }