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

Wed, 28 May 2008 21:06:24 -0700

author
coleenp
date
Wed, 28 May 2008 21:06:24 -0700
changeset 602
feeb96a45707
parent 548
ba764ed4b6f2
child 631
d1605aabd0a1
child 698
12eea04c8b06
permissions
-rw-r--r--

6696264: assert("narrow oop can never be zero") for GCBasher & ParNewGC
Summary: decouple set_klass() with zeroing the gap when compressed.
Reviewed-by: kvn, ysr, jrose

duke@435 1 /*
duke@435 2 * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_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_,
duke@435 37 size_t desired_plab_sz_,
duke@435 38 ParallelTaskTerminator& term_) :
duke@435 39 _to_space(to_space_), _old_gen(old_gen_), _thread_num(thread_num_),
duke@435 40 _work_queue(work_queue_set_->queue(thread_num_)), _to_space_full(false),
duke@435 41 _ageTable(false), // false ==> not the global age table, no perf data.
duke@435 42 _to_space_alloc_buffer(desired_plab_sz_),
duke@435 43 _to_space_closure(gen_, this), _old_gen_closure(gen_, this),
duke@435 44 _to_space_root_closure(gen_, this), _old_gen_root_closure(gen_, this),
duke@435 45 _older_gen_closure(gen_, this),
duke@435 46 _evacuate_followers(this, &_to_space_closure, &_old_gen_closure,
duke@435 47 &_to_space_root_closure, gen_, &_old_gen_root_closure,
duke@435 48 work_queue_set_, &term_),
duke@435 49 _is_alive_closure(gen_), _scan_weak_ref_closure(gen_, this),
duke@435 50 _keep_alive_closure(&_scan_weak_ref_closure),
duke@435 51 _pushes(0), _pops(0), _steals(0), _steal_attempts(0), _term_attempts(0),
duke@435 52 _strong_roots_time(0.0), _term_time(0.0)
duke@435 53 {
duke@435 54 _survivor_chunk_array =
duke@435 55 (ChunkArray*) old_gen()->get_data_recorder(thread_num());
duke@435 56 _hash_seed = 17; // Might want to take time-based random value.
duke@435 57 _start = os::elapsedTime();
duke@435 58 _old_gen_closure.set_generation(old_gen_);
duke@435 59 _old_gen_root_closure.set_generation(old_gen_);
duke@435 60 }
duke@435 61 #ifdef _MSC_VER
duke@435 62 #pragma warning( pop )
duke@435 63 #endif
duke@435 64
duke@435 65 void ParScanThreadState::record_survivor_plab(HeapWord* plab_start,
duke@435 66 size_t plab_word_size) {
duke@435 67 ChunkArray* sca = survivor_chunk_array();
duke@435 68 if (sca != NULL) {
duke@435 69 // A non-null SCA implies that we want the PLAB data recorded.
duke@435 70 sca->record_sample(plab_start, plab_word_size);
duke@435 71 }
duke@435 72 }
duke@435 73
duke@435 74 bool ParScanThreadState::should_be_partially_scanned(oop new_obj, oop old_obj) const {
duke@435 75 return new_obj->is_objArray() &&
duke@435 76 arrayOop(new_obj)->length() > ParGCArrayScanChunk &&
duke@435 77 new_obj != old_obj;
duke@435 78 }
duke@435 79
duke@435 80 void ParScanThreadState::scan_partial_array_and_push_remainder(oop old) {
duke@435 81 assert(old->is_objArray(), "must be obj array");
duke@435 82 assert(old->is_forwarded(), "must be forwarded");
duke@435 83 assert(Universe::heap()->is_in_reserved(old), "must be in heap.");
duke@435 84 assert(!_old_gen->is_in(old), "must be in young generation.");
duke@435 85
duke@435 86 objArrayOop obj = objArrayOop(old->forwardee());
duke@435 87 // Process ParGCArrayScanChunk elements now
duke@435 88 // and push the remainder back onto queue
duke@435 89 int start = arrayOop(old)->length();
duke@435 90 int end = obj->length();
duke@435 91 int remainder = end - start;
duke@435 92 assert(start <= end, "just checking");
duke@435 93 if (remainder > 2 * ParGCArrayScanChunk) {
duke@435 94 // Test above combines last partial chunk with a full chunk
duke@435 95 end = start + ParGCArrayScanChunk;
duke@435 96 arrayOop(old)->set_length(end);
duke@435 97 // Push remainder.
duke@435 98 bool ok = work_queue()->push(old);
duke@435 99 assert(ok, "just popped, push must be okay");
duke@435 100 note_push();
duke@435 101 } else {
duke@435 102 // Restore length so that it can be used if there
duke@435 103 // is a promotion failure and forwarding pointers
duke@435 104 // must be removed.
duke@435 105 arrayOop(old)->set_length(end);
duke@435 106 }
coleenp@548 107
duke@435 108 // process our set of indices (include header in first chunk)
coleenp@548 109 // should make sure end is even (aligned to HeapWord in case of compressed oops)
duke@435 110 if ((HeapWord *)obj < young_old_boundary()) {
duke@435 111 // object is in to_space
coleenp@548 112 obj->oop_iterate_range(&_to_space_closure, start, end);
duke@435 113 } else {
duke@435 114 // object is in old generation
coleenp@548 115 obj->oop_iterate_range(&_old_gen_closure, start, end);
duke@435 116 }
duke@435 117 }
duke@435 118
duke@435 119
duke@435 120 void ParScanThreadState::trim_queues(int max_size) {
duke@435 121 ObjToScanQueue* queue = work_queue();
duke@435 122 while (queue->size() > (juint)max_size) {
duke@435 123 oop obj_to_scan;
duke@435 124 if (queue->pop_local(obj_to_scan)) {
duke@435 125 note_pop();
duke@435 126
duke@435 127 if ((HeapWord *)obj_to_scan < young_old_boundary()) {
duke@435 128 if (obj_to_scan->is_objArray() &&
duke@435 129 obj_to_scan->is_forwarded() &&
duke@435 130 obj_to_scan->forwardee() != obj_to_scan) {
duke@435 131 scan_partial_array_and_push_remainder(obj_to_scan);
duke@435 132 } else {
duke@435 133 // object is in to_space
duke@435 134 obj_to_scan->oop_iterate(&_to_space_closure);
duke@435 135 }
duke@435 136 } else {
duke@435 137 // object is in old generation
duke@435 138 obj_to_scan->oop_iterate(&_old_gen_closure);
duke@435 139 }
duke@435 140 }
duke@435 141 }
duke@435 142 }
duke@435 143
duke@435 144 HeapWord* ParScanThreadState::alloc_in_to_space_slow(size_t word_sz) {
duke@435 145
duke@435 146 // Otherwise, if the object is small enough, try to reallocate the
duke@435 147 // buffer.
duke@435 148 HeapWord* obj = NULL;
duke@435 149 if (!_to_space_full) {
duke@435 150 ParGCAllocBuffer* const plab = to_space_alloc_buffer();
duke@435 151 Space* const sp = to_space();
duke@435 152 if (word_sz * 100 <
duke@435 153 ParallelGCBufferWastePct * plab->word_sz()) {
duke@435 154 // Is small enough; abandon this buffer and start a new one.
duke@435 155 plab->retire(false, false);
duke@435 156 size_t buf_size = plab->word_sz();
duke@435 157 HeapWord* buf_space = sp->par_allocate(buf_size);
duke@435 158 if (buf_space == NULL) {
duke@435 159 const size_t min_bytes =
duke@435 160 ParGCAllocBuffer::min_size() << LogHeapWordSize;
duke@435 161 size_t free_bytes = sp->free();
duke@435 162 while(buf_space == NULL && free_bytes >= min_bytes) {
duke@435 163 buf_size = free_bytes >> LogHeapWordSize;
duke@435 164 assert(buf_size == (size_t)align_object_size(buf_size),
duke@435 165 "Invariant");
duke@435 166 buf_space = sp->par_allocate(buf_size);
duke@435 167 free_bytes = sp->free();
duke@435 168 }
duke@435 169 }
duke@435 170 if (buf_space != NULL) {
duke@435 171 plab->set_word_size(buf_size);
duke@435 172 plab->set_buf(buf_space);
duke@435 173 record_survivor_plab(buf_space, buf_size);
duke@435 174 obj = plab->allocate(word_sz);
duke@435 175 // Note that we cannot compare buf_size < word_sz below
duke@435 176 // because of AlignmentReserve (see ParGCAllocBuffer::allocate()).
duke@435 177 assert(obj != NULL || plab->words_remaining() < word_sz,
duke@435 178 "Else should have been able to allocate");
duke@435 179 // It's conceivable that we may be able to use the
duke@435 180 // buffer we just grabbed for subsequent small requests
duke@435 181 // even if not for this one.
duke@435 182 } else {
duke@435 183 // We're used up.
duke@435 184 _to_space_full = true;
duke@435 185 }
duke@435 186
duke@435 187 } else {
duke@435 188 // Too large; allocate the object individually.
duke@435 189 obj = sp->par_allocate(word_sz);
duke@435 190 }
duke@435 191 }
duke@435 192 return obj;
duke@435 193 }
duke@435 194
duke@435 195
duke@435 196 void ParScanThreadState::undo_alloc_in_to_space(HeapWord* obj,
duke@435 197 size_t word_sz) {
duke@435 198 // Is the alloc in the current alloc buffer?
duke@435 199 if (to_space_alloc_buffer()->contains(obj)) {
duke@435 200 assert(to_space_alloc_buffer()->contains(obj + word_sz - 1),
duke@435 201 "Should contain whole object.");
duke@435 202 to_space_alloc_buffer()->undo_allocation(obj, word_sz);
duke@435 203 } else {
duke@435 204 SharedHeap::fill_region_with_object(MemRegion(obj, word_sz));
duke@435 205 }
duke@435 206 }
duke@435 207
duke@435 208 class ParScanThreadStateSet: private ResourceArray {
duke@435 209 public:
duke@435 210 // Initializes states for the specified number of threads;
duke@435 211 ParScanThreadStateSet(int num_threads,
duke@435 212 Space& to_space,
duke@435 213 ParNewGeneration& gen,
duke@435 214 Generation& old_gen,
duke@435 215 ObjToScanQueueSet& queue_set,
duke@435 216 size_t desired_plab_sz,
duke@435 217 ParallelTaskTerminator& term);
duke@435 218 inline ParScanThreadState& thread_sate(int i);
duke@435 219 int pushes() { return _pushes; }
duke@435 220 int pops() { return _pops; }
duke@435 221 int steals() { return _steals; }
duke@435 222 void reset();
duke@435 223 void flush();
duke@435 224 private:
duke@435 225 ParallelTaskTerminator& _term;
duke@435 226 ParNewGeneration& _gen;
duke@435 227 Generation& _next_gen;
duke@435 228 // staticstics
duke@435 229 int _pushes;
duke@435 230 int _pops;
duke@435 231 int _steals;
duke@435 232 };
duke@435 233
duke@435 234
duke@435 235 ParScanThreadStateSet::ParScanThreadStateSet(
duke@435 236 int num_threads, Space& to_space, ParNewGeneration& gen,
duke@435 237 Generation& old_gen, ObjToScanQueueSet& queue_set,
duke@435 238 size_t desired_plab_sz, ParallelTaskTerminator& term)
duke@435 239 : ResourceArray(sizeof(ParScanThreadState), num_threads),
duke@435 240 _gen(gen), _next_gen(old_gen), _term(term),
duke@435 241 _pushes(0), _pops(0), _steals(0)
duke@435 242 {
duke@435 243 assert(num_threads > 0, "sanity check!");
duke@435 244 // Initialize states.
duke@435 245 for (int i = 0; i < num_threads; ++i) {
duke@435 246 new ((ParScanThreadState*)_data + i)
duke@435 247 ParScanThreadState(&to_space, &gen, &old_gen, i, &queue_set,
duke@435 248 desired_plab_sz, term);
duke@435 249 }
duke@435 250 }
duke@435 251
duke@435 252 inline ParScanThreadState& ParScanThreadStateSet::thread_sate(int i)
duke@435 253 {
duke@435 254 assert(i >= 0 && i < length(), "sanity check!");
duke@435 255 return ((ParScanThreadState*)_data)[i];
duke@435 256 }
duke@435 257
duke@435 258
duke@435 259 void ParScanThreadStateSet::reset()
duke@435 260 {
duke@435 261 _term.reset_for_reuse();
duke@435 262 }
duke@435 263
duke@435 264 void ParScanThreadStateSet::flush()
duke@435 265 {
duke@435 266 for (int i = 0; i < length(); ++i) {
duke@435 267 ParScanThreadState& par_scan_state = thread_sate(i);
duke@435 268
duke@435 269 // Flush stats related to To-space PLAB activity and
duke@435 270 // retire the last buffer.
duke@435 271 par_scan_state.to_space_alloc_buffer()->
duke@435 272 flush_stats_and_retire(_gen.plab_stats(),
duke@435 273 false /* !retain */);
duke@435 274
duke@435 275 // Every thread has its own age table. We need to merge
duke@435 276 // them all into one.
duke@435 277 ageTable *local_table = par_scan_state.age_table();
duke@435 278 _gen.age_table()->merge(local_table);
duke@435 279
duke@435 280 // Inform old gen that we're done.
duke@435 281 _next_gen.par_promote_alloc_done(i);
duke@435 282 _next_gen.par_oop_since_save_marks_iterate_done(i);
duke@435 283
duke@435 284 // Flush stats related to work queue activity (push/pop/steal)
duke@435 285 // This could conceivably become a bottleneck; if so, we'll put the
duke@435 286 // stat's gathering under the flag.
duke@435 287 if (PAR_STATS_ENABLED) {
duke@435 288 _pushes += par_scan_state.pushes();
duke@435 289 _pops += par_scan_state.pops();
duke@435 290 _steals += par_scan_state.steals();
duke@435 291 if (ParallelGCVerbose) {
duke@435 292 gclog_or_tty->print("Thread %d complete:\n"
duke@435 293 " Pushes: %7d Pops: %7d Steals %7d (in %d attempts)\n",
duke@435 294 i, par_scan_state.pushes(), par_scan_state.pops(),
duke@435 295 par_scan_state.steals(), par_scan_state.steal_attempts());
duke@435 296 if (par_scan_state.overflow_pushes() > 0 ||
duke@435 297 par_scan_state.overflow_refills() > 0) {
duke@435 298 gclog_or_tty->print(" Overflow pushes: %7d "
duke@435 299 "Overflow refills: %7d for %d objs.\n",
duke@435 300 par_scan_state.overflow_pushes(),
duke@435 301 par_scan_state.overflow_refills(),
duke@435 302 par_scan_state.overflow_refill_objs());
duke@435 303 }
duke@435 304
duke@435 305 double elapsed = par_scan_state.elapsed();
duke@435 306 double strong_roots = par_scan_state.strong_roots_time();
duke@435 307 double term = par_scan_state.term_time();
duke@435 308 gclog_or_tty->print(
duke@435 309 " Elapsed: %7.2f ms.\n"
duke@435 310 " Strong roots: %7.2f ms (%6.2f%%)\n"
duke@435 311 " Termination: %7.2f ms (%6.2f%%) (in %d entries)\n",
duke@435 312 elapsed * 1000.0,
duke@435 313 strong_roots * 1000.0, (strong_roots*100.0/elapsed),
duke@435 314 term * 1000.0, (term*100.0/elapsed),
duke@435 315 par_scan_state.term_attempts());
duke@435 316 }
duke@435 317 }
duke@435 318 }
duke@435 319 }
duke@435 320
duke@435 321 ParScanClosure::ParScanClosure(ParNewGeneration* g,
duke@435 322 ParScanThreadState* par_scan_state) :
duke@435 323 OopsInGenClosure(g), _par_scan_state(par_scan_state), _g(g)
duke@435 324 {
duke@435 325 assert(_g->level() == 0, "Optimized for youngest generation");
duke@435 326 _boundary = _g->reserved().end();
duke@435 327 }
duke@435 328
coleenp@548 329 void ParScanWithBarrierClosure::do_oop(oop* p) { ParScanClosure::do_oop_work(p, true, false); }
coleenp@548 330 void ParScanWithBarrierClosure::do_oop(narrowOop* p) { ParScanClosure::do_oop_work(p, true, false); }
coleenp@548 331
coleenp@548 332 void ParScanWithoutBarrierClosure::do_oop(oop* p) { ParScanClosure::do_oop_work(p, false, false); }
coleenp@548 333 void ParScanWithoutBarrierClosure::do_oop(narrowOop* p) { ParScanClosure::do_oop_work(p, false, false); }
coleenp@548 334
coleenp@548 335 void ParRootScanWithBarrierTwoGensClosure::do_oop(oop* p) { ParScanClosure::do_oop_work(p, true, true); }
coleenp@548 336 void ParRootScanWithBarrierTwoGensClosure::do_oop(narrowOop* p) { ParScanClosure::do_oop_work(p, true, true); }
coleenp@548 337
coleenp@548 338 void ParRootScanWithoutBarrierClosure::do_oop(oop* p) { ParScanClosure::do_oop_work(p, false, true); }
coleenp@548 339 void ParRootScanWithoutBarrierClosure::do_oop(narrowOop* p) { ParScanClosure::do_oop_work(p, false, true); }
coleenp@548 340
duke@435 341 ParScanWeakRefClosure::ParScanWeakRefClosure(ParNewGeneration* g,
duke@435 342 ParScanThreadState* par_scan_state)
duke@435 343 : ScanWeakRefClosure(g), _par_scan_state(par_scan_state)
coleenp@548 344 {}
coleenp@548 345
coleenp@548 346 void ParScanWeakRefClosure::do_oop(oop* p) { ParScanWeakRefClosure::do_oop_work(p); }
coleenp@548 347 void ParScanWeakRefClosure::do_oop(narrowOop* p) { ParScanWeakRefClosure::do_oop_work(p); }
duke@435 348
duke@435 349 #ifdef WIN32
duke@435 350 #pragma warning(disable: 4786) /* identifier was truncated to '255' characters in the browser information */
duke@435 351 #endif
duke@435 352
duke@435 353 ParEvacuateFollowersClosure::ParEvacuateFollowersClosure(
duke@435 354 ParScanThreadState* par_scan_state_,
duke@435 355 ParScanWithoutBarrierClosure* to_space_closure_,
duke@435 356 ParScanWithBarrierClosure* old_gen_closure_,
duke@435 357 ParRootScanWithoutBarrierClosure* to_space_root_closure_,
duke@435 358 ParNewGeneration* par_gen_,
duke@435 359 ParRootScanWithBarrierTwoGensClosure* old_gen_root_closure_,
duke@435 360 ObjToScanQueueSet* task_queues_,
duke@435 361 ParallelTaskTerminator* terminator_) :
duke@435 362
duke@435 363 _par_scan_state(par_scan_state_),
duke@435 364 _to_space_closure(to_space_closure_),
duke@435 365 _old_gen_closure(old_gen_closure_),
duke@435 366 _to_space_root_closure(to_space_root_closure_),
duke@435 367 _old_gen_root_closure(old_gen_root_closure_),
duke@435 368 _par_gen(par_gen_),
duke@435 369 _task_queues(task_queues_),
duke@435 370 _terminator(terminator_)
duke@435 371 {}
duke@435 372
duke@435 373 void ParEvacuateFollowersClosure::do_void() {
duke@435 374 ObjToScanQueue* work_q = par_scan_state()->work_queue();
duke@435 375
duke@435 376 while (true) {
duke@435 377
duke@435 378 // Scan to-space and old-gen objs until we run out of both.
duke@435 379 oop obj_to_scan;
duke@435 380 par_scan_state()->trim_queues(0);
duke@435 381
duke@435 382 // We have no local work, attempt to steal from other threads.
duke@435 383
duke@435 384 // attempt to steal work from promoted.
duke@435 385 par_scan_state()->note_steal_attempt();
duke@435 386 if (task_queues()->steal(par_scan_state()->thread_num(),
duke@435 387 par_scan_state()->hash_seed(),
duke@435 388 obj_to_scan)) {
duke@435 389 par_scan_state()->note_steal();
duke@435 390 bool res = work_q->push(obj_to_scan);
duke@435 391 assert(res, "Empty queue should have room for a push.");
duke@435 392
duke@435 393 par_scan_state()->note_push();
duke@435 394 // if successful, goto Start.
duke@435 395 continue;
duke@435 396
duke@435 397 // try global overflow list.
duke@435 398 } else if (par_gen()->take_from_overflow_list(par_scan_state())) {
duke@435 399 continue;
duke@435 400 }
duke@435 401
duke@435 402 // Otherwise, offer termination.
duke@435 403 par_scan_state()->start_term_time();
duke@435 404 if (terminator()->offer_termination()) break;
duke@435 405 par_scan_state()->end_term_time();
duke@435 406 }
duke@435 407 // Finish the last termination pause.
duke@435 408 par_scan_state()->end_term_time();
duke@435 409 }
duke@435 410
duke@435 411 ParNewGenTask::ParNewGenTask(ParNewGeneration* gen, Generation* next_gen,
duke@435 412 HeapWord* young_old_boundary, ParScanThreadStateSet* state_set) :
duke@435 413 AbstractGangTask("ParNewGeneration collection"),
duke@435 414 _gen(gen), _next_gen(next_gen),
duke@435 415 _young_old_boundary(young_old_boundary),
duke@435 416 _state_set(state_set)
duke@435 417 {}
duke@435 418
duke@435 419 void ParNewGenTask::work(int i) {
duke@435 420 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 421 // Since this is being done in a separate thread, need new resource
duke@435 422 // and handle marks.
duke@435 423 ResourceMark rm;
duke@435 424 HandleMark hm;
duke@435 425 // We would need multiple old-gen queues otherwise.
duke@435 426 guarantee(gch->n_gens() == 2,
duke@435 427 "Par young collection currently only works with one older gen.");
duke@435 428
duke@435 429 Generation* old_gen = gch->next_gen(_gen);
duke@435 430
duke@435 431 ParScanThreadState& par_scan_state = _state_set->thread_sate(i);
duke@435 432 par_scan_state.set_young_old_boundary(_young_old_boundary);
duke@435 433
duke@435 434 par_scan_state.start_strong_roots();
duke@435 435 gch->gen_process_strong_roots(_gen->level(),
duke@435 436 true, // Process younger gens, if any,
duke@435 437 // as strong roots.
duke@435 438 false,// not collecting perm generation.
duke@435 439 SharedHeap::SO_AllClasses,
duke@435 440 &par_scan_state.older_gen_closure(),
duke@435 441 &par_scan_state.to_space_root_closure());
duke@435 442 par_scan_state.end_strong_roots();
duke@435 443
duke@435 444 // "evacuate followers".
duke@435 445 par_scan_state.evacuate_followers_closure().do_void();
duke@435 446 }
duke@435 447
duke@435 448 #ifdef _MSC_VER
duke@435 449 #pragma warning( push )
duke@435 450 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
duke@435 451 #endif
duke@435 452 ParNewGeneration::
duke@435 453 ParNewGeneration(ReservedSpace rs, size_t initial_byte_size, int level)
duke@435 454 : DefNewGeneration(rs, initial_byte_size, level, "PCopy"),
duke@435 455 _overflow_list(NULL),
duke@435 456 _is_alive_closure(this),
duke@435 457 _plab_stats(YoungPLABSize, PLABWeight)
duke@435 458 {
duke@435 459 _task_queues = new ObjToScanQueueSet(ParallelGCThreads);
duke@435 460 guarantee(_task_queues != NULL, "task_queues allocation failure.");
duke@435 461
duke@435 462 for (uint i1 = 0; i1 < ParallelGCThreads; i1++) {
duke@435 463 ObjToScanQueuePadded *q_padded = new ObjToScanQueuePadded();
duke@435 464 guarantee(q_padded != NULL, "work_queue Allocation failure.");
duke@435 465
duke@435 466 _task_queues->register_queue(i1, &q_padded->work_queue);
duke@435 467 }
duke@435 468
duke@435 469 for (uint i2 = 0; i2 < ParallelGCThreads; i2++)
duke@435 470 _task_queues->queue(i2)->initialize();
duke@435 471
duke@435 472 if (UsePerfData) {
duke@435 473 EXCEPTION_MARK;
duke@435 474 ResourceMark rm;
duke@435 475
duke@435 476 const char* cname =
duke@435 477 PerfDataManager::counter_name(_gen_counters->name_space(), "threads");
duke@435 478 PerfDataManager::create_constant(SUN_GC, cname, PerfData::U_None,
duke@435 479 ParallelGCThreads, CHECK);
duke@435 480 }
duke@435 481 }
duke@435 482 #ifdef _MSC_VER
duke@435 483 #pragma warning( pop )
duke@435 484 #endif
duke@435 485
duke@435 486 // ParNewGeneration::
duke@435 487 ParKeepAliveClosure::ParKeepAliveClosure(ParScanWeakRefClosure* cl) :
duke@435 488 DefNewGeneration::KeepAliveClosure(cl), _par_cl(cl) {}
duke@435 489
coleenp@548 490 template <class T>
coleenp@548 491 void /*ParNewGeneration::*/ParKeepAliveClosure::do_oop_work(T* p) {
coleenp@548 492 #ifdef ASSERT
coleenp@548 493 {
coleenp@548 494 assert(!oopDesc::is_null(*p), "expected non-null ref");
coleenp@548 495 oop obj = oopDesc::load_decode_heap_oop_not_null(p);
coleenp@548 496 // We never expect to see a null reference being processed
coleenp@548 497 // as a weak reference.
coleenp@548 498 assert(obj->is_oop(), "expected an oop while scanning weak refs");
coleenp@548 499 }
coleenp@548 500 #endif // ASSERT
duke@435 501
duke@435 502 _par_cl->do_oop_nv(p);
duke@435 503
duke@435 504 if (Universe::heap()->is_in_reserved(p)) {
coleenp@548 505 oop obj = oopDesc::load_decode_heap_oop_not_null(p);
coleenp@548 506 _rs->write_ref_field_gc_par(p, obj);
duke@435 507 }
duke@435 508 }
duke@435 509
coleenp@548 510 void /*ParNewGeneration::*/ParKeepAliveClosure::do_oop(oop* p) { ParKeepAliveClosure::do_oop_work(p); }
coleenp@548 511 void /*ParNewGeneration::*/ParKeepAliveClosure::do_oop(narrowOop* p) { ParKeepAliveClosure::do_oop_work(p); }
coleenp@548 512
duke@435 513 // ParNewGeneration::
duke@435 514 KeepAliveClosure::KeepAliveClosure(ScanWeakRefClosure* cl) :
duke@435 515 DefNewGeneration::KeepAliveClosure(cl) {}
duke@435 516
coleenp@548 517 template <class T>
coleenp@548 518 void /*ParNewGeneration::*/KeepAliveClosure::do_oop_work(T* p) {
coleenp@548 519 #ifdef ASSERT
coleenp@548 520 {
coleenp@548 521 assert(!oopDesc::is_null(*p), "expected non-null ref");
coleenp@548 522 oop obj = oopDesc::load_decode_heap_oop_not_null(p);
coleenp@548 523 // We never expect to see a null reference being processed
coleenp@548 524 // as a weak reference.
coleenp@548 525 assert(obj->is_oop(), "expected an oop while scanning weak refs");
coleenp@548 526 }
coleenp@548 527 #endif // ASSERT
duke@435 528
duke@435 529 _cl->do_oop_nv(p);
duke@435 530
duke@435 531 if (Universe::heap()->is_in_reserved(p)) {
coleenp@548 532 oop obj = oopDesc::load_decode_heap_oop_not_null(p);
coleenp@548 533 _rs->write_ref_field_gc_par(p, obj);
duke@435 534 }
duke@435 535 }
duke@435 536
coleenp@548 537 void /*ParNewGeneration::*/KeepAliveClosure::do_oop(oop* p) { KeepAliveClosure::do_oop_work(p); }
coleenp@548 538 void /*ParNewGeneration::*/KeepAliveClosure::do_oop(narrowOop* p) { KeepAliveClosure::do_oop_work(p); }
coleenp@548 539
coleenp@548 540 template <class T> void ScanClosureWithParBarrier::do_oop_work(T* p) {
coleenp@548 541 T heap_oop = oopDesc::load_heap_oop(p);
coleenp@548 542 if (!oopDesc::is_null(heap_oop)) {
coleenp@548 543 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
duke@435 544 if ((HeapWord*)obj < _boundary) {
duke@435 545 assert(!_g->to()->is_in_reserved(obj), "Scanning field twice?");
coleenp@548 546 oop new_obj = obj->is_forwarded()
coleenp@548 547 ? obj->forwardee()
coleenp@548 548 : _g->DefNewGeneration::copy_to_survivor_space(obj);
coleenp@548 549 oopDesc::encode_store_heap_oop_not_null(p, new_obj);
duke@435 550 }
duke@435 551 if (_gc_barrier) {
duke@435 552 // If p points to a younger generation, mark the card.
duke@435 553 if ((HeapWord*)obj < _gen_boundary) {
duke@435 554 _rs->write_ref_field_gc_par(p, obj);
duke@435 555 }
duke@435 556 }
duke@435 557 }
duke@435 558 }
duke@435 559
coleenp@548 560 void ScanClosureWithParBarrier::do_oop(oop* p) { ScanClosureWithParBarrier::do_oop_work(p); }
coleenp@548 561 void ScanClosureWithParBarrier::do_oop(narrowOop* p) { ScanClosureWithParBarrier::do_oop_work(p); }
coleenp@548 562
duke@435 563 class ParNewRefProcTaskProxy: public AbstractGangTask {
duke@435 564 typedef AbstractRefProcTaskExecutor::ProcessTask ProcessTask;
duke@435 565 public:
duke@435 566 ParNewRefProcTaskProxy(ProcessTask& task, ParNewGeneration& gen,
duke@435 567 Generation& next_gen,
duke@435 568 HeapWord* young_old_boundary,
duke@435 569 ParScanThreadStateSet& state_set);
duke@435 570
duke@435 571 private:
duke@435 572 virtual void work(int i);
duke@435 573
duke@435 574 private:
duke@435 575 ParNewGeneration& _gen;
duke@435 576 ProcessTask& _task;
duke@435 577 Generation& _next_gen;
duke@435 578 HeapWord* _young_old_boundary;
duke@435 579 ParScanThreadStateSet& _state_set;
duke@435 580 };
duke@435 581
duke@435 582 ParNewRefProcTaskProxy::ParNewRefProcTaskProxy(
duke@435 583 ProcessTask& task, ParNewGeneration& gen,
duke@435 584 Generation& next_gen,
duke@435 585 HeapWord* young_old_boundary,
duke@435 586 ParScanThreadStateSet& state_set)
duke@435 587 : AbstractGangTask("ParNewGeneration parallel reference processing"),
duke@435 588 _gen(gen),
duke@435 589 _task(task),
duke@435 590 _next_gen(next_gen),
duke@435 591 _young_old_boundary(young_old_boundary),
duke@435 592 _state_set(state_set)
duke@435 593 {
duke@435 594 }
duke@435 595
duke@435 596 void ParNewRefProcTaskProxy::work(int i)
duke@435 597 {
duke@435 598 ResourceMark rm;
duke@435 599 HandleMark hm;
duke@435 600 ParScanThreadState& par_scan_state = _state_set.thread_sate(i);
duke@435 601 par_scan_state.set_young_old_boundary(_young_old_boundary);
duke@435 602 _task.work(i, par_scan_state.is_alive_closure(),
duke@435 603 par_scan_state.keep_alive_closure(),
duke@435 604 par_scan_state.evacuate_followers_closure());
duke@435 605 }
duke@435 606
duke@435 607 class ParNewRefEnqueueTaskProxy: public AbstractGangTask {
duke@435 608 typedef AbstractRefProcTaskExecutor::EnqueueTask EnqueueTask;
duke@435 609 EnqueueTask& _task;
duke@435 610
duke@435 611 public:
duke@435 612 ParNewRefEnqueueTaskProxy(EnqueueTask& task)
duke@435 613 : AbstractGangTask("ParNewGeneration parallel reference enqueue"),
duke@435 614 _task(task)
duke@435 615 { }
duke@435 616
duke@435 617 virtual void work(int i)
duke@435 618 {
duke@435 619 _task.work(i);
duke@435 620 }
duke@435 621 };
duke@435 622
duke@435 623
duke@435 624 void ParNewRefProcTaskExecutor::execute(ProcessTask& task)
duke@435 625 {
duke@435 626 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 627 assert(gch->kind() == CollectedHeap::GenCollectedHeap,
duke@435 628 "not a generational heap");
duke@435 629 WorkGang* workers = gch->workers();
duke@435 630 assert(workers != NULL, "Need parallel worker threads.");
duke@435 631 ParNewRefProcTaskProxy rp_task(task, _generation, *_generation.next_gen(),
duke@435 632 _generation.reserved().end(), _state_set);
duke@435 633 workers->run_task(&rp_task);
duke@435 634 _state_set.reset();
duke@435 635 }
duke@435 636
duke@435 637 void ParNewRefProcTaskExecutor::execute(EnqueueTask& task)
duke@435 638 {
duke@435 639 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 640 WorkGang* workers = gch->workers();
duke@435 641 assert(workers != NULL, "Need parallel worker threads.");
duke@435 642 ParNewRefEnqueueTaskProxy enq_task(task);
duke@435 643 workers->run_task(&enq_task);
duke@435 644 }
duke@435 645
duke@435 646 void ParNewRefProcTaskExecutor::set_single_threaded_mode()
duke@435 647 {
duke@435 648 _state_set.flush();
duke@435 649 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 650 gch->set_par_threads(0); // 0 ==> non-parallel.
duke@435 651 gch->save_marks();
duke@435 652 }
duke@435 653
duke@435 654 ScanClosureWithParBarrier::
duke@435 655 ScanClosureWithParBarrier(ParNewGeneration* g, bool gc_barrier) :
duke@435 656 ScanClosure(g, gc_barrier) {}
duke@435 657
duke@435 658 EvacuateFollowersClosureGeneral::
duke@435 659 EvacuateFollowersClosureGeneral(GenCollectedHeap* gch, int level,
duke@435 660 OopsInGenClosure* cur,
duke@435 661 OopsInGenClosure* older) :
duke@435 662 _gch(gch), _level(level),
duke@435 663 _scan_cur_or_nonheap(cur), _scan_older(older)
duke@435 664 {}
duke@435 665
duke@435 666 void EvacuateFollowersClosureGeneral::do_void() {
duke@435 667 do {
duke@435 668 // Beware: this call will lead to closure applications via virtual
duke@435 669 // calls.
duke@435 670 _gch->oop_since_save_marks_iterate(_level,
duke@435 671 _scan_cur_or_nonheap,
duke@435 672 _scan_older);
duke@435 673 } while (!_gch->no_allocs_since_save_marks(_level));
duke@435 674 }
duke@435 675
duke@435 676
duke@435 677 bool ParNewGeneration::_avoid_promotion_undo = false;
duke@435 678
duke@435 679 void ParNewGeneration::adjust_desired_tenuring_threshold() {
duke@435 680 // Set the desired survivor size to half the real survivor space
duke@435 681 _tenuring_threshold =
duke@435 682 age_table()->compute_tenuring_threshold(to()->capacity()/HeapWordSize);
duke@435 683 }
duke@435 684
duke@435 685 // A Generation that does parallel young-gen collection.
duke@435 686
duke@435 687 void ParNewGeneration::collect(bool full,
duke@435 688 bool clear_all_soft_refs,
duke@435 689 size_t size,
duke@435 690 bool is_tlab) {
duke@435 691 assert(full || size > 0, "otherwise we don't want to collect");
duke@435 692 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 693 assert(gch->kind() == CollectedHeap::GenCollectedHeap,
duke@435 694 "not a CMS generational heap");
duke@435 695 AdaptiveSizePolicy* size_policy = gch->gen_policy()->size_policy();
duke@435 696 WorkGang* workers = gch->workers();
duke@435 697 _next_gen = gch->next_gen(this);
duke@435 698 assert(_next_gen != NULL,
duke@435 699 "This must be the youngest gen, and not the only gen");
duke@435 700 assert(gch->n_gens() == 2,
duke@435 701 "Par collection currently only works with single older gen.");
duke@435 702 // Do we have to avoid promotion_undo?
duke@435 703 if (gch->collector_policy()->is_concurrent_mark_sweep_policy()) {
duke@435 704 set_avoid_promotion_undo(true);
duke@435 705 }
duke@435 706
duke@435 707 // If the next generation is too full to accomodate worst-case promotion
duke@435 708 // from this generation, pass on collection; let the next generation
duke@435 709 // do it.
duke@435 710 if (!collection_attempt_is_safe()) {
duke@435 711 gch->set_incremental_collection_will_fail();
duke@435 712 return;
duke@435 713 }
duke@435 714 assert(to()->is_empty(), "Else not collection_attempt_is_safe");
duke@435 715
duke@435 716 init_assuming_no_promotion_failure();
duke@435 717
duke@435 718 if (UseAdaptiveSizePolicy) {
duke@435 719 set_survivor_overflow(false);
duke@435 720 size_policy->minor_collection_begin();
duke@435 721 }
duke@435 722
duke@435 723 TraceTime t1("GC", PrintGC && !PrintGCDetails, true, gclog_or_tty);
duke@435 724 // Capture heap used before collection (for printing).
duke@435 725 size_t gch_prev_used = gch->used();
duke@435 726
duke@435 727 SpecializationStats::clear();
duke@435 728
duke@435 729 age_table()->clear();
duke@435 730 to()->clear();
duke@435 731
duke@435 732 gch->save_marks();
duke@435 733 assert(workers != NULL, "Need parallel worker threads.");
duke@435 734 ParallelTaskTerminator _term(workers->total_workers(), task_queues());
duke@435 735 ParScanThreadStateSet thread_state_set(workers->total_workers(),
duke@435 736 *to(), *this, *_next_gen, *task_queues(),
duke@435 737 desired_plab_sz(), _term);
duke@435 738
duke@435 739 ParNewGenTask tsk(this, _next_gen, reserved().end(), &thread_state_set);
duke@435 740 int n_workers = workers->total_workers();
duke@435 741 gch->set_par_threads(n_workers);
duke@435 742 gch->change_strong_roots_parity();
duke@435 743 gch->rem_set()->prepare_for_younger_refs_iterate(true);
duke@435 744 // It turns out that even when we're using 1 thread, doing the work in a
duke@435 745 // separate thread causes wide variance in run times. We can't help this
duke@435 746 // in the multi-threaded case, but we special-case n=1 here to get
duke@435 747 // repeatable measurements of the 1-thread overhead of the parallel code.
duke@435 748 if (n_workers > 1) {
duke@435 749 workers->run_task(&tsk);
duke@435 750 } else {
duke@435 751 tsk.work(0);
duke@435 752 }
duke@435 753 thread_state_set.reset();
duke@435 754
duke@435 755 if (PAR_STATS_ENABLED && ParallelGCVerbose) {
duke@435 756 gclog_or_tty->print("Thread totals:\n"
duke@435 757 " Pushes: %7d Pops: %7d Steals %7d (sum = %7d).\n",
duke@435 758 thread_state_set.pushes(), thread_state_set.pops(),
duke@435 759 thread_state_set.steals(),
duke@435 760 thread_state_set.pops()+thread_state_set.steals());
duke@435 761 }
duke@435 762 assert(thread_state_set.pushes() == thread_state_set.pops() + thread_state_set.steals(),
duke@435 763 "Or else the queues are leaky.");
duke@435 764
duke@435 765 // For now, process discovered weak refs sequentially.
duke@435 766 #ifdef COMPILER2
duke@435 767 ReferencePolicy *soft_ref_policy = new LRUMaxHeapPolicy();
duke@435 768 #else
duke@435 769 ReferencePolicy *soft_ref_policy = new LRUCurrentHeapPolicy();
duke@435 770 #endif // COMPILER2
duke@435 771
duke@435 772 // Process (weak) reference objects found during scavenge.
duke@435 773 IsAliveClosure is_alive(this);
duke@435 774 ScanWeakRefClosure scan_weak_ref(this);
duke@435 775 KeepAliveClosure keep_alive(&scan_weak_ref);
duke@435 776 ScanClosure scan_without_gc_barrier(this, false);
duke@435 777 ScanClosureWithParBarrier scan_with_gc_barrier(this, true);
duke@435 778 set_promo_failure_scan_stack_closure(&scan_without_gc_barrier);
duke@435 779 EvacuateFollowersClosureGeneral evacuate_followers(gch, _level,
duke@435 780 &scan_without_gc_barrier, &scan_with_gc_barrier);
duke@435 781 if (ref_processor()->processing_is_mt()) {
duke@435 782 ParNewRefProcTaskExecutor task_executor(*this, thread_state_set);
duke@435 783 ref_processor()->process_discovered_references(
duke@435 784 soft_ref_policy, &is_alive, &keep_alive, &evacuate_followers,
duke@435 785 &task_executor);
duke@435 786 } else {
duke@435 787 thread_state_set.flush();
duke@435 788 gch->set_par_threads(0); // 0 ==> non-parallel.
duke@435 789 gch->save_marks();
duke@435 790 ref_processor()->process_discovered_references(
duke@435 791 soft_ref_policy, &is_alive, &keep_alive, &evacuate_followers,
duke@435 792 NULL);
duke@435 793 }
duke@435 794 if (!promotion_failed()) {
duke@435 795 // Swap the survivor spaces.
duke@435 796 eden()->clear();
duke@435 797 from()->clear();
duke@435 798 swap_spaces();
duke@435 799
duke@435 800 assert(to()->is_empty(), "to space should be empty now");
duke@435 801 } else {
duke@435 802 assert(HandlePromotionFailure,
duke@435 803 "Should only be here if promotion failure handling is on");
duke@435 804 if (_promo_failure_scan_stack != NULL) {
duke@435 805 // Can be non-null because of reference processing.
duke@435 806 // Free stack with its elements.
duke@435 807 delete _promo_failure_scan_stack;
duke@435 808 _promo_failure_scan_stack = NULL;
duke@435 809 }
duke@435 810 remove_forwarding_pointers();
duke@435 811 if (PrintGCDetails) {
duke@435 812 gclog_or_tty->print(" (promotion failed)");
duke@435 813 }
duke@435 814 // All the spaces are in play for mark-sweep.
duke@435 815 swap_spaces(); // Make life simpler for CMS || rescan; see 6483690.
duke@435 816 from()->set_next_compaction_space(to());
duke@435 817 gch->set_incremental_collection_will_fail();
jmasa@441 818
jmasa@441 819 // Reset the PromotionFailureALot counters.
jmasa@441 820 NOT_PRODUCT(Universe::heap()->reset_promotion_should_fail();)
duke@435 821 }
duke@435 822 // set new iteration safe limit for the survivor spaces
duke@435 823 from()->set_concurrent_iteration_safe_limit(from()->top());
duke@435 824 to()->set_concurrent_iteration_safe_limit(to()->top());
duke@435 825
duke@435 826 adjust_desired_tenuring_threshold();
duke@435 827 if (ResizePLAB) {
duke@435 828 plab_stats()->adjust_desired_plab_sz();
duke@435 829 }
duke@435 830
duke@435 831 if (PrintGC && !PrintGCDetails) {
duke@435 832 gch->print_heap_change(gch_prev_used);
duke@435 833 }
duke@435 834
duke@435 835 if (UseAdaptiveSizePolicy) {
duke@435 836 size_policy->minor_collection_end(gch->gc_cause());
duke@435 837 size_policy->avg_survived()->sample(from()->used());
duke@435 838 }
duke@435 839
duke@435 840 update_time_of_last_gc(os::javaTimeMillis());
duke@435 841
duke@435 842 SpecializationStats::print();
duke@435 843
duke@435 844 ref_processor()->set_enqueuing_is_done(true);
duke@435 845 if (ref_processor()->processing_is_mt()) {
duke@435 846 ParNewRefProcTaskExecutor task_executor(*this, thread_state_set);
duke@435 847 ref_processor()->enqueue_discovered_references(&task_executor);
duke@435 848 } else {
duke@435 849 ref_processor()->enqueue_discovered_references(NULL);
duke@435 850 }
duke@435 851 ref_processor()->verify_no_references_recorded();
duke@435 852 }
duke@435 853
duke@435 854 static int sum;
duke@435 855 void ParNewGeneration::waste_some_time() {
duke@435 856 for (int i = 0; i < 100; i++) {
duke@435 857 sum += i;
duke@435 858 }
duke@435 859 }
duke@435 860
duke@435 861 static const oop ClaimedForwardPtr = oop(0x4);
duke@435 862
duke@435 863 // Because of concurrency, there are times where an object for which
duke@435 864 // "is_forwarded()" is true contains an "interim" forwarding pointer
duke@435 865 // value. Such a value will soon be overwritten with a real value.
duke@435 866 // This method requires "obj" to have a forwarding pointer, and waits, if
duke@435 867 // necessary for a real one to be inserted, and returns it.
duke@435 868
duke@435 869 oop ParNewGeneration::real_forwardee(oop obj) {
duke@435 870 oop forward_ptr = obj->forwardee();
duke@435 871 if (forward_ptr != ClaimedForwardPtr) {
duke@435 872 return forward_ptr;
duke@435 873 } else {
duke@435 874 return real_forwardee_slow(obj);
duke@435 875 }
duke@435 876 }
duke@435 877
duke@435 878 oop ParNewGeneration::real_forwardee_slow(oop obj) {
duke@435 879 // Spin-read if it is claimed but not yet written by another thread.
duke@435 880 oop forward_ptr = obj->forwardee();
duke@435 881 while (forward_ptr == ClaimedForwardPtr) {
duke@435 882 waste_some_time();
duke@435 883 assert(obj->is_forwarded(), "precondition");
duke@435 884 forward_ptr = obj->forwardee();
duke@435 885 }
duke@435 886 return forward_ptr;
duke@435 887 }
duke@435 888
duke@435 889 #ifdef ASSERT
duke@435 890 bool ParNewGeneration::is_legal_forward_ptr(oop p) {
duke@435 891 return
duke@435 892 (_avoid_promotion_undo && p == ClaimedForwardPtr)
duke@435 893 || Universe::heap()->is_in_reserved(p);
duke@435 894 }
duke@435 895 #endif
duke@435 896
duke@435 897 void ParNewGeneration::preserve_mark_if_necessary(oop obj, markOop m) {
duke@435 898 if ((m != markOopDesc::prototype()) &&
duke@435 899 (!UseBiasedLocking || (m != markOopDesc::biased_locking_prototype()))) {
duke@435 900 MutexLocker ml(ParGCRareEvent_lock);
duke@435 901 DefNewGeneration::preserve_mark_if_necessary(obj, m);
duke@435 902 }
duke@435 903 }
duke@435 904
duke@435 905 // Multiple GC threads may try to promote an object. If the object
duke@435 906 // is successfully promoted, a forwarding pointer will be installed in
duke@435 907 // the object in the young generation. This method claims the right
duke@435 908 // to install the forwarding pointer before it copies the object,
duke@435 909 // thus avoiding the need to undo the copy as in
duke@435 910 // copy_to_survivor_space_avoiding_with_undo.
duke@435 911
duke@435 912 oop ParNewGeneration::copy_to_survivor_space_avoiding_promotion_undo(
duke@435 913 ParScanThreadState* par_scan_state, oop old, size_t sz, markOop m) {
duke@435 914 // In the sequential version, this assert also says that the object is
duke@435 915 // not forwarded. That might not be the case here. It is the case that
duke@435 916 // the caller observed it to be not forwarded at some time in the past.
duke@435 917 assert(is_in_reserved(old), "shouldn't be scavenging this oop");
duke@435 918
duke@435 919 // The sequential code read "old->age()" below. That doesn't work here,
duke@435 920 // since the age is in the mark word, and that might be overwritten with
duke@435 921 // a forwarding pointer by a parallel thread. So we must save the mark
duke@435 922 // word in a local and then analyze it.
duke@435 923 oopDesc dummyOld;
duke@435 924 dummyOld.set_mark(m);
duke@435 925 assert(!dummyOld.is_forwarded(),
duke@435 926 "should not be called with forwarding pointer mark word.");
duke@435 927
duke@435 928 oop new_obj = NULL;
duke@435 929 oop forward_ptr;
duke@435 930
duke@435 931 // Try allocating obj in to-space (unless too old)
duke@435 932 if (dummyOld.age() < tenuring_threshold()) {
duke@435 933 new_obj = (oop)par_scan_state->alloc_in_to_space(sz);
duke@435 934 if (new_obj == NULL) {
duke@435 935 set_survivor_overflow(true);
duke@435 936 }
duke@435 937 }
duke@435 938
duke@435 939 if (new_obj == NULL) {
duke@435 940 // Either to-space is full or we decided to promote
duke@435 941 // try allocating obj tenured
duke@435 942
duke@435 943 // Attempt to install a null forwarding pointer (atomically),
duke@435 944 // to claim the right to install the real forwarding pointer.
duke@435 945 forward_ptr = old->forward_to_atomic(ClaimedForwardPtr);
duke@435 946 if (forward_ptr != NULL) {
duke@435 947 // someone else beat us to it.
duke@435 948 return real_forwardee(old);
duke@435 949 }
duke@435 950
duke@435 951 new_obj = _next_gen->par_promote(par_scan_state->thread_num(),
duke@435 952 old, m, sz);
duke@435 953
duke@435 954 if (new_obj == NULL) {
duke@435 955 if (!HandlePromotionFailure) {
duke@435 956 // A failed promotion likely means the MaxLiveObjectEvacuationRatio flag
duke@435 957 // is incorrectly set. In any case, its seriously wrong to be here!
duke@435 958 vm_exit_out_of_memory(sz*wordSize, "promotion");
duke@435 959 }
duke@435 960 // promotion failed, forward to self
duke@435 961 _promotion_failed = true;
duke@435 962 new_obj = old;
duke@435 963
duke@435 964 preserve_mark_if_necessary(old, m);
duke@435 965 }
duke@435 966
duke@435 967 old->forward_to(new_obj);
duke@435 968 forward_ptr = NULL;
duke@435 969 } else {
duke@435 970 // Is in to-space; do copying ourselves.
duke@435 971 Copy::aligned_disjoint_words((HeapWord*)old, (HeapWord*)new_obj, sz);
duke@435 972 forward_ptr = old->forward_to_atomic(new_obj);
duke@435 973 // Restore the mark word copied above.
duke@435 974 new_obj->set_mark(m);
duke@435 975 // Increment age if obj still in new generation
duke@435 976 new_obj->incr_age();
duke@435 977 par_scan_state->age_table()->add(new_obj, sz);
duke@435 978 }
duke@435 979 assert(new_obj != NULL, "just checking");
duke@435 980
duke@435 981 if (forward_ptr == NULL) {
duke@435 982 oop obj_to_push = new_obj;
duke@435 983 if (par_scan_state->should_be_partially_scanned(obj_to_push, old)) {
duke@435 984 // Length field used as index of next element to be scanned.
duke@435 985 // Real length can be obtained from real_forwardee()
duke@435 986 arrayOop(old)->set_length(0);
duke@435 987 obj_to_push = old;
duke@435 988 assert(obj_to_push->is_forwarded() && obj_to_push->forwardee() != obj_to_push,
duke@435 989 "push forwarded object");
duke@435 990 }
duke@435 991 // Push it on one of the queues of to-be-scanned objects.
duke@435 992 if (!par_scan_state->work_queue()->push(obj_to_push)) {
duke@435 993 // Add stats for overflow pushes.
duke@435 994 if (Verbose && PrintGCDetails) {
duke@435 995 gclog_or_tty->print("queue overflow!\n");
duke@435 996 }
duke@435 997 push_on_overflow_list(old);
duke@435 998 par_scan_state->note_overflow_push();
duke@435 999 }
duke@435 1000 par_scan_state->note_push();
duke@435 1001
duke@435 1002 return new_obj;
duke@435 1003 }
duke@435 1004
duke@435 1005 // Oops. Someone beat us to it. Undo the allocation. Where did we
duke@435 1006 // allocate it?
duke@435 1007 if (is_in_reserved(new_obj)) {
duke@435 1008 // Must be in to_space.
duke@435 1009 assert(to()->is_in_reserved(new_obj), "Checking");
duke@435 1010 if (forward_ptr == ClaimedForwardPtr) {
duke@435 1011 // Wait to get the real forwarding pointer value.
duke@435 1012 forward_ptr = real_forwardee(old);
duke@435 1013 }
duke@435 1014 par_scan_state->undo_alloc_in_to_space((HeapWord*)new_obj, sz);
duke@435 1015 }
duke@435 1016
duke@435 1017 return forward_ptr;
duke@435 1018 }
duke@435 1019
duke@435 1020
duke@435 1021 // Multiple GC threads may try to promote the same object. If two
duke@435 1022 // or more GC threads copy the object, only one wins the race to install
duke@435 1023 // the forwarding pointer. The other threads have to undo their copy.
duke@435 1024
duke@435 1025 oop ParNewGeneration::copy_to_survivor_space_with_undo(
duke@435 1026 ParScanThreadState* par_scan_state, oop old, size_t sz, markOop m) {
duke@435 1027
duke@435 1028 // In the sequential version, this assert also says that the object is
duke@435 1029 // not forwarded. That might not be the case here. It is the case that
duke@435 1030 // the caller observed it to be not forwarded at some time in the past.
duke@435 1031 assert(is_in_reserved(old), "shouldn't be scavenging this oop");
duke@435 1032
duke@435 1033 // The sequential code read "old->age()" below. That doesn't work here,
duke@435 1034 // since the age is in the mark word, and that might be overwritten with
duke@435 1035 // a forwarding pointer by a parallel thread. So we must save the mark
duke@435 1036 // word here, install it in a local oopDesc, and then analyze it.
duke@435 1037 oopDesc dummyOld;
duke@435 1038 dummyOld.set_mark(m);
duke@435 1039 assert(!dummyOld.is_forwarded(),
duke@435 1040 "should not be called with forwarding pointer mark word.");
duke@435 1041
duke@435 1042 bool failed_to_promote = false;
duke@435 1043 oop new_obj = NULL;
duke@435 1044 oop forward_ptr;
duke@435 1045
duke@435 1046 // Try allocating obj in to-space (unless too old)
duke@435 1047 if (dummyOld.age() < tenuring_threshold()) {
duke@435 1048 new_obj = (oop)par_scan_state->alloc_in_to_space(sz);
duke@435 1049 if (new_obj == NULL) {
duke@435 1050 set_survivor_overflow(true);
duke@435 1051 }
duke@435 1052 }
duke@435 1053
duke@435 1054 if (new_obj == NULL) {
duke@435 1055 // Either to-space is full or we decided to promote
duke@435 1056 // try allocating obj tenured
duke@435 1057 new_obj = _next_gen->par_promote(par_scan_state->thread_num(),
duke@435 1058 old, m, sz);
duke@435 1059
duke@435 1060 if (new_obj == NULL) {
duke@435 1061 if (!HandlePromotionFailure) {
duke@435 1062 // A failed promotion likely means the MaxLiveObjectEvacuationRatio
duke@435 1063 // flag is incorrectly set. In any case, its seriously wrong to be
duke@435 1064 // here!
duke@435 1065 vm_exit_out_of_memory(sz*wordSize, "promotion");
duke@435 1066 }
duke@435 1067 // promotion failed, forward to self
duke@435 1068 forward_ptr = old->forward_to_atomic(old);
duke@435 1069 new_obj = old;
duke@435 1070
duke@435 1071 if (forward_ptr != NULL) {
duke@435 1072 return forward_ptr; // someone else succeeded
duke@435 1073 }
duke@435 1074
duke@435 1075 _promotion_failed = true;
duke@435 1076 failed_to_promote = true;
duke@435 1077
duke@435 1078 preserve_mark_if_necessary(old, m);
duke@435 1079 }
duke@435 1080 } else {
duke@435 1081 // Is in to-space; do copying ourselves.
duke@435 1082 Copy::aligned_disjoint_words((HeapWord*)old, (HeapWord*)new_obj, sz);
duke@435 1083 // Restore the mark word copied above.
duke@435 1084 new_obj->set_mark(m);
duke@435 1085 // Increment age if new_obj still in new generation
duke@435 1086 new_obj->incr_age();
duke@435 1087 par_scan_state->age_table()->add(new_obj, sz);
duke@435 1088 }
duke@435 1089 assert(new_obj != NULL, "just checking");
duke@435 1090
duke@435 1091 // Now attempt to install the forwarding pointer (atomically).
duke@435 1092 // We have to copy the mark word before overwriting with forwarding
duke@435 1093 // ptr, so we can restore it below in the copy.
duke@435 1094 if (!failed_to_promote) {
duke@435 1095 forward_ptr = old->forward_to_atomic(new_obj);
duke@435 1096 }
duke@435 1097
duke@435 1098 if (forward_ptr == NULL) {
duke@435 1099 oop obj_to_push = new_obj;
duke@435 1100 if (par_scan_state->should_be_partially_scanned(obj_to_push, old)) {
duke@435 1101 // Length field used as index of next element to be scanned.
duke@435 1102 // Real length can be obtained from real_forwardee()
duke@435 1103 arrayOop(old)->set_length(0);
duke@435 1104 obj_to_push = old;
duke@435 1105 assert(obj_to_push->is_forwarded() && obj_to_push->forwardee() != obj_to_push,
duke@435 1106 "push forwarded object");
duke@435 1107 }
duke@435 1108 // Push it on one of the queues of to-be-scanned objects.
duke@435 1109 if (!par_scan_state->work_queue()->push(obj_to_push)) {
duke@435 1110 // Add stats for overflow pushes.
duke@435 1111 push_on_overflow_list(old);
duke@435 1112 par_scan_state->note_overflow_push();
duke@435 1113 }
duke@435 1114 par_scan_state->note_push();
duke@435 1115
duke@435 1116 return new_obj;
duke@435 1117 }
duke@435 1118
duke@435 1119 // Oops. Someone beat us to it. Undo the allocation. Where did we
duke@435 1120 // allocate it?
duke@435 1121 if (is_in_reserved(new_obj)) {
duke@435 1122 // Must be in to_space.
duke@435 1123 assert(to()->is_in_reserved(new_obj), "Checking");
duke@435 1124 par_scan_state->undo_alloc_in_to_space((HeapWord*)new_obj, sz);
duke@435 1125 } else {
duke@435 1126 assert(!_avoid_promotion_undo, "Should not be here if avoiding.");
duke@435 1127 _next_gen->par_promote_alloc_undo(par_scan_state->thread_num(),
duke@435 1128 (HeapWord*)new_obj, sz);
duke@435 1129 }
duke@435 1130
duke@435 1131 return forward_ptr;
duke@435 1132 }
duke@435 1133
duke@435 1134 void ParNewGeneration::push_on_overflow_list(oop from_space_obj) {
duke@435 1135 oop cur_overflow_list = _overflow_list;
duke@435 1136 // if the object has been forwarded to itself, then we cannot
duke@435 1137 // use the klass pointer for the linked list. Instead we have
duke@435 1138 // to allocate an oopDesc in the C-Heap and use that for the linked list.
duke@435 1139 if (from_space_obj->forwardee() == from_space_obj) {
duke@435 1140 oopDesc* listhead = NEW_C_HEAP_ARRAY(oopDesc, 1);
duke@435 1141 listhead->forward_to(from_space_obj);
duke@435 1142 from_space_obj = listhead;
duke@435 1143 }
duke@435 1144 while (true) {
duke@435 1145 from_space_obj->set_klass_to_list_ptr(cur_overflow_list);
duke@435 1146 oop observed_overflow_list =
duke@435 1147 (oop)Atomic::cmpxchg_ptr(from_space_obj, &_overflow_list, cur_overflow_list);
duke@435 1148 if (observed_overflow_list == cur_overflow_list) break;
duke@435 1149 // Otherwise...
duke@435 1150 cur_overflow_list = observed_overflow_list;
duke@435 1151 }
duke@435 1152 }
duke@435 1153
duke@435 1154 bool
duke@435 1155 ParNewGeneration::take_from_overflow_list(ParScanThreadState* par_scan_state) {
duke@435 1156 ObjToScanQueue* work_q = par_scan_state->work_queue();
duke@435 1157 // How many to take?
duke@435 1158 int objsFromOverflow = MIN2(work_q->max_elems()/4,
duke@435 1159 (juint)ParGCDesiredObjsFromOverflowList);
duke@435 1160
duke@435 1161 if (_overflow_list == NULL) return false;
duke@435 1162
duke@435 1163 // Otherwise, there was something there; try claiming the list.
duke@435 1164 oop prefix = (oop)Atomic::xchg_ptr(NULL, &_overflow_list);
duke@435 1165
duke@435 1166 if (prefix == NULL) {
duke@435 1167 return false;
duke@435 1168 }
duke@435 1169 // Trim off a prefix of at most objsFromOverflow items
duke@435 1170 int i = 1;
duke@435 1171 oop cur = prefix;
coleenp@602 1172 while (i < objsFromOverflow && cur->klass_or_null() != NULL) {
duke@435 1173 i++; cur = oop(cur->klass());
duke@435 1174 }
duke@435 1175
duke@435 1176 // Reattach remaining (suffix) to overflow list
coleenp@602 1177 if (cur->klass_or_null() != NULL) {
duke@435 1178 oop suffix = oop(cur->klass());
duke@435 1179 cur->set_klass_to_list_ptr(NULL);
duke@435 1180
duke@435 1181 // Find last item of suffix list
duke@435 1182 oop last = suffix;
coleenp@602 1183 while (last->klass_or_null() != NULL) {
duke@435 1184 last = oop(last->klass());
duke@435 1185 }
duke@435 1186 // Atomically prepend suffix to current overflow list
duke@435 1187 oop cur_overflow_list = _overflow_list;
duke@435 1188 while (true) {
duke@435 1189 last->set_klass_to_list_ptr(cur_overflow_list);
duke@435 1190 oop observed_overflow_list =
duke@435 1191 (oop)Atomic::cmpxchg_ptr(suffix, &_overflow_list, cur_overflow_list);
duke@435 1192 if (observed_overflow_list == cur_overflow_list) break;
duke@435 1193 // Otherwise...
duke@435 1194 cur_overflow_list = observed_overflow_list;
duke@435 1195 }
duke@435 1196 }
duke@435 1197
duke@435 1198 // Push objects on prefix list onto this thread's work queue
duke@435 1199 assert(cur != NULL, "program logic");
duke@435 1200 cur = prefix;
duke@435 1201 int n = 0;
duke@435 1202 while (cur != NULL) {
duke@435 1203 oop obj_to_push = cur->forwardee();
duke@435 1204 oop next = oop(cur->klass());
duke@435 1205 cur->set_klass(obj_to_push->klass());
duke@435 1206 if (par_scan_state->should_be_partially_scanned(obj_to_push, cur)) {
duke@435 1207 obj_to_push = cur;
duke@435 1208 assert(arrayOop(cur)->length() == 0, "entire array remaining to be scanned");
duke@435 1209 }
duke@435 1210 work_q->push(obj_to_push);
duke@435 1211 cur = next;
duke@435 1212 n++;
duke@435 1213 }
duke@435 1214 par_scan_state->note_overflow_refill(n);
duke@435 1215 return true;
duke@435 1216 }
duke@435 1217
duke@435 1218 void ParNewGeneration::ref_processor_init()
duke@435 1219 {
duke@435 1220 if (_ref_processor == NULL) {
duke@435 1221 // Allocate and initialize a reference processor
duke@435 1222 _ref_processor = ReferenceProcessor::create_ref_processor(
duke@435 1223 _reserved, // span
duke@435 1224 refs_discovery_is_atomic(), // atomic_discovery
duke@435 1225 refs_discovery_is_mt(), // mt_discovery
duke@435 1226 NULL, // is_alive_non_header
duke@435 1227 ParallelGCThreads,
duke@435 1228 ParallelRefProcEnabled);
duke@435 1229 }
duke@435 1230 }
duke@435 1231
duke@435 1232 const char* ParNewGeneration::name() const {
duke@435 1233 return "par new generation";
duke@435 1234 }

mercurial