Path: blob/master/src/hotspot/share/services/memoryService.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/vmSymbols.hpp"26#include "gc/shared/collectedHeap.hpp"27#include "logging/logConfiguration.hpp"28#include "memory/heap.hpp"29#include "memory/memRegion.hpp"30#include "memory/resourceArea.hpp"31#include "oops/oop.inline.hpp"32#include "runtime/globals.hpp"33#include "runtime/handles.inline.hpp"34#include "runtime/javaCalls.hpp"35#include "services/classLoadingService.hpp"36#include "services/lowMemoryDetector.hpp"37#include "services/management.hpp"38#include "services/memoryManager.hpp"39#include "services/memoryPool.hpp"40#include "services/memoryService.hpp"41#include "utilities/growableArray.hpp"42#include "utilities/macros.hpp"4344GrowableArray<MemoryPool*>* MemoryService::_pools_list =45new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<MemoryPool*>(init_pools_list_size, mtServiceability);46GrowableArray<MemoryManager*>* MemoryService::_managers_list =47new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<MemoryManager*>(init_managers_list_size, mtServiceability);4849MemoryManager* MemoryService::_code_cache_manager = NULL;50GrowableArray<MemoryPool*>* MemoryService::_code_heap_pools =51new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<MemoryPool*>(init_code_heap_pools_size, mtServiceability);52MemoryPool* MemoryService::_metaspace_pool = NULL;53MemoryPool* MemoryService::_compressed_class_pool = NULL;5455class GcThreadCountClosure: public ThreadClosure {56private:57int _count;58public:59GcThreadCountClosure() : _count(0) {};60void do_thread(Thread* thread);61int count() { return _count; }62};6364void GcThreadCountClosure::do_thread(Thread* thread) {65_count++;66}6768void MemoryService::set_universe_heap(CollectedHeap* heap) {69ResourceMark rm; // For internal allocations in GrowableArray.7071GrowableArray<MemoryPool*> gc_mem_pools = heap->memory_pools();72_pools_list->appendAll(&gc_mem_pools);7374// set the GC thread count75GcThreadCountClosure gctcc;76heap->gc_threads_do(&gctcc);77int count = gctcc.count();7879GrowableArray<GCMemoryManager*> gc_memory_managers = heap->memory_managers();80for (int i = 0; i < gc_memory_managers.length(); i++) {81GCMemoryManager* gc_manager = gc_memory_managers.at(i);8283if (count > 0) {84gc_manager->set_num_gc_threads(count);85}86gc_manager->initialize_gc_stat_info();87_managers_list->append(gc_manager);88}89}9091void MemoryService::add_code_heap_memory_pool(CodeHeap* heap, const char* name) {92// Create new memory pool for this heap93MemoryPool* code_heap_pool = new CodeHeapPool(heap, name, true /* support_usage_threshold */);9495// Append to lists96_code_heap_pools->append(code_heap_pool);97_pools_list->append(code_heap_pool);9899if (_code_cache_manager == NULL) {100// Create CodeCache memory manager101_code_cache_manager = MemoryManager::get_code_cache_memory_manager();102_managers_list->append(_code_cache_manager);103}104105_code_cache_manager->add_pool(code_heap_pool);106}107108void MemoryService::add_metaspace_memory_pools() {109MemoryManager* mgr = MemoryManager::get_metaspace_memory_manager();110111_metaspace_pool = new MetaspacePool();112mgr->add_pool(_metaspace_pool);113_pools_list->append(_metaspace_pool);114115if (UseCompressedClassPointers) {116_compressed_class_pool = new CompressedKlassSpacePool();117mgr->add_pool(_compressed_class_pool);118_pools_list->append(_compressed_class_pool);119}120121_managers_list->append(mgr);122}123124MemoryManager* MemoryService::get_memory_manager(instanceHandle mh) {125for (int i = 0; i < _managers_list->length(); i++) {126MemoryManager* mgr = _managers_list->at(i);127if (mgr->is_manager(mh)) {128return mgr;129}130}131return NULL;132}133134MemoryPool* MemoryService::get_memory_pool(instanceHandle ph) {135for (int i = 0; i < _pools_list->length(); i++) {136MemoryPool* pool = _pools_list->at(i);137if (pool->is_pool(ph)) {138return pool;139}140}141return NULL;142}143144void MemoryService::track_memory_usage() {145// Track the peak memory usage146for (int i = 0; i < _pools_list->length(); i++) {147MemoryPool* pool = _pools_list->at(i);148pool->record_peak_memory_usage();149}150151// Detect low memory152LowMemoryDetector::detect_low_memory();153}154155void MemoryService::track_memory_pool_usage(MemoryPool* pool) {156// Track the peak memory usage157pool->record_peak_memory_usage();158159// Detect low memory160if (LowMemoryDetector::is_enabled(pool)) {161LowMemoryDetector::detect_low_memory(pool);162}163}164165void MemoryService::gc_begin(GCMemoryManager* manager, bool recordGCBeginTime,166bool recordAccumulatedGCTime,167bool recordPreGCUsage, bool recordPeakUsage) {168169manager->gc_begin(recordGCBeginTime, recordPreGCUsage, recordAccumulatedGCTime);170171// Track the peak memory usage when GC begins172if (recordPeakUsage) {173for (int i = 0; i < _pools_list->length(); i++) {174MemoryPool* pool = _pools_list->at(i);175pool->record_peak_memory_usage();176}177}178}179180void MemoryService::gc_end(GCMemoryManager* manager, bool recordPostGCUsage,181bool recordAccumulatedGCTime,182bool recordGCEndTime, bool countCollection,183GCCause::Cause cause,184bool allMemoryPoolsAffected) {185// register the GC end statistics and memory usage186manager->gc_end(recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime,187countCollection, cause, allMemoryPoolsAffected);188}189190bool MemoryService::set_verbose(bool verbose) {191MutexLocker m(Management_lock);192// verbose will be set to the previous value193if (verbose) {194LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(gc));195} else {196LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(gc));197}198ClassLoadingService::reset_trace_class_unloading();199200return verbose;201}202203Handle MemoryService::create_MemoryUsage_obj(MemoryUsage usage, TRAPS) {204InstanceKlass* ik = Management::java_lang_management_MemoryUsage_klass(CHECK_NH);205206JavaCallArguments args(10);207args.push_long(usage.init_size_as_jlong());208args.push_long(usage.used_as_jlong());209args.push_long(usage.committed_as_jlong());210args.push_long(usage.max_size_as_jlong());211212return JavaCalls::construct_new_instance(213ik,214vmSymbols::long_long_long_long_void_signature(),215&args,216CHECK_NH);217}218219TraceMemoryManagerStats::TraceMemoryManagerStats(GCMemoryManager* gc_memory_manager,220GCCause::Cause cause,221bool allMemoryPoolsAffected,222bool recordGCBeginTime,223bool recordPreGCUsage,224bool recordPeakUsage,225bool recordPostGCUsage,226bool recordAccumulatedGCTime,227bool recordGCEndTime,228bool countCollection) {229initialize(gc_memory_manager, cause, allMemoryPoolsAffected,230recordGCBeginTime, recordPreGCUsage, recordPeakUsage,231recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime,232countCollection);233}234235// for a subclass to create then initialize an instance before invoking236// the MemoryService237void TraceMemoryManagerStats::initialize(GCMemoryManager* gc_memory_manager,238GCCause::Cause cause,239bool allMemoryPoolsAffected,240bool recordGCBeginTime,241bool recordPreGCUsage,242bool recordPeakUsage,243bool recordPostGCUsage,244bool recordAccumulatedGCTime,245bool recordGCEndTime,246bool countCollection) {247_gc_memory_manager = gc_memory_manager;248_allMemoryPoolsAffected = allMemoryPoolsAffected;249_recordGCBeginTime = recordGCBeginTime;250_recordPreGCUsage = recordPreGCUsage;251_recordPeakUsage = recordPeakUsage;252_recordPostGCUsage = recordPostGCUsage;253_recordAccumulatedGCTime = recordAccumulatedGCTime;254_recordGCEndTime = recordGCEndTime;255_countCollection = countCollection;256_cause = cause;257258MemoryService::gc_begin(_gc_memory_manager, _recordGCBeginTime, _recordAccumulatedGCTime,259_recordPreGCUsage, _recordPeakUsage);260}261262TraceMemoryManagerStats::~TraceMemoryManagerStats() {263MemoryService::gc_end(_gc_memory_manager, _recordPostGCUsage, _recordAccumulatedGCTime,264_recordGCEndTime, _countCollection, _cause, _allMemoryPoolsAffected);265}266267268