Path: blob/master/src/hotspot/share/services/memoryPool.cpp
41145 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/javaClasses.hpp"26#include "classfile/vmSymbols.hpp"27#include "memory/metaspace.hpp"28#include "memory/metaspaceUtils.hpp"29#include "memory/universe.hpp"30#include "oops/oop.inline.hpp"31#include "oops/oopHandle.inline.hpp"32#include "runtime/atomic.hpp"33#include "runtime/globals_extension.hpp"34#include "runtime/handles.inline.hpp"35#include "runtime/javaCalls.hpp"36#include "services/lowMemoryDetector.hpp"37#include "services/management.hpp"38#include "services/memoryManager.hpp"39#include "services/memoryPool.hpp"40#include "utilities/globalDefinitions.hpp"41#include "utilities/macros.hpp"4243MemoryPool::MemoryPool(const char* name,44PoolType type,45size_t init_size,46size_t max_size,47bool support_usage_threshold,48bool support_gc_threshold) :49_name(name),50_type(type),51_initial_size(init_size),52_max_size(max_size),53_available_for_allocation(true),54_managers(),55_num_managers(0),56_peak_usage(),57_after_gc_usage(init_size, 0, 0, max_size),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_usage_sensor(),63_gc_usage_sensor(),64_memory_pool_obj()65{}6667bool MemoryPool::is_pool(instanceHandle pool) const {68return pool() == Atomic::load(&_memory_pool_obj).resolve();69}7071void MemoryPool::add_manager(MemoryManager* mgr) {72assert(_num_managers < MemoryPool::max_num_managers, "_num_managers exceeds the max");73if (_num_managers < MemoryPool::max_num_managers) {74_managers[_num_managers] = mgr;75_num_managers++;76}77}787980// Returns an instanceOop of a MemoryPool object.81// It creates a MemoryPool instance when the first time82// this function is called.83instanceOop MemoryPool::get_memory_pool_instance(TRAPS) {84// Must do an acquire so as to force ordering of subsequent85// loads from anything _memory_pool_obj points to or implies.86oop pool_obj = Atomic::load_acquire(&_memory_pool_obj).resolve();87if (pool_obj == NULL) {88// It's ok for more than one thread to execute the code up to the locked region.89// Extra pool instances will just be gc'ed.90InstanceKlass* ik = Management::sun_management_ManagementFactoryHelper_klass(CHECK_NULL);9192Handle pool_name = java_lang_String::create_from_str(_name, CHECK_NULL);93jlong usage_threshold_value = (_usage_threshold->is_high_threshold_supported() ? 0 : -1L);94jlong gc_usage_threshold_value = (_gc_usage_threshold->is_high_threshold_supported() ? 0 : -1L);9596JavaValue result(T_OBJECT);97JavaCallArguments args;98args.push_oop(pool_name); // Argument 199args.push_int((int) is_heap()); // Argument 2100101Symbol* method_name = vmSymbols::createMemoryPool_name();102Symbol* signature = vmSymbols::createMemoryPool_signature();103104args.push_long(usage_threshold_value); // Argument 3105args.push_long(gc_usage_threshold_value); // Argument 4106107JavaCalls::call_static(&result,108ik,109method_name,110signature,111&args,112CHECK_NULL);113114instanceOop p = (instanceOop) result.get_oop();115instanceHandle pool(THREAD, p);116117{118// Get lock since another thread may have create the instance119MutexLocker ml(THREAD, Management_lock);120121// Check if another thread has created the pool. We reload122// _memory_pool_obj here because some other thread may have123// initialized it while we were executing the code before the lock.124pool_obj = Atomic::load(&_memory_pool_obj).resolve();125if (pool_obj != NULL) {126return (instanceOop)pool_obj;127}128129// Get the address of the object we created via call_special.130pool_obj = pool();131132// Use store barrier to make sure the memory accesses associated133// with creating the pool are visible before publishing its address.134// The unlock will publish the store to _memory_pool_obj because135// it does a release first.136Atomic::release_store(&_memory_pool_obj, OopHandle(Universe::vm_global(), pool_obj));137}138}139140return (instanceOop)pool_obj;141}142143inline static size_t get_max_value(size_t val1, size_t val2) {144return (val1 > val2 ? val1 : val2);145}146147void MemoryPool::record_peak_memory_usage() {148// Caller in JDK is responsible for synchronization -149// acquire the lock for this memory pool before calling VM150MemoryUsage usage = get_memory_usage();151size_t peak_used = get_max_value(usage.used(), _peak_usage.used());152size_t peak_committed = get_max_value(usage.committed(), _peak_usage.committed());153size_t peak_max_size = get_max_value(usage.max_size(), _peak_usage.max_size());154155_peak_usage = MemoryUsage(initial_size(), peak_used, peak_committed, peak_max_size);156}157158static void set_sensor_obj_at(SensorInfo** sensor_ptr, instanceHandle sh) {159assert(*sensor_ptr == NULL, "Should be called only once");160SensorInfo* sensor = new SensorInfo();161sensor->set_sensor(sh());162*sensor_ptr = sensor;163}164165void MemoryPool::set_usage_sensor_obj(instanceHandle sh) {166set_sensor_obj_at(&_usage_sensor, sh);167}168169void MemoryPool::set_gc_usage_sensor_obj(instanceHandle sh) {170set_sensor_obj_at(&_gc_usage_sensor, sh);171}172173CodeHeapPool::CodeHeapPool(CodeHeap* codeHeap, const char* name, bool support_usage_threshold) :174MemoryPool(name, NonHeap, codeHeap->capacity(), codeHeap->max_capacity(),175support_usage_threshold, false), _codeHeap(codeHeap) {176}177178MemoryUsage CodeHeapPool::get_memory_usage() {179size_t used = used_in_bytes();180size_t committed = _codeHeap->capacity();181size_t maxSize = (available_for_allocation() ? max_size() : 0);182183return MemoryUsage(initial_size(), used, committed, maxSize);184}185186MetaspacePool::MetaspacePool() :187MemoryPool("Metaspace", NonHeap, 0, calculate_max_size(), true, false) { }188189MemoryUsage MetaspacePool::get_memory_usage() {190MetaspaceCombinedStats stats = MetaspaceUtils::get_combined_statistics();191return MemoryUsage(initial_size(), stats.used(), stats.committed(), max_size());192}193194size_t MetaspacePool::used_in_bytes() {195return MetaspaceUtils::used_bytes();196}197198size_t MetaspacePool::calculate_max_size() const {199return !FLAG_IS_DEFAULT(MaxMetaspaceSize) ? MaxMetaspaceSize :200MemoryUsage::undefined_size();201}202203CompressedKlassSpacePool::CompressedKlassSpacePool() :204MemoryPool("Compressed Class Space", NonHeap, 0, CompressedClassSpaceSize, true, false) { }205206size_t CompressedKlassSpacePool::used_in_bytes() {207return MetaspaceUtils::used_bytes(Metaspace::ClassType);208}209210MemoryUsage CompressedKlassSpacePool::get_memory_usage() {211MetaspaceStats stats = MetaspaceUtils::get_statistics(Metaspace::ClassType);212return MemoryUsage(initial_size(), stats.used(), stats.committed(), max_size());213}214215216