1 /* 2 * Copyright (c) 2001, 2009, Oracle and/or its affiliates. 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 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 // Inline allocation implementations. 26 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 } 33 34 void CollectedHeap::post_allocation_setup_no_klass_install(KlassHandle klass, 35 HeapWord* objPtr, 36 size_t size) { 37 oop obj = (oop)objPtr; 38 39 assert(obj != NULL, "NULL object pointer"); 40 if (UseBiasedLocking && (klass() != NULL)) { 41 obj->set_mark(klass->prototype_header()); 42 } else { 43 // May be bootstrapping 44 obj->set_mark(markOopDesc::prototype()); 45 } 46 } 47 48 void CollectedHeap::post_allocation_install_obj_klass(KlassHandle klass, 49 oop obj, 50 int size) { 51 // These asserts are kind of complicated because of klassKlass 52 // and the beginning of the world. 53 assert(klass() != NULL || !Universe::is_fully_initialized(), "NULL klass"); 54 assert(klass() == NULL || klass()->is_klass(), "not a klass"); 55 assert(klass() == NULL || klass()->klass_part() != NULL, "not a klass"); 56 assert(obj != NULL, "NULL object pointer"); 57 obj->set_klass(klass()); 58 assert(!Universe::is_fully_initialized() || obj->blueprint() != NULL, 59 "missing blueprint"); 60 } 61 62 // Support for jvmti and dtrace 63 inline void post_allocation_notify(KlassHandle klass, oop obj) { 64 // support low memory notifications (no-op if not enabled) 65 LowMemoryDetector::detect_low_memory_for_collected_pools(); 66 67 // support for JVMTI VMObjectAlloc event (no-op if not enabled) 68 JvmtiExport::vm_object_alloc_event_collector(obj); 69 70 if (DTraceAllocProbes) { 71 // support for Dtrace object alloc event (no-op most of the time) 72 if (klass() != NULL && klass()->klass_part()->name() != NULL) { 73 SharedRuntime::dtrace_object_alloc(obj); 74 } 75 } 76 } 77 78 void CollectedHeap::post_allocation_setup_obj(KlassHandle klass, 79 HeapWord* obj, 80 size_t size) { 81 post_allocation_setup_common(klass, obj, size); 82 assert(Universe::is_bootstrapping() || 83 !((oop)obj)->blueprint()->oop_is_array(), "must not be an array"); 84 // notify jvmti and dtrace 85 post_allocation_notify(klass, (oop)obj); 86 } 87 88 void CollectedHeap::post_allocation_setup_array(KlassHandle klass, 89 HeapWord* obj, 90 size_t size, 91 int length) { 92 // Set array length before setting the _klass field 93 // in post_allocation_setup_common() because the klass field 94 // indicates that the object is parsable by concurrent GC. 95 assert(length >= 0, "length should be non-negative"); 96 ((arrayOop)obj)->set_length(length); 97 post_allocation_setup_common(klass, obj, size); 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 } 102 103 HeapWord* CollectedHeap::common_mem_allocate_noinit(size_t size, bool is_noref, TRAPS) { 104 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();) 108 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 } 113 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 = false; 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 } 136 137 138 if (!gc_overhead_limit_was_exceeded) { 139 // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support 140 report_java_out_of_memory("Java heap space"); 141 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 } 147 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"); 152 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 } 158 159 THROW_OOP_0(Universe::out_of_memory_error_gc_overhead_limit()); 160 } 161 } 162 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 } 168 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 } 175 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 185 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"); 196 197 if (JvmtiExport::should_post_resource_exhausted()) { 198 JvmtiExport::post_resource_exhausted( 199 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR, 200 "PermGen space"); 201 } 202 203 THROW_OOP_0(Universe::out_of_memory_error_perm_gen()); 204 } 205 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 } 211 212 HeapWord* CollectedHeap::allocate_from_tlab(Thread* thread, size_t size) { 213 assert(UseTLAB, "should use UseTLAB"); 214 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 } 222 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 ((oop)obj)->set_klass_gap(0); 228 Copy::fill_to_aligned_words(obj + hs, size - hs); 229 } 230 231 oop CollectedHeap::obj_allocate(KlassHandle klass, int size, TRAPS) { 232 debug_only(check_for_valid_allocation_state()); 233 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed"); 234 assert(size >= 0, "int won't convert to size_t"); 235 HeapWord* obj = common_mem_allocate_init(size, false, CHECK_NULL); 236 post_allocation_setup_obj(klass, obj, size); 237 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size)); 238 return (oop)obj; 239 } 240 241 oop CollectedHeap::array_allocate(KlassHandle klass, 242 int size, 243 int length, 244 TRAPS) { 245 debug_only(check_for_valid_allocation_state()); 246 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed"); 247 assert(size >= 0, "int won't convert to size_t"); 248 HeapWord* obj = common_mem_allocate_init(size, false, CHECK_NULL); 249 post_allocation_setup_array(klass, obj, size, length); 250 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size)); 251 return (oop)obj; 252 } 253 254 oop CollectedHeap::large_typearray_allocate(KlassHandle klass, 255 int size, 256 int length, 257 TRAPS) { 258 debug_only(check_for_valid_allocation_state()); 259 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed"); 260 assert(size >= 0, "int won't convert to size_t"); 261 HeapWord* obj = common_mem_allocate_init(size, true, CHECK_NULL); 262 post_allocation_setup_array(klass, obj, size, length); 263 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size)); 264 return (oop)obj; 265 } 266 267 oop CollectedHeap::permanent_obj_allocate(KlassHandle klass, int size, TRAPS) { 268 oop obj = permanent_obj_allocate_no_klass_install(klass, size, CHECK_NULL); 269 post_allocation_install_obj_klass(klass, obj, size); 270 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value((HeapWord*) obj, 271 size)); 272 return obj; 273 } 274 275 oop CollectedHeap::permanent_obj_allocate_no_klass_install(KlassHandle klass, 276 int size, 277 TRAPS) { 278 debug_only(check_for_valid_allocation_state()); 279 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed"); 280 assert(size >= 0, "int won't convert to size_t"); 281 HeapWord* obj = common_permanent_mem_allocate_init(size, CHECK_NULL); 282 post_allocation_setup_no_klass_install(klass, obj, size); 283 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size)); 284 return (oop)obj; 285 } 286 287 oop CollectedHeap::permanent_array_allocate(KlassHandle klass, 288 int size, 289 int length, 290 TRAPS) { 291 debug_only(check_for_valid_allocation_state()); 292 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed"); 293 assert(size >= 0, "int won't convert to size_t"); 294 HeapWord* obj = common_permanent_mem_allocate_init(size, CHECK_NULL); 295 post_allocation_setup_array(klass, obj, size, length); 296 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size)); 297 return (oop)obj; 298 } 299 300 // Returns "TRUE" if "p" is a method oop in the 301 // current heap with high probability. NOTE: The main 302 // current consumers of this interface are Forte:: 303 // and ThreadProfiler::. In these cases, the 304 // interpreter frame from which "p" came, may be 305 // under construction when sampled asynchronously, so 306 // the clients want to check that it represents a 307 // valid method before using it. Nonetheless since 308 // the clients do not typically lock out GC, the 309 // predicate is_valid_method() is not stable, so 310 // it is possible that by the time "p" is used, it 311 // is no longer valid. 312 inline bool CollectedHeap::is_valid_method(oop p) const { 313 return 314 p != NULL && 315 316 // Check whether it is aligned at a HeapWord boundary. 317 Space::is_aligned(p) && 318 319 // Check whether "method" is in the allocated part of the 320 // permanent generation -- this needs to be checked before 321 // p->klass() below to avoid a SEGV (but see below 322 // for a potential window of vulnerability). 323 is_permanent((void*)p) && 324 325 // See if GC is active; however, there is still an 326 // apparently unavoidable window after this call 327 // and before the client of this interface uses "p". 328 // If the client chooses not to lock out GC, then 329 // it's a risk the client must accept. 330 !is_gc_active() && 331 332 // Check that p is a methodOop. 333 p->klass() == Universe::methodKlassObj(); 334 } 335 336 337 #ifndef PRODUCT 338 339 inline bool 340 CollectedHeap::promotion_should_fail(volatile size_t* count) { 341 // Access to count is not atomic; the value does not have to be exact. 342 if (PromotionFailureALot) { 343 const size_t gc_num = total_collections(); 344 const size_t elapsed_gcs = gc_num - _promotion_failure_alot_gc_number; 345 if (elapsed_gcs >= PromotionFailureALotInterval) { 346 // Test for unsigned arithmetic wrap-around. 347 if (++*count >= PromotionFailureALotCount) { 348 *count = 0; 349 return true; 350 } 351 } 352 } 353 return false; 354 } 355 356 inline bool CollectedHeap::promotion_should_fail() { 357 return promotion_should_fail(&_promotion_failure_alot_count); 358 } 359 360 inline void CollectedHeap::reset_promotion_should_fail(volatile size_t* count) { 361 if (PromotionFailureALot) { 362 _promotion_failure_alot_gc_number = total_collections(); 363 *count = 0; 364 } 365 } 366 367 inline void CollectedHeap::reset_promotion_should_fail() { 368 reset_promotion_should_fail(&_promotion_failure_alot_count); 369 } 370 #endif // #ifndef PRODUCT