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

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6627
cf9f24de0b93
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_GC_INTERFACE_COLLECTEDHEAP_INLINE_HPP
aoqi@0 26 #define SHARE_VM_GC_INTERFACE_COLLECTEDHEAP_INLINE_HPP
aoqi@0 27
aoqi@0 28 #include "gc_interface/allocTracer.hpp"
aoqi@0 29 #include "gc_interface/collectedHeap.hpp"
aoqi@0 30 #include "memory/threadLocalAllocBuffer.inline.hpp"
aoqi@0 31 #include "memory/universe.hpp"
aoqi@0 32 #include "oops/arrayOop.hpp"
aoqi@0 33 #include "prims/jvmtiExport.hpp"
aoqi@0 34 #include "runtime/sharedRuntime.hpp"
aoqi@0 35 #include "runtime/thread.inline.hpp"
aoqi@0 36 #include "services/lowMemoryDetector.hpp"
aoqi@0 37 #include "utilities/copy.hpp"
aoqi@0 38
aoqi@0 39 // Inline allocation implementations.
aoqi@0 40
aoqi@0 41 void CollectedHeap::post_allocation_setup_common(KlassHandle klass,
aoqi@0 42 HeapWord* obj) {
aoqi@0 43 post_allocation_setup_no_klass_install(klass, obj);
aoqi@0 44 post_allocation_install_obj_klass(klass, oop(obj));
aoqi@0 45 }
aoqi@0 46
aoqi@0 47 void CollectedHeap::post_allocation_setup_no_klass_install(KlassHandle klass,
aoqi@0 48 HeapWord* objPtr) {
aoqi@0 49 oop obj = (oop)objPtr;
aoqi@0 50
aoqi@0 51 assert(obj != NULL, "NULL object pointer");
aoqi@0 52 if (UseBiasedLocking && (klass() != NULL)) {
aoqi@0 53 obj->set_mark(klass->prototype_header());
aoqi@0 54 } else {
aoqi@0 55 // May be bootstrapping
aoqi@0 56 obj->set_mark(markOopDesc::prototype());
aoqi@0 57 }
aoqi@0 58 }
aoqi@0 59
aoqi@0 60 void CollectedHeap::post_allocation_install_obj_klass(KlassHandle klass,
aoqi@0 61 oop obj) {
aoqi@0 62 // These asserts are kind of complicated because of klassKlass
aoqi@0 63 // and the beginning of the world.
aoqi@0 64 assert(klass() != NULL || !Universe::is_fully_initialized(), "NULL klass");
aoqi@0 65 assert(klass() == NULL || klass()->is_klass(), "not a klass");
aoqi@0 66 assert(obj != NULL, "NULL object pointer");
aoqi@0 67 obj->set_klass(klass());
aoqi@0 68 assert(!Universe::is_fully_initialized() || obj->klass() != NULL,
aoqi@0 69 "missing klass");
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 // Support for jvmti and dtrace
aoqi@0 73 inline void post_allocation_notify(KlassHandle klass, oop obj, int size) {
aoqi@0 74 // support low memory notifications (no-op if not enabled)
aoqi@0 75 LowMemoryDetector::detect_low_memory_for_collected_pools();
aoqi@0 76
aoqi@0 77 // support for JVMTI VMObjectAlloc event (no-op if not enabled)
aoqi@0 78 JvmtiExport::vm_object_alloc_event_collector(obj);
aoqi@0 79
aoqi@0 80 if (DTraceAllocProbes) {
aoqi@0 81 // support for Dtrace object alloc event (no-op most of the time)
aoqi@0 82 if (klass() != NULL && klass()->name() != NULL) {
aoqi@0 83 SharedRuntime::dtrace_object_alloc(obj, size);
aoqi@0 84 }
aoqi@0 85 }
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 void CollectedHeap::post_allocation_setup_obj(KlassHandle klass,
aoqi@0 89 HeapWord* obj,
aoqi@0 90 int size) {
aoqi@0 91 post_allocation_setup_common(klass, obj);
aoqi@0 92 assert(Universe::is_bootstrapping() ||
aoqi@0 93 !((oop)obj)->is_array(), "must not be an array");
aoqi@0 94 // notify jvmti and dtrace
aoqi@0 95 post_allocation_notify(klass, (oop)obj, size);
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 void CollectedHeap::post_allocation_setup_array(KlassHandle klass,
aoqi@0 99 HeapWord* obj,
aoqi@0 100 int length) {
aoqi@0 101 // Set array length before setting the _klass field
aoqi@0 102 // in post_allocation_setup_common() because the klass field
aoqi@0 103 // indicates that the object is parsable by concurrent GC.
aoqi@0 104 assert(length >= 0, "length should be non-negative");
aoqi@0 105 ((arrayOop)obj)->set_length(length);
aoqi@0 106 post_allocation_setup_common(klass, obj);
aoqi@0 107 oop new_obj = (oop)obj;
aoqi@0 108 assert(new_obj->is_array(), "must be an array");
aoqi@0 109 // notify jvmti and dtrace (must be after length is set for dtrace)
aoqi@0 110 post_allocation_notify(klass, new_obj, new_obj->size());
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 HeapWord* CollectedHeap::common_mem_allocate_noinit(KlassHandle klass, size_t size, TRAPS) {
aoqi@0 114
aoqi@0 115 // Clear unhandled oops for memory allocation. Memory allocation might
aoqi@0 116 // not take out a lock if from tlab, so clear here.
aoqi@0 117 CHECK_UNHANDLED_OOPS_ONLY(THREAD->clear_unhandled_oops();)
aoqi@0 118
aoqi@0 119 if (HAS_PENDING_EXCEPTION) {
aoqi@0 120 NOT_PRODUCT(guarantee(false, "Should not allocate with exception pending"));
aoqi@0 121 return NULL; // caller does a CHECK_0 too
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 HeapWord* result = NULL;
aoqi@0 125 if (UseTLAB) {
aoqi@0 126 result = allocate_from_tlab(klass, THREAD, size);
aoqi@0 127 if (result != NULL) {
aoqi@0 128 assert(!HAS_PENDING_EXCEPTION,
aoqi@0 129 "Unexpected exception, will result in uninitialized storage");
aoqi@0 130 return result;
aoqi@0 131 }
aoqi@0 132 }
aoqi@0 133 bool gc_overhead_limit_was_exceeded = false;
aoqi@0 134 result = Universe::heap()->mem_allocate(size,
aoqi@0 135 &gc_overhead_limit_was_exceeded);
aoqi@0 136 if (result != NULL) {
aoqi@0 137 NOT_PRODUCT(Universe::heap()->
aoqi@0 138 check_for_non_bad_heap_word_value(result, size));
aoqi@0 139 assert(!HAS_PENDING_EXCEPTION,
aoqi@0 140 "Unexpected exception, will result in uninitialized storage");
aoqi@0 141 THREAD->incr_allocated_bytes(size * HeapWordSize);
aoqi@0 142
aoqi@0 143 AllocTracer::send_allocation_outside_tlab_event(klass, size * HeapWordSize);
aoqi@0 144
aoqi@0 145 return result;
aoqi@0 146 }
aoqi@0 147
aoqi@0 148
aoqi@0 149 if (!gc_overhead_limit_was_exceeded) {
aoqi@0 150 // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
aoqi@0 151 report_java_out_of_memory("Java heap space");
aoqi@0 152
aoqi@0 153 if (JvmtiExport::should_post_resource_exhausted()) {
aoqi@0 154 JvmtiExport::post_resource_exhausted(
aoqi@0 155 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
aoqi@0 156 "Java heap space");
aoqi@0 157 }
aoqi@0 158
aoqi@0 159 THROW_OOP_0(Universe::out_of_memory_error_java_heap());
aoqi@0 160 } else {
aoqi@0 161 // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
aoqi@0 162 report_java_out_of_memory("GC overhead limit exceeded");
aoqi@0 163
aoqi@0 164 if (JvmtiExport::should_post_resource_exhausted()) {
aoqi@0 165 JvmtiExport::post_resource_exhausted(
aoqi@0 166 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
aoqi@0 167 "GC overhead limit exceeded");
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 THROW_OOP_0(Universe::out_of_memory_error_gc_overhead_limit());
aoqi@0 171 }
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 HeapWord* CollectedHeap::common_mem_allocate_init(KlassHandle klass, size_t size, TRAPS) {
aoqi@0 175 HeapWord* obj = common_mem_allocate_noinit(klass, size, CHECK_NULL);
aoqi@0 176 init_obj(obj, size);
aoqi@0 177 return obj;
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 HeapWord* CollectedHeap::allocate_from_tlab(KlassHandle klass, Thread* thread, size_t size) {
aoqi@0 181 assert(UseTLAB, "should use UseTLAB");
aoqi@0 182
aoqi@0 183 HeapWord* obj = thread->tlab().allocate(size);
aoqi@0 184 if (obj != NULL) {
aoqi@0 185 return obj;
aoqi@0 186 }
aoqi@0 187 // Otherwise...
aoqi@0 188 return allocate_from_tlab_slow(klass, thread, size);
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 void CollectedHeap::init_obj(HeapWord* obj, size_t size) {
aoqi@0 192 assert(obj != NULL, "cannot initialize NULL object");
aoqi@0 193 const size_t hs = oopDesc::header_size();
aoqi@0 194 assert(size >= hs, "unexpected object size");
aoqi@0 195 ((oop)obj)->set_klass_gap(0);
aoqi@0 196 Copy::fill_to_aligned_words(obj + hs, size - hs);
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 oop CollectedHeap::obj_allocate(KlassHandle klass, int size, TRAPS) {
aoqi@0 200 debug_only(check_for_valid_allocation_state());
aoqi@0 201 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
aoqi@0 202 assert(size >= 0, "int won't convert to size_t");
aoqi@0 203 HeapWord* obj = common_mem_allocate_init(klass, size, CHECK_NULL);
aoqi@0 204 post_allocation_setup_obj(klass, obj, size);
aoqi@0 205 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
aoqi@0 206 return (oop)obj;
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 oop CollectedHeap::array_allocate(KlassHandle klass,
aoqi@0 210 int size,
aoqi@0 211 int length,
aoqi@0 212 TRAPS) {
aoqi@0 213 debug_only(check_for_valid_allocation_state());
aoqi@0 214 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
aoqi@0 215 assert(size >= 0, "int won't convert to size_t");
aoqi@0 216 HeapWord* obj = common_mem_allocate_init(klass, size, CHECK_NULL);
aoqi@0 217 post_allocation_setup_array(klass, obj, length);
aoqi@0 218 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
aoqi@0 219 return (oop)obj;
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 oop CollectedHeap::array_allocate_nozero(KlassHandle klass,
aoqi@0 223 int size,
aoqi@0 224 int length,
aoqi@0 225 TRAPS) {
aoqi@0 226 debug_only(check_for_valid_allocation_state());
aoqi@0 227 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
aoqi@0 228 assert(size >= 0, "int won't convert to size_t");
aoqi@0 229 HeapWord* obj = common_mem_allocate_noinit(klass, size, CHECK_NULL);
aoqi@0 230 ((oop)obj)->set_klass_gap(0);
aoqi@0 231 post_allocation_setup_array(klass, obj, length);
aoqi@0 232 #ifndef PRODUCT
aoqi@0 233 const size_t hs = oopDesc::header_size()+1;
aoqi@0 234 Universe::heap()->check_for_non_bad_heap_word_value(obj+hs, size-hs);
aoqi@0 235 #endif
aoqi@0 236 return (oop)obj;
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 inline void CollectedHeap::oop_iterate_no_header(OopClosure* cl) {
aoqi@0 240 NoHeaderExtendedOopClosure no_header_cl(cl);
aoqi@0 241 oop_iterate(&no_header_cl);
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 #ifndef PRODUCT
aoqi@0 245
aoqi@0 246 inline bool
aoqi@0 247 CollectedHeap::promotion_should_fail(volatile size_t* count) {
aoqi@0 248 // Access to count is not atomic; the value does not have to be exact.
aoqi@0 249 if (PromotionFailureALot) {
aoqi@0 250 const size_t gc_num = total_collections();
aoqi@0 251 const size_t elapsed_gcs = gc_num - _promotion_failure_alot_gc_number;
aoqi@0 252 if (elapsed_gcs >= PromotionFailureALotInterval) {
aoqi@0 253 // Test for unsigned arithmetic wrap-around.
aoqi@0 254 if (++*count >= PromotionFailureALotCount) {
aoqi@0 255 *count = 0;
aoqi@0 256 return true;
aoqi@0 257 }
aoqi@0 258 }
aoqi@0 259 }
aoqi@0 260 return false;
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 inline bool CollectedHeap::promotion_should_fail() {
aoqi@0 264 return promotion_should_fail(&_promotion_failure_alot_count);
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 inline void CollectedHeap::reset_promotion_should_fail(volatile size_t* count) {
aoqi@0 268 if (PromotionFailureALot) {
aoqi@0 269 _promotion_failure_alot_gc_number = total_collections();
aoqi@0 270 *count = 0;
aoqi@0 271 }
aoqi@0 272 }
aoqi@0 273
aoqi@0 274 inline void CollectedHeap::reset_promotion_should_fail() {
aoqi@0 275 reset_promotion_should_fail(&_promotion_failure_alot_count);
aoqi@0 276 }
aoqi@0 277 #endif // #ifndef PRODUCT
aoqi@0 278
aoqi@0 279 #endif // SHARE_VM_GC_INTERFACE_COLLECTEDHEAP_INLINE_HPP

mercurial