Sun, 13 Apr 2008 17:43:42 -0400
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
Summary: Compressed oops in instances, arrays, and headers. Code contributors are coleenp, phh, never, swamyv
Reviewed-by: jmasa, kamg, acorn, tbell, kvn, rasbold
1 /*
2 * Copyright 2001-2007 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 // Inline allocation implementations.
27 void CollectedHeap::post_allocation_setup_common(KlassHandle klass,
28 HeapWord* obj,
29 size_t size) {
30 post_allocation_setup_no_klass_install(klass, obj, size);
31 post_allocation_install_obj_klass(klass, oop(obj), (int) size);
32 }
34 void CollectedHeap::post_allocation_setup_no_klass_install(KlassHandle klass,
35 HeapWord* objPtr,
36 size_t size) {
38 oop obj = (oop)objPtr;
40 assert(obj != NULL, "NULL object pointer");
41 if (UseBiasedLocking && (klass() != NULL)) {
42 obj->set_mark(klass->prototype_header());
43 } else {
44 // May be bootstrapping
45 obj->set_mark(markOopDesc::prototype());
46 }
48 // support low memory notifications (no-op if not enabled)
49 LowMemoryDetector::detect_low_memory_for_collected_pools();
50 }
52 void CollectedHeap::post_allocation_install_obj_klass(KlassHandle klass,
53 oop obj,
54 int size) {
55 // These asserts are kind of complicated because of klassKlass
56 // and the beginning of the world.
57 assert(klass() != NULL || !Universe::is_fully_initialized(), "NULL klass");
58 assert(klass() == NULL || klass()->is_klass(), "not a klass");
59 assert(klass() == NULL || klass()->klass_part() != NULL, "not a klass");
60 assert(obj != NULL, "NULL object pointer");
61 obj->set_klass(klass());
62 assert(!Universe::is_fully_initialized() || obj->blueprint() != NULL,
63 "missing blueprint");
64 }
66 // Support for jvmti and dtrace
67 inline void post_allocation_notify(KlassHandle klass, oop obj) {
68 // support for JVMTI VMObjectAlloc event (no-op if not enabled)
69 JvmtiExport::vm_object_alloc_event_collector(obj);
71 if (DTraceAllocProbes) {
72 // support for Dtrace object alloc event (no-op most of the time)
73 if (klass() != NULL && klass()->klass_part()->name() != NULL) {
74 SharedRuntime::dtrace_object_alloc(obj);
75 }
76 }
77 }
79 void CollectedHeap::post_allocation_setup_obj(KlassHandle klass,
80 HeapWord* obj,
81 size_t size) {
82 post_allocation_setup_common(klass, obj, size);
83 assert(Universe::is_bootstrapping() ||
84 !((oop)obj)->blueprint()->oop_is_array(), "must not be an array");
85 // notify jvmti and dtrace
86 post_allocation_notify(klass, (oop)obj);
87 }
89 void CollectedHeap::post_allocation_setup_array(KlassHandle klass,
90 HeapWord* obj,
91 size_t size,
92 int length) {
93 assert(length >= 0, "length should be non-negative");
94 post_allocation_setup_common(klass, obj, size);
95 // Must set length after installing klass as set_klass zeros the length
96 // field in UseCompressedOops
97 ((arrayOop)obj)->set_length(length);
98 assert(((oop)obj)->blueprint()->oop_is_array(), "must be an array");
99 // notify jvmti and dtrace (must be after length is set for dtrace)
100 post_allocation_notify(klass, (oop)obj);
101 }
103 HeapWord* CollectedHeap::common_mem_allocate_noinit(size_t size, bool is_noref, TRAPS) {
105 // Clear unhandled oops for memory allocation. Memory allocation might
106 // not take out a lock if from tlab, so clear here.
107 CHECK_UNHANDLED_OOPS_ONLY(THREAD->clear_unhandled_oops();)
109 if (HAS_PENDING_EXCEPTION) {
110 NOT_PRODUCT(guarantee(false, "Should not allocate with exception pending"));
111 return NULL; // caller does a CHECK_0 too
112 }
114 // We may want to update this, is_noref objects might not be allocated in TLABs.
115 HeapWord* result = NULL;
116 if (UseTLAB) {
117 result = CollectedHeap::allocate_from_tlab(THREAD, size);
118 if (result != NULL) {
119 assert(!HAS_PENDING_EXCEPTION,
120 "Unexpected exception, will result in uninitialized storage");
121 return result;
122 }
123 }
124 bool gc_overhead_limit_was_exceeded;
125 result = Universe::heap()->mem_allocate(size,
126 is_noref,
127 false,
128 &gc_overhead_limit_was_exceeded);
129 if (result != NULL) {
130 NOT_PRODUCT(Universe::heap()->
131 check_for_non_bad_heap_word_value(result, size));
132 assert(!HAS_PENDING_EXCEPTION,
133 "Unexpected exception, will result in uninitialized storage");
134 return result;
135 }
138 if (!gc_overhead_limit_was_exceeded) {
139 // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
140 report_java_out_of_memory("Java heap space");
142 if (JvmtiExport::should_post_resource_exhausted()) {
143 JvmtiExport::post_resource_exhausted(
144 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
145 "Java heap space");
146 }
148 THROW_OOP_0(Universe::out_of_memory_error_java_heap());
149 } else {
150 // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
151 report_java_out_of_memory("GC overhead limit exceeded");
153 if (JvmtiExport::should_post_resource_exhausted()) {
154 JvmtiExport::post_resource_exhausted(
155 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
156 "GC overhead limit exceeded");
157 }
159 THROW_OOP_0(Universe::out_of_memory_error_gc_overhead_limit());
160 }
161 }
163 HeapWord* CollectedHeap::common_mem_allocate_init(size_t size, bool is_noref, TRAPS) {
164 HeapWord* obj = common_mem_allocate_noinit(size, is_noref, CHECK_NULL);
165 init_obj(obj, size);
166 return obj;
167 }
169 // Need to investigate, do we really want to throw OOM exception here?
170 HeapWord* CollectedHeap::common_permanent_mem_allocate_noinit(size_t size, TRAPS) {
171 if (HAS_PENDING_EXCEPTION) {
172 NOT_PRODUCT(guarantee(false, "Should not allocate with exception pending"));
173 return NULL; // caller does a CHECK_NULL too
174 }
176 #ifdef ASSERT
177 if (CIFireOOMAt > 0 && THREAD->is_Compiler_thread() &&
178 ++_fire_out_of_memory_count >= CIFireOOMAt) {
179 // For testing of OOM handling in the CI throw an OOM and see how
180 // it does. Historically improper handling of these has resulted
181 // in crashes which we really don't want to have in the CI.
182 THROW_OOP_0(Universe::out_of_memory_error_perm_gen());
183 }
184 #endif
186 HeapWord* result = Universe::heap()->permanent_mem_allocate(size);
187 if (result != NULL) {
188 NOT_PRODUCT(Universe::heap()->
189 check_for_non_bad_heap_word_value(result, size));
190 assert(!HAS_PENDING_EXCEPTION,
191 "Unexpected exception, will result in uninitialized storage");
192 return result;
193 }
194 // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
195 report_java_out_of_memory("PermGen space");
197 if (JvmtiExport::should_post_resource_exhausted()) {
198 JvmtiExport::post_resource_exhausted(
199 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,
200 "PermGen space");
201 }
203 THROW_OOP_0(Universe::out_of_memory_error_perm_gen());
204 }
206 HeapWord* CollectedHeap::common_permanent_mem_allocate_init(size_t size, TRAPS) {
207 HeapWord* obj = common_permanent_mem_allocate_noinit(size, CHECK_NULL);
208 init_obj(obj, size);
209 return obj;
210 }
212 HeapWord* CollectedHeap::allocate_from_tlab(Thread* thread, size_t size) {
213 assert(UseTLAB, "should use UseTLAB");
215 HeapWord* obj = thread->tlab().allocate(size);
216 if (obj != NULL) {
217 return obj;
218 }
219 // Otherwise...
220 return allocate_from_tlab_slow(thread, size);
221 }
223 void CollectedHeap::init_obj(HeapWord* obj, size_t size) {
224 assert(obj != NULL, "cannot initialize NULL object");
225 const size_t hs = oopDesc::header_size();
226 assert(size >= hs, "unexpected object size");
227 Copy::fill_to_aligned_words(obj + hs, size - hs);
228 }
230 oop CollectedHeap::obj_allocate(KlassHandle klass, int size, TRAPS) {
231 debug_only(check_for_valid_allocation_state());
232 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
233 assert(size >= 0, "int won't convert to size_t");
234 HeapWord* obj = common_mem_allocate_init(size, false, CHECK_NULL);
235 post_allocation_setup_obj(klass, obj, size);
236 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
237 return (oop)obj;
238 }
240 oop CollectedHeap::array_allocate(KlassHandle klass,
241 int size,
242 int length,
243 TRAPS) {
244 debug_only(check_for_valid_allocation_state());
245 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
246 assert(size >= 0, "int won't convert to size_t");
247 HeapWord* obj = common_mem_allocate_init(size, false, CHECK_NULL);
248 post_allocation_setup_array(klass, obj, size, length);
249 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
250 return (oop)obj;
251 }
253 oop CollectedHeap::large_typearray_allocate(KlassHandle klass,
254 int size,
255 int length,
256 TRAPS) {
257 debug_only(check_for_valid_allocation_state());
258 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
259 assert(size >= 0, "int won't convert to size_t");
260 HeapWord* obj = common_mem_allocate_init(size, true, CHECK_NULL);
261 post_allocation_setup_array(klass, obj, size, length);
262 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
263 return (oop)obj;
264 }
266 oop CollectedHeap::permanent_obj_allocate(KlassHandle klass, int size, TRAPS) {
267 oop obj = permanent_obj_allocate_no_klass_install(klass, size, CHECK_NULL);
268 post_allocation_install_obj_klass(klass, obj, size);
269 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value((HeapWord*) obj,
270 size));
271 return obj;
272 }
274 oop CollectedHeap::permanent_obj_allocate_no_klass_install(KlassHandle klass,
275 int size,
276 TRAPS) {
277 debug_only(check_for_valid_allocation_state());
278 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
279 assert(size >= 0, "int won't convert to size_t");
280 HeapWord* obj = common_permanent_mem_allocate_init(size, CHECK_NULL);
281 post_allocation_setup_no_klass_install(klass, obj, size);
282 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
283 return (oop)obj;
284 }
286 oop CollectedHeap::permanent_array_allocate(KlassHandle klass,
287 int size,
288 int length,
289 TRAPS) {
290 debug_only(check_for_valid_allocation_state());
291 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
292 assert(size >= 0, "int won't convert to size_t");
293 HeapWord* obj = common_permanent_mem_allocate_init(size, CHECK_NULL);
294 post_allocation_setup_array(klass, obj, size, length);
295 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
296 return (oop)obj;
297 }
299 // Returns "TRUE" if "p" is a method oop in the
300 // current heap with high probability. NOTE: The main
301 // current consumers of this interface are Forte::
302 // and ThreadProfiler::. In these cases, the
303 // interpreter frame from which "p" came, may be
304 // under construction when sampled asynchronously, so
305 // the clients want to check that it represents a
306 // valid method before using it. Nonetheless since
307 // the clients do not typically lock out GC, the
308 // predicate is_valid_method() is not stable, so
309 // it is possible that by the time "p" is used, it
310 // is no longer valid.
311 inline bool CollectedHeap::is_valid_method(oop p) const {
312 return
313 p != NULL &&
315 // Check whether it is aligned at a HeapWord boundary.
316 Space::is_aligned(p) &&
318 // Check whether "method" is in the allocated part of the
319 // permanent generation -- this needs to be checked before
320 // p->klass() below to avoid a SEGV (but see below
321 // for a potential window of vulnerability).
322 is_permanent((void*)p) &&
324 // See if GC is active; however, there is still an
325 // apparently unavoidable window after this call
326 // and before the client of this interface uses "p".
327 // If the client chooses not to lock out GC, then
328 // it's a risk the client must accept.
329 !is_gc_active() &&
331 // Check that p is a methodOop.
332 p->klass() == Universe::methodKlassObj();
333 }
336 #ifndef PRODUCT
338 inline bool
339 CollectedHeap::promotion_should_fail(volatile size_t* count) {
340 // Access to count is not atomic; the value does not have to be exact.
341 if (PromotionFailureALot) {
342 const size_t gc_num = total_collections();
343 const size_t elapsed_gcs = gc_num - _promotion_failure_alot_gc_number;
344 if (elapsed_gcs >= PromotionFailureALotInterval) {
345 // Test for unsigned arithmetic wrap-around.
346 if (++*count >= PromotionFailureALotCount) {
347 *count = 0;
348 return true;
349 }
350 }
351 }
352 return false;
353 }
355 inline bool CollectedHeap::promotion_should_fail() {
356 return promotion_should_fail(&_promotion_failure_alot_count);
357 }
359 inline void CollectedHeap::reset_promotion_should_fail(volatile size_t* count) {
360 if (PromotionFailureALot) {
361 _promotion_failure_alot_gc_number = total_collections();
362 *count = 0;
363 }
364 }
366 inline void CollectedHeap::reset_promotion_should_fail() {
367 reset_promotion_should_fail(&_promotion_failure_alot_count);
368 }
369 #endif // #ifndef PRODUCT