Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/services/memoryManager.cpp
32285 views
/*1* Copyright (c) 2003, 2019, 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/systemDictionary.hpp"26#include "classfile/vmSymbols.hpp"27#include "oops/oop.inline.hpp"28#include "runtime/handles.inline.hpp"29#include "runtime/javaCalls.hpp"30#include "runtime/orderAccess.inline.hpp"31#include "services/lowMemoryDetector.hpp"32#include "services/management.hpp"33#include "services/memoryManager.hpp"34#include "services/memoryPool.hpp"35#include "services/memoryService.hpp"36#include "services/gcNotifier.hpp"37#include "utilities/dtrace.hpp"3839#ifndef USDT240HS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__begin, char*, int, char*, int,41size_t, size_t, size_t, size_t);42HS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__end, char*, int, char*, int,43size_t, size_t, size_t, size_t);44#endif /* !USDT2 */4546MemoryManager::MemoryManager() {47_num_pools = 0;48(void)const_cast<instanceOop&>(_memory_mgr_obj = instanceOop(NULL));49}5051int MemoryManager::add_pool(MemoryPool* pool) {52int index = _num_pools;53assert(index < MemoryManager::max_num_pools, "_num_pools exceeds the max");54if (index < MemoryManager::max_num_pools) {55_pools[index] = pool;56_num_pools++;57}58pool->add_manager(this);59return index;60}6162MemoryManager* MemoryManager::get_code_cache_memory_manager() {63return (MemoryManager*) new CodeCacheMemoryManager();64}6566MemoryManager* MemoryManager::get_metaspace_memory_manager() {67return (MemoryManager*) new MetaspaceMemoryManager();68}6970GCMemoryManager* MemoryManager::get_copy_memory_manager() {71return (GCMemoryManager*) new CopyMemoryManager();72}7374GCMemoryManager* MemoryManager::get_msc_memory_manager() {75return (GCMemoryManager*) new MSCMemoryManager();76}7778GCMemoryManager* MemoryManager::get_parnew_memory_manager() {79return (GCMemoryManager*) new ParNewMemoryManager();80}8182GCMemoryManager* MemoryManager::get_cms_memory_manager() {83return (GCMemoryManager*) new CMSMemoryManager();84}8586GCMemoryManager* MemoryManager::get_psScavenge_memory_manager() {87return (GCMemoryManager*) new PSScavengeMemoryManager();88}8990GCMemoryManager* MemoryManager::get_psMarkSweep_memory_manager() {91return (GCMemoryManager*) new PSMarkSweepMemoryManager();92}9394GCMemoryManager* MemoryManager::get_g1YoungGen_memory_manager() {95return (GCMemoryManager*) new G1YoungGenMemoryManager();96}9798GCMemoryManager* MemoryManager::get_g1OldGen_memory_manager() {99return (GCMemoryManager*) new G1OldGenMemoryManager();100}101102GCMemoryManager* MemoryManager::get_shenandoah_cycles_memory_manager() {103return (GCMemoryManager*) new ShenandoahCyclesMemoryManager();104}105106GCMemoryManager* MemoryManager::get_shenandoah_pauses_memory_manager() {107return (GCMemoryManager*) new ShenandoahPausesMemoryManager();108}109110instanceOop MemoryManager::get_memory_manager_instance(TRAPS) {111// Must do an acquire so as to force ordering of subsequent112// loads from anything _memory_mgr_obj points to or implies.113instanceOop mgr_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_mgr_obj);114if (mgr_obj == NULL) {115// It's ok for more than one thread to execute the code up to the locked region.116// Extra manager instances will just be gc'ed.117Klass* k = Management::sun_management_ManagementFactory_klass(CHECK_0);118instanceKlassHandle ik(THREAD, k);119120Handle mgr_name = java_lang_String::create_from_str(name(), CHECK_0);121122JavaValue result(T_OBJECT);123JavaCallArguments args;124args.push_oop(mgr_name); // Argument 1125126Symbol* method_name = NULL;127Symbol* signature = NULL;128if (is_gc_memory_manager()) {129method_name = vmSymbols::createGarbageCollector_name();130signature = vmSymbols::createGarbageCollector_signature();131args.push_oop(Handle()); // Argument 2 (for future extension)132} else {133method_name = vmSymbols::createMemoryManager_name();134signature = vmSymbols::createMemoryManager_signature();135}136137JavaCalls::call_static(&result,138ik,139method_name,140signature,141&args,142CHECK_0);143144instanceOop m = (instanceOop) result.get_jobject();145instanceHandle mgr(THREAD, m);146147{148// Get lock before setting _memory_mgr_obj149// since another thread may have created the instance150MutexLocker ml(Management_lock);151152// Check if another thread has created the management object. We reload153// _memory_mgr_obj here because some other thread may have initialized154// it while we were executing the code before the lock.155//156// The lock has done an acquire, so the load can't float above it, but157// we need to do a load_acquire as above.158mgr_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_mgr_obj);159if (mgr_obj != NULL) {160return mgr_obj;161}162163// Get the address of the object we created via call_special.164mgr_obj = mgr();165166// Use store barrier to make sure the memory accesses associated167// with creating the management object are visible before publishing168// its address. The unlock will publish the store to _memory_mgr_obj169// because it does a release first.170OrderAccess::release_store_ptr(&_memory_mgr_obj, mgr_obj);171}172}173174return mgr_obj;175}176177void MemoryManager::oops_do(OopClosure* f) {178f->do_oop((oop*) &_memory_mgr_obj);179}180181GCStatInfo::GCStatInfo(int num_pools) {182// initialize the arrays for memory usage183_before_gc_usage_array = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools, mtInternal);184_after_gc_usage_array = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools, mtInternal);185_usage_array_size = num_pools;186clear();187}188189GCStatInfo::~GCStatInfo() {190FREE_C_HEAP_ARRAY(MemoryUsage*, _before_gc_usage_array, mtInternal);191FREE_C_HEAP_ARRAY(MemoryUsage*, _after_gc_usage_array, mtInternal);192}193194void GCStatInfo::set_gc_usage(int pool_index, MemoryUsage usage, bool before_gc) {195MemoryUsage* gc_usage_array;196if (before_gc) {197gc_usage_array = _before_gc_usage_array;198} else {199gc_usage_array = _after_gc_usage_array;200}201gc_usage_array[pool_index] = usage;202}203204void GCStatInfo::clear() {205_index = 0;206_start_time = 0L;207_end_time = 0L;208size_t len = _usage_array_size * sizeof(MemoryUsage);209memset(_before_gc_usage_array, 0, len);210memset(_after_gc_usage_array, 0, len);211}212213214GCMemoryManager::GCMemoryManager() : MemoryManager() {215_num_collections = 0;216_last_gc_stat = NULL;217_last_gc_lock = new Mutex(Mutex::leaf, "_last_gc_lock", true);218_current_gc_stat = NULL;219_num_gc_threads = 1;220_notification_enabled = false;221}222223GCMemoryManager::~GCMemoryManager() {224delete _last_gc_stat;225delete _last_gc_lock;226delete _current_gc_stat;227}228229void GCMemoryManager::add_pool(MemoryPool* pool) {230add_pool(pool, true);231}232233void GCMemoryManager::add_pool(MemoryPool* pool, bool always_affected_by_gc) {234int index = MemoryManager::add_pool(pool);235_pool_always_affected_by_gc[index] = always_affected_by_gc;236}237238void GCMemoryManager::initialize_gc_stat_info() {239assert(MemoryService::num_memory_pools() > 0, "should have one or more memory pools");240_last_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());241_current_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());242// tracking concurrent collections we need two objects: one to update, and one to243// hold the publicly available "last (completed) gc" information.244}245246void GCMemoryManager::gc_begin(bool recordGCBeginTime, bool recordPreGCUsage,247bool recordAccumulatedGCTime) {248assert(_last_gc_stat != NULL && _current_gc_stat != NULL, "Just checking");249if (recordAccumulatedGCTime) {250_accumulated_timer.start();251}252// _num_collections now increases in gc_end, to count completed collections253if (recordGCBeginTime) {254_current_gc_stat->set_index(_num_collections+1);255_current_gc_stat->set_start_time(Management::timestamp());256}257258if (recordPreGCUsage) {259// Keep memory usage of all memory pools260for (int i = 0; i < MemoryService::num_memory_pools(); i++) {261MemoryPool* pool = MemoryService::get_memory_pool(i);262MemoryUsage usage = pool->get_memory_usage();263_current_gc_stat->set_before_gc_usage(i, usage);264#ifndef USDT2265HS_DTRACE_PROBE8(hotspot, mem__pool__gc__begin,266name(), strlen(name()),267pool->name(), strlen(pool->name()),268usage.init_size(), usage.used(),269usage.committed(), usage.max_size());270#else /* USDT2 */271HOTSPOT_MEM_POOL_GC_BEGIN(272(char *) name(), strlen(name()),273(char *) pool->name(), strlen(pool->name()),274usage.init_size(), usage.used(),275usage.committed(), usage.max_size());276#endif /* USDT2 */277}278}279}280281// A collector MUST, even if it does not complete for some reason,282// make a TraceMemoryManagerStats object where countCollection is true,283// to ensure the current gc stat is placed in _last_gc_stat.284void GCMemoryManager::gc_end(bool recordPostGCUsage,285bool recordAccumulatedGCTime,286bool recordGCEndTime, bool countCollection,287GCCause::Cause cause,288bool allMemoryPoolsAffected) {289if (recordAccumulatedGCTime) {290_accumulated_timer.stop();291}292if (recordGCEndTime) {293_current_gc_stat->set_end_time(Management::timestamp());294}295296if (recordPostGCUsage) {297int i;298// keep the last gc statistics for all memory pools299for (i = 0; i < MemoryService::num_memory_pools(); i++) {300MemoryPool* pool = MemoryService::get_memory_pool(i);301MemoryUsage usage = pool->get_memory_usage();302303#ifndef USDT2304HS_DTRACE_PROBE8(hotspot, mem__pool__gc__end,305name(), strlen(name()),306pool->name(), strlen(pool->name()),307usage.init_size(), usage.used(),308usage.committed(), usage.max_size());309#else /* USDT2 */310HOTSPOT_MEM_POOL_GC_END(311(char *) name(), strlen(name()),312(char *) pool->name(), strlen(pool->name()),313usage.init_size(), usage.used(),314usage.committed(), usage.max_size());315#endif /* USDT2 */316317_current_gc_stat->set_after_gc_usage(i, usage);318}319320// Set last collection usage of the memory pools managed by this collector321for (i = 0; i < num_memory_pools(); i++) {322MemoryPool* pool = get_memory_pool(i);323MemoryUsage usage = pool->get_memory_usage();324325// Compare with GC usage threshold326if (allMemoryPoolsAffected || pool_always_affected_by_gc(i)) {327// Compare with GC usage threshold328pool->set_last_collection_usage(usage);329LowMemoryDetector::detect_after_gc_memory(pool);330}331}332}333334if (countCollection) {335_num_collections++;336// alternately update two objects making one public when complete337{338MutexLockerEx ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);339GCStatInfo *tmp = _last_gc_stat;340_last_gc_stat = _current_gc_stat;341_current_gc_stat = tmp;342// reset the current stat for diagnosability purposes343_current_gc_stat->clear();344}345346if (is_notification_enabled()) {347bool isMajorGC = this == MemoryService::get_major_gc_manager();348GCNotifier::pushNotification(this, isMajorGC ? "end of major GC" : "end of minor GC",349GCCause::to_string(cause));350}351}352}353354size_t GCMemoryManager::get_last_gc_stat(GCStatInfo* dest) {355MutexLockerEx ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);356if (_last_gc_stat->gc_index() != 0) {357dest->set_index(_last_gc_stat->gc_index());358dest->set_start_time(_last_gc_stat->start_time());359dest->set_end_time(_last_gc_stat->end_time());360assert(dest->usage_array_size() == _last_gc_stat->usage_array_size(),361"Must have same array size");362size_t len = dest->usage_array_size() * sizeof(MemoryUsage);363memcpy(dest->before_gc_usage_array(), _last_gc_stat->before_gc_usage_array(), len);364memcpy(dest->after_gc_usage_array(), _last_gc_stat->after_gc_usage_array(), len);365}366return _last_gc_stat->gc_index();367}368369370