56 57 // Perform GC if timer has expired. 58 const double time_since_last_gc = ZStatCycle::time_since_last(); 59 const double time_until_gc = ZCollectionInterval - time_since_last_gc; 60 61 log_debug(gc, director)("Rule: Timer, Interval: %us, TimeUntilGC: %.3fs", 62 ZCollectionInterval, time_until_gc); 63 64 return time_until_gc <= 0; 65 } 66 67 bool ZDirector::rule_warmup() const { 68 if (ZStatCycle::is_warm()) { 69 // Rule disabled 70 return false; 71 } 72 73 // Perform GC if heap usage passes 10/20/30% and no other GC has been 74 // performed yet. This allows us to get some early samples of the GC 75 // duration, which is needed by the other rules. 76 const size_t max_capacity = ZHeap::heap()->soft_max_capacity(); 77 const size_t used = ZHeap::heap()->used(); 78 const double used_threshold_percent = (ZStatCycle::nwarmup_cycles() + 1) * 0.1; 79 const size_t used_threshold = max_capacity * used_threshold_percent; 80 81 log_debug(gc, director)("Rule: Warmup %.0f%%, Used: " SIZE_FORMAT "MB, UsedThreshold: " SIZE_FORMAT "MB", 82 used_threshold_percent * 100, used / M, used_threshold / M); 83 84 return used >= used_threshold; 85 } 86 87 bool ZDirector::rule_allocation_rate() const { 88 if (!ZStatCycle::is_normalized_duration_trustable()) { 89 // Rule disabled 90 return false; 91 } 92 93 // Perform GC if the estimated max allocation rate indicates that we 94 // will run out of memory. The estimated max allocation rate is based 95 // on the moving average of the sampled allocation rate plus a safety 96 // margin based on variations in the allocation rate and unforeseen 97 // allocation spikes. 98 99 // Calculate amount of free memory available to Java threads. Note that 100 // the heap reserve is not available to Java threads and is therefore not 101 // considered part of the free memory. 102 const size_t max_capacity = ZHeap::heap()->soft_max_capacity(); 103 const size_t max_reserve = ZHeap::heap()->max_reserve(); 104 const size_t used = ZHeap::heap()->used(); 105 const size_t free_with_reserve = max_capacity - MIN2(max_capacity, used); 106 const size_t free = free_with_reserve - MIN2(free_with_reserve, max_reserve); 107 108 // Calculate time until OOM given the max allocation rate and the amount 109 // of free memory. The allocation rate is a moving average and we multiply 110 // that with an allocation spike tolerance factor to guard against unforeseen 111 // phase changes in the allocate rate. We then add ~3.3 sigma to account for 112 // the allocation rate variance, which means the probability is 1 in 1000 113 // that a sample is outside of the confidence interval. 114 const double max_alloc_rate = (ZStatAllocRate::avg() * ZAllocationSpikeTolerance) + (ZStatAllocRate::avg_sd() * one_in_1000); 115 const double time_until_oom = free / (max_alloc_rate + 1.0); // Plus 1.0B/s to avoid division by zero 116 117 // Calculate max duration of a GC cycle. The duration of GC is a moving 118 // average, we add ~3.3 sigma to account for the GC duration variance. 119 const AbsSeq& duration_of_gc = ZStatCycle::normalized_duration(); 120 const double max_duration_of_gc = duration_of_gc.davg() + (duration_of_gc.dsd() * one_in_1000); 121 122 // Calculate time until GC given the time until OOM and max duration of GC. 123 // We also deduct the sample interval, so that we don't overshoot the target 124 // time and end up starting the GC too late in the next interval. 125 const double sample_interval = 1.0 / ZStatAllocRate::sample_hz; 166 const double max_duration_of_gc = duration_of_gc.davg() + (duration_of_gc.dsd() * one_in_1000); 167 const double acceptable_gc_interval = max_duration_of_gc * ((assumed_throughput_drop_during_gc / acceptable_throughput_drop) - 1.0); 168 const double time_until_gc = acceptable_gc_interval - time_since_last_gc; 169 170 log_debug(gc, director)("Rule: Proactive, AcceptableGCInterval: %.3fs, TimeSinceLastGC: %.3fs, TimeUntilGC: %.3fs", 171 acceptable_gc_interval, time_since_last_gc, time_until_gc); 172 173 return time_until_gc <= 0; 174 } 175 176 bool ZDirector::rule_high_usage() const { 177 // Perform GC if the amount of free memory is 5% or less. This is a preventive 178 // meassure in the case where the application has a very low allocation rate, 179 // such that the allocation rate rule doesn't trigger, but the amount of free 180 // memory is still slowly but surely heading towards zero. In this situation, 181 // we start a GC cycle to avoid a potential allocation stall later. 182 183 // Calculate amount of free memory available to Java threads. Note that 184 // the heap reserve is not available to Java threads and is therefore not 185 // considered part of the free memory. 186 const size_t max_capacity = ZHeap::heap()->soft_max_capacity(); 187 const size_t max_reserve = ZHeap::heap()->max_reserve(); 188 const size_t used = ZHeap::heap()->used(); 189 const size_t free_with_reserve = max_capacity - used; 190 const size_t free = free_with_reserve - MIN2(free_with_reserve, max_reserve); 191 const double free_percent = percent_of(free, max_capacity); 192 193 log_debug(gc, director)("Rule: High Usage, Free: " SIZE_FORMAT "MB(%.1f%%)", 194 free / M, free_percent); 195 196 return free_percent <= 5.0; 197 } 198 199 GCCause::Cause ZDirector::make_gc_decision() const { 200 // Rule 0: Timer 201 if (rule_timer()) { 202 return GCCause::_z_timer; 203 } 204 205 // Rule 1: Warmup 206 if (rule_warmup()) { 207 return GCCause::_z_warmup; 208 } 209 210 // Rule 2: Allocation rate 211 if (rule_allocation_rate()) { | 56 57 // Perform GC if timer has expired. 58 const double time_since_last_gc = ZStatCycle::time_since_last(); 59 const double time_until_gc = ZCollectionInterval - time_since_last_gc; 60 61 log_debug(gc, director)("Rule: Timer, Interval: %us, TimeUntilGC: %.3fs", 62 ZCollectionInterval, time_until_gc); 63 64 return time_until_gc <= 0; 65 } 66 67 bool ZDirector::rule_warmup() const { 68 if (ZStatCycle::is_warm()) { 69 // Rule disabled 70 return false; 71 } 72 73 // Perform GC if heap usage passes 10/20/30% and no other GC has been 74 // performed yet. This allows us to get some early samples of the GC 75 // duration, which is needed by the other rules. 76 const size_t soft_max_capacity = ZHeap::heap()->soft_max_capacity(); 77 const size_t used = ZHeap::heap()->used(); 78 const double used_threshold_percent = (ZStatCycle::nwarmup_cycles() + 1) * 0.1; 79 const size_t used_threshold = soft_max_capacity * used_threshold_percent; 80 81 log_debug(gc, director)("Rule: Warmup %.0f%%, Used: " SIZE_FORMAT "MB, UsedThreshold: " SIZE_FORMAT "MB", 82 used_threshold_percent * 100, used / M, used_threshold / M); 83 84 return used >= used_threshold; 85 } 86 87 bool ZDirector::rule_allocation_rate() const { 88 if (!ZStatCycle::is_normalized_duration_trustable()) { 89 // Rule disabled 90 return false; 91 } 92 93 // Perform GC if the estimated max allocation rate indicates that we 94 // will run out of memory. The estimated max allocation rate is based 95 // on the moving average of the sampled allocation rate plus a safety 96 // margin based on variations in the allocation rate and unforeseen 97 // allocation spikes. 98 99 // Calculate amount of free memory available to Java threads. Note that 100 // the heap reserve is not available to Java threads and is therefore not 101 // considered part of the free memory. 102 const size_t soft_max_capacity = ZHeap::heap()->soft_max_capacity(); 103 const size_t max_reserve = ZHeap::heap()->max_reserve(); 104 const size_t used = ZHeap::heap()->used(); 105 const size_t free_with_reserve = soft_max_capacity - MIN2(soft_max_capacity, used); 106 const size_t free = free_with_reserve - MIN2(free_with_reserve, max_reserve); 107 108 // Calculate time until OOM given the max allocation rate and the amount 109 // of free memory. The allocation rate is a moving average and we multiply 110 // that with an allocation spike tolerance factor to guard against unforeseen 111 // phase changes in the allocate rate. We then add ~3.3 sigma to account for 112 // the allocation rate variance, which means the probability is 1 in 1000 113 // that a sample is outside of the confidence interval. 114 const double max_alloc_rate = (ZStatAllocRate::avg() * ZAllocationSpikeTolerance) + (ZStatAllocRate::avg_sd() * one_in_1000); 115 const double time_until_oom = free / (max_alloc_rate + 1.0); // Plus 1.0B/s to avoid division by zero 116 117 // Calculate max duration of a GC cycle. The duration of GC is a moving 118 // average, we add ~3.3 sigma to account for the GC duration variance. 119 const AbsSeq& duration_of_gc = ZStatCycle::normalized_duration(); 120 const double max_duration_of_gc = duration_of_gc.davg() + (duration_of_gc.dsd() * one_in_1000); 121 122 // Calculate time until GC given the time until OOM and max duration of GC. 123 // We also deduct the sample interval, so that we don't overshoot the target 124 // time and end up starting the GC too late in the next interval. 125 const double sample_interval = 1.0 / ZStatAllocRate::sample_hz; 166 const double max_duration_of_gc = duration_of_gc.davg() + (duration_of_gc.dsd() * one_in_1000); 167 const double acceptable_gc_interval = max_duration_of_gc * ((assumed_throughput_drop_during_gc / acceptable_throughput_drop) - 1.0); 168 const double time_until_gc = acceptable_gc_interval - time_since_last_gc; 169 170 log_debug(gc, director)("Rule: Proactive, AcceptableGCInterval: %.3fs, TimeSinceLastGC: %.3fs, TimeUntilGC: %.3fs", 171 acceptable_gc_interval, time_since_last_gc, time_until_gc); 172 173 return time_until_gc <= 0; 174 } 175 176 bool ZDirector::rule_high_usage() const { 177 // Perform GC if the amount of free memory is 5% or less. This is a preventive 178 // meassure in the case where the application has a very low allocation rate, 179 // such that the allocation rate rule doesn't trigger, but the amount of free 180 // memory is still slowly but surely heading towards zero. In this situation, 181 // we start a GC cycle to avoid a potential allocation stall later. 182 183 // Calculate amount of free memory available to Java threads. Note that 184 // the heap reserve is not available to Java threads and is therefore not 185 // considered part of the free memory. 186 const size_t soft_max_capacity = ZHeap::heap()->soft_max_capacity(); 187 const size_t max_reserve = ZHeap::heap()->max_reserve(); 188 const size_t used = ZHeap::heap()->used(); 189 const size_t free_with_reserve = soft_max_capacity - used; 190 const size_t free = free_with_reserve - MIN2(free_with_reserve, max_reserve); 191 const double free_percent = percent_of(free, soft_max_capacity); 192 193 log_debug(gc, director)("Rule: High Usage, Free: " SIZE_FORMAT "MB(%.1f%%)", 194 free / M, free_percent); 195 196 return free_percent <= 5.0; 197 } 198 199 GCCause::Cause ZDirector::make_gc_decision() const { 200 // Rule 0: Timer 201 if (rule_timer()) { 202 return GCCause::_z_timer; 203 } 204 205 // Rule 1: Warmup 206 if (rule_warmup()) { 207 return GCCause::_z_warmup; 208 } 209 210 // Rule 2: Allocation rate 211 if (rule_allocation_rate()) { |