Thu, 22 Jul 2010 10:27:41 -0400
6962589: remove breadth first scanning code from parallel gc
Summary: Remove the breadth-first copying order from ParallelScavenge and use depth-first by default.
Reviewed-by: jcoomes, ysr, johnc
1 /*
2 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_psPromotionManager.cpp.incl"
28 PSPromotionManager** PSPromotionManager::_manager_array = NULL;
29 OopStarTaskQueueSet* PSPromotionManager::_stack_array_depth = NULL;
30 PSOldGen* PSPromotionManager::_old_gen = NULL;
31 MutableSpace* PSPromotionManager::_young_space = NULL;
33 void PSPromotionManager::initialize() {
34 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
35 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
37 _old_gen = heap->old_gen();
38 _young_space = heap->young_gen()->to_space();
40 assert(_manager_array == NULL, "Attempt to initialize twice");
41 _manager_array = NEW_C_HEAP_ARRAY(PSPromotionManager*, ParallelGCThreads+1 );
42 guarantee(_manager_array != NULL, "Could not initialize promotion manager");
44 _stack_array_depth = new OopStarTaskQueueSet(ParallelGCThreads);
45 guarantee(_stack_array_depth != NULL, "Cound not initialize promotion manager");
47 // Create and register the PSPromotionManager(s) for the worker threads.
48 for(uint i=0; i<ParallelGCThreads; i++) {
49 _manager_array[i] = new PSPromotionManager();
50 guarantee(_manager_array[i] != NULL, "Could not create PSPromotionManager");
51 stack_array_depth()->register_queue(i, _manager_array[i]->claimed_stack_depth());
52 }
54 // The VMThread gets its own PSPromotionManager, which is not available
55 // for work stealing.
56 _manager_array[ParallelGCThreads] = new PSPromotionManager();
57 guarantee(_manager_array[ParallelGCThreads] != NULL, "Could not create PSPromotionManager");
58 }
60 PSPromotionManager* PSPromotionManager::gc_thread_promotion_manager(int index) {
61 assert(index >= 0 && index < (int)ParallelGCThreads, "index out of range");
62 assert(_manager_array != NULL, "Sanity");
63 return _manager_array[index];
64 }
66 PSPromotionManager* PSPromotionManager::vm_thread_promotion_manager() {
67 assert(_manager_array != NULL, "Sanity");
68 return _manager_array[ParallelGCThreads];
69 }
71 void PSPromotionManager::pre_scavenge() {
72 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
73 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
75 _young_space = heap->young_gen()->to_space();
77 for(uint i=0; i<ParallelGCThreads+1; i++) {
78 manager_array(i)->reset();
79 }
80 }
82 void PSPromotionManager::post_scavenge() {
83 TASKQUEUE_STATS_ONLY(if (PrintGCDetails && ParallelGCVerbose) print_stats());
84 for (uint i = 0; i < ParallelGCThreads + 1; i++) {
85 PSPromotionManager* manager = manager_array(i);
86 assert(manager->claimed_stack_depth()->is_empty(), "should be empty");
87 manager->flush_labs();
88 }
89 }
91 #if TASKQUEUE_STATS
92 void
93 PSPromotionManager::print_taskqueue_stats(uint i) const {
94 tty->print("%3u ", i);
95 _claimed_stack_depth.stats.print();
96 tty->cr();
97 }
99 void
100 PSPromotionManager::print_local_stats(uint i) const {
101 #define FMT " " SIZE_FORMAT_W(10)
102 tty->print_cr("%3u" FMT FMT FMT FMT, i, _masked_pushes, _masked_steals,
103 _arrays_chunked, _array_chunks_processed);
104 #undef FMT
105 }
107 static const char* const pm_stats_hdr[] = {
108 " --------masked------- arrays array",
109 "thr push steal chunked chunks",
110 "--- ---------- ---------- ---------- ----------"
111 };
113 void
114 PSPromotionManager::print_stats() {
115 tty->print_cr("== GC Tasks Stats, GC %3d",
116 Universe::heap()->total_collections());
118 tty->print("thr "); TaskQueueStats::print_header(1); tty->cr();
119 tty->print("--- "); TaskQueueStats::print_header(2); tty->cr();
120 for (uint i = 0; i < ParallelGCThreads + 1; ++i) {
121 manager_array(i)->print_taskqueue_stats(i);
122 }
124 const uint hlines = sizeof(pm_stats_hdr) / sizeof(pm_stats_hdr[0]);
125 for (uint i = 0; i < hlines; ++i) tty->print_cr(pm_stats_hdr[i]);
126 for (uint i = 0; i < ParallelGCThreads + 1; ++i) {
127 manager_array(i)->print_local_stats(i);
128 }
129 }
131 void
132 PSPromotionManager::reset_stats() {
133 claimed_stack_depth()->stats.reset();
134 _masked_pushes = _masked_steals = 0;
135 _arrays_chunked = _array_chunks_processed = 0;
136 }
137 #endif // TASKQUEUE_STATS
139 PSPromotionManager::PSPromotionManager() {
140 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
141 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
143 // We set the old lab's start array.
144 _old_lab.set_start_array(old_gen()->start_array());
146 uint queue_size;
147 claimed_stack_depth()->initialize();
148 queue_size = claimed_stack_depth()->max_elems();
150 _totally_drain = (ParallelGCThreads == 1) || (GCDrainStackTargetSize == 0);
151 if (_totally_drain) {
152 _target_stack_size = 0;
153 } else {
154 // don't let the target stack size to be more than 1/4 of the entries
155 _target_stack_size = (uint) MIN2((uint) GCDrainStackTargetSize,
156 (uint) (queue_size / 4));
157 }
159 _array_chunk_size = ParGCArrayScanChunk;
160 // let's choose 1.5x the chunk size
161 _min_array_size_for_chunking = 3 * _array_chunk_size / 2;
163 reset();
164 }
166 void PSPromotionManager::reset() {
167 assert(stacks_empty(), "reset of non-empty stack");
169 // We need to get an assert in here to make sure the labs are always flushed.
171 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
172 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
174 // Do not prefill the LAB's, save heap wastage!
175 HeapWord* lab_base = young_space()->top();
176 _young_lab.initialize(MemRegion(lab_base, (size_t)0));
177 _young_gen_is_full = false;
179 lab_base = old_gen()->object_space()->top();
180 _old_lab.initialize(MemRegion(lab_base, (size_t)0));
181 _old_gen_is_full = false;
183 TASKQUEUE_STATS_ONLY(reset_stats());
184 }
187 void PSPromotionManager::drain_stacks_depth(bool totally_drain) {
188 assert(claimed_stack_depth()->overflow_stack() != NULL, "invariant");
189 totally_drain = totally_drain || _totally_drain;
191 #ifdef ASSERT
192 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
193 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
194 MutableSpace* to_space = heap->young_gen()->to_space();
195 MutableSpace* old_space = heap->old_gen()->object_space();
196 MutableSpace* perm_space = heap->perm_gen()->object_space();
197 #endif /* ASSERT */
199 OopStarTaskQueue* const tq = claimed_stack_depth();
200 do {
201 StarTask p;
203 // Drain overflow stack first, so other threads can steal from
204 // claimed stack while we work.
205 while (tq->pop_overflow(p)) {
206 process_popped_location_depth(p);
207 }
209 if (totally_drain) {
210 while (tq->pop_local(p)) {
211 process_popped_location_depth(p);
212 }
213 } else {
214 while (tq->size() > _target_stack_size && tq->pop_local(p)) {
215 process_popped_location_depth(p);
216 }
217 }
218 } while (totally_drain && !tq->taskqueue_empty() || !tq->overflow_empty());
220 assert(!totally_drain || tq->taskqueue_empty(), "Sanity");
221 assert(totally_drain || tq->size() <= _target_stack_size, "Sanity");
222 assert(tq->overflow_empty(), "Sanity");
223 }
225 void PSPromotionManager::flush_labs() {
226 assert(stacks_empty(), "Attempt to flush lab with live stack");
228 // If either promotion lab fills up, we can flush the
229 // lab but not refill it, so check first.
230 assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity");
231 if (!_young_lab.is_flushed())
232 _young_lab.flush();
234 assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity");
235 if (!_old_lab.is_flushed())
236 _old_lab.flush();
238 // Let PSScavenge know if we overflowed
239 if (_young_gen_is_full) {
240 PSScavenge::set_survivor_overflow(true);
241 }
242 }
244 //
245 // This method is pretty bulky. It would be nice to split it up
246 // into smaller submethods, but we need to be careful not to hurt
247 // performance.
248 //
250 oop PSPromotionManager::copy_to_survivor_space(oop o) {
251 assert(PSScavenge::should_scavenge(&o), "Sanity");
253 oop new_obj = NULL;
255 // NOTE! We must be very careful with any methods that access the mark
256 // in o. There may be multiple threads racing on it, and it may be forwarded
257 // at any time. Do not use oop methods for accessing the mark!
258 markOop test_mark = o->mark();
260 // The same test as "o->is_forwarded()"
261 if (!test_mark->is_marked()) {
262 bool new_obj_is_tenured = false;
263 size_t new_obj_size = o->size();
265 // Find the objects age, MT safe.
266 int age = (test_mark->has_displaced_mark_helper() /* o->has_displaced_mark() */) ?
267 test_mark->displaced_mark_helper()->age() : test_mark->age();
269 // Try allocating obj in to-space (unless too old)
270 if (age < PSScavenge::tenuring_threshold()) {
271 new_obj = (oop) _young_lab.allocate(new_obj_size);
272 if (new_obj == NULL && !_young_gen_is_full) {
273 // Do we allocate directly, or flush and refill?
274 if (new_obj_size > (YoungPLABSize / 2)) {
275 // Allocate this object directly
276 new_obj = (oop)young_space()->cas_allocate(new_obj_size);
277 } else {
278 // Flush and fill
279 _young_lab.flush();
281 HeapWord* lab_base = young_space()->cas_allocate(YoungPLABSize);
282 if (lab_base != NULL) {
283 _young_lab.initialize(MemRegion(lab_base, YoungPLABSize));
284 // Try the young lab allocation again.
285 new_obj = (oop) _young_lab.allocate(new_obj_size);
286 } else {
287 _young_gen_is_full = true;
288 }
289 }
290 }
291 }
293 // Otherwise try allocating obj tenured
294 if (new_obj == NULL) {
295 #ifndef PRODUCT
296 if (Universe::heap()->promotion_should_fail()) {
297 return oop_promotion_failed(o, test_mark);
298 }
299 #endif // #ifndef PRODUCT
301 new_obj = (oop) _old_lab.allocate(new_obj_size);
302 new_obj_is_tenured = true;
304 if (new_obj == NULL) {
305 if (!_old_gen_is_full) {
306 // Do we allocate directly, or flush and refill?
307 if (new_obj_size > (OldPLABSize / 2)) {
308 // Allocate this object directly
309 new_obj = (oop)old_gen()->cas_allocate(new_obj_size);
310 } else {
311 // Flush and fill
312 _old_lab.flush();
314 HeapWord* lab_base = old_gen()->cas_allocate(OldPLABSize);
315 if(lab_base != NULL) {
316 _old_lab.initialize(MemRegion(lab_base, OldPLABSize));
317 // Try the old lab allocation again.
318 new_obj = (oop) _old_lab.allocate(new_obj_size);
319 }
320 }
321 }
323 // This is the promotion failed test, and code handling.
324 // The code belongs here for two reasons. It is slightly
325 // different thatn the code below, and cannot share the
326 // CAS testing code. Keeping the code here also minimizes
327 // the impact on the common case fast path code.
329 if (new_obj == NULL) {
330 _old_gen_is_full = true;
331 return oop_promotion_failed(o, test_mark);
332 }
333 }
334 }
336 assert(new_obj != NULL, "allocation should have succeeded");
338 // Copy obj
339 Copy::aligned_disjoint_words((HeapWord*)o, (HeapWord*)new_obj, new_obj_size);
341 // Now we have to CAS in the header.
342 if (o->cas_forward_to(new_obj, test_mark)) {
343 // We won any races, we "own" this object.
344 assert(new_obj == o->forwardee(), "Sanity");
346 // Increment age if obj still in new generation. Now that
347 // we're dealing with a markOop that cannot change, it is
348 // okay to use the non mt safe oop methods.
349 if (!new_obj_is_tenured) {
350 new_obj->incr_age();
351 assert(young_space()->contains(new_obj), "Attempt to push non-promoted obj");
352 }
354 // Do the size comparison first with new_obj_size, which we
355 // already have. Hopefully, only a few objects are larger than
356 // _min_array_size_for_chunking, and most of them will be arrays.
357 // So, the is->objArray() test would be very infrequent.
358 if (new_obj_size > _min_array_size_for_chunking &&
359 new_obj->is_objArray() &&
360 PSChunkLargeArrays) {
361 // we'll chunk it
362 oop* const masked_o = mask_chunked_array_oop(o);
363 push_depth(masked_o);
364 TASKQUEUE_STATS_ONLY(++_arrays_chunked; ++_masked_pushes);
365 } else {
366 // we'll just push its contents
367 new_obj->push_contents(this);
368 }
369 } else {
370 // We lost, someone else "owns" this object
371 guarantee(o->is_forwarded(), "Object must be forwarded if the cas failed.");
373 // Try to deallocate the space. If it was directly allocated we cannot
374 // deallocate it, so we have to test. If the deallocation fails,
375 // overwrite with a filler object.
376 if (new_obj_is_tenured) {
377 if (!_old_lab.unallocate_object(new_obj)) {
378 CollectedHeap::fill_with_object((HeapWord*) new_obj, new_obj_size);
379 }
380 } else if (!_young_lab.unallocate_object(new_obj)) {
381 CollectedHeap::fill_with_object((HeapWord*) new_obj, new_obj_size);
382 }
384 // don't update this before the unallocation!
385 new_obj = o->forwardee();
386 }
387 } else {
388 assert(o->is_forwarded(), "Sanity");
389 new_obj = o->forwardee();
390 }
392 #ifdef DEBUG
393 // This code must come after the CAS test, or it will print incorrect
394 // information.
395 if (TraceScavenge) {
396 gclog_or_tty->print_cr("{%s %s " PTR_FORMAT " -> " PTR_FORMAT " (" SIZE_FORMAT ")}",
397 PSScavenge::should_scavenge(&new_obj) ? "copying" : "tenuring",
398 new_obj->blueprint()->internal_name(), o, new_obj, new_obj->size());
399 }
400 #endif
402 return new_obj;
403 }
405 template <class T> void PSPromotionManager::process_array_chunk_work(
406 oop obj,
407 int start, int end) {
408 assert(start < end, "invariant");
409 T* const base = (T*)objArrayOop(obj)->base();
410 T* p = base + start;
411 T* const chunk_end = base + end;
412 while (p < chunk_end) {
413 if (PSScavenge::should_scavenge(p)) {
414 claim_or_forward_depth(p);
415 }
416 ++p;
417 }
418 }
420 void PSPromotionManager::process_array_chunk(oop old) {
421 assert(PSChunkLargeArrays, "invariant");
422 assert(old->is_objArray(), "invariant");
423 assert(old->is_forwarded(), "invariant");
425 TASKQUEUE_STATS_ONLY(++_array_chunks_processed);
427 oop const obj = old->forwardee();
429 int start;
430 int const end = arrayOop(old)->length();
431 if (end > (int) _min_array_size_for_chunking) {
432 // we'll chunk more
433 start = end - _array_chunk_size;
434 assert(start > 0, "invariant");
435 arrayOop(old)->set_length(start);
436 push_depth(mask_chunked_array_oop(old));
437 TASKQUEUE_STATS_ONLY(++_masked_pushes);
438 } else {
439 // this is the final chunk for this array
440 start = 0;
441 int const actual_length = arrayOop(obj)->length();
442 arrayOop(old)->set_length(actual_length);
443 }
445 if (UseCompressedOops) {
446 process_array_chunk_work<narrowOop>(obj, start, end);
447 } else {
448 process_array_chunk_work<oop>(obj, start, end);
449 }
450 }
452 oop PSPromotionManager::oop_promotion_failed(oop obj, markOop obj_mark) {
453 assert(_old_gen_is_full || PromotionFailureALot, "Sanity");
455 // Attempt to CAS in the header.
456 // This tests if the header is still the same as when
457 // this started. If it is the same (i.e., no forwarding
458 // pointer has been installed), then this thread owns
459 // it.
460 if (obj->cas_forward_to(obj, obj_mark)) {
461 // We won any races, we "own" this object.
462 assert(obj == obj->forwardee(), "Sanity");
464 obj->push_contents(this);
466 // Save the mark if needed
467 PSScavenge::oop_promotion_failed(obj, obj_mark);
468 } else {
469 // We lost, someone else "owns" this object
470 guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed.");
472 // No unallocation to worry about.
473 obj = obj->forwardee();
474 }
476 #ifdef DEBUG
477 if (TraceScavenge) {
478 gclog_or_tty->print_cr("{%s %s 0x%x (%d)}",
479 "promotion-failure",
480 obj->blueprint()->internal_name(),
481 obj, obj->size());
483 }
484 #endif
486 return obj;
487 }