src/share/vm/services/memoryPool.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial