src/share/vm/services/memoryManager.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6911
ce8f6bb717c9
parent 6876
710a3c8b516e
child 9448
73d689add964
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/systemDictionary.hpp"
aoqi@0 27 #include "classfile/vmSymbols.hpp"
aoqi@0 28 #include "oops/oop.inline.hpp"
aoqi@0 29 #include "runtime/handles.inline.hpp"
aoqi@0 30 #include "runtime/javaCalls.hpp"
goetz@6911 31 #include "runtime/orderAccess.inline.hpp"
aoqi@0 32 #include "services/lowMemoryDetector.hpp"
aoqi@0 33 #include "services/management.hpp"
aoqi@0 34 #include "services/memoryManager.hpp"
aoqi@0 35 #include "services/memoryPool.hpp"
aoqi@0 36 #include "services/memoryService.hpp"
aoqi@0 37 #include "services/gcNotifier.hpp"
aoqi@0 38 #include "utilities/dtrace.hpp"
aoqi@0 39
aoqi@0 40 #ifndef USDT2
aoqi@0 41 HS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__begin, char*, int, char*, int,
aoqi@0 42 size_t, size_t, size_t, size_t);
aoqi@0 43 HS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__end, char*, int, char*, int,
aoqi@0 44 size_t, size_t, size_t, size_t);
aoqi@0 45 #endif /* !USDT2 */
aoqi@0 46
aoqi@0 47 MemoryManager::MemoryManager() {
aoqi@0 48 _num_pools = 0;
aoqi@0 49 (void)const_cast<instanceOop&>(_memory_mgr_obj = NULL);
aoqi@0 50 }
aoqi@0 51
aoqi@0 52 void MemoryManager::add_pool(MemoryPool* pool) {
aoqi@0 53 assert(_num_pools < MemoryManager::max_num_pools, "_num_pools exceeds the max");
aoqi@0 54 if (_num_pools < MemoryManager::max_num_pools) {
aoqi@0 55 _pools[_num_pools] = pool;
aoqi@0 56 _num_pools++;
aoqi@0 57 }
aoqi@0 58 pool->add_manager(this);
aoqi@0 59 }
aoqi@0 60
aoqi@0 61 MemoryManager* MemoryManager::get_code_cache_memory_manager() {
aoqi@0 62 return (MemoryManager*) new CodeCacheMemoryManager();
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 MemoryManager* MemoryManager::get_metaspace_memory_manager() {
aoqi@0 66 return (MemoryManager*) new MetaspaceMemoryManager();
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 GCMemoryManager* MemoryManager::get_copy_memory_manager() {
aoqi@0 70 return (GCMemoryManager*) new CopyMemoryManager();
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 GCMemoryManager* MemoryManager::get_msc_memory_manager() {
aoqi@0 74 return (GCMemoryManager*) new MSCMemoryManager();
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 GCMemoryManager* MemoryManager::get_parnew_memory_manager() {
aoqi@0 78 return (GCMemoryManager*) new ParNewMemoryManager();
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 GCMemoryManager* MemoryManager::get_cms_memory_manager() {
aoqi@0 82 return (GCMemoryManager*) new CMSMemoryManager();
aoqi@0 83 }
aoqi@0 84
aoqi@0 85 GCMemoryManager* MemoryManager::get_psScavenge_memory_manager() {
aoqi@0 86 return (GCMemoryManager*) new PSScavengeMemoryManager();
aoqi@0 87 }
aoqi@0 88
aoqi@0 89 GCMemoryManager* MemoryManager::get_psMarkSweep_memory_manager() {
aoqi@0 90 return (GCMemoryManager*) new PSMarkSweepMemoryManager();
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 GCMemoryManager* MemoryManager::get_g1YoungGen_memory_manager() {
aoqi@0 94 return (GCMemoryManager*) new G1YoungGenMemoryManager();
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 GCMemoryManager* MemoryManager::get_g1OldGen_memory_manager() {
aoqi@0 98 return (GCMemoryManager*) new G1OldGenMemoryManager();
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 instanceOop MemoryManager::get_memory_manager_instance(TRAPS) {
aoqi@0 102 // Must do an acquire so as to force ordering of subsequent
aoqi@0 103 // loads from anything _memory_mgr_obj points to or implies.
aoqi@0 104 instanceOop mgr_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_mgr_obj);
aoqi@0 105 if (mgr_obj == NULL) {
aoqi@0 106 // It's ok for more than one thread to execute the code up to the locked region.
aoqi@0 107 // Extra manager instances will just be gc'ed.
aoqi@0 108 Klass* k = Management::sun_management_ManagementFactory_klass(CHECK_0);
aoqi@0 109 instanceKlassHandle ik(THREAD, k);
aoqi@0 110
aoqi@0 111 Handle mgr_name = java_lang_String::create_from_str(name(), CHECK_0);
aoqi@0 112
aoqi@0 113 JavaValue result(T_OBJECT);
aoqi@0 114 JavaCallArguments args;
aoqi@0 115 args.push_oop(mgr_name); // Argument 1
aoqi@0 116
aoqi@0 117 Symbol* method_name = NULL;
aoqi@0 118 Symbol* signature = NULL;
aoqi@0 119 if (is_gc_memory_manager()) {
aoqi@0 120 method_name = vmSymbols::createGarbageCollector_name();
aoqi@0 121 signature = vmSymbols::createGarbageCollector_signature();
aoqi@0 122 args.push_oop(Handle()); // Argument 2 (for future extension)
aoqi@0 123 } else {
aoqi@0 124 method_name = vmSymbols::createMemoryManager_name();
aoqi@0 125 signature = vmSymbols::createMemoryManager_signature();
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 JavaCalls::call_static(&result,
aoqi@0 129 ik,
aoqi@0 130 method_name,
aoqi@0 131 signature,
aoqi@0 132 &args,
aoqi@0 133 CHECK_0);
aoqi@0 134
aoqi@0 135 instanceOop m = (instanceOop) result.get_jobject();
aoqi@0 136 instanceHandle mgr(THREAD, m);
aoqi@0 137
aoqi@0 138 {
aoqi@0 139 // Get lock before setting _memory_mgr_obj
aoqi@0 140 // since another thread may have created the instance
aoqi@0 141 MutexLocker ml(Management_lock);
aoqi@0 142
aoqi@0 143 // Check if another thread has created the management object. We reload
aoqi@0 144 // _memory_mgr_obj here because some other thread may have initialized
aoqi@0 145 // it while we were executing the code before the lock.
aoqi@0 146 //
aoqi@0 147 // The lock has done an acquire, so the load can't float above it, but
aoqi@0 148 // we need to do a load_acquire as above.
aoqi@0 149 mgr_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_mgr_obj);
aoqi@0 150 if (mgr_obj != NULL) {
aoqi@0 151 return mgr_obj;
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 // Get the address of the object we created via call_special.
aoqi@0 155 mgr_obj = mgr();
aoqi@0 156
aoqi@0 157 // Use store barrier to make sure the memory accesses associated
aoqi@0 158 // with creating the management object are visible before publishing
aoqi@0 159 // its address. The unlock will publish the store to _memory_mgr_obj
aoqi@0 160 // because it does a release first.
aoqi@0 161 OrderAccess::release_store_ptr(&_memory_mgr_obj, mgr_obj);
aoqi@0 162 }
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 return mgr_obj;
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 void MemoryManager::oops_do(OopClosure* f) {
aoqi@0 169 f->do_oop((oop*) &_memory_mgr_obj);
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 GCStatInfo::GCStatInfo(int num_pools) {
aoqi@0 173 // initialize the arrays for memory usage
aoqi@0 174 _before_gc_usage_array = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools, mtInternal);
aoqi@0 175 _after_gc_usage_array = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools, mtInternal);
aoqi@0 176 _usage_array_size = num_pools;
aoqi@0 177 clear();
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 GCStatInfo::~GCStatInfo() {
aoqi@0 181 FREE_C_HEAP_ARRAY(MemoryUsage*, _before_gc_usage_array, mtInternal);
aoqi@0 182 FREE_C_HEAP_ARRAY(MemoryUsage*, _after_gc_usage_array, mtInternal);
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 void GCStatInfo::set_gc_usage(int pool_index, MemoryUsage usage, bool before_gc) {
aoqi@0 186 MemoryUsage* gc_usage_array;
aoqi@0 187 if (before_gc) {
aoqi@0 188 gc_usage_array = _before_gc_usage_array;
aoqi@0 189 } else {
aoqi@0 190 gc_usage_array = _after_gc_usage_array;
aoqi@0 191 }
aoqi@0 192 gc_usage_array[pool_index] = usage;
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 void GCStatInfo::clear() {
aoqi@0 196 _index = 0;
aoqi@0 197 _start_time = 0L;
aoqi@0 198 _end_time = 0L;
aoqi@0 199 size_t len = _usage_array_size * sizeof(MemoryUsage);
aoqi@0 200 memset(_before_gc_usage_array, 0, len);
aoqi@0 201 memset(_after_gc_usage_array, 0, len);
aoqi@0 202 }
aoqi@0 203
aoqi@0 204
aoqi@0 205 GCMemoryManager::GCMemoryManager() : MemoryManager() {
aoqi@0 206 _num_collections = 0;
aoqi@0 207 _last_gc_stat = NULL;
aoqi@0 208 _last_gc_lock = new Mutex(Mutex::leaf, "_last_gc_lock", true);
aoqi@0 209 _current_gc_stat = NULL;
aoqi@0 210 _num_gc_threads = 1;
aoqi@0 211 _notification_enabled = false;
aoqi@0 212 }
aoqi@0 213
aoqi@0 214 GCMemoryManager::~GCMemoryManager() {
aoqi@0 215 delete _last_gc_stat;
aoqi@0 216 delete _last_gc_lock;
aoqi@0 217 delete _current_gc_stat;
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 void GCMemoryManager::initialize_gc_stat_info() {
aoqi@0 221 assert(MemoryService::num_memory_pools() > 0, "should have one or more memory pools");
aoqi@0 222 _last_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());
aoqi@0 223 _current_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());
aoqi@0 224 // tracking concurrent collections we need two objects: one to update, and one to
aoqi@0 225 // hold the publicly available "last (completed) gc" information.
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 void GCMemoryManager::gc_begin(bool recordGCBeginTime, bool recordPreGCUsage,
aoqi@0 229 bool recordAccumulatedGCTime) {
aoqi@0 230 assert(_last_gc_stat != NULL && _current_gc_stat != NULL, "Just checking");
aoqi@0 231 if (recordAccumulatedGCTime) {
aoqi@0 232 _accumulated_timer.start();
aoqi@0 233 }
aoqi@0 234 // _num_collections now increases in gc_end, to count completed collections
aoqi@0 235 if (recordGCBeginTime) {
aoqi@0 236 _current_gc_stat->set_index(_num_collections+1);
aoqi@0 237 _current_gc_stat->set_start_time(Management::timestamp());
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 if (recordPreGCUsage) {
aoqi@0 241 // Keep memory usage of all memory pools
aoqi@0 242 for (int i = 0; i < MemoryService::num_memory_pools(); i++) {
aoqi@0 243 MemoryPool* pool = MemoryService::get_memory_pool(i);
aoqi@0 244 MemoryUsage usage = pool->get_memory_usage();
aoqi@0 245 _current_gc_stat->set_before_gc_usage(i, usage);
aoqi@0 246 #ifndef USDT2
aoqi@0 247 HS_DTRACE_PROBE8(hotspot, mem__pool__gc__begin,
aoqi@0 248 name(), strlen(name()),
aoqi@0 249 pool->name(), strlen(pool->name()),
aoqi@0 250 usage.init_size(), usage.used(),
aoqi@0 251 usage.committed(), usage.max_size());
aoqi@0 252 #else /* USDT2 */
aoqi@0 253 HOTSPOT_MEM_POOL_GC_BEGIN(
aoqi@0 254 (char *) name(), strlen(name()),
aoqi@0 255 (char *) pool->name(), strlen(pool->name()),
aoqi@0 256 usage.init_size(), usage.used(),
aoqi@0 257 usage.committed(), usage.max_size());
aoqi@0 258 #endif /* USDT2 */
aoqi@0 259 }
aoqi@0 260 }
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 // A collector MUST, even if it does not complete for some reason,
aoqi@0 264 // make a TraceMemoryManagerStats object where countCollection is true,
aoqi@0 265 // to ensure the current gc stat is placed in _last_gc_stat.
aoqi@0 266 void GCMemoryManager::gc_end(bool recordPostGCUsage,
aoqi@0 267 bool recordAccumulatedGCTime,
aoqi@0 268 bool recordGCEndTime, bool countCollection,
aoqi@0 269 GCCause::Cause cause) {
aoqi@0 270 if (recordAccumulatedGCTime) {
aoqi@0 271 _accumulated_timer.stop();
aoqi@0 272 }
aoqi@0 273 if (recordGCEndTime) {
aoqi@0 274 _current_gc_stat->set_end_time(Management::timestamp());
aoqi@0 275 }
aoqi@0 276
aoqi@0 277 if (recordPostGCUsage) {
aoqi@0 278 int i;
aoqi@0 279 // keep the last gc statistics for all memory pools
aoqi@0 280 for (i = 0; i < MemoryService::num_memory_pools(); i++) {
aoqi@0 281 MemoryPool* pool = MemoryService::get_memory_pool(i);
aoqi@0 282 MemoryUsage usage = pool->get_memory_usage();
aoqi@0 283
aoqi@0 284 #ifndef USDT2
aoqi@0 285 HS_DTRACE_PROBE8(hotspot, mem__pool__gc__end,
aoqi@0 286 name(), strlen(name()),
aoqi@0 287 pool->name(), strlen(pool->name()),
aoqi@0 288 usage.init_size(), usage.used(),
aoqi@0 289 usage.committed(), usage.max_size());
aoqi@0 290 #else /* USDT2 */
aoqi@0 291 HOTSPOT_MEM_POOL_GC_END(
aoqi@0 292 (char *) name(), strlen(name()),
aoqi@0 293 (char *) pool->name(), strlen(pool->name()),
aoqi@0 294 usage.init_size(), usage.used(),
aoqi@0 295 usage.committed(), usage.max_size());
aoqi@0 296 #endif /* USDT2 */
aoqi@0 297
aoqi@0 298 _current_gc_stat->set_after_gc_usage(i, usage);
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 // Set last collection usage of the memory pools managed by this collector
aoqi@0 302 for (i = 0; i < num_memory_pools(); i++) {
aoqi@0 303 MemoryPool* pool = get_memory_pool(i);
aoqi@0 304 MemoryUsage usage = pool->get_memory_usage();
aoqi@0 305
aoqi@0 306 // Compare with GC usage threshold
aoqi@0 307 pool->set_last_collection_usage(usage);
aoqi@0 308 LowMemoryDetector::detect_after_gc_memory(pool);
aoqi@0 309 }
aoqi@0 310 }
aoqi@0 311
aoqi@0 312 if (countCollection) {
aoqi@0 313 _num_collections++;
aoqi@0 314 // alternately update two objects making one public when complete
aoqi@0 315 {
aoqi@0 316 MutexLockerEx ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 317 GCStatInfo *tmp = _last_gc_stat;
aoqi@0 318 _last_gc_stat = _current_gc_stat;
aoqi@0 319 _current_gc_stat = tmp;
aoqi@0 320 // reset the current stat for diagnosability purposes
aoqi@0 321 _current_gc_stat->clear();
aoqi@0 322 }
aoqi@0 323
aoqi@0 324 if (is_notification_enabled()) {
aoqi@0 325 bool isMajorGC = this == MemoryService::get_major_gc_manager();
aoqi@0 326 GCNotifier::pushNotification(this, isMajorGC ? "end of major GC" : "end of minor GC",
aoqi@0 327 GCCause::to_string(cause));
aoqi@0 328 }
aoqi@0 329 }
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 size_t GCMemoryManager::get_last_gc_stat(GCStatInfo* dest) {
aoqi@0 333 MutexLockerEx ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 334 if (_last_gc_stat->gc_index() != 0) {
aoqi@0 335 dest->set_index(_last_gc_stat->gc_index());
aoqi@0 336 dest->set_start_time(_last_gc_stat->start_time());
aoqi@0 337 dest->set_end_time(_last_gc_stat->end_time());
aoqi@0 338 assert(dest->usage_array_size() == _last_gc_stat->usage_array_size(),
aoqi@0 339 "Must have same array size");
aoqi@0 340 size_t len = dest->usage_array_size() * sizeof(MemoryUsage);
aoqi@0 341 memcpy(dest->before_gc_usage_array(), _last_gc_stat->before_gc_usage_array(), len);
aoqi@0 342 memcpy(dest->after_gc_usage_array(), _last_gc_stat->after_gc_usage_array(), len);
aoqi@0 343 }
aoqi@0 344 return _last_gc_stat->gc_index();
aoqi@0 345 }

mercurial