src/share/vm/gc_implementation/parNew/parNewGeneration.cpp

Sat, 23 Oct 2010 23:03:49 -0700

author
ysr
date
Sat, 23 Oct 2010 23:03:49 -0700
changeset 2243
a7214d79fcf1
parent 2191
894b1d7c7e01
child 2314
f95d63e2154a
permissions
-rw-r--r--

6896603: CMS/GCH: collection_attempt_is_safe() ergo should use more recent data
Summary: Deprecated HandlePromotionFailure, removing the ability to turn off that feature, did away with one epoch look-ahead when deciding if a scavenge is likely to fail, relying on current data.
Reviewed-by: jmasa, johnc, poonam

duke@435 1 /*
trims@1907 2 * Copyright (c) 2001, 2010, 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
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_parNewGeneration.cpp.incl"
duke@435 27
duke@435 28 #ifdef _MSC_VER
duke@435 29 #pragma warning( push )
duke@435 30 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
duke@435 31 #endif
duke@435 32 ParScanThreadState::ParScanThreadState(Space* to_space_,
duke@435 33 ParNewGeneration* gen_,
duke@435 34 Generation* old_gen_,
duke@435 35 int thread_num_,
duke@435 36 ObjToScanQueueSet* work_queue_set_,
jcoomes@2191 37 Stack<oop>* overflow_stacks_,
duke@435 38 size_t desired_plab_sz_,
duke@435 39 ParallelTaskTerminator& term_) :
ysr@1114 40 _to_space(to_space_), _old_gen(old_gen_), _young_gen(gen_), _thread_num(thread_num_),
duke@435 41 _work_queue(work_queue_set_->queue(thread_num_)), _to_space_full(false),
jcoomes@2191 42 _overflow_stack(overflow_stacks_ ? overflow_stacks_ + thread_num_ : NULL),
duke@435 43 _ageTable(false), // false ==> not the global age table, no perf data.
duke@435 44 _to_space_alloc_buffer(desired_plab_sz_),
duke@435 45 _to_space_closure(gen_, this), _old_gen_closure(gen_, this),
duke@435 46 _to_space_root_closure(gen_, this), _old_gen_root_closure(gen_, this),
duke@435 47 _older_gen_closure(gen_, this),
duke@435 48 _evacuate_followers(this, &_to_space_closure, &_old_gen_closure,
duke@435 49 &_to_space_root_closure, gen_, &_old_gen_root_closure,
duke@435 50 work_queue_set_, &term_),
duke@435 51 _is_alive_closure(gen_), _scan_weak_ref_closure(gen_, this),
duke@435 52 _keep_alive_closure(&_scan_weak_ref_closure),
ysr@1580 53 _promotion_failure_size(0),
duke@435 54 _strong_roots_time(0.0), _term_time(0.0)
duke@435 55 {
jcoomes@2065 56 #if TASKQUEUE_STATS
jcoomes@2065 57 _term_attempts = 0;
jcoomes@2065 58 _overflow_refills = 0;
jcoomes@2065 59 _overflow_refill_objs = 0;
jcoomes@2065 60 #endif // TASKQUEUE_STATS
jcoomes@2065 61
duke@435 62 _survivor_chunk_array =
duke@435 63 (ChunkArray*) old_gen()->get_data_recorder(thread_num());
duke@435 64 _hash_seed = 17; // Might want to take time-based random value.
duke@435 65 _start = os::elapsedTime();
duke@435 66 _old_gen_closure.set_generation(old_gen_);
duke@435 67 _old_gen_root_closure.set_generation(old_gen_);
duke@435 68 }
duke@435 69 #ifdef _MSC_VER
duke@435 70 #pragma warning( pop )
duke@435 71 #endif
duke@435 72
duke@435 73 void ParScanThreadState::record_survivor_plab(HeapWord* plab_start,
duke@435 74 size_t plab_word_size) {
duke@435 75 ChunkArray* sca = survivor_chunk_array();
duke@435 76 if (sca != NULL) {
duke@435 77 // A non-null SCA implies that we want the PLAB data recorded.
duke@435 78 sca->record_sample(plab_start, plab_word_size);
duke@435 79 }
duke@435 80 }
duke@435 81
duke@435 82 bool ParScanThreadState::should_be_partially_scanned(oop new_obj, oop old_obj) const {
duke@435 83 return new_obj->is_objArray() &&
duke@435 84 arrayOop(new_obj)->length() > ParGCArrayScanChunk &&
duke@435 85 new_obj != old_obj;
duke@435 86 }
duke@435 87
duke@435 88 void ParScanThreadState::scan_partial_array_and_push_remainder(oop old) {
duke@435 89 assert(old->is_objArray(), "must be obj array");
duke@435 90 assert(old->is_forwarded(), "must be forwarded");
duke@435 91 assert(Universe::heap()->is_in_reserved(old), "must be in heap.");
ysr@1114 92 assert(!old_gen()->is_in(old), "must be in young generation.");
duke@435 93
duke@435 94 objArrayOop obj = objArrayOop(old->forwardee());
duke@435 95 // Process ParGCArrayScanChunk elements now
duke@435 96 // and push the remainder back onto queue
duke@435 97 int start = arrayOop(old)->length();
duke@435 98 int end = obj->length();
duke@435 99 int remainder = end - start;
duke@435 100 assert(start <= end, "just checking");
duke@435 101 if (remainder > 2 * ParGCArrayScanChunk) {
duke@435 102 // Test above combines last partial chunk with a full chunk
duke@435 103 end = start + ParGCArrayScanChunk;
duke@435 104 arrayOop(old)->set_length(end);
duke@435 105 // Push remainder.
duke@435 106 bool ok = work_queue()->push(old);
duke@435 107 assert(ok, "just popped, push must be okay");
duke@435 108 } else {
duke@435 109 // Restore length so that it can be used if there
duke@435 110 // is a promotion failure and forwarding pointers
duke@435 111 // must be removed.
duke@435 112 arrayOop(old)->set_length(end);
duke@435 113 }
coleenp@548 114
duke@435 115 // process our set of indices (include header in first chunk)
coleenp@548 116 // should make sure end is even (aligned to HeapWord in case of compressed oops)
duke@435 117 if ((HeapWord *)obj < young_old_boundary()) {
duke@435 118 // object is in to_space
coleenp@548 119 obj->oop_iterate_range(&_to_space_closure, start, end);
duke@435 120 } else {
duke@435 121 // object is in old generation
coleenp@548 122 obj->oop_iterate_range(&_old_gen_closure, start, end);
duke@435 123 }
duke@435 124 }
duke@435 125
duke@435 126
duke@435 127 void ParScanThreadState::trim_queues(int max_size) {
duke@435 128 ObjToScanQueue* queue = work_queue();
ysr@1114 129 do {
ysr@1114 130 while (queue->size() > (juint)max_size) {
ysr@1114 131 oop obj_to_scan;
ysr@1114 132 if (queue->pop_local(obj_to_scan)) {
ysr@1114 133 if ((HeapWord *)obj_to_scan < young_old_boundary()) {
ysr@1114 134 if (obj_to_scan->is_objArray() &&
ysr@1114 135 obj_to_scan->is_forwarded() &&
ysr@1114 136 obj_to_scan->forwardee() != obj_to_scan) {
ysr@1114 137 scan_partial_array_and_push_remainder(obj_to_scan);
ysr@1114 138 } else {
ysr@1114 139 // object is in to_space
ysr@1114 140 obj_to_scan->oop_iterate(&_to_space_closure);
ysr@1114 141 }
duke@435 142 } else {
ysr@1114 143 // object is in old generation
ysr@1114 144 obj_to_scan->oop_iterate(&_old_gen_closure);
duke@435 145 }
duke@435 146 }
duke@435 147 }
ysr@1114 148 // For the case of compressed oops, we have a private, non-shared
ysr@1114 149 // overflow stack, so we eagerly drain it so as to more evenly
ysr@1114 150 // distribute load early. Note: this may be good to do in
ysr@1114 151 // general rather than delay for the final stealing phase.
ysr@1114 152 // If applicable, we'll transfer a set of objects over to our
ysr@1114 153 // work queue, allowing them to be stolen and draining our
ysr@1114 154 // private overflow stack.
ysr@1114 155 } while (ParGCTrimOverflow && young_gen()->take_from_overflow_list(this));
ysr@1114 156 }
ysr@1114 157
ysr@1114 158 bool ParScanThreadState::take_from_overflow_stack() {
ysr@1130 159 assert(ParGCUseLocalOverflow, "Else should not call");
ysr@1114 160 assert(young_gen()->overflow_list() == NULL, "Error");
ysr@1114 161 ObjToScanQueue* queue = work_queue();
jcoomes@2191 162 Stack<oop>* const of_stack = overflow_stack();
jcoomes@2191 163 const size_t num_overflow_elems = of_stack->size();
jcoomes@2191 164 const size_t space_available = queue->max_elems() - queue->size();
jcoomes@2191 165 const size_t num_take_elems = MIN3(space_available / 4,
jcoomes@2191 166 ParGCDesiredObjsFromOverflowList,
jcoomes@2191 167 num_overflow_elems);
ysr@1114 168 // Transfer the most recent num_take_elems from the overflow
ysr@1114 169 // stack to our work queue.
ysr@1114 170 for (size_t i = 0; i != num_take_elems; i++) {
ysr@1114 171 oop cur = of_stack->pop();
ysr@1114 172 oop obj_to_push = cur->forwardee();
ysr@1114 173 assert(Universe::heap()->is_in_reserved(cur), "Should be in heap");
ysr@1114 174 assert(!old_gen()->is_in_reserved(cur), "Should be in young gen");
ysr@1114 175 assert(Universe::heap()->is_in_reserved(obj_to_push), "Should be in heap");
ysr@1114 176 if (should_be_partially_scanned(obj_to_push, cur)) {
ysr@1114 177 assert(arrayOop(cur)->length() == 0, "entire array remaining to be scanned");
ysr@1114 178 obj_to_push = cur;
ysr@1114 179 }
ysr@1114 180 bool ok = queue->push(obj_to_push);
ysr@1114 181 assert(ok, "Should have succeeded");
duke@435 182 }
ysr@1114 183 assert(young_gen()->overflow_list() == NULL, "Error");
ysr@1114 184 return num_take_elems > 0; // was something transferred?
ysr@1114 185 }
ysr@1114 186
ysr@1114 187 void ParScanThreadState::push_on_overflow_stack(oop p) {
ysr@1130 188 assert(ParGCUseLocalOverflow, "Else should not call");
ysr@1114 189 overflow_stack()->push(p);
ysr@1114 190 assert(young_gen()->overflow_list() == NULL, "Error");
duke@435 191 }
duke@435 192
duke@435 193 HeapWord* ParScanThreadState::alloc_in_to_space_slow(size_t word_sz) {
duke@435 194
duke@435 195 // Otherwise, if the object is small enough, try to reallocate the
duke@435 196 // buffer.
duke@435 197 HeapWord* obj = NULL;
duke@435 198 if (!_to_space_full) {
duke@435 199 ParGCAllocBuffer* const plab = to_space_alloc_buffer();
duke@435 200 Space* const sp = to_space();
duke@435 201 if (word_sz * 100 <
duke@435 202 ParallelGCBufferWastePct * plab->word_sz()) {
duke@435 203 // Is small enough; abandon this buffer and start a new one.
duke@435 204 plab->retire(false, false);
duke@435 205 size_t buf_size = plab->word_sz();
duke@435 206 HeapWord* buf_space = sp->par_allocate(buf_size);
duke@435 207 if (buf_space == NULL) {
duke@435 208 const size_t min_bytes =
duke@435 209 ParGCAllocBuffer::min_size() << LogHeapWordSize;
duke@435 210 size_t free_bytes = sp->free();
duke@435 211 while(buf_space == NULL && free_bytes >= min_bytes) {
duke@435 212 buf_size = free_bytes >> LogHeapWordSize;
duke@435 213 assert(buf_size == (size_t)align_object_size(buf_size),
duke@435 214 "Invariant");
duke@435 215 buf_space = sp->par_allocate(buf_size);
duke@435 216 free_bytes = sp->free();
duke@435 217 }
duke@435 218 }
duke@435 219 if (buf_space != NULL) {
duke@435 220 plab->set_word_size(buf_size);
duke@435 221 plab->set_buf(buf_space);
duke@435 222 record_survivor_plab(buf_space, buf_size);
duke@435 223 obj = plab->allocate(word_sz);
duke@435 224 // Note that we cannot compare buf_size < word_sz below
duke@435 225 // because of AlignmentReserve (see ParGCAllocBuffer::allocate()).
duke@435 226 assert(obj != NULL || plab->words_remaining() < word_sz,
duke@435 227 "Else should have been able to allocate");
duke@435 228 // It's conceivable that we may be able to use the
duke@435 229 // buffer we just grabbed for subsequent small requests
duke@435 230 // even if not for this one.
duke@435 231 } else {
duke@435 232 // We're used up.
duke@435 233 _to_space_full = true;
duke@435 234 }
duke@435 235
duke@435 236 } else {
duke@435 237 // Too large; allocate the object individually.
duke@435 238 obj = sp->par_allocate(word_sz);
duke@435 239 }
duke@435 240 }
duke@435 241 return obj;
duke@435 242 }
duke@435 243
duke@435 244
duke@435 245 void ParScanThreadState::undo_alloc_in_to_space(HeapWord* obj,
duke@435 246 size_t word_sz) {
duke@435 247 // Is the alloc in the current alloc buffer?
duke@435 248 if (to_space_alloc_buffer()->contains(obj)) {
duke@435 249 assert(to_space_alloc_buffer()->contains(obj + word_sz - 1),
duke@435 250 "Should contain whole object.");
duke@435 251 to_space_alloc_buffer()->undo_allocation(obj, word_sz);
duke@435 252 } else {
jcoomes@916 253 CollectedHeap::fill_with_object(obj, word_sz);
duke@435 254 }
duke@435 255 }
duke@435 256
ysr@1580 257 void ParScanThreadState::print_and_clear_promotion_failure_size() {
ysr@1580 258 if (_promotion_failure_size != 0) {
ysr@1580 259 if (PrintPromotionFailure) {
ysr@1580 260 gclog_or_tty->print(" (%d: promotion failure size = " SIZE_FORMAT ") ",
ysr@1580 261 _thread_num, _promotion_failure_size);
ysr@1580 262 }
ysr@1580 263 _promotion_failure_size = 0;
ysr@1580 264 }
ysr@1580 265 }
ysr@1580 266
duke@435 267 class ParScanThreadStateSet: private ResourceArray {
duke@435 268 public:
duke@435 269 // Initializes states for the specified number of threads;
duke@435 270 ParScanThreadStateSet(int num_threads,
duke@435 271 Space& to_space,
duke@435 272 ParNewGeneration& gen,
duke@435 273 Generation& old_gen,
duke@435 274 ObjToScanQueueSet& queue_set,
jcoomes@2191 275 Stack<oop>* overflow_stacks_,
duke@435 276 size_t desired_plab_sz,
duke@435 277 ParallelTaskTerminator& term);
jcoomes@2065 278
jcoomes@2065 279 ~ParScanThreadStateSet() { TASKQUEUE_STATS_ONLY(reset_stats()); }
jcoomes@2065 280
ysr@1580 281 inline ParScanThreadState& thread_state(int i);
jcoomes@2065 282
ysr@1580 283 void reset(bool promotion_failed);
duke@435 284 void flush();
jcoomes@2065 285
jcoomes@2065 286 #if TASKQUEUE_STATS
jcoomes@2065 287 static void
jcoomes@2065 288 print_termination_stats_hdr(outputStream* const st = gclog_or_tty);
jcoomes@2065 289 void print_termination_stats(outputStream* const st = gclog_or_tty);
jcoomes@2065 290 static void
jcoomes@2065 291 print_taskqueue_stats_hdr(outputStream* const st = gclog_or_tty);
jcoomes@2065 292 void print_taskqueue_stats(outputStream* const st = gclog_or_tty);
jcoomes@2065 293 void reset_stats();
jcoomes@2065 294 #endif // TASKQUEUE_STATS
jcoomes@2065 295
duke@435 296 private:
duke@435 297 ParallelTaskTerminator& _term;
duke@435 298 ParNewGeneration& _gen;
duke@435 299 Generation& _next_gen;
duke@435 300 };
duke@435 301
duke@435 302
duke@435 303 ParScanThreadStateSet::ParScanThreadStateSet(
duke@435 304 int num_threads, Space& to_space, ParNewGeneration& gen,
duke@435 305 Generation& old_gen, ObjToScanQueueSet& queue_set,
jcoomes@2191 306 Stack<oop>* overflow_stacks,
duke@435 307 size_t desired_plab_sz, ParallelTaskTerminator& term)
duke@435 308 : ResourceArray(sizeof(ParScanThreadState), num_threads),
jcoomes@2065 309 _gen(gen), _next_gen(old_gen), _term(term)
duke@435 310 {
duke@435 311 assert(num_threads > 0, "sanity check!");
jcoomes@2191 312 assert(ParGCUseLocalOverflow == (overflow_stacks != NULL),
jcoomes@2191 313 "overflow_stack allocation mismatch");
duke@435 314 // Initialize states.
duke@435 315 for (int i = 0; i < num_threads; ++i) {
duke@435 316 new ((ParScanThreadState*)_data + i)
duke@435 317 ParScanThreadState(&to_space, &gen, &old_gen, i, &queue_set,
jcoomes@2191 318 overflow_stacks, desired_plab_sz, term);
duke@435 319 }
duke@435 320 }
duke@435 321
ysr@1580 322 inline ParScanThreadState& ParScanThreadStateSet::thread_state(int i)
duke@435 323 {
duke@435 324 assert(i >= 0 && i < length(), "sanity check!");
duke@435 325 return ((ParScanThreadState*)_data)[i];
duke@435 326 }
duke@435 327
duke@435 328
ysr@1580 329 void ParScanThreadStateSet::reset(bool promotion_failed)
duke@435 330 {
duke@435 331 _term.reset_for_reuse();
ysr@1580 332 if (promotion_failed) {
ysr@1580 333 for (int i = 0; i < length(); ++i) {
ysr@1580 334 thread_state(i).print_and_clear_promotion_failure_size();
ysr@1580 335 }
ysr@1580 336 }
duke@435 337 }
duke@435 338
jcoomes@2065 339 #if TASKQUEUE_STATS
jcoomes@2065 340 void
jcoomes@2065 341 ParScanThreadState::reset_stats()
jcoomes@2065 342 {
jcoomes@2065 343 taskqueue_stats().reset();
jcoomes@2065 344 _term_attempts = 0;
jcoomes@2065 345 _overflow_refills = 0;
jcoomes@2065 346 _overflow_refill_objs = 0;
jcoomes@2065 347 }
jcoomes@2065 348
jcoomes@2065 349 void ParScanThreadStateSet::reset_stats()
jcoomes@2065 350 {
jcoomes@2065 351 for (int i = 0; i < length(); ++i) {
jcoomes@2065 352 thread_state(i).reset_stats();
jcoomes@2065 353 }
jcoomes@2065 354 }
jcoomes@2065 355
jcoomes@2065 356 void
jcoomes@2065 357 ParScanThreadStateSet::print_termination_stats_hdr(outputStream* const st)
jcoomes@2065 358 {
jcoomes@2065 359 st->print_raw_cr("GC Termination Stats");
jcoomes@2065 360 st->print_raw_cr(" elapsed --strong roots-- "
jcoomes@2065 361 "-------termination-------");
jcoomes@2065 362 st->print_raw_cr("thr ms ms % "
jcoomes@2065 363 " ms % attempts");
jcoomes@2065 364 st->print_raw_cr("--- --------- --------- ------ "
jcoomes@2065 365 "--------- ------ --------");
jcoomes@2065 366 }
jcoomes@2065 367
jcoomes@2065 368 void ParScanThreadStateSet::print_termination_stats(outputStream* const st)
jcoomes@2065 369 {
jcoomes@2065 370 print_termination_stats_hdr(st);
jcoomes@2065 371
jcoomes@2065 372 for (int i = 0; i < length(); ++i) {
jcoomes@2065 373 const ParScanThreadState & pss = thread_state(i);
jcoomes@2065 374 const double elapsed_ms = pss.elapsed_time() * 1000.0;
jcoomes@2065 375 const double s_roots_ms = pss.strong_roots_time() * 1000.0;
jcoomes@2065 376 const double term_ms = pss.term_time() * 1000.0;
jcoomes@2065 377 st->print_cr("%3d %9.2f %9.2f %6.2f "
jcoomes@2065 378 "%9.2f %6.2f " SIZE_FORMAT_W(8),
jcoomes@2065 379 i, elapsed_ms, s_roots_ms, s_roots_ms * 100 / elapsed_ms,
jcoomes@2065 380 term_ms, term_ms * 100 / elapsed_ms, pss.term_attempts());
jcoomes@2065 381 }
jcoomes@2065 382 }
jcoomes@2065 383
jcoomes@2065 384 // Print stats related to work queue activity.
jcoomes@2065 385 void ParScanThreadStateSet::print_taskqueue_stats_hdr(outputStream* const st)
jcoomes@2065 386 {
jcoomes@2065 387 st->print_raw_cr("GC Task Stats");
jcoomes@2065 388 st->print_raw("thr "); TaskQueueStats::print_header(1, st); st->cr();
jcoomes@2065 389 st->print_raw("--- "); TaskQueueStats::print_header(2, st); st->cr();
jcoomes@2065 390 }
jcoomes@2065 391
jcoomes@2065 392 void ParScanThreadStateSet::print_taskqueue_stats(outputStream* const st)
jcoomes@2065 393 {
jcoomes@2065 394 print_taskqueue_stats_hdr(st);
jcoomes@2065 395
jcoomes@2065 396 TaskQueueStats totals;
jcoomes@2065 397 for (int i = 0; i < length(); ++i) {
jcoomes@2065 398 const ParScanThreadState & pss = thread_state(i);
jcoomes@2065 399 const TaskQueueStats & stats = pss.taskqueue_stats();
jcoomes@2065 400 st->print("%3d ", i); stats.print(st); st->cr();
jcoomes@2065 401 totals += stats;
jcoomes@2065 402
jcoomes@2065 403 if (pss.overflow_refills() > 0) {
jcoomes@2065 404 st->print_cr(" " SIZE_FORMAT_W(10) " overflow refills "
jcoomes@2065 405 SIZE_FORMAT_W(10) " overflow objects",
jcoomes@2065 406 pss.overflow_refills(), pss.overflow_refill_objs());
jcoomes@2065 407 }
jcoomes@2065 408 }
jcoomes@2065 409 st->print("tot "); totals.print(st); st->cr();
jcoomes@2065 410
jcoomes@2065 411 DEBUG_ONLY(totals.verify());
jcoomes@2065 412 }
jcoomes@2065 413 #endif // TASKQUEUE_STATS
jcoomes@2065 414
duke@435 415 void ParScanThreadStateSet::flush()
duke@435 416 {
ysr@1580 417 // Work in this loop should be kept as lightweight as
ysr@1580 418 // possible since this might otherwise become a bottleneck
ysr@1580 419 // to scaling. Should we add heavy-weight work into this
ysr@1580 420 // loop, consider parallelizing the loop into the worker threads.
duke@435 421 for (int i = 0; i < length(); ++i) {
ysr@1580 422 ParScanThreadState& par_scan_state = thread_state(i);
duke@435 423
duke@435 424 // Flush stats related to To-space PLAB activity and
duke@435 425 // retire the last buffer.
duke@435 426 par_scan_state.to_space_alloc_buffer()->
duke@435 427 flush_stats_and_retire(_gen.plab_stats(),
duke@435 428 false /* !retain */);
duke@435 429
duke@435 430 // Every thread has its own age table. We need to merge
duke@435 431 // them all into one.
duke@435 432 ageTable *local_table = par_scan_state.age_table();
duke@435 433 _gen.age_table()->merge(local_table);
duke@435 434
duke@435 435 // Inform old gen that we're done.
duke@435 436 _next_gen.par_promote_alloc_done(i);
duke@435 437 _next_gen.par_oop_since_save_marks_iterate_done(i);
jcoomes@2065 438 }
duke@435 439
ysr@1580 440 if (UseConcMarkSweepGC && ParallelGCThreads > 0) {
ysr@1580 441 // We need to call this even when ResizeOldPLAB is disabled
ysr@1580 442 // so as to avoid breaking some asserts. While we may be able
ysr@1580 443 // to avoid this by reorganizing the code a bit, I am loathe
ysr@1580 444 // to do that unless we find cases where ergo leads to bad
ysr@1580 445 // performance.
ysr@1580 446 CFLS_LAB::compute_desired_plab_size();
ysr@1580 447 }
duke@435 448 }
duke@435 449
duke@435 450 ParScanClosure::ParScanClosure(ParNewGeneration* g,
duke@435 451 ParScanThreadState* par_scan_state) :
duke@435 452 OopsInGenClosure(g), _par_scan_state(par_scan_state), _g(g)
duke@435 453 {
duke@435 454 assert(_g->level() == 0, "Optimized for youngest generation");
duke@435 455 _boundary = _g->reserved().end();
duke@435 456 }
duke@435 457
coleenp@548 458 void ParScanWithBarrierClosure::do_oop(oop* p) { ParScanClosure::do_oop_work(p, true, false); }
coleenp@548 459 void ParScanWithBarrierClosure::do_oop(narrowOop* p) { ParScanClosure::do_oop_work(p, true, false); }
coleenp@548 460
coleenp@548 461 void ParScanWithoutBarrierClosure::do_oop(oop* p) { ParScanClosure::do_oop_work(p, false, false); }
coleenp@548 462 void ParScanWithoutBarrierClosure::do_oop(narrowOop* p) { ParScanClosure::do_oop_work(p, false, false); }
coleenp@548 463
coleenp@548 464 void ParRootScanWithBarrierTwoGensClosure::do_oop(oop* p) { ParScanClosure::do_oop_work(p, true, true); }
coleenp@548 465 void ParRootScanWithBarrierTwoGensClosure::do_oop(narrowOop* p) { ParScanClosure::do_oop_work(p, true, true); }
coleenp@548 466
coleenp@548 467 void ParRootScanWithoutBarrierClosure::do_oop(oop* p) { ParScanClosure::do_oop_work(p, false, true); }
coleenp@548 468 void ParRootScanWithoutBarrierClosure::do_oop(narrowOop* p) { ParScanClosure::do_oop_work(p, false, true); }
coleenp@548 469
duke@435 470 ParScanWeakRefClosure::ParScanWeakRefClosure(ParNewGeneration* g,
duke@435 471 ParScanThreadState* par_scan_state)
duke@435 472 : ScanWeakRefClosure(g), _par_scan_state(par_scan_state)
coleenp@548 473 {}
coleenp@548 474
coleenp@548 475 void ParScanWeakRefClosure::do_oop(oop* p) { ParScanWeakRefClosure::do_oop_work(p); }
coleenp@548 476 void ParScanWeakRefClosure::do_oop(narrowOop* p) { ParScanWeakRefClosure::do_oop_work(p); }
duke@435 477
duke@435 478 #ifdef WIN32
duke@435 479 #pragma warning(disable: 4786) /* identifier was truncated to '255' characters in the browser information */
duke@435 480 #endif
duke@435 481
duke@435 482 ParEvacuateFollowersClosure::ParEvacuateFollowersClosure(
duke@435 483 ParScanThreadState* par_scan_state_,
duke@435 484 ParScanWithoutBarrierClosure* to_space_closure_,
duke@435 485 ParScanWithBarrierClosure* old_gen_closure_,
duke@435 486 ParRootScanWithoutBarrierClosure* to_space_root_closure_,
duke@435 487 ParNewGeneration* par_gen_,
duke@435 488 ParRootScanWithBarrierTwoGensClosure* old_gen_root_closure_,
duke@435 489 ObjToScanQueueSet* task_queues_,
duke@435 490 ParallelTaskTerminator* terminator_) :
duke@435 491
duke@435 492 _par_scan_state(par_scan_state_),
duke@435 493 _to_space_closure(to_space_closure_),
duke@435 494 _old_gen_closure(old_gen_closure_),
duke@435 495 _to_space_root_closure(to_space_root_closure_),
duke@435 496 _old_gen_root_closure(old_gen_root_closure_),
duke@435 497 _par_gen(par_gen_),
duke@435 498 _task_queues(task_queues_),
duke@435 499 _terminator(terminator_)
duke@435 500 {}
duke@435 501
duke@435 502 void ParEvacuateFollowersClosure::do_void() {
duke@435 503 ObjToScanQueue* work_q = par_scan_state()->work_queue();
duke@435 504
duke@435 505 while (true) {
duke@435 506
duke@435 507 // Scan to-space and old-gen objs until we run out of both.
duke@435 508 oop obj_to_scan;
duke@435 509 par_scan_state()->trim_queues(0);
duke@435 510
duke@435 511 // We have no local work, attempt to steal from other threads.
duke@435 512
duke@435 513 // attempt to steal work from promoted.
duke@435 514 if (task_queues()->steal(par_scan_state()->thread_num(),
duke@435 515 par_scan_state()->hash_seed(),
duke@435 516 obj_to_scan)) {
duke@435 517 bool res = work_q->push(obj_to_scan);
duke@435 518 assert(res, "Empty queue should have room for a push.");
duke@435 519
duke@435 520 // if successful, goto Start.
duke@435 521 continue;
duke@435 522
duke@435 523 // try global overflow list.
duke@435 524 } else if (par_gen()->take_from_overflow_list(par_scan_state())) {
duke@435 525 continue;
duke@435 526 }
duke@435 527
duke@435 528 // Otherwise, offer termination.
duke@435 529 par_scan_state()->start_term_time();
duke@435 530 if (terminator()->offer_termination()) break;
duke@435 531 par_scan_state()->end_term_time();
duke@435 532 }
ysr@969 533 assert(par_gen()->_overflow_list == NULL && par_gen()->_num_par_pushes == 0,
ysr@969 534 "Broken overflow list?");
duke@435 535 // Finish the last termination pause.
duke@435 536 par_scan_state()->end_term_time();
duke@435 537 }
duke@435 538
duke@435 539 ParNewGenTask::ParNewGenTask(ParNewGeneration* gen, Generation* next_gen,
duke@435 540 HeapWord* young_old_boundary, ParScanThreadStateSet* state_set) :
duke@435 541 AbstractGangTask("ParNewGeneration collection"),
duke@435 542 _gen(gen), _next_gen(next_gen),
duke@435 543 _young_old_boundary(young_old_boundary),
duke@435 544 _state_set(state_set)
duke@435 545 {}
duke@435 546
duke@435 547 void ParNewGenTask::work(int i) {
duke@435 548 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 549 // Since this is being done in a separate thread, need new resource
duke@435 550 // and handle marks.
duke@435 551 ResourceMark rm;
duke@435 552 HandleMark hm;
duke@435 553 // We would need multiple old-gen queues otherwise.
ysr@1114 554 assert(gch->n_gens() == 2, "Par young collection currently only works with one older gen.");
duke@435 555
duke@435 556 Generation* old_gen = gch->next_gen(_gen);
duke@435 557
ysr@1580 558 ParScanThreadState& par_scan_state = _state_set->thread_state(i);
duke@435 559 par_scan_state.set_young_old_boundary(_young_old_boundary);
duke@435 560
duke@435 561 par_scan_state.start_strong_roots();
duke@435 562 gch->gen_process_strong_roots(_gen->level(),
jrose@1424 563 true, // Process younger gens, if any,
jrose@1424 564 // as strong roots.
jrose@1424 565 false, // no scope; this is parallel code
jrose@1424 566 false, // not collecting perm generation.
duke@435 567 SharedHeap::SO_AllClasses,
jrose@1424 568 &par_scan_state.to_space_root_closure(),
jrose@1424 569 true, // walk *all* scavengable nmethods
jrose@1424 570 &par_scan_state.older_gen_closure());
duke@435 571 par_scan_state.end_strong_roots();
duke@435 572
duke@435 573 // "evacuate followers".
duke@435 574 par_scan_state.evacuate_followers_closure().do_void();
duke@435 575 }
duke@435 576
duke@435 577 #ifdef _MSC_VER
duke@435 578 #pragma warning( push )
duke@435 579 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
duke@435 580 #endif
duke@435 581 ParNewGeneration::
duke@435 582 ParNewGeneration(ReservedSpace rs, size_t initial_byte_size, int level)
duke@435 583 : DefNewGeneration(rs, initial_byte_size, level, "PCopy"),
duke@435 584 _overflow_list(NULL),
duke@435 585 _is_alive_closure(this),
duke@435 586 _plab_stats(YoungPLABSize, PLABWeight)
duke@435 587 {
ysr@969 588 NOT_PRODUCT(_overflow_counter = ParGCWorkQueueOverflowInterval;)
ysr@969 589 NOT_PRODUCT(_num_par_pushes = 0;)
duke@435 590 _task_queues = new ObjToScanQueueSet(ParallelGCThreads);
duke@435 591 guarantee(_task_queues != NULL, "task_queues allocation failure.");
duke@435 592
duke@435 593 for (uint i1 = 0; i1 < ParallelGCThreads; i1++) {
jcoomes@2020 594 ObjToScanQueue *q = new ObjToScanQueue();
jcoomes@2020 595 guarantee(q != NULL, "work_queue Allocation failure.");
jcoomes@2020 596 _task_queues->register_queue(i1, q);
duke@435 597 }
duke@435 598
duke@435 599 for (uint i2 = 0; i2 < ParallelGCThreads; i2++)
duke@435 600 _task_queues->queue(i2)->initialize();
duke@435 601
jcoomes@2191 602 _overflow_stacks = NULL;
jcoomes@2191 603 if (ParGCUseLocalOverflow) {
jcoomes@2191 604 _overflow_stacks = NEW_C_HEAP_ARRAY(Stack<oop>, ParallelGCThreads);
jcoomes@2191 605 for (size_t i = 0; i < ParallelGCThreads; ++i) {
jcoomes@2191 606 new (_overflow_stacks + i) Stack<oop>();
ysr@1130 607 }
ysr@1130 608 }
ysr@1130 609
duke@435 610 if (UsePerfData) {
duke@435 611 EXCEPTION_MARK;
duke@435 612 ResourceMark rm;
duke@435 613
duke@435 614 const char* cname =
duke@435 615 PerfDataManager::counter_name(_gen_counters->name_space(), "threads");
duke@435 616 PerfDataManager::create_constant(SUN_GC, cname, PerfData::U_None,
duke@435 617 ParallelGCThreads, CHECK);
duke@435 618 }
duke@435 619 }
duke@435 620 #ifdef _MSC_VER
duke@435 621 #pragma warning( pop )
duke@435 622 #endif
duke@435 623
duke@435 624 // ParNewGeneration::
duke@435 625 ParKeepAliveClosure::ParKeepAliveClosure(ParScanWeakRefClosure* cl) :
duke@435 626 DefNewGeneration::KeepAliveClosure(cl), _par_cl(cl) {}
duke@435 627
coleenp@548 628 template <class T>
coleenp@548 629 void /*ParNewGeneration::*/ParKeepAliveClosure::do_oop_work(T* p) {
coleenp@548 630 #ifdef ASSERT
coleenp@548 631 {
coleenp@548 632 assert(!oopDesc::is_null(*p), "expected non-null ref");
coleenp@548 633 oop obj = oopDesc::load_decode_heap_oop_not_null(p);
coleenp@548 634 // We never expect to see a null reference being processed
coleenp@548 635 // as a weak reference.
coleenp@548 636 assert(obj->is_oop(), "expected an oop while scanning weak refs");
coleenp@548 637 }
coleenp@548 638 #endif // ASSERT
duke@435 639
duke@435 640 _par_cl->do_oop_nv(p);
duke@435 641
duke@435 642 if (Universe::heap()->is_in_reserved(p)) {
coleenp@548 643 oop obj = oopDesc::load_decode_heap_oop_not_null(p);
coleenp@548 644 _rs->write_ref_field_gc_par(p, obj);
duke@435 645 }
duke@435 646 }
duke@435 647
coleenp@548 648 void /*ParNewGeneration::*/ParKeepAliveClosure::do_oop(oop* p) { ParKeepAliveClosure::do_oop_work(p); }
coleenp@548 649 void /*ParNewGeneration::*/ParKeepAliveClosure::do_oop(narrowOop* p) { ParKeepAliveClosure::do_oop_work(p); }
coleenp@548 650
duke@435 651 // ParNewGeneration::
duke@435 652 KeepAliveClosure::KeepAliveClosure(ScanWeakRefClosure* cl) :
duke@435 653 DefNewGeneration::KeepAliveClosure(cl) {}
duke@435 654
coleenp@548 655 template <class T>
coleenp@548 656 void /*ParNewGeneration::*/KeepAliveClosure::do_oop_work(T* p) {
coleenp@548 657 #ifdef ASSERT
coleenp@548 658 {
coleenp@548 659 assert(!oopDesc::is_null(*p), "expected non-null ref");
coleenp@548 660 oop obj = oopDesc::load_decode_heap_oop_not_null(p);
coleenp@548 661 // We never expect to see a null reference being processed
coleenp@548 662 // as a weak reference.
coleenp@548 663 assert(obj->is_oop(), "expected an oop while scanning weak refs");
coleenp@548 664 }
coleenp@548 665 #endif // ASSERT
duke@435 666
duke@435 667 _cl->do_oop_nv(p);
duke@435 668
duke@435 669 if (Universe::heap()->is_in_reserved(p)) {
coleenp@548 670 oop obj = oopDesc::load_decode_heap_oop_not_null(p);
coleenp@548 671 _rs->write_ref_field_gc_par(p, obj);
duke@435 672 }
duke@435 673 }
duke@435 674
coleenp@548 675 void /*ParNewGeneration::*/KeepAliveClosure::do_oop(oop* p) { KeepAliveClosure::do_oop_work(p); }
coleenp@548 676 void /*ParNewGeneration::*/KeepAliveClosure::do_oop(narrowOop* p) { KeepAliveClosure::do_oop_work(p); }
coleenp@548 677
coleenp@548 678 template <class T> void ScanClosureWithParBarrier::do_oop_work(T* p) {
coleenp@548 679 T heap_oop = oopDesc::load_heap_oop(p);
coleenp@548 680 if (!oopDesc::is_null(heap_oop)) {
coleenp@548 681 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
duke@435 682 if ((HeapWord*)obj < _boundary) {
duke@435 683 assert(!_g->to()->is_in_reserved(obj), "Scanning field twice?");
coleenp@548 684 oop new_obj = obj->is_forwarded()
coleenp@548 685 ? obj->forwardee()
coleenp@548 686 : _g->DefNewGeneration::copy_to_survivor_space(obj);
coleenp@548 687 oopDesc::encode_store_heap_oop_not_null(p, new_obj);
duke@435 688 }
duke@435 689 if (_gc_barrier) {
duke@435 690 // If p points to a younger generation, mark the card.
duke@435 691 if ((HeapWord*)obj < _gen_boundary) {
duke@435 692 _rs->write_ref_field_gc_par(p, obj);
duke@435 693 }
duke@435 694 }
duke@435 695 }
duke@435 696 }
duke@435 697
coleenp@548 698 void ScanClosureWithParBarrier::do_oop(oop* p) { ScanClosureWithParBarrier::do_oop_work(p); }
coleenp@548 699 void ScanClosureWithParBarrier::do_oop(narrowOop* p) { ScanClosureWithParBarrier::do_oop_work(p); }
coleenp@548 700
duke@435 701 class ParNewRefProcTaskProxy: public AbstractGangTask {
duke@435 702 typedef AbstractRefProcTaskExecutor::ProcessTask ProcessTask;
duke@435 703 public:
duke@435 704 ParNewRefProcTaskProxy(ProcessTask& task, ParNewGeneration& gen,
duke@435 705 Generation& next_gen,
duke@435 706 HeapWord* young_old_boundary,
duke@435 707 ParScanThreadStateSet& state_set);
duke@435 708
duke@435 709 private:
duke@435 710 virtual void work(int i);
duke@435 711
duke@435 712 private:
duke@435 713 ParNewGeneration& _gen;
duke@435 714 ProcessTask& _task;
duke@435 715 Generation& _next_gen;
duke@435 716 HeapWord* _young_old_boundary;
duke@435 717 ParScanThreadStateSet& _state_set;
duke@435 718 };
duke@435 719
duke@435 720 ParNewRefProcTaskProxy::ParNewRefProcTaskProxy(
duke@435 721 ProcessTask& task, ParNewGeneration& gen,
duke@435 722 Generation& next_gen,
duke@435 723 HeapWord* young_old_boundary,
duke@435 724 ParScanThreadStateSet& state_set)
duke@435 725 : AbstractGangTask("ParNewGeneration parallel reference processing"),
duke@435 726 _gen(gen),
duke@435 727 _task(task),
duke@435 728 _next_gen(next_gen),
duke@435 729 _young_old_boundary(young_old_boundary),
duke@435 730 _state_set(state_set)
duke@435 731 {
duke@435 732 }
duke@435 733
duke@435 734 void ParNewRefProcTaskProxy::work(int i)
duke@435 735 {
duke@435 736 ResourceMark rm;
duke@435 737 HandleMark hm;
ysr@1580 738 ParScanThreadState& par_scan_state = _state_set.thread_state(i);
duke@435 739 par_scan_state.set_young_old_boundary(_young_old_boundary);
duke@435 740 _task.work(i, par_scan_state.is_alive_closure(),
duke@435 741 par_scan_state.keep_alive_closure(),
duke@435 742 par_scan_state.evacuate_followers_closure());
duke@435 743 }
duke@435 744
duke@435 745 class ParNewRefEnqueueTaskProxy: public AbstractGangTask {
duke@435 746 typedef AbstractRefProcTaskExecutor::EnqueueTask EnqueueTask;
duke@435 747 EnqueueTask& _task;
duke@435 748
duke@435 749 public:
duke@435 750 ParNewRefEnqueueTaskProxy(EnqueueTask& task)
duke@435 751 : AbstractGangTask("ParNewGeneration parallel reference enqueue"),
duke@435 752 _task(task)
duke@435 753 { }
duke@435 754
duke@435 755 virtual void work(int i)
duke@435 756 {
duke@435 757 _task.work(i);
duke@435 758 }
duke@435 759 };
duke@435 760
duke@435 761
duke@435 762 void ParNewRefProcTaskExecutor::execute(ProcessTask& task)
duke@435 763 {
duke@435 764 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 765 assert(gch->kind() == CollectedHeap::GenCollectedHeap,
duke@435 766 "not a generational heap");
duke@435 767 WorkGang* workers = gch->workers();
duke@435 768 assert(workers != NULL, "Need parallel worker threads.");
duke@435 769 ParNewRefProcTaskProxy rp_task(task, _generation, *_generation.next_gen(),
duke@435 770 _generation.reserved().end(), _state_set);
duke@435 771 workers->run_task(&rp_task);
ysr@1580 772 _state_set.reset(_generation.promotion_failed());
duke@435 773 }
duke@435 774
duke@435 775 void ParNewRefProcTaskExecutor::execute(EnqueueTask& task)
duke@435 776 {
duke@435 777 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 778 WorkGang* workers = gch->workers();
duke@435 779 assert(workers != NULL, "Need parallel worker threads.");
duke@435 780 ParNewRefEnqueueTaskProxy enq_task(task);
duke@435 781 workers->run_task(&enq_task);
duke@435 782 }
duke@435 783
duke@435 784 void ParNewRefProcTaskExecutor::set_single_threaded_mode()
duke@435 785 {
duke@435 786 _state_set.flush();
duke@435 787 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 788 gch->set_par_threads(0); // 0 ==> non-parallel.
duke@435 789 gch->save_marks();
duke@435 790 }
duke@435 791
duke@435 792 ScanClosureWithParBarrier::
duke@435 793 ScanClosureWithParBarrier(ParNewGeneration* g, bool gc_barrier) :
duke@435 794 ScanClosure(g, gc_barrier) {}
duke@435 795
duke@435 796 EvacuateFollowersClosureGeneral::
duke@435 797 EvacuateFollowersClosureGeneral(GenCollectedHeap* gch, int level,
duke@435 798 OopsInGenClosure* cur,
duke@435 799 OopsInGenClosure* older) :
duke@435 800 _gch(gch), _level(level),
duke@435 801 _scan_cur_or_nonheap(cur), _scan_older(older)
duke@435 802 {}
duke@435 803
duke@435 804 void EvacuateFollowersClosureGeneral::do_void() {
duke@435 805 do {
duke@435 806 // Beware: this call will lead to closure applications via virtual
duke@435 807 // calls.
duke@435 808 _gch->oop_since_save_marks_iterate(_level,
duke@435 809 _scan_cur_or_nonheap,
duke@435 810 _scan_older);
duke@435 811 } while (!_gch->no_allocs_since_save_marks(_level));
duke@435 812 }
duke@435 813
duke@435 814
duke@435 815 bool ParNewGeneration::_avoid_promotion_undo = false;
duke@435 816
duke@435 817 void ParNewGeneration::adjust_desired_tenuring_threshold() {
duke@435 818 // Set the desired survivor size to half the real survivor space
duke@435 819 _tenuring_threshold =
duke@435 820 age_table()->compute_tenuring_threshold(to()->capacity()/HeapWordSize);
duke@435 821 }
duke@435 822
duke@435 823 // A Generation that does parallel young-gen collection.
duke@435 824
duke@435 825 void ParNewGeneration::collect(bool full,
duke@435 826 bool clear_all_soft_refs,
duke@435 827 size_t size,
duke@435 828 bool is_tlab) {
duke@435 829 assert(full || size > 0, "otherwise we don't want to collect");
duke@435 830 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 831 assert(gch->kind() == CollectedHeap::GenCollectedHeap,
duke@435 832 "not a CMS generational heap");
duke@435 833 AdaptiveSizePolicy* size_policy = gch->gen_policy()->size_policy();
duke@435 834 WorkGang* workers = gch->workers();
duke@435 835 _next_gen = gch->next_gen(this);
duke@435 836 assert(_next_gen != NULL,
duke@435 837 "This must be the youngest gen, and not the only gen");
duke@435 838 assert(gch->n_gens() == 2,
duke@435 839 "Par collection currently only works with single older gen.");
duke@435 840 // Do we have to avoid promotion_undo?
duke@435 841 if (gch->collector_policy()->is_concurrent_mark_sweep_policy()) {
duke@435 842 set_avoid_promotion_undo(true);
duke@435 843 }
duke@435 844
duke@435 845 // If the next generation is too full to accomodate worst-case promotion
duke@435 846 // from this generation, pass on collection; let the next generation
duke@435 847 // do it.
duke@435 848 if (!collection_attempt_is_safe()) {
ysr@2243 849 gch->set_incremental_collection_failed(); // slight lie, in that we did not even attempt one
duke@435 850 return;
duke@435 851 }
duke@435 852 assert(to()->is_empty(), "Else not collection_attempt_is_safe");
duke@435 853
duke@435 854 init_assuming_no_promotion_failure();
duke@435 855
duke@435 856 if (UseAdaptiveSizePolicy) {
duke@435 857 set_survivor_overflow(false);
duke@435 858 size_policy->minor_collection_begin();
duke@435 859 }
duke@435 860
duke@435 861 TraceTime t1("GC", PrintGC && !PrintGCDetails, true, gclog_or_tty);
duke@435 862 // Capture heap used before collection (for printing).
duke@435 863 size_t gch_prev_used = gch->used();
duke@435 864
duke@435 865 SpecializationStats::clear();
duke@435 866
duke@435 867 age_table()->clear();
jmasa@698 868 to()->clear(SpaceDecorator::Mangle);
duke@435 869
duke@435 870 gch->save_marks();
duke@435 871 assert(workers != NULL, "Need parallel worker threads.");
duke@435 872 ParallelTaskTerminator _term(workers->total_workers(), task_queues());
duke@435 873 ParScanThreadStateSet thread_state_set(workers->total_workers(),
duke@435 874 *to(), *this, *_next_gen, *task_queues(),
ysr@1130 875 _overflow_stacks, desired_plab_sz(), _term);
duke@435 876
duke@435 877 ParNewGenTask tsk(this, _next_gen, reserved().end(), &thread_state_set);
duke@435 878 int n_workers = workers->total_workers();
duke@435 879 gch->set_par_threads(n_workers);
duke@435 880 gch->rem_set()->prepare_for_younger_refs_iterate(true);
duke@435 881 // It turns out that even when we're using 1 thread, doing the work in a
duke@435 882 // separate thread causes wide variance in run times. We can't help this
duke@435 883 // in the multi-threaded case, but we special-case n=1 here to get
duke@435 884 // repeatable measurements of the 1-thread overhead of the parallel code.
duke@435 885 if (n_workers > 1) {
jrose@1424 886 GenCollectedHeap::StrongRootsScope srs(gch);
duke@435 887 workers->run_task(&tsk);
duke@435 888 } else {
jrose@1424 889 GenCollectedHeap::StrongRootsScope srs(gch);
duke@435 890 tsk.work(0);
duke@435 891 }
ysr@1580 892 thread_state_set.reset(promotion_failed());
duke@435 893
duke@435 894 // Process (weak) reference objects found during scavenge.
ysr@888 895 ReferenceProcessor* rp = ref_processor();
duke@435 896 IsAliveClosure is_alive(this);
duke@435 897 ScanWeakRefClosure scan_weak_ref(this);
duke@435 898 KeepAliveClosure keep_alive(&scan_weak_ref);
duke@435 899 ScanClosure scan_without_gc_barrier(this, false);
duke@435 900 ScanClosureWithParBarrier scan_with_gc_barrier(this, true);
duke@435 901 set_promo_failure_scan_stack_closure(&scan_without_gc_barrier);
duke@435 902 EvacuateFollowersClosureGeneral evacuate_followers(gch, _level,
duke@435 903 &scan_without_gc_barrier, &scan_with_gc_barrier);
ysr@892 904 rp->setup_policy(clear_all_soft_refs);
ysr@888 905 if (rp->processing_is_mt()) {
duke@435 906 ParNewRefProcTaskExecutor task_executor(*this, thread_state_set);
ysr@888 907 rp->process_discovered_references(&is_alive, &keep_alive,
ysr@888 908 &evacuate_followers, &task_executor);
duke@435 909 } else {
duke@435 910 thread_state_set.flush();
duke@435 911 gch->set_par_threads(0); // 0 ==> non-parallel.
duke@435 912 gch->save_marks();
ysr@888 913 rp->process_discovered_references(&is_alive, &keep_alive,
ysr@888 914 &evacuate_followers, NULL);
duke@435 915 }
duke@435 916 if (!promotion_failed()) {
duke@435 917 // Swap the survivor spaces.
jmasa@698 918 eden()->clear(SpaceDecorator::Mangle);
jmasa@698 919 from()->clear(SpaceDecorator::Mangle);
jmasa@698 920 if (ZapUnusedHeapArea) {
jmasa@698 921 // This is now done here because of the piece-meal mangling which
jmasa@698 922 // can check for valid mangling at intermediate points in the
jmasa@698 923 // collection(s). When a minor collection fails to collect
jmasa@698 924 // sufficient space resizing of the young generation can occur
jmasa@698 925 // an redistribute the spaces in the young generation. Mangle
jmasa@698 926 // here so that unzapped regions don't get distributed to
jmasa@698 927 // other spaces.
jmasa@698 928 to()->mangle_unused_area();
jmasa@698 929 }
duke@435 930 swap_spaces();
duke@435 931
jmasa@1822 932 // A successful scavenge should restart the GC time limit count which is
jmasa@1822 933 // for full GC's.
jmasa@1822 934 size_policy->reset_gc_overhead_limit_count();
jmasa@1822 935
duke@435 936 assert(to()->is_empty(), "to space should be empty now");
duke@435 937 } else {
jcoomes@2191 938 assert(_promo_failure_scan_stack.is_empty(), "post condition");
jcoomes@2191 939 _promo_failure_scan_stack.clear(true); // Clear cached segments.
jcoomes@2191 940
duke@435 941 remove_forwarding_pointers();
duke@435 942 if (PrintGCDetails) {
duke@435 943 gclog_or_tty->print(" (promotion failed)");
duke@435 944 }
duke@435 945 // All the spaces are in play for mark-sweep.
duke@435 946 swap_spaces(); // Make life simpler for CMS || rescan; see 6483690.
duke@435 947 from()->set_next_compaction_space(to());
ysr@2243 948 gch->set_incremental_collection_failed();
ysr@1580 949 // Inform the next generation that a promotion failure occurred.
ysr@1580 950 _next_gen->promotion_failure_occurred();
jmasa@441 951
jmasa@441 952 // Reset the PromotionFailureALot counters.
jmasa@441 953 NOT_PRODUCT(Universe::heap()->reset_promotion_should_fail();)
duke@435 954 }
duke@435 955 // set new iteration safe limit for the survivor spaces
duke@435 956 from()->set_concurrent_iteration_safe_limit(from()->top());
duke@435 957 to()->set_concurrent_iteration_safe_limit(to()->top());
duke@435 958
duke@435 959 adjust_desired_tenuring_threshold();
duke@435 960 if (ResizePLAB) {
duke@435 961 plab_stats()->adjust_desired_plab_sz();
duke@435 962 }
duke@435 963
duke@435 964 if (PrintGC && !PrintGCDetails) {
duke@435 965 gch->print_heap_change(gch_prev_used);
duke@435 966 }
duke@435 967
jcoomes@2067 968 if (PrintGCDetails && ParallelGCVerbose) {
jcoomes@2067 969 TASKQUEUE_STATS_ONLY(thread_state_set.print_termination_stats());
jcoomes@2067 970 TASKQUEUE_STATS_ONLY(thread_state_set.print_taskqueue_stats());
jcoomes@2067 971 }
jcoomes@2065 972
duke@435 973 if (UseAdaptiveSizePolicy) {
duke@435 974 size_policy->minor_collection_end(gch->gc_cause());
duke@435 975 size_policy->avg_survived()->sample(from()->used());
duke@435 976 }
duke@435 977
duke@435 978 update_time_of_last_gc(os::javaTimeMillis());
duke@435 979
duke@435 980 SpecializationStats::print();
duke@435 981
ysr@888 982 rp->set_enqueuing_is_done(true);
ysr@888 983 if (rp->processing_is_mt()) {
duke@435 984 ParNewRefProcTaskExecutor task_executor(*this, thread_state_set);
ysr@888 985 rp->enqueue_discovered_references(&task_executor);
duke@435 986 } else {
ysr@888 987 rp->enqueue_discovered_references(NULL);
duke@435 988 }
ysr@888 989 rp->verify_no_references_recorded();
duke@435 990 }
duke@435 991
duke@435 992 static int sum;
duke@435 993 void ParNewGeneration::waste_some_time() {
duke@435 994 for (int i = 0; i < 100; i++) {
duke@435 995 sum += i;
duke@435 996 }
duke@435 997 }
duke@435 998
duke@435 999 static const oop ClaimedForwardPtr = oop(0x4);
duke@435 1000
duke@435 1001 // Because of concurrency, there are times where an object for which
duke@435 1002 // "is_forwarded()" is true contains an "interim" forwarding pointer
duke@435 1003 // value. Such a value will soon be overwritten with a real value.
duke@435 1004 // This method requires "obj" to have a forwarding pointer, and waits, if
duke@435 1005 // necessary for a real one to be inserted, and returns it.
duke@435 1006
duke@435 1007 oop ParNewGeneration::real_forwardee(oop obj) {
duke@435 1008 oop forward_ptr = obj->forwardee();
duke@435 1009 if (forward_ptr != ClaimedForwardPtr) {
duke@435 1010 return forward_ptr;
duke@435 1011 } else {
duke@435 1012 return real_forwardee_slow(obj);
duke@435 1013 }
duke@435 1014 }
duke@435 1015
duke@435 1016 oop ParNewGeneration::real_forwardee_slow(oop obj) {
duke@435 1017 // Spin-read if it is claimed but not yet written by another thread.
duke@435 1018 oop forward_ptr = obj->forwardee();
duke@435 1019 while (forward_ptr == ClaimedForwardPtr) {
duke@435 1020 waste_some_time();
duke@435 1021 assert(obj->is_forwarded(), "precondition");
duke@435 1022 forward_ptr = obj->forwardee();
duke@435 1023 }
duke@435 1024 return forward_ptr;
duke@435 1025 }
duke@435 1026
duke@435 1027 #ifdef ASSERT
duke@435 1028 bool ParNewGeneration::is_legal_forward_ptr(oop p) {
duke@435 1029 return
duke@435 1030 (_avoid_promotion_undo && p == ClaimedForwardPtr)
duke@435 1031 || Universe::heap()->is_in_reserved(p);
duke@435 1032 }
duke@435 1033 #endif
duke@435 1034
duke@435 1035 void ParNewGeneration::preserve_mark_if_necessary(oop obj, markOop m) {
duke@435 1036 if ((m != markOopDesc::prototype()) &&
duke@435 1037 (!UseBiasedLocking || (m != markOopDesc::biased_locking_prototype()))) {
duke@435 1038 MutexLocker ml(ParGCRareEvent_lock);
duke@435 1039 DefNewGeneration::preserve_mark_if_necessary(obj, m);
duke@435 1040 }
duke@435 1041 }
duke@435 1042
duke@435 1043 // Multiple GC threads may try to promote an object. If the object
duke@435 1044 // is successfully promoted, a forwarding pointer will be installed in
duke@435 1045 // the object in the young generation. This method claims the right
duke@435 1046 // to install the forwarding pointer before it copies the object,
duke@435 1047 // thus avoiding the need to undo the copy as in
duke@435 1048 // copy_to_survivor_space_avoiding_with_undo.
duke@435 1049
duke@435 1050 oop ParNewGeneration::copy_to_survivor_space_avoiding_promotion_undo(
duke@435 1051 ParScanThreadState* par_scan_state, oop old, size_t sz, markOop m) {
duke@435 1052 // In the sequential version, this assert also says that the object is
duke@435 1053 // not forwarded. That might not be the case here. It is the case that
duke@435 1054 // the caller observed it to be not forwarded at some time in the past.
duke@435 1055 assert(is_in_reserved(old), "shouldn't be scavenging this oop");
duke@435 1056
duke@435 1057 // The sequential code read "old->age()" below. That doesn't work here,
duke@435 1058 // since the age is in the mark word, and that might be overwritten with
duke@435 1059 // a forwarding pointer by a parallel thread. So we must save the mark
duke@435 1060 // word in a local and then analyze it.
duke@435 1061 oopDesc dummyOld;
duke@435 1062 dummyOld.set_mark(m);
duke@435 1063 assert(!dummyOld.is_forwarded(),
duke@435 1064 "should not be called with forwarding pointer mark word.");
duke@435 1065
duke@435 1066 oop new_obj = NULL;
duke@435 1067 oop forward_ptr;
duke@435 1068
duke@435 1069 // Try allocating obj in to-space (unless too old)
duke@435 1070 if (dummyOld.age() < tenuring_threshold()) {
duke@435 1071 new_obj = (oop)par_scan_state->alloc_in_to_space(sz);
duke@435 1072 if (new_obj == NULL) {
duke@435 1073 set_survivor_overflow(true);
duke@435 1074 }
duke@435 1075 }
duke@435 1076
duke@435 1077 if (new_obj == NULL) {
duke@435 1078 // Either to-space is full or we decided to promote
duke@435 1079 // try allocating obj tenured
duke@435 1080
duke@435 1081 // Attempt to install a null forwarding pointer (atomically),
duke@435 1082 // to claim the right to install the real forwarding pointer.
duke@435 1083 forward_ptr = old->forward_to_atomic(ClaimedForwardPtr);
duke@435 1084 if (forward_ptr != NULL) {
duke@435 1085 // someone else beat us to it.
duke@435 1086 return real_forwardee(old);
duke@435 1087 }
duke@435 1088
duke@435 1089 new_obj = _next_gen->par_promote(par_scan_state->thread_num(),
duke@435 1090 old, m, sz);
duke@435 1091
duke@435 1092 if (new_obj == NULL) {
duke@435 1093 // promotion failed, forward to self
duke@435 1094 _promotion_failed = true;
duke@435 1095 new_obj = old;
duke@435 1096
duke@435 1097 preserve_mark_if_necessary(old, m);
ysr@1580 1098 // Log the size of the maiden promotion failure
ysr@1580 1099 par_scan_state->log_promotion_failure(sz);
duke@435 1100 }
duke@435 1101
duke@435 1102 old->forward_to(new_obj);
duke@435 1103 forward_ptr = NULL;
duke@435 1104 } else {
duke@435 1105 // Is in to-space; do copying ourselves.
duke@435 1106 Copy::aligned_disjoint_words((HeapWord*)old, (HeapWord*)new_obj, sz);
duke@435 1107 forward_ptr = old->forward_to_atomic(new_obj);
duke@435 1108 // Restore the mark word copied above.
duke@435 1109 new_obj->set_mark(m);
duke@435 1110 // Increment age if obj still in new generation
duke@435 1111 new_obj->incr_age();
duke@435 1112 par_scan_state->age_table()->add(new_obj, sz);
duke@435 1113 }
duke@435 1114 assert(new_obj != NULL, "just checking");
duke@435 1115
duke@435 1116 if (forward_ptr == NULL) {
duke@435 1117 oop obj_to_push = new_obj;
duke@435 1118 if (par_scan_state->should_be_partially_scanned(obj_to_push, old)) {
duke@435 1119 // Length field used as index of next element to be scanned.
duke@435 1120 // Real length can be obtained from real_forwardee()
duke@435 1121 arrayOop(old)->set_length(0);
duke@435 1122 obj_to_push = old;
duke@435 1123 assert(obj_to_push->is_forwarded() && obj_to_push->forwardee() != obj_to_push,
duke@435 1124 "push forwarded object");
duke@435 1125 }
duke@435 1126 // Push it on one of the queues of to-be-scanned objects.
ysr@969 1127 bool simulate_overflow = false;
ysr@969 1128 NOT_PRODUCT(
ysr@969 1129 if (ParGCWorkQueueOverflowALot && should_simulate_overflow()) {
ysr@969 1130 // simulate a stack overflow
ysr@969 1131 simulate_overflow = true;
ysr@969 1132 }
ysr@969 1133 )
ysr@969 1134 if (simulate_overflow || !par_scan_state->work_queue()->push(obj_to_push)) {
duke@435 1135 // Add stats for overflow pushes.
duke@435 1136 if (Verbose && PrintGCDetails) {
duke@435 1137 gclog_or_tty->print("queue overflow!\n");
duke@435 1138 }
ysr@969 1139 push_on_overflow_list(old, par_scan_state);
jcoomes@2065 1140 TASKQUEUE_STATS_ONLY(par_scan_state->taskqueue_stats().record_overflow(0));
duke@435 1141 }
duke@435 1142
duke@435 1143 return new_obj;
duke@435 1144 }
duke@435 1145
duke@435 1146 // Oops. Someone beat us to it. Undo the allocation. Where did we
duke@435 1147 // allocate it?
duke@435 1148 if (is_in_reserved(new_obj)) {
duke@435 1149 // Must be in to_space.
duke@435 1150 assert(to()->is_in_reserved(new_obj), "Checking");
duke@435 1151 if (forward_ptr == ClaimedForwardPtr) {
duke@435 1152 // Wait to get the real forwarding pointer value.
duke@435 1153 forward_ptr = real_forwardee(old);
duke@435 1154 }
duke@435 1155 par_scan_state->undo_alloc_in_to_space((HeapWord*)new_obj, sz);
duke@435 1156 }
duke@435 1157
duke@435 1158 return forward_ptr;
duke@435 1159 }
duke@435 1160
duke@435 1161
duke@435 1162 // Multiple GC threads may try to promote the same object. If two
duke@435 1163 // or more GC threads copy the object, only one wins the race to install
duke@435 1164 // the forwarding pointer. The other threads have to undo their copy.
duke@435 1165
duke@435 1166 oop ParNewGeneration::copy_to_survivor_space_with_undo(
duke@435 1167 ParScanThreadState* par_scan_state, oop old, size_t sz, markOop m) {
duke@435 1168
duke@435 1169 // In the sequential version, this assert also says that the object is
duke@435 1170 // not forwarded. That might not be the case here. It is the case that
duke@435 1171 // the caller observed it to be not forwarded at some time in the past.
duke@435 1172 assert(is_in_reserved(old), "shouldn't be scavenging this oop");
duke@435 1173
duke@435 1174 // The sequential code read "old->age()" below. That doesn't work here,
duke@435 1175 // since the age is in the mark word, and that might be overwritten with
duke@435 1176 // a forwarding pointer by a parallel thread. So we must save the mark
duke@435 1177 // word here, install it in a local oopDesc, and then analyze it.
duke@435 1178 oopDesc dummyOld;
duke@435 1179 dummyOld.set_mark(m);
duke@435 1180 assert(!dummyOld.is_forwarded(),
duke@435 1181 "should not be called with forwarding pointer mark word.");
duke@435 1182
duke@435 1183 bool failed_to_promote = false;
duke@435 1184 oop new_obj = NULL;
duke@435 1185 oop forward_ptr;
duke@435 1186
duke@435 1187 // Try allocating obj in to-space (unless too old)
duke@435 1188 if (dummyOld.age() < tenuring_threshold()) {
duke@435 1189 new_obj = (oop)par_scan_state->alloc_in_to_space(sz);
duke@435 1190 if (new_obj == NULL) {
duke@435 1191 set_survivor_overflow(true);
duke@435 1192 }
duke@435 1193 }
duke@435 1194
duke@435 1195 if (new_obj == NULL) {
duke@435 1196 // Either to-space is full or we decided to promote
duke@435 1197 // try allocating obj tenured
duke@435 1198 new_obj = _next_gen->par_promote(par_scan_state->thread_num(),
duke@435 1199 old, m, sz);
duke@435 1200
duke@435 1201 if (new_obj == NULL) {
duke@435 1202 // promotion failed, forward to self
duke@435 1203 forward_ptr = old->forward_to_atomic(old);
duke@435 1204 new_obj = old;
duke@435 1205
duke@435 1206 if (forward_ptr != NULL) {
duke@435 1207 return forward_ptr; // someone else succeeded
duke@435 1208 }
duke@435 1209
duke@435 1210 _promotion_failed = true;
duke@435 1211 failed_to_promote = true;
duke@435 1212
duke@435 1213 preserve_mark_if_necessary(old, m);
ysr@1580 1214 // Log the size of the maiden promotion failure
ysr@1580 1215 par_scan_state->log_promotion_failure(sz);
duke@435 1216 }
duke@435 1217 } else {
duke@435 1218 // Is in to-space; do copying ourselves.
duke@435 1219 Copy::aligned_disjoint_words((HeapWord*)old, (HeapWord*)new_obj, sz);
duke@435 1220 // Restore the mark word copied above.
duke@435 1221 new_obj->set_mark(m);
duke@435 1222 // Increment age if new_obj still in new generation
duke@435 1223 new_obj->incr_age();
duke@435 1224 par_scan_state->age_table()->add(new_obj, sz);
duke@435 1225 }
duke@435 1226 assert(new_obj != NULL, "just checking");
duke@435 1227
duke@435 1228 // Now attempt to install the forwarding pointer (atomically).
duke@435 1229 // We have to copy the mark word before overwriting with forwarding
duke@435 1230 // ptr, so we can restore it below in the copy.
duke@435 1231 if (!failed_to_promote) {
duke@435 1232 forward_ptr = old->forward_to_atomic(new_obj);
duke@435 1233 }
duke@435 1234
duke@435 1235 if (forward_ptr == NULL) {
duke@435 1236 oop obj_to_push = new_obj;
duke@435 1237 if (par_scan_state->should_be_partially_scanned(obj_to_push, old)) {
duke@435 1238 // Length field used as index of next element to be scanned.
duke@435 1239 // Real length can be obtained from real_forwardee()
duke@435 1240 arrayOop(old)->set_length(0);
duke@435 1241 obj_to_push = old;
duke@435 1242 assert(obj_to_push->is_forwarded() && obj_to_push->forwardee() != obj_to_push,
duke@435 1243 "push forwarded object");
duke@435 1244 }
duke@435 1245 // Push it on one of the queues of to-be-scanned objects.
ysr@969 1246 bool simulate_overflow = false;
ysr@969 1247 NOT_PRODUCT(
ysr@969 1248 if (ParGCWorkQueueOverflowALot && should_simulate_overflow()) {
ysr@969 1249 // simulate a stack overflow
ysr@969 1250 simulate_overflow = true;
ysr@969 1251 }
ysr@969 1252 )
ysr@969 1253 if (simulate_overflow || !par_scan_state->work_queue()->push(obj_to_push)) {
duke@435 1254 // Add stats for overflow pushes.
ysr@969 1255 push_on_overflow_list(old, par_scan_state);
jcoomes@2065 1256 TASKQUEUE_STATS_ONLY(par_scan_state->taskqueue_stats().record_overflow(0));
duke@435 1257 }
duke@435 1258
duke@435 1259 return new_obj;
duke@435 1260 }
duke@435 1261
duke@435 1262 // Oops. Someone beat us to it. Undo the allocation. Where did we
duke@435 1263 // allocate it?
duke@435 1264 if (is_in_reserved(new_obj)) {
duke@435 1265 // Must be in to_space.
duke@435 1266 assert(to()->is_in_reserved(new_obj), "Checking");
duke@435 1267 par_scan_state->undo_alloc_in_to_space((HeapWord*)new_obj, sz);
duke@435 1268 } else {
duke@435 1269 assert(!_avoid_promotion_undo, "Should not be here if avoiding.");
duke@435 1270 _next_gen->par_promote_alloc_undo(par_scan_state->thread_num(),
duke@435 1271 (HeapWord*)new_obj, sz);
duke@435 1272 }
duke@435 1273
duke@435 1274 return forward_ptr;
duke@435 1275 }
duke@435 1276
ysr@969 1277 #ifndef PRODUCT
ysr@969 1278 // It's OK to call this multi-threaded; the worst thing
ysr@969 1279 // that can happen is that we'll get a bunch of closely
ysr@969 1280 // spaced simulated oveflows, but that's OK, in fact
ysr@969 1281 // probably good as it would exercise the overflow code
ysr@969 1282 // under contention.
ysr@969 1283 bool ParNewGeneration::should_simulate_overflow() {
ysr@969 1284 if (_overflow_counter-- <= 0) { // just being defensive
ysr@969 1285 _overflow_counter = ParGCWorkQueueOverflowInterval;
ysr@969 1286 return true;
ysr@969 1287 } else {
ysr@969 1288 return false;
ysr@969 1289 }
ysr@969 1290 }
ysr@969 1291 #endif
ysr@969 1292
ysr@1114 1293 // In case we are using compressed oops, we need to be careful.
ysr@1114 1294 // If the object being pushed is an object array, then its length
ysr@1114 1295 // field keeps track of the "grey boundary" at which the next
ysr@1114 1296 // incremental scan will be done (see ParGCArrayScanChunk).
ysr@1114 1297 // When using compressed oops, this length field is kept in the
ysr@1114 1298 // lower 32 bits of the erstwhile klass word and cannot be used
ysr@1114 1299 // for the overflow chaining pointer (OCP below). As such the OCP
ysr@1114 1300 // would itself need to be compressed into the top 32-bits in this
ysr@1114 1301 // case. Unfortunately, see below, in the event that we have a
ysr@1114 1302 // promotion failure, the node to be pushed on the list can be
ysr@1114 1303 // outside of the Java heap, so the heap-based pointer compression
ysr@1114 1304 // would not work (we would have potential aliasing between C-heap
ysr@1114 1305 // and Java-heap pointers). For this reason, when using compressed
ysr@1114 1306 // oops, we simply use a worker-thread-local, non-shared overflow
ysr@1114 1307 // list in the form of a growable array, with a slightly different
ysr@1114 1308 // overflow stack draining strategy. If/when we start using fat
ysr@1114 1309 // stacks here, we can go back to using (fat) pointer chains
ysr@1114 1310 // (although some performance comparisons would be useful since
ysr@1114 1311 // single global lists have their own performance disadvantages
ysr@1114 1312 // as we were made painfully aware not long ago, see 6786503).
ysr@969 1313 #define BUSY (oop(0x1aff1aff))
ysr@969 1314 void ParNewGeneration::push_on_overflow_list(oop from_space_obj, ParScanThreadState* par_scan_state) {
ysr@1114 1315 assert(is_in_reserved(from_space_obj), "Should be from this generation");
ysr@1130 1316 if (ParGCUseLocalOverflow) {
ysr@1114 1317 // In the case of compressed oops, we use a private, not-shared
ysr@1114 1318 // overflow stack.
ysr@1114 1319 par_scan_state->push_on_overflow_stack(from_space_obj);
ysr@1114 1320 } else {
ysr@1130 1321 assert(!UseCompressedOops, "Error");
ysr@1114 1322 // if the object has been forwarded to itself, then we cannot
ysr@1114 1323 // use the klass pointer for the linked list. Instead we have
ysr@1114 1324 // to allocate an oopDesc in the C-Heap and use that for the linked list.
ysr@1114 1325 // XXX This is horribly inefficient when a promotion failure occurs
ysr@1114 1326 // and should be fixed. XXX FIX ME !!!
ysr@969 1327 #ifndef PRODUCT
ysr@1114 1328 Atomic::inc_ptr(&_num_par_pushes);
ysr@1114 1329 assert(_num_par_pushes > 0, "Tautology");
ysr@969 1330 #endif
ysr@1114 1331 if (from_space_obj->forwardee() == from_space_obj) {
ysr@1114 1332 oopDesc* listhead = NEW_C_HEAP_ARRAY(oopDesc, 1);
ysr@1114 1333 listhead->forward_to(from_space_obj);
ysr@1114 1334 from_space_obj = listhead;
ysr@1114 1335 }
ysr@1114 1336 oop observed_overflow_list = _overflow_list;
ysr@1114 1337 oop cur_overflow_list;
ysr@1114 1338 do {
ysr@1114 1339 cur_overflow_list = observed_overflow_list;
ysr@1114 1340 if (cur_overflow_list != BUSY) {
ysr@1114 1341 from_space_obj->set_klass_to_list_ptr(cur_overflow_list);
ysr@1114 1342 } else {
ysr@1114 1343 from_space_obj->set_klass_to_list_ptr(NULL);
ysr@1114 1344 }
ysr@1114 1345 observed_overflow_list =
ysr@1114 1346 (oop)Atomic::cmpxchg_ptr(from_space_obj, &_overflow_list, cur_overflow_list);
ysr@1114 1347 } while (cur_overflow_list != observed_overflow_list);
duke@435 1348 }
duke@435 1349 }
duke@435 1350
ysr@1114 1351 bool ParNewGeneration::take_from_overflow_list(ParScanThreadState* par_scan_state) {
ysr@1114 1352 bool res;
ysr@1114 1353
ysr@1130 1354 if (ParGCUseLocalOverflow) {
ysr@1114 1355 res = par_scan_state->take_from_overflow_stack();
ysr@1114 1356 } else {
ysr@1130 1357 assert(!UseCompressedOops, "Error");
ysr@1114 1358 res = take_from_overflow_list_work(par_scan_state);
ysr@1114 1359 }
ysr@1114 1360 return res;
ysr@1114 1361 }
ysr@1114 1362
ysr@1114 1363
ysr@969 1364 // *NOTE*: The overflow list manipulation code here and
ysr@969 1365 // in CMSCollector:: are very similar in shape,
ysr@969 1366 // except that in the CMS case we thread the objects
ysr@969 1367 // directly into the list via their mark word, and do
ysr@969 1368 // not need to deal with special cases below related
ysr@969 1369 // to chunking of object arrays and promotion failure
ysr@969 1370 // handling.
ysr@969 1371 // CR 6797058 has been filed to attempt consolidation of
ysr@969 1372 // the common code.
ysr@969 1373 // Because of the common code, if you make any changes in
ysr@969 1374 // the code below, please check the CMS version to see if
ysr@969 1375 // similar changes might be needed.
ysr@969 1376 // See CMSCollector::par_take_from_overflow_list() for
ysr@969 1377 // more extensive documentation comments.
ysr@1114 1378 bool ParNewGeneration::take_from_overflow_list_work(ParScanThreadState* par_scan_state) {
duke@435 1379 ObjToScanQueue* work_q = par_scan_state->work_queue();
duke@435 1380 // How many to take?
ysr@1114 1381 size_t objsFromOverflow = MIN2((size_t)(work_q->max_elems() - work_q->size())/4,
ysr@969 1382 (size_t)ParGCDesiredObjsFromOverflowList);
duke@435 1383
jcoomes@2191 1384 assert(!UseCompressedOops, "Error");
ysr@1114 1385 assert(par_scan_state->overflow_stack() == NULL, "Error");
duke@435 1386 if (_overflow_list == NULL) return false;
duke@435 1387
duke@435 1388 // Otherwise, there was something there; try claiming the list.
ysr@969 1389 oop prefix = (oop)Atomic::xchg_ptr(BUSY, &_overflow_list);
ysr@969 1390 // Trim off a prefix of at most objsFromOverflow items
ysr@969 1391 Thread* tid = Thread::current();
ysr@969 1392 size_t spin_count = (size_t)ParallelGCThreads;
ysr@969 1393 size_t sleep_time_millis = MAX2((size_t)1, objsFromOverflow/100);
ysr@969 1394 for (size_t spin = 0; prefix == BUSY && spin < spin_count; spin++) {
ysr@969 1395 // someone grabbed it before we did ...
ysr@969 1396 // ... we spin for a short while...
ysr@969 1397 os::sleep(tid, sleep_time_millis, false);
ysr@969 1398 if (_overflow_list == NULL) {
ysr@969 1399 // nothing left to take
ysr@969 1400 return false;
ysr@969 1401 } else if (_overflow_list != BUSY) {
ysr@969 1402 // try and grab the prefix
ysr@969 1403 prefix = (oop)Atomic::xchg_ptr(BUSY, &_overflow_list);
ysr@969 1404 }
duke@435 1405 }
ysr@969 1406 if (prefix == NULL || prefix == BUSY) {
ysr@969 1407 // Nothing to take or waited long enough
ysr@969 1408 if (prefix == NULL) {
ysr@969 1409 // Write back the NULL in case we overwrote it with BUSY above
ysr@969 1410 // and it is still the same value.
ysr@969 1411 (void) Atomic::cmpxchg_ptr(NULL, &_overflow_list, BUSY);
ysr@969 1412 }
ysr@969 1413 return false;
ysr@969 1414 }
ysr@969 1415 assert(prefix != NULL && prefix != BUSY, "Error");
ysr@969 1416 size_t i = 1;
duke@435 1417 oop cur = prefix;
coleenp@602 1418 while (i < objsFromOverflow && cur->klass_or_null() != NULL) {
duke@435 1419 i++; cur = oop(cur->klass());
duke@435 1420 }
duke@435 1421
duke@435 1422 // Reattach remaining (suffix) to overflow list
ysr@969 1423 if (cur->klass_or_null() == NULL) {
ysr@969 1424 // Write back the NULL in lieu of the BUSY we wrote
ysr@969 1425 // above and it is still the same value.
ysr@969 1426 if (_overflow_list == BUSY) {
ysr@969 1427 (void) Atomic::cmpxchg_ptr(NULL, &_overflow_list, BUSY);
duke@435 1428 }
ysr@969 1429 } else {
ysr@969 1430 assert(cur->klass_or_null() != BUSY, "Error");
ysr@969 1431 oop suffix = oop(cur->klass()); // suffix will be put back on global list
ysr@969 1432 cur->set_klass_to_list_ptr(NULL); // break off suffix
ysr@969 1433 // It's possible that the list is still in the empty(busy) state
ysr@969 1434 // we left it in a short while ago; in that case we may be
ysr@969 1435 // able to place back the suffix.
ysr@969 1436 oop observed_overflow_list = _overflow_list;
ysr@969 1437 oop cur_overflow_list = observed_overflow_list;
ysr@969 1438 bool attached = false;
ysr@969 1439 while (observed_overflow_list == BUSY || observed_overflow_list == NULL) {
ysr@969 1440 observed_overflow_list =
ysr@969 1441 (oop) Atomic::cmpxchg_ptr(suffix, &_overflow_list, cur_overflow_list);
ysr@969 1442 if (cur_overflow_list == observed_overflow_list) {
ysr@969 1443 attached = true;
ysr@969 1444 break;
ysr@969 1445 } else cur_overflow_list = observed_overflow_list;
ysr@969 1446 }
ysr@969 1447 if (!attached) {
ysr@969 1448 // Too bad, someone else got in in between; we'll need to do a splice.
ysr@969 1449 // Find the last item of suffix list
ysr@969 1450 oop last = suffix;
ysr@969 1451 while (last->klass_or_null() != NULL) {
ysr@969 1452 last = oop(last->klass());
ysr@969 1453 }
ysr@969 1454 // Atomically prepend suffix to current overflow list
ysr@969 1455 observed_overflow_list = _overflow_list;
ysr@969 1456 do {
ysr@969 1457 cur_overflow_list = observed_overflow_list;
ysr@969 1458 if (cur_overflow_list != BUSY) {
ysr@969 1459 // Do the splice ...
ysr@969 1460 last->set_klass_to_list_ptr(cur_overflow_list);
ysr@969 1461 } else { // cur_overflow_list == BUSY
ysr@969 1462 last->set_klass_to_list_ptr(NULL);
ysr@969 1463 }
ysr@969 1464 observed_overflow_list =
ysr@969 1465 (oop)Atomic::cmpxchg_ptr(suffix, &_overflow_list, cur_overflow_list);
ysr@969 1466 } while (cur_overflow_list != observed_overflow_list);
duke@435 1467 }
duke@435 1468 }
duke@435 1469
duke@435 1470 // Push objects on prefix list onto this thread's work queue
ysr@969 1471 assert(prefix != NULL && prefix != BUSY, "program logic");
duke@435 1472 cur = prefix;
ysr@969 1473 ssize_t n = 0;
duke@435 1474 while (cur != NULL) {
duke@435 1475 oop obj_to_push = cur->forwardee();
ysr@889 1476 oop next = oop(cur->klass_or_null());
duke@435 1477 cur->set_klass(obj_to_push->klass());
ysr@969 1478 // This may be an array object that is self-forwarded. In that case, the list pointer
ysr@969 1479 // space, cur, is not in the Java heap, but rather in the C-heap and should be freed.
ysr@969 1480 if (!is_in_reserved(cur)) {
ysr@969 1481 // This can become a scaling bottleneck when there is work queue overflow coincident
ysr@969 1482 // with promotion failure.
ysr@969 1483 oopDesc* f = cur;
ysr@969 1484 FREE_C_HEAP_ARRAY(oopDesc, f);
ysr@969 1485 } else if (par_scan_state->should_be_partially_scanned(obj_to_push, cur)) {
ysr@969 1486 assert(arrayOop(cur)->length() == 0, "entire array remaining to be scanned");
duke@435 1487 obj_to_push = cur;
duke@435 1488 }
ysr@969 1489 bool ok = work_q->push(obj_to_push);
ysr@969 1490 assert(ok, "Should have succeeded");
duke@435 1491 cur = next;
duke@435 1492 n++;
duke@435 1493 }
jcoomes@2065 1494 TASKQUEUE_STATS_ONLY(par_scan_state->note_overflow_refill(n));
ysr@969 1495 #ifndef PRODUCT
ysr@969 1496 assert(_num_par_pushes >= n, "Too many pops?");
ysr@969 1497 Atomic::add_ptr(-(intptr_t)n, &_num_par_pushes);
ysr@969 1498 #endif
duke@435 1499 return true;
duke@435 1500 }
ysr@969 1501 #undef BUSY
duke@435 1502
duke@435 1503 void ParNewGeneration::ref_processor_init()
duke@435 1504 {
duke@435 1505 if (_ref_processor == NULL) {
duke@435 1506 // Allocate and initialize a reference processor
duke@435 1507 _ref_processor = ReferenceProcessor::create_ref_processor(
duke@435 1508 _reserved, // span
duke@435 1509 refs_discovery_is_atomic(), // atomic_discovery
duke@435 1510 refs_discovery_is_mt(), // mt_discovery
duke@435 1511 NULL, // is_alive_non_header
duke@435 1512 ParallelGCThreads,
duke@435 1513 ParallelRefProcEnabled);
duke@435 1514 }
duke@435 1515 }
duke@435 1516
duke@435 1517 const char* ParNewGeneration::name() const {
duke@435 1518 return "par new generation";
duke@435 1519 }
jmasa@2188 1520
jmasa@2188 1521 bool ParNewGeneration::in_use() {
jmasa@2188 1522 return UseParNewGC && ParallelGCThreads > 0;
jmasa@2188 1523 }

mercurial