1 /* 2 * Copyright (c) 2003, 2012, 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 #include "precompiled.hpp" 26 #include "classfile/systemDictionary.hpp" 27 #include "classfile/vmSymbols.hpp" 28 #include "oops/oop.inline.hpp" 29 #include "runtime/globals.hpp" 30 #include "runtime/handles.inline.hpp" 31 #include "runtime/javaCalls.hpp" 32 #include "services/lowMemoryDetector.hpp" 33 #include "services/management.hpp" 34 #include "services/memoryManager.hpp" 35 #include "services/memoryPool.hpp" 36 #include "utilities/macros.hpp" 37 38 MemoryPool::MemoryPool(const char* name, 39 PoolType type, 40 size_t init_size, 41 size_t max_size, 42 bool support_usage_threshold, 43 bool support_gc_threshold) { 44 _name = name; 45 _initial_size = init_size; 46 _max_size = max_size; 47 _memory_pool_obj = NULL; 48 _available_for_allocation = true; 49 _num_managers = 0; 50 _type = type; 51 52 // initialize the max and init size of collection usage 53 _after_gc_usage = MemoryUsage(_initial_size, 0, 0, _max_size); 54 55 _usage_sensor = NULL; 56 _gc_usage_sensor = NULL; 57 // usage threshold supports both high and low threshold 58 _usage_threshold = new ThresholdSupport(support_usage_threshold, support_usage_threshold); 59 // gc usage threshold supports only high threshold 60 _gc_usage_threshold = new ThresholdSupport(support_gc_threshold, support_gc_threshold); 61 } 62 63 void MemoryPool::add_manager(MemoryManager* mgr) { 64 assert(_num_managers < MemoryPool::max_num_managers, "_num_managers exceeds the max"); 65 if (_num_managers < MemoryPool::max_num_managers) { 66 _managers[_num_managers] = mgr; 67 _num_managers++; 68 } 69 } 70 71 72 // Returns an instanceHandle of a MemoryPool object. 73 // It creates a MemoryPool instance when the first time 74 // this function is called. 75 instanceOop MemoryPool::get_memory_pool_instance(TRAPS) { 76 // Must do an acquire so as to force ordering of subsequent 77 // loads from anything _memory_pool_obj points to or implies. 78 instanceOop pool_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_pool_obj); 79 if (pool_obj == NULL) { 80 // It's ok for more than one thread to execute the code up to the locked region. 81 // Extra pool instances will just be gc'ed. 82 Klass* k = Management::sun_management_ManagementFactory_klass(CHECK_NULL); 83 instanceKlassHandle ik(THREAD, k); 84 85 Handle pool_name = java_lang_String::create_from_str(_name, CHECK_NULL); 86 jlong usage_threshold_value = (_usage_threshold->is_high_threshold_supported() ? 0 : -1L); 87 jlong gc_usage_threshold_value = (_gc_usage_threshold->is_high_threshold_supported() ? 0 : -1L); 88 89 JavaValue result(T_OBJECT); 90 JavaCallArguments args; 91 args.push_oop(pool_name); // Argument 1 92 args.push_int((int) is_heap()); // Argument 2 93 94 Symbol* method_name = vmSymbols::createMemoryPool_name(); 95 Symbol* signature = vmSymbols::createMemoryPool_signature(); 96 97 args.push_long(usage_threshold_value); // Argument 3 98 args.push_long(gc_usage_threshold_value); // Argument 4 99 100 JavaCalls::call_static(&result, 101 ik, 102 method_name, 103 signature, 104 &args, 105 CHECK_NULL); 106 107 instanceOop p = (instanceOop) result.get_jobject(); 108 instanceHandle pool(THREAD, p); 109 110 { 111 // Get lock since another thread may have create the instance 112 MutexLocker ml(Management_lock); 113 114 // Check if another thread has created the pool. We reload 115 // _memory_pool_obj here because some other thread may have 116 // initialized it while we were executing the code before the lock. 117 // 118 // The lock has done an acquire, so the load can't float above it, 119 // but we need to do a load_acquire as above. 120 pool_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_pool_obj); 121 if (pool_obj != NULL) { 122 return pool_obj; 123 } 124 125 // Get the address of the object we created via call_special. 126 pool_obj = pool(); 127 128 // Use store barrier to make sure the memory accesses associated 129 // with creating the pool are visible before publishing its address. 130 // The unlock will publish the store to _memory_pool_obj because 131 // it does a release first. 132 OrderAccess::release_store_ptr(&_memory_pool_obj, pool_obj); 133 } 134 } 135 136 return pool_obj; 137 } 138 139 inline static size_t get_max_value(size_t val1, size_t val2) { 140 return (val1 > val2 ? val1 : val2); 141 } 142 143 void MemoryPool::record_peak_memory_usage() { 144 // Caller in JDK is responsible for synchronization - 145 // acquire the lock for this memory pool before calling VM 146 MemoryUsage usage = get_memory_usage(); 147 size_t peak_used = get_max_value(usage.used(), _peak_usage.used()); 148 size_t peak_committed = get_max_value(usage.committed(), _peak_usage.committed()); 149 size_t peak_max_size = get_max_value(usage.max_size(), _peak_usage.max_size()); 150 151 _peak_usage = MemoryUsage(initial_size(), peak_used, peak_committed, peak_max_size); 152 } 153 154 static void set_sensor_obj_at(SensorInfo** sensor_ptr, instanceHandle sh) { 155 assert(*sensor_ptr == NULL, "Should be called only once"); 156 SensorInfo* sensor = new SensorInfo(); 157 sensor->set_sensor(sh()); 158 *sensor_ptr = sensor; 159 } 160 161 void MemoryPool::set_usage_sensor_obj(instanceHandle sh) { 162 set_sensor_obj_at(&_usage_sensor, sh); 163 } 164 165 void MemoryPool::set_gc_usage_sensor_obj(instanceHandle sh) { 166 set_sensor_obj_at(&_gc_usage_sensor, sh); 167 } 168 169 void MemoryPool::oops_do(OopClosure* f) { 170 f->do_oop((oop*) &_memory_pool_obj); 171 if (_usage_sensor != NULL) { 172 _usage_sensor->oops_do(f); 173 } 174 if (_gc_usage_sensor != NULL) { 175 _gc_usage_sensor->oops_do(f); 176 } 177 } 178 179 ContiguousSpacePool::ContiguousSpacePool(ContiguousSpace* space, 180 const char* name, 181 PoolType type, 182 size_t max_size, 183 bool support_usage_threshold) : 184 CollectedMemoryPool(name, type, space->capacity(), max_size, 185 support_usage_threshold), _space(space) { 186 } 187 188 MemoryUsage ContiguousSpacePool::get_memory_usage() { 189 size_t maxSize = (available_for_allocation() ? max_size() : 0); 190 size_t used = used_in_bytes(); 191 size_t committed = _space->capacity(); 192 193 return MemoryUsage(initial_size(), used, committed, maxSize); 194 } 195 196 SurvivorContiguousSpacePool::SurvivorContiguousSpacePool(DefNewGeneration* gen, 197 const char* name, 198 PoolType type, 199 size_t max_size, 200 bool support_usage_threshold) : 201 CollectedMemoryPool(name, type, gen->from()->capacity(), max_size, 202 support_usage_threshold), _gen(gen) { 203 } 204 205 MemoryUsage SurvivorContiguousSpacePool::get_memory_usage() { 206 size_t maxSize = (available_for_allocation() ? max_size() : 0); 207 size_t used = used_in_bytes(); 208 size_t committed = committed_in_bytes(); 209 210 return MemoryUsage(initial_size(), used, committed, maxSize); 211 } 212 213 #if INCLUDE_ALL_GCS 214 CompactibleFreeListSpacePool::CompactibleFreeListSpacePool(CompactibleFreeListSpace* space, 215 const char* name, 216 PoolType type, 217 size_t max_size, 218 bool support_usage_threshold) : 219 CollectedMemoryPool(name, type, space->capacity(), max_size, 220 support_usage_threshold), _space(space) { 221 } 222 223 MemoryUsage CompactibleFreeListSpacePool::get_memory_usage() { 224 size_t maxSize = (available_for_allocation() ? max_size() : 0); 225 size_t used = used_in_bytes(); 226 size_t committed = _space->capacity(); 227 228 return MemoryUsage(initial_size(), used, committed, maxSize); 229 } 230 #endif // INCLUDE_ALL_GCS 231 232 GenerationPool::GenerationPool(Generation* gen, 233 const char* name, 234 PoolType type, 235 bool support_usage_threshold) : 236 CollectedMemoryPool(name, type, gen->capacity(), gen->max_capacity(), 237 support_usage_threshold), _gen(gen) { 238 } 239 240 MemoryUsage GenerationPool::get_memory_usage() { 241 size_t used = used_in_bytes(); 242 size_t committed = _gen->capacity(); 243 size_t maxSize = (available_for_allocation() ? max_size() : 0); 244 245 return MemoryUsage(initial_size(), used, committed, maxSize); 246 } 247 248 CodeHeapPool::CodeHeapPool(CodeHeap* codeHeap, const char* name, bool support_usage_threshold) : 249 MemoryPool(name, NonHeap, codeHeap->capacity(), codeHeap->max_capacity(), 250 support_usage_threshold, false), _codeHeap(codeHeap) { 251 } 252 253 MemoryUsage CodeHeapPool::get_memory_usage() { 254 size_t used = used_in_bytes(); 255 size_t committed = _codeHeap->capacity(); 256 size_t maxSize = (available_for_allocation() ? max_size() : 0); 257 258 return MemoryUsage(initial_size(), used, committed, maxSize); 259 } 260 261 MetaspacePoolBase::MetaspacePoolBase(const char *name, 262 Metaspace::MetadataType md_type, 263 size_t max_size) : 264 _md_type(md_type), 265 MemoryPool(name, NonHeap, MetaspaceAux::capacity_in_bytes(_md_type), max_size, 266 true, false) { 267 } 268 269 size_t MetaspacePoolBase::used_in_bytes() { 270 return MetaspaceAux::used_in_bytes(_md_type); 271 } 272 273 MemoryUsage MetaspacePoolBase::get_memory_usage() { 274 size_t used = MetaspaceAux::used_in_bytes(_md_type); 275 size_t committed = MetaspaceAux::capacity_in_bytes(_md_type); 276 return MemoryUsage(initial_size(), used, committed, max_size()); 277 } 278 279 ClassMetaspacePool::ClassMetaspacePool() : 280 MetaspacePoolBase("Class Metaspace", Metaspace::ClassType, calculate_max_size()) { 281 } 282 283 size_t ClassMetaspacePool::calculate_max_size() { 284 return UseCompressedKlassPointers ? ClassMetaspaceSize : _undefined_max_size; 285 } 286 287 MetaspacePool::MetaspacePool() : 288 MetaspacePoolBase("Metaspace", Metaspace::NonClassType, calculate_max_size()) { 289 } 290 291 size_t MetaspacePool::calculate_max_size() { 292 return FLAG_IS_CMDLINE(MaxMetaspaceSize) ? MaxMetaspaceSize : _undefined_max_size; 293 }