Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/services/memoryPool.cpp
32285 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/systemDictionary.hpp"26#include "classfile/vmSymbols.hpp"27#include "memory/metaspace.hpp"28#include "oops/oop.inline.hpp"29#include "runtime/handles.inline.hpp"30#include "runtime/javaCalls.hpp"31#include "runtime/orderAccess.inline.hpp"32#include "services/lowMemoryDetector.hpp"33#include "services/management.hpp"34#include "services/memoryManager.hpp"35#include "services/memoryPool.hpp"36#include "utilities/macros.hpp"37#include "utilities/globalDefinitions.hpp"3839MemoryPool::MemoryPool(const char* name,40PoolType type,41size_t init_size,42size_t max_size,43bool support_usage_threshold,44bool support_gc_threshold) {45_name = name;46_initial_size = init_size;47_max_size = max_size;48(void)const_cast<instanceOop&>(_memory_pool_obj = instanceOop(NULL));49_available_for_allocation = true;50_num_managers = 0;51_type = type;5253// initialize the max and init size of collection usage54_after_gc_usage = MemoryUsage(_initial_size, 0, 0, _max_size);5556_usage_sensor = NULL;57_gc_usage_sensor = NULL;58// usage threshold supports both high and low threshold59_usage_threshold = new ThresholdSupport(support_usage_threshold, support_usage_threshold);60// gc usage threshold supports only high threshold61_gc_usage_threshold = new ThresholdSupport(support_gc_threshold, support_gc_threshold);62}6364void MemoryPool::add_manager(MemoryManager* mgr) {65assert(_num_managers < MemoryPool::max_num_managers, "_num_managers exceeds the max");66if (_num_managers < MemoryPool::max_num_managers) {67_managers[_num_managers] = mgr;68_num_managers++;69}70}717273// Returns an instanceHandle of a MemoryPool object.74// It creates a MemoryPool instance when the first time75// this function is called.76instanceOop MemoryPool::get_memory_pool_instance(TRAPS) {77// Must do an acquire so as to force ordering of subsequent78// loads from anything _memory_pool_obj points to or implies.79instanceOop pool_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_pool_obj);80if (pool_obj == NULL) {81// It's ok for more than one thread to execute the code up to the locked region.82// Extra pool instances will just be gc'ed.83Klass* k = Management::sun_management_ManagementFactory_klass(CHECK_NULL);84instanceKlassHandle ik(THREAD, k);8586Handle pool_name = java_lang_String::create_from_str(_name, CHECK_NULL);87jlong usage_threshold_value = (_usage_threshold->is_high_threshold_supported() ? 0 : -1L);88jlong gc_usage_threshold_value = (_gc_usage_threshold->is_high_threshold_supported() ? 0 : -1L);8990JavaValue result(T_OBJECT);91JavaCallArguments args;92args.push_oop(pool_name); // Argument 193args.push_int((int) is_heap()); // Argument 29495Symbol* method_name = vmSymbols::createMemoryPool_name();96Symbol* signature = vmSymbols::createMemoryPool_signature();9798args.push_long(usage_threshold_value); // Argument 399args.push_long(gc_usage_threshold_value); // Argument 4100101JavaCalls::call_static(&result,102ik,103method_name,104signature,105&args,106CHECK_NULL);107108instanceOop p = (instanceOop) result.get_jobject();109instanceHandle pool(THREAD, p);110111{112// Get lock since another thread may have create the instance113MutexLocker ml(Management_lock);114115// Check if another thread has created the pool. We reload116// _memory_pool_obj here because some other thread may have117// initialized it while we were executing the code before the lock.118//119// The lock has done an acquire, so the load can't float above it,120// but we need to do a load_acquire as above.121pool_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_pool_obj);122if (pool_obj != NULL) {123return pool_obj;124}125126// Get the address of the object we created via call_special.127pool_obj = pool();128129// Use store barrier to make sure the memory accesses associated130// with creating the pool are visible before publishing its address.131// The unlock will publish the store to _memory_pool_obj because132// it does a release first.133OrderAccess::release_store_ptr(&_memory_pool_obj, pool_obj);134}135}136137return pool_obj;138}139140inline static size_t get_max_value(size_t val1, size_t val2) {141return (val1 > val2 ? val1 : val2);142}143144void MemoryPool::record_peak_memory_usage() {145// Caller in JDK is responsible for synchronization -146// acquire the lock for this memory pool before calling VM147MemoryUsage usage = get_memory_usage();148size_t peak_used = get_max_value(usage.used(), _peak_usage.used());149size_t peak_committed = get_max_value(usage.committed(), _peak_usage.committed());150size_t peak_max_size = get_max_value(usage.max_size(), _peak_usage.max_size());151152_peak_usage = MemoryUsage(initial_size(), peak_used, peak_committed, peak_max_size);153}154155static void set_sensor_obj_at(SensorInfo** sensor_ptr, instanceHandle sh) {156assert(*sensor_ptr == NULL, "Should be called only once");157SensorInfo* sensor = new SensorInfo();158sensor->set_sensor(sh());159*sensor_ptr = sensor;160}161162void MemoryPool::set_usage_sensor_obj(instanceHandle sh) {163set_sensor_obj_at(&_usage_sensor, sh);164}165166void MemoryPool::set_gc_usage_sensor_obj(instanceHandle sh) {167set_sensor_obj_at(&_gc_usage_sensor, sh);168}169170void MemoryPool::oops_do(OopClosure* f) {171f->do_oop((oop*) &_memory_pool_obj);172if (_usage_sensor != NULL) {173_usage_sensor->oops_do(f);174}175if (_gc_usage_sensor != NULL) {176_gc_usage_sensor->oops_do(f);177}178}179180ContiguousSpacePool::ContiguousSpacePool(ContiguousSpace* space,181const char* name,182PoolType type,183size_t max_size,184bool support_usage_threshold) :185CollectedMemoryPool(name, type, space->capacity(), max_size,186support_usage_threshold), _space(space) {187}188189MemoryUsage ContiguousSpacePool::get_memory_usage() {190size_t maxSize = (available_for_allocation() ? max_size() : 0);191size_t used = used_in_bytes();192size_t committed = _space->capacity();193194return MemoryUsage(initial_size(), used, committed, maxSize);195}196197SurvivorContiguousSpacePool::SurvivorContiguousSpacePool(DefNewGeneration* gen,198const char* name,199PoolType type,200size_t max_size,201bool support_usage_threshold) :202CollectedMemoryPool(name, type, gen->from()->capacity(), max_size,203support_usage_threshold), _gen(gen) {204}205206MemoryUsage SurvivorContiguousSpacePool::get_memory_usage() {207size_t maxSize = (available_for_allocation() ? max_size() : 0);208size_t used = used_in_bytes();209size_t committed = committed_in_bytes();210211return MemoryUsage(initial_size(), used, committed, maxSize);212}213214#if INCLUDE_ALL_GCS215CompactibleFreeListSpacePool::CompactibleFreeListSpacePool(CompactibleFreeListSpace* space,216const char* name,217PoolType type,218size_t max_size,219bool support_usage_threshold) :220CollectedMemoryPool(name, type, space->capacity(), max_size,221support_usage_threshold), _space(space) {222}223224MemoryUsage CompactibleFreeListSpacePool::get_memory_usage() {225size_t maxSize = (available_for_allocation() ? max_size() : 0);226size_t used = used_in_bytes();227size_t committed = _space->capacity();228229return MemoryUsage(initial_size(), used, committed, maxSize);230}231#endif // INCLUDE_ALL_GCS232233GenerationPool::GenerationPool(Generation* gen,234const char* name,235PoolType type,236bool support_usage_threshold) :237CollectedMemoryPool(name, type, gen->capacity(), gen->max_capacity(),238support_usage_threshold), _gen(gen) {239}240241MemoryUsage GenerationPool::get_memory_usage() {242size_t used = used_in_bytes();243size_t committed = _gen->capacity();244size_t maxSize = (available_for_allocation() ? max_size() : 0);245246return MemoryUsage(initial_size(), used, committed, maxSize);247}248249CodeHeapPool::CodeHeapPool(CodeHeap* codeHeap, const char* name, bool support_usage_threshold) :250MemoryPool(name, NonHeap, codeHeap->capacity(), codeHeap->max_capacity(),251support_usage_threshold, false), _codeHeap(codeHeap) {252}253254MemoryUsage CodeHeapPool::get_memory_usage() {255size_t used = used_in_bytes();256size_t committed = _codeHeap->capacity();257size_t maxSize = (available_for_allocation() ? max_size() : 0);258259return MemoryUsage(initial_size(), used, committed, maxSize);260}261262MetaspacePool::MetaspacePool() :263MemoryPool("Metaspace", NonHeap, 0, calculate_max_size(), true, false) { }264265MemoryUsage MetaspacePool::get_memory_usage() {266size_t committed = MetaspaceAux::committed_bytes();267return MemoryUsage(initial_size(), used_in_bytes(), committed, max_size());268}269270size_t MetaspacePool::used_in_bytes() {271return MetaspaceAux::used_bytes();272}273274size_t MetaspacePool::calculate_max_size() const {275return !FLAG_IS_DEFAULT(MaxMetaspaceSize) ? MaxMetaspaceSize :276MemoryUsage::undefined_size();277}278279CompressedKlassSpacePool::CompressedKlassSpacePool() :280MemoryPool("Compressed Class Space", NonHeap, 0, CompressedClassSpaceSize, true, false) { }281282size_t CompressedKlassSpacePool::used_in_bytes() {283return MetaspaceAux::used_bytes(Metaspace::ClassType);284}285286MemoryUsage CompressedKlassSpacePool::get_memory_usage() {287size_t committed = MetaspaceAux::committed_bytes(Metaspace::ClassType);288return MemoryUsage(initial_size(), used_in_bytes(), committed, max_size());289}290291292