blob: 1510cd596b4af0579e4797ddb8da628ba4437dd7 [file] [log] [blame]
J. Duke81537792007-12-01 00:00:00 +00001/*
Lois Foltan7bf953d2013-09-26 10:25:02 -04002 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
J. Duke81537792007-12-01 00:00:00 +00003 * 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 *
Erik Trimbleba7c1732010-05-27 19:08:38 -070019 * 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.
J. Duke81537792007-12-01 00:00:00 +000022 *
23 */
24
Stefan Karlsson8006fe82010-11-23 13:22:55 -080025#include "precompiled.hpp"
26#include "classfile/systemDictionary.hpp"
27#include "classfile/vmSymbols.hpp"
Erik Helin604a75f2013-06-26 16:58:37 +020028#include "memory/metaspace.hpp"
Stefan Karlsson8006fe82010-11-23 13:22:55 -080029#include "oops/oop.inline.hpp"
30#include "runtime/handles.inline.hpp"
31#include "runtime/javaCalls.hpp"
Goetz Lindenmaier6e6f0722014-04-29 15:17:27 +020032#include "runtime/orderAccess.inline.hpp"
Stefan Karlsson8006fe82010-11-23 13:22:55 -080033#include "services/lowMemoryDetector.hpp"
34#include "services/management.hpp"
35#include "services/memoryManager.hpp"
36#include "services/memoryPool.hpp"
Joseph Provino698fba92013-01-23 13:02:39 -050037#include "utilities/macros.hpp"
Erik Helin604a75f2013-06-26 16:58:37 +020038#include "utilities/globalDefinitions.hpp"
J. Duke81537792007-12-01 00:00:00 +000039
40MemoryPool::MemoryPool(const char* name,
41 PoolType type,
42 size_t init_size,
43 size_t max_size,
44 bool support_usage_threshold,
45 bool support_gc_threshold) {
46 _name = name;
47 _initial_size = init_size;
48 _max_size = max_size;
Lois Foltan7bf953d2013-09-26 10:25:02 -040049 (void)const_cast<instanceOop&>(_memory_pool_obj = NULL);
J. Duke81537792007-12-01 00:00:00 +000050 _available_for_allocation = true;
51 _num_managers = 0;
52 _type = type;
53
54 // initialize the max and init size of collection usage
55 _after_gc_usage = MemoryUsage(_initial_size, 0, 0, _max_size);
56
57 _usage_sensor = NULL;
58 _gc_usage_sensor = NULL;
59 // usage threshold supports both high and low threshold
60 _usage_threshold = new ThresholdSupport(support_usage_threshold, support_usage_threshold);
61 // gc usage threshold supports only high threshold
62 _gc_usage_threshold = new ThresholdSupport(support_gc_threshold, support_gc_threshold);
63}
64
65void MemoryPool::add_manager(MemoryManager* mgr) {
66 assert(_num_managers < MemoryPool::max_num_managers, "_num_managers exceeds the max");
67 if (_num_managers < MemoryPool::max_num_managers) {
68 _managers[_num_managers] = mgr;
69 _num_managers++;
70 }
71}
72
73
74// Returns an instanceHandle of a MemoryPool object.
75// It creates a MemoryPool instance when the first time
76// this function is called.
77instanceOop MemoryPool::get_memory_pool_instance(TRAPS) {
78 // Must do an acquire so as to force ordering of subsequent
79 // loads from anything _memory_pool_obj points to or implies.
80 instanceOop pool_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_pool_obj);
81 if (pool_obj == NULL) {
82 // It's ok for more than one thread to execute the code up to the locked region.
83 // Extra pool instances will just be gc'ed.
Jon Masamitsu5c58d272012-09-01 13:25:18 -040084 Klass* k = Management::sun_management_ManagementFactory_klass(CHECK_NULL);
J. Duke81537792007-12-01 00:00:00 +000085 instanceKlassHandle ik(THREAD, k);
86
87 Handle pool_name = java_lang_String::create_from_str(_name, CHECK_NULL);
88 jlong usage_threshold_value = (_usage_threshold->is_high_threshold_supported() ? 0 : -1L);
89 jlong gc_usage_threshold_value = (_gc_usage_threshold->is_high_threshold_supported() ? 0 : -1L);
90
91 JavaValue result(T_OBJECT);
92 JavaCallArguments args;
93 args.push_oop(pool_name); // Argument 1
94 args.push_int((int) is_heap()); // Argument 2
95
Coleen Phillimore7b4f8072011-01-27 16:11:27 -080096 Symbol* method_name = vmSymbols::createMemoryPool_name();
97 Symbol* signature = vmSymbols::createMemoryPool_signature();
J. Duke81537792007-12-01 00:00:00 +000098
99 args.push_long(usage_threshold_value); // Argument 3
100 args.push_long(gc_usage_threshold_value); // Argument 4
101
102 JavaCalls::call_static(&result,
103 ik,
104 method_name,
105 signature,
106 &args,
107 CHECK_NULL);
108
109 instanceOop p = (instanceOop) result.get_jobject();
110 instanceHandle pool(THREAD, p);
111
112 {
113 // Get lock since another thread may have create the instance
114 MutexLocker ml(Management_lock);
115
116 // Check if another thread has created the pool. We reload
117 // _memory_pool_obj here because some other thread may have
118 // initialized it while we were executing the code before the lock.
119 //
120 // The lock has done an acquire, so the load can't float above it,
121 // but we need to do a load_acquire as above.
122 pool_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_pool_obj);
123 if (pool_obj != NULL) {
124 return pool_obj;
125 }
126
127 // Get the address of the object we created via call_special.
128 pool_obj = pool();
129
130 // Use store barrier to make sure the memory accesses associated
131 // with creating the pool are visible before publishing its address.
132 // The unlock will publish the store to _memory_pool_obj because
133 // it does a release first.
134 OrderAccess::release_store_ptr(&_memory_pool_obj, pool_obj);
135 }
136 }
137
138 return pool_obj;
139}
140
141inline static size_t get_max_value(size_t val1, size_t val2) {
142 return (val1 > val2 ? val1 : val2);
143}
144
145void MemoryPool::record_peak_memory_usage() {
146 // Caller in JDK is responsible for synchronization -
147 // acquire the lock for this memory pool before calling VM
148 MemoryUsage usage = get_memory_usage();
149 size_t peak_used = get_max_value(usage.used(), _peak_usage.used());
150 size_t peak_committed = get_max_value(usage.committed(), _peak_usage.committed());
151 size_t peak_max_size = get_max_value(usage.max_size(), _peak_usage.max_size());
152
153 _peak_usage = MemoryUsage(initial_size(), peak_used, peak_committed, peak_max_size);
154}
155
156static void set_sensor_obj_at(SensorInfo** sensor_ptr, instanceHandle sh) {
157 assert(*sensor_ptr == NULL, "Should be called only once");
158 SensorInfo* sensor = new SensorInfo();
159 sensor->set_sensor(sh());
160 *sensor_ptr = sensor;
161}
162
163void MemoryPool::set_usage_sensor_obj(instanceHandle sh) {
164 set_sensor_obj_at(&_usage_sensor, sh);
165}
166
167void MemoryPool::set_gc_usage_sensor_obj(instanceHandle sh) {
168 set_sensor_obj_at(&_gc_usage_sensor, sh);
169}
170
171void MemoryPool::oops_do(OopClosure* f) {
172 f->do_oop((oop*) &_memory_pool_obj);
173 if (_usage_sensor != NULL) {
174 _usage_sensor->oops_do(f);
175 }
176 if (_gc_usage_sensor != NULL) {
177 _gc_usage_sensor->oops_do(f);
178 }
179}
180
181ContiguousSpacePool::ContiguousSpacePool(ContiguousSpace* space,
182 const char* name,
183 PoolType type,
184 size_t max_size,
185 bool support_usage_threshold) :
186 CollectedMemoryPool(name, type, space->capacity(), max_size,
187 support_usage_threshold), _space(space) {
188}
189
190MemoryUsage ContiguousSpacePool::get_memory_usage() {
191 size_t maxSize = (available_for_allocation() ? max_size() : 0);
192 size_t used = used_in_bytes();
193 size_t committed = _space->capacity();
194
195 return MemoryUsage(initial_size(), used, committed, maxSize);
196}
197
198SurvivorContiguousSpacePool::SurvivorContiguousSpacePool(DefNewGeneration* gen,
199 const char* name,
200 PoolType type,
201 size_t max_size,
202 bool support_usage_threshold) :
203 CollectedMemoryPool(name, type, gen->from()->capacity(), max_size,
204 support_usage_threshold), _gen(gen) {
205}
206
207MemoryUsage SurvivorContiguousSpacePool::get_memory_usage() {
208 size_t maxSize = (available_for_allocation() ? max_size() : 0);
209 size_t used = used_in_bytes();
210 size_t committed = committed_in_bytes();
211
212 return MemoryUsage(initial_size(), used, committed, maxSize);
213}
214
Joseph Provino698fba92013-01-23 13:02:39 -0500215#if INCLUDE_ALL_GCS
J. Duke81537792007-12-01 00:00:00 +0000216CompactibleFreeListSpacePool::CompactibleFreeListSpacePool(CompactibleFreeListSpace* space,
217 const char* name,
218 PoolType type,
219 size_t max_size,
220 bool support_usage_threshold) :
221 CollectedMemoryPool(name, type, space->capacity(), max_size,
222 support_usage_threshold), _space(space) {
223}
224
225MemoryUsage CompactibleFreeListSpacePool::get_memory_usage() {
226 size_t maxSize = (available_for_allocation() ? max_size() : 0);
227 size_t used = used_in_bytes();
228 size_t committed = _space->capacity();
229
230 return MemoryUsage(initial_size(), used, committed, maxSize);
231}
Joseph Provino698fba92013-01-23 13:02:39 -0500232#endif // INCLUDE_ALL_GCS
J. Duke81537792007-12-01 00:00:00 +0000233
234GenerationPool::GenerationPool(Generation* gen,
235 const char* name,
236 PoolType type,
237 bool support_usage_threshold) :
238 CollectedMemoryPool(name, type, gen->capacity(), gen->max_capacity(),
239 support_usage_threshold), _gen(gen) {
240}
241
242MemoryUsage GenerationPool::get_memory_usage() {
243 size_t used = used_in_bytes();
244 size_t committed = _gen->capacity();
245 size_t maxSize = (available_for_allocation() ? max_size() : 0);
246
247 return MemoryUsage(initial_size(), used, committed, maxSize);
248}
249
250CodeHeapPool::CodeHeapPool(CodeHeap* codeHeap, const char* name, bool support_usage_threshold) :
251 MemoryPool(name, NonHeap, codeHeap->capacity(), codeHeap->max_capacity(),
252 support_usage_threshold, false), _codeHeap(codeHeap) {
253}
254
255MemoryUsage CodeHeapPool::get_memory_usage() {
256 size_t used = used_in_bytes();
257 size_t committed = _codeHeap->capacity();
258 size_t maxSize = (available_for_allocation() ? max_size() : 0);
259
260 return MemoryUsage(initial_size(), used, committed, maxSize);
261}
Erik Helin604a75f2013-06-26 16:58:37 +0200262
263MetaspacePool::MetaspacePool() :
Erik Helin2cab7ea2013-09-17 20:59:07 +0200264 MemoryPool("Metaspace", NonHeap, 0, calculate_max_size(), true, false) { }
Erik Helin604a75f2013-06-26 16:58:37 +0200265
266MemoryUsage MetaspacePool::get_memory_usage() {
Erik Helin2cab7ea2013-09-17 20:59:07 +0200267 size_t committed = MetaspaceAux::committed_bytes();
Erik Helin604a75f2013-06-26 16:58:37 +0200268 return MemoryUsage(initial_size(), used_in_bytes(), committed, max_size());
269}
270
271size_t MetaspacePool::used_in_bytes() {
Erik Helin977d1f72014-03-31 17:09:38 +0200272 return MetaspaceAux::used_bytes();
Erik Helin604a75f2013-06-26 16:58:37 +0200273}
274
Erik Helin604a75f2013-06-26 16:58:37 +0200275size_t MetaspacePool::calculate_max_size() const {
Erik Helin2cab7ea2013-09-17 20:59:07 +0200276 return FLAG_IS_CMDLINE(MaxMetaspaceSize) ? MaxMetaspaceSize :
277 MemoryUsage::undefined_size();
Erik Helin604a75f2013-06-26 16:58:37 +0200278}
279
280CompressedKlassSpacePool::CompressedKlassSpacePool() :
Erik Helin2cab7ea2013-09-17 20:59:07 +0200281 MemoryPool("Compressed Class Space", NonHeap, 0, CompressedClassSpaceSize, true, false) { }
Erik Helin604a75f2013-06-26 16:58:37 +0200282
283size_t CompressedKlassSpacePool::used_in_bytes() {
Erik Helin977d1f72014-03-31 17:09:38 +0200284 return MetaspaceAux::used_bytes(Metaspace::ClassType);
Erik Helin604a75f2013-06-26 16:58:37 +0200285}
286
Erik Helin604a75f2013-06-26 16:58:37 +0200287MemoryUsage CompressedKlassSpacePool::get_memory_usage() {
Erik Helin2cab7ea2013-09-17 20:59:07 +0200288 size_t committed = MetaspaceAux::committed_bytes(Metaspace::ClassType);
Erik Helin604a75f2013-06-26 16:58:37 +0200289 return MemoryUsage(initial_size(), used_in_bytes(), committed, max_size());
290}