Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/src/share/vm/services/memoryManager.hpp
48773 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#ifndef SHARE_VM_SERVICES_MEMORYMANAGER_HPP25#define SHARE_VM_SERVICES_MEMORYMANAGER_HPP2627#include "memory/allocation.hpp"28#include "runtime/timer.hpp"29#include "services/memoryUsage.hpp"3031// A memory manager is responsible for managing one or more memory pools.32// The garbage collector is one type of memory managers responsible33// for reclaiming memory occupied by unreachable objects. A Java virtual34// machine may have one or more memory managers. It may35// add or remove memory managers during execution.36// A memory pool can be managed by more than one memory managers.3738class MemoryPool;39class GCMemoryManager;40class OopClosure;4142class MemoryManager : public CHeapObj<mtInternal> {43protected:44enum {45max_num_pools = 1046};4748private:49MemoryPool* _pools[max_num_pools];50int _num_pools;5152protected:53volatile instanceOop _memory_mgr_obj;5455public:56enum Name {57Abstract,58CodeCache,59Metaspace,60Copy,61MarkSweepCompact,62ParNew,63ConcurrentMarkSweep,64PSScavenge,65PSMarkSweep,66G1YoungGen,67G1OldGen68};6970MemoryManager();7172int num_memory_pools() const { return _num_pools; }73MemoryPool* get_memory_pool(int index) {74assert(index >= 0 && index < _num_pools, "Invalid index");75return _pools[index];76}7778int add_pool(MemoryPool* pool);7980bool is_manager(instanceHandle mh) { return mh() == _memory_mgr_obj; }8182virtual instanceOop get_memory_manager_instance(TRAPS);83virtual MemoryManager::Name kind() { return MemoryManager::Abstract; }84virtual bool is_gc_memory_manager() { return false; }85virtual const char* name() = 0;8687// GC support88void oops_do(OopClosure* f);8990// Static factory methods to get a memory manager of a specific type91static MemoryManager* get_code_cache_memory_manager();92static MemoryManager* get_metaspace_memory_manager();93static GCMemoryManager* get_copy_memory_manager();94static GCMemoryManager* get_msc_memory_manager();95static GCMemoryManager* get_parnew_memory_manager();96static GCMemoryManager* get_cms_memory_manager();97static GCMemoryManager* get_psScavenge_memory_manager();98static GCMemoryManager* get_psMarkSweep_memory_manager();99static GCMemoryManager* get_g1YoungGen_memory_manager();100static GCMemoryManager* get_g1OldGen_memory_manager();101102};103104class CodeCacheMemoryManager : public MemoryManager {105private:106public:107CodeCacheMemoryManager() : MemoryManager() {}108109MemoryManager::Name kind() { return MemoryManager::CodeCache; }110const char* name() { return "CodeCacheManager"; }111};112113class MetaspaceMemoryManager : public MemoryManager {114public:115MetaspaceMemoryManager() : MemoryManager() {}116117MemoryManager::Name kind() { return MemoryManager::Metaspace; }118const char *name() { return "Metaspace Manager"; }119};120121class GCStatInfo : public ResourceObj {122private:123size_t _index;124jlong _start_time;125jlong _end_time;126127// We keep memory usage of all memory pools128MemoryUsage* _before_gc_usage_array;129MemoryUsage* _after_gc_usage_array;130int _usage_array_size;131132void set_gc_usage(int pool_index, MemoryUsage, bool before_gc);133134public:135GCStatInfo(int num_pools);136~GCStatInfo();137138size_t gc_index() { return _index; }139jlong start_time() { return _start_time; }140jlong end_time() { return _end_time; }141int usage_array_size() { return _usage_array_size; }142MemoryUsage before_gc_usage_for_pool(int pool_index) {143assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");144return _before_gc_usage_array[pool_index];145}146MemoryUsage after_gc_usage_for_pool(int pool_index) {147assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");148return _after_gc_usage_array[pool_index];149}150151MemoryUsage* before_gc_usage_array() { return _before_gc_usage_array; }152MemoryUsage* after_gc_usage_array() { return _after_gc_usage_array; }153154void set_index(size_t index) { _index = index; }155void set_start_time(jlong time) { _start_time = time; }156void set_end_time(jlong time) { _end_time = time; }157void set_before_gc_usage(int pool_index, MemoryUsage usage) {158assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");159set_gc_usage(pool_index, usage, true /* before gc */);160}161void set_after_gc_usage(int pool_index, MemoryUsage usage) {162assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");163set_gc_usage(pool_index, usage, false /* after gc */);164}165166void clear();167};168169class GCMemoryManager : public MemoryManager {170private:171// TODO: We should unify the GCCounter and GCMemoryManager statistic172size_t _num_collections;173elapsedTimer _accumulated_timer;174elapsedTimer _gc_timer; // for measuring every GC duration175GCStatInfo* _last_gc_stat;176Mutex* _last_gc_lock;177GCStatInfo* _current_gc_stat;178int _num_gc_threads;179volatile bool _notification_enabled;180bool _pool_always_affected_by_gc[MemoryManager::max_num_pools];181182public:183GCMemoryManager();184~GCMemoryManager();185186void add_pool(MemoryPool* pool);187void add_pool(MemoryPool* pool, bool always_affected_by_gc);188189bool pool_always_affected_by_gc(int index) {190assert(index >= 0 && index < num_memory_pools(), "Invalid index");191return _pool_always_affected_by_gc[index];192}193194void initialize_gc_stat_info();195196bool is_gc_memory_manager() { return true; }197jlong gc_time_ms() { return _accumulated_timer.milliseconds(); }198size_t gc_count() { return _num_collections; }199int num_gc_threads() { return _num_gc_threads; }200void set_num_gc_threads(int count) { _num_gc_threads = count; }201202void gc_begin(bool recordGCBeginTime, bool recordPreGCUsage,203bool recordAccumulatedGCTime);204void gc_end(bool recordPostGCUsage, bool recordAccumulatedGCTime,205bool recordGCEndTime, bool countCollection, GCCause::Cause cause,206bool allMemoryPoolsAffected);207208void reset_gc_stat() { _num_collections = 0; _accumulated_timer.reset(); }209210// Copy out _last_gc_stat to the given destination, returning211// the collection count. Zero signifies no gc has taken place.212size_t get_last_gc_stat(GCStatInfo* dest);213214void set_notification_enabled(bool enabled) { _notification_enabled = enabled; }215bool is_notification_enabled() { return _notification_enabled; }216virtual MemoryManager::Name kind() = 0;217};218219// These subclasses of GCMemoryManager are defined to include220// GC-specific information.221// TODO: Add GC-specific information222class CopyMemoryManager : public GCMemoryManager {223private:224public:225CopyMemoryManager() : GCMemoryManager() {}226227MemoryManager::Name kind() { return MemoryManager::Copy; }228const char* name() { return "Copy"; }229};230231class MSCMemoryManager : public GCMemoryManager {232private:233public:234MSCMemoryManager() : GCMemoryManager() {}235236MemoryManager::Name kind() { return MemoryManager::MarkSweepCompact; }237const char* name() { return "MarkSweepCompact"; }238239};240241class ParNewMemoryManager : public GCMemoryManager {242private:243public:244ParNewMemoryManager() : GCMemoryManager() {}245246MemoryManager::Name kind() { return MemoryManager::ParNew; }247const char* name() { return "ParNew"; }248249};250251class CMSMemoryManager : public GCMemoryManager {252private:253public:254CMSMemoryManager() : GCMemoryManager() {}255256MemoryManager::Name kind() { return MemoryManager::ConcurrentMarkSweep; }257const char* name() { return "ConcurrentMarkSweep";}258259};260261class PSScavengeMemoryManager : public GCMemoryManager {262private:263public:264PSScavengeMemoryManager() : GCMemoryManager() {}265266MemoryManager::Name kind() { return MemoryManager::PSScavenge; }267const char* name() { return "PS Scavenge"; }268269};270271class PSMarkSweepMemoryManager : public GCMemoryManager {272private:273public:274PSMarkSweepMemoryManager() : GCMemoryManager() {}275276MemoryManager::Name kind() { return MemoryManager::PSMarkSweep; }277const char* name() { return "PS MarkSweep"; }278};279280class G1YoungGenMemoryManager : public GCMemoryManager {281private:282public:283G1YoungGenMemoryManager() : GCMemoryManager() {}284285MemoryManager::Name kind() { return MemoryManager::G1YoungGen; }286const char* name() { return "G1 Young Generation"; }287};288289class G1OldGenMemoryManager : public GCMemoryManager {290private:291public:292G1OldGenMemoryManager() : GCMemoryManager() {}293294MemoryManager::Name kind() { return MemoryManager::G1OldGen; }295const char* name() { return "G1 Old Generation"; }296};297298#endif // SHARE_VM_SERVICES_MEMORYMANAGER_HPP299300301