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 */
24
25 #include "precompiled.hpp"
26 #include "gc/parallel/parallelScavengeHeap.hpp"
27 #include "gc/parallel/psAdaptiveSizePolicy.hpp"
28 #include "gc/parallel/psMarkSweepDecorator.hpp"
29 #include "gc/parallel/psOldGen.hpp"
30 #include "gc/shared/cardTableModRefBS.hpp"
31 #include "gc/shared/gcLocker.inline.hpp"
32 #include "gc/shared/spaceDecorator.hpp"
33 #include "oops/oop.inline.hpp"
34 #include "runtime/java.hpp"
35
36 inline const char* PSOldGen::select_name() {
37 return UseParallelOldGC ? "ParOldGen" : "PSOldGen";
38 }
39
40 PSOldGen::PSOldGen(ReservedSpace rs, size_t alignment,
41 size_t initial_size, size_t min_size, size_t max_size,
42 const char* perf_data_name, int level):
43 _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size),
44 _max_gen_size(max_size)
45 {
46 initialize(rs, alignment, perf_data_name, level);
47 }
48
49 PSOldGen::PSOldGen(size_t initial_size,
50 size_t min_size, size_t max_size,
51 const char* perf_data_name, int level):
52 _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size),
239 // The alignment caused the number of bytes to wrap. An expand_by(0) will
240 // return true with the implication that and expansion was done when it
241 // was not. A call to expand implies a best effort to expand by "bytes"
242 // but not a guarantee. Align down to give a best effort. This is likely
243 // the most that the generation can expand since it has some capacity to
244 // start with.
245 aligned_bytes = align_size_down(bytes, alignment);
246 }
247
248 bool success = false;
249 if (aligned_expand_bytes > aligned_bytes) {
250 success = expand_by(aligned_expand_bytes);
251 }
252 if (!success) {
253 success = expand_by(aligned_bytes);
254 }
255 if (!success) {
256 success = expand_to_reserved();
257 }
258
259 if (PrintGC && Verbose) {
260 if (success && GC_locker::is_active_and_needs_gc()) {
261 gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead");
262 }
263 }
264 }
265
266 bool PSOldGen::expand_by(size_t bytes) {
267 assert_lock_strong(ExpandHeap_lock);
268 assert_locked_or_safepoint(Heap_lock);
269 if (bytes == 0) {
270 return true; // That's what virtual_space()->expand_by(0) would return
271 }
272 bool result = virtual_space()->expand_by(bytes);
273 if (result) {
274 if (ZapUnusedHeapArea) {
275 // We need to mangle the newly expanded area. The memregion spans
276 // end -> new_end, we assume that top -> end is already mangled.
277 // Do the mangling before post_resize() is called because
278 // the space is available for allocation after post_resize();
279 HeapWord* const virtual_space_high = (HeapWord*) virtual_space()->high();
280 assert(object_space()->end() < virtual_space_high,
281 "Should be true before post_resize()");
282 MemRegion mangle_region(object_space()->end(), virtual_space_high);
283 // Note that the object space has not yet been updated to
284 // coincide with the new underlying virtual space.
285 SpaceMangler::mangle_region(mangle_region);
286 }
287 post_resize();
288 if (UsePerfData) {
289 _space_counters->update_capacity();
290 _gen_counters->update_all();
291 }
292 }
293
294 if (result && Verbose && PrintGC) {
295 size_t new_mem_size = virtual_space()->committed_size();
296 size_t old_mem_size = new_mem_size - bytes;
297 gclog_or_tty->print_cr("Expanding %s from " SIZE_FORMAT "K by "
298 SIZE_FORMAT "K to "
299 SIZE_FORMAT "K",
300 name(), old_mem_size/K, bytes/K, new_mem_size/K);
301 }
302
303 return result;
304 }
305
306 bool PSOldGen::expand_to_reserved() {
307 assert_lock_strong(ExpandHeap_lock);
308 assert_locked_or_safepoint(Heap_lock);
309
310 bool result = true;
311 const size_t remaining_bytes = virtual_space()->uncommitted_size();
312 if (remaining_bytes > 0) {
313 result = expand_by(remaining_bytes);
314 DEBUG_ONLY(if (!result) warning("grow to reserve failed"));
315 }
316 return result;
317 }
318
319 void PSOldGen::shrink(size_t bytes) {
320 assert_lock_strong(ExpandHeap_lock);
321 assert_locked_or_safepoint(Heap_lock);
322
323 size_t size = align_size_down(bytes, virtual_space()->alignment());
324 if (size > 0) {
325 assert_lock_strong(ExpandHeap_lock);
326 virtual_space()->shrink_by(bytes);
327 post_resize();
328
329 if (Verbose && PrintGC) {
330 size_t new_mem_size = virtual_space()->committed_size();
331 size_t old_mem_size = new_mem_size + bytes;
332 gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K by "
333 SIZE_FORMAT "K to "
334 SIZE_FORMAT "K",
335 name(), old_mem_size/K, bytes/K, new_mem_size/K);
336 }
337 }
338 }
339
340 void PSOldGen::resize(size_t desired_free_space) {
341 const size_t alignment = virtual_space()->alignment();
342 const size_t size_before = virtual_space()->committed_size();
343 size_t new_size = used_in_bytes() + desired_free_space;
344 if (new_size < used_in_bytes()) {
345 // Overflowed the addition.
346 new_size = gen_size_limit();
347 }
348 // Adjust according to our min and max
349 new_size = MAX2(MIN2(new_size, gen_size_limit()), min_gen_size());
350
351 assert(gen_size_limit() >= reserved().byte_size(), "max new size problem?");
352 new_size = align_size_up(new_size, alignment);
353
354 const size_t current_size = capacity_in_bytes();
355
356 if (PrintAdaptiveSizePolicy && Verbose) {
357 gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: "
358 "desired free: " SIZE_FORMAT " used: " SIZE_FORMAT
359 " new size: " SIZE_FORMAT " current size " SIZE_FORMAT
360 " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT,
361 desired_free_space, used_in_bytes(), new_size, current_size,
362 gen_size_limit(), min_gen_size());
363 }
364
365 if (new_size == current_size) {
366 // No change requested
367 return;
368 }
369 if (new_size > current_size) {
370 size_t change_bytes = new_size - current_size;
371 expand(change_bytes);
372 } else {
373 size_t change_bytes = current_size - new_size;
374 // shrink doesn't grab this lock, expand does. Is that right?
375 MutexLocker x(ExpandHeap_lock);
376 shrink(change_bytes);
377 }
378
379 if (PrintAdaptiveSizePolicy) {
380 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
381 gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: "
382 "collection: %d "
383 "(" SIZE_FORMAT ") -> (" SIZE_FORMAT ") ",
384 heap->total_collections(),
385 size_before, virtual_space()->committed_size());
386 }
387 }
388
389 // NOTE! We need to be careful about resizing. During a GC, multiple
390 // allocators may be active during heap expansion. If we allow the
391 // heap resizing to become visible before we have correctly resized
392 // all heap related data structures, we may cause program failures.
393 void PSOldGen::post_resize() {
394 // First construct a memregion representing the new size
395 MemRegion new_memregion((HeapWord*)virtual_space()->low(),
396 (HeapWord*)virtual_space()->high());
397 size_t new_word_size = new_memregion.word_size();
398
399 start_array()->set_covered_region(new_memregion);
400 ParallelScavengeHeap::heap()->barrier_set()->resize_covered_region(new_memregion);
401
402 // ALWAYS do this last!!
403 object_space()->initialize(new_memregion,
404 SpaceDecorator::DontClear,
405 SpaceDecorator::DontMangle);
406
429
430 void PSOldGen::print() const { print_on(tty);}
431 void PSOldGen::print_on(outputStream* st) const {
432 st->print(" %-15s", name());
433 if (PrintGCDetails && Verbose) {
434 st->print(" total " SIZE_FORMAT ", used " SIZE_FORMAT,
435 capacity_in_bytes(), used_in_bytes());
436 } else {
437 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
438 capacity_in_bytes()/K, used_in_bytes()/K);
439 }
440 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
441 p2i(virtual_space()->low_boundary()),
442 p2i(virtual_space()->high()),
443 p2i(virtual_space()->high_boundary()));
444
445 st->print(" object"); object_space()->print_on(st);
446 }
447
448 void PSOldGen::print_used_change(size_t prev_used) const {
449 gclog_or_tty->print(" [%s:", name());
450 gclog_or_tty->print(" " SIZE_FORMAT "K"
451 "->" SIZE_FORMAT "K"
452 "(" SIZE_FORMAT "K)",
453 prev_used / K, used_in_bytes() / K,
454 capacity_in_bytes() / K);
455 gclog_or_tty->print("]");
456 }
457
458 void PSOldGen::update_counters() {
459 if (UsePerfData) {
460 _space_counters->update_all();
461 _gen_counters->update_all();
462 }
463 }
464
465 #ifndef PRODUCT
466
467 void PSOldGen::space_invariants() {
468 assert(object_space()->end() == (HeapWord*) virtual_space()->high(),
469 "Space invariant");
470 assert(object_space()->bottom() == (HeapWord*) virtual_space()->low(),
471 "Space invariant");
472 assert(virtual_space()->low_boundary() <= virtual_space()->low(),
473 "Space invariant");
474 assert(virtual_space()->high_boundary() >= virtual_space()->high(),
475 "Space invariant");
|
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 */
24
25 #include "precompiled.hpp"
26 #include "gc/parallel/parallelScavengeHeap.hpp"
27 #include "gc/parallel/psAdaptiveSizePolicy.hpp"
28 #include "gc/parallel/psMarkSweepDecorator.hpp"
29 #include "gc/parallel/psOldGen.hpp"
30 #include "gc/shared/cardTableModRefBS.hpp"
31 #include "gc/shared/gcLocker.inline.hpp"
32 #include "gc/shared/spaceDecorator.hpp"
33 #include "logging/log.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "runtime/java.hpp"
36
37 inline const char* PSOldGen::select_name() {
38 return UseParallelOldGC ? "ParOldGen" : "PSOldGen";
39 }
40
41 PSOldGen::PSOldGen(ReservedSpace rs, size_t alignment,
42 size_t initial_size, size_t min_size, size_t max_size,
43 const char* perf_data_name, int level):
44 _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size),
45 _max_gen_size(max_size)
46 {
47 initialize(rs, alignment, perf_data_name, level);
48 }
49
50 PSOldGen::PSOldGen(size_t initial_size,
51 size_t min_size, size_t max_size,
52 const char* perf_data_name, int level):
53 _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size),
240 // The alignment caused the number of bytes to wrap. An expand_by(0) will
241 // return true with the implication that and expansion was done when it
242 // was not. A call to expand implies a best effort to expand by "bytes"
243 // but not a guarantee. Align down to give a best effort. This is likely
244 // the most that the generation can expand since it has some capacity to
245 // start with.
246 aligned_bytes = align_size_down(bytes, alignment);
247 }
248
249 bool success = false;
250 if (aligned_expand_bytes > aligned_bytes) {
251 success = expand_by(aligned_expand_bytes);
252 }
253 if (!success) {
254 success = expand_by(aligned_bytes);
255 }
256 if (!success) {
257 success = expand_to_reserved();
258 }
259
260 if (success && GC_locker::is_active_and_needs_gc()) {
261 log_debug(gc)("Garbage collection disabled, expanded heap instead");
262 }
263 }
264
265 bool PSOldGen::expand_by(size_t bytes) {
266 assert_lock_strong(ExpandHeap_lock);
267 assert_locked_or_safepoint(Heap_lock);
268 if (bytes == 0) {
269 return true; // That's what virtual_space()->expand_by(0) would return
270 }
271 bool result = virtual_space()->expand_by(bytes);
272 if (result) {
273 if (ZapUnusedHeapArea) {
274 // We need to mangle the newly expanded area. The memregion spans
275 // end -> new_end, we assume that top -> end is already mangled.
276 // Do the mangling before post_resize() is called because
277 // the space is available for allocation after post_resize();
278 HeapWord* const virtual_space_high = (HeapWord*) virtual_space()->high();
279 assert(object_space()->end() < virtual_space_high,
280 "Should be true before post_resize()");
281 MemRegion mangle_region(object_space()->end(), virtual_space_high);
282 // Note that the object space has not yet been updated to
283 // coincide with the new underlying virtual space.
284 SpaceMangler::mangle_region(mangle_region);
285 }
286 post_resize();
287 if (UsePerfData) {
288 _space_counters->update_capacity();
289 _gen_counters->update_all();
290 }
291 }
292
293 if (result) {
294 size_t new_mem_size = virtual_space()->committed_size();
295 size_t old_mem_size = new_mem_size - bytes;
296 log_debug(gc)("Expanding %s from " SIZE_FORMAT "K by " SIZE_FORMAT "K to " SIZE_FORMAT "K",
297 name(), old_mem_size/K, bytes/K, new_mem_size/K);
298 }
299
300 return result;
301 }
302
303 bool PSOldGen::expand_to_reserved() {
304 assert_lock_strong(ExpandHeap_lock);
305 assert_locked_or_safepoint(Heap_lock);
306
307 bool result = true;
308 const size_t remaining_bytes = virtual_space()->uncommitted_size();
309 if (remaining_bytes > 0) {
310 result = expand_by(remaining_bytes);
311 DEBUG_ONLY(if (!result) warning("grow to reserve failed"));
312 }
313 return result;
314 }
315
316 void PSOldGen::shrink(size_t bytes) {
317 assert_lock_strong(ExpandHeap_lock);
318 assert_locked_or_safepoint(Heap_lock);
319
320 size_t size = align_size_down(bytes, virtual_space()->alignment());
321 if (size > 0) {
322 assert_lock_strong(ExpandHeap_lock);
323 virtual_space()->shrink_by(bytes);
324 post_resize();
325
326 size_t new_mem_size = virtual_space()->committed_size();
327 size_t old_mem_size = new_mem_size + bytes;
328 log_debug(gc)("Shrinking %s from " SIZE_FORMAT "K by " SIZE_FORMAT "K to " SIZE_FORMAT "K",
329 name(), old_mem_size/K, bytes/K, new_mem_size/K);
330 }
331 }
332
333 void PSOldGen::resize(size_t desired_free_space) {
334 const size_t alignment = virtual_space()->alignment();
335 const size_t size_before = virtual_space()->committed_size();
336 size_t new_size = used_in_bytes() + desired_free_space;
337 if (new_size < used_in_bytes()) {
338 // Overflowed the addition.
339 new_size = gen_size_limit();
340 }
341 // Adjust according to our min and max
342 new_size = MAX2(MIN2(new_size, gen_size_limit()), min_gen_size());
343
344 assert(gen_size_limit() >= reserved().byte_size(), "max new size problem?");
345 new_size = align_size_up(new_size, alignment);
346
347 const size_t current_size = capacity_in_bytes();
348
349 log_trace(gc, ergo)("AdaptiveSizePolicy::old generation size: "
350 "desired free: " SIZE_FORMAT " used: " SIZE_FORMAT
351 " new size: " SIZE_FORMAT " current size " SIZE_FORMAT
352 " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT,
353 desired_free_space, used_in_bytes(), new_size, current_size,
354 gen_size_limit(), min_gen_size());
355
356 if (new_size == current_size) {
357 // No change requested
358 return;
359 }
360 if (new_size > current_size) {
361 size_t change_bytes = new_size - current_size;
362 expand(change_bytes);
363 } else {
364 size_t change_bytes = current_size - new_size;
365 // shrink doesn't grab this lock, expand does. Is that right?
366 MutexLocker x(ExpandHeap_lock);
367 shrink(change_bytes);
368 }
369
370 log_trace(gc, ergo)("AdaptiveSizePolicy::old generation size: collection: %d (" SIZE_FORMAT ") -> (" SIZE_FORMAT ") ",
371 ParallelScavengeHeap::heap()->total_collections(),
372 size_before,
373 virtual_space()->committed_size());
374 }
375
376 // NOTE! We need to be careful about resizing. During a GC, multiple
377 // allocators may be active during heap expansion. If we allow the
378 // heap resizing to become visible before we have correctly resized
379 // all heap related data structures, we may cause program failures.
380 void PSOldGen::post_resize() {
381 // First construct a memregion representing the new size
382 MemRegion new_memregion((HeapWord*)virtual_space()->low(),
383 (HeapWord*)virtual_space()->high());
384 size_t new_word_size = new_memregion.word_size();
385
386 start_array()->set_covered_region(new_memregion);
387 ParallelScavengeHeap::heap()->barrier_set()->resize_covered_region(new_memregion);
388
389 // ALWAYS do this last!!
390 object_space()->initialize(new_memregion,
391 SpaceDecorator::DontClear,
392 SpaceDecorator::DontMangle);
393
416
417 void PSOldGen::print() const { print_on(tty);}
418 void PSOldGen::print_on(outputStream* st) const {
419 st->print(" %-15s", name());
420 if (PrintGCDetails && Verbose) {
421 st->print(" total " SIZE_FORMAT ", used " SIZE_FORMAT,
422 capacity_in_bytes(), used_in_bytes());
423 } else {
424 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
425 capacity_in_bytes()/K, used_in_bytes()/K);
426 }
427 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
428 p2i(virtual_space()->low_boundary()),
429 p2i(virtual_space()->high()),
430 p2i(virtual_space()->high_boundary()));
431
432 st->print(" object"); object_space()->print_on(st);
433 }
434
435 void PSOldGen::print_used_change(size_t prev_used) const {
436 log_info(gc, heap)("%s: " SIZE_FORMAT "K->" SIZE_FORMAT "K(" SIZE_FORMAT "K)",
437 name(), prev_used / K, used_in_bytes() / K, capacity_in_bytes() / K);
438 }
439
440 void PSOldGen::update_counters() {
441 if (UsePerfData) {
442 _space_counters->update_all();
443 _gen_counters->update_all();
444 }
445 }
446
447 #ifndef PRODUCT
448
449 void PSOldGen::space_invariants() {
450 assert(object_space()->end() == (HeapWord*) virtual_space()->high(),
451 "Space invariant");
452 assert(object_space()->bottom() == (HeapWord*) virtual_space()->low(),
453 "Space invariant");
454 assert(virtual_space()->low_boundary() <= virtual_space()->low(),
455 "Space invariant");
456 assert(virtual_space()->high_boundary() >= virtual_space()->high(),
457 "Space invariant");
|