annotate hotspot/src/share/vm/services/memoryManager.cpp @ 1:489c9b5090e2

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children eb506d590394
rev   line source
duke@1 1 /*
duke@1 2 * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
duke@1 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 21 * have any questions.
duke@1 22 *
duke@1 23 */
duke@1 24
duke@1 25 # include "incls/_precompiled.incl"
duke@1 26 # include "incls/_memoryManager.cpp.incl"
duke@1 27
duke@1 28 HS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__begin, char*, int, char*, int,
duke@1 29 size_t, size_t, size_t, size_t);
duke@1 30 HS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__end, char*, int, char*, int,
duke@1 31 size_t, size_t, size_t, size_t);
duke@1 32
duke@1 33 MemoryManager::MemoryManager() {
duke@1 34 _num_pools = 0;
duke@1 35 _memory_mgr_obj = NULL;
duke@1 36 }
duke@1 37
duke@1 38 void MemoryManager::add_pool(MemoryPool* pool) {
duke@1 39 assert(_num_pools < MemoryManager::max_num_pools, "_num_pools exceeds the max");
duke@1 40 if (_num_pools < MemoryManager::max_num_pools) {
duke@1 41 _pools[_num_pools] = pool;
duke@1 42 _num_pools++;
duke@1 43 }
duke@1 44 pool->add_manager(this);
duke@1 45 }
duke@1 46
duke@1 47 MemoryManager* MemoryManager::get_code_cache_memory_manager() {
duke@1 48 return (MemoryManager*) new CodeCacheMemoryManager();
duke@1 49 }
duke@1 50
duke@1 51 GCMemoryManager* MemoryManager::get_copy_memory_manager() {
duke@1 52 return (GCMemoryManager*) new CopyMemoryManager();
duke@1 53 }
duke@1 54
duke@1 55 GCMemoryManager* MemoryManager::get_msc_memory_manager() {
duke@1 56 return (GCMemoryManager*) new MSCMemoryManager();
duke@1 57 }
duke@1 58
duke@1 59 GCMemoryManager* MemoryManager::get_parnew_memory_manager() {
duke@1 60 return (GCMemoryManager*) new ParNewMemoryManager();
duke@1 61 }
duke@1 62
duke@1 63 GCMemoryManager* MemoryManager::get_cms_memory_manager() {
duke@1 64 return (GCMemoryManager*) new CMSMemoryManager();
duke@1 65 }
duke@1 66
duke@1 67 GCMemoryManager* MemoryManager::get_psScavenge_memory_manager() {
duke@1 68 return (GCMemoryManager*) new PSScavengeMemoryManager();
duke@1 69 }
duke@1 70
duke@1 71 GCMemoryManager* MemoryManager::get_psMarkSweep_memory_manager() {
duke@1 72 return (GCMemoryManager*) new PSMarkSweepMemoryManager();
duke@1 73 }
duke@1 74
duke@1 75 instanceOop MemoryManager::get_memory_manager_instance(TRAPS) {
duke@1 76 // Must do an acquire so as to force ordering of subsequent
duke@1 77 // loads from anything _memory_mgr_obj points to or implies.
duke@1 78 instanceOop mgr_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_mgr_obj);
duke@1 79 if (mgr_obj == NULL) {
duke@1 80 // It's ok for more than one thread to execute the code up to the locked region.
duke@1 81 // Extra manager instances will just be gc'ed.
duke@1 82 klassOop k = Management::sun_management_ManagementFactory_klass(CHECK_0);
duke@1 83 instanceKlassHandle ik(THREAD, k);
duke@1 84
duke@1 85 Handle mgr_name = java_lang_String::create_from_str(name(), CHECK_0);
duke@1 86
duke@1 87 JavaValue result(T_OBJECT);
duke@1 88 JavaCallArguments args;
duke@1 89 args.push_oop(mgr_name); // Argument 1
duke@1 90
duke@1 91 symbolHandle method_name;
duke@1 92 symbolHandle signature;
duke@1 93 if (is_gc_memory_manager()) {
duke@1 94 method_name = vmSymbolHandles::createGarbageCollector_name();
duke@1 95 signature = vmSymbolHandles::createGarbageCollector_signature();
duke@1 96 args.push_oop(Handle()); // Argument 2 (for future extension)
duke@1 97 } else {
duke@1 98 method_name = vmSymbolHandles::createMemoryManager_name();
duke@1 99 signature = vmSymbolHandles::createMemoryManager_signature();
duke@1 100 }
duke@1 101
duke@1 102 JavaCalls::call_static(&result,
duke@1 103 ik,
duke@1 104 method_name,
duke@1 105 signature,
duke@1 106 &args,
duke@1 107 CHECK_0);
duke@1 108
duke@1 109 instanceOop m = (instanceOop) result.get_jobject();
duke@1 110 instanceHandle mgr(THREAD, m);
duke@1 111
duke@1 112 {
duke@1 113 // Get lock before setting _memory_mgr_obj
duke@1 114 // since another thread may have created the instance
duke@1 115 MutexLocker ml(Management_lock);
duke@1 116
duke@1 117 // Check if another thread has created the management object. We reload
duke@1 118 // _memory_mgr_obj here because some other thread may have initialized
duke@1 119 // it while we were executing the code before the lock.
duke@1 120 //
duke@1 121 // The lock has done an acquire, so the load can't float above it, but
duke@1 122 // we need to do a load_acquire as above.
duke@1 123 mgr_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_mgr_obj);
duke@1 124 if (mgr_obj != NULL) {
duke@1 125 return mgr_obj;
duke@1 126 }
duke@1 127
duke@1 128 // Get the address of the object we created via call_special.
duke@1 129 mgr_obj = mgr();
duke@1 130
duke@1 131 // Use store barrier to make sure the memory accesses associated
duke@1 132 // with creating the management object are visible before publishing
duke@1 133 // its address. The unlock will publish the store to _memory_mgr_obj
duke@1 134 // because it does a release first.
duke@1 135 OrderAccess::release_store_ptr(&_memory_mgr_obj, mgr_obj);
duke@1 136 }
duke@1 137 }
duke@1 138
duke@1 139 return mgr_obj;
duke@1 140 }
duke@1 141
duke@1 142 void MemoryManager::oops_do(OopClosure* f) {
duke@1 143 f->do_oop((oop*) &_memory_mgr_obj);
duke@1 144 }
duke@1 145
duke@1 146 GCStatInfo::GCStatInfo(int num_pools) {
duke@1 147 // initialize the arrays for memory usage
duke@1 148 _before_gc_usage_array = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools);
duke@1 149 _after_gc_usage_array = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools);
duke@1 150 size_t len = num_pools * sizeof(MemoryUsage);
duke@1 151 memset(_before_gc_usage_array, 0, len);
duke@1 152 memset(_after_gc_usage_array, 0, len);
duke@1 153 _usage_array_size = num_pools;
duke@1 154 }
duke@1 155
duke@1 156 GCStatInfo::~GCStatInfo() {
duke@1 157 FREE_C_HEAP_ARRAY(MemoryUsage*, _before_gc_usage_array);
duke@1 158 FREE_C_HEAP_ARRAY(MemoryUsage*, _after_gc_usage_array);
duke@1 159 }
duke@1 160
duke@1 161 void GCStatInfo::copy_stat(GCStatInfo* stat) {
duke@1 162 set_index(stat->gc_index());
duke@1 163 set_start_time(stat->start_time());
duke@1 164 set_end_time(stat->end_time());
duke@1 165 assert(_usage_array_size == stat->usage_array_size(), "Must have same array size");
duke@1 166 for (int i = 0; i < _usage_array_size; i++) {
duke@1 167 set_before_gc_usage(i, stat->before_gc_usage_for_pool(i));
duke@1 168 set_after_gc_usage(i, stat->after_gc_usage_for_pool(i));
duke@1 169 }
duke@1 170 }
duke@1 171
duke@1 172 void GCStatInfo::set_gc_usage(int pool_index, MemoryUsage usage, bool before_gc) {
duke@1 173 MemoryUsage* gc_usage_array;
duke@1 174 if (before_gc) {
duke@1 175 gc_usage_array = _before_gc_usage_array;
duke@1 176 } else {
duke@1 177 gc_usage_array = _after_gc_usage_array;
duke@1 178 }
duke@1 179 gc_usage_array[pool_index] = usage;
duke@1 180 }
duke@1 181
duke@1 182 GCMemoryManager::GCMemoryManager() : MemoryManager() {
duke@1 183 _num_collections = 0;
duke@1 184 _last_gc_stat = NULL;
duke@1 185 _num_gc_threads = 1;
duke@1 186 }
duke@1 187
duke@1 188 GCMemoryManager::~GCMemoryManager() {
duke@1 189 delete _last_gc_stat;
duke@1 190 }
duke@1 191
duke@1 192 void GCMemoryManager::initialize_gc_stat_info() {
duke@1 193 assert(MemoryService::num_memory_pools() > 0, "should have one or more memory pools");
duke@1 194 _last_gc_stat = new GCStatInfo(MemoryService::num_memory_pools());
duke@1 195 }
duke@1 196
duke@1 197 void GCMemoryManager::gc_begin() {
duke@1 198 assert(_last_gc_stat != NULL, "Just checking");
duke@1 199 _accumulated_timer.start();
duke@1 200 _num_collections++;
duke@1 201 _last_gc_stat->set_index(_num_collections);
duke@1 202 _last_gc_stat->set_start_time(Management::timestamp());
duke@1 203
duke@1 204 // Keep memory usage of all memory pools
duke@1 205 for (int i = 0; i < MemoryService::num_memory_pools(); i++) {
duke@1 206 MemoryPool* pool = MemoryService::get_memory_pool(i);
duke@1 207 MemoryUsage usage = pool->get_memory_usage();
duke@1 208 _last_gc_stat->set_before_gc_usage(i, usage);
duke@1 209 HS_DTRACE_PROBE8(hotspot, mem__pool__gc__begin,
duke@1 210 name(), strlen(name()),
duke@1 211 pool->name(), strlen(pool->name()),
duke@1 212 usage.init_size(), usage.used(),
duke@1 213 usage.committed(), usage.max_size());
duke@1 214 }
duke@1 215 }
duke@1 216
duke@1 217 void GCMemoryManager::gc_end() {
duke@1 218 _accumulated_timer.stop();
duke@1 219 _last_gc_stat->set_end_time(Management::timestamp());
duke@1 220
duke@1 221 int i;
duke@1 222 // keep the last gc statistics for all memory pools
duke@1 223 for (i = 0; i < MemoryService::num_memory_pools(); i++) {
duke@1 224 MemoryPool* pool = MemoryService::get_memory_pool(i);
duke@1 225 MemoryUsage usage = pool->get_memory_usage();
duke@1 226
duke@1 227 HS_DTRACE_PROBE8(hotspot, mem__pool__gc__end,
duke@1 228 name(), strlen(name()),
duke@1 229 pool->name(), strlen(pool->name()),
duke@1 230 usage.init_size(), usage.used(),
duke@1 231 usage.committed(), usage.max_size());
duke@1 232
duke@1 233 _last_gc_stat->set_after_gc_usage(i, usage);
duke@1 234 }
duke@1 235
duke@1 236 // Set last collection usage of the memory pools managed by this collector
duke@1 237 for (i = 0; i < num_memory_pools(); i++) {
duke@1 238 MemoryPool* pool = get_memory_pool(i);
duke@1 239 MemoryUsage usage = pool->get_memory_usage();
duke@1 240
duke@1 241 // Compare with GC usage threshold
duke@1 242 pool->set_last_collection_usage(usage);
duke@1 243 LowMemoryDetector::detect_after_gc_memory(pool);
duke@1 244 }
duke@1 245 }