Path: blob/master/src/hotspot/share/services/memoryManager.cpp
41145 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/javaClasses.hpp"26#include "classfile/vmSymbols.hpp"27#include "memory/allocation.inline.hpp"28#include "memory/universe.hpp"29#include "oops/oop.inline.hpp"30#include "oops/oopHandle.inline.hpp"31#include "runtime/atomic.hpp"32#include "runtime/handles.inline.hpp"33#include "runtime/javaCalls.hpp"34#include "services/lowMemoryDetector.hpp"35#include "services/management.hpp"36#include "services/memoryManager.hpp"37#include "services/memoryPool.hpp"38#include "services/memoryService.hpp"39#include "services/gcNotifier.hpp"40#include "utilities/dtrace.hpp"4142MemoryManager::MemoryManager(const char* name) :43_num_pools(0), _name(name) {}4445int MemoryManager::add_pool(MemoryPool* pool) {46int index = _num_pools;47assert(index < MemoryManager::max_num_pools, "_num_pools exceeds the max");48if (index < MemoryManager::max_num_pools) {49_pools[index] = pool;50_num_pools++;51}52pool->add_manager(this);53return index;54}5556bool MemoryManager::is_manager(instanceHandle mh) const {57return mh() == Atomic::load(&_memory_mgr_obj).resolve();58}5960MemoryManager* MemoryManager::get_code_cache_memory_manager() {61return new MemoryManager("CodeCacheManager");62}6364MemoryManager* MemoryManager::get_metaspace_memory_manager() {65return new MemoryManager("Metaspace Manager");66}6768instanceOop MemoryManager::get_memory_manager_instance(TRAPS) {69// Must do an acquire so as to force ordering of subsequent70// loads from anything _memory_mgr_obj points to or implies.71oop mgr_obj = Atomic::load_acquire(&_memory_mgr_obj).resolve();72if (mgr_obj == NULL) {73// It's ok for more than one thread to execute the code up to the locked region.74// Extra manager instances will just be gc'ed.75Klass* k = Management::sun_management_ManagementFactoryHelper_klass(CHECK_NULL);7677Handle mgr_name = java_lang_String::create_from_str(name(), CHECK_NULL);7879JavaValue result(T_OBJECT);80JavaCallArguments args;81args.push_oop(mgr_name); // Argument 18283Symbol* method_name = NULL;84Symbol* signature = NULL;85if (is_gc_memory_manager()) {86Klass* extKlass = Management::com_sun_management_internal_GarbageCollectorExtImpl_klass(CHECK_NULL);87// com.sun.management.GarbageCollectorMXBean is in jdk.management module which may not be present.88if (extKlass != NULL) {89k = extKlass;90}9192method_name = vmSymbols::createGarbageCollector_name();9394signature = vmSymbols::createGarbageCollector_signature();95args.push_oop(Handle()); // Argument 2 (for future extension)96} else {97method_name = vmSymbols::createMemoryManager_name();98signature = vmSymbols::createMemoryManager_signature();99}100101InstanceKlass* ik = InstanceKlass::cast(k);102103JavaCalls::call_static(&result,104ik,105method_name,106signature,107&args,108CHECK_NULL);109110instanceOop m = (instanceOop) result.get_oop();111instanceHandle mgr(THREAD, m);112113{114// Get lock before setting _memory_mgr_obj115// since another thread may have created the instance116MutexLocker ml(THREAD, Management_lock);117118// Check if another thread has created the management object. We reload119// _memory_mgr_obj here because some other thread may have initialized120// it while we were executing the code before the lock.121mgr_obj = Atomic::load(&_memory_mgr_obj).resolve();122if (mgr_obj != NULL) {123return (instanceOop)mgr_obj;124}125126// Get the address of the object we created via call_special.127mgr_obj = mgr();128129// Use store barrier to make sure the memory accesses associated130// with creating the management object are visible before publishing131// its address. The unlock will publish the store to _memory_mgr_obj132// because it does a release first.133Atomic::release_store(&_memory_mgr_obj, OopHandle(Universe::vm_global(), mgr_obj));134}135}136137return (instanceOop)mgr_obj;138}139140GCStatInfo::GCStatInfo(int num_pools) {141// initialize the arrays for memory usage142_before_gc_usage_array = NEW_C_HEAP_ARRAY(MemoryUsage, num_pools, mtInternal);143_after_gc_usage_array = NEW_C_HEAP_ARRAY(MemoryUsage, num_pools, mtInternal);144_usage_array_size = num_pools;145clear();146}147148GCStatInfo::~GCStatInfo() {149FREE_C_HEAP_ARRAY(MemoryUsage*, _before_gc_usage_array);150FREE_C_HEAP_ARRAY(MemoryUsage*, _after_gc_usage_array);151}152153void GCStatInfo::set_gc_usage(int pool_index, MemoryUsage usage, bool before_gc) {154MemoryUsage* gc_usage_array;155if (before_gc) {156gc_usage_array = _before_gc_usage_array;157} else {158gc_usage_array = _after_gc_usage_array;159}160gc_usage_array[pool_index] = usage;161}162163void GCStatInfo::clear() {164_index = 0;165_start_time = 0L;166_end_time = 0L;167for (int i = 0; i < _usage_array_size; i++) ::new (&_before_gc_usage_array[i]) MemoryUsage();168for (int i = 0; i < _usage_array_size; i++) ::new (&_after_gc_usage_array[i]) MemoryUsage();169}170171172GCMemoryManager::GCMemoryManager(const char* name, const char* gc_end_message) :173MemoryManager(name), _gc_end_message(gc_end_message) {174_num_collections = 0;175_last_gc_stat = NULL;176_last_gc_lock = new Mutex(Mutex::leaf, "_last_gc_lock", true,177Mutex::_safepoint_check_never);178_current_gc_stat = NULL;179_num_gc_threads = 1;180_notification_enabled = false;181}182183GCMemoryManager::~GCMemoryManager() {184delete _last_gc_stat;185delete _last_gc_lock;186delete _current_gc_stat;187}188189void GCMemoryManager::add_pool(MemoryPool* pool) {190add_pool(pool, true);191}192193void GCMemoryManager::add_pool(MemoryPool* pool, bool always_affected_by_gc) {194int index = MemoryManager::add_pool(pool);195_pool_always_affected_by_gc[index] = always_affected_by_gc;196}197198void GCMemoryManager::initialize_gc_stat_info() {199assert(MemoryService::num_memory_pools() > 0, "should have one or more memory pools");200_last_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());201_current_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());202// tracking concurrent collections we need two objects: one to update, and one to203// hold the publicly available "last (completed) gc" information.204}205206void GCMemoryManager::gc_begin(bool recordGCBeginTime, bool recordPreGCUsage,207bool recordAccumulatedGCTime) {208assert(_last_gc_stat != NULL && _current_gc_stat != NULL, "Just checking");209if (recordAccumulatedGCTime) {210_accumulated_timer.start();211}212// _num_collections now increases in gc_end, to count completed collections213if (recordGCBeginTime) {214_current_gc_stat->set_index(_num_collections+1);215_current_gc_stat->set_start_time(Management::timestamp());216}217218if (recordPreGCUsage) {219// Keep memory usage of all memory pools220for (int i = 0; i < MemoryService::num_memory_pools(); i++) {221MemoryPool* pool = MemoryService::get_memory_pool(i);222MemoryUsage usage = pool->get_memory_usage();223_current_gc_stat->set_before_gc_usage(i, usage);224HOTSPOT_MEM_POOL_GC_BEGIN(225(char *) name(), strlen(name()),226(char *) pool->name(), strlen(pool->name()),227usage.init_size(), usage.used(),228usage.committed(), usage.max_size());229}230}231}232233// A collector MUST, even if it does not complete for some reason,234// make a TraceMemoryManagerStats object where countCollection is true,235// to ensure the current gc stat is placed in _last_gc_stat.236void GCMemoryManager::gc_end(bool recordPostGCUsage,237bool recordAccumulatedGCTime,238bool recordGCEndTime, bool countCollection,239GCCause::Cause cause,240bool allMemoryPoolsAffected) {241if (recordAccumulatedGCTime) {242_accumulated_timer.stop();243}244if (recordGCEndTime) {245_current_gc_stat->set_end_time(Management::timestamp());246}247248if (recordPostGCUsage) {249int i;250// keep the last gc statistics for all memory pools251for (i = 0; i < MemoryService::num_memory_pools(); i++) {252MemoryPool* pool = MemoryService::get_memory_pool(i);253MemoryUsage usage = pool->get_memory_usage();254255HOTSPOT_MEM_POOL_GC_END(256(char *) name(), strlen(name()),257(char *) pool->name(), strlen(pool->name()),258usage.init_size(), usage.used(),259usage.committed(), usage.max_size());260261_current_gc_stat->set_after_gc_usage(i, usage);262}263264// Set last collection usage of the memory pools managed by this collector265for (i = 0; i < num_memory_pools(); i++) {266MemoryPool* pool = get_memory_pool(i);267MemoryUsage usage = pool->get_memory_usage();268269if (allMemoryPoolsAffected || pool_always_affected_by_gc(i)) {270// Compare with GC usage threshold271pool->set_last_collection_usage(usage);272LowMemoryDetector::detect_after_gc_memory(pool);273}274}275}276277if (countCollection) {278_num_collections++;279// alternately update two objects making one public when complete280{281MutexLocker ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);282GCStatInfo *tmp = _last_gc_stat;283_last_gc_stat = _current_gc_stat;284_current_gc_stat = tmp;285// reset the current stat for diagnosability purposes286_current_gc_stat->clear();287}288289if (is_notification_enabled()) {290GCNotifier::pushNotification(this, _gc_end_message, GCCause::to_string(cause));291}292}293}294295size_t GCMemoryManager::get_last_gc_stat(GCStatInfo* dest) {296MutexLocker ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);297if (_last_gc_stat->gc_index() != 0) {298dest->set_index(_last_gc_stat->gc_index());299dest->set_start_time(_last_gc_stat->start_time());300dest->set_end_time(_last_gc_stat->end_time());301assert(dest->usage_array_size() == _last_gc_stat->usage_array_size(),302"Must have same array size");303size_t len = dest->usage_array_size() * sizeof(MemoryUsage);304memcpy(dest->before_gc_usage_array(), _last_gc_stat->before_gc_usage_array(), len);305memcpy(dest->after_gc_usage_array(), _last_gc_stat->after_gc_usage_array(), len);306}307return _last_gc_stat->gc_index();308}309310311