Sat, 01 Dec 2007 00:00:00 +0000
Initial load
duke@435 | 1 | /* |
duke@435 | 2 | * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | # include "incls/_precompiled.incl" |
duke@435 | 26 | # include "incls/_memoryPool.cpp.incl" |
duke@435 | 27 | |
duke@435 | 28 | MemoryPool::MemoryPool(const char* name, |
duke@435 | 29 | PoolType type, |
duke@435 | 30 | size_t init_size, |
duke@435 | 31 | size_t max_size, |
duke@435 | 32 | bool support_usage_threshold, |
duke@435 | 33 | bool support_gc_threshold) { |
duke@435 | 34 | _name = name; |
duke@435 | 35 | _initial_size = init_size; |
duke@435 | 36 | _max_size = max_size; |
duke@435 | 37 | _memory_pool_obj = NULL; |
duke@435 | 38 | _available_for_allocation = true; |
duke@435 | 39 | _num_managers = 0; |
duke@435 | 40 | _type = type; |
duke@435 | 41 | |
duke@435 | 42 | // initialize the max and init size of collection usage |
duke@435 | 43 | _after_gc_usage = MemoryUsage(_initial_size, 0, 0, _max_size); |
duke@435 | 44 | |
duke@435 | 45 | _usage_sensor = NULL; |
duke@435 | 46 | _gc_usage_sensor = NULL; |
duke@435 | 47 | // usage threshold supports both high and low threshold |
duke@435 | 48 | _usage_threshold = new ThresholdSupport(support_usage_threshold, support_usage_threshold); |
duke@435 | 49 | // gc usage threshold supports only high threshold |
duke@435 | 50 | _gc_usage_threshold = new ThresholdSupport(support_gc_threshold, support_gc_threshold); |
duke@435 | 51 | } |
duke@435 | 52 | |
duke@435 | 53 | void MemoryPool::add_manager(MemoryManager* mgr) { |
duke@435 | 54 | assert(_num_managers < MemoryPool::max_num_managers, "_num_managers exceeds the max"); |
duke@435 | 55 | if (_num_managers < MemoryPool::max_num_managers) { |
duke@435 | 56 | _managers[_num_managers] = mgr; |
duke@435 | 57 | _num_managers++; |
duke@435 | 58 | } |
duke@435 | 59 | } |
duke@435 | 60 | |
duke@435 | 61 | |
duke@435 | 62 | // Returns an instanceHandle of a MemoryPool object. |
duke@435 | 63 | // It creates a MemoryPool instance when the first time |
duke@435 | 64 | // this function is called. |
duke@435 | 65 | instanceOop MemoryPool::get_memory_pool_instance(TRAPS) { |
duke@435 | 66 | // Must do an acquire so as to force ordering of subsequent |
duke@435 | 67 | // loads from anything _memory_pool_obj points to or implies. |
duke@435 | 68 | instanceOop pool_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_pool_obj); |
duke@435 | 69 | if (pool_obj == NULL) { |
duke@435 | 70 | // It's ok for more than one thread to execute the code up to the locked region. |
duke@435 | 71 | // Extra pool instances will just be gc'ed. |
duke@435 | 72 | klassOop k = Management::sun_management_ManagementFactory_klass(CHECK_NULL); |
duke@435 | 73 | instanceKlassHandle ik(THREAD, k); |
duke@435 | 74 | |
duke@435 | 75 | Handle pool_name = java_lang_String::create_from_str(_name, CHECK_NULL); |
duke@435 | 76 | jlong usage_threshold_value = (_usage_threshold->is_high_threshold_supported() ? 0 : -1L); |
duke@435 | 77 | jlong gc_usage_threshold_value = (_gc_usage_threshold->is_high_threshold_supported() ? 0 : -1L); |
duke@435 | 78 | |
duke@435 | 79 | JavaValue result(T_OBJECT); |
duke@435 | 80 | JavaCallArguments args; |
duke@435 | 81 | args.push_oop(pool_name); // Argument 1 |
duke@435 | 82 | args.push_int((int) is_heap()); // Argument 2 |
duke@435 | 83 | |
duke@435 | 84 | symbolHandle method_name = vmSymbolHandles::createMemoryPool_name(); |
duke@435 | 85 | symbolHandle signature = vmSymbolHandles::createMemoryPool_signature(); |
duke@435 | 86 | |
duke@435 | 87 | args.push_long(usage_threshold_value); // Argument 3 |
duke@435 | 88 | args.push_long(gc_usage_threshold_value); // Argument 4 |
duke@435 | 89 | |
duke@435 | 90 | JavaCalls::call_static(&result, |
duke@435 | 91 | ik, |
duke@435 | 92 | method_name, |
duke@435 | 93 | signature, |
duke@435 | 94 | &args, |
duke@435 | 95 | CHECK_NULL); |
duke@435 | 96 | |
duke@435 | 97 | instanceOop p = (instanceOop) result.get_jobject(); |
duke@435 | 98 | instanceHandle pool(THREAD, p); |
duke@435 | 99 | |
duke@435 | 100 | { |
duke@435 | 101 | // Get lock since another thread may have create the instance |
duke@435 | 102 | MutexLocker ml(Management_lock); |
duke@435 | 103 | |
duke@435 | 104 | // Check if another thread has created the pool. We reload |
duke@435 | 105 | // _memory_pool_obj here because some other thread may have |
duke@435 | 106 | // initialized it while we were executing the code before the lock. |
duke@435 | 107 | // |
duke@435 | 108 | // The lock has done an acquire, so the load can't float above it, |
duke@435 | 109 | // but we need to do a load_acquire as above. |
duke@435 | 110 | pool_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_pool_obj); |
duke@435 | 111 | if (pool_obj != NULL) { |
duke@435 | 112 | return pool_obj; |
duke@435 | 113 | } |
duke@435 | 114 | |
duke@435 | 115 | // Get the address of the object we created via call_special. |
duke@435 | 116 | pool_obj = pool(); |
duke@435 | 117 | |
duke@435 | 118 | // Use store barrier to make sure the memory accesses associated |
duke@435 | 119 | // with creating the pool are visible before publishing its address. |
duke@435 | 120 | // The unlock will publish the store to _memory_pool_obj because |
duke@435 | 121 | // it does a release first. |
duke@435 | 122 | OrderAccess::release_store_ptr(&_memory_pool_obj, pool_obj); |
duke@435 | 123 | } |
duke@435 | 124 | } |
duke@435 | 125 | |
duke@435 | 126 | return pool_obj; |
duke@435 | 127 | } |
duke@435 | 128 | |
duke@435 | 129 | inline static size_t get_max_value(size_t val1, size_t val2) { |
duke@435 | 130 | return (val1 > val2 ? val1 : val2); |
duke@435 | 131 | } |
duke@435 | 132 | |
duke@435 | 133 | void MemoryPool::record_peak_memory_usage() { |
duke@435 | 134 | // Caller in JDK is responsible for synchronization - |
duke@435 | 135 | // acquire the lock for this memory pool before calling VM |
duke@435 | 136 | MemoryUsage usage = get_memory_usage(); |
duke@435 | 137 | size_t peak_used = get_max_value(usage.used(), _peak_usage.used()); |
duke@435 | 138 | size_t peak_committed = get_max_value(usage.committed(), _peak_usage.committed()); |
duke@435 | 139 | size_t peak_max_size = get_max_value(usage.max_size(), _peak_usage.max_size()); |
duke@435 | 140 | |
duke@435 | 141 | _peak_usage = MemoryUsage(initial_size(), peak_used, peak_committed, peak_max_size); |
duke@435 | 142 | } |
duke@435 | 143 | |
duke@435 | 144 | static void set_sensor_obj_at(SensorInfo** sensor_ptr, instanceHandle sh) { |
duke@435 | 145 | assert(*sensor_ptr == NULL, "Should be called only once"); |
duke@435 | 146 | SensorInfo* sensor = new SensorInfo(); |
duke@435 | 147 | sensor->set_sensor(sh()); |
duke@435 | 148 | *sensor_ptr = sensor; |
duke@435 | 149 | } |
duke@435 | 150 | |
duke@435 | 151 | void MemoryPool::set_usage_sensor_obj(instanceHandle sh) { |
duke@435 | 152 | set_sensor_obj_at(&_usage_sensor, sh); |
duke@435 | 153 | } |
duke@435 | 154 | |
duke@435 | 155 | void MemoryPool::set_gc_usage_sensor_obj(instanceHandle sh) { |
duke@435 | 156 | set_sensor_obj_at(&_gc_usage_sensor, sh); |
duke@435 | 157 | } |
duke@435 | 158 | |
duke@435 | 159 | void MemoryPool::oops_do(OopClosure* f) { |
duke@435 | 160 | f->do_oop((oop*) &_memory_pool_obj); |
duke@435 | 161 | if (_usage_sensor != NULL) { |
duke@435 | 162 | _usage_sensor->oops_do(f); |
duke@435 | 163 | } |
duke@435 | 164 | if (_gc_usage_sensor != NULL) { |
duke@435 | 165 | _gc_usage_sensor->oops_do(f); |
duke@435 | 166 | } |
duke@435 | 167 | } |
duke@435 | 168 | |
duke@435 | 169 | ContiguousSpacePool::ContiguousSpacePool(ContiguousSpace* space, |
duke@435 | 170 | const char* name, |
duke@435 | 171 | PoolType type, |
duke@435 | 172 | size_t max_size, |
duke@435 | 173 | bool support_usage_threshold) : |
duke@435 | 174 | CollectedMemoryPool(name, type, space->capacity(), max_size, |
duke@435 | 175 | support_usage_threshold), _space(space) { |
duke@435 | 176 | } |
duke@435 | 177 | |
duke@435 | 178 | MemoryUsage ContiguousSpacePool::get_memory_usage() { |
duke@435 | 179 | size_t maxSize = (available_for_allocation() ? max_size() : 0); |
duke@435 | 180 | size_t used = used_in_bytes(); |
duke@435 | 181 | size_t committed = _space->capacity(); |
duke@435 | 182 | |
duke@435 | 183 | return MemoryUsage(initial_size(), used, committed, maxSize); |
duke@435 | 184 | } |
duke@435 | 185 | |
duke@435 | 186 | SurvivorContiguousSpacePool::SurvivorContiguousSpacePool(DefNewGeneration* gen, |
duke@435 | 187 | const char* name, |
duke@435 | 188 | PoolType type, |
duke@435 | 189 | size_t max_size, |
duke@435 | 190 | bool support_usage_threshold) : |
duke@435 | 191 | CollectedMemoryPool(name, type, gen->from()->capacity(), max_size, |
duke@435 | 192 | support_usage_threshold), _gen(gen) { |
duke@435 | 193 | } |
duke@435 | 194 | |
duke@435 | 195 | MemoryUsage SurvivorContiguousSpacePool::get_memory_usage() { |
duke@435 | 196 | size_t maxSize = (available_for_allocation() ? max_size() : 0); |
duke@435 | 197 | size_t used = used_in_bytes(); |
duke@435 | 198 | size_t committed = committed_in_bytes(); |
duke@435 | 199 | |
duke@435 | 200 | return MemoryUsage(initial_size(), used, committed, maxSize); |
duke@435 | 201 | } |
duke@435 | 202 | |
duke@435 | 203 | #ifndef SERIALGC |
duke@435 | 204 | CompactibleFreeListSpacePool::CompactibleFreeListSpacePool(CompactibleFreeListSpace* space, |
duke@435 | 205 | const char* name, |
duke@435 | 206 | PoolType type, |
duke@435 | 207 | size_t max_size, |
duke@435 | 208 | bool support_usage_threshold) : |
duke@435 | 209 | CollectedMemoryPool(name, type, space->capacity(), max_size, |
duke@435 | 210 | support_usage_threshold), _space(space) { |
duke@435 | 211 | } |
duke@435 | 212 | |
duke@435 | 213 | MemoryUsage CompactibleFreeListSpacePool::get_memory_usage() { |
duke@435 | 214 | size_t maxSize = (available_for_allocation() ? max_size() : 0); |
duke@435 | 215 | size_t used = used_in_bytes(); |
duke@435 | 216 | size_t committed = _space->capacity(); |
duke@435 | 217 | |
duke@435 | 218 | return MemoryUsage(initial_size(), used, committed, maxSize); |
duke@435 | 219 | } |
duke@435 | 220 | #endif // SERIALGC |
duke@435 | 221 | |
duke@435 | 222 | GenerationPool::GenerationPool(Generation* gen, |
duke@435 | 223 | const char* name, |
duke@435 | 224 | PoolType type, |
duke@435 | 225 | bool support_usage_threshold) : |
duke@435 | 226 | CollectedMemoryPool(name, type, gen->capacity(), gen->max_capacity(), |
duke@435 | 227 | support_usage_threshold), _gen(gen) { |
duke@435 | 228 | } |
duke@435 | 229 | |
duke@435 | 230 | MemoryUsage GenerationPool::get_memory_usage() { |
duke@435 | 231 | size_t used = used_in_bytes(); |
duke@435 | 232 | size_t committed = _gen->capacity(); |
duke@435 | 233 | size_t maxSize = (available_for_allocation() ? max_size() : 0); |
duke@435 | 234 | |
duke@435 | 235 | return MemoryUsage(initial_size(), used, committed, maxSize); |
duke@435 | 236 | } |
duke@435 | 237 | |
duke@435 | 238 | CodeHeapPool::CodeHeapPool(CodeHeap* codeHeap, const char* name, bool support_usage_threshold) : |
duke@435 | 239 | MemoryPool(name, NonHeap, codeHeap->capacity(), codeHeap->max_capacity(), |
duke@435 | 240 | support_usage_threshold, false), _codeHeap(codeHeap) { |
duke@435 | 241 | } |
duke@435 | 242 | |
duke@435 | 243 | MemoryUsage CodeHeapPool::get_memory_usage() { |
duke@435 | 244 | size_t used = used_in_bytes(); |
duke@435 | 245 | size_t committed = _codeHeap->capacity(); |
duke@435 | 246 | size_t maxSize = (available_for_allocation() ? max_size() : 0); |
duke@435 | 247 | |
duke@435 | 248 | return MemoryUsage(initial_size(), used, committed, maxSize); |
duke@435 | 249 | } |