memoryPool.cpp revision 1472:c18cbe5936b8
1284345Ssjg/*
2284345Ssjg * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
3284345Ssjg * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4284345Ssjg *
5284345Ssjg * This code is free software; you can redistribute it and/or modify it
6284345Ssjg * under the terms of the GNU General Public License version 2 only, as
7284345Ssjg * published by the Free Software Foundation.
8284345Ssjg *
9284345Ssjg * This code is distributed in the hope that it will be useful, but WITHOUT
10284345Ssjg * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11284345Ssjg * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12284345Ssjg * version 2 for more details (a copy is included in the LICENSE file that
13284345Ssjg * accompanied this code).
14284345Ssjg *
15284345Ssjg * You should have received a copy of the GNU General Public License version
16284345Ssjg * 2 along with this work; if not, write to the Free Software Foundation,
17284345Ssjg * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18284345Ssjg *
19284345Ssjg * 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 "incls/_precompiled.incl"
26# include "incls/_memoryPool.cpp.incl"
27
28MemoryPool::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;
41
42  // initialize the max and init size of collection usage
43  _after_gc_usage = MemoryUsage(_initial_size, 0, 0, _max_size);
44
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}
52
53void 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}
60
61
62// Returns an instanceHandle of a MemoryPool object.
63// It creates a MemoryPool instance when the first time
64// this function is called.
65instanceOop 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);
74
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);
78
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
83
84    symbolHandle method_name = vmSymbolHandles::createMemoryPool_name();
85    symbolHandle signature = vmSymbolHandles::createMemoryPool_signature();
86
87    args.push_long(usage_threshold_value);    // Argument 3
88    args.push_long(gc_usage_threshold_value); // Argument 4
89
90    JavaCalls::call_static(&result,
91                           ik,
92                           method_name,
93                           signature,
94                           &args,
95                           CHECK_NULL);
96
97    instanceOop p = (instanceOop) result.get_jobject();
98    instanceHandle pool(THREAD, p);
99
100    {
101      // Get lock since another thread may have create the instance
102      MutexLocker ml(Management_lock);
103
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      }
114
115      // Get the address of the object we created via call_special.
116      pool_obj = pool();
117
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  }
125
126  return pool_obj;
127}
128
129inline static size_t get_max_value(size_t val1, size_t val2) {
130    return (val1 > val2 ? val1 : val2);
131}
132
133void 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());
140
141  _peak_usage = MemoryUsage(initial_size(), peak_used, peak_committed, peak_max_size);
142}
143
144static 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}
150
151void MemoryPool::set_usage_sensor_obj(instanceHandle sh) {
152  set_sensor_obj_at(&_usage_sensor, sh);
153}
154
155void MemoryPool::set_gc_usage_sensor_obj(instanceHandle sh) {
156  set_sensor_obj_at(&_gc_usage_sensor, sh);
157}
158
159void 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}
168
169ContiguousSpacePool::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}
177
178MemoryUsage 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();
182
183  return MemoryUsage(initial_size(), used, committed, maxSize);
184}
185
186SurvivorContiguousSpacePool::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}
194
195MemoryUsage 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();
199
200  return MemoryUsage(initial_size(), used, committed, maxSize);
201}
202
203#ifndef SERIALGC
204CompactibleFreeListSpacePool::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}
212
213MemoryUsage 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();
217
218  return MemoryUsage(initial_size(), used, committed, maxSize);
219}
220#endif // SERIALGC
221
222GenerationPool::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}
229
230MemoryUsage 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);
234
235  return MemoryUsage(initial_size(), used, committed, maxSize);
236}
237
238CodeHeapPool::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}
242
243MemoryUsage 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);
247
248  return MemoryUsage(initial_size(), used, committed, maxSize);
249}
250