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

Mon, 07 Jul 2014 10:12:40 +0200

author
stefank
date
Mon, 07 Jul 2014 10:12:40 +0200
changeset 6992
2c6ef90f030a
parent 6971
7426d8d76305
child 7031
ee019285a52c
permissions
-rw-r--r--

8049421: G1 Class Unloading after completing a concurrent mark cycle
Reviewed-by: tschatzl, ehelin, brutisso, coleenp, roland, iveresov
Contributed-by: stefan.karlsson@oracle.com, mikael.gerdin@oracle.com

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

mercurial