src/share/vm/gc_interface/collectedHeap.cpp

Thu, 21 Nov 2013 12:30:35 -0800

author
kvn
date
Thu, 21 Nov 2013 12:30:35 -0800
changeset 6485
da862781b584
parent 6085
8f07aa079343
child 6376
cfd4aac53239
permissions
-rw-r--r--

Merge

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
sla@5237 100 return MetaspaceSummary(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
ysr@1462 323 // Helper for ReduceInitialCardMarks. For performance,
ysr@1462 324 // compiled code may elide card-marks for initializing stores
ysr@1462 325 // to a newly allocated object along the fast-path. We
ysr@1462 326 // compensate for such elided card-marks as follows:
ysr@1462 327 // (a) Generational, non-concurrent collectors, such as
ysr@1462 328 // GenCollectedHeap(ParNew,DefNew,Tenured) and
ysr@1462 329 // ParallelScavengeHeap(ParallelGC, ParallelOldGC)
ysr@1462 330 // need the card-mark if and only if the region is
ysr@1462 331 // in the old gen, and do not care if the card-mark
ysr@1462 332 // succeeds or precedes the initializing stores themselves,
ysr@1462 333 // so long as the card-mark is completed before the next
ysr@1462 334 // scavenge. For all these cases, we can do a card mark
ysr@1462 335 // at the point at which we do a slow path allocation
ysr@1601 336 // in the old gen, i.e. in this call.
ysr@1462 337 // (b) GenCollectedHeap(ConcurrentMarkSweepGeneration) requires
ysr@1462 338 // in addition that the card-mark for an old gen allocated
ysr@1462 339 // object strictly follow any associated initializing stores.
ysr@1462 340 // In these cases, the memRegion remembered below is
ysr@1462 341 // used to card-mark the entire region either just before the next
ysr@1462 342 // slow-path allocation by this thread or just before the next scavenge or
ysr@1462 343 // CMS-associated safepoint, whichever of these events happens first.
ysr@1462 344 // (The implicit assumption is that the object has been fully
ysr@1462 345 // initialized by this point, a fact that we assert when doing the
ysr@1462 346 // card-mark.)
ysr@1462 347 // (c) G1CollectedHeap(G1) uses two kinds of write barriers. When a
ysr@1462 348 // G1 concurrent marking is in progress an SATB (pre-write-)barrier is
ysr@1462 349 // is used to remember the pre-value of any store. Initializing
ysr@1462 350 // stores will not need this barrier, so we need not worry about
ysr@1462 351 // compensating for the missing pre-barrier here. Turning now
ysr@1462 352 // to the post-barrier, we note that G1 needs a RS update barrier
ysr@1462 353 // which simply enqueues a (sequence of) dirty cards which may
ysr@1462 354 // optionally be refined by the concurrent update threads. Note
ysr@1462 355 // that this barrier need only be applied to a non-young write,
ysr@1462 356 // but, like in CMS, because of the presence of concurrent refinement
ysr@1462 357 // (much like CMS' precleaning), must strictly follow the oop-store.
ysr@1462 358 // Thus, using the same protocol for maintaining the intended
ysr@1601 359 // invariants turns out, serendepitously, to be the same for both
ysr@1601 360 // G1 and CMS.
ysr@1462 361 //
ysr@1601 362 // For any future collector, this code should be reexamined with
ysr@1601 363 // that specific collector in mind, and the documentation above suitably
ysr@1601 364 // extended and updated.
ysr@1601 365 oop CollectedHeap::new_store_pre_barrier(JavaThread* thread, oop new_obj) {
ysr@1462 366 // If a previous card-mark was deferred, flush it now.
ysr@1462 367 flush_deferred_store_barrier(thread);
ysr@1462 368 if (can_elide_initializing_store_barrier(new_obj)) {
ysr@1462 369 // The deferred_card_mark region should be empty
ysr@1462 370 // following the flush above.
ysr@1462 371 assert(thread->deferred_card_mark().is_empty(), "Error");
ysr@1462 372 } else {
ysr@1601 373 MemRegion mr((HeapWord*)new_obj, new_obj->size());
ysr@1601 374 assert(!mr.is_empty(), "Error");
ysr@1601 375 if (_defer_initial_card_mark) {
ysr@1601 376 // Defer the card mark
ysr@1601 377 thread->set_deferred_card_mark(mr);
ysr@1601 378 } else {
ysr@1601 379 // Do the card mark
ysr@1601 380 BarrierSet* bs = barrier_set();
ysr@1601 381 assert(bs->has_write_region_opt(), "No write_region() on BarrierSet");
ysr@1601 382 bs->write_region(mr);
ysr@1601 383 }
ysr@1462 384 }
ysr@1462 385 return new_obj;
ysr@1462 386 }
ysr@1462 387
jcoomes@916 388 size_t CollectedHeap::filler_array_hdr_size() {
kvn@1926 389 return size_t(align_object_offset(arrayOopDesc::header_size(T_INT))); // align to Long
jcoomes@916 390 }
jcoomes@916 391
jcoomes@916 392 size_t CollectedHeap::filler_array_min_size() {
kvn@1926 393 return align_object_size(filler_array_hdr_size()); // align to MinObjAlignment
jcoomes@916 394 }
jcoomes@916 395
jcoomes@916 396 #ifdef ASSERT
jcoomes@916 397 void CollectedHeap::fill_args_check(HeapWord* start, size_t words)
jcoomes@916 398 {
jcoomes@916 399 assert(words >= min_fill_size(), "too small to fill");
jcoomes@916 400 assert(words % MinObjAlignment == 0, "unaligned size");
jcoomes@916 401 assert(Universe::heap()->is_in_reserved(start), "not in heap");
jcoomes@916 402 assert(Universe::heap()->is_in_reserved(start + words - 1), "not in heap");
jcoomes@916 403 }
jcoomes@916 404
johnc@1600 405 void CollectedHeap::zap_filler_array(HeapWord* start, size_t words, bool zap)
jcoomes@916 406 {
johnc@1600 407 if (ZapFillerObjects && zap) {
jcoomes@916 408 Copy::fill_to_words(start + filler_array_hdr_size(),
jcoomes@916 409 words - filler_array_hdr_size(), 0XDEAFBABE);
jcoomes@916 410 }
jcoomes@916 411 }
jcoomes@916 412 #endif // ASSERT
jcoomes@916 413
jcoomes@916 414 void
johnc@1600 415 CollectedHeap::fill_with_array(HeapWord* start, size_t words, bool zap)
jcoomes@916 416 {
jcoomes@916 417 assert(words >= filler_array_min_size(), "too small for an array");
jcoomes@916 418 assert(words <= filler_array_max_size(), "too big for a single object");
jcoomes@916 419
jcoomes@916 420 const size_t payload_size = words - filler_array_hdr_size();
jcoomes@916 421 const size_t len = payload_size * HeapWordSize / sizeof(jint);
brutisso@3668 422 assert((int)len >= 0, err_msg("size too large " SIZE_FORMAT " becomes %d", words, (int)len));
jcoomes@916 423
jcoomes@916 424 // Set the length first for concurrent GC.
jcoomes@916 425 ((arrayOop)start)->set_length((int)len);
brutisso@3675 426 post_allocation_setup_common(Universe::intArrayKlassObj(), start);
johnc@1600 427 DEBUG_ONLY(zap_filler_array(start, words, zap);)
jcoomes@916 428 }
jcoomes@916 429
jcoomes@916 430 void
johnc@1600 431 CollectedHeap::fill_with_object_impl(HeapWord* start, size_t words, bool zap)
jcoomes@916 432 {
jcoomes@916 433 assert(words <= filler_array_max_size(), "too big for a single object");
jcoomes@916 434
jcoomes@916 435 if (words >= filler_array_min_size()) {
johnc@1600 436 fill_with_array(start, words, zap);
jcoomes@916 437 } else if (words > 0) {
jcoomes@916 438 assert(words == min_fill_size(), "unaligned size");
brutisso@3675 439 post_allocation_setup_common(SystemDictionary::Object_klass(), start);
jcoomes@916 440 }
jcoomes@916 441 }
jcoomes@916 442
johnc@1600 443 void CollectedHeap::fill_with_object(HeapWord* start, size_t words, bool zap)
jcoomes@916 444 {
jcoomes@916 445 DEBUG_ONLY(fill_args_check(start, words);)
jcoomes@916 446 HandleMark hm; // Free handles before leaving.
johnc@1600 447 fill_with_object_impl(start, words, zap);
jcoomes@916 448 }
jcoomes@916 449
johnc@1600 450 void CollectedHeap::fill_with_objects(HeapWord* start, size_t words, bool zap)
jcoomes@916 451 {
jcoomes@916 452 DEBUG_ONLY(fill_args_check(start, words);)
jcoomes@916 453 HandleMark hm; // Free handles before leaving.
jcoomes@916 454
ysr@1904 455 #ifdef _LP64
jcoomes@916 456 // A single array can fill ~8G, so multiple objects are needed only in 64-bit.
jcoomes@916 457 // First fill with arrays, ensuring that any remaining space is big enough to
jcoomes@916 458 // fill. The remainder is filled with a single object.
jcoomes@916 459 const size_t min = min_fill_size();
jcoomes@916 460 const size_t max = filler_array_max_size();
jcoomes@916 461 while (words > max) {
jcoomes@916 462 const size_t cur = words - max >= min ? max : max - min;
johnc@1600 463 fill_with_array(start, cur, zap);
jcoomes@916 464 start += cur;
jcoomes@916 465 words -= cur;
jcoomes@916 466 }
jcoomes@916 467 #endif
jcoomes@916 468
johnc@1600 469 fill_with_object_impl(start, words, zap);
jcoomes@916 470 }
jcoomes@916 471
jwilhelm@6085 472 void CollectedHeap::post_initialize() {
jwilhelm@6085 473 collector_policy()->post_heap_initialize();
jwilhelm@6085 474 }
jwilhelm@6085 475
duke@435 476 HeapWord* CollectedHeap::allocate_new_tlab(size_t size) {
duke@435 477 guarantee(false, "thread-local allocation buffers not supported");
duke@435 478 return NULL;
duke@435 479 }
duke@435 480
duke@435 481 void CollectedHeap::ensure_parsability(bool retire_tlabs) {
duke@435 482 // The second disjunct in the assertion below makes a concession
duke@435 483 // for the start-up verification done while the VM is being
duke@435 484 // created. Callers be careful that you know that mutators
duke@435 485 // aren't going to interfere -- for instance, this is permissible
duke@435 486 // if we are still single-threaded and have either not yet
duke@435 487 // started allocating (nothing much to verify) or we have
duke@435 488 // started allocating but are now a full-fledged JavaThread
duke@435 489 // (and have thus made our TLAB's) available for filling.
duke@435 490 assert(SafepointSynchronize::is_at_safepoint() ||
duke@435 491 !is_init_completed(),
duke@435 492 "Should only be called at a safepoint or at start-up"
duke@435 493 " otherwise concurrent mutator activity may make heap "
duke@435 494 " unparsable again");
ysr@1601 495 const bool use_tlab = UseTLAB;
ysr@1601 496 const bool deferred = _defer_initial_card_mark;
ysr@1601 497 // The main thread starts allocating via a TLAB even before it
ysr@1601 498 // has added itself to the threads list at vm boot-up.
ysr@1601 499 assert(!use_tlab || Threads::first() != NULL,
ysr@1601 500 "Attempt to fill tlabs before main thread has been added"
ysr@1601 501 " to threads list is doomed to failure!");
ysr@1601 502 for (JavaThread *thread = Threads::first(); thread; thread = thread->next()) {
ysr@1601 503 if (use_tlab) thread->tlab().make_parsable(retire_tlabs);
ysr@1601 504 #ifdef COMPILER2
ysr@1601 505 // The deferred store barriers must all have been flushed to the
ysr@1601 506 // card-table (or other remembered set structure) before GC starts
ysr@1601 507 // processing the card-table (or other remembered set).
ysr@1601 508 if (deferred) flush_deferred_store_barrier(thread);
ysr@1601 509 #else
ysr@1601 510 assert(!deferred, "Should be false");
ysr@1601 511 assert(thread->deferred_card_mark().is_empty(), "Should be empty");
ysr@1601 512 #endif
duke@435 513 }
duke@435 514 }
duke@435 515
duke@435 516 void CollectedHeap::accumulate_statistics_all_tlabs() {
duke@435 517 if (UseTLAB) {
duke@435 518 assert(SafepointSynchronize::is_at_safepoint() ||
duke@435 519 !is_init_completed(),
duke@435 520 "should only accumulate statistics on tlabs at safepoint");
duke@435 521
duke@435 522 ThreadLocalAllocBuffer::accumulate_statistics_before_gc();
duke@435 523 }
duke@435 524 }
duke@435 525
duke@435 526 void CollectedHeap::resize_all_tlabs() {
duke@435 527 if (UseTLAB) {
duke@435 528 assert(SafepointSynchronize::is_at_safepoint() ||
duke@435 529 !is_init_completed(),
duke@435 530 "should only resize tlabs at safepoint");
duke@435 531
duke@435 532 ThreadLocalAllocBuffer::resize_all_tlabs();
duke@435 533 }
duke@435 534 }
ysr@1050 535
sla@5237 536 void CollectedHeap::pre_full_gc_dump(GCTimer* timer) {
ysr@1050 537 if (HeapDumpBeforeFullGC) {
sla@5237 538 GCTraceTime tt("Heap Dump (before full gc): ", PrintGCDetails, false, timer);
ysr@1050 539 // We are doing a "major" collection and a heap dump before
ysr@1050 540 // major collection has been requested.
ysr@1050 541 HeapDumper::dump_heap();
ysr@1050 542 }
ysr@1050 543 if (PrintClassHistogramBeforeFullGC) {
sla@5237 544 GCTraceTime tt("Class Histogram (before full gc): ", PrintGCDetails, true, timer);
sla@5237 545 VM_GC_HeapInspection inspector(gclog_or_tty, false /* ! full gc */);
ysr@1050 546 inspector.doit();
ysr@1050 547 }
ysr@1050 548 }
ysr@1050 549
sla@5237 550 void CollectedHeap::post_full_gc_dump(GCTimer* timer) {
ysr@1050 551 if (HeapDumpAfterFullGC) {
sla@5237 552 GCTraceTime tt("Heap Dump (after full gc): ", PrintGCDetails, false, timer);
ysr@1050 553 HeapDumper::dump_heap();
ysr@1050 554 }
ysr@1050 555 if (PrintClassHistogramAfterFullGC) {
sla@5237 556 GCTraceTime tt("Class Histogram (after full gc): ", PrintGCDetails, true, timer);
sla@5237 557 VM_GC_HeapInspection inspector(gclog_or_tty, false /* ! full gc */);
ysr@1050 558 inspector.doit();
ysr@1050 559 }
ysr@1050 560 }
never@3205 561
never@3205 562 oop CollectedHeap::Class_obj_allocate(KlassHandle klass, int size, KlassHandle real_klass, TRAPS) {
never@3205 563 debug_only(check_for_valid_allocation_state());
never@3205 564 assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
never@3205 565 assert(size >= 0, "int won't convert to size_t");
never@3205 566 HeapWord* obj;
never@3205 567 assert(ScavengeRootsInCode > 0, "must be");
sla@5237 568 obj = common_mem_allocate_init(real_klass, size, CHECK_NULL);
brutisso@3675 569 post_allocation_setup_common(klass, obj);
never@3205 570 assert(Universe::is_bootstrapping() ||
coleenp@4037 571 !((oop)obj)->is_array(), "must not be an array");
never@3205 572 NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
never@3205 573 oop mirror = (oop)obj;
never@3205 574
never@3205 575 java_lang_Class::set_oop_size(mirror, size);
never@3205 576
never@3205 577 // Setup indirections
never@3205 578 if (!real_klass.is_null()) {
never@3205 579 java_lang_Class::set_klass(mirror, real_klass());
never@3205 580 real_klass->set_java_mirror(mirror);
never@3205 581 }
never@3205 582
coleenp@4047 583 InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
never@3205 584 assert(size == mk->instance_size(real_klass), "should have been set");
never@3205 585
never@3205 586 // notify jvmti and dtrace
never@3205 587 post_allocation_notify(klass, (oop)obj);
never@3205 588
never@3205 589 return mirror;
never@3205 590 }
stefank@3335 591
stefank@3335 592 /////////////// Unit tests ///////////////
stefank@3335 593
stefank@3335 594 #ifndef PRODUCT
stefank@3335 595 void CollectedHeap::test_is_in() {
stefank@3335 596 CollectedHeap* heap = Universe::heap();
stefank@3335 597
stefank@3375 598 uintptr_t epsilon = (uintptr_t) MinObjAlignment;
stefank@3375 599 uintptr_t heap_start = (uintptr_t) heap->_reserved.start();
stefank@3375 600 uintptr_t heap_end = (uintptr_t) heap->_reserved.end();
stefank@3375 601
stefank@3335 602 // Test that NULL is not in the heap.
stefank@3335 603 assert(!heap->is_in(NULL), "NULL is unexpectedly in the heap");
stefank@3335 604
stefank@3335 605 // Test that a pointer to before the heap start is reported as outside the heap.
stefank@3375 606 assert(heap_start >= ((uintptr_t)NULL + epsilon), "sanity");
stefank@3375 607 void* before_heap = (void*)(heap_start - epsilon);
stefank@3335 608 assert(!heap->is_in(before_heap),
stefank@3335 609 err_msg("before_heap: " PTR_FORMAT " is unexpectedly in the heap", before_heap));
stefank@3335 610
stefank@3335 611 // Test that a pointer to after the heap end is reported as outside the heap.
stefank@3375 612 assert(heap_end <= ((uintptr_t)-1 - epsilon), "sanity");
stefank@3375 613 void* after_heap = (void*)(heap_end + epsilon);
stefank@3335 614 assert(!heap->is_in(after_heap),
stefank@3335 615 err_msg("after_heap: " PTR_FORMAT " is unexpectedly in the heap", after_heap));
stefank@3335 616 }
stefank@3335 617 #endif

mercurial