Path: blob/master/src/hotspot/share/services/memoryManager.hpp
41144 views
/*1* Copyright (c) 2003, 2020, 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_SERVICES_MEMORYMANAGER_HPP25#define SHARE_SERVICES_MEMORYMANAGER_HPP2627#include "gc/shared/gcCause.hpp"28#include "memory/allocation.hpp"29#include "oops/oop.hpp"30#include "oops/oopsHierarchy.hpp"31#include "runtime/handles.hpp"32#include "runtime/timer.hpp"33#include "services/memoryUsage.hpp"3435// A memory manager is responsible for managing one or more memory pools.36// The garbage collector is one type of memory managers responsible37// for reclaiming memory occupied by unreachable objects. A Java virtual38// machine may have one or more memory managers. It may39// add or remove memory managers during execution.40// A memory pool can be managed by more than one memory managers.4142class MemoryPool;43class GCMemoryManager;44class OopClosure;4546class MemoryManager : public CHeapObj<mtInternal> {47protected:48enum {49max_num_pools = 1050};5152private:53MemoryPool* _pools[max_num_pools];54int _num_pools;5556const char* _name;5758protected:59volatile OopHandle _memory_mgr_obj;6061public:62MemoryManager(const char* name);6364int num_memory_pools() const { return _num_pools; }65MemoryPool* get_memory_pool(int index) {66assert(index >= 0 && index < _num_pools, "Invalid index");67return _pools[index];68}6970int add_pool(MemoryPool* pool);7172bool is_manager(instanceHandle mh) const;7374virtual instanceOop get_memory_manager_instance(TRAPS);75virtual bool is_gc_memory_manager() { return false; }7677const char* name() const { return _name; }7879// Static factory methods to get a memory manager of a specific type80static MemoryManager* get_code_cache_memory_manager();81static MemoryManager* get_metaspace_memory_manager();82};8384class GCStatInfo : public ResourceObj {85private:86size_t _index;87jlong _start_time;88jlong _end_time;8990// We keep memory usage of all memory pools91MemoryUsage* _before_gc_usage_array;92MemoryUsage* _after_gc_usage_array;93int _usage_array_size;9495void set_gc_usage(int pool_index, MemoryUsage, bool before_gc);9697public:98GCStatInfo(int num_pools);99~GCStatInfo();100101size_t gc_index() { return _index; }102jlong start_time() { return _start_time; }103jlong end_time() { return _end_time; }104int usage_array_size() { return _usage_array_size; }105MemoryUsage before_gc_usage_for_pool(int pool_index) {106assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");107return _before_gc_usage_array[pool_index];108}109MemoryUsage after_gc_usage_for_pool(int pool_index) {110assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");111return _after_gc_usage_array[pool_index];112}113114MemoryUsage* before_gc_usage_array() { return _before_gc_usage_array; }115MemoryUsage* after_gc_usage_array() { return _after_gc_usage_array; }116117void set_index(size_t index) { _index = index; }118void set_start_time(jlong time) { _start_time = time; }119void set_end_time(jlong time) { _end_time = time; }120void set_before_gc_usage(int pool_index, MemoryUsage usage) {121assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");122set_gc_usage(pool_index, usage, true /* before gc */);123}124void set_after_gc_usage(int pool_index, MemoryUsage usage) {125assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");126set_gc_usage(pool_index, usage, false /* after gc */);127}128129void clear();130};131132class GCMemoryManager : public MemoryManager {133private:134// TODO: We should unify the GCCounter and GCMemoryManager statistic135size_t _num_collections;136elapsedTimer _accumulated_timer;137GCStatInfo* _last_gc_stat;138Mutex* _last_gc_lock;139GCStatInfo* _current_gc_stat;140int _num_gc_threads;141volatile bool _notification_enabled;142const char* _gc_end_message;143bool _pool_always_affected_by_gc[MemoryManager::max_num_pools];144145public:146GCMemoryManager(const char* name, const char* gc_end_message);147~GCMemoryManager();148149void add_pool(MemoryPool* pool);150void add_pool(MemoryPool* pool, bool always_affected_by_gc);151152bool pool_always_affected_by_gc(int index) {153assert(index >= 0 && index < num_memory_pools(), "Invalid index");154return _pool_always_affected_by_gc[index];155}156157void initialize_gc_stat_info();158159bool is_gc_memory_manager() { return true; }160jlong gc_time_ms() { return _accumulated_timer.milliseconds(); }161size_t gc_count() { return _num_collections; }162int num_gc_threads() { return _num_gc_threads; }163void set_num_gc_threads(int count) { _num_gc_threads = count; }164165void gc_begin(bool recordGCBeginTime, bool recordPreGCUsage,166bool recordAccumulatedGCTime);167void gc_end(bool recordPostGCUsage, bool recordAccumulatedGCTime,168bool recordGCEndTime, bool countCollection, GCCause::Cause cause,169bool allMemoryPoolsAffected);170171void reset_gc_stat() { _num_collections = 0; _accumulated_timer.reset(); }172173// Copy out _last_gc_stat to the given destination, returning174// the collection count. Zero signifies no gc has taken place.175size_t get_last_gc_stat(GCStatInfo* dest);176177void set_notification_enabled(bool enabled) { _notification_enabled = enabled; }178bool is_notification_enabled() { return _notification_enabled; }179};180181#endif // SHARE_SERVICES_MEMORYMANAGER_HPP182183184