2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2018-03-08 09:56:29 +01:00
|
|
|
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-27 19:08:38 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "precompiled.hpp"
|
|
|
|
#include "classfile/systemDictionary.hpp"
|
|
|
|
#include "classfile/vmSymbols.hpp"
|
2018-03-08 09:56:29 +01:00
|
|
|
#include "memory/allocation.inline.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "oops/oop.inline.hpp"
|
|
|
|
#include "runtime/handles.inline.hpp"
|
|
|
|
#include "runtime/javaCalls.hpp"
|
2018-06-06 10:45:40 -04:00
|
|
|
#include "runtime/orderAccess.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "services/lowMemoryDetector.hpp"
|
|
|
|
#include "services/management.hpp"
|
|
|
|
#include "services/memoryManager.hpp"
|
|
|
|
#include "services/memoryPool.hpp"
|
|
|
|
#include "services/memoryService.hpp"
|
2011-05-12 10:30:11 -07:00
|
|
|
#include "services/gcNotifier.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "utilities/dtrace.hpp"
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2017-11-30 13:40:07 +01:00
|
|
|
MemoryManager::MemoryManager(const char* name) : _name(name) {
|
2007-12-01 00:00:00 +00:00
|
|
|
_num_pools = 0;
|
2014-05-29 08:58:51 -04:00
|
|
|
(void)const_cast<instanceOop&>(_memory_mgr_obj = instanceOop(NULL));
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::add_pool(MemoryPool* pool) {
|
|
|
|
assert(_num_pools < MemoryManager::max_num_pools, "_num_pools exceeds the max");
|
|
|
|
if (_num_pools < MemoryManager::max_num_pools) {
|
|
|
|
_pools[_num_pools] = pool;
|
|
|
|
_num_pools++;
|
|
|
|
}
|
|
|
|
pool->add_manager(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryManager* MemoryManager::get_code_cache_memory_manager() {
|
2017-11-30 13:40:07 +01:00
|
|
|
return new MemoryManager("CodeCacheManager");
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2013-06-26 16:58:37 +02:00
|
|
|
MemoryManager* MemoryManager::get_metaspace_memory_manager() {
|
2017-11-30 13:40:07 +01:00
|
|
|
return new MemoryManager("Metaspace Manager");
|
2009-11-20 14:47:01 -05:00
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
instanceOop MemoryManager::get_memory_manager_instance(TRAPS) {
|
|
|
|
// Must do an acquire so as to force ordering of subsequent
|
|
|
|
// loads from anything _memory_mgr_obj points to or implies.
|
2017-10-16 22:36:06 -04:00
|
|
|
instanceOop mgr_obj = OrderAccess::load_acquire(&_memory_mgr_obj);
|
2007-12-01 00:00:00 +00:00
|
|
|
if (mgr_obj == NULL) {
|
|
|
|
// It's ok for more than one thread to execute the code up to the locked region.
|
|
|
|
// Extra manager instances will just be gc'ed.
|
2015-04-17 10:53:31 +02:00
|
|
|
Klass* k = Management::sun_management_ManagementFactoryHelper_klass(CHECK_0);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
Handle mgr_name = java_lang_String::create_from_str(name(), CHECK_0);
|
|
|
|
|
|
|
|
JavaValue result(T_OBJECT);
|
|
|
|
JavaCallArguments args;
|
|
|
|
args.push_oop(mgr_name); // Argument 1
|
|
|
|
|
2011-01-27 16:11:27 -08:00
|
|
|
Symbol* method_name = NULL;
|
|
|
|
Symbol* signature = NULL;
|
2007-12-01 00:00:00 +00:00
|
|
|
if (is_gc_memory_manager()) {
|
2015-04-17 10:53:31 +02:00
|
|
|
Klass* extKlass = Management::com_sun_management_internal_GarbageCollectorExtImpl_klass(CHECK_0);
|
|
|
|
// com.sun.management.GarbageCollectorMXBean is in jdk.management module which may not be present.
|
|
|
|
if (extKlass != NULL) {
|
|
|
|
k = extKlass;
|
|
|
|
}
|
|
|
|
|
2011-01-27 16:11:27 -08:00
|
|
|
method_name = vmSymbols::createGarbageCollector_name();
|
2015-04-17 10:53:31 +02:00
|
|
|
|
2011-01-27 16:11:27 -08:00
|
|
|
signature = vmSymbols::createGarbageCollector_signature();
|
2007-12-01 00:00:00 +00:00
|
|
|
args.push_oop(Handle()); // Argument 2 (for future extension)
|
|
|
|
} else {
|
2011-01-27 16:11:27 -08:00
|
|
|
method_name = vmSymbols::createMemoryManager_name();
|
|
|
|
signature = vmSymbols::createMemoryManager_signature();
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 10:25:37 -04:00
|
|
|
InstanceKlass* ik = InstanceKlass::cast(k);
|
2015-04-17 10:53:31 +02:00
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
JavaCalls::call_static(&result,
|
|
|
|
ik,
|
|
|
|
method_name,
|
|
|
|
signature,
|
|
|
|
&args,
|
|
|
|
CHECK_0);
|
|
|
|
|
|
|
|
instanceOop m = (instanceOop) result.get_jobject();
|
|
|
|
instanceHandle mgr(THREAD, m);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Get lock before setting _memory_mgr_obj
|
|
|
|
// since another thread may have created the instance
|
|
|
|
MutexLocker ml(Management_lock);
|
|
|
|
|
|
|
|
// Check if another thread has created the management object. We reload
|
|
|
|
// _memory_mgr_obj here because some other thread may have initialized
|
|
|
|
// it while we were executing the code before the lock.
|
|
|
|
//
|
|
|
|
// The lock has done an acquire, so the load can't float above it, but
|
|
|
|
// we need to do a load_acquire as above.
|
2017-10-16 22:36:06 -04:00
|
|
|
mgr_obj = OrderAccess::load_acquire(&_memory_mgr_obj);
|
2007-12-01 00:00:00 +00:00
|
|
|
if (mgr_obj != NULL) {
|
|
|
|
return mgr_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the address of the object we created via call_special.
|
|
|
|
mgr_obj = mgr();
|
|
|
|
|
|
|
|
// Use store barrier to make sure the memory accesses associated
|
|
|
|
// with creating the management object are visible before publishing
|
|
|
|
// its address. The unlock will publish the store to _memory_mgr_obj
|
|
|
|
// because it does a release first.
|
2017-10-16 22:36:06 -04:00
|
|
|
OrderAccess::release_store(&_memory_mgr_obj, mgr_obj);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mgr_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::oops_do(OopClosure* f) {
|
|
|
|
f->do_oop((oop*) &_memory_mgr_obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
GCStatInfo::GCStatInfo(int num_pools) {
|
|
|
|
// initialize the arrays for memory usage
|
2012-06-28 17:03:16 -04:00
|
|
|
_before_gc_usage_array = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools, mtInternal);
|
|
|
|
_after_gc_usage_array = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools, mtInternal);
|
2007-12-01 00:00:00 +00:00
|
|
|
_usage_array_size = num_pools;
|
2011-11-21 09:24:56 -08:00
|
|
|
clear();
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GCStatInfo::~GCStatInfo() {
|
2014-12-01 12:16:15 -05:00
|
|
|
FREE_C_HEAP_ARRAY(MemoryUsage*, _before_gc_usage_array);
|
|
|
|
FREE_C_HEAP_ARRAY(MemoryUsage*, _after_gc_usage_array);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GCStatInfo::set_gc_usage(int pool_index, MemoryUsage usage, bool before_gc) {
|
|
|
|
MemoryUsage* gc_usage_array;
|
|
|
|
if (before_gc) {
|
|
|
|
gc_usage_array = _before_gc_usage_array;
|
|
|
|
} else {
|
|
|
|
gc_usage_array = _after_gc_usage_array;
|
|
|
|
}
|
|
|
|
gc_usage_array[pool_index] = usage;
|
|
|
|
}
|
|
|
|
|
2010-07-30 22:43:50 +01:00
|
|
|
void GCStatInfo::clear() {
|
|
|
|
_index = 0;
|
|
|
|
_start_time = 0L;
|
|
|
|
_end_time = 0L;
|
|
|
|
size_t len = _usage_array_size * sizeof(MemoryUsage);
|
|
|
|
memset(_before_gc_usage_array, 0, len);
|
|
|
|
memset(_after_gc_usage_array, 0, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-30 13:40:07 +01:00
|
|
|
GCMemoryManager::GCMemoryManager(const char* name, const char* gc_end_message) :
|
|
|
|
MemoryManager(name), _gc_end_message(gc_end_message) {
|
2007-12-01 00:00:00 +00:00
|
|
|
_num_collections = 0;
|
|
|
|
_last_gc_stat = NULL;
|
2014-12-11 21:34:43 -05:00
|
|
|
_last_gc_lock = new Mutex(Mutex::leaf, "_last_gc_lock", true,
|
|
|
|
Monitor::_safepoint_check_never);
|
2010-07-30 22:43:50 +01:00
|
|
|
_current_gc_stat = NULL;
|
2007-12-01 00:00:00 +00:00
|
|
|
_num_gc_threads = 1;
|
2011-05-12 10:30:11 -07:00
|
|
|
_notification_enabled = false;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GCMemoryManager::~GCMemoryManager() {
|
|
|
|
delete _last_gc_stat;
|
2010-07-30 22:43:50 +01:00
|
|
|
delete _last_gc_lock;
|
|
|
|
delete _current_gc_stat;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GCMemoryManager::initialize_gc_stat_info() {
|
|
|
|
assert(MemoryService::num_memory_pools() > 0, "should have one or more memory pools");
|
2012-06-28 17:03:16 -04:00
|
|
|
_last_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());
|
|
|
|
_current_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());
|
2010-07-30 22:43:50 +01:00
|
|
|
// tracking concurrent collections we need two objects: one to update, and one to
|
|
|
|
// hold the publicly available "last (completed) gc" information.
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2010-07-30 22:43:50 +01:00
|
|
|
void GCMemoryManager::gc_begin(bool recordGCBeginTime, bool recordPreGCUsage,
|
|
|
|
bool recordAccumulatedGCTime) {
|
|
|
|
assert(_last_gc_stat != NULL && _current_gc_stat != NULL, "Just checking");
|
|
|
|
if (recordAccumulatedGCTime) {
|
|
|
|
_accumulated_timer.start();
|
|
|
|
}
|
|
|
|
// _num_collections now increases in gc_end, to count completed collections
|
|
|
|
if (recordGCBeginTime) {
|
|
|
|
_current_gc_stat->set_index(_num_collections+1);
|
|
|
|
_current_gc_stat->set_start_time(Management::timestamp());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recordPreGCUsage) {
|
|
|
|
// Keep memory usage of all memory pools
|
|
|
|
for (int i = 0; i < MemoryService::num_memory_pools(); i++) {
|
|
|
|
MemoryPool* pool = MemoryService::get_memory_pool(i);
|
|
|
|
MemoryUsage usage = pool->get_memory_usage();
|
|
|
|
_current_gc_stat->set_before_gc_usage(i, usage);
|
2011-10-13 09:35:42 -07:00
|
|
|
HOTSPOT_MEM_POOL_GC_BEGIN(
|
|
|
|
(char *) name(), strlen(name()),
|
|
|
|
(char *) pool->name(), strlen(pool->name()),
|
|
|
|
usage.init_size(), usage.used(),
|
|
|
|
usage.committed(), usage.max_size());
|
2010-07-30 22:43:50 +01:00
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-30 22:43:50 +01:00
|
|
|
// A collector MUST, even if it does not complete for some reason,
|
|
|
|
// make a TraceMemoryManagerStats object where countCollection is true,
|
|
|
|
// to ensure the current gc stat is placed in _last_gc_stat.
|
|
|
|
void GCMemoryManager::gc_end(bool recordPostGCUsage,
|
|
|
|
bool recordAccumulatedGCTime,
|
2011-05-12 10:30:11 -07:00
|
|
|
bool recordGCEndTime, bool countCollection,
|
|
|
|
GCCause::Cause cause) {
|
2010-07-30 22:43:50 +01:00
|
|
|
if (recordAccumulatedGCTime) {
|
|
|
|
_accumulated_timer.stop();
|
|
|
|
}
|
|
|
|
if (recordGCEndTime) {
|
|
|
|
_current_gc_stat->set_end_time(Management::timestamp());
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2010-07-30 22:43:50 +01:00
|
|
|
if (recordPostGCUsage) {
|
|
|
|
int i;
|
|
|
|
// keep the last gc statistics for all memory pools
|
|
|
|
for (i = 0; i < MemoryService::num_memory_pools(); i++) {
|
|
|
|
MemoryPool* pool = MemoryService::get_memory_pool(i);
|
|
|
|
MemoryUsage usage = pool->get_memory_usage();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2011-10-13 09:35:42 -07:00
|
|
|
HOTSPOT_MEM_POOL_GC_END(
|
|
|
|
(char *) name(), strlen(name()),
|
|
|
|
(char *) pool->name(), strlen(pool->name()),
|
|
|
|
usage.init_size(), usage.used(),
|
|
|
|
usage.committed(), usage.max_size());
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2010-07-30 22:43:50 +01:00
|
|
|
_current_gc_stat->set_after_gc_usage(i, usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set last collection usage of the memory pools managed by this collector
|
|
|
|
for (i = 0; i < num_memory_pools(); i++) {
|
|
|
|
MemoryPool* pool = get_memory_pool(i);
|
|
|
|
MemoryUsage usage = pool->get_memory_usage();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2010-07-30 22:43:50 +01:00
|
|
|
// Compare with GC usage threshold
|
|
|
|
pool->set_last_collection_usage(usage);
|
|
|
|
LowMemoryDetector::detect_after_gc_memory(pool);
|
|
|
|
}
|
|
|
|
}
|
2011-11-21 09:24:56 -08:00
|
|
|
|
2010-07-30 22:43:50 +01:00
|
|
|
if (countCollection) {
|
|
|
|
_num_collections++;
|
|
|
|
// alternately update two objects making one public when complete
|
|
|
|
{
|
|
|
|
MutexLockerEx ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);
|
|
|
|
GCStatInfo *tmp = _last_gc_stat;
|
|
|
|
_last_gc_stat = _current_gc_stat;
|
|
|
|
_current_gc_stat = tmp;
|
|
|
|
// reset the current stat for diagnosability purposes
|
|
|
|
_current_gc_stat->clear();
|
|
|
|
}
|
2011-11-21 09:24:56 -08:00
|
|
|
|
|
|
|
if (is_notification_enabled()) {
|
2017-11-30 13:40:07 +01:00
|
|
|
GCNotifier::pushNotification(this, _gc_end_message, GCCause::to_string(cause));
|
2011-11-21 09:24:56 -08:00
|
|
|
}
|
2010-07-30 22:43:50 +01:00
|
|
|
}
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2010-07-30 22:43:50 +01:00
|
|
|
size_t GCMemoryManager::get_last_gc_stat(GCStatInfo* dest) {
|
|
|
|
MutexLockerEx ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);
|
|
|
|
if (_last_gc_stat->gc_index() != 0) {
|
|
|
|
dest->set_index(_last_gc_stat->gc_index());
|
|
|
|
dest->set_start_time(_last_gc_stat->start_time());
|
|
|
|
dest->set_end_time(_last_gc_stat->end_time());
|
|
|
|
assert(dest->usage_array_size() == _last_gc_stat->usage_array_size(),
|
|
|
|
"Must have same array size");
|
|
|
|
size_t len = dest->usage_array_size() * sizeof(MemoryUsage);
|
|
|
|
memcpy(dest->before_gc_usage_array(), _last_gc_stat->before_gc_usage_array(), len);
|
|
|
|
memcpy(dest->after_gc_usage_array(), _last_gc_stat->after_gc_usage_array(), len);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
2010-07-30 22:43:50 +01:00
|
|
|
return _last_gc_stat->gc_index();
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|