Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/src/share/vm/services/memoryManager.cpp
48785 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}101102instanceOop MemoryManager::get_memory_manager_instance(TRAPS) {103// Must do an acquire so as to force ordering of subsequent104// loads from anything _memory_mgr_obj points to or implies.105instanceOop mgr_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_mgr_obj);106if (mgr_obj == NULL) {107// It's ok for more than one thread to execute the code up to the locked region.108// Extra manager instances will just be gc'ed.109Klass* k = Management::sun_management_ManagementFactory_klass(CHECK_0);110instanceKlassHandle ik(THREAD, k);111112Handle mgr_name = java_lang_String::create_from_str(name(), CHECK_0);113114JavaValue result(T_OBJECT);115JavaCallArguments args;116args.push_oop(mgr_name); // Argument 1117118Symbol* method_name = NULL;119Symbol* signature = NULL;120if (is_gc_memory_manager()) {121method_name = vmSymbols::createGarbageCollector_name();122signature = vmSymbols::createGarbageCollector_signature();123args.push_oop(Handle()); // Argument 2 (for future extension)124} else {125method_name = vmSymbols::createMemoryManager_name();126signature = vmSymbols::createMemoryManager_signature();127}128129JavaCalls::call_static(&result,130ik,131method_name,132signature,133&args,134CHECK_0);135136instanceOop m = (instanceOop) result.get_jobject();137instanceHandle mgr(THREAD, m);138139{140// Get lock before setting _memory_mgr_obj141// since another thread may have created the instance142MutexLocker ml(Management_lock);143144// Check if another thread has created the management object. We reload145// _memory_mgr_obj here because some other thread may have initialized146// it while we were executing the code before the lock.147//148// The lock has done an acquire, so the load can't float above it, but149// we need to do a load_acquire as above.150mgr_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_mgr_obj);151if (mgr_obj != NULL) {152return mgr_obj;153}154155// Get the address of the object we created via call_special.156mgr_obj = mgr();157158// Use store barrier to make sure the memory accesses associated159// with creating the management object are visible before publishing160// its address. The unlock will publish the store to _memory_mgr_obj161// because it does a release first.162OrderAccess::release_store_ptr(&_memory_mgr_obj, mgr_obj);163}164}165166return mgr_obj;167}168169void MemoryManager::oops_do(OopClosure* f) {170f->do_oop((oop*) &_memory_mgr_obj);171}172173GCStatInfo::GCStatInfo(int num_pools) {174// initialize the arrays for memory usage175_before_gc_usage_array = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools, mtInternal);176_after_gc_usage_array = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools, mtInternal);177_usage_array_size = num_pools;178clear();179}180181GCStatInfo::~GCStatInfo() {182FREE_C_HEAP_ARRAY(MemoryUsage*, _before_gc_usage_array, mtInternal);183FREE_C_HEAP_ARRAY(MemoryUsage*, _after_gc_usage_array, mtInternal);184}185186void GCStatInfo::set_gc_usage(int pool_index, MemoryUsage usage, bool before_gc) {187MemoryUsage* gc_usage_array;188if (before_gc) {189gc_usage_array = _before_gc_usage_array;190} else {191gc_usage_array = _after_gc_usage_array;192}193gc_usage_array[pool_index] = usage;194}195196void GCStatInfo::clear() {197_index = 0;198_start_time = 0L;199_end_time = 0L;200size_t len = _usage_array_size * sizeof(MemoryUsage);201memset(_before_gc_usage_array, 0, len);202memset(_after_gc_usage_array, 0, len);203}204205206GCMemoryManager::GCMemoryManager() : MemoryManager() {207_num_collections = 0;208_last_gc_stat = NULL;209_last_gc_lock = new Mutex(Mutex::leaf, "_last_gc_lock", true);210_current_gc_stat = NULL;211_num_gc_threads = 1;212_notification_enabled = false;213}214215GCMemoryManager::~GCMemoryManager() {216delete _last_gc_stat;217delete _last_gc_lock;218delete _current_gc_stat;219}220221void GCMemoryManager::add_pool(MemoryPool* pool) {222add_pool(pool, true);223}224225void GCMemoryManager::add_pool(MemoryPool* pool, bool always_affected_by_gc) {226int index = MemoryManager::add_pool(pool);227_pool_always_affected_by_gc[index] = always_affected_by_gc;228}229230void GCMemoryManager::initialize_gc_stat_info() {231assert(MemoryService::num_memory_pools() > 0, "should have one or more memory pools");232_last_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());233_current_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());234// tracking concurrent collections we need two objects: one to update, and one to235// hold the publicly available "last (completed) gc" information.236}237238void GCMemoryManager::gc_begin(bool recordGCBeginTime, bool recordPreGCUsage,239bool recordAccumulatedGCTime) {240assert(_last_gc_stat != NULL && _current_gc_stat != NULL, "Just checking");241if (recordAccumulatedGCTime) {242_accumulated_timer.start();243}244// _num_collections now increases in gc_end, to count completed collections245if (recordGCBeginTime) {246_current_gc_stat->set_index(_num_collections+1);247_current_gc_stat->set_start_time(Management::timestamp());248}249250if (recordPreGCUsage) {251// Keep memory usage of all memory pools252for (int i = 0; i < MemoryService::num_memory_pools(); i++) {253MemoryPool* pool = MemoryService::get_memory_pool(i);254MemoryUsage usage = pool->get_memory_usage();255_current_gc_stat->set_before_gc_usage(i, usage);256#ifndef USDT2257HS_DTRACE_PROBE8(hotspot, mem__pool__gc__begin,258name(), strlen(name()),259pool->name(), strlen(pool->name()),260usage.init_size(), usage.used(),261usage.committed(), usage.max_size());262#else /* USDT2 */263HOTSPOT_MEM_POOL_GC_BEGIN(264(char *) name(), strlen(name()),265(char *) pool->name(), strlen(pool->name()),266usage.init_size(), usage.used(),267usage.committed(), usage.max_size());268#endif /* USDT2 */269}270}271}272273// A collector MUST, even if it does not complete for some reason,274// make a TraceMemoryManagerStats object where countCollection is true,275// to ensure the current gc stat is placed in _last_gc_stat.276void GCMemoryManager::gc_end(bool recordPostGCUsage,277bool recordAccumulatedGCTime,278bool recordGCEndTime, bool countCollection,279GCCause::Cause cause,280bool allMemoryPoolsAffected) {281if (recordAccumulatedGCTime) {282_accumulated_timer.stop();283}284if (recordGCEndTime) {285_current_gc_stat->set_end_time(Management::timestamp());286}287288if (recordPostGCUsage) {289int i;290// keep the last gc statistics for all memory pools291for (i = 0; i < MemoryService::num_memory_pools(); i++) {292MemoryPool* pool = MemoryService::get_memory_pool(i);293MemoryUsage usage = pool->get_memory_usage();294295#ifndef USDT2296HS_DTRACE_PROBE8(hotspot, mem__pool__gc__end,297name(), strlen(name()),298pool->name(), strlen(pool->name()),299usage.init_size(), usage.used(),300usage.committed(), usage.max_size());301#else /* USDT2 */302HOTSPOT_MEM_POOL_GC_END(303(char *) name(), strlen(name()),304(char *) pool->name(), strlen(pool->name()),305usage.init_size(), usage.used(),306usage.committed(), usage.max_size());307#endif /* USDT2 */308309_current_gc_stat->set_after_gc_usage(i, usage);310}311312// Set last collection usage of the memory pools managed by this collector313for (i = 0; i < num_memory_pools(); i++) {314MemoryPool* pool = get_memory_pool(i);315MemoryUsage usage = pool->get_memory_usage();316317// Compare with GC usage threshold318if (allMemoryPoolsAffected || pool_always_affected_by_gc(i)) {319// Compare with GC usage threshold320pool->set_last_collection_usage(usage);321LowMemoryDetector::detect_after_gc_memory(pool);322}323}324}325326if (countCollection) {327_num_collections++;328// alternately update two objects making one public when complete329{330MutexLockerEx ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);331GCStatInfo *tmp = _last_gc_stat;332_last_gc_stat = _current_gc_stat;333_current_gc_stat = tmp;334// reset the current stat for diagnosability purposes335_current_gc_stat->clear();336}337338if (is_notification_enabled()) {339bool isMajorGC = this == MemoryService::get_major_gc_manager();340GCNotifier::pushNotification(this, isMajorGC ? "end of major GC" : "end of minor GC",341GCCause::to_string(cause));342}343}344}345346size_t GCMemoryManager::get_last_gc_stat(GCStatInfo* dest) {347MutexLockerEx ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);348if (_last_gc_stat->gc_index() != 0) {349dest->set_index(_last_gc_stat->gc_index());350dest->set_start_time(_last_gc_stat->start_time());351dest->set_end_time(_last_gc_stat->end_time());352assert(dest->usage_array_size() == _last_gc_stat->usage_array_size(),353"Must have same array size");354size_t len = dest->usage_array_size() * sizeof(MemoryUsage);355memcpy(dest->before_gc_usage_array(), _last_gc_stat->before_gc_usage_array(), len);356memcpy(dest->after_gc_usage_array(), _last_gc_stat->after_gc_usage_array(), len);357}358return _last_gc_stat->gc_index();359}360361362