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