1 /* 2 * Copyright (c) 2003, 2017, 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_VM_SERVICES_MEMORYMANAGER_HPP 26 #define SHARE_VM_SERVICES_MEMORYMANAGER_HPP 27 28 #include "memory/allocation.hpp" 29 #include "runtime/timer.hpp" 30 #include "services/memoryUsage.hpp" 31 32 // A memory manager is responsible for managing one or more memory pools. 33 // The garbage collector is one type of memory managers responsible 34 // for reclaiming memory occupied by unreachable objects. A Java virtual 35 // machine may have one or more memory managers. It may 36 // add or remove memory managers during execution. 37 // A memory pool can be managed by more than one memory managers. 38 39 class MemoryPool; 40 class GCMemoryManager; 41 class OopClosure; 42 43 class MemoryManager : public CHeapObj<mtInternal> { 44 private: 45 enum { 46 max_num_pools = 10 47 }; 48 49 MemoryPool* _pools[max_num_pools]; 50 int _num_pools; 51 52 const char* _name; 53 54 protected: 55 volatile instanceOop _memory_mgr_obj; 56 57 public: 58 MemoryManager(const char* name); 59 60 int num_memory_pools() const { return _num_pools; } 61 MemoryPool* get_memory_pool(int index) { 62 assert(index >= 0 && index < _num_pools, "Invalid index"); 63 return _pools[index]; 64 } 65 66 void add_pool(MemoryPool* pool); 67 68 bool is_manager(instanceHandle mh) { return mh() == _memory_mgr_obj; } 69 70 virtual instanceOop get_memory_manager_instance(TRAPS); 71 virtual bool is_gc_memory_manager() { return false; } 72 73 const char* name() const { return _name; } 74 75 // GC support 76 void oops_do(OopClosure* f); 77 78 // Static factory methods to get a memory manager of a specific type 79 static MemoryManager* get_code_cache_memory_manager(); 80 static MemoryManager* get_metaspace_memory_manager(); 81 }; 82 83 class GCStatInfo : public ResourceObj { 84 private: 85 size_t _index; 86 jlong _start_time; 87 jlong _end_time; 88 89 // We keep memory usage of all memory pools 90 MemoryUsage* _before_gc_usage_array; 91 MemoryUsage* _after_gc_usage_array; 92 int _usage_array_size; 93 94 void set_gc_usage(int pool_index, MemoryUsage, bool before_gc); 95 96 public: 97 GCStatInfo(int num_pools); 98 ~GCStatInfo(); 99 100 size_t gc_index() { return _index; } 101 jlong start_time() { return _start_time; } 102 jlong end_time() { return _end_time; } 103 int usage_array_size() { return _usage_array_size; } 104 MemoryUsage before_gc_usage_for_pool(int pool_index) { 105 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking"); 106 return _before_gc_usage_array[pool_index]; 107 } 108 MemoryUsage after_gc_usage_for_pool(int pool_index) { 109 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking"); 110 return _after_gc_usage_array[pool_index]; 111 } 112 113 MemoryUsage* before_gc_usage_array() { return _before_gc_usage_array; } 114 MemoryUsage* after_gc_usage_array() { return _after_gc_usage_array; } 115 116 void set_index(size_t index) { _index = index; } 117 void set_start_time(jlong time) { _start_time = time; } 118 void set_end_time(jlong time) { _end_time = time; } 119 void set_before_gc_usage(int pool_index, MemoryUsage usage) { 120 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking"); 121 set_gc_usage(pool_index, usage, true /* before gc */); 122 } 123 void set_after_gc_usage(int pool_index, MemoryUsage usage) { 124 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking"); 125 set_gc_usage(pool_index, usage, false /* after gc */); 126 } 127 128 void clear(); 129 }; 130 131 class GCMemoryManager : public MemoryManager { 132 private: 133 // TODO: We should unify the GCCounter and GCMemoryManager statistic 134 size_t _num_collections; 135 elapsedTimer _accumulated_timer; 136 elapsedTimer _gc_timer; // for measuring every GC duration 137 GCStatInfo* _last_gc_stat; 138 Mutex* _last_gc_lock; 139 GCStatInfo* _current_gc_stat; 140 int _num_gc_threads; 141 volatile bool _notification_enabled; 142 const char* _gc_end_message; 143 public: 144 GCMemoryManager(const char* name, const char* gc_end_message); 145 ~GCMemoryManager(); 146 147 void initialize_gc_stat_info(); 148 149 bool is_gc_memory_manager() { return true; } 150 jlong gc_time_ms() { return _accumulated_timer.milliseconds(); } 151 size_t gc_count() { return _num_collections; } 152 int num_gc_threads() { return _num_gc_threads; } 153 void set_num_gc_threads(int count) { _num_gc_threads = count; } 154 155 void gc_begin(bool recordGCBeginTime, bool recordPreGCUsage, 156 bool recordAccumulatedGCTime); 157 void gc_end(bool recordPostGCUsage, bool recordAccumulatedGCTime, 158 bool recordGCEndTime, bool countCollection, GCCause::Cause cause); 159 160 void reset_gc_stat() { _num_collections = 0; _accumulated_timer.reset(); } 161 162 // Copy out _last_gc_stat to the given destination, returning 163 // the collection count. Zero signifies no gc has taken place. 164 size_t get_last_gc_stat(GCStatInfo* dest); 165 166 void set_notification_enabled(bool enabled) { _notification_enabled = enabled; } 167 bool is_notification_enabled() { return _notification_enabled; } 168 }; 169 170 #endif // SHARE_VM_SERVICES_MEMORYMANAGER_HPP