src/share/vm/services/memoryPool.cpp

Thu, 05 Jun 2008 15:57:56 -0700

author
ysr
date
Thu, 05 Jun 2008 15:57:56 -0700
changeset 777
37f87013dfd8
parent 435
a61af66fc99e
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6711316: Open source the Garbage-First garbage collector
Summary: First mercurial integration of the code for the Garbage-First garbage collector.
Reviewed-by: apetrusenko, iveresov, jmasa, sgoldman, tonyp, ysr

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

mercurial