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