hotspot/src/share/vm/memory/collectorPolicy.cpp
changeset 22551 9bf46d16dcc6
parent 22548 d54e4339811f
child 22776 d99c54477401
equal deleted inserted replaced
22550:820966182ab9 22551:9bf46d16dcc6
    43 #if INCLUDE_ALL_GCS
    43 #if INCLUDE_ALL_GCS
    44 #include "gc_implementation/concurrentMarkSweep/cmsAdaptiveSizePolicy.hpp"
    44 #include "gc_implementation/concurrentMarkSweep/cmsAdaptiveSizePolicy.hpp"
    45 #include "gc_implementation/concurrentMarkSweep/cmsGCAdaptivePolicyCounters.hpp"
    45 #include "gc_implementation/concurrentMarkSweep/cmsGCAdaptivePolicyCounters.hpp"
    46 #endif // INCLUDE_ALL_GCS
    46 #endif // INCLUDE_ALL_GCS
    47 
    47 
    48 // CollectorPolicy methods.
    48 // CollectorPolicy methods
    49 
    49 
    50 CollectorPolicy::CollectorPolicy() :
    50 CollectorPolicy::CollectorPolicy() :
    51     _space_alignment(0),
    51     _space_alignment(0),
    52     _heap_alignment(0),
    52     _heap_alignment(0),
    53     _initial_heap_byte_size(InitialHeapSize),
    53     _initial_heap_byte_size(InitialHeapSize),
   183   // Parallel GC does its own alignment of the generations to avoid requiring a
   183   // Parallel GC does its own alignment of the generations to avoid requiring a
   184   // large page (256M on some platforms) for the permanent generation.  The
   184   // large page (256M on some platforms) for the permanent generation.  The
   185   // other collectors should also be updated to do their own alignment and then
   185   // other collectors should also be updated to do their own alignment and then
   186   // this use of lcm() should be removed.
   186   // this use of lcm() should be removed.
   187   if (UseLargePages && !UseParallelGC) {
   187   if (UseLargePages && !UseParallelGC) {
   188       // in presence of large pages we have to make sure that our
   188       // In presence of large pages we have to make sure that our
   189       // alignment is large page aware
   189       // alignment is large page aware
   190       alignment = lcm(os::large_page_size(), alignment);
   190       alignment = lcm(os::large_page_size(), alignment);
   191   }
   191   }
   192 
   192 
   193   return alignment;
   193   return alignment;
   194 }
   194 }
   195 
   195 
   196 // GenCollectorPolicy methods.
   196 // GenCollectorPolicy methods
   197 
   197 
   198 GenCollectorPolicy::GenCollectorPolicy() :
   198 GenCollectorPolicy::GenCollectorPolicy() :
   199     _min_gen0_size(0),
   199     _min_gen0_size(0),
   200     _initial_gen0_size(0),
   200     _initial_gen0_size(0),
   201     _max_gen0_size(0),
   201     _max_gen0_size(0),
   373     _max_heap_byte_size = MaxHeapSize;
   373     _max_heap_byte_size = MaxHeapSize;
   374     FLAG_SET_ERGO(uintx, InitialHeapSize, calculated_heapsize);
   374     FLAG_SET_ERGO(uintx, InitialHeapSize, calculated_heapsize);
   375     _initial_heap_byte_size = InitialHeapSize;
   375     _initial_heap_byte_size = InitialHeapSize;
   376   }
   376   }
   377 
   377 
   378   // adjust max heap size if necessary
   378   // Adjust NewSize and OldSize or MaxHeapSize to match each other
   379   if (NewSize + OldSize > MaxHeapSize) {
   379   if (NewSize + OldSize > MaxHeapSize) {
   380     if (_max_heap_size_cmdline) {
   380     if (_max_heap_size_cmdline) {
   381       // somebody set a maximum heap size with the intention that we should not
   381       // Somebody has set a maximum heap size with the intention that we should not
   382       // exceed it. Adjust New/OldSize as necessary.
   382       // exceed it. Adjust New/OldSize as necessary.
   383       uintx calculated_size = NewSize + OldSize;
   383       uintx calculated_size = NewSize + OldSize;
   384       double shrink_factor = (double) MaxHeapSize / calculated_size;
   384       double shrink_factor = (double) MaxHeapSize / calculated_size;
   385       uintx smaller_new_size = align_size_down((uintx)(NewSize * shrink_factor), _gen_alignment);
   385       uintx smaller_new_size = align_size_down((uintx)(NewSize * shrink_factor), _gen_alignment);
   386       FLAG_SET_ERGO(uintx, NewSize, MAX2(young_gen_size_lower_bound(), smaller_new_size));
   386       FLAG_SET_ERGO(uintx, NewSize, MAX2(young_gen_size_lower_bound(), smaller_new_size));
   437 
   437 
   438   // Given the maximum gen0 size, determine the initial and
   438   // Given the maximum gen0 size, determine the initial and
   439   // minimum gen0 sizes.
   439   // minimum gen0 sizes.
   440 
   440 
   441   if (_max_heap_byte_size == _min_heap_byte_size) {
   441   if (_max_heap_byte_size == _min_heap_byte_size) {
   442     // The maximum and minimum heap sizes are the same so
   442     // The maximum and minimum heap sizes are the same so the generations
   443     // the generations minimum and initial must be the
   443     // minimum and initial must be the same as its maximum.
   444     // same as its maximum.
       
   445     _min_gen0_size = max_new_size;
   444     _min_gen0_size = max_new_size;
   446     _initial_gen0_size = max_new_size;
   445     _initial_gen0_size = max_new_size;
   447     _max_gen0_size = max_new_size;
   446     _max_gen0_size = max_new_size;
   448   } else {
   447   } else {
   449     size_t desired_new_size = 0;
   448     size_t desired_new_size = 0;
   461       max_new_size = MAX2(max_new_size, NewSize);
   460       max_new_size = MAX2(max_new_size, NewSize);
   462     } else {
   461     } else {
   463       // For the case where NewSize is the default, use NewRatio
   462       // For the case where NewSize is the default, use NewRatio
   464       // to size the minimum and initial generation sizes.
   463       // to size the minimum and initial generation sizes.
   465       // Use the default NewSize as the floor for these values.  If
   464       // Use the default NewSize as the floor for these values.  If
   466       // NewRatio is overly large, the resulting sizes can be too
   465       // NewRatio is overly large, the resulting sizes can be too small.
   467       // small.
       
   468       _min_gen0_size = MAX2(scale_by_NewRatio_aligned(_min_heap_byte_size), NewSize);
   466       _min_gen0_size = MAX2(scale_by_NewRatio_aligned(_min_heap_byte_size), NewSize);
   469       desired_new_size =
   467       desired_new_size =
   470         MAX2(scale_by_NewRatio_aligned(_initial_heap_byte_size), NewSize);
   468         MAX2(scale_by_NewRatio_aligned(_initial_heap_byte_size), NewSize);
   471     }
   469     }
   472 
   470 
   481     _min_gen0_size = bound_minus_alignment(_min_gen0_size, _min_heap_byte_size);
   479     _min_gen0_size = bound_minus_alignment(_min_gen0_size, _min_heap_byte_size);
   482     _initial_gen0_size = bound_minus_alignment(_initial_gen0_size, _initial_heap_byte_size);
   480     _initial_gen0_size = bound_minus_alignment(_initial_gen0_size, _initial_heap_byte_size);
   483     _max_gen0_size = bound_minus_alignment(_max_gen0_size, _max_heap_byte_size);
   481     _max_gen0_size = bound_minus_alignment(_max_gen0_size, _max_heap_byte_size);
   484 
   482 
   485     // At this point all three sizes have been checked against the
   483     // At this point all three sizes have been checked against the
   486     // maximum sizes but have not been checked for consistency
   484     // maximum sizes but have not been checked for consistency among the three.
   487     // among the three.
       
   488 
   485 
   489     // Final check min <= initial <= max
   486     // Final check min <= initial <= max
   490     _min_gen0_size = MIN2(_min_gen0_size, _max_gen0_size);
   487     _min_gen0_size = MIN2(_min_gen0_size, _max_gen0_size);
   491     _initial_gen0_size = MAX2(MIN2(_initial_gen0_size, _max_gen0_size), _min_gen0_size);
   488     _initial_gen0_size = MAX2(MIN2(_initial_gen0_size, _max_gen0_size), _min_gen0_size);
   492     _min_gen0_size = MIN2(_min_gen0_size, _initial_gen0_size);
   489     _min_gen0_size = MIN2(_min_gen0_size, _initial_gen0_size);
   493   }
   490   }
   494 
   491 
   495   // Write back to flags if necessary
   492   // Write back to flags if necessary.
   496   if (NewSize != _initial_gen0_size) {
   493   if (NewSize != _initial_gen0_size) {
   497     FLAG_SET_ERGO(uintx, NewSize, _initial_gen0_size);
   494     FLAG_SET_ERGO(uintx, NewSize, _initial_gen0_size);
   498   }
   495   }
   499 
   496 
   500   if (MaxNewSize != _max_gen0_size) {
   497   if (MaxNewSize != _max_gen0_size) {
   536   }
   533   }
   537   return result;
   534   return result;
   538 }
   535 }
   539 
   536 
   540 // Minimum sizes of the generations may be different than
   537 // Minimum sizes of the generations may be different than
   541 // the initial sizes.  An inconsistently is permitted here
   538 // the initial sizes.  An inconsistency is permitted here
   542 // in the total size that can be specified explicitly by
   539 // in the total size that can be specified explicitly by
   543 // command line specification of OldSize and NewSize and
   540 // command line specification of OldSize and NewSize and
   544 // also a command line specification of -Xms.  Issue a warning
   541 // also a command line specification of -Xms.  Issue a warning
   545 // but allow the values to pass.
   542 // but allow the values to pass.
   546 
   543 
   548   GenCollectorPolicy::initialize_size_info();
   545   GenCollectorPolicy::initialize_size_info();
   549 
   546 
   550   // At this point the minimum, initial and maximum sizes
   547   // At this point the minimum, initial and maximum sizes
   551   // of the overall heap and of gen0 have been determined.
   548   // of the overall heap and of gen0 have been determined.
   552   // The maximum gen1 size can be determined from the maximum gen0
   549   // The maximum gen1 size can be determined from the maximum gen0
   553   // and maximum heap size since no explicit flags exits
   550   // and maximum heap size since no explicit flags exist
   554   // for setting the gen1 maximum.
   551   // for setting the gen1 maximum.
   555   _max_gen1_size = MAX2(_max_heap_byte_size - _max_gen0_size, _gen_alignment);
   552   _max_gen1_size = MAX2(_max_heap_byte_size - _max_gen0_size, _gen_alignment);
   556 
   553 
   557   // If no explicit command line flag has been set for the
   554   // If no explicit command line flag has been set for the
   558   // gen1 size, use what is left for gen1.
   555   // gen1 size, use what is left for gen1
   559   if (!FLAG_IS_CMDLINE(OldSize)) {
   556   if (!FLAG_IS_CMDLINE(OldSize)) {
   560     // The user has not specified any value but the ergonomics
   557     // The user has not specified any value but the ergonomics
   561     // may have chosen a value (which may or may not be consistent
   558     // may have chosen a value (which may or may not be consistent
   562     // with the overall heap size).  In either case make
   559     // with the overall heap size).  In either case make
   563     // the minimum, maximum and initial sizes consistent
   560     // the minimum, maximum and initial sizes consistent
   565     _min_gen1_size = MAX2(_min_heap_byte_size - _min_gen0_size, _gen_alignment);
   562     _min_gen1_size = MAX2(_min_heap_byte_size - _min_gen0_size, _gen_alignment);
   566     _initial_gen1_size = MAX2(_initial_heap_byte_size - _initial_gen0_size, _gen_alignment);
   563     _initial_gen1_size = MAX2(_initial_heap_byte_size - _initial_gen0_size, _gen_alignment);
   567     // _max_gen1_size has already been made consistent above
   564     // _max_gen1_size has already been made consistent above
   568     FLAG_SET_ERGO(uintx, OldSize, _initial_gen1_size);
   565     FLAG_SET_ERGO(uintx, OldSize, _initial_gen1_size);
   569   } else {
   566   } else {
   570     // It's been explicitly set on the command line.  Use the
   567     // OldSize has been explicitly set on the command line. Use the
   571     // OldSize and then determine the consequences.
   568     // OldSize and then determine the consequences.
   572     _min_gen1_size = MIN2(OldSize, _min_heap_byte_size - _min_gen0_size);
   569     _min_gen1_size = MIN2(OldSize, _min_heap_byte_size - _min_gen0_size);
   573     _initial_gen1_size = OldSize;
   570     _initial_gen1_size = OldSize;
   574 
   571 
   575     // If the user has explicitly set an OldSize that is inconsistent
   572     // If the user has explicitly set an OldSize that is inconsistent
   576     // with other command line flags, issue a warning.
   573     // with other command line flags, issue a warning.
   577     // The generation minimums and the overall heap mimimum should
   574     // The generation minimums and the overall heap minimum should
   578     // be within one generation alignment.
   575     // be within one generation alignment.
   579     if ((_min_gen1_size + _min_gen0_size + _gen_alignment) < _min_heap_byte_size) {
   576     if ((_min_gen1_size + _min_gen0_size + _gen_alignment) < _min_heap_byte_size) {
   580       warning("Inconsistency between minimum heap size and minimum "
   577       warning("Inconsistency between minimum heap size and minimum "
   581               "generation sizes: using minimum heap = " SIZE_FORMAT,
   578               "generation sizes: using minimum heap = " SIZE_FORMAT,
   582               _min_heap_byte_size);
   579               _min_heap_byte_size);
   594         gclog_or_tty->print_cr("2: Minimum gen0 " SIZE_FORMAT "  Initial gen0 "
   591         gclog_or_tty->print_cr("2: Minimum gen0 " SIZE_FORMAT "  Initial gen0 "
   595               SIZE_FORMAT "  Maximum gen0 " SIZE_FORMAT,
   592               SIZE_FORMAT "  Maximum gen0 " SIZE_FORMAT,
   596               _min_gen0_size, _initial_gen0_size, _max_gen0_size);
   593               _min_gen0_size, _initial_gen0_size, _max_gen0_size);
   597       }
   594       }
   598     }
   595     }
   599     // Initial size
   596     // The same as above for the old gen initial size.
   600     if (adjust_gen0_sizes(&_initial_gen0_size, &_initial_gen1_size,
   597     if (adjust_gen0_sizes(&_initial_gen0_size, &_initial_gen1_size,
   601                           _initial_heap_byte_size)) {
   598                           _initial_heap_byte_size)) {
   602       if (PrintGCDetails && Verbose) {
   599       if (PrintGCDetails && Verbose) {
   603         gclog_or_tty->print_cr("3: Minimum gen0 " SIZE_FORMAT "  Initial gen0 "
   600         gclog_or_tty->print_cr("3: Minimum gen0 " SIZE_FORMAT "  Initial gen0 "
   604           SIZE_FORMAT "  Maximum gen0 " SIZE_FORMAT,
   601           SIZE_FORMAT "  Maximum gen0 " SIZE_FORMAT,
   605           _min_gen0_size, _initial_gen0_size, _max_gen0_size);
   602           _min_gen0_size, _initial_gen0_size, _max_gen0_size);
   606       }
   603       }
   607     }
   604     }
   608   }
   605   }
   609   // Enforce the maximum gen1 size.
   606 
   610   _min_gen1_size = MIN2(_min_gen1_size, _max_gen1_size);
   607   _min_gen1_size = MIN2(_min_gen1_size, _max_gen1_size);
   611 
   608 
   612   // Check that min gen1 <= initial gen1 <= max gen1
   609   // Make sure that min gen1 <= initial gen1 <= max gen1.
   613   _initial_gen1_size = MAX2(_initial_gen1_size, _min_gen1_size);
   610   _initial_gen1_size = MAX2(_initial_gen1_size, _min_gen1_size);
   614   _initial_gen1_size = MIN2(_initial_gen1_size, _max_gen1_size);
   611   _initial_gen1_size = MIN2(_initial_gen1_size, _max_gen1_size);
   615 
   612 
   616   // Write back to flags if necessary
   613   // Write back to flags if necessary
   617   if (NewSize != _initial_gen0_size) {
   614   if (NewSize != _initial_gen0_size) {
   648   // limit is being exceeded as checked below.
   645   // limit is being exceeded as checked below.
   649   *gc_overhead_limit_was_exceeded = false;
   646   *gc_overhead_limit_was_exceeded = false;
   650 
   647 
   651   HeapWord* result = NULL;
   648   HeapWord* result = NULL;
   652 
   649 
   653   // Loop until the allocation is satisified,
   650   // Loop until the allocation is satisfied, or unsatisfied after GC.
   654   // or unsatisfied after GC.
       
   655   for (int try_count = 1, gclocker_stalled_count = 0; /* return or throw */; try_count += 1) {
   651   for (int try_count = 1, gclocker_stalled_count = 0; /* return or throw */; try_count += 1) {
   656     HandleMark hm; // discard any handles allocated in each iteration
   652     HandleMark hm; // Discard any handles allocated in each iteration.
   657 
   653 
   658     // First allocation attempt is lock-free.
   654     // First allocation attempt is lock-free.
   659     Generation *gen0 = gch->get_gen(0);
   655     Generation *gen0 = gch->get_gen(0);
   660     assert(gen0->supports_inline_contig_alloc(),
   656     assert(gen0->supports_inline_contig_alloc(),
   661       "Otherwise, must do alloc within heap lock");
   657       "Otherwise, must do alloc within heap lock");
   664       if (result != NULL) {
   660       if (result != NULL) {
   665         assert(gch->is_in_reserved(result), "result not in heap");
   661         assert(gch->is_in_reserved(result), "result not in heap");
   666         return result;
   662         return result;
   667       }
   663       }
   668     }
   664     }
   669     unsigned int gc_count_before;  // read inside the Heap_lock locked region
   665     unsigned int gc_count_before;  // Read inside the Heap_lock locked region.
   670     {
   666     {
   671       MutexLocker ml(Heap_lock);
   667       MutexLocker ml(Heap_lock);
   672       if (PrintGC && Verbose) {
   668       if (PrintGC && Verbose) {
   673         gclog_or_tty->print_cr("TwoGenerationCollectorPolicy::mem_allocate_work:"
   669         gclog_or_tty->print_cr("TwoGenerationCollectorPolicy::mem_allocate_work:"
   674                       " attempting locked slow path allocation");
   670                       " attempting locked slow path allocation");
   683         return result;
   679         return result;
   684       }
   680       }
   685 
   681 
   686       if (GC_locker::is_active_and_needs_gc()) {
   682       if (GC_locker::is_active_and_needs_gc()) {
   687         if (is_tlab) {
   683         if (is_tlab) {
   688           return NULL;  // Caller will retry allocating individual object
   684           return NULL;  // Caller will retry allocating individual object.
   689         }
   685         }
   690         if (!gch->is_maximal_no_gc()) {
   686         if (!gch->is_maximal_no_gc()) {
   691           // Try and expand heap to satisfy request
   687           // Try and expand heap to satisfy request.
   692           result = expand_heap_and_allocate(size, is_tlab);
   688           result = expand_heap_and_allocate(size, is_tlab);
   693           // result could be null if we are out of space
   689           // Result could be null if we are out of space.
   694           if (result != NULL) {
   690           if (result != NULL) {
   695             return result;
   691             return result;
   696           }
   692           }
   697         }
   693         }
   698 
   694 
   699         if (gclocker_stalled_count > GCLockerRetryAllocationCount) {
   695         if (gclocker_stalled_count > GCLockerRetryAllocationCount) {
   700           return NULL; // we didn't get to do a GC and we didn't get any memory
   696           return NULL; // We didn't get to do a GC and we didn't get any memory.
   701         }
   697         }
   702 
   698 
   703         // If this thread is not in a jni critical section, we stall
   699         // If this thread is not in a jni critical section, we stall
   704         // the requestor until the critical section has cleared and
   700         // the requestor until the critical section has cleared and
   705         // GC allowed. When the critical section clears, a GC is
   701         // GC allowed. When the critical section clears, a GC is
   730     VMThread::execute(&op);
   726     VMThread::execute(&op);
   731     if (op.prologue_succeeded()) {
   727     if (op.prologue_succeeded()) {
   732       result = op.result();
   728       result = op.result();
   733       if (op.gc_locked()) {
   729       if (op.gc_locked()) {
   734          assert(result == NULL, "must be NULL if gc_locked() is true");
   730          assert(result == NULL, "must be NULL if gc_locked() is true");
   735          continue;  // retry and/or stall as necessary
   731          continue;  // Retry and/or stall as necessary.
   736       }
   732       }
   737 
   733 
   738       // Allocation has failed and a collection
   734       // Allocation has failed and a collection
   739       // has been done.  If the gc time limit was exceeded the
   735       // has been done.  If the gc time limit was exceeded the
   740       // this time, return NULL so that an out-of-memory
   736       // this time, return NULL so that an out-of-memory
   791     // GC locker is active; instead of a collection we will attempt
   787     // GC locker is active; instead of a collection we will attempt
   792     // to expand the heap, if there's room for expansion.
   788     // to expand the heap, if there's room for expansion.
   793     if (!gch->is_maximal_no_gc()) {
   789     if (!gch->is_maximal_no_gc()) {
   794       result = expand_heap_and_allocate(size, is_tlab);
   790       result = expand_heap_and_allocate(size, is_tlab);
   795     }
   791     }
   796     return result;   // could be null if we are out of space
   792     return result;   // Could be null if we are out of space.
   797   } else if (!gch->incremental_collection_will_fail(false /* don't consult_young */)) {
   793   } else if (!gch->incremental_collection_will_fail(false /* don't consult_young */)) {
   798     // Do an incremental collection.
   794     // Do an incremental collection.
   799     gch->do_collection(false            /* full */,
   795     gch->do_collection(false            /* full */,
   800                        false            /* clear_all_soft_refs */,
   796                        false            /* clear_all_soft_refs */,
   801                        size             /* size */,
   797                        size             /* size */,
   913                                        gc_count,
   909                                        gc_count,
   914                                        full_gc_count,
   910                                        full_gc_count,
   915                                        GCCause::_metadata_GC_threshold);
   911                                        GCCause::_metadata_GC_threshold);
   916     VMThread::execute(&op);
   912     VMThread::execute(&op);
   917 
   913 
   918     // If GC was locked out, try again.  Check
   914     // If GC was locked out, try again. Check before checking success because the
   919     // before checking success because the prologue
   915     // prologue could have succeeded and the GC still have been locked out.
   920     // could have succeeded and the GC still have
       
   921     // been locked out.
       
   922     if (op.gc_locked()) {
   916     if (op.gc_locked()) {
   923       continue;
   917       continue;
   924     }
   918     }
   925 
   919 
   926     if (op.prologue_succeeded()) {
   920     if (op.prologue_succeeded()) {
   977     vm_exit_during_initialization("Unable to allocate gen spec");
   971     vm_exit_during_initialization("Unable to allocate gen spec");
   978   }
   972   }
   979 }
   973 }
   980 
   974 
   981 void MarkSweepPolicy::initialize_gc_policy_counters() {
   975 void MarkSweepPolicy::initialize_gc_policy_counters() {
   982   // initialize the policy counters - 2 collectors, 3 generations
   976   // Initialize the policy counters - 2 collectors, 3 generations.
   983   if (UseParNewGC) {
   977   if (UseParNewGC) {
   984     _gc_policy_counters = new GCPolicyCounters("ParNew:MSC", 2, 3);
   978     _gc_policy_counters = new GCPolicyCounters("ParNew:MSC", 2, 3);
   985   } else {
   979   } else {
   986     _gc_policy_counters = new GCPolicyCounters("Copy:MSC", 2, 3);
   980     _gc_policy_counters = new GCPolicyCounters("Copy:MSC", 2, 3);
   987   }
   981   }