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