src/share/vm/gc_interface/collectedHeap.cpp

Fri, 16 Oct 2009 02:05:46 -0700

author
ysr
date
Fri, 16 Oct 2009 02:05:46 -0700
changeset 1462
39b01ab7035a
parent 1063
7bb995fbd3c0
child 1577
4ce7240d622c
permissions
-rw-r--r--

6888898: CMS: ReduceInitialCardMarks unsafe in the presence of cms precleaning
6889757: G1: enable card mark elision for initializing writes from compiled code (ReduceInitialCardMarks)
Summary: Defer the (compiler-elided) card-mark upon a slow-path allocation until after the store and before the next subsequent safepoint; G1 now answers yes to can_elide_tlab_write_barriers().
Reviewed-by: jcoomes, kvn, never

duke@435 1 /*
xdono@1014 2 * Copyright 2001-2009 Sun Microsystems, Inc. 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 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_collectedHeap.cpp.incl"
duke@435 27
duke@435 28
duke@435 29 #ifdef ASSERT
duke@435 30 int CollectedHeap::_fire_out_of_memory_count = 0;
duke@435 31 #endif
duke@435 32
jcoomes@916 33 size_t CollectedHeap::_filler_array_max_size = 0;
jcoomes@916 34
duke@435 35 // Memory state functions.
duke@435 36
jcoomes@916 37 CollectedHeap::CollectedHeap()
jcoomes@916 38 {
jcoomes@916 39 const size_t max_len = size_t(arrayOopDesc::max_array_length(T_INT));
jcoomes@916 40 const size_t elements_per_word = HeapWordSize / sizeof(jint);
jcoomes@916 41 _filler_array_max_size = align_object_size(filler_array_hdr_size() +
jcoomes@916 42 max_len * elements_per_word);
jcoomes@916 43
jcoomes@916 44 _barrier_set = NULL;
jcoomes@916 45 _is_gc_active = false;
jcoomes@916 46 _total_collections = _total_full_collections = 0;
jcoomes@916 47 _gc_cause = _gc_lastcause = GCCause::_no_gc;
duke@435 48 NOT_PRODUCT(_promotion_failure_alot_count = 0;)
duke@435 49 NOT_PRODUCT(_promotion_failure_alot_gc_number = 0;)
duke@435 50
duke@435 51 if (UsePerfData) {
duke@435 52 EXCEPTION_MARK;
duke@435 53
duke@435 54 // create the gc cause jvmstat counters
duke@435 55 _perf_gc_cause = PerfDataManager::create_string_variable(SUN_GC, "cause",
duke@435 56 80, GCCause::to_string(_gc_cause), CHECK);
duke@435 57
duke@435 58 _perf_gc_lastcause =
duke@435 59 PerfDataManager::create_string_variable(SUN_GC, "lastCause",
duke@435 60 80, GCCause::to_string(_gc_lastcause), CHECK);
duke@435 61 }
duke@435 62 }
duke@435 63
duke@435 64
duke@435 65 #ifndef PRODUCT
duke@435 66 void CollectedHeap::check_for_bad_heap_word_value(HeapWord* addr, size_t size) {
duke@435 67 if (CheckMemoryInitialization && ZapUnusedHeapArea) {
duke@435 68 for (size_t slot = 0; slot < size; slot += 1) {
duke@435 69 assert((*(intptr_t*) (addr + slot)) != ((intptr_t) badHeapWordVal),
duke@435 70 "Found badHeapWordValue in post-allocation check");
duke@435 71 }
duke@435 72 }
duke@435 73 }
duke@435 74
duke@435 75 void CollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr, size_t size)
duke@435 76 {
duke@435 77 if (CheckMemoryInitialization && ZapUnusedHeapArea) {
duke@435 78 for (size_t slot = 0; slot < size; slot += 1) {
duke@435 79 assert((*(intptr_t*) (addr + slot)) == ((intptr_t) badHeapWordVal),
duke@435 80 "Found non badHeapWordValue in pre-allocation check");
duke@435 81 }
duke@435 82 }
duke@435 83 }
duke@435 84 #endif // PRODUCT
duke@435 85
duke@435 86 #ifdef ASSERT
duke@435 87 void CollectedHeap::check_for_valid_allocation_state() {
duke@435 88 Thread *thread = Thread::current();
duke@435 89 // How to choose between a pending exception and a potential
duke@435 90 // OutOfMemoryError? Don't allow pending exceptions.
duke@435 91 // This is a VM policy failure, so how do we exhaustively test it?
duke@435 92 assert(!thread->has_pending_exception(),
duke@435 93 "shouldn't be allocating with pending exception");
duke@435 94 if (StrictSafepointChecks) {
duke@435 95 assert(thread->allow_allocation(),
duke@435 96 "Allocation done by thread for which allocation is blocked "
duke@435 97 "by No_Allocation_Verifier!");
duke@435 98 // Allocation of an oop can always invoke a safepoint,
duke@435 99 // hence, the true argument
duke@435 100 thread->check_for_valid_safepoint_state(true);
duke@435 101 }
duke@435 102 }
duke@435 103 #endif
duke@435 104
duke@435 105 HeapWord* CollectedHeap::allocate_from_tlab_slow(Thread* thread, size_t size) {
duke@435 106
duke@435 107 // Retain tlab and allocate object in shared space if
duke@435 108 // the amount free in the tlab is too large to discard.
duke@435 109 if (thread->tlab().free() > thread->tlab().refill_waste_limit()) {
duke@435 110 thread->tlab().record_slow_allocation(size);
duke@435 111 return NULL;
duke@435 112 }
duke@435 113
duke@435 114 // Discard tlab and allocate a new one.
duke@435 115 // To minimize fragmentation, the last TLAB may be smaller than the rest.
duke@435 116 size_t new_tlab_size = thread->tlab().compute_size(size);
duke@435 117
duke@435 118 thread->tlab().clear_before_allocation();
duke@435 119
duke@435 120 if (new_tlab_size == 0) {
duke@435 121 return NULL;
duke@435 122 }
duke@435 123
duke@435 124 // Allocate a new TLAB...
duke@435 125 HeapWord* obj = Universe::heap()->allocate_new_tlab(new_tlab_size);
duke@435 126 if (obj == NULL) {
duke@435 127 return NULL;
duke@435 128 }
duke@435 129 if (ZeroTLAB) {
duke@435 130 // ..and clear it.
duke@435 131 Copy::zero_to_words(obj, new_tlab_size);
duke@435 132 } else {
duke@435 133 // ...and clear just the allocated object.
duke@435 134 Copy::zero_to_words(obj, size);
duke@435 135 }
duke@435 136 thread->tlab().fill(obj, obj + size, new_tlab_size);
duke@435 137 return obj;
duke@435 138 }
duke@435 139
ysr@1462 140 void CollectedHeap::flush_deferred_store_barrier(JavaThread* thread) {
ysr@1462 141 MemRegion deferred = thread->deferred_card_mark();
ysr@1462 142 if (!deferred.is_empty()) {
ysr@1462 143 {
ysr@1462 144 // Verify that the storage points to a parsable object in heap
ysr@1462 145 DEBUG_ONLY(oop old_obj = oop(deferred.start());)
ysr@1462 146 assert(is_in(old_obj), "Not in allocated heap");
ysr@1462 147 assert(!can_elide_initializing_store_barrier(old_obj),
ysr@1462 148 "Else should have been filtered in defer_store_barrier()");
ysr@1462 149 assert(!is_in_permanent(old_obj), "Sanity: not expected");
ysr@1462 150 assert(old_obj->is_oop(true), "Not an oop");
ysr@1462 151 assert(old_obj->is_parsable(), "Will not be concurrently parsable");
ysr@1462 152 assert(deferred.word_size() == (size_t)(old_obj->size()),
ysr@1462 153 "Mismatch: multiple objects?");
ysr@1462 154 }
ysr@1462 155 BarrierSet* bs = barrier_set();
ysr@1462 156 assert(bs->has_write_region_opt(), "No write_region() on BarrierSet");
ysr@1462 157 bs->write_region(deferred);
ysr@1462 158 // "Clear" the deferred_card_mark field
ysr@1462 159 thread->set_deferred_card_mark(MemRegion());
ysr@1462 160 }
ysr@1462 161 assert(thread->deferred_card_mark().is_empty(), "invariant");
ysr@1462 162 }
ysr@1462 163
ysr@1462 164 // Helper for ReduceInitialCardMarks. For performance,
ysr@1462 165 // compiled code may elide card-marks for initializing stores
ysr@1462 166 // to a newly allocated object along the fast-path. We
ysr@1462 167 // compensate for such elided card-marks as follows:
ysr@1462 168 // (a) Generational, non-concurrent collectors, such as
ysr@1462 169 // GenCollectedHeap(ParNew,DefNew,Tenured) and
ysr@1462 170 // ParallelScavengeHeap(ParallelGC, ParallelOldGC)
ysr@1462 171 // need the card-mark if and only if the region is
ysr@1462 172 // in the old gen, and do not care if the card-mark
ysr@1462 173 // succeeds or precedes the initializing stores themselves,
ysr@1462 174 // so long as the card-mark is completed before the next
ysr@1462 175 // scavenge. For all these cases, we can do a card mark
ysr@1462 176 // at the point at which we do a slow path allocation
ysr@1462 177 // in the old gen. For uniformity, however, we end
ysr@1462 178 // up using the same scheme (see below) for all three
ysr@1462 179 // cases (deferring the card-mark appropriately).
ysr@1462 180 // (b) GenCollectedHeap(ConcurrentMarkSweepGeneration) requires
ysr@1462 181 // in addition that the card-mark for an old gen allocated
ysr@1462 182 // object strictly follow any associated initializing stores.
ysr@1462 183 // In these cases, the memRegion remembered below is
ysr@1462 184 // used to card-mark the entire region either just before the next
ysr@1462 185 // slow-path allocation by this thread or just before the next scavenge or
ysr@1462 186 // CMS-associated safepoint, whichever of these events happens first.
ysr@1462 187 // (The implicit assumption is that the object has been fully
ysr@1462 188 // initialized by this point, a fact that we assert when doing the
ysr@1462 189 // card-mark.)
ysr@1462 190 // (c) G1CollectedHeap(G1) uses two kinds of write barriers. When a
ysr@1462 191 // G1 concurrent marking is in progress an SATB (pre-write-)barrier is
ysr@1462 192 // is used to remember the pre-value of any store. Initializing
ysr@1462 193 // stores will not need this barrier, so we need not worry about
ysr@1462 194 // compensating for the missing pre-barrier here. Turning now
ysr@1462 195 // to the post-barrier, we note that G1 needs a RS update barrier
ysr@1462 196 // which simply enqueues a (sequence of) dirty cards which may
ysr@1462 197 // optionally be refined by the concurrent update threads. Note
ysr@1462 198 // that this barrier need only be applied to a non-young write,
ysr@1462 199 // but, like in CMS, because of the presence of concurrent refinement
ysr@1462 200 // (much like CMS' precleaning), must strictly follow the oop-store.
ysr@1462 201 // Thus, using the same protocol for maintaining the intended
ysr@1462 202 // invariants turns out, serendepitously, to be the same for all
ysr@1462 203 // three collectors/heap types above.
ysr@1462 204 //
ysr@1462 205 // For each future collector, this should be reexamined with
ysr@1462 206 // that specific collector in mind.
ysr@1462 207 oop CollectedHeap::defer_store_barrier(JavaThread* thread, oop new_obj) {
ysr@1462 208 // If a previous card-mark was deferred, flush it now.
ysr@1462 209 flush_deferred_store_barrier(thread);
ysr@1462 210 if (can_elide_initializing_store_barrier(new_obj)) {
ysr@1462 211 // The deferred_card_mark region should be empty
ysr@1462 212 // following the flush above.
ysr@1462 213 assert(thread->deferred_card_mark().is_empty(), "Error");
ysr@1462 214 } else {
ysr@1462 215 // Remember info for the newly deferred store barrier
ysr@1462 216 MemRegion deferred = MemRegion((HeapWord*)new_obj, new_obj->size());
ysr@1462 217 assert(!deferred.is_empty(), "Error");
ysr@1462 218 thread->set_deferred_card_mark(deferred);
ysr@1462 219 }
ysr@1462 220 return new_obj;
ysr@1462 221 }
ysr@1462 222
jcoomes@916 223 size_t CollectedHeap::filler_array_hdr_size() {
jcoomes@916 224 return size_t(arrayOopDesc::header_size(T_INT));
jcoomes@916 225 }
jcoomes@916 226
jcoomes@916 227 size_t CollectedHeap::filler_array_min_size() {
jcoomes@916 228 return align_object_size(filler_array_hdr_size());
jcoomes@916 229 }
jcoomes@916 230
jcoomes@916 231 size_t CollectedHeap::filler_array_max_size() {
jcoomes@916 232 return _filler_array_max_size;
jcoomes@916 233 }
jcoomes@916 234
jcoomes@916 235 #ifdef ASSERT
jcoomes@916 236 void CollectedHeap::fill_args_check(HeapWord* start, size_t words)
jcoomes@916 237 {
jcoomes@916 238 assert(words >= min_fill_size(), "too small to fill");
jcoomes@916 239 assert(words % MinObjAlignment == 0, "unaligned size");
jcoomes@916 240 assert(Universe::heap()->is_in_reserved(start), "not in heap");
jcoomes@916 241 assert(Universe::heap()->is_in_reserved(start + words - 1), "not in heap");
jcoomes@916 242 }
jcoomes@916 243
jcoomes@916 244 void CollectedHeap::zap_filler_array(HeapWord* start, size_t words)
jcoomes@916 245 {
jcoomes@916 246 if (ZapFillerObjects) {
jcoomes@916 247 Copy::fill_to_words(start + filler_array_hdr_size(),
jcoomes@916 248 words - filler_array_hdr_size(), 0XDEAFBABE);
jcoomes@916 249 }
jcoomes@916 250 }
jcoomes@916 251 #endif // ASSERT
jcoomes@916 252
jcoomes@916 253 void
jcoomes@916 254 CollectedHeap::fill_with_array(HeapWord* start, size_t words)
jcoomes@916 255 {
jcoomes@916 256 assert(words >= filler_array_min_size(), "too small for an array");
jcoomes@916 257 assert(words <= filler_array_max_size(), "too big for a single object");
jcoomes@916 258
jcoomes@916 259 const size_t payload_size = words - filler_array_hdr_size();
jcoomes@916 260 const size_t len = payload_size * HeapWordSize / sizeof(jint);
jcoomes@916 261
jcoomes@916 262 // Set the length first for concurrent GC.
jcoomes@916 263 ((arrayOop)start)->set_length((int)len);
jcoomes@929 264 post_allocation_setup_common(Universe::intArrayKlassObj(), start, words);
jcoomes@916 265 DEBUG_ONLY(zap_filler_array(start, words);)
jcoomes@916 266 }
jcoomes@916 267
jcoomes@916 268 void
jcoomes@916 269 CollectedHeap::fill_with_object_impl(HeapWord* start, size_t words)
jcoomes@916 270 {
jcoomes@916 271 assert(words <= filler_array_max_size(), "too big for a single object");
jcoomes@916 272
jcoomes@916 273 if (words >= filler_array_min_size()) {
jcoomes@916 274 fill_with_array(start, words);
jcoomes@916 275 } else if (words > 0) {
jcoomes@916 276 assert(words == min_fill_size(), "unaligned size");
jcoomes@916 277 post_allocation_setup_common(SystemDictionary::object_klass(), start,
jcoomes@916 278 words);
jcoomes@916 279 }
jcoomes@916 280 }
jcoomes@916 281
jcoomes@916 282 void CollectedHeap::fill_with_object(HeapWord* start, size_t words)
jcoomes@916 283 {
jcoomes@916 284 DEBUG_ONLY(fill_args_check(start, words);)
jcoomes@916 285 HandleMark hm; // Free handles before leaving.
jcoomes@916 286 fill_with_object_impl(start, words);
jcoomes@916 287 }
jcoomes@916 288
jcoomes@916 289 void CollectedHeap::fill_with_objects(HeapWord* start, size_t words)
jcoomes@916 290 {
jcoomes@916 291 DEBUG_ONLY(fill_args_check(start, words);)
jcoomes@916 292 HandleMark hm; // Free handles before leaving.
jcoomes@916 293
jcoomes@916 294 #ifdef LP64
jcoomes@916 295 // A single array can fill ~8G, so multiple objects are needed only in 64-bit.
jcoomes@916 296 // First fill with arrays, ensuring that any remaining space is big enough to
jcoomes@916 297 // fill. The remainder is filled with a single object.
jcoomes@916 298 const size_t min = min_fill_size();
jcoomes@916 299 const size_t max = filler_array_max_size();
jcoomes@916 300 while (words > max) {
jcoomes@916 301 const size_t cur = words - max >= min ? max : max - min;
jcoomes@916 302 fill_with_array(start, cur);
jcoomes@916 303 start += cur;
jcoomes@916 304 words -= cur;
jcoomes@916 305 }
jcoomes@916 306 #endif
jcoomes@916 307
jcoomes@916 308 fill_with_object_impl(start, words);
jcoomes@916 309 }
jcoomes@916 310
duke@435 311 HeapWord* CollectedHeap::allocate_new_tlab(size_t size) {
duke@435 312 guarantee(false, "thread-local allocation buffers not supported");
duke@435 313 return NULL;
duke@435 314 }
duke@435 315
duke@435 316 void CollectedHeap::fill_all_tlabs(bool retire) {
duke@435 317 assert(UseTLAB, "should not reach here");
duke@435 318 // See note in ensure_parsability() below.
duke@435 319 assert(SafepointSynchronize::is_at_safepoint() ||
duke@435 320 !is_init_completed(),
duke@435 321 "should only fill tlabs at safepoint");
duke@435 322 // The main thread starts allocating via a TLAB even before it
duke@435 323 // has added itself to the threads list at vm boot-up.
duke@435 324 assert(Threads::first() != NULL,
duke@435 325 "Attempt to fill tlabs before main thread has been added"
duke@435 326 " to threads list is doomed to failure!");
duke@435 327 for(JavaThread *thread = Threads::first(); thread; thread = thread->next()) {
duke@435 328 thread->tlab().make_parsable(retire);
duke@435 329 }
duke@435 330 }
duke@435 331
duke@435 332 void CollectedHeap::ensure_parsability(bool retire_tlabs) {
duke@435 333 // The second disjunct in the assertion below makes a concession
duke@435 334 // for the start-up verification done while the VM is being
duke@435 335 // created. Callers be careful that you know that mutators
duke@435 336 // aren't going to interfere -- for instance, this is permissible
duke@435 337 // if we are still single-threaded and have either not yet
duke@435 338 // started allocating (nothing much to verify) or we have
duke@435 339 // started allocating but are now a full-fledged JavaThread
duke@435 340 // (and have thus made our TLAB's) available for filling.
duke@435 341 assert(SafepointSynchronize::is_at_safepoint() ||
duke@435 342 !is_init_completed(),
duke@435 343 "Should only be called at a safepoint or at start-up"
duke@435 344 " otherwise concurrent mutator activity may make heap "
duke@435 345 " unparsable again");
duke@435 346 if (UseTLAB) {
duke@435 347 fill_all_tlabs(retire_tlabs);
duke@435 348 }
duke@435 349 }
duke@435 350
duke@435 351 void CollectedHeap::accumulate_statistics_all_tlabs() {
duke@435 352 if (UseTLAB) {
duke@435 353 assert(SafepointSynchronize::is_at_safepoint() ||
duke@435 354 !is_init_completed(),
duke@435 355 "should only accumulate statistics on tlabs at safepoint");
duke@435 356
duke@435 357 ThreadLocalAllocBuffer::accumulate_statistics_before_gc();
duke@435 358 }
duke@435 359 }
duke@435 360
duke@435 361 void CollectedHeap::resize_all_tlabs() {
duke@435 362 if (UseTLAB) {
duke@435 363 assert(SafepointSynchronize::is_at_safepoint() ||
duke@435 364 !is_init_completed(),
duke@435 365 "should only resize tlabs at safepoint");
duke@435 366
duke@435 367 ThreadLocalAllocBuffer::resize_all_tlabs();
duke@435 368 }
duke@435 369 }
ysr@1050 370
ysr@1050 371 void CollectedHeap::pre_full_gc_dump() {
ysr@1050 372 if (HeapDumpBeforeFullGC) {
ysr@1050 373 TraceTime tt("Heap Dump: ", PrintGCDetails, false, gclog_or_tty);
ysr@1050 374 // We are doing a "major" collection and a heap dump before
ysr@1050 375 // major collection has been requested.
ysr@1050 376 HeapDumper::dump_heap();
ysr@1050 377 }
ysr@1050 378 if (PrintClassHistogramBeforeFullGC) {
ysr@1050 379 TraceTime tt("Class Histogram: ", PrintGCDetails, true, gclog_or_tty);
ysr@1050 380 VM_GC_HeapInspection inspector(gclog_or_tty, false /* ! full gc */, false /* ! prologue */);
ysr@1050 381 inspector.doit();
ysr@1050 382 }
ysr@1050 383 }
ysr@1050 384
ysr@1050 385 void CollectedHeap::post_full_gc_dump() {
ysr@1050 386 if (HeapDumpAfterFullGC) {
ysr@1050 387 TraceTime tt("Heap Dump", PrintGCDetails, false, gclog_or_tty);
ysr@1050 388 HeapDumper::dump_heap();
ysr@1050 389 }
ysr@1050 390 if (PrintClassHistogramAfterFullGC) {
ysr@1050 391 TraceTime tt("Class Histogram", PrintGCDetails, true, gclog_or_tty);
ysr@1050 392 VM_GC_HeapInspection inspector(gclog_or_tty, false /* ! full gc */, false /* ! prologue */);
ysr@1050 393 inspector.doit();
ysr@1050 394 }
ysr@1050 395 }

mercurial