Path: blob/master/src/hotspot/share/gc/z/zDirector.cpp
66644 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "gc/shared/gc_globals.hpp"25#include "gc/z/zDirector.hpp"26#include "gc/z/zDriver.hpp"27#include "gc/z/zHeap.inline.hpp"28#include "gc/z/zHeuristics.hpp"29#include "gc/z/zStat.hpp"30#include "logging/log.hpp"3132constexpr double one_in_1000 = 3.290527;33constexpr double sample_interval = 1.0 / ZStatAllocRate::sample_hz;3435ZDirector::ZDirector(ZDriver* driver) :36_driver(driver),37_metronome(ZStatAllocRate::sample_hz) {38set_name("ZDirector");39create_and_start();40}4142static void sample_allocation_rate() {43// Sample allocation rate. This is needed by rule_allocation_rate()44// below to estimate the time we have until we run out of memory.45const double bytes_per_second = ZStatAllocRate::sample_and_reset();4647log_debug(gc, alloc)("Allocation Rate: %.1fMB/s, Predicted: %.1fMB/s, Avg: %.1f(+/-%.1f)MB/s",48bytes_per_second / M,49ZStatAllocRate::predict() / M,50ZStatAllocRate::avg() / M,51ZStatAllocRate::sd() / M);52}5354static ZDriverRequest rule_allocation_stall() {55// Perform GC if we've observed at least one allocation stall since56// the last GC started.57if (!ZHeap::heap()->has_alloc_stalled()) {58return GCCause::_no_gc;59}6061log_debug(gc, director)("Rule: Allocation Stall Observed");6263return GCCause::_z_allocation_stall;64}6566static ZDriverRequest rule_warmup() {67if (ZStatCycle::is_warm()) {68// Rule disabled69return GCCause::_no_gc;70}7172// Perform GC if heap usage passes 10/20/30% and no other GC has been73// performed yet. This allows us to get some early samples of the GC74// duration, which is needed by the other rules.75const size_t soft_max_capacity = ZHeap::heap()->soft_max_capacity();76const size_t used = ZHeap::heap()->used();77const double used_threshold_percent = (ZStatCycle::nwarmup_cycles() + 1) * 0.1;78const size_t used_threshold = soft_max_capacity * used_threshold_percent;7980log_debug(gc, director)("Rule: Warmup %.0f%%, Used: " SIZE_FORMAT "MB, UsedThreshold: " SIZE_FORMAT "MB",81used_threshold_percent * 100, used / M, used_threshold / M);8283if (used < used_threshold) {84return GCCause::_no_gc;85}8687return GCCause::_z_warmup;88}8990static ZDriverRequest rule_timer() {91if (ZCollectionInterval <= 0) {92// Rule disabled93return GCCause::_no_gc;94}9596// Perform GC if timer has expired.97const double time_since_last_gc = ZStatCycle::time_since_last();98const double time_until_gc = ZCollectionInterval - time_since_last_gc;99100log_debug(gc, director)("Rule: Timer, Interval: %.3fs, TimeUntilGC: %.3fs",101ZCollectionInterval, time_until_gc);102103if (time_until_gc > 0) {104return GCCause::_no_gc;105}106107return GCCause::_z_timer;108}109110static double estimated_gc_workers(double serial_gc_time, double parallelizable_gc_time, double time_until_deadline) {111const double parallelizable_time_until_deadline = MAX2(time_until_deadline - serial_gc_time, 0.001);112return parallelizable_gc_time / parallelizable_time_until_deadline;113}114115static uint discrete_gc_workers(double gc_workers) {116return clamp<uint>(ceil(gc_workers), 1, ConcGCThreads);117}118119static double select_gc_workers(double serial_gc_time, double parallelizable_gc_time, double alloc_rate_sd_percent, double time_until_oom) {120// Use all workers until we're warm121if (!ZStatCycle::is_warm()) {122const double not_warm_gc_workers = ConcGCThreads;123log_debug(gc, director)("Select GC Workers (Not Warm), GCWorkers: %.3f", not_warm_gc_workers);124return not_warm_gc_workers;125}126127// Calculate number of GC workers needed to avoid a long GC cycle and to avoid OOM.128const double avoid_long_gc_workers = estimated_gc_workers(serial_gc_time, parallelizable_gc_time, 10 /* seconds */);129const double avoid_oom_gc_workers = estimated_gc_workers(serial_gc_time, parallelizable_gc_time, time_until_oom);130131const double gc_workers = MAX2(avoid_long_gc_workers, avoid_oom_gc_workers);132const uint actual_gc_workers = discrete_gc_workers(gc_workers);133const uint last_gc_workers = ZStatCycle::last_active_workers();134135// More than 15% division from the average is considered unsteady136if (alloc_rate_sd_percent >= 0.15) {137const double half_gc_workers = ConcGCThreads / 2.0;138const double unsteady_gc_workers = MAX3<double>(gc_workers, last_gc_workers, half_gc_workers);139log_debug(gc, director)("Select GC Workers (Unsteady), "140"AvoidLongGCWorkers: %.3f, AvoidOOMGCWorkers: %.3f, LastGCWorkers: %.3f, HalfGCWorkers: %.3f, GCWorkers: %.3f",141avoid_long_gc_workers, avoid_oom_gc_workers, (double)last_gc_workers, half_gc_workers, unsteady_gc_workers);142return unsteady_gc_workers;143}144145if (actual_gc_workers < last_gc_workers) {146// Before decreasing number of GC workers compared to the previous GC cycle, check if the147// next GC cycle will need to increase it again. If so, use the same number of GC workers148// that will be needed in the next cycle.149const double gc_duration_delta = (parallelizable_gc_time / actual_gc_workers) - (parallelizable_gc_time / last_gc_workers);150const double additional_time_for_allocations = ZStatCycle::time_since_last() - gc_duration_delta - sample_interval;151const double next_time_until_oom = time_until_oom + additional_time_for_allocations;152const double next_avoid_oom_gc_workers = estimated_gc_workers(serial_gc_time, parallelizable_gc_time, next_time_until_oom);153154// Add 0.5 to increase friction and avoid lowering too eagerly155const double next_gc_workers = next_avoid_oom_gc_workers + 0.5;156const double try_lowering_gc_workers = clamp<double>(next_gc_workers, actual_gc_workers, last_gc_workers);157158log_debug(gc, director)("Select GC Workers (Try Lowering), "159"AvoidLongGCWorkers: %.3f, AvoidOOMGCWorkers: %.3f, NextAvoidOOMGCWorkers: %.3f, LastGCWorkers: %.3f, GCWorkers: %.3f",160avoid_long_gc_workers, avoid_oom_gc_workers, next_avoid_oom_gc_workers, (double)last_gc_workers, try_lowering_gc_workers);161return try_lowering_gc_workers;162}163164log_debug(gc, director)("Select GC Workers (Normal), "165"AvoidLongGCWorkers: %.3f, AvoidOOMGCWorkers: %.3f, LastGCWorkers: %.3f, GCWorkers: %.3f",166avoid_long_gc_workers, avoid_oom_gc_workers, (double)last_gc_workers, gc_workers);167return gc_workers;168}169170ZDriverRequest rule_allocation_rate_dynamic() {171if (!ZStatCycle::is_time_trustable()) {172// Rule disabled173return GCCause::_no_gc;174}175176// Calculate amount of free memory available. Note that we take the177// relocation headroom into account to avoid in-place relocation.178const size_t soft_max_capacity = ZHeap::heap()->soft_max_capacity();179const size_t used = ZHeap::heap()->used();180const size_t free_including_headroom = soft_max_capacity - MIN2(soft_max_capacity, used);181const size_t free = free_including_headroom - MIN2(free_including_headroom, ZHeuristics::relocation_headroom());182183// Calculate time until OOM given the max allocation rate and the amount184// of free memory. The allocation rate is a moving average and we multiply185// that with an allocation spike tolerance factor to guard against unforeseen186// phase changes in the allocate rate. We then add ~3.3 sigma to account for187// the allocation rate variance, which means the probability is 1 in 1000188// that a sample is outside of the confidence interval.189const double alloc_rate_predict = ZStatAllocRate::predict();190const double alloc_rate_avg = ZStatAllocRate::avg();191const double alloc_rate_sd = ZStatAllocRate::sd();192const double alloc_rate_sd_percent = alloc_rate_sd / (alloc_rate_avg + 1.0);193const double alloc_rate = (MAX2(alloc_rate_predict, alloc_rate_avg) * ZAllocationSpikeTolerance) + (alloc_rate_sd * one_in_1000) + 1.0;194const double time_until_oom = (free / alloc_rate) / (1.0 + alloc_rate_sd_percent);195196// Calculate max serial/parallel times of a GC cycle. The times are197// moving averages, we add ~3.3 sigma to account for the variance.198const double serial_gc_time = ZStatCycle::serial_time().davg() + (ZStatCycle::serial_time().dsd() * one_in_1000);199const double parallelizable_gc_time = ZStatCycle::parallelizable_time().davg() + (ZStatCycle::parallelizable_time().dsd() * one_in_1000);200201// Calculate number of GC workers needed to avoid OOM.202const double gc_workers = select_gc_workers(serial_gc_time, parallelizable_gc_time, alloc_rate_sd_percent, time_until_oom);203204// Convert to a discrete number of GC workers within limits.205const uint actual_gc_workers = discrete_gc_workers(gc_workers);206207// Calculate GC duration given number of GC workers needed.208const double actual_gc_duration = serial_gc_time + (parallelizable_gc_time / actual_gc_workers);209const uint last_gc_workers = ZStatCycle::last_active_workers();210211// Calculate time until GC given the time until OOM and GC duration.212// We also subtract the sample interval, so that we don't overshoot the213// target time and end up starting the GC too late in the next interval.214const double time_until_gc = time_until_oom - actual_gc_duration - sample_interval;215216log_debug(gc, director)("Rule: Allocation Rate (Dynamic GC Workers), "217"MaxAllocRate: %.1fMB/s (+/-%.1f%%), Free: " SIZE_FORMAT "MB, GCCPUTime: %.3f, "218"GCDuration: %.3fs, TimeUntilOOM: %.3fs, TimeUntilGC: %.3fs, GCWorkers: %u -> %u",219alloc_rate / M,220alloc_rate_sd_percent * 100,221free / M,222serial_gc_time + parallelizable_gc_time,223serial_gc_time + (parallelizable_gc_time / actual_gc_workers),224time_until_oom,225time_until_gc,226last_gc_workers,227actual_gc_workers);228229if (actual_gc_workers <= last_gc_workers && time_until_gc > 0) {230return ZDriverRequest(GCCause::_no_gc, actual_gc_workers);231}232233return ZDriverRequest(GCCause::_z_allocation_rate, actual_gc_workers);234}235236static ZDriverRequest rule_allocation_rate_static() {237if (!ZStatCycle::is_time_trustable()) {238// Rule disabled239return GCCause::_no_gc;240}241242// Perform GC if the estimated max allocation rate indicates that we243// will run out of memory. The estimated max allocation rate is based244// on the moving average of the sampled allocation rate plus a safety245// margin based on variations in the allocation rate and unforeseen246// allocation spikes.247248// Calculate amount of free memory available. Note that we take the249// relocation headroom into account to avoid in-place relocation.250const size_t soft_max_capacity = ZHeap::heap()->soft_max_capacity();251const size_t used = ZHeap::heap()->used();252const size_t free_including_headroom = soft_max_capacity - MIN2(soft_max_capacity, used);253const size_t free = free_including_headroom - MIN2(free_including_headroom, ZHeuristics::relocation_headroom());254255// Calculate time until OOM given the max allocation rate and the amount256// of free memory. The allocation rate is a moving average and we multiply257// that with an allocation spike tolerance factor to guard against unforeseen258// phase changes in the allocate rate. We then add ~3.3 sigma to account for259// the allocation rate variance, which means the probability is 1 in 1000260// that a sample is outside of the confidence interval.261const double max_alloc_rate = (ZStatAllocRate::avg() * ZAllocationSpikeTolerance) + (ZStatAllocRate::sd() * one_in_1000);262const double time_until_oom = free / (max_alloc_rate + 1.0); // Plus 1.0B/s to avoid division by zero263264// Calculate max serial/parallel times of a GC cycle. The times are265// moving averages, we add ~3.3 sigma to account for the variance.266const double serial_gc_time = ZStatCycle::serial_time().davg() + (ZStatCycle::serial_time().dsd() * one_in_1000);267const double parallelizable_gc_time = ZStatCycle::parallelizable_time().davg() + (ZStatCycle::parallelizable_time().dsd() * one_in_1000);268269// Calculate GC duration given number of GC workers needed.270const double gc_duration = serial_gc_time + (parallelizable_gc_time / ConcGCThreads);271272// Calculate time until GC given the time until OOM and max duration of GC.273// We also deduct the sample interval, so that we don't overshoot the target274// time and end up starting the GC too late in the next interval.275const double time_until_gc = time_until_oom - gc_duration - sample_interval;276277log_debug(gc, director)("Rule: Allocation Rate (Static GC Workers), MaxAllocRate: %.1fMB/s, Free: " SIZE_FORMAT "MB, GCDuration: %.3fs, TimeUntilGC: %.3fs",278max_alloc_rate / M, free / M, gc_duration, time_until_gc);279280if (time_until_gc > 0) {281return GCCause::_no_gc;282}283284return GCCause::_z_allocation_rate;285}286287static ZDriverRequest rule_allocation_rate() {288if (UseDynamicNumberOfGCThreads) {289return rule_allocation_rate_dynamic();290} else {291return rule_allocation_rate_static();292}293}294295static ZDriverRequest rule_high_usage() {296// Perform GC if the amount of free memory is 5% or less. This is a preventive297// meassure in the case where the application has a very low allocation rate,298// such that the allocation rate rule doesn't trigger, but the amount of free299// memory is still slowly but surely heading towards zero. In this situation,300// we start a GC cycle to avoid a potential allocation stall later.301302// Calculate amount of free memory available. Note that we take the303// relocation headroom into account to avoid in-place relocation.304const size_t soft_max_capacity = ZHeap::heap()->soft_max_capacity();305const size_t used = ZHeap::heap()->used();306const size_t free_including_headroom = soft_max_capacity - MIN2(soft_max_capacity, used);307const size_t free = free_including_headroom - MIN2(free_including_headroom, ZHeuristics::relocation_headroom());308const double free_percent = percent_of(free, soft_max_capacity);309310log_debug(gc, director)("Rule: High Usage, Free: " SIZE_FORMAT "MB(%.1f%%)",311free / M, free_percent);312313if (free_percent > 5.0) {314return GCCause::_no_gc;315}316317return GCCause::_z_high_usage;318}319320static ZDriverRequest rule_proactive() {321if (!ZProactive || !ZStatCycle::is_warm()) {322// Rule disabled323return GCCause::_no_gc;324}325326// Perform GC if the impact of doing so, in terms of application throughput327// reduction, is considered acceptable. This rule allows us to keep the heap328// size down and allow reference processing to happen even when we have a lot329// of free space on the heap.330331// Only consider doing a proactive GC if the heap usage has grown by at least332// 10% of the max capacity since the previous GC, or more than 5 minutes has333// passed since the previous GC. This helps avoid superfluous GCs when running334// applications with very low allocation rate.335const size_t used_after_last_gc = ZStatHeap::used_at_relocate_end();336const size_t used_increase_threshold = ZHeap::heap()->soft_max_capacity() * 0.10; // 10%337const size_t used_threshold = used_after_last_gc + used_increase_threshold;338const size_t used = ZHeap::heap()->used();339const double time_since_last_gc = ZStatCycle::time_since_last();340const double time_since_last_gc_threshold = 5 * 60; // 5 minutes341if (used < used_threshold && time_since_last_gc < time_since_last_gc_threshold) {342// Don't even consider doing a proactive GC343log_debug(gc, director)("Rule: Proactive, UsedUntilEnabled: " SIZE_FORMAT "MB, TimeUntilEnabled: %.3fs",344(used_threshold - used) / M,345time_since_last_gc_threshold - time_since_last_gc);346return GCCause::_no_gc;347}348349const double assumed_throughput_drop_during_gc = 0.50; // 50%350const double acceptable_throughput_drop = 0.01; // 1%351const double serial_gc_time = ZStatCycle::serial_time().davg() + (ZStatCycle::serial_time().dsd() * one_in_1000);352const double parallelizable_gc_time = ZStatCycle::parallelizable_time().davg() + (ZStatCycle::parallelizable_time().dsd() * one_in_1000);353const double gc_duration = serial_gc_time + (parallelizable_gc_time / ConcGCThreads);354const double acceptable_gc_interval = gc_duration * ((assumed_throughput_drop_during_gc / acceptable_throughput_drop) - 1.0);355const double time_until_gc = acceptable_gc_interval - time_since_last_gc;356357log_debug(gc, director)("Rule: Proactive, AcceptableGCInterval: %.3fs, TimeSinceLastGC: %.3fs, TimeUntilGC: %.3fs",358acceptable_gc_interval, time_since_last_gc, time_until_gc);359360if (time_until_gc > 0) {361return GCCause::_no_gc;362}363364return GCCause::_z_proactive;365}366367static ZDriverRequest make_gc_decision() {368// List of rules369using ZDirectorRule = ZDriverRequest (*)();370const ZDirectorRule rules[] = {371rule_allocation_stall,372rule_warmup,373rule_timer,374rule_allocation_rate,375rule_high_usage,376rule_proactive,377};378379// Execute rules380for (size_t i = 0; i < ARRAY_SIZE(rules); i++) {381const ZDriverRequest request = rules[i]();382if (request.cause() != GCCause::_no_gc) {383return request;384}385}386387return GCCause::_no_gc;388}389390void ZDirector::run_service() {391// Main loop392while (_metronome.wait_for_tick()) {393sample_allocation_rate();394if (!_driver->is_busy()) {395const ZDriverRequest request = make_gc_decision();396if (request.cause() != GCCause::_no_gc) {397_driver->collect(request);398}399}400}401}402403void ZDirector::stop_service() {404_metronome.stop();405}406407408