src/share/vm/services/memoryPool.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 9448
73d689add964
permissions
-rw-r--r--

Merge

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

mercurial