0N/A/*
2273N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "classfile/systemDictionary.hpp"
1879N/A#include "classfile/vmSymbols.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/handles.inline.hpp"
1879N/A#include "runtime/javaCalls.hpp"
1879N/A#include "services/lowMemoryDetector.hpp"
1879N/A#include "services/management.hpp"
1879N/A#include "services/memoryManager.hpp"
1879N/A#include "services/memoryPool.hpp"
1879N/A#include "services/memoryService.hpp"
2453N/A#include "services/gcNotifier.hpp"
1879N/A#include "utilities/dtrace.hpp"
0N/A
2842N/A#ifndef USDT2
0N/AHS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__begin, char*, int, char*, int,
0N/A size_t, size_t, size_t, size_t);
0N/AHS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__end, char*, int, char*, int,
0N/A size_t, size_t, size_t, size_t);
2842N/A#endif /* !USDT2 */
0N/A
0N/AMemoryManager::MemoryManager() {
0N/A _num_pools = 0;
0N/A _memory_mgr_obj = NULL;
0N/A}
0N/A
0N/Avoid MemoryManager::add_pool(MemoryPool* pool) {
0N/A assert(_num_pools < MemoryManager::max_num_pools, "_num_pools exceeds the max");
0N/A if (_num_pools < MemoryManager::max_num_pools) {
0N/A _pools[_num_pools] = pool;
0N/A _num_pools++;
0N/A }
0N/A pool->add_manager(this);
0N/A}
0N/A
0N/AMemoryManager* MemoryManager::get_code_cache_memory_manager() {
0N/A return (MemoryManager*) new CodeCacheMemoryManager();
0N/A}
0N/A
0N/AGCMemoryManager* MemoryManager::get_copy_memory_manager() {
0N/A return (GCMemoryManager*) new CopyMemoryManager();
0N/A}
0N/A
0N/AGCMemoryManager* MemoryManager::get_msc_memory_manager() {
0N/A return (GCMemoryManager*) new MSCMemoryManager();
0N/A}
0N/A
0N/AGCMemoryManager* MemoryManager::get_parnew_memory_manager() {
0N/A return (GCMemoryManager*) new ParNewMemoryManager();
0N/A}
0N/A
0N/AGCMemoryManager* MemoryManager::get_cms_memory_manager() {
0N/A return (GCMemoryManager*) new CMSMemoryManager();
0N/A}
0N/A
0N/AGCMemoryManager* MemoryManager::get_psScavenge_memory_manager() {
0N/A return (GCMemoryManager*) new PSScavengeMemoryManager();
0N/A}
0N/A
0N/AGCMemoryManager* MemoryManager::get_psMarkSweep_memory_manager() {
0N/A return (GCMemoryManager*) new PSMarkSweepMemoryManager();
0N/A}
0N/A
1089N/AGCMemoryManager* MemoryManager::get_g1YoungGen_memory_manager() {
1089N/A return (GCMemoryManager*) new G1YoungGenMemoryManager();
1089N/A}
1089N/A
1089N/AGCMemoryManager* MemoryManager::get_g1OldGen_memory_manager() {
1089N/A return (GCMemoryManager*) new G1OldGenMemoryManager();
1089N/A}
1089N/A
0N/AinstanceOop MemoryManager::get_memory_manager_instance(TRAPS) {
0N/A // Must do an acquire so as to force ordering of subsequent
0N/A // loads from anything _memory_mgr_obj points to or implies.
0N/A instanceOop mgr_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_mgr_obj);
0N/A if (mgr_obj == NULL) {
0N/A // It's ok for more than one thread to execute the code up to the locked region.
0N/A // Extra manager instances will just be gc'ed.
0N/A klassOop k = Management::sun_management_ManagementFactory_klass(CHECK_0);
0N/A instanceKlassHandle ik(THREAD, k);
0N/A
0N/A Handle mgr_name = java_lang_String::create_from_str(name(), CHECK_0);
0N/A
0N/A JavaValue result(T_OBJECT);
0N/A JavaCallArguments args;
0N/A args.push_oop(mgr_name); // Argument 1
0N/A
2062N/A Symbol* method_name = NULL;
2062N/A Symbol* signature = NULL;
0N/A if (is_gc_memory_manager()) {
2062N/A method_name = vmSymbols::createGarbageCollector_name();
2062N/A signature = vmSymbols::createGarbageCollector_signature();
0N/A args.push_oop(Handle()); // Argument 2 (for future extension)
0N/A } else {
2062N/A method_name = vmSymbols::createMemoryManager_name();
2062N/A signature = vmSymbols::createMemoryManager_signature();
0N/A }
0N/A
0N/A JavaCalls::call_static(&result,
0N/A ik,
0N/A method_name,
0N/A signature,
0N/A &args,
0N/A CHECK_0);
0N/A
0N/A instanceOop m = (instanceOop) result.get_jobject();
0N/A instanceHandle mgr(THREAD, m);
0N/A
0N/A {
0N/A // Get lock before setting _memory_mgr_obj
0N/A // since another thread may have created the instance
0N/A MutexLocker ml(Management_lock);
0N/A
0N/A // Check if another thread has created the management object. We reload
0N/A // _memory_mgr_obj here because some other thread may have initialized
0N/A // it while we were executing the code before the lock.
0N/A //
0N/A // The lock has done an acquire, so the load can't float above it, but
0N/A // we need to do a load_acquire as above.
0N/A mgr_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_mgr_obj);
0N/A if (mgr_obj != NULL) {
0N/A return mgr_obj;
0N/A }
0N/A
0N/A // Get the address of the object we created via call_special.
0N/A mgr_obj = mgr();
0N/A
0N/A // Use store barrier to make sure the memory accesses associated
0N/A // with creating the management object are visible before publishing
0N/A // its address. The unlock will publish the store to _memory_mgr_obj
0N/A // because it does a release first.
0N/A OrderAccess::release_store_ptr(&_memory_mgr_obj, mgr_obj);
0N/A }
0N/A }
0N/A
0N/A return mgr_obj;
0N/A}
0N/A
0N/Avoid MemoryManager::oops_do(OopClosure* f) {
0N/A f->do_oop((oop*) &_memory_mgr_obj);
0N/A}
0N/A
0N/AGCStatInfo::GCStatInfo(int num_pools) {
0N/A // initialize the arrays for memory usage
3863N/A _before_gc_usage_array = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools, mtInternal);
3863N/A _after_gc_usage_array = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools, mtInternal);
0N/A _usage_array_size = num_pools;
2938N/A clear();
0N/A}
0N/A
0N/AGCStatInfo::~GCStatInfo() {
3863N/A FREE_C_HEAP_ARRAY(MemoryUsage*, _before_gc_usage_array, mtInternal);
3863N/A FREE_C_HEAP_ARRAY(MemoryUsage*, _after_gc_usage_array, mtInternal);
0N/A}
0N/A
0N/Avoid GCStatInfo::set_gc_usage(int pool_index, MemoryUsage usage, bool before_gc) {
0N/A MemoryUsage* gc_usage_array;
0N/A if (before_gc) {
0N/A gc_usage_array = _before_gc_usage_array;
0N/A } else {
0N/A gc_usage_array = _after_gc_usage_array;
0N/A }
0N/A gc_usage_array[pool_index] = usage;
0N/A}
0N/A
1623N/Avoid GCStatInfo::clear() {
1623N/A _index = 0;
1623N/A _start_time = 0L;
1623N/A _end_time = 0L;
1623N/A size_t len = _usage_array_size * sizeof(MemoryUsage);
1623N/A memset(_before_gc_usage_array, 0, len);
1623N/A memset(_after_gc_usage_array, 0, len);
1623N/A}
1623N/A
1623N/A
0N/AGCMemoryManager::GCMemoryManager() : MemoryManager() {
0N/A _num_collections = 0;
0N/A _last_gc_stat = NULL;
1623N/A _last_gc_lock = new Mutex(Mutex::leaf, "_last_gc_lock", true);
1623N/A _current_gc_stat = NULL;
0N/A _num_gc_threads = 1;
2453N/A _notification_enabled = false;
0N/A}
0N/A
0N/AGCMemoryManager::~GCMemoryManager() {
0N/A delete _last_gc_stat;
1623N/A delete _last_gc_lock;
1623N/A delete _current_gc_stat;
0N/A}
0N/A
0N/Avoid GCMemoryManager::initialize_gc_stat_info() {
0N/A assert(MemoryService::num_memory_pools() > 0, "should have one or more memory pools");
3863N/A _last_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());
3863N/A _current_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());
1623N/A // tracking concurrent collections we need two objects: one to update, and one to
1623N/A // hold the publicly available "last (completed) gc" information.
0N/A}
0N/A
1623N/Avoid GCMemoryManager::gc_begin(bool recordGCBeginTime, bool recordPreGCUsage,
1623N/A bool recordAccumulatedGCTime) {
1623N/A assert(_last_gc_stat != NULL && _current_gc_stat != NULL, "Just checking");
1623N/A if (recordAccumulatedGCTime) {
1623N/A _accumulated_timer.start();
1623N/A }
1623N/A // _num_collections now increases in gc_end, to count completed collections
1623N/A if (recordGCBeginTime) {
1623N/A _current_gc_stat->set_index(_num_collections+1);
1623N/A _current_gc_stat->set_start_time(Management::timestamp());
1623N/A }
0N/A
1623N/A if (recordPreGCUsage) {
1623N/A // Keep memory usage of all memory pools
1623N/A for (int i = 0; i < MemoryService::num_memory_pools(); i++) {
1623N/A MemoryPool* pool = MemoryService::get_memory_pool(i);
1623N/A MemoryUsage usage = pool->get_memory_usage();
1623N/A _current_gc_stat->set_before_gc_usage(i, usage);
2842N/A#ifndef USDT2
1623N/A HS_DTRACE_PROBE8(hotspot, mem__pool__gc__begin,
1623N/A name(), strlen(name()),
1623N/A pool->name(), strlen(pool->name()),
1623N/A usage.init_size(), usage.used(),
1623N/A usage.committed(), usage.max_size());
2842N/A#else /* USDT2 */
2842N/A HOTSPOT_MEM_POOL_GC_BEGIN(
2842N/A (char *) name(), strlen(name()),
2842N/A (char *) pool->name(), strlen(pool->name()),
2842N/A usage.init_size(), usage.used(),
2842N/A usage.committed(), usage.max_size());
2842N/A#endif /* USDT2 */
1623N/A }
0N/A }
0N/A}
0N/A
1623N/A// A collector MUST, even if it does not complete for some reason,
1623N/A// make a TraceMemoryManagerStats object where countCollection is true,
1623N/A// to ensure the current gc stat is placed in _last_gc_stat.
1623N/Avoid GCMemoryManager::gc_end(bool recordPostGCUsage,
1623N/A bool recordAccumulatedGCTime,
2453N/A bool recordGCEndTime, bool countCollection,
2453N/A GCCause::Cause cause) {
1623N/A if (recordAccumulatedGCTime) {
1623N/A _accumulated_timer.stop();
1623N/A }
1623N/A if (recordGCEndTime) {
1623N/A _current_gc_stat->set_end_time(Management::timestamp());
0N/A }
0N/A
1623N/A if (recordPostGCUsage) {
1623N/A int i;
1623N/A // keep the last gc statistics for all memory pools
1623N/A for (i = 0; i < MemoryService::num_memory_pools(); i++) {
1623N/A MemoryPool* pool = MemoryService::get_memory_pool(i);
1623N/A MemoryUsage usage = pool->get_memory_usage();
1623N/A
2842N/A#ifndef USDT2
1623N/A HS_DTRACE_PROBE8(hotspot, mem__pool__gc__end,
1623N/A name(), strlen(name()),
1623N/A pool->name(), strlen(pool->name()),
1623N/A usage.init_size(), usage.used(),
1623N/A usage.committed(), usage.max_size());
2842N/A#else /* USDT2 */
2842N/A HOTSPOT_MEM_POOL_GC_END(
2842N/A (char *) name(), strlen(name()),
2842N/A (char *) pool->name(), strlen(pool->name()),
2842N/A usage.init_size(), usage.used(),
2842N/A usage.committed(), usage.max_size());
2842N/A#endif /* USDT2 */
1623N/A
1623N/A _current_gc_stat->set_after_gc_usage(i, usage);
1623N/A }
0N/A
1623N/A // Set last collection usage of the memory pools managed by this collector
1623N/A for (i = 0; i < num_memory_pools(); i++) {
1623N/A MemoryPool* pool = get_memory_pool(i);
1623N/A MemoryUsage usage = pool->get_memory_usage();
1623N/A
1623N/A // Compare with GC usage threshold
1623N/A pool->set_last_collection_usage(usage);
1623N/A LowMemoryDetector::detect_after_gc_memory(pool);
1623N/A }
1623N/A }
2938N/A
1623N/A if (countCollection) {
1623N/A _num_collections++;
1623N/A // alternately update two objects making one public when complete
1623N/A {
1623N/A MutexLockerEx ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);
1623N/A GCStatInfo *tmp = _last_gc_stat;
1623N/A _last_gc_stat = _current_gc_stat;
1623N/A _current_gc_stat = tmp;
1623N/A // reset the current stat for diagnosability purposes
1623N/A _current_gc_stat->clear();
1623N/A }
2938N/A
2938N/A if (is_notification_enabled()) {
2938N/A bool isMajorGC = this == MemoryService::get_major_gc_manager();
2938N/A GCNotifier::pushNotification(this, isMajorGC ? "end of major GC" : "end of minor GC",
2938N/A GCCause::to_string(cause));
2938N/A }
0N/A }
0N/A}
1623N/A
1623N/Asize_t GCMemoryManager::get_last_gc_stat(GCStatInfo* dest) {
1623N/A MutexLockerEx ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);
1623N/A if (_last_gc_stat->gc_index() != 0) {
1623N/A dest->set_index(_last_gc_stat->gc_index());
1623N/A dest->set_start_time(_last_gc_stat->start_time());
1623N/A dest->set_end_time(_last_gc_stat->end_time());
1623N/A assert(dest->usage_array_size() == _last_gc_stat->usage_array_size(),
1623N/A "Must have same array size");
1623N/A size_t len = dest->usage_array_size() * sizeof(MemoryUsage);
1623N/A memcpy(dest->before_gc_usage_array(), _last_gc_stat->before_gc_usage_array(), len);
1623N/A memcpy(dest->after_gc_usage_array(), _last_gc_stat->after_gc_usage_array(), len);
1623N/A }
1623N/A return _last_gc_stat->gc_index();
1623N/A}