Fri, 21 Mar 2014 10:16:35 +0100
8036696: Add metaspace gc threshold to metaspace summary trace event
Reviewed-by: jmasa, stefank, mgerdin
duke@435 | 1 | /* |
sla@5237 | 2 | * Copyright (c) 2001, 2013, Oracle and/or its affiliates. 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 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
stefank@2314 | 26 | #include "classfile/systemDictionary.hpp" |
sla@5237 | 27 | #include "gc_implementation/shared/gcHeapSummary.hpp" |
sla@5237 | 28 | #include "gc_implementation/shared/gcTrace.hpp" |
sla@5237 | 29 | #include "gc_implementation/shared/gcTraceTime.hpp" |
sla@5237 | 30 | #include "gc_implementation/shared/gcWhen.hpp" |
stefank@2314 | 31 | #include "gc_implementation/shared/vmGCOperations.hpp" |
sla@5237 | 32 | #include "gc_interface/allocTracer.hpp" |
stefank@2314 | 33 | #include "gc_interface/collectedHeap.hpp" |
stefank@2314 | 34 | #include "gc_interface/collectedHeap.inline.hpp" |
sla@5237 | 35 | #include "memory/metaspace.hpp" |
stefank@2314 | 36 | #include "oops/oop.inline.hpp" |
never@3205 | 37 | #include "oops/instanceMirrorKlass.hpp" |
stefank@2314 | 38 | #include "runtime/init.hpp" |
stefank@4299 | 39 | #include "runtime/thread.inline.hpp" |
stefank@2314 | 40 | #include "services/heapDumper.hpp" |
duke@435 | 41 | |
duke@435 | 42 | |
duke@435 | 43 | #ifdef ASSERT |
duke@435 | 44 | int CollectedHeap::_fire_out_of_memory_count = 0; |
duke@435 | 45 | #endif |
duke@435 | 46 | |
jcoomes@916 | 47 | size_t CollectedHeap::_filler_array_max_size = 0; |
jcoomes@916 | 48 | |
never@3499 | 49 | template <> |
never@3499 | 50 | void EventLogBase<GCMessage>::print(outputStream* st, GCMessage& m) { |
never@3499 | 51 | st->print_cr("GC heap %s", m.is_before ? "before" : "after"); |
never@3499 | 52 | st->print_raw(m); |
never@3499 | 53 | } |
never@3499 | 54 | |
never@3499 | 55 | void GCHeapLog::log_heap(bool before) { |
never@3499 | 56 | if (!should_log()) { |
never@3499 | 57 | return; |
never@3499 | 58 | } |
never@3499 | 59 | |
never@3571 | 60 | double timestamp = fetch_timestamp(); |
never@3499 | 61 | MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag); |
never@3499 | 62 | int index = compute_log_index(); |
never@3499 | 63 | _records[index].thread = NULL; // Its the GC thread so it's not that interesting. |
never@3499 | 64 | _records[index].timestamp = timestamp; |
never@3499 | 65 | _records[index].data.is_before = before; |
never@3499 | 66 | stringStream st(_records[index].data.buffer(), _records[index].data.size()); |
never@3499 | 67 | if (before) { |
never@3571 | 68 | Universe::print_heap_before_gc(&st, true); |
never@3499 | 69 | } else { |
never@3571 | 70 | Universe::print_heap_after_gc(&st, true); |
never@3499 | 71 | } |
never@3499 | 72 | } |
never@3499 | 73 | |
sla@5237 | 74 | VirtualSpaceSummary CollectedHeap::create_heap_space_summary() { |
sla@5237 | 75 | size_t capacity_in_words = capacity() / HeapWordSize; |
sla@5237 | 76 | |
sla@5237 | 77 | return VirtualSpaceSummary( |
sla@5237 | 78 | reserved_region().start(), reserved_region().start() + capacity_in_words, reserved_region().end()); |
sla@5237 | 79 | } |
sla@5237 | 80 | |
sla@5237 | 81 | GCHeapSummary CollectedHeap::create_heap_summary() { |
sla@5237 | 82 | VirtualSpaceSummary heap_space = create_heap_space_summary(); |
sla@5237 | 83 | return GCHeapSummary(heap_space, used()); |
sla@5237 | 84 | } |
sla@5237 | 85 | |
sla@5237 | 86 | MetaspaceSummary CollectedHeap::create_metaspace_summary() { |
sla@5237 | 87 | const MetaspaceSizes meta_space( |
ehelin@5389 | 88 | MetaspaceAux::allocated_capacity_bytes(), |
ehelin@5389 | 89 | MetaspaceAux::allocated_used_bytes(), |
ehelin@5703 | 90 | MetaspaceAux::reserved_bytes()); |
sla@5237 | 91 | const MetaspaceSizes data_space( |
ehelin@5389 | 92 | MetaspaceAux::allocated_capacity_bytes(Metaspace::NonClassType), |
ehelin@5389 | 93 | MetaspaceAux::allocated_used_bytes(Metaspace::NonClassType), |
ehelin@5703 | 94 | MetaspaceAux::reserved_bytes(Metaspace::NonClassType)); |
sla@5237 | 95 | const MetaspaceSizes class_space( |
ehelin@5389 | 96 | MetaspaceAux::allocated_capacity_bytes(Metaspace::ClassType), |
ehelin@5389 | 97 | MetaspaceAux::allocated_used_bytes(Metaspace::ClassType), |
ehelin@5703 | 98 | MetaspaceAux::reserved_bytes(Metaspace::ClassType)); |
sla@5237 | 99 | |
ehelin@6416 | 100 | return MetaspaceSummary(MetaspaceGC::capacity_until_GC(), meta_space, data_space, class_space); |
sla@5237 | 101 | } |
sla@5237 | 102 | |
sla@5237 | 103 | void CollectedHeap::print_heap_before_gc() { |
sla@5237 | 104 | if (PrintHeapAtGC) { |
sla@5237 | 105 | Universe::print_heap_before_gc(); |
sla@5237 | 106 | } |
sla@5237 | 107 | if (_gc_heap_log != NULL) { |
sla@5237 | 108 | _gc_heap_log->log_heap_before(); |
sla@5237 | 109 | } |
sla@5237 | 110 | } |
sla@5237 | 111 | |
sla@5237 | 112 | void CollectedHeap::print_heap_after_gc() { |
sla@5237 | 113 | if (PrintHeapAtGC) { |
sla@5237 | 114 | Universe::print_heap_after_gc(); |
sla@5237 | 115 | } |
sla@5237 | 116 | if (_gc_heap_log != NULL) { |
sla@5237 | 117 | _gc_heap_log->log_heap_after(); |
sla@5237 | 118 | } |
sla@5237 | 119 | } |
sla@5237 | 120 | |
johnc@5548 | 121 | void CollectedHeap::register_nmethod(nmethod* nm) { |
johnc@5548 | 122 | assert_locked_or_safepoint(CodeCache_lock); |
johnc@5548 | 123 | } |
johnc@5548 | 124 | |
johnc@5548 | 125 | void CollectedHeap::unregister_nmethod(nmethod* nm) { |
johnc@5548 | 126 | assert_locked_or_safepoint(CodeCache_lock); |
johnc@5548 | 127 | } |
johnc@5548 | 128 | |
sla@5237 | 129 | void CollectedHeap::trace_heap(GCWhen::Type when, GCTracer* gc_tracer) { |
sla@5237 | 130 | const GCHeapSummary& heap_summary = create_heap_summary(); |
sla@5237 | 131 | const MetaspaceSummary& metaspace_summary = create_metaspace_summary(); |
sla@5237 | 132 | gc_tracer->report_gc_heap_summary(when, heap_summary, metaspace_summary); |
sla@5237 | 133 | } |
sla@5237 | 134 | |
sla@5237 | 135 | void CollectedHeap::trace_heap_before_gc(GCTracer* gc_tracer) { |
sla@5237 | 136 | trace_heap(GCWhen::BeforeGC, gc_tracer); |
sla@5237 | 137 | } |
sla@5237 | 138 | |
sla@5237 | 139 | void CollectedHeap::trace_heap_after_gc(GCTracer* gc_tracer) { |
sla@5237 | 140 | trace_heap(GCWhen::AfterGC, gc_tracer); |
sla@5237 | 141 | } |
sla@5237 | 142 | |
duke@435 | 143 | // Memory state functions. |
duke@435 | 144 | |
jmasa@2188 | 145 | |
jmasa@2188 | 146 | CollectedHeap::CollectedHeap() : _n_par_threads(0) |
jcoomes@916 | 147 | { |
jcoomes@916 | 148 | const size_t max_len = size_t(arrayOopDesc::max_array_length(T_INT)); |
jcoomes@916 | 149 | const size_t elements_per_word = HeapWordSize / sizeof(jint); |
jcoomes@916 | 150 | _filler_array_max_size = align_object_size(filler_array_hdr_size() + |
brutisso@3668 | 151 | max_len / elements_per_word); |
jcoomes@916 | 152 | |
jcoomes@916 | 153 | _barrier_set = NULL; |
jcoomes@916 | 154 | _is_gc_active = false; |
jcoomes@916 | 155 | _total_collections = _total_full_collections = 0; |
jcoomes@916 | 156 | _gc_cause = _gc_lastcause = GCCause::_no_gc; |
duke@435 | 157 | NOT_PRODUCT(_promotion_failure_alot_count = 0;) |
duke@435 | 158 | NOT_PRODUCT(_promotion_failure_alot_gc_number = 0;) |
duke@435 | 159 | |
duke@435 | 160 | if (UsePerfData) { |
duke@435 | 161 | EXCEPTION_MARK; |
duke@435 | 162 | |
duke@435 | 163 | // create the gc cause jvmstat counters |
duke@435 | 164 | _perf_gc_cause = PerfDataManager::create_string_variable(SUN_GC, "cause", |
duke@435 | 165 | 80, GCCause::to_string(_gc_cause), CHECK); |
duke@435 | 166 | |
duke@435 | 167 | _perf_gc_lastcause = |
duke@435 | 168 | PerfDataManager::create_string_variable(SUN_GC, "lastCause", |
duke@435 | 169 | 80, GCCause::to_string(_gc_lastcause), CHECK); |
duke@435 | 170 | } |
ysr@1601 | 171 | _defer_initial_card_mark = false; // strengthened by subclass in pre_initialize() below. |
never@3499 | 172 | // Create the ring log |
never@3499 | 173 | if (LogEvents) { |
never@3499 | 174 | _gc_heap_log = new GCHeapLog(); |
never@3499 | 175 | } else { |
never@3499 | 176 | _gc_heap_log = NULL; |
never@3499 | 177 | } |
duke@435 | 178 | } |
duke@435 | 179 | |
coleenp@4037 | 180 | // This interface assumes that it's being called by the |
coleenp@4037 | 181 | // vm thread. It collects the heap assuming that the |
coleenp@4037 | 182 | // heap lock is already held and that we are executing in |
coleenp@4037 | 183 | // the context of the vm thread. |
coleenp@4037 | 184 | void CollectedHeap::collect_as_vm_thread(GCCause::Cause cause) { |
coleenp@4037 | 185 | assert(Thread::current()->is_VM_thread(), "Precondition#1"); |
coleenp@4037 | 186 | assert(Heap_lock->is_locked(), "Precondition#2"); |
coleenp@4037 | 187 | GCCauseSetter gcs(this, cause); |
coleenp@4037 | 188 | switch (cause) { |
coleenp@4037 | 189 | case GCCause::_heap_inspection: |
coleenp@4037 | 190 | case GCCause::_heap_dump: |
coleenp@4037 | 191 | case GCCause::_metadata_GC_threshold : { |
coleenp@4037 | 192 | HandleMark hm; |
coleenp@4037 | 193 | do_full_collection(false); // don't clear all soft refs |
coleenp@4037 | 194 | break; |
coleenp@4037 | 195 | } |
coleenp@4037 | 196 | case GCCause::_last_ditch_collection: { |
coleenp@4037 | 197 | HandleMark hm; |
coleenp@4037 | 198 | do_full_collection(true); // do clear all soft refs |
coleenp@4037 | 199 | break; |
coleenp@4037 | 200 | } |
coleenp@4037 | 201 | default: |
coleenp@4037 | 202 | ShouldNotReachHere(); // Unexpected use of this function |
coleenp@4037 | 203 | } |
coleenp@4037 | 204 | } |
coleenp@4037 | 205 | |
ysr@1601 | 206 | void CollectedHeap::pre_initialize() { |
ysr@1601 | 207 | // Used for ReduceInitialCardMarks (when COMPILER2 is used); |
ysr@1601 | 208 | // otherwise remains unused. |
ysr@1903 | 209 | #ifdef COMPILER2 |
ysr@1629 | 210 | _defer_initial_card_mark = ReduceInitialCardMarks && can_elide_tlab_store_barriers() |
ysr@1629 | 211 | && (DeferInitialCardMark || card_mark_must_follow_store()); |
ysr@1601 | 212 | #else |
ysr@1601 | 213 | assert(_defer_initial_card_mark == false, "Who would set it?"); |
ysr@1601 | 214 | #endif |
ysr@1601 | 215 | } |
duke@435 | 216 | |
duke@435 | 217 | #ifndef PRODUCT |
duke@435 | 218 | void CollectedHeap::check_for_bad_heap_word_value(HeapWord* addr, size_t size) { |
duke@435 | 219 | if (CheckMemoryInitialization && ZapUnusedHeapArea) { |
duke@435 | 220 | for (size_t slot = 0; slot < size; slot += 1) { |
duke@435 | 221 | assert((*(intptr_t*) (addr + slot)) != ((intptr_t) badHeapWordVal), |
duke@435 | 222 | "Found badHeapWordValue in post-allocation check"); |
duke@435 | 223 | } |
duke@435 | 224 | } |
duke@435 | 225 | } |
duke@435 | 226 | |
ysr@2533 | 227 | void CollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr, size_t size) { |
duke@435 | 228 | if (CheckMemoryInitialization && ZapUnusedHeapArea) { |
duke@435 | 229 | for (size_t slot = 0; slot < size; slot += 1) { |
duke@435 | 230 | assert((*(intptr_t*) (addr + slot)) == ((intptr_t) badHeapWordVal), |
duke@435 | 231 | "Found non badHeapWordValue in pre-allocation check"); |
duke@435 | 232 | } |
duke@435 | 233 | } |
duke@435 | 234 | } |
duke@435 | 235 | #endif // PRODUCT |
duke@435 | 236 | |
duke@435 | 237 | #ifdef ASSERT |
duke@435 | 238 | void CollectedHeap::check_for_valid_allocation_state() { |
duke@435 | 239 | Thread *thread = Thread::current(); |
duke@435 | 240 | // How to choose between a pending exception and a potential |
duke@435 | 241 | // OutOfMemoryError? Don't allow pending exceptions. |
duke@435 | 242 | // This is a VM policy failure, so how do we exhaustively test it? |
duke@435 | 243 | assert(!thread->has_pending_exception(), |
duke@435 | 244 | "shouldn't be allocating with pending exception"); |
duke@435 | 245 | if (StrictSafepointChecks) { |
duke@435 | 246 | assert(thread->allow_allocation(), |
duke@435 | 247 | "Allocation done by thread for which allocation is blocked " |
duke@435 | 248 | "by No_Allocation_Verifier!"); |
duke@435 | 249 | // Allocation of an oop can always invoke a safepoint, |
duke@435 | 250 | // hence, the true argument |
duke@435 | 251 | thread->check_for_valid_safepoint_state(true); |
duke@435 | 252 | } |
duke@435 | 253 | } |
duke@435 | 254 | #endif |
duke@435 | 255 | |
sla@5237 | 256 | HeapWord* CollectedHeap::allocate_from_tlab_slow(KlassHandle klass, Thread* thread, size_t size) { |
duke@435 | 257 | |
duke@435 | 258 | // Retain tlab and allocate object in shared space if |
duke@435 | 259 | // the amount free in the tlab is too large to discard. |
duke@435 | 260 | if (thread->tlab().free() > thread->tlab().refill_waste_limit()) { |
duke@435 | 261 | thread->tlab().record_slow_allocation(size); |
duke@435 | 262 | return NULL; |
duke@435 | 263 | } |
duke@435 | 264 | |
duke@435 | 265 | // Discard tlab and allocate a new one. |
duke@435 | 266 | // To minimize fragmentation, the last TLAB may be smaller than the rest. |
duke@435 | 267 | size_t new_tlab_size = thread->tlab().compute_size(size); |
duke@435 | 268 | |
duke@435 | 269 | thread->tlab().clear_before_allocation(); |
duke@435 | 270 | |
duke@435 | 271 | if (new_tlab_size == 0) { |
duke@435 | 272 | return NULL; |
duke@435 | 273 | } |
duke@435 | 274 | |
duke@435 | 275 | // Allocate a new TLAB... |
duke@435 | 276 | HeapWord* obj = Universe::heap()->allocate_new_tlab(new_tlab_size); |
duke@435 | 277 | if (obj == NULL) { |
duke@435 | 278 | return NULL; |
duke@435 | 279 | } |
sla@5237 | 280 | |
sla@5237 | 281 | AllocTracer::send_allocation_in_new_tlab_event(klass, new_tlab_size * HeapWordSize, size * HeapWordSize); |
sla@5237 | 282 | |
duke@435 | 283 | if (ZeroTLAB) { |
duke@435 | 284 | // ..and clear it. |
duke@435 | 285 | Copy::zero_to_words(obj, new_tlab_size); |
duke@435 | 286 | } else { |
kvn@3092 | 287 | // ...and zap just allocated object. |
kvn@3092 | 288 | #ifdef ASSERT |
kvn@3092 | 289 | // Skip mangling the space corresponding to the object header to |
kvn@3092 | 290 | // ensure that the returned space is not considered parsable by |
kvn@3092 | 291 | // any concurrent GC thread. |
kvn@3092 | 292 | size_t hdr_size = oopDesc::header_size(); |
kvn@3092 | 293 | Copy::fill_to_words(obj + hdr_size, new_tlab_size - hdr_size, badHeapWordVal); |
kvn@3092 | 294 | #endif // ASSERT |
duke@435 | 295 | } |
duke@435 | 296 | thread->tlab().fill(obj, obj + size, new_tlab_size); |
duke@435 | 297 | return obj; |
duke@435 | 298 | } |
duke@435 | 299 | |
ysr@1462 | 300 | void CollectedHeap::flush_deferred_store_barrier(JavaThread* thread) { |
ysr@1462 | 301 | MemRegion deferred = thread->deferred_card_mark(); |
ysr@1462 | 302 | if (!deferred.is_empty()) { |
ysr@1601 | 303 | assert(_defer_initial_card_mark, "Otherwise should be empty"); |
ysr@1462 | 304 | { |
ysr@1462 | 305 | // Verify that the storage points to a parsable object in heap |
ysr@1462 | 306 | DEBUG_ONLY(oop old_obj = oop(deferred.start());) |
ysr@1462 | 307 | assert(is_in(old_obj), "Not in allocated heap"); |
ysr@1462 | 308 | assert(!can_elide_initializing_store_barrier(old_obj), |
ysr@1601 | 309 | "Else should have been filtered in new_store_pre_barrier()"); |
ysr@1462 | 310 | assert(old_obj->is_oop(true), "Not an oop"); |
ysr@1462 | 311 | assert(deferred.word_size() == (size_t)(old_obj->size()), |
ysr@1462 | 312 | "Mismatch: multiple objects?"); |
ysr@1462 | 313 | } |
ysr@1462 | 314 | BarrierSet* bs = barrier_set(); |
ysr@1462 | 315 | assert(bs->has_write_region_opt(), "No write_region() on BarrierSet"); |
ysr@1462 | 316 | bs->write_region(deferred); |
ysr@1462 | 317 | // "Clear" the deferred_card_mark field |
ysr@1462 | 318 | thread->set_deferred_card_mark(MemRegion()); |
ysr@1462 | 319 | } |
ysr@1462 | 320 | assert(thread->deferred_card_mark().is_empty(), "invariant"); |
ysr@1462 | 321 | } |
ysr@1462 | 322 | |
brutisso@6376 | 323 | size_t CollectedHeap::max_tlab_size() const { |
brutisso@6376 | 324 | // TLABs can't be bigger than we can fill with a int[Integer.MAX_VALUE]. |
brutisso@6376 | 325 | // This restriction could be removed by enabling filling with multiple arrays. |
brutisso@6376 | 326 | // If we compute that the reasonable way as |
brutisso@6376 | 327 | // header_size + ((sizeof(jint) * max_jint) / HeapWordSize) |
brutisso@6376 | 328 | // we'll overflow on the multiply, so we do the divide first. |
brutisso@6376 | 329 | // We actually lose a little by dividing first, |
brutisso@6376 | 330 | // but that just makes the TLAB somewhat smaller than the biggest array, |
brutisso@6376 | 331 | // which is fine, since we'll be able to fill that. |
brutisso@6376 | 332 | size_t max_int_size = typeArrayOopDesc::header_size(T_INT) + |
brutisso@6376 | 333 | sizeof(jint) * |
brutisso@6376 | 334 | ((juint) max_jint / (size_t) HeapWordSize); |
brutisso@6376 | 335 | return align_size_down(max_int_size, MinObjAlignment); |
brutisso@6376 | 336 | } |
brutisso@6376 | 337 | |
ysr@1462 | 338 | // Helper for ReduceInitialCardMarks. For performance, |
ysr@1462 | 339 | // compiled code may elide card-marks for initializing stores |
ysr@1462 | 340 | // to a newly allocated object along the fast-path. We |
ysr@1462 | 341 | // compensate for such elided card-marks as follows: |
ysr@1462 | 342 | // (a) Generational, non-concurrent collectors, such as |
ysr@1462 | 343 | // GenCollectedHeap(ParNew,DefNew,Tenured) and |
ysr@1462 | 344 | // ParallelScavengeHeap(ParallelGC, ParallelOldGC) |
ysr@1462 | 345 | // need the card-mark if and only if the region is |
ysr@1462 | 346 | // in the old gen, and do not care if the card-mark |
ysr@1462 | 347 | // succeeds or precedes the initializing stores themselves, |
ysr@1462 | 348 | // so long as the card-mark is completed before the next |
ysr@1462 | 349 | // scavenge. For all these cases, we can do a card mark |
ysr@1462 | 350 | // at the point at which we do a slow path allocation |
ysr@1601 | 351 | // in the old gen, i.e. in this call. |
ysr@1462 | 352 | // (b) GenCollectedHeap(ConcurrentMarkSweepGeneration) requires |
ysr@1462 | 353 | // in addition that the card-mark for an old gen allocated |
ysr@1462 | 354 | // object strictly follow any associated initializing stores. |
ysr@1462 | 355 | // In these cases, the memRegion remembered below is |
ysr@1462 | 356 | // used to card-mark the entire region either just before the next |
ysr@1462 | 357 | // slow-path allocation by this thread or just before the next scavenge or |
ysr@1462 | 358 | // CMS-associated safepoint, whichever of these events happens first. |
ysr@1462 | 359 | // (The implicit assumption is that the object has been fully |
ysr@1462 | 360 | // initialized by this point, a fact that we assert when doing the |
ysr@1462 | 361 | // card-mark.) |
ysr@1462 | 362 | // (c) G1CollectedHeap(G1) uses two kinds of write barriers. When a |
ysr@1462 | 363 | // G1 concurrent marking is in progress an SATB (pre-write-)barrier is |
ysr@1462 | 364 | // is used to remember the pre-value of any store. Initializing |
ysr@1462 | 365 | // stores will not need this barrier, so we need not worry about |
ysr@1462 | 366 | // compensating for the missing pre-barrier here. Turning now |
ysr@1462 | 367 | // to the post-barrier, we note that G1 needs a RS update barrier |
ysr@1462 | 368 | // which simply enqueues a (sequence of) dirty cards which may |
ysr@1462 | 369 | // optionally be refined by the concurrent update threads. Note |
ysr@1462 | 370 | // that this barrier need only be applied to a non-young write, |
ysr@1462 | 371 | // but, like in CMS, because of the presence of concurrent refinement |
ysr@1462 | 372 | // (much like CMS' precleaning), must strictly follow the oop-store. |
ysr@1462 | 373 | // Thus, using the same protocol for maintaining the intended |
ysr@1601 | 374 | // invariants turns out, serendepitously, to be the same for both |
ysr@1601 | 375 | // G1 and CMS. |
ysr@1462 | 376 | // |
ysr@1601 | 377 | // For any future collector, this code should be reexamined with |
ysr@1601 | 378 | // that specific collector in mind, and the documentation above suitably |
ysr@1601 | 379 | // extended and updated. |
ysr@1601 | 380 | oop CollectedHeap::new_store_pre_barrier(JavaThread* thread, oop new_obj) { |
ysr@1462 | 381 | // If a previous card-mark was deferred, flush it now. |
ysr@1462 | 382 | flush_deferred_store_barrier(thread); |
ysr@1462 | 383 | if (can_elide_initializing_store_barrier(new_obj)) { |
ysr@1462 | 384 | // The deferred_card_mark region should be empty |
ysr@1462 | 385 | // following the flush above. |
ysr@1462 | 386 | assert(thread->deferred_card_mark().is_empty(), "Error"); |
ysr@1462 | 387 | } else { |
ysr@1601 | 388 | MemRegion mr((HeapWord*)new_obj, new_obj->size()); |
ysr@1601 | 389 | assert(!mr.is_empty(), "Error"); |
ysr@1601 | 390 | if (_defer_initial_card_mark) { |
ysr@1601 | 391 | // Defer the card mark |
ysr@1601 | 392 | thread->set_deferred_card_mark(mr); |
ysr@1601 | 393 | } else { |
ysr@1601 | 394 | // Do the card mark |
ysr@1601 | 395 | BarrierSet* bs = barrier_set(); |
ysr@1601 | 396 | assert(bs->has_write_region_opt(), "No write_region() on BarrierSet"); |
ysr@1601 | 397 | bs->write_region(mr); |
ysr@1601 | 398 | } |
ysr@1462 | 399 | } |
ysr@1462 | 400 | return new_obj; |
ysr@1462 | 401 | } |
ysr@1462 | 402 | |
jcoomes@916 | 403 | size_t CollectedHeap::filler_array_hdr_size() { |
kvn@1926 | 404 | return size_t(align_object_offset(arrayOopDesc::header_size(T_INT))); // align to Long |
jcoomes@916 | 405 | } |
jcoomes@916 | 406 | |
jcoomes@916 | 407 | size_t CollectedHeap::filler_array_min_size() { |
kvn@1926 | 408 | return align_object_size(filler_array_hdr_size()); // align to MinObjAlignment |
jcoomes@916 | 409 | } |
jcoomes@916 | 410 | |
jcoomes@916 | 411 | #ifdef ASSERT |
jcoomes@916 | 412 | void CollectedHeap::fill_args_check(HeapWord* start, size_t words) |
jcoomes@916 | 413 | { |
jcoomes@916 | 414 | assert(words >= min_fill_size(), "too small to fill"); |
jcoomes@916 | 415 | assert(words % MinObjAlignment == 0, "unaligned size"); |
jcoomes@916 | 416 | assert(Universe::heap()->is_in_reserved(start), "not in heap"); |
jcoomes@916 | 417 | assert(Universe::heap()->is_in_reserved(start + words - 1), "not in heap"); |
jcoomes@916 | 418 | } |
jcoomes@916 | 419 | |
johnc@1600 | 420 | void CollectedHeap::zap_filler_array(HeapWord* start, size_t words, bool zap) |
jcoomes@916 | 421 | { |
johnc@1600 | 422 | if (ZapFillerObjects && zap) { |
jcoomes@916 | 423 | Copy::fill_to_words(start + filler_array_hdr_size(), |
jcoomes@916 | 424 | words - filler_array_hdr_size(), 0XDEAFBABE); |
jcoomes@916 | 425 | } |
jcoomes@916 | 426 | } |
jcoomes@916 | 427 | #endif // ASSERT |
jcoomes@916 | 428 | |
jcoomes@916 | 429 | void |
johnc@1600 | 430 | CollectedHeap::fill_with_array(HeapWord* start, size_t words, bool zap) |
jcoomes@916 | 431 | { |
jcoomes@916 | 432 | assert(words >= filler_array_min_size(), "too small for an array"); |
jcoomes@916 | 433 | assert(words <= filler_array_max_size(), "too big for a single object"); |
jcoomes@916 | 434 | |
jcoomes@916 | 435 | const size_t payload_size = words - filler_array_hdr_size(); |
jcoomes@916 | 436 | const size_t len = payload_size * HeapWordSize / sizeof(jint); |
brutisso@3668 | 437 | assert((int)len >= 0, err_msg("size too large " SIZE_FORMAT " becomes %d", words, (int)len)); |
jcoomes@916 | 438 | |
jcoomes@916 | 439 | // Set the length first for concurrent GC. |
jcoomes@916 | 440 | ((arrayOop)start)->set_length((int)len); |
brutisso@3675 | 441 | post_allocation_setup_common(Universe::intArrayKlassObj(), start); |
johnc@1600 | 442 | DEBUG_ONLY(zap_filler_array(start, words, zap);) |
jcoomes@916 | 443 | } |
jcoomes@916 | 444 | |
jcoomes@916 | 445 | void |
johnc@1600 | 446 | CollectedHeap::fill_with_object_impl(HeapWord* start, size_t words, bool zap) |
jcoomes@916 | 447 | { |
jcoomes@916 | 448 | assert(words <= filler_array_max_size(), "too big for a single object"); |
jcoomes@916 | 449 | |
jcoomes@916 | 450 | if (words >= filler_array_min_size()) { |
johnc@1600 | 451 | fill_with_array(start, words, zap); |
jcoomes@916 | 452 | } else if (words > 0) { |
jcoomes@916 | 453 | assert(words == min_fill_size(), "unaligned size"); |
brutisso@3675 | 454 | post_allocation_setup_common(SystemDictionary::Object_klass(), start); |
jcoomes@916 | 455 | } |
jcoomes@916 | 456 | } |
jcoomes@916 | 457 | |
johnc@1600 | 458 | void CollectedHeap::fill_with_object(HeapWord* start, size_t words, bool zap) |
jcoomes@916 | 459 | { |
jcoomes@916 | 460 | DEBUG_ONLY(fill_args_check(start, words);) |
jcoomes@916 | 461 | HandleMark hm; // Free handles before leaving. |
johnc@1600 | 462 | fill_with_object_impl(start, words, zap); |
jcoomes@916 | 463 | } |
jcoomes@916 | 464 | |
johnc@1600 | 465 | void CollectedHeap::fill_with_objects(HeapWord* start, size_t words, bool zap) |
jcoomes@916 | 466 | { |
jcoomes@916 | 467 | DEBUG_ONLY(fill_args_check(start, words);) |
jcoomes@916 | 468 | HandleMark hm; // Free handles before leaving. |
jcoomes@916 | 469 | |
ysr@1904 | 470 | #ifdef _LP64 |
jcoomes@916 | 471 | // A single array can fill ~8G, so multiple objects are needed only in 64-bit. |
jcoomes@916 | 472 | // First fill with arrays, ensuring that any remaining space is big enough to |
jcoomes@916 | 473 | // fill. The remainder is filled with a single object. |
jcoomes@916 | 474 | const size_t min = min_fill_size(); |
jcoomes@916 | 475 | const size_t max = filler_array_max_size(); |
jcoomes@916 | 476 | while (words > max) { |
jcoomes@916 | 477 | const size_t cur = words - max >= min ? max : max - min; |
johnc@1600 | 478 | fill_with_array(start, cur, zap); |
jcoomes@916 | 479 | start += cur; |
jcoomes@916 | 480 | words -= cur; |
jcoomes@916 | 481 | } |
jcoomes@916 | 482 | #endif |
jcoomes@916 | 483 | |
johnc@1600 | 484 | fill_with_object_impl(start, words, zap); |
jcoomes@916 | 485 | } |
jcoomes@916 | 486 | |
jwilhelm@6085 | 487 | void CollectedHeap::post_initialize() { |
jwilhelm@6085 | 488 | collector_policy()->post_heap_initialize(); |
jwilhelm@6085 | 489 | } |
jwilhelm@6085 | 490 | |
duke@435 | 491 | HeapWord* CollectedHeap::allocate_new_tlab(size_t size) { |
duke@435 | 492 | guarantee(false, "thread-local allocation buffers not supported"); |
duke@435 | 493 | return NULL; |
duke@435 | 494 | } |
duke@435 | 495 | |
duke@435 | 496 | void CollectedHeap::ensure_parsability(bool retire_tlabs) { |
duke@435 | 497 | // The second disjunct in the assertion below makes a concession |
duke@435 | 498 | // for the start-up verification done while the VM is being |
duke@435 | 499 | // created. Callers be careful that you know that mutators |
duke@435 | 500 | // aren't going to interfere -- for instance, this is permissible |
duke@435 | 501 | // if we are still single-threaded and have either not yet |
duke@435 | 502 | // started allocating (nothing much to verify) or we have |
duke@435 | 503 | // started allocating but are now a full-fledged JavaThread |
duke@435 | 504 | // (and have thus made our TLAB's) available for filling. |
duke@435 | 505 | assert(SafepointSynchronize::is_at_safepoint() || |
duke@435 | 506 | !is_init_completed(), |
duke@435 | 507 | "Should only be called at a safepoint or at start-up" |
duke@435 | 508 | " otherwise concurrent mutator activity may make heap " |
duke@435 | 509 | " unparsable again"); |
ysr@1601 | 510 | const bool use_tlab = UseTLAB; |
ysr@1601 | 511 | const bool deferred = _defer_initial_card_mark; |
ysr@1601 | 512 | // The main thread starts allocating via a TLAB even before it |
ysr@1601 | 513 | // has added itself to the threads list at vm boot-up. |
ysr@1601 | 514 | assert(!use_tlab || Threads::first() != NULL, |
ysr@1601 | 515 | "Attempt to fill tlabs before main thread has been added" |
ysr@1601 | 516 | " to threads list is doomed to failure!"); |
ysr@1601 | 517 | for (JavaThread *thread = Threads::first(); thread; thread = thread->next()) { |
ysr@1601 | 518 | if (use_tlab) thread->tlab().make_parsable(retire_tlabs); |
ysr@1601 | 519 | #ifdef COMPILER2 |
ysr@1601 | 520 | // The deferred store barriers must all have been flushed to the |
ysr@1601 | 521 | // card-table (or other remembered set structure) before GC starts |
ysr@1601 | 522 | // processing the card-table (or other remembered set). |
ysr@1601 | 523 | if (deferred) flush_deferred_store_barrier(thread); |
ysr@1601 | 524 | #else |
ysr@1601 | 525 | assert(!deferred, "Should be false"); |
ysr@1601 | 526 | assert(thread->deferred_card_mark().is_empty(), "Should be empty"); |
ysr@1601 | 527 | #endif |
duke@435 | 528 | } |
duke@435 | 529 | } |
duke@435 | 530 | |
duke@435 | 531 | void CollectedHeap::accumulate_statistics_all_tlabs() { |
duke@435 | 532 | if (UseTLAB) { |
duke@435 | 533 | assert(SafepointSynchronize::is_at_safepoint() || |
duke@435 | 534 | !is_init_completed(), |
duke@435 | 535 | "should only accumulate statistics on tlabs at safepoint"); |
duke@435 | 536 | |
duke@435 | 537 | ThreadLocalAllocBuffer::accumulate_statistics_before_gc(); |
duke@435 | 538 | } |
duke@435 | 539 | } |
duke@435 | 540 | |
duke@435 | 541 | void CollectedHeap::resize_all_tlabs() { |
duke@435 | 542 | if (UseTLAB) { |
duke@435 | 543 | assert(SafepointSynchronize::is_at_safepoint() || |
duke@435 | 544 | !is_init_completed(), |
duke@435 | 545 | "should only resize tlabs at safepoint"); |
duke@435 | 546 | |
duke@435 | 547 | ThreadLocalAllocBuffer::resize_all_tlabs(); |
duke@435 | 548 | } |
duke@435 | 549 | } |
ysr@1050 | 550 | |
sla@5237 | 551 | void CollectedHeap::pre_full_gc_dump(GCTimer* timer) { |
ysr@1050 | 552 | if (HeapDumpBeforeFullGC) { |
sla@5237 | 553 | GCTraceTime tt("Heap Dump (before full gc): ", PrintGCDetails, false, timer); |
ysr@1050 | 554 | // We are doing a "major" collection and a heap dump before |
ysr@1050 | 555 | // major collection has been requested. |
ysr@1050 | 556 | HeapDumper::dump_heap(); |
ysr@1050 | 557 | } |
ysr@1050 | 558 | if (PrintClassHistogramBeforeFullGC) { |
sla@5237 | 559 | GCTraceTime tt("Class Histogram (before full gc): ", PrintGCDetails, true, timer); |
sla@5237 | 560 | VM_GC_HeapInspection inspector(gclog_or_tty, false /* ! full gc */); |
ysr@1050 | 561 | inspector.doit(); |
ysr@1050 | 562 | } |
ysr@1050 | 563 | } |
ysr@1050 | 564 | |
sla@5237 | 565 | void CollectedHeap::post_full_gc_dump(GCTimer* timer) { |
ysr@1050 | 566 | if (HeapDumpAfterFullGC) { |
sla@5237 | 567 | GCTraceTime tt("Heap Dump (after full gc): ", PrintGCDetails, false, timer); |
ysr@1050 | 568 | HeapDumper::dump_heap(); |
ysr@1050 | 569 | } |
ysr@1050 | 570 | if (PrintClassHistogramAfterFullGC) { |
sla@5237 | 571 | GCTraceTime tt("Class Histogram (after full gc): ", PrintGCDetails, true, timer); |
sla@5237 | 572 | VM_GC_HeapInspection inspector(gclog_or_tty, false /* ! full gc */); |
ysr@1050 | 573 | inspector.doit(); |
ysr@1050 | 574 | } |
ysr@1050 | 575 | } |
never@3205 | 576 | |
never@3205 | 577 | oop CollectedHeap::Class_obj_allocate(KlassHandle klass, int size, KlassHandle real_klass, TRAPS) { |
never@3205 | 578 | debug_only(check_for_valid_allocation_state()); |
never@3205 | 579 | assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed"); |
never@3205 | 580 | assert(size >= 0, "int won't convert to size_t"); |
never@3205 | 581 | HeapWord* obj; |
never@3205 | 582 | assert(ScavengeRootsInCode > 0, "must be"); |
sla@5237 | 583 | obj = common_mem_allocate_init(real_klass, size, CHECK_NULL); |
brutisso@3675 | 584 | post_allocation_setup_common(klass, obj); |
never@3205 | 585 | assert(Universe::is_bootstrapping() || |
coleenp@4037 | 586 | !((oop)obj)->is_array(), "must not be an array"); |
never@3205 | 587 | NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size)); |
never@3205 | 588 | oop mirror = (oop)obj; |
never@3205 | 589 | |
never@3205 | 590 | java_lang_Class::set_oop_size(mirror, size); |
never@3205 | 591 | |
never@3205 | 592 | // Setup indirections |
never@3205 | 593 | if (!real_klass.is_null()) { |
never@3205 | 594 | java_lang_Class::set_klass(mirror, real_klass()); |
never@3205 | 595 | real_klass->set_java_mirror(mirror); |
never@3205 | 596 | } |
never@3205 | 597 | |
coleenp@4047 | 598 | InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass()); |
never@3205 | 599 | assert(size == mk->instance_size(real_klass), "should have been set"); |
never@3205 | 600 | |
never@3205 | 601 | // notify jvmti and dtrace |
never@3205 | 602 | post_allocation_notify(klass, (oop)obj); |
never@3205 | 603 | |
never@3205 | 604 | return mirror; |
never@3205 | 605 | } |
stefank@3335 | 606 | |
stefank@3335 | 607 | /////////////// Unit tests /////////////// |
stefank@3335 | 608 | |
stefank@3335 | 609 | #ifndef PRODUCT |
stefank@3335 | 610 | void CollectedHeap::test_is_in() { |
stefank@3335 | 611 | CollectedHeap* heap = Universe::heap(); |
stefank@3335 | 612 | |
stefank@3375 | 613 | uintptr_t epsilon = (uintptr_t) MinObjAlignment; |
stefank@3375 | 614 | uintptr_t heap_start = (uintptr_t) heap->_reserved.start(); |
stefank@3375 | 615 | uintptr_t heap_end = (uintptr_t) heap->_reserved.end(); |
stefank@3375 | 616 | |
stefank@3335 | 617 | // Test that NULL is not in the heap. |
stefank@3335 | 618 | assert(!heap->is_in(NULL), "NULL is unexpectedly in the heap"); |
stefank@3335 | 619 | |
stefank@3335 | 620 | // Test that a pointer to before the heap start is reported as outside the heap. |
stefank@3375 | 621 | assert(heap_start >= ((uintptr_t)NULL + epsilon), "sanity"); |
stefank@3375 | 622 | void* before_heap = (void*)(heap_start - epsilon); |
stefank@3335 | 623 | assert(!heap->is_in(before_heap), |
stefank@3335 | 624 | err_msg("before_heap: " PTR_FORMAT " is unexpectedly in the heap", before_heap)); |
stefank@3335 | 625 | |
stefank@3335 | 626 | // Test that a pointer to after the heap end is reported as outside the heap. |
stefank@3375 | 627 | assert(heap_end <= ((uintptr_t)-1 - epsilon), "sanity"); |
stefank@3375 | 628 | void* after_heap = (void*)(heap_end + epsilon); |
stefank@3335 | 629 | assert(!heap->is_in(after_heap), |
stefank@3335 | 630 | err_msg("after_heap: " PTR_FORMAT " is unexpectedly in the heap", after_heap)); |
stefank@3335 | 631 | } |
stefank@3335 | 632 | #endif |