src/share/vm/gc_interface/collectedHeap.inline.hpp

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

author
ysr
date
Thu, 05 Jun 2008 15:57:56 -0700
changeset 777
37f87013dfd8
parent 548
ba764ed4b6f2
child 779
6aae2f9d0294
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

duke@435 1 /*
duke@435 2 * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 // Inline allocation implementations.
duke@435 26
duke@435 27 void CollectedHeap::post_allocation_setup_common(KlassHandle klass,
duke@435 28 HeapWord* obj,
duke@435 29 size_t size) {
duke@435 30 post_allocation_setup_no_klass_install(klass, obj, size);
duke@435 31 post_allocation_install_obj_klass(klass, oop(obj), (int) size);
duke@435 32 }
duke@435 33
duke@435 34 void CollectedHeap::post_allocation_setup_no_klass_install(KlassHandle klass,
duke@435 35 HeapWord* objPtr,
duke@435 36 size_t size) {
duke@435 37
duke@435 38 oop obj = (oop)objPtr;
duke@435 39
duke@435 40 assert(obj != NULL, "NULL object pointer");
duke@435 41 if (UseBiasedLocking && (klass() != NULL)) {
duke@435 42 obj->set_mark(klass->prototype_header());
duke@435 43 } else {
duke@435 44 // May be bootstrapping
duke@435 45 obj->set_mark(markOopDesc::prototype());
duke@435 46 }
duke@435 47
duke@435 48 // support low memory notifications (no-op if not enabled)
duke@435 49 LowMemoryDetector::detect_low_memory_for_collected_pools();
duke@435 50 }
duke@435 51
duke@435 52 void CollectedHeap::post_allocation_install_obj_klass(KlassHandle klass,
duke@435 53 oop obj,
duke@435 54 int size) {
duke@435 55 // These asserts are kind of complicated because of klassKlass
duke@435 56 // and the beginning of the world.
duke@435 57 assert(klass() != NULL || !Universe::is_fully_initialized(), "NULL klass");
duke@435 58 assert(klass() == NULL || klass()->is_klass(), "not a klass");
duke@435 59 assert(klass() == NULL || klass()->klass_part() != NULL, "not a klass");
duke@435 60 assert(obj != NULL, "NULL object pointer");
duke@435 61 obj->set_klass(klass());
duke@435 62 assert(!Universe::is_fully_initialized() || obj->blueprint() != NULL,
duke@435 63 "missing blueprint");
coleenp@548 64 }
duke@435 65
coleenp@548 66 // Support for jvmti and dtrace
coleenp@548 67 inline void post_allocation_notify(KlassHandle klass, oop obj) {
duke@435 68 // support for JVMTI VMObjectAlloc event (no-op if not enabled)
duke@435 69 JvmtiExport::vm_object_alloc_event_collector(obj);
duke@435 70
duke@435 71 if (DTraceAllocProbes) {
duke@435 72 // support for Dtrace object alloc event (no-op most of the time)
duke@435 73 if (klass() != NULL && klass()->klass_part()->name() != NULL) {
duke@435 74 SharedRuntime::dtrace_object_alloc(obj);
duke@435 75 }
duke@435 76 }
duke@435 77 }
duke@435 78
duke@435 79 void CollectedHeap::post_allocation_setup_obj(KlassHandle klass,
duke@435 80 HeapWord* obj,
duke@435 81 size_t size) {
duke@435 82 post_allocation_setup_common(klass, obj, size);
duke@435 83 assert(Universe::is_bootstrapping() ||
duke@435 84 !((oop)obj)->blueprint()->oop_is_array(), "must not be an array");
coleenp@548 85 // notify jvmti and dtrace
coleenp@548 86 post_allocation_notify(klass, (oop)obj);
duke@435 87 }
duke@435 88
duke@435 89 void CollectedHeap::post_allocation_setup_array(KlassHandle klass,
duke@435 90 HeapWord* obj,
duke@435 91 size_t size,
duke@435 92 int length) {
duke@435 93 assert(length >= 0, "length should be non-negative");
coleenp@548 94 post_allocation_setup_common(klass, obj, size);
coleenp@548 95 // Must set length after installing klass as set_klass zeros the length
coleenp@548 96 // field in UseCompressedOops
duke@435 97 ((arrayOop)obj)->set_length(length);
duke@435 98 assert(((oop)obj)->blueprint()->oop_is_array(), "must be an array");
coleenp@548 99 // notify jvmti and dtrace (must be after length is set for dtrace)
coleenp@548 100 post_allocation_notify(klass, (oop)obj);
duke@435 101 }
duke@435 102
duke@435 103 HeapWord* CollectedHeap::common_mem_allocate_noinit(size_t size, bool is_noref, TRAPS) {
duke@435 104
duke@435 105 // Clear unhandled oops for memory allocation. Memory allocation might
duke@435 106 // not take out a lock if from tlab, so clear here.
duke@435 107 CHECK_UNHANDLED_OOPS_ONLY(THREAD->clear_unhandled_oops();)
duke@435 108
duke@435 109 if (HAS_PENDING_EXCEPTION) {
duke@435 110 NOT_PRODUCT(guarantee(false, "Should not allocate with exception pending"));
duke@435 111 return NULL; // caller does a CHECK_0 too
duke@435 112 }
duke@435 113
duke@435 114 // We may want to update this, is_noref objects might not be allocated in TLABs.
duke@435 115 HeapWord* result = NULL;
duke@435 116 if (UseTLAB) {
duke@435 117 result = CollectedHeap::allocate_from_tlab(THREAD, size);
duke@435 118 if (result != NULL) {
duke@435 119 assert(!HAS_PENDING_EXCEPTION,
duke@435 120 "Unexpected exception, will result in uninitialized storage");
duke@435 121 return result;
duke@435 122 }
duke@435 123 }
ysr@777 124 bool gc_overhead_limit_was_exceeded = false;
duke@435 125 result = Universe::heap()->mem_allocate(size,
duke@435 126 is_noref,
duke@435 127 false,
duke@435 128 &gc_overhead_limit_was_exceeded);
duke@435 129 if (result != NULL) {
duke@435 130 NOT_PRODUCT(Universe::heap()->
duke@435 131 check_for_non_bad_heap_word_value(result, size));
duke@435 132 assert(!HAS_PENDING_EXCEPTION,
duke@435 133 "Unexpected exception, will result in uninitialized storage");
duke@435 134 return result;
duke@435 135 }
duke@435 136
duke@435 137
duke@435 138 if (!gc_overhead_limit_was_exceeded) {
duke@435 139 // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
duke@435 140 report_java_out_of_memory("Java heap space");
duke@435 141
duke@435 142 if (JvmtiExport::should_post_resource_exhausted()) {
duke@435 143 JvmtiExport::post_resource_exhausted(
duke@435 144 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
duke@435 145 "Java heap space");
duke@435 146 }
duke@435 147
duke@435 148 THROW_OOP_0(Universe::out_of_memory_error_java_heap());
duke@435 149 } else {
duke@435 150 // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
duke@435 151 report_java_out_of_memory("GC overhead limit exceeded");
duke@435 152
duke@435 153 if (JvmtiExport::should_post_resource_exhausted()) {
duke@435 154 JvmtiExport::post_resource_exhausted(
duke@435 155 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
duke@435 156 "GC overhead limit exceeded");
duke@435 157 }
duke@435 158
duke@435 159 THROW_OOP_0(Universe::out_of_memory_error_gc_overhead_limit());
duke@435 160 }
duke@435 161 }
duke@435 162
duke@435 163 HeapWord* CollectedHeap::common_mem_allocate_init(size_t size, bool is_noref, TRAPS) {
duke@435 164 HeapWord* obj = common_mem_allocate_noinit(size, is_noref, CHECK_NULL);
duke@435 165 init_obj(obj, size);
duke@435 166 return obj;
duke@435 167 }
duke@435 168
duke@435 169 // Need to investigate, do we really want to throw OOM exception here?
duke@435 170 HeapWord* CollectedHeap::common_permanent_mem_allocate_noinit(size_t size, TRAPS) {
duke@435 171 if (HAS_PENDING_EXCEPTION) {
duke@435 172 NOT_PRODUCT(guarantee(false, "Should not allocate with exception pending"));
duke@435 173 return NULL; // caller does a CHECK_NULL too
duke@435 174 }
duke@435 175
duke@435 176 #ifdef ASSERT
duke@435 177 if (CIFireOOMAt > 0 && THREAD->is_Compiler_thread() &&
duke@435 178 ++_fire_out_of_memory_count >= CIFireOOMAt) {
duke@435 179 // For testing of OOM handling in the CI throw an OOM and see how
duke@435 180 // it does. Historically improper handling of these has resulted
duke@435 181 // in crashes which we really don't want to have in the CI.
duke@435 182 THROW_OOP_0(Universe::out_of_memory_error_perm_gen());
duke@435 183 }
duke@435 184 #endif
duke@435 185
duke@435 186 HeapWord* result = Universe::heap()->permanent_mem_allocate(size);
duke@435 187 if (result != NULL) {
duke@435 188 NOT_PRODUCT(Universe::heap()->
duke@435 189 check_for_non_bad_heap_word_value(result, size));
duke@435 190 assert(!HAS_PENDING_EXCEPTION,
duke@435 191 "Unexpected exception, will result in uninitialized storage");
duke@435 192 return result;
duke@435 193 }
duke@435 194 // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
duke@435 195 report_java_out_of_memory("PermGen space");
duke@435 196
duke@435 197 if (JvmtiExport::should_post_resource_exhausted()) {
duke@435 198 JvmtiExport::post_resource_exhausted(
duke@435 199 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,
duke@435 200 "PermGen space");
duke@435 201 }
duke@435 202
duke@435 203 THROW_OOP_0(Universe::out_of_memory_error_perm_gen());
duke@435 204 }
duke@435 205
duke@435 206 HeapWord* CollectedHeap::common_permanent_mem_allocate_init(size_t size, TRAPS) {
duke@435 207 HeapWord* obj = common_permanent_mem_allocate_noinit(size, CHECK_NULL);
duke@435 208 init_obj(obj, size);
duke@435 209 return obj;
duke@435 210 }
duke@435 211
duke@435 212 HeapWord* CollectedHeap::allocate_from_tlab(Thread* thread, size_t size) {
duke@435 213 assert(UseTLAB, "should use UseTLAB");
duke@435 214
duke@435 215 HeapWord* obj = thread->tlab().allocate(size);
duke@435 216 if (obj != NULL) {
duke@435 217 return obj;
duke@435 218 }
duke@435 219 // Otherwise...
duke@435 220 return allocate_from_tlab_slow(thread, size);
duke@435 221 }
duke@435 222
duke@435 223 void CollectedHeap::init_obj(HeapWord* obj, size_t size) {
duke@435 224 assert(obj != NULL, "cannot initialize NULL object");
duke@435 225 const size_t hs = oopDesc::header_size();
duke@435 226 assert(size >= hs, "unexpected object size");
duke@435 227 Copy::fill_to_aligned_words(obj + hs, size - hs);
duke@435 228 }
duke@435 229
duke@435 230 oop CollectedHeap::obj_allocate(KlassHandle klass, int size, TRAPS) {
duke@435 231 debug_only(check_for_valid_allocation_state());
duke@435 232 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
duke@435 233 assert(size >= 0, "int won't convert to size_t");
duke@435 234 HeapWord* obj = common_mem_allocate_init(size, false, CHECK_NULL);
duke@435 235 post_allocation_setup_obj(klass, obj, size);
duke@435 236 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
duke@435 237 return (oop)obj;
duke@435 238 }
duke@435 239
duke@435 240 oop CollectedHeap::array_allocate(KlassHandle klass,
duke@435 241 int size,
duke@435 242 int length,
duke@435 243 TRAPS) {
duke@435 244 debug_only(check_for_valid_allocation_state());
duke@435 245 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
duke@435 246 assert(size >= 0, "int won't convert to size_t");
duke@435 247 HeapWord* obj = common_mem_allocate_init(size, false, CHECK_NULL);
duke@435 248 post_allocation_setup_array(klass, obj, size, length);
duke@435 249 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
duke@435 250 return (oop)obj;
duke@435 251 }
duke@435 252
duke@435 253 oop CollectedHeap::large_typearray_allocate(KlassHandle klass,
duke@435 254 int size,
duke@435 255 int length,
duke@435 256 TRAPS) {
duke@435 257 debug_only(check_for_valid_allocation_state());
duke@435 258 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
duke@435 259 assert(size >= 0, "int won't convert to size_t");
duke@435 260 HeapWord* obj = common_mem_allocate_init(size, true, CHECK_NULL);
duke@435 261 post_allocation_setup_array(klass, obj, size, length);
duke@435 262 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
duke@435 263 return (oop)obj;
duke@435 264 }
duke@435 265
duke@435 266 oop CollectedHeap::permanent_obj_allocate(KlassHandle klass, int size, TRAPS) {
duke@435 267 oop obj = permanent_obj_allocate_no_klass_install(klass, size, CHECK_NULL);
duke@435 268 post_allocation_install_obj_klass(klass, obj, size);
duke@435 269 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value((HeapWord*) obj,
duke@435 270 size));
duke@435 271 return obj;
duke@435 272 }
duke@435 273
duke@435 274 oop CollectedHeap::permanent_obj_allocate_no_klass_install(KlassHandle klass,
duke@435 275 int size,
duke@435 276 TRAPS) {
duke@435 277 debug_only(check_for_valid_allocation_state());
duke@435 278 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
duke@435 279 assert(size >= 0, "int won't convert to size_t");
duke@435 280 HeapWord* obj = common_permanent_mem_allocate_init(size, CHECK_NULL);
duke@435 281 post_allocation_setup_no_klass_install(klass, obj, size);
duke@435 282 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
duke@435 283 return (oop)obj;
duke@435 284 }
duke@435 285
duke@435 286 oop CollectedHeap::permanent_array_allocate(KlassHandle klass,
duke@435 287 int size,
duke@435 288 int length,
duke@435 289 TRAPS) {
duke@435 290 debug_only(check_for_valid_allocation_state());
duke@435 291 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
duke@435 292 assert(size >= 0, "int won't convert to size_t");
duke@435 293 HeapWord* obj = common_permanent_mem_allocate_init(size, CHECK_NULL);
duke@435 294 post_allocation_setup_array(klass, obj, size, length);
duke@435 295 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
duke@435 296 return (oop)obj;
duke@435 297 }
duke@435 298
duke@435 299 // Returns "TRUE" if "p" is a method oop in the
duke@435 300 // current heap with high probability. NOTE: The main
duke@435 301 // current consumers of this interface are Forte::
duke@435 302 // and ThreadProfiler::. In these cases, the
duke@435 303 // interpreter frame from which "p" came, may be
duke@435 304 // under construction when sampled asynchronously, so
duke@435 305 // the clients want to check that it represents a
duke@435 306 // valid method before using it. Nonetheless since
duke@435 307 // the clients do not typically lock out GC, the
duke@435 308 // predicate is_valid_method() is not stable, so
duke@435 309 // it is possible that by the time "p" is used, it
duke@435 310 // is no longer valid.
duke@435 311 inline bool CollectedHeap::is_valid_method(oop p) const {
duke@435 312 return
duke@435 313 p != NULL &&
duke@435 314
duke@435 315 // Check whether it is aligned at a HeapWord boundary.
duke@435 316 Space::is_aligned(p) &&
duke@435 317
duke@435 318 // Check whether "method" is in the allocated part of the
duke@435 319 // permanent generation -- this needs to be checked before
duke@435 320 // p->klass() below to avoid a SEGV (but see below
duke@435 321 // for a potential window of vulnerability).
duke@435 322 is_permanent((void*)p) &&
duke@435 323
duke@435 324 // See if GC is active; however, there is still an
duke@435 325 // apparently unavoidable window after this call
duke@435 326 // and before the client of this interface uses "p".
duke@435 327 // If the client chooses not to lock out GC, then
duke@435 328 // it's a risk the client must accept.
duke@435 329 !is_gc_active() &&
duke@435 330
duke@435 331 // Check that p is a methodOop.
duke@435 332 p->klass() == Universe::methodKlassObj();
duke@435 333 }
duke@435 334
duke@435 335
duke@435 336 #ifndef PRODUCT
duke@435 337
duke@435 338 inline bool
duke@435 339 CollectedHeap::promotion_should_fail(volatile size_t* count) {
duke@435 340 // Access to count is not atomic; the value does not have to be exact.
duke@435 341 if (PromotionFailureALot) {
duke@435 342 const size_t gc_num = total_collections();
duke@435 343 const size_t elapsed_gcs = gc_num - _promotion_failure_alot_gc_number;
duke@435 344 if (elapsed_gcs >= PromotionFailureALotInterval) {
duke@435 345 // Test for unsigned arithmetic wrap-around.
duke@435 346 if (++*count >= PromotionFailureALotCount) {
duke@435 347 *count = 0;
duke@435 348 return true;
duke@435 349 }
duke@435 350 }
duke@435 351 }
duke@435 352 return false;
duke@435 353 }
duke@435 354
duke@435 355 inline bool CollectedHeap::promotion_should_fail() {
duke@435 356 return promotion_should_fail(&_promotion_failure_alot_count);
duke@435 357 }
duke@435 358
duke@435 359 inline void CollectedHeap::reset_promotion_should_fail(volatile size_t* count) {
duke@435 360 if (PromotionFailureALot) {
duke@435 361 _promotion_failure_alot_gc_number = total_collections();
duke@435 362 *count = 0;
duke@435 363 }
duke@435 364 }
duke@435 365
duke@435 366 inline void CollectedHeap::reset_promotion_should_fail() {
duke@435 367 reset_promotion_should_fail(&_promotion_failure_alot_count);
duke@435 368 }
duke@435 369 #endif // #ifndef PRODUCT

mercurial