1 /* 2 * Copyright (c) 2015, 2018, 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 24 #include "precompiled.hpp" 25 #include "gc/z/zCollectedHeap.hpp" 26 #include "gc/z/zDirector.hpp" 27 #include "gc/z/zHeap.inline.hpp" 28 #include "gc/z/zStat.hpp" 29 #include "gc/z/zUtils.hpp" 30 #include "logging/log.hpp" 31 32 const double ZDirector::one_in_1000 = 3.290527; 33 34 ZDirector::ZDirector() : 35 _metronome(ZStatAllocRate::sample_hz) { 36 set_name("ZDirector"); 37 create_and_start(); 38 } 39 40 void ZDirector::sample_allocation_rate() const { 41 // Sample allocation rate. This is needed by rule_allocation_rate() 42 // below to estimate the time we have until we run out of memory. 43 const double bytes_per_second = ZStatAllocRate::sample_and_reset(); 44 45 log_debug(gc, alloc)("Allocation Rate: %.3fMB/s, Avg: %.3f(+/-%.3f)MB/s", 46 bytes_per_second / M, 47 ZStatAllocRate::avg() / M, 48 ZStatAllocRate::avg_sd() / M); 49 } 50 51 bool ZDirector::is_first() const { 52 return ZStatCycle::ncycles() == 0; 53 } 54 55 bool ZDirector::is_warm() const { 56 return ZStatCycle::ncycles() >= 3; 57 } 58 59 bool ZDirector::rule_timer() const { 60 if (ZCollectionInterval == 0) { 61 // Rule disabled 62 return false; 63 } 64 65 // Perform GC if timer has expired. 66 const double time_since_last_gc = ZStatCycle::time_since_last(); 67 const double time_until_gc = ZCollectionInterval - time_since_last_gc; 68 69 log_debug(gc, director)("Rule: Timer, Interval: %us, TimeUntilGC: %.3lfs", 70 ZCollectionInterval, time_until_gc); 71 72 return time_until_gc <= 0; 73 } 74 75 bool ZDirector::rule_warmup() const { 76 if (is_warm()) { 77 // Rule disabled 78 return false; 79 } 80 81 // Perform GC if heap usage passes 10/20/30% and no other GC has been 82 // performed yet. This allows us to get some early samples of the GC 83 // duration, which is needed by the other rules. 84 const size_t max_capacity = ZHeap::heap()->max_capacity(); 85 const size_t used = ZHeap::heap()->used(); 86 const double used_threshold_percent = (ZStatCycle::ncycles() + 1) * 0.1; 87 const size_t used_threshold = max_capacity * used_threshold_percent; 88 89 log_debug(gc, director)("Rule: Warmup %.0f%%, Used: " SIZE_FORMAT "MB, UsedThreshold: " SIZE_FORMAT "MB", 90 used_threshold_percent * 100, used / M, used_threshold / M); 91 92 return used >= used_threshold; 93 } 94 95 bool ZDirector::rule_allocation_rate() const { 96 if (is_first()) { 97 // Rule disabled 98 return false; 99 } 100 101 // Perform GC if the estimated max allocation rate indicates that we 102 // will run out of memory. The estimated max allocation rate is based 103 // on the moving average of the sampled allocation rate plus a safety 104 // margin based on variations in the allocation rate and unforseen 105 // allocation spikes. 106 107 // Calculate amount of free memory available to Java threads. Note that 108 // the heap reserve is not available to Java threads and is therefore not 109 // considered part of the free memory. 110 const size_t max_capacity = ZHeap::heap()->max_capacity(); 111 const size_t max_reserve = ZHeap::heap()->max_reserve(); 112 const size_t used = ZHeap::heap()->used(); 113 const size_t free_with_reserve = max_capacity - used; 114 const size_t free = free_with_reserve - MIN2(free_with_reserve, max_reserve); 115 116 // Calculate time until OOM given the max allocation rate and the amount 117 // of free memory. The allocation rate is a moving average and we multiply 118 // that with an alllcation spike tolerance factor to guard against unforseen 119 // phase changes in the allocate rate. We then add ~3.3 sigma to account for 120 // the allocation rate variance, which means the probablility is 1 in 1000 121 // that a sample is outside of the confidence interval. 122 const double max_alloc_rate = (ZStatAllocRate::avg() * ZAllocationSpikeTolerance) + (ZStatAllocRate::avg_sd() * one_in_1000); 123 const double time_until_oom = free / (max_alloc_rate + 1.0); // Plus 1.0B/s to avoid division by zero 124 125 // Calculate max duration of a GC cycle. The duration of GC is a moving 126 // average, we add ~3.3 sigma to account for the GC duration variance. 127 const AbsSeq& duration_of_gc = ZStatCycle::normalized_duration(); 128 const double max_duration_of_gc = duration_of_gc.davg() + (duration_of_gc.dsd() * one_in_1000); 129 130 // Calculate time until GC given the time until OOM and max duration of GC. 131 // We also deduct the sample interval, so that we don't overshoot the target 132 // time and end up starting the GC too late in the next interval. 133 const double sample_interval = 1.0 / ZStatAllocRate::sample_hz; 134 const double time_until_gc = time_until_oom - max_duration_of_gc - sample_interval; 135 136 log_debug(gc, director)("Rule: Allocation Rate, MaxAllocRate: %.3lfMB/s, Free: " SIZE_FORMAT "MB, MaxDurationOfGC: %.3lfs, TimeUntilGC: %.3lfs", 137 max_alloc_rate / M, free / M, max_duration_of_gc, time_until_gc); 138 139 return time_until_gc <= 0; 140 } 141 142 bool ZDirector::rule_proactive() const { 143 if (!ZProactive || !is_warm()) { 144 // Rule disabled 145 return false; 146 } 147 148 // Perform GC if the impact of doing so, in terms of application throughput 149 // reduction, is considered acceptable. This rule allows us to keep the heap 150 // size down and allow reference processing to happen even when we have a lot 151 // of free space on the heap. 152 153 // Only consider doing a proactive GC if the heap usage has grown by at least 154 // 10% of the max capacity since the previous GC, or more than 5 minutes has 155 // passed since the previous GC. This helps avoid superfluous GCs when running 156 // applications with very low allocation rate. 157 const size_t used_after_last_gc = ZStatHeap::used_at_relocate_end(); 158 const size_t used_increase_threshold = ZHeap::heap()->max_capacity() * 0.10; // 10% 159 const size_t used_threshold = used_after_last_gc + used_increase_threshold; 160 const size_t used = ZHeap::heap()->used(); 161 const double time_since_last_gc = ZStatCycle::time_since_last(); 162 const double time_since_last_gc_threshold = 5 * 60; // 5 minutes 163 if (used < used_threshold && time_since_last_gc < time_since_last_gc_threshold) { 164 // Don't even consider doing a proactive GC 165 log_debug(gc, director)("Rule: Proactive, UsedUntilEnabled: " SIZE_FORMAT "MB, TimeUntilEnabled: %.3lfs", 166 (used_threshold - used) / M, 167 time_since_last_gc_threshold - time_since_last_gc); 168 return false; 169 } 170 171 const double assumed_throughput_drop_during_gc = 0.50; // 50% 172 const double acceptable_throughput_drop = 0.01; // 1% 173 const AbsSeq& duration_of_gc = ZStatCycle::normalized_duration(); 174 const double max_duration_of_gc = duration_of_gc.davg() + (duration_of_gc.dsd() * one_in_1000); 175 const double acceptable_gc_interval = max_duration_of_gc * ((assumed_throughput_drop_during_gc / acceptable_throughput_drop) - 1.0); 176 const double time_until_gc = acceptable_gc_interval - time_since_last_gc; 177 178 log_debug(gc, director)("Rule: Proactive, AcceptableGCInterval: %.3lfs, TimeSinceLastGC: %.3lfs, TimeUntilGC: %.3lfs", 179 acceptable_gc_interval, time_since_last_gc, time_until_gc); 180 181 return time_until_gc <= 0; 182 } 183 184 GCCause::Cause ZDirector::make_gc_decision() const { 185 // Rule 0: Timer 186 if (rule_timer()) { 187 return GCCause::_z_timer; 188 } 189 190 // Rule 1: Warmup 191 if (rule_warmup()) { 192 return GCCause::_z_warmup; 193 } 194 195 // Rule 2: Allocation rate 196 if (rule_allocation_rate()) { 197 return GCCause::_z_allocation_rate; 198 } 199 200 // Rule 3: Proactive 201 if (rule_proactive()) { 202 return GCCause::_z_proactive; 203 } 204 205 // No GC 206 return GCCause::_no_gc; 207 } 208 209 void ZDirector::run_service() { 210 // Main loop 211 while (_metronome.wait_for_tick()) { 212 sample_allocation_rate(); 213 const GCCause::Cause cause = make_gc_decision(); 214 if (cause != GCCause::_no_gc) { 215 ZCollectedHeap::heap()->collect(cause); 216 } 217 } 218 } 219 220 void ZDirector::stop_service() { 221 _metronome.stop(); 222 }