1 /* 2 * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_SERVICES_MEMORYMANAGER_HPP 26 #define SHARE_SERVICES_MEMORYMANAGER_HPP 27 28 #include "gc/shared/gcCause.hpp" 29 #include "memory/allocation.hpp" 30 #include "oops/oop.hpp" 31 #include "oops/oopsHierarchy.hpp" 32 #include "runtime/handles.hpp" 33 #include "runtime/timer.hpp" 34 #include "services/memoryUsage.hpp" 35 36 // A memory manager is responsible for managing one or more memory pools. 37 // The garbage collector is one type of memory managers responsible 38 // for reclaiming memory occupied by unreachable objects. A Java virtual 39 // machine may have one or more memory managers. It may 40 // add or remove memory managers during execution. 41 // A memory pool can be managed by more than one memory managers. 42 43 class MemoryPool; 44 class GCMemoryManager; 45 class OopClosure; 46 47 class MemoryManager : public CHeapObj<mtInternal> { 48 protected: 49 enum { 50 max_num_pools = 10 51 }; 52 53 private: 54 MemoryPool* _pools[max_num_pools]; 55 int _num_pools; 56 57 const char* _name; 58 59 protected: 60 volatile OopHandle _memory_mgr_obj; 61 62 public: 63 MemoryManager(const char* name); 64 num_memory_pools() const65 int num_memory_pools() const { return _num_pools; } get_memory_pool(int index)66 MemoryPool* get_memory_pool(int index) { 67 assert(index >= 0 && index < _num_pools, "Invalid index"); 68 return _pools[index]; 69 } 70 71 int add_pool(MemoryPool* pool); 72 73 bool is_manager(instanceHandle mh) const; 74 75 virtual instanceOop get_memory_manager_instance(TRAPS); is_gc_memory_manager()76 virtual bool is_gc_memory_manager() { return false; } 77 name() const78 const char* name() const { return _name; } 79 80 // Static factory methods to get a memory manager of a specific type 81 static MemoryManager* get_code_cache_memory_manager(); 82 static MemoryManager* get_metaspace_memory_manager(); 83 }; 84 85 class GCStatInfo : public ResourceObj { 86 private: 87 size_t _index; 88 jlong _start_time; 89 jlong _end_time; 90 91 // We keep memory usage of all memory pools 92 MemoryUsage* _before_gc_usage_array; 93 MemoryUsage* _after_gc_usage_array; 94 int _usage_array_size; 95 96 void set_gc_usage(int pool_index, MemoryUsage, bool before_gc); 97 98 public: 99 GCStatInfo(int num_pools); 100 ~GCStatInfo(); 101 gc_index()102 size_t gc_index() { return _index; } start_time()103 jlong start_time() { return _start_time; } end_time()104 jlong end_time() { return _end_time; } usage_array_size()105 int usage_array_size() { return _usage_array_size; } before_gc_usage_for_pool(int pool_index)106 MemoryUsage before_gc_usage_for_pool(int pool_index) { 107 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking"); 108 return _before_gc_usage_array[pool_index]; 109 } after_gc_usage_for_pool(int pool_index)110 MemoryUsage after_gc_usage_for_pool(int pool_index) { 111 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking"); 112 return _after_gc_usage_array[pool_index]; 113 } 114 before_gc_usage_array()115 MemoryUsage* before_gc_usage_array() { return _before_gc_usage_array; } after_gc_usage_array()116 MemoryUsage* after_gc_usage_array() { return _after_gc_usage_array; } 117 set_index(size_t index)118 void set_index(size_t index) { _index = index; } set_start_time(jlong time)119 void set_start_time(jlong time) { _start_time = time; } set_end_time(jlong time)120 void set_end_time(jlong time) { _end_time = time; } set_before_gc_usage(int pool_index,MemoryUsage usage)121 void set_before_gc_usage(int pool_index, MemoryUsage usage) { 122 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking"); 123 set_gc_usage(pool_index, usage, true /* before gc */); 124 } set_after_gc_usage(int pool_index,MemoryUsage usage)125 void set_after_gc_usage(int pool_index, MemoryUsage usage) { 126 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking"); 127 set_gc_usage(pool_index, usage, false /* after gc */); 128 } 129 130 void clear(); 131 }; 132 133 class GCMemoryManager : public MemoryManager { 134 private: 135 // TODO: We should unify the GCCounter and GCMemoryManager statistic 136 size_t _num_collections; 137 elapsedTimer _accumulated_timer; 138 GCStatInfo* _last_gc_stat; 139 Mutex* _last_gc_lock; 140 GCStatInfo* _current_gc_stat; 141 int _num_gc_threads; 142 volatile bool _notification_enabled; 143 const char* _gc_end_message; 144 bool _pool_always_affected_by_gc[MemoryManager::max_num_pools]; 145 146 public: 147 GCMemoryManager(const char* name, const char* gc_end_message); 148 ~GCMemoryManager(); 149 150 void add_pool(MemoryPool* pool); 151 void add_pool(MemoryPool* pool, bool always_affected_by_gc); 152 pool_always_affected_by_gc(int index)153 bool pool_always_affected_by_gc(int index) { 154 assert(index >= 0 && index < num_memory_pools(), "Invalid index"); 155 return _pool_always_affected_by_gc[index]; 156 } 157 158 void initialize_gc_stat_info(); 159 is_gc_memory_manager()160 bool is_gc_memory_manager() { return true; } gc_time_ms()161 jlong gc_time_ms() { return _accumulated_timer.milliseconds(); } gc_count()162 size_t gc_count() { return _num_collections; } num_gc_threads()163 int num_gc_threads() { return _num_gc_threads; } set_num_gc_threads(int count)164 void set_num_gc_threads(int count) { _num_gc_threads = count; } 165 166 void gc_begin(bool recordGCBeginTime, bool recordPreGCUsage, 167 bool recordAccumulatedGCTime); 168 void gc_end(bool recordPostGCUsage, bool recordAccumulatedGCTime, 169 bool recordGCEndTime, bool countCollection, GCCause::Cause cause, 170 bool allMemoryPoolsAffected); 171 reset_gc_stat()172 void reset_gc_stat() { _num_collections = 0; _accumulated_timer.reset(); } 173 174 // Copy out _last_gc_stat to the given destination, returning 175 // the collection count. Zero signifies no gc has taken place. 176 size_t get_last_gc_stat(GCStatInfo* dest); 177 set_notification_enabled(bool enabled)178 void set_notification_enabled(bool enabled) { _notification_enabled = enabled; } is_notification_enabled()179 bool is_notification_enabled() { return _notification_enabled; } 180 }; 181 182 #endif // SHARE_SERVICES_MEMORYMANAGER_HPP 183