1 /*
   2  * Copyright (c) 2001, 2016, 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_SHARED_COLLECTEDHEAP_INLINE_HPP
  26 #define SHARE_VM_GC_SHARED_COLLECTEDHEAP_INLINE_HPP
  27 
  28 #include "gc/shared/allocTracer.hpp"
  29 #include "gc/shared/collectedHeap.hpp"
  30 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
  31 #include "memory/universe.hpp"
  32 #include "oops/arrayOop.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "prims/jvmtiExport.hpp"
  35 #include "runtime/sharedRuntime.hpp"
  36 #include "runtime/thread.inline.hpp"
  37 #include "services/lowMemoryDetector.hpp"
  38 #include "utilities/copy.hpp"
  39 
  40 // Inline allocation implementations.
  41 
  42 void CollectedHeap::post_allocation_setup_common(KlassHandle klass,
  43                                                  HeapWord* obj) {
  44   post_allocation_setup_no_klass_install(klass, obj);
  45   post_allocation_install_obj_klass(klass, oop(obj));
  46 }
  47 
  48 void CollectedHeap::post_allocation_setup_no_klass_install(KlassHandle klass,
  49                                                            HeapWord* objPtr) {
  50   oop obj = (oop)objPtr;
  51 
  52   assert(obj != NULL, "NULL object pointer");
  53   if (UseBiasedLocking && (klass() != NULL)) {
  54     obj->set_mark(klass->prototype_header());
  55   } else {
  56     // May be bootstrapping
  57     obj->set_mark(markOopDesc::prototype());
  58   }
  59 }
  60 
  61 void CollectedHeap::post_allocation_install_obj_klass(KlassHandle klass,
  62                                                    oop obj) {
  63   // These asserts are kind of complicated because of klassKlass
  64   // and the beginning of the world.
  65   assert(klass() != NULL || !Universe::is_fully_initialized(), "NULL klass");
  66   assert(klass() == NULL || klass()->is_klass(), "not a klass");
  67   assert(obj != NULL, "NULL object pointer");
  68   obj->set_klass(klass());
  69   assert(!Universe::is_fully_initialized() || obj->klass() != NULL,
  70          "missing klass");
  71 }
  72 
  73 // Support for jvmti and dtrace
  74 inline void post_allocation_notify(KlassHandle klass, oop obj, int size) {
  75   // support low memory notifications (no-op if not enabled)
  76   LowMemoryDetector::detect_low_memory_for_collected_pools();
  77 
  78   // support for JVMTI VMObjectAlloc event (no-op if not enabled)
  79   JvmtiExport::vm_object_alloc_event_collector(obj);
  80 
  81   if (DTraceAllocProbes) {
  82     // support for Dtrace object alloc event (no-op most of the time)
  83     if (klass() != NULL && klass()->name() != NULL) {
  84       SharedRuntime::dtrace_object_alloc(obj, size);
  85     }
  86   }
  87 }
  88 
  89 void CollectedHeap::post_allocation_setup_obj(KlassHandle klass,
  90                                               HeapWord* obj,
  91                                               int size) {
  92   post_allocation_setup_common(klass, obj);
  93   assert(Universe::is_bootstrapping() ||
  94          !((oop)obj)->is_array(), "must not be an array");
  95   // notify jvmti and dtrace
  96   post_allocation_notify(klass, (oop)obj, size);
  97 }
  98 
  99 void CollectedHeap::post_allocation_setup_class(KlassHandle klass,
 100                                                 HeapWord* obj,
 101                                                 int size,
 102                                                 int size_field_offset) {
 103   // Set oop_size field before setting the _klass field
 104   // in post_allocation_setup_common() because the klass field
 105   // indicates that the object is parsable by concurrent GC.
 106   oop new_obj = (oop)obj;
 107   assert(size > 0, "oop_size must be positive.");
 108   assert(size_field_offset > 0, "size_field_offset must be initialized.");
 109   new_obj->int_field_put(size_field_offset, size);
 110   post_allocation_setup_common(klass, obj);
 111   assert(Universe::is_bootstrapping() ||
 112          !new_obj->is_array(), "must not be an array");
 113   // notify jvmti and dtrace
 114   post_allocation_notify(klass, new_obj, size);
 115 }
 116 
 117 void CollectedHeap::post_allocation_setup_array(KlassHandle klass,
 118                                                 HeapWord* obj,
 119                                                 int length) {
 120   // Set array length before setting the _klass field
 121   // in post_allocation_setup_common() because the klass field
 122   // indicates that the object is parsable by concurrent GC.
 123   assert(length >= 0, "length should be non-negative");
 124   ((arrayOop)obj)->set_length(length);
 125   post_allocation_setup_common(klass, obj);
 126   oop new_obj = (oop)obj;
 127   assert(new_obj->is_array(), "must be an array");
 128   // notify jvmti and dtrace (must be after length is set for dtrace)
 129   post_allocation_notify(klass, new_obj, new_obj->size());
 130 }
 131 
 132 HeapWord* CollectedHeap::common_mem_allocate_noinit(KlassHandle klass, size_t size, TRAPS) {
 133 
 134   // Clear unhandled oops for memory allocation.  Memory allocation might
 135   // not take out a lock if from tlab, so clear here.
 136   CHECK_UNHANDLED_OOPS_ONLY(THREAD->clear_unhandled_oops();)
 137 
 138   if (HAS_PENDING_EXCEPTION) {
 139     NOT_PRODUCT(guarantee(false, "Should not allocate with exception pending"));
 140     return NULL;  // caller does a CHECK_0 too
 141   }
 142 
 143   HeapWord* result = NULL;
 144   if (UseTLAB) {
 145     result = allocate_from_tlab(klass, THREAD, size);
 146     if (result != NULL) {
 147       assert(!HAS_PENDING_EXCEPTION,
 148              "Unexpected exception, will result in uninitialized storage");
 149       return result;
 150     }
 151   }
 152   bool gc_overhead_limit_was_exceeded = false;
 153   result = Universe::heap()->mem_allocate(size,
 154                                           &gc_overhead_limit_was_exceeded);
 155   if (result != NULL) {
 156     NOT_PRODUCT(Universe::heap()->
 157       check_for_non_bad_heap_word_value(result, size));
 158     assert(!HAS_PENDING_EXCEPTION,
 159            "Unexpected exception, will result in uninitialized storage");
 160     THREAD->incr_allocated_bytes(size * HeapWordSize);
 161 
 162     AllocTracer::send_allocation_outside_tlab_event(klass, size * HeapWordSize);
 163 
 164     return result;
 165   }
 166 
 167 
 168   if (!gc_overhead_limit_was_exceeded) {
 169     // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
 170     report_java_out_of_memory("Java heap space");
 171 
 172     if (JvmtiExport::should_post_resource_exhausted()) {
 173       JvmtiExport::post_resource_exhausted(
 174         JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
 175         "Java heap space");
 176     }
 177 
 178     THROW_OOP_0(Universe::out_of_memory_error_java_heap());
 179   } else {
 180     // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
 181     report_java_out_of_memory("GC overhead limit exceeded");
 182 
 183     if (JvmtiExport::should_post_resource_exhausted()) {
 184       JvmtiExport::post_resource_exhausted(
 185         JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
 186         "GC overhead limit exceeded");
 187     }
 188 
 189     THROW_OOP_0(Universe::out_of_memory_error_gc_overhead_limit());
 190   }
 191 }
 192 
 193 HeapWord* CollectedHeap::common_mem_allocate_init(KlassHandle klass, size_t size, TRAPS) {
 194   HeapWord* obj = common_mem_allocate_noinit(klass, size, CHECK_NULL);
 195   init_obj(obj, size);
 196   return obj;
 197 }
 198 
 199 HeapWord* CollectedHeap::allocate_from_tlab(KlassHandle klass, Thread* thread, size_t size) {
 200   assert(UseTLAB, "should use UseTLAB");
 201 
 202   HeapWord* obj = thread->tlab().allocate(size);
 203   if (obj != NULL) {
 204     return obj;
 205   }
 206   // Otherwise...
 207   return allocate_from_tlab_slow(klass, thread, size);
 208 }
 209 
 210 void CollectedHeap::init_obj(HeapWord* obj, size_t size) {
 211   assert(obj != NULL, "cannot initialize NULL object");
 212   const size_t hs = oopDesc::header_size();
 213   assert(size >= hs, "unexpected object size");
 214   ((oop)obj)->set_klass_gap(0);
 215   Copy::fill_to_aligned_words(obj + hs, size - hs);
 216 }
 217 
 218 oop CollectedHeap::obj_allocate(KlassHandle klass, int size, TRAPS) {
 219   debug_only(check_for_valid_allocation_state());
 220   assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
 221   assert(size >= 0, "int won't convert to size_t");
 222   HeapWord* obj = common_mem_allocate_init(klass, size, CHECK_NULL);
 223   post_allocation_setup_obj(klass, obj, size);
 224   NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
 225   return (oop)obj;
 226 }
 227 
 228 // Instances of j.l.Class have an oop_size field that must be set before the 
 229 // the header is set in order to parse the instances's size correctly.
 230 oop CollectedHeap::class_allocate(KlassHandle klass, int size, int size_field_offset, 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(klass, size, CHECK_NULL);
 235   post_allocation_setup_class(klass, obj, size, size_field_offset); // set oop_size
 236   NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
 237   return (oop)obj;
 238 }
 239 
 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(klass, size, CHECK_NULL);
 248   post_allocation_setup_array(klass, obj, length);
 249   NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
 250   return (oop)obj;
 251 }
 252 
 253 oop CollectedHeap::array_allocate_nozero(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_noinit(klass, size, CHECK_NULL);
 261   ((oop)obj)->set_klass_gap(0);
 262   post_allocation_setup_array(klass, obj, length);
 263 #ifndef PRODUCT
 264   const size_t hs = oopDesc::header_size()+1;
 265   Universe::heap()->check_for_non_bad_heap_word_value(obj+hs, size-hs);
 266 #endif
 267   return (oop)obj;
 268 }
 269 
 270 inline HeapWord* CollectedHeap::align_allocation_or_fail(HeapWord* addr,
 271                                                          HeapWord* end,
 272                                                          unsigned short alignment_in_bytes) {
 273   if (alignment_in_bytes <= ObjectAlignmentInBytes) {
 274     return addr;
 275   }
 276 
 277   assert(is_ptr_aligned(addr, HeapWordSize),
 278          "Address " PTR_FORMAT " is not properly aligned.", p2i(addr));
 279   assert(is_size_aligned(alignment_in_bytes, HeapWordSize),
 280          "Alignment size %u is incorrect.", alignment_in_bytes);
 281 
 282   HeapWord* new_addr = (HeapWord*) align_ptr_up(addr, alignment_in_bytes);
 283   size_t padding = pointer_delta(new_addr, addr);
 284 
 285   if (padding == 0) {
 286     return addr;
 287   }
 288 
 289   if (padding < CollectedHeap::min_fill_size()) {
 290     padding += alignment_in_bytes / HeapWordSize;
 291     assert(padding >= CollectedHeap::min_fill_size(),
 292            "alignment_in_bytes %u is expect to be larger "
 293            "than the minimum object size", alignment_in_bytes);
 294     new_addr = addr + padding;
 295   }
 296 
 297   assert(new_addr > addr, "Unexpected arithmetic overflow "
 298          PTR_FORMAT " not greater than " PTR_FORMAT, p2i(new_addr), p2i(addr));
 299   if(new_addr < end) {
 300     CollectedHeap::fill_with_object(addr, padding);
 301     return new_addr;
 302   } else {
 303     return NULL;
 304   }
 305 }
 306 
 307 #ifndef PRODUCT
 308 
 309 inline bool
 310 CollectedHeap::promotion_should_fail(volatile size_t* count) {
 311   // Access to count is not atomic; the value does not have to be exact.
 312   if (PromotionFailureALot) {
 313     const size_t gc_num = total_collections();
 314     const size_t elapsed_gcs = gc_num - _promotion_failure_alot_gc_number;
 315     if (elapsed_gcs >= PromotionFailureALotInterval) {
 316       // Test for unsigned arithmetic wrap-around.
 317       if (++*count >= PromotionFailureALotCount) {
 318         *count = 0;
 319         return true;
 320       }
 321     }
 322   }
 323   return false;
 324 }
 325 
 326 inline bool CollectedHeap::promotion_should_fail() {
 327   return promotion_should_fail(&_promotion_failure_alot_count);
 328 }
 329 
 330 inline void CollectedHeap::reset_promotion_should_fail(volatile size_t* count) {
 331   if (PromotionFailureALot) {
 332     _promotion_failure_alot_gc_number = total_collections();
 333     *count = 0;
 334   }
 335 }
 336 
 337 inline void CollectedHeap::reset_promotion_should_fail() {
 338   reset_promotion_should_fail(&_promotion_failure_alot_count);
 339 }
 340 #endif  // #ifndef PRODUCT
 341 
 342 #endif // SHARE_VM_GC_SHARED_COLLECTEDHEAP_INLINE_HPP